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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
#include "identitycollection.hpp"
#include <cassert>
#include <QDebug>
#include <QIcon>
IdentityCollection::IdentityCollection(QObject *parent)
: QAbstractItemModel(parent)
{
}
void IdentityCollection::load(QSettings &settings)
{
this->beginResetModel();
this->root.children.clear();
int group_cnt = settings.beginReadArray("groups");
for(int i = 0; i < group_cnt; i++)
{
settings.setArrayIndex(i);
auto group = std::make_unique<GroupNode>();
group->title = settings.value("name").toString();
int id_cnt = settings.beginReadArray("identities");
for(int j = 0; j < id_cnt; j++)
{
settings.setArrayIndex(j);
auto id = std::make_unique<IdentityNode>();
id->identity.is_persistent = true;
id->identity.display_name = settings.value("display_name").toString();
id->identity.user_notes = settings.value("user_notes").toString();
id->identity.certificate = QSslCertificate::fromData(
settings.value("certificate").toByteArray(),
QSsl::Der
).first();
id->identity.private_key = QSslKey(
settings.value("private_key").toByteArray(),
QSsl::Rsa,
QSsl::Der
);
group->children.emplace_back(std::move(id));
}
settings.endArray();
this->root.children.emplace_back(std::move(group));
}
settings.endArray();
relayout();
this->endResetModel();
}
void IdentityCollection::save(QSettings &settings) const
{
settings.beginWriteArray("groups", int(root.children.size()));
int grp_index = 0;
for(auto const & grp : root.children)
{
settings.setArrayIndex(grp_index);
grp_index += 1;
auto & group = grp->as<GroupNode>();
settings.setValue("name", group.title);
settings.beginWriteArray("identities", int(group.children.size()));
int id_index = 0;
for(auto const & _id : group.children)
{
settings.setArrayIndex(id_index);
id_index += 1;
auto & id = _id->as<IdentityNode>();
settings.setValue("display_name", id.identity.display_name);
settings.setValue("user_notes", id.identity.user_notes);
settings.setValue("certificate", id.identity.certificate.toDer());
settings.setValue("private_key", id.identity.private_key.toDer());
}
settings.endArray();
}
settings.endArray();
}
bool IdentityCollection::addGroup(const QString &group_name)
{
GroupNode * group;
return internalAddGroup(group_name, group);
}
bool IdentityCollection::addCertificate(const QString &group_name, const CryptoIdentity &crypto_id)
{
// Don't allow saving transient certificates
if(not crypto_id.is_persistent)
return false;
GroupNode * group;
internalAddGroup(group_name, group);
QModelIndex parent_index = createIndex(group->index, 0, group);
beginInsertRows(parent_index, group->children.size(), group->children.size() + 1);
auto id = std::make_unique<IdentityNode>();
id->identity = crypto_id;
group->children.emplace_back(std::move(id));
this->relayout();
this->endInsertRows();
return true;
}
CryptoIdentity IdentityCollection::getIdentity(const QModelIndex &index) const
{
if (!index.isValid())
return CryptoIdentity();
if (index.column() != 0)
return CryptoIdentity();
Node const *item = static_cast<Node const*>(index.internalPointer());
switch(item->type) {
case Node::Identity: return static_cast<IdentityNode const *>(item)->identity;
default:
return CryptoIdentity();
}
}
CryptoIdentity * IdentityCollection::getMutableIdentity(const QModelIndex &index)
{
if (!index.isValid())
return nullptr;
if (index.column() != 0)
return nullptr;
Node *item = static_cast<Node*>(index.internalPointer());
switch(item->type) {
case Node::Identity: return &static_cast<IdentityNode *>(item)->identity;
default:
return nullptr;
}
}
QStringList IdentityCollection::groups() const
{
QStringList result;
for(auto const & grp : root.children)
{
result.append(grp->as<GroupNode>().title);
}
return result;
}
QString IdentityCollection::group(const QModelIndex &index) const
{
if (!index.isValid())
return QString { };
Node const *item = static_cast<Node const*>(index.internalPointer());
switch(item->type) {
case Node::Root: return QString { };
case Node::Group: return static_cast<GroupNode const *>(item)->title;
case Node::Identity: return static_cast<IdentityNode const *>(item)->parent->as<GroupNode>().title;
default: return QString { };
}
}
bool IdentityCollection::destroyIdentity(const QModelIndex &index)
{
if (!index.isValid())
return false;
Node * childItem = static_cast<Node *>(index.internalPointer());
Node * parent = childItem->parent;
if (parent == &root)
return false;
beginRemoveRows(this->parent(index), index.row(), index.row() + 1);
parent->children.erase(parent->children.begin() + childItem->index);
endRemoveRows();
return true;
}
bool IdentityCollection::canDeleteGroup(const QString &group_name)
{
for(auto const & group_node : root.children)
{
auto & group = group_node->as<GroupNode>();
if((group.children.size() == 0) and (group.title == group_name))
return true;
}
return false;
}
bool IdentityCollection::deleteGroup(const QString &group_name)
{
size_t index = 0;
for(auto it = root.children.begin(); it != root.children.end(); it++, index++)
{
auto & group = it->get()->as<GroupNode>();
if(group.title == group_name) {
if(group.children.size() > 0) {
qDebug() << "cannot delete non-empty group" << group_name;
return false;
}
beginRemoveRows(QModelIndex { }, index, index + 1);
root.children.erase(it);
endRemoveRows();
return true;
}
}
return false;
}
QModelIndex IdentityCollection::index(int row, int column, const QModelIndex &parent) const
{
if (not hasIndex(row, column, parent))
return QModelIndex();
Node const * parentItem;
if(!parent.isValid())
parentItem = &this->root;
else
parentItem = static_cast<Node*>(parent.internalPointer());
auto & children = parentItem->children;
if(row < 0 or size_t(row) >= children.size())
return QModelIndex { };
return createIndex(
row,
column,
reinterpret_cast<quintptr>(children[row].get())
);
}
QModelIndex IdentityCollection::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
Node const *childItem = static_cast<Node const *>(index.internalPointer());
Node const * parent = childItem->parent;
if (parent == &root)
return QModelIndex();
return createIndex(
parent->index,
0,
reinterpret_cast<quintptr>(parent));
}
int IdentityCollection::rowCount(const QModelIndex &parent) const
{
Node const * parentItem;
if (!parent.isValid())
parentItem = &root;
else
parentItem = static_cast<Node const *>(parent.internalPointer());
return parentItem->children.size();
}
int IdentityCollection::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 1;
}
QVariant IdentityCollection::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
Node const *item = static_cast<Node const*>(index.internalPointer());
if (role == Qt::DisplayRole)
{
switch(item->type) {
case Node::Root: return "root";
case Node::Group: return static_cast<GroupNode const *>(item)->title;
case Node::Identity: return static_cast<IdentityNode const *>(item)->identity.display_name;
default:
return "Unknown";
}
}
else if(role == Qt::DecorationRole) {
switch(item->type) {
case Node::Root: return QVariant { };
case Node::Group: return QIcon(":/icons/folder-open.svg");
case Node::Identity: return QIcon(":/icons/certificate.svg");
default: return QVariant { };
}
}
return QVariant();
}
void IdentityCollection::relayout()
{
for(size_t i = 0; i < root.children.size(); i++)
{
auto & group = *root.children[i];
group.parent = &root;
group.index = i;
// qDebug() << "group[" << group.index << "]" << group.as<GroupNode>().title;
for(size_t j = 0; j < group.children.size(); j++)
{
auto & id = *group.children[j];
id.parent = &group;
id.index = j;
assert(id.children.size() == 0);
// qDebug() << "id[" << id.index << "]" << id.as<IdentityNode>().identity.display_name;
}
}
}
bool IdentityCollection::internalAddGroup(const QString &group_name, GroupNode * & group)
{
for(auto const & grp : root.children)
{
auto * g = static_cast<GroupNode*>(grp.get());
if(g->title == group_name) {
group = g;
return false;
}
}
auto parent = QModelIndex { };
beginInsertRows(parent, this->root.children.size(), this->root.children.size() + 1);
group = new GroupNode();
group->title = group_name;
this->root.children.emplace_back(group);
this->relayout();
endInsertRows();
return true;
}
|