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;
51 static GSettingsSchemaSource *schema_sources;
54 prepend_schema_table (GvdbTable *table)
56 GSettingsSchemaSource *source;
58 source = g_slice_new (GSettingsSchemaSource);
59 source->parent = schema_sources;
60 source->table = table;
62 schema_sources = source;
66 initialise_schema_sources (void)
68 static gsize initialised;
70 /* need a separate variable because 'schema_sources' may legitimately
71 * be null if we have zero valid schema sources
73 if G_UNLIKELY (g_once_init_enter (&initialised))
75 const gchar * const *dirs;
79 /* iterate in reverse: count up, then count down */
80 dirs = g_get_system_data_dirs ();
81 for (i = 0; dirs[i]; i++);
88 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
89 table = gvdb_table_new (filename, TRUE, NULL);
92 prepend_schema_table (table);
97 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
102 filename = g_build_filename (path, "gschemas.compiled", NULL);
103 table = gvdb_table_new (filename, TRUE, NULL);
106 prepend_schema_table (table);
111 g_once_init_leave (&initialised, TRUE);
116 steal_item (gpointer key,
120 gchar ***ptr = user_data;
122 *(*ptr)++ = (gchar *) key;
127 static const gchar * const *non_relocatable_schema_list;
128 static const gchar * const *relocatable_schema_list;
129 static gsize schema_lists_initialised;
132 ensure_schema_lists (void)
134 if (g_once_init_enter (&schema_lists_initialised))
136 GSettingsSchemaSource *source;
137 GHashTable *single, *reloc;
142 initialise_schema_sources ();
144 /* We use hash tables to avoid duplicate listings for schemas that
145 * appear in more than one file.
147 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
148 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
150 for (source = schema_sources; source; source = source->parent)
152 list = gvdb_table_list (source->table, "");
154 g_assert (list != NULL);
156 for (i = 0; list[i]; i++)
158 if (!g_hash_table_lookup (single, list[i]) &&
159 !g_hash_table_lookup (reloc, list[i]))
163 table = gvdb_table_get_table (source->table, list[i]);
164 g_assert (table != NULL);
166 if (gvdb_table_has_value (table, ".path"))
167 g_hash_table_insert (single, g_strdup (list[i]), NULL);
169 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
171 gvdb_table_unref (table);
178 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
179 non_relocatable_schema_list = ptr;
180 g_hash_table_foreach_steal (single, steal_item, &ptr);
181 g_hash_table_unref (single);
184 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
185 relocatable_schema_list = ptr;
186 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
187 g_hash_table_unref (reloc);
190 g_once_init_leave (&schema_lists_initialised, TRUE);
195 * g_settings_list_schemas:
197 * Gets a list of the #GSettings schemas installed on the system. The
198 * returned list is exactly the list of schemas for which you may call
199 * g_settings_new() without adverse effects.
201 * This function does not list the schemas that do not provide their own
202 * paths (ie: schemas for which you must use
203 * g_settings_new_with_path()). See
204 * g_settings_list_relocatable_schemas() for that.
206 * Returns: (element-type utf8) (transfer none): a list of #GSettings
207 * schemas that are available. The list must not be modified or
212 const gchar * const *
213 g_settings_list_schemas (void)
215 ensure_schema_lists ();
217 return non_relocatable_schema_list;
221 * g_settings_list_relocatable_schemas:
223 * Gets a list of the relocatable #GSettings schemas installed on the
224 * system. These are schemas that do not provide their own path. It is
225 * usual to instantiate these schemas directly, but if you want to you
226 * can use g_settings_new_with_path() to specify the path.
228 * The output of this function, tTaken together with the output of
229 * g_settings_list_schemas() represents the complete list of all
232 * Returns: (element-type utf8) (transfer none): a list of relocatable
233 * #GSettings schemas that are available. The list must not be
238 const gchar * const *
239 g_settings_list_relocatable_schemas (void)
241 ensure_schema_lists ();
243 return relocatable_schema_list;
247 g_settings_schema_ref (GSettingsSchema *schema)
249 g_atomic_int_inc (&schema->ref_count);
255 g_settings_schema_unref (GSettingsSchema *schema)
257 if (g_atomic_int_dec_and_test (&schema->ref_count))
259 gvdb_table_unref (schema->table);
260 g_free (schema->items);
261 g_free (schema->name);
263 g_slice_free (GSettingsSchema, schema);
268 g_settings_schema_get_string (GSettingsSchema *schema,
271 const gchar *result = NULL;
274 if ((value = gvdb_table_get_raw_value (schema->table, key)))
276 result = g_variant_get_string (value, NULL);
277 g_variant_unref (value);
284 g_settings_schema_new (const gchar *name)
286 GSettingsSchemaSource *source;
287 GSettingsSchema *schema;
288 GvdbTable *table = NULL;
290 g_return_val_if_fail (name != NULL, NULL);
292 initialise_schema_sources ();
294 for (source = schema_sources; source; source = source->parent)
295 if ((table = gvdb_table_get_table (source->table, name)))
299 g_error ("Settings schema '%s' is not installed\n", name);
301 schema = g_slice_new0 (GSettingsSchema);
302 schema->ref_count = 1;
303 schema->name = g_strdup (name);
304 schema->table = table;
306 g_settings_schema_get_string (schema, ".path");
307 schema->gettext_domain =
308 g_settings_schema_get_string (schema, ".gettext-domain");
310 if (schema->gettext_domain)
311 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
317 g_settings_schema_get_value (GSettingsSchema *schema,
323 value = gvdb_table_get_raw_value (schema->table, key);
325 if G_UNLIKELY (value == NULL)
326 g_error ("Settings schema '%s' does not contain a key named '%s'",
329 iter = g_variant_iter_new (value);
330 g_variant_unref (value);
336 g_settings_schema_get_path (GSettingsSchema *schema)
342 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
344 return schema->gettext_domain;
348 g_settings_schema_has_key (GSettingsSchema *schema,
351 return gvdb_table_has_value (schema->table, key);
355 g_settings_schema_list (GSettingsSchema *schema,
360 if (schema->items == NULL)
365 list = gvdb_table_list (schema->table, "");
366 len = list ? g_strv_length (list) : 0;
368 schema->items = g_new (GQuark, len);
371 for (i = 0; i < len; i++)
372 if (list[i][0] != '.')
373 schema->items[j++] = g_quark_from_string (list[i]);
379 *n_items = schema->n_items;
380 return schema->items;
384 g_settings_schema_get_name (GSettingsSchema *schema)
390 endian_fixup (GVariant **value)
392 #if G_BYTE_ORDER == G_BIG_ENDIAN
395 tmp = g_variant_byteswap (*value);
396 g_variant_unref (*value);
402 g_settings_schema_key_init (GSettingsSchemaKey *key,
403 GSettingsSchema *schema,
410 memset (key, 0, sizeof *key);
412 iter = g_settings_schema_get_value (schema, name);
414 key->schema = g_settings_schema_ref (schema);
415 key->default_value = g_variant_iter_next_value (iter);
416 endian_fixup (&key->default_value);
417 key->type = g_variant_get_type (key->default_value);
418 key->name = g_intern_string (name);
420 while (g_variant_iter_next (iter, "(y*)", &code, &data))
425 /* translation requested */
426 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
430 /* enumerated types... */
436 key->is_flags = TRUE;
440 /* ..., choices, aliases */
441 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
445 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
446 endian_fixup (&key->minimum);
447 endian_fixup (&key->maximum);
451 g_warning ("unknown schema extension '%c'", code);
455 g_variant_unref (data);
458 g_variant_iter_free (iter);
462 g_settings_schema_key_clear (GSettingsSchemaKey *key)
465 g_variant_unref (key->minimum);
468 g_variant_unref (key->maximum);
470 g_variant_unref (key->default_value);
472 g_settings_schema_unref (key->schema);
476 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
479 g_return_val_if_fail (value != NULL, FALSE);
481 return g_variant_is_of_type (value, key->type);
485 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
488 if (key->minimum == NULL && key->strinfo == NULL)
491 if (g_variant_is_container (value))
497 g_variant_iter_init (&iter, value);
498 while (ok && (child = g_variant_iter_next_value (&iter)))
500 ok = g_settings_schema_key_range_check (key, child);
501 g_variant_unref (child);
509 return g_variant_compare (key->minimum, value) <= 0 &&
510 g_variant_compare (value, key->maximum) <= 0;
513 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
514 g_variant_get_string (value, NULL));
518 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
523 if (g_settings_schema_key_range_check (key, value))
524 return g_variant_ref (value);
526 if (key->strinfo == NULL)
529 if (g_variant_is_container (value))
531 GVariantBuilder builder;
535 g_variant_iter_init (&iter, value);
536 g_variant_builder_init (&builder, g_variant_get_type (value));
538 while ((child = g_variant_iter_next_value (&iter)))
542 fixed = g_settings_schema_key_range_fixup (key, child);
543 g_variant_unref (child);
547 g_variant_builder_clear (&builder);
551 g_variant_builder_add_value (&builder, fixed);
552 g_variant_unref (fixed);
555 return g_variant_ref_sink (g_variant_builder_end (&builder));
558 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
559 g_variant_get_string (value, NULL));
560 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
565 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
567 const gchar *translated;
568 GError *error = NULL;
572 domain = g_settings_schema_get_gettext_domain (key->schema);
574 if (key->lc_char == '\0')
575 /* translation not requested for this key */
578 if (key->lc_char == 't')
579 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
581 translated = g_dgettext (domain, key->unparsed);
583 if (translated == key->unparsed)
584 /* the default value was not translated */
587 /* try to parse the translation of the unparsed default */
588 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
592 g_warning ("Failed to parse translated string `%s' for "
593 "key `%s' in schema `%s': %s", key->unparsed, key->name,
594 g_settings_schema_get_name (key->schema), error->message);
595 g_warning ("Using untranslated default instead.");
596 g_error_free (error);
599 else if (!g_settings_schema_key_range_check (key, value))
601 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
602 "is outside of valid range", key->unparsed, key->name,
603 g_settings_schema_get_name (key->schema));
604 g_variant_unref (value);
612 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
618 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
619 g_variant_get_string (value, NULL),
622 /* 'value' can only come from the backend after being filtered for validity,
623 * from the translation after being filtered for validity, or from the schema
624 * itself (which the schema compiler checks for validity). If this assertion
625 * fails then it's really a bug in GSettings or the schema compiler...
627 g_assert (it_worked);
633 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
638 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
643 return g_variant_new_string (string);
647 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
655 g_variant_iter_init (&iter, value);
656 while (g_variant_iter_next (&iter, "&s", &flag))
661 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
662 /* as in g_settings_to_enum() */
663 g_assert (it_worked);
665 result |= flag_value;
672 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
675 GVariantBuilder builder;
678 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
680 for (i = 0; i < 32; i++)
681 if (value & (1u << i))
685 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
689 g_variant_builder_clear (&builder);
693 g_variant_builder_add (&builder, "s", string);
696 return g_variant_builder_end (&builder);