GSettings: don't abort on missing schemas
[platform/upstream/glib.git] / gio / gsettingsschema.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include "gsettingsschema.h"
23 #include "gvdb/gvdb-reader.h"
24
25 #include <glibintl.h>
26
27 G_DEFINE_TYPE (GSettingsSchema, g_settings_schema, G_TYPE_OBJECT)
28
29 struct _GSettingsSchemaPrivate
30 {
31   const gchar *gettext_domain;
32   const gchar *path;
33   GQuark *items;
34   gint n_items;
35   GvdbTable *table;
36   gchar *name;
37 };
38
39 static GSList *schema_sources;
40
41 static void
42 initialise_schema_sources (void)
43 {
44   static gsize initialised;
45
46   if G_UNLIKELY (g_once_init_enter (&initialised))
47     {
48       const gchar * const *dir;
49       const gchar *path;
50
51       for (dir = g_get_system_data_dirs (); *dir; dir++)
52         {
53           gchar *filename;
54           GvdbTable *table;
55
56           filename = g_build_filename (*dir, "glib-2.0", "schemas",
57                                        "gschemas.compiled", NULL);
58           table = gvdb_table_new (filename, TRUE, NULL);
59
60           if (table != NULL)
61             schema_sources = g_slist_prepend (schema_sources, table);
62
63           g_free (filename);
64         }
65
66       schema_sources = g_slist_reverse (schema_sources);
67
68       if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
69         {
70           gchar *filename;
71           GvdbTable *table;
72
73           filename = g_build_filename (path, "gschemas.compiled", NULL);
74           table = gvdb_table_new (filename, TRUE, NULL);
75
76           if (table != NULL)
77             schema_sources = g_slist_prepend (schema_sources, table);
78
79           g_free (filename);
80         }
81
82       g_once_init_leave (&initialised, TRUE);
83     }
84 }
85
86 static gboolean
87 steal_item (gpointer key,
88             gpointer value,
89             gpointer user_data)
90 {
91   gchar ***ptr = user_data;
92
93   *(*ptr)++ = (gchar *) key;
94
95   return TRUE;
96 }
97
98 static const gchar * const *non_relocatable_schema_list;
99 static const gchar * const *relocatable_schema_list;
100 static gsize schema_lists_initialised;
101
102 static void
103 ensure_schema_lists (void)
104 {
105   if (g_once_init_enter (&schema_lists_initialised))
106     {
107       GHashTable *single, *reloc;
108       const gchar **ptr;
109       GSList *source;
110       gchar **list;
111       gint i;
112
113       initialise_schema_sources ();
114
115       /* We use hash tables to avoid duplicate listings for schemas that
116        * appear in more than one file.
117        */
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);
120
121       for (source = schema_sources; source; source = source->next)
122         {
123           list = gvdb_table_list (source->data, "");
124
125           g_assert (list != NULL);
126
127           for (i = 0; list[i]; i++)
128             {
129               if (!g_hash_table_lookup (single, list[i]) &&
130                   !g_hash_table_lookup (reloc, list[i]))
131                 {
132                   GvdbTable *table;
133
134                   table = gvdb_table_get_table (source->data, list[i]);
135                   g_assert (table != NULL);
136
137                   if (gvdb_table_has_value (table, ".path"))
138                     g_hash_table_insert (single, g_strdup (list[i]), NULL);
139                   else
140                     g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
141
142                   gvdb_table_unref (table);
143                 }
144             }
145
146           g_strfreev (list);
147         }
148
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);
153       *ptr = NULL;
154
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);
159       *ptr = NULL;
160
161       g_once_init_leave (&schema_lists_initialised, TRUE);
162     }
163 }
164
165 /**
166  * g_settings_list_schemas:
167  *
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.
171  *
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.
176  *
177  * Returns: (element-type utf8) (transfer none):  a list of #GSettings
178  *   schemas that are available.  The list must not be modified or
179  *   freed.
180  *
181  * Since: 2.26
182  **/
183 const gchar * const *
184 g_settings_list_schemas (void)
185 {
186   ensure_schema_lists ();
187
188   return non_relocatable_schema_list;
189 }
190
191 /**
192  * g_settings_list_relocatable_schemas:
193  *
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.
198  *
199  * The output of this function, tTaken together with the output of
200  * g_settings_list_schemas() represents the complete list of all
201  * installed schemas.
202  *
203  * Returns: (element-type utf8) (transfer none): a list of relocatable
204  *   #GSettings schemas that are available.  The list must not be
205  *   modified or freed.
206  *
207  * Since: 2.28
208  **/
209 const gchar * const *
210 g_settings_list_relocatable_schemas (void)
211 {
212   ensure_schema_lists ();
213
214   return relocatable_schema_list;
215 }
216
217 static void
218 g_settings_schema_finalize (GObject *object)
219 {
220   GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
221
222   gvdb_table_unref (schema->priv->table);
223   g_free (schema->priv->items);
224   g_free (schema->priv->name);
225
226   G_OBJECT_CLASS (g_settings_schema_parent_class)
227     ->finalize (object);
228 }
229
230 static void
231 g_settings_schema_init (GSettingsSchema *schema)
232 {
233   schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
234                                               GSettingsSchemaPrivate);
235 }
236
237 static void
238 g_settings_schema_class_init (GSettingsSchemaClass *class)
239 {
240   GObjectClass *object_class = G_OBJECT_CLASS (class);
241
242   object_class->finalize = g_settings_schema_finalize;
243
244   g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
245 }
246
247 const gchar *
248 g_settings_schema_get_string (GSettingsSchema *schema,
249                               const gchar     *key)
250 {
251   const gchar *result = NULL;
252   GVariant *value;
253
254   if ((value = gvdb_table_get_raw_value (schema->priv->table, key)))
255     {
256       result = g_variant_get_string (value, NULL);
257       g_variant_unref (value);
258     }
259
260   return result;
261 }
262
263 GSettingsSchema *
264 g_settings_schema_new (const gchar *name)
265 {
266   GSettingsSchema *schema;
267   GvdbTable *table = NULL;
268   GSList *source;
269
270   g_return_val_if_fail (name != NULL, NULL);
271
272   initialise_schema_sources ();
273
274   for (source = schema_sources; source; source = source->next)
275     {
276       GvdbTable *file = source->data;
277
278       if ((table = gvdb_table_get_table (file, name)))
279         break;
280     }
281
282   if (table == NULL)
283     {
284       g_critical ("Settings schema '%s' is not installed\n", name);
285       return NULL;
286     }
287
288   schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
289   schema->priv->name = g_strdup (name);
290   schema->priv->table = table;
291   schema->priv->path =
292     g_settings_schema_get_string (schema, ".path");
293   schema->priv->gettext_domain =
294     g_settings_schema_get_string (schema, ".gettext-domain");
295
296   if (schema->priv->gettext_domain)
297     bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
298
299   return schema;
300 }
301
302 GVariantIter *
303 g_settings_schema_get_value (GSettingsSchema *schema,
304                              const gchar     *key)
305 {
306   GVariantIter *iter;
307   GVariant *value;
308
309   value = gvdb_table_get_raw_value (schema->priv->table, key);
310
311   if G_UNLIKELY (value == NULL)
312     g_error ("Settings schema '%s' does not contain a key named '%s'",
313              schema->priv->name, key);
314
315   iter = g_variant_iter_new (value);
316   g_variant_unref (value);
317
318   return iter;
319 }
320
321 const gchar *
322 g_settings_schema_get_path (GSettingsSchema *schema)
323 {
324   return schema->priv->path;
325 }
326
327 const gchar *
328 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
329 {
330   return schema->priv->gettext_domain;
331 }
332
333 gboolean
334 g_settings_schema_has_key (GSettingsSchema *schema,
335                            const gchar     *key)
336 {
337   return gvdb_table_has_value (schema->priv->table, key);
338 }
339
340 const GQuark *
341 g_settings_schema_list (GSettingsSchema *schema,
342                         gint            *n_items)
343 {
344   gint i, j;
345
346   if (schema->priv->items == NULL)
347     {
348       gchar **list;
349       gint len;
350
351       list = gvdb_table_list (schema->priv->table, "");
352       len = g_strv_length (list);
353
354       schema->priv->items = g_new (GQuark, len);
355       j = 0;
356
357       for (i = 0; i < len; i++)
358         if (list[i][0] != '.')
359           schema->priv->items[j++] = g_quark_from_string (list[i]);
360       schema->priv->n_items = j;
361
362       g_strfreev (list);
363     }
364
365   *n_items = schema->priv->n_items;
366   return schema->priv->items;
367 }