2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gsettingsschema.h"
23 #include "gvdb/gvdb-reader.h"
27 G_DEFINE_TYPE (GSettingsSchema, g_settings_schema, G_TYPE_OBJECT)
29 struct _GSettingsSchemaPrivate
31 const gchar *gettext_domain;
39 static GSList *schema_sources;
42 initialise_schema_sources (void)
44 static gsize initialised;
46 if G_UNLIKELY (g_once_init_enter (&initialised))
48 const gchar * const *dir;
51 for (dir = g_get_system_data_dirs (); *dir; dir++)
56 filename = g_build_filename (*dir, "glib-2.0", "schemas",
57 "gschemas.compiled", NULL);
58 table = gvdb_table_new (filename, TRUE, NULL);
61 schema_sources = g_slist_prepend (schema_sources, table);
66 schema_sources = g_slist_reverse (schema_sources);
68 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
73 filename = g_build_filename (path, "gschemas.compiled", NULL);
74 table = gvdb_table_new (filename, TRUE, NULL);
77 schema_sources = g_slist_prepend (schema_sources, table);
82 g_once_init_leave (&initialised, TRUE);
87 steal_item (gpointer key,
91 gchar ***ptr = user_data;
93 *(*ptr)++ = (gchar *) key;
98 static const gchar * const *non_relocatable_schema_list;
99 static const gchar * const *relocatable_schema_list;
100 static gsize schema_lists_initialised;
103 ensure_schema_lists (void)
105 if (g_once_init_enter (&schema_lists_initialised))
107 GHashTable *single, *reloc;
113 initialise_schema_sources ();
115 /* We use hash tables to avoid duplicate listings for schemas that
116 * appear in more than one file.
118 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
119 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
121 for (source = schema_sources; source; source = source->next)
123 list = gvdb_table_list (source->data, "");
125 g_assert (list != NULL);
127 for (i = 0; list[i]; i++)
129 if (!g_hash_table_lookup (single, list[i]) &&
130 !g_hash_table_lookup (reloc, list[i]))
134 table = gvdb_table_get_table (source->data, list[i]);
135 g_assert (table != NULL);
137 if (gvdb_table_has_value (table, ".path"))
138 g_hash_table_insert (single, g_strdup (list[i]), NULL);
140 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
142 gvdb_table_unref (table);
149 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
150 non_relocatable_schema_list = ptr;
151 g_hash_table_foreach_steal (single, steal_item, &ptr);
152 g_hash_table_unref (single);
155 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
156 relocatable_schema_list = ptr;
157 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
158 g_hash_table_unref (reloc);
161 g_once_init_leave (&schema_lists_initialised, TRUE);
166 * g_settings_list_schemas:
168 * Gets a list of the #GSettings schemas installed on the system. The
169 * returned list is exactly the list of schemas for which you may call
170 * g_settings_new() without adverse effects.
172 * This function does not list the schemas that do not provide their own
173 * paths (ie: schemas for which you must use
174 * g_settings_new_with_path()). See
175 * g_settings_list_relocatable_schemas() for that.
177 * Returns: (element-type utf8) (transfer none): a list of #GSettings
178 * schemas that are available. The list must not be modified or
183 const gchar * const *
184 g_settings_list_schemas (void)
186 ensure_schema_lists ();
188 return non_relocatable_schema_list;
192 * g_settings_list_relocatable_schemas:
194 * Gets a list of the relocatable #GSettings schemas installed on the
195 * system. These are schemas that do not provide their own path. It is
196 * usual to instantiate these schemas directly, but if you want to you
197 * can use g_settings_new_with_path() to specify the path.
199 * The output of this function, tTaken together with the output of
200 * g_settings_list_schemas() represents the complete list of all
203 * Returns: (element-type utf8) (transfer none): a list of relocatable
204 * #GSettings schemas that are available. The list must not be
209 const gchar * const *
210 g_settings_list_relocatable_schemas (void)
212 ensure_schema_lists ();
214 return relocatable_schema_list;
218 g_settings_schema_finalize (GObject *object)
220 GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
222 gvdb_table_unref (schema->priv->table);
223 g_free (schema->priv->items);
224 g_free (schema->priv->name);
226 G_OBJECT_CLASS (g_settings_schema_parent_class)
231 g_settings_schema_init (GSettingsSchema *schema)
233 schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
234 GSettingsSchemaPrivate);
238 g_settings_schema_class_init (GSettingsSchemaClass *class)
240 GObjectClass *object_class = G_OBJECT_CLASS (class);
242 object_class->finalize = g_settings_schema_finalize;
244 g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
248 g_settings_schema_get_string (GSettingsSchema *schema,
251 const gchar *result = NULL;
254 if ((value = gvdb_table_get_raw_value (schema->priv->table, key)))
256 result = g_variant_get_string (value, NULL);
257 g_variant_unref (value);
264 g_settings_schema_new (const gchar *name)
266 GSettingsSchema *schema;
267 GvdbTable *table = NULL;
270 g_return_val_if_fail (name != NULL, NULL);
272 initialise_schema_sources ();
274 for (source = schema_sources; source; source = source->next)
276 GvdbTable *file = source->data;
278 if ((table = gvdb_table_get_table (file, name)))
283 g_error ("Settings schema '%s' is not installed\n", name);
285 schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
286 schema->priv->name = g_strdup (name);
287 schema->priv->table = table;
289 g_settings_schema_get_string (schema, ".path");
290 schema->priv->gettext_domain =
291 g_settings_schema_get_string (schema, ".gettext-domain");
293 if (schema->priv->gettext_domain)
294 bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
300 g_settings_schema_get_value (GSettingsSchema *schema,
306 value = gvdb_table_get_raw_value (schema->priv->table, key);
308 if G_UNLIKELY (value == NULL)
309 g_error ("schema does not contain a key named '%s'", key);
311 iter = g_variant_iter_new (value);
312 g_variant_unref (value);
318 g_settings_schema_get_path (GSettingsSchema *schema)
320 return schema->priv->path;
324 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
326 return schema->priv->gettext_domain;
330 g_settings_schema_has_key (GSettingsSchema *schema,
333 return gvdb_table_has_value (schema->priv->table, key);
337 g_settings_schema_list (GSettingsSchema *schema,
342 if (schema->priv->items == NULL)
347 list = gvdb_table_list (schema->priv->table, "");
348 len = g_strv_length (list);
350 schema->priv->items = g_new (GQuark, len);
353 for (i = 0; i < len; i++)
354 if (list[i][0] != '.')
355 schema->priv->items[j++] = g_quark_from_string (list[i]);
356 schema->priv->n_items = j;
361 *n_items = schema->priv->n_items;
362 return schema->priv->items;