Add includes to all gio docs
[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
36  *     of GSettings schemas
37  * @include: gio/gio.h
38  *
39  * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
40  * mechanism for advanced control over the loading of schemas and a
41  * mechanism for introspecting their content.
42  *
43  * Plugin loading systems that wish to provide plugins a way to access
44  * settings face the problem of how to make the schemas for these
45  * settings visible to GSettings.  Typically, a plugin will want to ship
46  * the schema along with itself and it won't be installed into the
47  * standard system directories for schemas.
48  *
49  * #GSettingsSchemaSource provides a mechanism for dealing with this by
50  * allowing the creation of a new 'schema source' from which schemas can
51  * be acquired.  This schema source can then become part of the metadata
52  * associated with the plugin and queried whenever the plugin requires
53  * access to some settings.
54  *
55  * Consider the following example:
56  *
57  * |[
58  * typedef struct
59  * {
60  *    ...
61  *    GSettingsSchemaSource *schema_source;
62  *    ...
63  * } Plugin;
64  *
65  * Plugin *
66  * initialise_plugin (const gchar *dir)
67  * {
68  *   Plugin *plugin;
69  *
70  *   ...
71  *
72  *   plugin->schema_source =
73  *     g_settings_new_schema_source_from_directory (dir,
74  *       g_settings_schema_source_get_default (), FALSE, NULL);
75  *
76  *   ...
77  *
78  *   return plugin;
79  * }
80  *
81  * ...
82  *
83  * GSettings *
84  * plugin_get_settings (Plugin      *plugin,
85  *                      const gchar *schema_id)
86  * {
87  *   GSettingsSchema *schema;
88  *
89  *   if (schema_id == NULL)
90  *     schema_id = plugin->identifier;
91  *
92  *   schema = g_settings_schema_source_lookup (plugin->schema_source,
93  *                                             schema_id, FALSE);
94  *
95  *   if (schema == NULL)
96  *     {
97  *       ... disable the plugin or abort, etc ...
98  *     }
99  *
100  *   return g_settings_new_full (schema, NULL, NULL);
101  * }
102  * ]|
103  *
104  * The code above shows how hooks should be added to the code that
105  * initialises (or enables) the plugin to create the schema source and
106  * how an API can be added to the plugin system to provide a convenient
107  * way for the plugin to access its settings, using the schemas that it
108  * ships.
109  *
110  * From the standpoint of the plugin, it would need to ensure that it
111  * ships a gschemas.compiled file as part of itself, and then simply do
112  * the following:
113  *
114  * |[
115  * {
116  *   GSettings *settings;
117  *   gint some_value;
118  *
119  *   settings = plugin_get_settings (self, NULL);
120  *   some_value = g_settings_get_int (settings, "some-value");
121  *   ...
122  * }
123  * ]|
124  *
125  * It's also possible that the plugin system expects the schema source
126  * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
127  * In that case, the plugin loading system must compile the schemas for
128  * itself before attempting to create the settings source.
129  *
130  * Since: 2.32
131  **/
132
133 /**
134  * GSettingsSchema:
135  *
136  * This is an opaque structure type.  You may not access it directly.
137  *
138  * Since: 2.32
139  **/
140 struct _GSettingsSchema
141 {
142   GSettingsSchemaSource *source;
143   const gchar *gettext_domain;
144   const gchar *path;
145   GQuark *items;
146   gint n_items;
147   GvdbTable *table;
148   gchar *id;
149
150   GSettingsSchema *extends;
151
152   gint ref_count;
153 };
154
155 /**
156  * G_TYPE_SETTINGS_SCHEMA_SOURCE:
157  *
158  * A boxed #GType corresponding to #GSettingsSchemaSource.
159  *
160  * Since: 2.32
161  **/
162 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
163
164 /**
165  * G_TYPE_SETTINGS_SCHEMA:
166  *
167  * A boxed #GType corresponding to #GSettingsSchema.
168  *
169  * Since: 2.32
170  **/
171 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
172
173 /**
174  * GSettingsSchemaSource:
175  *
176  * This is an opaque structure type.  You may not access it directly.
177  *
178  * Since: 2.32
179  **/
180 struct _GSettingsSchemaSource
181 {
182   GSettingsSchemaSource *parent;
183   gchar *directory;
184   GvdbTable *table;
185   GHashTable **text_tables;
186
187   gint ref_count;
188 };
189
190 static GSettingsSchemaSource *schema_sources;
191
192 /**
193  * g_settings_schema_source_ref:
194  * @source: a #GSettingsSchemaSource
195  *
196  * Increase the reference count of @source, returning a new reference.
197  *
198  * Returns: a new reference to @source
199  *
200  * Since: 2.32
201  **/
202 GSettingsSchemaSource *
203 g_settings_schema_source_ref (GSettingsSchemaSource *source)
204 {
205   g_atomic_int_inc (&source->ref_count);
206
207   return source;
208 }
209
210 /**
211  * g_settings_schema_source_unref:
212  * @source: a #GSettingsSchemaSource
213  *
214  * Decrease the reference count of @source, possibly freeing it.
215  *
216  * Since: 2.32
217  **/
218 void
219 g_settings_schema_source_unref (GSettingsSchemaSource *source)
220 {
221   if (g_atomic_int_dec_and_test (&source->ref_count))
222     {
223       if (source == schema_sources)
224         g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
225
226       if (source->parent)
227         g_settings_schema_source_unref (source->parent);
228       gvdb_table_unref (source->table);
229       g_free (source->directory);
230
231       if (source->text_tables)
232         {
233           g_hash_table_unref (source->text_tables[0]);
234           g_hash_table_unref (source->text_tables[1]);
235           g_free (source->text_tables);
236         }
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->directory = g_strdup (directory);
301   source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
302   source->text_tables = NULL;
303   source->table = table;
304   source->ref_count = 1;
305
306   return source;
307 }
308
309 static void
310 try_prepend_dir (const gchar *directory)
311 {
312   GSettingsSchemaSource *source;
313
314   source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
315
316   /* If we successfully created it then prepend it to the global list */
317   if (source != NULL)
318     schema_sources = source;
319 }
320
321 static void
322 initialise_schema_sources (void)
323 {
324   static gsize initialised;
325
326   /* need a separate variable because 'schema_sources' may legitimately
327    * be null if we have zero valid schema sources
328    */
329   if G_UNLIKELY (g_once_init_enter (&initialised))
330     {
331       const gchar * const *dirs;
332       const gchar *path;
333       gint i;
334
335       /* iterate in reverse: count up, then count down */
336       dirs = g_get_system_data_dirs ();
337       for (i = 0; dirs[i]; i++);
338
339       while (i--)
340         {
341           gchar *dirname;
342
343           dirname = g_build_filename (dirs[i], "glib-2.0", "schemas", NULL);
344           try_prepend_dir (dirname);
345           g_free (dirname);
346         }
347
348       if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
349         try_prepend_dir (path);
350
351       g_once_init_leave (&initialised, TRUE);
352     }
353 }
354
355 /**
356  * g_settings_schema_source_get_default:
357  *
358  * Gets the default system schema source.
359  *
360  * This function is not required for normal uses of #GSettings but it
361  * may be useful to authors of plugin management systems or to those who
362  * want to introspect the content of schemas.
363  *
364  * If no schemas are installed, %NULL will be returned.
365  *
366  * The returned source may actually consist of multiple schema sources
367  * from different directories, depending on which directories were given
368  * in <envar>XDG_DATA_DIRS</envar> and
369  * <envar>GSETTINGS_SCHEMA_DIR</envar>.  For this reason, all lookups
370  * performed against the default source should probably be done
371  * recursively.
372  *
373  * Returns: (transfer none): the default schema source
374  *
375  * Since: 2.32
376  **/
377  GSettingsSchemaSource *
378 g_settings_schema_source_get_default (void)
379 {
380   initialise_schema_sources ();
381
382   return schema_sources;
383 }
384
385 /**
386  * g_settings_schema_source_lookup:
387  * @source: a #GSettingsSchemaSource
388  * @schema_id: a schema ID
389  * @recursive: %TRUE if the lookup should be recursive
390  *
391  * Looks up a schema with the identifier @schema_id in @source.
392  *
393  * This function is not required for normal uses of #GSettings but it
394  * may be useful to authors of plugin management systems or to those who
395  * want to introspect the content of schemas.
396  *
397  * If the schema isn't found directly in @source and @recursive is %TRUE
398  * then the parent sources will also be checked.
399  *
400  * If the schema isn't found, %NULL is returned.
401  *
402  * Returns: (transfer full): a new #GSettingsSchema
403  *
404  * Since: 2.32
405  **/
406 GSettingsSchema *
407 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
408                                  const gchar           *schema_id,
409                                  gboolean               recursive)
410 {
411   GSettingsSchema *schema;
412   GvdbTable *table;
413   const gchar *extends;
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->source = g_settings_schema_source_ref (source);
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   extends = g_settings_schema_get_string (schema, ".extends");
440   if (extends)
441     {
442       schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
443       if (schema->extends == NULL)
444         g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
445     }
446
447   return schema;
448 }
449
450 typedef struct
451 {
452   GHashTable *summaries;
453   GHashTable *descriptions;
454   GSList     *gettext_domain;
455   GSList     *schema_id;
456   GSList     *key_name;
457   GString    *string;
458 } TextTableParseInfo;
459
460 static const gchar *
461 get_attribute_value (GSList *list)
462 {
463   GSList *node;
464
465   for (node = list; node; node = node->next)
466     if (node->data)
467       return node->data;
468
469   return NULL;
470 }
471
472 static void
473 pop_attribute_value (GSList **list)
474 {
475   gchar *top;
476
477   top = (*list)->data;
478   *list = g_slist_remove (*list, top);
479
480   g_free (top);
481 }
482
483 static void
484 push_attribute_value (GSList      **list,
485                       const gchar  *value)
486 {
487   *list = g_slist_prepend (*list, g_strdup (value));
488 }
489
490 static void
491 start_element (GMarkupParseContext  *context,
492                const gchar          *element_name,
493                const gchar         **attribute_names,
494                const gchar         **attribute_values,
495                gpointer              user_data,
496                GError              **error)
497 {
498   TextTableParseInfo *info = user_data;
499   const gchar *gettext_domain = NULL;
500   const gchar *schema_id = NULL;
501   const gchar *key_name = NULL;
502   gint i;
503
504   for (i = 0; attribute_names[i]; i++)
505     {
506       if (g_str_equal (attribute_names[i], "gettext-domain"))
507         gettext_domain = attribute_values[i];
508       else if (g_str_equal (attribute_names[i], "id"))
509         schema_id = attribute_values[i];
510       else if (g_str_equal (attribute_names[i], "name"))
511         key_name = attribute_values[i];
512     }
513
514   push_attribute_value (&info->gettext_domain, gettext_domain);
515   push_attribute_value (&info->schema_id, schema_id);
516   push_attribute_value (&info->key_name, key_name);
517
518   if (info->string)
519     {
520       g_string_free (info->string, TRUE);
521       info->string = NULL;
522     }
523
524   if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
525     info->string = g_string_new (NULL);
526 }
527
528 static gchar *
529 normalise_whitespace (const gchar *orig)
530 {
531   /* We normalise by the same rules as in intltool:
532    *
533    *   sub cleanup {
534    *       s/^\s+//;
535    *       s/\s+$//;
536    *       s/\s+/ /g;
537    *       return $_;
538    *   }
539    *
540    *   $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
541    *
542    * Where \s is an ascii space character.
543    *
544    * We aim for ease of implementation over efficiency -- this code is
545    * not run in normal applications.
546    */
547   static GRegex *cleanup[3];
548   static GRegex *splitter;
549   gchar **lines;
550   gchar *result;
551   gint i;
552
553   if (g_once_init_enter (&splitter))
554     {
555       GRegex *s;
556
557       cleanup[0] = g_regex_new ("^\\s+", 0, 0, 0);
558       cleanup[1] = g_regex_new ("\\s+$", 0, 0, 0);
559       cleanup[2] = g_regex_new ("\\s+", 0, 0, 0);
560       s = g_regex_new ("\\n\\s*\\n+", 0, 0, 0);
561
562       g_once_init_leave (&splitter, s);
563     }
564
565   lines = g_regex_split (splitter, orig, 0);
566   for (i = 0; lines[i]; i++)
567     {
568       gchar *a, *b, *c;
569
570       a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
571       b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
572       c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
573       g_free (lines[i]);
574       g_free (a);
575       g_free (b);
576       lines[i] = c;
577     }
578
579   result = g_strjoinv ("\n\n", lines);
580   g_strfreev (lines);
581
582   return result;
583 }
584
585 static void
586 end_element (GMarkupParseContext *context,
587              const gchar *element_name,
588              gpointer user_data,
589              GError **error)
590 {
591   TextTableParseInfo *info = user_data;
592
593   pop_attribute_value (&info->gettext_domain);
594   pop_attribute_value (&info->schema_id);
595   pop_attribute_value (&info->key_name);
596
597   if (info->string)
598     {
599       GHashTable *source_table = NULL;
600       const gchar *gettext_domain;
601       const gchar *schema_id;
602       const gchar *key_name;
603
604       gettext_domain = get_attribute_value (info->gettext_domain);
605       schema_id = get_attribute_value (info->schema_id);
606       key_name = get_attribute_value (info->key_name);
607
608       if (g_str_equal (element_name, "summary"))
609         source_table = info->summaries;
610       else if (g_str_equal (element_name, "description"))
611         source_table = info->descriptions;
612
613       if (source_table && schema_id && key_name)
614         {
615           GHashTable *schema_table;
616           gchar *normalised;
617
618           schema_table = g_hash_table_lookup (source_table, schema_id);
619
620           if (schema_table == NULL)
621             {
622               schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
623               g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
624             }
625
626           normalised = normalise_whitespace (info->string->str);
627
628           if (gettext_domain)
629             {
630               gchar *translated;
631
632               translated = g_strdup (g_dgettext (gettext_domain, normalised));
633               g_free (normalised);
634               normalised = translated;
635             }
636
637           g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
638         }
639
640       g_string_free (info->string, TRUE);
641       info->string = NULL;
642     }
643 }
644
645 static void
646 text (GMarkupParseContext  *context,
647       const gchar          *text,
648       gsize                 text_len,
649       gpointer              user_data,
650       GError              **error)
651 {
652   TextTableParseInfo *info = user_data;
653
654   if (info->string)
655     g_string_append_len (info->string, text, text_len);
656 }
657
658 static void
659 parse_into_text_tables (const gchar *directory,
660                         GHashTable  *summaries,
661                         GHashTable  *descriptions)
662 {
663   GMarkupParser parser = { start_element, end_element, text };
664   TextTableParseInfo info = { summaries, descriptions };
665   const gchar *basename;
666   GDir *dir;
667
668   dir = g_dir_open (directory, 0, NULL);
669   while ((basename = g_dir_read_name (dir)))
670     {
671       gchar *filename;
672       gchar *contents;
673       gsize size;
674
675       filename = g_build_filename (directory, basename, NULL);
676       if (g_file_get_contents (filename, &contents, &size, NULL))
677         {
678           GMarkupParseContext *context;
679
680           context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
681           if (g_markup_parse_context_parse (context, contents, size, NULL))
682             g_markup_parse_context_end_parse (context, NULL);
683           g_markup_parse_context_free (context);
684
685           /* Clean up dangling stuff in case there was an error. */
686           g_slist_free_full (info.gettext_domain, g_free);
687           g_slist_free_full (info.schema_id, g_free);
688           g_slist_free_full (info.key_name, g_free);
689
690           info.gettext_domain = NULL;
691           info.schema_id = NULL;
692           info.key_name = NULL;
693
694           if (info.string)
695             {
696               g_string_free (info.string, TRUE);
697               info.string = NULL;
698             }
699
700           g_free (contents);
701         }
702
703       g_free (filename);
704     }
705 }
706
707 static GHashTable **
708 g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
709 {
710   if (g_once_init_enter (&source->text_tables))
711     {
712       GHashTable **text_tables;
713
714       text_tables = g_new (GHashTable *, 2);
715       text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
716       text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
717
718       if (source->directory)
719         parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
720
721       g_once_init_leave (&source->text_tables, text_tables);
722     }
723
724   return source->text_tables;
725 }
726
727 /**
728  * g_settings_schema_source_list_schemas:
729  * @source: a #GSettingsSchemaSource
730  * @recursive: if we should recurse
731  * @non_relocatable: (out) (transfer full): the list of non-relocatable
732  *   schemas
733  * @relocatable: (out) (transfer full): the list of relocatable schemas
734  *
735  * Lists the schemas in a given source.
736  *
737  * If @recursive is %TRUE then include parent sources.  If %FALSE then
738  * only include the schemas from one source (ie: one directory).  You
739  * probably want %TRUE.
740  *
741  * Non-relocatable schemas are those for which you can call
742  * g_settings_new().  Relocatable schemas are those for which you must
743  * use g_settings_new_with_path().
744  *
745  * Do not call this function from normal programs.  This is designed for
746  * use by database editors, commandline tools, etc.
747  *
748  * Since: 2.40
749  **/
750 void
751 g_settings_schema_source_list_schemas (GSettingsSchemaSource   *source,
752                                        gboolean                 recursive,
753                                        gchar                 ***non_relocatable,
754                                        gchar                 ***relocatable)
755 {
756   GHashTable *single, *reloc;
757   GSettingsSchemaSource *s;
758
759   /* We use hash tables to avoid duplicate listings for schemas that
760    * appear in more than one file.
761    */
762   single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
763   reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
764
765   for (s = source; s; s = s->parent)
766     {
767       gchar **list;
768       gint i;
769
770       list = gvdb_table_list (s->table, "");
771
772       /* empty schema cache file? */
773       if (list == NULL)
774         continue;
775
776       for (i = 0; list[i]; i++)
777         {
778           if (!g_hash_table_lookup (single, list[i]) &&
779               !g_hash_table_lookup (reloc, list[i]))
780             {
781               GvdbTable *table;
782
783               table = gvdb_table_get_table (s->table, list[i]);
784               g_assert (table != NULL);
785
786               if (gvdb_table_has_value (table, ".path"))
787                 g_hash_table_insert (single, g_strdup (list[i]), NULL);
788               else
789                 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
790
791               gvdb_table_unref (table);
792             }
793         }
794
795       g_strfreev (list);
796
797       /* Only the first source if recursive not requested */
798       if (!recursive)
799         break;
800     }
801
802   if (non_relocatable)
803     {
804       *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
805       g_hash_table_steal_all (single);
806     }
807
808   if (relocatable)
809     {
810       *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
811       g_hash_table_steal_all (reloc);
812     }
813
814   g_hash_table_unref (single);
815   g_hash_table_unref (reloc);
816 }
817
818 static gchar **non_relocatable_schema_list;
819 static gchar **relocatable_schema_list;
820 static gsize schema_lists_initialised;
821
822 static void
823 ensure_schema_lists (void)
824 {
825   if (g_once_init_enter (&schema_lists_initialised))
826     {
827       initialise_schema_sources ();
828
829       g_settings_schema_source_list_schemas (schema_sources, TRUE,
830                                              &non_relocatable_schema_list,
831                                              &relocatable_schema_list);
832
833       g_once_init_leave (&schema_lists_initialised, TRUE);
834     }
835 }
836
837 /**
838  * g_settings_list_schemas:
839  *
840  * Returns: (element-type utf8) (transfer none):  a list of #GSettings
841  *   schemas that are available.  The list must not be modified or
842  *   freed.
843  *
844  * Since: 2.26
845  *
846  * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead.
847  * If you used g_settings_list_schemas() to check for the presence of
848  * a particular schema, use g_settings_schema_source_lookup() instead
849  * of your whole loop.
850  **/
851 const gchar * const *
852 g_settings_list_schemas (void)
853 {
854   ensure_schema_lists ();
855
856   return (const gchar **) non_relocatable_schema_list;
857 }
858
859 /**
860  * g_settings_list_relocatable_schemas:
861  *
862  * Returns: (element-type utf8) (transfer none): a list of relocatable
863  *   #GSettings schemas that are available.  The list must not be
864  *   modified or freed.
865  *
866  * Since: 2.28
867  *
868  * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
869  **/
870 const gchar * const *
871 g_settings_list_relocatable_schemas (void)
872 {
873   ensure_schema_lists ();
874
875   return (const gchar **) relocatable_schema_list;
876 }
877
878 /**
879  * g_settings_schema_ref:
880  * @schema: a #GSettingsSchema
881  *
882  * Increase the reference count of @schema, returning a new reference.
883  *
884  * Returns: a new reference to @schema
885  *
886  * Since: 2.32
887  **/
888 GSettingsSchema *
889 g_settings_schema_ref (GSettingsSchema *schema)
890 {
891   g_atomic_int_inc (&schema->ref_count);
892
893   return schema;
894 }
895
896 /**
897  * g_settings_schema_unref:
898  * @schema: a #GSettingsSchema
899  *
900  * Decrease the reference count of @schema, possibly freeing it.
901  *
902  * Since: 2.32
903  **/
904 void
905 g_settings_schema_unref (GSettingsSchema *schema)
906 {
907   if (g_atomic_int_dec_and_test (&schema->ref_count))
908     {
909       if (schema->extends)
910         g_settings_schema_unref (schema->extends);
911
912       g_settings_schema_source_unref (schema->source);
913       gvdb_table_unref (schema->table);
914       g_free (schema->items);
915       g_free (schema->id);
916
917       g_slice_free (GSettingsSchema, schema);
918     }
919 }
920
921 const gchar *
922 g_settings_schema_get_string (GSettingsSchema *schema,
923                               const gchar     *key)
924 {
925   const gchar *result = NULL;
926   GVariant *value;
927
928   if ((value = gvdb_table_get_raw_value (schema->table, key)))
929     {
930       result = g_variant_get_string (value, NULL);
931       g_variant_unref (value);
932     }
933
934   return result;
935 }
936
937 GVariantIter *
938 g_settings_schema_get_value (GSettingsSchema *schema,
939                              const gchar     *key)
940 {
941   GSettingsSchema *s = schema;
942   GVariantIter *iter;
943   GVariant *value;
944
945   g_return_val_if_fail (schema != NULL, NULL);
946
947   for (s = schema; s; s = s->extends)
948     if ((value = gvdb_table_get_raw_value (s->table, key)))
949       break;
950
951   if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
952     g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
953
954   iter = g_variant_iter_new (value);
955   g_variant_unref (value);
956
957   return iter;
958 }
959
960 /**
961  * g_settings_schema_get_path:
962  * @schema: a #GSettingsSchema
963  *
964  * Gets the path associated with @schema, or %NULL.
965  *
966  * Schemas may be single-instance or relocatable.  Single-instance
967  * schemas correspond to exactly one set of keys in the backend
968  * database: those located at the path returned by this function.
969  *
970  * Relocatable schemas can be referenced by other schemas and can
971  * threfore describe multiple sets of keys at different locations.  For
972  * relocatable schemas, this function will return %NULL.
973  *
974  * Returns: (transfer none): the path of the schema, or %NULL
975  *
976  * Since: 2.32
977  **/
978 const gchar *
979 g_settings_schema_get_path (GSettingsSchema *schema)
980 {
981   return schema->path;
982 }
983
984 const gchar *
985 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
986 {
987   return schema->gettext_domain;
988 }
989
990 /**
991  * g_settings_schema_has_key:
992  * @schema: a #GSettingsSchema
993  * @name: the name of a key
994  *
995  * Checks if @schema has a key named @name.
996  *
997  * Returns: %TRUE if such a key exists
998  *
999  * Since: 2.40
1000  **/
1001 gboolean
1002 g_settings_schema_has_key (GSettingsSchema *schema,
1003                            const gchar     *key)
1004 {
1005   return gvdb_table_has_value (schema->table, key);
1006 }
1007
1008 const GQuark *
1009 g_settings_schema_list (GSettingsSchema *schema,
1010                         gint            *n_items)
1011 {
1012   if (schema->items == NULL)
1013     {
1014       GSettingsSchema *s;
1015       GHashTableIter iter;
1016       GHashTable *items;
1017       gpointer name;
1018       gint len;
1019       gint i;
1020
1021       items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1022
1023       for (s = schema; s; s = s->extends)
1024         {
1025           gchar **list;
1026
1027           list = gvdb_table_list (s->table, "");
1028
1029           if (list)
1030             {
1031               for (i = 0; list[i]; i++)
1032                 g_hash_table_add (items, list[i]); /* transfer ownership */
1033
1034               g_free (list); /* free container only */
1035             }
1036         }
1037
1038       /* Do a first pass to eliminate child items that do not map to
1039        * valid schemas (ie: ones that would crash us if we actually
1040        * tried to create them).
1041        */
1042       g_hash_table_iter_init (&iter, items);
1043       while (g_hash_table_iter_next (&iter, &name, NULL))
1044         if (g_str_has_suffix (name, "/"))
1045           {
1046             GSettingsSchemaSource *source;
1047             GVariant *child_schema;
1048             GvdbTable *child_table;
1049
1050             child_schema = gvdb_table_get_raw_value (schema->table, name);
1051             if (!child_schema)
1052               continue;
1053
1054             child_table = NULL;
1055
1056             for (source = schema_sources; source; source = source->parent)
1057               if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1058                 break;
1059
1060             g_variant_unref (child_schema);
1061
1062             /* Schema is not found -> remove it from the list */
1063             if (child_table == NULL)
1064               {
1065                 g_hash_table_iter_remove (&iter);
1066                 continue;
1067               }
1068
1069             /* Make sure the schema is relocatable or at the
1070              * expected path
1071              */
1072             if (gvdb_table_has_value (child_table, ".path"))
1073               {
1074                 GVariant *path;
1075                 gchar *expected;
1076                 gboolean same;
1077
1078                 path = gvdb_table_get_raw_value (child_table, ".path");
1079                 expected = g_strconcat (schema->path, name, NULL);
1080                 same = g_str_equal (expected, g_variant_get_string (path, NULL));
1081                 g_variant_unref (path);
1082                 g_free (expected);
1083
1084                 /* Schema is non-relocatable and did not have the
1085                  * expected path -> remove it from the list
1086                  */
1087                 if (!same)
1088                   g_hash_table_iter_remove (&iter);
1089               }
1090
1091             gvdb_table_unref (child_table);
1092           }
1093
1094       /* Now create the list */
1095       len = g_hash_table_size (items);
1096       schema->items = g_new (GQuark, len);
1097       i = 0;
1098       g_hash_table_iter_init (&iter, items);
1099
1100       while (g_hash_table_iter_next (&iter, &name, NULL))
1101         schema->items[i++] = g_quark_from_string (name);
1102       schema->n_items = i;
1103       g_assert (i == len);
1104
1105       g_hash_table_unref (items);
1106     }
1107
1108   *n_items = schema->n_items;
1109   return schema->items;
1110 }
1111
1112 /**
1113  * g_settings_schema_get_id:
1114  * @schema: a #GSettingsSchema
1115  *
1116  * Get the ID of @schema.
1117  *
1118  * Returns: (transfer none): the ID
1119  **/
1120 const gchar *
1121 g_settings_schema_get_id (GSettingsSchema *schema)
1122 {
1123   return schema->id;
1124 }
1125
1126 static inline void
1127 endian_fixup (GVariant **value)
1128 {
1129 #if G_BYTE_ORDER == G_BIG_ENDIAN
1130   GVariant *tmp;
1131
1132   tmp = g_variant_byteswap (*value);
1133   g_variant_unref (*value);
1134   *value = tmp;
1135 #endif
1136 }
1137
1138 void
1139 g_settings_schema_key_init (GSettingsSchemaKey *key,
1140                             GSettingsSchema    *schema,
1141                             const gchar        *name)
1142 {
1143   GVariantIter *iter;
1144   GVariant *data;
1145   guchar code;
1146
1147   memset (key, 0, sizeof *key);
1148
1149   iter = g_settings_schema_get_value (schema, name);
1150
1151   key->schema = g_settings_schema_ref (schema);
1152   key->default_value = g_variant_iter_next_value (iter);
1153   endian_fixup (&key->default_value);
1154   key->type = g_variant_get_type (key->default_value);
1155   key->name = g_intern_string (name);
1156
1157   while (g_variant_iter_next (iter, "(y*)", &code, &data))
1158     {
1159       switch (code)
1160         {
1161         case 'l':
1162           /* translation requested */
1163           g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1164           break;
1165
1166         case 'e':
1167           /* enumerated types... */
1168           key->is_enum = TRUE;
1169           goto choice;
1170
1171         case 'f':
1172           /* flags... */
1173           key->is_flags = TRUE;
1174           goto choice;
1175
1176         choice: case 'c':
1177           /* ..., choices, aliases */
1178           key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1179           break;
1180
1181         case 'r':
1182           g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1183           endian_fixup (&key->minimum);
1184           endian_fixup (&key->maximum);
1185           break;
1186
1187         default:
1188           g_warning ("unknown schema extension '%c'", code);
1189           break;
1190         }
1191
1192       g_variant_unref (data);
1193     }
1194
1195   g_variant_iter_free (iter);
1196 }
1197
1198 void
1199 g_settings_schema_key_clear (GSettingsSchemaKey *key)
1200 {
1201   if (key->minimum)
1202     g_variant_unref (key->minimum);
1203
1204   if (key->maximum)
1205     g_variant_unref (key->maximum);
1206
1207   g_variant_unref (key->default_value);
1208
1209   g_settings_schema_unref (key->schema);
1210 }
1211
1212 gboolean
1213 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1214                                   GVariant           *value)
1215 {
1216   g_return_val_if_fail (value != NULL, FALSE);
1217
1218   return g_variant_is_of_type (value, key->type);
1219 }
1220
1221 GVariant *
1222 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1223                                    GVariant           *value)
1224 {
1225   const gchar *target;
1226
1227   if (g_settings_schema_key_range_check (key, value))
1228     return g_variant_ref (value);
1229
1230   if (key->strinfo == NULL)
1231     return NULL;
1232
1233   if (g_variant_is_container (value))
1234     {
1235       GVariantBuilder builder;
1236       GVariantIter iter;
1237       GVariant *child;
1238
1239       g_variant_iter_init (&iter, value);
1240       g_variant_builder_init (&builder, g_variant_get_type (value));
1241
1242       while ((child = g_variant_iter_next_value (&iter)))
1243         {
1244           GVariant *fixed;
1245
1246           fixed = g_settings_schema_key_range_fixup (key, child);
1247           g_variant_unref (child);
1248
1249           if (fixed == NULL)
1250             {
1251               g_variant_builder_clear (&builder);
1252               return NULL;
1253             }
1254
1255           g_variant_builder_add_value (&builder, fixed);
1256           g_variant_unref (fixed);
1257         }
1258
1259       return g_variant_ref_sink (g_variant_builder_end (&builder));
1260     }
1261
1262   target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1263                                       g_variant_get_string (value, NULL));
1264   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1265 }
1266
1267 GVariant *
1268 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1269 {
1270   const gchar *translated;
1271   GError *error = NULL;
1272   const gchar *domain;
1273   GVariant *value;
1274
1275   domain = g_settings_schema_get_gettext_domain (key->schema);
1276
1277   if (key->lc_char == '\0')
1278     /* translation not requested for this key */
1279     return NULL;
1280
1281   if (key->lc_char == 't')
1282     translated = g_dcgettext (domain, key->unparsed, LC_TIME);
1283   else
1284     translated = g_dgettext (domain, key->unparsed);
1285
1286   if (translated == key->unparsed)
1287     /* the default value was not translated */
1288     return NULL;
1289
1290   /* try to parse the translation of the unparsed default */
1291   value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1292
1293   if (value == NULL)
1294     {
1295       g_warning ("Failed to parse translated string '%s' for "
1296                  "key '%s' in schema '%s': %s", key->unparsed, key->name,
1297                  g_settings_schema_get_id (key->schema), error->message);
1298       g_warning ("Using untranslated default instead.");
1299       g_error_free (error);
1300     }
1301
1302   else if (!g_settings_schema_key_range_check (key, value))
1303     {
1304       g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1305                  "is outside of valid range", key->unparsed, key->name,
1306                  g_settings_schema_get_id (key->schema));
1307       g_variant_unref (value);
1308       value = NULL;
1309     }
1310
1311   return value;
1312 }
1313
1314 gint
1315 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1316                                GVariant           *value)
1317 {
1318   gboolean it_worked;
1319   guint result;
1320
1321   it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1322                                         g_variant_get_string (value, NULL),
1323                                         &result);
1324
1325   /* 'value' can only come from the backend after being filtered for validity,
1326    * from the translation after being filtered for validity, or from the schema
1327    * itself (which the schema compiler checks for validity).  If this assertion
1328    * fails then it's really a bug in GSettings or the schema compiler...
1329    */
1330   g_assert (it_worked);
1331
1332   return result;
1333 }
1334
1335 GVariant *
1336 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1337                                  gint                value)
1338 {
1339   const gchar *string;
1340
1341   string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1342
1343   if (string == NULL)
1344     return NULL;
1345
1346   return g_variant_new_string (string);
1347 }
1348
1349 guint
1350 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1351                                 GVariant           *value)
1352 {
1353   GVariantIter iter;
1354   const gchar *flag;
1355   guint result;
1356
1357   result = 0;
1358   g_variant_iter_init (&iter, value);
1359   while (g_variant_iter_next (&iter, "&s", &flag))
1360     {
1361       gboolean it_worked;
1362       guint flag_value;
1363
1364       it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1365       /* as in g_settings_to_enum() */
1366       g_assert (it_worked);
1367
1368       result |= flag_value;
1369     }
1370
1371   return result;
1372 }
1373
1374 GVariant *
1375 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1376                                   guint               value)
1377 {
1378   GVariantBuilder builder;
1379   gint i;
1380
1381   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1382
1383   for (i = 0; i < 32; i++)
1384     if (value & (1u << i))
1385       {
1386         const gchar *string;
1387
1388         string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1389
1390         if (string == NULL)
1391           {
1392             g_variant_builder_clear (&builder);
1393             return NULL;
1394           }
1395
1396         g_variant_builder_add (&builder, "s", string);
1397       }
1398
1399   return g_variant_builder_end (&builder);
1400 }
1401
1402 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1403
1404 /**
1405  * g_settings_schema_key_ref:
1406  * @key: a #GSettingsSchemaKey
1407  *
1408  * Increase the reference count of @key, returning a new reference.
1409  *
1410  * Returns: a new reference to @key
1411  *
1412  * Since: 2.40
1413  **/
1414 GSettingsSchemaKey *
1415 g_settings_schema_key_ref (GSettingsSchemaKey *key)
1416 {
1417   g_return_val_if_fail (key != NULL, NULL);
1418
1419   g_atomic_int_inc (&key->ref_count);
1420
1421   return key;
1422 }
1423
1424 /**
1425  * g_settings_schema_key_unref:
1426  * @key: a #GSettingsSchemaKey
1427  *
1428  * Decrease the reference count of @key, possibly freeing it.
1429  *
1430  * Since: 2.40
1431  **/
1432 void
1433 g_settings_schema_key_unref (GSettingsSchemaKey *key)
1434 {
1435   g_return_if_fail (key != NULL);
1436
1437   if (g_atomic_int_dec_and_test (&key->ref_count))
1438     {
1439       g_settings_schema_key_clear (key);
1440
1441       g_slice_free (GSettingsSchemaKey, key);
1442     }
1443 }
1444
1445 /**
1446  * g_settings_schema_get_key:
1447  * @schema: a #GSettingsSchema
1448  * @name: the name of a key
1449  *
1450  * Gets the key named @name from @schema.
1451  *
1452  * It is a programmer error to request a key that does not exist.  See
1453  * g_settings_schema_list_keys().
1454  *
1455  * Returns: (transfer full): the #GSettingsSchemaKey for @name
1456  *
1457  * Since: 2.40
1458  **/
1459 GSettingsSchemaKey *
1460 g_settings_schema_get_key (GSettingsSchema *schema,
1461                            const gchar     *name)
1462 {
1463   GSettingsSchemaKey *key;
1464
1465   g_return_val_if_fail (schema != NULL, NULL);
1466   g_return_val_if_fail (name != NULL, NULL);
1467
1468   key = g_slice_new (GSettingsSchemaKey);
1469   g_settings_schema_key_init (key, schema, name);
1470   key->ref_count = 1;
1471
1472   return key;
1473 }
1474
1475 /**
1476  * g_settings_schema_key_get_summary:
1477  * @key: a #GSettingsSchemaKey
1478  *
1479  * Gets the summary for @key.
1480  *
1481  * If no summary has been provided in the schema for @key, returns
1482  * %NULL.
1483  *
1484  * The summary is a short description of the purpose of the key; usually
1485  * one short sentence.  Summaries can be translated and the value
1486  * returned from this function is is the current locale.
1487  *
1488  * This function is slow.  The summary and description information for
1489  * the schemas is not stored in the compiled schema database so this
1490  * function has to parse all of the source XML files in the schema
1491  * directory.
1492  *
1493  * Returns: the summary for @key, or %NULL
1494  *
1495  * Since: 2.34
1496  **/
1497 const gchar *
1498 g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1499 {
1500   GHashTable **text_tables;
1501   GHashTable *summaries;
1502
1503   text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1504   summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1505
1506   return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1507 }
1508
1509 /**
1510  * g_settings_schema_key_get_description:
1511  * @key: a #GSettingsSchemaKey
1512  *
1513  * Gets the description for @key.
1514  *
1515  * If no description has been provided in the schema for @key, returns
1516  * %NULL.
1517  *
1518  * The description can be one sentence to several paragraphs in length.
1519  * Paragraphs are delimited with a double newline.  Descriptions can be
1520  * translated and the value returned from this function is is the
1521  * current locale.
1522  *
1523  * This function is slow.  The summary and description information for
1524  * the schemas is not stored in the compiled schema database so this
1525  * function has to parse all of the source XML files in the schema
1526  * directory.
1527  *
1528  * Returns: the description for @key, or %NULL
1529  *
1530  * Since: 2.34
1531  **/
1532 const gchar *
1533 g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1534 {
1535   GHashTable **text_tables;
1536   GHashTable *descriptions;
1537
1538   text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1539   descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1540
1541   return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1542 }
1543
1544 /**
1545  * g_settings_schema_key_get_value_type:
1546  * @key: a #GSettingsSchemaKey
1547  *
1548  * Gets the #GVariantType of @key.
1549  *
1550  * Returns: (transfer none): the type of @key
1551  *
1552  * Since: 2.40
1553  **/
1554 const GVariantType *
1555 g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1556 {
1557   g_return_val_if_fail (key, NULL);
1558
1559   return key->type;
1560 }
1561
1562 /**
1563  * g_settings_schema_key_get_default_value:
1564  * @key: a #GSettingsSchemaKey
1565  *
1566  * Gets the default value for @key.
1567  *
1568  * Note that this is the default value according to the schema.  System
1569  * administrator defaults and lockdown are not visible via this API.
1570  *
1571  * Returns: (transfer full): the default value for the key
1572  *
1573  * Since: 2.40
1574  **/
1575 GVariant *
1576 g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1577 {
1578   GVariant *value;
1579
1580   g_return_val_if_fail (key, NULL);
1581
1582   value = g_settings_schema_key_get_translated_default (key);
1583
1584   if (!value)
1585     value = g_variant_ref (key->default_value);
1586
1587   return value;
1588 }
1589
1590 /**
1591  * g_settings_schema_key_get_range:
1592  * @key: a #GSettingsSchemaKey
1593  *
1594  * Queries the range of a key.
1595  *
1596  * This function will return a #GVariant that fully describes the range
1597  * of values that are valid for @key.
1598  *
1599  * The type of #GVariant returned is <literal>(sv)</literal>.  The
1600  * string describes the type of range restriction in effect.  The type
1601  * and meaning of the value contained in the variant depends on the
1602  * string.
1603  *
1604  * If the string is <literal>'type'</literal> then the variant contains
1605  * an empty array.  The element type of that empty array is the expected
1606  * type of value and all values of that type are valid.
1607  *
1608  * If the string is <literal>'enum'</literal> then the variant contains
1609  * an array enumerating the possible values.  Each item in the array is
1610  * a possible valid value and no other values are valid.
1611  *
1612  * If the string is <literal>'flags'</literal> then the variant contains
1613  * an array.  Each item in the array is a value that may appear zero or
1614  * one times in an array to be used as the value for this key.  For
1615  * example, if the variant contained the array <literal>['x',
1616  * 'y']</literal> then the valid values for the key would be
1617  * <literal>[]</literal>, <literal>['x']</literal>,
1618  * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
1619  * <literal>['y', 'x']</literal>.
1620  *
1621  * Finally, if the string is <literal>'range'</literal> then the variant
1622  * contains a pair of like-typed values -- the minimum and maximum
1623  * permissible values for this key.
1624  *
1625  * This information should not be used by normal programs.  It is
1626  * considered to be a hint for introspection purposes.  Normal programs
1627  * should already know what is permitted by their own schema.  The
1628  * format may change in any way in the future -- but particularly, new
1629  * forms may be added to the possibilities described above.
1630  *
1631  * You should free the returned value with g_variant_unref() when it is
1632  * no longer needed.
1633  *
1634  * Returns: (transfer full): a #GVariant describing the range
1635  *
1636  * Since: 2.40
1637  **/
1638 GVariant *
1639 g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1640 {
1641   const gchar *type;
1642   GVariant *range;
1643
1644   if (key->minimum)
1645     {
1646       range = g_variant_new ("(**)", key->minimum, key->maximum);
1647       type = "range";
1648     }
1649   else if (key->strinfo)
1650     {
1651       range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1652       type = key->is_flags ? "flags" : "enum";
1653     }
1654   else
1655     {
1656       range = g_variant_new_array (key->type, NULL, 0);
1657       type = "type";
1658     }
1659
1660   return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1661 }
1662
1663 /**
1664  * g_settings_schema_key_range_check:
1665  * @key: a #GSettingsSchemaKey
1666  * @value: the value to check
1667  *
1668  * Checks if the given @value is of the correct type and within the
1669  * permitted range for @key.
1670  *
1671  * It is a programmer error if @value is not of the correct type -- you
1672  * must check for this first.
1673  *
1674  * Returns: %TRUE if @value is valid for @key
1675  *
1676  * Since: 2.40
1677  **/
1678 gboolean
1679 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1680                                    GVariant           *value)
1681 {
1682   if (key->minimum == NULL && key->strinfo == NULL)
1683     return TRUE;
1684
1685   if (g_variant_is_container (value))
1686     {
1687       gboolean ok = TRUE;
1688       GVariantIter iter;
1689       GVariant *child;
1690
1691       g_variant_iter_init (&iter, value);
1692       while (ok && (child = g_variant_iter_next_value (&iter)))
1693         {
1694           ok = g_settings_schema_key_range_check (key, child);
1695           g_variant_unref (child);
1696         }
1697
1698       return ok;
1699     }
1700
1701   if (key->minimum)
1702     {
1703       return g_variant_compare (key->minimum, value) <= 0 &&
1704              g_variant_compare (value, key->maximum) <= 0;
1705     }
1706
1707   return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1708                                   g_variant_get_string (value, NULL));
1709 }