Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <string>
- #include <ctime>
- #include <thread>
- #include <chrono>
- #include <fstream>
- #include <sstream>
- #include <vector>
- namespace time_constant
- {
- const std::string days[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
- constexpr unsigned short day = 0, warn_time = 1, start_time = 2, end_time = 3, warn_title = 4, warn_msg = 5, start_title = 6, start_msg = 7, end_title = 8,
- end_msg = 9, warning_title = 10, warning_msg = 11, progress_title = 12, progress_msg = 13;
- constexpr unsigned short warn = 0, start = 1, end = 2;
- const std::string cmd = "notify-send";
- }
- void load_events(std::vector<std::vector<std::string>> &);
- void event_loop(std::vector<std::vector<std::string>> &, const bool);
- int main(int argc, char *argv[])
- {
- if (argc != 2) return -1;
- bool loop = false;
- {
- std::string arg = argv[1];
- if (arg == "loop")
- {
- loop = true;
- std::system((time_constant::cmd + " \"Event Loop\" \"Event loop has started.\"").c_str());
- }
- else if (arg == "now") loop = false;
- else return -1;
- }
- std::vector<std::vector<std::string>> events;
- load_events(events);
- event_loop(events, loop);
- return 0;
- }
- void load_events(std::vector<std::vector<std::string>> &events)
- {
- std::ifstream times("notify-time-conf");
- std::string line;
- while (std::getline(times, line))
- {
- std::stringstream stream(line);
- std::vector<std::string> event;
- std::string word;
- while (stream >> word)
- {
- if (word.find('+') != std::string::npos)
- for (char &c : word)
- if (c == '+') c = ' ';
- event.push_back(word);
- }
- events.push_back(event);
- }
- times.close();
- }
- void event_loop(std::vector<std::vector<std::string>> &events, const bool loop)
- {
- do
- {
- std::time_t t = std::time(0);
- struct tm *now = std::localtime(&t);
- const unsigned daily_time = now->tm_hour * 3600 + now->tm_min * 60 + now->tm_sec;
- std::string weekday = time_constant::days[now->tm_wday];
- for (const std::vector<std::string> event : events)
- {
- if (event[time_constant::day] == weekday)
- {
- const std::string time_parse[3] = { event[time_constant::warn_time], event[time_constant::start_time], event[time_constant::end_time] };
- unsigned time[3] = { 0 };
- {
- unsigned short index = 0;
- for (const std::string s : time_parse)
- {
- std::string hour = s.substr(0, 2), minute = s.substr(3, 2);
- if (hour[0] == '0') hour.erase(0,1);
- if (minute[0] == '0') minute.erase(0,1);
- time[index] = std::stoul(hour) * 3600 + std::stoul(minute) * 60;
- ++index;
- }
- }
- if (loop)
- {
- if (daily_time == time[time_constant::warn])
- std::system((time_constant::cmd + " \"" + event[time_constant::warn_title] + "\" " + "\"" + event[time_constant::warn_msg] + "\" ").c_str());
- else if (daily_time == time[time_constant::start])
- std::system((time_constant::cmd + " \"" + event[time_constant::start_title] + "\" " + "\"" + event[time_constant::start_msg] + "\" ").c_str());
- else if (daily_time == time[time_constant::end])
- std::system((time_constant::cmd + " \"" + event[time_constant::end_title] + "\" " + "\"" + event[time_constant::end_msg] + "\" ").c_str());
- }
- else
- {
- if (daily_time >= time[time_constant::warn] && daily_time < time[time_constant::start])
- std::system((time_constant::cmd + " \"" + event[time_constant::warning_title] + "\" " + "\"" + event[time_constant::warning_msg] + "\" ").c_str());
- else if (daily_time >= time[time_constant::start] && daily_time < time[time_constant::end])
- std::system((time_constant::cmd + " \"" + event[time_constant::progress_title] + "\" " + "\"" + event[time_constant::progress_msg] + "\" ").c_str());
- }
- }
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- while (loop);
- }
Add Comment
Please, Sign In to add comment