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);
147 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
148 non_relocatable_schema_list = ptr;
149 g_hash_table_foreach_steal (single, steal_item, &ptr);
150 g_hash_table_unref (single);
153 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
154 relocatable_schema_list = ptr;
155 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
156 g_hash_table_unref (reloc);
159 g_once_init_leave (&schema_lists_initialised, TRUE);
164 * g_settings_list_schemas:
166 * Gets a list of the #GSettings schemas installed on the system. The
167 * returned list is exactly the list of schemas for which you may call
168 * g_settings_new() without adverse effects.
170 * This function does not list the schemas that do not provide their own
171 * paths (ie: schemas for which you must use
172 * g_settings_new_with_path()). See
173 * g_settings_list_relocatable_schemas() for that.
175 * Returns: (element-type utf8) (transfer none): a list of #GSettings
176 * schemas that are available. The list must not be modified or
181 const gchar * const *
182 g_settings_list_schemas (void)
184 ensure_schema_lists ();
186 return non_relocatable_schema_list;
190 * g_settings_list_relocatable_schemas:
192 * Gets a list of the relocatable #GSettings schemas installed on the
193 * system. These are schemas that do not provide their own path. It is
194 * usual to instantiate these schemas directly, but if you want to you
195 * can use g_settings_new_with_path() to specify the path.
197 * The output of this function, tTaken together with the output of
198 * g_settings_list_schemas() represents the complete list of all
201 * Returns: (element-type utf8) (transfer none): a list of relocatable
202 * #GSettings schemas that are available. The list must not be
207 const gchar * const *
208 g_settings_list_relocatable_schemas (void)
210 ensure_schema_lists ();
212 return relocatable_schema_list;
216 g_settings_schema_finalize (GObject *object)
218 GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
220 gvdb_table_unref (schema->priv->table);
221 g_free (schema->priv->items);
222 g_free (schema->priv->name);
224 G_OBJECT_CLASS (g_settings_schema_parent_class)
229 g_settings_schema_init (GSettingsSchema *schema)
231 schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
232 GSettingsSchemaPrivate);
236 g_settings_schema_class_init (GSettingsSchemaClass *class)
238 GObjectClass *object_class = G_OBJECT_CLASS (class);
240 object_class->finalize = g_settings_schema_finalize;
242 g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
246 g_settings_schema_get_string (GSettingsSchema *schema,
249 const gchar *result = NULL;
252 if ((value = gvdb_table_get_raw_value (schema->priv->table, key)))
254 result = g_variant_get_string (value, NULL);
255 g_variant_unref (value);
262 g_settings_schema_new (const gchar *name)
264 GSettingsSchema *schema;
265 GvdbTable *table = NULL;
268 initialise_schema_sources ();
270 for (source = schema_sources; source; source = source->next)
272 GvdbTable *file = source->data;
274 if ((table = gvdb_table_get_table (file, name)))
279 g_error ("Settings schema '%s' is not installed\n", name);
281 schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
282 schema->priv->name = g_strdup (name);
283 schema->priv->table = table;
285 g_settings_schema_get_string (schema, ".path");
286 schema->priv->gettext_domain =
287 g_settings_schema_get_string (schema, ".gettext-domain");
289 if (schema->priv->gettext_domain)
290 bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
296 g_settings_schema_get_value (GSettingsSchema *schema,
302 value = gvdb_table_get_raw_value (schema->priv->table, key);
304 if G_UNLIKELY (value == NULL)
305 g_error ("schema does not contain a key named '%s'", key);
307 iter = g_variant_iter_new (value);
308 g_variant_unref (value);
314 g_settings_schema_get_path (GSettingsSchema *schema)
316 return schema->priv->path;
320 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
322 return schema->priv->gettext_domain;
326 g_settings_schema_has_key (GSettingsSchema *schema,
329 return gvdb_table_has_value (schema->priv->table, key);
333 g_settings_schema_list (GSettingsSchema *schema,
338 if (schema->priv->items == NULL)
343 list = gvdb_table_list (schema->priv->table, "");
344 len = g_strv_length (list);
346 schema->priv->items = g_new (GQuark, len);
349 for (i = 0; i < len; i++)
350 if (list[i][0] != '.')
351 schema->priv->items[j++] = g_quark_from_string (list[i]);
352 schema->priv->n_items = j;
357 *n_items = schema->priv->n_items;
358 return schema->priv->items;