TimerApplication.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "TimerApplication.h"
  2. #include "TimerMainWindow.h"
  3. #include <sys/stat.h>
  4. #define TIMER_CONFIG_FILE_NAME "settings.conf"
  5. #define TIMER_DEFAULT_DATA_FILE_NAME "data.json"
  6. typedef struct {
  7. gboolean versionSwitch;
  8. } TimerOptions;
  9. struct _TimerApplication {
  10. GtkApplication parent;
  11. char *configDir;
  12. char *configFile;
  13. char *defaultDataFile;
  14. TimerOptions options;
  15. };
  16. G_DEFINE_TYPE(TimerApplication, timer_application, GTK_TYPE_APPLICATION);
  17. static void timer_application_add_options(TimerApplication *app) {
  18. const GOptionEntry entries[] = {
  19. {
  20. .long_name = "version",
  21. .short_name = 'v',
  22. .flags = G_OPTION_FLAG_NONE,
  23. .arg = G_OPTION_ARG_NONE,
  24. .arg_data = &(app->options.versionSwitch),
  25. .description = "Print the version and then exit",
  26. .arg_description = NULL
  27. },
  28. {NULL}
  29. };
  30. g_application_add_main_option_entries(G_APPLICATION(app), entries);
  31. }
  32. static void timer_application_check_command_line_options(TimerApplication *self) {
  33. if (self->options.versionSwitch) {
  34. printf("%s\n", APPLICATION_VERSION);
  35. exit(EXIT_SUCCESS);
  36. }
  37. }
  38. static void timer_application_activate(GApplication *app) {
  39. timer_application_check_command_line_options(TIMER_APPLICATION(app));
  40. if (g_mkdir_with_parents(TIMER_APPLICATION(app)->configDir, 0700) != 0) {
  41. GtkWidget *msgDiag = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not create config dir: %s", strerror(errno));
  42. gtk_window_set_position(GTK_WINDOW(msgDiag), GTK_WIN_POS_MOUSE);
  43. gtk_dialog_run(GTK_DIALOG(msgDiag));
  44. gtk_widget_destroy(msgDiag);
  45. }
  46. TimerMainWindow *win = timer_main_window_new(TIMER_APPLICATION(app));
  47. timer_main_window_show_for_mode(win);
  48. }
  49. TimerApplication *timer_application_new() {
  50. #pragma GCC diagnostic push
  51. #pragma GCC diagnostic ignored "-Wdeprecated"
  52. return g_object_new(TIMER_TYPE_APPLICATION, "application-id", "org.zander.practicetiemr", "flags", G_APPLICATION_FLAGS_NONE, NULL);
  53. #pragma GCC diagnostic pop
  54. }
  55. const char *timer_application_get_config_file(TimerApplication *self) {
  56. return self->configFile;
  57. }
  58. const char *timer_application_get_default_data_file(TimerApplication *self) {
  59. return self->defaultDataFile;
  60. }
  61. static void timer_application_finalize(GObject *self) {
  62. g_free(TIMER_APPLICATION(self)->configFile);
  63. g_free(TIMER_APPLICATION(self)->configDir);
  64. g_free(TIMER_APPLICATION(self)->defaultDataFile);
  65. G_OBJECT_CLASS(timer_application_parent_class)->finalize(self);
  66. }
  67. static void timer_application_class_init(TimerApplicationClass *class) {
  68. G_OBJECT_CLASS(class)->finalize = timer_application_finalize;
  69. G_APPLICATION_CLASS(class)->activate = timer_application_activate;
  70. }
  71. static void timer_application_init(TimerApplication *self) {
  72. self->configFile = g_strdup_printf("%s/practicetimer/%s", g_get_user_config_dir(), TIMER_CONFIG_FILE_NAME);
  73. self->configDir = g_strdup_printf("%s/practicetimer", g_get_user_config_dir());
  74. self->defaultDataFile = g_strdup_printf("%s/practicetimer/%s", g_get_user_config_dir(), TIMER_DEFAULT_DATA_FILE_NAME);
  75. timer_application_add_options(self);
  76. }