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);
143 steal_item (gpointer key,
147 gchar ***ptr = user_data;
149 *(*ptr)++ = (gchar *) key;
154 static const gchar * const *non_relocatable_schema_list;
155 static const gchar * const *relocatable_schema_list;
156 static gsize schema_lists_initialised;
159 ensure_schema_lists (void)
161 if (g_once_init_enter (&schema_lists_initialised))
163 GSettingsSchemaSource *source;
164 GHashTable *single, *reloc;
169 initialise_schema_sources ();
171 /* We use hash tables to avoid duplicate listings for schemas that
172 * appear in more than one file.
174 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
175 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
177 for (source = schema_sources; source; source = source->parent)
179 list = gvdb_table_list (source->table, "");
181 g_assert (list != NULL);
183 for (i = 0; list[i]; i++)
185 if (!g_hash_table_lookup (single, list[i]) &&
186 !g_hash_table_lookup (reloc, list[i]))
190 table = gvdb_table_get_table (source->table, list[i]);
191 g_assert (table != NULL);
193 if (gvdb_table_has_value (table, ".path"))
194 g_hash_table_insert (single, g_strdup (list[i]), NULL);
196 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
198 gvdb_table_unref (table);
205 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
206 non_relocatable_schema_list = ptr;
207 g_hash_table_foreach_steal (single, steal_item, &ptr);
208 g_hash_table_unref (single);
211 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
212 relocatable_schema_list = ptr;
213 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
214 g_hash_table_unref (reloc);
217 g_once_init_leave (&schema_lists_initialised, TRUE);
222 * g_settings_list_schemas:
224 * Gets a list of the #GSettings schemas installed on the system. The
225 * returned list is exactly the list of schemas for which you may call
226 * g_settings_new() without adverse effects.
228 * This function does not list the schemas that do not provide their own
229 * paths (ie: schemas for which you must use
230 * g_settings_new_with_path()). See
231 * g_settings_list_relocatable_schemas() for that.
233 * Returns: (element-type utf8) (transfer none): a list of #GSettings
234 * schemas that are available. The list must not be modified or
239 const gchar * const *
240 g_settings_list_schemas (void)
242 ensure_schema_lists ();
244 return non_relocatable_schema_list;
248 * g_settings_list_relocatable_schemas:
250 * Gets a list of the relocatable #GSettings schemas installed on the
251 * system. These are schemas that do not provide their own path. It is
252 * usual to instantiate these schemas directly, but if you want to you
253 * can use g_settings_new_with_path() to specify the path.
255 * The output of this function, tTaken together with the output of
256 * g_settings_list_schemas() represents the complete list of all
259 * Returns: (element-type utf8) (transfer none): a list of relocatable
260 * #GSettings schemas that are available. The list must not be
265 const gchar * const *
266 g_settings_list_relocatable_schemas (void)
268 ensure_schema_lists ();
270 return relocatable_schema_list;
274 g_settings_schema_ref (GSettingsSchema *schema)
276 g_atomic_int_inc (&schema->ref_count);
282 g_settings_schema_unref (GSettingsSchema *schema)
284 if (g_atomic_int_dec_and_test (&schema->ref_count))
286 gvdb_table_unref (schema->table);
287 g_free (schema->items);
288 g_free (schema->name);
290 g_slice_free (GSettingsSchema, schema);
295 g_settings_schema_get_string (GSettingsSchema *schema,
298 const gchar *result = NULL;
301 if ((value = gvdb_table_get_raw_value (schema->table, key)))
303 result = g_variant_get_string (value, NULL);
304 g_variant_unref (value);
311 g_settings_schema_new (const gchar *name)
313 GSettingsSchemaSource *source;
314 GSettingsSchema *schema;
315 GvdbTable *table = NULL;
317 g_return_val_if_fail (name != NULL, NULL);
319 initialise_schema_sources ();
321 for (source = schema_sources; source; source = source->parent)
322 if ((table = gvdb_table_get_table (source->table, name)))
326 g_error ("Settings schema '%s' is not installed\n", name);
328 schema = g_slice_new0 (GSettingsSchema);
329 schema->ref_count = 1;
330 schema->name = g_strdup (name);
331 schema->table = table;
333 g_settings_schema_get_string (schema, ".path");
334 schema->gettext_domain =
335 g_settings_schema_get_string (schema, ".gettext-domain");
337 if (schema->gettext_domain)
338 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
344 g_settings_schema_get_value (GSettingsSchema *schema,
350 value = gvdb_table_get_raw_value (schema->table, key);
352 if G_UNLIKELY (value == NULL)
353 g_error ("Settings schema '%s' does not contain a key named '%s'",
356 iter = g_variant_iter_new (value);
357 g_variant_unref (value);
363 g_settings_schema_get_path (GSettingsSchema *schema)
369 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
371 return schema->gettext_domain;
375 g_settings_schema_has_key (GSettingsSchema *schema,
378 return gvdb_table_has_value (schema->table, key);
382 g_settings_schema_list (GSettingsSchema *schema,
387 if (schema->items == NULL)
392 list = gvdb_table_list (schema->table, "");
393 len = list ? g_strv_length (list) : 0;
395 schema->items = g_new (GQuark, len);
398 for (i = 0; i < len; i++)
399 if (list[i][0] != '.')
400 schema->items[j++] = g_quark_from_string (list[i]);
406 *n_items = schema->n_items;
407 return schema->items;
411 g_settings_schema_get_name (GSettingsSchema *schema)
417 endian_fixup (GVariant **value)
419 #if G_BYTE_ORDER == G_BIG_ENDIAN
422 tmp = g_variant_byteswap (*value);
423 g_variant_unref (*value);
429 g_settings_schema_key_init (GSettingsSchemaKey *key,
430 GSettingsSchema *schema,
437 memset (key, 0, sizeof *key);
439 iter = g_settings_schema_get_value (schema, name);
441 key->schema = g_settings_schema_ref (schema);
442 key->default_value = g_variant_iter_next_value (iter);
443 endian_fixup (&key->default_value);
444 key->type = g_variant_get_type (key->default_value);
445 key->name = g_intern_string (name);
447 while (g_variant_iter_next (iter, "(y*)", &code, &data))
452 /* translation requested */
453 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
457 /* enumerated types... */
463 key->is_flags = TRUE;
467 /* ..., choices, aliases */
468 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
472 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
473 endian_fixup (&key->minimum);
474 endian_fixup (&key->maximum);
478 g_warning ("unknown schema extension '%c'", code);
482 g_variant_unref (data);
485 g_variant_iter_free (iter);
489 g_settings_schema_key_clear (GSettingsSchemaKey *key)
492 g_variant_unref (key->minimum);
495 g_variant_unref (key->maximum);
497 g_variant_unref (key->default_value);
499 g_settings_schema_unref (key->schema);
503 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
506 g_return_val_if_fail (value != NULL, FALSE);
508 return g_variant_is_of_type (value, key->type);
512 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
515 if (key->minimum == NULL && key->strinfo == NULL)
518 if (g_variant_is_container (value))
524 g_variant_iter_init (&iter, value);
525 while (ok && (child = g_variant_iter_next_value (&iter)))
527 ok = g_settings_schema_key_range_check (key, child);
528 g_variant_unref (child);
536 return g_variant_compare (key->minimum, value) <= 0 &&
537 g_variant_compare (value, key->maximum) <= 0;
540 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
541 g_variant_get_string (value, NULL));
545 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
550 if (g_settings_schema_key_range_check (key, value))
551 return g_variant_ref (value);
553 if (key->strinfo == NULL)
556 if (g_variant_is_container (value))
558 GVariantBuilder builder;
562 g_variant_iter_init (&iter, value);
563 g_variant_builder_init (&builder, g_variant_get_type (value));
565 while ((child = g_variant_iter_next_value (&iter)))
569 fixed = g_settings_schema_key_range_fixup (key, child);
570 g_variant_unref (child);
574 g_variant_builder_clear (&builder);
578 g_variant_builder_add_value (&builder, fixed);
579 g_variant_unref (fixed);
582 return g_variant_ref_sink (g_variant_builder_end (&builder));
585 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
586 g_variant_get_string (value, NULL));
587 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
592 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
594 const gchar *translated;
595 GError *error = NULL;
599 domain = g_settings_schema_get_gettext_domain (key->schema);
601 if (key->lc_char == '\0')
602 /* translation not requested for this key */
605 if (key->lc_char == 't')
606 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
608 translated = g_dgettext (domain, key->unparsed);
610 if (translated == key->unparsed)
611 /* the default value was not translated */
614 /* try to parse the translation of the unparsed default */
615 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
619 g_warning ("Failed to parse translated string `%s' for "
620 "key `%s' in schema `%s': %s", key->unparsed, key->name,
621 g_settings_schema_get_name (key->schema), error->message);
622 g_warning ("Using untranslated default instead.");
623 g_error_free (error);
626 else if (!g_settings_schema_key_range_check (key, value))
628 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
629 "is outside of valid range", key->unparsed, key->name,
630 g_settings_schema_get_name (key->schema));
631 g_variant_unref (value);
639 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
645 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
646 g_variant_get_string (value, NULL),
649 /* 'value' can only come from the backend after being filtered for validity,
650 * from the translation after being filtered for validity, or from the schema
651 * itself (which the schema compiler checks for validity). If this assertion
652 * fails then it's really a bug in GSettings or the schema compiler...
654 g_assert (it_worked);
660 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
665 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
670 return g_variant_new_string (string);
674 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
682 g_variant_iter_init (&iter, value);
683 while (g_variant_iter_next (&iter, "&s", &flag))
688 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
689 /* as in g_settings_to_enum() */
690 g_assert (it_worked);
692 result |= flag_value;
699 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
702 GVariantBuilder builder;
705 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
707 for (i = 0; i < 32; i++)
708 if (value & (1u << i))
712 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
716 g_variant_builder_clear (&builder);
720 g_variant_builder_add (&builder, "s", string);
723 return g_variant_builder_end (&builder);