2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2011 Canonical Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gsettingsschema-internal.h"
24 #include "gsettings.h"
26 #include "gvdb/gvdb-reader.h"
32 struct _GSettingsSchema
34 const gchar *gettext_domain;
44 typedef struct _GSettingsSchemaSource GSettingsSchemaSource;
46 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
47 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
49 struct _GSettingsSchemaSource
51 GSettingsSchemaSource *parent;
57 static GSettingsSchemaSource *schema_sources;
60 prepend_schema_table (GvdbTable *table)
62 GSettingsSchemaSource *source;
64 /* we steal the reference from 'schema_sources' for our ->parent */
65 source = g_slice_new (GSettingsSchemaSource);
66 source->parent = schema_sources;
67 source->table = table;
68 source->ref_count = 1;
70 schema_sources = source;
73 GSettingsSchemaSource *
74 g_settings_schema_source_ref (GSettingsSchemaSource *source)
76 g_atomic_int_inc (&source->ref_count);
82 g_settings_schema_source_unref (GSettingsSchemaSource *source)
84 if (g_atomic_int_dec_and_test (&source->ref_count))
86 if (source == schema_sources)
87 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
89 g_settings_schema_source_unref (source->parent);
90 gvdb_table_unref (source->table);
92 g_slice_free (GSettingsSchemaSource, source);
96 GSettingsSchemaSource *
97 g_settings_schema_source_new_from_directory (GSettingsSchemaSource *parent,
98 const gchar *directory,
102 GSettingsSchemaSource *source;
106 filename = g_build_filename (directory, "gschemas.compiled", NULL);
107 table = gvdb_table_new (filename, TRUE, error);
113 source = g_slice_new (GSettingsSchemaSource);
114 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
115 source->table = table;
116 source->ref_count = 1;
122 initialise_schema_sources (void)
124 static gsize initialised;
126 /* need a separate variable because 'schema_sources' may legitimately
127 * be null if we have zero valid schema sources
129 if G_UNLIKELY (g_once_init_enter (&initialised))
131 const gchar * const *dirs;
135 /* iterate in reverse: count up, then count down */
136 dirs = g_get_system_data_dirs ();
137 for (i = 0; dirs[i]; i++);
144 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
145 table = gvdb_table_new (filename, TRUE, NULL);
148 prepend_schema_table (table);
153 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
158 filename = g_build_filename (path, "gschemas.compiled", NULL);
159 table = gvdb_table_new (filename, TRUE, NULL);
162 prepend_schema_table (table);
167 g_once_init_leave (&initialised, TRUE);
171 GSettingsSchemaSource *
172 g_settings_schema_source_get_default (void)
174 initialise_schema_sources ();
176 return schema_sources;
180 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
181 const gchar *schema_id,
184 GSettingsSchema *schema;
187 g_return_val_if_fail (source != NULL, NULL);
188 g_return_val_if_fail (schema_id != NULL, NULL);
190 table = gvdb_table_get_table (source->table, schema_id);
192 if (table == NULL && recursive)
193 for (source = source->parent; source; source = source->parent)
194 if ((table = gvdb_table_get_table (source->table, schema_id)))
200 schema = g_slice_new0 (GSettingsSchema);
201 schema->ref_count = 1;
202 schema->id = g_strdup (schema_id);
203 schema->table = table;
204 schema->path = g_settings_schema_get_string (schema, ".path");
205 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
207 if (schema->gettext_domain)
208 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
214 steal_item (gpointer key,
218 gchar ***ptr = user_data;
220 *(*ptr)++ = (gchar *) key;
225 static const gchar * const *non_relocatable_schema_list;
226 static const gchar * const *relocatable_schema_list;
227 static gsize schema_lists_initialised;
230 ensure_schema_lists (void)
232 if (g_once_init_enter (&schema_lists_initialised))
234 GSettingsSchemaSource *source;
235 GHashTable *single, *reloc;
240 initialise_schema_sources ();
242 /* We use hash tables to avoid duplicate listings for schemas that
243 * appear in more than one file.
245 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
246 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
248 for (source = schema_sources; source; source = source->parent)
250 list = gvdb_table_list (source->table, "");
252 g_assert (list != NULL);
254 for (i = 0; list[i]; i++)
256 if (!g_hash_table_lookup (single, list[i]) &&
257 !g_hash_table_lookup (reloc, list[i]))
261 table = gvdb_table_get_table (source->table, list[i]);
262 g_assert (table != NULL);
264 if (gvdb_table_has_value (table, ".path"))
265 g_hash_table_insert (single, g_strdup (list[i]), NULL);
267 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
269 gvdb_table_unref (table);
276 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
277 non_relocatable_schema_list = ptr;
278 g_hash_table_foreach_steal (single, steal_item, &ptr);
279 g_hash_table_unref (single);
282 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
283 relocatable_schema_list = ptr;
284 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
285 g_hash_table_unref (reloc);
288 g_once_init_leave (&schema_lists_initialised, TRUE);
293 * g_settings_list_schemas:
295 * Gets a list of the #GSettings schemas installed on the system. The
296 * returned list is exactly the list of schemas for which you may call
297 * g_settings_new() without adverse effects.
299 * This function does not list the schemas that do not provide their own
300 * paths (ie: schemas for which you must use
301 * g_settings_new_with_path()). See
302 * g_settings_list_relocatable_schemas() for that.
304 * Returns: (element-type utf8) (transfer none): a list of #GSettings
305 * schemas that are available. The list must not be modified or
310 const gchar * const *
311 g_settings_list_schemas (void)
313 ensure_schema_lists ();
315 return non_relocatable_schema_list;
319 * g_settings_list_relocatable_schemas:
321 * Gets a list of the relocatable #GSettings schemas installed on the
322 * system. These are schemas that do not provide their own path. It is
323 * usual to instantiate these schemas directly, but if you want to you
324 * can use g_settings_new_with_path() to specify the path.
326 * The output of this function, tTaken together with the output of
327 * g_settings_list_schemas() represents the complete list of all
330 * Returns: (element-type utf8) (transfer none): a list of relocatable
331 * #GSettings schemas that are available. The list must not be
336 const gchar * const *
337 g_settings_list_relocatable_schemas (void)
339 ensure_schema_lists ();
341 return relocatable_schema_list;
345 g_settings_schema_ref (GSettingsSchema *schema)
347 g_atomic_int_inc (&schema->ref_count);
353 g_settings_schema_unref (GSettingsSchema *schema)
355 if (g_atomic_int_dec_and_test (&schema->ref_count))
357 gvdb_table_unref (schema->table);
358 g_free (schema->items);
361 g_slice_free (GSettingsSchema, schema);
366 g_settings_schema_get_string (GSettingsSchema *schema,
369 const gchar *result = NULL;
372 if ((value = gvdb_table_get_raw_value (schema->table, key)))
374 result = g_variant_get_string (value, NULL);
375 g_variant_unref (value);
382 g_settings_schema_get_value (GSettingsSchema *schema,
388 value = gvdb_table_get_raw_value (schema->table, key);
390 if G_UNLIKELY (value == NULL)
391 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
393 iter = g_variant_iter_new (value);
394 g_variant_unref (value);
400 g_settings_schema_get_path (GSettingsSchema *schema)
406 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
408 return schema->gettext_domain;
412 g_settings_schema_has_key (GSettingsSchema *schema,
415 return gvdb_table_has_value (schema->table, key);
419 g_settings_schema_list (GSettingsSchema *schema,
424 if (schema->items == NULL)
429 list = gvdb_table_list (schema->table, "");
430 len = list ? g_strv_length (list) : 0;
432 schema->items = g_new (GQuark, len);
435 for (i = 0; i < len; i++)
436 if (list[i][0] != '.')
437 schema->items[j++] = g_quark_from_string (list[i]);
443 *n_items = schema->n_items;
444 return schema->items;
448 g_settings_schema_get_id (GSettingsSchema *schema)
454 endian_fixup (GVariant **value)
456 #if G_BYTE_ORDER == G_BIG_ENDIAN
459 tmp = g_variant_byteswap (*value);
460 g_variant_unref (*value);
466 g_settings_schema_key_init (GSettingsSchemaKey *key,
467 GSettingsSchema *schema,
474 memset (key, 0, sizeof *key);
476 iter = g_settings_schema_get_value (schema, name);
478 key->schema = g_settings_schema_ref (schema);
479 key->default_value = g_variant_iter_next_value (iter);
480 endian_fixup (&key->default_value);
481 key->type = g_variant_get_type (key->default_value);
482 key->name = g_intern_string (name);
484 while (g_variant_iter_next (iter, "(y*)", &code, &data))
489 /* translation requested */
490 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
494 /* enumerated types... */
500 key->is_flags = TRUE;
504 /* ..., choices, aliases */
505 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
509 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
510 endian_fixup (&key->minimum);
511 endian_fixup (&key->maximum);
515 g_warning ("unknown schema extension '%c'", code);
519 g_variant_unref (data);
522 g_variant_iter_free (iter);
526 g_settings_schema_key_clear (GSettingsSchemaKey *key)
529 g_variant_unref (key->minimum);
532 g_variant_unref (key->maximum);
534 g_variant_unref (key->default_value);
536 g_settings_schema_unref (key->schema);
540 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
543 g_return_val_if_fail (value != NULL, FALSE);
545 return g_variant_is_of_type (value, key->type);
549 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
552 if (key->minimum == NULL && key->strinfo == NULL)
555 if (g_variant_is_container (value))
561 g_variant_iter_init (&iter, value);
562 while (ok && (child = g_variant_iter_next_value (&iter)))
564 ok = g_settings_schema_key_range_check (key, child);
565 g_variant_unref (child);
573 return g_variant_compare (key->minimum, value) <= 0 &&
574 g_variant_compare (value, key->maximum) <= 0;
577 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
578 g_variant_get_string (value, NULL));
582 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
587 if (g_settings_schema_key_range_check (key, value))
588 return g_variant_ref (value);
590 if (key->strinfo == NULL)
593 if (g_variant_is_container (value))
595 GVariantBuilder builder;
599 g_variant_iter_init (&iter, value);
600 g_variant_builder_init (&builder, g_variant_get_type (value));
602 while ((child = g_variant_iter_next_value (&iter)))
606 fixed = g_settings_schema_key_range_fixup (key, child);
607 g_variant_unref (child);
611 g_variant_builder_clear (&builder);
615 g_variant_builder_add_value (&builder, fixed);
616 g_variant_unref (fixed);
619 return g_variant_ref_sink (g_variant_builder_end (&builder));
622 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
623 g_variant_get_string (value, NULL));
624 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
629 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
631 const gchar *translated;
632 GError *error = NULL;
636 domain = g_settings_schema_get_gettext_domain (key->schema);
638 if (key->lc_char == '\0')
639 /* translation not requested for this key */
642 if (key->lc_char == 't')
643 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
645 translated = g_dgettext (domain, key->unparsed);
647 if (translated == key->unparsed)
648 /* the default value was not translated */
651 /* try to parse the translation of the unparsed default */
652 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
656 g_warning ("Failed to parse translated string `%s' for "
657 "key `%s' in schema `%s': %s", key->unparsed, key->name,
658 g_settings_schema_get_id (key->schema), error->message);
659 g_warning ("Using untranslated default instead.");
660 g_error_free (error);
663 else if (!g_settings_schema_key_range_check (key, value))
665 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
666 "is outside of valid range", key->unparsed, key->name,
667 g_settings_schema_get_id (key->schema));
668 g_variant_unref (value);
676 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
682 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
683 g_variant_get_string (value, NULL),
686 /* 'value' can only come from the backend after being filtered for validity,
687 * from the translation after being filtered for validity, or from the schema
688 * itself (which the schema compiler checks for validity). If this assertion
689 * fails then it's really a bug in GSettings or the schema compiler...
691 g_assert (it_worked);
697 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
702 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
707 return g_variant_new_string (string);
711 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
719 g_variant_iter_init (&iter, value);
720 while (g_variant_iter_next (&iter, "&s", &flag))
725 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
726 /* as in g_settings_to_enum() */
727 g_assert (it_worked);
729 result |= flag_value;
736 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
739 GVariantBuilder builder;
742 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
744 for (i = 0; i < 32; i++)
745 if (value & (1u << i))
749 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
753 g_variant_builder_clear (&builder);
757 g_variant_builder_add (&builder, "s", string);
760 return g_variant_builder_end (&builder);