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