-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifications.cpp
More file actions
95 lines (74 loc) · 3.02 KB
/
Notifications.cpp
File metadata and controls
95 lines (74 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
#include "../include/Notifications.h"
#include <fstream>
#include <ctime>
#include <iostream>
#include <iomanip>
#include "../include/Utils.h"
#include "../include/constants.h"
using namespace std;
void Notifications::createNotification(const string& content, int type) {
if (content.empty()) {
cout << "[-] Error: Empty notification content" << endl;
return;
}
if (type != NOTIFICATION_WARNING && type != NOTIFICATION_EMERGENCY) {
cout << "[-] Error: Invalid notification type" << endl;
return;
}
ofstream out("data/notifications.txt", ios::app);
if (!out) {
cout << "[-] Error: Could not open notifications file for writing" << endl;
return;
}
time_t now = time(nullptr);
if (now == -1) {
cout << "[-] Error: Failed to get current time" << endl;
out.close();
return;
}
out << now << "|" << type << "|" << content << "\n";
if (out.fail()) {
cout << "[-] Error: Failed to write notification" << endl;
}
out.close();
}
void Notifications::viewNotifications() {
ifstream in("data/notifications.txt");
if (!in) {
cout << "[-] No notifications found." << endl;
return;
}
bool hasNotifications = false;
string line;
cout << "=== Notifications ===" << endl;
cout << "┌═════════════════════════════════════════════════════════════════════════════════════════════════════════════════┐" << endl;
while (getline(in, line)) {
if (line.empty() || line[0] == '#') {
continue;
}
string* parts = nullptr;
parts = split(line, '|');
time_t t = atol(parts[0].c_str());
string timestamp = prettyprint(t);
string typeStr;
int type = atoi(parts[1].c_str());
if (type == NOTIFICATION_WARNING) {
typeStr = "Warning";
} else if (type == NOTIFICATION_EMERGENCY) {
typeStr = "Emergency";
} else {
cout << "[-] Warning: Unknown notification type" << endl;
delete[] parts;
continue;
}
string message = parts[2];
cout << "█ " << left << setw(12) << ((typeStr == "Warning") ? "(Warning)":"(Emergency)") << left << setw(100) << trim(message) << "█" << endl;
hasNotifications = true;
delete[] parts;
}
if (!hasNotifications) {
cout << "No notifications to display." << endl;
}
cout << "└═════════════════════════════════════════════════════════════════════════════════════════════════════════════════┘" << endl;
in.close();
}