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"
32 struct _GSettingsSchema
34 const gchar *gettext_domain;
44 typedef struct _GSettingsSchemaSource GSettingsSchemaSource;
46 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
47 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
49 struct _GSettingsSchemaSource
51 GSettingsSchemaSource *parent;
57 static GSettingsSchemaSource *schema_sources;
60 prepend_schema_table (GvdbTable *table)
62 GSettingsSchemaSource *source;
64 /* we steal the reference from 'schema_sources' for our ->parent */
65 source = g_slice_new (GSettingsSchemaSource);
66 source->parent = schema_sources;
67 source->table = table;
68 source->ref_count = 1;
70 schema_sources = source;
73 GSettingsSchemaSource *
74 g_settings_schema_source_ref (GSettingsSchemaSource *source)
76 g_atomic_int_inc (&source->ref_count);
82 g_settings_schema_source_unref (GSettingsSchemaSource *source)
84 if (g_atomic_int_dec_and_test (&source->ref_count))
86 if (source == schema_sources)
87 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
90 g_settings_schema_source_unref (source->parent);
91 gvdb_table_unref (source->table);
93 g_slice_free (GSettingsSchemaSource, source);
97 GSettingsSchemaSource *
98 g_settings_schema_source_new_from_directory (GSettingsSchemaSource *parent,
99 const gchar *directory,
103 GSettingsSchemaSource *source;
107 filename = g_build_filename (directory, "gschemas.compiled", NULL);
108 table = gvdb_table_new (filename, TRUE, error);
114 source = g_slice_new (GSettingsSchemaSource);
115 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
116 source->table = table;
117 source->ref_count = 1;
123 initialise_schema_sources (void)
125 static gsize initialised;
127 /* need a separate variable because 'schema_sources' may legitimately
128 * be null if we have zero valid schema sources
130 if G_UNLIKELY (g_once_init_enter (&initialised))
132 const gchar * const *dirs;
136 /* iterate in reverse: count up, then count down */
137 dirs = g_get_system_data_dirs ();
138 for (i = 0; dirs[i]; i++);
145 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
146 table = gvdb_table_new (filename, TRUE, NULL);
149 prepend_schema_table (table);
154 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
159 filename = g_build_filename (path, "gschemas.compiled", NULL);
160 table = gvdb_table_new (filename, TRUE, NULL);
163 prepend_schema_table (table);
168 g_once_init_leave (&initialised, TRUE);
172 GSettingsSchemaSource *
173 g_settings_schema_source_get_default (void)
175 initialise_schema_sources ();
177 return schema_sources;
181 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
182 const gchar *schema_id,
185 GSettingsSchema *schema;
188 g_return_val_if_fail (source != NULL, NULL);
189 g_return_val_if_fail (schema_id != NULL, NULL);
191 table = gvdb_table_get_table (source->table, schema_id);
193 if (table == NULL && recursive)
194 for (source = source->parent; source; source = source->parent)
195 if ((table = gvdb_table_get_table (source->table, schema_id)))
201 schema = g_slice_new0 (GSettingsSchema);
202 schema->ref_count = 1;
203 schema->id = g_strdup (schema_id);
204 schema->table = table;
205 schema->path = g_settings_schema_get_string (schema, ".path");
206 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
208 if (schema->gettext_domain)
209 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
215 steal_item (gpointer key,
219 gchar ***ptr = user_data;
221 *(*ptr)++ = (gchar *) key;
226 static const gchar * const *non_relocatable_schema_list;
227 static const gchar * const *relocatable_schema_list;
228 static gsize schema_lists_initialised;
231 ensure_schema_lists (void)
233 if (g_once_init_enter (&schema_lists_initialised))
235 GSettingsSchemaSource *source;
236 GHashTable *single, *reloc;
241 initialise_schema_sources ();
243 /* We use hash tables to avoid duplicate listings for schemas that
244 * appear in more than one file.
246 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
247 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
249 for (source = schema_sources; source; source = source->parent)
251 list = gvdb_table_list (source->table, "");
253 g_assert (list != NULL);
255 for (i = 0; list[i]; i++)
257 if (!g_hash_table_lookup (single, list[i]) &&
258 !g_hash_table_lookup (reloc, list[i]))
262 table = gvdb_table_get_table (source->table, list[i]);
263 g_assert (table != NULL);
265 if (gvdb_table_has_value (table, ".path"))
266 g_hash_table_insert (single, g_strdup (list[i]), NULL);
268 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
270 gvdb_table_unref (table);
277 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
278 non_relocatable_schema_list = ptr;
279 g_hash_table_foreach_steal (single, steal_item, &ptr);
280 g_hash_table_unref (single);
283 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
284 relocatable_schema_list = ptr;
285 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
286 g_hash_table_unref (reloc);
289 g_once_init_leave (&schema_lists_initialised, TRUE);
294 * g_settings_list_schemas:
296 * Gets a list of the #GSettings schemas installed on the system. The
297 * returned list is exactly the list of schemas for which you may call
298 * g_settings_new() without adverse effects.
300 * This function does not list the schemas that do not provide their own
301 * paths (ie: schemas for which you must use
302 * g_settings_new_with_path()). See
303 * g_settings_list_relocatable_schemas() for that.
305 * Returns: (element-type utf8) (transfer none): a list of #GSettings
306 * schemas that are available. The list must not be modified or
311 const gchar * const *
312 g_settings_list_schemas (void)
314 ensure_schema_lists ();
316 return non_relocatable_schema_list;
320 * g_settings_list_relocatable_schemas:
322 * Gets a list of the relocatable #GSettings schemas installed on the
323 * system. These are schemas that do not provide their own path. It is
324 * usual to instantiate these schemas directly, but if you want to you
325 * can use g_settings_new_with_path() to specify the path.
327 * The output of this function, tTaken together with the output of
328 * g_settings_list_schemas() represents the complete list of all
331 * Returns: (element-type utf8) (transfer none): a list of relocatable
332 * #GSettings schemas that are available. The list must not be
337 const gchar * const *
338 g_settings_list_relocatable_schemas (void)
340 ensure_schema_lists ();
342 return relocatable_schema_list;
346 g_settings_schema_ref (GSettingsSchema *schema)
348 g_atomic_int_inc (&schema->ref_count);
354 g_settings_schema_unref (GSettingsSchema *schema)
356 if (g_atomic_int_dec_and_test (&schema->ref_count))
358 gvdb_table_unref (schema->table);
359 g_free (schema->items);
362 g_slice_free (GSettingsSchema, schema);
367 g_settings_schema_get_string (GSettingsSchema *schema,
370 const gchar *result = NULL;
373 if ((value = gvdb_table_get_raw_value (schema->table, key)))
375 result = g_variant_get_string (value, NULL);
376 g_variant_unref (value);
383 g_settings_schema_get_value (GSettingsSchema *schema,
389 value = gvdb_table_get_raw_value (schema->table, key);
391 if G_UNLIKELY (value == NULL)
392 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
394 iter = g_variant_iter_new (value);
395 g_variant_unref (value);
401 g_settings_schema_get_path (GSettingsSchema *schema)
407 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
409 return schema->gettext_domain;
413 g_settings_schema_has_key (GSettingsSchema *schema,
416 return gvdb_table_has_value (schema->table, key);
420 g_settings_schema_list (GSettingsSchema *schema,
425 if (schema->items == NULL)
430 list = gvdb_table_list (schema->table, "");
431 len = list ? g_strv_length (list) : 0;
433 schema->items = g_new (GQuark, len);
436 for (i = 0; i < len; i++)
437 if (list[i][0] != '.')
438 schema->items[j++] = g_quark_from_string (list[i]);
444 *n_items = schema->n_items;
445 return schema->items;
449 g_settings_schema_get_id (GSettingsSchema *schema)
455 endian_fixup (GVariant **value)
457 #if G_BYTE_ORDER == G_BIG_ENDIAN
460 tmp = g_variant_byteswap (*value);
461 g_variant_unref (*value);
467 g_settings_schema_key_init (GSettingsSchemaKey *key,
468 GSettingsSchema *schema,
475 memset (key, 0, sizeof *key);
477 iter = g_settings_schema_get_value (schema, name);
479 key->schema = g_settings_schema_ref (schema);
480 key->default_value = g_variant_iter_next_value (iter);
481 endian_fixup (&key->default_value);
482 key->type = g_variant_get_type (key->default_value);
483 key->name = g_intern_string (name);
485 while (g_variant_iter_next (iter, "(y*)", &code, &data))
490 /* translation requested */
491 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
495 /* enumerated types... */
501 key->is_flags = TRUE;
505 /* ..., choices, aliases */
506 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
510 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
511 endian_fixup (&key->minimum);
512 endian_fixup (&key->maximum);
516 g_warning ("unknown schema extension '%c'", code);
520 g_variant_unref (data);
523 g_variant_iter_free (iter);
527 g_settings_schema_key_clear (GSettingsSchemaKey *key)
530 g_variant_unref (key->minimum);
533 g_variant_unref (key->maximum);
535 g_variant_unref (key->default_value);
537 g_settings_schema_unref (key->schema);
541 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
544 g_return_val_if_fail (value != NULL, FALSE);
546 return g_variant_is_of_type (value, key->type);
550 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
553 if (key->minimum == NULL && key->strinfo == NULL)
556 if (g_variant_is_container (value))
562 g_variant_iter_init (&iter, value);
563 while (ok && (child = g_variant_iter_next_value (&iter)))
565 ok = g_settings_schema_key_range_check (key, child);
566 g_variant_unref (child);
574 return g_variant_compare (key->minimum, value) <= 0 &&
575 g_variant_compare (value, key->maximum) <= 0;
578 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
579 g_variant_get_string (value, NULL));
583 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
588 if (g_settings_schema_key_range_check (key, value))
589 return g_variant_ref (value);
591 if (key->strinfo == NULL)
594 if (g_variant_is_container (value))
596 GVariantBuilder builder;
600 g_variant_iter_init (&iter, value);
601 g_variant_builder_init (&builder, g_variant_get_type (value));
603 while ((child = g_variant_iter_next_value (&iter)))
607 fixed = g_settings_schema_key_range_fixup (key, child);
608 g_variant_unref (child);
612 g_variant_builder_clear (&builder);
616 g_variant_builder_add_value (&builder, fixed);
617 g_variant_unref (fixed);
620 return g_variant_ref_sink (g_variant_builder_end (&builder));
623 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
624 g_variant_get_string (value, NULL));
625 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
630 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
632 const gchar *translated;
633 GError *error = NULL;
637 domain = g_settings_schema_get_gettext_domain (key->schema);
639 if (key->lc_char == '\0')
640 /* translation not requested for this key */
643 if (key->lc_char == 't')
644 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
646 translated = g_dgettext (domain, key->unparsed);
648 if (translated == key->unparsed)
649 /* the default value was not translated */
652 /* try to parse the translation of the unparsed default */
653 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
657 g_warning ("Failed to parse translated string `%s' for "
658 "key `%s' in schema `%s': %s", key->unparsed, key->name,
659 g_settings_schema_get_id (key->schema), error->message);
660 g_warning ("Using untranslated default instead.");
661 g_error_free (error);
664 else if (!g_settings_schema_key_range_check (key, value))
666 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
667 "is outside of valid range", key->unparsed, key->name,
668 g_settings_schema_get_id (key->schema));
669 g_variant_unref (value);
677 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
683 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
684 g_variant_get_string (value, NULL),
687 /* 'value' can only come from the backend after being filtered for validity,
688 * from the translation after being filtered for validity, or from the schema
689 * itself (which the schema compiler checks for validity). If this assertion
690 * fails then it's really a bug in GSettings or the schema compiler...
692 g_assert (it_worked);
698 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
703 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
708 return g_variant_new_string (string);
712 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
720 g_variant_iter_init (&iter, value);
721 while (g_variant_iter_next (&iter, "&s", &flag))
726 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
727 /* as in g_settings_to_enum() */
728 g_assert (it_worked);
730 result |= flag_value;
737 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
740 GVariantBuilder builder;
743 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
745 for (i = 0; i < 32; i++)
746 if (value & (1u << i))
750 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
754 g_variant_builder_clear (&builder);
758 g_variant_builder_add (&builder, "s", string);
761 return g_variant_builder_end (&builder);