mdiary/src/main.c

96 lines
2.8 KiB
C
Raw Normal View History

2017-01-31 19:42:41 +01:00
#include <stdio.h>
#include <gtk/gtk.h>
#include "mainwindow.h"
#include "mdiary.h"
2017-02-09 16:14:38 +01:00
#include "settings.h"
2017-01-31 19:42:41 +01:00
2017-02-19 13:42:02 +01:00
struct app_state {
struct mainWindow *mainWindow;
struct mdiary_settings *settings;
};
2017-02-18 15:43:12 +01:00
static void quit_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
2017-02-18 17:04:39 +01:00
struct mainWindow *mainWindow = *((struct mainWindow **) user_data);
2017-02-18 15:43:12 +01:00
2017-02-18 17:04:39 +01:00
gtk_window_close(GTK_WINDOW(mainWindow->mainWindow));
2017-02-18 15:43:12 +01:00
}
static void settings_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
2017-02-18 17:04:39 +01:00
struct mainWindow *mainWindow = *((struct mainWindow **) user_data);
mainWindow_settings_show(mainWindow);
2017-02-18 15:43:12 +01:00
}
const GActionEntry app_actions[] = {
{ "quit", quit_cb },
{ "settings", settings_cb }
};
2017-02-18 15:21:34 +01:00
static void activate(GApplication *app, gpointer user_data)
2017-01-31 19:42:41 +01:00
{
2017-02-19 13:42:02 +01:00
struct app_state *app_state = user_data;
struct mainWindow **mainWindow = &(app_state->mainWindow);
struct mdiary_settings **settings = &(app_state->settings);
2017-02-09 16:14:38 +01:00
gchar *settings_path;
2017-01-31 19:42:41 +01:00
2017-02-19 13:31:11 +01:00
*mainWindow = mainWindow_new();
gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW((*mainWindow)->mainWindow));
2017-02-11 18:09:07 +01:00
2017-02-09 16:14:38 +01:00
settings_path = g_strdup_printf("%s/.mdiary.conf", g_get_home_dir());
2017-02-19 13:42:02 +01:00
*settings = mdiary_settings_new(*mainWindow);
mdiary_load_settings(settings_path, *settings);
g_free(settings_path);
2017-02-09 16:14:38 +01:00
2017-02-19 13:31:11 +01:00
gtk_widget_show((*mainWindow)->mainWindow);
2017-02-19 13:42:02 +01:00
}
static void shutdown(GApplication *app, gpointer user_data)
{
struct app_state *app_state = user_data;
struct mainWindow **mainWindow = &(app_state->mainWindow);
struct mdiary_settings **settings = &(app_state->settings);
gchar *settings_path;
2017-01-31 19:42:41 +01:00
2017-02-19 13:42:02 +01:00
settings_path = g_strdup_printf("%s/.mdiary.conf", g_get_home_dir());
mdiary_save_settings(settings_path, *settings);
mdiary_settings_free(*settings);
2017-02-09 16:14:38 +01:00
g_free(settings_path);
2017-01-31 19:42:41 +01:00
}
2017-02-18 15:21:34 +01:00
int main(int argc, char *argv[])
{
2017-02-19 13:42:02 +01:00
struct app_state app_state;
2017-02-18 15:21:34 +01:00
GtkApplication *app;
2017-02-18 15:43:12 +01:00
GMenu *app_menu;
GMenu *p1;
GMenu *p2;
2017-02-18 15:21:34 +01:00
int status;
app = gtk_application_new("net.notsyncing.mdiary", G_APPLICATION_FLAGS_NONE);
2017-02-18 17:04:39 +01:00
g_application_register(G_APPLICATION(app), NULL, NULL);
2017-02-19 13:42:02 +01:00
g_signal_connect(app, "activate", G_CALLBACK(activate), &app_state);
g_signal_connect(app, "shutdown", G_CALLBACK(shutdown), &app_state);
2017-02-18 15:43:12 +01:00
app_menu = g_menu_new();
p1 = g_menu_new();
p2 = g_menu_new();
g_menu_append(p1, "Settings", "app.settings");
g_menu_append(p2, "Quit", "app.quit");
g_menu_append_section(app_menu, NULL, G_MENU_MODEL(p1));
g_menu_append_section(app_menu, NULL, G_MENU_MODEL(p2));
gtk_application_set_app_menu(app, G_MENU_MODEL(app_menu));
2017-02-19 13:42:02 +01:00
g_action_map_add_action_entries(G_ACTION_MAP(app), app_actions, G_N_ELEMENTS(app_actions), &app_state);
2017-02-18 15:43:12 +01:00
gtk_application_set_app_menu(GTK_APPLICATION(app), G_MENU_MODEL(app_menu));
g_object_unref(p1);
g_object_unref(p2);
g_object_unref(app_menu);
2017-02-18 15:21:34 +01:00
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
2017-03-24 20:49:10 +01:00
return status;
2017-02-18 15:21:34 +01:00
}