split GSettings.list_items => list_{children,keys}

This is an incompatible public API/ABI change.
This commit is contained in:
Ryan Lortie
2010-09-06 12:47:37 -04:00
parent 7b4cbbb7b2
commit 77e3badcf3
5 changed files with 97 additions and 36 deletions

View File

@@ -1556,7 +1556,8 @@ g_settings_set_enum
g_settings_get_flags g_settings_get_flags
g_settings_set_flags g_settings_set_flags
g_settings_sync g_settings_sync
g_settings_list_items g_settings_list_keys
g_settings_list_children
g_settings_get_mapped g_settings_get_mapped
#endif #endif
#endif #endif

View File

@@ -157,21 +157,20 @@ static gboolean
key_exists (GSettings *settings, key_exists (GSettings *settings,
const gchar *name) const gchar *name)
{ {
const gchar **keys; gchar **keys;
gint i; gint i;
gboolean ret; gboolean ret;
ret = FALSE; ret = FALSE;
keys = g_settings_list_items (settings); keys = g_settings_list_keys (settings);
for (i = 0; keys[i]; i++) for (i = 0; keys[i]; i++)
if (!g_str_has_suffix (keys[i], "/") && if (g_strcmp0 (keys[i], name) == 0)
g_strcmp0 (keys[i], name) == 0)
{ {
ret = TRUE; ret = TRUE;
break; break;
} }
g_free (keys); g_strfreev (keys);
return ret; return ret;
} }
@@ -180,17 +179,16 @@ static void
list_keys (GSettings *settings, list_keys (GSettings *settings,
const gchar *prefix) const gchar *prefix)
{ {
const gchar **keys; gchar **keys;
gint i; gint i;
keys = g_settings_list_items (settings); keys = g_settings_list_keys (settings);
for (i = 0; keys[i]; i++) for (i = 0; keys[i]; i++)
{ {
if (!g_str_has_suffix (keys[i], "/") && if (prefix == NULL || g_str_has_prefix (keys[i], prefix))
(prefix == NULL || g_str_has_prefix (keys[i], prefix)))
g_print ("%s \n", keys[i]); g_print ("%s \n", keys[i]);
} }
g_free (keys); g_strfreev (keys);
} }
static void static void

View File

@@ -1981,7 +1981,7 @@ g_settings_get_has_unapplied (GSettings *settings)
G_DELAYED_SETTINGS_BACKEND (settings->priv->backend)); 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: * g_settings_reset:
* @settings: a #GSettings object * @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 * @settings: a #GSettings object
* @returns: a list of the keys on @settings * @returns: a list of the keys on @settings
* *
* Introspects the list of keys and children on @settings. * Introspects the list of keys 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.
* *
* You should probably not be calling this function from "normal" code * You should probably not be calling this function from "normal" code
* (since you should already know what keys are in your schema). This * (since you should already know what keys are in your schema). This
* function is intended for introspection reasons. * function is intended for introspection reasons.
* *
* You should free the return value with g_free() when you are done with * You should free the return value with g_strfreev() when you are done
* it. * with it.
*/ */
const gchar ** gchar **
g_settings_list_items (GSettings *settings) g_settings_list_keys (GSettings *settings)
{ {
const GQuark *keys; const GQuark *keys;
const gchar **strv; gchar **strv;
gint n_keys; gint n_keys;
gint i; gint i, j;
keys = g_settings_schema_list (settings->priv->schema, &n_keys); keys = g_settings_schema_list (settings->priv->schema, &n_keys);
strv = g_new (const gchar *, n_keys + 1); strv = g_new (gchar *, n_keys + 1);
for (i = 0; i < n_keys; i++) for (i = j = 0; i < n_keys; i++)
strv[i] = g_quark_to_string (keys[i]); {
strv[i] = NULL; 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; return strv;
} }

View File

@@ -79,7 +79,11 @@ GSettings * g_settings_new_with_backend (const g
GSettings * g_settings_new_with_backend_and_path (const gchar *schema, GSettings * g_settings_new_with_backend_and_path (const gchar *schema,
GSettingsBackend *backend, GSettingsBackend *backend,
const gchar *path); 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, gboolean g_settings_set_value (GSettings *settings,
const gchar *key, const gchar *key,

View File

@@ -1697,7 +1697,7 @@ test_range (void)
} }
static gboolean static gboolean
strv_has_string (const gchar **haystack, strv_has_string (gchar **haystack,
const gchar *needle) const gchar *needle)
{ {
guint n; guint n;
@@ -1711,7 +1711,7 @@ strv_has_string (const gchar **haystack,
} }
static gboolean static gboolean
strv_set_equal (const gchar **strv, ...) strv_set_equal (gchar **strv, ...)
{ {
gint count; gint count;
va_list list; va_list list;
@@ -1745,14 +1745,18 @@ static void
test_list_items (void) test_list_items (void)
{ {
GSettings *settings; GSettings *settings;
const gchar **items; gchar **children;
gchar **keys;
settings = g_settings_new ("org.gtk.test"); 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); g_object_unref (settings);
} }
@@ -1764,7 +1768,7 @@ test_list_schemas (void)
schemas = g_settings_list_schemas (); 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",
"org.gtk.test.no-path", "org.gtk.test.no-path",
"org.gtk.test.basic-types", "org.gtk.test.basic-types",