-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeviceModel.cpp
499 lines (400 loc) · 10.6 KB
/
DeviceModel.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//
// Copyright (C) 2016 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Daniel Kopecek <[email protected]>
//
#include "DeviceModel.h"
#include "Log.h"
#include <iostream>
#include <QVector>
#include <QCoreApplication>
DeviceModelItem::DeviceModelItem()
{
_parent = nullptr;
_requested_target = Rule::Target::Invalid;
}
DeviceModelItem::DeviceModelItem(const Rule& device_rule, DeviceModelItem* parent)
{
_parent = parent;
_device_rule = device_rule;
_requested_target = device_rule.getTarget();
}
DeviceModelItem::~DeviceModelItem()
{
_parent = nullptr;
qDeleteAll(_children);
}
void DeviceModelItem::appendChild(DeviceModelItem* child)
{
_children.append(child);
}
void DeviceModelItem::removeChild(DeviceModelItem* child)
{
(void)_children.takeAt(_children.indexOf(child));
}
DeviceModelItem* DeviceModelItem::child(int row)
{
return _children.value(row);
}
int DeviceModelItem::childCount() const
{
return _children.count();
}
int DeviceModelItem::columnCount() const
{
return 8;
}
QVariant DeviceModelItem::data(int column)
{
switch (column) {
case 0:
return QVariant(_device_rule.getRuleID());
case 1:
return QVariant(_requested_target != _device_rule.getTarget() ? QLatin1String("*") : QString());
case 2:
switch (_requested_target) {
case Rule::Target::Allow:
return QCoreApplication::translate("DeviceModel", "Allow");
case Rule::Target::Block:
return QCoreApplication::translate("DeviceModel", "Block");
case Rule::Target::Reject:
return QCoreApplication::translate("DeviceModel", "Reject");
case Rule::Target::Unknown:
case Rule::Target::Empty:
case Rule::Target::Invalid:
case Rule::Target::Match:
case Rule::Target::Device:
default:
return QVariant(Rule::targetToString(_requested_target));
}
case 3:
return QVariant(QString::fromStdString(_device_rule.getDeviceID().toString()));
case 4:
return QVariant(_device_rule.getName());
case 5:
return QVariant(_device_rule.getSerial());
case 6:
return QVariant(_device_rule.getViaPort());
case 7: {
QString interface_string;
for (auto interface : _device_rule.attributeWithInterface().values()) {
interface_string.append(QString::fromStdString(interface.toRuleString()));
interface_string.append(QLatin1String(" "));
}
return QVariant(interface_string);
}
default:
return QVariant();
}
}
int DeviceModelItem::row() const
{
if (_parent) {
return _parent->_children.indexOf(const_cast<DeviceModelItem*>(this));
}
else {
return 0;
}
}
DeviceModelItem* DeviceModelItem::parent()
{
return _parent;
}
Rule::Target DeviceModelItem::getRequestedTarget() const
{
return _requested_target;
}
Rule::Target DeviceModelItem::getDeviceTarget() const
{
return _device_rule.getTarget();
}
void DeviceModelItem::setRequestedTarget(Rule::Target target)
{
_requested_target = target;
}
void DeviceModelItem::setDeviceTarget(Rule::Target target)
{
_device_rule.setTarget(target);
_requested_target = target;
}
QString DeviceModelItem::getDeviceHash() const
{
return _device_rule.getHash();
}
quint32 DeviceModelItem::getDeviceID() const
{
return _device_rule.getRuleID();
}
DeviceModel::DeviceModel(QObject* parent)
: QAbstractItemModel(parent)
{
_root_item = new DeviceModelItem();
}
DeviceModel::~DeviceModel()
{
clear();
delete _root_item;
}
QVariant DeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal) {
return QVariant();
}
if (role != Qt::DisplayRole && role != Qt::TextAlignmentRole) {
return QVariant();
}
switch (section) {
case 0:
if (role == Qt::DisplayRole) {
return tr("ID");
}
else {
return Qt::AlignCenter;
}
break;
case 1:
if (role == Qt::DisplayRole) {
return tr(" M "); /* Modified flag */
}
if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
break;
case 2:
if (role == Qt::DisplayRole) {
return tr("Target");
}
if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
break;
case 3:
if (role == Qt::DisplayRole) {
return tr("USB ID");
}
if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
break;
case 4:
if (role == Qt::DisplayRole) {
return tr("Name");
}
break;
case 5:
if (role == Qt::DisplayRole) {
return tr("Serial");
}
break;
case 6:
if (role == Qt::DisplayRole) {
return tr("Port");
}
break;
case 7:
if (role == Qt::DisplayRole) {
return tr("Interfaces");
}
break;
default:
break;
}
return QVariant();
}
QModelIndex DeviceModel::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
DeviceModelItem* parent_item = nullptr;
if (!parent.isValid()) {
parent_item = _root_item;
}
else {
parent_item = static_cast<DeviceModelItem*>(parent.internalPointer());
}
DeviceModelItem* child_item = parent_item->child(row);
if (child_item) {
return createIndex(row, column, child_item);
}
else {
return QModelIndex();
}
}
QModelIndex DeviceModel::parent(const QModelIndex& index) const
{
if (!index.isValid()) {
return QModelIndex();
}
DeviceModelItem* child_item = static_cast<DeviceModelItem*>(index.internalPointer());
DeviceModelItem* parent_item = child_item->parent();
if (parent_item == _root_item) {
return QModelIndex();
}
else {
return createIndex(parent_item->row(), 0, parent_item);
}
}
int DeviceModel::rowCount(const QModelIndex& parent) const
{
if (parent.column() > 0) {
return 0;
}
DeviceModelItem* parent_item = nullptr;
if (!parent.isValid()) {
parent_item = _root_item;
}
else {
parent_item = static_cast<DeviceModelItem*>(parent.internalPointer());
}
return parent_item->childCount();
}
int DeviceModel::columnCount(const QModelIndex& parent) const
{
if (!parent.isValid()) {
return _root_item->columnCount();
}
else {
return static_cast<DeviceModelItem*>(parent.internalPointer())->columnCount();
}
}
QVariant DeviceModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (role == Qt::TextAlignmentRole) {
return headerData(index.column(), Qt::Horizontal, role);
}
if (role != Qt::DisplayRole) {
return QVariant();
}
DeviceModelItem* item = static_cast<DeviceModelItem*>(index.internalPointer());
return item->data(index.column());
}
Qt::ItemFlags DeviceModel::flags(const QModelIndex& index) const
{
if (!index.isValid()) {
return Qt::NoItemFlags;
}
if (index.column() == 2) {
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
else {
return QAbstractItemModel::flags(index);
}
}
void DeviceModel::insertDevice(const Rule& device_rule)
{
qCDebug(LOG) << "device_rule=" << device_rule;
const uint32_t device_id = device_rule.getRuleID();
const QString device_hash = device_rule.getHash();
const QString parent_hash = device_rule.getParentHash();
DeviceModelItem* parent_item = _hash_map.value(parent_hash, _root_item);
DeviceModelItem* child_item = new DeviceModelItem(device_rule, parent_item);
beginInsertRows(createIndex(parent_item->row(), 0, parent_item),
parent_item->childCount(), parent_item->childCount());
parent_item->appendChild(child_item);
_hash_map.insert(device_hash, child_item);
_id_map.insert(device_id, child_item);
endInsertRows();
}
void DeviceModel::updateDeviceTarget(quint32 device_id, Rule::Target target)
{
qCDebug(LOG) << "device_id=" << device_id
<< " target=" << target;
DeviceModelItem* item = _id_map.value(device_id, nullptr);
if (item == nullptr) {
return;
}
if (item->getDeviceTarget() != target) {
item->setDeviceTarget(target);
Q_EMIT dataChanged(createIndex(item->row(), 0, item),
createIndex(item->row(), item->columnCount() - 1, item),
QVector<int>() << Qt::DisplayRole);
}
}
void DeviceModel::updateRequestedTarget(DeviceModelItem* item, Rule::Target target)
{
qCDebug(LOG) << "item=" << item
<< " target=" << target;
if (item->getRequestedTarget() != target) {
item->setRequestedTarget(target);
Q_EMIT dataChanged(createIndex(item->row(), 0, item),
createIndex(item->row(), item->columnCount() - 1, item),
QVector<int>() << Qt::DisplayRole);
}
}
void DeviceModel::removeDevice(quint32 device_id)
{
qCDebug(LOG) << "device_id=" << device_id;
DeviceModelItem* item = _id_map.value(device_id, nullptr);
if (item == nullptr) {
return;
}
else {
removeDevice(item, /*notify=*/true);
}
}
void DeviceModel::removeDevice(DeviceModelItem* item, bool notify)
{
qCDebug(LOG) << "item=" << item << " notify=" << notify;
DeviceModelItem* parent_item = item->parent();
if (parent_item == nullptr) {
return;
}
if (notify) {
beginRemoveRows(createIndex(parent_item->row(), 0, parent_item), item->row(), item->row());
}
while (item->childCount() > 0) {
removeDevice(item->child(0), /*notify=*/false);
}
_hash_map.remove(item->getDeviceHash());
_id_map.remove(item->getDeviceID());
parent_item->removeChild(item);
delete item;
if (notify) {
endRemoveRows();
}
}
bool DeviceModel::containsDevice(quint32 device_id) const
{
return _id_map.count(device_id) > 0;
}
QModelIndex DeviceModel::createRowEditIndex(const QModelIndex& index) const
{
return createIndex(index.row(), 2, index.internalPointer());
}
QMap<quint32, Rule::Target> DeviceModel::getModifiedDevices() const
{
QMap<quint32, Rule::Target> modified_map;
for (auto item : _id_map) {
if (item->getDeviceTarget() != item->getRequestedTarget()) {
modified_map.insert(item->getDeviceID(), item->getRequestedTarget());
}
}
return modified_map;
}
void DeviceModel::clear()
{
qCDebug(LOG);
beginResetModel();
while (_root_item->childCount() > 0) {
removeDevice(_root_item->child(0), /*notify=*/false);
}
endResetModel();
}
/* vim: set ts=2 sw=2 et */