-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
87 lines (75 loc) · 2.17 KB
/
mainwindow.cpp
File metadata and controls
87 lines (75 loc) · 2.17 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "common.h"
#include <QMessageBox>
#include <QProcess>
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow) {
ui->setupUi(this);
connect(ui->pushButtonShutdown,
&QPushButton::clicked,
this,
&MainWindow::onPushButtonShutdownClicked);
connect(ui->pushButtonCancel,
&QPushButton::clicked,
this,
&MainWindow::onCancelShutdownClicked);
}
MainWindow::~MainWindow() {
delete ui;
}
int MainWindow::m_getInputTime() {
int hours = ui->spinBoxHours->value();
int minutes = ui->spinBoxMinutes->value();
int seconds = ui->spinBoxSeconds->value();
return hours * 3600 + minutes * 60 + seconds;
}
int MainWindow::m_getSystemTime() {
return QTime::currentTime().msecsSinceStartOfDay() / 1000;
}
void MainWindow::onPushButtonShutdownClicked() {
QStringList args;
int input_time = m_getInputTime();
int current_time = m_getSystemTime();
int option = ui->comboBoxOption->currentIndex();
int mode = ui->comboBoxMode->currentIndex();
// "turn off" / "reboot"
switch (option) {
case 0: // turn off
args << "/s";
break;
case 1: // reboot
args << "/r";
break;
default:
return;
}
DEBUG("Args after option switch: " << args);
// "after" / "at"
int diff;
switch (mode) {
case 0: // after
diff = input_time;
break;
case 1: // at
diff = (input_time - current_time + 24 * 3600) % (24 * 3600);
if (diff == 0)
diff = 24 * 3600;
break;
default:
return;
}
args << "/t" << QString::number(diff);
DEBUG("Args after mode switch: " << args);
m_runCommand("shutdown", args);
}
void MainWindow::onCancelShutdownClicked() {
QStringList args{"/a"};
m_runCommand("shutdown", args);
}
void MainWindow::m_runCommand(const QString &command, const QStringList &args) {
DEBUG("Running " << command << args);
QProcess::startDetached(command, args);
}