-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
445 lines (374 loc) · 13.5 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
#include "mainwindow.h"
#include "connectionhandler.h"
#include "connectdialog.h"
#include "randomart.h"
#include "transferdialog.h"
#include <QSplitter>
#include <QListWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMimeDatabase>
#include <QSettings>
#include <QStandardPaths>
#include <QFileDialog>
#include <QCheckBox>
#include <QSystemTrayIcon>
#ifdef Q_OS_LINUX
#include <QApplication>
#include <QDesktopWidget>
#include <X11/extensions/XTest.h>
#include <QX11Info>
#elif Q_OS_WINDOWS
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_tray = new QSystemTrayIcon(QIcon::fromTheme("state-offline"), this);
m_tray->show();
QSplitter *splitter = new QSplitter;
setCentralWidget(splitter);
QWidget *leftWidget = new QWidget;
leftWidget->setLayout(new QVBoxLayout);
splitter->addWidget(leftWidget);
m_list = new QListWidget;
m_trustButton = new QPushButton("Trust");
m_trustButton->setEnabled(false);
m_fileList = new QListWidget;
splitter->addWidget(m_fileList);
m_mouseControlButton = new QPushButton("Control remote mouse");
m_mouseControlButton->setEnabled(false);
m_connectionHandler = new ConnectionHandler(this);
RandomArt *ourRandomart = new RandomArt(m_connectionHandler->ourCertificate());
QCheckBox *useIconsCheckbox = new QCheckBox(tr("Show icons"));
leftWidget->layout()->addWidget(m_list);
leftWidget->layout()->addWidget(m_trustButton);
leftWidget->layout()->addWidget(m_mouseControlButton);
leftWidget->layout()->addWidget(new QLabel(tr("Our fingerprint:")));
leftWidget->layout()->addWidget(ourRandomart);
leftWidget->layout()->addWidget(useIconsCheckbox);
leftWidget->setMaximumWidth(ourRandomart->maximumWidth());
connect(m_connectionHandler, &ConnectionHandler::pingFromHost, this, &MainWindow::onPingFromHost);
connect(m_trustButton, &QPushButton::clicked, this, &MainWindow::onTrustClicked);
connect(m_mouseControlButton, &QPushButton::clicked, this, &MainWindow::onMouseControlClicked);
connect(m_list, &QListWidget::currentRowChanged, this, &MainWindow::onHostSelectionChanged);
connect(m_fileList, &QListWidget::itemDoubleClicked, this, &MainWindow::onFileItemDoubleClicked);
connect(useIconsCheckbox, &QCheckBox::stateChanged, ourRandomart, &RandomArt::setUseIcons);
connect(m_tray, &QSystemTrayIcon::activated, this, [this]() { setVisible(!isVisible()); });
connect(m_connectionHandler, &ConnectionHandler::mouseClickRequested, this, &MainWindow::onMouseClickRequested);
connect(m_connectionHandler, &ConnectionHandler::mouseMoveRequested, this, [](const QPoint &position) {
#ifdef Q_OS_LINUX
Display* display = QX11Info::display();
Q_ASSERT(display);
XTestFakeMotionEvent(display, -1, position.x(), position.y(), CurrentTime);
XFlush(display);
#else
QCursor::setPos(position);
#endif
});
m_cleanupTimer = new QTimer(this);
m_cleanupTimer->setSingleShot(true);
m_cleanupTimer->setInterval(10000);
connect(m_cleanupTimer, &QTimer::timeout, this, &MainWindow::onCleanup);
}
void MainWindow::onMouseControlClicked()
{
int row = m_list->currentRow();
if (row < 0 || row >= m_visibleHosts.count()) {
qWarning() << "Invalid selection" << row;
return;
}
MouseControlWindow *mouseInputDialog = new MouseControlWindow;
mouseInputDialog->setWindowFlags(Qt::Dialog | Qt::NoDropShadowWindowHint | Qt::FramelessWindowHint | mouseInputDialog->windowFlags());
// Semi-translucent background
mouseInputDialog->setAttribute(Qt::WA_TranslucentBackground);
mouseInputDialog->setAttribute(Qt::WA_NoSystemBackground, false);
QColor background = palette().window().color();
background.setAlpha(128);
mouseInputDialog->setStyleSheet(QStringLiteral("background:%1").arg(background.name(QColor::HexArgb)));
mouseInputDialog->connection = new Connection(m_connectionHandler);
connect(mouseInputDialog->connection, &Connection::disconnected, mouseInputDialog, &MouseControlWindow::close);
connect(mouseInputDialog, &MouseControlWindow::destroyed, m_mouseControlButton, [this, mouseInputDialog]() {
this->m_mouseControlButton->setEnabled(true);
mouseInputDialog->connection->socket()->disconnectFromHost();
});
connect(mouseInputDialog->connection, &Connection::connectionEstablished, mouseInputDialog, [mouseInputDialog]() {
mouseInputDialog->setGeometry(QApplication::desktop()->availableGeometry(mouseInputDialog));
connect(mouseInputDialog, &MouseControlWindow::mouseMoved, mouseInputDialog->connection, &Connection::sendMouseMoveEvent);
connect(mouseInputDialog, &MouseControlWindow::mouseClicked, mouseInputDialog->connection, &Connection::sendMouseClickEvent);
mouseInputDialog->setText("Press escape to cancel mouse control");
mouseInputDialog->setMouseTracking(true);
});
mouseInputDialog->setAlignment(Qt::AlignCenter);
mouseInputDialog->setText("Connecting to host...");
mouseInputDialog->show();
mouseInputDialog->connection->initiateMouseControl(m_visibleHosts[row]);
}
void MainWindow::onMouseClickRequested(const QPoint &position, const MouseButton button)
{
m_mouseCommandTimer.restart();
updateTrayIcon();
QCursor::setPos(position);
#ifdef Q_OS_LINUX
int xButton = 0;
switch(button) {
case LeftButton:
xButton = Button1;
break;
case MiddleButton:
xButton = Button2;
break;
case RightButton:
xButton = Button3;
break;
case ScrollUp:
xButton = Button4;
break;
case ScrollDown:
xButton = Button5;
break;
default:
qWarning() << "unhandled button" << button;
return;
}
Display* display = QX11Info::display();
Q_ASSERT(display);
XTestFakeButtonEvent(display, xButton, True, 0);
XTestFakeButtonEvent(display, xButton, False, 0);
XFlush(display);
#elif Q_OS_WINDOWS
INPUT inputEvent;
ZeroMemory(&inputEvent, sizeof(inputEvent));
inputEvent.type = INPUT_MOUSE;
// Press
switch(button) {
case LeftButton:
inputEvent.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
break;
case MidButton:
inputEvent.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN;
break;
case RightButton:
inputEvent.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
break;
default:
qWarning() << "unhandled button" << button;
return;
}
SendInput(1, &inputEvent, sizeof(inputEvent));
// Release
switch(button) {
case LeftButton:
inputEvent.mi.dwFlags = MOUSEEVENTF_LEFTUP;
break;
case MidButton:
inputEvent.mi.dwFlags = MOUSEEVENTF_MIDDLEUP;
break;
case RightButton:
inputEvent.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
break;
default:
qWarning() << "unhandled button" << button;
return;
}
SendInput(1, &inputEvent, sizeof(inputEvent));
#else
qWarning() << "Mouse stuff only available on Linux/X11 and windows";
Q_UNUSED(position);
Q_UNUSED(button);
#endif
}
void MainWindow::onPingFromHost(const Host &host)
{
QString displayName = host.name + " (" + host.address.toString() + ")";;
int index = m_visibleHosts.indexOf(host);
if (index >= 0) {
m_visibleHosts[index] = host;
m_visibleHosts[index].offline = false;
m_list->item(index)->setText(displayName);
m_list->item(index)->setIcon(host.trusted ? QIcon::fromTheme("security-high") : QIcon::fromTheme("security-low"));
if (!m_cleanupTimer->isActive()) {
m_cleanupTimer->start();
}
return;
}
Q_ASSERT(m_visibleHosts.count() == m_list->count());
if (m_visibleHosts.count() > 100) {
qWarning() << "Too many";
if (m_visibleHosts.last().trusted) {
// full with trusted hosts, can't do anything
return;
}
m_visibleHosts.removeLast();
delete m_list->takeItem(m_list->count() - 1);
Q_ASSERT(m_visibleHosts.count() == m_list->count());
}
// Insert before the first non-trusted
int insertPosition = 0;
for (int i=0; i<m_visibleHosts.count(); i++) {
if (!m_visibleHosts[i].trusted) {
break;
}
}
QListWidgetItem *item = new QListWidgetItem(displayName);
item->setIcon(host.trusted ? QIcon::fromTheme("security-high") : QIcon::fromTheme("security-low"));
m_list->insertItem(insertPosition, item);
m_visibleHosts.insert(insertPosition, host);
if (!m_cleanupTimer->isActive()) {
m_cleanupTimer->start();
}
updateTrayIcon();
}
void MainWindow::onCleanup()
{
updateTrayIcon();
Q_ASSERT(m_visibleHosts.count() == m_list->count());
if (m_visibleHosts.isEmpty()) {
return;
}
bool anyActive = false;
for (int i=0; i<m_visibleHosts.size(); i++) {
if (m_visibleHosts[i].offline) {
continue;
}
if (m_visibleHosts[i].lastSeen.msecsTo(QDateTime::currentDateTime()) > 5000) {
m_visibleHosts[i].offline = true;
m_list->item(i)->setIcon(QIcon::fromTheme("network-offline"));
continue;
}
anyActive = true;
}
if (anyActive) {
m_cleanupTimer->start(10000);
}
}
void MainWindow::onTrustClicked()
{
int row = m_list->currentRow();
if (row < 0 || row >= m_visibleHosts.count()) {
qWarning() << "Invalid selection" << row;
return;
}
ConnectDialog *dialog = new ConnectDialog(m_visibleHosts[row]);
if (dialog->exec() == QDialog::Rejected) {
return;
}
m_visibleHosts[row].trusted = true;
m_list->item(row)->setIcon(QIcon::fromTheme("security-high"));
m_connectionHandler->trustHost(m_visibleHosts[row]);
}
void MainWindow::onHostSelectionChanged(int row)
{
if (row < 0 || row >= m_visibleHosts.count()) {
qWarning() << "Invalid selection" << row;
m_trustButton->setEnabled(false);
m_mouseControlButton->setEnabled(false);
return;
}
const Host &host = m_visibleHosts[row];
if (host.offline) {
m_trustButton->setEnabled(false);
m_mouseControlButton->setEnabled(false);
m_fileList->clear();
return;
}
if (!host.trusted) {
m_trustButton->setEnabled(true);
return;
}
m_trustButton->setEnabled(false);
m_mouseControlButton->setEnabled(true);
m_currentPath = "/";
updateFileList();
}
void MainWindow::onListingFinished(const QString &path, const QStringList &names)
{
if (path != m_currentPath) {
return;
}
QMimeDatabase mimeDb;
QIcon folderIcon = QIcon::fromTheme(mimeDb.mimeTypeForName("inode/directory").iconName());
for (QString name : names) {
int separatorPos = name.indexOf(':');
if (separatorPos == -1) {
qDebug() << "Invalid line" << name;
}
int size = name.left(separatorPos).toInt();
name = name.mid(separatorPos + 1);
QListWidgetItem *item = new QListWidgetItem(name);
if (name.endsWith('/')) {
item->setIcon(folderIcon);
} else {
item->setIcon(QIcon::fromTheme(mimeDb.mimeTypeForFile(name).iconName()));
item->setData(Qt::UserRole, size);
}
m_fileList->addItem(item);
}
}
void MainWindow::onFileItemDoubleClicked(QListWidgetItem *item)
{
if (currentHost().offline) {
return;
}
Q_ASSERT(item && !item->text().isEmpty());
const QString filename = item->text();
if (filename.endsWith('/')) {
m_currentPath += filename;
updateFileList();
return;
}
QSettings settings;
QString lastPath = settings.value("lastsavepath", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
QString localPath = QFileDialog::getSaveFileName(this, "Where to save", lastPath + '/' + filename);
if (localPath.isEmpty()) {
return;
}
Connection *connection = new Connection(m_connectionHandler);
new TransferDialog(this, connection, item->data(Qt::UserRole).toInt());
connection->download(currentHost(), m_currentPath + filename, localPath);
}
Host MainWindow::currentHost()
{
int row = m_list->currentRow();
if (row < 0 || row >= m_visibleHosts.count()) {
qWarning() << "Invalid selection" << row;
return Host();
}
return m_visibleHosts[row];
}
void MainWindow::updateFileList()
{
m_fileList->clear();
if (m_currentConnection) {
disconnect(m_currentConnection, &Connection::listingReceived, this, nullptr);
m_currentConnection->deleteLater();
}
m_currentConnection = new Connection(m_connectionHandler);
connect(m_currentConnection, &Connection::listingReceived, this, &MainWindow::onListingFinished);
m_currentConnection->list(currentHost(), m_currentPath);
}
void MainWindow::updateTrayIcon()
{
QString trayIcon = "state-offline";
if (m_mouseCommandTimer.isValid() && m_mouseCommandTimer.elapsed() < 10000) {
trayIcon = "state-warning";
} else if (!m_visibleHosts.isEmpty()) {
trayIcon = "state-information";
for (const Host &host : m_visibleHosts) {
if (host.offline) {
continue;
}
if (host.trusted) {
trayIcon = "state-ok";
break;
}
}
}
if (trayIcon == m_trayIcon) {
return;
}
m_trayIcon = trayIcon;
m_tray->setIcon(QIcon::fromTheme(m_trayIcon));
}