2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gsettingsschema-internal.h"
23 #include "gsettings.h"
25 #include "gvdb/gvdb-reader.h"
31 struct _GSettingsSchema
33 const gchar *gettext_domain;
43 typedef struct _GSettingsSchemaSource GSettingsSchemaSource;
45 struct _GSettingsSchemaSource
47 GSettingsSchemaSource *parent;
53 static GSettingsSchemaSource *schema_sources;
56 prepend_schema_table (GvdbTable *table)
58 GSettingsSchemaSource *source;
60 /* we steal the reference from 'schema_sources' for our ->parent */
61 source = g_slice_new (GSettingsSchemaSource);
62 source->parent = schema_sources;
63 source->table = table;
64 source->ref_count = 1;
66 schema_sources = source;
69 GSettingsSchemaSource *
70 g_settings_schema_source_ref (GSettingsSchemaSource *source)
72 g_atomic_int_inc (&source->ref_count);
78 g_settings_schema_source_unref (GSettingsSchemaSource *source)
80 if (g_atomic_int_dec_and_test (&source->ref_count))
82 if (source == schema_sources)
83 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
85 g_settings_schema_source_unref (source->parent);
86 gvdb_table_unref (source->table);
88 g_slice_free (GSettingsSchemaSource, source);
93 initialise_schema_sources (void)
95 static gsize initialised;
97 /* need a separate variable because 'schema_sources' may legitimately
98 * be null if we have zero valid schema sources
100 if G_UNLIKELY (g_once_init_enter (&initialised))
102 const gchar * const *dirs;
106 /* iterate in reverse: count up, then count down */
107 dirs = g_get_system_data_dirs ();
108 for (i = 0; dirs[i]; i++);
115 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
116 table = gvdb_table_new (filename, TRUE, NULL);
119 prepend_schema_table (table);
124 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
129 filename = g_build_filename (path, "gschemas.compiled", NULL);
130 table = gvdb_table_new (filename, TRUE, NULL);
133 prepend_schema_table (table);
138 g_once_init_leave (&initialised, TRUE);
142 GSettingsSchemaSource *
143 g_settings_schema_source_get_default (void)
145 initialise_schema_sources ();
147 return schema_sources;
151 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
152 const gchar *schema_name,
155 GSettingsSchema *schema;
158 g_return_val_if_fail (source != NULL, NULL);
159 g_return_val_if_fail (schema_name != NULL, NULL);
161 table = gvdb_table_get_table (source->table, schema_name);
163 if (table == NULL && recursive)
164 for (source = source->parent; source; source = source->parent)
165 if ((table = gvdb_table_get_table (source->table, schema_name)))
171 schema = g_slice_new0 (GSettingsSchema);
172 schema->ref_count = 1;
173 schema->name = g_strdup (schema_name);
174 schema->table = table;
175 schema->path = g_settings_schema_get_string (schema, ".path");
176 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
178 if (schema->gettext_domain)
179 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
185 steal_item (gpointer key,
189 gchar ***ptr = user_data;
191 *(*ptr)++ = (gchar *) key;
196 static const gchar * const *non_relocatable_schema_list;
197 static const gchar * const *relocatable_schema_list;
198 static gsize schema_lists_initialised;
201 ensure_schema_lists (void)
203 if (g_once_init_enter (&schema_lists_initialised))
205 GSettingsSchemaSource *source;
206 GHashTable *single, *reloc;
211 initialise_schema_sources ();
213 /* We use hash tables to avoid duplicate listings for schemas that
214 * appear in more than one file.
216 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
217 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
219 for (source = schema_sources; source; source = source->parent)
221 list = gvdb_table_list (source->table, "");
223 g_assert (list != NULL);
225 for (i = 0; list[i]; i++)
227 if (!g_hash_table_lookup (single, list[i]) &&
228 !g_hash_table_lookup (reloc, list[i]))
232 table = gvdb_table_get_table (source->table, list[i]);
233 g_assert (table != NULL);
235 if (gvdb_table_has_value (table, ".path"))
236 g_hash_table_insert (single, g_strdup (list[i]), NULL);
238 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
240 gvdb_table_unref (table);
247 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
248 non_relocatable_schema_list = ptr;
249 g_hash_table_foreach_steal (single, steal_item, &ptr);
250 g_hash_table_unref (single);
253 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
254 relocatable_schema_list = ptr;
255 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
256 g_hash_table_unref (reloc);
259 g_once_init_leave (&schema_lists_initialised, TRUE);
264 * g_settings_list_schemas:
266 * Gets a list of the #GSettings schemas installed on the system. The
267 * returned list is exactly the list of schemas for which you may call
268 * g_settings_new() without adverse effects.
270 * This function does not list the schemas that do not provide their own
271 * paths (ie: schemas for which you must use
272 * g_settings_new_with_path()). See
273 * g_settings_list_relocatable_schemas() for that.
275 * Returns: (element-type utf8) (transfer none): a list of #GSettings
276 * schemas that are available. The list must not be modified or
281 const gchar * const *
282 g_settings_list_schemas (void)
284 ensure_schema_lists ();
286 return non_relocatable_schema_list;
290 * g_settings_list_relocatable_schemas:
292 * Gets a list of the relocatable #GSettings schemas installed on the
293 * system. These are schemas that do not provide their own path. It is
294 * usual to instantiate these schemas directly, but if you want to you
295 * can use g_settings_new_with_path() to specify the path.
297 * The output of this function, tTaken together with the output of
298 * g_settings_list_schemas() represents the complete list of all
301 * Returns: (element-type utf8) (transfer none): a list of relocatable
302 * #GSettings schemas that are available. The list must not be
307 const gchar * const *
308 g_settings_list_relocatable_schemas (void)
310 ensure_schema_lists ();
312 return relocatable_schema_list;
316 g_settings_schema_ref (GSettingsSchema *schema)
318 g_atomic_int_inc (&schema->ref_count);
324 g_settings_schema_unref (GSettingsSchema *schema)
326 if (g_atomic_int_dec_and_test (&schema->ref_count))
328 gvdb_table_unref (schema->table);
329 g_free (schema->items);
330 g_free (schema->name);
332 g_slice_free (GSettingsSchema, schema);
337 g_settings_schema_get_string (GSettingsSchema *schema,
340 const gchar *result = NULL;
343 if ((value = gvdb_table_get_raw_value (schema->table, key)))
345 result = g_variant_get_string (value, NULL);
346 g_variant_unref (value);
353 g_settings_schema_new (const gchar *name)
355 GSettingsSchemaSource *source;
356 GSettingsSchema *schema;
358 source = g_settings_schema_source_get_default ();
361 g_error ("No GSettings schemas are installed on the system");
363 schema = g_settings_schema_source_lookup (source, name, TRUE);
366 g_error ("Settings schema '%s' is not installed\n", name);
372 g_settings_schema_get_value (GSettingsSchema *schema,
378 value = gvdb_table_get_raw_value (schema->table, key);
380 if G_UNLIKELY (value == NULL)
381 g_error ("Settings schema '%s' does not contain a key named '%s'",
384 iter = g_variant_iter_new (value);
385 g_variant_unref (value);
391 g_settings_schema_get_path (GSettingsSchema *schema)
397 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
399 return schema->gettext_domain;
403 g_settings_schema_has_key (GSettingsSchema *schema,
406 return gvdb_table_has_value (schema->table, key);
410 g_settings_schema_list (GSettingsSchema *schema,
415 if (schema->items == NULL)
420 list = gvdb_table_list (schema->table, "");
421 len = list ? g_strv_length (list) : 0;
423 schema->items = g_new (GQuark, len);
426 for (i = 0; i < len; i++)
427 if (list[i][0] != '.')
428 schema->items[j++] = g_quark_from_string (list[i]);
434 *n_items = schema->n_items;
435 return schema->items;
439 g_settings_schema_get_name (GSettingsSchema *schema)
445 endian_fixup (GVariant **value)
447 #if G_BYTE_ORDER == G_BIG_ENDIAN
450 tmp = g_variant_byteswap (*value);
451 g_variant_unref (*value);
457 g_settings_schema_key_init (GSettingsSchemaKey *key,
458 GSettingsSchema *schema,
465 memset (key, 0, sizeof *key);
467 iter = g_settings_schema_get_value (schema, name);
469 key->schema = g_settings_schema_ref (schema);
470 key->default_value = g_variant_iter_next_value (iter);
471 endian_fixup (&key->default_value);
472 key->type = g_variant_get_type (key->default_value);
473 key->name = g_intern_string (name);
475 while (g_variant_iter_next (iter, "(y*)", &code, &data))
480 /* translation requested */
481 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
485 /* enumerated types... */
491 key->is_flags = TRUE;
495 /* ..., choices, aliases */
496 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
500 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
501 endian_fixup (&key->minimum);
502 endian_fixup (&key->maximum);
506 g_warning ("unknown schema extension '%c'", code);
510 g_variant_unref (data);
513 g_variant_iter_free (iter);
517 g_settings_schema_key_clear (GSettingsSchemaKey *key)
520 g_variant_unref (key->minimum);
523 g_variant_unref (key->maximum);
525 g_variant_unref (key->default_value);
527 g_settings_schema_unref (key->schema);
531 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
534 g_return_val_if_fail (value != NULL, FALSE);
536 return g_variant_is_of_type (value, key->type);
540 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
543 if (key->minimum == NULL && key->strinfo == NULL)
546 if (g_variant_is_container (value))
552 g_variant_iter_init (&iter, value);
553 while (ok && (child = g_variant_iter_next_value (&iter)))
555 ok = g_settings_schema_key_range_check (key, child);
556 g_variant_unref (child);
564 return g_variant_compare (key->minimum, value) <= 0 &&
565 g_variant_compare (value, key->maximum) <= 0;
568 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
569 g_variant_get_string (value, NULL));
573 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
578 if (g_settings_schema_key_range_check (key, value))
579 return g_variant_ref (value);
581 if (key->strinfo == NULL)
584 if (g_variant_is_container (value))
586 GVariantBuilder builder;
590 g_variant_iter_init (&iter, value);
591 g_variant_builder_init (&builder, g_variant_get_type (value));
593 while ((child = g_variant_iter_next_value (&iter)))
597 fixed = g_settings_schema_key_range_fixup (key, child);
598 g_variant_unref (child);
602 g_variant_builder_clear (&builder);
606 g_variant_builder_add_value (&builder, fixed);
607 g_variant_unref (fixed);
610 return g_variant_ref_sink (g_variant_builder_end (&builder));
613 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
614 g_variant_get_string (value, NULL));
615 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
620 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
622 const gchar *translated;
623 GError *error = NULL;
627 domain = g_settings_schema_get_gettext_domain (key->schema);
629 if (key->lc_char == '\0')
630 /* translation not requested for this key */
633 if (key->lc_char == 't')
634 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
636 translated = g_dgettext (domain, key->unparsed);
638 if (translated == key->unparsed)
639 /* the default value was not translated */
642 /* try to parse the translation of the unparsed default */
643 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
647 g_warning ("Failed to parse translated string `%s' for "
648 "key `%s' in schema `%s': %s", key->unparsed, key->name,
649 g_settings_schema_get_name (key->schema), error->message);
650 g_warning ("Using untranslated default instead.");
651 g_error_free (error);
654 else if (!g_settings_schema_key_range_check (key, value))
656 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
657 "is outside of valid range", key->unparsed, key->name,
658 g_settings_schema_get_name (key->schema));
659 g_variant_unref (value);
667 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
673 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
674 g_variant_get_string (value, NULL),
677 /* 'value' can only come from the backend after being filtered for validity,
678 * from the translation after being filtered for validity, or from the schema
679 * itself (which the schema compiler checks for validity). If this assertion
680 * fails then it's really a bug in GSettings or the schema compiler...
682 g_assert (it_worked);
688 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
693 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
698 return g_variant_new_string (string);
702 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
710 g_variant_iter_init (&iter, value);
711 while (g_variant_iter_next (&iter, "&s", &flag))
716 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
717 /* as in g_settings_to_enum() */
718 g_assert (it_worked);
720 result |= flag_value;
727 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
730 GVariantBuilder builder;
733 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
735 for (i = 0; i < 32; i++)
736 if (value & (1u << i))
740 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
744 g_variant_builder_clear (&builder);
748 g_variant_builder_add (&builder, "s", string);
751 return g_variant_builder_end (&builder);