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;
151 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
153 * A boxed #GType corresponding to #GSettingsSchemaSource.
157 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
160 * G_TYPE_SETTINGS_SCHEMA:
162 * A boxed #GType corresponding to #GSettingsSchema.
166 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
169 * GSettingsSchemaSource:
171 * This is an opaque structure type. You may not access it directly.
175 struct _GSettingsSchemaSource
177 GSettingsSchemaSource *parent;
183 static GSettingsSchemaSource *schema_sources;
186 prepend_schema_table (GvdbTable *table)
188 GSettingsSchemaSource *source;
190 /* we steal the reference from 'schema_sources' for our ->parent */
191 source = g_slice_new (GSettingsSchemaSource);
192 source->parent = schema_sources;
193 source->table = table;
194 source->ref_count = 1;
196 schema_sources = source;
200 * g_settings_schema_source_ref:
201 * @source: a #GSettingsSchemaSource
203 * Increase the reference count of @source, returning a new reference.
205 * Returns: a new reference to @source
209 GSettingsSchemaSource *
210 g_settings_schema_source_ref (GSettingsSchemaSource *source)
212 g_atomic_int_inc (&source->ref_count);
218 * g_settings_schema_source_unref:
219 * @source: a #GSettingsSchemaSource
221 * Decrease the reference count of @source, possibly freeing it.
226 g_settings_schema_source_unref (GSettingsSchemaSource *source)
228 if (g_atomic_int_dec_and_test (&source->ref_count))
230 if (source == schema_sources)
231 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
234 g_settings_schema_source_unref (source->parent);
235 gvdb_table_unref (source->table);
237 g_slice_free (GSettingsSchemaSource, source);
242 * g_settings_schema_source_new_from_directory:
243 * @directory: the filename of a directory
244 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
245 * @trusted: %TRUE, if the directory is trusted
246 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
248 * Attempts to create a new schema source corresponding to the contents
249 * of the given directory.
251 * This function is not required for normal uses of #GSettings but it
252 * may be useful to authors of plugin management systems.
254 * The directory should contain a file called
255 * <filename>gschemas.compiled</filename> as produced by
256 * <command>glib-compile-schemas</command>.
258 * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
259 * trusted not to be corrupted. This assumption has a performance
260 * advantage, but can result in crashes or inconsistent behaviour in the
261 * case of a corrupted file. Generally, you should set @trusted to
262 * %TRUE for files installed by the system and to %FALSE for files in
263 * the home directory.
265 * If @parent is non-%NULL then there are two effects.
267 * First, if g_settings_schema_source_lookup() is called with the
268 * @recursive flag set to %TRUE and the schema can not be found in the
269 * source, the lookup will recurse to the parent.
271 * Second, any references to other schemas specified within this
272 * source (ie: <literal>child</literal> or <literal>extents</literal>)
273 * references may be resolved from the @parent.
275 * For this second reason, except in very unusual situations, the
276 * @parent should probably be given as the default schema source, as
277 * returned by g_settings_schema_source_get_default().
281 GSettingsSchemaSource *
282 g_settings_schema_source_new_from_directory (const gchar *directory,
283 GSettingsSchemaSource *parent,
287 GSettingsSchemaSource *source;
291 filename = g_build_filename (directory, "gschemas.compiled", NULL);
292 table = gvdb_table_new (filename, trusted, error);
298 source = g_slice_new (GSettingsSchemaSource);
299 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
300 source->table = table;
301 source->ref_count = 1;
307 initialise_schema_sources (void)
309 static gsize initialised;
311 /* need a separate variable because 'schema_sources' may legitimately
312 * be null if we have zero valid schema sources
314 if G_UNLIKELY (g_once_init_enter (&initialised))
316 const gchar * const *dirs;
320 /* iterate in reverse: count up, then count down */
321 dirs = g_get_system_data_dirs ();
322 for (i = 0; dirs[i]; i++);
329 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
330 table = gvdb_table_new (filename, TRUE, NULL);
333 prepend_schema_table (table);
338 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
343 filename = g_build_filename (path, "gschemas.compiled", NULL);
344 table = gvdb_table_new (filename, TRUE, NULL);
347 prepend_schema_table (table);
352 g_once_init_leave (&initialised, TRUE);
357 * g_settings_schema_source_get_default:
359 * Gets the default system schema source.
361 * This function is not required for normal uses of #GSettings but it
362 * may be useful to authors of plugin management systems or to those who
363 * want to introspect the content of schemas.
365 * If no schemas are installed, %NULL will be returned.
367 * The returned source may actually consist of multiple schema sources
368 * from different directories, depending on which directories were given
369 * in <envar>XDG_DATA_DIRS</envar> and
370 * <envar>GSETTINGS_SCHEMA_DIR</envar>. For this reason, all lookups
371 * performed against the default source should probably be done
374 * Returns: (transfer none): the default schema source
378 GSettingsSchemaSource *
379 g_settings_schema_source_get_default (void)
381 initialise_schema_sources ();
383 return schema_sources;
387 * g_settings_schema_source_lookup:
388 * @source: a #GSettingsSchemaSource
389 * @schema_id: a schema ID
390 * @recursive: %TRUE if the lookup should be recursive
392 * Looks up a schema with the identifier @schema_id in @source.
394 * This function is not required for normal uses of #GSettings but it
395 * may be useful to authors of plugin management systems or to those who
396 * want to introspect the content of schemas.
398 * If the schema isn't found directly in @source and @recursive is %TRUE
399 * then the parent sources will also be checked.
401 * If the schema isn't found, %NULL is returned.
403 * Returns: (transfer full): a new #GSettingsSchema
408 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
409 const gchar *schema_id,
412 GSettingsSchema *schema;
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->ref_count = 1;
430 schema->id = g_strdup (schema_id);
431 schema->table = table;
432 schema->path = g_settings_schema_get_string (schema, ".path");
433 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
435 if (schema->gettext_domain)
436 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
442 steal_item (gpointer key,
446 gchar ***ptr = user_data;
448 *(*ptr)++ = (gchar *) key;
453 static const gchar * const *non_relocatable_schema_list;
454 static const gchar * const *relocatable_schema_list;
455 static gsize schema_lists_initialised;
458 ensure_schema_lists (void)
460 if (g_once_init_enter (&schema_lists_initialised))
462 GSettingsSchemaSource *source;
463 GHashTable *single, *reloc;
468 initialise_schema_sources ();
470 /* We use hash tables to avoid duplicate listings for schemas that
471 * appear in more than one file.
473 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
474 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
476 for (source = schema_sources; source; source = source->parent)
478 list = gvdb_table_list (source->table, "");
480 g_assert (list != NULL);
482 for (i = 0; list[i]; i++)
484 if (!g_hash_table_lookup (single, list[i]) &&
485 !g_hash_table_lookup (reloc, list[i]))
489 table = gvdb_table_get_table (source->table, list[i]);
490 g_assert (table != NULL);
492 if (gvdb_table_has_value (table, ".path"))
493 g_hash_table_insert (single, g_strdup (list[i]), NULL);
495 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
497 gvdb_table_unref (table);
504 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
505 non_relocatable_schema_list = ptr;
506 g_hash_table_foreach_steal (single, steal_item, &ptr);
507 g_hash_table_unref (single);
510 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
511 relocatable_schema_list = ptr;
512 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
513 g_hash_table_unref (reloc);
516 g_once_init_leave (&schema_lists_initialised, TRUE);
521 * g_settings_list_schemas:
523 * Gets a list of the #GSettings schemas installed on the system. The
524 * returned list is exactly the list of schemas for which you may call
525 * g_settings_new() without adverse effects.
527 * This function does not list the schemas that do not provide their own
528 * paths (ie: schemas for which you must use
529 * g_settings_new_with_path()). See
530 * g_settings_list_relocatable_schemas() for that.
532 * Returns: (element-type utf8) (transfer none): a list of #GSettings
533 * schemas that are available. The list must not be modified or
538 const gchar * const *
539 g_settings_list_schemas (void)
541 ensure_schema_lists ();
543 return non_relocatable_schema_list;
547 * g_settings_list_relocatable_schemas:
549 * Gets a list of the relocatable #GSettings schemas installed on the
550 * system. These are schemas that do not provide their own path. It is
551 * usual to instantiate these schemas directly, but if you want to you
552 * can use g_settings_new_with_path() to specify the path.
554 * The output of this function, taken together with the output of
555 * g_settings_list_schemas() represents the complete list of all
558 * Returns: (element-type utf8) (transfer none): a list of relocatable
559 * #GSettings schemas that are available. The list must not be
564 const gchar * const *
565 g_settings_list_relocatable_schemas (void)
567 ensure_schema_lists ();
569 return relocatable_schema_list;
573 * g_settings_schema_ref:
574 * @schema: a #GSettingsSchema
576 * Increase the reference count of @schema, returning a new reference.
578 * Returns: a new reference to @schema
583 g_settings_schema_ref (GSettingsSchema *schema)
585 g_atomic_int_inc (&schema->ref_count);
591 * g_settings_schema_unref:
592 * @schema: a #GSettingsSchema
594 * Decrease the reference count of @schema, possibly freeing it.
599 g_settings_schema_unref (GSettingsSchema *schema)
601 if (g_atomic_int_dec_and_test (&schema->ref_count))
603 gvdb_table_unref (schema->table);
604 g_free (schema->items);
607 g_slice_free (GSettingsSchema, schema);
612 g_settings_schema_get_string (GSettingsSchema *schema,
615 const gchar *result = NULL;
618 if ((value = gvdb_table_get_raw_value (schema->table, key)))
620 result = g_variant_get_string (value, NULL);
621 g_variant_unref (value);
628 g_settings_schema_get_value (GSettingsSchema *schema,
634 value = gvdb_table_get_raw_value (schema->table, key);
636 if G_UNLIKELY (value == NULL)
637 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
639 iter = g_variant_iter_new (value);
640 g_variant_unref (value);
646 * g_settings_schema_get_path:
647 * @schema: a #GSettingsSchema
649 * Gets the path associated with @schema, or %NULL.
651 * Schemas may be single-instance or relocatable. Single-instance
652 * schemas correspond to exactly one set of keys in the backend
653 * database: those located at the path returned by this function.
655 * Relocatable schemas can be referenced by other schemas and can
656 * threfore describe multiple sets of keys at different locations. For
657 * relocatable schemas, this function will return %NULL.
659 * Returns: (transfer none): the path of the schema, or %NULL
664 g_settings_schema_get_path (GSettingsSchema *schema)
670 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
672 return schema->gettext_domain;
676 g_settings_schema_has_key (GSettingsSchema *schema,
679 return gvdb_table_has_value (schema->table, key);
683 g_settings_schema_list (GSettingsSchema *schema,
688 if (schema->items == NULL)
693 list = gvdb_table_list (schema->table, "");
694 len = list ? g_strv_length (list) : 0;
696 schema->items = g_new (GQuark, len);
699 for (i = 0; i < len; i++)
700 if (list[i][0] != '.')
701 schema->items[j++] = g_quark_from_string (list[i]);
707 *n_items = schema->n_items;
708 return schema->items;
712 * g_settings_schema_get_id:
713 * @schema: a #GSettingsSchema
715 * Get the ID of @schema.
717 * Returns: (transfer none): the ID
720 g_settings_schema_get_id (GSettingsSchema *schema)
726 endian_fixup (GVariant **value)
728 #if G_BYTE_ORDER == G_BIG_ENDIAN
731 tmp = g_variant_byteswap (*value);
732 g_variant_unref (*value);
738 g_settings_schema_key_init (GSettingsSchemaKey *key,
739 GSettingsSchema *schema,
746 memset (key, 0, sizeof *key);
748 iter = g_settings_schema_get_value (schema, name);
750 key->schema = g_settings_schema_ref (schema);
751 key->default_value = g_variant_iter_next_value (iter);
752 endian_fixup (&key->default_value);
753 key->type = g_variant_get_type (key->default_value);
754 key->name = g_intern_string (name);
756 while (g_variant_iter_next (iter, "(y*)", &code, &data))
761 /* translation requested */
762 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
766 /* enumerated types... */
772 key->is_flags = TRUE;
776 /* ..., choices, aliases */
777 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
781 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
782 endian_fixup (&key->minimum);
783 endian_fixup (&key->maximum);
787 g_warning ("unknown schema extension '%c'", code);
791 g_variant_unref (data);
794 g_variant_iter_free (iter);
798 g_settings_schema_key_clear (GSettingsSchemaKey *key)
801 g_variant_unref (key->minimum);
804 g_variant_unref (key->maximum);
806 g_variant_unref (key->default_value);
808 g_settings_schema_unref (key->schema);
812 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
815 g_return_val_if_fail (value != NULL, FALSE);
817 return g_variant_is_of_type (value, key->type);
821 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
824 if (key->minimum == NULL && key->strinfo == NULL)
827 if (g_variant_is_container (value))
833 g_variant_iter_init (&iter, value);
834 while (ok && (child = g_variant_iter_next_value (&iter)))
836 ok = g_settings_schema_key_range_check (key, child);
837 g_variant_unref (child);
845 return g_variant_compare (key->minimum, value) <= 0 &&
846 g_variant_compare (value, key->maximum) <= 0;
849 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
850 g_variant_get_string (value, NULL));
854 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
859 if (g_settings_schema_key_range_check (key, value))
860 return g_variant_ref (value);
862 if (key->strinfo == NULL)
865 if (g_variant_is_container (value))
867 GVariantBuilder builder;
871 g_variant_iter_init (&iter, value);
872 g_variant_builder_init (&builder, g_variant_get_type (value));
874 while ((child = g_variant_iter_next_value (&iter)))
878 fixed = g_settings_schema_key_range_fixup (key, child);
879 g_variant_unref (child);
883 g_variant_builder_clear (&builder);
887 g_variant_builder_add_value (&builder, fixed);
888 g_variant_unref (fixed);
891 return g_variant_ref_sink (g_variant_builder_end (&builder));
894 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
895 g_variant_get_string (value, NULL));
896 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
901 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
903 const gchar *translated;
904 GError *error = NULL;
908 domain = g_settings_schema_get_gettext_domain (key->schema);
910 if (key->lc_char == '\0')
911 /* translation not requested for this key */
914 if (key->lc_char == 't')
915 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
917 translated = g_dgettext (domain, key->unparsed);
919 if (translated == key->unparsed)
920 /* the default value was not translated */
923 /* try to parse the translation of the unparsed default */
924 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
928 g_warning ("Failed to parse translated string `%s' for "
929 "key `%s' in schema `%s': %s", key->unparsed, key->name,
930 g_settings_schema_get_id (key->schema), error->message);
931 g_warning ("Using untranslated default instead.");
932 g_error_free (error);
935 else if (!g_settings_schema_key_range_check (key, value))
937 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
938 "is outside of valid range", key->unparsed, key->name,
939 g_settings_schema_get_id (key->schema));
940 g_variant_unref (value);
948 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
954 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
955 g_variant_get_string (value, NULL),
958 /* 'value' can only come from the backend after being filtered for validity,
959 * from the translation after being filtered for validity, or from the schema
960 * itself (which the schema compiler checks for validity). If this assertion
961 * fails then it's really a bug in GSettings or the schema compiler...
963 g_assert (it_worked);
969 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
974 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
979 return g_variant_new_string (string);
983 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
991 g_variant_iter_init (&iter, value);
992 while (g_variant_iter_next (&iter, "&s", &flag))
997 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
998 /* as in g_settings_to_enum() */
999 g_assert (it_worked);
1001 result |= flag_value;
1008 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1011 GVariantBuilder builder;
1014 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1016 for (i = 0; i < 32; i++)
1017 if (value & (1u << i))
1019 const gchar *string;
1021 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1025 g_variant_builder_clear (&builder);
1029 g_variant_builder_add (&builder, "s", string);
1032 return g_variant_builder_end (&builder);