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;
}
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
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
}
/**
- * 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;
}
}
static gboolean
-strv_has_string (const gchar **haystack,
+strv_has_string (gchar **haystack,
const gchar *needle)
{
guint n;
}
static gboolean
-strv_set_equal (const gchar **strv, ...)
+strv_set_equal (gchar **strv, ...)
{
gint count;
va_list list;
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);
}
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",