Guest User

C++ timed events notifier

a guest
Mar 31st, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <string>
  3. #include <ctime>
  4. #include <thread>
  5. #include <chrono>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <vector>
  9.  
  10. namespace time_constant
  11. {
  12.     const std::string days[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  13.     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,
  14.                              end_msg = 9, warning_title = 10, warning_msg = 11, progress_title = 12, progress_msg = 13;
  15.     constexpr unsigned short warn = 0, start = 1, end = 2;
  16.     const std::string cmd = "notify-send";
  17. }
  18.  
  19. void load_events(std::vector<std::vector<std::string>> &);
  20. void event_loop(std::vector<std::vector<std::string>> &, const bool);
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.     if (argc != 2) return -1;
  25.     bool loop = false;
  26.     {
  27.         std::string arg = argv[1];
  28.         if (arg == "loop")
  29.         {
  30.             loop = true;
  31.             std::system((time_constant::cmd + " \"Event Loop\" \"Event loop has started.\"").c_str());
  32.         }
  33.         else if (arg == "now") loop = false;
  34.         else return -1;
  35.     }
  36.    
  37.     std::vector<std::vector<std::string>> events;
  38.     load_events(events);
  39.    
  40.     event_loop(events, loop);
  41.    
  42.     return 0;
  43. }
  44.  
  45. void load_events(std::vector<std::vector<std::string>> &events)
  46. {
  47.     std::ifstream times("notify-time-conf");
  48.     std::string line;
  49.     while (std::getline(times, line))
  50.     {
  51.         std::stringstream stream(line);
  52.         std::vector<std::string> event;
  53.         std::string word;
  54.         while (stream >> word)
  55.         {
  56.             if (word.find('+') != std::string::npos)
  57.                 for (char &c : word)
  58.                     if (c == '+') c = ' ';
  59.             event.push_back(word);
  60.         }
  61.         events.push_back(event);
  62.     }
  63.     times.close();
  64. }
  65.  
  66. void event_loop(std::vector<std::vector<std::string>> &events, const bool loop)
  67. {
  68.     do
  69.     {
  70.         std::time_t t = std::time(0);
  71.         struct tm *now = std::localtime(&t);
  72.         const unsigned daily_time = now->tm_hour * 3600 + now->tm_min * 60 + now->tm_sec;
  73.         std::string weekday = time_constant::days[now->tm_wday];
  74.        
  75.         for (const std::vector<std::string> event : events)
  76.         {
  77.             if (event[time_constant::day] == weekday)
  78.             {
  79.                 const std::string time_parse[3] = { event[time_constant::warn_time], event[time_constant::start_time], event[time_constant::end_time] };
  80.                 unsigned time[3] = { 0 };
  81.                 {
  82.                     unsigned short index = 0;
  83.                     for (const std::string s : time_parse)
  84.                     {
  85.                         std::string hour = s.substr(0, 2), minute = s.substr(3, 2);
  86.                         if (hour[0] == '0') hour.erase(0,1);
  87.                         if (minute[0] == '0') minute.erase(0,1);
  88.                         time[index] = std::stoul(hour) * 3600 + std::stoul(minute) * 60;
  89.                         ++index;       
  90.                     }
  91.                 }
  92.                 if (loop)
  93.                 {
  94.                     if (daily_time == time[time_constant::warn])
  95.                         std::system((time_constant::cmd + " \"" + event[time_constant::warn_title] + "\" " + "\"" + event[time_constant::warn_msg] + "\" ").c_str());
  96.                     else if (daily_time == time[time_constant::start])
  97.                         std::system((time_constant::cmd + " \"" + event[time_constant::start_title] + "\" " + "\"" + event[time_constant::start_msg] + "\" ").c_str());
  98.                     else if (daily_time == time[time_constant::end])
  99.                         std::system((time_constant::cmd + " \"" + event[time_constant::end_title] + "\" " + "\"" + event[time_constant::end_msg] + "\" ").c_str());
  100.                 }
  101.                 else
  102.                 {
  103.                    
  104.                     if (daily_time >= time[time_constant::warn] && daily_time < time[time_constant::start])
  105.                         std::system((time_constant::cmd + " \"" + event[time_constant::warning_title] + "\" " + "\"" + event[time_constant::warning_msg] + "\" ").c_str());
  106.                     else if (daily_time >= time[time_constant::start] && daily_time < time[time_constant::end])
  107.                         std::system((time_constant::cmd + " \"" + event[time_constant::progress_title] + "\" " + "\"" + event[time_constant::progress_msg] + "\" ").c_str());
  108.                 }
  109.             }
  110.         }
  111.         std::this_thread::sleep_for(std::chrono::seconds(1));
  112.     }
  113.     while (loop);
  114. }
Add Comment
Please, Sign In to add comment