extensions-tool: Split out settings_list_add/remove helpers

With the addition of the 'disabled-extensions' key in commit ce1bee727,
the way extensions are enabled/disabled changed: Now a UUID is always
added to one list and removed from another.

Prepare for that by generalizing the relevant bits of the existing
enable/disable commands as helper functions.

https://gitlab.gnome.org/GNOME/gnome-shell/issues/1234
This commit is contained in:
Florian Müllner 2019-07-24 01:24:52 +02:00
parent 2df7757905
commit 7141c5be6d
4 changed files with 70 additions and 45 deletions

View File

@ -29,33 +29,11 @@ static gboolean
disable_extension (const char *uuid) disable_extension (const char *uuid)
{ {
g_autoptr(GSettings) settings = get_shell_settings (); 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) if (settings == NULL)
return FALSE; return FALSE;
if (!g_settings_is_writable (settings, "enabled-extensions")) return settings_list_remove (settings, "enabled-extensions", uuid);
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;
} }
int int

View File

@ -29,32 +29,11 @@ static gboolean
enable_extension (const char *uuid) enable_extension (const char *uuid)
{ {
g_autoptr(GSettings) settings = get_shell_settings (); 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) if (settings == NULL)
return FALSE; return FALSE;
if (!g_settings_is_writable (settings, "enabled-extensions")) return settings_list_add (settings, "enabled-extensions", uuid);
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;
} }
int int

View File

@ -54,6 +54,13 @@ void print_extension_info (GVariantDict *info,
GDBusProxy *get_shell_proxy (GError **error); GDBusProxy *get_shell_proxy (GError **error);
GSettings *get_shell_settings (void); 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, gboolean file_delete_recursively (GFile *file,
GError **error); GError **error);

View File

@ -91,6 +91,67 @@ get_shell_settings (void)
return g_settings_new_full (schema, NULL, NULL); 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 void
print_extension_info (GVariantDict *info, print_extension_info (GVariantDict *info,
DisplayFormat format) DisplayFormat format)