-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmanage_window.cpp
More file actions
319 lines (239 loc) · 9.21 KB
/
Copy pathmanage_window.cpp
File metadata and controls
319 lines (239 loc) · 9.21 KB
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
#include "manage_window.hpp"
#include "add_edit_window.hpp"
#include "network.hpp"
#include "utils.hpp"
#include <QWidget>
#include <QVBoxLayout>
#include <QListWidget>
#include <QPushButton>
#include <QToolTip>
#include <QMenu>
#include <QCloseEvent>
#include <QToolButton>
#include <QActionGroup>
#include <QDateTime>
#include <QSettings>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <cctype>
#include <sdbus-c++/Types.h>
constexpr auto SORT_ICON_PATH = ":/images/sort.png";
enum KnownRoles{
Show = Qt::UserRole,
Manage
};
ManageWindow::ManageWindow(iwd &manager, QWidget *parent): QDialog(parent), manager(manager){
setWindowIcon(Utils::getIcon(EXCELLENT_ICON_PATH));
setFlags();
createItems();
refreshNetworks();
if(settings.value(AVOID_SCANS_SETTING, false).toBool()){
avoidScansCheckbox->setChecked(true);
}
connect(avoidScansCheckbox, &QCheckBox::checkStateChanged, this, [=, this]{
settings.setValue(AVOID_SCANS_SETTING, avoidScansCheckbox->isChecked());
});
if(settings.value(SHOW_NOTIFICATIONS_SETTING, true).toBool()){
showNotificationsCheckbox->setChecked(true);
}
connect(showNotificationsCheckbox, &QCheckBox::checkStateChanged, this, [=, this]{
settings.setValue(SHOW_NOTIFICATIONS_SETTING, showNotificationsCheckbox->isChecked());
});
auto currentIconTheme = settings.value(ICON_THEME_SETTING, ICON_THEME_AUTO).toString();
if(currentIconTheme != ICON_THEME_DARK && currentIconTheme != ICON_THEME_LIGHT) {
currentIconTheme = ICON_THEME_AUTO;
}
auto iconThemeIndex = iconThemeComboBox->findData(currentIconTheme);
if(iconThemeIndex >= 0) {
iconThemeComboBox->setCurrentIndex(iconThemeIndex);
}
connect(iconThemeComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, [this](int index) {
if(index < 0) {
return;
}
settings.setValue(ICON_THEME_SETTING, iconThemeComboBox->itemData(index).toString());
emit iconThemeChanged();
});
connect(refreshButton, &QPushButton::clicked, this, [=, this]{
try{
refreshNetworks();
} catch(...){
//we might as well just ignore, handle it in the tray
}
});
connect(addButton, &QPushButton::clicked, this, [=, this]{
AddEditWindow *win = new AddEditWindow(nullptr, this);
win->exec();
refreshNetworks();
});
connect(listWidget, &QListWidget::itemEntered, this,
[=, this](QListWidgetItem* it){
QString info = it->data(KnownRoles::Show).toString();
auto pos = QCursor::pos();
pos.setY(pos.y() - 5);
QToolTip::showText(pos, info, listWidget);
});
setLayout(layout);
setMinimumWidth(350);
}
void ManageWindow::keyPressEvent(QKeyEvent *event) {
if(event->key() == Qt::Key_Escape){
event->ignore();
return;
}
}
void ManageWindow::closeEvent(QCloseEvent *event){
hide();
event->ignore();
}
void ManageWindow::sortNetworks(std::vector<known_network> &nets){
switch(currentSortMethod){
case SortType::ByName:
std::sort(nets.begin(), nets.end(), [](known_network &a, known_network &b){
return QString::fromStdString(a.name).toLower() < QString::fromStdString(b.name).toLower();
});
break;
case SortType::ByLast:
std::sort(nets.begin(), nets.end(), [](known_network &a, known_network &b){
auto dtA = QDateTime::fromString(QString::fromStdString(a.last_connected),
Qt::ISODate);
auto dtB = QDateTime::fromString(QString::fromStdString(b.last_connected),
Qt::ISODate);
return dtA.toSecsSinceEpoch() > dtB.toSecsSinceEpoch();
});
break;
case SortType::ByType:
std::sort(nets.begin(), nets.end(), [](known_network &a, known_network &b){
return QString::fromStdString(a.type).toLower() < QString::fromStdString(b.type).toLower();
});
break;
default:
break;
}
}
void ManageWindow::refreshNetworks(){
listWidget->clear();
auto inetworks = this->manager.known_networks();
sortNetworks(inetworks);
for(auto n: inetworks){
auto *it = new QListWidgetItem(n.name.c_str());
QString to_show = QString("Type: %1\nHidden: %2\nAutoconnect: %3\nLast connected: %4")
.arg(n.type)
.arg(n.hidden ? "true" : "false")
.arg(n.autoconnect ? "true" : "false")
.arg(n.last_connected);
it->setData(KnownRoles::Show, to_show);
QVariant data;
data.setValue(n);
it->setData(KnownRoles::Manage, data);
listWidget->addItem(it);
}
}
QMenu *ManageWindow::createSortItems(){
auto *menu = new QMenu(sortButton);
auto *group = new QActionGroup(menu);
auto addAction = [=](const char *val){
QAction *p = menu->addAction(tr(val));
group->addAction(p);
p->setCheckable(true);
return p;
};
QAction *name = addAction("By name");
connect(name, &QAction::triggered, this, [this](){
currentSortMethod = SortType::ByName;
settings.setValue(SORT_SETTING, (int)currentSortMethod);
refreshNetworks();
});
QAction *last = addAction("By last connected");
connect(last, &QAction::triggered, this, [this](){
currentSortMethod = SortType::ByLast;
settings.setValue(SORT_SETTING, (int)currentSortMethod);
refreshNetworks();
});
QAction *type = addAction("By type");
connect(type, &QAction::triggered, this, [this](){
currentSortMethod = SortType::ByType;
settings.setValue(SORT_SETTING, (int)currentSortMethod);
refreshNetworks();
});
auto sortType = (SortType)settings.value(SORT_SETTING, 0).toInt();
switch(sortType){
case SortType::ByName:
name->setChecked(true);
break;
case SortType::ByLast:
last->setChecked(true);
break;
case SortType::ByType:
last->setChecked(true);
break;
}
currentSortMethod = sortType;
return menu;
}
void ManageWindow::createItems(){
sortButton = new QToolButton(this);
sortButton->setFixedSize(15, 15);
sortButton->setIconSize(QSize(15, 15));
sortButton->setIcon(QIcon(SORT_ICON_PATH));
sortButton->setStyleSheet(QString{"QToolButton {border: 0px; margin-top: -3px;} QToolButton::menu-indicator { image: none; }"});
sortButton->setPopupMode(QToolButton::InstantPopup);
sortButton->setMenu(createSortItems());
listWidget = new QListWidget(this);
listWidget->setMouseTracking(true);
listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(listWidget, &QWidget::customContextMenuRequested,
this, [&](const QPoint &pos){
QMenu menu(this);
QAction *editAct = menu.addAction("Edit");
QAction *deleteAct = menu.addAction("Forget");
QAction *chosen = menu.exec(QCursor::pos());
if (!chosen) {
return;
}
auto cur = listWidget->indexAt(pos);
auto data = cur.data(KnownRoles::Manage).value<known_network>();
if (chosen == editAct) {
AddEditWindow *win = new AddEditWindow(&data, this);
win->exec();
refreshNetworks(); //in case autoconnect is changed
} else if (chosen == deleteAct) {
this->manager.forget_known_network(data);
delete listWidget->takeItem(cur.row());
}
});
avoidScansCheckbox = new QCheckBox("Avoid scans", this);
showNotificationsCheckbox = new QCheckBox("Show notifications", this);
iconThemeLabel = new QLabel(tr("Icon theme"), this);
iconThemeComboBox = new QComboBox(this);
iconThemeComboBox->addItem(tr("Auto"), QString(ICON_THEME_AUTO));
iconThemeComboBox->addItem(tr("Dark panel"), QString(ICON_THEME_DARK));
iconThemeComboBox->addItem(tr("Light panel"), QString(ICON_THEME_LIGHT));
refreshButton = new QPushButton("Refresh", this);
refreshButton->setFixedSize(95, 25);
addButton = new QPushButton("Add new", this);
addButton->setFixedSize(95, 25);
layout = new QVBoxLayout(this);
layout->addWidget(sortButton, 0, Qt::AlignRight);
layout->addWidget(listWidget);
QHBoxLayout *iconThemeLayout = new QHBoxLayout();
iconThemeLayout->addStretch();
iconThemeLayout->addWidget(iconThemeLabel);
iconThemeLayout->addWidget(iconThemeComboBox);
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addStretch();
layout2->addWidget(avoidScansCheckbox, 0, Qt::AlignLeft);
layout2->addWidget(showNotificationsCheckbox, 0, Qt::AlignLeft);
layout2->addWidget(refreshButton);
layout2->addWidget(addButton);
layout->addLayout(iconThemeLayout);
layout->addLayout(layout2);
}
void ManageWindow::setFlags(){
Qt::WindowFlags flags = windowFlags();
flags &= ~Qt::WindowFullscreenButtonHint;
flags &= ~Qt::WindowMaximizeButtonHint;
flags &= ~Qt::WindowMinimizeButtonHint;
setWindowFlags(flags);
}