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