[glib/wip/gsettings-work: 9/10] Add g_settings_schema_source_list_schemas()



commit 27840ede86a6a4785f59ed4ec706c4ac1a31936b
Author: Ryan Lortie <desrt desrt ca>
Date:   Sat Oct 26 19:12:46 2013 -0700

    Add g_settings_schema_source_list_schemas()
    
    g_settings_list_schemas() and g_settings_list_relocatable_schemas() are
    now deprecated.
    
    This will allow listing off schemas on non-default sources and is a
    better fit with the new direction the API is going.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680838

 gio/gsettings.h       |    4 +-
 gio/gsettingsschema.c |  172 +++++++++++++++++++++++++++----------------------
 gio/gsettingsschema.h |    6 ++
 3 files changed, 102 insertions(+), 80 deletions(-)
---
diff --git a/gio/gsettings.h b/gio/gsettings.h
index bef89b8..7a1f5a9 100644
--- a/gio/gsettings.h
+++ b/gio/gsettings.h
@@ -72,9 +72,9 @@ struct _GSettings
 GLIB_AVAILABLE_IN_ALL
 GType                   g_settings_get_type                             (void);
 
-GLIB_AVAILABLE_IN_ALL
+GLIB_DEPRECATED_IN_2_40_FOR(g_settings_schema_source_list_schemas)
 const gchar * const *   g_settings_list_schemas                         (void);
-GLIB_AVAILABLE_IN_ALL
+GLIB_DEPRECATED_IN_2_40_FOR(g_settings_schema_source_list_schemas)
 const gchar * const *   g_settings_list_relocatable_schemas             (void);
 GLIB_AVAILABLE_IN_ALL
 GSettings *             g_settings_new                                  (const gchar        *schema_id);
diff --git a/gio/gsettingsschema.c b/gio/gsettingsschema.c
index be31c4f..0312f7b 100644
--- a/gio/gsettingsschema.c
+++ b/gio/gsettingsschema.c
@@ -712,82 +712,112 @@ g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
   return source->text_tables;
 }
 
-static gboolean
-steal_item (gpointer key,
-            gpointer value,
-            gpointer user_data)
+/**
+ * g_settings_schema_source_list_schemas:
+ * @source: a #GSettingsSchemaSource
+ * @recursive: if we should recurse
+ * @non_relocatable_schemas: (out) (transfer full): the list of
+ *   non-relocatable schemas
+ * @relocatable_schemas: (out) (transfer full): the list of relocatable
+ *   schemas
+ *
+ * Lists the schemas in a given source.
+ *
+ * If @recursive is %TRUE then include parent sources.  If %FALSE then
+ * only include the schemas from one source (ie: one directory).  You
+ * probably want %TRUE.
+ *
+ * Non-relocatable schemas are those for which you can call
+ * g_settings_new().  Relocatable schemas are those for which you must
+ * use g_settings_new_with_path().
+ *
+ * Do not call this function from normal programs.  This is designed for
+ * use by database editors, commandline tools, etc.
+ *
+ * Since: 2.40
+ **/
+void
+g_settings_schema_source_list_schemas (GSettingsSchemaSource   *source,
+                                       gboolean                 recursive,
+                                       gchar                 ***non_relocatable_schemas,
+                                       gchar                 ***relocatable_schemas)
 {
-  gchar ***ptr = user_data;
-
-  *(*ptr)++ = (gchar *) key;
-
-  return TRUE;
-}
+  GHashTable *single, *reloc;
+  GSettingsSchemaSource *s;
 
