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