-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.cpp
131 lines (112 loc) · 4.69 KB
/
event.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
#include <string>
#include <algorithm>
#include <array>
#include <cstdlib>
#include "notify_time.hpp"
#include "event.hpp"
#include "event_size.hpp"
unsigned event_t::parse_time(const std::string& wday, const std::string& time) const // parse time in 24 hr format; e.g. 13:55, 07:08;
{
std::string hr{time.substr(0, 2)}, min{time.substr(3, 2)}; // e.g. time: 14:35 hr == 14; min == 35;
// get index of day and use in time calculation;
return (std::find(time_constant::days.cbegin(), time_constant::days.cend(), wday) - time_constant::days.cbegin()) * 86400 + std::stoull(hr) * 3600 + std::stoull(min) * 60;
// std::stoull ignores leading zero; e.g. time: 07:08 hr == 7; min == 8;
}
inline bool event_t::validate_times(const unsigned time1, const unsigned time2, const unsigned time3) const
{
if (time1 <= time2 && time2 <= time3) return true;
else return false;
}
std::string event_t::unparse_notif(std::string title, std::string msg) const
{
find_replace(title, "+", " ");
find_replace(msg, "+", " ");
return compose_notif(title, msg);
}
void event_t::find_replace(std::string& str, const std::string& search, const std::string& replace) const
{
if (search == replace) return;
const std::string::size_type diff{replace.length() - search.length() + 1};
for (std::string::size_type pos{0}; pos != std::string::npos; )
{
pos = str.find(search, pos);
if (pos != std::string::npos)
{
str.replace(pos, search.length(), replace);
pos += diff;
}
}
}
std::string event_t::compose_notif(const std::string& title, const std::string& msg) const
{
return "\"" + title + "\" \"" + msg + "\"";
}
std::string event_t::unparse_cmd(const std::string& notif) const
{
return time_constant::cmd + " " + notif;
}
event_t::event_t(const std::array<std::string, event_constant::size>& event)
{
load_event(event);
}
bool event_t::operator<(const event_t& event) const
{
return warn_time < event.warn_time;
}
void event_t::load_event(const std::array<std::string, event_constant::size>& event)
{
weekday = event.front();
warn_time = parse_time(weekday, event[1]);
start_time = parse_time(weekday, event[2]);
end_time = parse_time(weekday, event[3]);
if (!validate_times(warn_time, start_time, end_time)) throw std::make_pair<std::string, std::string>("Error", "Incorrect times.");
warn_notif = unparse_notif(event[4], event[5]);
start_notif = unparse_notif(event[6], event[7]);
end_notif = unparse_notif(event[8], event[9]);
about_to_start_notif = unparse_notif(event[10], event[11]);
in_progress_notif = unparse_notif(event[12], event[13]);
}
void send_notif(const event_t& event, const unsigned short option)
{
std::string notif;
switch (option)
{
case time_constant::warn_notif: notif = event.notif(time_constant::warn_notif); break;
case time_constant::start_notif: notif = event.notif(time_constant::start_notif); break;
case time_constant::end_notif: notif = event.notif(time_constant::end_notif); break;
case time_constant::about_to_start_notif: notif = event.notif(time_constant::about_to_start_notif); break;
case time_constant::in_progress_notif: notif = event.notif(time_constant::in_progress_notif); break;
default: return;
}
std::system(event.unparse_cmd(notif).c_str());
}
void send_notif(const std::string& title, const std::string& msg)
{
std::system(event_t().unparse_cmd("\"" + title + "\" \"" + msg + "\"").c_str());
}
void send_notif(const std::string ¬if)
{
std::system(event_t().unparse_cmd("\"" + notif + "\"").c_str());
}
unsigned event_t::time(const unsigned short option) const
{
switch (option)
{
case time_constant::warn_time: return warn_time; break;
case time_constant::start_time: return start_time; break;
case time_constant::end_time: return end_time; break;
default: return -1;
}
}
std::string event_t::notif(const unsigned short option) const
{
switch (option)
{
case time_constant::warn_notif: return warn_notif; break;
case time_constant::start_notif: return start_notif; break;
case time_constant::end_notif: return end_notif; break;
case time_constant::about_to_start_notif: return about_to_start_notif; break;
case time_constant::in_progress_notif: return in_progress_notif; break;
default: return "";
}
}