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", "gschemas.compiled", NULL);
57 table = gvdb_table_new (filename, TRUE, NULL);
60 schema_sources = g_slist_prepend (schema_sources, table);
65 schema_sources = g_slist_reverse (schema_sources);
67 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
72 filename = g_build_filename (path, "gschemas.compiled", NULL);
73 table = gvdb_table_new (filename, TRUE, NULL);
76 schema_sources = g_slist_prepend (schema_sources, table);
81 g_once_init_leave (&initialised, TRUE);
86 g_settings_schema_finalize (GObject *object)
88 GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
90 gvdb_table_unref (schema->priv->table);
91 g_free (schema->priv->items);
92 g_free (schema->priv->name);
94 G_OBJECT_CLASS (g_settings_schema_parent_class)
99 g_settings_schema_init (GSettingsSchema *schema)
101 schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
102 GSettingsSchemaPrivate);
106 g_settings_schema_class_init (GSettingsSchemaClass *class)
108 GObjectClass *object_class = G_OBJECT_CLASS (class);
110 object_class->finalize = g_settings_schema_finalize;
112 g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
116 g_settings_schema_get_string (GSettingsSchema *schema,
119 const gchar *result = NULL;
122 if ((value = gvdb_table_get_value (schema->priv->table, key)))
124 result = g_variant_get_string (value, NULL);
125 g_variant_unref (value);
132 g_settings_schema_new (const gchar *name)
134 GSettingsSchema *schema;
135 GvdbTable *table = NULL;
138 initialise_schema_sources ();
140 for (source = schema_sources; source; source = source->next)
142 GvdbTable *file = source->data;
144 if ((table = gvdb_table_get_table (file, name)))
149 g_error ("Settings schema '%s' is not installed\n", name);
151 schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
152 schema->priv->name = g_strdup (name);
153 schema->priv->table = table;
155 g_settings_schema_get_string (schema, ".path");
156 schema->priv->gettext_domain =
157 g_settings_schema_get_string (schema, ".gettext-domain");
159 if (schema->priv->gettext_domain)
160 bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
166 g_settings_schema_get_value (GSettingsSchema *schema,
172 value = gvdb_table_get_value (schema->priv->table, key);
174 if G_UNLIKELY (value == NULL)
175 g_error ("schema does not contain a key named '%s'", key);
177 #if G_BYTE_ORDER == G_BIG_ENDIAN
181 tmp = g_variant_byteswap (value);
182 g_variant_unref (value);
187 iter = g_variant_iter_new (value);
188 g_variant_unref (value);
194 g_settings_schema_get_path (GSettingsSchema *schema)
196 return schema->priv->path;
200 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
202 return schema->priv->gettext_domain;
206 g_settings_schema_has_key (GSettingsSchema *schema,
209 return gvdb_table_has_value (schema->priv->table, key);
213 g_settings_schema_list (GSettingsSchema *schema,
218 if (schema->priv->items == NULL)
223 list = gvdb_table_list (schema->priv->table, "");
224 len = g_strv_length (list);
226 schema->priv->items = g_new (GQuark, len);
229 for (i = 0; i < len; i++)
230 if (list[i][0] != '.')
231 schema->priv->items[j++] = g_quark_from_string (list[i]);
232 schema->priv->n_items = j;
237 *n_items = schema->priv->n_items;
238 return schema->priv->items;
242 * g_settings_schema_exists:
243 * @schema_name: the schema name to query for
244 * Returns: %TRUE if @schema_name exists
246 * Checks if the named schema is installed.
249 g_settings_schema_exists (const gchar *schema_name)
253 initialise_schema_sources ();
255 for (source = schema_sources; source; source = source->next)
257 GvdbTable *file = source->data;
259 if (gvdb_table_get_table (file, schema_name))