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.

125 lines
3.4 KiB

  1. #include <dirent.h>
  2. #include <iterator>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <sys/stat.h>
  10. #include <syslog.h>
  11. #include <unistd.h>
  12. #include <vector>
  13. #include <db/db.h>
  14. #include <db/create.h>
  15. #include <db/update.h>
  16. #include <util/notifications.h>
  17. #include <util/calendar_parsing.h>
  18. #include <ical/IcalObject.h>
  19. #include <io/debug.h>
  20. const int MINUTE = 1000000;
  21. sql::Database* init_database(std::string calendar) {
  22. return db::init(calendar + ".db");
  23. }
  24. void update_database(std::string directory, sql::Database *db) {
  25. std::vector<ical::IcalObject*> objects = util::parse_cal_dir(directory);
  26. for(auto const& obj : objects) {
  27. if(obj->empty()) continue;
  28. db::insert_object(db, obj);
  29. }
  30. }
  31. void do_heartbeat(std::string directory, sql::Database *db) {
  32. util::notify("Updating files from " + directory, "");
  33. update_database(directory, db);
  34. }
  35. // For security purposes, we don't allow any arguments to be passed into the daemon
  36. int main(void)
  37. {
  38. // Define variables
  39. //pid_t pid, sid;
  40. //// Fork the current process
  41. //pid = fork();
  42. //// The parent process continues with a process ID greater than 0
  43. //if(pid > 0)
  44. //{
  45. // exit(EXIT_SUCCESS);
  46. //}
  47. //// A process ID lower than 0 indicates a failure in either process
  48. //else if(pid < 0)
  49. //{
  50. // exit(EXIT_FAILURE);
  51. //}
  52. //// The parent process has now terminated, and the forked child process will continue
  53. //// (the pid of the child process was 0)
  54. //// Since the child process is a daemon, the umask needs to be set so files and logs can be written
  55. //umask(0);
  56. //// Open system logs for the child process
  57. //openlog("daemon-named", LOG_NOWAIT | LOG_PID, LOG_USER);
  58. //syslog(LOG_NOTICE, "Successfully started daemon-name");
  59. //// Generate a session ID for the child process
  60. //sid = setsid();
  61. //// Ensure a valid SID for the child process
  62. //if(sid < 0)
  63. //{
  64. // // Log failure and exit
  65. // syslog(LOG_ERR, "Could not generate session ID for child process");
  66. // // If a new session ID could not be generated, we must terminate the child process
  67. // // or it will be orphaned
  68. // exit(EXIT_FAILURE);
  69. //}
  70. //// Change the current working directory to a directory guaranteed to exist
  71. //if((chdir("/")) < 0)
  72. //{
  73. // // Log failure and exit
  74. // syslog(LOG_ERR, "Could not change working directory to /");
  75. // // If our guaranteed directory does not exist, terminate the child process to ensure
  76. // // the daemon has not been hijacked
  77. // exit(EXIT_FAILURE);
  78. //}
  79. //// A daemon cannot use the terminal, so close standard file descriptors for security reasons
  80. //close(STDIN_FILENO);
  81. //close(STDOUT_FILENO);
  82. //close(STDERR_FILENO);
  83. // Daemon-specific intialization should go here
  84. const int SLEEP_INTERVAL = 5 * MINUTE;
  85. std::string calendar = "dummy";
  86. std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/";
  87. sql::Database* db = init_database(calendar);
  88. // Enter daemon loop
  89. while(1)
  90. {
  91. // Execute daemon heartbeat, where your recurring activity occurs
  92. do_heartbeat(directory, db);
  93. exit(0);
  94. // Sleep for a period of time
  95. sleep(SLEEP_INTERVAL);
  96. }
  97. // Close system logs for the child process
  98. //syslog(LOG_NOTICE, "Stopping daemon-name");
  99. //closelog();
  100. // Terminate the child process when the daemon completes
  101. exit(EXIT_SUCCESS);
  102. }