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.

270 lines
9.2 KiB

  1. #include <util/calendar_parsing.h>
  2. #include <memory>
  3. #include <iomanip>
  4. #include <string>
  5. namespace util {
  6. std::vector<ical::IcalObject*> parse_cal_dir(std::string directory) {
  7. std::vector<ical::IcalObject*> objects;
  8. std::vector<icalcomponent*> components;
  9. uint counter = 0;
  10. DEBUG << "Parsing: " << directory << " for .ics files";
  11. for (boost::filesystem::directory_entry& entry : boost::filesystem::directory_iterator(directory)) {
  12. ical::IcalObject* object = new ical::IcalObject();
  13. parse_main_component(object, parse_ics_file(entry.path().generic_string()), entry.path().generic_string());
  14. objects.push_back(object);
  15. counter++;
  16. }
  17. DEBUG << "files parsed: " << counter;
  18. return objects;
  19. }
  20. icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component, const std::string &file) {
  21. icalcomponent* c = icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
  22. while(c != 0) {
  23. if(c != 0) {
  24. parse_component(object, c, file);
  25. }
  26. c = parse_main_component(object, c, file);
  27. if(c == 0) {
  28. c = icalcomponent_get_next_component(component, ICAL_ANY_COMPONENT);
  29. }
  30. }
  31. return c;
  32. }
  33. void parse_component(ical::IcalObject* object, icalcomponent* component, const std::string &file) {
  34. switch(icalcomponent_isa(component)) {
  35. case ICAL_VALARM_COMPONENT: break;
  36. case ICAL_NO_COMPONENT:
  37. {
  38. //DEBUG << "parsed component: ICAL_NO_COMPONENT";
  39. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  40. break;
  41. }
  42. case ICAL_ANY_COMPONENT:
  43. {
  44. //DEBUG << "parsed component: ICAL_ANY_COMPONENT";
  45. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  46. break;
  47. }
  48. case ICAL_XROOT_COMPONENT:
  49. {
  50. //DEBUG << "parsed component: ICAL_XROOT_COMPONENT";
  51. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  52. break;
  53. }
  54. case ICAL_XATTACH_COMPONENT:
  55. {
  56. //DEBUG << "parsed component: ICAL_XATTACH_COMPONENT";
  57. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  58. break;
  59. }
  60. case ICAL_VEVENT_COMPONENT:
  61. {
  62. //DEBUG << "parsed component: ICAL_VEVENT_COMPONENT";
  63. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  64. object->add_event(new ical::Event(component, file));
  65. break;
  66. }
  67. case ICAL_VTODO_COMPONENT:
  68. {
  69. //DEBUG << "parsed component: ICAL_VTODO_COMPONENT";
  70. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  71. break;
  72. }
  73. case ICAL_VJOURNAL_COMPONENT:
  74. {
  75. //DEBUG << "parsed component: ICAL_VJOURNAL_COMPONENT";
  76. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  77. break;
  78. }
  79. case ICAL_VCALENDAR_COMPONENT:
  80. {
  81. //DEBUG << "parsed component: ICAL_VCALENDAR_COMPONENT";
  82. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  83. break;
  84. }
  85. case ICAL_VAGENDA_COMPONENT:
  86. {
  87. //DEBUG << "parsed component: ICAL_VAGENDA_COMPONENT";
  88. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  89. break;
  90. }
  91. case ICAL_VFREEBUSY_COMPONENT:
  92. {
  93. //DEBUG << "parsed component: ICAL_VFREEBUSY_COMPONENT";
  94. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  95. break;
  96. }
  97. case ICAL_XAUDIOALARM_COMPONENT:
  98. {
  99. //DEBUG << "parsed component: ICAL_XAUDIOALARM_COMPONENT";
  100. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  101. break;
  102. }
  103. case ICAL_XDISPLAYALARM_COMPONENT:
  104. {
  105. //DEBUG << "parsed component: ICAL_XDISPLAYALARM_COMPONENT";
  106. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  107. break;
  108. }
  109. case ICAL_XEMAILALARM_COMPONENT:
  110. {
  111. //DEBUG << "parsed component: ICAL_XEMAILALARM_COMPONENT";
  112. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  113. break;
  114. }
  115. case ICAL_XPROCEDUREALARM_COMPONENT:
  116. {
  117. //DEBUG << "parsed component: ICAL_XPROCEDUREALARM_COMPONENT";
  118. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  119. break;
  120. }
  121. case ICAL_VTIMEZONE_COMPONENT:
  122. {
  123. //DEBUG << "parsed component: ICAL_VTIMEZONE_COMPONENT";
  124. DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  125. //icaltimezone* zone;
  126. //icalproperty *prop;
  127. //const char *tzid;
  128. //prop = icalcomponent_get_first_property(component, ICAL_TZID_PROPERTY);
  129. //tzid = icalproperty_get_tzid(prop);
  130. //zone->tzid = strdup(tzid);
  131. //zone->component = component;
  132. //zone->location = icaltimezone_get_location_from_vtimezone(component);
  133. //zone->tznames = icaltimezone_get_tznames_from_vtimezone(component);
  134. //DEBUG << icaltimezone_get_tzid(zone);
  135. //int offset = icaltimezone_get_utc_offset(zone, struct icaltimetype *tt, int *is_daylight)
  136. break;
  137. }
  138. case ICAL_XSTANDARD_COMPONENT:
  139. {
  140. //DEBUG << "parsed component: ICAL_XSTANDARD_COMPONENT";
  141. break;
  142. }
  143. case ICAL_XDAYLIGHT_COMPONENT:
  144. {
  145. //DEBUG << "parsed component: ICAL_XDAYLIGHT_COMPONENT";
  146. break;
  147. }
  148. case ICAL_X_COMPONENT:
  149. {
  150. //DEBUG << "parsed component: ICAL_X_COMPONENT";
  151. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  152. break;
  153. }
  154. case ICAL_VSCHEDULE_COMPONENT:
  155. {
  156. //DEBUG << "parsed component: ICAL_VSCHEDULE_COMPONENT";
  157. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  158. break;
  159. }
  160. case ICAL_VQUERY_COMPONENT:
  161. {
  162. //DEBUG << "parsed component: ICAL_VQUERY_COMPONENT";
  163. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  164. break;
  165. }
  166. case ICAL_VREPLY_COMPONENT:
  167. {
  168. //DEBUG << "parsed component: ICAL_VREPLY_COMPONENT";
  169. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  170. break;
  171. }
  172. case ICAL_VCAR_COMPONENT:
  173. {
  174. //DEBUG << "parsed component: ICAL_VCAR_COMPONENT";
  175. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  176. break;
  177. }
  178. case ICAL_VCOMMAND_COMPONENT:
  179. {
  180. //DEBUG << "parsed component: ICAL_VCOMMAND_COMPONENT";
  181. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  182. break;
  183. }
  184. case ICAL_XLICINVALID_COMPONENT:
  185. {
  186. //DEBUG << "parsed component: ICAL_XLICINVALID_COMPONENT";
  187. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  188. break;
  189. }
  190. case ICAL_XLICMIMEPART_COMPONENT:
  191. {
  192. //DEBUG << "parsed component: ICAL_XLICMIMEPART_COMPONENT";
  193. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  194. break;
  195. }
  196. case ICAL_VAVAILABILITY_COMPONENT:
  197. {
  198. //DEBUG << "parsed component: ICAL_VAVAILABILITY_COMPONENT";
  199. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  200. break;
  201. }
  202. case ICAL_XAVAILABLE_COMPONENT:
  203. {
  204. //DEBUG << "parsed component: ICAL_XAVAILABLE_COMPONENT";
  205. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  206. break;
  207. }
  208. case ICAL_VPOLL_COMPONENT:
  209. {
  210. //DEBUG << "parsed component: ICAL_VPOLL_COMPONENT";
  211. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  212. break;
  213. }
  214. case ICAL_VVOTER_COMPONENT:
  215. {
  216. //DEBUG << "parsed component: ICAL_VVOTER_COMPONENT";
  217. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  218. break;
  219. }
  220. case ICAL_XVOTE_COMPONENT:
  221. {
  222. //DEBUG << "parsed component: ICAL_XVOTE_COMPONENT";
  223. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  224. break;
  225. }
  226. case ICAL_VPATCH_COMPONENT:
  227. {
  228. //DEBUG << "parsed component: ICAL_VPATCH_COMPONENT";
  229. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  230. break;
  231. }
  232. case ICAL_XPATCH_COMPONENT:
  233. {
  234. //DEBUG << "parsed component: ICAL_XPATCH_COMPONENT";
  235. //DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  236. break;
  237. }
  238. default:
  239. DEBUG << "parse_component: something else found";
  240. break;
  241. }
  242. }
  243. std::string slurp(std::ifstream& in) {
  244. std::ostringstream sstr;
  245. sstr << in.rdbuf();
  246. return sstr.str();
  247. }
  248. icalcomponent* parse_ics_file(std::string filename) {
  249. std::ifstream ifs (filename, std::ifstream::in);
  250. return icalparser_parse_string(slurp(ifs).c_str());
  251. }
  252. void debug(icalcomponent* component) {
  253. DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
  254. }
  255. std::string date_string(const uint32_t &epoch, const std::string &format) {
  256. std::time_t time = static_cast<time_t>(epoch);
  257. char buf[80];
  258. strftime(buf, sizeof(buf), format.c_str(), std::localtime(&time));
  259. return buf;
  260. }
  261. }