Advertisement
Guest User

event.cpp

a guest
Apr 16th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <cstdlib>
  4.  
  5. #include "notify_time.h"
  6. #include "event.h"
  7.  
  8. const unsigned event_t::parse_time(const std::string& time) const
  9. {
  10.     std::string hr = time.substr(0, 2), min = time.substr(3, 2);
  11.     if (hr.front() == '0') hr.erase(0, 1);
  12.     if (min.front() == '0') min.erase(0, 1);
  13.     return std::stoul(hr) * 3600 + std::stoul(min) * 60;
  14. }
  15.  
  16. const std::string event_t::unparse_notif(std::string title, std::string msg) const
  17. {
  18.     find_replace(title, "+", " ");
  19.     find_replace(msg, "+", " ");
  20.     return compose_notif(title, msg);
  21. }
  22.  
  23. const std::string& event_t::find_replace(std::string& str, const std::string& search, const std::string& replace) const
  24. {
  25.     if (search == replace) return str;
  26.  
  27.     const std::string::size_type diff = replace.length() - search.length() + 1;
  28.  
  29.     for (std::string::size_type pos = 0; pos != std::string::npos; )
  30.     {
  31.         pos = str.find(search, pos);
  32.         if (pos != std::string::npos)
  33.         {
  34.             str.replace(pos, search.length(), replace);
  35.             pos += diff;
  36.         }
  37.     }
  38.     return str;
  39. }
  40.  
  41. const std::string event_t::compose_notif(const std::string& title, const std::string& msg) const
  42. {
  43.     return "\"" + title + "\" \"" + msg + "\"";
  44. }
  45.  
  46. const char* event_t::unparse_cmd(const std::string& notif) const
  47. {
  48.     return (time_constant::cmd + " " + notif).c_str();
  49. }
  50.  
  51. event_t::event_t()
  52. {
  53.  
  54. }
  55.  
  56. event_t::event_t(const std::vector<std::string>& event)
  57. {
  58.     load_event(event);
  59. }
  60.  
  61. void event_t::load_event(const std::vector<std::string>& event)
  62. {
  63.     weekday = event.front();
  64.  
  65.     warn_time = parse_time(event[1]);
  66.     start_time = parse_time(event[2]);
  67.     end_time = parse_time(event[3]);
  68.  
  69.     warn_notif = unparse_notif(event[4], event[5]);
  70.     start_notif = unparse_notif(event[6], event[7]);
  71.     end_notif = unparse_notif(event[8], event[9]);
  72.     about_to_start_notif = unparse_notif(event[10], event[11]);
  73.     in_progress_notif = unparse_notif(event[12], event[13]);
  74. }
  75.  
  76. void send_notif(const event_t& event, const unsigned short option)
  77. {
  78.     std::string notif;
  79.     switch (option)
  80.     {
  81.         case time_constant::warn_notif:
  82.             notif = event.notif(time_constant::warn_notif);
  83.             break;
  84.  
  85.         case time_constant::start_notif:
  86.             notif = event.notif(time_constant::start_notif);
  87.             break;
  88.  
  89.         case time_constant::end_notif:
  90.             notif = event.notif(time_constant::end_notif);
  91.             break;
  92.  
  93.         case time_constant::about_to_start_notif:
  94.             notif = event.notif(time_constant::about_to_start_notif);
  95.             break;
  96.  
  97.         case time_constant::in_progress_notif:
  98.             notif = event.notif(time_constant::in_progress_notif);
  99.             break;
  100.  
  101.         default:
  102.             return;
  103.     }
  104.     std::system(event.unparse_cmd(notif));
  105. }
  106.  
  107. void send_notif(const std::string& notif)
  108. {
  109.     std::system(event_t().unparse_cmd(notif));
  110. }
  111.  
  112. const std::string& event_t::day() const
  113. {
  114.     return weekday;
  115. }
  116.  
  117. const unsigned event_t::time(const unsigned short option) const
  118. {
  119.     switch (option)
  120.     {
  121.         case time_constant::warn_time:
  122.             return warn_time;
  123.             break;
  124.  
  125.         case time_constant::start_time:
  126.             return start_time;
  127.             break;
  128.  
  129.         case time_constant::end_time:
  130.             return end_time;
  131.             break;
  132.  
  133.         default:
  134.             return -1;
  135.     }
  136. }
  137.  
  138. const std::string event_t::notif(const unsigned short option) const
  139. {
  140.     switch (option)
  141.     {
  142.         case time_constant::warn_notif:
  143.             return warn_notif;
  144.             break;
  145.  
  146.         case time_constant::start_notif:
  147.             return start_notif;
  148.             break;
  149.  
  150.         case time_constant::end_notif:
  151.             return end_notif;
  152.             break;
  153.  
  154.         case time_constant::about_to_start_notif:
  155.             return about_to_start_notif;
  156.             break;
  157.  
  158.         case time_constant::in_progress_notif:
  159.             return in_progress_notif;
  160.             break;
  161.  
  162.         default:
  163.             return "";
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement