Skip to content

Commit

Permalink
main,callbacks: add menu entry for easy user config file editing
Browse files Browse the repository at this point in the history
re #110
  • Loading branch information
bk138 committed Mar 29, 2024
1 parent c7ff625 commit df8114a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,60 @@ void on_intro(GtkMenuItem *menuitem,
gtk_widget_show_all (assistant);
}

void on_edit_config(GtkMenuItem *menuitem,
gpointer user_data)
{
/*
Check if user config does not exist or is empty.
If so, copy system config to user config.
*/
gchar *user_config_path = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_config_dir(), "gromit-mpx.cfg", NULL);
GFile *user_config_file = g_file_new_for_path(user_config_path);

guint64 user_config_size = 0;
GFileInfo *user_config_info = g_file_query_info(user_config_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, NULL);
if (user_config_info != NULL) {
user_config_size = g_file_info_get_size(user_config_info);
g_object_unref(user_config_info);
}

if (!g_file_query_exists(user_config_file, NULL) || user_config_size == 0) {
g_print("User config does not exist or is empty, copying system config\n");

gchar *system_config_path = g_strjoin (G_DIR_SEPARATOR_S, SYSCONFDIR, "gromit-mpx", "gromit-mpx.cfg", NULL);
GFile *system_config_file = g_file_new_for_path(system_config_path);

GError *error = NULL;
gboolean result = g_file_copy(system_config_file, user_config_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
if (!result) {
g_printerr("Error copying system config to user config: %s\n", error->message);
g_error_free(error);
}

g_object_unref(system_config_file);
g_free(system_config_path);
}


/*
Open user config for editing.
*/
gchar *user_config_uri = g_strjoin (G_DIR_SEPARATOR_S, "file://", user_config_path, NULL);

gtk_show_uri_on_window (NULL,
user_config_uri,
GDK_CURRENT_TIME,
NULL);

/*
Clean up
*/
g_object_unref(user_config_file);
g_free(user_config_path);
g_free(user_config_uri);
}


void on_issues(GtkMenuItem *menuitem,
gpointer user_data)
{
Expand Down
3 changes: 3 additions & 0 deletions src/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ void on_about(GtkMenuItem *menuitem,
void on_intro(GtkMenuItem *menuitem,
gpointer user_data);

void on_edit_config(GtkMenuItem *menuitem,
gpointer user_data);

void on_issues(GtkMenuItem *menuitem,
gpointer user_data);

Expand Down
6 changes: 6 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv)

GtkWidget* sep1_item = gtk_separator_menu_item_new();
GtkWidget* intro_item = gtk_menu_item_new_with_mnemonic(_("_Introduction"));
GtkWidget* edit_config_item = gtk_menu_item_new_with_mnemonic(_("_Edit Config"));
GtkWidget* issues_item = gtk_menu_item_new_with_mnemonic(_("_Report Bug / Request Feature"));
GtkWidget* support_item = gtk_menu_item_new_with_mnemonic(_("_Support Gromit-MPX"));
GtkWidget* about_item = gtk_menu_item_new_with_mnemonic(_("_About"));
Expand All @@ -919,6 +920,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv)

gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep1_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), intro_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), edit_config_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), issues_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), support_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), about_item);
Expand Down Expand Up @@ -981,6 +983,9 @@ void setup_main_app (GromitData *data, int argc, char ** argv)
g_signal_connect(G_OBJECT (intro_item), "activate",
G_CALLBACK (on_intro),
data);
g_signal_connect(G_OBJECT (edit_config_item), "activate",
G_CALLBACK (on_edit_config),
data);
g_signal_connect(G_OBJECT (issues_item), "activate",
G_CALLBACK (on_issues),
data);
Expand All @@ -1005,6 +1010,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv)

gtk_widget_show (sep1_item);
gtk_widget_show (intro_item);
gtk_widget_show (edit_config_item);
gtk_widget_show (issues_item);
gtk_widget_show (support_item);
gtk_widget_show (about_item);
Expand Down

0 comments on commit df8114a

Please sign in to comment.