tests: Skip some more date tests if translations are not installed
[glib.git] / gio / gsettingsschema.c
blob17b7e3b01c30b7ef9ac6d737f7683854cf3503a5
1 /*
2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2011 Canonical Limited
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.1 of the License, or (at your option) any later version.
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.
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/>.
19 #include "config.h"
21 #include "gsettingsschema-internal.h"
22 #include "gsettings.h"
24 #include "gvdb/gvdb-reader.h"
25 #include "strinfo.c"
27 #include <glibintl.h>
28 #include <locale.h>
29 #include <string.h>
30 #include <stdlib.h>
32 /**
33 * SECTION:gsettingsschema
34 * @short_description: Introspecting and controlling the loading
35 * of GSettings schemas
36 * @include: gio/gio.h
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.
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.
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.
54 * Consider the following example:
56 * |[<!-- language="C" -->
57 * typedef struct
58 * {
59 * ...
60 * GSettingsSchemaSource *schema_source;
61 * ...
62 * } Plugin;
64 * Plugin *
65 * initialise_plugin (const gchar *dir)
66 * {
67 * Plugin *plugin;
69 * ...
71 * plugin->schema_source =
72 * g_settings_schema_source_new_from_directory (dir,
73 * g_settings_schema_source_get_default (), FALSE, NULL);
75 * ...
77 * return plugin;
78 * }
80 * ...
82 * GSettings *
83 * plugin_get_settings (Plugin *plugin,
84 * const gchar *schema_id)
85 * {
86 * GSettingsSchema *schema;
88 * if (schema_id == NULL)
89 * schema_id = plugin->identifier;
91 * schema = g_settings_schema_source_lookup (plugin->schema_source,
92 * schema_id, FALSE);
94 * if (schema == NULL)
95 * {
96 * ... disable the plugin or abort, etc ...
97 * }
99 * return g_settings_new_full (schema, NULL, NULL);
101 * ]|
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.
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:
113 * |[<!-- language="C" -->
115 * GSettings *settings;
116 * gint some_value;
118 * settings = plugin_get_settings (self, NULL);
119 * some_value = g_settings_get_int (settings, "some-value");
120 * ...
122 * ]|
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.
129 * Since: 2.32
133 * GSettingsSchemaKey:
135 * #GSettingsSchemaKey is an opaque data structure and can only be accessed
136 * using the following functions.
140 * GSettingsSchema:
142 * This is an opaque structure type. You may not access it directly.
144 * Since: 2.32
146 struct _GSettingsSchema
148 GSettingsSchemaSource *source;
149 const gchar *gettext_domain;
150 const gchar *path;
151 GQuark *items;
152 gint n_items;
153 GvdbTable *table;
154 gchar *id;
156 GSettingsSchema *extends;
158 gint ref_count;
162 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
164 * A boxed #GType corresponding to #GSettingsSchemaSource.
166 * Since: 2.32
168 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
171 * G_TYPE_SETTINGS_SCHEMA:
173 * A boxed #GType corresponding to #GSettingsSchema.
175 * Since: 2.32
177 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
180 * GSettingsSchemaSource:
182 * This is an opaque structure type. You may not access it directly.
184 * Since: 2.32
186 struct _GSettingsSchemaSource
188 GSettingsSchemaSource *parent;
189 gchar *directory;
190 GvdbTable *table;
191 GHashTable **text_tables;
193 gint ref_count;
196 static GSettingsSchemaSource *schema_sources;
199 * g_settings_schema_source_ref:
200 * @source: a #GSettingsSchemaSource
202 * Increase the reference count of @source, returning a new reference.
204 * Returns: a new reference to @source
206 * Since: 2.32
208 GSettingsSchemaSource *
209 g_settings_schema_source_ref (GSettingsSchemaSource *source)
211 g_atomic_int_inc (&source->ref_count);
213 return source;
217 * g_settings_schema_source_unref:
218 * @source: a #GSettingsSchemaSource
220 * Decrease the reference count of @source, possibly freeing it.
222 * Since: 2.32
224 void
225 g_settings_schema_source_unref (GSettingsSchemaSource *source)
227 if (g_atomic_int_dec_and_test (&source->ref_count))
229 if (source == schema_sources)
230 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
232 if (source->parent)
233 g_settings_schema_source_unref (source->parent);
234 gvdb_table_unref (source->table);
235 g_free (source->directory);
237 if (source->text_tables)
239 g_hash_table_unref (source->text_tables[0]);
240 g_hash_table_unref (source->text_tables[1]);
241 g_free (source->text_tables);
244 g_slice_free (GSettingsSchemaSource, source);
249 * g_settings_schema_source_new_from_directory:
250 * @directory: (type filename): the filename of a directory
251 * @parent: (nullable): a #GSettingsSchemaSource, or %NULL
252 * @trusted: %TRUE, if the directory is trusted
253 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
255 * Attempts to create a new schema source corresponding to the contents
256 * of the given directory.
258 * This function is not required for normal uses of #GSettings but it
259 * may be useful to authors of plugin management systems.
261 * The directory should contain a file called `gschemas.compiled` as
262 * produced by the [glib-compile-schemas][glib-compile-schemas] tool.
264 * If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
265 * corrupted. This assumption has a performance advantage, but can result
266 * in crashes or inconsistent behaviour in the case of a corrupted file.
267 * Generally, you should set @trusted to %TRUE for files installed by the
268 * system and to %FALSE for files in the home directory.
270 * If @parent is non-%NULL then there are two effects.
272 * First, if g_settings_schema_source_lookup() is called with the
273 * @recursive flag set to %TRUE and the schema can not be found in the
274 * source, the lookup will recurse to the parent.
276 * Second, any references to other schemas specified within this
277 * source (ie: `child` or `extends`) references may be resolved
278 * from the @parent.
280 * For this second reason, except in very unusual situations, the
281 * @parent should probably be given as the default schema source, as
282 * returned by g_settings_schema_source_get_default().
284 * Since: 2.32
286 GSettingsSchemaSource *
287 g_settings_schema_source_new_from_directory (const gchar *directory,
288 GSettingsSchemaSource *parent,
289 gboolean trusted,
290 GError **error)
292 GSettingsSchemaSource *source;
293 GvdbTable *table;
294 gchar *filename;
296 filename = g_build_filename (directory, "gschemas.compiled", NULL);
297 table = gvdb_table_new (filename, trusted, error);
298 g_free (filename);
300 if (table == NULL)
301 return NULL;
303 source = g_slice_new (GSettingsSchemaSource);
304 source->directory = g_strdup (directory);
305 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
306 source->text_tables = NULL;
307 source->table = table;
308 source->ref_count = 1;
310 return source;
313 static void
314 try_prepend_dir (const gchar *directory)
316 GSettingsSchemaSource *source;
318 source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
320 /* If we successfully created it then prepend it to the global list */
321 if (source != NULL)
322 schema_sources = source;
325 static void
326 try_prepend_data_dir (const gchar *directory)
328 gchar *dirname = g_build_filename (directory, "glib-2.0", "schemas", NULL);
329 try_prepend_dir (dirname);
330 g_free (dirname);
333 static void
334 initialise_schema_sources (void)
336 static gsize initialised;
338 /* need a separate variable because 'schema_sources' may legitimately
339 * be null if we have zero valid schema sources
341 if G_UNLIKELY (g_once_init_enter (&initialised))
343 const gchar * const *dirs;
344 const gchar *path;
345 gint i;
347 /* iterate in reverse: count up, then count down */
348 dirs = g_get_system_data_dirs ();
349 for (i = 0; dirs[i]; i++);
351 while (i--)
352 try_prepend_data_dir (dirs[i]);
354 try_prepend_data_dir (g_get_user_data_dir ());
356 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
357 try_prepend_dir (path);
359 g_once_init_leave (&initialised, TRUE);
364 * g_settings_schema_source_get_default:
366 * Gets the default system schema source.
368 * This function is not required for normal uses of #GSettings but it
369 * may be useful to authors of plugin management systems or to those who
370 * want to introspect the content of schemas.
372 * If no schemas are installed, %NULL will be returned.
374 * The returned source may actually consist of multiple schema sources
375 * from different directories, depending on which directories were given
376 * in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
377 * lookups performed against the default source should probably be done
378 * recursively.
380 * Returns: (transfer none) (nullable): the default schema source
382 * Since: 2.32
384 GSettingsSchemaSource *
385 g_settings_schema_source_get_default (void)
387 initialise_schema_sources ();
389 return schema_sources;
393 * g_settings_schema_source_lookup:
394 * @source: a #GSettingsSchemaSource
395 * @schema_id: a schema ID
396 * @recursive: %TRUE if the lookup should be recursive
398 * Looks up a schema with the identifier @schema_id in @source.
400 * This function is not required for normal uses of #GSettings but it
401 * may be useful to authors of plugin management systems or to those who
402 * want to introspect the content of schemas.
404 * If the schema isn't found directly in @source and @recursive is %TRUE
405 * then the parent sources will also be checked.
407 * If the schema isn't found, %NULL is returned.
409 * Returns: (nullable) (transfer full): a new #GSettingsSchema
411 * Since: 2.32
413 GSettingsSchema *
414 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
415 const gchar *schema_id,
416 gboolean recursive)
418 GSettingsSchema *schema;
419 GvdbTable *table;
420 const gchar *extends;
422 g_return_val_if_fail (source != NULL, NULL);
423 g_return_val_if_fail (schema_id != NULL, NULL);
425 table = gvdb_table_get_table (source->table, schema_id);
427 if (table == NULL && recursive)
428 for (source = source->parent; source; source = source->parent)
429 if ((table = gvdb_table_get_table (source->table, schema_id)))
430 break;
432 if (table == NULL)
433 return NULL;
435 schema = g_slice_new0 (GSettingsSchema);
436 schema->source = g_settings_schema_source_ref (source);
437 schema->ref_count = 1;
438 schema->id = g_strdup (schema_id);
439 schema->table = table;
440 schema->path = g_settings_schema_get_string (schema, ".path");
441 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
443 if (schema->gettext_domain)
444 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
446 extends = g_settings_schema_get_string (schema, ".extends");
447 if (extends)
449 schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
450 if (schema->extends == NULL)
451 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
454 return schema;
457 typedef struct
459 GHashTable *summaries;
460 GHashTable *descriptions;
461 GSList *gettext_domain;
462 GSList *schema_id;
463 GSList *key_name;
464 GString *string;
465 } TextTableParseInfo;
467 static const gchar *
468 get_attribute_value (GSList *list)
470 GSList *node;
472 for (node = list; node; node = node->next)
473 if (node->data)
474 return node->data;
476 return NULL;
479 static void
480 pop_attribute_value (GSList **list)
482 gchar *top;
484 top = (*list)->data;
485 *list = g_slist_remove (*list, top);
487 g_free (top);
490 static void
491 push_attribute_value (GSList **list,
492 const gchar *value)
494 *list = g_slist_prepend (*list, g_strdup (value));
497 static void
498 start_element (GMarkupParseContext *context,
499 const gchar *element_name,
500 const gchar **attribute_names,
501 const gchar **attribute_values,
502 gpointer user_data,
503 GError **error)
505 TextTableParseInfo *info = user_data;
506 const gchar *gettext_domain = NULL;
507 const gchar *schema_id = NULL;
508 const gchar *key_name = NULL;
509 gint i;
511 for (i = 0; attribute_names[i]; i++)
513 if (g_str_equal (attribute_names[i], "gettext-domain"))
514 gettext_domain = attribute_values[i];
515 else if (g_str_equal (attribute_names[i], "id"))
516 schema_id = attribute_values[i];
517 else if (g_str_equal (attribute_names[i], "name"))
518 key_name = attribute_values[i];
521 push_attribute_value (&info->gettext_domain, gettext_domain);
522 push_attribute_value (&info->schema_id, schema_id);
523 push_attribute_value (&info->key_name, key_name);
525 if (info->string)
527 g_string_free (info->string, TRUE);
528 info->string = NULL;
531 if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
532 info->string = g_string_new (NULL);
535 static gchar *
536 normalise_whitespace (const gchar *orig)
538 /* We normalise by the same rules as in intltool:
540 * sub cleanup {
541 * s/^\s+//;
542 * s/\s+$//;
543 * s/\s+/ /g;
544 * return $_;
547 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
549 * Where \s is an ascii space character.
551 * We aim for ease of implementation over efficiency -- this code is
552 * not run in normal applications.
554 static GRegex *cleanup[3];
555 static GRegex *splitter;
556 gchar **lines;
557 gchar *result;
558 gint i;
560 if (g_once_init_enter (&splitter))
562 GRegex *s;
564 cleanup[0] = g_regex_new ("^\\s+", 0, 0, 0);
565 cleanup[1] = g_regex_new ("\\s+$", 0, 0, 0);
566 cleanup[2] = g_regex_new ("\\s+", 0, 0, 0);
567 s = g_regex_new ("\\n\\s*\\n+", 0, 0, 0);
569 g_once_init_leave (&splitter, s);
572 lines = g_regex_split (splitter, orig, 0);
573 for (i = 0; lines[i]; i++)
575 gchar *a, *b, *c;
577 a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
578 b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
579 c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
580 g_free (lines[i]);
581 g_free (a);
582 g_free (b);
583 lines[i] = c;
586 result = g_strjoinv ("\n\n", lines);
587 g_strfreev (lines);
589 return result;
592 static void
593 end_element (GMarkupParseContext *context,
594 const gchar *element_name,
595 gpointer user_data,
596 GError **error)
598 TextTableParseInfo *info = user_data;
600 pop_attribute_value (&info->gettext_domain);
601 pop_attribute_value (&info->schema_id);
602 pop_attribute_value (&info->key_name);
604 if (info->string)
606 GHashTable *source_table = NULL;
607 const gchar *gettext_domain;
608 const gchar *schema_id;
609 const gchar *key_name;
611 gettext_domain = get_attribute_value (info->gettext_domain);
612 schema_id = get_attribute_value (info->schema_id);
613 key_name = get_attribute_value (info->key_name);
615 if (g_str_equal (element_name, "summary"))
616 source_table = info->summaries;
617 else if (g_str_equal (element_name, "description"))
618 source_table = info->descriptions;
620 if (source_table && schema_id && key_name)
622 GHashTable *schema_table;
623 gchar *normalised;
625 schema_table = g_hash_table_lookup (source_table, schema_id);
627 if (schema_table == NULL)
629 schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
630 g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
633 normalised = normalise_whitespace (info->string->str);
635 if (gettext_domain && normalised[0])
637 gchar *translated;
639 translated = g_strdup (g_dgettext (gettext_domain, normalised));
640 g_free (normalised);
641 normalised = translated;
644 g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
647 g_string_free (info->string, TRUE);
648 info->string = NULL;
652 static void
653 text (GMarkupParseContext *context,
654 const gchar *text,
655 gsize text_len,
656 gpointer user_data,
657 GError **error)
659 TextTableParseInfo *info = user_data;
661 if (info->string)
662 g_string_append_len (info->string, text, text_len);
665 static void
666 parse_into_text_tables (const gchar *directory,
667 GHashTable *summaries,
668 GHashTable *descriptions)
670 GMarkupParser parser = { start_element, end_element, text };
671 TextTableParseInfo info = { summaries, descriptions };
672 const gchar *basename;
673 GDir *dir;
675 dir = g_dir_open (directory, 0, NULL);
676 while ((basename = g_dir_read_name (dir)))
678 gchar *filename;
679 gchar *contents;
680 gsize size;
682 filename = g_build_filename (directory, basename, NULL);
683 if (g_file_get_contents (filename, &contents, &size, NULL))
685 GMarkupParseContext *context;
687 context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
688 /* Ignore errors here, this is best effort only. */
689 if (g_markup_parse_context_parse (context, contents, size, NULL))
690 (void) g_markup_parse_context_end_parse (context, NULL);
691 g_markup_parse_context_free (context);
693 /* Clean up dangling stuff in case there was an error. */
694 g_slist_free_full (info.gettext_domain, g_free);
695 g_slist_free_full (info.schema_id, g_free);
696 g_slist_free_full (info.key_name, g_free);
698 info.gettext_domain = NULL;
699 info.schema_id = NULL;
700 info.key_name = NULL;
702 if (info.string)
704 g_string_free (info.string, TRUE);
705 info.string = NULL;
708 g_free (contents);
711 g_free (filename);
714 g_dir_close (dir);
717 static GHashTable **
718 g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
720 if (g_once_init_enter (&source->text_tables))
722 GHashTable **text_tables;
724 text_tables = g_new (GHashTable *, 2);
725 text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
726 text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
728 if (source->directory)
729 parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
731 g_once_init_leave (&source->text_tables, text_tables);
734 return source->text_tables;
738 * g_settings_schema_source_list_schemas:
739 * @source: a #GSettingsSchemaSource
740 * @recursive: if we should recurse
741 * @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
742 * list of non-relocatable schemas
743 * @relocatable: (out) (transfer full) (array zero-terminated=1): the list
744 * of relocatable schemas
746 * Lists the schemas in a given source.
748 * If @recursive is %TRUE then include parent sources. If %FALSE then
749 * only include the schemas from one source (ie: one directory). You
750 * probably want %TRUE.
752 * Non-relocatable schemas are those for which you can call
753 * g_settings_new(). Relocatable schemas are those for which you must
754 * use g_settings_new_with_path().
756 * Do not call this function from normal programs. This is designed for
757 * use by database editors, commandline tools, etc.
759 * Since: 2.40
761 void
762 g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
763 gboolean recursive,
764 gchar ***non_relocatable,
765 gchar ***relocatable)
767 GHashTable *single, *reloc;
768 GSettingsSchemaSource *s;
770 /* We use hash tables to avoid duplicate listings for schemas that
771 * appear in more than one file.
773 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
774 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
776 for (s = source; s; s = s->parent)
778 gchar **list;
779 gint i;
781 list = gvdb_table_list (s->table, "");
783 /* empty schema cache file? */
784 if (list == NULL)
785 continue;
787 for (i = 0; list[i]; i++)
789 if (!g_hash_table_contains (single, list[i]) &&
790 !g_hash_table_contains (reloc, list[i]))
792 gchar *schema;
793 GvdbTable *table;
795 schema = g_strdup (list[i]);
797 table = gvdb_table_get_table (s->table, list[i]);
798 g_assert (table != NULL);
800 if (gvdb_table_has_value (table, ".path"))
801 g_hash_table_add (single, schema);
802 else
803 g_hash_table_add (reloc, schema);
805 gvdb_table_unref (table);
809 g_strfreev (list);
811 /* Only the first source if recursive not requested */
812 if (!recursive)
813 break;
816 if (non_relocatable)
818 *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
819 g_hash_table_steal_all (single);
822 if (relocatable)
824 *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
825 g_hash_table_steal_all (reloc);
828 g_hash_table_unref (single);
829 g_hash_table_unref (reloc);
832 static gchar **non_relocatable_schema_list;
833 static gchar **relocatable_schema_list;
834 static gsize schema_lists_initialised;
836 static void
837 ensure_schema_lists (void)
839 if (g_once_init_enter (&schema_lists_initialised))
841 initialise_schema_sources ();
843 g_settings_schema_source_list_schemas (schema_sources, TRUE,
844 &non_relocatable_schema_list,
845 &relocatable_schema_list);
847 g_once_init_leave (&schema_lists_initialised, TRUE);
852 * g_settings_list_schemas:
854 * Deprecated.
856 * Returns: (element-type utf8) (transfer none): a list of #GSettings
857 * schemas that are available. The list must not be modified or
858 * freed.
860 * Since: 2.26
862 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead.
863 * If you used g_settings_list_schemas() to check for the presence of
864 * a particular schema, use g_settings_schema_source_lookup() instead
865 * of your whole loop.
867 const gchar * const *
868 g_settings_list_schemas (void)
870 ensure_schema_lists ();
872 return (const gchar **) non_relocatable_schema_list;
876 * g_settings_list_relocatable_schemas:
878 * Deprecated.
880 * Returns: (element-type utf8) (transfer none): a list of relocatable
881 * #GSettings schemas that are available. The list must not be
882 * modified or freed.
884 * Since: 2.28
886 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead
888 const gchar * const *
889 g_settings_list_relocatable_schemas (void)
891 ensure_schema_lists ();
893 return (const gchar **) relocatable_schema_list;
897 * g_settings_schema_ref:
898 * @schema: a #GSettingsSchema
900 * Increase the reference count of @schema, returning a new reference.
902 * Returns: a new reference to @schema
904 * Since: 2.32
906 GSettingsSchema *
907 g_settings_schema_ref (GSettingsSchema *schema)
909 g_atomic_int_inc (&schema->ref_count);
911 return schema;
915 * g_settings_schema_unref:
916 * @schema: a #GSettingsSchema
918 * Decrease the reference count of @schema, possibly freeing it.
920 * Since: 2.32
922 void
923 g_settings_schema_unref (GSettingsSchema *schema)
925 if (g_atomic_int_dec_and_test (&schema->ref_count))
927 if (schema->extends)
928 g_settings_schema_unref (schema->extends);
930 g_settings_schema_source_unref (schema->source);
931 gvdb_table_unref (schema->table);
932 g_free (schema->items);
933 g_free (schema->id);
935 g_slice_free (GSettingsSchema, schema);
939 const gchar *
940 g_settings_schema_get_string (GSettingsSchema *schema,
941 const gchar *key)
943 const gchar *result = NULL;
944 GVariant *value;
946 if ((value = gvdb_table_get_raw_value (schema->table, key)))
948 result = g_variant_get_string (value, NULL);
949 g_variant_unref (value);
952 return result;
955 GVariantIter *
956 g_settings_schema_get_value (GSettingsSchema *schema,
957 const gchar *key)
959 GSettingsSchema *s = schema;
960 GVariantIter *iter;
961 GVariant *value = NULL;
963 g_return_val_if_fail (schema != NULL, NULL);
965 for (s = schema; s; s = s->extends)
966 if ((value = gvdb_table_get_raw_value (s->table, key)))
967 break;
969 if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
970 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
972 iter = g_variant_iter_new (value);
973 g_variant_unref (value);
975 return iter;
979 * g_settings_schema_get_path:
980 * @schema: a #GSettingsSchema
982 * Gets the path associated with @schema, or %NULL.
984 * Schemas may be single-instance or relocatable. Single-instance
985 * schemas correspond to exactly one set of keys in the backend
986 * database: those located at the path returned by this function.
988 * Relocatable schemas can be referenced by other schemas and can
989 * threfore describe multiple sets of keys at different locations. For
990 * relocatable schemas, this function will return %NULL.
992 * Returns: (transfer none): the path of the schema, or %NULL
994 * Since: 2.32
996 const gchar *
997 g_settings_schema_get_path (GSettingsSchema *schema)
999 return schema->path;
1002 const gchar *
1003 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
1005 return schema->gettext_domain;
1009 * g_settings_schema_has_key:
1010 * @schema: a #GSettingsSchema
1011 * @name: the name of a key
1013 * Checks if @schema has a key named @name.
1015 * Returns: %TRUE if such a key exists
1017 * Since: 2.40
1019 gboolean
1020 g_settings_schema_has_key (GSettingsSchema *schema,
1021 const gchar *key)
1023 return gvdb_table_has_value (schema->table, key);
1027 * g_settings_schema_list_children:
1028 * @schema: a #GSettingsSchema
1030 * Gets the list of children in @schema.
1032 * You should free the return value with g_strfreev() when you are done
1033 * with it.
1035 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
1037 * Since: 2.44
1039 gchar **
1040 g_settings_schema_list_children (GSettingsSchema *schema)
1042 const GQuark *keys;
1043 gchar **strv;
1044 gint n_keys;
1045 gint i, j;
1047 g_return_val_if_fail (schema != NULL, NULL);
1049 keys = g_settings_schema_list (schema, &n_keys);
1050 strv = g_new (gchar *, n_keys + 1);
1051 for (i = j = 0; i < n_keys; i++)
1053 const gchar *key = g_quark_to_string (keys[i]);
1055 if (g_str_has_suffix (key, "/"))
1057 gint length = strlen (key);
1059 strv[j] = g_memdup (key, length);
1060 strv[j][length - 1] = '\0';
1061 j++;
1064 strv[j] = NULL;
1066 return strv;
1070 * g_settings_schema_list_keys:
1071 * @schema: a #GSettingsSchema
1073 * Introspects the list of keys on @schema.
1075 * You should probably not be calling this function from "normal" code
1076 * (since you should already know what keys are in your schema). This
1077 * function is intended for introspection reasons.
1079 * Returns: (transfer full) (element-type utf8): a list of the keys on
1080 * @schema
1082 * Since: 2.46
1084 gchar **
1085 g_settings_schema_list_keys (GSettingsSchema *schema)
1087 const GQuark *keys;
1088 gchar **strv;
1089 gint n_keys;
1090 gint i, j;
1092 g_return_val_if_fail (schema != NULL, NULL);
1094 keys = g_settings_schema_list (schema, &n_keys);
1095 strv = g_new (gchar *, n_keys + 1);
1096 for (i = j = 0; i < n_keys; i++)
1098 const gchar *key = g_quark_to_string (keys[i]);
1100 if (!g_str_has_suffix (key, "/"))
1101 strv[j++] = g_strdup (key);
1103 strv[j] = NULL;
1105 return strv;
1108 const GQuark *
1109 g_settings_schema_list (GSettingsSchema *schema,
1110 gint *n_items)
1112 if (schema->items == NULL)
1114 GSettingsSchema *s;
1115 GHashTableIter iter;
1116 GHashTable *items;
1117 gpointer name;
1118 gint len;
1119 gint i;
1121 items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1123 for (s = schema; s; s = s->extends)
1125 gchar **list;
1127 list = gvdb_table_list (s->table, "");
1129 if (list)
1131 for (i = 0; list[i]; i++)
1132 g_hash_table_add (items, list[i]); /* transfer ownership */
1134 g_free (list); /* free container only */
1138 /* Do a first pass to eliminate child items that do not map to
1139 * valid schemas (ie: ones that would crash us if we actually
1140 * tried to create them).
1142 g_hash_table_iter_init (&iter, items);
1143 while (g_hash_table_iter_next (&iter, &name, NULL))
1144 if (g_str_has_suffix (name, "/"))
1146 GSettingsSchemaSource *source;
1147 GVariant *child_schema;
1148 GvdbTable *child_table;
1150 child_schema = gvdb_table_get_raw_value (schema->table, name);
1151 if (!child_schema)
1152 continue;
1154 child_table = NULL;
1156 for (source = schema->source; source; source = source->parent)
1157 if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1158 break;
1160 g_variant_unref (child_schema);
1162 /* Schema is not found -> remove it from the list */
1163 if (child_table == NULL)
1165 g_hash_table_iter_remove (&iter);
1166 continue;
1169 /* Make sure the schema is relocatable or at the
1170 * expected path
1172 if (gvdb_table_has_value (child_table, ".path"))
1174 GVariant *path;
1175 gchar *expected;
1176 gboolean same;
1178 path = gvdb_table_get_raw_value (child_table, ".path");
1179 expected = g_strconcat (schema->path, name, NULL);
1180 same = g_str_equal (expected, g_variant_get_string (path, NULL));
1181 g_variant_unref (path);
1182 g_free (expected);
1184 /* Schema is non-relocatable and did not have the
1185 * expected path -> remove it from the list
1187 if (!same)
1188 g_hash_table_iter_remove (&iter);
1191 gvdb_table_unref (child_table);
1194 /* Now create the list */
1195 len = g_hash_table_size (items);
1196 schema->items = g_new (GQuark, len);
1197 i = 0;
1198 g_hash_table_iter_init (&iter, items);
1200 while (g_hash_table_iter_next (&iter, &name, NULL))
1201 schema->items[i++] = g_quark_from_string (name);
1202 schema->n_items = i;
1203 g_assert (i == len);
1205 g_hash_table_unref (items);
1208 *n_items = schema->n_items;
1209 return schema->items;
1213 * g_settings_schema_get_id:
1214 * @schema: a #GSettingsSchema
1216 * Get the ID of @schema.
1218 * Returns: (transfer none): the ID
1220 const gchar *
1221 g_settings_schema_get_id (GSettingsSchema *schema)
1223 return schema->id;
1226 static inline void
1227 endian_fixup (GVariant **value)
1229 #if G_BYTE_ORDER == G_BIG_ENDIAN
1230 GVariant *tmp;
1232 tmp = g_variant_byteswap (*value);
1233 g_variant_unref (*value);
1234 *value = tmp;
1235 #endif
1238 void
1239 g_settings_schema_key_init (GSettingsSchemaKey *key,
1240 GSettingsSchema *schema,
1241 const gchar *name)
1243 GVariantIter *iter;
1244 GVariant *data;
1245 guchar code;
1247 memset (key, 0, sizeof *key);
1249 iter = g_settings_schema_get_value (schema, name);
1251 key->schema = g_settings_schema_ref (schema);
1252 key->default_value = g_variant_iter_next_value (iter);
1253 endian_fixup (&key->default_value);
1254 key->type = g_variant_get_type (key->default_value);
1255 key->name = g_intern_string (name);
1257 while (g_variant_iter_next (iter, "(y*)", &code, &data))
1259 switch (code)
1261 case 'l':
1262 /* translation requested */
1263 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1264 break;
1266 case 'e':
1267 /* enumerated types... */
1268 key->is_enum = TRUE;
1269 goto choice;
1271 case 'f':
1272 /* flags... */
1273 key->is_flags = TRUE;
1274 goto choice;
1276 choice: case 'c':
1277 /* ..., choices, aliases */
1278 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1279 break;
1281 case 'r':
1282 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1283 endian_fixup (&key->minimum);
1284 endian_fixup (&key->maximum);
1285 break;
1287 case 'd':
1288 g_variant_get (data, "@a{sv}", &key->desktop_overrides);
1289 endian_fixup (&key->desktop_overrides);
1290 break;
1292 default:
1293 g_warning ("unknown schema extension '%c'", code);
1294 break;
1297 g_variant_unref (data);
1300 g_variant_iter_free (iter);
1303 void
1304 g_settings_schema_key_clear (GSettingsSchemaKey *key)
1306 if (key->minimum)
1307 g_variant_unref (key->minimum);
1309 if (key->maximum)
1310 g_variant_unref (key->maximum);
1312 if (key->desktop_overrides)
1313 g_variant_unref (key->desktop_overrides);
1315 g_variant_unref (key->default_value);
1317 g_settings_schema_unref (key->schema);
1320 gboolean
1321 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1322 GVariant *value)
1324 g_return_val_if_fail (value != NULL, FALSE);
1326 return g_variant_is_of_type (value, key->type);
1329 GVariant *
1330 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1331 GVariant *value)
1333 const gchar *target;
1335 if (g_settings_schema_key_range_check (key, value))
1336 return g_variant_ref (value);
1338 if (key->strinfo == NULL)
1339 return NULL;
1341 if (g_variant_is_container (value))
1343 GVariantBuilder builder;
1344 GVariantIter iter;
1345 GVariant *child;
1347 g_variant_iter_init (&iter, value);
1348 g_variant_builder_init (&builder, g_variant_get_type (value));
1350 while ((child = g_variant_iter_next_value (&iter)))
1352 GVariant *fixed;
1354 fixed = g_settings_schema_key_range_fixup (key, child);
1355 g_variant_unref (child);
1357 if (fixed == NULL)
1359 g_variant_builder_clear (&builder);
1360 return NULL;
1363 g_variant_builder_add_value (&builder, fixed);
1364 g_variant_unref (fixed);
1367 return g_variant_ref_sink (g_variant_builder_end (&builder));
1370 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1371 g_variant_get_string (value, NULL));
1372 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1375 GVariant *
1376 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1378 const gchar *translated;
1379 GError *error = NULL;
1380 const gchar *domain;
1381 GVariant *value;
1383 domain = g_settings_schema_get_gettext_domain (key->schema);
1385 if (key->lc_char == '\0')
1386 /* translation not requested for this key */
1387 return NULL;
1389 if (key->lc_char == 't')
1390 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
1391 else
1392 translated = g_dgettext (domain, key->unparsed);
1394 if (translated == key->unparsed)
1395 /* the default value was not translated */
1396 return NULL;
1398 /* try to parse the translation of the unparsed default */
1399 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1401 if (value == NULL)
1403 g_warning ("Failed to parse translated string '%s' for "
1404 "key '%s' in schema '%s': %s", translated, key->name,
1405 g_settings_schema_get_id (key->schema), error->message);
1406 g_warning ("Using untranslated default instead.");
1407 g_error_free (error);
1410 else if (!g_settings_schema_key_range_check (key, value))
1412 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1413 "is outside of valid range", key->unparsed, key->name,
1414 g_settings_schema_get_id (key->schema));
1415 g_variant_unref (value);
1416 value = NULL;
1419 return value;
1422 GVariant *
1423 g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
1425 static const gchar * const *current_desktops;
1426 GVariant *value = NULL;
1427 gint i;
1429 if (!key->desktop_overrides)
1430 return NULL;
1432 if (g_once_init_enter (&current_desktops))
1434 const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
1435 gchar **tmp;
1437 if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
1438 tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
1439 else
1440 tmp = g_new0 (gchar *, 0 + 1);
1442 g_once_init_leave (&current_desktops, (const gchar **) tmp);
1445 for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
1446 value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
1448 return value;
1451 gint
1452 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1453 GVariant *value)
1455 gboolean it_worked;
1456 guint result;
1458 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1459 g_variant_get_string (value, NULL),
1460 &result);
1462 /* 'value' can only come from the backend after being filtered for validity,
1463 * from the translation after being filtered for validity, or from the schema
1464 * itself (which the schema compiler checks for validity). If this assertion
1465 * fails then it's really a bug in GSettings or the schema compiler...
1467 g_assert (it_worked);
1469 return result;
1472 GVariant *
1473 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1474 gint value)
1476 const gchar *string;
1478 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1480 if (string == NULL)
1481 return NULL;
1483 return g_variant_new_string (string);
1486 guint
1487 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1488 GVariant *value)
1490 GVariantIter iter;
1491 const gchar *flag;
1492 guint result;
1494 result = 0;
1495 g_variant_iter_init (&iter, value);
1496 while (g_variant_iter_next (&iter, "&s", &flag))
1498 gboolean it_worked;
1499 guint flag_value;
1501 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1502 /* as in g_settings_to_enum() */
1503 g_assert (it_worked);
1505 result |= flag_value;
1508 return result;
1511 GVariant *
1512 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1513 guint value)
1515 GVariantBuilder builder;
1516 gint i;
1518 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1520 for (i = 0; i < 32; i++)
1521 if (value & (1u << i))
1523 const gchar *string;
1525 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1527 if (string == NULL)
1529 g_variant_builder_clear (&builder);
1530 return NULL;
1533 g_variant_builder_add (&builder, "s", string);
1536 return g_variant_builder_end (&builder);
1539 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1542 * g_settings_schema_key_ref:
1543 * @key: a #GSettingsSchemaKey
1545 * Increase the reference count of @key, returning a new reference.
1547 * Returns: a new reference to @key
1549 * Since: 2.40
1551 GSettingsSchemaKey *
1552 g_settings_schema_key_ref (GSettingsSchemaKey *key)
1554 g_return_val_if_fail (key != NULL, NULL);
1556 g_atomic_int_inc (&key->ref_count);
1558 return key;
1562 * g_settings_schema_key_unref:
1563 * @key: a #GSettingsSchemaKey
1565 * Decrease the reference count of @key, possibly freeing it.
1567 * Since: 2.40
1569 void
1570 g_settings_schema_key_unref (GSettingsSchemaKey *key)
1572 g_return_if_fail (key != NULL);
1574 if (g_atomic_int_dec_and_test (&key->ref_count))
1576 g_settings_schema_key_clear (key);
1578 g_slice_free (GSettingsSchemaKey, key);
1583 * g_settings_schema_get_key:
1584 * @schema: a #GSettingsSchema
1585 * @name: the name of a key
1587 * Gets the key named @name from @schema.
1589 * It is a programmer error to request a key that does not exist. See
1590 * g_settings_schema_list_keys().
1592 * Returns: (transfer full): the #GSettingsSchemaKey for @name
1594 * Since: 2.40
1596 GSettingsSchemaKey *
1597 g_settings_schema_get_key (GSettingsSchema *schema,
1598 const gchar *name)
1600 GSettingsSchemaKey *key;
1602 g_return_val_if_fail (schema != NULL, NULL);
1603 g_return_val_if_fail (name != NULL, NULL);
1605 key = g_slice_new (GSettingsSchemaKey);
1606 g_settings_schema_key_init (key, schema, name);
1607 key->ref_count = 1;
1609 return key;
1613 * g_settings_schema_key_get_name:
1614 * @key: a #GSettingsSchemaKey
1616 * Gets the name of @key.
1618 * Returns: the name of @key.
1620 * Since: 2.44
1622 const gchar *
1623 g_settings_schema_key_get_name (GSettingsSchemaKey *key)
1625 g_return_val_if_fail (key != NULL, NULL);
1627 return key->name;
1631 * g_settings_schema_key_get_summary:
1632 * @key: a #GSettingsSchemaKey
1634 * Gets the summary for @key.
1636 * If no summary has been provided in the schema for @key, returns
1637 * %NULL.
1639 * The summary is a short description of the purpose of the key; usually
1640 * one short sentence. Summaries can be translated and the value
1641 * returned from this function is is the current locale.
1643 * This function is slow. The summary and description information for
1644 * the schemas is not stored in the compiled schema database so this
1645 * function has to parse all of the source XML files in the schema
1646 * directory.
1648 * Returns: the summary for @key, or %NULL
1650 * Since: 2.34
1652 const gchar *
1653 g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1655 GHashTable **text_tables;
1656 GHashTable *summaries;
1658 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1659 summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1661 return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1665 * g_settings_schema_key_get_description:
1666 * @key: a #GSettingsSchemaKey
1668 * Gets the description for @key.
1670 * If no description has been provided in the schema for @key, returns
1671 * %NULL.
1673 * The description can be one sentence to several paragraphs in length.
1674 * Paragraphs are delimited with a double newline. Descriptions can be
1675 * translated and the value returned from this function is is the
1676 * current locale.
1678 * This function is slow. The summary and description information for
1679 * the schemas is not stored in the compiled schema database so this
1680 * function has to parse all of the source XML files in the schema
1681 * directory.
1683 * Returns: the description for @key, or %NULL
1685 * Since: 2.34
1687 const gchar *
1688 g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1690 GHashTable **text_tables;
1691 GHashTable *descriptions;
1693 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1694 descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1696 return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1700 * g_settings_schema_key_get_value_type:
1701 * @key: a #GSettingsSchemaKey
1703 * Gets the #GVariantType of @key.
1705 * Returns: (transfer none): the type of @key
1707 * Since: 2.40
1709 const GVariantType *
1710 g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1712 g_return_val_if_fail (key, NULL);
1714 return key->type;
1718 * g_settings_schema_key_get_default_value:
1719 * @key: a #GSettingsSchemaKey
1721 * Gets the default value for @key.
1723 * Note that this is the default value according to the schema. System
1724 * administrator defaults and lockdown are not visible via this API.
1726 * Returns: (transfer full): the default value for the key
1728 * Since: 2.40
1730 GVariant *
1731 g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1733 GVariant *value;
1735 g_return_val_if_fail (key, NULL);
1737 value = g_settings_schema_key_get_translated_default (key);
1739 if (!value)
1740 value = g_settings_schema_key_get_per_desktop_default (key);
1742 if (!value)
1743 value = g_variant_ref (key->default_value);
1745 return value;
1749 * g_settings_schema_key_get_range:
1750 * @key: a #GSettingsSchemaKey
1752 * Queries the range of a key.
1754 * This function will return a #GVariant that fully describes the range
1755 * of values that are valid for @key.
1757 * The type of #GVariant returned is `(sv)`. The string describes
1758 * the type of range restriction in effect. The type and meaning of
1759 * the value contained in the variant depends on the string.
1761 * If the string is `'type'` then the variant contains an empty array.
1762 * The element type of that empty array is the expected type of value
1763 * and all values of that type are valid.
1765 * If the string is `'enum'` then the variant contains an array
1766 * enumerating the possible values. Each item in the array is
1767 * a possible valid value and no other values are valid.
1769 * If the string is `'flags'` then the variant contains an array. Each
1770 * item in the array is a value that may appear zero or one times in an
1771 * array to be used as the value for this key. For example, if the
1772 * variant contained the array `['x', 'y']` then the valid values for
1773 * the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
1774 * `['y', 'x']`.
1776 * Finally, if the string is `'range'` then the variant contains a pair
1777 * of like-typed values -- the minimum and maximum permissible values
1778 * for this key.
1780 * This information should not be used by normal programs. It is
1781 * considered to be a hint for introspection purposes. Normal programs
1782 * should already know what is permitted by their own schema. The
1783 * format may change in any way in the future -- but particularly, new
1784 * forms may be added to the possibilities described above.
1786 * You should free the returned value with g_variant_unref() when it is
1787 * no longer needed.
1789 * Returns: (transfer full): a #GVariant describing the range
1791 * Since: 2.40
1793 GVariant *
1794 g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1796 const gchar *type;
1797 GVariant *range;
1799 if (key->minimum)
1801 range = g_variant_new ("(**)", key->minimum, key->maximum);
1802 type = "range";
1804 else if (key->strinfo)
1806 range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1807 type = key->is_flags ? "flags" : "enum";
1809 else
1811 range = g_variant_new_array (key->type, NULL, 0);
1812 type = "type";
1815 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1819 * g_settings_schema_key_range_check:
1820 * @key: a #GSettingsSchemaKey
1821 * @value: the value to check
1823 * Checks if the given @value is of the correct type and within the
1824 * permitted range for @key.
1826 * It is a programmer error if @value is not of the correct type -- you
1827 * must check for this first.
1829 * Returns: %TRUE if @value is valid for @key
1831 * Since: 2.40
1833 gboolean
1834 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1835 GVariant *value)
1837 if (key->minimum == NULL && key->strinfo == NULL)
1838 return TRUE;
1840 if (g_variant_is_container (value))
1842 gboolean ok = TRUE;
1843 GVariantIter iter;
1844 GVariant *child;
1846 g_variant_iter_init (&iter, value);
1847 while (ok && (child = g_variant_iter_next_value (&iter)))
1849 ok = g_settings_schema_key_range_check (key, child);
1850 g_variant_unref (child);
1853 return ok;
1856 if (key->minimum)
1858 return g_variant_compare (key->minimum, value) <= 0 &&
1859 g_variant_compare (value, key->maximum) <= 0;
1862 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1863 g_variant_get_string (value, NULL));