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.

82 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <db/FSWatcher.h>
  2. #include <fstream>
  3. #include <string>
  4. #include <filesystem>
  5. #include <io/debug.h>
  6. #include <db/db.h>
  7. #include <db/update.h>
  8. #include <util/calendar_parsing.h>
  9. bool is_ics(const std::filesystem::path filepath) {
  10. return filepath.extension() == ".ics";
  11. }
  12. namespace db {
  13. FSWatcher::FSWatcher(sql::Database *db, const std::string &directory) : m_db(db), m_directory(directory) {}
  14. void FSWatcher::run() {
  15. // Parse the directory to watch
  16. std::filesystem::path path(m_directory);
  17. // Set the event handler which will be used to process particular events
  18. auto create_entry = [&](inotify::Notification notification) {
  19. create_event(notification.path);
  20. };
  21. auto modify_entry = [&](inotify::Notification notification) {
  22. modify_event(notification.path);
  23. };
  24. auto remove_entry = [&](inotify::Notification notification) {
  25. delete_event(notification.path);
  26. };
  27. /*
  28. auto handleUnexpectedNotification = [](inotify::Notification notification) {
  29. std::cout << "Event " << notification.event << " on " << notification.path << " at "
  30. << notification.time.time_since_epoch().count()
  31. << " was triggered, but was not expected" << std::endl;
  32. };
  33. */
  34. auto file_creation = { inotify::Event::create };
  35. auto file_deletion = { inotify::Event::remove };
  36. auto file_modification = { inotify::Event::moved_to };
  37. // The notifier is configured to watch the parsed path for the defined events. Particular files
  38. // or paths can be ignored(once).
  39. auto notifier = inotify::BuildNotifier()
  40. .watchPathRecursively(path)
  41. .onEvents(file_creation, create_entry)
  42. .onEvents(file_modification, modify_entry)
  43. .onEvents(file_deletion, remove_entry);
  44. // The event loop is started in a separate thread context.
  45. notifier.run();
  46. }
  47. void FSWatcher::create_event(const std::string event_file) {
  48. if(!is_ics(event_file)) return;
  49. ical::IcalObject* object = new ical::IcalObject();
  50. util::parse_main_component(object, util::parse_ics_file(event_file), event_file);
  51. DEBUG << "Created Event " << event_file;
  52. db::insert_events(m_db, object);
  53. }
  54. void FSWatcher::modify_event(const std::string event_file) {
  55. if(!is_ics(event_file)) return;
  56. ical::IcalObject* object = new ical::IcalObject();
  57. util::parse_main_component(object, util::parse_ics_file(event_file), event_file);
  58. DEBUG << "Modified Event " << event_file;
  59. db::insert_events(m_db, object);
  60. }
  61. void FSWatcher::delete_event(const std::string event_file) {
  62. if(!is_ics(event_file)) return;
  63. DEBUG << "Removing Event " << event_file;
  64. db::remove_events(m_db, event_file);
  65. }
  66. }