Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
335 lines (275 loc) · 12.8 KB
/
Copy pathmain.cpp
File metadata and controls
335 lines (275 loc) · 12.8 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
#include <ncurses.h>
#undef timeout
#include <vector>
#include "Menu.h"
#include "Download.h"
#include "Settings.h"
#include "DownloadManager.h"
#include "DownloadDisplayMenu.h"
#include "FileManager.h"
void initialize_ncurses() {
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
if (has_colors() == FALSE) {
endwin();
printf("Your terminal does not support color\n");
exit(1);
}
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
}
bool checkSettingsFileExists() {
std::ifstream file("../settings.txt"); // Change this to your actual settings file path
return file.good();
}
int main() {
initialize_ncurses();
Settings settings;
DownloadManager downloadManager;
FileManager fileManager;
const std::chrono::seconds cleanupInterval(5);
std::thread cleanupThread([&]() {
while (true) {
downloadManager.cleanupCompletedDownloads();
downloadManager.checkAndInitiateScheduledDownloads();
std::this_thread::sleep_for(cleanupInterval);
}
});
Menu mainMenu("WELCOME!\nChoose from menu by pressing enter\n",
{"Download", "Currently downloading", "History", "Download manager", "Schedule download","File manager","Exit"});
Menu scheduleMenu("Schedule download\nFill the inputs\n",
{"Enter hostname", "Enter save path", "Enter download path", "Enter username", "Enter password",
"Enter priority", "Enter protocol", "Enter time", "Submit schedule", "Back"});
Menu downloadMenu("Choose protocol\n", {"HTTP", "HTTPS", "FTP", "FTPS", "Back"});
Menu ftpCredentialsMenu("FTP Credentials\nEnter the following details:\n",
{"Hostname", "Path to file", "Port", "Username", "Password", "Download file from ftp",
"Back"});
Menu ftpsCredentialsMenu("FTPS Credentials\nEnter the following details:\n",
{"Hostname", "Path to file", "Port", "Username", "Password", "Download file from ftp",
"Back"});
Menu httpsMenu("HTTPS\nEnter the following details:\n",
{"Hostname", "Path to file", "Download file from https",
"Back"});
Menu httpMenu("HTTP\nEnter the following details:\n",
{"Hostname", "Path to file", "Download file from https",
"Back"});
DownloadDisplayMenu downloadDisplayMenu("Downloads\nPress esc to exit.\n", downloadManager);
Menu managerMenu("Download manager\n", {});
Menu downloadHistoryMenu("Download history\n", {});
Menu fileMenu("File Manager\n\n", {"Enter path: ","List Contents", "Remove Directory", "Create Directory", "Back"});
//download menu
downloadMenu.setAction(0, [&]() {
httpMenu.display();
});
downloadMenu.setAction(1, [&]() {
httpsMenu.display();
});
downloadMenu.setAction(2, [&]() {
ftpCredentialsMenu.display();
});
downloadMenu.setAction(3, [&]() {
ftpsCredentialsMenu.display();
});
downloadMenu.setAction(4, [&]() {
mainMenu.display();
});
//schedule menu
scheduleMenu.setInput(0, "Enter hostname");
scheduleMenu.setAction(0, [&]() { scheduleMenu.getInput(0); });
scheduleMenu.setInput(1, "Enter save path");
scheduleMenu.setAction(1, [&]() { scheduleMenu.getInput(1); });
scheduleMenu.setInput(2, "Enter download path");
scheduleMenu.setAction(2, [&]() { scheduleMenu.getInput(2); });
scheduleMenu.setInput(3, "Enter username");
scheduleMenu.setAction(3, [&]() { scheduleMenu.getInput(3); });
scheduleMenu.setInput(4, "Enter password");
scheduleMenu.setAction(4, [&]() { scheduleMenu.getInput(4); });
scheduleMenu.setInput(5, "Enter priority");
scheduleMenu.setAction(5, [&]() { scheduleMenu.getInput(5); });
scheduleMenu.setInput(6, "Enter protocol");
scheduleMenu.setAction(6, [&]() { scheduleMenu.getInput(6); });
scheduleMenu.setInput(7, "Enter time - H:M");
scheduleMenu.setAction(7, [&]() { scheduleMenu.getInput(7); });
scheduleMenu.setAction(8, [&]() {
std::string priority = scheduleMenu.getInputAsString(5);
std::string username = scheduleMenu.getInputAsString(3);
std::string password = scheduleMenu.getInputAsString(4);
std::shared_ptr<Download> download = std::make_shared<Download>(
scheduleMenu.getInputAsString(6),
scheduleMenu.getInputAsString(0),
scheduleMenu.getInputAsString(1),
scheduleMenu.getInputAsString(2),
scheduleMenu.getInputAsString(3),
scheduleMenu.getInputAsString(4), 1,
scheduleMenu.getInputAsString(7));
downloadManager.addScheduledDownload(download);
std::cout << "Download scheduled" << std::endl;
});
scheduleMenu.setAction(9, [&]() {
mainMenu.display(); });
//ftp menu
ftpCredentialsMenu.setInput(0, "Enter Hostname");
ftpCredentialsMenu.setAction(0, [&]() { ftpCredentialsMenu.getInput(0); });
ftpCredentialsMenu.setInput(1, "Path to file");
ftpCredentialsMenu.setAction(1, [&]() { ftpCredentialsMenu.getInput(1); });
ftpCredentialsMenu.setInput(2, "Enter Port");
ftpCredentialsMenu.setAction(2, [&]() { ftpCredentialsMenu.getInput(2); });
ftpCredentialsMenu.setInput(3, "Enter Username");
ftpCredentialsMenu.setAction(3, [&]() { ftpCredentialsMenu.getInput(3); });
ftpCredentialsMenu.setInput(4, "Enter Password");
ftpCredentialsMenu.setAction(4, [&]() { ftpCredentialsMenu.getInput(4); });
// Set up actions for FTP credentials
ftpCredentialsMenu.setAction(5, [&]() {
std::shared_ptr<Download> download = std::make_shared<Download>("FTP", ftpCredentialsMenu.getInputAsString(0),
settings.getSavePath().c_str(), ftpCredentialsMenu.getInputAsString(1),
ftpCredentialsMenu.getInputAsString(3), ftpCredentialsMenu.getInputAsString(4), 1);
downloadManager.addDownload(download);
settings.saveDownload(download->getHostname(), download->getDownloadPath());
});
ftpCredentialsMenu.setAction(6, [&]() { downloadMenu.display(); });
//Ftps menu
ftpsCredentialsMenu.setInput(0, "Enter Hostname");
ftpsCredentialsMenu.setAction(0, [&]() { ftpCredentialsMenu.getInput(0); });
ftpsCredentialsMenu.setInput(1, "Path to file");
ftpsCredentialsMenu.setAction(1, [&]() { ftpCredentialsMenu.getInput(1); });
ftpsCredentialsMenu.setInput(2, "Enter Port");
ftpsCredentialsMenu.setAction(2, [&]() { ftpCredentialsMenu.getInput(2); });
ftpsCredentialsMenu.setInput(3, "Enter Username");
ftpsCredentialsMenu.setAction(3, [&]() { ftpCredentialsMenu.getInput(3); });
ftpsCredentialsMenu.setInput(4, "Enter Password");
ftpsCredentialsMenu.setAction(4, [&]() { ftpCredentialsMenu.getInput(4); });
// Set up actions for FTP credentials
ftpsCredentialsMenu.setAction(5, [&]() {
std::shared_ptr<Download> download = std::make_shared<Download>("FTPS", ftpCredentialsMenu.getInputAsString(0),
settings.getSavePath().c_str(), ftpCredentialsMenu.getInputAsString(1),
ftpCredentialsMenu.getInputAsString(3), ftpCredentialsMenu.getInputAsString(4), 1);
downloadManager.addDownload(download);
settings.saveDownload(download->getHostname(), download->getDownloadPath());
});
ftpsCredentialsMenu.setAction(6, [&]() { downloadMenu.display(); });
//Https menu
httpsMenu.setInput(0, "Enter Hostname");
httpsMenu.setAction(0, [&]() { httpsMenu.getInput(0); });
httpsMenu.setInput(1, "Path to file");
httpsMenu.setAction(1, [&]() { httpsMenu.getInput(1); });
httpsMenu.setAction(2, [&]() {
std::shared_ptr<Download> download = std::make_shared<Download>("HTTPS", httpsMenu.getInputAsString(0), settings.getSavePath().c_str(), httpsMenu.getInputAsString(1), "", "", 1);
downloadManager.addDownload(download);
settings.saveDownload(download->getHostname(), download->getDownloadPath());
});
httpsMenu.setAction(3, [&]() {
downloadMenu.display();
});
//Http menu
httpMenu.setInput(0, "Enter Hostname");
httpMenu.setAction(0, [&]() { httpsMenu.getInput(0); });
httpMenu.setInput(1, "Path to file");
httpMenu.setAction(1, [&]() { httpsMenu.getInput(1); });
httpMenu.setAction(2, [&]() {
std::shared_ptr<Download> download = std::make_shared<Download>("HTTP", httpsMenu.getInputAsString(0), settings.getSavePath().c_str(), httpsMenu.getInputAsString(1), "", "", 1);
downloadManager.addDownload(download);
settings.saveDownload(download->getHostname(), download->getDownloadPath());
});
httpMenu.setAction(3, [&]() {
downloadMenu.display();
});
//file manager
fileMenu.setInput(0, "Enter path: ");
fileMenu.setAction(0, [&]() { fileMenu.getInput(0); });
fileMenu.setAction(1, [&]() {
if (fileMenu.getInputAsString(0) == "") {
fileMenu.getInput(0);
} else {
fileManager.listDirectoryContents(fileMenu.getInputAsString(0));
}
});
fileMenu.setAction(2, [&]() {
if (fileMenu.getInputAsString(0) == "") {
fileMenu.getInput(0);
} else {
fileManager.removeDirectory(fileMenu.getInputAsString(0));
}
});
fileMenu.setAction(3, [&]() {
if (fileMenu.getInputAsString(0) == "") {
fileMenu.getInput(0);
} else {
fileManager.createDirectory(fileMenu.getInputAsString(0));
}
});
fileMenu.setAction(4, [&]() {
mainMenu.display();
});
//MAIN MENU
mainMenu.setAction(0, [&]() { downloadMenu.display(); });
mainMenu.setAction(1, [&]() { downloadDisplayMenu.display(); });
mainMenu.setAction(2, [&]() {
std::vector<std::string> history;
for (int i = 0; i < settings.loadHistory().size(); ++i) {
history.push_back(settings.loadHistory()[i]);
}
history.emplace_back("back");
downloadHistoryMenu.setOptions(history);
downloadHistoryMenu.setAction(history.size() - 1, [&]() { mainMenu.display(); });
downloadHistoryMenu.display();
});
//DOWNLOAD MANAGER
mainMenu.setAction(3, [&]() {
std::vector<std::vector<Menu>> managerSubMenus; // Create a vector to hold submenus for each download
std::vector<std::string> managerOptions;
for (int i = 0; i < downloadManager.getDownloads().size(); ++i) {
// Create a submenu for each download
managerSubMenus.push_back(std::vector<Menu>());
managerOptions.push_back(downloadManager.getDownloads()[i]->getDownloadPath());
managerSubMenus.back().emplace_back("Download Options\n",
std::vector<std::string>{"Pause", "Resume","Cancel", "Back"});
int index = i; // Capture the current index
// Set 'Pause' action
managerSubMenus.back().back().setAction(0, [&, index]() {
downloadManager.pauseDownload(index);
managerMenu.display(); // Display the manager menu again after action
});
// Set 'Resume' action
managerSubMenus.back().back().setAction(1, [&, index]() {
downloadManager.resumeDownload(index);
managerMenu.display();
});
// Set 'Cancell' action
managerSubMenus.back().back().setAction(2, [&, index]() {
downloadManager.cancelDownload(index);
managerMenu.display();
});
// Set 'Back' action
managerSubMenus.back().back().setAction(3, [&]() {
managerMenu.display();
});
}
// Setting up action for each download to open its submenu
for (int i = 0; i < managerOptions.size(); ++i) {
int index = i; // Capture the current index
managerMenu.setAction(index, [&, index]() {
managerSubMenus[index].back().display(); // Display the corresponding submenu
});
}
managerOptions.emplace_back("Back");
managerMenu.setAction(managerOptions.size() - 1, [&]() { mainMenu.display(); });
managerMenu.setOptions(managerOptions);
managerMenu.display();
});
mainMenu.setAction(4, [&]() {
scheduleMenu.display();
});
mainMenu.setAction(5, [&]() {
fileMenu.display();
});
mainMenu.setAction(6, [&]() {
endwin();
exit(0);
});
mainMenu.display();
endwin();
cleanupThread.join();
return 0;
}