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.

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