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 "gsettings.h"
25 #include "gvdb/gvdb-reader.h"
29 G_DEFINE_TYPE (GSettingsSchema, g_settings_schema, G_TYPE_OBJECT)
31 struct _GSettingsSchemaPrivate
33 const gchar *gettext_domain;
41 static GSList *schema_sources;
44 initialise_schema_sources (void)
46 static gsize initialised;
48 if G_UNLIKELY (g_once_init_enter (&initialised))
50 const gchar * const *dir;
53 for (dir = g_get_system_data_dirs (); *dir; dir++)
58 filename = g_build_filename (*dir, "glib-2.0", "schemas",
59 "gschemas.compiled", NULL);
60 table = gvdb_table_new (filename, TRUE, NULL);
63 schema_sources = g_slist_prepend (schema_sources, table);
68 schema_sources = g_slist_reverse (schema_sources);
70 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
75 filename = g_build_filename (path, "gschemas.compiled", NULL);
76 table = gvdb_table_new (filename, TRUE, NULL);
79 schema_sources = g_slist_prepend (schema_sources, table);
84 g_once_init_leave (&initialised, TRUE);
89 steal_item (gpointer key,
93 gchar ***ptr = user_data;
95 *(*ptr)++ = (gchar *) key;
100 static const gchar * const *non_relocatable_schema_list;
101 static const gchar * const *relocatable_schema_list;
102 static gsize schema_lists_initialised;
105 ensure_schema_lists (void)
107 if (g_once_init_enter (&schema_lists_initialised))
109 GHashTable *single, *reloc;
115 initialise_schema_sources ();
117 /* We use hash tables to avoid duplicate listings for schemas that
118 * appear in more than one file.
120 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
121 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
123 for (source = schema_sources; source; source = source->next)
125 list = gvdb_table_list (source->data, "");
127 g_assert (list != NULL);
129 for (i = 0; list[i]; i++)
131 if (!g_hash_table_lookup (single, list[i]) &&
132 !g_hash_table_lookup (reloc, list[i]))
136 table = gvdb_table_get_table (source->data, list[i]);
137 g_assert (table != NULL);
139 if (gvdb_table_has_value (table, ".path"))
140 g_hash_table_insert (single, g_strdup (list[i]), NULL);
142 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
144 gvdb_table_unref (table);
151 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
152 non_relocatable_schema_list = ptr;
153 g_hash_table_foreach_steal (single, steal_item, &ptr);
154 g_hash_table_unref (single);
157 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
158 relocatable_schema_list = ptr;
159 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
160 g_hash_table_unref (reloc);
163 g_once_init_leave (&schema_lists_initialised, TRUE);
168 * g_settings_list_schemas:
170 * Gets a list of the #GSettings schemas installed on the system. The
171 * returned list is exactly the list of schemas for which you may call
172 * g_settings_new() without adverse effects.
174 * This function does not list the schemas that do not provide their own
175 * paths (ie: schemas for which you must use
176 * g_settings_new_with_path()). See
177 * g_settings_list_relocatable_schemas() for that.
179 * Returns: (element-type utf8) (transfer none): a list of #GSettings
180 * schemas that are available. The list must not be modified or
185 const gchar * const *
186 g_settings_list_schemas (void)
188 ensure_schema_lists ();
190 return non_relocatable_schema_list;
194 * g_settings_list_relocatable_schemas:
196 * Gets a list of the relocatable #GSettings schemas installed on the
197 * system. These are schemas that do not provide their own path. It is
198 * usual to instantiate these schemas directly, but if you want to you
199 * can use g_settings_new_with_path() to specify the path.
201 * The output of this function, tTaken together with the output of
202 * g_settings_list_schemas() represents the complete list of all
205 * Returns: (element-type utf8) (transfer none): a list of relocatable
206 * #GSettings schemas that are available. The list must not be
211 const gchar * const *
212 g_settings_list_relocatable_schemas (void)
214 ensure_schema_lists ();
216 return relocatable_schema_list;
220 g_settings_schema_finalize (GObject *object)
222 GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
224 gvdb_table_unref (schema->priv->table);
225 g_free (schema->priv->items);
226 g_free (schema->priv->name);
228 G_OBJECT_CLASS (g_settings_schema_parent_class)
233 g_settings_schema_init (GSettingsSchema *schema)
235 schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
236 GSettingsSchemaPrivate);
240 g_settings_schema_class_init (GSettingsSchemaClass *class)
242 GObjectClass *object_class = G_OBJECT_CLASS (class);
244 object_class->finalize = g_settings_schema_finalize;
246 g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
250 g_settings_schema_get_string (GSettingsSchema *schema,
253 const gchar *result = NULL;
256 if ((value = gvdb_table_get_raw_value (schema->priv->table, key)))
258 result = g_variant_get_string (value, NULL);
259 g_variant_unref (value);
266 g_settings_schema_new (const gchar *name)
268 GSettingsSchema *schema;
269 GvdbTable *table = NULL;
272 g_return_val_if_fail (name != NULL, NULL);
274 initialise_schema_sources ();
276 for (source = schema_sources; source; source = source->next)
278 GvdbTable *file = source->data;
280 if ((table = gvdb_table_get_table (file, name)))
285 g_error ("Settings schema '%s' is not installed\n", name);
287 schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
288 schema->priv->name = g_strdup (name);
289 schema->priv->table = table;
291 g_settings_schema_get_string (schema, ".path");
292 schema->priv->gettext_domain =
293 g_settings_schema_get_string (schema, ".gettext-domain");
295 if (schema->priv->gettext_domain)
296 bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
302 g_settings_schema_get_value (GSettingsSchema *schema,
308 value = gvdb_table_get_raw_value (schema->priv->table, key);
310 if G_UNLIKELY (value == NULL)
311 g_error ("Settings schema '%s' does not contain a key named '%s'",
312 schema->priv->name, key);
314 iter = g_variant_iter_new (value);
315 g_variant_unref (value);
321 g_settings_schema_get_path (GSettingsSchema *schema)
323 return schema->priv->path;
327 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
329 return schema->priv->gettext_domain;
333 g_settings_schema_has_key (GSettingsSchema *schema,
336 return gvdb_table_has_value (schema->priv->table, key);
340 g_settings_schema_list (GSettingsSchema *schema,
345 if (schema->priv->items == NULL)
350 list = gvdb_table_list (schema->priv->table, "");
351 len = list ? g_strv_length (list) : 0;
353 schema->priv->items = g_new (GQuark, len);
356 for (i = 0; i < len; i++)
357 if (list[i][0] != '.')
358 schema->priv->items[j++] = g_quark_from_string (list[i]);
359 schema->priv->n_items = j;
364 *n_items = schema->priv->n_items;
365 return schema->priv->items;