Repository navigation
Expand file tree
/
Copy pathlogic.cpp
More file actions
96 lines (87 loc) · 3.02 KB
/
Copy pathlogic.cpp
File metadata and controls
96 lines (87 loc) · 3.02 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
#include "logic.h"
#include <string>
#include <iostream>
#include <sstream>
#if defined(_WIN32)
#define PLATFORM_NAME "windows"
#elif defined(__linux__) || defined(__linux) || defined(linux)
#define PLATFORM_NAME "linux"
#else
#define PLATFORM_NAME "unknown"
#endif
Logic::Logic(QRadioButton *radioButtonSeconds,
QRadioButton *radioButtonMinutes,
QRadioButton *radioButtonHours,
QLineEdit *inputFieldTime,
QObject *parent) : QObject(parent),
radioButtonSeconds(radioButtonSeconds),
radioButtonMinutes(radioButtonMinutes),
radioButtonHours(radioButtonHours),
inputFieldTime(inputFieldTime) {}
void Logic::onClickShutdown() {
while (true) {
// TODO: Error handling for empty strings
QString inputQString = inputFieldTime->text();
std::string inputString = inputQString.toStdString();
std::replace(inputString.begin(),
inputString.end(),
',', '.');
if (isNumber(inputString)) {
float inputFloat = std::stof(inputString);
char inputType = 0;
if (radioButtonSeconds->isChecked()) {
inputType = 'S';
} else if (radioButtonMinutes->isChecked()) {
inputType = 'M';
} else if (radioButtonHours->isChecked()) {
inputType = 'H';
}
float timeInSec;
switch (inputType) {
case 'S':
timeInSec = inputFloat;
break;
case 'M':
timeInSec = inputFloat * 60;
break;
case 'H':
timeInSec = inputFloat * 3600;
break;
default:
std::exit(-1);
}
std::string prompt;
if (std::string(PLATFORM_NAME) == "windows") {
prompt = "shutdown -s -t " + std::to_string((int)timeInSec);
} else if (std::string(PLATFORM_NAME) == "linux") {
prompt = "shutdown -h" + std::to_string((int)timeInSec);
} else {
std::exit(-2);
}
system(prompt.c_str());
inputFieldTime->clear();
break;
} else {
inputFieldTime->clear();
inputFieldTime->setPlaceholderText("Wrong input!");
break;
}
}
}
void Logic::onClickCancel() {
std::string prompt;
if (std::string(PLATFORM_NAME) == "windows") {
prompt = "shutdown -a";
} else if (std::string(PLATFORM_NAME) == "linux") {
prompt = "shutdown -c";
} else {
std::exit(-2);
}
system(prompt.c_str());
}
bool Logic::isNumber(const std::string& testedString) {
std::istringstream istrstream(testedString);
float f;
istrstream >> std::noskipws >> f;
return !istrstream.fail() && istrstream.eof();
}