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 of the licence, 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/>.
21 #include "gsettingsschema-internal.h"
22 #include "gsettings.h"
24 #include "gvdb/gvdb-reader.h"
32 * SECTION:gsettingsschema
33 * @short_description: Introspecting and controlling the loading
34 * of GSettings schemas
37 * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
38 * mechanism for advanced control over the loading of schemas and a
39 * mechanism for introspecting their content.
41 * Plugin loading systems that wish to provide plugins a way to access
42 * settings face the problem of how to make the schemas for these
43 * settings visible to GSettings. Typically, a plugin will want to ship
44 * the schema along with itself and it won't be installed into the
45 * standard system directories for schemas.
47 * #GSettingsSchemaSource provides a mechanism for dealing with this by
48 * allowing the creation of a new 'schema source' from which schemas can
49 * be acquired. This schema source can then become part of the metadata
50 * associated with the plugin and queried whenever the plugin requires
51 * access to some settings.
53 * Consider the following example:
55 * |[<!-- language="C" -->
59 * GSettingsSchemaSource *schema_source;
64 * initialise_plugin (const gchar *dir)
70 * plugin->schema_source =
71 * g_settings_new_schema_source_from_directory (dir,
72 * g_settings_schema_source_get_default (), FALSE, NULL);
82 * plugin_get_settings (Plugin *plugin,
83 * const gchar *schema_id)
85 * GSettingsSchema *schema;
87 * if (schema_id == NULL)
88 * schema_id = plugin->identifier;
90 * schema = g_settings_schema_source_lookup (plugin->schema_source,
95 * ... disable the plugin or abort, etc ...
98 * return g_settings_new_full (schema, NULL, NULL);
102 * The code above shows how hooks should be added to the code that
103 * initialises (or enables) the plugin to create the schema source and
104 * how an API can be added to the plugin system to provide a convenient
105 * way for the plugin to access its settings, using the schemas that it
108 * From the standpoint of the plugin, it would need to ensure that it
109 * ships a gschemas.compiled file as part of itself, and then simply do
112 * |[<!-- language="C" -->
114 * GSettings *settings;
117 * settings = plugin_get_settings (self, NULL);
118 * some_value = g_settings_get_int (settings, "some-value");
123 * It's also possible that the plugin system expects the schema source
124 * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
125 * In that case, the plugin loading system must compile the schemas for
126 * itself before attempting to create the settings source.
134 * This is an opaque structure type. You may not access it directly.
138 struct _GSettingsSchema
140 GSettingsSchemaSource *source;
141 const gchar *gettext_domain;
148 GSettingsSchema *extends;
154 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
156 * A boxed #GType corresponding to #GSettingsSchemaSource.
160 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
163 * G_TYPE_SETTINGS_SCHEMA:
165 * A boxed #GType corresponding to #GSettingsSchema.
169 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
172 * GSettingsSchemaSource:
174 * This is an opaque structure type. You may not access it directly.
178 struct _GSettingsSchemaSource
180 GSettingsSchemaSource *parent;
183 GHashTable **text_tables;
188 static GSettingsSchemaSource *schema_sources;
191 * g_settings_schema_source_ref:
192 * @source: a #GSettingsSchemaSource
194 * Increase the reference count of @source, returning a new reference.
196 * Returns: a new reference to @source
200 GSettingsSchemaSource *
201 g_settings_schema_source_ref (GSettingsSchemaSource *source)
203 g_atomic_int_inc (&source->ref_count);
209 * g_settings_schema_source_unref:
210 * @source: a #GSettingsSchemaSource
212 * Decrease the reference count of @source, possibly freeing it.
217 g_settings_schema_source_unref (GSettingsSchemaSource *source)
219 if (g_atomic_int_dec_and_test (&source->ref_count))
221 if (source == schema_sources)
222 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
225 g_settings_schema_source_unref (source->parent);
226 gvdb_table_unref (source->table);
227 g_free (source->directory);
229 if (source->text_tables)
231 g_hash_table_unref (source->text_tables[0]);
232 g_hash_table_unref (source->text_tables[1]);
233 g_free (source->text_tables);
236 g_slice_free (GSettingsSchemaSource, source);
241 * g_settings_schema_source_new_from_directory:
242 * @directory: the filename of a directory
243 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
244 * @trusted: %TRUE, if the directory is trusted
245 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
247 * Attempts to create a new schema source corresponding to the contents
248 * of the given directory.
250 * This function is not required for normal uses of #GSettings but it
251 * may be useful to authors of plugin management systems.
253 * The directory should contain a file called `gschemas.compiled` as
254 * produced by the [glib-compile-schemas][glib-compile-schemas] tool.
256 * If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
257 * corrupted. This assumption has a performance advantage, but can result
258 * in crashes or inconsistent behaviour in the case of a corrupted file.
259 * Generally, you should set @trusted to %TRUE for files installed by the
260 * system and to %FALSE for files in the home directory.
262 * If @parent is non-%NULL then there are two effects.
264 * First, if g_settings_schema_source_lookup() is called with the
265 * @recursive flag set to %TRUE and the schema can not be found in the
266 * source, the lookup will recurse to the parent.
268 * Second, any references to other schemas specified within this
269 * source (ie: `child` or `extends`) references may be resolved
272 * For this second reason, except in very unusual situations, the
273 * @parent should probably be given as the default schema source, as
274 * returned by g_settings_schema_source_get_default().
278 GSettingsSchemaSource *
279 g_settings_schema_source_new_from_directory (const gchar *directory,
280 GSettingsSchemaSource *parent,
284 GSettingsSchemaSource *source;
288 filename = g_build_filename (directory, "gschemas.compiled", NULL);
289 table = gvdb_table_new (filename, trusted, error);
295 source = g_slice_new (GSettingsSchemaSource);
296 source->directory = g_strdup (directory);
297 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
298 source->text_tables = NULL;
299 source->table = table;
300 source->ref_count = 1;
306 try_prepend_dir (const gchar *directory)
308 GSettingsSchemaSource *source;
310 source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
312 /* If we successfully created it then prepend it to the global list */
314 schema_sources = source;
318 initialise_schema_sources (void)
320 static gsize initialised;
322 /* need a separate variable because 'schema_sources' may legitimately
323 * be null if we have zero valid schema sources
325 if G_UNLIKELY (g_once_init_enter (&initialised))
327 const gchar * const *dirs;
331 /* iterate in reverse: count up, then count down */
332 dirs = g_get_system_data_dirs ();
333 for (i = 0; dirs[i]; i++);
339 dirname = g_build_filename (dirs[i], "glib-2.0", "schemas", NULL);
340 try_prepend_dir (dirname);
344 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
345 try_prepend_dir (path);
347 g_once_init_leave (&initialised, TRUE);
352 * g_settings_schema_source_get_default:
354 * Gets the default system schema source.
356 * This function is not required for normal uses of #GSettings but it
357 * may be useful to authors of plugin management systems or to those who
358 * want to introspect the content of schemas.
360 * If no schemas are installed, %NULL will be returned.
362 * The returned source may actually consist of multiple schema sources
363 * from different directories, depending on which directories were given
364 * in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
365 * lookups performed against the default source should probably be done
368 * Returns: (transfer none): the default schema source
372 GSettingsSchemaSource *
373 g_settings_schema_source_get_default (void)
375 initialise_schema_sources ();
377 return schema_sources;
381 * g_settings_schema_source_lookup:
382 * @source: a #GSettingsSchemaSource
383 * @schema_id: a schema ID
384 * @recursive: %TRUE if the lookup should be recursive
386 * Looks up a schema with the identifier @schema_id in @source.
388 * This function is not required for normal uses of #GSettings but it
389 * may be useful to authors of plugin management systems or to those who
390 * want to introspect the content of schemas.
392 * If the schema isn't found directly in @source and @recursive is %TRUE
393 * then the parent sources will also be checked.
395 * If the schema isn't found, %NULL is returned.
397 * Returns: (nullable) (transfer full): a new #GSettingsSchema
402 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
403 const gchar *schema_id,
406 GSettingsSchema *schema;
408 const gchar *extends;
410 g_return_val_if_fail (source != NULL, NULL);
411 g_return_val_if_fail (schema_id != NULL, NULL);
413 table = gvdb_table_get_table (source->table, schema_id);
415 if (table == NULL && recursive)
416 for (source = source->parent; source; source = source->parent)
417 if ((table = gvdb_table_get_table (source->table, schema_id)))
423 schema = g_slice_new0 (GSettingsSchema);
424 schema->source = g_settings_schema_source_ref (source);
425 schema->ref_count = 1;
426 schema->id = g_strdup (schema_id);
427 schema->table = table;
428 schema->path = g_settings_schema_get_string (schema, ".path");
429 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
431 if (schema->gettext_domain)
432 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
434 extends = g_settings_schema_get_string (schema, ".extends");
437 schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
438 if (schema->extends == NULL)
439 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
447 GHashTable *summaries;
448 GHashTable *descriptions;
449 GSList *gettext_domain;
453 } TextTableParseInfo;
456 get_attribute_value (GSList *list)
460 for (node = list; node; node = node->next)
468 pop_attribute_value (GSList **list)
473 *list = g_slist_remove (*list, top);
479 push_attribute_value (GSList **list,
482 *list = g_slist_prepend (*list, g_strdup (value));
486 start_element (GMarkupParseContext *context,
487 const gchar *element_name,
488 const gchar **attribute_names,
489 const gchar **attribute_values,
493 TextTableParseInfo *info = user_data;
494 const gchar *gettext_domain = NULL;
495 const gchar *schema_id = NULL;
496 const gchar *key_name = NULL;
499 for (i = 0; attribute_names[i]; i++)
501 if (g_str_equal (attribute_names[i], "gettext-domain"))
502 gettext_domain = attribute_values[i];
503 else if (g_str_equal (attribute_names[i], "id"))
504 schema_id = attribute_values[i];
505 else if (g_str_equal (attribute_names[i], "name"))
506 key_name = attribute_values[i];
509 push_attribute_value (&info->gettext_domain, gettext_domain);
510 push_attribute_value (&info->schema_id, schema_id);
511 push_attribute_value (&info->key_name, key_name);
515 g_string_free (info->string, TRUE);
519 if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
520 info->string = g_string_new (NULL);
524 normalise_whitespace (const gchar *orig)
526 /* We normalise by the same rules as in intltool:
535 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
537 * Where \s is an ascii space character.
539 * We aim for ease of implementation over efficiency -- this code is
540 * not run in normal applications.
542 static GRegex *cleanup[3];
543 static GRegex *splitter;
548 if (g_once_init_enter (&splitter))
552 cleanup[0] = g_regex_new ("^\\s+", 0, 0, 0);
553 cleanup[1] = g_regex_new ("\\s+$", 0, 0, 0);
554 cleanup[2] = g_regex_new ("\\s+", 0, 0, 0);
555 s = g_regex_new ("\\n\\s*\\n+", 0, 0, 0);
557 g_once_init_leave (&splitter, s);
560 lines = g_regex_split (splitter, orig, 0);
561 for (i = 0; lines[i]; i++)
565 a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
566 b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
567 c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
574 result = g_strjoinv ("\n\n", lines);
581 end_element (GMarkupParseContext *context,
582 const gchar *element_name,
586 TextTableParseInfo *info = user_data;
588 pop_attribute_value (&info->gettext_domain);
589 pop_attribute_value (&info->schema_id);
590 pop_attribute_value (&info->key_name);
594 GHashTable *source_table = NULL;
595 const gchar *gettext_domain;
596 const gchar *schema_id;
597 const gchar *key_name;
599 gettext_domain = get_attribute_value (info->gettext_domain);
600 schema_id = get_attribute_value (info->schema_id);
601 key_name = get_attribute_value (info->key_name);
603 if (g_str_equal (element_name, "summary"))
604 source_table = info->summaries;
605 else if (g_str_equal (element_name, "description"))
606 source_table = info->descriptions;
608 if (source_table && schema_id && key_name)
610 GHashTable *schema_table;
613 schema_table = g_hash_table_lookup (source_table, schema_id);
615 if (schema_table == NULL)
617 schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
618 g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
621 normalised = normalise_whitespace (info->string->str);
627 translated = g_strdup (g_dgettext (gettext_domain, normalised));
629 normalised = translated;
632 g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
635 g_string_free (info->string, TRUE);
641 text (GMarkupParseContext *context,
647 TextTableParseInfo *info = user_data;
650 g_string_append_len (info->string, text, text_len);
654 parse_into_text_tables (const gchar *directory,
655 GHashTable *summaries,
656 GHashTable *descriptions)
658 GMarkupParser parser = { start_element, end_element, text };
659 TextTableParseInfo info = { summaries, descriptions };
660 const gchar *basename;
663 dir = g_dir_open (directory, 0, NULL);
664 while ((basename = g_dir_read_name (dir)))
670 filename = g_build_filename (directory, basename, NULL);
671 if (g_file_get_contents (filename, &contents, &size, NULL))
673 GMarkupParseContext *context;
675 context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
676 if (g_markup_parse_context_parse (context, contents, size, NULL))
677 g_markup_parse_context_end_parse (context, NULL);
678 g_markup_parse_context_free (context);
680 /* Clean up dangling stuff in case there was an error. */
681 g_slist_free_full (info.gettext_domain, g_free);
682 g_slist_free_full (info.schema_id, g_free);
683 g_slist_free_full (info.key_name, g_free);
685 info.gettext_domain = NULL;
686 info.schema_id = NULL;
687 info.key_name = NULL;
691 g_string_free (info.string, TRUE);
705 g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
707 if (g_once_init_enter (&source->text_tables))
709 GHashTable **text_tables;
711 text_tables = g_new (GHashTable *, 2);
712 text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
713 text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
715 if (source->directory)
716 parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
718 g_once_init_leave (&source->text_tables, text_tables);
721 return source->text_tables;
725 * g_settings_schema_source_list_schemas:
726 * @source: a #GSettingsSchemaSource
727 * @recursive: if we should recurse
728 * @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
729 * list of non-relocatable schemas
730 * @relocatable: (out) (transfer full) (array zero-terminated=1): the list
731 * of relocatable schemas
733 * Lists the schemas in a given source.
735 * If @recursive is %TRUE then include parent sources. If %FALSE then
736 * only include the schemas from one source (ie: one directory). You
737 * probably want %TRUE.
739 * Non-relocatable schemas are those for which you can call
740 * g_settings_new(). Relocatable schemas are those for which you must
741 * use g_settings_new_with_path().
743 * Do not call this function from normal programs. This is designed for
744 * use by database editors, commandline tools, etc.
749 g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
751 gchar ***non_relocatable,
752 gchar ***relocatable)
754 GHashTable *single, *reloc;
755 GSettingsSchemaSource *s;
757 /* We use hash tables to avoid duplicate listings for schemas that
758 * appear in more than one file.
760 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
761 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
763 for (s = source; s; s = s->parent)
768 list = gvdb_table_list (s->table, "");
770 /* empty schema cache file? */
774 for (i = 0; list[i]; i++)
776 if (!g_hash_table_lookup (single, list[i]) &&
777 !g_hash_table_lookup (reloc, list[i]))
781 table = gvdb_table_get_table (s->table, list[i]);
782 g_assert (table != NULL);
784 if (gvdb_table_has_value (table, ".path"))
785 g_hash_table_insert (single, g_strdup (list[i]), NULL);
787 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
789 gvdb_table_unref (table);
795 /* Only the first source if recursive not requested */
802 *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
803 g_hash_table_steal_all (single);
808 *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
809 g_hash_table_steal_all (reloc);
812 g_hash_table_unref (single);
813 g_hash_table_unref (reloc);
816 static gchar **non_relocatable_schema_list;
817 static gchar **relocatable_schema_list;
818 static gsize schema_lists_initialised;
821 ensure_schema_lists (void)
823 if (g_once_init_enter (&schema_lists_initialised))
825 initialise_schema_sources ();
827 g_settings_schema_source_list_schemas (schema_sources, TRUE,
828 &non_relocatable_schema_list,
829 &relocatable_schema_list);
831 g_once_init_leave (&schema_lists_initialised, TRUE);
836 * g_settings_list_schemas:
838 * Returns: (element-type utf8) (transfer none): a list of #GSettings
839 * schemas that are available. The list must not be modified or
844 * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead.
845 * If you used g_settings_list_schemas() to check for the presence of
846 * a particular schema, use g_settings_schema_source_lookup() instead
847 * of your whole loop.
849 const gchar * const *
850 g_settings_list_schemas (void)
852 ensure_schema_lists ();
854 return (const gchar **) non_relocatable_schema_list;
858 * g_settings_list_relocatable_schemas:
860 * Returns: (element-type utf8) (transfer none): a list of relocatable
861 * #GSettings schemas that are available. The list must not be
866 * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
868 const gchar * const *
869 g_settings_list_relocatable_schemas (void)
871 ensure_schema_lists ();
873 return (const gchar **) relocatable_schema_list;
877 * g_settings_schema_ref:
878 * @schema: a #GSettingsSchema
880 * Increase the reference count of @schema, returning a new reference.
882 * Returns: a new reference to @schema
887 g_settings_schema_ref (GSettingsSchema *schema)
889 g_atomic_int_inc (&schema->ref_count);
895 * g_settings_schema_unref:
896 * @schema: a #GSettingsSchema
898 * Decrease the reference count of @schema, possibly freeing it.
903 g_settings_schema_unref (GSettingsSchema *schema)
905 if (g_atomic_int_dec_and_test (&schema->ref_count))
908 g_settings_schema_unref (schema->extends);
910 g_settings_schema_source_unref (schema->source);
911 gvdb_table_unref (schema->table);
912 g_free (schema->items);
915 g_slice_free (GSettingsSchema, schema);
920 g_settings_schema_get_string (GSettingsSchema *schema,
923 const gchar *result = NULL;
926 if ((value = gvdb_table_get_raw_value (schema->table, key)))
928 result = g_variant_get_string (value, NULL);
929 g_variant_unref (value);
936 g_settings_schema_get_value (GSettingsSchema *schema,
939 GSettingsSchema *s = schema;
943 g_return_val_if_fail (schema != NULL, NULL);
945 for (s = schema; s; s = s->extends)
946 if ((value = gvdb_table_get_raw_value (s->table, key)))
949 if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
950 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
952 iter = g_variant_iter_new (value);
953 g_variant_unref (value);
959 * g_settings_schema_get_path:
960 * @schema: a #GSettingsSchema
962 * Gets the path associated with @schema, or %NULL.
964 * Schemas may be single-instance or relocatable. Single-instance
965 * schemas correspond to exactly one set of keys in the backend
966 * database: those located at the path returned by this function.
968 * Relocatable schemas can be referenced by other schemas and can
969 * threfore describe multiple sets of keys at different locations. For
970 * relocatable schemas, this function will return %NULL.
972 * Returns: (transfer none): the path of the schema, or %NULL
977 g_settings_schema_get_path (GSettingsSchema *schema)
983 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
985 return schema->gettext_domain;
989 * g_settings_schema_has_key:
990 * @schema: a #GSettingsSchema
991 * @name: the name of a key
993 * Checks if @schema has a key named @name.
995 * Returns: %TRUE if such a key exists
1000 g_settings_schema_has_key (GSettingsSchema *schema,
1003 return gvdb_table_has_value (schema->table, key);
1007 g_settings_schema_list (GSettingsSchema *schema,
1010 if (schema->items == NULL)
1013 GHashTableIter iter;
1019 items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1021 for (s = schema; s; s = s->extends)
1025 list = gvdb_table_list (s->table, "");
1029 for (i = 0; list[i]; i++)
1030 g_hash_table_add (items, list[i]); /* transfer ownership */
1032 g_free (list); /* free container only */
1036 /* Do a first pass to eliminate child items that do not map to
1037 * valid schemas (ie: ones that would crash us if we actually
1038 * tried to create them).
1040 g_hash_table_iter_init (&iter, items);
1041 while (g_hash_table_iter_next (&iter, &name, NULL))
1042 if (g_str_has_suffix (name, "/"))
1044 GSettingsSchemaSource *source;
1045 GVariant *child_schema;
1046 GvdbTable *child_table;
1048 child_schema = gvdb_table_get_raw_value (schema->table, name);
1054 for (source = schema_sources; source; source = source->parent)
1055 if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1058 g_variant_unref (child_schema);
1060 /* Schema is not found -> remove it from the list */
1061 if (child_table == NULL)
1063 g_hash_table_iter_remove (&iter);
1067 /* Make sure the schema is relocatable or at the
1070 if (gvdb_table_has_value (child_table, ".path"))
1076 path = gvdb_table_get_raw_value (child_table, ".path");
1077 expected = g_strconcat (schema->path, name, NULL);
1078 same = g_str_equal (expected, g_variant_get_string (path, NULL));
1079 g_variant_unref (path);
1082 /* Schema is non-relocatable and did not have the
1083 * expected path -> remove it from the list
1086 g_hash_table_iter_remove (&iter);
1089 gvdb_table_unref (child_table);
1092 /* Now create the list */
1093 len = g_hash_table_size (items);
1094 schema->items = g_new (GQuark, len);
1096 g_hash_table_iter_init (&iter, items);
1098 while (g_hash_table_iter_next (&iter, &name, NULL))
1099 schema->items[i++] = g_quark_from_string (name);
1100 schema->n_items = i;
1101 g_assert (i == len);
1103 g_hash_table_unref (items);
1106 *n_items = schema->n_items;
1107 return schema->items;
1111 * g_settings_schema_get_id:
1112 * @schema: a #GSettingsSchema
1114 * Get the ID of @schema.
1116 * Returns: (transfer none): the ID
1119 g_settings_schema_get_id (GSettingsSchema *schema)
1125 endian_fixup (GVariant **value)
1127 #if G_BYTE_ORDER == G_BIG_ENDIAN
1130 tmp = g_variant_byteswap (*value);
1131 g_variant_unref (*value);
1137 g_settings_schema_key_init (GSettingsSchemaKey *key,
1138 GSettingsSchema *schema,
1145 memset (key, 0, sizeof *key);
1147 iter = g_settings_schema_get_value (schema, name);
1149 key->schema = g_settings_schema_ref (schema);
1150 key->default_value = g_variant_iter_next_value (iter);
1151 endian_fixup (&key->default_value);
1152 key->type = g_variant_get_type (key->default_value);
1153 key->name = g_intern_string (name);
1155 while (g_variant_iter_next (iter, "(y*)", &code, &data))
1160 /* translation requested */
1161 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1165 /* enumerated types... */
1166 key->is_enum = TRUE;
1171 key->is_flags = TRUE;
1175 /* ..., choices, aliases */
1176 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1180 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1181 endian_fixup (&key->minimum);
1182 endian_fixup (&key->maximum);
1186 g_warning ("unknown schema extension '%c'", code);
1190 g_variant_unref (data);
1193 g_variant_iter_free (iter);
1197 g_settings_schema_key_clear (GSettingsSchemaKey *key)
1200 g_variant_unref (key->minimum);
1203 g_variant_unref (key->maximum);
1205 g_variant_unref (key->default_value);
1207 g_settings_schema_unref (key->schema);
1211 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1214 g_return_val_if_fail (value != NULL, FALSE);
1216 return g_variant_is_of_type (value, key->type);
1220 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1223 const gchar *target;
1225 if (g_settings_schema_key_range_check (key, value))
1226 return g_variant_ref (value);
1228 if (key->strinfo == NULL)
1231 if (g_variant_is_container (value))
1233 GVariantBuilder builder;
1237 g_variant_iter_init (&iter, value);
1238 g_variant_builder_init (&builder, g_variant_get_type (value));
1240 while ((child = g_variant_iter_next_value (&iter)))
1244 fixed = g_settings_schema_key_range_fixup (key, child);
1245 g_variant_unref (child);
1249 g_variant_builder_clear (&builder);
1253 g_variant_builder_add_value (&builder, fixed);
1254 g_variant_unref (fixed);
1257 return g_variant_ref_sink (g_variant_builder_end (&builder));
1260 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1261 g_variant_get_string (value, NULL));
1262 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1266 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1268 const gchar *translated;
1269 GError *error = NULL;
1270 const gchar *domain;
1273 domain = g_settings_schema_get_gettext_domain (key->schema);
1275 if (key->lc_char == '\0')
1276 /* translation not requested for this key */
1279 if (key->lc_char == 't')
1280 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
1282 translated = g_dgettext (domain, key->unparsed);
1284 if (translated == key->unparsed)
1285 /* the default value was not translated */
1288 /* try to parse the translation of the unparsed default */
1289 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1293 g_warning ("Failed to parse translated string '%s' for "
1294 "key '%s' in schema '%s': %s", key->unparsed, key->name,
1295 g_settings_schema_get_id (key->schema), error->message);
1296 g_warning ("Using untranslated default instead.");
1297 g_error_free (error);
1300 else if (!g_settings_schema_key_range_check (key, value))
1302 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1303 "is outside of valid range", key->unparsed, key->name,
1304 g_settings_schema_get_id (key->schema));
1305 g_variant_unref (value);
1313 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1319 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1320 g_variant_get_string (value, NULL),
1323 /* 'value' can only come from the backend after being filtered for validity,
1324 * from the translation after being filtered for validity, or from the schema
1325 * itself (which the schema compiler checks for validity). If this assertion
1326 * fails then it's really a bug in GSettings or the schema compiler...
1328 g_assert (it_worked);
1334 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1337 const gchar *string;
1339 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1344 return g_variant_new_string (string);
1348 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1356 g_variant_iter_init (&iter, value);
1357 while (g_variant_iter_next (&iter, "&s", &flag))
1362 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1363 /* as in g_settings_to_enum() */
1364 g_assert (it_worked);
1366 result |= flag_value;
1373 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1376 GVariantBuilder builder;
1379 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1381 for (i = 0; i < 32; i++)
1382 if (value & (1u << i))
1384 const gchar *string;
1386 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1390 g_variant_builder_clear (&builder);
1394 g_variant_builder_add (&builder, "s", string);
1397 return g_variant_builder_end (&builder);
1400 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1403 * g_settings_schema_key_ref:
1404 * @key: a #GSettingsSchemaKey
1406 * Increase the reference count of @key, returning a new reference.
1408 * Returns: a new reference to @key
1412 GSettingsSchemaKey *
1413 g_settings_schema_key_ref (GSettingsSchemaKey *key)
1415 g_return_val_if_fail (key != NULL, NULL);
1417 g_atomic_int_inc (&key->ref_count);
1423 * g_settings_schema_key_unref:
1424 * @key: a #GSettingsSchemaKey
1426 * Decrease the reference count of @key, possibly freeing it.
1431 g_settings_schema_key_unref (GSettingsSchemaKey *key)
1433 g_return_if_fail (key != NULL);
1435 if (g_atomic_int_dec_and_test (&key->ref_count))
1437 g_settings_schema_key_clear (key);
1439 g_slice_free (GSettingsSchemaKey, key);
1444 * g_settings_schema_get_key:
1445 * @schema: a #GSettingsSchema
1446 * @name: the name of a key
1448 * Gets the key named @name from @schema.
1450 * It is a programmer error to request a key that does not exist. See
1451 * g_settings_schema_list_keys().
1453 * Returns: (transfer full): the #GSettingsSchemaKey for @name
1457 GSettingsSchemaKey *
1458 g_settings_schema_get_key (GSettingsSchema *schema,
1461 GSettingsSchemaKey *key;
1463 g_return_val_if_fail (schema != NULL, NULL);
1464 g_return_val_if_fail (name != NULL, NULL);
1466 key = g_slice_new (GSettingsSchemaKey);
1467 g_settings_schema_key_init (key, schema, name);
1474 * g_settings_schema_key_get_summary:
1475 * @key: a #GSettingsSchemaKey
1477 * Gets the summary for @key.
1479 * If no summary has been provided in the schema for @key, returns
1482 * The summary is a short description of the purpose of the key; usually
1483 * one short sentence. Summaries can be translated and the value
1484 * returned from this function is is the current locale.
1486 * This function is slow. The summary and description information for
1487 * the schemas is not stored in the compiled schema database so this
1488 * function has to parse all of the source XML files in the schema
1491 * Returns: the summary for @key, or %NULL
1496 g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1498 GHashTable **text_tables;
1499 GHashTable *summaries;
1501 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1502 summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1504 return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1508 * g_settings_schema_key_get_description:
1509 * @key: a #GSettingsSchemaKey
1511 * Gets the description for @key.
1513 * If no description has been provided in the schema for @key, returns
1516 * The description can be one sentence to several paragraphs in length.
1517 * Paragraphs are delimited with a double newline. Descriptions can be
1518 * translated and the value returned from this function is is the
1521 * This function is slow. The summary and description information for
1522 * the schemas is not stored in the compiled schema database so this
1523 * function has to parse all of the source XML files in the schema
1526 * Returns: the description for @key, or %NULL
1531 g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1533 GHashTable **text_tables;
1534 GHashTable *descriptions;
1536 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1537 descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1539 return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1543 * g_settings_schema_key_get_value_type:
1544 * @key: a #GSettingsSchemaKey
1546 * Gets the #GVariantType of @key.
1548 * Returns: (transfer none): the type of @key
1552 const GVariantType *
1553 g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1555 g_return_val_if_fail (key, NULL);
1561 * g_settings_schema_key_get_default_value:
1562 * @key: a #GSettingsSchemaKey
1564 * Gets the default value for @key.
1566 * Note that this is the default value according to the schema. System
1567 * administrator defaults and lockdown are not visible via this API.
1569 * Returns: (transfer full): the default value for the key
1574 g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1578 g_return_val_if_fail (key, NULL);
1580 value = g_settings_schema_key_get_translated_default (key);
1583 value = g_variant_ref (key->default_value);
1589 * g_settings_schema_key_get_range:
1590 * @key: a #GSettingsSchemaKey
1592 * Queries the range of a key.
1594 * This function will return a #GVariant that fully describes the range
1595 * of values that are valid for @key.
1597 * The type of #GVariant returned is `(sv)`. The string describes
1598 * the type of range restriction in effect. The type and meaning of
1599 * the value contained in the variant depends on the string.
1601 * If the string is `'type'` then the variant contains an empty array.
1602 * The element type of that empty array is the expected type of value
1603 * and all values of that type are valid.
1605 * If the string is `'enum'` then the variant contains an array
1606 * enumerating the possible values. Each item in the array is
1607 * a possible valid value and no other values are valid.
1609 * If the string is `'flags'` then the variant contains an array. Each
1610 * item in the array is a value that may appear zero or one times in an
1611 * array to be used as the value for this key. For example, if the
1612 * variant contained the array `['x', 'y']` then the valid values for
1613 * the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
1616 * Finally, if the string is `'range'` then the variant contains a pair
1617 * of like-typed values -- the minimum and maximum permissible values
1620 * This information should not be used by normal programs. It is
1621 * considered to be a hint for introspection purposes. Normal programs
1622 * should already know what is permitted by their own schema. The
1623 * format may change in any way in the future -- but particularly, new
1624 * forms may be added to the possibilities described above.
1626 * You should free the returned value with g_variant_unref() when it is
1629 * Returns: (transfer full): a #GVariant describing the range
1634 g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1641 range = g_variant_new ("(**)", key->minimum, key->maximum);
1644 else if (key->strinfo)
1646 range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1647 type = key->is_flags ? "flags" : "enum";
1651 range = g_variant_new_array (key->type, NULL, 0);
1655 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1659 * g_settings_schema_key_range_check:
1660 * @key: a #GSettingsSchemaKey
1661 * @value: the value to check
1663 * Checks if the given @value is of the correct type and within the
1664 * permitted range for @key.
1666 * It is a programmer error if @value is not of the correct type -- you
1667 * must check for this first.
1669 * Returns: %TRUE if @value is valid for @key
1674 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1677 if (key->minimum == NULL && key->strinfo == NULL)
1680 if (g_variant_is_container (value))
1686 g_variant_iter_init (&iter, value);
1687 while (ok && (child = g_variant_iter_next_value (&iter)))
1689 ok = g_settings_schema_key_range_check (key, child);
1690 g_variant_unref (child);
1698 return g_variant_compare (key->minimum, value) <= 0 &&
1699 g_variant_compare (value, key->maximum) <= 0;
1702 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1703 g_variant_get_string (value, NULL));