diff --git a/src/extensions-tool/command-disable.c b/src/extensions-tool/command-disable.c index e578959ab..9c50e1a3d 100644 --- a/src/extensions-tool/command-disable.c +++ b/src/extensions-tool/command-disable.c @@ -29,33 +29,11 @@ static gboolean disable_extension (const char *uuid) { g_autoptr(GSettings) settings = get_shell_settings (); - g_auto(GStrv) extensions = NULL; - g_auto(GStrv) new_value = NULL; - const char **s; - guint n_extensions; - int i; if (settings == NULL) return FALSE; - if (!g_settings_is_writable (settings, "enabled-extensions")) - return FALSE; - - extensions = g_settings_get_strv (settings, "enabled-extensions"); - - if (!g_strv_contains ((const char **)extensions, uuid)) - return TRUE; - - n_extensions = g_strv_length (extensions); - new_value = g_new0 (char *, n_extensions); - for (i = 0, s = (const char **)extensions; i < n_extensions; i++, s++) - if (!g_str_equal (*s, uuid)) - new_value[i] = g_strdup (*s); - - g_settings_set_strv (settings, "enabled-extensions", (const char **)new_value); - g_settings_sync (); - - return TRUE; + return settings_list_remove (settings, "enabled-extensions", uuid); } int diff --git a/src/extensions-tool/command-enable.c b/src/extensions-tool/command-enable.c index 25b3e9230..2358f8ba2 100644 --- a/src/extensions-tool/command-enable.c +++ b/src/extensions-tool/command-enable.c @@ -29,32 +29,11 @@ static gboolean enable_extension (const char *uuid) { g_autoptr(GSettings) settings = get_shell_settings (); - g_auto(GStrv) extensions = NULL; - g_auto(GStrv) new_value = NULL; - guint n_extensions; - int i; if (settings == NULL) return FALSE; - if (!g_settings_is_writable (settings, "enabled-extensions")) - return FALSE; - - extensions = g_settings_get_strv (settings, "enabled-extensions"); - - if (g_strv_contains ((const char **)extensions, uuid)) - return TRUE; - - n_extensions = g_strv_length (extensions); - new_value = g_new0 (char *, n_extensions + 2); - for (i = 0; i < n_extensions; i++) - new_value[i] = g_strdup (extensions[i]); - new_value[i] = g_strdup (uuid); - - g_settings_set_strv (settings, "enabled-extensions", (const char **)new_value); - g_settings_sync (); - - return TRUE; + return settings_list_add (settings, "enabled-extensions", uuid); } int diff --git a/src/extensions-tool/common.h b/src/extensions-tool/common.h index 860813a07..32f4b788b 100644 --- a/src/extensions-tool/common.h +++ b/src/extensions-tool/common.h @@ -54,6 +54,13 @@ void print_extension_info (GVariantDict *info, GDBusProxy *get_shell_proxy (GError **error); GSettings *get_shell_settings (void); +gboolean settings_list_add (GSettings *settings, + const char *key, + const char *value); +gboolean settings_list_remove (GSettings *settings, + const char *key, + const char *value); + gboolean file_delete_recursively (GFile *file, GError **error); diff --git a/src/extensions-tool/main.c b/src/extensions-tool/main.c index e6792ad64..96117a90d 100644 --- a/src/extensions-tool/main.c +++ b/src/extensions-tool/main.c @@ -91,6 +91,67 @@ get_shell_settings (void) return g_settings_new_full (schema, NULL, NULL); } +gboolean +settings_list_add (GSettings *settings, + const char *key, + const char *value) +{ + g_auto(GStrv) list = NULL; + g_auto(GStrv) new_value = NULL; + guint n_values; + int i; + + if (!g_settings_is_writable (settings, key)) + return FALSE; + + list = g_settings_get_strv (settings, key); + + if (g_strv_contains ((const char **)list, value)) + return TRUE; + + n_values = g_strv_length (list); + new_value = g_new0 (char *, n_values + 2); + for (i = 0; i < n_values; i++) + new_value[i] = g_strdup (list[i]); + new_value[i] = g_strdup (value); + + g_settings_set_strv (settings, key, (const char **)new_value); + g_settings_sync (); + + return TRUE; +} + +gboolean +settings_list_remove (GSettings *settings, + const char *key, + const char *value) +{ + g_auto(GStrv) list = NULL; + g_auto(GStrv) new_value = NULL; + const char **s; + guint n_values; + int i; + + if (!g_settings_is_writable (settings, key)) + return FALSE; + + list = g_settings_get_strv (settings, key); + + if (!g_strv_contains ((const char **)list, value)) + return TRUE; + + n_values = g_strv_length (list); + new_value = g_new0 (char *, n_values); + for (i = 0, s = (const char **)list; i < n_values; i++, s++) + if (!g_str_equal (*s, value)) + new_value[i] = g_strdup (*s); + + g_settings_set_strv (settings, key, (const char **)new_value); + g_settings_sync (); + + return TRUE; +} + void print_extension_info (GVariantDict *info, DisplayFormat format)