2 * Copyright © 2009, 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.
19 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "gsettings.h"
32 #include "gdelayedsettingsbackend.h"
33 #include "gsettingsbackendinternal.h"
34 #include "gsettings-mapping.h"
35 #include "gio-marshal.h"
36 #include "gsettingsschema.h"
45 * @short_description: a high-level API for application settings
47 * The #GSettings class provides a convenient API for storing and retrieving
48 * application settings.
50 * When creating a GSettings instance, you have to specify a schema
51 * that describes the keys in your settings and their types and default
52 * values, as well as some other information.
54 * Normally, a schema has as fixed path that determines where the settings
55 * are stored in the conceptual global tree of settings. However, schemas
56 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
57 * useful e.g. when the schema describes an 'account', and you want to be
58 * able to store a arbitrary number of accounts.
60 * Unlike other configuration systems (like GConf), GSettings does not
61 * restrict keys to basic types like strings and numbers. GSettings stores
62 * values as #GVariant, and allows any #GVariantType for keys. Key names
63 * are restricted to lowercase characters, numbers and '-'. Furthermore,
64 * the names must begin with a lowercase character, must not end
65 * with a '-', and must not contain consecutive dashes. Key names can
66 * be up to 32 characters long.
68 * Similar to GConf, the default values in GSettings schemas can be
69 * localized, but the localized values are stored in gettext catalogs
70 * and looked up with the domain that is specified in the
71 * <tag class="attribute">gettext-domain</tag> attribute of the
72 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
73 * elements and the category that is specified in the l10n attribute of the
74 * <tag class="starttag">key</tag> element.
76 * GSettings uses schemas in a compact binary form that is created
77 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
78 * utility. The input is a schema description in an XML format that can be
79 * described by the following DTD:
80 * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/gschema.dtd"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
82 * At runtime, schemas are identified by their id (as specified
83 * in the <tag class="attribute">id</tag> attribute of the
84 * <tag class="starttag">schema</tag> element). The
85 * convention for schema ids is to use a dotted name, similar in
86 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
88 * <example><title>Default values</title>
89 * <programlisting><![CDATA[
91 * <schema id="org.gtk.test" path="/tests/" gettext-domain="test">
93 * <key name="greeting" type="s">
94 * <default l10n="messages">"Hello, earthlings"</default>
95 * <summary>A greeting</summary>
97 * Greeting of the invading martians
101 * <key name="box" type="(ii)">
102 * <default>(20,30)</default>
107 * ]]></programlisting></example>
109 * <example><title>Ranges, choices and enumerated types</title>
110 * <programlisting><![CDATA[
114 * <value nick="first" value="1"/>
115 * <value nick="second" value="2"/>
118 * <schema id="org.gtk.test">
120 * <key name="key-with-range" type="i">
121 * <range min="1" max="100"/>
122 * <default>10</default>
125 * <key name="key-with-choices" type="s">
127 * <choice value='Elisabeth'/>
128 * <choice value='Annabeth'/>
129 * <choice value='Joe'/>
132 * <alias value='Anna' target='Annabeth'/>
133 * <alias value='Beth' target='Elisabeth'/>
135 * <default>'Joe'</default>
138 * <key name='enumerated-key' enum='myenum'>
139 * <default>'first'</default>
144 * ]]></programlisting></example>
147 * <title>Vendor overrides</title>
149 * Default values are defined in the schemas that get installed by
150 * an application. Sometimes, it is necessary for a vendor or distributor
151 * to adjust these defaults. Since patching the XML source for the schema
152 * is inconvenient and error-prone,
153 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
154 * so-called 'vendor override' files. These are keyfiles in the same
155 * directory as the XML schema sources which can override default values.
156 * The schema id serves as the group name in the key file, and the values
157 * are expected in serialized GVariant form, as in the following example:
158 * <informalexample><programlisting>
162 * </programlisting></informalexample>
167 * <title>Binding</title>
169 * A very convenient feature of GSettings lets you bind #GObject properties
170 * directly to settings, using g_settings_bind(). Once a GObject property
171 * has been bound to a setting, changes on either side are automatically
172 * propagated to the other side. GSettings handles details like
173 * mapping between GObject and GVariant types, and preventing infinite
177 * This makes it very easy to hook up a preferences dialog to the
178 * underlying settings. To make this even more convenient, GSettings
179 * looks for a boolean property with the name "sensitivity" and
180 * automatically binds it to the writability of the bound setting.
181 * If this 'magic' gets in the way, it can be suppressed with the
182 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
187 struct _GSettingsPrivate
189 /* where the signals go... */
190 GMainContext *main_context;
192 GSettingsBackend *backend;
193 GSettingsSchema *schema;
197 GDelayedSettingsBackend *delayed;
211 SIGNAL_WRITABLE_CHANGE_EVENT,
212 SIGNAL_WRITABLE_CHANGED,
218 static guint g_settings_signals[N_SIGNALS];
220 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
224 g_settings_real_change_event (GSettings *settings,
231 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
233 for (i = 0; i < n_keys; i++)
234 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
235 keys[i], g_quark_to_string (keys[i]));
241 g_settings_real_writable_change_event (GSettings *settings,
244 const GQuark *keys = &key;
249 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
251 for (i = 0; i < n_keys; i++)
253 const gchar *string = g_quark_to_string (keys[i]);
255 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
257 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
265 settings_backend_changed (GObject *target,
266 GSettingsBackend *backend,
270 GSettings *settings = G_SETTINGS (target);
271 gboolean ignore_this;
274 g_assert (settings->priv->backend == backend);
276 for (i = 0; key[i] == settings->priv->path[i]; i++);
278 if (settings->priv->path[i] == '\0' &&
279 g_settings_schema_has_key (settings->priv->schema, key + i))
283 quark = g_quark_from_string (key + i);
284 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
285 0, &quark, 1, &ignore_this);
290 settings_backend_path_changed (GObject *target,
291 GSettingsBackend *backend,
295 GSettings *settings = G_SETTINGS (target);
296 gboolean ignore_this;
298 g_assert (settings->priv->backend == backend);
300 if (g_str_has_prefix (settings->priv->path, path))
301 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
302 0, NULL, 0, &ignore_this);
306 settings_backend_keys_changed (GObject *target,
307 GSettingsBackend *backend,
309 const gchar * const *items,
312 GSettings *settings = G_SETTINGS (target);
313 gboolean ignore_this;
316 g_assert (settings->priv->backend == backend);
318 for (i = 0; settings->priv->path[i] &&
319 settings->priv->path[i] == path[i]; i++);
326 for (j = 0; items[j]; j++)
328 const gchar *item = items[j];
331 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
333 if (settings->priv->path[i + k] == '\0' &&
334 g_settings_schema_has_key (settings->priv->schema, item + k))
335 quarks[l++] = g_quark_from_string (item + k);
337 /* "256 quarks ought to be enough for anybody!"
338 * If this bites you, I'm sorry. Please file a bug.
344 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
345 0, quarks, l, &ignore_this);
350 settings_backend_writable_changed (GObject *target,
351 GSettingsBackend *backend,
354 GSettings *settings = G_SETTINGS (target);
355 gboolean ignore_this;
358 g_assert (settings->priv->backend == backend);
360 for (i = 0; key[i] == settings->priv->path[i]; i++);
362 if (settings->priv->path[i] == '\0' &&
363 g_settings_schema_has_key (settings->priv->schema, key + i))
364 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
365 0, g_quark_from_string (key + i), &ignore_this);
369 settings_backend_path_writable_changed (GObject *target,
370 GSettingsBackend *backend,
373 GSettings *settings = G_SETTINGS (target);
374 gboolean ignore_this;
376 g_assert (settings->priv->backend == backend);
378 if (g_str_has_prefix (settings->priv->path, path))
379 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
380 0, (GQuark) 0, &ignore_this);
383 /* Properties, Construction, Destruction {{{1 */
385 g_settings_set_property (GObject *object,
390 GSettings *settings = G_SETTINGS (object);
395 g_assert (settings->priv->schema_name == NULL);
396 settings->priv->schema_name = g_value_dup_string (value);
400 settings->priv->path = g_value_dup_string (value);
404 settings->priv->backend = g_value_dup_object (value);
408 g_assert_not_reached ();
413 g_settings_get_property (GObject *object,
418 GSettings *settings = G_SETTINGS (object);
423 g_value_set_string (value, settings->priv->schema_name);
426 case PROP_HAS_UNAPPLIED:
427 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
431 g_assert_not_reached ();
435 static const GSettingsListenerVTable listener_vtable = {
436 settings_backend_changed,
437 settings_backend_path_changed,
438 settings_backend_keys_changed,
439 settings_backend_writable_changed,
440 settings_backend_path_writable_changed
444 g_settings_constructed (GObject *object)
446 GSettings *settings = G_SETTINGS (object);
447 const gchar *schema_path;
449 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
450 schema_path = g_settings_schema_get_path (settings->priv->schema);
452 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
453 g_error ("settings object created with schema '%s' and path '%s', but "
454 "path '%s' is specified by schema",
455 settings->priv->schema_name, settings->priv->path, schema_path);
457 if (settings->priv->path == NULL)
459 if (schema_path == NULL)
460 g_error ("attempting to create schema '%s' without a path",
461 settings->priv->schema_name);
463 settings->priv->path = g_strdup (schema_path);
466 if (settings->priv->backend == NULL)
467 settings->priv->backend = g_settings_backend_get_default ();
469 g_settings_backend_watch (settings->priv->backend,
470 &listener_vtable, G_OBJECT (settings),
471 settings->priv->main_context);
472 g_settings_backend_subscribe (settings->priv->backend,
473 settings->priv->path);
477 g_settings_finalize (GObject *object)
479 GSettings *settings = G_SETTINGS (object);
481 g_settings_backend_unsubscribe (settings->priv->backend,
482 settings->priv->path);
483 g_main_context_unref (settings->priv->main_context);
484 g_object_unref (settings->priv->backend);
485 g_object_unref (settings->priv->schema);
486 g_free (settings->priv->schema_name);
487 g_free (settings->priv->path);
489 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
493 g_settings_init (GSettings *settings)
495 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
499 settings->priv->main_context = g_main_context_get_thread_default ();
501 if (settings->priv->main_context == NULL)
502 settings->priv->main_context = g_main_context_default ();
504 g_main_context_ref (settings->priv->main_context);
508 g_settings_class_init (GSettingsClass *class)
510 GObjectClass *object_class = G_OBJECT_CLASS (class);
512 class->writable_change_event = g_settings_real_writable_change_event;
513 class->change_event = g_settings_real_change_event;
515 object_class->set_property = g_settings_set_property;
516 object_class->get_property = g_settings_get_property;
517 object_class->constructed = g_settings_constructed;
518 object_class->finalize = g_settings_finalize;
520 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
523 * GSettings::changed:
524 * @settings: the object on which the signal was emitted
525 * @key: the name of the key that changed
527 * The "changed" signal is emitted when a key has potentially changed.
528 * You should call one of the g_settings_get() calls to check the new
531 * This signal supports detailed connections. You can connect to the
532 * detailed signal "changed::x" in order to only receive callbacks
533 * when key "x" changes.
535 g_settings_signals[SIGNAL_CHANGED] =
536 g_signal_new ("changed", G_TYPE_SETTINGS,
537 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
538 G_STRUCT_OFFSET (GSettingsClass, changed),
539 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
540 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
543 * GSettings::change-event:
544 * @settings: the object on which the signal was emitted
545 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
546 * @n_keys: the length of the @keys array, or 0
547 * @returns: %TRUE to stop other handlers from being invoked for the
548 * event. FALSE to propagate the event further.
550 * The "change-event" signal is emitted once per change event that
551 * affects this settings object. You should connect to this signal
552 * only if you are interested in viewing groups of changes before they
553 * are split out into multiple emissions of the "changed" signal.
554 * For most use cases it is more appropriate to use the "changed" signal.
556 * In the event that the change event applies to one or more specified
557 * keys, @keys will be an array of #GQuark of length @n_keys. In the
558 * event that the change event applies to the #GSettings object as a
559 * whole (ie: potentially every key has been changed) then @keys will
560 * be %NULL and @n_keys will be 0.
562 * The default handler for this signal invokes the "changed" signal
563 * for each affected key. If any other connected handler returns
564 * %TRUE then this default functionality will be supressed.
566 g_settings_signals[SIGNAL_CHANGE_EVENT] =
567 g_signal_new ("change-event", G_TYPE_SETTINGS,
569 G_STRUCT_OFFSET (GSettingsClass, change_event),
570 g_signal_accumulator_true_handled, NULL,
571 _gio_marshal_BOOL__POINTER_INT,
572 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
575 * GSettings::writable-changed:
576 * @settings: the object on which the signal was emitted
579 * The "writable-changed" signal is emitted when the writability of a
580 * key has potentially changed. You should call
581 * g_settings_is_writable() in order to determine the new status.
583 * This signal supports detailed connections. You can connect to the
584 * detailed signal "writable-changed::x" in order to only receive
585 * callbacks when the writability of "x" changes.
587 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
588 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
589 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
590 G_STRUCT_OFFSET (GSettingsClass, changed),
591 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
592 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
595 * GSettings::writable-change-event:
596 * @settings: the object on which the signal was emitted
597 * @key: the quark of the key, or 0
598 * @returns: %TRUE to stop other handlers from being invoked for the
599 * event. FALSE to propagate the event further.
601 * The "writable-change-event" signal is emitted once per writability
602 * change event that affects this settings object. You should connect
603 * to this signal if you are interested in viewing groups of changes
604 * before they are split out into multiple emissions of the
605 * "writable-changed" signal. For most use cases it is more
606 * appropriate to use the "writable-changed" signal.
608 * In the event that the writability change applies only to a single
609 * key, @key will be set to the #GQuark for that key. In the event
610 * that the writability change affects the entire settings object,
613 * The default handler for this signal invokes the "writable-changed"
614 * and "changed" signals for each affected key. This is done because
615 * changes in writability might also imply changes in value (if for
616 * example, a new mandatory setting is introduced). If any other
617 * connected handler returns %TRUE then this default functionality
620 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
621 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
623 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
624 g_signal_accumulator_true_handled, NULL,
625 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
630 * The name of the context that the settings are stored in.
632 g_object_class_install_property (object_class, PROP_BACKEND,
633 g_param_spec_object ("backend",
634 P_("GSettingsBackend"),
635 P_("The GSettingsBackend for this settings object"),
636 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
637 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
642 * The name of the schema that describes the types of keys
643 * for this #GSettings object.
645 g_object_class_install_property (object_class, PROP_SCHEMA,
646 g_param_spec_string ("schema",
648 P_("The name of the schema for this settings object"),
650 G_PARAM_CONSTRUCT_ONLY |
651 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
656 * The path within the backend where the settings are stored.
658 g_object_class_install_property (object_class, PROP_PATH,
659 g_param_spec_string ("path",
661 P_("The path within the backend where the settings are"),
663 G_PARAM_CONSTRUCT_ONLY |
664 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
667 * GSettings:has-unapplied:
669 * If this property is %TRUE, the #GSettings object has outstanding
670 * changes that will be applied when g_settings_apply() is called.
672 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
673 g_param_spec_boolean ("has-unapplied",
674 P_("Has unapplied changes"),
675 P_("TRUE if there are outstanding changes to apply()"),
677 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
681 /* Construction (new, new_with_path, etc.) {{{1 */
684 * @schema: the name of the schema
685 * @returns: a new #GSettings object
687 * Creates a new #GSettings object with a given schema.
689 * Signals on the newly created #GSettings object will be dispatched
690 * via the thread-default #GMainContext in effect at the time of the
691 * call to g_settings_new(). The new #GSettings will hold a reference
692 * on the context. See g_main_context_push_thread_default().
697 g_settings_new (const gchar *schema)
699 g_return_val_if_fail (schema != NULL, NULL);
701 return g_object_new (G_TYPE_SETTINGS,
707 * g_settings_new_with_path:
708 * @schema: the name of the schema
709 * @path: the path to use
710 * @returns: a new #GSettings object
712 * Creates a new #GSettings object with a given schema and path.
714 * You only need to do this if you want to directly create a settings
715 * object with a schema that doesn't have a specified path of its own.
718 * It is a programmer error to call this function for a schema that
719 * has an explicitly specified path.
724 g_settings_new_with_path (const gchar *schema,
727 g_return_val_if_fail (schema != NULL, NULL);
728 g_return_val_if_fail (path != NULL, NULL);
730 return g_object_new (G_TYPE_SETTINGS,
737 * g_settings_new_with_backend:
738 * @schema: the name of the schema
739 * @backend: the #GSettingsBackend to use
740 * @returns: a new #GSettings object
742 * Creates a new #GSettings object with a given schema and backend.
744 * Creating settings objects with an different backend allows accessing settings
745 * from a database other than the usual one. For example, it may make
746 * sense to pass a backend corresponding to the "defaults" settings database on
747 * the system to get a settings object that modifies the system default
748 * settings instead of the settings for this user.
753 g_settings_new_with_backend (const gchar *schema,
754 GSettingsBackend *backend)
756 g_return_val_if_fail (schema != NULL, NULL);
757 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
759 return g_object_new (G_TYPE_SETTINGS,
766 * g_settings_new_with_backend_and_path:
767 * @schema: the name of the schema
768 * @backend: the #GSettingsBackend to use
769 * @path: the path to use
770 * @returns: a new #GSettings object
772 * Creates a new #GSettings object with a given schema, backend and
775 * This is a mix of g_settings_new_with_backend() and
776 * g_settings_new_with_path().
781 g_settings_new_with_backend_and_path (const gchar *schema,
782 GSettingsBackend *backend,
785 g_return_val_if_fail (schema != NULL, NULL);
786 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
787 g_return_val_if_fail (path != NULL, NULL);
789 return g_object_new (G_TYPE_SETTINGS,
796 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
802 GSettingsSchema *schema;
807 const guint32 *strinfo;
808 gsize strinfo_length;
810 const gchar *unparsed;
813 const GVariantType *type;
814 GVariant *minimum, *maximum;
815 GVariant *default_value;
819 g_settings_get_key_info (GSettingsKeyInfo *info,
827 memset (info, 0, sizeof *info);
829 iter = g_settings_schema_get_value (settings->priv->schema, key);
831 info->default_value = g_variant_iter_next_value (iter);
832 info->type = g_variant_get_type (info->default_value);
833 info->settings = g_object_ref (settings);
834 info->key = g_intern_string (key);
836 while (g_variant_iter_next (iter, "(y*)", &code, &data))
841 /* translation requested */
842 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
846 /* enumerated types... */
847 info->is_enum = TRUE;
852 info->is_flags = TRUE;
856 /* ..., choices, aliases */
857 info->strinfo = g_variant_get_fixed_array (data,
858 &info->strinfo_length,
863 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
867 g_warning ("unknown schema extension '%c'", code);
871 g_variant_unref (data);
874 g_variant_iter_free (iter);
878 g_settings_free_key_info (GSettingsKeyInfo *info)
881 g_variant_unref (info->minimum);
884 g_variant_unref (info->maximum);
886 g_variant_unref (info->default_value);
887 g_object_unref (info->settings);
891 g_settings_write_to_backend (GSettingsKeyInfo *info,
897 path = g_strconcat (info->settings->priv->path, info->key, NULL);
898 success = g_settings_backend_write (info->settings->priv->backend,
906 g_settings_type_check (GSettingsKeyInfo *info,
909 g_return_val_if_fail (value != NULL, FALSE);
911 return g_variant_is_of_type (value, info->type);
915 g_settings_range_check (GSettingsKeyInfo *info,
918 if (info->minimum == NULL && info->strinfo == NULL)
921 if (g_variant_is_container (value))
927 g_variant_iter_init (&iter, value);
928 while (ok && (child = g_variant_iter_next_value (&iter)))
930 ok = g_settings_range_check (info, child);
931 g_variant_unref (child);
939 return g_variant_compare (info->minimum, value) <= 0 &&
940 g_variant_compare (value, info->maximum) <= 0;
943 return strinfo_is_string_valid (info->strinfo,
944 info->strinfo_length,
945 g_variant_get_string (value, NULL));
949 g_settings_range_fixup (GSettingsKeyInfo *info,
954 if (g_settings_range_check (info, value))
955 return g_variant_ref (value);
957 if (info->strinfo == NULL)
960 if (g_variant_is_container (value))
962 GVariantBuilder builder;
966 g_variant_iter_init (&iter, value);
967 g_variant_builder_init (&builder, g_variant_get_type (value));
969 while ((child = g_variant_iter_next_value (&iter)))
973 fixed = g_settings_range_fixup (info, child);
974 g_variant_unref (child);
978 g_variant_builder_clear (&builder);
982 g_variant_builder_add_value (&builder, fixed);
983 g_variant_unref (fixed);
986 return g_variant_ref_sink (g_variant_builder_end (&builder));
989 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
990 g_variant_get_string (value, NULL));
991 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
995 g_settings_read_from_backend (GSettingsKeyInfo *info)
1001 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1002 value = g_settings_backend_read (info->settings->priv->backend,
1003 path, info->type, FALSE);
1008 fixup = g_settings_range_fixup (info, value);
1009 g_variant_unref (value);
1018 g_settings_get_translated_default (GSettingsKeyInfo *info)
1020 const gchar *translated;
1021 GError *error = NULL;
1022 const gchar *domain;
1025 if (info->lc_char == '\0')
1026 /* translation not requested for this key */
1029 domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
1031 if (info->lc_char == 't')
1032 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1034 translated = g_dgettext (domain, info->unparsed);
1036 if (translated == info->unparsed)
1037 /* the default value was not translated */
1040 /* try to parse the translation of the unparsed default */
1041 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1045 g_warning ("Failed to parse translated string `%s' for "
1046 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1047 info->settings->priv->schema_name, error->message);
1048 g_warning ("Using untranslated default instead.");
1049 g_error_free (error);
1052 else if (!g_settings_range_check (info, value))
1054 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1055 "is outside of valid range", info->unparsed, info->key,
1056 info->settings->priv->schema_name);
1057 g_variant_unref (value);
1065 g_settings_to_enum (GSettingsKeyInfo *info,
1071 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1072 g_variant_get_string (value, NULL),
1075 /* 'value' can only come from the backend after being filtered for validity,
1076 * from the translation after being filtered for validity, or from the schema
1077 * itself (which the schema compiler checks for validity). If this assertion
1078 * fails then it's really a bug in GSettings or the schema compiler...
1080 g_assert (it_worked);
1086 g_settings_from_enum (GSettingsKeyInfo *info,
1089 const gchar *string;
1091 string = strinfo_string_from_enum (info->strinfo,
1092 info->strinfo_length,
1098 return g_variant_new_string (string);
1102 g_settings_to_flags (GSettingsKeyInfo *info,
1110 g_variant_iter_init (&iter, value);
1111 while (g_variant_iter_next (&iter, "&s", &flag))
1116 it_worked = strinfo_enum_from_string (info->strinfo,
1117 info->strinfo_length,
1119 /* as in g_settings_to_enum() */
1120 g_assert (it_worked);
1122 result |= flag_value;
1129 g_settings_from_flags (GSettingsKeyInfo *info,
1132 GVariantBuilder builder;
1135 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1137 for (i = 0; i < 32; i++)
1138 if (value & (1u << i))
1140 const gchar *string;
1142 string = strinfo_string_from_enum (info->strinfo,
1143 info->strinfo_length,
1148 g_variant_builder_clear (&builder);
1152 g_variant_builder_add (&builder, "s", string);
1155 return g_variant_builder_end (&builder);
1158 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1160 * g_settings_get_value:
1161 * @settings: a #GSettings object
1162 * @key: the key to get the value for
1163 * @returns: a new #GVariant
1165 * Gets the value that is stored in @settings for @key.
1167 * It is a programmer error to give a @key that isn't contained in the
1168 * schema for @settings.
1173 g_settings_get_value (GSettings *settings,
1176 GSettingsKeyInfo info;
1179 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1180 g_return_val_if_fail (key != NULL, NULL);
1182 g_settings_get_key_info (&info, settings, key);
1183 value = g_settings_read_from_backend (&info);
1186 value = g_settings_get_translated_default (&info);
1189 value = g_variant_ref (info.default_value);
1191 g_settings_free_key_info (&info);
1197 * g_settings_get_enum:
1198 * @settings: a #GSettings object
1199 * @key: the key to get the value for
1200 * @returns: the enum value
1202 * Gets the value that is stored in @settings for @key and converts it
1203 * to the enum value that it represents.
1205 * In order to use this function the type of the value must be a string
1206 * and it must be marked in the schema file as an enumerated type.
1208 * It is a programmer error to give a @key that isn't contained in the
1209 * schema for @settings or is not marked as an enumerated type.
1211 * If the value stored in the configuration database is not a valid
1212 * value for the enumerated type then this function will return the
1218 g_settings_get_enum (GSettings *settings,
1221 GSettingsKeyInfo info;
1225 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1226 g_return_val_if_fail (key != NULL, -1);
1228 g_settings_get_key_info (&info, settings, key);
1232 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1233 "associated with an enumerated type", info.key);
1234 g_settings_free_key_info (&info);
1238 value = g_settings_read_from_backend (&info);
1241 value = g_settings_get_translated_default (&info);
1244 value = g_variant_ref (info.default_value);
1246 result = g_settings_to_enum (&info, value);
1247 g_settings_free_key_info (&info);
1248 g_variant_unref (value);
1254 * g_settings_set_enum:
1255 * @settings: a #GSettings object
1256 * @key: a key, within @settings
1257 * @value: an enumerated value
1258 * @returns: %TRUE, if the set succeeds
1260 * Looks up the enumerated type nick for @value and writes it to @key,
1263 * It is a programmer error to give a @key that isn't contained in the
1264 * schema for @settings or is not marked as an enumerated type, or for
1265 * @value not to be a valid value for the named type.
1267 * After performing the write, accessing @key directly with
1268 * g_settings_get_string() will return the 'nick' associated with
1272 g_settings_set_enum (GSettings *settings,
1276 GSettingsKeyInfo info;
1280 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1281 g_return_val_if_fail (key != NULL, FALSE);
1283 g_settings_get_key_info (&info, settings, key);
1287 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1288 "associated with an enumerated type", info.key);
1292 if (!(variant = g_settings_from_enum (&info, value)))
1294 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1295 "in schema `%s'. Doing nothing.", value, info.key,
1296 info.settings->priv->schema_name);
1297 g_settings_free_key_info (&info);
1301 success = g_settings_write_to_backend (&info, variant);
1302 g_settings_free_key_info (&info);
1308 * g_settings_get_flags:
1309 * @settings: a #GSettings object
1310 * @key: the key to get the value for
1311 * @returns: the flags value
1313 * Gets the value that is stored in @settings for @key and converts it
1314 * to the flags value that it represents.
1316 * In order to use this function the type of the value must be an array
1317 * of strings and it must be marked in the schema file as an flags type.
1319 * It is a programmer error to give a @key that isn't contained in the
1320 * schema for @settings or is not marked as a flags type.
1322 * If the value stored in the configuration database is not a valid
1323 * value for the flags type then this function will return the default
1329 g_settings_get_flags (GSettings *settings,
1332 GSettingsKeyInfo info;
1336 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1337 g_return_val_if_fail (key != NULL, -1);
1339 g_settings_get_key_info (&info, settings, key);
1343 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1344 "associated with a flags type", info.key);
1345 g_settings_free_key_info (&info);
1349 value = g_settings_read_from_backend (&info);
1352 value = g_settings_get_translated_default (&info);
1355 value = g_variant_ref (info.default_value);
1357 result = g_settings_to_flags (&info, value);
1358 g_settings_free_key_info (&info);
1359 g_variant_unref (value);
1365 * g_settings_set_flags:
1366 * @settings: a #GSettings object
1367 * @key: a key, within @settings
1368 * @value: a flags value
1369 * @returns: %TRUE, if the set succeeds
1371 * Looks up the flags type nicks for the bits specified by @value, puts
1372 * them in an array of strings and writes the array to @key, withing
1375 * It is a programmer error to give a @key that isn't contained in the
1376 * schema for @settings or is not marked as a flags type, or for @value
1377 * to contain any bits that are not value for the named type.
1379 * After performing the write, accessing @key directly with
1380 * g_settings_get_strv() will return an array of 'nicks'; one for each
1384 g_settings_set_flags (GSettings *settings,
1388 GSettingsKeyInfo info;
1392 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1393 g_return_val_if_fail (key != NULL, FALSE);
1395 g_settings_get_key_info (&info, settings, key);
1399 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1400 "associated with a flags type", info.key);
1404 if (!(variant = g_settings_from_flags (&info, value)))
1406 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1407 "for key `%s' in schema `%s'. Doing nothing.",
1408 value, info.key, info.settings->priv->schema_name);
1409 g_settings_free_key_info (&info);
1413 success = g_settings_write_to_backend (&info, variant);
1414 g_settings_free_key_info (&info);
1420 * g_settings_set_value:
1421 * @settings: a #GSettings object
1422 * @key: the name of the key to set
1423 * @value: a #GVariant of the correct type
1424 * @returns: %TRUE if setting the key succeeded,
1425 * %FALSE if the key was not writable
1427 * Sets @key in @settings to @value.
1429 * It is a programmer error to give a @key that isn't contained in the
1430 * schema for @settings or for @value to have the incorrect type, per
1433 * If @value is floating then this function consumes the reference.
1438 g_settings_set_value (GSettings *settings,
1442 GSettingsKeyInfo info;
1444 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1445 g_return_val_if_fail (key != NULL, FALSE);
1447 g_settings_get_key_info (&info, settings, key);
1448 g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1449 g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1450 g_settings_free_key_info (&info);
1452 return g_settings_write_to_backend (&info, value);
1457 * @settings: a #GSettings object
1458 * @key: the key to get the value for
1459 * @format: a #GVariant format string
1460 * @...: arguments as per @format
1462 * Gets the value that is stored at @key in @settings.
1464 * A convenience function that combines g_settings_get_value() with
1467 * It is a programmer error to give a @key that isn't contained in the
1468 * schema for @settings or for the #GVariantType of @format to mismatch
1469 * the type given in the schema.
1474 g_settings_get (GSettings *settings,
1476 const gchar *format,
1482 value = g_settings_get_value (settings, key);
1484 va_start (ap, format);
1485 g_variant_get_va (value, format, NULL, &ap);
1488 g_variant_unref (value);
1493 * @settings: a #GSettings object
1494 * @key: the name of the key to set
1495 * @format: a #GVariant format string
1496 * @...: arguments as per @format
1497 * @returns: %TRUE if setting the key succeeded,
1498 * %FALSE if the key was not writable
1500 * Sets @key in @settings to @value.
1502 * A convenience function that combines g_settings_set_value() with
1505 * It is a programmer error to give a @key that isn't contained in the
1506 * schema for @settings or for the #GVariantType of @format to mismatch
1507 * the type given in the schema.
1512 g_settings_set (GSettings *settings,
1514 const gchar *format,
1520 va_start (ap, format);
1521 value = g_variant_new_va (format, NULL, &ap);
1524 return g_settings_set_value (settings, key, value);
1528 * g_settings_get_mapped:
1529 * @settings: a #GSettings object
1530 * @key: the key to get the value for
1531 * @mapping: the function to map the value in the settings database to
1532 * the value used by the application
1533 * @user_data: user data for @mapping
1534 * @returns: the result, which may be %NULL
1536 * Gets the value that is stored at @key in @settings, subject to
1537 * application-level validation/mapping.
1539 * You should use this function when the application needs to perform
1540 * some processing on the value of the key (for example, parsing). The
1541 * @mapping function performs that processing. If the function
1542 * indicates that the processing was unsuccessful (due to a parse error,
1543 * for example) then the mapping is tried again with another value.
1545 * This allows a robust 'fall back to defaults' behaviour to be
1546 * implemented somewhat automatically.
1548 * The first value that is tried is the user's setting for the key. If
1549 * the mapping function fails to map this value, other values may be
1550 * tried in an unspecified order (system or site defaults, translated
1551 * schema default values, untranslated schema default values, etc).
1553 * If the mapping function fails for all possible values, one additional
1554 * attempt is made: the mapping function is called with a %NULL value.
1555 * If the mapping function still indicates failure at this point then
1556 * the application will be aborted.
1558 * The result parameter for the @mapping function is pointed to a
1559 * #gpointer which is initially set to %NULL. The same pointer is given
1560 * to each invocation of @mapping. The final value of that #gpointer is
1561 * what is returned by this function. %NULL is valid; it is returned
1562 * just as any other value would be.
1565 g_settings_get_mapped (GSettings *settings,
1567 GSettingsGetMapping mapping,
1570 gpointer result = NULL;
1571 GSettingsKeyInfo info;
1575 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1576 g_return_val_if_fail (key != NULL, NULL);
1577 g_return_val_if_fail (mapping != NULL, NULL);
1579 g_settings_get_key_info (&info, settings, key);
1581 if ((value = g_settings_read_from_backend (&info)))
1583 okay = mapping (value, &result, user_data);
1584 g_variant_unref (value);
1585 if (okay) goto okay;
1588 if ((value = g_settings_get_translated_default (&info)))
1590 okay = mapping (value, &result, user_data);
1591 g_variant_unref (value);
1592 if (okay) goto okay;
1595 if (mapping (info.default_value, &result, user_data))
1598 if (!mapping (NULL, &result, user_data))
1599 g_error ("The mapping function given to g_settings_get_mapped() for key "
1600 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1601 key, settings->priv->schema_name);
1604 g_settings_free_key_info (&info);
1609 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1611 * g_settings_get_string:
1612 * @settings: a #GSettings object
1613 * @key: the key to get the value for
1614 * @returns: a newly-allocated string
1616 * Gets the value that is stored at @key in @settings.
1618 * A convenience variant of g_settings_get() for strings.
1620 * It is a programmer error to give a @key that isn't specified as
1621 * having a string type in the schema for @settings.
1626 g_settings_get_string (GSettings *settings,
1632 value = g_settings_get_value (settings, key);
1633 result = g_variant_dup_string (value, NULL);
1634 g_variant_unref (value);
1640 * g_settings_set_string:
1641 * @settings: a #GSettings object
1642 * @key: the name of the key to set
1643 * @value: the value to set it to
1644 * @returns: %TRUE if setting the key succeeded,
1645 * %FALSE if the key was not writable
1647 * Sets @key in @settings to @value.
1649 * A convenience variant of g_settings_set() for strings.
1651 * It is a programmer error to give a @key that isn't specified as
1652 * having a string type in the schema for @settings.
1657 g_settings_set_string (GSettings *settings,
1661 return g_settings_set_value (settings, key, g_variant_new_string (value));
1665 * g_settings_get_int:
1666 * @settings: a #GSettings object
1667 * @key: the key to get the value for
1668 * @returns: an integer
1670 * Gets the value that is stored at @key in @settings.
1672 * A convenience variant of g_settings_get() for 32-bit integers.
1674 * It is a programmer error to give a @key that isn't specified as
1675 * having a int32 type in the schema for @settings.
1680 g_settings_get_int (GSettings *settings,
1686 value = g_settings_get_value (settings, key);
1687 result = g_variant_get_int32 (value);
1688 g_variant_unref (value);
1694 * g_settings_set_int:
1695 * @settings: a #GSettings object
1696 * @key: the name of the key to set
1697 * @value: the value to set it to
1698 * @returns: %TRUE if setting the key succeeded,
1699 * %FALSE if the key was not writable
1701 * Sets @key in @settings to @value.
1703 * A convenience variant of g_settings_set() for 32-bit integers.
1705 * It is a programmer error to give a @key that isn't specified as
1706 * having a int32 type in the schema for @settings.
1711 g_settings_set_int (GSettings *settings,
1715 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1719 * g_settings_get_double:
1720 * @settings: a #GSettings object
1721 * @key: the key to get the value for
1722 * @returns: a double
1724 * Gets the value that is stored at @key in @settings.
1726 * A convenience variant of g_settings_get() for doubles.
1728 * It is a programmer error to give a @key that isn't specified as
1729 * having a 'double' type in the schema for @settings.
1734 g_settings_get_double (GSettings *settings,
1740 value = g_settings_get_value (settings, key);
1741 result = g_variant_get_double (value);
1742 g_variant_unref (value);
1748 * g_settings_set_double:
1749 * @settings: a #GSettings object
1750 * @key: the name of the key to set
1751 * @value: the value to set it to
1752 * @returns: %TRUE if setting the key succeeded,
1753 * %FALSE if the key was not writable
1755 * Sets @key in @settings to @value.
1757 * A convenience variant of g_settings_set() for doubles.
1759 * It is a programmer error to give a @key that isn't specified as
1760 * having a 'double' type in the schema for @settings.
1765 g_settings_set_double (GSettings *settings,
1769 return g_settings_set_value (settings, key, g_variant_new_double (value));
1773 * g_settings_get_boolean:
1774 * @settings: a #GSettings object
1775 * @key: the key to get the value for
1776 * @returns: a boolean
1778 * Gets the value that is stored at @key in @settings.
1780 * A convenience variant of g_settings_get() for booleans.
1782 * It is a programmer error to give a @key that isn't specified as
1783 * having a boolean type in the schema for @settings.
1788 g_settings_get_boolean (GSettings *settings,
1794 value = g_settings_get_value (settings, key);
1795 result = g_variant_get_boolean (value);
1796 g_variant_unref (value);
1802 * g_settings_set_boolean:
1803 * @settings: a #GSettings object
1804 * @key: the name of the key to set
1805 * @value: the value to set it to
1806 * @returns: %TRUE if setting the key succeeded,
1807 * %FALSE if the key was not writable
1809 * Sets @key in @settings to @value.
1811 * A convenience variant of g_settings_set() for booleans.
1813 * It is a programmer error to give a @key that isn't specified as
1814 * having a boolean type in the schema for @settings.
1819 g_settings_set_boolean (GSettings *settings,
1823 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1827 * g_settings_get_strv:
1828 * @settings: a #GSettings object
1829 * @key: the key to get the value for
1830 * @returns: a newly-allocated, %NULL-terminated array of strings
1832 * Gets the value that is stored at @key in @settings.
1834 * A convenience variant of g_settings_get() for string arrays.
1836 * It is a programmer error to give a @key that isn't specified as
1837 * having an array of strings type in the schema for @settings.
1842 g_settings_get_strv (GSettings *settings,
1848 value = g_settings_get_value (settings, key);
1849 result = g_variant_dup_strv (value, NULL);
1850 g_variant_unref (value);
1856 * g_settings_set_strv:
1857 * @settings: a #GSettings object
1858 * @key: the name of the key to set
1859 * @value: (allow-none): the value to set it to, or %NULL
1860 * @returns: %TRUE if setting the key succeeded,
1861 * %FALSE if the key was not writable
1863 * Sets @key in @settings to @value.
1865 * A convenience variant of g_settings_set() for string arrays. If
1866 * @value is %NULL, then @key is set to be the empty array.
1868 * It is a programmer error to give a @key that isn't specified as
1869 * having an array of strings type in the schema for @settings.
1874 g_settings_set_strv (GSettings *settings,
1876 const gchar * const *value)
1881 array = g_variant_new_strv (value, -1);
1883 array = g_variant_new_strv (NULL, 0);
1885 return g_settings_set_value (settings, key, array);
1888 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1891 * @settings: a #GSettings object
1893 * Changes the #GSettings object into 'delay-apply' mode. In this
1894 * mode, changes to @settings are not immediately propagated to the
1895 * backend, but kept locally until g_settings_apply() is called.
1900 g_settings_delay (GSettings *settings)
1902 g_return_if_fail (G_IS_SETTINGS (settings));
1904 if (settings->priv->delayed)
1907 settings->priv->delayed =
1908 g_delayed_settings_backend_new (settings->priv->backend,
1910 settings->priv->main_context);
1911 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1912 g_object_unref (settings->priv->backend);
1914 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1915 g_settings_backend_watch (settings->priv->backend,
1916 &listener_vtable, G_OBJECT (settings),
1917 settings->priv->main_context);
1922 * @settings: a #GSettings instance
1924 * Applies any changes that have been made to the settings. This
1925 * function does nothing unless @settings is in 'delay-apply' mode;
1926 * see g_settings_delay(). In the normal case settings are always
1927 * applied immediately.
1930 g_settings_apply (GSettings *settings)
1932 if (settings->priv->delayed)
1934 GDelayedSettingsBackend *delayed;
1936 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1937 g_delayed_settings_backend_apply (delayed);
1942 * g_settings_revert:
1943 * @settings: a #GSettings instance
1945 * Reverts all non-applied changes to the settings. This function
1946 * does nothing unless @settings is in 'delay-apply' mode; see
1947 * g_settings_delay(). In the normal case settings are always applied
1950 * Change notifications will be emitted for affected keys.
1953 g_settings_revert (GSettings *settings)
1955 if (settings->priv->delayed)
1957 GDelayedSettingsBackend *delayed;
1959 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1960 g_delayed_settings_backend_revert (delayed);
1965 * g_settings_get_has_unapplied:
1966 * @settings: a #GSettings object
1967 * @returns: %TRUE if @settings has unapplied changes
1969 * Returns whether the #GSettings object has any unapplied
1970 * changes. This can only be the case if it is in 'delayed-apply' mode.
1975 g_settings_get_has_unapplied (GSettings *settings)
1977 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1979 return settings->priv->delayed &&
1980 g_delayed_settings_backend_get_has_unapplied (
1981 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1984 /* Extra API (reset, sync, get_child, is_writable, list_items) {{{1 */
1987 * @settings: a #GSettings object
1988 * @key: the name of a key
1990 * Resets @key to its default value.
1992 * This call resets the key, as much as possible, to its default value.
1993 * That might the value specified in the schema or the one set by the
1997 g_settings_reset (GSettings *settings,
2002 path = g_strconcat (settings->priv->path, key, NULL);
2003 g_settings_backend_reset (settings->priv->backend, path, NULL);
2010 * Ensures that all pending operations for the given are complete for
2011 * the default backend.
2013 * Writes made to a #GSettings are handled asynchronously. For this
2014 * reason, it is very unlikely that the changes have it to disk by the
2015 * time g_settings_set() returns.
2017 * This call will block until all of the writes have made it to the
2018 * backend. Since the mainloop is not running, no change notifications
2019 * will be dispatched during this call (but some may be queued by the
2020 * time the call is done).
2023 g_settings_sync (void)
2025 g_settings_backend_sync_default ();
2029 * g_settings_is_writable:
2030 * @settings: a #GSettings object
2031 * @name: the name of a key
2032 * @returns: %TRUE if the key @name is writable
2034 * Finds out if a key can be written or not
2039 g_settings_is_writable (GSettings *settings,
2045 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2047 path = g_strconcat (settings->priv->path, name, NULL);
2048 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2055 * g_settings_get_child:
2056 * @settings: a #GSettings object
2057 * @name: the name of the 'child' schema
2058 * @returns: a 'child' settings object
2060 * Creates a 'child' settings object which has a base path of
2061 * <replaceable>base-path</replaceable>/@name", where
2062 * <replaceable>base-path</replaceable> is the base path of @settings.
2064 * The schema for the child settings object must have been declared
2065 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2070 g_settings_get_child (GSettings *settings,
2073 const gchar *child_schema;
2078 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2080 child_name = g_strconcat (name, "/", NULL);
2081 child_schema = g_settings_schema_get_string (settings->priv->schema,
2083 if (child_schema == NULL)
2084 g_error ("Schema '%s' has no child '%s'",
2085 settings->priv->schema_name, name);
2087 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2088 child = g_object_new (G_TYPE_SETTINGS,
2089 "schema", child_schema,
2092 g_free (child_path);
2093 g_free (child_name);
2099 * g_settings_list_items:
2100 * @settings: a #GSettings object
2101 * @returns: a list of the keys on @settings
2103 * Introspects the list of keys and children on @settings.
2105 * The list that is returned is a mix of the keys and children. The
2106 * names of the children are suffixed with '/'. The names of the keys
2109 * You should probably not be calling this function from "normal" code
2110 * (since you should already know what keys are in your schema). This
2111 * function is intended for introspection reasons.
2113 * You should free the return value with g_free() when you are done with
2117 g_settings_list_items (GSettings *settings)
2124 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2125 strv = g_new (const gchar *, n_keys + 1);
2126 for (i = 0; i < n_keys; i++)
2127 strv[i] = g_quark_to_string (keys[i]);
2136 GSettingsKeyInfo info;
2139 GSettingsBindGetMapping get_mapping;
2140 GSettingsBindSetMapping set_mapping;
2142 GDestroyNotify destroy;
2144 guint writable_handler_id;
2145 guint property_handler_id;
2146 const GParamSpec *property;
2147 guint key_handler_id;
2149 /* prevent recursion */
2154 g_settings_binding_free (gpointer data)
2156 GSettingsBinding *binding = data;
2158 g_assert (!binding->running);
2160 if (binding->writable_handler_id)
2161 g_signal_handler_disconnect (binding->info.settings,
2162 binding->writable_handler_id);
2164 if (binding->key_handler_id)
2165 g_signal_handler_disconnect (binding->info.settings,
2166 binding->key_handler_id);
2168 if (g_signal_handler_is_connected (binding->object,
2169 binding->property_handler_id))
2170 g_signal_handler_disconnect (binding->object,
2171 binding->property_handler_id);
2173 g_settings_free_key_info (&binding->info);
2175 if (binding->destroy)
2176 binding->destroy (binding->user_data);
2178 g_slice_free (GSettingsBinding, binding);
2182 g_settings_binding_quark (const char *property)
2187 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2188 quark = g_quark_from_string (tmp);
2195 g_settings_binding_key_changed (GSettings *settings,
2199 GSettingsBinding *binding = user_data;
2200 GValue value = { 0, };
2203 g_assert (settings == binding->info.settings);
2204 g_assert (key == binding->info.key);
2206 if (binding->running)
2209 binding->running = TRUE;
2211 g_value_init (&value, binding->property->value_type);
2213 variant = g_settings_read_from_backend (&binding->info);
2214 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2216 /* silently ignore errors in the user's config database */
2217 g_variant_unref (variant);
2221 if (variant == NULL)
2223 variant = g_settings_get_translated_default (&binding->info);
2225 !binding->get_mapping (&value, variant, binding->user_data))
2227 /* flag translation errors with a warning */
2228 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2229 "was rejected by the binding mapping function",
2230 binding->info.unparsed, binding->info.key,
2231 binding->info.settings->priv->schema_name);
2232 g_variant_unref (variant);
2237 if (variant == NULL)
2239 variant = g_variant_ref (binding->info.default_value);
2240 if (!binding->get_mapping (&value, variant, binding->user_data))
2241 g_error ("The schema default value for key `%s' in schema `%s' "
2242 "was rejected by the binding mapping function.",
2244 binding->info.settings->priv->schema_name);
2247 g_object_set_property (binding->object, binding->property->name, &value);
2248 g_variant_unref (variant);
2249 g_value_unset (&value);
2251 binding->running = FALSE;
2255 g_settings_binding_property_changed (GObject *object,
2256 const GParamSpec *pspec,
2259 GSettingsBinding *binding = user_data;
2260 GValue value = { 0, };
2263 g_assert (object == binding->object);
2264 g_assert (pspec == binding->property);
2266 if (binding->running)
2269 binding->running = TRUE;
2271 g_value_init (&value, pspec->value_type);
2272 g_object_get_property (object, pspec->name, &value);
2273 if ((variant = binding->set_mapping (&value, binding->info.type,
2274 binding->user_data)))
2276 if (g_variant_is_floating (variant))
2277 g_variant_ref_sink (variant);
2279 if (!g_settings_type_check (&binding->info, variant))
2281 g_critical ("binding mapping function for key `%s' returned "
2282 "GVariant of type `%s' when type `%s' was requested",
2283 binding->info.key, g_variant_get_type_string (variant),
2284 g_variant_type_dup_string (binding->info.type));
2288 if (!g_settings_range_check (&binding->info, variant))
2290 g_critical ("GObject property `%s' on a `%s' object is out of "
2291 "schema-specified range for key `%s' of `%s': %s",
2292 binding->property->name,
2293 g_type_name (binding->property->owner_type),
2295 binding->info.settings->priv->schema_name,
2296 g_variant_print (variant, TRUE));
2300 g_settings_write_to_backend (&binding->info, variant);
2301 g_variant_unref (variant);
2303 g_value_unset (&value);
2305 binding->running = FALSE;
2309 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2313 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2318 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2319 const GVariantType *expected_type,
2322 return g_variant_new_boolean (!g_value_get_boolean (value));
2327 * @settings: a #GSettings object
2328 * @key: the key to bind
2329 * @object: a #GObject
2330 * @property: the name of the property to bind
2331 * @flags: flags for the binding
2333 * Create a binding between the @key in the @settings object
2334 * and the property @property of @object.
2336 * The binding uses the default GIO mapping functions to map
2337 * between the settings and property values. These functions
2338 * handle booleans, numeric types and string types in a
2339 * straightforward way. Use g_settings_bind_with_mapping() if
2340 * you need a custom mapping, or map between types that are not
2341 * supported by the default mapping functions.
2343 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2344 * function also establishes a binding between the writability of
2345 * @key and the "sensitive" property of @object (if @object has
2346 * a boolean property by that name). See g_settings_bind_writable()
2347 * for more details about writable bindings.
2349 * Note that the lifecycle of the binding is tied to the object,
2350 * and that you can have only one binding per object property.
2351 * If you bind the same property twice on the same object, the second
2352 * binding overrides the first one.
2357 g_settings_bind (GSettings *settings,
2360 const gchar *property,
2361 GSettingsBindFlags flags)
2363 GSettingsBindGetMapping get_mapping = NULL;
2364 GSettingsBindSetMapping set_mapping = NULL;
2366 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2368 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2369 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2371 /* can't pass this flag to g_settings_bind_with_mapping() */
2372 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2375 g_settings_bind_with_mapping (settings, key, object, property, flags,
2376 get_mapping, set_mapping, NULL, NULL);
2380 * g_settings_bind_with_mapping:
2381 * @settings: a #GSettings object
2382 * @key: the key to bind
2383 * @object: a #GObject
2384 * @property: the name of the property to bind
2385 * @flags: flags for the binding
2386 * @get_mapping: a function that gets called to convert values
2387 * from @settings to @object, or %NULL to use the default GIO mapping
2388 * @set_mapping: a function that gets called to convert values
2389 * from @object to @settings, or %NULL to use the default GIO mapping
2390 * @user_data: data that gets passed to @get_mapping and @set_mapping
2391 * @destroy: #GDestroyNotify function for @user_data
2393 * Create a binding between the @key in the @settings object
2394 * and the property @property of @object.
2396 * The binding uses the provided mapping functions to map between
2397 * settings and property values.
2399 * Note that the lifecycle of the binding is tied to the object,
2400 * and that you can have only one binding per object property.
2401 * If you bind the same property twice on the same object, the second
2402 * binding overrides the first one.
2407 g_settings_bind_with_mapping (GSettings *settings,
2410 const gchar *property,
2411 GSettingsBindFlags flags,
2412 GSettingsBindGetMapping get_mapping,
2413 GSettingsBindSetMapping set_mapping,
2415 GDestroyNotify destroy)
2417 GSettingsBinding *binding;
2418 GObjectClass *objectclass;
2419 gchar *detailed_signal;
2420 GQuark binding_quark;
2422 g_return_if_fail (G_IS_SETTINGS (settings));
2423 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2425 objectclass = G_OBJECT_GET_CLASS (object);
2427 binding = g_slice_new0 (GSettingsBinding);
2428 g_settings_get_key_info (&binding->info, settings, key);
2429 binding->object = object;
2430 binding->property = g_object_class_find_property (objectclass, property);
2431 binding->user_data = user_data;
2432 binding->destroy = destroy;
2433 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2434 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2436 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2437 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2439 if (binding->property == NULL)
2441 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2442 property, G_OBJECT_TYPE_NAME (object));
2446 if ((flags & G_SETTINGS_BIND_GET) &&
2447 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2449 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2450 "writable", property, G_OBJECT_TYPE_NAME (object));
2453 if ((flags & G_SETTINGS_BIND_SET) &&
2454 (binding->property->flags & G_PARAM_READABLE) == 0)
2456 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2457 "readable", property, G_OBJECT_TYPE_NAME (object));
2461 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2463 /* g_settings_bind_invert_boolean_get_mapping() is a private
2464 * function, so if we are here it means that g_settings_bind() was
2465 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2467 * Ensure that both sides are boolean.
2470 if (binding->property->value_type != G_TYPE_BOOLEAN)
2472 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2473 "was specified, but property `%s' on type `%s' has "
2474 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2475 g_type_name ((binding->property->value_type)));
2479 if (!g_variant_type_equal (binding->info.type, G_VARIANT_TYPE_BOOLEAN))
2481 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2482 "was specified, but key `%s' on schema `%s' has "
2483 "type `%s'", key, settings->priv->schema_name,
2484 g_variant_type_dup_string (binding->info.type));
2490 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2491 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2492 !g_settings_mapping_is_compatible (binding->property->value_type,
2493 binding->info.type))
2495 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2496 "'%s' which is not compatible with type '%s' of key '%s' "
2497 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2498 g_type_name (binding->property->value_type),
2499 g_variant_type_dup_string (binding->info.type), key,
2500 settings->priv->schema_name);
2504 if ((flags & G_SETTINGS_BIND_SET) &&
2505 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2507 GParamSpec *sensitive;
2509 sensitive = g_object_class_find_property (objectclass, "sensitive");
2511 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2512 (sensitive->flags & G_PARAM_WRITABLE))
2513 g_settings_bind_writable (settings, binding->info.key,
2514 object, "sensitive", FALSE);
2517 if (flags & G_SETTINGS_BIND_SET)
2519 detailed_signal = g_strdup_printf ("notify::%s", property);
2520 binding->property_handler_id =
2521 g_signal_connect (object, detailed_signal,
2522 G_CALLBACK (g_settings_binding_property_changed),
2524 g_free (detailed_signal);
2526 if (~flags & G_SETTINGS_BIND_GET)
2527 g_settings_binding_property_changed (object,
2532 if (flags & G_SETTINGS_BIND_GET)
2534 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2536 detailed_signal = g_strdup_printf ("changed::%s", key);
2537 binding->key_handler_id =
2538 g_signal_connect (settings, detailed_signal,
2539 G_CALLBACK (g_settings_binding_key_changed),
2541 g_free (detailed_signal);
2544 g_settings_binding_key_changed (settings, binding->info.key, binding);
2547 binding_quark = g_settings_binding_quark (property);
2548 g_object_set_qdata_full (object, binding_quark,
2549 binding, g_settings_binding_free);
2552 /* Writability binding {{{1 */
2555 GSettings *settings;
2558 const gchar *property;
2561 } GSettingsWritableBinding;
2564 g_settings_writable_binding_free (gpointer data)
2566 GSettingsWritableBinding *binding = data;
2568 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2569 g_object_unref (binding->settings);
2570 g_slice_free (GSettingsWritableBinding, binding);
2574 g_settings_binding_writable_changed (GSettings *settings,
2578 GSettingsWritableBinding *binding = user_data;
2581 g_assert (settings == binding->settings);
2582 g_assert (key == binding->key);
2584 writable = g_settings_is_writable (settings, key);
2586 if (binding->inverted)
2587 writable = !writable;
2589 g_object_set (binding->object, binding->property, writable, NULL);
2593 * g_settings_bind_writable:
2594 * @settings: a #GSettings object
2595 * @key: the key to bind
2596 * @object: a #GObject
2597 * @property: the name of a boolean property to bind
2598 * @inverted: whether to 'invert' the value
2600 * Create a binding between the writability of @key in the
2601 * @settings object and the property @property of @object.
2602 * The property must be boolean; "sensitive" or "visible"
2603 * properties of widgets are the most likely candidates.
2605 * Writable bindings are always uni-directional; changes of the
2606 * writability of the setting will be propagated to the object
2607 * property, not the other way.
2609 * When the @inverted argument is %TRUE, the binding inverts the
2610 * value as it passes from the setting to the object, i.e. @property
2611 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2614 * Note that the lifecycle of the binding is tied to the object,
2615 * and that you can have only one binding per object property.
2616 * If you bind the same property twice on the same object, the second
2617 * binding overrides the first one.
2622 g_settings_bind_writable (GSettings *settings,
2625 const gchar *property,
2628 GSettingsWritableBinding *binding;
2629 gchar *detailed_signal;
2632 g_return_if_fail (G_IS_SETTINGS (settings));
2634 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2637 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2638 property, G_OBJECT_TYPE_NAME (object));
2641 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2643 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2644 property, G_OBJECT_TYPE_NAME (object));
2648 binding = g_slice_new (GSettingsWritableBinding);
2649 binding->settings = g_object_ref (settings);
2650 binding->object = object;
2651 binding->key = g_intern_string (key);
2652 binding->property = g_intern_string (property);
2653 binding->inverted = inverted;
2655 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2656 binding->handler_id =
2657 g_signal_connect (settings, detailed_signal,
2658 G_CALLBACK (g_settings_binding_writable_changed),
2660 g_free (detailed_signal);
2662 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2663 binding, g_settings_writable_binding_free);
2665 g_settings_binding_writable_changed (settings, binding->key, binding);
2669 * g_settings_unbind:
2670 * @object: the object
2671 * @property: the property whose binding is removed
2673 * Removes an existing binding for @property on @object.
2675 * Note that bindings are automatically removed when the
2676 * object is finalized, so it is rarely necessary to call this
2682 g_settings_unbind (gpointer object,
2683 const gchar *property)
2685 GQuark binding_quark;
2687 binding_quark = g_settings_binding_quark (property);
2688 g_object_set_qdata (object, binding_quark, NULL);
2693 /* vim:set foldmethod=marker: */