GSettingsSchema: use our own linked lists
[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-internal.h"
23 #include "gsettings.h"
24
25 #include "gvdb/gvdb-reader.h"
26 #include "strinfo.c"
27
28 #include <glibintl.h>
29 #include <string.h>
30
31 struct _GSettingsSchema
32 {
33   const gchar *gettext_domain;
34   const gchar *path;
35   GQuark *items;
36   gint n_items;
37   GvdbTable *table;
38   gchar *name;
39
40   gint ref_count;
41 };
42
43 typedef struct _GSettingsSchemaSource GSettingsSchemaSource;
44
45 struct _GSettingsSchemaSource
46 {
47   GSettingsSchemaSource *parent;
48   GvdbTable *table;
49 };
50
51 static GSettingsSchemaSource *schema_sources;
52
53 static void
54 prepend_schema_table (GvdbTable *table)
55 {
56   GSettingsSchemaSource *source;
57
58   source = g_slice_new (GSettingsSchemaSource);
59   source->parent = schema_sources;
60   source->table = table;
61
62   schema_sources = source;
63 }
64
65 static void
66 initialise_schema_sources (void)
67 {
68   static gsize initialised;
69
70   /* need a separate variable because 'schema_sources' may legitimately
71    * be null if we have zero valid schema sources
72    */
73   if G_UNLIKELY (g_once_init_enter (&initialised))
74     {
75       const gchar * const *dirs;
76       const gchar *path;
77       gint i;
78
79       /* iterate in reverse: count up, then count down */
80       dirs = g_get_system_data_dirs ();
81       for (i = 0; dirs[i]; i++);
82
83       while (i--)
84         {
85           gchar *filename;
86           GvdbTable *table;
87
88           filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
89           table = gvdb_table_new (filename, TRUE, NULL);
90
91           if (table != NULL)
92             prepend_schema_table (table);
93
94           g_free (filename);
95         }
96
97       if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
98         {
99           gchar *filename;
100           GvdbTable *table;
101
102           filename = g_build_filename (path, "gschemas.compiled", NULL);
103           table = gvdb_table_new (filename, TRUE, NULL);
104
105           if (table != NULL)
106             prepend_schema_table (table);
107
108           g_free (filename);
109         }
110
111       g_once_init_leave (&initialised, TRUE);
112     }
113 }
114
115 static gboolean
116 steal_item (gpointer key,
117             gpointer value,
118             gpointer user_data)
119 {
120   gchar ***ptr = user_data;
121
122   *(*ptr)++ = (gchar *) key;
123
124   return TRUE;
125 }
126
127 static const gchar * const *non_relocatable_schema_list;
128 static const gchar * const *relocatable_schema_list;
129 static gsize schema_lists_initialised;
130
131 static void
132 ensure_schema_lists (void)
133 {
134   if (g_once_init_enter (&schema_lists_initialised))
135     {
136       GSettingsSchemaSource *source;
137       GHashTable *single, *reloc;
138       const gchar **ptr;
139       gchar **list;
140       gint i;
141
142       initialise_schema_sources ();
143
144       /* We use hash tables to avoid duplicate listings for schemas that
145        * appear in more than one file.
146        */
147       single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
148       reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
149
150       for (source = schema_sources; source; source = source->parent)
151         {
152           list = gvdb_table_list (source->table, "");
153
154           g_assert (list != NULL);
155
156           for (i = 0; list[i]; i++)
157             {
158               if (!g_hash_table_lookup (single, list[i]) &&
159                   !g_hash_table_lookup (reloc, list[i]))
160                 {
161                   GvdbTable *table;
162
163                   table = gvdb_table_get_table (source->table, list[i]);
164                   g_assert (table != NULL);
165
166                   if (gvdb_table_has_value (table, ".path"))
167                     g_hash_table_insert (single, g_strdup (list[i]), NULL);
168                   else
169                     g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
170
171                   gvdb_table_unref (table);
172                 }
173             }
174
175           g_strfreev (list);
176         }
177
178       ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
179       non_relocatable_schema_list = ptr;
180       g_hash_table_foreach_steal (single, steal_item, &ptr);
181       g_hash_table_unref (single);
182       *ptr = NULL;
183
184       ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
185       relocatable_schema_list = ptr;
186       g_hash_table_foreach_steal (reloc, steal_item, &ptr);
187       g_hash_table_unref (reloc);
188       *ptr = NULL;
189
190       g_once_init_leave (&schema_lists_initialised, TRUE);
191     }
192 }
193
194 /**
195  * g_settings_list_schemas:
196  *
197  * Gets a list of the #GSettings schemas installed on the system.  The
198  * returned list is exactly the list of schemas for which you may call
199  * g_settings_new() without adverse effects.
200  *
201  * This function does not list the schemas that do not provide their own
202  * paths (ie: schemas for which you must use
203  * g_settings_new_with_path()).  See
204  * g_settings_list_relocatable_schemas() for that.
205  *
206  * Returns: (element-type utf8) (transfer none):  a list of #GSettings
207  *   schemas that are available.  The list must not be modified or
208  *   freed.
209  *
210  * Since: 2.26
211  **/
212 const gchar * const *
213 g_settings_list_schemas (void)
214 {
215   ensure_schema_lists ();
216
217   return non_relocatable_schema_list;
218 }
219
220 /**
221  * g_settings_list_relocatable_schemas:
222  *
223  * Gets a list of the relocatable #GSettings schemas installed on the
224  * system.  These are schemas that do not provide their own path.  It is
225  * usual to instantiate these schemas directly, but if you want to you
226  * can use g_settings_new_with_path() to specify the path.
227  *
228  * The output of this function, tTaken together with the output of
229  * g_settings_list_schemas() represents the complete list of all
230  * installed schemas.
231  *
232  * Returns: (element-type utf8) (transfer none): a list of relocatable
233  *   #GSettings schemas that are available.  The list must not be
234  *   modified or freed.
235  *
236  * Since: 2.28
237  **/
238 const gchar * const *
239 g_settings_list_relocatable_schemas (void)
240 {
241   ensure_schema_lists ();
242
243   return relocatable_schema_list;
244 }
245
246 GSettingsSchema *
247 g_settings_schema_ref (GSettingsSchema *schema)
248 {
249   g_atomic_int_inc (&schema->ref_count);
250
251   return schema;
252 }
253
254 void
255 g_settings_schema_unref (GSettingsSchema *schema)
256 {
257   if (g_atomic_int_dec_and_test (&schema->ref_count))
258     {
259       gvdb_table_unref (schema->table);
260       g_free (schema->items);
261       g_free (schema->name);
262
263       g_slice_free (GSettingsSchema, schema);
264     }
265 }
266
267 const gchar *
268 g_settings_schema_get_string (GSettingsSchema *schema,
269                               const gchar     *key)
270 {
271   const gchar *result = NULL;
272   GVariant *value;
273
274   if ((value = gvdb_table_get_raw_value (schema->table, key)))
275     {
276       result = g_variant_get_string (value, NULL);
277       g_variant_unref (value);
278     }
279
280   return result;
281 }
282
283 GSettingsSchema *
284 g_settings_schema_new (const gchar *name)
285 {
286   GSettingsSchemaSource *source;
287   GSettingsSchema *schema;
288   GvdbTable *table = NULL;
289
290   g_return_val_if_fail (name != NULL, NULL);
291
292   initialise_schema_sources ();
293
294   for (source = schema_sources; source; source = source->parent)
295     if ((table = gvdb_table_get_table (source->table, name)))
296       break;
297
298   if (table == NULL)
299     g_error ("Settings schema '%s' is not installed\n", name);
300
301   schema = g_slice_new0 (GSettingsSchema);
302   schema->ref_count = 1;
303   schema->name = g_strdup (name);
304   schema->table = table;
305   schema->path =
306     g_settings_schema_get_string (schema, ".path");
307   schema->gettext_domain =
308     g_settings_schema_get_string (schema, ".gettext-domain");
309
310   if (schema->gettext_domain)
311     bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
312
313   return schema;
314 }
315
316 GVariantIter *
317 g_settings_schema_get_value (GSettingsSchema *schema,
318                              const gchar     *key)
319 {
320   GVariantIter *iter;
321   GVariant *value;
322
323   value = gvdb_table_get_raw_value (schema->table, key);
324
325   if G_UNLIKELY (value == NULL)
326     g_error ("Settings schema '%s' does not contain a key named '%s'",
327              schema->name, key);
328
329   iter = g_variant_iter_new (value);
330   g_variant_unref (value);
331
332   return iter;
333 }
334
335 const gchar *
336 g_settings_schema_get_path (GSettingsSchema *schema)
337 {
338   return schema->path;
339 }
340
341 const gchar *
342 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
343 {
344   return schema->gettext_domain;
345 }
346
347 gboolean
348 g_settings_schema_has_key (GSettingsSchema *schema,
349                            const gchar     *key)
350 {
351   return gvdb_table_has_value (schema->table, key);
352 }
353
354 const GQuark *
355 g_settings_schema_list (GSettingsSchema *schema,
356                         gint            *n_items)
357 {
358   gint i, j;
359
360   if (schema->items == NULL)
361     {
362       gchar **list;
363       gint len;
364
365       list = gvdb_table_list (schema->table, "");
366       len = list ? g_strv_length (list) : 0;
367
368       schema->items = g_new (GQuark, len);
369       j = 0;
370
371       for (i = 0; i < len; i++)
372         if (list[i][0] != '.')
373           schema->items[j++] = g_quark_from_string (list[i]);
374       schema->n_items = j;
375
376       g_strfreev (list);
377     }
378
379   *n_items = schema->n_items;
380   return schema->items;
381 }
382
383 const gchar *
384 g_settings_schema_get_name (GSettingsSchema *schema)
385 {
386   return schema->name;
387 }
388
389 static inline void
390 endian_fixup (GVariant **value)
391 {
392 #if G_BYTE_ORDER == G_BIG_ENDIAN
393   GVariant *tmp;
394
395   tmp = g_variant_byteswap (*value);
396   g_variant_unref (*value);
397   *value = tmp;
398 #endif
399 }
400
401 void
402 g_settings_schema_key_init (GSettingsSchemaKey *key,
403                             GSettingsSchema    *schema,
404                             const gchar        *name)
405 {
406   GVariantIter *iter;
407   GVariant *data;
408   guchar code;
409
410   memset (key, 0, sizeof *key);
411
412   iter = g_settings_schema_get_value (schema, name);
413
414   key->schema = g_settings_schema_ref (schema);
415   key->default_value = g_variant_iter_next_value (iter);
416   endian_fixup (&key->default_value);
417   key->type = g_variant_get_type (key->default_value);
418   key->name = g_intern_string (name);
419
420   while (g_variant_iter_next (iter, "(y*)", &code, &data))
421     {
422       switch (code)
423         {
424         case 'l':
425           /* translation requested */
426           g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
427           break;
428
429         case 'e':
430           /* enumerated types... */
431           key->is_enum = TRUE;
432           goto choice;
433
434         case 'f':
435           /* flags... */
436           key->is_flags = TRUE;
437           goto choice;
438
439         choice: case 'c':
440           /* ..., choices, aliases */
441           key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
442           break;
443
444         case 'r':
445           g_variant_get (data, "(**)", &key->minimum, &key->maximum);
446           endian_fixup (&key->minimum);
447           endian_fixup (&key->maximum);
448           break;
449
450         default:
451           g_warning ("unknown schema extension '%c'", code);
452           break;
453         }
454
455       g_variant_unref (data);
456     }
457
458   g_variant_iter_free (iter);
459 }
460
461 void
462 g_settings_schema_key_clear (GSettingsSchemaKey *key)
463 {
464   if (key->minimum)
465     g_variant_unref (key->minimum);
466
467   if (key->maximum)
468     g_variant_unref (key->maximum);
469
470   g_variant_unref (key->default_value);
471
472   g_settings_schema_unref (key->schema);
473 }
474
475 gboolean
476 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
477                                   GVariant           *value)
478 {
479   g_return_val_if_fail (value != NULL, FALSE);
480
481   return g_variant_is_of_type (value, key->type);
482 }
483
484 gboolean
485 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
486                                    GVariant           *value)
487 {
488   if (key->minimum == NULL && key->strinfo == NULL)
489     return TRUE;
490
491   if (g_variant_is_container (value))
492     {
493       gboolean ok = TRUE;
494       GVariantIter iter;
495       GVariant *child;
496
497       g_variant_iter_init (&iter, value);
498       while (ok && (child = g_variant_iter_next_value (&iter)))
499         {
500           ok = g_settings_schema_key_range_check (key, child);
501           g_variant_unref (child);
502         }
503
504       return ok;
505     }
506
507   if (key->minimum)
508     {
509       return g_variant_compare (key->minimum, value) <= 0 &&
510              g_variant_compare (value, key->maximum) <= 0;
511     }
512
513   return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
514                                   g_variant_get_string (value, NULL));
515 }
516
517 GVariant *
518 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
519                                    GVariant           *value)
520 {
521   const gchar *target;
522
523   if (g_settings_schema_key_range_check (key, value))
524     return g_variant_ref (value);
525
526   if (key->strinfo == NULL)
527     return NULL;
528
529   if (g_variant_is_container (value))
530     {
531       GVariantBuilder builder;
532       GVariantIter iter;
533       GVariant *child;
534
535       g_variant_iter_init (&iter, value);
536       g_variant_builder_init (&builder, g_variant_get_type (value));
537
538       while ((child = g_variant_iter_next_value (&iter)))
539         {
540           GVariant *fixed;
541
542           fixed = g_settings_schema_key_range_fixup (key, child);
543           g_variant_unref (child);
544
545           if (fixed == NULL)
546             {
547               g_variant_builder_clear (&builder);
548               return NULL;
549             }
550
551           g_variant_builder_add_value (&builder, fixed);
552           g_variant_unref (fixed);
553         }
554
555       return g_variant_ref_sink (g_variant_builder_end (&builder));
556     }
557
558   target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
559                                       g_variant_get_string (value, NULL));
560   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
561 }
562
563
564 GVariant *
565 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
566 {
567   const gchar *translated;
568   GError *error = NULL;
569   const gchar *domain;
570   GVariant *value;
571
572   domain = g_settings_schema_get_gettext_domain (key->schema);
573
574   if (key->lc_char == '\0')
575     /* translation not requested for this key */
576     return NULL;
577
578   if (key->lc_char == 't')
579     translated = g_dcgettext (domain, key->unparsed, LC_TIME);
580   else
581     translated = g_dgettext (domain, key->unparsed);
582
583   if (translated == key->unparsed)
584     /* the default value was not translated */
585     return NULL;
586
587   /* try to parse the translation of the unparsed default */
588   value = g_variant_parse (key->type, translated, NULL, NULL, &error);
589
590   if (value == NULL)
591     {
592       g_warning ("Failed to parse translated string `%s' for "
593                  "key `%s' in schema `%s': %s", key->unparsed, key->name,
594                  g_settings_schema_get_name (key->schema), error->message);
595       g_warning ("Using untranslated default instead.");
596       g_error_free (error);
597     }
598
599   else if (!g_settings_schema_key_range_check (key, value))
600     {
601       g_warning ("Translated default `%s' for key `%s' in schema `%s' "
602                  "is outside of valid range", key->unparsed, key->name,
603                  g_settings_schema_get_name (key->schema));
604       g_variant_unref (value);
605       value = NULL;
606     }
607
608   return value;
609 }
610
611 gint
612 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
613                                GVariant           *value)
614 {
615   gboolean it_worked;
616   guint result;
617
618   it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
619                                         g_variant_get_string (value, NULL),
620                                         &result);
621
622   /* 'value' can only come from the backend after being filtered for validity,
623    * from the translation after being filtered for validity, or from the schema
624    * itself (which the schema compiler checks for validity).  If this assertion
625    * fails then it's really a bug in GSettings or the schema compiler...
626    */
627   g_assert (it_worked);
628
629   return result;
630 }
631
632 GVariant *
633 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
634                                  gint                value)
635 {
636   const gchar *string;
637
638   string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
639
640   if (string == NULL)
641     return NULL;
642
643   return g_variant_new_string (string);
644 }
645
646 guint
647 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
648                                 GVariant           *value)
649 {
650   GVariantIter iter;
651   const gchar *flag;
652   guint result;
653
654   result = 0;
655   g_variant_iter_init (&iter, value);
656   while (g_variant_iter_next (&iter, "&s", &flag))
657     {
658       gboolean it_worked;
659       guint flag_value;
660
661       it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
662       /* as in g_settings_to_enum() */
663       g_assert (it_worked);
664
665       result |= flag_value;
666     }
667
668   return result;
669 }
670
671 GVariant *
672 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
673                                   guint               value)
674 {
675   GVariantBuilder builder;
676   gint i;
677
678   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
679
680   for (i = 0; i < 32; i++)
681     if (value & (1u << i))
682       {
683         const gchar *string;
684
685         string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
686
687         if (string == NULL)
688           {
689             g_variant_builder_clear (&builder);
690             return NULL;
691           }
692
693         g_variant_builder_add (&builder, "s", string);
694       }
695
696   return g_variant_builder_end (&builder);
697 }