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.

262 lines
8.8 KiB

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