-static const gchar * const *non_relocatable_schema_list;
-static const gchar * const *relocatable_schema_list;
-static gsize schema_lists_initialised;
+  /* We use hash tables to avoid duplicate listings for schemas that
+   * appear in more than one file.
+   */
+  single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+  reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
-static void
-ensure_schema_lists (void)
-{
-  if (g_once_init_enter (&schema_lists_initialised))
+  for (s = source; s; s = s->parent)
     {
-      GSettingsSchemaSource *source;
-      GHashTable *single, *reloc;
-      const gchar **ptr;
       gchar **list;
       gint i;
 
-      initialise_schema_sources ();
+      list = gvdb_table_list (s->table, "");
 
-      /* We use hash tables to avoid duplicate listings for schemas that
-       * appear in more than one file.
-       */
-      single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-      reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+      /* empty schema cache file? */
+      if (list == NULL)
+        continue;
 
-      for (source = schema_sources; source; source = source->parent)
+      for (i = 0; list[i]; i++)
         {
-          list = gvdb_table_list (source->table, "");
-
-          /* empty schema cache file? */
-          if (list == NULL)
-            continue;
-
-          for (i = 0; list[i]; i++)
+          if (!g_hash_table_lookup (single, list[i]) &&
+              !g_hash_table_lookup (reloc, list[i]))
             {
-              if (!g_hash_table_lookup (single, list[i]) &&
-                  !g_hash_table_lookup (reloc, list[i]))
-                {
-                  GvdbTable *table;
+              GvdbTable *table;
 
-                  table = gvdb_table_get_table (source->table, list[i]);
-                  g_assert (table != NULL);
+              table = gvdb_table_get_table (s->table, list[i]);
+              g_assert (table != NULL);
 
-                  if (gvdb_table_has_value (table, ".path"))
-                    g_hash_table_insert (single, g_strdup (list[i]), NULL);
-                  else
-                    g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
+              if (gvdb_table_has_value (table, ".path"))
+                g_hash_table_insert (single, g_strdup (list[i]), NULL);
+              else
+                g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
 
-                  gvdb_table_unref (table);
-                }
+              gvdb_table_unref (table);
             }
-
-          g_strfreev (list);
         }
 
-      ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
-      non_relocatable_schema_list = ptr;
-      g_hash_table_foreach_steal (single, steal_item, &ptr);
-      g_hash_table_unref (single);
-      *ptr = NULL;
+      g_strfreev (list);
+
+      /* Only the first source if recursive not requested */
+      if (!recursive)
+        break;
+    }
 
-      ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
-      relocatable_schema_list = ptr;
-      g_hash_table_foreach_steal (reloc, steal_item, &ptr);
-      g_hash_table_unref (reloc);
-      *ptr = NULL;
+  if (non_relocatable_schemas)
+    {
+      *non_relocatable_schemas = (gchar **) g_hash_table_get_key_array (single, NULL);
+      g_hash_table_steal_all (single);
+    }
+
+  if (relocatable_schemas)
+    {
+      *relocatable_schemas = (gchar **) g_hash_table_get_key_array (reloc, NULL);
+      g_hash_table_steal_all (reloc);
+    }
+
+  g_hash_table_unref (single);
+  g_hash_table_unref (reloc);
+}
+
+static gchar **non_relocatable_schema_list;
+static gchar **relocatable_schema_list;
+static gsize schema_lists_initialised;
+
+static void
+ensure_schema_lists (void)
+{
+  if (g_once_init_enter (&schema_lists_initialised))
+    {
+      initialise_schema_sources ();
+
+      g_settings_schema_source_list_schemas (schema_sources, TRUE,
+                                             &non_relocatable_schema_list,
+                                             &relocatable_schema_list);
 
       g_once_init_leave (&schema_lists_initialised, TRUE);
     }
@@ -796,53 +826,39 @@ ensure_schema_lists (void)
 /**
  * g_settings_list_schemas:
  *
- * Gets a list of the #GSettings schemas installed on the system.  The
- * returned list is exactly the list of schemas for which you may call
- * g_settings_new() without adverse effects.
- *
- * This function does not list the schemas that do not provide their own
- * paths (ie: schemas for which you must use
- * g_settings_new_with_path()).  See
- * g_settings_list_relocatable_schemas() for that.
- *
  * Returns: (element-type utf8) (transfer none):  a list of #GSettings
  *   schemas that are available.  The list must not be modified or
  *   freed.
  *
  * Since: 2.26
+ *
+ * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
  **/
 const gchar * const *
 g_settings_list_schemas (void)
 {
   ensure_schema_lists ();
 
-  return non_relocatable_schema_list;
+  return (const gchar **) non_relocatable_schema_list;
 }
 
 /**
  * g_settings_list_relocatable_schemas:
  *
- * Gets a list of the relocatable #GSettings schemas installed on the
- * system.  These are schemas that do not provide their own path.  It is
- * usual to instantiate these schemas directly, but if you want to you
- * can use g_settings_new_with_path() to specify the path.
- *
- * The output of this function, taken together with the output of
- * g_settings_list_schemas() represents the complete list of all
- * installed schemas.
- *
  * Returns: (element-type utf8) (transfer none): a list of relocatable
  *   #GSettings schemas that are available.  The list must not be
  *   modified or freed.
  *
  * Since: 2.28
+ *
+ * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
  **/
 const gchar * const *
 g_settings_list_relocatable_schemas (void)
 {
   ensure_schema_lists ();
 
-  return relocatable_schema_list;
+  return (const gchar **) relocatable_schema_list;
 }
 
 /**
diff --git a/gio/gsettingsschema.h b/gio/gsettingsschema.h
index 078e550..90ef37a 100644
--- a/gio/gsettingsschema.h
+++ b/gio/gsettingsschema.h
@@ -51,6 +51,12 @@ GSettingsSchema *       g_settings_schema_source_lookup                 (GSettin
                                                                          const gchar            *schema_id,
                                                                          gboolean                recursive);
 
+GLIB_AVAILABLE_IN_2_40
+void                    g_settings_schema_source_list_schemas           (GSettingsSchemaSource   *source,
+                                                                         gboolean                 recursive,
+                                                                         gchar                 
***non_recursive_schemas,
+                                                                         gchar                 
***recursive_schemas);
+
 #define                 G_TYPE_SETTINGS_SCHEMA                          (g_settings_schema_get_type ())
 GLIB_AVAILABLE_IN_2_32
 GType                   g_settings_schema_get_type                      (void) G_GNUC_CONST;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]