More clearly define 'named menu' in the XML parser
[glib.git] / gio / gsettingsschema.c
blob1de717721837c723cfb4b8caf22f126d12100814
1 /*
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.
21 #include "config.h"
23 #include "gsettingsschema-internal.h"
24 #include "gsettings.h"
26 #include "gvdb/gvdb-reader.h"
27 #include "strinfo.c"
29 #include <glibintl.h>
30 #include <locale.h>
31 #include <string.h>
33 /**
34 * SECTION:gsettingsschema
35 * @short_description: introspecting and controlling the loading of
36 * #GSettings schemas
38 * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
39 * mechanism for advanced control over the loading of schemas and a
40 * mechanism for introspecting their content.
42 * Plugin loading systems that wish to provide plugins a way to access
43 * settings face the problem of how to make the schemas for these
44 * settings visible to GSettings. Typically, a plugin will want to ship
45 * the schema along with itself and it won't be installed into the
46 * standard system directories for schemas.
48 * #GSettingsSchemaSource provides a mechanism for dealing with this by
49 * allowing the creation of a new 'schema source' from which schemas can
50 * be acquired. This schema source can then become part of the metadata
51 * associated with the plugin and queried whenever the plugin requires
52 * access to some settings.
54 * Consider the following example:
56 * |[
57 * typedef struct
58 * {
59 * ...
60 * GSettingsSchemaSource *schema_source;
61 * ...
62 * } Plugin;
64 * Plugin *
65 * initialise_plugin (const gchar *dir)
66 * {
67 * Plugin *plugin;
69 * ...
71 * plugin->schema_source =
72 * g_settings_new_schema_source_from_directory (dir,
73 * g_settings_schema_source_get_default (), FALSE, NULL);
75 * ...
77 * return plugin;
78 * }
80 * ...
82 * GSettings *
83 * plugin_get_settings (Plugin *plugin,
84 * const gchar *schema_id)
85 * {
86 * GSettingsSchema *schema;
88 * if (schema_id == NULL)
89 * schema_id = plugin->identifier;
91 * schema = g_settings_schema_source_lookup (plugin->schema_source,
92 * schema_id, FALSE);
94 * if (schema == NULL)
95 * {
96 * ... disable the plugin or abort, etc ...
97 * }
99 * return g_settings_new_full (schema, NULL, NULL);
101 * ]|
103 * The code above shows how hooks should be added to the code that
104 * initialises (or enables) the plugin to create the schema source and
105 * how an API can be added to the plugin system to provide a convenient
106 * way for the plugin to access its settings, using the schemas that it
107 * ships.
109 * From the standpoint of the plugin, it would need to ensure that it
110 * ships a gschemas.compiled file as part of itself, and then simply do
111 * the following:
113 * |[
115 * GSettings *settings;
116 * gint some_value;
118 * settings = plugin_get_settings (self, NULL);
119 * some_value = g_settings_get_int (settings, "some-value");
120 * ...
122 * ]|
124 * It's also possible that the plugin system expects the schema source
125 * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
126 * In that case, the plugin loading system must compile the schemas for
127 * itself before attempting to create the settings source.
129 * Since: 2.32
133 * GSettingsSchema:
135 * This is an opaque structure type. You may not access it directly.
137 * Since: 2.32
139 struct _GSettingsSchema
141 const gchar *gettext_domain;
142 const gchar *path;
143 GQuark *items;
144 gint n_items;
145 GvdbTable *table;
146 gchar *id;
148 gint ref_count;
152 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
154 * A boxed #GType corresponding to #GSettingsSchemaSource.
156 * Since: 2.32
158 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
161 * G_TYPE_SETTINGS_SCHEMA:
163 * A boxed #GType corresponding to #GSettingsSchema.
165 * Since: 2.32
167 G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
170 * GSettingsSchemaSource:
172 * This is an opaque structure type. You may not access it directly.
174 * Since: 2.32
176 struct _GSettingsSchemaSource
178 GSettingsSchemaSource *parent;
179 GvdbTable *table;
181 gint ref_count;
184 static GSettingsSchemaSource *schema_sources;
186 static void
187 prepend_schema_table (GvdbTable *table)
189 GSettingsSchemaSource *source;
191 /* we steal the reference from 'schema_sources' for our ->parent */
192 source = g_slice_new (GSettingsSchemaSource);
193 source->parent = schema_sources;
194 source->table = table;
195 source->ref_count = 1;
197 schema_sources = source;
201 * g_settings_schema_source_ref:
202 * @source: a #GSettingsSchemaSource
204 * Increase the reference count of @source, returning a new reference.
206 * Returns: a new reference to @source
208 * Since: 2.32
210 GSettingsSchemaSource *
211 g_settings_schema_source_ref (GSettingsSchemaSource *source)
213 g_atomic_int_inc (&source->ref_count);
215 return source;
219 * g_settings_schema_source_unref:
220 * @source: a #GSettingsSchemaSource
222 * Decrease the reference count of @source, possibly freeing it.
224 * Since: 2.32
226 void
227 g_settings_schema_source_unref (GSettingsSchemaSource *source)
229 if (g_atomic_int_dec_and_test (&source->ref_count))
231 if (source == schema_sources)
232 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
234 if (source->parent)
235 g_settings_schema_source_unref (source->parent);
236 gvdb_table_unref (source->table);
238 g_slice_free (GSettingsSchemaSource, source);
243 * g_settings_schema_source_new_from_directory:
244 * @directory: the filename of a directory
245 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
246 * @trusted: %TRUE, if the directory is trusted
247 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
249 * Attempts to create a new schema source corresponding to the contents
250 * of the given directory.
252 * This function is not required for normal uses of #GSettings but it
253 * may be useful to authors of plugin management systems.
255 * The directory should contain a file called
256 * <filename>gschemas.compiled</filename> as produced by
257 * <command>glib-compile-schemas</command>.
259 * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
260 * trusted not to be corrupted. This assumption has a performance
261 * advantage, but can result in crashes or inconsistent behaviour in the
262 * case of a corrupted file. Generally, you should set @trusted to
263 * %TRUE for files installed by the system and to %FALSE for files in
264 * the home directory.
266 * If @parent is non-%NULL then there are two effects.
268 * First, if g_settings_schema_source_lookup() is called with the
269 * @recursive flag set to %TRUE and the schema can not be found in the
270 * source, the lookup will recurse to the parent.
272 * Second, any references to other schemas specified within this
273 * source (ie: <literal>child</literal> or <literal>extents</literal>)
274 * references may be resolved from the @parent.
276 * For this second reason, except in very unusual situations, the
277 * @parent should probably be given as the default schema source, as
278 * returned by g_settings_schema_source_get_default().
280 * Since: 2.32
282 GSettingsSchemaSource *
283 g_settings_schema_source_new_from_directory (const gchar *directory,
284 GSettingsSchemaSource *parent,
285 gboolean trusted,
286 GError **error)
288 GSettingsSchemaSource *source;
289 GvdbTable *table;
290 gchar *filename;
292 filename = g_build_filename (directory, "gschemas.compiled", NULL);
293 table = gvdb_table_new (filename, trusted, error);
294 g_free (filename);
296 if (table == NULL)
297 return NULL;
299 source = g_slice_new (GSettingsSchemaSource);
300 source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
301 source->table = table;
302 source->ref_count = 1;
304 return source;
307 static void
308 initialise_schema_sources (void)
310 static gsize initialised;
312 /* need a separate variable because 'schema_sources' may legitimately
313 * be null if we have zero valid schema sources
315 if G_UNLIKELY (g_once_init_enter (&initialised))
317 const gchar * const *dirs;
318 const gchar *path;
319 gint i;
321 /* iterate in reverse: count up, then count down */
322 dirs = g_get_system_data_dirs ();
323 for (i = 0; dirs[i]; i++);
325 while (i--)
327 gchar *filename;
328 GvdbTable *table;
330 filename = g_build_filename (dirs[i], "glib-2.0", "schemas", "gschemas.compiled", NULL);
331 table = gvdb_table_new (filename, TRUE, NULL);
333 if (table != NULL)
334 prepend_schema_table (table);
336 g_free (filename);
339 if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
341 gchar *filename;
342 GvdbTable *table;
344 filename = g_build_filename (path, "gschemas.compiled", NULL);
345 table = gvdb_table_new (filename, TRUE, NULL);
347 if (table != NULL)
348 prepend_schema_table (table);
350 g_free (filename);
353 g_once_init_leave (&initialised, TRUE);
358 * g_settings_schema_source_get_default:
360 * Gets the default system schema source.
362 * This function is not required for normal uses of #GSettings but it
363 * may be useful to authors of plugin management systems or to those who
364 * want to introspect the content of schemas.
366 * If no schemas are installed, %NULL will be returned.
368 * The returned source may actually consist of multiple schema sources
369 * from different directories, depending on which directories were given
370 * in <envar>XDG_DATA_DIRS</envar> and
371 * <envar>GSETTINGS_SCHEMA_DIR</envar>. For this reason, all lookups
372 * performed against the default source should probably be done
373 * recursively.
375 * Returns: (transfer none): the default schema source
377 * Since: 2.32
379 GSettingsSchemaSource *
380 g_settings_schema_source_get_default (void)
382 initialise_schema_sources ();
384 return schema_sources;
388 * g_settings_schema_source_lookup:
389 * @source: a #GSettingsSchemaSource
390 * @schema_id: a schema ID
391 * @recursive: %TRUE if the lookup should be recursive
393 * Looks up a schema with the identifier @schema_id in @source.
395 * This function is not required for normal uses of #GSettings but it
396 * may be useful to authors of plugin management systems or to those who
397 * want to introspect the content of schemas.
399 * If the schema isn't found directly in @source and @recursive is %TRUE
400 * then the parent sources will also be checked.
402 * If the schema isn't found, %NULL is returned.
404 * Returns: (transfer full): a new #GSettingsSchema
406 * Since: 2.32
408 GSettingsSchema *
409 g_settings_schema_source_lookup (GSettingsSchemaSource *source,
410 const gchar *schema_id,
411 gboolean recursive)
413 GSettingsSchema *schema;
414 GvdbTable *table;
416 g_return_val_if_fail (source != NULL, NULL);
417 g_return_val_if_fail (schema_id != NULL, NULL);
419 table = gvdb_table_get_table (source->table, schema_id);
421 if (table == NULL && recursive)
422 for (source = source->parent; source; source = source->parent)
423 if ((table = gvdb_table_get_table (source->table, schema_id)))
424 break;
426 if (table == NULL)
427 return NULL;
429 schema = g_slice_new0 (GSettingsSchema);
430 schema->ref_count = 1;
431 schema->id = g_strdup (schema_id);
432 schema->table = table;
433 schema->path = g_settings_schema_get_string (schema, ".path");
434 schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
436 if (schema->gettext_domain)
437 bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
439 return schema;
442 static gboolean
443 steal_item (gpointer key,
444 gpointer value,
445 gpointer user_data)
447 gchar ***ptr = user_data;
449 *(*ptr)++ = (gchar *) key;
451 return TRUE;
454 static const gchar * const *non_relocatable_schema_list;
455 static const gchar * const *relocatable_schema_list;
456 static gsize schema_lists_initialised;
458 static void
459 ensure_schema_lists (void)
461 if (g_once_init_enter (&schema_lists_initialised))
463 GSettingsSchemaSource *source;
464 GHashTable *single, *reloc;
465 const gchar **ptr;
466 gchar **list;
467 gint i;
469 initialise_schema_sources ();
471 /* We use hash tables to avoid duplicate listings for schemas that
472 * appear in more than one file.
474 single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
475 reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
477 for (source = schema_sources; source; source = source->parent)
479 list = gvdb_table_list (source->table, "");
481 g_assert (list != NULL);
483 for (i = 0; list[i]; i++)
485 if (!g_hash_table_lookup (single, list[i]) &&
486 !g_hash_table_lookup (reloc, list[i]))
488 GvdbTable *table;
490 table = gvdb_table_get_table (source->table, list[i]);
491 g_assert (table != NULL);
493 if (gvdb_table_has_value (table, ".path"))
494 g_hash_table_insert (single, g_strdup (list[i]), NULL);
495 else
496 g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
498 gvdb_table_unref (table);
502 g_strfreev (list);
505 ptr = g_new (const gchar *, g_hash_table_size (single) + 1);
506 non_relocatable_schema_list = ptr;
507 g_hash_table_foreach_steal (single, steal_item, &ptr);
508 g_hash_table_unref (single);
509 *ptr = NULL;
511 ptr = g_new (const gchar *, g_hash_table_size (reloc) + 1);
512 relocatable_schema_list = ptr;
513 g_hash_table_foreach_steal (reloc, steal_item, &ptr);
514 g_hash_table_unref (reloc);
515 *ptr = NULL;
517 g_once_init_leave (&schema_lists_initialised, TRUE);
522 * g_settings_list_schemas:
524 * Gets a list of the #GSettings schemas installed on the system. The
525 * returned list is exactly the list of schemas for which you may call
526 * g_settings_new() without adverse effects.
528 * This function does not list the schemas that do not provide their own
529 * paths (ie: schemas for which you must use
530 * g_settings_new_with_path()). See
531 * g_settings_list_relocatable_schemas() for that.
533 * Returns: (element-type utf8) (transfer none): a list of #GSettings
534 * schemas that are available. The list must not be modified or
535 * freed.
537 * Since: 2.26
539 const gchar * const *
540 g_settings_list_schemas (void)
542 ensure_schema_lists ();
544 return non_relocatable_schema_list;
548 * g_settings_list_relocatable_schemas:
550 * Gets a list of the relocatable #GSettings schemas installed on the
551 * system. These are schemas that do not provide their own path. It is
552 * usual to instantiate these schemas directly, but if you want to you
553 * can use g_settings_new_with_path() to specify the path.
555 * The output of this function, taken together with the output of
556 * g_settings_list_schemas() represents the complete list of all
557 * installed schemas.
559 * Returns: (element-type utf8) (transfer none): a list of relocatable
560 * #GSettings schemas that are available. The list must not be
561 * modified or freed.
563 * Since: 2.28
565 const gchar * const *
566 g_settings_list_relocatable_schemas (void)
568 ensure_schema_lists ();
570 return relocatable_schema_list;
574 * g_settings_schema_ref:
575 * @schema: a #GSettingsSchema
577 * Increase the reference count of @schema, returning a new reference.
579 * Returns: a new reference to @schema
581 * Since: 2.32
583 GSettingsSchema *
584 g_settings_schema_ref (GSettingsSchema *schema)
586 g_atomic_int_inc (&schema->ref_count);
588 return schema;
592 * g_settings_schema_unref:
593 * @schema: a #GSettingsSchema
595 * Decrease the reference count of @schema, possibly freeing it.
597 * Since: 2.32
599 void
600 g_settings_schema_unref (GSettingsSchema *schema)
602 if (g_atomic_int_dec_and_test (&schema->ref_count))
604 gvdb_table_unref (schema->table);
605 g_free (schema->items);
606 g_free (schema->id);
608 g_slice_free (GSettingsSchema, schema);
612 const gchar *
613 g_settings_schema_get_string (GSettingsSchema *schema,
614 const gchar *key)
616 const gchar *result = NULL;
617 GVariant *value;
619 if ((value = gvdb_table_get_raw_value (schema->table, key)))
621 result = g_variant_get_string (value, NULL);
622 g_variant_unref (value);
625 return result;
628 GVariantIter *
629 g_settings_schema_get_value (GSettingsSchema *schema,
630 const gchar *key)
632 GVariantIter *iter;
633 GVariant *value;
635 value = gvdb_table_get_raw_value (schema->table, key);
637 if G_UNLIKELY (value == NULL)
638 g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
640 iter = g_variant_iter_new (value);
641 g_variant_unref (value);
643 return iter;
647 * g_settings_schema_get_path:
648 * @schema: a #GSettingsSchema
650 * Gets the path associated with @schema, or %NULL.
652 * Schemas may be single-instance or relocatable. Single-instance
653 * schemas correspond to exactly one set of keys in the backend
654 * database: those located at the path returned by this function.
656 * Relocatable schemas can be referenced by other schemas and can
657 * threfore describe multiple sets of keys at different locations. For
658 * relocatable schemas, this function will return %NULL.
660 * Returns: (transfer none): the path of the schema, or %NULL
662 * Since: 2.32
664 const gchar *
665 g_settings_schema_get_path (GSettingsSchema *schema)
667 return schema->path;
670 const gchar *
671 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
673 return schema->gettext_domain;
676 gboolean
677 g_settings_schema_has_key (GSettingsSchema *schema,
678 const gchar *key)
680 return gvdb_table_has_value (schema->table, key);
683 const GQuark *
684 g_settings_schema_list (GSettingsSchema *schema,
685 gint *n_items)
687 gint i, j;
689 if (schema->items == NULL)
691 gchar **list;
692 gint len;
694 list = gvdb_table_list (schema->table, "");
695 len = list ? g_strv_length (list) : 0;
697 schema->items = g_new (GQuark, len);
698 j = 0;
700 for (i = 0; i < len; i++)
701 if (list[i][0] != '.')
702 schema->items[j++] = g_quark_from_string (list[i]);
703 schema->n_items = j;
705 g_strfreev (list);
708 *n_items = schema->n_items;
709 return schema->items;
713 * g_settings_schema_get_id:
714 * @schema: a #GSettingsSchema
716 * Get the ID of @schema.
718 * Returns: (transfer none): the ID
720 const gchar *
721 g_settings_schema_get_id (GSettingsSchema *schema)
723 return schema->id;
726 static inline void
727 endian_fixup (GVariant **value)
729 #if G_BYTE_ORDER == G_BIG_ENDIAN
730 GVariant *tmp;
732 tmp = g_variant_byteswap (*value);
733 g_variant_unref (*value);
734 *value = tmp;
735 #endif
738 void
739 g_settings_schema_key_init (GSettingsSchemaKey *key,
740 GSettingsSchema *schema,
741 const gchar *name)
743 GVariantIter *iter;
744 GVariant *data;
745 guchar code;
747 memset (key, 0, sizeof *key);
749 iter = g_settings_schema_get_value (schema, name);
751 key->schema = g_settings_schema_ref (schema);
752 key->default_value = g_variant_iter_next_value (iter);
753 endian_fixup (&key->default_value);
754 key->type = g_variant_get_type (key->default_value);
755 key->name = g_intern_string (name);
757 while (g_variant_iter_next (iter, "(y*)", &code, &data))
759 switch (code)
761 case 'l':
762 /* translation requested */
763 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
764 break;
766 case 'e':
767 /* enumerated types... */
768 key->is_enum = TRUE;
769 goto choice;
771 case 'f':
772 /* flags... */
773 key->is_flags = TRUE;
774 goto choice;
776 choice: case 'c':
777 /* ..., choices, aliases */
778 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
779 break;
781 case 'r':
782 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
783 endian_fixup (&key->minimum);
784 endian_fixup (&key->maximum);
785 break;
787 default:
788 g_warning ("unknown schema extension '%c'", code);
789 break;
792 g_variant_unref (data);
795 g_variant_iter_free (iter);
798 void
799 g_settings_schema_key_clear (GSettingsSchemaKey *key)
801 if (key->minimum)
802 g_variant_unref (key->minimum);
804 if (key->maximum)
805 g_variant_unref (key->maximum);
807 g_variant_unref (key->default_value);
809 g_settings_schema_unref (key->schema);
812 gboolean
813 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
814 GVariant *value)
816 g_return_val_if_fail (value != NULL, FALSE);
818 return g_variant_is_of_type (value, key->type);
821 gboolean
822 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
823 GVariant *value)
825 if (key->minimum == NULL && key->strinfo == NULL)
826 return TRUE;
828 if (g_variant_is_container (value))
830 gboolean ok = TRUE;
831 GVariantIter iter;
832 GVariant *child;
834 g_variant_iter_init (&iter, value);
835 while (ok && (child = g_variant_iter_next_value (&iter)))
837 ok = g_settings_schema_key_range_check (key, child);
838 g_variant_unref (child);
841 return ok;
844 if (key->minimum)
846 return g_variant_compare (key->minimum, value) <= 0 &&
847 g_variant_compare (value, key->maximum) <= 0;
850 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
851 g_variant_get_string (value, NULL));
854 GVariant *
855 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
856 GVariant *value)
858 const gchar *target;
860 if (g_settings_schema_key_range_check (key, value))
861 return g_variant_ref (value);
863 if (key->strinfo == NULL)
864 return NULL;
866 if (g_variant_is_container (value))
868 GVariantBuilder builder;
869 GVariantIter iter;
870 GVariant *child;
872 g_variant_iter_init (&iter, value);
873 g_variant_builder_init (&builder, g_variant_get_type (value));
875 while ((child = g_variant_iter_next_value (&iter)))
877 GVariant *fixed;
879 fixed = g_settings_schema_key_range_fixup (key, child);
880 g_variant_unref (child);
882 if (fixed == NULL)
884 g_variant_builder_clear (&builder);
885 return NULL;
888 g_variant_builder_add_value (&builder, fixed);
889 g_variant_unref (fixed);
892 return g_variant_ref_sink (g_variant_builder_end (&builder));
895 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
896 g_variant_get_string (value, NULL));
897 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
901 GVariant *
902 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
904 const gchar *translated;
905 GError *error = NULL;
906 const gchar *domain;
907 GVariant *value;
909 domain = g_settings_schema_get_gettext_domain (key->schema);
911 if (key->lc_char == '\0')
912 /* translation not requested for this key */
913 return NULL;
915 if (key->lc_char == 't')
916 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
917 else
918 translated = g_dgettext (domain, key->unparsed);
920 if (translated == key->unparsed)
921 /* the default value was not translated */
922 return NULL;
924 /* try to parse the translation of the unparsed default */
925 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
927 if (value == NULL)
929 g_warning ("Failed to parse translated string `%s' for "
930 "key `%s' in schema `%s': %s", key->unparsed, key->name,
931 g_settings_schema_get_id (key->schema), error->message);
932 g_warning ("Using untranslated default instead.");
933 g_error_free (error);
936 else if (!g_settings_schema_key_range_check (key, value))
938 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
939 "is outside of valid range", key->unparsed, key->name,
940 g_settings_schema_get_id (key->schema));
941 g_variant_unref (value);
942 value = NULL;
945 return value;
948 gint
949 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
950 GVariant *value)
952 gboolean it_worked;
953 guint result;
955 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
956 g_variant_get_string (value, NULL),
957 &result);
959 /* 'value' can only come from the backend after being filtered for validity,
960 * from the translation after being filtered for validity, or from the schema
961 * itself (which the schema compiler checks for validity). If this assertion
962 * fails then it's really a bug in GSettings or the schema compiler...
964 g_assert (it_worked);
966 return result;
969 GVariant *
970 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
971 gint value)
973 const gchar *string;
975 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
977 if (string == NULL)
978 return NULL;
980 return g_variant_new_string (string);
983 guint
984 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
985 GVariant *value)
987 GVariantIter iter;
988 const gchar *flag;
989 guint result;
991 result = 0;
992 g_variant_iter_init (&iter, value);
993 while (g_variant_iter_next (&iter, "&s", &flag))
995 gboolean it_worked;
996 guint flag_value;
998 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
999 /* as in g_settings_to_enum() */
1000 g_assert (it_worked);
1002 result |= flag_value;
1005 return result;
1008 GVariant *
1009 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1010 guint value)
1012 GVariantBuilder builder;
1013 gint i;
1015 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1017 for (i = 0; i < 32; i++)
1018 if (value & (1u << i))
1020 const gchar *string;
1022 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1024 if (string == NULL)
1026 g_variant_builder_clear (&builder);
1027 return NULL;
1030 g_variant_builder_add (&builder, "s", string);
1033 return g_variant_builder_end (&builder);