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 struct _GSettingsSchemaSource
48 GSettingsSchemaSource *parent;
54 static GSettingsSchemaSource *schema_sources;
57 prepend_schema_table (GvdbTable *table)
59 GSettingsSchemaSource *source;
61 /* we steal the reference from 'schema_sources' for our ->parent */
62 source = g_slice_new (GSettingsSchemaSource);
63 source->parent = schema_sources;
64 source->table = table;
65 source->ref_count = 1;
67 schema_sources = source;
70 GSettingsSchemaSource *
71 g_settings_schema_source_ref (GSettingsSchemaSource *source)
73 g_atomic_int_inc (&source->ref_count);
79 g_settings_schema_source_unref (GSettingsSchemaSource *source)
81 if (g_atomic_int_dec_and_test (&source->ref_count))
83 if (source == schema_sources)
84 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
86 g_settings_schema_source_unref (source->parent);
87 gvdb_table_unref (source->table);
89 g_slice_free (GSettingsSchemaSource, source);
94 initialise_schema_sources (void)
96 static gsize initialised;
98 /* need a separate variable because 'schema_sources' may legitimately
99 * be null if we have zero valid schema sources
101 if G_UNLIKELY (g_once_init_enter (&initialised))
103 const gchar * const *dirs;
107 /* iterate in reverse: count up, then count down */
108 dirs = g_get_system_data_dirs ();
109 for (i = 0; dirs[i]; i++);
116 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
117 table = gvdb_table_new (filename, TRUE, NULL);
120 prepend_schema_table (table);
125 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
130 filename = g_build_filename (path, "gschemas.compiled", NULL);
131 table = gvdb_table_new (filename, TRUE, NULL);
134 prepend_schema_table (table);
139 g_once_init_leave (&initialised, TRUE);
143 GSettingsSchemaSource *
144 g_settings_schema_source_get_default (void)
146 initialise_schema_sources ();
148 return schema_sources;
152 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
153 const gchar *schema_name,
156 GSettingsSchema *schema;
159 g_return_val_if_fail (source != NULL, NULL);
160 g_return_val_if_fail (schema_name != NULL, NULL);
162 table = gvdb_table_get_table (source->table, schema_name);
164 if (table == NULL && recursive)
165 for (source = source->parent; source; source = source->parent)
166 if ((table = gvdb_table_get_table (source->table, schema_name)))
172 schema = g_slice_new0 (GSettingsSchema);
173 schema->ref_count = 1;
174 schema->name = g_strdup (schema_name);
175 schema->table = table;
176 schema->path = g_settings_schema_get_string (schema, ".path");
177 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
179 if (schema->gettext_domain)
180 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
186 steal_item (gpointer key,
190 gchar ***ptr = user_data;
192 *(*ptr)++ = (gchar *) key;
197 static const gchar * const *non_relocatable_schema_list;
198 static const gchar * const *relocatable_schema_list;
199 static gsize schema_lists_initialised;
202 ensure_schema_lists (void)
204 if (g_once_init_enter (&schema_lists_initialised))
206 GSettingsSchemaSource *source;
207 GHashTable *single, *reloc;
212 initialise_schema_sources ();
214 /* We use hash tables to avoid duplicate listings for schemas that
215 * appear in more than one file.
217 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
218 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
220 for (source = schema_sources; source; source = source->parent)
222 list = gvdb_table_list (source->table, "");
224 g_assert (list != NULL);
226 for (i = 0; list[i]; i++)
228 if (!g_hash_table_lookup (single, list[i]) &&
229 !g_hash_table_lookup (reloc, list[i]))
233 table = gvdb_table_get_table (source->table, list[i]);
234 g_assert (table != NULL);
236 if (gvdb_table_has_value (table, ".path"))
237 g_hash_table_insert (single, g_strdup (list[i]), NULL);
239 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
241 gvdb_table_unref (table);
248 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
249 non_relocatable_schema_list = ptr;
250 g_hash_table_foreach_steal (single, steal_item, &ptr);
251 g_hash_table_unref (single);
254 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
255 relocatable_schema_list = ptr;
256 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
257 g_hash_table_unref (reloc);
260 g_once_init_leave (&schema_lists_initialised, TRUE);
265 * g_settings_list_schemas:
267 * Gets a list of the #GSettings schemas installed on the system. The
268 * returned list is exactly the list of schemas for which you may call
269 * g_settings_new() without adverse effects.
271 * This function does not list the schemas that do not provide their own
272 * paths (ie: schemas for which you must use
273 * g_settings_new_with_path()). See
274 * g_settings_list_relocatable_schemas() for that.
276 * Returns: (element-type utf8) (transfer none): a list of #GSettings
277 * schemas that are available. The list must not be modified or
282 const gchar * const *
283 g_settings_list_schemas (void)
285 ensure_schema_lists ();
287 return non_relocatable_schema_list;
291 * g_settings_list_relocatable_schemas:
293 * Gets a list of the relocatable #GSettings schemas installed on the
294 * system. These are schemas that do not provide their own path. It is
295 * usual to instantiate these schemas directly, but if you want to you
296 * can use g_settings_new_with_path() to specify the path.
298 * The output of this function, tTaken together with the output of
299 * g_settings_list_schemas() represents the complete list of all
302 * Returns: (element-type utf8) (transfer none): a list of relocatable
303 * #GSettings schemas that are available. The list must not be
308 const gchar * const *
309 g_settings_list_relocatable_schemas (void)
311 ensure_schema_lists ();
313 return relocatable_schema_list;
317 g_settings_schema_ref (GSettingsSchema *schema)
319 g_atomic_int_inc (&schema->ref_count);
325 g_settings_schema_unref (GSettingsSchema *schema)
327 if (g_atomic_int_dec_and_test (&schema->ref_count))
329 gvdb_table_unref (schema->table);
330 g_free (schema->items);
331 g_free (schema->name);
333 g_slice_free (GSettingsSchema, schema);
338 g_settings_schema_get_string (GSettingsSchema *schema,
341 const gchar *result = NULL;
344 if ((value = gvdb_table_get_raw_value (schema->table, key)))
346 result = g_variant_get_string (value, NULL);
347 g_variant_unref (value);
354 g_settings_schema_new (const gchar *name)
356 GSettingsSchemaSource *source;
357 GSettingsSchema *schema;
359 source = g_settings_schema_source_get_default ();
362 g_error ("No GSettings schemas are installed on the system");
364 schema = g_settings_schema_source_lookup (source, name, TRUE);
367 g_error ("Settings schema '%s' is not installed\n", name);
373 g_settings_schema_get_value (GSettingsSchema *schema,
379 value = gvdb_table_get_raw_value (schema->table, key);
381 if G_UNLIKELY (value == NULL)
382 g_error ("Settings schema '%s' does not contain a key named '%s'",
385 iter = g_variant_iter_new (value);
386 g_variant_unref (value);
392 g_settings_schema_get_path (GSettingsSchema *schema)
398 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
400 return schema->gettext_domain;
404 g_settings_schema_has_key (GSettingsSchema *schema,
407 return gvdb_table_has_value (schema->table, key);
411 g_settings_schema_list (GSettingsSchema *schema,
416 if (schema->items == NULL)
421 list = gvdb_table_list (schema->table, "");
422 len = list ? g_strv_length (list) : 0;
424 schema->items = g_new (GQuark, len);
427 for (i = 0; i < len; i++)
428 if (list[i][0] != '.')
429 schema->items[j++] = g_quark_from_string (list[i]);
435 *n_items = schema->n_items;
436 return schema->items;
440 g_settings_schema_get_name (GSettingsSchema *schema)
446 endian_fixup (GVariant **value)
448 #if G_BYTE_ORDER == G_BIG_ENDIAN
451 tmp = g_variant_byteswap (*value);
452 g_variant_unref (*value);
458 g_settings_schema_key_init (GSettingsSchemaKey *key,
459 GSettingsSchema *schema,
466 memset (key, 0, sizeof *key);
468 iter = g_settings_schema_get_value (schema, name);
470 key->schema = g_settings_schema_ref (schema);
471 key->default_value = g_variant_iter_next_value (iter);
472 endian_fixup (&key->default_value);
473 key->type = g_variant_get_type (key->default_value);
474 key->name = g_intern_string (name);
476 while (g_variant_iter_next (iter, "(y*)", &code, &data))
481 /* translation requested */
482 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
486 /* enumerated types... */
492 key->is_flags = TRUE;
496 /* ..., choices, aliases */
497 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
501 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
502 endian_fixup (&key->minimum);
503 endian_fixup (&key->maximum);
507 g_warning ("unknown schema extension '%c'", code);
511 g_variant_unref (data);
514 g_variant_iter_free (iter);
518 g_settings_schema_key_clear (GSettingsSchemaKey *key)
521 g_variant_unref (key->minimum);
524 g_variant_unref (key->maximum);
526 g_variant_unref (key->default_value);
528 g_settings_schema_unref (key->schema);
532 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
535 g_return_val_if_fail (value != NULL, FALSE);
537 return g_variant_is_of_type (value, key->type);
541 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
544 if (key->minimum == NULL && key->strinfo == NULL)
547 if (g_variant_is_container (value))
553 g_variant_iter_init (&iter, value);
554 while (ok && (child = g_variant_iter_next_value (&iter)))
556 ok = g_settings_schema_key_range_check (key, child);
557 g_variant_unref (child);
565 return g_variant_compare (key->minimum, value) <= 0 &&
566 g_variant_compare (value, key->maximum) <= 0;
569 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
570 g_variant_get_string (value, NULL));
574 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
579 if (g_settings_schema_key_range_check (key, value))
580 return g_variant_ref (value);
582 if (key->strinfo == NULL)
585 if (g_variant_is_container (value))
587 GVariantBuilder builder;
591 g_variant_iter_init (&iter, value);
592 g_variant_builder_init (&builder, g_variant_get_type (value));
594 while ((child = g_variant_iter_next_value (&iter)))
598 fixed = g_settings_schema_key_range_fixup (key, child);
599 g_variant_unref (child);
603 g_variant_builder_clear (&builder);
607 g_variant_builder_add_value (&builder, fixed);
608 g_variant_unref (fixed);
611 return g_variant_ref_sink (g_variant_builder_end (&builder));
614 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
615 g_variant_get_string (value, NULL));
616 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
621 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
623 const gchar *translated;
624 GError *error = NULL;
628 domain = g_settings_schema_get_gettext_domain (key->schema);
630 if (key->lc_char == '\0')
631 /* translation not requested for this key */
634 if (key->lc_char == 't')
635 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
637 translated = g_dgettext (domain, key->unparsed);
639 if (translated == key->unparsed)
640 /* the default value was not translated */
643 /* try to parse the translation of the unparsed default */
644 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
648 g_warning ("Failed to parse translated string `%s' for "
649 "key `%s' in schema `%s': %s", key->unparsed, key->name,
650 g_settings_schema_get_name (key->schema), error->message);
651 g_warning ("Using untranslated default instead.");
652 g_error_free (error);
655 else if (!g_settings_schema_key_range_check (key, value))
657 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
658 "is outside of valid range", key->unparsed, key->name,
659 g_settings_schema_get_name (key->schema));
660 g_variant_unref (value);
668 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
674 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
675 g_variant_get_string (value, NULL),
678 /* 'value' can only come from the backend after being filtered for validity,
679 * from the translation after being filtered for validity, or from the schema
680 * itself (which the schema compiler checks for validity). If this assertion
681 * fails then it's really a bug in GSettings or the schema compiler...
683 g_assert (it_worked);
689 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
694 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
699 return g_variant_new_string (string);
703 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
711 g_variant_iter_init (&iter, value);
712 while (g_variant_iter_next (&iter, "&s", &flag))
717 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
718 /* as in g_settings_to_enum() */
719 g_assert (it_worked);
721 result |= flag_value;
728 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
731 GVariantBuilder builder;
734 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
736 for (i = 0; i < 32; i++)
737 if (value & (1u << i))
741 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
745 g_variant_builder_clear (&builder);
749 g_variant_builder_add (&builder, "s", string);
752 return g_variant_builder_end (&builder);