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 if G_UNLIKELY (g_once_init_enter (&initialised))
52 const gchar * const *dir;
55 for (dir = g_get_system_data_dirs (); *dir; dir++)
60 filename = g_build_filename (*dir, "glib-2.0", "schemas",
61 "gschemas.compiled", NULL);
62 table = gvdb_table_new (filename, TRUE, NULL);
65 schema_sources = g_slist_prepend (schema_sources, table);
70 schema_sources = g_slist_reverse (schema_sources);
72 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
77 filename = g_build_filename (path, "gschemas.compiled", NULL);
78 table = gvdb_table_new (filename, TRUE, NULL);
81 schema_sources = g_slist_prepend (schema_sources, table);
86 g_once_init_leave (&initialised, TRUE);
91 steal_item (gpointer key,
95 gchar ***ptr = user_data;
97 *(*ptr)++ = (gchar *) key;
102 static const gchar * const *non_relocatable_schema_list;
103 static const gchar * const *relocatable_schema_list;
104 static gsize schema_lists_initialised;
107 ensure_schema_lists (void)
109 if (g_once_init_enter (&schema_lists_initialised))
111 GHashTable *single, *reloc;
117 initialise_schema_sources ();
119 /* We use hash tables to avoid duplicate listings for schemas that
120 * appear in more than one file.
122 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
123 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
125 for (source = schema_sources; source; source = source->next)
127 list = gvdb_table_list (source->data, "");
129 g_assert (list != NULL);
131 for (i = 0; list[i]; i++)
133 if (!g_hash_table_lookup (single, list[i]) &&
134 !g_hash_table_lookup (reloc, list[i]))
138 table = gvdb_table_get_table (source->data, list[i]);
139 g_assert (table != NULL);
141 if (gvdb_table_has_value (table, ".path"))
142 g_hash_table_insert (single, g_strdup (list[i]), NULL);
144 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
146 gvdb_table_unref (table);
153 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
154 non_relocatable_schema_list = ptr;
155 g_hash_table_foreach_steal (single, steal_item, &ptr);
156 g_hash_table_unref (single);
159 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
160 relocatable_schema_list = ptr;
161 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
162 g_hash_table_unref (reloc);
165 g_once_init_leave (&schema_lists_initialised, TRUE);
170 * g_settings_list_schemas:
172 * Gets a list of the #GSettings schemas installed on the system. The
173 * returned list is exactly the list of schemas for which you may call
174 * g_settings_new() without adverse effects.
176 * This function does not list the schemas that do not provide their own
177 * paths (ie: schemas for which you must use
178 * g_settings_new_with_path()). See
179 * g_settings_list_relocatable_schemas() for that.
181 * Returns: (element-type utf8) (transfer none): a list of #GSettings
182 * schemas that are available. The list must not be modified or
187 const gchar * const *
188 g_settings_list_schemas (void)
190 ensure_schema_lists ();
192 return non_relocatable_schema_list;
196 * g_settings_list_relocatable_schemas:
198 * Gets a list of the relocatable #GSettings schemas installed on the
199 * system. These are schemas that do not provide their own path. It is
200 * usual to instantiate these schemas directly, but if you want to you
201 * can use g_settings_new_with_path() to specify the path.
203 * The output of this function, tTaken together with the output of
204 * g_settings_list_schemas() represents the complete list of all
207 * Returns: (element-type utf8) (transfer none): a list of relocatable
208 * #GSettings schemas that are available. The list must not be
213 const gchar * const *
214 g_settings_list_relocatable_schemas (void)
216 ensure_schema_lists ();
218 return relocatable_schema_list;
222 g_settings_schema_ref (GSettingsSchema *schema)
224 g_atomic_int_inc (&schema->ref_count);
230 g_settings_schema_unref (GSettingsSchema *schema)
232 if (g_atomic_int_dec_and_test (&schema->ref_count))
234 gvdb_table_unref (schema->table);
235 g_free (schema->items);
236 g_free (schema->name);
238 g_slice_free (GSettingsSchema, schema);
243 g_settings_schema_get_string (GSettingsSchema *schema,
246 const gchar *result = NULL;
249 if ((value = gvdb_table_get_raw_value (schema->table, key)))
251 result = g_variant_get_string (value, NULL);
252 g_variant_unref (value);
259 g_settings_schema_new (const gchar *name)
261 GSettingsSchema *schema;
262 GvdbTable *table = NULL;
265 g_return_val_if_fail (name != NULL, NULL);
267 initialise_schema_sources ();
269 for (source = schema_sources; source; source = source->next)
271 GvdbTable *file = source->data;
273 if ((table = gvdb_table_get_table (file, name)))
278 g_error ("Settings schema '%s' is not installed\n", name);
280 schema = g_slice_new0 (GSettingsSchema);
281 schema->ref_count = 1;
282 schema->name = g_strdup (name);
283 schema->table = table;
285 g_settings_schema_get_string (schema, ".path");
286 schema->gettext_domain =
287 g_settings_schema_get_string (schema, ".gettext-domain");
289 if (schema->gettext_domain)
290 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
296 g_settings_schema_get_value (GSettingsSchema *schema,
302 value = gvdb_table_get_raw_value (schema->table, key);
304 if G_UNLIKELY (value == NULL)
305 g_error ("Settings schema '%s' does not contain a key named '%s'",
308 iter = g_variant_iter_new (value);
309 g_variant_unref (value);
315 g_settings_schema_get_path (GSettingsSchema *schema)
321 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
323 return schema->gettext_domain;
327 g_settings_schema_has_key (GSettingsSchema *schema,
330 return gvdb_table_has_value (schema->table, key);
334 g_settings_schema_list (GSettingsSchema *schema,
339 if (schema->items == NULL)
344 list = gvdb_table_list (schema->table, "");
345 len = list ? g_strv_length (list) : 0;
347 schema->items = g_new (GQuark, len);
350 for (i = 0; i < len; i++)
351 if (list[i][0] != '.')
352 schema->items[j++] = g_quark_from_string (list[i]);
358 *n_items = schema->n_items;
359 return schema->items;
363 g_settings_schema_get_name (GSettingsSchema *schema)
369 endian_fixup (GVariant **value)
371 #if G_BYTE_ORDER == G_BIG_ENDIAN
374 tmp = g_variant_byteswap (*value);
375 g_variant_unref (*value);
381 g_settings_schema_key_init (GSettingsSchemaKey *key,
382 GSettingsSchema *schema,
389 memset (key, 0, sizeof *key);
391 iter = g_settings_schema_get_value (schema, name);
393 key->schema = g_settings_schema_ref (schema);
394 key->default_value = g_variant_iter_next_value (iter);
395 endian_fixup (&key->default_value);
396 key->type = g_variant_get_type (key->default_value);
397 key->name = g_intern_string (name);
399 while (g_variant_iter_next (iter, "(y*)", &code, &data))
404 /* translation requested */
405 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
409 /* enumerated types... */
415 key->is_flags = TRUE;
419 /* ..., choices, aliases */
420 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
424 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
425 endian_fixup (&key->minimum);
426 endian_fixup (&key->maximum);
430 g_warning ("unknown schema extension '%c'", code);
434 g_variant_unref (data);
437 g_variant_iter_free (iter);
441 g_settings_schema_key_clear (GSettingsSchemaKey *key)
444 g_variant_unref (key->minimum);
447 g_variant_unref (key->maximum);
449 g_variant_unref (key->default_value);
451 g_settings_schema_unref (key->schema);
455 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
458 g_return_val_if_fail (value != NULL, FALSE);
460 return g_variant_is_of_type (value, key->type);
464 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
467 if (key->minimum == NULL && key->strinfo == NULL)
470 if (g_variant_is_container (value))
476 g_variant_iter_init (&iter, value);
477 while (ok && (child = g_variant_iter_next_value (&iter)))
479 ok = g_settings_schema_key_range_check (key, child);
480 g_variant_unref (child);
488 return g_variant_compare (key->minimum, value) <= 0 &&
489 g_variant_compare (value, key->maximum) <= 0;
492 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
493 g_variant_get_string (value, NULL));
497 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
502 if (g_settings_schema_key_range_check (key, value))
503 return g_variant_ref (value);
505 if (key->strinfo == NULL)
508 if (g_variant_is_container (value))
510 GVariantBuilder builder;
514 g_variant_iter_init (&iter, value);
515 g_variant_builder_init (&builder, g_variant_get_type (value));
517 while ((child = g_variant_iter_next_value (&iter)))
521 fixed = g_settings_schema_key_range_fixup (key, child);
522 g_variant_unref (child);
526 g_variant_builder_clear (&builder);
530 g_variant_builder_add_value (&builder, fixed);
531 g_variant_unref (fixed);
534 return g_variant_ref_sink (g_variant_builder_end (&builder));
537 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
538 g_variant_get_string (value, NULL));
539 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
544 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
546 const gchar *translated;
547 GError *error = NULL;
551 domain = g_settings_schema_get_gettext_domain (key->schema);
553 if (key->lc_char == '\0')
554 /* translation not requested for this key */
557 if (key->lc_char == 't')
558 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
560 translated = g_dgettext (domain, key->unparsed);
562 if (translated == key->unparsed)
563 /* the default value was not translated */
566 /* try to parse the translation of the unparsed default */
567 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
571 g_warning ("Failed to parse translated string `%s' for "
572 "key `%s' in schema `%s': %s", key->unparsed, key->name,
573 g_settings_schema_get_name (key->schema), error->message);
574 g_warning ("Using untranslated default instead.");
575 g_error_free (error);
578 else if (!g_settings_schema_key_range_check (key, value))
580 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
581 "is outside of valid range", key->unparsed, key->name,
582 g_settings_schema_get_name (key->schema));
583 g_variant_unref (value);
591 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
597 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
598 g_variant_get_string (value, NULL),
601 /* 'value' can only come from the backend after being filtered for validity,
602 * from the translation after being filtered for validity, or from the schema
603 * itself (which the schema compiler checks for validity). If this assertion
604 * fails then it's really a bug in GSettings or the schema compiler...
606 g_assert (it_worked);
612 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
617 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
622 return g_variant_new_string (string);
626 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
634 g_variant_iter_init (&iter, value);
635 while (g_variant_iter_next (&iter, "&s", &flag))
640 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
641 /* as in g_settings_to_enum() */
642 g_assert (it_worked);
644 result |= flag_value;
651 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
654 GVariantBuilder builder;
657 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
659 for (i = 0; i < 32; i++)
660 if (value & (1u << i))
664 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
668 g_variant_builder_clear (&builder);
672 g_variant_builder_add (&builder, "s", string);
675 return g_variant_builder_end (&builder);