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