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 add_item (gpointer key,
91 gchar ***ptr = user_data;
93 *(*ptr)++ = (gchar *) key;
97 * g_settings_list_schemas:
98 * @returns: a list of the schemas installed on the system
100 * Returns a list of GSettings schemas that are available. The list
101 * must not be modified or freed.
103 const gchar * const *
104 g_settings_list_schemas (void)
106 static gsize schema_list;
108 if (g_once_init_enter (&schema_list))
116 initialise_schema_sources ();
118 builder = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
120 for (source = schema_sources; source; source = source->next)
122 list = gvdb_table_list (source->data, "");
126 for (i = 0; list[i]; i++)
127 g_hash_table_insert (builder, list[i], NULL);
129 /* not strfreev: we stole the strings into the hashtable */
134 ptr = list = g_new (gchar *, g_hash_table_size (builder) + 1);
135 g_hash_table_foreach (builder, add_item, &ptr);
138 g_hash_table_steal_all (builder);
139 g_hash_table_unref (builder);
141 g_once_init_leave (&schema_list, (gsize) list);
144 return (const gchar **) schema_list;
148 g_settings_schema_finalize (GObject *object)
150 GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
152 gvdb_table_unref (schema->priv->table);
153 g_free (schema->priv->items);
154 g_free (schema->priv->name);
156 G_OBJECT_CLASS (g_settings_schema_parent_class)
161 g_settings_schema_init (GSettingsSchema *schema)
163 schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
164 GSettingsSchemaPrivate);
168 g_settings_schema_class_init (GSettingsSchemaClass *class)
170 GObjectClass *object_class = G_OBJECT_CLASS (class);
172 object_class->finalize = g_settings_schema_finalize;
174 g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
178 g_settings_schema_get_string (GSettingsSchema *schema,
181 const gchar *result = NULL;
184 if ((value = gvdb_table_get_value (schema->priv->table, key)))
186 result = g_variant_get_string (value, NULL);
187 g_variant_unref (value);
194 g_settings_schema_new (const gchar *name)
196 GSettingsSchema *schema;
197 GvdbTable *table = NULL;
200 initialise_schema_sources ();
202 for (source = schema_sources; source; source = source->next)
204 GvdbTable *file = source->data;
206 if ((table = gvdb_table_get_table (file, name)))
211 g_error ("Settings schema '%s' is not installed\n", name);
213 schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
214 schema->priv->name = g_strdup (name);
215 schema->priv->table = table;
217 g_settings_schema_get_string (schema, ".path");
218 schema->priv->gettext_domain =
219 g_settings_schema_get_string (schema, ".gettext-domain");
221 if (schema->priv->gettext_domain)
222 bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
228 g_settings_schema_get_value (GSettingsSchema *schema,
234 value = gvdb_table_get_value (schema->priv->table, key);
236 if G_UNLIKELY (value == NULL)
237 g_error ("schema does not contain a key named '%s'", key);
239 #if G_BYTE_ORDER == G_BIG_ENDIAN
243 tmp = g_variant_byteswap (value);
244 g_variant_unref (value);
249 iter = g_variant_iter_new (value);
250 g_variant_unref (value);
256 g_settings_schema_get_path (GSettingsSchema *schema)
258 return schema->priv->path;
262 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
264 return schema->priv->gettext_domain;
268 g_settings_schema_has_key (GSettingsSchema *schema,
271 return gvdb_table_has_value (schema->priv->table, key);
275 g_settings_schema_list (GSettingsSchema *schema,
280 if (schema->priv->items == NULL)
285 list = gvdb_table_list (schema->priv->table, "");
286 len = g_strv_length (list);
288 schema->priv->items = g_new (GQuark, len);
291 for (i = 0; i < len; i++)
292 if (list[i][0] != '.')
293 schema->priv->items[j++] = g_quark_from_string (list[i]);
294 schema->priv->n_items = j;
299 *n_items = schema->priv->n_items;
300 return schema->priv->items;