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() */
41 * SECTION:gsettingsschema
42 * @short_description: Introspecting and controlling the loading
43 * of GSettings schemas
46 * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
47 * mechanism for advanced control over the loading of schemas and a
48 * mechanism for introspecting their content.
50 * Plugin loading systems that wish to provide plugins a way to access
51 * settings face the problem of how to make the schemas for these
52 * settings visible to GSettings. Typically, a plugin will want to ship
53 * the schema along with itself and it won't be installed into the
54 * standard system directories for schemas.
56 * #GSettingsSchemaSource provides a mechanism for dealing with this by
57 * allowing the creation of a new 'schema source' from which schemas can
58 * be acquired. This schema source can then become part of the metadata
59 * associated with the plugin and queried whenever the plugin requires
60 * access to some settings.
62 * Consider the following example:
64 * |[<!-- language="C" -->
68 * GSettingsSchemaSource *schema_source;
73 * initialise_plugin (const gchar *dir)
79 * plugin->schema_source =
80 * g_settings_schema_source_new_from_directory (dir,
81 * g_settings_schema_source_get_default (), FALSE, NULL);
91 * plugin_get_settings (Plugin *plugin,
92 * const gchar *schema_id)
94 * GSettingsSchema *schema;
96 * if (schema_id == NULL)
97 * schema_id = plugin->identifier;
99 * schema = g_settings_schema_source_lookup (plugin->schema_source,
102 * if (schema == NULL)
104 * ... disable the plugin or abort, etc ...
107 * return g_settings_new_full (schema, NULL, NULL);
111 * The code above shows how hooks should be added to the code that
112 * initialises (or enables) the plugin to create the schema source and
113 * how an API can be added to the plugin system to provide a convenient
114 * way for the plugin to access its settings, using the schemas that it
117 * From the standpoint of the plugin, it would need to ensure that it
118 * ships a gschemas.compiled file as part of itself, and then simply do
121 * |[<!-- language="C" -->
123 * GSettings *settings;
126 * settings = plugin_get_settings (self, NULL);
127 * some_value = g_settings_get_int (settings, "some-value");
132 * It's also possible that the plugin system expects the schema source
133 * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
134 * In that case, the plugin loading system must compile the schemas for
135 * itself before attempting to create the settings source.
141 * GSettingsSchemaKey:
143 * #GSettingsSchemaKey is an opaque data structure and can only be accessed
144 * using the following functions.
150 * This is an opaque structure type. You may not access it directly.
154 struct _GSettingsSchema
156 GSettingsSchemaSource *source;
157 const gchar *gettext_domain;
164 GSettingsSchema *extends;
170 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
172 * A boxed #GType corresponding to #GSettingsSchemaSource.
176 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
179 * G_TYPE_SETTINGS_SCHEMA:
181 * A boxed #GType corresponding to #GSettingsSchema.
185 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
188 * GSettingsSchemaSource:
190 * This is an opaque structure type. You may not access it directly.
194 struct _GSettingsSchemaSource
196 GSettingsSchemaSource *parent;
199 GHashTable **text_tables;
204 static GSettingsSchemaSource *schema_sources;
207 * g_settings_schema_source_ref:
208 * @source: a #GSettingsSchemaSource
210 * Increase the reference count of @source, returning a new reference.
212 * Returns: (transfer full) (not nullable): a new reference to @source
216 GSettingsSchemaSource *
217 g_settings_schema_source_ref (GSettingsSchemaSource *source)
219 g_atomic_int_inc (&source->ref_count);
225 * g_settings_schema_source_unref:
226 * @source: a #GSettingsSchemaSource
228 * Decrease the reference count of @source, possibly freeing it.
233 g_settings_schema_source_unref (GSettingsSchemaSource *source)
235 if (g_atomic_int_dec_and_test (&source->ref_count))
237 if (source == schema_sources)
238 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
241 g_settings_schema_source_unref (source->parent);
242 gvdb_table_free (source->table);
243 g_free (source->directory);
245 if (source->text_tables)
247 g_hash_table_unref (source->text_tables[0]);
248 g_hash_table_unref (source->text_tables[1]);
249 g_free (source->text_tables);
252 g_slice_free (GSettingsSchemaSource, source);
257 * g_settings_schema_source_new_from_directory:
258 * @directory: (type filename): the filename of a directory
259 * @parent: (nullable): a #GSettingsSchemaSource, or %NULL
260 * @trusted: %TRUE, if the directory is trusted
261 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
263 * Attempts to create a new schema source corresponding to the contents
264 * of the given directory.
266 * This function is not required for normal uses of #GSettings but it
267 * may be useful to authors of plugin management systems.
269 * The directory should contain a file called `gschemas.compiled` as
270 * produced by the [glib-compile-schemas][glib-compile-schemas] tool.
272 * If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
273 * corrupted. This assumption has a performance advantage, but can result
274 * in crashes or inconsistent behaviour in the case of a corrupted file.
275 * Generally, you should set @trusted to %TRUE for files installed by the
276 * system and to %FALSE for files in the home directory.
278 * In either case, an empty file or some types of corruption in the file will
279 * result in %G_FILE_ERROR_INVAL being returned.
281 * If @parent is non-%NULL then there are two effects.
283 * First, if g_settings_schema_source_lookup() is called with the
284 * @recursive flag set to %TRUE and the schema can not be found in the
285 * source, the lookup will recurse to the parent.
287 * Second, any references to other schemas specified within this
288 * source (ie: `child` or `extends`) references may be resolved
291 * For this second reason, except in very unusual situations, the
292 * @parent should probably be given as the default schema source, as
293 * returned by g_settings_schema_source_get_default().
297 GSettingsSchemaSource *
298 g_settings_schema_source_new_from_directory (const gchar *directory,
299 GSettingsSchemaSource *parent,
303 GSettingsSchemaSource *source;
307 filename = g_build_filename (directory, "gschemas.compiled", NULL);
308 table = gvdb_table_new (filename, trusted, error);
314 source = g_slice_new (GSettingsSchemaSource);
315 source->directory = g_strdup (directory);
316 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
317 source->text_tables = NULL;
318 source->table = table;
319 source->ref_count = 1;
325 try_prepend_dir (const gchar *directory)
327 GSettingsSchemaSource *source;
329 source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
331 /* If we successfully created it then prepend it to the global list */
333 schema_sources = source;
337 try_prepend_data_dir (const gchar *directory)
339 gchar *dirname = g_build_filename (directory, "glib-2.0", "schemas", NULL);
340 try_prepend_dir (dirname);
345 initialise_schema_sources (void)
347 static gsize initialised;
349 /* need a separate variable because 'schema_sources' may legitimately
350 * be null if we have zero valid schema sources
352 if G_UNLIKELY (g_once_init_enter (&initialised))
354 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
355 const gchar * const *dirs;
357 gchar **extra_schema_dirs;
360 /* iterate in reverse: count up, then count down */
361 dirs = g_get_system_data_dirs ();
362 for (i = 0; dirs[i]; i++);
365 try_prepend_data_dir (dirs[i]);
367 try_prepend_data_dir (g_get_user_data_dir ());
369 /* Disallow loading extra schemas if running as setuid, as that could
370 * allow reading privileged files. */
371 if (!is_setuid && (path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
373 extra_schema_dirs = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 0);
374 for (i = 0; extra_schema_dirs[i]; i++);
377 try_prepend_dir (extra_schema_dirs[i]);
379 g_strfreev (extra_schema_dirs);
382 g_once_init_leave (&initialised, TRUE);
387 * g_settings_schema_source_get_default:
389 * Gets the default system schema source.
391 * This function is not required for normal uses of #GSettings but it
392 * may be useful to authors of plugin management systems or to those who
393 * want to introspect the content of schemas.
395 * If no schemas are installed, %NULL will be returned.
397 * The returned source may actually consist of multiple schema sources
398 * from different directories, depending on which directories were given
399 * in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
400 * lookups performed against the default source should probably be done
403 * Returns: (transfer none) (nullable): the default schema source
407 GSettingsSchemaSource *
408 g_settings_schema_source_get_default (void)
410 initialise_schema_sources ();
412 return schema_sources;
416 * g_settings_schema_source_lookup:
417 * @source: a #GSettingsSchemaSource
418 * @schema_id: a schema ID
419 * @recursive: %TRUE if the lookup should be recursive
421 * Looks up a schema with the identifier @schema_id in @source.
423 * This function is not required for normal uses of #GSettings but it
424 * may be useful to authors of plugin management systems or to those who
425 * want to introspect the content of schemas.
427 * If the schema isn't found directly in @source and @recursive is %TRUE
428 * then the parent sources will also be checked.
430 * If the schema isn't found, %NULL is returned.
432 * Returns: (nullable) (transfer full): a new #GSettingsSchema
437 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
438 const gchar *schema_id,
441 GSettingsSchema *schema;
443 const gchar *extends;
445 g_return_val_if_fail (source != NULL, NULL);
446 g_return_val_if_fail (schema_id != NULL, NULL);
448 table = gvdb_table_get_table (source->table, schema_id);
450 if (table == NULL && recursive)
451 for (source = source->parent; source; source = source->parent)
452 if ((table = gvdb_table_get_table (source->table, schema_id)))
458 schema = g_slice_new0 (GSettingsSchema);
459 schema->source = g_settings_schema_source_ref (source);
460 schema->ref_count = 1;
461 schema->id = g_strdup (schema_id);
462 schema->table = table;
463 schema->path = g_settings_schema_get_string (schema, ".path");
464 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
466 if (schema->gettext_domain)
467 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
469 extends = g_settings_schema_get_string (schema, ".extends");
472 schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
473 if (schema->extends == NULL)
474 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
482 GHashTable *summaries;
483 GHashTable *descriptions;
484 GSList *gettext_domain;
488 } TextTableParseInfo;
491 get_attribute_value (GSList *list)
495 for (node = list; node; node = node->next)
503 pop_attribute_value (GSList **list)
508 *list = g_slist_remove (*list, top);
514 push_attribute_value (GSList **list,
517 *list = g_slist_prepend (*list, g_strdup (value));
521 start_element (GMarkupParseContext *context,
522 const gchar *element_name,
523 const gchar **attribute_names,
524 const gchar **attribute_values,
528 TextTableParseInfo *info = user_data;
529 const gchar *gettext_domain = NULL;
530 const gchar *schema_id = NULL;
531 const gchar *key_name = NULL;
534 for (i = 0; attribute_names[i]; i++)
536 if (g_str_equal (attribute_names[i], "gettext-domain"))
537 gettext_domain = attribute_values[i];
538 else if (g_str_equal (attribute_names[i], "id"))
539 schema_id = attribute_values[i];
540 else if (g_str_equal (attribute_names[i], "name"))
541 key_name = attribute_values[i];
544 push_attribute_value (&info->gettext_domain, gettext_domain);
545 push_attribute_value (&info->schema_id, schema_id);
546 push_attribute_value (&info->key_name, key_name);
550 g_string_free (info->string, TRUE);
554 if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
555 info->string = g_string_new (NULL);
559 normalise_whitespace (const gchar *orig)
561 /* We normalise by the same rules as in intltool:
570 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
572 * Where \s is an ascii space character.
574 * We aim for ease of implementation over efficiency -- this code is
575 * not run in normal applications.
577 static GRegex *cleanup[3];
578 static GRegex *splitter;
583 if (g_once_init_enter (&splitter))
587 cleanup[0] = g_regex_new ("^\\s+", G_REGEX_DEFAULT,
588 G_REGEX_MATCH_DEFAULT, NULL);
589 cleanup[1] = g_regex_new ("\\s+$", G_REGEX_DEFAULT,
590 G_REGEX_MATCH_DEFAULT, NULL);
591 cleanup[2] = g_regex_new ("\\s+", G_REGEX_DEFAULT,
592 G_REGEX_MATCH_DEFAULT, NULL);
593 s = g_regex_new ("\\n\\s*\\n+", G_REGEX_DEFAULT,
594 G_REGEX_MATCH_DEFAULT, NULL);
596 g_once_init_leave (&splitter, s);
599 lines = g_regex_split (splitter, orig, 0);
600 for (i = 0; lines[i]; i++)
604 a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
605 b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
606 c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
613 result = g_strjoinv ("\n\n", lines);
620 end_element (GMarkupParseContext *context,
621 const gchar *element_name,
625 TextTableParseInfo *info = user_data;
627 pop_attribute_value (&info->gettext_domain);
628 pop_attribute_value (&info->schema_id);
629 pop_attribute_value (&info->key_name);
633 GHashTable *source_table = NULL;
634 const gchar *gettext_domain;
635 const gchar *schema_id;
636 const gchar *key_name;
638 gettext_domain = get_attribute_value (info->gettext_domain);
639 schema_id = get_attribute_value (info->schema_id);
640 key_name = get_attribute_value (info->key_name);
642 if (g_str_equal (element_name, "summary"))
643 source_table = info->summaries;
644 else if (g_str_equal (element_name, "description"))
645 source_table = info->descriptions;
647 if (source_table && schema_id && key_name)
649 GHashTable *schema_table;
652 schema_table = g_hash_table_lookup (source_table, schema_id);
654 if (schema_table == NULL)
656 schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
657 g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
660 normalised = normalise_whitespace (info->string->str);
662 if (gettext_domain && normalised[0])
666 translated = g_strdup (g_dgettext (gettext_domain, normalised));
668 normalised = translated;
671 g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
674 g_string_free (info->string, TRUE);
680 text (GMarkupParseContext *context,
686 TextTableParseInfo *info = user_data;
689 g_string_append_len (info->string, text, text_len);
693 parse_into_text_tables (const gchar *directory,
694 GHashTable *summaries,
695 GHashTable *descriptions)
697 GMarkupParser parser = { start_element, end_element, text, NULL, NULL };
698 TextTableParseInfo info = { summaries, descriptions, NULL, NULL, NULL, NULL };
699 const gchar *basename;
702 dir = g_dir_open (directory, 0, NULL);
703 while ((basename = g_dir_read_name (dir)))
709 filename = g_build_filename (directory, basename, NULL);
710 if (g_file_get_contents (filename, &contents, &size, NULL))
712 GMarkupParseContext *context;
714 context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
715 /* Ignore errors here, this is best effort only. */
716 if (g_markup_parse_context_parse (context, contents, size, NULL))
717 (void) g_markup_parse_context_end_parse (context, NULL);
718 g_markup_parse_context_free (context);
720 /* Clean up dangling stuff in case there was an error. */
721 g_slist_free_full (info.gettext_domain, g_free);
722 g_slist_free_full (info.schema_id, g_free);
723 g_slist_free_full (info.key_name, g_free);
725 info.gettext_domain = NULL;
726 info.schema_id = NULL;
727 info.key_name = NULL;
731 g_string_free (info.string, TRUE);
745 g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
747 if (g_once_init_enter (&source->text_tables))
749 GHashTable **text_tables;
751 text_tables = g_new (GHashTable *, 2);
752 text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
753 text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
755 if (source->directory)
756 parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
758 g_once_init_leave (&source->text_tables, text_tables);
761 return source->text_tables;
765 * g_settings_schema_source_list_schemas:
766 * @source: a #GSettingsSchemaSource
767 * @recursive: if we should recurse
768 * @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
769 * list of non-relocatable schemas, in no defined order
770 * @relocatable: (out) (transfer full) (array zero-terminated=1): the list
771 * of relocatable schemas, in no defined order
773 * Lists the schemas in a given source.
775 * If @recursive is %TRUE then include parent sources. If %FALSE then
776 * only include the schemas from one source (ie: one directory). You
777 * probably want %TRUE.
779 * Non-relocatable schemas are those for which you can call
780 * g_settings_new(). Relocatable schemas are those for which you must
781 * use g_settings_new_with_path().
783 * Do not call this function from normal programs. This is designed for
784 * use by database editors, commandline tools, etc.
789 g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
791 gchar ***non_relocatable,
792 gchar ***relocatable)
794 GHashTable *single, *reloc;
795 GSettingsSchemaSource *s;
797 /* We use hash tables to avoid duplicate listings for schemas that
798 * appear in more than one file.
800 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
801 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
803 for (s = source; s; s = s->parent)
808 list = gvdb_table_list (s->table, "");
810 /* empty schema cache file? */
814 for (i = 0; list[i]; i++)
816 if (!g_hash_table_contains (single, list[i]) &&
817 !g_hash_table_contains (reloc, list[i]))
822 schema = g_strdup (list[i]);
824 table = gvdb_table_get_table (s->table, list[i]);
825 g_assert (table != NULL);
827 if (gvdb_table_has_value (table, ".path"))
828 g_hash_table_add (single, schema);
830 g_hash_table_add (reloc, schema);
832 gvdb_table_free (table);
838 /* Only the first source if recursive not requested */
845 *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
846 g_hash_table_steal_all (single);
851 *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
852 g_hash_table_steal_all (reloc);
855 g_hash_table_unref (single);
856 g_hash_table_unref (reloc);
859 static gchar **non_relocatable_schema_list;
860 static gchar **relocatable_schema_list;
861 static gsize schema_lists_initialised;
864 ensure_schema_lists (void)
866 if (g_once_init_enter (&schema_lists_initialised))
868 initialise_schema_sources ();
870 g_settings_schema_source_list_schemas (schema_sources, TRUE,
871 &non_relocatable_schema_list,
872 &relocatable_schema_list);
874 g_once_init_leave (&schema_lists_initialised, TRUE);
879 * g_settings_list_schemas:
883 * Returns: (element-type utf8) (transfer none) (not nullable): a list of
884 * #GSettings schemas that are available, in no defined order. The list
885 * must not be modified or freed.
889 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead.
890 * If you used g_settings_list_schemas() to check for the presence of
891 * a particular schema, use g_settings_schema_source_lookup() instead
892 * of your whole loop.
894 const gchar * const *
895 g_settings_list_schemas (void)
897 ensure_schema_lists ();
899 return (const gchar **) non_relocatable_schema_list;
903 * g_settings_list_relocatable_schemas:
907 * Returns: (element-type utf8) (transfer none) (not nullable): a list of
908 * relocatable #GSettings schemas that are available, in no defined order.
909 * The list must not be modified or freed.
913 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead
915 const gchar * const *
916 g_settings_list_relocatable_schemas (void)
918 ensure_schema_lists ();
920 return (const gchar **) relocatable_schema_list;
924 * g_settings_schema_ref:
925 * @schema: a #GSettingsSchema
927 * Increase the reference count of @schema, returning a new reference.
929 * Returns: (transfer full) (not nullable): a new reference to @schema
934 g_settings_schema_ref (GSettingsSchema *schema)
936 g_atomic_int_inc (&schema->ref_count);
942 * g_settings_schema_unref:
943 * @schema: a #GSettingsSchema
945 * Decrease the reference count of @schema, possibly freeing it.
950 g_settings_schema_unref (GSettingsSchema *schema)
952 if (g_atomic_int_dec_and_test (&schema->ref_count))
955 g_settings_schema_unref (schema->extends);
957 g_settings_schema_source_unref (schema->source);
958 gvdb_table_free (schema->table);
959 g_free (schema->items);
962 g_slice_free (GSettingsSchema, schema);
967 g_settings_schema_get_string (GSettingsSchema *schema,
970 const gchar *result = NULL;
973 if ((value = gvdb_table_get_raw_value (schema->table, key)))
975 result = g_variant_get_string (value, NULL);
976 g_variant_unref (value);
983 g_settings_schema_get_child_schema (GSettingsSchema *schema,
986 const gchar *child_id;
989 child_name = g_strconcat (name, "/", NULL);
990 child_id = g_settings_schema_get_string (schema, child_name);
994 if (child_id == NULL)
997 return g_settings_schema_source_lookup (schema->source, child_id, TRUE);
1001 g_settings_schema_get_value (GSettingsSchema *schema,
1004 GSettingsSchema *s = schema;
1006 GVariant *value = NULL;
1008 g_return_val_if_fail (schema != NULL, NULL);
1010 for (s = schema; s; s = s->extends)
1011 if ((value = gvdb_table_get_raw_value (s->table, key)))
1014 if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
1015 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
1017 iter = g_variant_iter_new (value);
1018 g_variant_unref (value);
1024 * g_settings_schema_get_path:
1025 * @schema: a #GSettingsSchema
1027 * Gets the path associated with @schema, or %NULL.
1029 * Schemas may be single-instance or relocatable. Single-instance
1030 * schemas correspond to exactly one set of keys in the backend
1031 * database: those located at the path returned by this function.
1033 * Relocatable schemas can be referenced by other schemas and can
1034 * therefore describe multiple sets of keys at different locations. For
1035 * relocatable schemas, this function will return %NULL.
1037 * Returns: (nullable) (transfer none): the path of the schema, or %NULL
1042 g_settings_schema_get_path (GSettingsSchema *schema)
1044 return schema->path;
1048 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
1050 return schema->gettext_domain;
1054 * g_settings_schema_has_key:
1055 * @schema: a #GSettingsSchema
1056 * @name: the name of a key
1058 * Checks if @schema has a key named @name.
1060 * Returns: %TRUE if such a key exists
1065 g_settings_schema_has_key (GSettingsSchema *schema,
1068 return gvdb_table_has_value (schema->table, key);
1072 * g_settings_schema_list_children:
1073 * @schema: a #GSettingsSchema
1075 * Gets the list of children in @schema.
1077 * You should free the return value with g_strfreev() when you are done
1080 * Returns: (not nullable) (transfer full) (element-type utf8): a list of
1081 * the children on @settings, in no defined order
1086 g_settings_schema_list_children (GSettingsSchema *schema)
1093 g_return_val_if_fail (schema != NULL, NULL);
1095 keys = g_settings_schema_list (schema, &n_keys);
1096 strv = g_new (gchar *, n_keys + 1);
1097 for (i = j = 0; i < n_keys; i++)
1099 const gchar *key = g_quark_to_string (keys[i]);
1101 if (g_str_has_suffix (key, "/"))
1103 gsize length = strlen (key);
1105 strv[j] = g_memdup2 (key, length);
1106 strv[j][length - 1] = '\0';
1116 * g_settings_schema_list_keys:
1117 * @schema: a #GSettingsSchema
1119 * Introspects the list of keys on @schema.
1121 * You should probably not be calling this function from "normal" code
1122 * (since you should already know what keys are in your schema). This
1123 * function is intended for introspection reasons.
1125 * Returns: (not nullable) (transfer full) (element-type utf8): a list
1126 * of the keys on @schema, in no defined order
1131 g_settings_schema_list_keys (GSettingsSchema *schema)
1138 g_return_val_if_fail (schema != NULL, NULL);
1140 keys = g_settings_schema_list (schema, &n_keys);
1141 strv = g_new (gchar *, n_keys + 1);
1142 for (i = j = 0; i < n_keys; i++)
1144 const gchar *key = g_quark_to_string (keys[i]);
1146 if (!g_str_has_suffix (key, "/"))
1147 strv[j++] = g_strdup (key);
1155 g_settings_schema_list (GSettingsSchema *schema,
1158 if (schema->items == NULL)
1161 GHashTableIter iter;
1167 items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1169 for (s = schema; s; s = s->extends)
1173 list = gvdb_table_list (s->table, "");
1177 for (i = 0; list[i]; i++)
1178 g_hash_table_add (items, list[i]); /* transfer ownership */
1180 g_free (list); /* free container only */
1184 /* Do a first pass to eliminate child items that do not map to
1185 * valid schemas (ie: ones that would crash us if we actually
1186 * tried to create them).
1188 g_hash_table_iter_init (&iter, items);
1189 while (g_hash_table_iter_next (&iter, &name, NULL))
1190 if (g_str_has_suffix (name, "/"))
1192 GSettingsSchemaSource *source;
1193 GVariant *child_schema;
1194 GvdbTable *child_table;
1196 child_schema = gvdb_table_get_raw_value (schema->table, name);
1202 for (source = schema->source; source; source = source->parent)
1203 if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1206 g_variant_unref (child_schema);
1208 /* Schema is not found -> remove it from the list */
1209 if (child_table == NULL)
1211 g_hash_table_iter_remove (&iter);
1215 /* Make sure the schema is relocatable or at the
1218 if (gvdb_table_has_value (child_table, ".path"))
1224 path = gvdb_table_get_raw_value (child_table, ".path");
1225 expected = g_strconcat (schema->path, name, NULL);
1226 same = g_str_equal (expected, g_variant_get_string (path, NULL));
1227 g_variant_unref (path);
1230 /* Schema is non-relocatable and did not have the
1231 * expected path -> remove it from the list
1234 g_hash_table_iter_remove (&iter);
1237 gvdb_table_free (child_table);
1240 /* Now create the list */
1241 len = g_hash_table_size (items);
1242 schema->items = g_new (GQuark, len);
1244 g_hash_table_iter_init (&iter, items);
1246 while (g_hash_table_iter_next (&iter, &name, NULL))
1247 schema->items[i++] = g_quark_from_string (name);
1248 schema->n_items = i;
1249 g_assert (i == len);
1251 g_hash_table_unref (items);
1254 *n_items = schema->n_items;
1255 return schema->items;
1259 * g_settings_schema_get_id:
1260 * @schema: a #GSettingsSchema
1262 * Get the ID of @schema.
1264 * Returns: (not nullable) (transfer none): the ID
1267 g_settings_schema_get_id (GSettingsSchema *schema)
1273 endian_fixup (GVariant **value)
1275 #if G_BYTE_ORDER == G_BIG_ENDIAN
1278 tmp = g_variant_byteswap (*value);
1279 g_variant_unref (*value);
1285 g_settings_schema_key_init (GSettingsSchemaKey *key,
1286 GSettingsSchema *schema,
1293 memset (key, 0, sizeof *key);
1295 iter = g_settings_schema_get_value (schema, name);
1297 key->schema = g_settings_schema_ref (schema);
1298 key->default_value = g_variant_iter_next_value (iter);
1299 endian_fixup (&key->default_value);
1300 key->type = g_variant_get_type (key->default_value);
1301 key->name = g_intern_string (name);
1303 while (g_variant_iter_next (iter, "(y*)", &code, &data))
1308 /* translation requested */
1309 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1313 /* enumerated types... */
1314 key->is_enum = TRUE;
1319 key->is_flags = TRUE;
1323 /* ..., choices, aliases */
1324 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1328 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1329 endian_fixup (&key->minimum);
1330 endian_fixup (&key->maximum);
1334 g_variant_get (data, "@a{sv}", &key->desktop_overrides);
1335 endian_fixup (&key->desktop_overrides);
1339 g_warning ("unknown schema extension '%c'", code);
1343 g_variant_unref (data);
1346 g_variant_iter_free (iter);
1350 g_settings_schema_key_clear (GSettingsSchemaKey *key)
1353 g_variant_unref (key->minimum);
1356 g_variant_unref (key->maximum);
1358 if (key->desktop_overrides)
1359 g_variant_unref (key->desktop_overrides);
1361 g_variant_unref (key->default_value);
1363 g_settings_schema_unref (key->schema);
1367 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1370 g_return_val_if_fail (value != NULL, FALSE);
1372 return g_variant_is_of_type (value, key->type);
1376 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1379 const gchar *target;
1381 if (g_settings_schema_key_range_check (key, value))
1382 return g_variant_ref (value);
1384 if (key->strinfo == NULL)
1387 if (g_variant_is_container (value))
1389 GVariantBuilder builder;
1393 g_variant_iter_init (&iter, value);
1394 g_variant_builder_init (&builder, g_variant_get_type (value));
1396 while ((child = g_variant_iter_next_value (&iter)))
1400 fixed = g_settings_schema_key_range_fixup (key, child);
1401 g_variant_unref (child);
1405 g_variant_builder_clear (&builder);
1409 g_variant_builder_add_value (&builder, fixed);
1410 g_variant_unref (fixed);
1413 return g_variant_ref_sink (g_variant_builder_end (&builder));
1416 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1417 g_variant_get_string (value, NULL));
1418 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1422 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1424 const gchar *translated = NULL;
1425 GError *error = NULL;
1426 const gchar *domain;
1427 #ifdef HAVE_USELOCALE
1428 const gchar *lc_time;
1429 locale_t old_locale;
1434 domain = g_settings_schema_get_gettext_domain (key->schema);
1436 if (key->lc_char == '\0')
1437 /* translation not requested for this key */
1440 #ifdef HAVE_USELOCALE
1441 if (key->lc_char == 't')
1443 lc_time = setlocale (LC_TIME, NULL);
1446 locale = newlocale (LC_MESSAGES_MASK, lc_time, (locale_t) 0);
1447 if (locale != (locale_t) 0)
1449 old_locale = uselocale (locale);
1450 translated = g_dgettext (domain, key->unparsed);
1451 uselocale (old_locale);
1452 freelocale (locale);
1458 if (translated == NULL)
1459 translated = g_dgettext (domain, key->unparsed);
1461 if (translated == key->unparsed)
1462 /* the default value was not translated */
1465 /* try to parse the translation of the unparsed default */
1466 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1470 g_warning ("Failed to parse translated string '%s' for "
1471 "key '%s' in schema '%s': %s", translated, key->name,
1472 g_settings_schema_get_id (key->schema), error->message);
1473 g_warning ("Using untranslated default instead.");
1474 g_error_free (error);
1477 else if (!g_settings_schema_key_range_check (key, value))
1479 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1480 "is outside of valid range", key->unparsed, key->name,
1481 g_settings_schema_get_id (key->schema));
1482 g_variant_unref (value);
1490 g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
1492 static const gchar * const *current_desktops;
1493 GVariant *value = NULL;
1496 if (!key->desktop_overrides)
1499 if (g_once_init_enter (¤t_desktops))
1501 const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
1504 if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
1505 tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
1507 tmp = g_new0 (gchar *, 0 + 1);
1509 g_once_init_leave (¤t_desktops, (const gchar **) tmp);
1512 for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
1513 value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
1519 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1522 gboolean it_worked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
1525 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1526 g_variant_get_string (value, NULL),
1529 /* 'value' can only come from the backend after being filtered for validity,
1530 * from the translation after being filtered for validity, or from the schema
1531 * itself (which the schema compiler checks for validity). If this assertion
1532 * fails then it's really a bug in GSettings or the schema compiler...
1534 g_assert (it_worked);
1539 /* Returns a new floating #GVariant. */
1541 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1544 const gchar *string;
1546 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1551 return g_variant_new_string (string);
1555 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1563 g_variant_iter_init (&iter, value);
1564 while (g_variant_iter_next (&iter, "&s", &flag))
1566 gboolean it_worked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
1569 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1570 /* as in g_settings_to_enum() */
1571 g_assert (it_worked);
1573 result |= flag_value;
1579 /* Returns a new floating #GVariant. */
1581 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1584 GVariantBuilder builder;
1587 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1589 for (i = 0; i < 32; i++)
1590 if (value & (1u << i))
1592 const gchar *string;
1594 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1598 g_variant_builder_clear (&builder);
1602 g_variant_builder_add (&builder, "s", string);
1605 return g_variant_builder_end (&builder);
1608 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1611 * g_settings_schema_key_ref:
1612 * @key: a #GSettingsSchemaKey
1614 * Increase the reference count of @key, returning a new reference.
1616 * Returns: (not nullable) (transfer full): a new reference to @key
1620 GSettingsSchemaKey *
1621 g_settings_schema_key_ref (GSettingsSchemaKey *key)
1623 g_return_val_if_fail (key != NULL, NULL);
1625 g_atomic_int_inc (&key->ref_count);
1631 * g_settings_schema_key_unref:
1632 * @key: a #GSettingsSchemaKey
1634 * Decrease the reference count of @key, possibly freeing it.
1639 g_settings_schema_key_unref (GSettingsSchemaKey *key)
1641 g_return_if_fail (key != NULL);
1643 if (g_atomic_int_dec_and_test (&key->ref_count))
1645 g_settings_schema_key_clear (key);
1647 g_slice_free (GSettingsSchemaKey, key);
1652 * g_settings_schema_get_key:
1653 * @schema: a #GSettingsSchema
1654 * @name: the name of a key
1656 * Gets the key named @name from @schema.
1658 * It is a programmer error to request a key that does not exist. See
1659 * g_settings_schema_list_keys().
1661 * Returns: (not nullable) (transfer full): the #GSettingsSchemaKey for @name
1665 GSettingsSchemaKey *
1666 g_settings_schema_get_key (GSettingsSchema *schema,
1669 GSettingsSchemaKey *key;
1671 g_return_val_if_fail (schema != NULL, NULL);
1672 g_return_val_if_fail (name != NULL, NULL);
1674 key = g_slice_new (GSettingsSchemaKey);
1675 g_settings_schema_key_init (key, schema, name);
1682 * g_settings_schema_key_get_name:
1683 * @key: a #GSettingsSchemaKey
1685 * Gets the name of @key.
1687 * Returns: (not nullable) (transfer none): the name of @key.
1692 g_settings_schema_key_get_name (GSettingsSchemaKey *key)
1694 g_return_val_if_fail (key != NULL, NULL);
1700 * g_settings_schema_key_get_summary:
1701 * @key: a #GSettingsSchemaKey
1703 * Gets the summary for @key.
1705 * If no summary has been provided in the schema for @key, returns
1708 * The summary is a short description of the purpose of the key; usually
1709 * one short sentence. Summaries can be translated and the value
1710 * returned from this function is is the current locale.
1712 * This function is slow. The summary and description information for
1713 * the schemas is not stored in the compiled schema database so this
1714 * function has to parse all of the source XML files in the schema
1717 * Returns: (nullable) (transfer none): the summary for @key, or %NULL
1722 g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1724 GHashTable **text_tables;
1725 GHashTable *summaries;
1727 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1728 summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1730 return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1734 * g_settings_schema_key_get_description:
1735 * @key: a #GSettingsSchemaKey
1737 * Gets the description for @key.
1739 * If no description has been provided in the schema for @key, returns
1742 * The description can be one sentence to several paragraphs in length.
1743 * Paragraphs are delimited with a double newline. Descriptions can be
1744 * translated and the value returned from this function is is the
1747 * This function is slow. The summary and description information for
1748 * the schemas is not stored in the compiled schema database so this
1749 * function has to parse all of the source XML files in the schema
1752 * Returns: (nullable) (transfer none): the description for @key, or %NULL
1757 g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1759 GHashTable **text_tables;
1760 GHashTable *descriptions;
1762 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1763 descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1765 return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1769 * g_settings_schema_key_get_value_type:
1770 * @key: a #GSettingsSchemaKey
1772 * Gets the #GVariantType of @key.
1774 * Returns: (not nullable) (transfer none): the type of @key
1778 const GVariantType *
1779 g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1781 g_return_val_if_fail (key, NULL);
1787 * g_settings_schema_key_get_default_value:
1788 * @key: a #GSettingsSchemaKey
1790 * Gets the default value for @key.
1792 * Note that this is the default value according to the schema. System
1793 * administrator defaults and lockdown are not visible via this API.
1795 * Returns: (not nullable) (transfer full): the default value for the key
1800 g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1804 g_return_val_if_fail (key, NULL);
1806 value = g_settings_schema_key_get_translated_default (key);
1809 value = g_settings_schema_key_get_per_desktop_default (key);
1812 value = g_variant_ref (key->default_value);
1818 * g_settings_schema_key_get_range:
1819 * @key: a #GSettingsSchemaKey
1821 * Queries the range of a key.
1823 * This function will return a #GVariant that fully describes the range
1824 * of values that are valid for @key.
1826 * The type of #GVariant returned is `(sv)`. The string describes
1827 * the type of range restriction in effect. The type and meaning of
1828 * the value contained in the variant depends on the string.
1830 * If the string is `'type'` then the variant contains an empty array.
1831 * The element type of that empty array is the expected type of value
1832 * and all values of that type are valid.
1834 * If the string is `'enum'` then the variant contains an array
1835 * enumerating the possible values. Each item in the array is
1836 * a possible valid value and no other values are valid.
1838 * If the string is `'flags'` then the variant contains an array. Each
1839 * item in the array is a value that may appear zero or one times in an
1840 * array to be used as the value for this key. For example, if the
1841 * variant contained the array `['x', 'y']` then the valid values for
1842 * the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
1845 * Finally, if the string is `'range'` then the variant contains a pair
1846 * of like-typed values -- the minimum and maximum permissible values
1849 * This information should not be used by normal programs. It is
1850 * considered to be a hint for introspection purposes. Normal programs
1851 * should already know what is permitted by their own schema. The
1852 * format may change in any way in the future -- but particularly, new
1853 * forms may be added to the possibilities described above.
1855 * You should free the returned value with g_variant_unref() when it is
1858 * Returns: (not nullable) (transfer full): a #GVariant describing the range
1863 g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1870 range = g_variant_new ("(**)", key->minimum, key->maximum);
1873 else if (key->strinfo)
1875 range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1876 type = key->is_flags ? "flags" : "enum";
1880 range = g_variant_new_array (key->type, NULL, 0);
1884 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1888 * g_settings_schema_key_range_check:
1889 * @key: a #GSettingsSchemaKey
1890 * @value: the value to check
1892 * Checks if the given @value is within the
1893 * permitted range for @key.
1895 * It is a programmer error if @value is not of the correct type — you
1896 * must check for this first.
1898 * Returns: %TRUE if @value is valid for @key
1903 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1906 if (key->minimum == NULL && key->strinfo == NULL)
1909 if (g_variant_is_container (value))
1915 g_variant_iter_init (&iter, value);
1916 while (ok && (child = g_variant_iter_next_value (&iter)))
1918 ok = g_settings_schema_key_range_check (key, child);
1919 g_variant_unref (child);
1927 return g_variant_compare (key->minimum, value) <= 0 &&
1928 g_variant_compare (value, key->maximum) <= 0;
1931 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1932 g_variant_get_string (value, NULL));