You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.9 KiB

  1. #include <ical/Alarm.h>
  2. #include <libical/icalproperty_cxx.h>
  3. #include <db/db.h>
  4. namespace ical {
  5. Alarm::Alarm(icalcomponent* alarm_component, const uint32_t& event_dtstart) : m_event_dtstart(event_dtstart) {
  6. parse(alarm_component);
  7. }
  8. void Alarm::parse(icalcomponent* alarm_component) {
  9. parse_action(alarm_component);
  10. parse_description(alarm_component);
  11. parse_trigger(alarm_component);
  12. }
  13. void Alarm::parse_action(icalcomponent* alarm_component) {
  14. std::string action = icalproperty_get_value_as_string(icalcomponent_get_first_property(alarm_component, ICAL_ACTION_PROPERTY));
  15. if(action == "DISPLAY") m_action = ical::action::DISPLAY;
  16. else if(action == "AUDIO") m_action = ical::action::AUDIO;
  17. else if(action == "EMAIL") m_action = ical::action::EMAIL;
  18. else if(action == "PROC" ) m_action = ical::action::PROC;
  19. else m_action = ical::action::DISPLAY;
  20. }
  21. void Alarm::parse_description(icalcomponent* alarm_component) {
  22. icalproperty* description_property = icalcomponent_get_first_property(alarm_component, ICAL_DESCRIPTION_PROPERTY);
  23. if(description_property != 0) {
  24. m_description = icalproperty_get_value_as_string(description_property);
  25. }
  26. }
  27. void Alarm::parse_trigger(icalcomponent* alarm_component) {
  28. icalproperty* trigger_property = icalcomponent_get_first_property(alarm_component, ICAL_TRIGGER_PROPERTY);
  29. struct icaltriggertype tr;
  30. if(trigger_property != 0) {
  31. tr = icalproperty_get_trigger(trigger_property);
  32. if (!icaltime_is_null_time(tr.time)) {
  33. m_trigger = ical::trigger_type::ABSOLUTE;
  34. } else {
  35. m_trigger = ical::trigger_type::RELATIVE;
  36. m_absolute_trigger = m_event_dtstart + icaldurationtype_as_int(tr.duration);
  37. }
  38. }
  39. }
  40. std::string Alarm::get_action() const {
  41. switch(m_action) {
  42. case ical::action::DISPLAY: return "display";
  43. case ical::action::AUDIO: return "audio";
  44. case ical::action::EMAIL: return "email";
  45. case ical::action::PROC: return "proc";
  46. default: return "";
  47. }
  48. return "";
  49. }
  50. std::string Alarm::get_trigger_type() const {
  51. switch(m_trigger) {
  52. case ical::trigger_type::ABSOLUTE: return "absolute";
  53. case ical::trigger_type::RELATIVE: return "relative";
  54. default: return "";
  55. }
  56. }
  57. //std::string Alarm::get_absolute_trigger() const {
  58. // return m_absolute_trigger;
  59. //}
  60. std::vector<std::string> Alarm::get_db_row() const {
  61. return {db_safe(get_action()),
  62. db_safe(m_description),
  63. db_safe(std::to_string(m_absolute_trigger))};
  64. }
  65. std::string Alarm::print() const {
  66. std::string s = "";
  67. //s += "DESCRIPTION: " + m_description + "\n";
  68. ////s += m_trigger == ical::trigger_type::ABSOLUTE ? "ABSOLUTE: " : "RELATIVE: ";
  69. //s += "ABSOLUTE_TRIGGER: " + std::to_string(m_absolute_trigger) + "\n";
  70. return s;
  71. }
  72. }