forked from krogank9/WifiMouseServer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
executable file
·180 lines (148 loc) · 4.97 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "encryptutils.h"
#include <QTimer>
#include <QPainter>
#include <QtNetwork/QNetworkInterface>
#include <QSettings>
#include <QCryptographicHash>
#include <QMessageBox>
#include <QCloseEvent>
#include "fakeinput.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
programIcon(new QIcon(":/images/icon64.png"))
{
ui->setupUi(this);
setPasswordDialog = new SetPasswordDialog(this);
helpIpDialog = new HelpIpDialog(this);
loadSettings();
this->setWindowIcon(*programIcon);
this->setFixedSize(this->geometry().width(),this->geometry().height());
this->setWindowFlags(Qt::Dialog);
this->setWindowFlag(Qt::WindowContextHelpButtonHint, false);
updateServerIp();
createActions();
createTrayIcon();
if(ui->startMinimizedCheck->isChecked() == false)
this->show();
}
MainWindow::~MainWindow()
{
delete ui;
delete programIcon;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
saveSettings();
QMainWindow::closeEvent(event);
}
void MainWindow::loadSettings()
{
QSettings settings;
serverPassword = settings.value("pass","").toByteArray();
if(serverPassword.length() != 16)
serverPassword = EncryptUtils::makeHash16("");
ui->startMinimizedCheck->setChecked( settings.value("startMinimized", true).toBool() );
ui->tcpEnabledCheck->setChecked( settings.value("tcpServerEnabled", true).toBool() );
ui->bluetoothEnabledCheck->setChecked( settings.value("bluetoothServerEnabled", true).toBool() );
}
void MainWindow::saveSettings()
{
QSettings settings;
settings.setValue("pass", serverPassword);
settings.setValue("startMinimized", ui->startMinimizedCheck->isChecked());
settings.setValue("tcpServerEnabled", ui->tcpEnabledCheck->isChecked());
settings.setValue("bluetoothServerEnabled", ui->bluetoothEnabledCheck->isChecked());
emit configChanged();
}
QByteArray MainWindow::getPassword()
{
return serverPassword;
}
void MainWindow::setPassword(QString newPassword)
{
// Store password as 16 length hash ready to be used as AES key
serverPassword = EncryptUtils::makeHash16(newPassword.toUtf8());
saveSettings();
}
void MainWindow::setClientIp(QString ip)
{
if (ip != lastClient ) {
lastClient = ip;
if(ip == "Not connected") {
if ( ui->tcpEnabledCheck->isChecked() || ui->bluetoothEnabledCheck->isChecked() )
ui->clientInfoLabel->setText("Listening for clients");
else
ui->clientInfoLabel->setText("Server Disabled");
} else {
ui->clientInfoLabel->setText("Client: "+ip);
}
}
}
void MainWindow::updateServerIp()
{
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) {
setWindowTitle( QString("WifiMouseServer: %1").arg(address.toString()) );
return;
}
}
}
void MainWindow::clickMaximized()
{
this->show();
this->raise();
}
void MainWindow::clickMinimized()
{
this->hide();
}
void MainWindow::clickQuit()
{
this->close();
}
void MainWindow::clickSetPassword()
{
if(!setPasswordDialog->isVisible())
setPasswordDialog->show();
}
void MainWindow::clickIpHelper()
{
helpIpDialog->show();
}
void MainWindow::createActions()
{
quitAction = new QAction(tr("Quit"),this);
connect(quitAction, SIGNAL(triggered()), this, SLOT(clickQuit()));
connect(ui->minimizeButton, SIGNAL(released()), this, SLOT(clickMinimized()));
maximizeAction = new QAction(tr("Display"),this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(clickMaximized()));
passwordAction = new QAction(tr("Set password"),this);
connect(passwordAction, SIGNAL(triggered()), this, SLOT(clickSetPassword()));
connect(ui->passwordButton, SIGNAL(released()), this, SLOT(clickSetPassword()));
connect(ui->startMinimizedCheck, SIGNAL(released()), this, SLOT(saveSettings()));
connect(ui->tcpEnabledCheck, SIGNAL(released()), this, SLOT(saveSettings()));
connect(ui->bluetoothEnabledCheck, SIGNAL(released()), this, SLOT(saveSettings()));
connect(ui->helpButton, SIGNAL(released()), this, SLOT(clickIpHelper()));
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger)
clickMaximized();
}
void MainWindow::createTrayIcon()
{
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(*programIcon);
trayIcon->setToolTip("WifiMouseServer v" + QString(APP_VERSION) );
trayIcon->show();
trayIconMenu = new QMenu(this);
trayIconMenu->addSeparator();
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(passwordAction);
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::iconActivated);
}