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"
46 * @short_description: a high-level API for application settings
48 * The #GSettings class provides a convenient API for storing and retrieving
49 * application settings.
51 * When creating a GSettings instance, you have to specify a schema
52 * that describes the keys in your settings and their types and default
53 * values, as well as some other information.
55 * Normally, a schema has as fixed path that determines where the settings
56 * are stored in the conceptual global tree of settings. However, schemas
57 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
58 * useful e.g. when the schema describes an 'account', and you want to be
59 * able to store a arbitrary number of accounts.
61 * Unlike other configuration systems (like GConf), GSettings does not
62 * restrict keys to basic types like strings and numbers. GSettings stores
63 * values as #GVariant, and allows any #GVariantType for keys. Key names
64 * are restricted to lowercase characters, numbers and '-'. Furthermore,
65 * the names must begin with a lowercase character, must not end
66 * with a '-', and must not contain consecutive dashes. Key names can
67 * be up to 32 characters long.
69 * Similar to GConf, the default values in GSettings schemas can be
70 * localized, but the localized values are stored in gettext catalogs
71 * and looked up with the domain that is specified in the
72 * <tag class="attribute">gettext-domain</tag> attribute of the
73 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
74 * elements and the category that is specified in the l10n attribute of the
75 * <tag class="starttag">key</tag> element.
77 * GSettings uses schemas in a compact binary form that is created
78 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
79 * utility. The input is a schema description in an XML format that can be
80 * described by the following DTD:
82 * <!ELEMENT schemalist (schema|enum)* >
83 * <!ATTLIST schemalist gettext-domain #IMPLIED >
85 * <!ELEMENT schema (key|child)* >
86 * <!ATTLIST schema id CDATA #REQUIRED
88 * gettext-domain CDATA #IMPLIED >
90 * <!-- defines an enumerated type -->
91 * <!-- each value element maps a nick to a numeric value -->
92 * <!ELEMENT enum (value*) >
93 * <!ATTLIST enum id CDATA #REQUIRED >
94 * <!ELEMENT value EMPTY >
95 * <!-- nick must be at least 2 characters long -->
96 * <!-- value must be parsable as a 32-bit integer -->
97 * <!ELEMENT value nick #REQUIRED
100 * <!ELEMENT key (default|summary?|description?|range?|choices?|aliases?) >
101 * <!-- name can only contain lowercase letters, numbers and '-' -->
102 * <!-- type must be a GVariant type string -->
103 * <!-- enum must be the id of an enum that has been defined earlier -->
104 * <!-- exactly one of enum or type must be given -->
105 * <!ATTLIST key name CDATA #REQUIRED
106 * type CDATA #IMPLIED
107 * enum CDATA #IMPLIED >
109 * <!-- the default value is specified a a serialized GVariant,
110 * i.e. you have to include the quotes when specifying a string -->
111 * <!ELEMENT default (#PCDATA) >
112 * <!-- the presence of the l10n attribute marks a default value for
113 * translation, its value is the gettext category to use -->
114 * <!-- if context is present, it specifies msgctxt to use -->
115 * <!ATTLIST default l10n (messages|time) #IMPLIED
116 * context CDATA #IMPLIED >
118 * <!ELEMENT summary (#PCDATA) >
119 * <!ELEMENT description (#PCDATA) >
121 * <!-- range is only allowed for keys with numeric type -->
122 * <!ELEMENT range EMPTY >
123 * <!-- min and max must be parseable as values of the key type and min < max -->
124 * <!ATTLIST range min CDATA #REQUIRED
125 * max CDATA #REQUIRED >
127 * <!-- choices is only allowed for keys with string or string array type -->
128 * <!ELEMENT choices (choice+) >
129 * <!-- each choice element specifies one possible value -->
130 * <!ELEMENT choice EMPTY >
131 * <!ATTLIST choice value CDATA #REQUIRED >
133 * <!-- aliases is only allowed for keys with enumerated type or with choices -->
134 * <!ELEMENT aliases (alias+) >
135 * <!-- each alias element specifies an alias for one of the possible values -->
136 * <!ELEMENT alias EMPTY >
137 * <!ATTLIST alias value CDATA #REQUIRED >
139 * <!ELEMENT child EMPTY >
140 * <!ATTLIST child name CDATA #REQUIRED
141 * schema CDATA #REQUIRED >
145 * At runtime, schemas are identified by their id (as specified
146 * in the <tag class="attribute">id</tag> attribute of the
147 * <tag class="starttag">schema</tag> element). The
148 * convention for schema ids is to use a dotted name, similar in
149 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
151 * <example><title>Default values</title>
152 * <programlisting><![CDATA[
154 * <schema id="org.gtk.test" path="/tests/" gettext-domain="test">
156 * <key name="greeting" type="s">
157 * <default l10n="messages">"Hello, earthlings"</default>
158 * <summary>A greeting</summary>
160 * Greeting of the invading martians
164 * <key name="box" type="(ii)">
165 * <default>(20,30)</default>
170 * ]]></programlisting></example>
172 * <example><title>Ranges, choices and enumerated types</title>
173 * <programlisting><![CDATA[
177 * <value nick="first" value="1"/>
178 * <value nick="second" value="2"/>
181 * <schema id="org.gtk.test">
183 * <key name="key-with-range" type="i">
184 * <range min="1" max="100"/>
185 * <default>10</default>
188 * <key name="key-with-choices" type="s">
190 * <choice value='Elisabeth'/>
191 * <choice value='Annabeth'/>
192 * <choice value='Joe'/>
195 * <alias value="Annabeth" alias="Anna"/>
196 * <alias value="Elisabeth" alias="Beth"/>
198 * <default>'Joe'</default>
201 * <key name="enumerated-key" enum="myenum">
202 * <default>'first'</default>
207 * ]]></programlisting></example>
210 * <title>Binding</title>
212 * A very convenient feature of GSettings lets you bind #GObject properties
213 * directly to settings, using g_settings_bind(). Once a GObject property
214 * has been bound to a setting, changes on either side are automatically
215 * propagated to the other side. GSettings handles details like
216 * mapping between GObject and GVariant types, and preventing infinite
220 * This makes it very easy to hook up a preferences dialog to the
221 * underlying settings. To make this even more convenient, GSettings
222 * looks for a boolean property with the name "sensitivity" and
223 * automatically binds it to the writability of the bound setting.
224 * If this 'magic' gets in the way, it can be suppressed with the
225 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
230 struct _GSettingsPrivate
232 /* where the signals go... */
233 GMainContext *main_context;
235 GSettingsBackend *backend;
236 GSettingsSchema *schema;
241 GDelayedSettingsBackend *delayed;
256 SIGNAL_WRITABLE_CHANGE_EVENT,
257 SIGNAL_WRITABLE_CHANGED,
263 static guint g_settings_signals[N_SIGNALS];
265 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
269 g_settings_real_change_event (GSettings *settings,
276 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
278 for (i = 0; i < n_keys; i++)
279 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
280 keys[i], g_quark_to_string (keys[i]));
286 g_settings_real_writable_change_event (GSettings *settings,
289 const GQuark *keys = &key;
294 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
296 for (i = 0; i < n_keys; i++)
298 const gchar *string = g_quark_to_string (keys[i]);
300 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
302 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
310 settings_backend_changed (GSettingsBackend *backend,
315 GSettings *settings = G_SETTINGS (target);
316 gboolean ignore_this;
319 g_assert (settings->priv->backend == backend);
321 for (i = 0; key[i] == settings->priv->path[i]; i++);
323 if (settings->priv->path[i] == '\0' &&
324 g_settings_schema_has_key (settings->priv->schema, key + i))
328 quark = g_quark_from_string (key + i);
329 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
330 0, &quark, 1, &ignore_this);
335 settings_backend_path_changed (GSettingsBackend *backend,
340 GSettings *settings = G_SETTINGS (target);
341 gboolean ignore_this;
343 g_assert (settings->priv->backend == backend);
345 if (g_str_has_prefix (settings->priv->path, path))
346 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
347 0, NULL, 0, &ignore_this);
351 settings_backend_keys_changed (GSettingsBackend *backend,
354 const gchar * const *items,
357 GSettings *settings = G_SETTINGS (target);
358 gboolean ignore_this;
361 g_assert (settings->priv->backend == backend);
363 for (i = 0; settings->priv->path[i] &&
364 settings->priv->path[i] == path[i]; i++);
371 for (j = 0; items[j]; j++)
373 const gchar *item = items[j];
376 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
378 if (settings->priv->path[i + k] == '\0' &&
379 g_settings_schema_has_key (settings->priv->schema, item + k))
380 quarks[l++] = g_quark_from_string (item + k);
382 /* "256 quarks ought to be enough for anybody!"
383 * If this bites you, I'm sorry. Please file a bug.
389 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
390 0, quarks, l, &ignore_this);
395 settings_backend_writable_changed (GSettingsBackend *backend,
399 GSettings *settings = G_SETTINGS (target);
400 gboolean ignore_this;
403 g_assert (settings->priv->backend == backend);
405 for (i = 0; key[i] == settings->priv->path[i]; i++);
407 if (settings->priv->path[i] == '\0' &&
408 g_settings_schema_has_key (settings->priv->schema, key + i))
409 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
410 0, g_quark_from_string (key + i), &ignore_this);
414 settings_backend_path_writable_changed (GSettingsBackend *backend,
418 GSettings *settings = G_SETTINGS (target);
419 gboolean ignore_this;
421 g_assert (settings->priv->backend == backend);
423 if (g_str_has_prefix (settings->priv->path, path))
424 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
425 0, (GQuark) 0, &ignore_this);
428 /* Properties, Construction, Destruction {{{1 */
430 g_settings_set_property (GObject *object,
435 GSettings *settings = G_SETTINGS (object);
440 g_assert (settings->priv->schema_name == NULL);
441 settings->priv->schema_name = g_value_dup_string (value);
445 settings->priv->path = g_value_dup_string (value);
449 settings->priv->context = g_value_dup_string (value);
453 g_assert_not_reached ();
458 g_settings_get_property (GObject *object,
463 GSettings *settings = G_SETTINGS (object);
468 g_value_set_object (value, settings->priv->schema);
471 case PROP_HAS_UNAPPLIED:
472 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
476 g_assert_not_reached ();
481 g_settings_constructed (GObject *object)
483 GSettings *settings = G_SETTINGS (object);
484 const gchar *schema_path;
486 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
487 schema_path = g_settings_schema_get_path (settings->priv->schema);
489 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
490 g_error ("settings object created with schema '%s' and path '%s', but "
491 "path '%s' is specified by schema",
492 settings->priv->schema_name, settings->priv->path, schema_path);
494 if (settings->priv->path == NULL)
496 if (schema_path == NULL)
497 g_error ("attempting to create schema '%s' without a path",
498 settings->priv->schema_name);
500 settings->priv->path = g_strdup (schema_path);
503 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
504 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
505 settings->priv->main_context,
506 settings_backend_changed,
507 settings_backend_path_changed,
508 settings_backend_keys_changed,
509 settings_backend_writable_changed,
510 settings_backend_path_writable_changed);
511 g_settings_backend_subscribe (settings->priv->backend,
512 settings->priv->path);
516 g_settings_finalize (GObject *object)
518 GSettings *settings = G_SETTINGS (object);
520 g_settings_backend_unsubscribe (settings->priv->backend,
521 settings->priv->path);
522 g_main_context_unref (settings->priv->main_context);
523 g_object_unref (settings->priv->backend);
524 g_object_unref (settings->priv->schema);
525 g_free (settings->priv->schema_name);
526 g_free (settings->priv->context);
527 g_free (settings->priv->path);
531 g_settings_init (GSettings *settings)
533 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
537 settings->priv->main_context = g_main_context_get_thread_default ();
539 if (settings->priv->main_context == NULL)
540 settings->priv->main_context = g_main_context_default ();
542 g_main_context_ref (settings->priv->main_context);
546 g_settings_class_init (GSettingsClass *class)
548 GObjectClass *object_class = G_OBJECT_CLASS (class);
550 class->writable_change_event = g_settings_real_writable_change_event;
551 class->change_event = g_settings_real_change_event;
553 object_class->set_property = g_settings_set_property;
554 object_class->get_property = g_settings_get_property;
555 object_class->constructed = g_settings_constructed;
556 object_class->finalize = g_settings_finalize;
558 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
561 * GSettings::changed:
562 * @settings: the object on which the signal was emitted
563 * @key: the name of the key that changed
565 * The "changed" signal is emitted when a key has potentially changed.
566 * You should call one of the g_settings_get() calls to check the new
569 * This signal supports detailed connections. You can connect to the
570 * detailed signal "changed::x" in order to only receive callbacks
571 * when key "x" changes.
573 g_settings_signals[SIGNAL_CHANGED] =
574 g_signal_new ("changed", G_TYPE_SETTINGS,
575 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
576 G_STRUCT_OFFSET (GSettingsClass, changed),
577 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
578 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
581 * GSettings::change-event:
582 * @settings: the object on which the signal was emitted
583 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
584 * @n_keys: the length of the @keys array, or 0
585 * @returns: %TRUE to stop other handlers from being invoked for the
586 * event. FALSE to propagate the event further.
588 * The "change-event" signal is emitted once per change event that
589 * affects this settings object. You should connect to this signal
590 * only if you are interested in viewing groups of changes before they
591 * are split out into multiple emissions of the "changed" signal.
592 * For most use cases it is more appropriate to use the "changed" signal.
594 * In the event that the change event applies to one or more specified
595 * keys, @keys will be an array of #GQuark of length @n_keys. In the
596 * event that the change event applies to the #GSettings object as a
597 * whole (ie: potentially every key has been changed) then @keys will
598 * be %NULL and @n_keys will be 0.
600 * The default handler for this signal invokes the "changed" signal
601 * for each affected key. If any other connected handler returns
602 * %TRUE then this default functionality will be supressed.
604 g_settings_signals[SIGNAL_CHANGE_EVENT] =
605 g_signal_new ("change-event", G_TYPE_SETTINGS,
607 G_STRUCT_OFFSET (GSettingsClass, change_event),
608 g_signal_accumulator_true_handled, NULL,
609 _gio_marshal_BOOL__POINTER_INT,
610 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
613 * GSettings::writable-changed:
614 * @settings: the object on which the signal was emitted
617 * The "writable-changed" signal is emitted when the writability of a
618 * key has potentially changed. You should call
619 * g_settings_is_writable() in order to determine the new status.
621 * This signal supports detailed connections. You can connect to the
622 * detailed signal "writable-changed::x" in order to only receive
623 * callbacks when the writability of "x" changes.
625 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
626 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
627 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
628 G_STRUCT_OFFSET (GSettingsClass, changed),
629 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
630 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
633 * GSettings::writable-change-event:
634 * @settings: the object on which the signal was emitted
635 * @key: the quark of the key, or 0
636 * @returns: %TRUE to stop other handlers from being invoked for the
637 * event. FALSE to propagate the event further.
639 * The "writable-change-event" signal is emitted once per writability
640 * change event that affects this settings object. You should connect
641 * to this signal if you are interested in viewing groups of changes
642 * before they are split out into multiple emissions of the
643 * "writable-changed" signal. For most use cases it is more
644 * appropriate to use the "writable-changed" signal.
646 * In the event that the writability change applies only to a single
647 * key, @key will be set to the #GQuark for that key. In the event
648 * that the writability change affects the entire settings object,
651 * The default handler for this signal invokes the "writable-changed"
652 * and "changed" signals for each affected key. This is done because
653 * changes in writability might also imply changes in value (if for
654 * example, a new mandatory setting is introduced). If any other
655 * connected handler returns %TRUE then this default functionality
658 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
659 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
661 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
662 g_signal_accumulator_true_handled, NULL,
663 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
668 * The name of the context that the settings are stored in.
670 g_object_class_install_property (object_class, PROP_CONTEXT,
671 g_param_spec_string ("context",
673 P_("The name of the context for this settings object"),
674 "", G_PARAM_CONSTRUCT_ONLY |
675 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
680 * The name of the schema that describes the types of keys
681 * for this #GSettings object.
683 g_object_class_install_property (object_class, PROP_SCHEMA,
684 g_param_spec_string ("schema",
686 P_("The name of the schema for this settings object"),
688 G_PARAM_CONSTRUCT_ONLY |
689 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
694 * The path within the backend where the settings are stored.
696 g_object_class_install_property (object_class, PROP_PATH,
697 g_param_spec_string ("path",
699 P_("The path within the backend where the settings are"),
701 G_PARAM_CONSTRUCT_ONLY |
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
705 * GSettings:has-unapplied:
707 * If this property is %TRUE, the #GSettings object has outstanding
708 * changes that will be applied when g_settings_apply() is called.
710 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
711 g_param_spec_boolean ("has-unapplied",
712 P_("Has unapplied changes"),
713 P_("TRUE if there are outstanding changes to apply()"),
715 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
719 /* Construction (new, new_with_path, etc.) {{{1 */
722 * @schema: the name of the schema
723 * @returns: a new #GSettings object
725 * Creates a new #GSettings object with a given schema.
727 * Signals on the newly created #GSettings object will be dispatched
728 * via the thread-default #GMainContext in effect at the time of the
729 * call to g_settings_new(). The new #GSettings will hold a reference
730 * on the context. See g_main_context_push_thread_default().
735 g_settings_new (const gchar *schema)
737 g_return_val_if_fail (schema != NULL, NULL);
739 return g_object_new (G_TYPE_SETTINGS,
745 * g_settings_new_with_path:
746 * @schema: the name of the schema
747 * @path: the path to use
748 * @returns: a new #GSettings object
750 * Creates a new #GSettings object with a given schema and path.
752 * You only need to do this if you want to directly create a settings
753 * object with a schema that doesn't have a specified path of its own.
756 * It is a programmer error to call this function for a schema that
757 * has an explicitly specified path.
762 g_settings_new_with_path (const gchar *schema,
765 g_return_val_if_fail (schema != NULL, NULL);
766 g_return_val_if_fail (path != NULL, NULL);
768 return g_object_new (G_TYPE_SETTINGS,
775 * g_settings_new_with_context:
776 * @schema: the name of the schema
777 * @context: the context to use
778 * @returns: a new #GSettings object
780 * Creates a new #GSettings object with a given schema and context.
782 * Creating settings objects with a context allow accessing settings
783 * from a database other than the usual one. For example, it may make
784 * sense to specify "defaults" in order to get a settings object that
785 * modifies the system default settings instead of the settings for this
788 * It is a programmer error to call this function for an unsupported
789 * context. Use g_settings_supports_context() to determine if a context
790 * is supported if you are unsure.
795 g_settings_new_with_context (const gchar *schema,
796 const gchar *context)
798 g_return_val_if_fail (schema != NULL, NULL);
799 g_return_val_if_fail (context != NULL, NULL);
801 return g_object_new (G_TYPE_SETTINGS,
808 * g_settings_new_with_context_and_path:
809 * @schema: the name of the schema
810 * @path: the path to use
811 * @returns: a new #GSettings object
813 * Creates a new #GSettings object with a given schema, context and
816 * This is a mix of g_settings_new_with_context() and
817 * g_settings_new_with_path().
822 g_settings_new_with_context_and_path (const gchar *schema,
823 const gchar *context,
826 g_return_val_if_fail (schema != NULL, NULL);
827 g_return_val_if_fail (context != NULL, NULL);
828 g_return_val_if_fail (path != NULL, NULL);
830 return g_object_new (G_TYPE_SETTINGS,
837 /* Internal read/write utilities, enum conversion, validation {{{1 */
843 GSettingsSchema *schema;
846 const guint32 *strinfo;
847 gsize strinfo_length;
849 const gchar *unparsed;
852 const GVariantType *type;
853 GVariant *minimum, *maximum;
854 GVariant *default_value;
858 g_settings_get_key_info (GSettingsKeyInfo *info,
866 memset (info, 0, sizeof *info);
868 iter = g_settings_schema_get_value (settings->priv->schema, key);
870 info->default_value = g_variant_iter_next_value (iter);
871 info->type = g_variant_get_type (info->default_value);
872 info->schema = settings->priv->schema;
873 info->settings = settings;
876 while (g_variant_iter_next (iter, "(y*)", &code, &data))
881 /* translation requested */
882 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
886 /* enumerated types, ... */
887 info->is_enum = TRUE;
889 /* ..., choices, aliases */
890 info->strinfo = g_variant_get_fixed_array (data,
891 &info->strinfo_length,
896 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
900 g_warning ("unknown schema extension '%c'", code);
904 g_variant_unref (data);
907 g_variant_iter_free (iter);
911 g_settings_free_key_info (GSettingsKeyInfo *info)
914 g_variant_unref (info->minimum);
917 g_variant_unref (info->maximum);
919 g_variant_unref (info->default_value);
923 g_settings_write_to_backend (GSettingsKeyInfo *info,
929 path = g_strconcat (info->settings->priv->path, info->key, NULL);
930 success = g_settings_backend_write (info->settings->priv->backend,
938 g_settings_type_check (GSettingsKeyInfo *info,
941 g_return_val_if_fail (value != NULL, FALSE);
943 return g_variant_is_of_type (value, info->type);
947 g_settings_range_check (GSettingsKeyInfo *info,
950 if (info->minimum == NULL && info->strinfo == NULL)
953 if (g_variant_is_container (value))
959 g_variant_iter_init (&iter, value);
960 while (ok && (child = g_variant_iter_next_value (&iter)))
962 ok = g_settings_range_check (info, value);
963 g_variant_unref (child);
971 return g_variant_compare (info->minimum, value) <= 0 &&
972 g_variant_compare (value, info->maximum) <= 0;
975 return strinfo_is_string_valid (info->strinfo,
976 info->strinfo_length,
977 g_variant_get_string (value, NULL));
981 g_settings_range_fixup (GSettingsKeyInfo *info,
986 if (g_settings_range_check (info, value))
987 return g_variant_ref (value);
989 if (info->strinfo == NULL)
992 if (g_variant_is_container (value))
994 GVariantBuilder builder;
998 g_variant_builder_init (&builder, g_variant_get_type (value));
1000 while ((child = g_variant_iter_next_value (&iter)))
1004 fixed = g_settings_range_fixup (info, child);
1005 g_variant_unref (child);
1009 g_variant_builder_clear (&builder);
1013 g_variant_builder_add_value (&builder, fixed);
1014 g_variant_unref (fixed);
1017 return g_variant_ref_sink (g_variant_builder_end (&builder));
1020 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1021 g_variant_get_string (value, NULL));
1022 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1026 g_settings_read_from_backend (GSettingsKeyInfo *info)
1032 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1033 value = g_settings_backend_read (info->settings->priv->backend,
1034 path, info->type, FALSE);
1039 fixup = g_settings_range_fixup (info, value);
1040 g_variant_unref (value);
1049 g_settings_get_translated_default (GSettingsKeyInfo *info)
1051 const gchar *translated;
1052 GError *error = NULL;
1053 const gchar *domain;
1056 if (info->lc_char == '\0')
1057 /* translation not requested for this key */
1060 domain = g_settings_schema_get_gettext_domain (info->schema);
1062 if (info->lc_char == 't')
1063 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1065 translated = g_dgettext (domain, info->unparsed);
1067 if (translated == info->unparsed)
1068 /* the default value was not translated */
1071 /* try to parse the translation of the unparsed default */
1072 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1076 g_warning ("Failed to parse translated string `%s' for "
1077 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1078 info->settings->priv->schema_name, error->message);
1079 g_warning ("Using untranslated default instead.");
1080 g_error_free (error);
1083 else if (!g_settings_range_check (info, value))
1085 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1086 "is outside of valid range", info->unparsed, info->key,
1087 info->settings->priv->schema_name);
1088 g_variant_unref (value);
1096 g_settings_to_enum (GSettingsKeyInfo *info,
1102 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1103 g_variant_get_string (value, NULL),
1106 /* 'value' can only come from the backend after being filtered for validity,
1107 * from the translation after being filtered for validity, or from the schema
1108 * itself (which the schema compiler checks for validity). If this assertion
1109 * fails then it's really a bug in GSettings or the schema compiler...
1111 g_assert (it_worked);
1117 g_settings_from_enum (GSettingsKeyInfo *info,
1120 const gchar *string;
1122 string = strinfo_string_from_enum (info->strinfo,
1123 info->strinfo_length,
1129 return g_variant_ref_sink (g_variant_new_string (string));
1132 /* Public Get/Set API {{{1 (get, get_value, set, set_value) */
1134 * g_settings_get_value:
1135 * @settings: a #GSettings object
1136 * @key: the key to get the value for
1137 * @returns: a new #GVariant
1139 * Gets the value that is stored in @settings for @key.
1141 * It is a programmer error to give a @key that isn't valid for
1147 g_settings_get_value (GSettings *settings,
1150 GSettingsKeyInfo info;
1153 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1154 g_return_val_if_fail (key != NULL, NULL);
1156 g_settings_get_key_info (&info, settings, key);
1157 value = g_settings_read_from_backend (&info);
1160 value = g_settings_get_translated_default (&info);
1163 value = g_variant_ref (info.default_value);
1165 g_settings_free_key_info (&info);
1171 * g_settings_get_enum:
1172 * @settings: a #GSettings object
1173 * @key: the key to get the value for
1174 * @returns: the enum value
1176 * Gets the value that is stored in @settings for @key and converts it
1177 * to the enum value that it represents.
1179 * In order to use this function the type of the value must be a string
1180 * and it must be marked in the schema file as an enumerated type.
1182 * It is a programmer error to give a @key that isn't valid for
1183 * @settings, or is not marked as an enumerated type in the schema.
1185 * If the value stored in the configuration database is not a valid
1186 * value for the enumerated type then this function will return the
1192 g_settings_get_enum (GSettings *settings,
1195 GSettingsKeyInfo info;
1199 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1200 g_return_val_if_fail (key != NULL, -1);
1202 g_settings_get_key_info (&info, settings, key);
1206 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1207 "associated with an enumerated type", info.key);
1208 g_settings_free_key_info (&info);
1212 value = g_settings_read_from_backend (&info);
1215 value = g_settings_get_translated_default (&info);
1218 value = g_variant_ref (info.default_value);
1220 result = g_settings_to_enum (&info, value);
1221 g_settings_free_key_info (&info);
1222 g_variant_unref (value);
1228 * g_settings_set_enum:
1229 * @settings: a #GSettings object
1230 * @key: a key, within @settings
1231 * @value: an enumerated value
1232 * Returns: %TRUE, if the set succeeds
1234 * Looks up the enumerated type nick for @value and writes it to @key,
1237 * It is a programmer error for @key to not be listed in the schema or
1238 * for it not to be tagged as an enumerated type, or for @value not to
1239 * be a valid value for the named type.
1241 * After performing the write, accessing @key directly
1242 * g_settings_get_string() will return the 'nick' associated with
1246 g_settings_set_enum (GSettings *settings,
1250 GSettingsKeyInfo info;
1254 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1255 g_return_val_if_fail (key != NULL, FALSE);
1257 g_settings_get_key_info (&info, settings, key);
1261 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1262 "associated with an enumerated type", info.key);
1266 if (!(variant = g_settings_from_enum (&info, value)))
1268 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1269 "in schema `%s'. Doing nothing.", value, info.key,
1270 info.settings->priv->schema_name);
1271 g_settings_free_key_info (&info);
1275 success = g_settings_write_to_backend (&info, variant);
1276 g_settings_free_key_info (&info);
1277 g_variant_unref (variant);
1283 * g_settings_set_value:
1284 * @settings: a #GSettings object
1285 * @key: the name of the key to set
1286 * @value: a #GVariant of the correct type
1287 * @returns: %TRUE if setting the key succeeded,
1288 * %FALSE if the key was not writable
1290 * Sets @key in @settings to @value.
1292 * It is a programmer error to give a @key that isn't valid for
1293 * @settings. It is a programmer error to give a @value of the
1296 * If @value is floating then this function consumes the reference.
1301 g_settings_set_value (GSettings *settings,
1305 GSettingsKeyInfo info;
1307 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1308 g_return_val_if_fail (key != NULL, FALSE);
1310 g_settings_get_key_info (&info, settings, key);
1311 g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1312 g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1313 g_settings_free_key_info (&info);
1315 return g_settings_write_to_backend (&info, value);
1320 * @settings: a #GSettings object
1321 * @key: the key to get the value for
1322 * @format: a #GVariant format string
1323 * @...: arguments as per @format
1325 * Gets the value that is stored at @key in @settings.
1327 * A convenience function that combines g_settings_get_value() with
1330 * It is a programmer error to pass a @key that isn't valid for
1331 * @settings or a @format_string that doesn't match the type of @key according
1332 * to the schema of @settings.
1337 g_settings_get (GSettings *settings,
1339 const gchar *format,
1345 value = g_settings_get_value (settings, key);
1347 va_start (ap, format);
1348 g_variant_get_va (value, format, NULL, &ap);
1351 g_variant_unref (value);
1356 * @settings: a #GSettings object
1357 * @key: the name of the key to set
1358 * @format: a #GVariant format string
1359 * @...: arguments as per @format
1360 * @returns: %TRUE if setting the key succeeded,
1361 * %FALSE if the key was not writable
1363 * Sets @key in @settings to @value.
1365 * A convenience function that combines g_settings_set_value() with
1368 * It is a programmer error to pass a @key that isn't valid for
1369 * @settings or a @format that doesn't match the type of @key according
1370 * to the schema of @settings.
1375 g_settings_set (GSettings *settings,
1377 const gchar *format,
1383 va_start (ap, format);
1384 value = g_variant_new_va (format, NULL, &ap);
1387 return g_settings_set_value (settings, key, value);
1390 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1392 * g_settings_get_string:
1393 * @settings: a #GSettings object
1394 * @key: the key to get the value for
1395 * @returns: a newly-allocated string
1397 * Gets the value that is stored at @key in @settings.
1399 * A convenience variant of g_settings_get() for strings.
1401 * It is a programmer error to pass a @key that isn't valid for
1402 * @settings or is not of type string.
1407 g_settings_get_string (GSettings *settings,
1413 value = g_settings_get_value (settings, key);
1414 result = g_variant_dup_string (value, NULL);
1415 g_variant_unref (value);
1421 * g_settings_set_string:
1422 * @settings: a #GSettings object
1423 * @key: the name of the key to set
1424 * @value: the value to set it to
1425 * @returns: %TRUE if setting the key succeeded,
1426 * %FALSE if the key was not writable
1428 * Sets @key in @settings to @value.
1430 * A convenience variant of g_settings_set() for strings.
1432 * It is a programmer error to pass a @key that isn't valid for
1433 * @settings or is not of type string.
1438 g_settings_set_string (GSettings *settings,
1442 return g_settings_set_value (settings, key, g_variant_new_string (value));
1446 * g_settings_get_int:
1447 * @settings: a #GSettings object
1448 * @key: the key to get the value for
1449 * @returns: an integer
1451 * Gets the value that is stored at @key in @settings.
1453 * A convenience variant of g_settings_get() for 32-bit integers.
1455 * It is a programmer error to pass a @key that isn't valid for
1456 * @settings or is not of type int32.
1461 g_settings_get_int (GSettings *settings,
1467 value = g_settings_get_value (settings, key);
1468 result = g_variant_get_int32 (value);
1469 g_variant_unref (value);
1475 * g_settings_set_int:
1476 * @settings: a #GSettings object
1477 * @key: the name of the key to set
1478 * @value: the value to set it to
1479 * @returns: %TRUE if setting the key succeeded,
1480 * %FALSE if the key was not writable
1482 * Sets @key in @settings to @value.
1484 * A convenience variant of g_settings_set() for 32-bit integers.
1486 * It is a programmer error to pass a @key that isn't valid for
1487 * @settings or is not of type int32.
1492 g_settings_set_int (GSettings *settings,
1496 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1500 * g_settings_get_double:
1501 * @settings: a #GSettings object
1502 * @key: the key to get the value for
1503 * @returns: a double
1505 * Gets the value that is stored at @key in @settings.
1507 * A convenience variant of g_settings_get() for doubles.
1509 * It is a programmer error to pass a @key that isn't valid for
1510 * @settings or is not of type double.
1515 g_settings_get_double (GSettings *settings,
1521 value = g_settings_get_value (settings, key);
1522 result = g_variant_get_double (value);
1523 g_variant_unref (value);
1529 * g_settings_set_double:
1530 * @settings: a #GSettings object
1531 * @key: the name of the key to set
1532 * @value: the value to set it to
1533 * @returns: %TRUE if setting the key succeeded,
1534 * %FALSE if the key was not writable
1536 * Sets @key in @settings to @value.
1538 * A convenience variant of g_settings_set() for doubles.
1540 * It is a programmer error to pass a @key that isn't valid for
1541 * @settings or is not of type double.
1546 g_settings_set_double (GSettings *settings,
1550 return g_settings_set_value (settings, key, g_variant_new_double (value));
1554 * g_settings_get_boolean:
1555 * @settings: a #GSettings object
1556 * @key: the key to get the value for
1557 * @returns: a boolean
1559 * Gets the value that is stored at @key in @settings.
1561 * A convenience variant of g_settings_get() for booleans.
1563 * It is a programmer error to pass a @key that isn't valid for
1564 * @settings or is not of type boolean.
1569 g_settings_get_boolean (GSettings *settings,
1575 value = g_settings_get_value (settings, key);
1576 result = g_variant_get_boolean (value);
1577 g_variant_unref (value);
1583 * g_settings_set_boolean:
1584 * @settings: a #GSettings object
1585 * @key: the name of the key to set
1586 * @value: the value to set it to
1587 * @returns: %TRUE if setting the key succeeded,
1588 * %FALSE if the key was not writable
1590 * Sets @key in @settings to @value.
1592 * A convenience variant of g_settings_set() for booleans.
1594 * It is a programmer error to pass a @key that isn't valid for
1595 * @settings or is not of type boolean.
1600 g_settings_set_boolean (GSettings *settings,
1604 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1608 * g_settings_get_strv:
1609 * @settings: a #GSettings object
1610 * @key: the key to get the value for
1611 * @returns: a newly-allocated, %NULL-terminated array of strings
1613 * Gets the value that is stored at @key in @settings.
1615 * A convenience variant of g_settings_get() for string arrays.
1617 * It is a programmer error to pass a @key that isn't valid for
1618 * @settings or is not of type 'string array'.
1623 g_settings_get_strv (GSettings *settings,
1629 value = g_settings_get_value (settings, key);
1630 result = g_variant_dup_strv (value, NULL);
1631 g_variant_unref (value);
1637 * g_settings_set_strv:
1638 * @settings: a #GSettings object
1639 * @key: the name of the key to set
1640 * @value: (allow-none): the value to set it to, or %NULL
1641 * @returns: %TRUE if setting the key succeeded,
1642 * %FALSE if the key was not writable
1644 * Sets @key in @settings to @value.
1646 * A convenience variant of g_settings_set() for string arrays. If
1647 * @value is %NULL, then @key is set to be the empty array.
1649 * It is a programmer error to pass a @key that isn't valid for
1650 * @settings or is not of type 'string array'.
1655 g_settings_set_strv (GSettings *settings,
1657 const gchar * const *value)
1662 array = g_variant_new_strv (value, -1);
1664 array = g_variant_new_strv (NULL, 0);
1666 return g_settings_set_value (settings, key, array);
1668 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1671 * @settings: a #GSettings object
1673 * Changes the #GSettings object into 'delay-apply' mode. In this
1674 * mode, changes to @settings are not immediately propagated to the
1675 * backend, but kept locally until g_settings_apply() is called.
1680 g_settings_delay (GSettings *settings)
1682 g_return_if_fail (G_IS_SETTINGS (settings));
1684 if (settings->priv->delayed)
1687 settings->priv->delayed =
1688 g_delayed_settings_backend_new (settings->priv->backend,
1690 settings->priv->main_context);
1691 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1692 g_object_unref (settings->priv->backend);
1694 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1695 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
1696 settings->priv->main_context,
1697 settings_backend_changed,
1698 settings_backend_path_changed,
1699 settings_backend_keys_changed,
1700 settings_backend_writable_changed,
1701 settings_backend_path_writable_changed);
1706 * @settings: a #GSettings instance
1708 * Applies any changes that have been made to the settings. This
1709 * function does nothing unless @settings is in 'delay-apply' mode;
1710 * see g_settings_set_delay_apply(). In the normal case settings are
1711 * always applied immediately.
1714 g_settings_apply (GSettings *settings)
1716 if (settings->priv->delayed)
1718 GDelayedSettingsBackend *delayed;
1720 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1721 g_delayed_settings_backend_apply (delayed);
1726 * g_settings_revert:
1727 * @settings: a #GSettings instance
1729 * Reverts all non-applied changes to the settings. This function
1730 * does nothing unless @settings is in 'delay-apply' mode; see
1731 * g_settings_set_delay_apply(). In the normal case settings are
1732 * always applied immediately.
1734 * Change notifications will be emitted for affected keys.
1737 g_settings_revert (GSettings *settings)
1739 if (settings->priv->delayed)
1741 GDelayedSettingsBackend *delayed;
1743 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1744 g_delayed_settings_backend_revert (delayed);
1749 * g_settings_get_has_unapplied:
1750 * @settings: a #GSettings object
1751 * @returns: %TRUE if @settings has unapplied changes
1753 * Returns whether the #GSettings object has any unapplied
1754 * changes. This can only be the case if it is in 'delayed-apply' mode.
1759 g_settings_get_has_unapplied (GSettings *settings)
1761 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1763 return settings->priv->delayed &&
1764 g_delayed_settings_backend_get_has_unapplied (
1765 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1768 /* Extra API (sync, get_child, is_writable) {{{1 */
1771 * @context: the context to sync, or %NULL
1773 * Ensures that all pending operations for the given context are
1776 * Writes made to a #GSettings are handled asynchronously. For this
1777 * reason, it is very unlikely that the changes have it to disk by the
1778 * time g_settings_set() returns.
1780 * This call will block until all of the writes have made it to the
1781 * backend. Since the mainloop is not running, no change notifications
1782 * will be dispatched during this call (but some may be queued by the
1783 * time the call is done).
1786 g_settings_sync (const gchar *context)
1788 GSettingsBackend *backend;
1790 if (context == NULL)
1793 backend = g_settings_backend_get_with_context (context);
1794 g_settings_backend_sync (backend);
1798 * g_settings_is_writable:
1799 * @settings: a #GSettings object
1800 * @name: the name of a key
1801 * @returns: %TRUE if the key @name is writable
1803 * Finds out if a key can be written or not
1808 g_settings_is_writable (GSettings *settings,
1814 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1816 path = g_strconcat (settings->priv->path, name, NULL);
1817 writable = g_settings_backend_get_writable (settings->priv->backend, path);
1824 * g_settings_get_child:
1825 * @settings: a #GSettings object
1826 * @name: the name of the 'child' schema
1827 * @returns: a 'child' settings object
1829 * Creates a 'child' settings object which has a base path of
1830 * <replaceable>base-path</replaceable>/@name", where
1831 * <replaceable>base-path</replaceable> is the base path of @settings.
1833 * The schema for the child settings object must have been declared
1834 * in the schema of @settings using a <tag class="starttag">child</tag> element.
1839 g_settings_get_child (GSettings *settings,
1842 const gchar *child_schema;
1847 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1849 child_name = g_strconcat (name, "/", NULL);
1850 child_schema = g_settings_schema_get_string (settings->priv->schema,
1852 if (child_schema == NULL)
1853 g_error ("Schema '%s' has no child '%s'",
1854 settings->priv->schema_name, name);
1856 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1857 child = g_object_new (G_TYPE_SETTINGS,
1858 "schema", child_schema,
1861 g_free (child_path);
1862 g_free (child_name);
1870 GSettings *settings;
1873 GSettingsBindGetMapping get_mapping;
1874 GSettingsBindSetMapping set_mapping;
1876 GDestroyNotify destroy;
1878 guint writable_handler_id;
1879 guint property_handler_id;
1880 const GParamSpec *property;
1881 guint key_handler_id;
1885 /* prevent recursion */
1890 g_settings_binding_free (gpointer data)
1892 GSettingsBinding *binding = data;
1894 g_assert (!binding->running);
1896 if (binding->writable_handler_id)
1897 g_signal_handler_disconnect (binding->settings,
1898 binding->writable_handler_id);
1900 if (binding->key_handler_id)
1901 g_signal_handler_disconnect (binding->settings,
1902 binding->key_handler_id);
1904 if (g_signal_handler_is_connected (binding->object,
1905 binding->property_handler_id))
1906 g_signal_handler_disconnect (binding->object,
1907 binding->property_handler_id);
1909 g_variant_type_free (binding->type);
1910 g_object_unref (binding->settings);
1912 if (binding->destroy)
1913 binding->destroy (binding->user_data);
1915 g_slice_free (GSettingsBinding, binding);
1919 g_settings_binding_quark (const char *property)
1924 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1925 quark = g_quark_from_string (tmp);
1932 g_settings_binding_key_changed (GSettings *settings,
1936 GSettingsBinding *binding = user_data;
1937 GValue value = { 0, };
1940 g_assert (settings == binding->settings);
1941 g_assert (key == binding->key);
1943 if (binding->running)
1946 binding->running = TRUE;
1948 g_value_init (&value, binding->property->value_type);
1949 variant = g_settings_get_value (settings, key);
1950 if (binding->get_mapping (&value, variant, binding->user_data))
1951 g_object_set_property (binding->object,
1952 binding->property->name,
1954 g_variant_unref (variant);
1955 g_value_unset (&value);
1957 binding->running = FALSE;
1961 g_settings_binding_property_changed (GObject *object,
1962 const GParamSpec *pspec,
1965 GSettingsBinding *binding = user_data;
1966 GValue value = { 0, };
1969 g_assert (object == binding->object);
1970 g_assert (pspec == binding->property);
1972 if (binding->running)
1975 binding->running = TRUE;
1977 g_value_init (&value, pspec->value_type);
1978 g_object_get_property (object, pspec->name, &value);
1979 if ((variant = binding->set_mapping (&value, binding->type,
1980 binding->user_data)))
1982 g_settings_set_value (binding->settings,
1984 g_variant_ref_sink (variant));
1985 g_variant_unref (variant);
1987 g_value_unset (&value);
1989 binding->running = FALSE;
1994 * @settings: a #GSettings object
1995 * @key: the key to bind
1996 * @object: a #GObject
1997 * @property: the name of the property to bind
1998 * @flags: flags for the binding
2000 * Create a binding between the @key in the @settings object
2001 * and the property @property of @object.
2003 * The binding uses the default GIO mapping functions to map
2004 * between the settings and property values. These functions
2005 * handle booleans, numeric types and string types in a
2006 * straightforward way. Use g_settings_bind_with_mapping() if
2007 * you need a custom mapping, or map between types that are not
2008 * supported by the default mapping functions.
2010 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2011 * function also establishes a binding between the writability of
2012 * @key and the "sensitive" property of @object (if @object has
2013 * a boolean property by that name). See g_settings_bind_writable()
2014 * for more details about writable bindings.
2016 * Note that the lifecycle of the binding is tied to the object,
2017 * and that you can have only one binding per object property.
2018 * If you bind the same property twice on the same object, the second
2019 * binding overrides the first one.
2024 g_settings_bind (GSettings *settings,
2027 const gchar *property,
2028 GSettingsBindFlags flags)
2030 g_settings_bind_with_mapping (settings, key, object, property,
2031 flags, NULL, NULL, NULL, NULL);
2035 * g_settings_bind_with_mapping:
2036 * @settings: a #GSettings object
2037 * @key: the key to bind
2038 * @object: a #GObject
2039 * @property: the name of the property to bind
2040 * @flags: flags for the binding
2041 * @get_mapping: a function that gets called to convert values
2042 * from @settings to @object, or %NULL to use the default GIO mapping
2043 * @set_mapping: a function that gets called to convert values
2044 * from @object to @settings, or %NULL to use the default GIO mapping
2045 * @user_data: data that gets passed to @get_mapping and @set_mapping
2046 * @destroy: #GDestroyNotify function for @user_data
2048 * Create a binding between the @key in the @settings object
2049 * and the property @property of @object.
2051 * The binding uses the provided mapping functions to map between
2052 * settings and property values.
2054 * Note that the lifecycle of the binding is tied to the object,
2055 * and that you can have only one binding per object property.
2056 * If you bind the same property twice on the same object, the second
2057 * binding overrides the first one.
2062 g_settings_bind_with_mapping (GSettings *settings,
2065 const gchar *property,
2066 GSettingsBindFlags flags,
2067 GSettingsBindGetMapping get_mapping,
2068 GSettingsBindSetMapping set_mapping,
2070 GDestroyNotify destroy)
2072 GSettingsBinding *binding;
2073 GObjectClass *objectclass;
2074 gchar *detailed_signal;
2075 GQuark binding_quark;
2077 g_return_if_fail (G_IS_SETTINGS (settings));
2079 objectclass = G_OBJECT_GET_CLASS (object);
2081 binding = g_slice_new0 (GSettingsBinding);
2082 binding->settings = g_object_ref (settings);
2083 binding->object = object;
2084 binding->key = g_intern_string (key);
2085 binding->property = g_object_class_find_property (objectclass, property);
2086 binding->user_data = user_data;
2087 binding->destroy = destroy;
2088 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2089 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2091 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2092 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2094 if (binding->property == NULL)
2096 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2097 property, G_OBJECT_TYPE_NAME (object));
2101 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
2103 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
2104 property, G_OBJECT_TYPE_NAME (object));
2107 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
2109 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
2110 property, G_OBJECT_TYPE_NAME (object));
2118 iter = g_settings_schema_get_value (settings->priv->schema, key);
2119 value = g_variant_iter_next_value (iter);
2120 binding->type = g_variant_type_copy (g_variant_get_type (value));
2121 g_variant_iter_free (iter);
2122 g_variant_unref (value);
2125 if (binding->type == NULL)
2127 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
2128 key, settings->priv->schema_name);
2132 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2133 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2134 !g_settings_mapping_is_compatible (binding->property->value_type,
2137 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2138 "'%s' which is not compatible with type '%s' of key '%s' "
2139 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2140 g_type_name (binding->property->value_type),
2141 g_variant_type_dup_string (binding->type), key,
2142 settings->priv->schema_name);
2146 if ((flags & G_SETTINGS_BIND_SET) &&
2147 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2149 GParamSpec *sensitive;
2151 sensitive = g_object_class_find_property (objectclass, "sensitive");
2153 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2154 (sensitive->flags & G_PARAM_WRITABLE))
2155 g_settings_bind_writable (settings, binding->key,
2156 object, "sensitive", FALSE);
2159 if (flags & G_SETTINGS_BIND_SET)
2161 detailed_signal = g_strdup_printf ("notify::%s", property);
2162 binding->property_handler_id =
2163 g_signal_connect (object, detailed_signal,
2164 G_CALLBACK (g_settings_binding_property_changed),
2166 g_free (detailed_signal);
2168 if (~flags & G_SETTINGS_BIND_GET)
2169 g_settings_binding_property_changed (object,
2174 if (flags & G_SETTINGS_BIND_GET)
2176 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2178 detailed_signal = g_strdup_printf ("changed::%s", key);
2179 binding->key_handler_id =
2180 g_signal_connect (settings, detailed_signal,
2181 G_CALLBACK (g_settings_binding_key_changed),
2183 g_free (detailed_signal);
2186 g_settings_binding_key_changed (settings, binding->key, binding);
2189 binding_quark = g_settings_binding_quark (property);
2190 g_object_set_qdata_full (object, binding_quark,
2191 binding, g_settings_binding_free);
2194 /* Writability binding {{{1 */
2197 GSettings *settings;
2200 const gchar *property;
2203 } GSettingsWritableBinding;
2206 g_settings_writable_binding_free (gpointer data)
2208 GSettingsWritableBinding *binding = data;
2210 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2211 g_object_unref (binding->settings);
2212 g_slice_free (GSettingsWritableBinding, binding);
2216 g_settings_binding_writable_changed (GSettings *settings,
2220 GSettingsWritableBinding *binding = user_data;
2223 g_assert (settings == binding->settings);
2224 g_assert (key == binding->key);
2226 writable = g_settings_is_writable (settings, key);
2228 if (binding->inverted)
2229 writable = !writable;
2231 g_object_set (binding->object, binding->property, writable, NULL);
2235 * g_settings_bind_writable:
2236 * @settings: a #GSettings object
2237 * @key: the key to bind
2238 * @object: a #GObject
2239 * @property: the name of a boolean property to bind
2240 * @inverted: whether to 'invert' the value
2242 * Create a binding between the writability of @key in the
2243 * @settings object and the property @property of @object.
2244 * The property must be boolean; "sensitive" or "visible"
2245 * properties of widgets are the most likely candidates.
2247 * Writable bindings are always uni-directional; changes of the
2248 * writability of the setting will be propagated to the object
2249 * property, not the other way.
2251 * When the @inverted argument is %TRUE, the binding inverts the
2252 * value as it passes from the setting to the object, i.e. @property
2253 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2256 * Note that the lifecycle of the binding is tied to the object,
2257 * and that you can have only one binding per object property.
2258 * If you bind the same property twice on the same object, the second
2259 * binding overrides the first one.
2264 g_settings_bind_writable (GSettings *settings,
2267 const gchar *property,
2270 GSettingsWritableBinding *binding;
2271 gchar *detailed_signal;
2274 g_return_if_fail (G_IS_SETTINGS (settings));
2276 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2279 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2280 property, G_OBJECT_TYPE_NAME (object));
2283 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2285 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2286 property, G_OBJECT_TYPE_NAME (object));
2290 binding = g_slice_new (GSettingsWritableBinding);
2291 binding->settings = g_object_ref (settings);
2292 binding->object = object;
2293 binding->key = g_intern_string (key);
2294 binding->property = g_intern_string (property);
2295 binding->inverted = inverted;
2297 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2298 binding->handler_id =
2299 g_signal_connect (settings, detailed_signal,
2300 G_CALLBACK (g_settings_binding_writable_changed),
2302 g_free (detailed_signal);
2304 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2305 binding, g_settings_writable_binding_free);
2307 g_settings_binding_writable_changed (settings, binding->key, binding);
2311 * g_settings_unbind:
2312 * @object: the object
2313 * @property: the property whose binding is removed
2315 * Removes an existing binding for @property on @object.
2317 * Note that bindings are automatically removed when the
2318 * object is finalized, so it is rarely necessary to call this
2324 g_settings_unbind (gpointer object,
2325 const gchar *property)
2327 GQuark binding_quark;
2329 binding_quark = g_settings_binding_quark (property);
2330 g_object_set_qdata (object, binding_quark, NULL);
2334 #define __G_SETTINGS_C__
2335 #include "gioaliasdef.c"
2337 /* vim:set foldmethod=marker: */