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