-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
782 lines (692 loc) · 26.6 KB
/
mainwindow.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
* Copyright 2020 Sinodun Internet Technologies Ltd.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <QDebug>
#include <QMessageBox>
#include <QCloseEvent>
#include <QPixmap>
#include <QPainter>
#include <QTimer>
#include <QSettings>
#include <QDesktopServices>
#include <QDateTime>
#include "mainwindow.h"
#include "ui_mainwindow.h"
// This is for the colored balls used to display status
class CirclePixmap: public QPixmap {
public:
CirclePixmap(QColor col);
};
CirclePixmap::CirclePixmap(QColor col)
:QPixmap(15,15) {
fill(QColor(255, 0, 0, 0));
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
QBrush brush(col);
p.setBrush(brush);
p.drawEllipse(0, 0, 15, 15);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_serviceState(ServiceMgr::Unknown)
, m_networkState(NetworkMgr::Unknown)
, updateState(Init)
, m_configMgr(), m_serviceMgr(), m_networkMgr()
, m_untrustedNetworkWidget(), m_trustedNetworkWidget()
, m_hostileNetworkWidget(), m_networksWidget(), m_logMgr()
, timer(), probeTimer(), quitAction(), trayIcon()
, trayIconMenu(), greenPixmap(), yellowPixmap()
, redPixmap(), greyPixmap()
{
ui->setupUi(this);
//#ifdef Q_OS_MAC
// QFont f = ui->tabWidget->font();
// f.setPointSize(14);
// ui->tabWidget->setFont(f);
// ui->statusOutput->setFont(f);
//#endif
// For now, make unimplemented buttons disappear
ui->revertAllButton->setVisible(false);
ui->toggleDNSButton->setVisible(false);
ui->toggleServiceButton->setVisible(false);
setToolTips();
// TODO - add a 'clear status messages' button to the GUI
statusMsg("Stubby Manager Started.");
ui->runningStatus->setText("Checking status...");
// Set up system tray
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, this, &MainWindow::closeFromSystray);
openAction = new QAction(tr("&Open"), this);
connect(openAction, SIGNAL(triggered()), this, SLOT(show()));
trayIconMenu = new QMenu(this);
trayIconMenu->addSeparator();
trayIconMenu->addAction(openAction);
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/images/[email protected]"));
trayIcon->show();
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
// Set up circle icons
greenPixmap = new CirclePixmap(Qt::green);
yellowPixmap = new CirclePixmap(Qt::yellow);
redPixmap = new CirclePixmap(Qt::red);
greyPixmap = new CirclePixmap(Qt::lightGray);
ui->serviceStatus->setPixmap(*greyPixmap);
ui->networkStatus->setPixmap(*greyPixmap);
ui->connectStatus->setPixmap(*greyPixmap);
ui->stubbyStatus->setPixmap(*greyPixmap);
// Set up config
m_configMgr = ConfigMgr::factory(this);
if (!m_configMgr) {
qFatal("Could not initialise Config Mgr");
abort();
}
m_configMgr->init();
m_configMgr->load();
m_untrustedNetworkWidget = new NetworkProfileWidget(*m_configMgr, Config::NetworkProfile::untrusted);
m_trustedNetworkWidget = new NetworkProfileWidget(*m_configMgr, Config::NetworkProfile::trusted);
m_hostileNetworkWidget = new NetworkProfileWidget(*m_configMgr, Config::NetworkProfile::hostile);
ui->networkProfileConfig->clear();
ui->networkProfileConfig->addTab(m_untrustedNetworkWidget, QString::fromUtf8("Untrusted"));
ui->networkProfileConfig->addTab(m_trustedNetworkWidget, QString::fromUtf8("Trusted"));
ui->networkProfileConfig->addTab(m_hostileNetworkWidget, QString::fromUtf8("Hostile"));
connect(m_untrustedNetworkWidget, &NetworkProfileWidget::userProfileEditInProgress,
this, &MainWindow::on_userProfileEditInProgress);
connect(m_trustedNetworkWidget, &NetworkProfileWidget::userProfileEditInProgress,
this, &MainWindow::on_userProfileEditInProgress);
connect(m_hostileNetworkWidget, &NetworkProfileWidget::userProfileEditInProgress,
this, &MainWindow::on_userProfileEditInProgress);
connect(m_configMgr, &ConfigMgr::configChanged,
this, &MainWindow::on_SavedConfigChanged);
// Set up service state
m_serviceMgr = ServiceMgr::factory(this);
if (!m_serviceMgr) {
qFatal("Could not initialise Service Mgr");
abort();
}
connect(m_serviceMgr, SIGNAL(serviceStateChanged(ServiceMgr::ServiceState)), this, SLOT(on_serviceStateChanged(ServiceMgr::ServiceState)));
// Set up network manager
m_networkMgr = NetworkMgr::factory(this);
if (!m_networkMgr) {
qFatal("Could not initialise Service Mgr");
abort();
}
connect(m_networkMgr, SIGNAL(DNSStateChanged(NetworkMgr::NetworkState)), this, SLOT(on_DNSStateChanged(NetworkMgr::NetworkState)));
connect(m_networkMgr, SIGNAL(testQueryResult(bool)), this, SLOT(on_testQueryResult(bool)));
// Set up networks tab.
m_networksWidget = new NetworksWidget(*m_configMgr, this);
ui->mainTabWidget->removeTab(2);
ui->mainTabWidget->insertTab(2, m_networksWidget, "Networks");
connect(m_networksWidget, &NetworksWidget::userNetworksEditInProgress,
this, &MainWindow::on_userNetworksEditInProgress);
// Create a log manager
m_logMgr = ILogMgr::factory(this);
if (!m_networkMgr) {
qFatal("Could not initialise Service Mgr");
abort();
}
// Set initially displayed tab.
ui->mainTabWidget->setCurrentIndex(0);
ui->statusTab->setFocus();
m_serviceMgr->getState();
m_networkMgr->getDNSState(true);
m_networksWidget->setNWGuiState();
m_untrustedNetworkWidget->setNPWGuiState();
m_trustedNetworkWidget->setNPWGuiState();
m_hostileNetworkWidget->setNPWGuiState();
setTopPanelNetworkInfo();
// Create some timers
timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&MainWindow::timerExpired));
probeTimer = new QTimer(this);
connect(probeTimer, &QTimer::timeout, this, QOverload<>::of(&MainWindow::probeTimerExpired));
probeTimer->start(300000);
// Create the settings
stubbySettings = new QSettings("Sinodun.com", "Stubby Manager");
QVariant details = stubbySettings->value("info/hideDetails");
if (!details.isNull())
ui->hideDetailsCheckBox->setChecked(details.toBool());
updateState = None;
}
MainWindow::~MainWindow()
{
delete ui;
delete m_configMgr;
delete m_networkMgr;
delete m_serviceMgr;
delete m_untrustedNetworkWidget;
delete m_trustedNetworkWidget;
delete m_hostileNetworkWidget;
delete m_networksWidget;
delete timer;
delete probeTimer;
delete quitAction;
delete trayIcon;
delete trayIconMenu;
delete greenPixmap;
delete yellowPixmap;
delete redPixmap;
delete greyPixmap;
}
void MainWindow::setToolTips() {
ui->helpButton->setToolTip("Open the webpage with the Stubby Manager help");
ui->hideDetailsCheckBox->setToolTip("Hide most of the text displayed on the Info tab");
ui->restartButton->setToolTip("Restart just the Stubby Service");
ui->probeButton->setToolTip("Check the status of the service, DNS settings and do a connection test");
ui->testButton->setToolTip("Perform a test DNS query using Stubby");
ui->serviceRunningLabel->setToolTip("Reports the status of the Stubby Service");
ui->serviceInUseLabel->setToolTip("Report the status of the system DNS settings:\n when the Stubby service is in use the DNS is set to Localhost,\n otherwise it is using the system DNS defaults");
ui->connectionLabel->setToolTip("Report the result of a test DNS query using Stubby");
ui->showLogButton->setToolTip("Enable display of logs from the Stubby service");
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
if(!this->isVisible()){
this->show();
} else {
this->hide();
}
break;
default:
break;
}
}
void MainWindow::timerExpired() {
if (updateState == None)
return;
statusMsg("WARNING: Stubby timed out trying to complete an action");
setTopPanelStatus();
updateState = None;
}
void MainWindow::probeTimerExpired() {
if (updateState != None)
return;
statusMsg("Action: Checking Stubby status...");
updateState = Probe;
m_serviceMgr->getState();
}
void MainWindow::closeFromSystray() {
if (handleUnsavedChanges() == 1 )
return;
qApp->quit();
return;
}
void MainWindow::closeEvent(QCloseEvent *event) {
#ifdef Q_OS_OSX
if (!event->spontaneous() || !isVisible()) {
return;
}
#endif
if (handleUnsavedChanges() == 1) {
event->ignore();
return;
}
if (trayIcon->isVisible()) {
QVariant exitMessage = stubbySettings->value("app/exitMessage");
if (!exitMessage.isNull() && exitMessage.toBool() == false) {
hide();
event->ignore();
return;
}
QMessageBox msgBox;
setMinimumSize(600,200);
msgBox.setText("Stubby Manager will keep running in the system tray.");
msgBox.setInformativeText("*We recommend you keep Stubby Manager running to actively monitor Stubby*<br><br>"
"To terminate Stubby Manager, choose 'Quit' in the context menu "
"of the system tray entry (this won't stop Stubby itself!).<br><br>"
"Hit 'Ok' if you don't want to see this message everytime the window is closed,"
"otherwise hit 'Close'.");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Close);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Ok:
stubbySettings->setValue("app/exitMessage", false);
break;
case QMessageBox::Close:
break;
default:
// should never be reached
break;
}
hide();
event->ignore();
}
}
void MainWindow::statusMsg(QString statusMsg) {
ui->statusOutput->moveCursor (QTextCursor::End);
ui->statusOutput->insertPlainText (QDateTime::currentDateTime().toString());
ui->statusOutput->insertPlainText (": ");
ui->statusOutput->insertPlainText (statusMsg);
ui->statusOutput->insertPlainText ("\n");
ui->statusOutput->moveCursor (QTextCursor::End);
}
void MainWindow::systrayMsg(QString status_msg) {
trayIcon->showMessage("Stubby message",
status_msg, QSystemTrayIcon::Information, 6000*1000);
}
void MainWindow::systrayAlert(QString status_msg) {
trayIcon->showMessage("Stubby ALERT",
status_msg, QSystemTrayIcon::Critical);
}
void MainWindow::logMsg(QString logMsg) {
ui->logOutput->moveCursor (QTextCursor::End);
ui->logOutput->insertPlainText (logMsg);
ui->logOutput->insertPlainText ("\n");
ui->logOutput->moveCursor (QTextCursor::End);
}
void MainWindow::firstRunPopUp()
{
// On the very first run pop up a useful message
QVariant firstRun = stubbySettings->value("app/firstRun");
if (!firstRun.isNull())
return;
QMessageBox::information(this, "Systray",
"Stubby Manager runs as a system tray application.<br> "
"We recommend that you make the Stubby Manager icon visible "
"in your system tray so you can easily see the state of "
"Stubby Manager (select the Start menu and type 'select "
"which icons appear on the taskbar').<br>");
stubbySettings->setValue("app/firstRun", false);
}
/*
* Slots functions
*/
void MainWindow::on_onOffSlider_stateChanged()
{
qInfo("Slider toggled, update State is %d", updateState);
if (updateState != None)
return;
statusMsg("");
bool value = ui->onOffSlider->isChecked();
if (value == true) {
firstRunPopUp();
// Currently we handle the service status first and based on the result of that action we later update the system DNS settings
updateState = Start;
ui->runningStatus->setText("Stubby starting...");
if (m_serviceState != ServiceMgr::Running && m_serviceState != ServiceMgr::Starting) {
if (handleUnsavedChanges() == 1) {
handleCancel();
return;
}
if (m_serviceMgr->start(*m_configMgr))
handleError();
}
else if (m_networkState != NetworkMgr::Localhost) {
if (m_networkMgr->setLocalhost())
handleError();
}
else {
// Nothing to do.... possilby recovering from error?
setTopPanelStatus();
updateState = None;
}
}
else {
// Currently we handle network status first and based on the result of that action we later update the service
updateState = Stop;
ui->connectStatus->setPixmap(*greyPixmap);
ui->runningStatus->setText("Stubby stopping...");
if (m_networkState != NetworkMgr::NotLocalhost) {
if (m_networkMgr->unsetLocalhost())
handleError();
}
else if (m_serviceState != ServiceMgr::Stopped && m_serviceState != ServiceMgr::Stopping) {
if (m_serviceMgr->stop())
handleError();
}
else {
// Nothing to do.... possilby recovering from error?
setTopPanelStatus();
updateState = None;
}
}
timer->start(20000);
}
void MainWindow::on_restartButton_clicked() {
if (handleUnsavedChanges() == 1) {
handleCancel();
return;
}
statusMsg("");
// Currently we handle the service status first and based on the result of that action we later update the system DNS settings
updateState = Restart;
ui->connectStatus->setPixmap(*greyPixmap);
ui->runningStatus->setText("Stubby restarting...");
if (m_serviceMgr->restart())
handleError();
timer->start(20000);
}
void MainWindow::on_probeButton_clicked() {
statusMsg("Action: Checking Stubby status...");
updateState = Probe;
m_serviceMgr->getState();
}
void MainWindow::on_testButton_clicked() {
if (!(m_serviceState == ServiceMgr::Running && m_networkState == NetworkMgr::Localhost)) {
statusMsg("Status: Stubby not running - no connection test performed");
ui->connectStatus->setPixmap(*greyPixmap);
return;
}
statusMsg("Action: Testing connection");
ui->connectStatus->setPixmap(*yellowPixmap);
setTopPanelStatus();
m_networkMgr->testQuery();
}
void MainWindow::on_testQueryResult(bool result) {
if (result) {
statusMsg("Status: Connection test was a success");
ui->connectStatus->setPixmap(*greenPixmap);
}
else {
statusMsg("Status: Connection test failed");
ui->connectStatus->setPixmap(*redPixmap);
systrayAlert("WARNING: Connection Test Failed. There was a problem with a test connection to the active server. Please check your settings.");
}
setTopPanelStatus();
}
void MainWindow::alertOnNewNetwork(std::string network, Config::NetworkProfile profile) {
if (updateState == Init || m_serviceState != ServiceMgr::Running)
return;
QString message = "A new network '";
message.append(network.c_str());
message.append("' was joined which will use the default ");
message.append(Config::networkProfileDisplayName(profile).c_str());
message.append(" network profile. If you want to change the profile for this network go to the Networks tab.");
systrayAlert(message);
message.prepend("Status: ");
statusMsg(message);
}
void MainWindow::alertOnNetworksUpdatedRestart() {
if (updateState == Init || m_serviceState != ServiceMgr::Running)
return;
QString message = "There was a change in the active networks - Stubby is restarting to switch to the ";
message.append(m_configMgr->getCurrentProfileString().c_str());
message.append(" network profile.");
systrayMsg(message);
message.prepend("Status: ");
statusMsg(message);
}
void MainWindow::on_showLogButton_toggled() {
if (ui->showLogButton->isChecked()) {
logMsg("\n**Log display started**\n");
m_logMgr->start();
}
else {
logMsg("\n**Log display stopped**\n");
m_logMgr->stop();
}
}
void MainWindow::on_hideDetailsCheckBox_toggled() {
if (ui->hideDetailsCheckBox->isChecked()) {
ui->detailsLabel->setVisible(false);
stubbySettings->setValue("info/hideDetails", true);
}
else {
ui->detailsLabel->setVisible(true);
stubbySettings->setValue("info/hideDetails", false);
}
}
void MainWindow::on_helpButton_clicked() {
QDesktopServices::openUrl (QUrl("https://dnsprivacy.org/wiki/display/DP/Stubby+Manager+GUI"));
}
void MainWindow::on_serviceStateChanged(ServiceMgr::ServiceState state) {
qDebug("SERVICE: Stubby Service state changed from '%s' to '%s' ", getServiceStateString(m_serviceState).toLatin1().data(), getServiceStateString(state).toLatin1().data());
m_serviceState = state;
setTopPanelStatus();
switch (m_serviceState) {
case ServiceMgr::Running:
ui->serviceStatus->setPixmap(*greenPixmap);
break;
case ServiceMgr::Stopped:
ui->serviceStatus->setPixmap(*greyPixmap);
break;
case ServiceMgr::Error:
ui->serviceStatus->setPixmap(*redPixmap);
if (updateState == Start || updateState == Stop || updateState == Restart) {
updateState = None;
}
return;
default:
ui->serviceStatus->setPixmap(*yellowPixmap);
break;
}
if (updateState == None) {
qInfo("*** Hitting on_serviceStateChanged while updateState is None");
if (m_serviceState == ServiceMgr::Stopped && m_networkState == NetworkMgr::Localhost) {
// error, reset network
updateState = Stop;
statusMsg("ERROR: The service stopped unexpectedly, disabling Stubby. Try to restart mually.");
systrayAlert("ERROR: The Stubby service stopped unexpectedly, disabling Stubby. Try to restart mually.");
if (m_networkMgr->unsetLocalhost())
handleError();
}
return;
}
if (updateState == Start) {
if (m_serviceState == ServiceMgr::Running) {
if (m_networkMgr->setLocalhost())
handleError();
}
else if (m_serviceState == ServiceMgr::Stopped) {
// error, reset network
if (m_networkMgr->unsetLocalhost())
handleError();
}
}
else if (updateState == Stop && m_serviceState == ServiceMgr::Stopped) {
updateState = None;
return;
}
else if (updateState == Restart) {
if (m_serviceState == ServiceMgr::Stopped) {
if (m_serviceMgr->start(*m_configMgr))
handleError();
}
else if (m_serviceState == ServiceMgr::Running) {
on_testButton_clicked();
setTopPanelStatus();
updateState = None;
return;
}
}
else if (updateState == Probe)
m_networkMgr->getDNSState(true);
setTopPanelStatus();
}
void MainWindow::on_DNSStateChanged(NetworkMgr::NetworkState state) {
if (state == m_networkState)
return;
qDebug("DNS: Network DNS state changed from '%s' to '%s' ", getNetworkStateString(m_networkState).toLatin1().data(), getNetworkStateString(state).toLatin1().data());
m_networkState = state;
setTopPanelStatus();
if (m_networkState == NetworkMgr::Localhost) ui->networkStatus->setPixmap(*greenPixmap);
else if (m_networkState == NetworkMgr::NotLocalhost) ui->networkStatus->setPixmap(*greyPixmap);
else ui->networkStatus->setPixmap(*yellowPixmap);
if (updateState == None) {
qInfo("*** Hitting on_DNSStateChanged while updateState is None");
return;
}
if (updateState == Stop && (m_serviceState == ServiceMgr::Running || m_serviceState == ServiceMgr::Starting)) {
if (m_serviceMgr->stop())
handleError();
setTopPanelStatus();
return;
}
on_testButton_clicked();
updateState = None;
}
void MainWindow::refreshNetworks(std::map<std::string, NetworkMgr::interfaceInfo> running_networks) {
m_configMgr->updateNetworks(running_networks);
}
/*
* Private functions
*/
QString MainWindow::getServiceStateString(const ServiceMgr::ServiceState state)
{
switch (state) {
case ServiceMgr::Stopped : return "Not running";
case ServiceMgr::Starting : return "Starting";
case ServiceMgr::Stopping : return "Stopping";
case ServiceMgr::Running : return "Running";
case ServiceMgr::Error : return "Error determining state";
case ServiceMgr::Unknown :
default : return "Unknown";
}
}
QString MainWindow::getNetworkStateString(const NetworkMgr::NetworkState state)
{
switch (state) {
case NetworkMgr::Localhost : return "Localhost";
case NetworkMgr::NotLocalhost : return "Not Localhost";
//case NetworkMgr::Error : return "Error determining state";
case NetworkMgr::Unknown :
default : return "Unknown";
}
}
void MainWindow::handleError() {
ui->runningStatus->setText("An error occurred");
statusMsg("ERROR: An Error occurred while stubby was starting or stopping");
ui->stubbyStatus->setPixmap(*redPixmap);
updateState = None;
timer->stop();
}
void MainWindow::handleCancel() {
statusMsg("Status: The action was cancelled");
setTopPanelStatus();
updateState = None;
timer->stop();
}
bool MainWindow::isServiceRunning() const {
return (m_serviceState == ServiceMgr::Running);
}
int MainWindow::handleUnsavedChanges() {
// Are there unsaved changes to any config?
if (!ui->applyAllButton->isEnabled())
return 0;
//TODO: We should be able to offer saving just the bits that matter...
QMessageBox msgBox;
setMinimumSize(200,200);
msgBox.setText("There are unsaved changes to profiles or networks");
msgBox.setInformativeText("Do you want to save ALL your changes before continuing?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
int result = 0;
switch (ret) {
case QMessageBox::Save:
result = m_configMgr->saveAll(true);
break;
case QMessageBox::Discard:
m_configMgr->restoreSaved();
break;
case QMessageBox::Cancel:
result = 1;
break;
default:
// should never be reached
break;
}
return result;
}
void MainWindow::setTopPanelStatus() {
if (updateState == None)
return;
qDebug ("STATE: Updating overall state: Service is '%s' and Network '%s' ", getServiceStateString(m_serviceState).toLatin1().data(), getNetworkStateString(m_networkState).toLatin1().data());
if (m_serviceState == ServiceMgr::Running &&
m_networkState == NetworkMgr::Localhost) {
ui->runningStatus->setText(getServiceStateString(m_serviceState));
ui->stubbyStatus->setPixmap(*greenPixmap);
trayIcon->setIcon(QIcon(":/images/stubby@245x145_green.png"));
ui->onOffSlider->setChecked(true);
}
else if (m_serviceState == ServiceMgr::Stopped &&
m_networkState == NetworkMgr::NotLocalhost) {
ui->runningStatus->setText(getServiceStateString(m_serviceState));
ui->stubbyStatus->setPixmap(*greyPixmap);
trayIcon->setIcon(QIcon(":/images/[email protected]"));
ui->onOffSlider->setChecked(false);
}
else if ((m_serviceState == ServiceMgr::Running &&
m_networkState == NetworkMgr::NotLocalhost) ||
(m_serviceState == ServiceMgr::Stopped &&
m_networkState == NetworkMgr::Localhost)) {
if (updateState == Start || updateState == Stop || updateState == Restart)
ui->runningStatus->setText("Updating...");
else
ui->runningStatus->setText("Partly running...");
ui->stubbyStatus->setPixmap(*yellowPixmap);
trayIcon->setIcon(QIcon(":/images/stubby@245x145_red.png"));
}
else if ((m_serviceState == ServiceMgr::Unknown &&
m_networkState == NetworkMgr::Unknown) ||
m_serviceState == ServiceMgr::Error ) {
//m_networkState == NetworkMgr::Unknown) {
ui->runningStatus->setText(getServiceStateString(m_serviceState));
ui->stubbyStatus->setPixmap(*redPixmap);
trayIcon->setIcon(QIcon(":/images/stubby@245x145_red.png"));
ui->onOffSlider->setChecked(false);
}
else {
ui->runningStatus->setText("Updating...");
ui->stubbyStatus->setPixmap(*yellowPixmap);
trayIcon->setIcon(QIcon(":/images/stubby@245x145_red.png"));
}
ui->restartButton->setEnabled(m_serviceState == ServiceMgr::Running);
}
void MainWindow::on_userProfileEditInProgress()
{
setMainButtonStates();
}
void MainWindow::on_userNetworksEditInProgress()
{
setMainButtonStates();
}
void MainWindow::on_SavedConfigChanged(bool restart) {
//qInfo("Refreshing displayed Config and Current Info");
m_networksWidget->setNWGuiState();
m_untrustedNetworkWidget->setNPWGuiState();
m_trustedNetworkWidget->setNPWGuiState();
m_hostileNetworkWidget->setNPWGuiState();
setMainButtonStates();
setTopPanelNetworkInfo();
if (m_serviceState == ServiceMgr::Running && restart && updateState != Init) {
updateState = Restart;
m_serviceMgr->restart();
}
}
void MainWindow::setMainButtonStates()
{
bool unsaved = m_configMgr->modifiedFromSavedConfig();
bool notdefault = m_configMgr->modifiedFromFactoryDefaults();
ui->applyAllButton->setEnabled(unsaved);
ui->discardAllButton->setEnabled(unsaved);
ui->revertAllButton->setEnabled(notdefault);
}
void MainWindow::on_applyAllButton_clicked()
{
m_configMgr->saveAll(true);
}
void MainWindow::on_discardAllButton_clicked()
{
m_configMgr->restoreSaved();
}
void MainWindow::on_revertAllButton_clicked()
{
m_configMgr->restoreFactory();
}
void MainWindow::setTopPanelNetworkInfo()
{
ui->network_profile->setText(m_configMgr->getCurrentProfileString().c_str());
ui->network_name->setText(m_configMgr->getCurrentNetworksString().c_str());
}