drop the now-trivial g_settings_schema_new
[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 *name;
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_name,
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_name != NULL, NULL);
161
162   table = gvdb_table_get_table (source->table, schema_name);
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_name)))
167         break;
168
169   if (table == NULL)
170     return NULL;
171
172   schema = g_slice_new0 (GSettingsSchema);
173   schema->ref_count = 1;
174   schema->name = g_strdup (schema_name);
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->name);
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'",
364              schema->name, key);
365
366   iter = g_variant_iter_new (value);
367   g_variant_unref (value);
368
369   return iter;
370 }
371
372 const gchar *
373 g_settings_schema_get_path (GSettingsSchema *schema)
374 {
375   return schema->path;
376 }
377
378 const gchar *
379 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
380 {
381   return schema->gettext_domain;
382 }
383
384 gboolean
385 g_settings_schema_has_key (GSettingsSchema *schema,
386                            const gchar     *key)
387 {
388   return gvdb_table_has_value (schema->table, key);
389 }
390
391 const GQuark *
392 g_settings_schema_list (GSettingsSchema *schema,
393                         gint            *n_items)
394 {
395   gint i, j;
396
397   if (schema->items == NULL)
398     {
399       gchar **list;
400       gint len;
401
402       list = gvdb_table_list (schema->table, "");
403       len = list ? g_strv_length (list) : 0;
404
405       schema->items = g_new (GQuark, len);
406       j = 0;
407
408       for (i = 0; i < len; i++)
409         if (list[i][0] != '.')
410           schema->items[j++] = g_quark_from_string (list[i]);
411       schema->n_items = j;
412
413       g_strfreev (list);
414     }
415
416   *n_items = schema->n_items;
417   return schema->items;
418 }
419
420 const gchar *
421 g_settings_schema_get_name (GSettingsSchema *schema)
422 {
423   return schema->name;
424 }
425
426 static inline void
427 endian_fixup (GVariant **value)
428 {
429 #if G_BYTE_ORDER == G_BIG_ENDIAN
430   GVariant *tmp;
431
432   tmp = g_variant_byteswap (*value);
433   g_variant_unref (*value);
434   *value = tmp;
435 #endif
436 }
437
438 void
439 g_settings_schema_key_init (GSettingsSchemaKey *key,
440                             GSettingsSchema    *schema,
441                             const gchar        *name)
442 {
443   GVariantIter *iter;
444   GVariant *data;
445   guchar code;
446
447   memset (key, 0, sizeof *key);
448
449   iter = g_settings_schema_get_value (schema, name);
450
451   key->schema = g_settings_schema_ref (schema);
452   key->default_value = g_variant_iter_next_value (iter);
453   endian_fixup (&key->default_value);
454   key->type = g_variant_get_type (key->default_value);
455   key->name = g_intern_string (name);
456
457   while (g_variant_iter_next (iter, "(y*)", &code, &data))
458     {
459       switch (code)
460         {
461         case 'l':
462           /* translation requested */
463           g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
464           break;
465
466         case 'e':
467           /* enumerated types... */
468           key->is_enum = TRUE;
469           goto choice;
470
471         case 'f':
472           /* flags... */
473           key->is_flags = TRUE;
474           goto choice;
475
476         choice: case 'c':
477           /* ..., choices, aliases */
478           key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
479           break;
480
481         case 'r':
482           g_variant_get (data, "(**)", &key->minimum, &key->maximum);
483           endian_fixup (&key->minimum);
484           endian_fixup (&key->maximum);
485           break;
486
487         default:
488           g_warning ("unknown schema extension '%c'", code);
489           break;
490         }
491
492       g_variant_unref (data);
493     }
494
495   g_variant_iter_free (iter);
496 }
497
498 void
499 g_settings_schema_key_clear (GSettingsSchemaKey *key)
500 {
501   if (key->minimum)
502     g_variant_unref (key->minimum);
503
504   if (key->maximum)
505     g_variant_unref (key->maximum);
506
507   g_variant_unref (key->default_value);
508
509   g_settings_schema_unref (key->schema);
510 }
511
512 gboolean
513 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
514                                   GVariant           *value)
515 {
516   g_return_val_if_fail (value != NULL, FALSE);
517
518   return g_variant_is_of_type (value, key->type);
519 }
520
521 gboolean
522 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
523                                    GVariant           *value)
524 {
525   if (key->minimum == NULL && key->strinfo == NULL)
526     return TRUE;
527
528   if (g_variant_is_container (value))
529     {
530       gboolean ok = TRUE;
531       GVariantIter iter;
532       GVariant *child;
533
534       g_variant_iter_init (&iter, value);
535       while (ok && (child = g_variant_iter_next_value (&iter)))
536         {
537           ok = g_settings_schema_key_range_check (key, child);
538           g_variant_unref (child);
539         }
540
541       return ok;
542     }
543
544   if (key->minimum)
545     {
546       return g_variant_compare (key->minimum, value) <= 0 &&
547              g_variant_compare (value, key->maximum) <= 0;
548     }
549
550   return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
551                                   g_variant_get_string (value, NULL));
552 }
553
554 GVariant *
555 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
556                                    GVariant           *value)
557 {
558   const gchar *target;
559
560   if (g_settings_schema_key_range_check (key, value))
561     return g_variant_ref (value);
562
563   if (key->strinfo == NULL)
564     return NULL;
565
566   if (g_variant_is_container (value))
567     {
568       GVariantBuilder builder;
569       GVariantIter iter;
570       GVariant *child;
571
572       g_variant_iter_init (&iter, value);
573       g_variant_builder_init (&builder, g_variant_get_type (value));
574
575       while ((child = g_variant_iter_next_value (&iter)))
576         {
577           GVariant *fixed;
578
579           fixed = g_settings_schema_key_range_fixup (key, child);
580           g_variant_unref (child);
581
582           if (fixed == NULL)
583             {
584               g_variant_builder_clear (&builder);
585               return NULL;
586             }
587
588           g_variant_builder_add_value (&builder, fixed);
589           g_variant_unref (fixed);
590         }
591
592       return g_variant_ref_sink (g_variant_builder_end (&builder));
593     }
594
595   target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
596                                       g_variant_get_string (value, NULL));
597   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
598 }
599
600
601 GVariant *
602 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
603 {
604   const gchar *translated;
605   GError *error = NULL;
606   const gchar *domain;
607   GVariant *value;
608
609   domain = g_settings_schema_get_gettext_domain (key->schema);
610
611   if (key->lc_char == '\0')
612     /* translation not requested for this key */
613     return NULL;
614
615   if (key->lc_char == 't')
616     translated = g_dcgettext (domain, key->unparsed, LC_TIME);
617   else
618     translated = g_dgettext (domain, key->unparsed);
619
620   if (translated == key->unparsed)
621     /* the default value was not translated */
622     return NULL;
623
624   /* try to parse the translation of the unparsed default */
625   value = g_variant_parse (key->type, translated, NULL, NULL, &error);
626
627   if (value == NULL)
628     {
629       g_warning ("Failed to parse translated string `%s' for "
630                  "key `%s' in schema `%s': %s", key->unparsed, key->name,
631                  g_settings_schema_get_name (key->schema), error->message);
632       g_warning ("Using untranslated default instead.");
633       g_error_free (error);
634     }
635
636   else if (!g_settings_schema_key_range_check (key, value))
637     {
638       g_warning ("Translated default `%s' for key `%s' in schema `%s' "
639                  "is outside of valid range", key->unparsed, key->name,
640                  g_settings_schema_get_name (key->schema));
641       g_variant_unref (value);
642       value = NULL;
643     }
644
645   return value;
646 }
647
648 gint
649 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
650                                GVariant           *value)
651 {
652   gboolean it_worked;
653   guint result;
654
655   it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
656                                         g_variant_get_string (value, NULL),
657                                         &result);
658
659   /* 'value' can only come from the backend after being filtered for validity,
660    * from the translation after being filtered for validity, or from the schema
661    * itself (which the schema compiler checks for validity).  If this assertion
662    * fails then it's really a bug in GSettings or the schema compiler...
663    */
664   g_assert (it_worked);
665
666   return result;
667 }
668
669 GVariant *
670 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
671                                  gint                value)
672 {
673   const gchar *string;
674
675   string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
676
677   if (string == NULL)
678     return NULL;
679
680   return g_variant_new_string (string);
681 }
682
683 guint
684 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
685                                 GVariant           *value)
686 {
687   GVariantIter iter;
688   const gchar *flag;
689   guint result;
690
691   result = 0;
692   g_variant_iter_init (&iter, value);
693   while (g_variant_iter_next (&iter, "&s", &flag))
694     {
695       gboolean it_worked;
696       guint flag_value;
697
698       it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
699       /* as in g_settings_to_enum() */
700       g_assert (it_worked);
701
702       result |= flag_value;
703     }
704
705   return result;
706 }
707
708 GVariant *
709 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
710                                   guint               value)
711 {
712   GVariantBuilder builder;
713   gint i;
714
715   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
716
717   for (i = 0; i < 32; i++)
718     if (value & (1u << i))
719       {
720         const gchar *string;
721
722         string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
723
724         if (string == NULL)
725           {
726             g_variant_builder_clear (&builder);
727             return NULL;
728           }
729
730         g_variant_builder_add (&builder, "s", string);
731       }
732
733   return g_variant_builder_end (&builder);
734 }