g_settings_list_children: only list viable schemas
[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 <locale.h>
31 #include <string.h>
32
33 /**
34  * SECTION:gsettingsschema
35  * @short_description: Introspecting and controlling the loading of
36  *                     GSettings schemas
37  *
38  * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
39  * mechanism for advanced control over the loading of schemas and a
40  * mechanism for introspecting their content.
41  *
42  * Plugin loading systems that wish to provide plugins a way to access
43  * settings face the problem of how to make the schemas for these
44  * settings visible to GSettings.  Typically, a plugin will want to ship
45  * the schema along with itself and it won't be installed into the
46  * standard system directories for schemas.
47  *
48  * #GSettingsSchemaSource provides a mechanism for dealing with this by
49  * allowing the creation of a new 'schema source' from which schemas can
50  * be acquired.  This schema source can then become part of the metadata
51  * associated with the plugin and queried whenever the plugin requires
52  * access to some settings.
53  *
54  * Consider the following example:
55  *
56  * |[
57  * typedef struct
58  * {
59  *    ...
60  *    GSettingsSchemaSource *schema_source;
61  *    ...
62  * } Plugin;
63  *
64  * Plugin *
65  * initialise_plugin (const gchar *dir)
66  * {
67  *   Plugin *plugin;
68  *
69  *   ...
70  *
71  *   plugin->schema_source =
72  *     g_settings_new_schema_source_from_directory (dir,
73  *       g_settings_schema_source_get_default (), FALSE, NULL);
74  *
75  *   ...
76  *
77  *   return plugin;
78  * }
79  *
80  * ...
81  *
82  * GSettings *
83  * plugin_get_settings (Plugin      *plugin,
84  *                      const gchar *schema_id)
85  * {
86  *   GSettingsSchema *schema;
87  *
88  *   if (schema_id == NULL)
89  *     schema_id = plugin->identifier;
90  *
91  *   schema = g_settings_schema_source_lookup (plugin->schema_source,
92  *                                             schema_id, FALSE);
93  *
94  *   if (schema == NULL)
95  *     {
96  *       ... disable the plugin or abort, etc ...
97  *     }
98  *
99  *   return g_settings_new_full (schema, NULL, NULL);
100  * }
101  * ]|
102  *
103  * The code above shows how hooks should be added to the code that
104  * initialises (or enables) the plugin to create the schema source and
105  * how an API can be added to the plugin system to provide a convenient
106  * way for the plugin to access its settings, using the schemas that it
107  * ships.
108  *
109  * From the standpoint of the plugin, it would need to ensure that it
110  * ships a gschemas.compiled file as part of itself, and then simply do
111  * the following:
112  *
113  * |[
114  * {
115  *   GSettings *settings;
116  *   gint some_value;
117  *
118  *   settings = plugin_get_settings (self, NULL);
119  *   some_value = g_settings_get_int (settings, "some-value");
120  *   ...
121  * }
122  * ]|
123  *
124  * It's also possible that the plugin system expects the schema source
125  * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
126  * In that case, the plugin loading system must compile the schemas for
127  * itself before attempting to create the settings source.
128  *
129  * Since: 2.32
130  **/
131
132 /**
133  * GSettingsSchema:
134  *
135  * This is an opaque structure type.  You may not access it directly.
136  *
137  * Since: 2.32
138  **/
139 struct _GSettingsSchema
140 {
141   const gchar *gettext_domain;
142   const gchar *path;
143   GQuark *items;
144   gint n_items;
145   GvdbTable *table;
146   gchar *id;
147
148   gint ref_count;
149 };
150
151 /**
152  * G_TYPE_SETTINGS_SCHEMA_SOURCE:
153  *
154  * A boxed #GType corresponding to #GSettingsSchemaSource.
155  *
156  * Since: 2.32
157  **/
158 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
159
160 /**
161  * G_TYPE_SETTINGS_SCHEMA:
162  *
163  * A boxed #GType corresponding to #GSettingsSchema.
164  *
165  * Since: 2.32
166  **/
167 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
168
169 /**
170  * GSettingsSchemaSource:
171  *
172  * This is an opaque structure type.  You may not access it directly.
173  *
174  * Since: 2.32
175  **/
176 struct _GSettingsSchemaSource
177 {
178   GSettingsSchemaSource *parent;
179   GvdbTable *table;
180
181   gint ref_count;
182 };
183
184 static GSettingsSchemaSource *schema_sources;
185
186 static void
187 prepend_schema_table (GvdbTable *table)
188 {
189   GSettingsSchemaSource *source;
190
191   /* we steal the reference from 'schema_sources' for our ->parent */
192   source = g_slice_new (GSettingsSchemaSource);
193   source->parent = schema_sources;
194   source->table = table;
195   source->ref_count = 1;
196
197   schema_sources = source;
198 }
199
200 /**
201  * g_settings_schema_source_ref:
202  * @source: a #GSettingsSchemaSource
203  *
204  * Increase the reference count of @source, returning a new reference.
205  *
206  * Returns: a new reference to @source
207  *
208  * Since: 2.32
209  **/
210 GSettingsSchemaSource *
211 g_settings_schema_source_ref (GSettingsSchemaSource *source)
212 {
213   g_atomic_int_inc (&source->ref_count);
214
215   return source;
216 }
217
218 /**
219  * g_settings_schema_source_unref:
220  * @source: a #GSettingsSchemaSource
221  *
222  * Decrease the reference count of @source, possibly freeing it.
223  *
224  * Since: 2.32
225  **/
226 void
227 g_settings_schema_source_unref (GSettingsSchemaSource *source)
228 {
229   if (g_atomic_int_dec_and_test (&source->ref_count))
230     {
231       if (source == schema_sources)
232         g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
233
234       if (source->parent)
235         g_settings_schema_source_unref (source->parent);
236       gvdb_table_unref (source->table);
237
238       g_slice_free (GSettingsSchemaSource, source);
239     }
240 }
241
242 /**
243  * g_settings_schema_source_new_from_directory:
244  * @directory: the filename of a directory
245  * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
246  * @trusted: %TRUE, if the directory is trusted
247  * @error: a pointer to a #GError pointer set to %NULL, or %NULL
248  *
249  * Attempts to create a new schema source corresponding to the contents
250  * of the given directory.
251  *
252  * This function is not required for normal uses of #GSettings but it
253  * may be useful to authors of plugin management systems.
254  *
255  * The directory should contain a file called
256  * <filename>gschemas.compiled</filename> as produced by
257  * <command>glib-compile-schemas</command>.
258  *
259  * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
260  * trusted not to be corrupted.  This assumption has a performance
261  * advantage, but can result in crashes or inconsistent behaviour in the
262  * case of a corrupted file.  Generally, you should set @trusted to
263  * %TRUE for files installed by the system and to %FALSE for files in
264  * the home directory.
265  *
266  * If @parent is non-%NULL then there are two effects.
267  *
268  * First, if g_settings_schema_source_lookup() is called with the
269  * @recursive flag set to %TRUE and the schema can not be found in the
270  * source, the lookup will recurse to the parent.
271  *
272  * Second, any references to other schemas specified within this
273  * source (ie: <literal>child</literal> or <literal>extends</literal>)
274  * references may be resolved from the @parent.
275  *
276  * For this second reason, except in very unusual situations, the
277  * @parent should probably be given as the default schema source, as
278  * returned by g_settings_schema_source_get_default().
279  *
280  * Since: 2.32
281  **/
282 GSettingsSchemaSource *
283 g_settings_schema_source_new_from_directory (const gchar            *directory,
284                                              GSettingsSchemaSource  *parent,
285                                              gboolean                trusted,
286                                              GError                **error)
287 {
288   GSettingsSchemaSource *source;
289   GvdbTable *table;
290   gchar *filename;
291
292   filename = g_build_filename (directory, "gschemas.compiled", NULL);
293   table = gvdb_table_new (filename, trusted, error);
294   g_free (filename);
295
296   if (table == NULL)
297     return NULL;
298
299   source = g_slice_new (GSettingsSchemaSource);
300   source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
301   source->table = table;
302   source->ref_count = 1;
303
304   return source;
305 }
306
307 static void
308 initialise_schema_sources (void)
309 {
310   static gsize initialised;
311
312   /* need a separate variable because 'schema_sources' may legitimately
313    * be null if we have zero valid schema sources
314    */
315   if G_UNLIKELY (g_once_init_enter (&initialised))
316     {
317       const gchar * const *dirs;
318       const gchar *path;
319       gint i;
320
321       /* iterate in reverse: count up, then count down */
322       dirs = g_get_system_data_dirs ();
323       for (i = 0; dirs[i]; i++);
324
325       while (i--)
326         {
327           gchar *filename;
328           GvdbTable *table;
329
330           filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
331           table = gvdb_table_new (filename, TRUE, NULL);
332
333           if (table != NULL)
334             prepend_schema_table (table);
335
336           g_free (filename);
337         }
338
339       if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
340         {
341           gchar *filename;
342           GvdbTable *table;
343
344           filename = g_build_filename (path, "gschemas.compiled", NULL);
345           table = gvdb_table_new (filename, TRUE, NULL);
346
347           if (table != NULL)
348             prepend_schema_table (table);
349
350           g_free (filename);
351         }
352
353       g_once_init_leave (&initialised, TRUE);
354     }
355 }
356
357 /**
358  * g_settings_schema_source_get_default:
359  *
360  * Gets the default system schema source.
361  *
362  * This function is not required for normal uses of #GSettings but it
363  * may be useful to authors of plugin management systems or to those who
364  * want to introspect the content of schemas.
365  *
366  * If no schemas are installed, %NULL will be returned.
367  *
368  * The returned source may actually consist of multiple schema sources
369  * from different directories, depending on which directories were given
370  * in <envar>XDG_DATA_DIRS</envar> and
371  * <envar>GSETTINGS_SCHEMA_DIR</envar>.  For this reason, all lookups
372  * performed against the default source should probably be done
373  * recursively.
374  *
375  * Returns: (transfer none): the default schema source
376  *
377  * Since: 2.32
378  **/
379  GSettingsSchemaSource *
380 g_settings_schema_source_get_default (void)
381 {
382   initialise_schema_sources ();
383
384   return schema_sources;
385 }
386
387 /**
388  * g_settings_schema_source_lookup:
389  * @source: a #GSettingsSchemaSource
390  * @schema_id: a schema ID
391  * @recursive: %TRUE if the lookup should be recursive
392  *
393  * Looks up a schema with the identifier @schema_id in @source.
394  *
395  * This function is not required for normal uses of #GSettings but it
396  * may be useful to authors of plugin management systems or to those who
397  * want to introspect the content of schemas.
398  *
399  * If the schema isn't found directly in @source and @recursive is %TRUE
400  * then the parent sources will also be checked.
401  *
402  * If the schema isn't found, %NULL is returned.
403  *
404  * Returns: (transfer full): a new #GSettingsSchema
405  *
406  * Since: 2.32
407  **/
408 GSettingsSchema *
409 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
410                                  const gchar           *schema_id,
411                                  gboolean               recursive)
412 {
413   GSettingsSchema *schema;
414   GvdbTable *table;
415
416   g_return_val_if_fail (source != NULL, NULL);
417   g_return_val_if_fail (schema_id != NULL, NULL);
418
419   table = gvdb_table_get_table (source->table, schema_id);
420
421   if (table == NULL && recursive)
422     for (source = source->parent; source; source = source->parent)
423       if ((table = gvdb_table_get_table (source->table, schema_id)))
424         break;
425
426   if (table == NULL)
427     return NULL;
428
429   schema = g_slice_new0 (GSettingsSchema);
430   schema->ref_count = 1;
431   schema->id = g_strdup (schema_id);
432   schema->table = table;
433   schema->path = g_settings_schema_get_string (schema, ".path");
434   schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
435
436   if (schema->gettext_domain)
437     bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
438
439   return schema;
440 }
441
442 static gboolean
443 steal_item (gpointer key,
444             gpointer value,
445             gpointer user_data)
446 {
447   gchar ***ptr = user_data;
448
449   *(*ptr)++ = (gchar *) key;
450
451   return TRUE;
452 }
453
454 static const gchar * const *non_relocatable_schema_list;
455 static const gchar * const *relocatable_schema_list;
456 static gsize schema_lists_initialised;
457
458 static void
459 ensure_schema_lists (void)
460 {
461   if (g_once_init_enter (&schema_lists_initialised))
462     {
463       GSettingsSchemaSource *source;
464       GHashTable *single, *reloc;
465       const gchar **ptr;
466       gchar **list;
467       gint i;
468
469       initialise_schema_sources ();
470
471       /* We use hash tables to avoid duplicate listings for schemas that
472        * appear in more than one file.
473        */
474       single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
475       reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
476
477       for (source = schema_sources; source; source = source->parent)
478         {
479           list = gvdb_table_list (source->table, "");
480
481           /* empty schema cache file? */
482           if (list == NULL)
483             continue;
484
485           for (i = 0; list[i]; i++)
486             {
487               if (!g_hash_table_lookup (single, list[i]) &&
488                   !g_hash_table_lookup (reloc, list[i]))
489                 {
490                   GvdbTable *table;
491
492                   table = gvdb_table_get_table (source->table, list[i]);
493                   g_assert (table != NULL);
494
495                   if (gvdb_table_has_value (table, ".path"))
496                     g_hash_table_insert (single, g_strdup (list[i]), NULL);
497                   else
498                     g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
499
500                   gvdb_table_unref (table);
501                 }
502             }
503
504           g_strfreev (list);
505         }
506
507       ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
508       non_relocatable_schema_list = ptr;
509       g_hash_table_foreach_steal (single, steal_item, &ptr);
510       g_hash_table_unref (single);
511       *ptr = NULL;
512
513       ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
514       relocatable_schema_list = ptr;
515       g_hash_table_foreach_steal (reloc, steal_item, &ptr);
516       g_hash_table_unref (reloc);
517       *ptr = NULL;
518
519       g_once_init_leave (&schema_lists_initialised, TRUE);
520     }
521 }
522
523 /**
524  * g_settings_list_schemas:
525  *
526  * Gets a list of the #GSettings schemas installed on the system.  The
527  * returned list is exactly the list of schemas for which you may call
528  * g_settings_new() without adverse effects.
529  *
530  * This function does not list the schemas that do not provide their own
531  * paths (ie: schemas for which you must use
532  * g_settings_new_with_path()).  See
533  * g_settings_list_relocatable_schemas() for that.
534  *
535  * Returns: (element-type utf8) (transfer none):  a list of #GSettings
536  *   schemas that are available.  The list must not be modified or
537  *   freed.
538  *
539  * Since: 2.26
540  **/
541 const gchar * const *
542 g_settings_list_schemas (void)
543 {
544   ensure_schema_lists ();
545
546   return non_relocatable_schema_list;
547 }
548
549 /**
550  * g_settings_list_relocatable_schemas:
551  *
552  * Gets a list of the relocatable #GSettings schemas installed on the
553  * system.  These are schemas that do not provide their own path.  It is
554  * usual to instantiate these schemas directly, but if you want to you
555  * can use g_settings_new_with_path() to specify the path.
556  *
557  * The output of this function, taken together with the output of
558  * g_settings_list_schemas() represents the complete list of all
559  * installed schemas.
560  *
561  * Returns: (element-type utf8) (transfer none): a list of relocatable
562  *   #GSettings schemas that are available.  The list must not be
563  *   modified or freed.
564  *
565  * Since: 2.28
566  **/
567 const gchar * const *
568 g_settings_list_relocatable_schemas (void)
569 {
570   ensure_schema_lists ();
571
572   return relocatable_schema_list;
573 }
574
575 /**
576  * g_settings_schema_ref:
577  * @schema: a #GSettingsSchema
578  *
579  * Increase the reference count of @schema, returning a new reference.
580  *
581  * Returns: a new reference to @schema
582  *
583  * Since: 2.32
584  **/
585 GSettingsSchema *
586 g_settings_schema_ref (GSettingsSchema *schema)
587 {
588   g_atomic_int_inc (&schema->ref_count);
589
590   return schema;
591 }
592
593 /**
594  * g_settings_schema_unref:
595  * @schema: a #GSettingsSchema
596  *
597  * Decrease the reference count of @schema, possibly freeing it.
598  *
599  * Since: 2.32
600  **/
601 void
602 g_settings_schema_unref (GSettingsSchema *schema)
603 {
604   if (g_atomic_int_dec_and_test (&schema->ref_count))
605     {
606       gvdb_table_unref (schema->table);
607       g_free (schema->items);
608       g_free (schema->id);
609
610       g_slice_free (GSettingsSchema, schema);
611     }
612 }
613
614 const gchar *
615 g_settings_schema_get_string (GSettingsSchema *schema,
616                               const gchar     *key)
617 {
618   const gchar *result = NULL;
619   GVariant *value;
620
621   if ((value = gvdb_table_get_raw_value (schema->table, key)))
622     {
623       result = g_variant_get_string (value, NULL);
624       g_variant_unref (value);
625     }
626
627   return result;
628 }
629
630 GVariantIter *
631 g_settings_schema_get_value (GSettingsSchema *schema,
632                              const gchar     *key)
633 {
634   GVariantIter *iter;
635   GVariant *value;
636
637   value = gvdb_table_get_raw_value (schema->table, key);
638
639   if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
640     g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
641
642   iter = g_variant_iter_new (value);
643   g_variant_unref (value);
644
645   return iter;
646 }
647
648 /**
649  * g_settings_schema_get_path:
650  * @schema: a #GSettingsSchema
651  *
652  * Gets the path associated with @schema, or %NULL.
653  *
654  * Schemas may be single-instance or relocatable.  Single-instance
655  * schemas correspond to exactly one set of keys in the backend
656  * database: those located at the path returned by this function.
657  *
658  * Relocatable schemas can be referenced by other schemas and can
659  * threfore describe multiple sets of keys at different locations.  For
660  * relocatable schemas, this function will return %NULL.
661  *
662  * Returns: (transfer none): the path of the schema, or %NULL
663  *
664  * Since: 2.32
665  **/
666 const gchar *
667 g_settings_schema_get_path (GSettingsSchema *schema)
668 {
669   return schema->path;
670 }
671
672 const gchar *
673 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
674 {
675   return schema->gettext_domain;
676 }
677
678 gboolean
679 g_settings_schema_has_key (GSettingsSchema *schema,
680                            const gchar     *key)
681 {
682   return gvdb_table_has_value (schema->table, key);
683 }
684
685 const GQuark *
686 g_settings_schema_list (GSettingsSchema *schema,
687                         gint            *n_items)
688 {
689   gint i, j;
690
691   if (schema->items == NULL)
692     {
693       gchar **list;
694       gint len;
695
696       list = gvdb_table_list (schema->table, "");
697       len = list ? g_strv_length (list) : 0;
698
699       schema->items = g_new (GQuark, len);
700       j = 0;
701
702       for (i = 0; i < len; i++)
703         if (list[i][0] != '.')
704           {
705             if (g_str_has_suffix (list[i], "/"))
706               {
707                 /* This is a child.  Check to make sure that
708                  * instantiating the child would actually work before we
709                  * return it from list() and cause a crash.
710                  */
711                 GSettingsSchemaSource *source;
712                 GVariant *child_schema;
713                 GvdbTable *child_table;
714
715                 child_schema = gvdb_table_get_raw_value (schema->table, list[i]);
716                 if (!child_schema)
717                   continue;
718
719                 child_table = NULL;
720
721                 for (source = schema_sources; source; source = source->parent)
722                   if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
723                     break;
724
725                 g_variant_unref (child_schema);
726
727                 /* Schema is not found -> don't add it to the list */
728                 if (child_table == NULL)
729                   continue;
730
731                 /* Make sure the schema is relocatable or at the
732                  * expected path
733                  */
734                 if (gvdb_table_has_value (child_table, ".path"))
735                   {
736                     GVariant *path;
737                     gchar *expected;
738                     gboolean same;
739
740                     path = gvdb_table_get_raw_value (child_table, ".path");
741                     expected = g_strconcat (schema->path, list[i], NULL);
742                     same = g_str_equal (expected, g_variant_get_string (path, NULL));
743                     g_variant_unref (path);
744                     g_free (expected);
745
746                     if (!same)
747                       {
748                         gvdb_table_unref (child_table);
749                         continue;
750                       }
751                   }
752
753                 gvdb_table_unref (child_table);
754                 /* Else, it's good... */
755               }
756
757             schema->items[j++] = g_quark_from_string (list[i]);
758           }
759       schema->n_items = j;
760
761       g_strfreev (list);
762     }
763
764   *n_items = schema->n_items;
765   return schema->items;
766 }
767
768 /**
769  * g_settings_schema_get_id:
770  * @schema: a #GSettingsSchema
771  *
772  * Get the ID of @schema.
773  *
774  * Returns: (transfer none): the ID
775  **/
776 const gchar *
777 g_settings_schema_get_id (GSettingsSchema *schema)
778 {
779   return schema->id;
780 }
781
782 static inline void
783 endian_fixup (GVariant **value)
784 {
785 #if G_BYTE_ORDER == G_BIG_ENDIAN
786   GVariant *tmp;
787
788   tmp = g_variant_byteswap (*value);
789   g_variant_unref (*value);
790   *value = tmp;
791 #endif
792 }
793
794 void
795 g_settings_schema_key_init (GSettingsSchemaKey *key,
796                             GSettingsSchema    *schema,
797                             const gchar        *name)
798 {
799   GVariantIter *iter;
800   GVariant *data;
801   guchar code;
802
803   memset (key, 0, sizeof *key);
804
805   iter = g_settings_schema_get_value (schema, name);
806
807   key->schema = g_settings_schema_ref (schema);
808   key->default_value = g_variant_iter_next_value (iter);
809   endian_fixup (&key->default_value);
810   key->type = g_variant_get_type (key->default_value);
811   key->name = g_intern_string (name);
812
813   while (g_variant_iter_next (iter, "(y*)", &code, &data))
814     {
815       switch (code)
816         {
817         case 'l':
818           /* translation requested */
819           g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
820           break;
821
822         case 'e':
823           /* enumerated types... */
824           key->is_enum = TRUE;
825           goto choice;
826
827         case 'f':
828           /* flags... */
829           key->is_flags = TRUE;
830           goto choice;
831
832         choice: case 'c':
833           /* ..., choices, aliases */
834           key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
835           break;
836
837         case 'r':
838           g_variant_get (data, "(**)", &key->minimum, &key->maximum);
839           endian_fixup (&key->minimum);
840           endian_fixup (&key->maximum);
841           break;
842
843         default:
844           g_warning ("unknown schema extension '%c'", code);
845           break;
846         }
847
848       g_variant_unref (data);
849     }
850
851   g_variant_iter_free (iter);
852 }
853
854 void
855 g_settings_schema_key_clear (GSettingsSchemaKey *key)
856 {
857   if (key->minimum)
858     g_variant_unref (key->minimum);
859
860   if (key->maximum)
861     g_variant_unref (key->maximum);
862
863   g_variant_unref (key->default_value);
864
865   g_settings_schema_unref (key->schema);
866 }
867
868 gboolean
869 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
870                                   GVariant           *value)
871 {
872   g_return_val_if_fail (value != NULL, FALSE);
873
874   return g_variant_is_of_type (value, key->type);
875 }
876
877 gboolean
878 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
879                                    GVariant           *value)
880 {
881   if (key->minimum == NULL && key->strinfo == NULL)
882     return TRUE;
883
884   if (g_variant_is_container (value))
885     {
886       gboolean ok = TRUE;
887       GVariantIter iter;
888       GVariant *child;
889
890       g_variant_iter_init (&iter, value);
891       while (ok && (child = g_variant_iter_next_value (&iter)))
892         {
893           ok = g_settings_schema_key_range_check (key, child);
894           g_variant_unref (child);
895         }
896
897       return ok;
898     }
899
900   if (key->minimum)
901     {
902       return g_variant_compare (key->minimum, value) <= 0 &&
903              g_variant_compare (value, key->maximum) <= 0;
904     }
905
906   return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
907                                   g_variant_get_string (value, NULL));
908 }
909
910 GVariant *
911 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
912                                    GVariant           *value)
913 {
914   const gchar *target;
915
916   if (g_settings_schema_key_range_check (key, value))
917     return g_variant_ref (value);
918
919   if (key->strinfo == NULL)
920     return NULL;
921
922   if (g_variant_is_container (value))
923     {
924       GVariantBuilder builder;
925       GVariantIter iter;
926       GVariant *child;
927
928       g_variant_iter_init (&iter, value);
929       g_variant_builder_init (&builder, g_variant_get_type (value));
930
931       while ((child = g_variant_iter_next_value (&iter)))
932         {
933           GVariant *fixed;
934
935           fixed = g_settings_schema_key_range_fixup (key, child);
936           g_variant_unref (child);
937
938           if (fixed == NULL)
939             {
940               g_variant_builder_clear (&builder);
941               return NULL;
942             }
943
944           g_variant_builder_add_value (&builder, fixed);
945           g_variant_unref (fixed);
946         }
947
948       return g_variant_ref_sink (g_variant_builder_end (&builder));
949     }
950
951   target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
952                                       g_variant_get_string (value, NULL));
953   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
954 }
955
956
957 GVariant *
958 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
959 {
960   const gchar *translated;
961   GError *error = NULL;
962   const gchar *domain;
963   GVariant *value;
964
965   domain = g_settings_schema_get_gettext_domain (key->schema);
966
967   if (key->lc_char == '\0')
968     /* translation not requested for this key */
969     return NULL;
970
971   if (key->lc_char == 't')
972     translated = g_dcgettext (domain, key->unparsed, LC_TIME);
973   else
974     translated = g_dgettext (domain, key->unparsed);
975
976   if (translated == key->unparsed)
977     /* the default value was not translated */
978     return NULL;
979
980   /* try to parse the translation of the unparsed default */
981   value = g_variant_parse (key->type, translated, NULL, NULL, &error);
982
983   if (value == NULL)
984     {
985       g_warning ("Failed to parse translated string '%s' for "
986                  "key '%s' in schema '%s': %s", key->unparsed, key->name,
987                  g_settings_schema_get_id (key->schema), error->message);
988       g_warning ("Using untranslated default instead.");
989       g_error_free (error);
990     }
991
992   else if (!g_settings_schema_key_range_check (key, value))
993     {
994       g_warning ("Translated default '%s' for key '%s' in schema '%s' "
995                  "is outside of valid range", key->unparsed, key->name,
996                  g_settings_schema_get_id (key->schema));
997       g_variant_unref (value);
998       value = NULL;
999     }
1000
1001   return value;
1002 }
1003
1004 gint
1005 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1006                                GVariant           *value)
1007 {
1008   gboolean it_worked;
1009   guint result;
1010
1011   it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1012                                         g_variant_get_string (value, NULL),
1013                                         &result);
1014
1015   /* 'value' can only come from the backend after being filtered for validity,
1016    * from the translation after being filtered for validity, or from the schema
1017    * itself (which the schema compiler checks for validity).  If this assertion
1018    * fails then it's really a bug in GSettings or the schema compiler...
1019    */
1020   g_assert (it_worked);
1021
1022   return result;
1023 }
1024
1025 GVariant *
1026 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1027                                  gint                value)
1028 {
1029   const gchar *string;
1030
1031   string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1032
1033   if (string == NULL)
1034     return NULL;
1035
1036   return g_variant_new_string (string);
1037 }
1038
1039 guint
1040 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1041                                 GVariant           *value)
1042 {
1043   GVariantIter iter;
1044   const gchar *flag;
1045   guint result;
1046
1047   result = 0;
1048   g_variant_iter_init (&iter, value);
1049   while (g_variant_iter_next (&iter, "&s", &flag))
1050     {
1051       gboolean it_worked;
1052       guint flag_value;
1053
1054       it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1055       /* as in g_settings_to_enum() */
1056       g_assert (it_worked);
1057
1058       result |= flag_value;
1059     }
1060
1061   return result;
1062 }
1063
1064 GVariant *
1065 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1066                                   guint               value)
1067 {
1068   GVariantBuilder builder;
1069   gint i;
1070
1071   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1072
1073   for (i = 0; i < 32; i++)
1074     if (value & (1u << i))
1075       {
1076         const gchar *string;
1077
1078         string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1079
1080         if (string == NULL)
1081           {
1082             g_variant_builder_clear (&builder);
1083             return NULL;
1084           }
1085
1086         g_variant_builder_add (&builder, "s", string);
1087       }
1088
1089   return g_variant_builder_end (&builder);
1090 }