-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
752 lines (549 loc) · 21.9 KB
/
mainwindow.cpp
File metadata and controls
752 lines (549 loc) · 21.9 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
exeDir = findExeDir(QCoreApplication::arguments().at(0));
tempFileDir = exeDir + QString("\\tmpfile.txt");
// defFileToLoad = "C:\\Users\\Davide\\Desktop\\MouseActions\\CoalDung.txt";
defFileToLoad = tempFileDir;
recording = false;
timeZero = 0;
actionIndex = 0;
repeatTotal = 1;
currRep = 0;
// initialise timer
timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), this, SLOT(doActionList()));
// initialise stuff in UI
this->setWindowTitle("MoveMouse");
this->setFixedSize(this->size());
ui->stopButton->setDisabled(true);
ui->startButton->setDisabled(false);
ui->currActEdit->setEnabled(false);
ui->totalActEdit->setEnabled(false);
ui->console->setEnabled(false);
ui->saveButton->setEnabled(false);
ui->loadButton->setEnabled(false);
ui->repeatTimesEdit->setEnabled(false);
ui->repeatTimesEdit->setValidator(new QIntValidator(0, 9999999, this));
// initialise UI
// put text in the various edits, labels and buttons
ui->console->setText("No file loaded");
ui->repeatTimesEdit->setText(QString::number(repeatTotal));
ui->loadPathEdit->setText(defFileToLoad);
// initialise the UI view for the monitors, put in the monitors of the current device
getMyMonitors(); // make sure this runs before anything requiring the myMonitors list
listMyMonitorsInUI();
drawMyMonitorsInUI(); // TODO: implement this function
// initialise data for mouse cursor
Mouse::tmpActFile = tempFileDir;
loadActions(tempFileDir.toStdString());
// initialise stuff for network operations
showNetworkInterfaces();
port = 60000;
// debug ===============================================================
// qDebug() << "argc: " << QCoreApplication::arguments().size();
// for (int i = 0; i < QCoreApplication::arguments().size(); i++) {
// qDebug() << i << "\targv: " << QCoreApplication::arguments().at(i);
// }
qDebug() << "exeDir: " << exeDir;
qDebug() << "tempFileDir; " << tempFileDir;
// initialise mouse things
Mouse *mouseObj = new Mouse();
mouseObj->moveToThread(&mousePollThread);
connect(&mousePollThread, SIGNAL(finished()), mouseObj, SLOT(deleteLater()));
connect(this, SIGNAL(startMousePolling(int)), mouseObj, SLOT(trackMousePosition(int)));
// mousePollThread.start();
// emit startMousePolling(0.1);
monitorDataToJSON();
jsonToMonitorData(QJsonDocument(monitorDataToJSON()));
}
MainWindow::~MainWindow() {
mousePollThread.quit();
timer->stop();
// UnhookWindowsHookEx(MouseHook);
delete timer;
delete ui;
}
QString MainWindow::findExeDir(QString f) {
int t;
t = (int)f.toStdString().find_last_of('\\');
return QString(f.toStdString().substr(0, t).c_str());
}
void MainWindow::on_updateButton_clicked() {
// update all the variables from UI to memory
ui->updateButton->setDisabled(true);
}
void MainWindow::on_tIntervalEdit_textChanged(const QString &arg1) {
ui->updateButton->setDisabled(false);
}
void MainWindow::on_startButton_clicked() {
// start going through the list of actions
ui->stopButton->setDisabled(false);
ui->startButton->setDisabled(true);
ui->recordButton->setDisabled(true);
ui->repeatTimesEdit->setDisabled(true);
currRep = 0;
repeatTotal = ui->repeatTimesEdit->text().toInt();
actionIndex = 0;
updateActionCounterLabel();
timer->start(1000);
}
void MainWindow::on_stopButton_clicked() {
// stop the timer
ui->stopButton->setDisabled(true);
ui->startButton->setDisabled(false);
ui->recordButton->setEnabled(true);
ui->repeatTimesEdit->setEnabled(true);
timer->stop();
}
void MainWindow::on_recordButton_clicked() {
// start or stop recording mouse events
if (recording) {
// stop recording
recording = false;
UnhookWindowsHookEx(MouseHook);
ui->recordButton->setText("Record");
ui->startButton->setEnabled(true);
// load actions from tempfile
loadActions(tempFileDir.toStdString());
// showAllActions();
} else {
// start recording
recording = true;
ui->startButton->setEnabled(false);
// rewrite the temp file
ofstream myfile;
// myfile.open(MainWindow::tempFileDir.c_str());
myfile.open(tempFileDir.toStdString().c_str());
if (myfile.is_open()) {
qDebug() << "Re-opened temp file";
timeZero = QDateTime::currentMSecsSinceEpoch();
MouseHook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)Mouse::MouseHookProc, GetModuleHandle(NULL), 0);
ui->recordButton->setText("Done");
qDebug() << "timeZero: " << timeZero;
// delete all actions previously recorded
actions.clear();
} else {
qDebug() << "Could not open temp file";
return;
}
}
}
void MainWindow::loadActions(string load) {
// load actions from tempfile, overwrite last set of actions
actions.clear();
ifstream read(load.c_str());
if (read.is_open()) {
string line;
while(getline(read, line)) {
int pos1 = line.find('\t', 0);
int pos2 = line.find('\t', pos1+1);
int pos3 = line.find('\t', pos2+1);
mouseAction temp;
// qDebug() << pos1 << "\t" << pos2 << "\t" << pos3;
temp.time = (uint64_t)strtoull(line.substr(0, pos1).c_str(), NULL, 10);
temp.action = (uint64_t)strtoull(line.substr(pos1+1, pos2-pos1-1).c_str(), NULL, 10);
temp.pos = QPoint((int)strtoul(line.substr(pos2+1, pos3-pos2-1).c_str(), NULL, 10),
(int)strtoul(line.substr(pos3+1, line.length()-pos3-1).c_str(), NULL, 10));
// calculate delay
if (actions.size() > 0) {
temp.delay = temp.time - actions.at(actions.size()-1).time;
} else {
temp.delay = 0;
}
actions.append(temp);
}
// file is loaded here
ui->currActEdit->setText("0");
ui->totalActEdit->setText(QString::number(actions.size()));
ui->console->setText("File loaded: ");
if (tempFileDir.toStdString() == load) {
// loading the temp file
ui->console->append(QString("tmpfile"));
} else {
ui->console->append(QString(load.c_str()));
}
fileLoaded = QString(load.c_str());
} else {
qDebug() << "Could not open tempFile to read actions";
}
}
void MainWindow::saveActions(string save) {
// save actions to file
qDebug() << "Saving actions to file";
ofstream write(save.c_str());
if (write.is_open()) {
for (int i = 0; i < actions.size(); i++) {
write << actions.at(i).time << "\t" << actions.at(i).action
<< "\t" << actions.at(i).pos.x() << "\t" << actions.at(i).pos.y();
write << "\n";
}
write.close();
// load actions from saved file
loadActions(save);
} else {
qDebug() << "Could not open save file";
}
}
void MainWindow::doActionList() {
// go through the action list and execute all of them
// the last two actions are used to stop recording, avoid them
// this function is called by the timer
// while (repeatTotal > currRep) {
// // prepare to repeat the action list
// qDebug() << "repeatTotal: " << repeatTotal << "\tcurrRep" << currRep;
// actionIndex = 0;
// // do action list
// for (int i = 0; i < actions.size(); i++) {
// qDebug() << "i: " << i << "\tactions.size()" << actions.size();
//// qApp->processEvents();
// if (actionIndex < actions.size()-2) {
// // keep going through the list
// // wait a second to do the first action
//// Sleep(actions.at(actionIndex).delay);
// timer->start((int)actions.at(actionIndex).delay);
// ui->currActEdit->setText(QString::number(actionIndex));
//// doAction(actionIndex);
// actionIndex++;
// } else {
// break;
// }
// }
// currRep++;
// updateActionCounterLabel(); // update counter label
// }
if (actionIndex < actions.size()-2) {
// keep going through the list
qDebug() << "actionIndex: " << actionIndex
<< "\tactions.size()" << actions.size()
<< "\tdelay: " << (int)actions.at(actionIndex).delay;
timer->start((int)actions.at(actionIndex+1).delay);
ui->currActEdit->setText(QString::number(actionIndex));
doAction(actionIndex);
actionIndex++;
} else {
// finished list
currRep++;
updateActionCounterLabel(); // update counter label
if (repeatTotal > currRep) {
// repeat the list
actionIndex = 0;
timer->start(1000);
} else {
on_stopButton_clicked();
}
}
}
void MainWindow::doAction(int index) {
// perform the action at actionindex
// qDebug() << "Doing action " << index;
SetCursorPos(actions.at(index).pos.x(), actions.at(index).pos.y());
switch (actions.at(index).action) {
case (513) : {
// Left mouse DOWN
qDebug() << "\tLeft mouse DOWN ... index: " << index << "\t" << actions.at(index).pos;
mouse_event(MOUSEEVENTF_LEFTDOWN, actions.at(index).pos.x(), actions.at(index).pos.y(), 0, 0);
break;
}
case (514) : {
// Left mouse UP
qDebug() << "\tLeft mouse UP ... index: " << index << "\t" << actions.at(index).pos;
mouse_event(MOUSEEVENTF_LEFTUP, actions.at(index).pos.x(), actions.at(index).pos.y(), 0, 0);
break;
}
case (516) : {
// Right mouse DOWN
qDebug() << "\tRight mouse DOWN ... index: " << index << "\t" << actions.at(index).pos;
mouse_event(MOUSEEVENTF_RIGHTDOWN, actions.at(index).pos.x(), actions.at(index).pos.y(), 0, 0);
break;
}
case (517) : {
// Right mouse UP
qDebug() << "\tRight mouse UP ... index: " << index << "\t" << actions.at(index).pos;
mouse_event(MOUSEEVENTF_RIGHTUP, actions.at(index).pos.x(), actions.at(index).pos.y(), 0, 0);
break;
}
}
}
void MainWindow::updateActionCounterLabel() {
// build the string in this format <rep> / <totalRep>
ui->actionCounterLabel->setText(QString(QString::number(currRep) + " / " + QString::number(repeatTotal)));
}
void MainWindow::updateConnectionStatus(QAbstractSocket::SocketState s) {
// status of clientSocket has changed, update the UI
switch (s) {
case QAbstractSocket::UnconnectedState:
ui->connStatusLabel->setText("Not Connected");
break;
case QAbstractSocket::HostLookupState:
ui->connStatusLabel->setText("Looking up hostname...");
break;
case QAbstractSocket::ConnectingState:
ui->connStatusLabel->setText("Connecting...");
break;
case QAbstractSocket::ConnectedState:
ui->connStatusLabel->setText("Connected to " + clientSocket->peerAddress().toString());
break;
case QAbstractSocket::BoundState:
ui->connStatusLabel->setText("Socket bound");
break;
case QAbstractSocket::ClosingState:
ui->connStatusLabel->setText("Closing connection...");
break;
default:
ui->connStatusLabel->setText("Unknown connection state");
}
}
void MainWindow::getMyMonitors() {
// get list of monitors and store it in myMonitors
// using a QHash here implicitly discards any duplicates
for (int i = 0; i < QApplication::screens().size(); i++) {
monitor tmp;
tmp.name = QApplication::screens().at(i)->name();
tmp.geometry = QApplication::screens().at(i)->geometry();
myMonitors[tmp.name] = tmp;
}
}
QJsonObject MainWindow::monitorDataToJSON() {
// function uses QApplication::screens() to get the monitor configuration of the device
// and packs it in a QByteArray
QJsonObject data;
getMyMonitors(); // run this just in case
data["monitorNum"] = QString::number(myMonitors.size()); // number of monitors
// put data for each monitor
QJsonArray jArray;
for (int i = 0; i < myMonitors.keys().size(); i++) {
QJsonObject jMonitorObject;
QString currKey = myMonitors.keys().at(i);
// jMonitorObject.insert("x", QString::number(myMonitors.at(i).geometry.x()).toStdString().c_str());
// jMonitorObject.insert("y", QString::number(myMonitors.at(i).geometry.y()).toStdString().c_str());
// jMonitorObject.insert("width", QString::number(myMonitors.at(i).geometry.width()).toStdString().c_str());
// jMonitorObject.insert("height", QString::number(myMonitors.at(i).geometry.height()).toStdString().c_str());
jMonitorObject.insert("x", QString::number(myMonitors[currKey].geometry.x()).toStdString().c_str());
jMonitorObject.insert("y", QString::number(myMonitors[currKey].geometry.y()).toStdString().c_str());
jMonitorObject.insert("width", QString::number(myMonitors[currKey].geometry.width()).toStdString().c_str());
jMonitorObject.insert("height", QString::number(myMonitors[currKey].geometry.height()).toStdString().c_str());
QJsonValue tmpValue = jMonitorObject;
jArray.insert(i, tmpValue);
}
data.insert("monitorData", jArray);
qDebug() << QJsonDocument(data);
return data;
}
void MainWindow::jsonToMonitorData(QJsonDocument jDoc) {
// called by the server instance to decode the JSON object received by the client with its monitor configuration
qDebug() << "jsonToMonitorData()";
qDebug() << jDoc;
// for whatever reason this toString().toInt() is necessary, instead of doing toInt() only
int monitors = jDoc.object()["monitorNum"].toString().toInt();
QJsonArray monitorData = jDoc.object()["monitorData"].toArray();
for (int i = 0; i < monitors; i++) {
monitor tmp;
tmp.geometry.setX(monitorData.at(i)["x"].toString().toInt());
tmp.geometry.setY(monitorData.at(i)["y"].toString().toInt());
tmp.geometry.setWidth(monitorData.at(i)["width"].toString().toInt());
tmp.geometry.setHeight(monitorData.at(i)["height"].toString().toInt());
tmp.name = monitorData.at(i)["name"].toString();
clientMonitors.append(tmp);
}
for (int i = 0; i < clientMonitors.size(); i++) { qDebug() << clientMonitors.at(i).geometry; } // debug
}
void MainWindow::listMyMonitorsInUI() {
// put the configurations of the monitors on this device
// initialise and create the model for the UI
qDebug() << "listMyMonitorsInUI()";
monitorModel = new QStandardItemModel(this);
monitorModel->setHorizontalHeaderItem(0, new QStandardItem(QString("Monitor")));
monitorModel->setHorizontalHeaderItem(1, new QStandardItem(QString("Owner")));
monitorModel->setHorizontalHeaderItem(2, new QStandardItem(QString("Pos X")));
monitorModel->setHorizontalHeaderItem(3, new QStandardItem(QString("Pos Y")));
monitorModel->setHorizontalHeaderItem(4, new QStandardItem(QString("Resolution")));
ui->monitorTableView->setModel(monitorModel);
// show items in model
for (int i = 0; i < myMonitors.keys().size(); i++) {
QString currKey = myMonitors.keys().at(i);
monitorModel->setItem(i, 0, new QStandardItem(myMonitors[currKey].name));
monitorModel->setItem(i, 1, new QStandardItem(QString("ME")));
monitorModel->setItem(i, 2, new QStandardItem(QString::number(myMonitors[currKey].geometry.x())));
monitorModel->setItem(i, 3, new QStandardItem(QString::number(myMonitors[currKey].geometry.y())));
QString resolution = QString::number(myMonitors[currKey].geometry.size().width());
resolution += "x";
resolution += QString::number(myMonitors[currKey].geometry.size().height());
monitorModel->setItem(i, 4, new QStandardItem(resolution));
}
ui->monitorTableView->resizeColumnsToContents();
ui->monitorTableView->resizeRowsToContents();
}
void MainWindow::drawMyMonitorsInUI() {
}
void MainWindow::listClientMonitorsInUI() {
// displays the list of monitors received by the client in the UI
qDebug() << "listClientMonitorsInUI()";
// show items in model
int o = myMonitors.size();
for (int i = 0; i < clientMonitors.size(); i++) {
monitorModel->setItem(i+o, 0, new QStandardItem(clientMonitors.at(i).name));
monitorModel->setItem(i+o, 1, new QStandardItem(serverSocket->peerAddress().toString()));
monitorModel->setItem(i+o, 2, new QStandardItem(QString::number(clientMonitors.at(i).geometry.x())));
monitorModel->setItem(i+o, 3, new QStandardItem(QString::number(clientMonitors.at(i).geometry.y())));
QString resolution = QString::number(clientMonitors.at(i).geometry.size().width());
resolution += "x";
resolution += QString::number(clientMonitors.at(i).geometry.size().height());
monitorModel->setItem(i+o, 4, new QStandardItem(resolution));
}
ui->monitorTableView->resizeColumnsToContents();
ui->monitorTableView->resizeRowsToContents();
}
void MainWindow::drawClientMonitorsInUI() {
}
void MainWindow::showNetworkInterfaces() {
// display list of network interfaces in the UI
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
QStringList strList;
strList.push_back("All Interfaces");
for (int i = 0; i < list.size(); i++) {
strList.push_back(list.at(i).humanReadableName());
// strList.push_back(list.at(i).name()); // these names on windows are not very descriptive
for (int i1 = 0; i1 < list.at(i).addressEntries().size(); i1++) {
qDebug() << "\t" << list.at(i).addressEntries().at(i1).ip().toString();
}
}
ui->networkIfaceComboBox->addItems(strList);
}
void MainWindow::newConnection() {
// called when server instance received an incoming connection
// the server will be the one where the hardware mouse/keyboard is being moved on
qDebug() << "newConnection()";
serverSocket = listenSocket->nextPendingConnection();
connect(serverSocket, SIGNAL(disconnected()), this, SLOT(clearClientData()));
QByteArray data;
// receive monitor details of the other device
qDebug() << "Waiting to receive monitor information from client...";
if (serverSocket->waitForReadyRead()) {
data = serverSocket->readAll();
qDebug() << "Read bytes " << data.size();
qDebug() << data;
} else {
qDebug() << "Did not receive monitor information in time";
}
// decode monitor data and save it
jsonToMonitorData(QJsonDocument::fromBinaryData(data));
// display monitors received by clients in the UI
listClientMonitorsInUI();
drawClientMonitorsInUI(); // TODO: implement this function
// start the loop to send mouse movements to the client
}
void MainWindow::connectedToServer() {
// called when client socket connects to server instance
qDebug() << "connectedToServer()";
// send information about this machine's monitor to server
// send it in JSON format, so it is easy to encode/decode
QThread::sleep(1); // this is to wait for the receiver to start the waitForReadyRead()
qDebug () << "Sending monitor info...";
QJsonObject jMonitorData = monitorDataToJSON();
QByteArray monitorData = QJsonDocument(jMonitorData).toJson();
int dataSent = (int)clientSocket->write(monitorData);
if (dataSent == -1) { qDebug() << "Failed to send monitor data"; }
}
void MainWindow::clearClientData() {
// this is called if the connection between the server and client drops
clientMonitors.clear();
}
void MainWindow::showAllActions() {
// shows all the actions recorded
qDebug() << "\n\nShowing all the actions recorded ... " << actions.size();
qDebug() << "index\ttime\tdelay\taction\tpos";
for (int i = 0; i < actions.size(); i++) {
qDebug() << i << "\t" << actions.at(i).time << "\t" << actions.at(i).delay << "\t" << actions.at(i).action << "\t" << actions.at(i).pos;
}
}
void MainWindow::on_loadButton_clicked() {
loadActions(ui->loadPathEdit->text().toStdString());
}
void MainWindow::on_loadPathEdit_textChanged(const QString &arg1) {
// qDebug() << "on_loadPathEdit_textChanged";
if (!arg1.isEmpty()) {
ui->loadButton->setEnabled(true);
} else {
ui->loadButton->setEnabled(false);
}
}
void MainWindow::on_savePathEdit_textChanged(const QString &arg1) {
// qDebug() << "on_savePathEdit_textChanged";
if (!arg1.isEmpty()) {
ui->saveButton->setEnabled(true);
} else {
ui->saveButton->setEnabled(false);
}
}
void MainWindow::on_saveButton_clicked() {
saveActions(ui->savePathEdit->text().toStdString());
}
void MainWindow::on_repeatBox_toggled(bool checked) {
if (checked) {
// set up to repeat actions multiple times
ui->repeatTimesEdit->setEnabled(true);
} else {
repeatTotal = 1;
ui->repeatTimesEdit->setText(QString::number(repeatTotal));
ui->repeatTimesEdit->setEnabled(false);
}
}
void MainWindow::on_repeatTimesEdit_textChanged(const QString &arg1) {
repeatTotal = arg1.toInt();
}
void MainWindow::on_listenButton_clicked() {
// start listening on the interface specified in the combobox
// update text in the label to show where the program is listening on
int intIndex = ui->networkIfaceComboBox->currentIndex();
QNetworkInterface netInt;
QString intIP;
QString labelMessage = "Listening on ";
if (intIndex == 0) {
intIP = "0.0.0.0";
} else {
qDebug () << "intIndex: " << QString::number(intIndex);
netInt = QNetworkInterface::allInterfaces().at(intIndex-1);
if (!netInt.isValid()) {
qDebug() << "netInt is invalid";
return;
}
int max = netInt.addressEntries().size();
intIP = netInt.addressEntries().at(max-1).ip().toString();
labelMessage.append(intIP);
}
labelMessage.append(intIP + ":" + QString::number(port));
ui->ifacePortLabel->setText(labelMessage);
qDebug() << labelMessage;
// open TCP socket and start listening
listenSocket = new QTcpServer(this);
connect(listenSocket, SIGNAL(newConnection()), this, SLOT(newConnection()));
if (intIndex == 0) {
if (!listenSocket->listen(QHostAddress::Any, port)) {
qDebug() << "Server could not start";
return;
}
} else {
if (!listenSocket->listen(QHostAddress(intIP), port)) {
qDebug() << "Server could not start";
return;
}
}
}
void MainWindow::on_connectButton_clicked() {
// connect to the host running the server instance
clientSocket = new QTcpSocket(this);
connect(clientSocket, SIGNAL(connected()), this, SLOT(connectedToServer()));
connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this
, SLOT(updateConnectionStatus(QAbstractSocket::SocketState)));
clientSocket->connectToHost(ui->serverIPEdit->text(), port);
if (!clientSocket->waitForConnected(5000)) {
qDebug() << "Could not connect to server";
return;
}
}