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 of
38 * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
39 * mechanism for advanced control over the loading of schemas and a
40 * mechanism for introspecting their content.
42 * Plugin loading systems that wish to provide plugins a way to access
43 * settings face the problem of how to make the schemas for these
44 * settings visible to GSettings. Typically, a plugin will want to ship
45 * the schema along with itself and it won't be installed into the
46 * standard system directories for schemas.
48 * #GSettingsSchemaSource provides a mechanism for dealing with this by
49 * allowing the creation of a new 'schema source' from which schemas can
50 * be acquired. This schema source can then become part of the metadata
51 * associated with the plugin and queried whenever the plugin requires
52 * access to some settings.
54 * Consider the following example:
60 * GSettingsSchemaSource *schema_source;
65 * initialise_plugin (const gchar *dir)
71 * plugin->schema_source =
72 * g_settings_new_schema_source_from_directory (dir,
73 * g_settings_schema_source_get_default (), FALSE, NULL);
83 * plugin_get_settings (Plugin *plugin,
84 * const gchar *schema_id)
86 * GSettingsSchema *schema;
88 * if (schema_id == NULL)
89 * schema_id = plugin->identifier;
91 * schema = g_settings_schema_source_lookup (plugin->schema_source,
96 * ... disable the plugin or abort, etc ...
99 * return g_settings_new_full (schema, NULL, NULL);
103 * The code above shows how hooks should be added to the code that
104 * initialises (or enables) the plugin to create the schema source and
105 * how an API can be added to the plugin system to provide a convenient
106 * way for the plugin to access its settings, using the schemas that it
109 * From the standpoint of the plugin, it would need to ensure that it
110 * ships a gschemas.compiled file as part of itself, and then simply do
115 * GSettings *settings;
118 * settings = plugin_get_settings (self, NULL);
119 * some_value = g_settings_get_int (settings, "some-value");
124 * It's also possible that the plugin system expects the schema source
125 * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
126 * In that case, the plugin loading system must compile the schemas for
127 * itself before attempting to create the settings source.
135 * This is an opaque structure type. You may not access it directly.
139 struct _GSettingsSchema
141 const gchar *gettext_domain;
152 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
154 * A boxed #GType corresponding to #GSettingsSchemaSource.
158 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
161 * G_TYPE_SETTINGS_SCHEMA:
163 * A boxed #GType corresponding to #GSettingsSchema.
167 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
170 * GSettingsSchemaSource:
172 * This is an opaque structure type. You may not access it directly.
176 struct _GSettingsSchemaSource
178 GSettingsSchemaSource *parent;
185 static GSettingsSchemaSource *schema_sources;
188 * g_settings_schema_source_ref:
189 * @source: a #GSettingsSchemaSource
191 * Increase the reference count of @source, returning a new reference.
193 * Returns: a new reference to @source
197 GSettingsSchemaSource *
198 g_settings_schema_source_ref (GSettingsSchemaSource *source)
200 g_atomic_int_inc (&source->ref_count);
206 * g_settings_schema_source_unref:
207 * @source: a #GSettingsSchemaSource
209 * Decrease the reference count of @source, possibly freeing it.
214 g_settings_schema_source_unref (GSettingsSchemaSource *source)
216 if (g_atomic_int_dec_and_test (&source->ref_count))
218 if (source == schema_sources)
219 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
222 g_settings_schema_source_unref (source->parent);
223 gvdb_table_unref (source->table);
224 g_free (source->directory);
226 g_slice_free (GSettingsSchemaSource, source);
231 * g_settings_schema_source_new_from_directory:
232 * @directory: the filename of a directory
233 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
234 * @trusted: %TRUE, if the directory is trusted
235 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
237 * Attempts to create a new schema source corresponding to the contents
238 * of the given directory.
240 * This function is not required for normal uses of #GSettings but it
241 * may be useful to authors of plugin management systems.
243 * The directory should contain a file called
244 * <filename>gschemas.compiled</filename> as produced by
245 * <command>glib-compile-schemas</command>.
247 * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
248 * trusted not to be corrupted. This assumption has a performance
249 * advantage, but can result in crashes or inconsistent behaviour in the
250 * case of a corrupted file. Generally, you should set @trusted to
251 * %TRUE for files installed by the system and to %FALSE for files in
252 * the home directory.
254 * If @parent is non-%NULL then there are two effects.
256 * First, if g_settings_schema_source_lookup() is called with the
257 * @recursive flag set to %TRUE and the schema can not be found in the
258 * source, the lookup will recurse to the parent.
260 * Second, any references to other schemas specified within this
261 * source (ie: <literal>child</literal> or <literal>extends</literal>)
262 * references may be resolved from the @parent.
264 * For this second reason, except in very unusual situations, the
265 * @parent should probably be given as the default schema source, as
266 * returned by g_settings_schema_source_get_default().
270 GSettingsSchemaSource *
271 g_settings_schema_source_new_from_directory (const gchar *directory,
272 GSettingsSchemaSource *parent,
276 GSettingsSchemaSource *source;
280 filename = g_build_filename (directory, "gschemas.compiled", NULL);
281 table = gvdb_table_new (filename, trusted, error);
287 source = g_slice_new (GSettingsSchemaSource);
288 source->directory = g_strdup (directory);
289 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
290 source->table = table;
291 source->ref_count = 1;
297 try_prepend_dir (const gchar *directory)
299 GSettingsSchemaSource *source;
301 source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
303 /* If we successfully created it then prepend it to the global list */
305 schema_sources = source;
309 initialise_schema_sources (void)
311 static gsize initialised;
313 /* need a separate variable because 'schema_sources' may legitimately
314 * be null if we have zero valid schema sources
316 if G_UNLIKELY (g_once_init_enter (&initialised))
318 const gchar * const *dirs;
322 /* iterate in reverse: count up, then count down */
323 dirs = g_get_system_data_dirs ();
324 for (i = 0; dirs[i]; i++);
330 dirname = g_build_filename (dirs[i], "glib-2.0", "schemas", NULL);
331 try_prepend_dir (dirname);
335 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
336 try_prepend_dir (path);
338 g_once_init_leave (&initialised, TRUE);
343 * g_settings_schema_source_get_default:
345 * Gets the default system schema source.
347 * This function is not required for normal uses of #GSettings but it
348 * may be useful to authors of plugin management systems or to those who
349 * want to introspect the content of schemas.
351 * If no schemas are installed, %NULL will be returned.
353 * The returned source may actually consist of multiple schema sources
354 * from different directories, depending on which directories were given
355 * in <envar>XDG_DATA_DIRS</envar> and
356 * <envar>GSETTINGS_SCHEMA_DIR</envar>. For this reason, all lookups
357 * performed against the default source should probably be done
360 * Returns: (transfer none): the default schema source
364 GSettingsSchemaSource *
365 g_settings_schema_source_get_default (void)
367 initialise_schema_sources ();
369 return schema_sources;
373 * g_settings_schema_source_lookup:
374 * @source: a #GSettingsSchemaSource
375 * @schema_id: a schema ID
376 * @recursive: %TRUE if the lookup should be recursive
378 * Looks up a schema with the identifier @schema_id in @source.
380 * This function is not required for normal uses of #GSettings but it
381 * may be useful to authors of plugin management systems or to those who
382 * want to introspect the content of schemas.
384 * If the schema isn't found directly in @source and @recursive is %TRUE
385 * then the parent sources will also be checked.
387 * If the schema isn't found, %NULL is returned.
389 * Returns: (transfer full): a new #GSettingsSchema
394 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
395 const gchar *schema_id,
398 GSettingsSchema *schema;
401 g_return_val_if_fail (source != NULL, NULL);
402 g_return_val_if_fail (schema_id != NULL, NULL);
404 table = gvdb_table_get_table (source->table, schema_id);
406 if (table == NULL && recursive)
407 for (source = source->parent; source; source = source->parent)
408 if ((table = gvdb_table_get_table (source->table, schema_id)))
414 schema = g_slice_new0 (GSettingsSchema);
415 schema->ref_count = 1;
416 schema->id = g_strdup (schema_id);
417 schema->table = table;
418 schema->path = g_settings_schema_get_string (schema, ".path");
419 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
421 if (schema->gettext_domain)
422 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
428 steal_item (gpointer key,
432 gchar ***ptr = user_data;
434 *(*ptr)++ = (gchar *) key;
439 static const gchar * const *non_relocatable_schema_list;
440 static const gchar * const *relocatable_schema_list;
441 static gsize schema_lists_initialised;
444 ensure_schema_lists (void)
446 if (g_once_init_enter (&schema_lists_initialised))
448 GSettingsSchemaSource *source;
449 GHashTable *single, *reloc;
454 initialise_schema_sources ();
456 /* We use hash tables to avoid duplicate listings for schemas that
457 * appear in more than one file.
459 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
460 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
462 for (source = schema_sources; source; source = source->parent)
464 list = gvdb_table_list (source->table, "");
466 /* empty schema cache file? */
470 for (i = 0; list[i]; i++)
472 if (!g_hash_table_lookup (single, list[i]) &&
473 !g_hash_table_lookup (reloc, list[i]))
477 table = gvdb_table_get_table (source->table, list[i]);
478 g_assert (table != NULL);
480 if (gvdb_table_has_value (table, ".path"))
481 g_hash_table_insert (single, g_strdup (list[i]), NULL);
483 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
485 gvdb_table_unref (table);
492 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
493 non_relocatable_schema_list = ptr;
494 g_hash_table_foreach_steal (single, steal_item, &ptr);
495 g_hash_table_unref (single);
498 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
499 relocatable_schema_list = ptr;
500 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
501 g_hash_table_unref (reloc);
504 g_once_init_leave (&schema_lists_initialised, TRUE);
509 * g_settings_list_schemas:
511 * Gets a list of the #GSettings schemas installed on the system. The
512 * returned list is exactly the list of schemas for which you may call
513 * g_settings_new() without adverse effects.
515 * This function does not list the schemas that do not provide their own
516 * paths (ie: schemas for which you must use
517 * g_settings_new_with_path()). See
518 * g_settings_list_relocatable_schemas() for that.
520 * Returns: (element-type utf8) (transfer none): a list of #GSettings
521 * schemas that are available. The list must not be modified or
526 const gchar * const *
527 g_settings_list_schemas (void)
529 ensure_schema_lists ();
531 return non_relocatable_schema_list;
535 * g_settings_list_relocatable_schemas:
537 * Gets a list of the relocatable #GSettings schemas installed on the
538 * system. These are schemas that do not provide their own path. It is
539 * usual to instantiate these schemas directly, but if you want to you
540 * can use g_settings_new_with_path() to specify the path.
542 * The output of this function, taken together with the output of
543 * g_settings_list_schemas() represents the complete list of all
546 * Returns: (element-type utf8) (transfer none): a list of relocatable
547 * #GSettings schemas that are available. The list must not be
552 const gchar * const *
553 g_settings_list_relocatable_schemas (void)
555 ensure_schema_lists ();
557 return relocatable_schema_list;
561 * g_settings_schema_ref:
562 * @schema: a #GSettingsSchema
564 * Increase the reference count of @schema, returning a new reference.
566 * Returns: a new reference to @schema
571 g_settings_schema_ref (GSettingsSchema *schema)
573 g_atomic_int_inc (&schema->ref_count);
579 * g_settings_schema_unref:
580 * @schema: a #GSettingsSchema
582 * Decrease the reference count of @schema, possibly freeing it.
587 g_settings_schema_unref (GSettingsSchema *schema)
589 if (g_atomic_int_dec_and_test (&schema->ref_count))
591 gvdb_table_unref (schema->table);
592 g_free (schema->items);
595 g_slice_free (GSettingsSchema, schema);
600 g_settings_schema_get_string (GSettingsSchema *schema,
603 const gchar *result = NULL;
606 if ((value = gvdb_table_get_raw_value (schema->table, key)))
608 result = g_variant_get_string (value, NULL);
609 g_variant_unref (value);
616 g_settings_schema_get_value (GSettingsSchema *schema,
622 value = gvdb_table_get_raw_value (schema->table, key);
624 if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
625 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
627 iter = g_variant_iter_new (value);
628 g_variant_unref (value);
634 * g_settings_schema_get_path:
635 * @schema: a #GSettingsSchema
637 * Gets the path associated with @schema, or %NULL.
639 * Schemas may be single-instance or relocatable. Single-instance
640 * schemas correspond to exactly one set of keys in the backend
641 * database: those located at the path returned by this function.
643 * Relocatable schemas can be referenced by other schemas and can
644 * threfore describe multiple sets of keys at different locations. For
645 * relocatable schemas, this function will return %NULL.
647 * Returns: (transfer none): the path of the schema, or %NULL
652 g_settings_schema_get_path (GSettingsSchema *schema)
658 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
660 return schema->gettext_domain;
664 g_settings_schema_has_key (GSettingsSchema *schema,
667 return gvdb_table_has_value (schema->table, key);
671 g_settings_schema_list (GSettingsSchema *schema,
676 if (schema->items == NULL)
681 list = gvdb_table_list (schema->table, "");
682 len = list ? g_strv_length (list) : 0;
684 schema->items = g_new (GQuark, len);
687 for (i = 0; i < len; i++)
688 if (list[i][0] != '.')
690 if (g_str_has_suffix (list[i], "/"))
692 /* This is a child. Check to make sure that
693 * instantiating the child would actually work before we
694 * return it from list() and cause a crash.
696 GSettingsSchemaSource *source;
697 GVariant *child_schema;
698 GvdbTable *child_table;
700 child_schema = gvdb_table_get_raw_value (schema->table, list[i]);
706 for (source = schema_sources; source; source = source->parent)
707 if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
710 g_variant_unref (child_schema);
712 /* Schema is not found -> don't add it to the list */
713 if (child_table == NULL)
716 /* Make sure the schema is relocatable or at the
719 if (gvdb_table_has_value (child_table, ".path"))
725 path = gvdb_table_get_raw_value (child_table, ".path");
726 expected = g_strconcat (schema->path, list[i], NULL);
727 same = g_str_equal (expected, g_variant_get_string (path, NULL));
728 g_variant_unref (path);
733 gvdb_table_unref (child_table);
738 gvdb_table_unref (child_table);
739 /* Else, it's good... */
742 schema->items[j++] = g_quark_from_string (list[i]);
749 *n_items = schema->n_items;
750 return schema->items;
754 * g_settings_schema_get_id:
755 * @schema: a #GSettingsSchema
757 * Get the ID of @schema.
759 * Returns: (transfer none): the ID
762 g_settings_schema_get_id (GSettingsSchema *schema)
768 endian_fixup (GVariant **value)
770 #if G_BYTE_ORDER == G_BIG_ENDIAN
773 tmp = g_variant_byteswap (*value);
774 g_variant_unref (*value);
780 g_settings_schema_key_init (GSettingsSchemaKey *key,
781 GSettingsSchema *schema,
788 memset (key, 0, sizeof *key);
790 iter = g_settings_schema_get_value (schema, name);
792 key->schema = g_settings_schema_ref (schema);
793 key->default_value = g_variant_iter_next_value (iter);
794 endian_fixup (&key->default_value);
795 key->type = g_variant_get_type (key->default_value);
796 key->name = g_intern_string (name);
798 while (g_variant_iter_next (iter, "(y*)", &code, &data))
803 /* translation requested */
804 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
808 /* enumerated types... */
814 key->is_flags = TRUE;
818 /* ..., choices, aliases */
819 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
823 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
824 endian_fixup (&key->minimum);
825 endian_fixup (&key->maximum);
829 g_warning ("unknown schema extension '%c'", code);
833 g_variant_unref (data);
836 g_variant_iter_free (iter);
840 g_settings_schema_key_clear (GSettingsSchemaKey *key)
843 g_variant_unref (key->minimum);
846 g_variant_unref (key->maximum);
848 g_variant_unref (key->default_value);
850 g_settings_schema_unref (key->schema);
854 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
857 g_return_val_if_fail (value != NULL, FALSE);
859 return g_variant_is_of_type (value, key->type);
863 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
866 if (key->minimum == NULL && key->strinfo == NULL)
869 if (g_variant_is_container (value))
875 g_variant_iter_init (&iter, value);
876 while (ok && (child = g_variant_iter_next_value (&iter)))
878 ok = g_settings_schema_key_range_check (key, child);
879 g_variant_unref (child);
887 return g_variant_compare (key->minimum, value) <= 0 &&
888 g_variant_compare (value, key->maximum) <= 0;
891 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
892 g_variant_get_string (value, NULL));
896 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
901 if (g_settings_schema_key_range_check (key, value))
902 return g_variant_ref (value);
904 if (key->strinfo == NULL)
907 if (g_variant_is_container (value))
909 GVariantBuilder builder;
913 g_variant_iter_init (&iter, value);
914 g_variant_builder_init (&builder, g_variant_get_type (value));
916 while ((child = g_variant_iter_next_value (&iter)))
920 fixed = g_settings_schema_key_range_fixup (key, child);
921 g_variant_unref (child);
925 g_variant_builder_clear (&builder);
929 g_variant_builder_add_value (&builder, fixed);
930 g_variant_unref (fixed);
933 return g_variant_ref_sink (g_variant_builder_end (&builder));
936 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
937 g_variant_get_string (value, NULL));
938 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
943 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
945 const gchar *translated;
946 GError *error = NULL;
950 domain = g_settings_schema_get_gettext_domain (key->schema);
952 if (key->lc_char == '\0')
953 /* translation not requested for this key */
956 if (key->lc_char == 't')
957 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
959 translated = g_dgettext (domain, key->unparsed);
961 if (translated == key->unparsed)
962 /* the default value was not translated */
965 /* try to parse the translation of the unparsed default */
966 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
970 g_warning ("Failed to parse translated string '%s' for "
971 "key '%s' in schema '%s': %s", key->unparsed, key->name,
972 g_settings_schema_get_id (key->schema), error->message);
973 g_warning ("Using untranslated default instead.");
974 g_error_free (error);
977 else if (!g_settings_schema_key_range_check (key, value))
979 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
980 "is outside of valid range", key->unparsed, key->name,
981 g_settings_schema_get_id (key->schema));
982 g_variant_unref (value);
990 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
996 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
997 g_variant_get_string (value, NULL),
1000 /* 'value' can only come from the backend after being filtered for validity,
1001 * from the translation after being filtered for validity, or from the schema
1002 * itself (which the schema compiler checks for validity). If this assertion
1003 * fails then it's really a bug in GSettings or the schema compiler...
1005 g_assert (it_worked);
1011 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1014 const gchar *string;
1016 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1021 return g_variant_new_string (string);
1025 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1033 g_variant_iter_init (&iter, value);
1034 while (g_variant_iter_next (&iter, "&s", &flag))
1039 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1040 /* as in g_settings_to_enum() */
1041 g_assert (it_worked);
1043 result |= flag_value;
1050 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1053 GVariantBuilder builder;
1056 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1058 for (i = 0; i < 32; i++)
1059 if (value & (1u << i))
1061 const gchar *string;
1063 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1067 g_variant_builder_clear (&builder);
1071 g_variant_builder_add (&builder, "s", string);
1074 return g_variant_builder_end (&builder);