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