-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShutdownManager.cpp
162 lines (134 loc) · 3.91 KB
/
ShutdownManager.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
#pragma once
#include <iostream>
#include <string>
#include <time.h>
#include "coutColors.h"
#include "XML_TaskController.h"
using namespace std;
void buildTask(string duration, int unitm, string mode);
void createTask();
void deleteTask();
void cancelSameDayTask();
int inputUnit();
int calcTime(int hourInput, int minInput, int secInput);
string inputRelTime();
string inputSpecificTime();
int main()
{
int input;
cout << "main mode:\n";
cout << "[1] create new task\n";
cout << "[2] cancel existing task\n";
cin >> input;
if (input == 2) {
deleteTask();
return 0;
}
createTask();
return 0;
}
void deleteTask() {
int input;
system("cls");
cout << "What do you want to cancel?\n";
cout << "[1] same day task\n";
cout << "[2] other (future) day task\n";
cin >> input;
switch (input) {
case 1: cancelSameDayTask(); break;
case 2: deleteOtherDayTask(); break;
default: cancelSameDayTask();
}
}
void createTask() {
system("cls");
int input;
cout << "task mode:\n";
cout << "shutdowns:\n";
cout << " [1] same day shutdown - countdown\n";
cout << " [2] same day shutdown - specific time\n";
cout << " [3] other day shutdown - specific time\n\n";
cout << "reboots:\n";
cout << " [4] same day reboot - countdown\n";
cout << " [5] same day reboot - specific time\n";
cout << " [6] other day reboot - specific time\n";
cin >> input;
switch (input) {
case 1: buildTask(inputRelTime(), inputUnit(), " -s -d p:0:0"); break;
case 2: buildTask(inputSpecificTime() , 1, " -s -d p:0:0"); break;
case 3: buildXML("-s"); break;
case 4: buildTask(inputRelTime(), inputUnit(), " -r -d p:0:0"); break;
case 5: buildTask(inputSpecificTime(), 1, " -r -d p:0:0"); break;
case 6: buildXML("-r"); break;
default: buildTask(inputRelTime(), inputUnit(), " -s -d p:0:0");
}
system("pause");
}
int inputUnit() {
int unitm;
system("cls");
int input;
cout << "unit mode:\n";
cout << "[1] seconds\n";
cout << "[2] minutes\n";
cout << "[3] hours\n";
cin >> input;
switch (input) {
case 1: unitm = 1; break;
case 2: unitm = 60; break;
case 3: unitm = 3600; break;
default: unitm = 60;
}
return unitm;
}
string inputRelTime() {
string duration;
system("cls");
cout << "duration: ";
cin >> duration;
return duration;
}
void buildTask(string duration, int unitm, string mode) {
duration = " -t " + to_string(stoi(duration) * unitm);
string command = "shutdown" + mode + duration;
system(command.c_str());
system("cls");
cout << GREEN << "created task successfully\n" << RESET;
}
void cancelSameDayTask() {
string s = "shutdown -a";
int output = system(s.c_str());
system("cls");
switch (output) {
case 0: cout << GREEN << "canceled task successfully\n" << RESET; break;
case 1116: cout << RED << "Error: " << RESET << "no task found\n"; break;
default: cout << RED << "Error: " << RESET << "unkown error\n";
}
system("pause");
}
string inputSpecificTime() {
int hourInput, minInput, secInput;
system("cls");
cout << "hour: ";
cin >> hourInput;
system("cls");
cout << "minute: ";
cin >> minInput;
system("cls");
cout << "second: ";
cin >> secInput;
system("cls");
return to_string(calcTime(hourInput, minInput, secInput));
}
int calcTime(int hourInput, int minInput, int secInput) {
time_t now = time(NULL);
struct tm timeinfo;
localtime_s(&timeinfo, &now);
int result = (3600 * (hourInput - timeinfo.tm_hour)) + (60 * (minInput - timeinfo.tm_min)) + (secInput - timeinfo.tm_sec);
if (result <= 0) {
cout << RED << "Error: given time is in the past. Please restart the programm\n" << RESET;
system("pause");
exit(-1);
}
return result;
}