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"
33 * SECTION:gsettingsschema
34 * @short_description: introspecting and controlling the loading of
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:
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
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 const gchar *gettext_domain;
150 typedef struct _GSettingsSchemaSource GSettingsSchemaSource;
153 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
155 * A boxed #GType corresponding to #GSettingsSchemaSource.
159 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
162 * G_TYPE_SETTINGS_SCHEMA:
164 * A boxed #GType corresponding to #GSettingsSchema.
168 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
171 * GSettingsSchemaSource:
173 * This is an opaque structure type. You may not access it directly.
177 struct _GSettingsSchemaSource
179 GSettingsSchemaSource *parent;
185 static GSettingsSchemaSource *schema_sources;
188 prepend_schema_table (GvdbTable *table)
190 GSettingsSchemaSource *source;
192 /* we steal the reference from 'schema_sources' for our ->parent */
193 source = g_slice_new (GSettingsSchemaSource);
194 source->parent = schema_sources;
195 source->table = table;
196 source->ref_count = 1;
198 schema_sources = source;
202 * g_settings_schema_source_ref:
203 * @source: a #GSettingsSchemaSource
205 * Increase the reference count of @source, returning a new reference.
207 * Returns: a new reference to @source
211 GSettingsSchemaSource *
212 g_settings_schema_source_ref (GSettingsSchemaSource *source)
214 g_atomic_int_inc (&source->ref_count);
220 * g_settings_schema_source_unref:
221 * @source: a #GSettingsSchemaSource
223 * Decrease the reference count of @source, possibly freeing it.
228 g_settings_schema_source_unref (GSettingsSchemaSource *source)
230 if (g_atomic_int_dec_and_test (&source->ref_count))
232 if (source == schema_sources)
233 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
236 g_settings_schema_source_unref (source->parent);
237 gvdb_table_unref (source->table);
239 g_slice_free (GSettingsSchemaSource, source);
244 * g_settings_schema_source_new_from_directory:
245 * @directory: the filename of a directory
246 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
247 * @trusted: %TRUE, if the directory is trusted
248 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
250 * Attempts to create a new schema source corresponding to the contents
251 * of the given directory.
253 * This function is not required for normal uses of #GSettings but it
254 * may be useful to authors of plugin management systems.
256 * The directory should contain a file called
257 * <filename>gschemas.compiled</filename> as produced by
258 * <command>glib-compile-schemas</command>.
260 * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
261 * trusted not to be corrupted. This assumption has a performance
262 * advantage, but can result in crashes or inconsistent behaviour in the
263 * case of a corrupted file. Generally, you should set @trusted to
264 * %TRUE for files installed by the system and to %FALSE for files in
265 * the home directory.
267 * If @parent is non-%NULL then there are two effects.
269 * First, if g_settings_schema_source_lookup() is called with the
270 * @recursive flag set to %TRUE and the schema can not be found in the
271 * source, the lookup will recurse to the parent.
273 * Second, any references to other schemas specified within this
274 * source (ie: <literal>child</literal> or <literal>extents</literal>)
275 * references may be resolved from the @parent.
277 * For this second reason, except in very unusual situations, the
278 * @parent should probably be given as the default schema source, as
279 * returned by g_settings_schema_source_get_default().
283 GSettingsSchemaSource *
284 g_settings_schema_source_new_from_directory (const gchar *directory,
285 GSettingsSchemaSource *parent,
289 GSettingsSchemaSource *source;
293 filename = g_build_filename (directory, "gschemas.compiled", NULL);
294 table = gvdb_table_new (filename, trusted, error);
300 source = g_slice_new (GSettingsSchemaSource);
301 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
302 source->table = table;
303 source->ref_count = 1;
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++);
331 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
332 table = gvdb_table_new (filename, TRUE, NULL);
335 prepend_schema_table (table);
340 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
345 filename = g_build_filename (path, "gschemas.compiled", NULL);
346 table = gvdb_table_new (filename, TRUE, NULL);
349 prepend_schema_table (table);
354 g_once_init_leave (&initialised, TRUE);
359 * g_settings_schema_source_get_default:
361 * Gets the default system schema source.
363 * This function is not required for normal uses of #GSettings but it
364 * may be useful to authors of plugin management systems or to those who
365 * want to introspect the content of schemas.
367 * If no schemas are installed, %NULL will be returned.
369 * The returned source may actually consist of multiple schema sources
370 * from different directories, depending on which directories were given
371 * in <envar>XDG_DATA_DIRS</envar> and
372 * <envar>GSETTINGS_SCHEMA_DIR</envar>. For this reason, all lookups
373 * performed against the default source should probably be done
376 * Returns: (transfer none): the default schema source
380 GSettingsSchemaSource *
381 g_settings_schema_source_get_default (void)
383 initialise_schema_sources ();
385 return schema_sources;
389 * g_settings_schema_source_lookup:
390 * @source: a #GSettingsSchemaSource
391 * @schema_id: a schema ID
392 * @recursive: %TRUE if the lookup should be recursive
394 * Looks up a schema with the identifier @schema_id in @source.
396 * This function is not required for normal uses of #GSettings but it
397 * may be useful to authors of plugin management systems or to those who
398 * want to introspect the content of schemas.
400 * If the schema isn't found directly in @source and @recursive is %TRUE
401 * then the parent sources will also be checked.
403 * If the schema isn't found, %NULL is returned.
405 * Returns: (transfer full): a new #GSettingsSchema
410 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
411 const gchar *schema_id,
414 GSettingsSchema *schema;
417 g_return_val_if_fail (source != NULL, NULL);
418 g_return_val_if_fail (schema_id != NULL, NULL);
420 table = gvdb_table_get_table (source->table, schema_id);
422 if (table == NULL && recursive)
423 for (source = source->parent; source; source = source->parent)
424 if ((table = gvdb_table_get_table (source->table, schema_id)))
430 schema = g_slice_new0 (GSettingsSchema);
431 schema->ref_count = 1;
432 schema->id = g_strdup (schema_id);
433 schema->table = table;
434 schema->path = g_settings_schema_get_string (schema, ".path");
435 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
437 if (schema->gettext_domain)
438 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
444 steal_item (gpointer key,
448 gchar ***ptr = user_data;
450 *(*ptr)++ = (gchar *) key;
455 static const gchar * const *non_relocatable_schema_list;
456 static const gchar * const *relocatable_schema_list;
457 static gsize schema_lists_initialised;
460 ensure_schema_lists (void)
462 if (g_once_init_enter (&schema_lists_initialised))
464 GSettingsSchemaSource *source;
465 GHashTable *single, *reloc;
470 initialise_schema_sources ();
472 /* We use hash tables to avoid duplicate listings for schemas that
473 * appear in more than one file.
475 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
476 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
478 for (source = schema_sources; source; source = source->parent)
480 list = gvdb_table_list (source->table, "");
482 g_assert (list != NULL);
484 for (i = 0; list[i]; i++)
486 if (!g_hash_table_lookup (single, list[i]) &&
487 !g_hash_table_lookup (reloc, list[i]))
491 table = gvdb_table_get_table (source->table, list[i]);
492 g_assert (table != NULL);
494 if (gvdb_table_has_value (table, ".path"))
495 g_hash_table_insert (single, g_strdup (list[i]), NULL);
497 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
499 gvdb_table_unref (table);
506 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
507 non_relocatable_schema_list = ptr;
508 g_hash_table_foreach_steal (single, steal_item, &ptr);
509 g_hash_table_unref (single);
512 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
513 relocatable_schema_list = ptr;
514 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
515 g_hash_table_unref (reloc);
518 g_once_init_leave (&schema_lists_initialised, TRUE);
523 * g_settings_list_schemas:
525 * Gets a list of the #GSettings schemas installed on the system. The
526 * returned list is exactly the list of schemas for which you may call
527 * g_settings_new() without adverse effects.
529 * This function does not list the schemas that do not provide their own
530 * paths (ie: schemas for which you must use
531 * g_settings_new_with_path()). See
532 * g_settings_list_relocatable_schemas() for that.
534 * Returns: (element-type utf8) (transfer none): a list of #GSettings
535 * schemas that are available. The list must not be modified or
540 const gchar * const *
541 g_settings_list_schemas (void)
543 ensure_schema_lists ();
545 return non_relocatable_schema_list;
549 * g_settings_list_relocatable_schemas:
551 * Gets a list of the relocatable #GSettings schemas installed on the
552 * system. These are schemas that do not provide their own path. It is
553 * usual to instantiate these schemas directly, but if you want to you
554 * can use g_settings_new_with_path() to specify the path.
556 * The output of this function, taken together with the output of
557 * g_settings_list_schemas() represents the complete list of all
560 * Returns: (element-type utf8) (transfer none): a list of relocatable
561 * #GSettings schemas that are available. The list must not be
566 const gchar * const *
567 g_settings_list_relocatable_schemas (void)
569 ensure_schema_lists ();
571 return relocatable_schema_list;
575 * g_settings_schema_ref:
576 * @schema: a #GSettingsSchema
578 * Increase the reference count of @schema, returning a new reference.
580 * Returns: a new reference to @schema
585 g_settings_schema_ref (GSettingsSchema *schema)
587 g_atomic_int_inc (&schema->ref_count);
593 * g_settings_schema_unref:
594 * @schema: a #GSettingsSchema
596 * Decrease the reference count of @schema, possibly freeing it.
601 g_settings_schema_unref (GSettingsSchema *schema)
603 if (g_atomic_int_dec_and_test (&schema->ref_count))
605 gvdb_table_unref (schema->table);
606 g_free (schema->items);
609 g_slice_free (GSettingsSchema, schema);
614 g_settings_schema_get_string (GSettingsSchema *schema,
617 const gchar *result = NULL;
620 if ((value = gvdb_table_get_raw_value (schema->table, key)))
622 result = g_variant_get_string (value, NULL);
623 g_variant_unref (value);
630 g_settings_schema_get_value (GSettingsSchema *schema,
636 value = gvdb_table_get_raw_value (schema->table, key);
638 if G_UNLIKELY (value == NULL)
639 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
641 iter = g_variant_iter_new (value);
642 g_variant_unref (value);
648 * g_settings_schema_get_path:
649 * @schema: a #GSettingsSchema
651 * Gets the path associated with @schema, or %NULL.
653 * Schemas may be single-instance or relocatable. Single-instance
654 * schemas correspond to exactly one set of keys in the backend
655 * database: those located at the path returned by this function.
657 * Relocatable schemas can be referenced by other schemas and can
658 * threfore describe multiple sets of keys at different locations. For
659 * relocatable schemas, this function will return %NULL.
661 * Returns: (transfer none): the path of the schema, or %NULL
666 g_settings_schema_get_path (GSettingsSchema *schema)
672 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
674 return schema->gettext_domain;
678 g_settings_schema_has_key (GSettingsSchema *schema,
681 return gvdb_table_has_value (schema->table, key);
685 g_settings_schema_list (GSettingsSchema *schema,
690 if (schema->items == NULL)
695 list = gvdb_table_list (schema->table, "");
696 len = list ? g_strv_length (list) : 0;
698 schema->items = g_new (GQuark, len);
701 for (i = 0; i < len; i++)
702 if (list[i][0] != '.')
703 schema->items[j++] = g_quark_from_string (list[i]);
709 *n_items = schema->n_items;
710 return schema->items;
714 * g_settings_schema_get_id:
715 * @schema: a #GSettingsSchema
717 * Get the ID of @schema.
719 * Returns: (transfer none): the ID
722 g_settings_schema_get_id (GSettingsSchema *schema)
728 endian_fixup (GVariant **value)
730 #if G_BYTE_ORDER == G_BIG_ENDIAN
733 tmp = g_variant_byteswap (*value);
734 g_variant_unref (*value);
740 g_settings_schema_key_init (GSettingsSchemaKey *key,
741 GSettingsSchema *schema,
748 memset (key, 0, sizeof *key);
750 iter = g_settings_schema_get_value (schema, name);
752 key->schema = g_settings_schema_ref (schema);
753 key->default_value = g_variant_iter_next_value (iter);
754 endian_fixup (&key->default_value);
755 key->type = g_variant_get_type (key->default_value);
756 key->name = g_intern_string (name);
758 while (g_variant_iter_next (iter, "(y*)", &code, &data))
763 /* translation requested */
764 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
768 /* enumerated types... */
774 key->is_flags = TRUE;
778 /* ..., choices, aliases */
779 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
783 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
784 endian_fixup (&key->minimum);
785 endian_fixup (&key->maximum);
789 g_warning ("unknown schema extension '%c'", code);
793 g_variant_unref (data);
796 g_variant_iter_free (iter);
800 g_settings_schema_key_clear (GSettingsSchemaKey *key)
803 g_variant_unref (key->minimum);
806 g_variant_unref (key->maximum);
808 g_variant_unref (key->default_value);
810 g_settings_schema_unref (key->schema);
814 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
817 g_return_val_if_fail (value != NULL, FALSE);
819 return g_variant_is_of_type (value, key->type);
823 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
826 if (key->minimum == NULL && key->strinfo == NULL)
829 if (g_variant_is_container (value))
835 g_variant_iter_init (&iter, value);
836 while (ok && (child = g_variant_iter_next_value (&iter)))
838 ok = g_settings_schema_key_range_check (key, child);
839 g_variant_unref (child);
847 return g_variant_compare (key->minimum, value) <= 0 &&
848 g_variant_compare (value, key->maximum) <= 0;
851 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
852 g_variant_get_string (value, NULL));
856 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
861 if (g_settings_schema_key_range_check (key, value))
862 return g_variant_ref (value);
864 if (key->strinfo == NULL)
867 if (g_variant_is_container (value))
869 GVariantBuilder builder;
873 g_variant_iter_init (&iter, value);
874 g_variant_builder_init (&builder, g_variant_get_type (value));
876 while ((child = g_variant_iter_next_value (&iter)))
880 fixed = g_settings_schema_key_range_fixup (key, child);
881 g_variant_unref (child);
885 g_variant_builder_clear (&builder);
889 g_variant_builder_add_value (&builder, fixed);
890 g_variant_unref (fixed);
893 return g_variant_ref_sink (g_variant_builder_end (&builder));
896 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
897 g_variant_get_string (value, NULL));
898 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
903 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
905 const gchar *translated;
906 GError *error = NULL;
910 domain = g_settings_schema_get_gettext_domain (key->schema);
912 if (key->lc_char == '\0')
913 /* translation not requested for this key */
916 if (key->lc_char == 't')
917 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
919 translated = g_dgettext (domain, key->unparsed);
921 if (translated == key->unparsed)
922 /* the default value was not translated */
925 /* try to parse the translation of the unparsed default */
926 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
930 g_warning ("Failed to parse translated string `%s' for "
931 "key `%s' in schema `%s': %s", key->unparsed, key->name,
932 g_settings_schema_get_id (key->schema), error->message);
933 g_warning ("Using untranslated default instead.");
934 g_error_free (error);
937 else if (!g_settings_schema_key_range_check (key, value))
939 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
940 "is outside of valid range", key->unparsed, key->name,
941 g_settings_schema_get_id (key->schema));
942 g_variant_unref (value);
950 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
956 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
957 g_variant_get_string (value, NULL),
960 /* 'value' can only come from the backend after being filtered for validity,
961 * from the translation after being filtered for validity, or from the schema
962 * itself (which the schema compiler checks for validity). If this assertion
963 * fails then it's really a bug in GSettings or the schema compiler...
965 g_assert (it_worked);
971 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
976 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
981 return g_variant_new_string (string);
985 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
993 g_variant_iter_init (&iter, value);
994 while (g_variant_iter_next (&iter, "&s", &flag))
999 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1000 /* as in g_settings_to_enum() */
1001 g_assert (it_worked);
1003 result |= flag_value;
1010 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1013 GVariantBuilder builder;
1016 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1018 for (i = 0; i < 32; i++)
1019 if (value & (1u << i))
1021 const gchar *string;
1023 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1027 g_variant_builder_clear (&builder);
1031 g_variant_builder_add (&builder, "s", string);
1034 return g_variant_builder_end (&builder);