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 static GSList *schema_sources;
46 initialise_schema_sources (void)
48 static gsize initialised;
50 /* need a separate variable because 'schema_sources' may legitimately
51 * be null if we have zero valid schema sources
53 if G_UNLIKELY (g_once_init_enter (&initialised))
55 const gchar * const *dirs;
59 /* iterate in reverse: count up, then count down */
60 dirs = g_get_system_data_dirs ();
61 for (i = 0; dirs[i]; i++);
68 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
69 table = gvdb_table_new (filename, TRUE, NULL);
72 schema_sources = g_slist_prepend (schema_sources, table);
77 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
82 filename = g_build_filename (path, "gschemas.compiled", NULL);
83 table = gvdb_table_new (filename, TRUE, NULL);
86 schema_sources = g_slist_prepend (schema_sources, table);
91 g_once_init_leave (&initialised, TRUE);
96 steal_item (gpointer key,
100 gchar ***ptr = user_data;
102 *(*ptr)++ = (gchar *) key;
107 static const gchar * const *non_relocatable_schema_list;
108 static const gchar * const *relocatable_schema_list;
109 static gsize schema_lists_initialised;
112 ensure_schema_lists (void)
114 if (g_once_init_enter (&schema_lists_initialised))
116 GHashTable *single, *reloc;
122 initialise_schema_sources ();
124 /* We use hash tables to avoid duplicate listings for schemas that
125 * appear in more than one file.
127 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
128 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
130 for (source = schema_sources; source; source = source->next)
132 list = gvdb_table_list (source->data, "");
134 g_assert (list != NULL);
136 for (i = 0; list[i]; i++)
138 if (!g_hash_table_lookup (single, list[i]) &&
139 !g_hash_table_lookup (reloc, list[i]))
143 table = gvdb_table_get_table (source->data, list[i]);
144 g_assert (table != NULL);
146 if (gvdb_table_has_value (table, ".path"))
147 g_hash_table_insert (single, g_strdup (list[i]), NULL);
149 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
151 gvdb_table_unref (table);
158 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
159 non_relocatable_schema_list = ptr;
160 g_hash_table_foreach_steal (single, steal_item, &ptr);
161 g_hash_table_unref (single);
164 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
165 relocatable_schema_list = ptr;
166 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
167 g_hash_table_unref (reloc);
170 g_once_init_leave (&schema_lists_initialised, TRUE);
175 * g_settings_list_schemas:
177 * Gets a list of the #GSettings schemas installed on the system. The
178 * returned list is exactly the list of schemas for which you may call
179 * g_settings_new() without adverse effects.
181 * This function does not list the schemas that do not provide their own
182 * paths (ie: schemas for which you must use
183 * g_settings_new_with_path()). See
184 * g_settings_list_relocatable_schemas() for that.
186 * Returns: (element-type utf8) (transfer none): a list of #GSettings
187 * schemas that are available. The list must not be modified or
192 const gchar * const *
193 g_settings_list_schemas (void)
195 ensure_schema_lists ();
197 return non_relocatable_schema_list;
201 * g_settings_list_relocatable_schemas:
203 * Gets a list of the relocatable #GSettings schemas installed on the
204 * system. These are schemas that do not provide their own path. It is
205 * usual to instantiate these schemas directly, but if you want to you
206 * can use g_settings_new_with_path() to specify the path.
208 * The output of this function, tTaken together with the output of
209 * g_settings_list_schemas() represents the complete list of all
212 * Returns: (element-type utf8) (transfer none): a list of relocatable
213 * #GSettings schemas that are available. The list must not be
218 const gchar * const *
219 g_settings_list_relocatable_schemas (void)
221 ensure_schema_lists ();
223 return relocatable_schema_list;
227 g_settings_schema_ref (GSettingsSchema *schema)
229 g_atomic_int_inc (&schema->ref_count);
235 g_settings_schema_unref (GSettingsSchema *schema)
237 if (g_atomic_int_dec_and_test (&schema->ref_count))
239 gvdb_table_unref (schema->table);
240 g_free (schema->items);
241 g_free (schema->name);
243 g_slice_free (GSettingsSchema, schema);
248 g_settings_schema_get_string (GSettingsSchema *schema,
251 const gchar *result = NULL;
254 if ((value = gvdb_table_get_raw_value (schema->table, key)))
256 result = g_variant_get_string (value, NULL);
257 g_variant_unref (value);
264 g_settings_schema_new (const gchar *name)
266 GSettingsSchema *schema;
267 GvdbTable *table = NULL;
270 g_return_val_if_fail (name != NULL, NULL);
272 initialise_schema_sources ();
274 for (source = schema_sources; source; source = source->next)
276 GvdbTable *file = source->data;
278 if ((table = gvdb_table_get_table (file, name)))
283 g_error ("Settings schema '%s' is not installed\n", name);
285 schema = g_slice_new0 (GSettingsSchema);
286 schema->ref_count = 1;
287 schema->name = g_strdup (name);
288 schema->table = table;
290 g_settings_schema_get_string (schema, ".path");
291 schema->gettext_domain =
292 g_settings_schema_get_string (schema, ".gettext-domain");
294 if (schema->gettext_domain)
295 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
301 g_settings_schema_get_value (GSettingsSchema *schema,
307 value = gvdb_table_get_raw_value (schema->table, key);
309 if G_UNLIKELY (value == NULL)
310 g_error ("Settings schema '%s' does not contain a key named '%s'",
313 iter = g_variant_iter_new (value);
314 g_variant_unref (value);
320 g_settings_schema_get_path (GSettingsSchema *schema)
326 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
328 return schema->gettext_domain;
332 g_settings_schema_has_key (GSettingsSchema *schema,
335 return gvdb_table_has_value (schema->table, key);
339 g_settings_schema_list (GSettingsSchema *schema,
344 if (schema->items == NULL)
349 list = gvdb_table_list (schema->table, "");
350 len = list ? g_strv_length (list) : 0;
352 schema->items = g_new (GQuark, len);
355 for (i = 0; i < len; i++)
356 if (list[i][0] != '.')
357 schema->items[j++] = g_quark_from_string (list[i]);
363 *n_items = schema->n_items;
364 return schema->items;
368 g_settings_schema_get_name (GSettingsSchema *schema)
374 endian_fixup (GVariant **value)
376 #if G_BYTE_ORDER == G_BIG_ENDIAN
379 tmp = g_variant_byteswap (*value);
380 g_variant_unref (*value);
386 g_settings_schema_key_init (GSettingsSchemaKey *key,
387 GSettingsSchema *schema,
394 memset (key, 0, sizeof *key);
396 iter = g_settings_schema_get_value (schema, name);
398 key->schema = g_settings_schema_ref (schema);
399 key->default_value = g_variant_iter_next_value (iter);
400 endian_fixup (&key->default_value);
401 key->type = g_variant_get_type (key->default_value);
402 key->name = g_intern_string (name);
404 while (g_variant_iter_next (iter, "(y*)", &code, &data))
409 /* translation requested */
410 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
414 /* enumerated types... */
420 key->is_flags = TRUE;
424 /* ..., choices, aliases */
425 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
429 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
430 endian_fixup (&key->minimum);
431 endian_fixup (&key->maximum);
435 g_warning ("unknown schema extension '%c'", code);
439 g_variant_unref (data);
442 g_variant_iter_free (iter);
446 g_settings_schema_key_clear (GSettingsSchemaKey *key)
449 g_variant_unref (key->minimum);
452 g_variant_unref (key->maximum);
454 g_variant_unref (key->default_value);
456 g_settings_schema_unref (key->schema);
460 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
463 g_return_val_if_fail (value != NULL, FALSE);
465 return g_variant_is_of_type (value, key->type);
469 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
472 if (key->minimum == NULL && key->strinfo == NULL)
475 if (g_variant_is_container (value))
481 g_variant_iter_init (&iter, value);
482 while (ok && (child = g_variant_iter_next_value (&iter)))
484 ok = g_settings_schema_key_range_check (key, child);
485 g_variant_unref (child);
493 return g_variant_compare (key->minimum, value) <= 0 &&
494 g_variant_compare (value, key->maximum) <= 0;
497 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
498 g_variant_get_string (value, NULL));
502 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
507 if (g_settings_schema_key_range_check (key, value))
508 return g_variant_ref (value);
510 if (key->strinfo == NULL)
513 if (g_variant_is_container (value))
515 GVariantBuilder builder;
519 g_variant_iter_init (&iter, value);
520 g_variant_builder_init (&builder, g_variant_get_type (value));
522 while ((child = g_variant_iter_next_value (&iter)))
526 fixed = g_settings_schema_key_range_fixup (key, child);
527 g_variant_unref (child);
531 g_variant_builder_clear (&builder);
535 g_variant_builder_add_value (&builder, fixed);
536 g_variant_unref (fixed);
539 return g_variant_ref_sink (g_variant_builder_end (&builder));
542 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
543 g_variant_get_string (value, NULL));
544 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
549 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
551 const gchar *translated;
552 GError *error = NULL;
556 domain = g_settings_schema_get_gettext_domain (key->schema);
558 if (key->lc_char == '\0')
559 /* translation not requested for this key */
562 if (key->lc_char == 't')
563 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
565 translated = g_dgettext (domain, key->unparsed);
567 if (translated == key->unparsed)
568 /* the default value was not translated */
571 /* try to parse the translation of the unparsed default */
572 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
576 g_warning ("Failed to parse translated string `%s' for "
577 "key `%s' in schema `%s': %s", key->unparsed, key->name,
578 g_settings_schema_get_name (key->schema), error->message);
579 g_warning ("Using untranslated default instead.");
580 g_error_free (error);
583 else if (!g_settings_schema_key_range_check (key, value))
585 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
586 "is outside of valid range", key->unparsed, key->name,
587 g_settings_schema_get_name (key->schema));
588 g_variant_unref (value);
596 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
602 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
603 g_variant_get_string (value, NULL),
606 /* 'value' can only come from the backend after being filtered for validity,
607 * from the translation after being filtered for validity, or from the schema
608 * itself (which the schema compiler checks for validity). If this assertion
609 * fails then it's really a bug in GSettings or the schema compiler...
611 g_assert (it_worked);
617 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
622 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
627 return g_variant_new_string (string);
631 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
639 g_variant_iter_init (&iter, value);
640 while (g_variant_iter_next (&iter, "&s", &flag))
645 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
646 /* as in g_settings_to_enum() */
647 g_assert (it_worked);
649 result |= flag_value;
656 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
659 GVariantBuilder builder;
662 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
664 for (i = 0; i < 32; i++)
665 if (value & (1u << i))
669 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
673 g_variant_builder_clear (&builder);
677 g_variant_builder_add (&builder, "s", string);
680 return g_variant_builder_end (&builder);