From cd37da9c59c6d124d8c0c67ec35dcc732845afbe Mon Sep 17 00:00:00 2001 From: Lawrence Murray Date: Thu, 6 Feb 2025 10:54:02 +0700 Subject: [PATCH] Added guard to ensure that desktop entries reported by file monitor are actually in menus.conf file if they are to be written to local applications directory. Fixes #10. Fixes #11 (related). --- data/org.indii.mendingwall.watch.service.in | 2 +- src/mendingwalldapplication.c | 50 +++++++++++---------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/data/org.indii.mendingwall.watch.service.in b/data/org.indii.mendingwall.watch.service.in index 64ee2a9..f51d59c 100644 --- a/data/org.indii.mendingwall.watch.service.in +++ b/data/org.indii.mendingwall.watch.service.in @@ -1,3 +1,3 @@ [D-BUS Service] -Name=org.indii.mendingwall.daemon +Name=org.indii.mendingwall.watch Exec=@bindir@/mendingwalld --gapplication-service --watch diff --git a/src/mendingwalldapplication.c b/src/mendingwalldapplication.c index 7f71025..fc5de2f 100644 --- a/src/mendingwalldapplication.c +++ b/src/mendingwalldapplication.c @@ -131,8 +131,8 @@ static void restore_file(MendingwallDApplication* self, GFile* file) { } } -static void update_app(MendingwallDApplication* self, const char* basename, - GKeyFile* app_file) { +static gboolean update_app(MendingwallDApplication* self, + const char* basename, GKeyFile* app_file) { /* update OnlyShowIn */ g_auto(GStrv) only_show_in = g_key_file_get_string_list( self->menus_config, basename, "OnlyShowIn", NULL, NULL); @@ -152,6 +152,8 @@ static void update_app(MendingwallDApplication* self, const char* basename, /* add custom marker */ g_key_file_set_boolean(app_file, "Desktop Entry", "X-MendingWall-Tidy", TRUE); + + return only_show_in || not_show_in; } static void tidy_app(MendingwallDApplication* self, const char* basename) { @@ -161,14 +163,14 @@ static void tidy_app(MendingwallDApplication* self, const char* basename) { if (g_key_file_load_from_data_dirs(app_file, app_path, NULL, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL)) { /* update app file */ - update_app(self, basename, app_file); - - /* save to user's data directory, if it does not already exist there */ - g_autofree gchar* to_path = g_build_filename(g_get_user_data_dir(), - "applications", basename, NULL); - g_autoptr(GFile) to_file = g_file_new_for_path(to_path); - if (!g_file_query_exists(to_file, NULL)) { - g_key_file_save_to_file(app_file, to_path, NULL); + if (update_app(self, basename, app_file)) { + /* save to user's data directory, if it does not already exist there */ + g_autofree gchar* to_path = g_build_filename(g_get_user_data_dir(), + "applications", basename, NULL); + g_autoptr(GFile) to_file = g_file_new_for_path(to_path); + if (!g_file_query_exists(to_file, NULL)) { + g_key_file_save_to_file(app_file, to_path, NULL); + } } } } @@ -180,20 +182,20 @@ static void untidy_app(MendingwallDApplication* self, const char* basename) { if (g_key_file_load_from_data_dirs(app_file, app_path, NULL, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL)) { /* update app file */ - update_app(self, basename, app_file); - - /* check if a matching desktop entry file exists in the user's data - * directory; if so and its contents match what would be written, delete - * it, otherwise custom changes have been made so leave it */ - g_autofree gchar* to_path = g_build_filename(g_get_user_data_dir(), - app_path, NULL); - g_autoptr(GFile) to_file = g_file_new_for_path(to_path); - if (g_file_query_exists(to_file, NULL)) { - g_autofree gchar* app_data = g_key_file_to_data(app_file, NULL, NULL); - g_autofree gchar* to_data = NULL; - g_file_load_contents(to_file, NULL, &to_data, NULL, NULL, NULL); - if (g_str_equal(app_data, to_data)) { - g_file_delete_async(to_file, G_PRIORITY_DEFAULT, NULL, NULL, NULL); + if (update_app(self, basename, app_file)) { + /* check if a matching desktop entry file exists in the user's data + * directory; if so and its contents match what would be written, delete + * it, otherwise custom changes have been made so leave it */ + g_autofree gchar* to_path = g_build_filename(g_get_user_data_dir(), + app_path, NULL); + g_autoptr(GFile) to_file = g_file_new_for_path(to_path); + if (g_file_query_exists(to_file, NULL)) { + g_autofree gchar* app_data = g_key_file_to_data(app_file, NULL, NULL); + g_autofree gchar* to_data = NULL; + g_file_load_contents(to_file, NULL, &to_data, NULL, NULL, NULL); + if (g_str_equal(app_data, to_data)) { + g_file_delete_async(to_file, G_PRIORITY_DEFAULT, NULL, NULL, NULL); + } } } }