#include "TimerApplication.h" #include "TimerMainWindow.h" #include #define TIMER_CONFIG_FILE_NAME "settings.conf" #define TIMER_DEFAULT_DATA_FILE_NAME "data.json" typedef struct { gboolean versionSwitch; } TimerOptions; struct _TimerApplication { GtkApplication parent; char *configDir; char *configFile; char *defaultDataFile; TimerOptions options; }; G_DEFINE_TYPE(TimerApplication, timer_application, GTK_TYPE_APPLICATION); static void timer_application_add_options(TimerApplication *app) { const GOptionEntry entries[] = { { .long_name = "version", .short_name = 'v', .flags = G_OPTION_FLAG_NONE, .arg = G_OPTION_ARG_NONE, .arg_data = &(app->options.versionSwitch), .description = "Print the version and then exit", .arg_description = NULL }, {NULL} }; g_application_add_main_option_entries(G_APPLICATION(app), entries); } static void timer_application_check_command_line_options(TimerApplication *self) { if (self->options.versionSwitch) { printf("%s\n", APPLICATION_VERSION); exit(EXIT_SUCCESS); } } static void timer_application_activate(GApplication *app) { timer_application_check_command_line_options(TIMER_APPLICATION(app)); if (g_mkdir_with_parents(TIMER_APPLICATION(app)->configDir, 0700) != 0) { GtkWidget *msgDiag = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not create config dir: %s", strerror(errno)); gtk_window_set_position(GTK_WINDOW(msgDiag), GTK_WIN_POS_MOUSE); gtk_dialog_run(GTK_DIALOG(msgDiag)); gtk_widget_destroy(msgDiag); } TimerMainWindow *win = timer_main_window_new(TIMER_APPLICATION(app)); timer_main_window_show_for_mode(win); } TimerApplication *timer_application_new() { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated" return g_object_new(TIMER_TYPE_APPLICATION, "application-id", "org.zander.practicetiemr", "flags", G_APPLICATION_FLAGS_NONE, NULL); #pragma GCC diagnostic pop } const char *timer_application_get_config_file(TimerApplication *self) { return self->configFile; } const char *timer_application_get_default_data_file(TimerApplication *self) { return self->defaultDataFile; } static void timer_application_finalize(GObject *self) { g_free(TIMER_APPLICATION(self)->configFile); g_free(TIMER_APPLICATION(self)->configDir); g_free(TIMER_APPLICATION(self)->defaultDataFile); G_OBJECT_CLASS(timer_application_parent_class)->finalize(self); } static void timer_application_class_init(TimerApplicationClass *class) { G_OBJECT_CLASS(class)->finalize = timer_application_finalize; G_APPLICATION_CLASS(class)->activate = timer_application_activate; } static void timer_application_init(TimerApplication *self) { self->configFile = g_strdup_printf("%s/practicetimer/%s", g_get_user_config_dir(), TIMER_CONFIG_FILE_NAME); self->configDir = g_strdup_printf("%s/practicetimer", g_get_user_config_dir()); self->defaultDataFile = g_strdup_printf("%s/practicetimer/%s", g_get_user_config_dir(), TIMER_DEFAULT_DATA_FILE_NAME); timer_application_add_options(self); }