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