From 77e3badcf3f15c7ac235904160d775febfcebcb5 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Mon, 6 Sep 2010 12:47:37 -0400 Subject: [PATCH] split GSettings.list_items => list_{children,keys} This is an incompatible public API/ABI change. --- gio/gio.symbols | 3 +- gio/gsettings-tool.c | 18 ++++----- gio/gsettings.c | 88 ++++++++++++++++++++++++++++++++++--------- gio/gsettings.h | 6 ++- gio/tests/gsettings.c | 18 +++++---- 5 files changed, 97 insertions(+), 36 deletions(-) diff --git a/gio/gio.symbols b/gio/gio.symbols index ab53ec982..cd5530f51 100644 --- a/gio/gio.symbols +++ b/gio/gio.symbols @@ -1556,7 +1556,8 @@ g_settings_set_enum g_settings_get_flags g_settings_set_flags g_settings_sync -g_settings_list_items +g_settings_list_keys +g_settings_list_children g_settings_get_mapped #endif #endif diff --git a/gio/gsettings-tool.c b/gio/gsettings-tool.c index bd7d40406..4a516a586 100644 --- a/gio/gsettings-tool.c +++ b/gio/gsettings-tool.c @@ -157,21 +157,20 @@ static gboolean key_exists (GSettings *settings, const gchar *name) { - const gchar **keys; + gchar **keys; gint i; gboolean ret; ret = FALSE; - keys = g_settings_list_items (settings); + keys = g_settings_list_keys (settings); for (i = 0; keys[i]; i++) - if (!g_str_has_suffix (keys[i], "/") && - g_strcmp0 (keys[i], name) == 0) + if (g_strcmp0 (keys[i], name) == 0) { ret = TRUE; break; } - g_free (keys); + g_strfreev (keys); return ret; } @@ -180,17 +179,16 @@ static void list_keys (GSettings *settings, const gchar *prefix) { - const gchar **keys; + gchar **keys; gint i; - keys = g_settings_list_items (settings); + keys = g_settings_list_keys (settings); for (i = 0; keys[i]; i++) { - if (!g_str_has_suffix (keys[i], "/") && - (prefix == NULL || g_str_has_prefix (keys[i], prefix))) + if (prefix == NULL || g_str_has_prefix (keys[i], prefix)) g_print ("%s \n", keys[i]); } - g_free (keys); + g_strfreev (keys); } static void diff --git a/gio/gsettings.c b/gio/gsettings.c index 905882c32..b8f0d30c7 100644 --- a/gio/gsettings.c +++ b/gio/gsettings.c @@ -1981,7 +1981,7 @@ g_settings_get_has_unapplied (GSettings *settings) G_DELAYED_SETTINGS_BACKEND (settings->priv->backend)); } -/* Extra API (reset, sync, get_child, is_writable, list_items) {{{1 */ +/* Extra API (reset, sync, get_child, is_writable, list_*) {{{1 */ /** * g_settings_reset: * @settings: a #GSettings object @@ -2096,36 +2096,90 @@ g_settings_get_child (GSettings *settings, } /** - * g_settings_list_items: + * g_settings_list_keys: * @settings: a #GSettings object * @returns: a list of the keys on @settings * - * Introspects the list of keys and children on @settings. - * - * The list that is returned is a mix of the keys and children. The - * names of the children are suffixed with '/'. The names of the keys - * are not. + * Introspects the list of keys on @settings. * * You should probably not be calling this function from "normal" code * (since you should already know what keys are in your schema). This * function is intended for introspection reasons. * - * You should free the return value with g_free() when you are done with - * it. + * You should free the return value with g_strfreev() when you are done + * with it. */ -const gchar ** -g_settings_list_items (GSettings *settings) +gchar ** +g_settings_list_keys (GSettings *settings) { const GQuark *keys; - const gchar **strv; + gchar **strv; gint n_keys; - gint i; + gint i, j; keys = g_settings_schema_list (settings->priv->schema, &n_keys); - strv = g_new (const gchar *, n_keys + 1); - for (i = 0; i < n_keys; i++) - strv[i] = g_quark_to_string (keys[i]); - strv[i] = NULL; + strv = g_new (gchar *, n_keys + 1); + for (i = j = 0; i < n_keys; i++) + { + const gchar *key = g_quark_to_string (keys[i]); + + if (!g_str_has_suffix (key, "/")) + strv[j++] = g_strdup (key); + } + strv[j] = NULL; + + return strv; +} + +/** + * g_settings_list_children: + * @settings: a #GSettings object + * @returns: a list of the children on @settings + * + * Gets the list of children on @settings. + * + * The list is exactly the list of strings for which it is not an error + * to call g_settings_get_child(). + * + * For GSettings objects that are lists, this value can change at any + * time and you should connect to the "children-changed" signal to watch + * for those changes. Note that there is a race condition here: you may + * request a child after listing it only for it to have been destroyed + * in the meantime. For this reason, g_settings_get_chuld() may return + * %NULL even for a child that was listed by this function. + * + * For GSettings objects that are not lists, you should probably not be + * calling this function from "normal" code (since you should already + * know what children are in your schema). This function may still be + * useful there for introspection reasons, however. + * + * You should free the return value with g_strfreev() when you are done + * with it. + */ +gchar ** +g_settings_list_children (GSettings *settings) +{ + const GQuark *keys; + gchar **strv; + gint n_keys; + gint i, j; + + keys = g_settings_schema_list (settings->priv->schema, &n_keys); + strv = g_new (gchar *, n_keys + 1); + for (i = j = 0; i < n_keys; i++) + { + const gchar *key = g_quark_to_string (keys[i]); + + if (g_str_has_suffix (key, "/")) + { + gint length = strlen (key); + + strv[j] = g_memdup (key, length); + strv[j][length - 1] = '\0'; + j++; + } + } + strv[j] = NULL; return strv; } diff --git a/gio/gsettings.h b/gio/gsettings.h index 938ec1ead..294d885d1 100644 --- a/gio/gsettings.h +++ b/gio/gsettings.h @@ -79,7 +79,11 @@ GSettings * g_settings_new_with_backend (const g GSettings * g_settings_new_with_backend_and_path (const gchar *schema, GSettingsBackend *backend, const gchar *path); -const gchar ** g_settings_list_items (GSettings *settings); +gchar ** g_settings_list_children (GSettings *settings); +gchar ** g_settings_list_keys (GSettings *settings); + +gboolean g_settings_get_destroyed (GSettings *settings); +GPermission * g_settings_get_permission (GSettings *settings); gboolean g_settings_set_value (GSettings *settings, const gchar *key, diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c index 412d909aa..fdadf9633 100644 --- a/gio/tests/gsettings.c +++ b/gio/tests/gsettings.c @@ -1697,7 +1697,7 @@ test_range (void) } static gboolean -strv_has_string (const gchar **haystack, +strv_has_string (gchar **haystack, const gchar *needle) { guint n; @@ -1711,7 +1711,7 @@ strv_has_string (const gchar **haystack, } static gboolean -strv_set_equal (const gchar **strv, ...) +strv_set_equal (gchar **strv, ...) { gint count; va_list list; @@ -1745,14 +1745,18 @@ static void test_list_items (void) { GSettings *settings; - const gchar **items; + gchar **children; + gchar **keys; settings = g_settings_new ("org.gtk.test"); - items = g_settings_list_items (settings); + children = g_settings_list_children (settings); + keys = g_settings_list_keys (settings); - g_assert (strv_set_equal (items, "greeting", "farewell", "basic-types/", "complex-types/", "localized/", NULL)); + g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL)); + g_assert (strv_set_equal (keys, "greeting", "farewell", NULL)); - g_free (items); + g_strfreev (children); + g_strfreev (keys); g_object_unref (settings); } @@ -1764,7 +1768,7 @@ test_list_schemas (void) schemas = g_settings_list_schemas (); - g_assert (strv_set_equal ((const gchar **)schemas, + g_assert (strv_set_equal ((gchar **)schemas, "org.gtk.test", "org.gtk.test.no-path", "org.gtk.test.basic-types",