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.

247 lines
8.0 KiB

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