-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.cpp
More file actions
535 lines (433 loc) · 19.7 KB
/
UserInterface.cpp
File metadata and controls
535 lines (433 loc) · 19.7 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
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
#include <fmt/ranges.h>
#include <iostream>
#include <algorithm>
#include "UserInterface.h"
#include "Passwords.h"
#include "Files.h"
#include "Operations.h"
void UI::addCategory(){
fmt::println("Wprowadź nową kategorię:");
auto success = false;
auto newCategory = std::string();
while (!success) {
std::getline(std::cin, newCategory);
success = true;
if (newCategory.find(',') != std::string::npos){
fmt::println("Kategoria zawiera przecinek,"
" wykorzystywania takiej kategorii spowoduje błędne działanie programu.");
success = false;
}
}
categories.emplace_back(newCategory);
fmt::println("Kategoria została dodana");
}
std::string UI::getCategoryToBeRemoved(){
auto toRemove = std::string();
auto action = int();
do{
fmt::println("Wybierz kategorię, którą chcesz usunąć:");
for (auto i = 0; i < categories.size(); i++)
fmt::println("[{}]: {}", i + 1, categories.at(i));
fmt::println("[{}]: Anulować operację", categories.size() + 1);
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while (action < 1 || action > categories.size() + 1);
if (action == categories.size() + 1)
toRemove.clear();
else
toRemove = categories.at(action - 1);
return toRemove;
}
std::vector<Password> UI::getPasswordsToBeRemoved(const std::vector<Password> &passwords){
auto action = -1;
auto toRemove = std::vector<Password>();
auto passwordsSize = passwords.size();
while (action != passwordsSize + 1 && action != passwordsSize + 2) {
fmt::println("========================");
fmt::println("Wybrane hasła do usunięcia:");
for (const auto &pass : toRemove)
fmt::println("{}", pass.toString());
fmt::println("========================");
fmt::println("Wybierz hasło(-a), które chcesz usunąć:");
for (int i = 0; i < passwordsSize; i++) {
const auto &str = passwords.at(i).toString();
fmt::println("[{}]: {}", i + 1, str);
}
fmt::println("[{}]: Anulować i wrócić", passwordsSize + 1);
fmt::println("[{}]: Zatwierdzić i usunąć", passwordsSize + 2);
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (action < 1 || action > passwordsSize + 2)
fmt::println("Wybrane niepoprawne hasło.");
else if (action != passwordsSize + 1 && action != passwordsSize + 2) {
const auto &candidate = passwords.at(action - 1);
if (std::ranges::find(toRemove, candidate) == toRemove.end())
toRemove.push_back(candidate);
}
}
if (action == passwordsSize + 1)
toRemove.clear();
return toRemove;
}
void UI::displayActionsForFile(const std::string &filePath){
auto action = int();
while (action != 12) {
fmt::println("Plik -> {}", filePath);
auto stringRepresentation = File::readFromFile(filePath);
auto passwordRepresentation = Password::castToPasswords(stringRepresentation);
fmt::println("Wybierz opcje:");
fmt::println("[1]: Pokazać zawartości");
fmt::println("[2]: Zaszyfrować plik");
fmt::println("[3]: Odszyfrować plik");
fmt::println("[4]: Odszukać hasła");
fmt::println("[5]: Posortować hasła");
fmt::println("[6]: Dodać hasło");
fmt::println("[7]: Usunąć hasło");
fmt::println("[8]: Edytować hasło");
fmt::println("[9]: Pokazać kategorię haseł");
fmt::println("[10]: Dodać kategorię");
fmt::println("[11]: Usunąć kategorię");
fmt::println("[12]: Wrócić na początkowy ekran");
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (action < 1 || action > 12){
fmt::println("Wybrana niepoprawna opcja.");
continue;
}
else if (action == 12)
return;
else if (action == 1) {
fmt::println("==================================");
for (const std::string &s: stringRepresentation)
fmt::println("{}", s);
fmt::println("==================================");
}
else if (action == 2) {
auto password = std::string();
fmt::println("Wprowadź hasło dla zaszyfrowania:");
std::cin >> password;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
int key = Operations::generateKeyForEncoding(password);
for (auto &str: stringRepresentation)
Operations::encodeOrDecode(str, key, true);
fmt::println("Plik został zaszyfrowany.");
File::saveToFile(filePath, stringRepresentation);
}
else if (action == 3) {
auto password = std::string();
fmt::println("Wprowadź hasło dla odszyfrowania:");
std::cin >> password;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
int key = Operations::generateKeyForEncoding(password);
for (auto &str: stringRepresentation)
Operations::encodeOrDecode(str, key, false);
fmt::println("Plik został odszyfrowany.");
File::saveToFile(filePath, stringRepresentation);
}
else if (action == 4) {
const auto availableParameters =
std::vector<std::string>{"nazwa", "hasło", "kategoria", "login", "strona"};
fmt::println("==================================");
auto chosenParameters = UI::getParametersForSearching(availableParameters);
if (!chosenParameters.empty()) {
auto passwordsThatMatch =
Operations::findMatches(passwordRepresentation, chosenParameters);
if (passwordsThatMatch.empty())
fmt::println("Brak wyników wyszukiwania.");
else {
fmt::println("==================================");
fmt::println("Wyniki wyszukiwania:");
for (const auto pass: passwordsThatMatch)
fmt::println("{}", pass->toString());
fmt::println("==================================");
}
}
}
else if (action == 5) {
const auto availableParameters =
std::vector<std::string>{"nazwa", "hasło", "kategoria", "login", "strona"};
auto chosenParameters = std::vector<std::string>();
fmt::println("==================================");
bool proceed = UI::getParametersForSorting(chosenParameters, availableParameters);
if (proceed) {
std::ranges::sort(passwordRepresentation,
[chosenParameters](const Password &one, const Password &another) -> bool {
return Operations::comparatorByParameters(one,
another, chosenParameters);
});
UI::updateStringRepresentation(stringRepresentation, passwordRepresentation);
File::saveToFile(filePath, stringRepresentation);
fmt::println("Hasła zostały posortowane");
fmt::println("==================================");
}
}
else if (action == 6) {
fmt::println("==================================");
Operations::addPassword(passwordRepresentation);
stringRepresentation.push_back(passwordRepresentation.back().toString());
File::saveToFile(filePath, stringRepresentation);
}
else if (action == 7) {
const auto &toRemove =
UI::getPasswordsToBeRemoved(passwordRepresentation);
if (!toRemove.empty()) {
bool acceptedOrDeclined = false;
while (!acceptedOrDeclined) {
fmt::println("============UWAGA============");
fmt::println("Zamierzasz usunąć następujące hasła:");
for (const auto &pass: toRemove)
fmt::println("{}", pass.toString());
fmt::println("Czy chcesz kontynuować?");
fmt::println("[1]: Anulować operację");
fmt::println("[2]: Zatwierdzić i usunąć");
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (action == 1 || action == 2) acceptedOrDeclined = true;
}
if (action == 1)
fmt::println("Operacja została anulowana");
else {
Operations::removePasswords(passwordRepresentation, toRemove);
UI::updateStringRepresentation(stringRepresentation, passwordRepresentation);
File::saveToFile(filePath, stringRepresentation);
}
}
}
else if (action == 8) {
fmt::println("==================================");
bool saveChanges = Password::editPassword(passwordRepresentation);
if (saveChanges) {
fmt::println("Zmiany zostały zapisane.");
UI::updateStringRepresentation(stringRepresentation, passwordRepresentation);
File::saveToFile(filePath, stringRepresentation);
}
else
fmt::println("Zmiany zostały usunięte.");
}
else if (action == 9){
fmt::println("==================================");
fmt::println("Kategorie haseł:");
for (const auto &cat : categories)
fmt::println("{}", cat);
fmt::println("==================================");
}
else if (action == 10) {
fmt::println("==================================");
UI::addCategory();
}
else if (action == 11){
fmt::println("==================================");
if (categories.size() == 1){
fmt::println("Ostatnia kategoria nie może być usunięta.");
continue;
}
auto toRemove = UI::getCategoryToBeRemoved();
if (toRemove.empty()) {
fmt::println("Operacja anulowana");
continue;
}
auto matchesPointers = Operations::findMatches(passwordRepresentation,
std::map<std::string, std::string>{{"kategoria", toRemove}});
if (!matchesPointers.empty()) {
fmt::println("============UWAGA============");
fmt::println("Usuwając tę kategorię, usuniesz również następujące hasła:");
for (const auto pass: matchesPointers)
fmt::println("{}", pass->toString());
fmt::println("Czy chcesz kontynuować?");
fmt::println("[1]: kontynuować");
fmt::println("[2]: anulować");
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else action = 1;
if (action == 1){
categories.erase(
std::remove(categories.begin(), categories.end(), toRemove),
categories.end());
auto matches = std::vector<Password>();
for (const auto &pass : matchesPointers)
matches.emplace_back(*pass);
Operations::removePasswords(passwordRepresentation, matches);
UI::updateStringRepresentation(stringRepresentation, passwordRepresentation);
File::saveToFile(filePath, stringRepresentation);
fmt::println("Kategoria zostałą usunięta");
}
else
fmt::println("Operacja anulowana.");
}
}
}
void UI::updateStringRepresentation(std::vector<std::string> &str, const std::vector<Password> &pass){
str.clear();
for (const auto &p : pass)
str.emplace_back(p.toString());
}
void UI::getParametersForRandomPassword(int &length, bool &hasUpperCase,
bool &hasLowerCase, bool &hasSpecial){
bool success = false;
auto action = std::string();
while (!success) {
fmt::println("Wybierz długość hasła (min.5)");
std::cin >> length;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (length < 5)
fmt::println("Hasło o takiej długości nie może być wygenerowane.");
else success = true;
}
success = false;
while (!success) {
fmt::println("Czy chesz, żeby hasło zawierało duże litery?(\"tak\"/\"nie\")");
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
success = true;
if (action == "nie")
hasUpperCase = false;
else if (action == "tak")
hasUpperCase = true;
else {
fmt::println("Wprowadzona niepoprawna komenda.");
success = false;
}
}
success = false;
while (!success) {
fmt::println("Czy chesz, żeby hasło zawierało małe litery?(\"tak\"/\"nie\")");
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
success = true;
if (action == "nie")
hasLowerCase = false;
else if (action == "tak")
hasLowerCase = true;
else {
fmt::println("Wprowadzona niepoprawna komenda.");
success = false;
}
}
success = false;
while (!success) {
fmt::println("Czy chesz, żeby hasło zawierało znaki specjalne?(\"tak\"/\"nie\")");
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
success = true;
if (action == "nie")
hasSpecial = false;
else if (action == "tak")
hasSpecial = true;
else {
fmt::println("Wprowadzona niepoprawna komenda.");
success = false;
}
}
if (!hasLowerCase && !hasUpperCase && !hasSpecial){
fmt::println("Hasło o takich parametrych nie może być wygenerowane, hasło musi co najmniej zawierać\n"
"małe litery. Zostanie wygenerowane hasło zawierające tylko małe litery");
hasLowerCase = true;
}
}
bool UI::getParametersForSorting(std::vector<std::string> &chosenParameters,
const std::vector<std::string> &availableParameters){
fmt::println("Wybierz parametry według których chcesz posortować hasła z pliku");
auto action = 0;
while (action != availableParameters.size() + 1 && action != availableParameters.size() + 2){
fmt::println("Wybrane parametry: {}", chosenParameters);
for (int i = 0; i < availableParameters.size(); i++)
fmt::println("[{}]: {}", i + 1, availableParameters.at(i));
fmt::println("[{}]: Anulować i wrócić do pliku", availableParameters.size() + 1);
fmt::println("[{}]: Posortować według wybranych parametrów", availableParameters.size() + 2);
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (action > 0 && action < availableParameters.size() + 1){
const std::string &chosen = availableParameters.at(action - 1);
bool notAddedYet =
std::ranges::find(chosenParameters, chosen) == chosenParameters.end();
if (notAddedYet)
chosenParameters.push_back(chosen);
else
chosenParameters.erase(std::remove(chosenParameters.begin(),
chosenParameters.end(),
chosen),
chosenParameters.end());
}
else if (action != availableParameters.size() + 1 && action != availableParameters.size() + 2)
fmt::println("Wybrany błędny parametr.");
}
if (action == availableParameters.size() + 1)
return false;
else if (action == availableParameters.size() + 2)
return true;
else {
fmt::println("Operacja anulowana");
return false;
}
}
std::map<std::string, std::string> UI::getParametersForSearching(
const std::vector<std::string> &availableParameters){
auto chosenParameters = std::map<std::string, std::string>();
auto action = 0;
fmt::println("Wybierz parametry, według których chcesz dokonać wyszukiwania");
while (action != availableParameters.size() + 1 && action != availableParameters.size() + 2){
fmt::println("Wybrane parametry: {}", chosenParameters);
for (int i = 0; i < availableParameters.size(); i++)
fmt::println("[{}]: {}", i + 1, availableParameters.at(i));
fmt::println("[{}]: Wrócić do pliku", availableParameters.size() + 1);
fmt::println("[{}]: Wyszukać hasła według wybranych parametrów",
availableParameters.size() + 2);
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (action > 0 && action < availableParameters.size() + 1){
const std::string &chosenOption = availableParameters.at(action - 1);
bool notAddedYet = !chosenParameters.contains(chosenOption);
if (notAddedYet){
auto chosenText = std::string();
if (chosenOption != "kategoria") {
fmt::println("Wprowadź tekst, który będzie wykorzystywany podczas wyszukiwania");
std::getline(std::cin, chosenText);
}
else{
auto chosenProperly = false;
while (!chosenProperly) {
fmt::println("Wybierz kategorię:");
for (auto i = 0; i < categories.size(); i++)
fmt::println("[{}]: {}", i + 1, categories.at(i));
std::cin >> action;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (action < 1 || action > categories.size()) {
fmt::println("Wybrana niepoprawna kategoria");
continue;
}
else{
chosenText = categories.at(action - 1);
chosenProperly = true;
}
}
}
chosenParameters.insert({chosenOption, chosenText});
}
else
chosenParameters.erase(chosenOption);
}
}
if (action == availableParameters.size() + 1)
chosenParameters.clear();
else if (action != availableParameters.size() + 2){
fmt::println("Operacja anulowana.");
chosenParameters.clear();
}
return chosenParameters;
}