-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeviceDialog.cpp
280 lines (239 loc) · 6.18 KB
/
DeviceDialog.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
//
// Copyright (C) 2015 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 <dkopecek@redhat.com>
//
#include "DeviceDialog.h"
#include "Log.h"
#include <ui_DeviceDialog.h>
#include <QRandomGenerator>
#include <QScreen>
#include <QStyle>
DeviceDialog::DeviceDialog(quint32 id, QWidget* parent) :
QDialog(parent),
ui(new Ui::DeviceDialog)
{
qCDebug(LOG) << "Creating DeviceDialog for device_id=" << id;
ui->setupUi(this);
setWindowTitle(tr("USB Device Inserted"));
setWindowIcon(QIcon(QLatin1String(":/usbguard-icon.svg")));
setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowStaysOnTopHint);
connect(&timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
device_id = id;
setDecisionMethod(DecisionMethod::Buttons);
setDefaultDecisionTimeout(23);
setRandomizePosition(false);
setDefaultDecision(Rule::Target::Block);
updateDialog();
timer.start(1000);
}
void DeviceDialog::setName(const QString& name)
{
ui->name_label->setText(name);
}
void DeviceDialog::setDeviceID(const QString& vendor_id, const QString& product_id)
{
ui->deviceid_label->setText(QString::fromLatin1("%1:%2").arg(vendor_id).arg(product_id));
}
void DeviceDialog::setSerial(const QString& serial)
{
qCDebug(LOG) << "Masking serial number value";
_serial = serial;
if (_mask_serial_number) {
for (auto i = _serial.size(), p = 1; i > 0; --i, ++p) {
if ((p % 2) == 0) {
_serial[i - 1] = QLatin1Char('*');
}
}
}
ui->serial_label->setText(_serial);
}
void DeviceDialog::setInterfaceTypes(const std::vector<usbguard::USBInterfaceType>& interfaces)
{
ui->interface_list->clear();
for (auto const& type : interfaces) {
ui->interface_list->addItem(QString::fromStdString(type.typeString()));
}
}
void DeviceDialog::setDefaultDecision(Rule::Target target)
{
switch (target) {
case Rule::Target::Allow:
ui->allow_button->setFocus();
break;
case Rule::Target::Block:
ui->block_button->setFocus();
break;
case Rule::Target::Reject:
case Rule::Target::Unknown:
case Rule::Target::Empty:
case Rule::Target::Invalid:
case Rule::Target::Match:
case Rule::Target::Device:
default:
ui->reject_button->setFocus();
}
_default_decision = target;
}
void DeviceDialog::setDefaultDecisionTimeout(quint32 seconds)
{
_default_decision_timeout = seconds;
time_left = seconds;
}
void DeviceDialog::setDecisionMethod(DeviceDialog::DecisionMethod method)
{
_decision_method = method;
}
void DeviceDialog::setDecisionIsPermanent(bool state)
{
_decision_is_permanent = state;
ui->permanent_checkbox->setChecked(state);
}
void DeviceDialog::setRejectVisible(bool state)
{
ui->reject_button->setHidden(!state);
}
static int randomInteger(int a, int b)
{
int min_val;
int max_val;
if (a > b) {
min_val = b;
max_val = a;
} else {
min_val = a;
max_val = b;
}
return QRandomGenerator::global()->bounded(min_val, max_val);
}
void DeviceDialog::setRandomizePosition(bool randomize)
{
QRect position_rect = \
QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,
size(), qGuiApp->primaryScreen()->availableGeometry());
if (randomize) {
const int h = ui->block_button->height();
const int w = ui->block_button->width();
const int dy = randomInteger(-2.618*h, 2.618*h);
const int dx = randomInteger(-2.618*w, 2.618*w);
position_rect.translate(dx, dy);
}
setGeometry(position_rect);
}
void DeviceDialog::setMaskSerialNumber(bool state)
{
_mask_serial_number = state;
}
void DeviceDialog::timerUpdate()
{
if (time_left > 0) {
--time_left;
updateDialog();
}
else {
timer.stop();
executeDefaultDecision();
}
}
void DeviceDialog::reject()
{
if (timer.isActive()) {
timer.stop();
updateDialog();
}
else {
QDialog::reject();
}
}
void DeviceDialog::accept()
{
if (timer.isActive()) {
timer.stop();
}
QDialog::accept();
}
void DeviceDialog::updateDialog()
{
QPushButton* button = nullptr;
QString label;
switch (_default_decision) {
case Rule::Target::Allow:
button = ui->allow_button;
label = tr("Allow");
break;
case Rule::Target::Block:
button = ui->block_button;
label = tr("Block");
break;
case Rule::Target::Reject:
case Rule::Target::Match:
case Rule::Target::Device:
case Rule::Target::Invalid:
case Rule::Target::Empty:
case Rule::Target::Unknown:
default:
button = ui->reject_button;
label = tr("Reject");
}
if (timer.isActive()) {
button->setText(QString::fromLatin1("%1 [%2]").arg(label).arg(time_left));
}
else {
button->setText(label);
ui->hint_label->setText(tr("(Press Escape to close this window)"));
}
}
void DeviceDialog::executeDefaultDecision()
{
qCDebug(LOG) << "Executing default decision: "
<< _default_decision;
switch (_default_decision) {
case Rule::Target::Allow:
on_allow_button_clicked();
break;
case Rule::Target::Block:
on_block_button_clicked();
break;
case Rule::Target::Reject:
case Rule::Target::Unknown:
case Rule::Target::Empty:
case Rule::Target::Invalid:
case Rule::Target::Device:
case Rule::Target::Match:
default:
on_allow_button_clicked();
}
}
DeviceDialog::~DeviceDialog()
{
delete ui;
}
void DeviceDialog::on_allow_button_clicked()
{
Q_EMIT allowed(device_id, ui->permanent_checkbox->isChecked());
accept();
}
void DeviceDialog::on_block_button_clicked()
{
Q_EMIT blocked(device_id, ui->permanent_checkbox->isChecked());
accept();
}
void DeviceDialog::on_reject_button_clicked()
{
Q_EMIT rejected(device_id, ui->permanent_checkbox->isChecked());
accept();
}
/* vim: set ts=2 sw=2 et */