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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gsettingsschema-internal.h"
24 #include "gsettings.h"
26 #include "gvdb/gvdb-reader.h"
34 * SECTION:gsettingsschema
35 * @short_description: Introspecting and controlling the loading
36 * of GSettings schemas
39 * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
40 * mechanism for advanced control over the loading of schemas and a
41 * mechanism for introspecting their content.
43 * Plugin loading systems that wish to provide plugins a way to access
44 * settings face the problem of how to make the schemas for these
45 * settings visible to GSettings. Typically, a plugin will want to ship
46 * the schema along with itself and it won't be installed into the
47 * standard system directories for schemas.
49 * #GSettingsSchemaSource provides a mechanism for dealing with this by
50 * allowing the creation of a new 'schema source' from which schemas can
51 * be acquired. This schema source can then become part of the metadata
52 * associated with the plugin and queried whenever the plugin requires
53 * access to some settings.
55 * Consider the following example:
61 * GSettingsSchemaSource *schema_source;
66 * initialise_plugin (const gchar *dir)
72 * plugin->schema_source =
73 * g_settings_new_schema_source_from_directory (dir,
74 * g_settings_schema_source_get_default (), FALSE, NULL);
84 * plugin_get_settings (Plugin *plugin,
85 * const gchar *schema_id)
87 * GSettingsSchema *schema;
89 * if (schema_id == NULL)
90 * schema_id = plugin->identifier;
92 * schema = g_settings_schema_source_lookup (plugin->schema_source,
97 * ... disable the plugin or abort, etc ...
100 * return g_settings_new_full (schema, NULL, NULL);
104 * The code above shows how hooks should be added to the code that
105 * initialises (or enables) the plugin to create the schema source and
106 * how an API can be added to the plugin system to provide a convenient
107 * way for the plugin to access its settings, using the schemas that it
110 * From the standpoint of the plugin, it would need to ensure that it
111 * ships a gschemas.compiled file as part of itself, and then simply do
116 * GSettings *settings;
119 * settings = plugin_get_settings (self, NULL);
120 * some_value = g_settings_get_int (settings, "some-value");
125 * It's also possible that the plugin system expects the schema source
126 * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
127 * In that case, the plugin loading system must compile the schemas for
128 * itself before attempting to create the settings source.
136 * This is an opaque structure type. You may not access it directly.
140 struct _GSettingsSchema
142 GSettingsSchemaSource *source;
143 const gchar *gettext_domain;
150 GSettingsSchema *extends;
156 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
158 * A boxed #GType corresponding to #GSettingsSchemaSource.
162 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
165 * G_TYPE_SETTINGS_SCHEMA:
167 * A boxed #GType corresponding to #GSettingsSchema.
171 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
174 * GSettingsSchemaSource:
176 * This is an opaque structure type. You may not access it directly.
180 struct _GSettingsSchemaSource
182 GSettingsSchemaSource *parent;
185 GHashTable **text_tables;
190 static GSettingsSchemaSource *schema_sources;
193 * g_settings_schema_source_ref:
194 * @source: a #GSettingsSchemaSource
196 * Increase the reference count of @source, returning a new reference.
198 * Returns: a new reference to @source
202 GSettingsSchemaSource *
203 g_settings_schema_source_ref (GSettingsSchemaSource *source)
205 g_atomic_int_inc (&source->ref_count);
211 * g_settings_schema_source_unref:
212 * @source: a #GSettingsSchemaSource
214 * Decrease the reference count of @source, possibly freeing it.
219 g_settings_schema_source_unref (GSettingsSchemaSource *source)
221 if (g_atomic_int_dec_and_test (&source->ref_count))
223 if (source == schema_sources)
224 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
227 g_settings_schema_source_unref (source->parent);
228 gvdb_table_unref (source->table);
229 g_free (source->directory);
231 if (source->text_tables)
233 g_hash_table_unref (source->text_tables[0]);
234 g_hash_table_unref (source->text_tables[1]);
235 g_free (source->text_tables);
238 g_slice_free (GSettingsSchemaSource, source);
243 * g_settings_schema_source_new_from_directory:
244 * @directory: the filename of a directory
245 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
246 * @trusted: %TRUE, if the directory is trusted
247 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
249 * Attempts to create a new schema source corresponding to the contents
250 * of the given directory.
252 * This function is not required for normal uses of #GSettings but it
253 * may be useful to authors of plugin management systems.
255 * The directory should contain a file called
256 * <filename>gschemas.compiled</filename> as produced by
257 * <command>glib-compile-schemas</command>.
259 * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
260 * trusted not to be corrupted. This assumption has a performance
261 * advantage, but can result in crashes or inconsistent behaviour in the
262 * case of a corrupted file. Generally, you should set @trusted to
263 * %TRUE for files installed by the system and to %FALSE for files in
264 * the home directory.
266 * If @parent is non-%NULL then there are two effects.
268 * First, if g_settings_schema_source_lookup() is called with the
269 * @recursive flag set to %TRUE and the schema can not be found in the
270 * source, the lookup will recurse to the parent.
272 * Second, any references to other schemas specified within this
273 * source (ie: <literal>child</literal> or <literal>extends</literal>)
274 * references may be resolved from the @parent.
276 * For this second reason, except in very unusual situations, the
277 * @parent should probably be given as the default schema source, as
278 * returned by g_settings_schema_source_get_default().
282 GSettingsSchemaSource *
283 g_settings_schema_source_new_from_directory (const gchar *directory,
284 GSettingsSchemaSource *parent,
288 GSettingsSchemaSource *source;
292 filename = g_build_filename (directory, "gschemas.compiled", NULL);
293 table = gvdb_table_new (filename, trusted, error);
299 source = g_slice_new (GSettingsSchemaSource);
300 source->directory = g_strdup (directory);
301 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
302 source->text_tables = NULL;
303 source->table = table;
304 source->ref_count = 1;
310 try_prepend_dir (const gchar *directory)
312 GSettingsSchemaSource *source;
314 source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
316 /* If we successfully created it then prepend it to the global list */
318 schema_sources = source;
322 initialise_schema_sources (void)
324 static gsize initialised;
326 /* need a separate variable because 'schema_sources' may legitimately
327 * be null if we have zero valid schema sources
329 if G_UNLIKELY (g_once_init_enter (&initialised))
331 const gchar * const *dirs;
335 /* iterate in reverse: count up, then count down */
336 dirs = g_get_system_data_dirs ();
337 for (i = 0; dirs[i]; i++);
343 dirname = g_build_filename (dirs[i], "glib-2.0", "schemas", NULL);
344 try_prepend_dir (dirname);
348 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
349 try_prepend_dir (path);
351 g_once_init_leave (&initialised, TRUE);
356 * g_settings_schema_source_get_default:
358 * Gets the default system schema source.
360 * This function is not required for normal uses of #GSettings but it
361 * may be useful to authors of plugin management systems or to those who
362 * want to introspect the content of schemas.
364 * If no schemas are installed, %NULL will be returned.
366 * The returned source may actually consist of multiple schema sources
367 * from different directories, depending on which directories were given
368 * in <envar>XDG_DATA_DIRS</envar> and
369 * <envar>GSETTINGS_SCHEMA_DIR</envar>. For this reason, all lookups
370 * performed against the default source should probably be done
373 * Returns: (transfer none): the default schema source
377 GSettingsSchemaSource *
378 g_settings_schema_source_get_default (void)
380 initialise_schema_sources ();
382 return schema_sources;
386 * g_settings_schema_source_lookup:
387 * @source: a #GSettingsSchemaSource
388 * @schema_id: a schema ID
389 * @recursive: %TRUE if the lookup should be recursive
391 * Looks up a schema with the identifier @schema_id in @source.
393 * This function is not required for normal uses of #GSettings but it
394 * may be useful to authors of plugin management systems or to those who
395 * want to introspect the content of schemas.
397 * If the schema isn't found directly in @source and @recursive is %TRUE
398 * then the parent sources will also be checked.
400 * If the schema isn't found, %NULL is returned.
402 * Returns: (transfer full): a new #GSettingsSchema
407 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
408 const gchar *schema_id,
411 GSettingsSchema *schema;
413 const gchar *extends;
415 g_return_val_if_fail (source != NULL, NULL);
416 g_return_val_if_fail (schema_id != NULL, NULL);
418 table = gvdb_table_get_table (source->table, schema_id);
420 if (table == NULL && recursive)
421 for (source = source->parent; source; source = source->parent)
422 if ((table = gvdb_table_get_table (source->table, schema_id)))
428 schema = g_slice_new0 (GSettingsSchema);
429 schema->source = g_settings_schema_source_ref (source);
430 schema->ref_count = 1;
431 schema->id = g_strdup (schema_id);
432 schema->table = table;
433 schema->path = g_settings_schema_get_string (schema, ".path");
434 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
436 if (schema->gettext_domain)
437 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
439 extends = g_settings_schema_get_string (schema, ".extends");
442 schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
443 if (schema->extends == NULL)
444 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
452 GHashTable *summaries;
453 GHashTable *descriptions;
454 GSList *gettext_domain;
458 } TextTableParseInfo;
461 get_attribute_value (GSList *list)
465 for (node = list; node; node = node->next)
473 pop_attribute_value (GSList **list)
478 *list = g_slist_remove (*list, top);
484 push_attribute_value (GSList **list,
487 *list = g_slist_prepend (*list, g_strdup (value));
491 start_element (GMarkupParseContext *context,
492 const gchar *element_name,
493 const gchar **attribute_names,
494 const gchar **attribute_values,
498 TextTableParseInfo *info = user_data;
499 const gchar *gettext_domain = NULL;
500 const gchar *schema_id = NULL;
501 const gchar *key_name = NULL;
504 for (i = 0; attribute_names[i]; i++)
506 if (g_str_equal (attribute_names[i], "gettext-domain"))
507 gettext_domain = attribute_values[i];
508 else if (g_str_equal (attribute_names[i], "id"))
509 schema_id = attribute_values[i];
510 else if (g_str_equal (attribute_names[i], "name"))
511 key_name = attribute_values[i];
514 push_attribute_value (&info->gettext_domain, gettext_domain);
515 push_attribute_value (&info->schema_id, schema_id);
516 push_attribute_value (&info->key_name, key_name);
520 g_string_free (info->string, TRUE);
524 if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
525 info->string = g_string_new (NULL);
529 normalise_whitespace (const gchar *orig)
531 /* We normalise by the same rules as in intltool:
540 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
542 * Where \s is an ascii space character.
544 * We aim for ease of implementation over efficiency -- this code is
545 * not run in normal applications.
547 static GRegex *cleanup[3];
548 static GRegex *splitter;
553 if (g_once_init_enter (&splitter))
557 cleanup[0] = g_regex_new ("^\\s+", 0, 0, 0);
558 cleanup[1] = g_regex_new ("\\s+$", 0, 0, 0);
559 cleanup[2] = g_regex_new ("\\s+", 0, 0, 0);
560 s = g_regex_new ("\\n\\s*\\n+", 0, 0, 0);
562 g_once_init_leave (&splitter, s);
565 lines = g_regex_split (splitter, orig, 0);
566 for (i = 0; lines[i]; i++)
570 a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
571 b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
572 c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
579 result = g_strjoinv ("\n\n", lines);
586 end_element (GMarkupParseContext *context,
587 const gchar *element_name,
591 TextTableParseInfo *info = user_data;
593 pop_attribute_value (&info->gettext_domain);
594 pop_attribute_value (&info->schema_id);
595 pop_attribute_value (&info->key_name);
599 GHashTable *source_table = NULL;
600 const gchar *gettext_domain;
601 const gchar *schema_id;
602 const gchar *key_name;
604 gettext_domain = get_attribute_value (info->gettext_domain);
605 schema_id = get_attribute_value (info->schema_id);
606 key_name = get_attribute_value (info->key_name);
608 if (g_str_equal (element_name, "summary"))
609 source_table = info->summaries;
610 else if (g_str_equal (element_name, "description"))
611 source_table = info->descriptions;
613 if (source_table && schema_id && key_name)
615 GHashTable *schema_table;
618 schema_table = g_hash_table_lookup (source_table, schema_id);
620 if (schema_table == NULL)
622 schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
623 g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
626 normalised = normalise_whitespace (info->string->str);
632 translated = g_strdup (g_dgettext (gettext_domain, normalised));
634 normalised = translated;
637 g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
640 g_string_free (info->string, TRUE);
646 text (GMarkupParseContext *context,
652 TextTableParseInfo *info = user_data;
655 g_string_append_len (info->string, text, text_len);
659 parse_into_text_tables (const gchar *directory,
660 GHashTable *summaries,
661 GHashTable *descriptions)
663 GMarkupParser parser = { start_element, end_element, text };
664 TextTableParseInfo info = { summaries, descriptions };
665 const gchar *basename;
668 dir = g_dir_open (directory, 0, NULL);
669 while ((basename = g_dir_read_name (dir)))
675 filename = g_build_filename (directory, basename, NULL);
676 if (g_file_get_contents (filename, &contents, &size, NULL))
678 GMarkupParseContext *context;
680 context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
681 if (g_markup_parse_context_parse (context, contents, size, NULL))
682 g_markup_parse_context_end_parse (context, NULL);
683 g_markup_parse_context_free (context);
685 /* Clean up dangling stuff in case there was an error. */
686 g_slist_free_full (info.gettext_domain, g_free);
687 g_slist_free_full (info.schema_id, g_free);
688 g_slist_free_full (info.key_name, g_free);
690 info.gettext_domain = NULL;
691 info.schema_id = NULL;
692 info.key_name = NULL;
696 g_string_free (info.string, TRUE);
708 g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
710 if (g_once_init_enter (&source->text_tables))
712 GHashTable **text_tables;
714 text_tables = g_new (GHashTable *, 2);
715 text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
716 text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
718 if (source->directory)
719 parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
721 g_once_init_leave (&source->text_tables, text_tables);
724 return source->text_tables;
728 * g_settings_schema_source_list_schemas:
729 * @source: a #GSettingsSchemaSource
730 * @recursive: if we should recurse
731 * @non_relocatable: (out) (transfer full): the list of non-relocatable
733 * @relocatable: (out) (transfer full): the list of relocatable schemas
735 * Lists the schemas in a given source.
737 * If @recursive is %TRUE then include parent sources. If %FALSE then
738 * only include the schemas from one source (ie: one directory). You
739 * probably want %TRUE.
741 * Non-relocatable schemas are those for which you can call
742 * g_settings_new(). Relocatable schemas are those for which you must
743 * use g_settings_new_with_path().
745 * Do not call this function from normal programs. This is designed for
746 * use by database editors, commandline tools, etc.
751 g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
753 gchar ***non_relocatable,
754 gchar ***relocatable)
756 GHashTable *single, *reloc;
757 GSettingsSchemaSource *s;
759 /* We use hash tables to avoid duplicate listings for schemas that
760 * appear in more than one file.
762 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
763 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
765 for (s = source; s; s = s->parent)
770 list = gvdb_table_list (s->table, "");
772 /* empty schema cache file? */
776 for (i = 0; list[i]; i++)
778 if (!g_hash_table_lookup (single, list[i]) &&
779 !g_hash_table_lookup (reloc, list[i]))
783 table = gvdb_table_get_table (s->table, list[i]);
784 g_assert (table != NULL);
786 if (gvdb_table_has_value (table, ".path"))
787 g_hash_table_insert (single, g_strdup (list[i]), NULL);
789 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
791 gvdb_table_unref (table);
797 /* Only the first source if recursive not requested */
804 *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
805 g_hash_table_steal_all (single);
810 *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
811 g_hash_table_steal_all (reloc);
814 g_hash_table_unref (single);
815 g_hash_table_unref (reloc);
818 static gchar **non_relocatable_schema_list;
819 static gchar **relocatable_schema_list;
820 static gsize schema_lists_initialised;
823 ensure_schema_lists (void)
825 if (g_once_init_enter (&schema_lists_initialised))
827 initialise_schema_sources ();
829 g_settings_schema_source_list_schemas (schema_sources, TRUE,
830 &non_relocatable_schema_list,
831 &relocatable_schema_list);
833 g_once_init_leave (&schema_lists_initialised, TRUE);
838 * g_settings_list_schemas:
840 * Returns: (element-type utf8) (transfer none): a list of #GSettings
841 * schemas that are available. The list must not be modified or
846 * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead.
847 * If you used g_settings_list_schemas() to check for the presence of
848 * a particular schema, use g_settings_schema_source_lookup() instead
849 * of your whole loop.
851 const gchar * const *
852 g_settings_list_schemas (void)
854 ensure_schema_lists ();
856 return (const gchar **) non_relocatable_schema_list;
860 * g_settings_list_relocatable_schemas:
862 * Returns: (element-type utf8) (transfer none): a list of relocatable
863 * #GSettings schemas that are available. The list must not be
868 * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
870 const gchar * const *
871 g_settings_list_relocatable_schemas (void)
873 ensure_schema_lists ();
875 return (const gchar **) relocatable_schema_list;
879 * g_settings_schema_ref:
880 * @schema: a #GSettingsSchema
882 * Increase the reference count of @schema, returning a new reference.
884 * Returns: a new reference to @schema
889 g_settings_schema_ref (GSettingsSchema *schema)
891 g_atomic_int_inc (&schema->ref_count);
897 * g_settings_schema_unref:
898 * @schema: a #GSettingsSchema
900 * Decrease the reference count of @schema, possibly freeing it.
905 g_settings_schema_unref (GSettingsSchema *schema)
907 if (g_atomic_int_dec_and_test (&schema->ref_count))
910 g_settings_schema_unref (schema->extends);
912 g_settings_schema_source_unref (schema->source);
913 gvdb_table_unref (schema->table);
914 g_free (schema->items);
917 g_slice_free (GSettingsSchema, schema);
922 g_settings_schema_get_string (GSettingsSchema *schema,
925 const gchar *result = NULL;
928 if ((value = gvdb_table_get_raw_value (schema->table, key)))
930 result = g_variant_get_string (value, NULL);
931 g_variant_unref (value);
938 g_settings_schema_get_value (GSettingsSchema *schema,
941 GSettingsSchema *s = schema;
945 g_return_val_if_fail (schema != NULL, NULL);
947 for (s = schema; s; s = s->extends)
948 if ((value = gvdb_table_get_raw_value (s->table, key)))
951 if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
952 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
954 iter = g_variant_iter_new (value);
955 g_variant_unref (value);
961 * g_settings_schema_get_path:
962 * @schema: a #GSettingsSchema
964 * Gets the path associated with @schema, or %NULL.
966 * Schemas may be single-instance or relocatable. Single-instance
967 * schemas correspond to exactly one set of keys in the backend
968 * database: those located at the path returned by this function.
970 * Relocatable schemas can be referenced by other schemas and can
971 * threfore describe multiple sets of keys at different locations. For
972 * relocatable schemas, this function will return %NULL.
974 * Returns: (transfer none): the path of the schema, or %NULL
979 g_settings_schema_get_path (GSettingsSchema *schema)
985 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
987 return schema->gettext_domain;
991 * g_settings_schema_has_key:
992 * @schema: a #GSettingsSchema
993 * @name: the name of a key
995 * Checks if @schema has a key named @name.
997 * Returns: %TRUE if such a key exists
1002 g_settings_schema_has_key (GSettingsSchema *schema,
1005 return gvdb_table_has_value (schema->table, key);
1009 g_settings_schema_list (GSettingsSchema *schema,
1012 if (schema->items == NULL)
1015 GHashTableIter iter;
1021 items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1023 for (s = schema; s; s = s->extends)
1027 list = gvdb_table_list (s->table, "");
1031 for (i = 0; list[i]; i++)
1032 g_hash_table_add (items, list[i]); /* transfer ownership */
1034 g_free (list); /* free container only */
1038 /* Do a first pass to eliminate child items that do not map to
1039 * valid schemas (ie: ones that would crash us if we actually
1040 * tried to create them).
1042 g_hash_table_iter_init (&iter, items);
1043 while (g_hash_table_iter_next (&iter, &name, NULL))
1044 if (g_str_has_suffix (name, "/"))
1046 GSettingsSchemaSource *source;
1047 GVariant *child_schema;
1048 GvdbTable *child_table;
1050 child_schema = gvdb_table_get_raw_value (schema->table, name);
1056 for (source = schema_sources; source; source = source->parent)
1057 if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1060 g_variant_unref (child_schema);
1062 /* Schema is not found -> remove it from the list */
1063 if (child_table == NULL)
1065 g_hash_table_iter_remove (&iter);
1069 /* Make sure the schema is relocatable or at the
1072 if (gvdb_table_has_value (child_table, ".path"))
1078 path = gvdb_table_get_raw_value (child_table, ".path");
1079 expected = g_strconcat (schema->path, name, NULL);
1080 same = g_str_equal (expected, g_variant_get_string (path, NULL));
1081 g_variant_unref (path);
1084 /* Schema is non-relocatable and did not have the
1085 * expected path -> remove it from the list
1088 g_hash_table_iter_remove (&iter);
1091 gvdb_table_unref (child_table);
1094 /* Now create the list */
1095 len = g_hash_table_size (items);
1096 schema->items = g_new (GQuark, len);
1098 g_hash_table_iter_init (&iter, items);
1100 while (g_hash_table_iter_next (&iter, &name, NULL))
1101 schema->items[i++] = g_quark_from_string (name);
1102 schema->n_items = i;
1103 g_assert (i == len);
1105 g_hash_table_unref (items);
1108 *n_items = schema->n_items;
1109 return schema->items;
1113 * g_settings_schema_get_id:
1114 * @schema: a #GSettingsSchema
1116 * Get the ID of @schema.
1118 * Returns: (transfer none): the ID
1121 g_settings_schema_get_id (GSettingsSchema *schema)
1127 endian_fixup (GVariant **value)
1129 #if G_BYTE_ORDER == G_BIG_ENDIAN
1132 tmp = g_variant_byteswap (*value);
1133 g_variant_unref (*value);
1139 g_settings_schema_key_init (GSettingsSchemaKey *key,
1140 GSettingsSchema *schema,
1147 memset (key, 0, sizeof *key);
1149 iter = g_settings_schema_get_value (schema, name);
1151 key->schema = g_settings_schema_ref (schema);
1152 key->default_value = g_variant_iter_next_value (iter);
1153 endian_fixup (&key->default_value);
1154 key->type = g_variant_get_type (key->default_value);
1155 key->name = g_intern_string (name);
1157 while (g_variant_iter_next (iter, "(y*)", &code, &data))
1162 /* translation requested */
1163 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1167 /* enumerated types... */
1168 key->is_enum = TRUE;
1173 key->is_flags = TRUE;
1177 /* ..., choices, aliases */
1178 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1182 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1183 endian_fixup (&key->minimum);
1184 endian_fixup (&key->maximum);
1188 g_warning ("unknown schema extension '%c'", code);
1192 g_variant_unref (data);
1195 g_variant_iter_free (iter);
1199 g_settings_schema_key_clear (GSettingsSchemaKey *key)
1202 g_variant_unref (key->minimum);
1205 g_variant_unref (key->maximum);
1207 g_variant_unref (key->default_value);
1209 g_settings_schema_unref (key->schema);
1213 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1216 g_return_val_if_fail (value != NULL, FALSE);
1218 return g_variant_is_of_type (value, key->type);
1222 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1225 const gchar *target;
1227 if (g_settings_schema_key_range_check (key, value))
1228 return g_variant_ref (value);
1230 if (key->strinfo == NULL)
1233 if (g_variant_is_container (value))
1235 GVariantBuilder builder;
1239 g_variant_iter_init (&iter, value);
1240 g_variant_builder_init (&builder, g_variant_get_type (value));
1242 while ((child = g_variant_iter_next_value (&iter)))
1246 fixed = g_settings_schema_key_range_fixup (key, child);
1247 g_variant_unref (child);
1251 g_variant_builder_clear (&builder);
1255 g_variant_builder_add_value (&builder, fixed);
1256 g_variant_unref (fixed);
1259 return g_variant_ref_sink (g_variant_builder_end (&builder));
1262 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1263 g_variant_get_string (value, NULL));
1264 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1268 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1270 const gchar *translated;
1271 GError *error = NULL;
1272 const gchar *domain;
1275 domain = g_settings_schema_get_gettext_domain (key->schema);
1277 if (key->lc_char == '\0')
1278 /* translation not requested for this key */
1281 if (key->lc_char == 't')
1282 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
1284 translated = g_dgettext (domain, key->unparsed);
1286 if (translated == key->unparsed)
1287 /* the default value was not translated */
1290 /* try to parse the translation of the unparsed default */
1291 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1295 g_warning ("Failed to parse translated string '%s' for "
1296 "key '%s' in schema '%s': %s", key->unparsed, key->name,
1297 g_settings_schema_get_id (key->schema), error->message);
1298 g_warning ("Using untranslated default instead.");
1299 g_error_free (error);
1302 else if (!g_settings_schema_key_range_check (key, value))
1304 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1305 "is outside of valid range", key->unparsed, key->name,
1306 g_settings_schema_get_id (key->schema));
1307 g_variant_unref (value);
1315 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1321 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1322 g_variant_get_string (value, NULL),
1325 /* 'value' can only come from the backend after being filtered for validity,
1326 * from the translation after being filtered for validity, or from the schema
1327 * itself (which the schema compiler checks for validity). If this assertion
1328 * fails then it's really a bug in GSettings or the schema compiler...
1330 g_assert (it_worked);
1336 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1339 const gchar *string;
1341 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1346 return g_variant_new_string (string);
1350 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1358 g_variant_iter_init (&iter, value);
1359 while (g_variant_iter_next (&iter, "&s", &flag))
1364 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1365 /* as in g_settings_to_enum() */
1366 g_assert (it_worked);
1368 result |= flag_value;
1375 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1378 GVariantBuilder builder;
1381 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1383 for (i = 0; i < 32; i++)
1384 if (value & (1u << i))
1386 const gchar *string;
1388 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1392 g_variant_builder_clear (&builder);
1396 g_variant_builder_add (&builder, "s", string);
1399 return g_variant_builder_end (&builder);
1402 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1405 * g_settings_schema_key_ref:
1406 * @key: a #GSettingsSchemaKey
1408 * Increase the reference count of @key, returning a new reference.
1410 * Returns: a new reference to @key
1414 GSettingsSchemaKey *
1415 g_settings_schema_key_ref (GSettingsSchemaKey *key)
1417 g_return_val_if_fail (key != NULL, NULL);
1419 g_atomic_int_inc (&key->ref_count);
1425 * g_settings_schema_key_unref:
1426 * @key: a #GSettingsSchemaKey
1428 * Decrease the reference count of @key, possibly freeing it.
1433 g_settings_schema_key_unref (GSettingsSchemaKey *key)
1435 g_return_if_fail (key != NULL);
1437 if (g_atomic_int_dec_and_test (&key->ref_count))
1439 g_settings_schema_key_clear (key);
1441 g_slice_free (GSettingsSchemaKey, key);
1446 * g_settings_schema_get_key:
1447 * @schema: a #GSettingsSchema
1448 * @name: the name of a key
1450 * Gets the key named @name from @schema.
1452 * It is a programmer error to request a key that does not exist. See
1453 * g_settings_schema_list_keys().
1455 * Returns: (transfer full): the #GSettingsSchemaKey for @name
1459 GSettingsSchemaKey *
1460 g_settings_schema_get_key (GSettingsSchema *schema,
1463 GSettingsSchemaKey *key;
1465 g_return_val_if_fail (schema != NULL, NULL);
1466 g_return_val_if_fail (name != NULL, NULL);
1468 key = g_slice_new (GSettingsSchemaKey);
1469 g_settings_schema_key_init (key, schema, name);
1476 * g_settings_schema_key_get_summary:
1477 * @key: a #GSettingsSchemaKey
1479 * Gets the summary for @key.
1481 * If no summary has been provided in the schema for @key, returns
1484 * The summary is a short description of the purpose of the key; usually
1485 * one short sentence. Summaries can be translated and the value
1486 * returned from this function is is the current locale.
1488 * This function is slow. The summary and description information for
1489 * the schemas is not stored in the compiled schema database so this
1490 * function has to parse all of the source XML files in the schema
1493 * Returns: the summary for @key, or %NULL
1498 g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1500 GHashTable **text_tables;
1501 GHashTable *summaries;
1503 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1504 summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1506 return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1510 * g_settings_schema_key_get_description:
1511 * @key: a #GSettingsSchemaKey
1513 * Gets the description for @key.
1515 * If no description has been provided in the schema for @key, returns
1518 * The description can be one sentence to several paragraphs in length.
1519 * Paragraphs are delimited with a double newline. Descriptions can be
1520 * translated and the value returned from this function is is the
1523 * This function is slow. The summary and description information for
1524 * the schemas is not stored in the compiled schema database so this
1525 * function has to parse all of the source XML files in the schema
1528 * Returns: the description for @key, or %NULL
1533 g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1535 GHashTable **text_tables;
1536 GHashTable *descriptions;
1538 text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1539 descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1541 return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1545 * g_settings_schema_key_get_value_type:
1546 * @key: a #GSettingsSchemaKey
1548 * Gets the #GVariantType of @key.
1550 * Returns: (transfer none): the type of @key
1554 const GVariantType *
1555 g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1557 g_return_val_if_fail (key, NULL);
1563 * g_settings_schema_key_get_default_value:
1564 * @key: a #GSettingsSchemaKey
1566 * Gets the default value for @key.
1568 * Note that this is the default value according to the schema. System
1569 * administrator defaults and lockdown are not visible via this API.
1571 * Returns: (transfer full): the default value for the key
1576 g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1580 g_return_val_if_fail (key, NULL);
1582 value = g_settings_schema_key_get_translated_default (key);
1585 value = g_variant_ref (key->default_value);
1591 * g_settings_schema_key_get_range:
1592 * @key: a #GSettingsSchemaKey
1594 * Queries the range of a key.
1596 * This function will return a #GVariant that fully describes the range
1597 * of values that are valid for @key.
1599 * The type of #GVariant returned is <literal>(sv)</literal>. The
1600 * string describes the type of range restriction in effect. The type
1601 * and meaning of the value contained in the variant depends on the
1604 * If the string is <literal>'type'</literal> then the variant contains
1605 * an empty array. The element type of that empty array is the expected
1606 * type of value and all values of that type are valid.
1608 * If the string is <literal>'enum'</literal> then the variant contains
1609 * an array enumerating the possible values. Each item in the array is
1610 * a possible valid value and no other values are valid.
1612 * If the string is <literal>'flags'</literal> then the variant contains
1613 * an array. Each item in the array is a value that may appear zero or
1614 * one times in an array to be used as the value for this key. For
1615 * example, if the variant contained the array <literal>['x',
1616 * 'y']</literal> then the valid values for the key would be
1617 * <literal>[]</literal>, <literal>['x']</literal>,
1618 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
1619 * <literal>['y', 'x']</literal>.
1621 * Finally, if the string is <literal>'range'</literal> then the variant
1622 * contains a pair of like-typed values -- the minimum and maximum
1623 * permissible values for this key.
1625 * This information should not be used by normal programs. It is
1626 * considered to be a hint for introspection purposes. Normal programs
1627 * should already know what is permitted by their own schema. The
1628 * format may change in any way in the future -- but particularly, new
1629 * forms may be added to the possibilities described above.
1631 * You should free the returned value with g_variant_unref() when it is
1634 * Returns: (transfer full): a #GVariant describing the range
1639 g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1646 range = g_variant_new ("(**)", key->minimum, key->maximum);
1649 else if (key->strinfo)
1651 range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1652 type = key->is_flags ? "flags" : "enum";
1656 range = g_variant_new_array (key->type, NULL, 0);
1660 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1664 * g_settings_schema_key_range_check:
1665 * @key: a #GSettingsSchemaKey
1666 * @value: the value to check
1668 * Checks if the given @value is of the correct type and within the
1669 * permitted range for @key.
1671 * It is a programmer error if @value is not of the correct type -- you
1672 * must check for this first.
1674 * Returns: %TRUE if @value is valid for @key
1679 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1682 if (key->minimum == NULL && key->strinfo == NULL)
1685 if (g_variant_is_container (value))
1691 g_variant_iter_init (&iter, value);
1692 while (ok && (child = g_variant_iter_next_value (&iter)))
1694 ok = g_settings_schema_key_range_check (key, child);
1695 g_variant_unref (child);
1703 return g_variant_compare (key->minimum, value) <= 0 &&
1704 g_variant_compare (value, key->maximum) <= 0;
1707 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1708 g_variant_get_string (value, NULL));