forked from InvictusInnovations/keyhotee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
134 lines (113 loc) · 3.49 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "utils.hpp"
#include "public_key_address.hpp"
#include <bts/profile.hpp>
#include <bts/addressbook/addressbook.hpp>
#include <bts/addressbook/contact.hpp>
#include <QStringList>
namespace Utils
{
QString toString(const fc::ecc::public_key& pk, TContactTextFormatting contactFormatting,
bts::addressbook::contact* matchingContact /*= nullptr*/)
{
assert(pk.valid());
auto address_book = bts::get_profile()->get_addressbook();
auto c = address_book->get_contact_by_public_key(pk);
if (c)
{
if(matchingContact != nullptr)
*matchingContact = *c;
switch(contactFormatting)
{
case KEYHOTEE_IDENTIFIER:
return QString(c->dac_id_string.c_str());
case CONTACT_ALIAS_FULL_NAME:
return QString(std::string(c->first_name + " " + c->last_name).c_str());
case FULL_CONTACT_DETAILS:
return QString(c->get_display_name().c_str());
default:
assert(false);
return QString();
}
}
else
{
auto profile = bts::get_profile();
/// If no contact found try one of registered identities.
std::vector<bts::addressbook::wallet_identity> identities = profile->identities();
for(const auto& identity : identities)
{
assert(identity.public_key.valid());
if(identity.public_key == pk)
{
if(matchingContact != nullptr)
*matchingContact = identity;
switch(contactFormatting)
{
case KEYHOTEE_IDENTIFIER:
return QString(identity.dac_id_string.c_str());
case CONTACT_ALIAS_FULL_NAME:
return QString(std::string(identity.first_name + " " + identity.last_name).c_str());
case FULL_CONTACT_DETAILS:
return QString(identity.get_display_name().c_str());
default:
assert(false);
return QString();
}
break;
}
}
if(matchingContact != nullptr)
{
*matchingContact = bts::addressbook::wallet_contact();
matchingContact->public_key = pk;
}
/// If code reached this point the publick key is unknown - lets display it as base58
std::string public_key_string = public_key_address(pk);
return QString(public_key_string.c_str());
}
}
bool matchContact(const fc::ecc::public_key& pk, bts::addressbook::wallet_contact* matchedContact)
{
assert(pk.valid());
auto address_book = bts::get_profile()->get_addressbook();
auto c = address_book->get_contact_by_public_key(pk);
if (c)
{
*matchedContact = *c;
return true;
}
else
{
*matchedContact = bts::addressbook::wallet_contact();
matchedContact->public_key = pk;
}
return false;
}
QString
makeContactListString(const std::vector<fc::ecc::public_key>& key_list, char separator /*= ','*/,
TContactTextFormatting contactFormatting /* = KEYHOTEE_IDENTIFIER*/)
{
QStringList to_list;
for(const auto& public_key : key_list)
to_list.append(toString(public_key, contactFormatting));
return to_list.join(separator);
}
QString lTrim(QString const& str)
{
if (str.size() == 0)
return str;
const QChar *s = (const QChar*)str.data();
if (!s->isSpace() )
return str;
int start = 0;
int end = str.size() - 1;
while (start<=end && s[start].isSpace()) // skip white space from start
start++;
int l = end - start + 1;
if (l <= 0) {
QStringDataPtr empty = { QString::Data::allocate(0) };
return QString(empty);
}
return QString(s + start, l);
}
} ///namespace Utils