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>
29 #include "gsettings.h"
31 #include "gdelayedsettingsbackend.h"
32 #include "gsettingsbackendinternal.h"
33 #include "gsettings-mapping.h"
34 #include "gsettingsschema-internal.h"
40 * @short_description: High-level API for application settings
42 * The #GSettings class provides a convenient API for storing and retrieving
43 * application settings.
45 * Reads and writes can be considered to be non-blocking. Reading
46 * settings with #GSettings is typically extremely fast: on
47 * approximately the same order of magnitude (but slower than) a
48 * #GHashTable lookup. Writing settings is also extremely fast in terms
49 * of time to return to your application, but can be extremely expensive
50 * for other threads and other processes. Many settings backends
51 * (including dconf) have lazy initialisation which means in the common
52 * case of the user using their computer without modifying any settings
53 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
54 * even need to be started in this case. For this reason, you should
55 * only ever modify #GSettings keys in response to explicit user action.
56 * Particular care should be paid to ensure that modifications are not
57 * made during startup -- for example, when setting the initial value
58 * of preferences widgets. The built-in g_settings_bind() functionality
59 * is careful not to write settings in response to notify signals as a
60 * result of modifications that it makes to widgets.
62 * When creating a GSettings instance, you have to specify a schema
63 * that describes the keys in your settings and their types and default
64 * values, as well as some other information.
66 * Normally, a schema has as fixed path that determines where the settings
67 * are stored in the conceptual global tree of settings. However, schemas
68 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
69 * useful e.g. when the schema describes an 'account', and you want to be
70 * able to store a arbitrary number of accounts.
72 * Unlike other configuration systems (like GConf), GSettings does not
73 * restrict keys to basic types like strings and numbers. GSettings stores
74 * values as #GVariant, and allows any #GVariantType for keys. Key names
75 * are restricted to lowercase characters, numbers and '-'. Furthermore,
76 * the names must begin with a lowercase character, must not end
77 * with a '-', and must not contain consecutive dashes.
79 * Similar to GConf, the default values in GSettings schemas can be
80 * localized, but the localized values are stored in gettext catalogs
81 * and looked up with the domain that is specified in the
82 * <tag class="attribute">gettext-domain</tag> attribute of the
83 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
84 * elements and the category that is specified in the l10n attribute of the
85 * <tag class="starttag">key</tag> element.
87 * GSettings uses schemas in a compact binary form that is created
88 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
89 * utility. The input is a schema description in an XML format that can be
90 * described by the following DTD:
91 * |[<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>]|
93 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
95 * At runtime, schemas are identified by their id (as specified
96 * in the <tag class="attribute">id</tag> attribute of the
97 * <tag class="starttag">schema</tag> element). The
98 * convention for schema ids is to use a dotted name, similar in
99 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
100 * if the settings are for a specific service that owns a D-Bus bus name,
101 * the D-Bus bus name and schema id should match. For schemas which deal
102 * with settings not associated with one named application, the id should
103 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
105 * In addition to #GVariant types, keys can have types that have enumerated
106 * types. These can be described by a <tag class="starttag">choice</tag>,
107 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
108 * <xref linkend="schema-enumerated"/>. The underlying type of
109 * such a key is string, but you can use g_settings_get_enum(),
110 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
111 * access the numeric values corresponding to the string value of enum
114 * <example id="schema-default-values"><title>Default values</title>
115 * <programlisting><![CDATA[
117 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
119 * <key name="greeting" type="s">
120 * <default l10n="messages">"Hello, earthlings"</default>
121 * <summary>A greeting</summary>
123 * Greeting of the invading martians
127 * <key name="box" type="(ii)">
128 * <default>(20,30)</default>
133 * ]]></programlisting></example>
135 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
136 * <programlisting><![CDATA[
139 * <enum id="org.gtk.Test.myenum">
140 * <value nick="first" value="1"/>
141 * <value nick="second" value="2"/>
144 * <flags id="org.gtk.Test.myflags">
145 * <value nick="flag1" value="1"/>
146 * <value nick="flag2" value="2"/>
147 * <value nick="flag3" value="4"/>
150 * <schema id="org.gtk.Test">
152 * <key name="key-with-range" type="i">
153 * <range min="1" max="100"/>
154 * <default>10</default>
157 * <key name="key-with-choices" type="s">
159 * <choice value='Elisabeth'/>
160 * <choice value='Annabeth'/>
161 * <choice value='Joe'/>
164 * <alias value='Anna' target='Annabeth'/>
165 * <alias value='Beth' target='Elisabeth'/>
167 * <default>'Joe'</default>
170 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
171 * <default>'first'</default>
174 * <key name='flags-key' flags='org.gtk.Test.myflags'>
175 * <default>["flag1",flag2"]</default>
179 * ]]></programlisting></example>
182 * <title>Vendor overrides</title>
184 * Default values are defined in the schemas that get installed by
185 * an application. Sometimes, it is necessary for a vendor or distributor
186 * to adjust these defaults. Since patching the XML source for the schema
187 * is inconvenient and error-prone,
188 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
189 * so-called 'vendor override' files. These are keyfiles in the same
190 * directory as the XML schema sources which can override default values.
191 * The schema id serves as the group name in the key file, and the values
192 * are expected in serialized GVariant form, as in the following example:
193 * <informalexample><programlisting>
197 * </programlisting></informalexample>
200 * glib-compile-schemas expects schema files to have the extension
201 * <filename>.gschema.override</filename>
206 * <title>Binding</title>
208 * A very convenient feature of GSettings lets you bind #GObject properties
209 * directly to settings, using g_settings_bind(). Once a GObject property
210 * has been bound to a setting, changes on either side are automatically
211 * propagated to the other side. GSettings handles details like
212 * mapping between GObject and GVariant types, and preventing infinite
216 * This makes it very easy to hook up a preferences dialog to the
217 * underlying settings. To make this even more convenient, GSettings
218 * looks for a boolean property with the name "sensitivity" and
219 * automatically binds it to the writability of the bound setting.
220 * If this 'magic' gets in the way, it can be suppressed with the
221 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
226 struct _GSettingsPrivate
228 /* where the signals go... */
229 GMainContext *main_context;
231 GSettingsBackend *backend;
232 GSettingsSchema *schema;
235 GDelayedSettingsBackend *delayed;
251 SIGNAL_WRITABLE_CHANGE_EVENT,
252 SIGNAL_WRITABLE_CHANGED,
258 static guint g_settings_signals[N_SIGNALS];
260 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
264 g_settings_real_change_event (GSettings *settings,
271 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
273 for (i = 0; i < n_keys; i++)
274 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
275 keys[i], g_quark_to_string (keys[i]));
281 g_settings_real_writable_change_event (GSettings *settings,
284 const GQuark *keys = &key;
289 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
291 for (i = 0; i < n_keys; i++)
292 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
293 keys[i], g_quark_to_string (keys[i]));
299 settings_backend_changed (GObject *target,
300 GSettingsBackend *backend,
304 GSettings *settings = G_SETTINGS (target);
305 gboolean ignore_this;
308 /* We used to assert here:
310 * settings->priv->backend == backend
312 * but it could be the case that a notification is queued for delivery
313 * while someone calls g_settings_delay() (which changes the backend).
315 * Since the delay backend would just pass that straight through
316 * anyway, it doesn't make sense to try to detect this case.
317 * Therefore, we just accept it.
320 for (i = 0; key[i] == settings->priv->path[i]; i++);
322 if (settings->priv->path[i] == '\0' &&
323 g_settings_schema_has_key (settings->priv->schema, key + i))
327 quark = g_quark_from_string (key + i);
328 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
329 0, &quark, 1, &ignore_this);
334 settings_backend_path_changed (GObject *target,
335 GSettingsBackend *backend,
339 GSettings *settings = G_SETTINGS (target);
340 gboolean ignore_this;
342 if (g_str_has_prefix (settings->priv->path, path))
343 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
344 0, NULL, 0, &ignore_this);
348 settings_backend_keys_changed (GObject *target,
349 GSettingsBackend *backend,
351 const gchar * const *items,
354 GSettings *settings = G_SETTINGS (target);
355 gboolean ignore_this;
358 for (i = 0; settings->priv->path[i] &&
359 settings->priv->path[i] == path[i]; i++);
366 for (j = 0; items[j]; j++)
368 const gchar *item = items[j];
371 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
373 if (settings->priv->path[i + k] == '\0' &&
374 g_settings_schema_has_key (settings->priv->schema, item + k))
375 quarks[l++] = g_quark_from_string (item + k);
377 /* "256 quarks ought to be enough for anybody!"
378 * If this bites you, I'm sorry. Please file a bug.
384 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
385 0, quarks, l, &ignore_this);
390 settings_backend_writable_changed (GObject *target,
391 GSettingsBackend *backend,
394 GSettings *settings = G_SETTINGS (target);
395 gboolean ignore_this;
398 for (i = 0; key[i] == settings->priv->path[i]; i++);
400 if (settings->priv->path[i] == '\0' &&
401 g_settings_schema_has_key (settings->priv->schema, key + i))
402 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
403 0, g_quark_from_string (key + i), &ignore_this);
407 settings_backend_path_writable_changed (GObject *target,
408 GSettingsBackend *backend,
411 GSettings *settings = G_SETTINGS (target);
412 gboolean ignore_this;
414 if (g_str_has_prefix (settings->priv->path, path))
415 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
416 0, (GQuark) 0, &ignore_this);
419 /* Properties, Construction, Destruction {{{1 */
421 g_settings_set_property (GObject *object,
426 GSettings *settings = G_SETTINGS (object);
432 GSettingsSchema *schema;
434 schema = g_value_dup_boxed (value);
436 /* we receive a set_property() call for "settings-schema" even
437 * if it was not specified (ie: with NULL value). ->schema
438 * could already be set at this point (ie: via "schema-id").
439 * check for NULL to avoid clobbering the existing value.
443 g_assert (settings->priv->schema == NULL);
444 settings->priv->schema = schema;
451 const gchar *schema_id;
453 schema_id = g_value_get_string (value);
455 /* we receive a set_property() call for both "schema" and
456 * "schema-id", even if they are not set. Hopefully only one of
459 if (schema_id != NULL)
461 GSettingsSchemaSource *default_source;
463 g_assert (settings->priv->schema == NULL);
464 default_source = g_settings_schema_source_get_default ();
466 if (default_source == NULL)
467 g_error ("No GSettings schemas are installed on the system");
469 settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
471 if (settings->priv->schema == NULL)
472 g_error ("Settings schema '%s' is not installed\n", schema_id);
478 settings->priv->path = g_value_dup_string (value);
482 settings->priv->backend = g_value_dup_object (value);
486 g_assert_not_reached ();
491 g_settings_get_property (GObject *object,
496 GSettings *settings = G_SETTINGS (object);
501 g_value_set_boxed (value, settings->priv->schema);
505 g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
509 g_value_set_object (value, settings->priv->backend);
513 g_value_set_string (value, settings->priv->path);
516 case PROP_HAS_UNAPPLIED:
517 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
520 case PROP_DELAY_APPLY:
521 g_value_set_boolean (value, settings->priv->delayed != NULL);
525 g_assert_not_reached ();
529 static const GSettingsListenerVTable listener_vtable = {
530 settings_backend_changed,
531 settings_backend_path_changed,
532 settings_backend_keys_changed,
533 settings_backend_writable_changed,
534 settings_backend_path_writable_changed
538 g_settings_constructed (GObject *object)
540 GSettings *settings = G_SETTINGS (object);
541 const gchar *schema_path;
543 schema_path = g_settings_schema_get_path (settings->priv->schema);
545 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
546 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
547 g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
549 if (settings->priv->path == NULL)
551 if (schema_path == NULL)
552 g_error ("attempting to create schema '%s' without a path",
553 g_settings_schema_get_id (settings->priv->schema));
555 settings->priv->path = g_strdup (schema_path);
558 if (settings->priv->backend == NULL)
559 settings->priv->backend = g_settings_backend_get_default ();
561 g_settings_backend_watch (settings->priv->backend,
562 &listener_vtable, G_OBJECT (settings),
563 settings->priv->main_context);
564 g_settings_backend_subscribe (settings->priv->backend,
565 settings->priv->path);
569 g_settings_finalize (GObject *object)
571 GSettings *settings = G_SETTINGS (object);
573 g_settings_backend_unsubscribe (settings->priv->backend,
574 settings->priv->path);
575 g_main_context_unref (settings->priv->main_context);
576 g_object_unref (settings->priv->backend);
577 g_settings_schema_unref (settings->priv->schema);
578 g_free (settings->priv->path);
580 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
584 g_settings_init (GSettings *settings)
586 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
590 settings->priv->main_context = g_main_context_ref_thread_default ();
594 g_settings_class_init (GSettingsClass *class)
596 GObjectClass *object_class = G_OBJECT_CLASS (class);
598 class->writable_change_event = g_settings_real_writable_change_event;
599 class->change_event = g_settings_real_change_event;
601 object_class->set_property = g_settings_set_property;
602 object_class->get_property = g_settings_get_property;
603 object_class->constructed = g_settings_constructed;
604 object_class->finalize = g_settings_finalize;
606 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
609 * GSettings::changed:
610 * @settings: the object on which the signal was emitted
611 * @key: the name of the key that changed
613 * The "changed" signal is emitted when a key has potentially changed.
614 * You should call one of the g_settings_get() calls to check the new
617 * This signal supports detailed connections. You can connect to the
618 * detailed signal "changed::x" in order to only receive callbacks
619 * when key "x" changes.
621 g_settings_signals[SIGNAL_CHANGED] =
622 g_signal_new ("changed", G_TYPE_SETTINGS,
623 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
624 G_STRUCT_OFFSET (GSettingsClass, changed),
625 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
626 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
629 * GSettings::change-event:
630 * @settings: the object on which the signal was emitted
631 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
632 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
633 * @n_keys: the length of the @keys array, or 0
634 * @returns: %TRUE to stop other handlers from being invoked for the
635 * event. FALSE to propagate the event further.
637 * The "change-event" signal is emitted once per change event that
638 * affects this settings object. You should connect to this signal
639 * only if you are interested in viewing groups of changes before they
640 * are split out into multiple emissions of the "changed" signal.
641 * For most use cases it is more appropriate to use the "changed" signal.
643 * In the event that the change event applies to one or more specified
644 * keys, @keys will be an array of #GQuark of length @n_keys. In the
645 * event that the change event applies to the #GSettings object as a
646 * whole (ie: potentially every key has been changed) then @keys will
647 * be %NULL and @n_keys will be 0.
649 * The default handler for this signal invokes the "changed" signal
650 * for each affected key. If any other connected handler returns
651 * %TRUE then this default functionality will be suppressed.
653 g_settings_signals[SIGNAL_CHANGE_EVENT] =
654 g_signal_new ("change-event", G_TYPE_SETTINGS,
656 G_STRUCT_OFFSET (GSettingsClass, change_event),
657 g_signal_accumulator_true_handled, NULL,
659 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
662 * GSettings::writable-changed:
663 * @settings: the object on which the signal was emitted
666 * The "writable-changed" signal is emitted when the writability of a
667 * key has potentially changed. You should call
668 * g_settings_is_writable() in order to determine the new status.
670 * This signal supports detailed connections. You can connect to the
671 * detailed signal "writable-changed::x" in order to only receive
672 * callbacks when the writability of "x" changes.
674 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
675 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
676 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
677 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
678 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
679 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
682 * GSettings::writable-change-event:
683 * @settings: the object on which the signal was emitted
684 * @key: the quark of the key, or 0
685 * @returns: %TRUE to stop other handlers from being invoked for the
686 * event. FALSE to propagate the event further.
688 * The "writable-change-event" signal is emitted once per writability
689 * change event that affects this settings object. You should connect
690 * to this signal if you are interested in viewing groups of changes
691 * before they are split out into multiple emissions of the
692 * "writable-changed" signal. For most use cases it is more
693 * appropriate to use the "writable-changed" signal.
695 * In the event that the writability change applies only to a single
696 * key, @key will be set to the #GQuark for that key. In the event
697 * that the writability change affects the entire settings object,
700 * The default handler for this signal invokes the "writable-changed"
701 * and "changed" signals for each affected key. This is done because
702 * changes in writability might also imply changes in value (if for
703 * example, a new mandatory setting is introduced). If any other
704 * connected handler returns %TRUE then this default functionality
705 * will be suppressed.
707 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
708 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
710 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
711 g_signal_accumulator_true_handled, NULL,
712 NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
717 * The name of the context that the settings are stored in.
719 g_object_class_install_property (object_class, PROP_BACKEND,
720 g_param_spec_object ("backend",
721 P_("GSettingsBackend"),
722 P_("The GSettingsBackend for this settings object"),
723 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
724 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
727 * GSettings:settings-schema:
729 * The #GSettingsSchema describing the types of keys for this
732 * Ideally, this property would be called 'schema'. #GSettingsSchema
733 * has only existed since version 2.32, however, and before then the
734 * 'schema' property was used to refer to the ID of the schema rather
735 * than the schema itself. Take care.
737 g_object_class_install_property (object_class, PROP_SCHEMA,
738 g_param_spec_boxed ("settings-schema",
740 P_("The GSettingsSchema for this settings object"),
741 G_TYPE_SETTINGS_SCHEMA,
742 G_PARAM_CONSTRUCT_ONLY |
743 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
748 * The name of the schema that describes the types of keys
749 * for this #GSettings object.
751 * The type of this property is *not* #GSettingsSchema.
752 * #GSettingsSchema has only existed since version 2.32 and
753 * unfortunately this name was used in previous versions to refer to
754 * the schema ID rather than the schema itself. Take care to use the
755 * 'settings-schema' property if you wish to pass in a
758 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
759 * version, this property may instead refer to a #GSettingsSchema.
761 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
762 g_param_spec_string ("schema",
764 P_("The name of the schema for this settings object"),
766 G_PARAM_CONSTRUCT_ONLY |
767 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
770 * GSettings:schema-id:
772 * The name of the schema that describes the types of keys
773 * for this #GSettings object.
775 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
776 g_param_spec_string ("schema-id",
778 P_("The name of the schema for this settings object"),
780 G_PARAM_CONSTRUCT_ONLY |
781 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
786 * The path within the backend where the settings are stored.
788 g_object_class_install_property (object_class, PROP_PATH,
789 g_param_spec_string ("path",
791 P_("The path within the backend where the settings are"),
793 G_PARAM_CONSTRUCT_ONLY |
794 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
797 * GSettings:has-unapplied:
799 * If this property is %TRUE, the #GSettings object has outstanding
800 * changes that will be applied when g_settings_apply() is called.
802 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
803 g_param_spec_boolean ("has-unapplied",
804 P_("Has unapplied changes"),
805 P_("TRUE if there are outstanding changes to apply()"),
807 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
810 * GSettings:delay-apply:
812 * Whether the #GSettings object is in 'delay-apply' mode. See
813 * g_settings_delay() for details.
817 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
818 g_param_spec_boolean ("delay-apply",
819 P_("Delay-apply mode"),
820 P_("Whether this settings object is in 'delay-apply' mode"),
822 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
825 /* Construction (new, new_with_path, etc.) {{{1 */
828 * @schema_id: the id of the schema
829 * @returns: a new #GSettings object
831 * Creates a new #GSettings object with the schema specified by
834 * Signals on the newly created #GSettings object will be dispatched
835 * via the thread-default #GMainContext in effect at the time of the
836 * call to g_settings_new(). The new #GSettings will hold a reference
837 * on the context. See g_main_context_push_thread_default().
842 g_settings_new (const gchar *schema_id)
844 g_return_val_if_fail (schema_id != NULL, NULL);
846 return g_object_new (G_TYPE_SETTINGS,
847 "schema-id", schema_id,
852 * g_settings_new_with_path:
853 * @schema_id: the id of the schema
854 * @path: the path to use
855 * @returns: a new #GSettings object
857 * Creates a new #GSettings object with the relocatable schema specified
858 * by @schema_id and a given path.
860 * You only need to do this if you want to directly create a settings
861 * object with a schema that doesn't have a specified path of its own.
864 * It is a programmer error to call this function for a schema that
865 * has an explicitly specified path.
870 g_settings_new_with_path (const gchar *schema_id,
873 g_return_val_if_fail (schema_id != NULL, NULL);
874 g_return_val_if_fail (path != NULL, NULL);
876 return g_object_new (G_TYPE_SETTINGS,
877 "schema-id", schema_id,
883 * g_settings_new_with_backend:
884 * @schema_id: the id of the schema
885 * @backend: the #GSettingsBackend to use
886 * @returns: a new #GSettings object
888 * Creates a new #GSettings object with the schema specified by
889 * @schema_id and a given #GSettingsBackend.
891 * Creating a #GSettings object with a different backend allows accessing
892 * settings from a database other than the usual one. For example, it may make
893 * sense to pass a backend corresponding to the "defaults" settings database on
894 * the system to get a settings object that modifies the system default
895 * settings instead of the settings for this user.
900 g_settings_new_with_backend (const gchar *schema_id,
901 GSettingsBackend *backend)
903 g_return_val_if_fail (schema_id != NULL, NULL);
904 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
906 return g_object_new (G_TYPE_SETTINGS,
907 "schema-id", schema_id,
913 * g_settings_new_with_backend_and_path:
914 * @schema_id: the id of the schema
915 * @backend: the #GSettingsBackend to use
916 * @path: the path to use
917 * @returns: a new #GSettings object
919 * Creates a new #GSettings object with the schema specified by
920 * @schema_id and a given #GSettingsBackend and path.
922 * This is a mix of g_settings_new_with_backend() and
923 * g_settings_new_with_path().
928 g_settings_new_with_backend_and_path (const gchar *schema_id,
929 GSettingsBackend *backend,
932 g_return_val_if_fail (schema_id != NULL, NULL);
933 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
934 g_return_val_if_fail (path != NULL, NULL);
936 return g_object_new (G_TYPE_SETTINGS,
937 "schema-id", schema_id,
944 * g_settings_new_full:
945 * @schema: a #GSettingsSchema
946 * @backend: a #GSettingsBackend
947 * @path: the path to use
948 * @returns: a new #GSettings object
950 * Creates a new #GSettings object with a given schema, backend and
953 * It should be extremely rare that you ever want to use this function.
954 * It is made available for advanced use-cases (such as plugin systems
955 * that want to provide access to schemas loaded from custom locations,
958 * At the most basic level, a #GSettings object is a pure composition of
959 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
960 * backend, and a #GMainContext to which signals are dispatched.
962 * This constructor therefore gives you full control over constructing
963 * #GSettings instances. The first 4 parameters are given directly as
964 * @schema, @backend and @path, and the main context is taken from the
965 * thread-default (as per g_settings_new()).
970 g_settings_new_full (GSettingsSchema *schema,
971 GSettingsBackend *backend,
974 return g_object_new (G_TYPE_SETTINGS,
975 "settings-schema", schema,
980 /* Internal read/write utilities {{{1 */
982 g_settings_write_to_backend (GSettings *settings,
983 GSettingsSchemaKey *key,
989 path = g_strconcat (settings->priv->path, key->name, NULL);
990 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
997 g_settings_read_from_backend (GSettings *settings,
998 GSettingsSchemaKey *key)
1004 path = g_strconcat (settings->priv->path, key->name, NULL);
1005 value = g_settings_backend_read (settings->priv->backend, path, key->type, FALSE);
1010 fixup = g_settings_schema_key_range_fixup (key, value);
1011 g_variant_unref (value);
1019 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1021 * g_settings_get_value:
1022 * @settings: a #GSettings object
1023 * @key: the key to get the value for
1024 * @returns: a new #GVariant
1026 * Gets the value that is stored in @settings for @key.
1028 * It is a programmer error to give a @key that isn't contained in the
1029 * schema for @settings.
1034 g_settings_get_value (GSettings *settings,
1037 GSettingsSchemaKey skey;
1040 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1041 g_return_val_if_fail (key != NULL, NULL);
1043 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1044 value = g_settings_read_from_backend (settings, &skey);
1047 value = g_settings_schema_key_get_translated_default (&skey);
1050 value = g_variant_ref (skey.default_value);
1052 g_settings_schema_key_clear (&skey);
1058 * g_settings_get_enum:
1059 * @settings: a #GSettings object
1060 * @key: the key to get the value for
1061 * @returns: the enum value
1063 * Gets the value that is stored in @settings for @key and converts it
1064 * to the enum value that it represents.
1066 * In order to use this function the type of the value must be a string
1067 * and it must be marked in the schema file as an enumerated type.
1069 * It is a programmer error to give a @key that isn't contained in the
1070 * schema for @settings or is not marked as an enumerated type.
1072 * If the value stored in the configuration database is not a valid
1073 * value for the enumerated type then this function will return the
1079 g_settings_get_enum (GSettings *settings,
1082 GSettingsSchemaKey skey;
1086 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1087 g_return_val_if_fail (key != NULL, -1);
1089 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1093 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1094 "associated with an enumerated type", skey.name);
1095 g_settings_schema_key_clear (&skey);
1099 value = g_settings_read_from_backend (settings, &skey);
1102 value = g_settings_schema_key_get_translated_default (&skey);
1105 value = g_variant_ref (skey.default_value);
1107 result = g_settings_schema_key_to_enum (&skey, value);
1108 g_settings_schema_key_clear (&skey);
1109 g_variant_unref (value);
1115 * g_settings_set_enum:
1116 * @settings: a #GSettings object
1117 * @key: a key, within @settings
1118 * @value: an enumerated value
1119 * @returns: %TRUE, if the set succeeds
1121 * Looks up the enumerated type nick for @value and writes it to @key,
1124 * It is a programmer error to give a @key that isn't contained in the
1125 * schema for @settings or is not marked as an enumerated type, or for
1126 * @value not to be a valid value for the named type.
1128 * After performing the write, accessing @key directly with
1129 * g_settings_get_string() will return the 'nick' associated with
1133 g_settings_set_enum (GSettings *settings,
1137 GSettingsSchemaKey skey;
1141 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1142 g_return_val_if_fail (key != NULL, FALSE);
1144 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1148 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1149 "associated with an enumerated type", skey.name);
1153 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1155 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1156 "in schema `%s'. Doing nothing.", value, skey.name,
1157 g_settings_schema_get_id (skey.schema));
1158 g_settings_schema_key_clear (&skey);
1162 success = g_settings_write_to_backend (settings, &skey, variant);
1163 g_settings_schema_key_clear (&skey);
1169 * g_settings_get_flags:
1170 * @settings: a #GSettings object
1171 * @key: the key to get the value for
1172 * @returns: the flags value
1174 * Gets the value that is stored in @settings for @key and converts it
1175 * to the flags value that it represents.
1177 * In order to use this function the type of the value must be an array
1178 * of strings and it must be marked in the schema file as an flags type.
1180 * It is a programmer error to give a @key that isn't contained in the
1181 * schema for @settings or is not marked as a flags type.
1183 * If the value stored in the configuration database is not a valid
1184 * value for the flags type then this function will return the default
1190 g_settings_get_flags (GSettings *settings,
1193 GSettingsSchemaKey skey;
1197 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1198 g_return_val_if_fail (key != NULL, -1);
1200 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1204 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1205 "associated with a flags type", skey.name);
1206 g_settings_schema_key_clear (&skey);
1210 value = g_settings_read_from_backend (settings, &skey);
1213 value = g_settings_schema_key_get_translated_default (&skey);
1216 value = g_variant_ref (skey.default_value);
1218 result = g_settings_schema_key_to_flags (&skey, value);
1219 g_settings_schema_key_clear (&skey);
1220 g_variant_unref (value);
1226 * g_settings_set_flags:
1227 * @settings: a #GSettings object
1228 * @key: a key, within @settings
1229 * @value: a flags value
1230 * @returns: %TRUE, if the set succeeds
1232 * Looks up the flags type nicks for the bits specified by @value, puts
1233 * them in an array of strings and writes the array to @key, within
1236 * It is a programmer error to give a @key that isn't contained in the
1237 * schema for @settings or is not marked as a flags type, or for @value
1238 * to contain any bits that are not value for the named type.
1240 * After performing the write, accessing @key directly with
1241 * g_settings_get_strv() will return an array of 'nicks'; one for each
1245 g_settings_set_flags (GSettings *settings,
1249 GSettingsSchemaKey skey;
1253 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1254 g_return_val_if_fail (key != NULL, FALSE);
1256 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1260 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1261 "associated with a flags type", skey.name);
1265 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1267 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1268 "for key `%s' in schema `%s'. Doing nothing.",
1269 value, skey.name, g_settings_schema_get_id (skey.schema));
1270 g_settings_schema_key_clear (&skey);
1274 success = g_settings_write_to_backend (settings, &skey, variant);
1275 g_settings_schema_key_clear (&skey);
1281 * g_settings_set_value:
1282 * @settings: a #GSettings object
1283 * @key: the name of the key to set
1284 * @value: a #GVariant of the correct type
1285 * @returns: %TRUE if setting the key succeeded,
1286 * %FALSE if the key was not writable
1288 * Sets @key in @settings to @value.
1290 * It is a programmer error to give a @key that isn't contained in the
1291 * schema for @settings or for @value to have the incorrect type, per
1294 * If @value is floating then this function consumes the reference.
1299 g_settings_set_value (GSettings *settings,
1303 GSettingsSchemaKey skey;
1305 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1306 g_return_val_if_fail (key != NULL, FALSE);
1308 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1310 if (!g_settings_schema_key_type_check (&skey, value))
1312 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1314 g_settings_schema_get_id (settings->priv->schema),
1315 g_variant_type_peek_string (skey.type),
1316 g_variant_get_type_string (value));
1321 if (!g_settings_schema_key_range_check (&skey, value))
1323 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1324 "is outside of valid range",
1326 g_settings_schema_get_id (settings->priv->schema));
1331 g_settings_schema_key_clear (&skey);
1333 return g_settings_write_to_backend (settings, &skey, value);
1338 * @settings: a #GSettings object
1339 * @key: the key to get the value for
1340 * @format: a #GVariant format string
1341 * @...: arguments as per @format
1343 * Gets the value that is stored at @key in @settings.
1345 * A convenience function that combines g_settings_get_value() with
1348 * It is a programmer error to give a @key that isn't contained in the
1349 * schema for @settings or for the #GVariantType of @format to mismatch
1350 * the type given in the schema.
1355 g_settings_get (GSettings *settings,
1357 const gchar *format,
1363 value = g_settings_get_value (settings, key);
1365 va_start (ap, format);
1366 g_variant_get_va (value, format, NULL, &ap);
1369 g_variant_unref (value);
1374 * @settings: a #GSettings object
1375 * @key: the name of the key to set
1376 * @format: a #GVariant format string
1377 * @...: arguments as per @format
1378 * @returns: %TRUE if setting the key succeeded,
1379 * %FALSE if the key was not writable
1381 * Sets @key in @settings to @value.
1383 * A convenience function that combines g_settings_set_value() with
1386 * It is a programmer error to give a @key that isn't contained in the
1387 * schema for @settings or for the #GVariantType of @format to mismatch
1388 * the type given in the schema.
1393 g_settings_set (GSettings *settings,
1395 const gchar *format,
1401 va_start (ap, format);
1402 value = g_variant_new_va (format, NULL, &ap);
1405 return g_settings_set_value (settings, key, value);
1409 * g_settings_get_mapped:
1410 * @settings: a #GSettings object
1411 * @key: the key to get the value for
1412 * @mapping: (scope call): the function to map the value in the
1413 * settings database to the value used by the application
1414 * @user_data: user data for @mapping
1415 * @returns: (transfer full): the result, which may be %NULL
1417 * Gets the value that is stored at @key in @settings, subject to
1418 * application-level validation/mapping.
1420 * You should use this function when the application needs to perform
1421 * some processing on the value of the key (for example, parsing). The
1422 * @mapping function performs that processing. If the function
1423 * indicates that the processing was unsuccessful (due to a parse error,
1424 * for example) then the mapping is tried again with another value.
1426 * This allows a robust 'fall back to defaults' behaviour to be
1427 * implemented somewhat automatically.
1429 * The first value that is tried is the user's setting for the key. If
1430 * the mapping function fails to map this value, other values may be
1431 * tried in an unspecified order (system or site defaults, translated
1432 * schema default values, untranslated schema default values, etc).
1434 * If the mapping function fails for all possible values, one additional
1435 * attempt is made: the mapping function is called with a %NULL value.
1436 * If the mapping function still indicates failure at this point then
1437 * the application will be aborted.
1439 * The result parameter for the @mapping function is pointed to a
1440 * #gpointer which is initially set to %NULL. The same pointer is given
1441 * to each invocation of @mapping. The final value of that #gpointer is
1442 * what is returned by this function. %NULL is valid; it is returned
1443 * just as any other value would be.
1446 g_settings_get_mapped (GSettings *settings,
1448 GSettingsGetMapping mapping,
1451 gpointer result = NULL;
1452 GSettingsSchemaKey skey;
1456 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1457 g_return_val_if_fail (key != NULL, NULL);
1458 g_return_val_if_fail (mapping != NULL, NULL);
1460 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1462 if ((value = g_settings_read_from_backend (settings, &skey)))
1464 okay = mapping (value, &result, user_data);
1465 g_variant_unref (value);
1466 if (okay) goto okay;
1469 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1471 okay = mapping (value, &result, user_data);
1472 g_variant_unref (value);
1473 if (okay) goto okay;
1476 if (mapping (skey.default_value, &result, user_data))
1479 if (!mapping (NULL, &result, user_data))
1480 g_error ("The mapping function given to g_settings_get_mapped() for key "
1481 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1482 key, g_settings_schema_get_id (settings->priv->schema));
1485 g_settings_schema_key_clear (&skey);
1490 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1492 * g_settings_get_string:
1493 * @settings: a #GSettings object
1494 * @key: the key to get the value for
1495 * @returns: a newly-allocated string
1497 * Gets the value that is stored at @key in @settings.
1499 * A convenience variant of g_settings_get() for strings.
1501 * It is a programmer error to give a @key that isn't specified as
1502 * having a string type in the schema for @settings.
1507 g_settings_get_string (GSettings *settings,
1513 value = g_settings_get_value (settings, key);
1514 result = g_variant_dup_string (value, NULL);
1515 g_variant_unref (value);
1521 * g_settings_set_string:
1522 * @settings: a #GSettings object
1523 * @key: the name of the key to set
1524 * @value: the value to set it to
1525 * @returns: %TRUE if setting the key succeeded,
1526 * %FALSE if the key was not writable
1528 * Sets @key in @settings to @value.
1530 * A convenience variant of g_settings_set() for strings.
1532 * It is a programmer error to give a @key that isn't specified as
1533 * having a string type in the schema for @settings.
1538 g_settings_set_string (GSettings *settings,
1542 return g_settings_set_value (settings, key, g_variant_new_string (value));
1546 * g_settings_get_int:
1547 * @settings: a #GSettings object
1548 * @key: the key to get the value for
1549 * @returns: an integer
1551 * Gets the value that is stored at @key in @settings.
1553 * A convenience variant of g_settings_get() for 32-bit integers.
1555 * It is a programmer error to give a @key that isn't specified as
1556 * having a int32 type in the schema for @settings.
1561 g_settings_get_int (GSettings *settings,
1567 value = g_settings_get_value (settings, key);
1568 result = g_variant_get_int32 (value);
1569 g_variant_unref (value);
1575 * g_settings_set_int:
1576 * @settings: a #GSettings object
1577 * @key: the name of the key to set
1578 * @value: the value to set it to
1579 * @returns: %TRUE if setting the key succeeded,
1580 * %FALSE if the key was not writable
1582 * Sets @key in @settings to @value.
1584 * A convenience variant of g_settings_set() for 32-bit integers.
1586 * It is a programmer error to give a @key that isn't specified as
1587 * having a int32 type in the schema for @settings.
1592 g_settings_set_int (GSettings *settings,
1596 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1600 * g_settings_get_uint:
1601 * @settings: a #GSettings object
1602 * @key: the key to get the value for
1603 * @returns: an unsigned integer
1605 * Gets the value that is stored at @key in @settings.
1607 * A convenience variant of g_settings_get() for 32-bit unsigned
1610 * It is a programmer error to give a @key that isn't specified as
1611 * having a uint32 type in the schema for @settings.
1616 g_settings_get_uint (GSettings *settings,
1622 value = g_settings_get_value (settings, key);
1623 result = g_variant_get_uint32 (value);
1624 g_variant_unref (value);
1630 * g_settings_set_uint:
1631 * @settings: a #GSettings object
1632 * @key: the name of the key to set
1633 * @value: the value to set it to
1634 * @returns: %TRUE if setting the key succeeded,
1635 * %FALSE if the key was not writable
1637 * Sets @key in @settings to @value.
1639 * A convenience variant of g_settings_set() for 32-bit unsigned
1642 * It is a programmer error to give a @key that isn't specified as
1643 * having a uint32 type in the schema for @settings.
1648 g_settings_set_uint (GSettings *settings,
1652 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1656 * g_settings_get_double:
1657 * @settings: a #GSettings object
1658 * @key: the key to get the value for
1659 * @returns: a double
1661 * Gets the value that is stored at @key in @settings.
1663 * A convenience variant of g_settings_get() for doubles.
1665 * It is a programmer error to give a @key that isn't specified as
1666 * having a 'double' type in the schema for @settings.
1671 g_settings_get_double (GSettings *settings,
1677 value = g_settings_get_value (settings, key);
1678 result = g_variant_get_double (value);
1679 g_variant_unref (value);
1685 * g_settings_set_double:
1686 * @settings: a #GSettings object
1687 * @key: the name of the key to set
1688 * @value: the value to set it to
1689 * @returns: %TRUE if setting the key succeeded,
1690 * %FALSE if the key was not writable
1692 * Sets @key in @settings to @value.
1694 * A convenience variant of g_settings_set() for doubles.
1696 * It is a programmer error to give a @key that isn't specified as
1697 * having a 'double' type in the schema for @settings.
1702 g_settings_set_double (GSettings *settings,
1706 return g_settings_set_value (settings, key, g_variant_new_double (value));
1710 * g_settings_get_boolean:
1711 * @settings: a #GSettings object
1712 * @key: the key to get the value for
1713 * @returns: a boolean
1715 * Gets the value that is stored at @key in @settings.
1717 * A convenience variant of g_settings_get() for booleans.
1719 * It is a programmer error to give a @key that isn't specified as
1720 * having a boolean type in the schema for @settings.
1725 g_settings_get_boolean (GSettings *settings,
1731 value = g_settings_get_value (settings, key);
1732 result = g_variant_get_boolean (value);
1733 g_variant_unref (value);
1739 * g_settings_set_boolean:
1740 * @settings: a #GSettings object
1741 * @key: the name of the key to set
1742 * @value: the value to set it to
1743 * @returns: %TRUE if setting the key succeeded,
1744 * %FALSE if the key was not writable
1746 * Sets @key in @settings to @value.
1748 * A convenience variant of g_settings_set() for booleans.
1750 * It is a programmer error to give a @key that isn't specified as
1751 * having a boolean type in the schema for @settings.
1756 g_settings_set_boolean (GSettings *settings,
1760 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1764 * g_settings_get_strv:
1765 * @settings: a #GSettings object
1766 * @key: the key to get the value for
1767 * @returns: (array zero-terminated=1) (transfer full): a
1768 * newly-allocated, %NULL-terminated array of strings, the value that
1769 * is stored at @key in @settings.
1771 * A convenience variant of g_settings_get() for string arrays.
1773 * It is a programmer error to give a @key that isn't specified as
1774 * having an array of strings type in the schema for @settings.
1779 g_settings_get_strv (GSettings *settings,
1785 value = g_settings_get_value (settings, key);
1786 result = g_variant_dup_strv (value, NULL);
1787 g_variant_unref (value);
1793 * g_settings_set_strv:
1794 * @settings: a #GSettings object
1795 * @key: the name of the key to set
1796 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1797 * @returns: %TRUE if setting the key succeeded,
1798 * %FALSE if the key was not writable
1800 * Sets @key in @settings to @value.
1802 * A convenience variant of g_settings_set() for string arrays. If
1803 * @value is %NULL, then @key is set to be the empty array.
1805 * It is a programmer error to give a @key that isn't specified as
1806 * having an array of strings type in the schema for @settings.
1811 g_settings_set_strv (GSettings *settings,
1813 const gchar * const *value)
1818 array = g_variant_new_strv (value, -1);
1820 array = g_variant_new_strv (NULL, 0);
1822 return g_settings_set_value (settings, key, array);
1825 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1828 * @settings: a #GSettings object
1830 * Changes the #GSettings object into 'delay-apply' mode. In this
1831 * mode, changes to @settings are not immediately propagated to the
1832 * backend, but kept locally until g_settings_apply() is called.
1837 g_settings_delay (GSettings *settings)
1839 g_return_if_fail (G_IS_SETTINGS (settings));
1841 if (settings->priv->delayed)
1844 settings->priv->delayed =
1845 g_delayed_settings_backend_new (settings->priv->backend,
1847 settings->priv->main_context);
1848 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1849 g_object_unref (settings->priv->backend);
1851 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1852 g_settings_backend_watch (settings->priv->backend,
1853 &listener_vtable, G_OBJECT (settings),
1854 settings->priv->main_context);
1856 g_object_notify (G_OBJECT (settings), "delay-apply");
1861 * @settings: a #GSettings instance
1863 * Applies any changes that have been made to the settings. This
1864 * function does nothing unless @settings is in 'delay-apply' mode;
1865 * see g_settings_delay(). In the normal case settings are always
1866 * applied immediately.
1869 g_settings_apply (GSettings *settings)
1871 if (settings->priv->delayed)
1873 GDelayedSettingsBackend *delayed;
1875 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1876 g_delayed_settings_backend_apply (delayed);
1881 * g_settings_revert:
1882 * @settings: a #GSettings instance
1884 * Reverts all non-applied changes to the settings. This function
1885 * does nothing unless @settings is in 'delay-apply' mode; see
1886 * g_settings_delay(). In the normal case settings are always applied
1889 * Change notifications will be emitted for affected keys.
1892 g_settings_revert (GSettings *settings)
1894 if (settings->priv->delayed)
1896 GDelayedSettingsBackend *delayed;
1898 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1899 g_delayed_settings_backend_revert (delayed);
1904 * g_settings_get_has_unapplied:
1905 * @settings: a #GSettings object
1906 * @returns: %TRUE if @settings has unapplied changes
1908 * Returns whether the #GSettings object has any unapplied
1909 * changes. This can only be the case if it is in 'delayed-apply' mode.
1914 g_settings_get_has_unapplied (GSettings *settings)
1916 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1918 return settings->priv->delayed &&
1919 g_delayed_settings_backend_get_has_unapplied (
1920 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1923 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
1926 * @settings: a #GSettings object
1927 * @key: the name of a key
1929 * Resets @key to its default value.
1931 * This call resets the key, as much as possible, to its default value.
1932 * That might the value specified in the schema or the one set by the
1936 g_settings_reset (GSettings *settings,
1941 path = g_strconcat (settings->priv->path, key, NULL);
1942 g_settings_backend_reset (settings->priv->backend, path, NULL);
1949 * Ensures that all pending operations for the given are complete for
1950 * the default backend.
1952 * Writes made to a #GSettings are handled asynchronously. For this
1953 * reason, it is very unlikely that the changes have it to disk by the
1954 * time g_settings_set() returns.
1956 * This call will block until all of the writes have made it to the
1957 * backend. Since the mainloop is not running, no change notifications
1958 * will be dispatched during this call (but some may be queued by the
1959 * time the call is done).
1962 g_settings_sync (void)
1964 g_settings_backend_sync_default ();
1968 * g_settings_is_writable:
1969 * @settings: a #GSettings object
1970 * @name: the name of a key
1971 * @returns: %TRUE if the key @name is writable
1973 * Finds out if a key can be written or not
1978 g_settings_is_writable (GSettings *settings,
1984 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1986 path = g_strconcat (settings->priv->path, name, NULL);
1987 writable = g_settings_backend_get_writable (settings->priv->backend, path);
1994 * g_settings_get_child:
1995 * @settings: a #GSettings object
1996 * @name: the name of the 'child' schema
1997 * @returns: (transfer full): a 'child' settings object
1999 * Creates a 'child' settings object which has a base path of
2000 * <replaceable>base-path</replaceable>/@name, where
2001 * <replaceable>base-path</replaceable> is the base path of @settings.
2003 * The schema for the child settings object must have been declared
2004 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2009 g_settings_get_child (GSettings *settings,
2012 const gchar *child_schema;
2017 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2019 child_name = g_strconcat (name, "/", NULL);
2020 child_schema = g_settings_schema_get_string (settings->priv->schema,
2022 if (child_schema == NULL)
2023 g_error ("Schema '%s' has no child '%s'",
2024 g_settings_schema_get_id (settings->priv->schema), name);
2026 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2027 child = g_object_new (G_TYPE_SETTINGS,
2028 "schema-id", child_schema,
2031 g_free (child_path);
2032 g_free (child_name);
2038 * g_settings_list_keys:
2039 * @settings: a #GSettings object
2040 * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2042 * Introspects the list of keys on @settings.
2044 * You should probably not be calling this function from "normal" code
2045 * (since you should already know what keys are in your schema). This
2046 * function is intended for introspection reasons.
2048 * You should free the return value with g_strfreev() when you are done
2052 g_settings_list_keys (GSettings *settings)
2059 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2060 strv = g_new (gchar *, n_keys + 1);
2061 for (i = j = 0; i < n_keys; i++)
2063 const gchar *key = g_quark_to_string (keys[i]);
2065 if (!g_str_has_suffix (key, "/"))
2066 strv[j++] = g_strdup (key);
2074 * g_settings_list_children:
2075 * @settings: a #GSettings object
2076 * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2078 * Gets the list of children on @settings.
2080 * The list is exactly the list of strings for which it is not an error
2081 * to call g_settings_get_child().
2083 * For GSettings objects that are lists, this value can change at any
2084 * time and you should connect to the "children-changed" signal to watch
2085 * for those changes. Note that there is a race condition here: you may
2086 * request a child after listing it only for it to have been destroyed
2087 * in the meantime. For this reason, g_settings_get_child() may return
2088 * %NULL even for a child that was listed by this function.
2090 * For GSettings objects that are not lists, you should probably not be
2091 * calling this function from "normal" code (since you should already
2092 * know what children are in your schema). This function may still be
2093 * useful there for introspection reasons, however.
2095 * You should free the return value with g_strfreev() when you are done
2099 g_settings_list_children (GSettings *settings)
2106 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2107 strv = g_new (gchar *, n_keys + 1);
2108 for (i = j = 0; i < n_keys; i++)
2110 const gchar *key = g_quark_to_string (keys[i]);
2112 if (g_str_has_suffix (key, "/"))
2114 gint length = strlen (key);
2116 strv[j] = g_memdup (key, length);
2117 strv[j][length - 1] = '\0';
2127 * g_settings_get_range:
2128 * @settings: a #GSettings
2129 * @key: the key to query the range of
2130 * @returns: a #GVariant describing the range
2132 * Queries the range of a key.
2134 * This function will return a #GVariant that fully describes the range
2135 * of values that are valid for @key.
2137 * The type of #GVariant returned is <literal>(sv)</literal>. The
2138 * string describes the type of range restriction in effect. The type
2139 * and meaning of the value contained in the variant depends on the
2142 * If the string is <literal>'type'</literal> then the variant contains
2143 * an empty array. The element type of that empty array is the expected
2144 * type of value and all values of that type are valid.
2146 * If the string is <literal>'enum'</literal> then the variant contains
2147 * an array enumerating the possible values. Each item in the array is
2148 * a possible valid value and no other values are valid.
2150 * If the string is <literal>'flags'</literal> then the variant contains
2151 * an array. Each item in the array is a value that may appear zero or
2152 * one times in an array to be used as the value for this key. For
2153 * example, if the variant contained the array <literal>['x',
2154 * 'y']</literal> then the valid values for the key would be
2155 * <literal>[]</literal>, <literal>['x']</literal>,
2156 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2157 * <literal>['y', 'x']</literal>.
2159 * Finally, if the string is <literal>'range'</literal> then the variant
2160 * contains a pair of like-typed values -- the minimum and maximum
2161 * permissible values for this key.
2163 * This information should not be used by normal programs. It is
2164 * considered to be a hint for introspection purposes. Normal programs
2165 * should already know what is permitted by their own schema. The
2166 * format may change in any way in the future -- but particularly, new
2167 * forms may be added to the possibilities described above.
2169 * It is a programmer error to give a @key that isn't contained in the
2170 * schema for @settings.
2172 * You should free the returned value with g_variant_unref() when it is
2178 g_settings_get_range (GSettings *settings,
2181 GSettingsSchemaKey skey;
2185 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2189 range = g_variant_new ("(**)", skey.minimum, skey.maximum);
2192 else if (skey.strinfo)
2194 range = strinfo_enumerate (skey.strinfo, skey.strinfo_length);
2195 type = skey.is_flags ? "flags" : "enum";
2199 range = g_variant_new_array (skey.type, NULL, 0);
2203 g_settings_schema_key_clear (&skey);
2205 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2209 * g_settings_range_check:
2210 * @settings: a #GSettings
2211 * @key: the key to check
2212 * @value: the value to check
2213 * @returns: %TRUE if @value is valid for @key
2215 * Checks if the given @value is of the correct type and within the
2216 * permitted range for @key.
2218 * This API is not intended to be used by normal programs -- they should
2219 * already know what is permitted by their own schemas. This API is
2220 * meant to be used by programs such as editors or commandline tools.
2222 * It is a programmer error to give a @key that isn't contained in the
2223 * schema for @settings.
2228 g_settings_range_check (GSettings *settings,
2232 GSettingsSchemaKey skey;
2235 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2236 good = g_settings_schema_key_type_check (&skey, value) &&
2237 g_settings_schema_key_range_check (&skey, value);
2238 g_settings_schema_key_clear (&skey);
2246 GSettingsSchemaKey key;
2247 GSettings *settings;
2250 GSettingsBindGetMapping get_mapping;
2251 GSettingsBindSetMapping set_mapping;
2253 GDestroyNotify destroy;
2255 guint writable_handler_id;
2256 guint property_handler_id;
2257 const GParamSpec *property;
2258 guint key_handler_id;
2260 /* prevent recursion */
2265 g_settings_binding_free (gpointer data)
2267 GSettingsBinding *binding = data;
2269 g_assert (!binding->running);
2271 if (binding->writable_handler_id)
2272 g_signal_handler_disconnect (binding->settings,
2273 binding->writable_handler_id);
2275 if (binding->key_handler_id)
2276 g_signal_handler_disconnect (binding->settings,
2277 binding->key_handler_id);
2279 if (g_signal_handler_is_connected (binding->object,
2280 binding->property_handler_id))
2281 g_signal_handler_disconnect (binding->object,
2282 binding->property_handler_id);
2284 g_settings_schema_key_clear (&binding->key);
2286 if (binding->destroy)
2287 binding->destroy (binding->user_data);
2289 g_object_unref (binding->settings);
2291 g_slice_free (GSettingsBinding, binding);
2295 g_settings_binding_quark (const char *property)
2300 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2301 quark = g_quark_from_string (tmp);
2308 g_settings_binding_key_changed (GSettings *settings,
2312 GSettingsBinding *binding = user_data;
2313 GValue value = G_VALUE_INIT;
2316 g_assert (settings == binding->settings);
2317 g_assert (key == binding->key.name);
2319 if (binding->running)
2322 binding->running = TRUE;
2324 g_value_init (&value, binding->property->value_type);
2326 variant = g_settings_read_from_backend (binding->settings, &binding->key);
2327 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2329 /* silently ignore errors in the user's config database */
2330 g_variant_unref (variant);
2334 if (variant == NULL)
2336 variant = g_settings_schema_key_get_translated_default (&binding->key);
2338 !binding->get_mapping (&value, variant, binding->user_data))
2340 /* flag translation errors with a warning */
2341 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2342 "was rejected by the binding mapping function",
2343 binding->key.unparsed, binding->key.name,
2344 g_settings_schema_get_id (binding->key.schema));
2345 g_variant_unref (variant);
2350 if (variant == NULL)
2352 variant = g_variant_ref (binding->key.default_value);
2353 if (!binding->get_mapping (&value, variant, binding->user_data))
2354 g_error ("The schema default value for key `%s' in schema `%s' "
2355 "was rejected by the binding mapping function.",
2356 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2359 g_object_set_property (binding->object, binding->property->name, &value);
2360 g_variant_unref (variant);
2361 g_value_unset (&value);
2363 binding->running = FALSE;
2367 g_settings_binding_property_changed (GObject *object,
2368 const GParamSpec *pspec,
2371 GSettingsBinding *binding = user_data;
2372 GValue value = G_VALUE_INIT;
2375 g_assert (object == binding->object);
2376 g_assert (pspec == binding->property);
2378 if (binding->running)
2381 binding->running = TRUE;
2383 g_value_init (&value, pspec->value_type);
2384 g_object_get_property (object, pspec->name, &value);
2385 if ((variant = binding->set_mapping (&value, binding->key.type,
2386 binding->user_data)))
2388 g_variant_take_ref (variant);
2390 if (!g_settings_schema_key_type_check (&binding->key, variant))
2392 g_critical ("binding mapping function for key `%s' returned "
2393 "GVariant of type `%s' when type `%s' was requested",
2394 binding->key.name, g_variant_get_type_string (variant),
2395 g_variant_type_dup_string (binding->key.type));
2399 if (!g_settings_schema_key_range_check (&binding->key, variant))
2401 g_critical ("GObject property `%s' on a `%s' object is out of "
2402 "schema-specified range for key `%s' of `%s': %s",
2403 binding->property->name, g_type_name (binding->property->owner_type),
2404 binding->key.name, g_settings_schema_get_id (binding->key.schema),
2405 g_variant_print (variant, TRUE));
2409 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2410 g_variant_unref (variant);
2412 g_value_unset (&value);
2414 binding->running = FALSE;
2418 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2422 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2427 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2428 const GVariantType *expected_type,
2431 return g_variant_new_boolean (!g_value_get_boolean (value));
2436 * @settings: a #GSettings object
2437 * @key: the key to bind
2438 * @object: (type GObject.Object): a #GObject
2439 * @property: the name of the property to bind
2440 * @flags: flags for the binding
2442 * Create a binding between the @key in the @settings object
2443 * and the property @property of @object.
2445 * The binding uses the default GIO mapping functions to map
2446 * between the settings and property values. These functions
2447 * handle booleans, numeric types and string types in a
2448 * straightforward way. Use g_settings_bind_with_mapping() if
2449 * you need a custom mapping, or map between types that are not
2450 * supported by the default mapping functions.
2452 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2453 * function also establishes a binding between the writability of
2454 * @key and the "sensitive" property of @object (if @object has
2455 * a boolean property by that name). See g_settings_bind_writable()
2456 * for more details about writable bindings.
2458 * Note that the lifecycle of the binding is tied to the object,
2459 * and that you can have only one binding per object property.
2460 * If you bind the same property twice on the same object, the second
2461 * binding overrides the first one.
2466 g_settings_bind (GSettings *settings,
2469 const gchar *property,
2470 GSettingsBindFlags flags)
2472 GSettingsBindGetMapping get_mapping = NULL;
2473 GSettingsBindSetMapping set_mapping = NULL;
2475 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2477 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2478 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2480 /* can't pass this flag to g_settings_bind_with_mapping() */
2481 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2484 g_settings_bind_with_mapping (settings, key, object, property, flags,
2485 get_mapping, set_mapping, NULL, NULL);
2489 * g_settings_bind_with_mapping: (skip)
2490 * @settings: a #GSettings object
2491 * @key: the key to bind
2492 * @object: (type GObject.Object): a #GObject
2493 * @property: the name of the property to bind
2494 * @flags: flags for the binding
2495 * @get_mapping: a function that gets called to convert values
2496 * from @settings to @object, or %NULL to use the default GIO mapping
2497 * @set_mapping: a function that gets called to convert values
2498 * from @object to @settings, or %NULL to use the default GIO mapping
2499 * @user_data: data that gets passed to @get_mapping and @set_mapping
2500 * @destroy: #GDestroyNotify function for @user_data
2502 * Create a binding between the @key in the @settings object
2503 * and the property @property of @object.
2505 * The binding uses the provided mapping functions to map between
2506 * settings and property values.
2508 * Note that the lifecycle of the binding is tied to the object,
2509 * and that you can have only one binding per object property.
2510 * If you bind the same property twice on the same object, the second
2511 * binding overrides the first one.
2516 g_settings_bind_with_mapping (GSettings *settings,
2519 const gchar *property,
2520 GSettingsBindFlags flags,
2521 GSettingsBindGetMapping get_mapping,
2522 GSettingsBindSetMapping set_mapping,
2524 GDestroyNotify destroy)
2526 GSettingsBinding *binding;
2527 GObjectClass *objectclass;
2528 gchar *detailed_signal;
2529 GQuark binding_quark;
2531 g_return_if_fail (G_IS_SETTINGS (settings));
2532 g_return_if_fail (key != NULL);
2533 g_return_if_fail (G_IS_OBJECT (object));
2534 g_return_if_fail (property != NULL);
2535 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2537 objectclass = G_OBJECT_GET_CLASS (object);
2539 binding = g_slice_new0 (GSettingsBinding);
2540 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2541 binding->settings = g_object_ref (settings);
2542 binding->object = object;
2543 binding->property = g_object_class_find_property (objectclass, property);
2544 binding->user_data = user_data;
2545 binding->destroy = destroy;
2546 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2547 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2549 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2550 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2552 if (binding->property == NULL)
2554 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2555 property, G_OBJECT_TYPE_NAME (object));
2559 if ((flags & G_SETTINGS_BIND_GET) &&
2560 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2562 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2563 "writable", property, G_OBJECT_TYPE_NAME (object));
2566 if ((flags & G_SETTINGS_BIND_SET) &&
2567 (binding->property->flags & G_PARAM_READABLE) == 0)
2569 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2570 "readable", property, G_OBJECT_TYPE_NAME (object));
2574 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2576 /* g_settings_bind_invert_boolean_get_mapping() is a private
2577 * function, so if we are here it means that g_settings_bind() was
2578 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2580 * Ensure that both sides are boolean.
2583 if (binding->property->value_type != G_TYPE_BOOLEAN)
2585 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2586 "was specified, but property `%s' on type `%s' has "
2587 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2588 g_type_name ((binding->property->value_type)));
2592 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2594 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2595 "was specified, but key `%s' on schema `%s' has "
2596 "type `%s'", key, g_settings_schema_get_id (settings->priv->schema),
2597 g_variant_type_dup_string (binding->key.type));
2603 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2604 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2605 !g_settings_mapping_is_compatible (binding->property->value_type,
2608 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2609 "'%s' which is not compatible with type '%s' of key '%s' "
2610 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2611 g_type_name (binding->property->value_type),
2612 g_variant_type_dup_string (binding->key.type), key,
2613 g_settings_schema_get_id (settings->priv->schema));
2617 if ((flags & G_SETTINGS_BIND_SET) &&
2618 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2620 GParamSpec *sensitive;
2622 sensitive = g_object_class_find_property (objectclass, "sensitive");
2624 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2625 (sensitive->flags & G_PARAM_WRITABLE))
2626 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2629 if (flags & G_SETTINGS_BIND_SET)
2631 detailed_signal = g_strdup_printf ("notify::%s", property);
2632 binding->property_handler_id =
2633 g_signal_connect (object, detailed_signal,
2634 G_CALLBACK (g_settings_binding_property_changed),
2636 g_free (detailed_signal);
2638 if (~flags & G_SETTINGS_BIND_GET)
2639 g_settings_binding_property_changed (object,
2644 if (flags & G_SETTINGS_BIND_GET)
2646 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2648 detailed_signal = g_strdup_printf ("changed::%s", key);
2649 binding->key_handler_id =
2650 g_signal_connect (settings, detailed_signal,
2651 G_CALLBACK (g_settings_binding_key_changed),
2653 g_free (detailed_signal);
2656 g_settings_binding_key_changed (settings, binding->key.name, binding);
2659 binding_quark = g_settings_binding_quark (property);
2660 g_object_set_qdata_full (object, binding_quark,
2661 binding, g_settings_binding_free);
2664 /* Writability binding {{{1 */
2667 GSettings *settings;
2670 const gchar *property;
2673 } GSettingsWritableBinding;
2676 g_settings_writable_binding_free (gpointer data)
2678 GSettingsWritableBinding *binding = data;
2680 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2681 g_object_unref (binding->settings);
2682 g_slice_free (GSettingsWritableBinding, binding);
2686 g_settings_binding_writable_changed (GSettings *settings,
2690 GSettingsWritableBinding *binding = user_data;
2693 g_assert (settings == binding->settings);
2694 g_assert (key == binding->key);
2696 writable = g_settings_is_writable (settings, key);
2698 if (binding->inverted)
2699 writable = !writable;
2701 g_object_set (binding->object, binding->property, writable, NULL);
2705 * g_settings_bind_writable:
2706 * @settings: a #GSettings object
2707 * @key: the key to bind
2708 * @object: (type GObject.Object):a #GObject
2709 * @property: the name of a boolean property to bind
2710 * @inverted: whether to 'invert' the value
2712 * Create a binding between the writability of @key in the
2713 * @settings object and the property @property of @object.
2714 * The property must be boolean; "sensitive" or "visible"
2715 * properties of widgets are the most likely candidates.
2717 * Writable bindings are always uni-directional; changes of the
2718 * writability of the setting will be propagated to the object
2719 * property, not the other way.
2721 * When the @inverted argument is %TRUE, the binding inverts the
2722 * value as it passes from the setting to the object, i.e. @property
2723 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2726 * Note that the lifecycle of the binding is tied to the object,
2727 * and that you can have only one binding per object property.
2728 * If you bind the same property twice on the same object, the second
2729 * binding overrides the first one.
2734 g_settings_bind_writable (GSettings *settings,
2737 const gchar *property,
2740 GSettingsWritableBinding *binding;
2741 gchar *detailed_signal;
2744 g_return_if_fail (G_IS_SETTINGS (settings));
2746 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2749 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2750 property, G_OBJECT_TYPE_NAME (object));
2753 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2755 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2756 property, G_OBJECT_TYPE_NAME (object));
2760 binding = g_slice_new (GSettingsWritableBinding);
2761 binding->settings = g_object_ref (settings);
2762 binding->object = object;
2763 binding->key = g_intern_string (key);
2764 binding->property = g_intern_string (property);
2765 binding->inverted = inverted;
2767 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2768 binding->handler_id =
2769 g_signal_connect (settings, detailed_signal,
2770 G_CALLBACK (g_settings_binding_writable_changed),
2772 g_free (detailed_signal);
2774 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2775 binding, g_settings_writable_binding_free);
2777 g_settings_binding_writable_changed (settings, binding->key, binding);
2781 * g_settings_unbind:
2782 * @object: the object
2783 * @property: the property whose binding is removed
2785 * Removes an existing binding for @property on @object.
2787 * Note that bindings are automatically removed when the
2788 * object is finalized, so it is rarely necessary to call this
2794 g_settings_unbind (gpointer object,
2795 const gchar *property)
2797 GQuark binding_quark;
2799 binding_quark = g_settings_binding_quark (property);
2800 g_object_set_qdata (object, binding_quark, NULL);
2805 /* vim:set foldmethod=marker: */