2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2011 Canonical Limited
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "glib-private.h"
24 #include "gsettingsschema-internal.h"
25 #include "gsettings.h"
27 #include "gvdb/gvdb-reader.h"
36 /* Needed on macOS and FreeBSD for uselocale() */
43 * The [struct@Gio.SettingsSchemaSource] and `GSettingsSchema` APIs provide a
44 * mechanism for advanced control over the loading of schemas and a
45 * mechanism for introspecting their content.
47 * Plugin loading systems that wish to provide plugins a way to access
48 * settings face the problem of how to make the schemas for these
49 * settings visible to GSettings. Typically, a plugin will want to ship
50 * the schema along with itself and it won't be installed into the
51 * standard system directories for schemas.
53 * [struct@Gio.SettingsSchemaSource] provides a mechanism for dealing with this
54 * by allowing the creation of a new ‘schema source’ from which schemas can
55 * be acquired. This schema source can then become part of the metadata
56 * associated with the plugin and queried whenever the plugin requires
57 * access to some settings.
59 * Consider the following example:
65 * GSettingsSchemaSource *schema_source;
70 * initialise_plugin (const gchar *dir)
76 * plugin->schema_source =
77 * g_settings_schema_source_new_from_directory (dir,
78 * g_settings_schema_source_get_default (), FALSE, NULL);
88 * plugin_get_settings (Plugin *plugin,
89 * const gchar *schema_id)
91 * GSettingsSchema *schema;
93 * if (schema_id == NULL)
94 * schema_id = plugin->identifier;
96 * schema = g_settings_schema_source_lookup (plugin->schema_source,
101 * … disable the plugin or abort, etc …
104 * return g_settings_new_full (schema, NULL, NULL);
108 * The code above shows how hooks should be added to the code that
109 * initialises (or enables) the plugin to create the schema source and
110 * how an API can be added to the plugin system to provide a convenient
111 * way for the plugin to access its settings, using the schemas that it
114 * From the standpoint of the plugin, it would need to ensure that it
115 * ships a gschemas.compiled file as part of itself, and then simply do
120 * GSettings *settings;
123 * settings = plugin_get_settings (self, NULL);
124 * some_value = g_settings_get_int (settings, "some-value");
129 * It's also possible that the plugin system expects the schema source
130 * files (ie: `.gschema.xml` files) instead of a `gschemas.compiled` file.
131 * In that case, the plugin loading system must compile the schemas for
132 * itself before attempting to create the settings source.
138 * GSettingsSchemaKey:
140 * #GSettingsSchemaKey is an opaque data structure and can only be accessed
141 * using the following functions.
144 struct _GSettingsSchema
146 GSettingsSchemaSource *source;
147 const gchar *gettext_domain;
154 GSettingsSchema *extends;
160 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
162 * A boxed #GType corresponding to #GSettingsSchemaSource.
166 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
169 * G_TYPE_SETTINGS_SCHEMA:
171 * A boxed #GType corresponding to #GSettingsSchema.
175 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
178 * GSettingsSchemaSource:
180 * This is an opaque structure type. You may not access it directly.
184 struct _GSettingsSchemaSource
186 GSettingsSchemaSource *parent;
189 GHashTable **text_tables;
194 static GSettingsSchemaSource *schema_sources;
197 * g_settings_schema_source_ref:
198 * @source: a #GSettingsSchemaSource
200 * Increase the reference count of @source, returning a new reference.
202 * Returns: (transfer full) (not nullable): a new reference to @source
206 GSettingsSchemaSource *
207 g_settings_schema_source_ref (GSettingsSchemaSource *source)
209 g_atomic_int_inc (&source->ref_count);
215 * g_settings_schema_source_unref:
216 * @source: a #GSettingsSchemaSource
218 * Decrease the reference count of @source, possibly freeing it.
223 g_settings_schema_source_unref (GSettingsSchemaSource *source)
225 if (g_atomic_int_dec_and_test (&source->ref_count))
227 if (source == schema_sources)
228 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
231 g_settings_schema_source_unref (source->parent);
232 gvdb_table_free (source->table);
233 g_free (source->directory);
235 if (source->text_tables)
237 g_hash_table_unref (source->text_tables[0]);
238 g_hash_table_unref (source->text_tables[1]);
239 g_free (source->text_tables);
242 g_slice_free (GSettingsSchemaSource, source);
247 * g_settings_schema_source_new_from_directory:
248 * @directory: (type filename): the filename of a directory
249 * @parent: (nullable): a #GSettingsSchemaSource, or %NULL
250 * @trusted: %TRUE, if the directory is trusted
251 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
253 * Attempts to create a new schema source corresponding to the contents
254 * of the given directory.
256 * This function is not required for normal uses of #GSettings but it
257 * may be useful to authors of plugin management systems.
259 * The directory should contain a file called `gschemas.compiled` as
260 * produced by the [glib-compile-schemas][glib-compile-schemas] tool.
262 * If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
263 * corrupted. This assumption has a performance advantage, but can result
264 * in crashes or inconsistent behaviour in the case of a corrupted file.
265 * Generally, you should set @trusted to %TRUE for files installed by the
266 * system and to %FALSE for files in the home directory.
268 * In either case, an empty file or some types of corruption in the file will
269 * result in %G_FILE_ERROR_INVAL being returned.
271 * If @parent is non-%NULL then there are two effects.
273 * First, if g_settings_schema_source_lookup() is called with the
274 * @recursive flag set to %TRUE and the schema can not be found in the
275 * source, the lookup will recurse to the parent.
277 * Second, any references to other schemas specified within this
278 * source (ie: `child` or `extends`) references may be resolved
281 * For this second reason, except in very unusual situations, the
282 * @parent should probably be given as the default schema source, as
283 * returned by g_settings_schema_source_get_default().
287 GSettingsSchemaSource *
288 g_settings_schema_source_new_from_directory (const gchar *directory,
289 GSettingsSchemaSource *parent,
293 GSettingsSchemaSource *source;
297 filename = g_build_filename (directory, "gschemas.compiled", NULL);
298 table = gvdb_table_new (filename, trusted, error);
304 source = g_slice_new (GSettingsSchemaSource);
305 source->directory = g_strdup (directory);
306 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
307 source->text_tables = NULL;
308 source->table = table;
309 source->ref_count = 1;
315 try_prepend_dir (const gchar *directory)
317 GSettingsSchemaSource *source;
319 source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
321 /* If we successfully created it then prepend it to the global list */
323 schema_sources = source;
327 try_prepend_data_dir (const gchar *directory)
329 gchar *dirname = g_build_filename (directory, "glib-2.0", "schemas", NULL);
330 try_prepend_dir (dirname);
335 initialise_schema_sources (void)
337 static gsize initialised;
339 /* need a separate variable because 'schema_sources' may legitimately
340 * be null if we have zero valid schema sources
342 if G_UNLIKELY (g_once_init_enter (&initialised))
344 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
345 const gchar * const *dirs;
347 gchar **extra_schema_dirs;
350 /* iterate in reverse: count up, then count down */
351 dirs = g_get_system_data_dirs ();
352 for (i = 0; dirs[i]; i++);
355 try_prepend_data_dir (dirs[i]);
357 try_prepend_data_dir (g_get_user_data_dir ());
359 /* Disallow loading extra schemas if running as setuid, as that could
360 * allow reading privileged files. */
361 if (!is_setuid && (path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
363 extra_schema_dirs = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 0);
364 for (i = 0; extra_schema_dirs[i]; i++);
367 try_prepend_dir (extra_schema_dirs[i]);
369 g_strfreev (extra_schema_dirs);
372 g_once_init_leave (&initialised, TRUE);
377 * g_settings_schema_source_get_default:
379 * Gets the default system schema source.
381 * This function is not required for normal uses of #GSettings but it
382 * may be useful to authors of plugin management systems or to those who
383 * want to introspect the content of schemas.
385 * If no schemas are installed, %NULL will be returned.
387 * The returned source may actually consist of multiple schema sources
388 * from different directories, depending on which directories were given
389 * in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
390 * lookups performed against the default source should probably be done
393 * Returns: (transfer none) (nullable): the default schema source
397 GSettingsSchemaSource *
398 g_settings_schema_source_get_default (void)
400 initialise_schema_sources ();
402 return schema_sources;
406 * g_settings_schema_source_lookup:
407 * @source: a #GSettingsSchemaSource
408 * @schema_id: a schema ID
409 * @recursive: %TRUE if the lookup should be recursive
411 * Looks up a schema with the identifier @schema_id in @source.
413 * This function is not required for normal uses of #GSettings but it
414 * may be useful to authors of plugin management systems or to those who
415 * want to introspect the content of schemas.
417 * If the schema isn't found directly in @source and @recursive is %TRUE
418 * then the parent sources will also be checked.
420 * If the schema isn't found, %NULL is returned.
422 * Returns: (nullable) (transfer full): a new #GSettingsSchema
427 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
428 const gchar *schema_id,
431 GSettingsSchema *schema;
433 const gchar *extends;
435 g_return_val_if_fail (source != NULL, NULL);
436 g_return_val_if_fail (schema_id != NULL, NULL);
438 table = gvdb_table_get_table (source->table, schema_id);
440 if (table == NULL && recursive)
441 for (source = source->parent; source; source = source->parent)
442 if ((table = gvdb_table_get_table (source->table, schema_id)))
448 schema = g_slice_new0 (GSettingsSchema);
449 schema->source = g_settings_schema_source_ref (source);
450 schema->ref_count = 1;
451 schema->id = g_strdup (schema_id);
452 schema->table = table;
453 schema->path = g_settings_schema_get_string (schema, ".path");
454 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
456 if (schema->gettext_domain)
457 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
459 extends = g_settings_schema_get_string (schema, ".extends");
462 schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
463 if (schema->extends == NULL)
464 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
472 GHashTable *summaries;
473 GHashTable *descriptions;
474 GSList *gettext_domain;
478 } TextTableParseInfo;
481 get_attribute_value (GSList *list)
485 for (node = list; node; node = node->next)
493 pop_attribute_value (GSList **list)
498 *list = g_slist_remove (*list, top);
504 push_attribute_value (GSList **list,
507 *list = g_slist_prepend (*list, g_strdup (value));
511 start_element (GMarkupParseContext *context,
512 const gchar *element_name,
513 const gchar **attribute_names,
514 const gchar **attribute_values,
518 TextTableParseInfo *info = user_data;
519 const gchar *gettext_domain = NULL;
520 const gchar *schema_id = NULL;
521 const gchar *key_name = NULL;
524 for (i = 0; attribute_names[i]; i++)
526 if (g_str_equal (attribute_names[i], "gettext-domain"))
527 gettext_domain = attribute_values[i];
528 else if (g_str_equal (attribute_names[i], "id"))
529 schema_id = attribute_values[i];
530 else if (g_str_equal (attribute_names[i], "name"))
531 key_name = attribute_values[i];
534 push_attribute_value (&info->gettext_domain, gettext_domain);
535 push_attribute_value (&info->schema_id, schema_id);
536 push_attribute_value (&info->key_name, key_name);
540 g_string_free (info->string, TRUE);
544 if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
545 info->string = g_string_new (NULL);
549 normalise_whitespace (const gchar *orig)
551 /* We normalise by the same rules as in intltool:
560 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
562 * Where \s is an ascii space character.
564 * We aim for ease of implementation over efficiency -- this code is
565 * not run in normal applications.
567 static GRegex *cleanup[3];
568 static GRegex *splitter;
573 if (g_once_init_enter_pointer (&splitter))
577 cleanup[0] = g_regex_new ("^\\s+", G_REGEX_DEFAULT,
578 G_REGEX_MATCH_DEFAULT, NULL);
579 cleanup[1] = g_regex_new ("\\s+$", G_REGEX_DEFAULT,
580 G_REGEX_MATCH_DEFAULT, NULL);
581 cleanup[2] = g_regex_new ("\\s+", G_REGEX_DEFAULT,
582 G_REGEX_MATCH_DEFAULT, NULL);
583 s = g_regex_new ("\\n\\s*\\n+", G_REGEX_DEFAULT,
584 G_REGEX_MATCH_DEFAULT, NULL);
586 g_once_init_leave_pointer (&splitter, s);
589 lines = g_regex_split (splitter, orig, 0);
590 for (i = 0; lines[i]; i++)
594 a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
595 b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
596 c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
603 result = g_strjoinv ("\n\n", lines);
610 end_element (GMarkupParseContext *context,
611 const gchar *element_name,
615 TextTableParseInfo *info = user_data;
617 pop_attribute_value (&info->gettext_domain);
618 pop_attribute_value (&info->schema_id);
619 pop_attribute_value (&info->key_name);
623 GHashTable *source_table = NULL;
624 const gchar *gettext_domain;
625 const gchar *schema_id;
626 const gchar *key_name;
628 gettext_domain = get_attribute_value (info->gettext_domain);
629 schema_id = get_attribute_value (info->schema_id);
630 key_name = get_attribute_value (info->key_name);
632 if (g_str_equal (element_name, "summary"))
633 source_table = info->summaries;
634 else if (g_str_equal (element_name, "description"))
635 source_table = info->descriptions;
637 if (source_table && schema_id && key_name)
639 GHashTable *schema_table;
642 schema_table = g_hash_table_lookup (source_table, schema_id);
644 if (schema_table == NULL)
646 schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
647 g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
650 normalised = normalise_whitespace (info->string->str);
652 if (gettext_domain && normalised[0])
656 translated = g_strdup (g_dgettext (gettext_domain, normalised));
658 normalised = translated;
661 g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
664 g_string_free (info->string, TRUE);
670 text (GMarkupParseContext *context,
676 TextTableParseInfo *info = user_data;
679 g_string_append_len (info->string, text, text_len);
683 parse_into_text_tables (const gchar *directory,
684 GHashTable *summaries,
685 GHashTable *descriptions)
687 GMarkupParser parser = { start_element, end_element, text, NULL, NULL };
688 TextTableParseInfo info = { summaries, descriptions, NULL, NULL, NULL, NULL };
689 const gchar *basename;
692 dir = g_dir_open (directory, 0, NULL);
693 while ((basename = g_dir_read_name (dir)))
699 filename = g_build_filename (directory, basename, NULL);
700 if (g_file_get_contents (filename, &contents, &size, NULL))
702 GMarkupParseContext *context;
704 context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
705 /* Ignore errors here, this is best effort only. */
706 if (g_markup_parse_context_parse (context, contents, size, NULL))
707 (void) g_markup_parse_context_end_parse (context, NULL);
708 g_markup_parse_context_free (context);
710 /* Clean up dangling stuff in case there was an error. */
711 g_slist_free_full (info.gettext_domain, g_free);
712 g_slist_free_full (info.schema_id, g_free);
713 g_slist_free_full (info.key_name, g_free);
715 info.gettext_domain = NULL;
716 info.schema_id = NULL;
717 info.key_name = NULL;
721 g_string_free (info.string, TRUE);
735 g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
737 if (g_once_init_enter_pointer (&source->text_tables))
739 GHashTable **text_tables;
741 text_tables = g_new (GHashTable *, 2);
742 text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
743 text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
745 if (source->directory)
746 parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
748 g_once_init_leave_pointer (&source->text_tables, text_tables);
751 return source->text_tables;
755 * g_settings_schema_source_list_schemas:
756 * @source: a #GSettingsSchemaSource
757 * @recursive: if we should recurse
758 * @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
759 * list of non-relocatable schemas, in no defined order
760 * @relocatable: (out) (transfer full) (array zero-terminated=1): the list
761 * of relocatable schemas, in no defined order
763 * Lists the schemas in a given source.
765 * If @recursive is %TRUE then include parent sources. If %FALSE then
766 * only include the schemas from one source (ie: one directory). You
767 * probably want %TRUE.
769 * Non-relocatable schemas are those for which you can call
770 * g_settings_new(). Relocatable schemas are those for which you must
771 * use g_settings_new_with_path().
773 * Do not call this function from normal programs. This is designed for
774 * use by database editors, commandline tools, etc.
779 g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
781 gchar ***non_relocatable,
782 gchar ***relocatable)
784 GHashTable *single, *reloc;
785 GSettingsSchemaSource *s;
787 /* We use hash tables to avoid duplicate listings for schemas that
788 * appear in more than one file.
790 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
791 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
793 for (s = source; s; s = s->parent)
798 list = gvdb_table_list (s->table, "");
800 /* empty schema cache file? */
804 for (i = 0; list[i]; i++)
806 if (!g_hash_table_contains (single, list[i]) &&
807 !g_hash_table_contains (reloc, list[i]))
812 schema = g_strdup (list[i]);
814 table = gvdb_table_get_table (s->table, list[i]);
815 g_assert (table != NULL);
817 if (gvdb_table_has_value (table, ".path"))
818 g_hash_table_add (single, schema);
820 g_hash_table_add (reloc, schema);
822 gvdb_table_free (table);
828 /* Only the first source if recursive not requested */
835 *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
836 g_hash_table_steal_all (single);
841 *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
842 g_hash_table_steal_all (reloc);
845 g_hash_table_unref (single);
846 g_hash_table_unref (reloc);
849 static gchar **non_relocatable_schema_list;
850 static gchar **relocatable_schema_list;
851 static gsize schema_lists_initialised;
854 ensure_schema_lists (void)
856 if (g_once_init_enter (&schema_lists_initialised))
858 initialise_schema_sources ();
860 g_settings_schema_source_list_schemas (schema_sources, TRUE,
861 &non_relocatable_schema_list,
862 &relocatable_schema_list);
864 g_once_init_leave (&schema_lists_initialised, TRUE);
869 * g_settings_list_schemas:
873 * Returns: (element-type utf8) (transfer none) (not nullable): a list of
874 * #GSettings schemas that are available, in no defined order. The list
875 * must not be modified or freed.
879 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead.
880 * If you used g_settings_list_schemas() to check for the presence of
881 * a particular schema, use g_settings_schema_source_lookup() instead
882 * of your whole loop.
884 const gchar * const *
885 g_settings_list_schemas (void)
887 ensure_schema_lists ();
889 return (const gchar **) non_relocatable_schema_list;
893 * g_settings_list_relocatable_schemas:
897 * Returns: (element-type utf8) (transfer none) (not nullable): a list of
898 * relocatable #GSettings schemas that are available, in no defined order.
899 * The list must not be modified or freed.
903 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead
905 const gchar * const *
906 g_settings_list_relocatable_schemas (void)
908 ensure_schema_lists ();
910 return (const gchar **) relocatable_schema_list;
914 * g_settings_schema_ref:
915 * @schema: a #GSettingsSchema
917 * Increase the reference count of @schema, returning a new reference.
919 * Returns: (transfer full) (not nullable): a new reference to @schema
924 g_settings_schema_ref (GSettingsSchema *schema)
926 g_atomic_int_inc (&schema->ref_count);
932 * g_settings_schema_unref:
933 * @schema: a #GSettingsSchema
935 * Decrease the reference count of @schema, possibly freeing it.
940 g_settings_schema_unref (GSettingsSchema *schema)
942 if (g_atomic_int_dec_and_test (&schema->ref_count))
945 g_settings_schema_unref (schema->extends);
947 g_settings_schema_source_unref (schema->source);
948 gvdb_table_free (schema->table);
949 g_free (schema->items);
952 g_slice_free (GSettingsSchema, schema);
957 g_settings_schema_get_string (GSettingsSchema *schema,
960 const gchar *result = NULL;
963 if ((value = gvdb_table_get_raw_value (schema->table, key)))
965 result = g_variant_get_string (value, NULL);
966 g_variant_unref (value);
973 g_settings_schema_get_child_schema (GSettingsSchema *schema,
976 const gchar *child_id;
979 child_name = g_strconcat (name, "/", NULL);
980 child_id = g_settings_schema_get_string (schema, child_name);
984 if (child_id == NULL)
987 return g_settings_schema_source_lookup (schema->source, child_id, TRUE);
991 g_settings_schema_get_value (GSettingsSchema *schema,
994 GSettingsSchema *s = schema;
996 GVariant *value = NULL;
998 g_return_val_if_fail (schema != NULL, NULL);
1000 for (s = schema; s; s = s->extends)
1001 if ((value = gvdb_table_get_raw_value (s->table, key)))
1004 if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
1005 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
1007 iter = g_variant_iter_new (value);
1008 g_variant_unref (value);
1014 * g_settings_schema_get_path:
1015 * @schema: a #GSettingsSchema
1017 * Gets the path associated with @schema, or %NULL.
1019 * Schemas may be single-instance or relocatable. Single-instance
1020 * schemas correspond to exactly one set of keys in the backend
1021 * database: those located at the path returned by this function.
1023 * Relocatable schemas can be referenced by other schemas and can
1024 * therefore describe multiple sets of keys at different locations. For
1025 * relocatable schemas, this function will return %NULL.
1027 * Returns: (nullable) (transfer none): the path of the schema, or %NULL
1032 g_settings_schema_get_path (GSettingsSchema *schema)
1034 return schema->path;
1038 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
1040 return schema->gettext_domain;
1044 * g_settings_schema_has_key:
1045 * @schema: a #GSettingsSchema
1046 * @name: the name of a key
1048 * Checks if @schema has a key named @name.
1050 * Returns: %TRUE if such a key exists
1055 g_settings_schema_has_key (GSettingsSchema *schema,
1058 return gvdb_table_has_value (schema->table, key);
1062 * g_settings_schema_list_children:
1063 * @schema: a #GSettingsSchema
1065 * Gets the list of children in @schema.
1067 * You should free the return value with g_strfreev() when you are done
1070 * Returns: (not nullable) (transfer full) (element-type utf8): a list of
1071 * the children on @settings, in no defined order
1076 g_settings_schema_list_children (GSettingsSchema *schema)
1083 g_return_val_if_fail (schema != NULL, NULL);
1085 keys = g_settings_schema_list (schema, &n_keys);
1086 strv = g_new (gchar *, n_keys + 1);
1087 for (i = j = 0; i < n_keys; i++)
1089 const gchar *key = g_quark_to_string (keys[i]);
1091 if (g_str_has_suffix (key, "/"))
1093 gsize length = strlen (key);
1095 strv[j] = g_memdup2 (key, length);
1096 strv[j][length - 1] = '\0';
1106 * g_settings_schema_list_keys:
1107 * @schema: a #GSettingsSchema
1109 * Introspects the list of keys on @schema.
1111 * You should probably not be calling this function from "normal" code
1112 * (since you should already know what keys are in your schema). This
1113 * function is intended for introspection reasons.
1115 * Returns: (not nullable) (transfer full) (element-type utf8): a list
1116 * of the keys on @schema, in no defined order
1121 g_settings_schema_list_keys (GSettingsSchema *schema)
1128 g_return_val_if_fail (schema != NULL, NULL);
1130 keys = g_settings_schema_list (schema, &n_keys);
1131 strv = g_new (gchar *, n_keys + 1);
1132 for (i = j = 0; i < n_keys; i++)
1134 const gchar *key = g_quark_to_string (keys[i]);
1136 if (!g_str_has_suffix (key, "/"))
1137 strv[j++] = g_strdup (key);
1145 g_settings_schema_list (GSettingsSchema *schema,
1148 if (schema->items == NULL)
1151 GHashTableIter iter;
1157 items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1159 for (s = schema; s; s = s->extends)
1163 list = gvdb_table_list (s->table, "");
1167 for (i = 0; list[i]; i++)
1168 g_hash_table_add (items, list[i]); /* transfer ownership */
1170 g_free (list); /* free container only */
1174 /* Do a first pass to eliminate child items that do not map to
1175 * valid schemas (ie: ones that would crash us if we actually
1176 * tried to create them).
1178 g_hash_table_iter_init (&iter, items);
1179 while (g_hash_table_iter_next (&iter, &name, NULL))
1180 if (g_str_has_suffix (name, "/"))
1182 GSettingsSchemaSource *source;
1183 GVariant *child_schema;
1184 GvdbTable *child_table;
1186 child_schema = gvdb_table_get_raw_value (schema->table, name);
1192 for (source = schema->source; source; source = source->parent)
1193 if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1196 g_variant_unref (child_schema);
1198 /* Schema is not found -> remove it from the list */
1199 if (child_table == NULL)
1201 g_hash_table_iter_remove (&iter);
1205 /* Make sure the schema is relocatable or at the
1208 if (gvdb_table_has_value (child_table, ".path"))
1214 path = gvdb_table_get_raw_value (child_table, ".path");
1215 expected = g_strconcat (schema->path, name, NULL);
1216 same = g_str_equal (expected, g_variant_get_string (path, NULL));
1217 g_variant_unref (path);
1220 /* Schema is non-relocatable and did not have the
1221 * expected path -> remove it from the list
1224 g_hash_table_iter_remove (&iter);
1227 gvdb_table_free (child_table);
1230 /* Now create the list */
1231 len = g_hash_table_size (items);
1232 schema->items = g_new (GQuark, len);
1234 g_hash_table_iter_init (&iter, items);
1236 while (g_hash_table_iter_next (&iter, &name, NULL))
1237 schema->items[i++] = g_quark_from_string (name);
1238 schema->n_items = i;
1239 g_assert (i == len);
1241 g_hash_table_unref (items);
1244 *n_items = schema->n_items;
1245 return schema->items;
1249 * g_settings_schema_get_id:
1250 * @schema: a #GSettingsSchema
1252 * Get the ID of @schema.
1254 * Returns: (not nullable) (transfer none): the ID
1257 g_settings_schema_get_id (GSettingsSchema *schema)
1263 endian_fixup (GVariant **value)
1265 #if G_BYTE_ORDER == G_BIG_ENDIAN
1268 tmp = g_variant_byteswap (*value);
1269 g_variant_unref (*value);
1275 g_settings_schema_key_init (GSettingsSchemaKey *key,
1276 GSettingsSchema *schema,
1283 memset (key, 0, sizeof *key);
1285 iter = g_settings_schema_get_value (schema, name);
1287 key->schema = g_settings_schema_ref (schema);
1288 key->default_value = g_variant_iter_next_value (iter);
1289 endian_fixup (&key->default_value);
1290 key->type = g_variant_get_type (key->default_value);
1291 key->name = g_intern_string (name);
1293 while (g_variant_iter_next (iter, "(y*)", &code, &data))
1298 /* translation requested */
1299 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1303 /* enumerated types... */
1304 key->is_enum = TRUE;
1309 key->is_flags = TRUE;
1313 /* ..., choices, aliases */
1314 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1318 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1319 endian_fixup (&key->minimum);
1320 endian_fixup (&key->maximum);
1324 g_variant_get (data, "@a{sv}", &key->desktop_overrides);
1325 endian_fixup (&key->desktop_overrides);
1329 g_warning ("unknown schema extension '%c'", code);
1333 g_variant_unref (data);
1336 g_variant_iter_free (iter);
1340 g_settings_schema_key_clear (GSettingsSchemaKey *key)
1343 g_variant_unref (key->minimum);
1346 g_variant_unref (key->maximum);
1348 if (key->desktop_overrides)
1349 g_variant_unref (key->desktop_overrides);
1351 g_variant_unref (key->default_value);
1353 g_settings_schema_unref (key->schema);
1357 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1360 g_return_val_if_fail (value != NULL, FALSE);
1362 return g_variant_is_of_type (value, key->type);
1366 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1369 const gchar *target;
1371 if (g_settings_schema_key_range_check (key, value))
1372 return g_variant_ref (value);
1374 if (key->strinfo == NULL)
1377 if (g_variant_is_container (value))
1379 GVariantBuilder builder;
1383 g_variant_iter_init (&iter, value);
1384 g_variant_builder_init (&builder, g_variant_get_type (value));
1386 while ((child = g_variant_iter_next_value (&iter)))
1390 fixed = g_settings_schema_key_range_fixup (key, child);
1391 g_variant_unref (child);
1395 g_variant_builder_clear (&builder);
1399 g_variant_builder_add_value (&builder, fixed);
1400 g_variant_unref (fixed);
1403 return g_variant_ref_sink (g_variant_builder_end (&builder));
1406 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1407 g_variant_get_string (value, NULL));
1408 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1412 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1414 const gchar *translated = NULL;
1415 GError *error = NULL;
1416 const gchar *domain;
1417 #ifdef HAVE_USELOCALE
1418 const gchar *lc_time;
1419 locale_t old_locale;
1424 domain = g_settings_schema_get_gettext_domain (key->schema);
1426 if (key->lc_char == '\0')
1427 /* translation not requested for this key */
1430 #ifdef HAVE_USELOCALE
1431 if (key->lc_char == 't')
1433 lc_time = setlocale (LC_TIME, NULL);
1436 locale = newlocale (LC_MESSAGES_MASK, lc_time, (locale_t) 0);
1437 if (locale != (locale_t) 0)
1439 old_locale = uselocale (locale);
1440 translated = g_dgettext (domain, key->unparsed);
1441 uselocale (old_locale);
1442 freelocale (locale);
1448 if (translated == NULL)
1449 translated = g_dgettext (domain, key->unparsed);
1451 if (translated == key->unparsed)
1452 /* the default value was not translated */
1455 /* try to parse the translation of the unparsed default */
1456 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1460 g_warning ("Failed to parse translated string '%s' for "
1461 "key '%s' in schema '%s': %s", translated, key->name,
1462 g_settings_schema_get_id (key->schema), error->message);
1463 g_warning ("Using untranslated default instead.");
1464 g_error_free (error);
1467 else if (!g_settings_schema_key_range_check (key, value))
1469 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1470 "is outside of valid range", key->unparsed, key->name,
1471 g_settings_schema_get_id (key->schema));
1472 g_variant_unref (value);
1480 g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
1482 static const gchar * const *current_desktops;
1483 GVariant *value = NULL;
1486 if (!key->desktop_overrides)
1489 if (g_once_init_enter_pointer (¤t_desktops))
1491 const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
1494 if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
1495 tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
1497 tmp = g_new0 (gchar *, 0 + 1);
1499 g_once_init_leave_pointer (¤t_desktops, (const gchar **) tmp);
1502 for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
1503 value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
1509 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1512 gboolean it_worked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
1515 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1516 g_variant_get_string (value, NULL),
1519 /* 'value' can only come from the backend after being filtered for validity,
1520 * from the translation after being filtered for validity, or from the schema
1521 * itself (which the schema compiler checks for validity). If this assertion
1522 * fails then it's really a bug in GSettings or the schema compiler...
1524 g_assert (it_worked);
1529 /* Returns a new floating #GVariant. */
1531 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1534 const gchar *string;
1536 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1541 return g_variant_new_string (string);
1545 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1553 g_variant_iter_init (&iter, value);
1554 while (g_variant_iter_next (&iter, "&s", &flag))
1556 gboolean it_worked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
1559 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1560 /* as in g_settings_to_enum() */
1561 g_assert (it_worked);
1563 result |= flag_value;
1569 /* Returns a new floating #GVariant. */
1571 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1574 GVariantBuilder builder;
1577 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1579 for (i = 0; i < 32; i++)
1580 if (value & (1u << i))
1582 const gchar *string;
1584 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1588 g_variant_builder_clear (&builder);
1592 g_variant_builder_add (&builder, "s", string);
1595 return g_variant_builder_end (&builder);
1598 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1601 * g_settings_schema_key_ref:
1602 * @key: a #GSettingsSchemaKey
1604 * Increase the reference count of @key, returning a new reference.
1606 * Returns: (not nullable) (transfer full): a new reference to @key
1610 GSettingsSchemaKey *
1611 g_settings_schema_key_ref (GSettingsSchemaKey *key)
1613 g_return_val_if_fail (key != NULL, NULL);
1615 g_atomic_int_inc (&key->ref_count);
1621 * g_settings_schema_key_unref:
1622 * @key: a #GSettingsSchemaKey
1624 * Decrease the reference count of @key, possibly freeing it.
1629 g_settings_schema_key_unref (GSettingsSchemaKey *key)
1631 g_return_if_fail (key != NULL);
1633 if (g_atomic_int_dec_and_test (&key->ref_count))
1635 g_settings_schema_key_clear (key);
1637 g_slice_free (GSettingsSchemaKey, key);
1642 * g_settings_schema_get_key:
1643 * @schema: a #GSettingsSchema
1644 * @name: the name of a key
1646 * Gets the key named @name from @schema.
1648 * It is a programmer error to request a key that does not exist. See
1649 * g_settings_schema_list_keys().
1651 * Returns: (not nullable) (transfer full): the #GSettingsSchemaKey for @name
1655 GSettingsSchemaKey *
1656 g_settings_schema_get_key (GSettingsSchema *schema,
1659 GSettingsSchemaKey *key;
1661 g_return_val_if_fail (schema != NULL, NULL);
1662 g_return_val_if_fail (name != NULL, NULL);
1664 key = g_slice_new (GSettingsSchemaKey);
1665 g_settings_schema_key_init (key, schema, name);
1672 * g_settings_schema_key_get_name:
1673 * @key: a #GSettingsSchemaKey
1675 * Gets the name of @key.
1677 * Returns: (not nullable) (transfer none): the name of @key.
1682 g_settings_schema_key_get_name (GSettingsSchemaKey *key)
1684 g_return_val_if_fail (key != NULL, NULL);
1690 * g_settings_schema_key_get_summary:
1691 * @key: a #GSettingsSchemaKey
1693 * Gets the summary for @key.
1695 * If no summary has been provided in the schema for @key, returns
1698 * The summary is a short description of the purpose of the key; usually
1699 * one short sentence. Summaries can be translated and the value
1700 * returned from this function is is the current locale.
1702 * This function is slow. The summary and description information for
1703 * the schemas is not stored in the compiled schema database so this
1704 * function has to parse all of the source XML files in the schema
1707 * Returns: (nullable) (transfer none): the summary for @key, or %NULL
1712 g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1714 GHashTable **text_tables;
1715 GHashTable *summaries;
1717 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1718 summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1720 return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1724 * g_settings_schema_key_get_description:
1725 * @key: a #GSettingsSchemaKey
1727 * Gets the description for @key.
1729 * If no description has been provided in the schema for @key, returns
1732 * The description can be one sentence to several paragraphs in length.
1733 * Paragraphs are delimited with a double newline. Descriptions can be
1734 * translated and the value returned from this function is is the
1737 * This function is slow. The summary and description information for
1738 * the schemas is not stored in the compiled schema database so this
1739 * function has to parse all of the source XML files in the schema
1742 * Returns: (nullable) (transfer none): the description for @key, or %NULL
1747 g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1749 GHashTable **text_tables;
1750 GHashTable *descriptions;
1752 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1753 descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1755 return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1759 * g_settings_schema_key_get_value_type:
1760 * @key: a #GSettingsSchemaKey
1762 * Gets the #GVariantType of @key.
1764 * Returns: (not nullable) (transfer none): the type of @key
1768 const GVariantType *
1769 g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1771 g_return_val_if_fail (key, NULL);
1777 * g_settings_schema_key_get_default_value:
1778 * @key: a #GSettingsSchemaKey
1780 * Gets the default value for @key.
1782 * Note that this is the default value according to the schema. System
1783 * administrator defaults and lockdown are not visible via this API.
1785 * Returns: (not nullable) (transfer full): the default value for the key
1790 g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1794 g_return_val_if_fail (key, NULL);
1796 value = g_settings_schema_key_get_translated_default (key);
1799 value = g_settings_schema_key_get_per_desktop_default (key);
1802 value = g_variant_ref (key->default_value);
1808 * g_settings_schema_key_get_range:
1809 * @key: a #GSettingsSchemaKey
1811 * Queries the range of a key.
1813 * This function will return a #GVariant that fully describes the range
1814 * of values that are valid for @key.
1816 * The type of #GVariant returned is `(sv)`. The string describes
1817 * the type of range restriction in effect. The type and meaning of
1818 * the value contained in the variant depends on the string.
1820 * If the string is `'type'` then the variant contains an empty array.
1821 * The element type of that empty array is the expected type of value
1822 * and all values of that type are valid.
1824 * If the string is `'enum'` then the variant contains an array
1825 * enumerating the possible values. Each item in the array is
1826 * a possible valid value and no other values are valid.
1828 * If the string is `'flags'` then the variant contains an array. Each
1829 * item in the array is a value that may appear zero or one times in an
1830 * array to be used as the value for this key. For example, if the
1831 * variant contained the array `['x', 'y']` then the valid values for
1832 * the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
1835 * Finally, if the string is `'range'` then the variant contains a pair
1836 * of like-typed values -- the minimum and maximum permissible values
1839 * This information should not be used by normal programs. It is
1840 * considered to be a hint for introspection purposes. Normal programs
1841 * should already know what is permitted by their own schema. The
1842 * format may change in any way in the future -- but particularly, new
1843 * forms may be added to the possibilities described above.
1845 * You should free the returned value with g_variant_unref() when it is
1848 * Returns: (not nullable) (transfer full): a #GVariant describing the range
1853 g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1860 range = g_variant_new ("(**)", key->minimum, key->maximum);
1863 else if (key->strinfo)
1865 range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1866 type = key->is_flags ? "flags" : "enum";
1870 range = g_variant_new_array (key->type, NULL, 0);
1874 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1878 * g_settings_schema_key_range_check:
1879 * @key: a #GSettingsSchemaKey
1880 * @value: the value to check
1882 * Checks if the given @value is within the
1883 * permitted range for @key.
1885 * It is a programmer error if @value is not of the correct type — you
1886 * must check for this first.
1888 * Returns: %TRUE if @value is valid for @key
1893 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1896 if (key->minimum == NULL && key->strinfo == NULL)
1899 if (g_variant_is_container (value))
1905 g_variant_iter_init (&iter, value);
1906 while (ok && (child = g_variant_iter_next_value (&iter)))
1908 ok = g_settings_schema_key_range_check (key, child);
1909 g_variant_unref (child);
1917 return g_variant_compare (key->minimum, value) <= 0 &&
1918 g_variant_compare (value, key->maximum) <= 0;
1921 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1922 g_variant_get_string (value, NULL));