GSettings big endian tweaks
[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             }
143
144           g_strfreev (list);
145         }
146
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);
151       *ptr = NULL;
152
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);
157       *ptr = NULL;
158
159       g_once_init_leave (&schema_lists_initialised, TRUE);
160     }
161 }
162
163 /**
164  * g_settings_list_schemas:
165  *
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.
169  *
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.
174  *
175  * Returns: (element-type utf8) (transfer none):  a list of #GSettings
176  *   schemas that are available.  The list must not be modified or
177  *   freed.
178  *
179  * Since: 2.26
180  **/
181 const gchar * const *
182 g_settings_list_schemas (void)
183 {
184   ensure_schema_lists ();
185
186   return non_relocatable_schema_list;
187 }
188
189 /**
190  * g_settings_list_relocatable_schemas:
191  *
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.
196  *
197  * The output of this function, tTaken together with the output of
198  * g_settings_list_schemas() represents the complete list of all
199  * installed schemas.
200  *
201  * Returns: (element-type utf8) (transfer none): a list of relocatable
202  *   #GSettings schemas that are available.  The list must not be
203  *   modified or freed.
204  *
205  * Since: 2.28
206  **/
207 const gchar * const *
208 g_settings_list_relocatable_schemas (void)
209 {
210   ensure_schema_lists ();
211
212   return relocatable_schema_list;
213 }
214
215 static void
216 g_settings_schema_finalize (GObject *object)
217 {
218   GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
219
220   gvdb_table_unref (schema->priv->table);
221   g_free (schema->priv->items);
222   g_free (schema->priv->name);
223
224   G_OBJECT_CLASS (g_settings_schema_parent_class)
225     ->finalize (object);
226 }
227
228 static void
229 g_settings_schema_init (GSettingsSchema *schema)
230 {
231   schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
232                                               GSettingsSchemaPrivate);
233 }
234
235 static void
236 g_settings_schema_class_init (GSettingsSchemaClass *class)
237 {
238   GObjectClass *object_class = G_OBJECT_CLASS (class);
239
240   object_class->finalize = g_settings_schema_finalize;
241
242   g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
243 }
244
245 const gchar *
246 g_settings_schema_get_string (GSettingsSchema *schema,
247                               const gchar     *key)
248 {
249   const gchar *result = NULL;
250   GVariant *value;
251
252   if ((value = gvdb_table_get_raw_value (schema->priv->table, key)))
253     {
254       result = g_variant_get_string (value, NULL);
255       g_variant_unref (value);
256     }
257
258   return result;
259 }
260
261 GSettingsSchema *
262 g_settings_schema_new (const gchar *name)
263 {
264   GSettingsSchema *schema;
265   GvdbTable *table = NULL;
266   GSList *source;
267
268   initialise_schema_sources ();
269
270   for (source = schema_sources; source; source = source->next)
271     {
272       GvdbTable *file = source->data;
273
274       if ((table = gvdb_table_get_table (file, name)))
275         break;
276     }
277
278   if (table == NULL)
279     g_error ("Settings schema '%s' is not installed\n", name);
280
281   schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
282   schema->priv->name = g_strdup (name);
283   schema->priv->table = table;
284   schema->priv->path =
285     g_settings_schema_get_string (schema, ".path");
286   schema->priv->gettext_domain =
287     g_settings_schema_get_string (schema, ".gettext-domain");
288
289   if (schema->priv->gettext_domain)
290     bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
291
292   return schema;
293 }
294
295 GVariantIter *
296 g_settings_schema_get_value (GSettingsSchema *schema,
297                              const gchar     *key)
298 {
299   GVariantIter *iter;
300   GVariant *value;
301
302   value = gvdb_table_get_value (schema->priv->table, key);
303
304   if G_UNLIKELY (value == NULL)
305     g_error ("schema does not contain a key named '%s'", key);
306
307   iter = g_variant_iter_new (value);
308   g_variant_unref (value);
309
310   return iter;
311 }
312
313 const gchar *
314 g_settings_schema_get_path (GSettingsSchema *schema)
315 {
316   return schema->priv->path;
317 }
318
319 const gchar *
320 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
321 {
322   return schema->priv->gettext_domain;
323 }
324
325 gboolean
326 g_settings_schema_has_key (GSettingsSchema *schema,
327                            const gchar     *key)
328 {
329   return gvdb_table_has_value (schema->priv->table, key);
330 }
331
332 const GQuark *
333 g_settings_schema_list (GSettingsSchema *schema,
334                         gint            *n_items)
335 {
336   gint i, j;
337
338   if (schema->priv->items == NULL)
339     {
340       gchar **list;
341       gint len;
342
343       list = gvdb_table_list (schema->priv->table, "");
344       len = g_strv_length (list);
345
346       schema->priv->items = g_new (GQuark, len);
347       j = 0;
348
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;
353
354       g_strfreev (list);
355     }
356
357   *n_items = schema->priv->n_items;
358   return schema->priv->items;
359 }