-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
631 lines (551 loc) · 17.1 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
/*
*
* Copyright 2013 Mithat Konar
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "preferencesdialog.h"
#include <QMessageBox>
#include <QDebug>
#include <QProcess>
#include <QSettings>
#include <QCloseEvent>
// TODO: Convert UI "VM Name" lineEdit to auto-populated menu of some sort.
// TODO: Do STATUSBAR_TIMEOUT in a less kludgey way.
#define STATUSBAR_TIMEOUT 8000
/**
* @brief constructor
* @param parent The window's parent (Qwidget*)
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
configureTrayIcon();
readSettings();
}
/**
* @brief deconstructor
*/
MainWindow::~MainWindow()
{
writeSettings();
delete ui;
}
// ===============
// Action handlers
// ===============
/**
* @brief Exit the app.
*/
void MainWindow::on_actionQuit_triggered()
{
this->appQuit();
}
/**
* @brief Display a dialog box that allows app preferences to be set.
*/
void MainWindow::on_actionPreferences_triggered()
{
// Show preferences dialog, record changes in settings file
PreferencesDialog preferencesDialog(this);
preferencesDialog.setModal(true);
preferencesDialog.exec();
// Reset settings group(s) changed in dialog from file
readSettingsExecutables();
ui->statusBar->showMessage(tr("Preferences saved."), STATUSBAR_TIMEOUT);
}
/**
* @brief Start the VM.
*/
void MainWindow::on_actionRun_emulator_triggered()
{
// notification defaults
QString theTitle = tr("VM startup");
QString theMessage;
QSystemTrayIcon::MessageIcon theIcon = QSystemTrayIcon::Information;
int timeout = STATUSBAR_TIMEOUT;
if (isVMrunning()) {
theMessage = tr("VM is already running.");
theIcon = QSystemTrayIcon::Warning;
}
else {
QString program = "VBoxManage";
QStringList arguments;
arguments << "startvm" << getVMname();
QProcess *theProcess = new QProcess(this);
theProcess->start(program, arguments);
theProcess->waitForFinished(-1);
if (theProcess->exitCode() == 0) {
theMessage = tr("VM started.");
}
else {
theMessage = tr("Problem starting VM. Does it exist?");
theIcon = QSystemTrayIcon::Critical;
timeout = 0;
}
}
updateNotification(theTitle, theMessage, theIcon, timeout);
}
/**
* @brief Send the 'acpipowerbutton" signal to the VM.
*/
void MainWindow::on_actionACPI_shutdown_triggered()
{
// notification defaults
QString theTitle = tr("ACPI shutdown");
QString theMessage;
QSystemTrayIcon::MessageIcon theIcon = QSystemTrayIcon::Information;
int timeout = STATUSBAR_TIMEOUT;
if (!isVMrunning()) {
theMessage = tr("VM is not running.");
theIcon = QSystemTrayIcon::Warning;
}
else {
QString program = "VBoxManage";
QStringList arguments;
arguments << "controlvm" << getVMname() << "acpipowerbutton";
QProcess *theProcess = new QProcess(this);
// double-click powers off for 4.4
for (int i = 0; i < 2; i++) {
theProcess->start(program, arguments);
theProcess->waitForFinished(-1);
if (theProcess->exitCode() != 0) {
theMessage = tr("Problem sending shutdown signal.");
theIcon = QSystemTrayIcon::Critical;
timeout = 0;
return;
}
theProcess->waitForFinished(-1);
}
theMessage = tr("Shutdown signal sent to VM.");
}
updateNotification(theTitle, theMessage, theIcon, timeout);
}
/**
* @brief Display a dialog that shows information about the VM.
*/
void MainWindow::on_actionVM_info_triggered()
{
// Ask VBoxManage for a available VM properties.
QString program = "VBoxManage";
QStringList arguments;
arguments << "guestproperty" << "enumerate" << getVMname();
QProcess *theProcess = new QProcess(this);
theProcess->setProcessChannelMode(QProcess::MergedChannels);
theProcess->start(program, arguments);
theProcess->waitForFinished(-1);
QString p_stdout = theProcess->readAllStandardOutput();
// Report results
// TODO: Custom dialog that makes VM info less ugly.
QMessageBox::information(this, tr("VM info"), p_stdout);
}
/**
* @brief Start ADB server.
*/
void MainWindow::on_actionStart_server_triggered()
{
// set up and execute a process to start adb server
QString program = getADB();
QStringList arguments;
arguments << "start-server";
QProcess *theProcess = new QProcess(this);
theProcess->setProcessChannelMode(QProcess::MergedChannels);
theProcess->start(program, arguments);
// capture standard out and show.
theProcess->waitForFinished(-1);
QString p_stdout = theProcess->readAllStandardOutput();
if (p_stdout.isEmpty())
ui->statusBar->showMessage(tr("Server not started. Already running? Path set?"));
else
{
ui->statusBar->showMessage(lastLine(p_stdout));
qDebug() << p_stdout;
}
}
/**
* @brief Kill ADB server.
*/
void MainWindow::on_actionStop_server_triggered()
{
// set up and execute a process to kill adb server
QString program = getADB();
QStringList arguments;
arguments << "kill-server";
QProcess *theProcess = new QProcess(this);
theProcess->setProcessChannelMode(QProcess::MergedChannels);
theProcess->start(program, arguments);
// capture standard out and show.
theProcess->waitForFinished(-1);
QString p_stdout = theProcess->readAllStandardOutput();
if (p_stdout.isEmpty())
ui->statusBar->showMessage(tr("Message sent."));
else
ui->statusBar->showMessage(lastLine(p_stdout));
}
/**
* @brief Connect the VM to the ADB.
*/
void MainWindow::on_actionConnect_triggered()
{
// notification defaults
QString theTitle = tr("VM to ADB connection");
QString theMessage;
QSystemTrayIcon::MessageIcon theIcon = QSystemTrayIcon::Information;
int timeout = STATUSBAR_TIMEOUT;
if (!isVMrunning()) {
theMessage = tr("VM is not running.");
theIcon = QSystemTrayIcon::Warning;
}
else {
ui->statusBar->showMessage(tr("connecting..."));
// set up and execute a process to start adb and connect
QString program = getADB();
QStringList arguments;
arguments << "connect" << getIPAddr();
QProcess *theProcess = new QProcess(this);
theProcess->setProcessChannelMode(QProcess::MergedChannels);
theProcess->start(program, arguments);
// capture standard out and show.
theProcess->waitForFinished(-1);
theMessage = theProcess->readAllStandardOutput();
if (theMessage.isEmpty()) {
theMessage=tr("Problem running ADB. Is the location set?");
theIcon = QSystemTrayIcon::Critical;
timeout = 0;
}
}
updateNotification(theTitle, theMessage, theIcon, timeout);
}
/**
* @brief Disconnect the VM from the ADB.
*/
void MainWindow::on_actionDisconnect_triggered()
{
// notification defaults
QString theTitle = tr("VM to ADB connection");
QString theMessage;
QSystemTrayIcon::MessageIcon theIcon = QSystemTrayIcon::Information;
int timeout = STATUSBAR_TIMEOUT;
if (!isVMrunning()) {
theMessage = tr("VM is not running.");
theIcon = QSystemTrayIcon::Warning;
}
else {
ui->statusBar->showMessage(tr("disconnecting..."));
// set up and execute a process to start adb and disconnect
QString program = getADB();
QStringList arguments;
arguments << "disconnect" << getIPAddr();
QProcess *theProcess = new QProcess(this);
theProcess->setProcessChannelMode(QProcess::MergedChannels);
theProcess->start(program, arguments);
// capture standard out and show.
theProcess->waitForFinished(-1);
theMessage = theProcess->readAllStandardOutput();
if (theMessage.isEmpty()) {
theMessage = tr("Problem running ADB. Is the location set?");
theIcon = QSystemTrayIcon::Critical;
timeout = 0;
}
else if (theMessage == "\n")
theMessage = tr("disconnected");
}
updateNotification(theTitle, theMessage, theIcon, timeout);
}
/**
* @brief Display a dialog that shows information about this app.
*/
void MainWindow::on_actionAbout_triggered()
{
// TODO: About box strings should go someplace better.
QMessageBox::about(this,
tr("About Android VM Manager"),
"<b>" + tr("Android VM Manager") + " " + VER + "</b><br />" +
"<br />" +
tr("Copyright © 2013 Mithat Konar") + "<br />" +
tr("Licensed under the <a href=\"https://www.gnu.org/licenses/gpl-3.0.html\">GPLv3</a>") + "<br />" +
"<br />" +
"<a href=\"https://bitbucket.org/mithat/androidvmgr\">https://bitbucket.org/mithat/androidvmgr</a><br />"
);
}
// ===========
// Other slots
// ===========
/**
* @brief Safely quit the app
*/
void MainWindow::appQuit()
{
bool isQuit = true;
if (isVMrunning()) {
// first part of kludge to work around action initialed by notification icon:
bool wasHidden = this->isHidden();
if (wasHidden)
this->show();
// prompt for ACPI shutdown of VM:
QMessageBox::StandardButton isSend = QMessageBox::question(
this,tr("VM is running"),
tr("Send ACPI shutdown signal to the Virtual Machine?"),
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,
QMessageBox::Yes);
switch (isSend)
{
case QMessageBox::Yes:
on_actionACPI_shutdown_triggered();
break;
case QMessageBox::No:
break;
default:
isQuit = false;
break;
}
// second part of kludge:
if (wasHidden)
this->hide();
}
if (isQuit) {
qApp->quit();
// this->close();
}
}
/**
* @brief Process mouse clicks on system tray icon.
* @param reason The kind of mouse click (QSystemTrayIcon::ActivationReason)
*/
void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
// toggle visibility
this->setVisible(!this->isVisible());
break;
default:
;
}
}
// ===============
// Service methods
// ===============
void MainWindow::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
hide();
event->ignore();
}
}
/**
* @brief Show statusbar or systray ballon notification as appropriate.
* @param theTitle the title to be used in balloon notifications (QString)
* @param theMessage the message to be shown; can be multi-line; only last line shown in statusbar (QString)
* @param theIcon the icon to be used balloon notifications (QString)
* @param timeout timeout for both statusbar and balloon notification (int)
*/
void MainWindow::updateNotification(const QString &theTitle,
const QString &theMessage,
const QSystemTrayIcon::MessageIcon theIcon,
const int timeout)
{
ui->statusBar->showMessage(lastLine(theMessage), timeout);
if (this->isHidden() && trayIcon->isVisible())
trayIcon->showMessage(theTitle,
theMessage + " (" + this->getVMname()+")",
theIcon, timeout);
}
/**
* @brief Return the last non-blank line contained in msg.
* @param msg A body of (optionally multiline) text. (QString)
* @return The last line in msg (QString)
*/
QString MainWindow::lastLine(const QString &msg)
{
// The following will effectively nab the last non-blank line
// and send other lines to console.
QString lastLine = "";
QStringList lines = msg.split("\n");
// Q_FOREACH (QString line, lines) {
// if (!line.isEmpty())
// lastLine = line;
// }
for (int i=0; i<lines.length(); i++)
{
qDebug() << lines[i];
if (!lines[i].isEmpty())
lastLine = lines[i];
}
return lastLine;
}
/**
* @brief Return a string that is the name and path if required to the ADB executable.
* @return ADB executable (QString)
*/
inline QString MainWindow::getADB()
{
if (isAdbOnPath)
return "adb"; // TODO: Conditionally compile for different platforms
return adbExe;
}
/**
* @brief Get the IP addres of the VM.
* @return VM's IP address (QString)
*/
inline QString MainWindow::getIPAddr()
{
return ui->lineEdit_ipAddr->text();
}
/**
* @brief Get the name of the VM.
* @return VM's name (QString)
*/
inline QString MainWindow::getVMname()
{
return ui->lineEdit_vmName->text();
}
/**
* @brief Is the VM running?
* @return Running state of VM (boolean).
*/
bool MainWindow::isVMrunning()
{
// get list of running VMS:
QString program = "VBoxManage";
QStringList arguments;
arguments << "list" << "runningvms";
QProcess *theProcess = new QProcess(this);
theProcess->start(program, arguments);
theProcess->waitForFinished(-1);
// see if VM name is in results
if (theProcess->exitCode() == 0) {
QString p_stdout = theProcess->readAllStandardOutput();
if (p_stdout.contains("\"" + getVMname() + "\"", Qt::CaseSensitive))
return true;
}
return false;
}
// --------
// Settings
// --------
/**
* @brief Write all settings.
*/
void MainWindow::writeSettings()
{
writeSettingsVM();
writeSettingsExecutables();
writeWindowGeometry();
}
/**
* @brief Write settings related to Virtual Machine.
*/
void MainWindow::writeSettingsVM()
{
QSettings settings;
settings.beginGroup("vm");
settings.setValue("name", getVMname());
settings.setValue("ip_addr", getIPAddr());
settings.endGroup();
}
/**
* @brief Write settings related to executables.
*/
void MainWindow::writeSettingsExecutables()
{
QSettings settings;
settings.beginGroup("executables");
settings.setValue("adb_dir", adbExe);
settings.setValue("adb_on_path", isAdbOnPath);
settings.endGroup();
}
/**
* @brief Write settings related to this window's geometry.
*/
void MainWindow::writeWindowGeometry()
{
QSettings settings;
settings.beginGroup("mainwindow");
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
settings.endGroup();
}
/**
* @brief Read all settings.
*/
void MainWindow::readSettings()
{
readSettingsVM();
readSettingsExecutables();
readSettingsGeometry();
}
/**
* @brief Read settings related to Virtual Machine.
*/
void MainWindow::readSettingsVM()
{
QSettings settings;
settings.beginGroup("vm");
ui->lineEdit_vmName->setText(settings.value("name").toString());
ui->lineEdit_ipAddr->setText(settings.value("ip_addr").toString());
settings.endGroup();
}
/**
* @brief Read settings related to executables.
*/
void MainWindow::readSettingsExecutables()
{
QSettings settings;
settings.beginGroup("executables");
adbExe = settings.value("adb_dir").toString();
isAdbOnPath = settings.value("adb_on_path").toBool();
settings.endGroup();
}
/**
* @brief Read settings related to this window's geometry.
*/
void MainWindow::readSettingsGeometry()
{
QSettings settings;
settings.beginGroup("mainwindow");
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("windowState").toByteArray());
settings.endGroup();
}
/**
* @brief Configure the system tray icon.
*/
void MainWindow::configureTrayIcon()
{
//TODO: Popup status bar action messages (i.e., status bar messages) when main UI is hidden
QMenu *trayIconMenu = new QMenu(this);
trayIconMenu->addAction(ui->actionRun_emulator);
trayIconMenu->addAction(ui->actionACPI_shutdown);
trayIconMenu->addAction(ui->actionConnect);
trayIconMenu->addAction(ui->actionDisconnect);
trayIconMenu->addSeparator();
trayIconMenu->addAction(ui->actionQuit);
trayIcon = new QSystemTrayIcon(this->windowIcon(), this);
trayIcon->setToolTip(this->windowTitle());
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->show();
}