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"
43 * @short_description: High-level API for application settings
45 * The #GSettings class provides a convenient API for storing and retrieving
46 * application settings.
48 * Reads and writes can be considered to be non-blocking. Reading
49 * settings with #GSettings is typically extremely fast: on
50 * approximately the same order of magnitude (but slower than) a
51 * #GHashTable lookup. Writing settings is also extremely fast in terms
52 * of time to return to your application, but can be extremely expensive
53 * for other threads and other processes. Many settings backends
54 * (including dconf) have lazy initialisation which means in the common
55 * case of the user using their computer without modifying any settings
56 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
57 * even need to be started in this case. For this reason, you should
58 * only ever modify #GSettings keys in response to explicit user action.
59 * Particular care should be paid to ensure that modifications are not
60 * made during startup -- for example, when setting the initial value
61 * of preferences widgets. The built-in g_settings_bind() functionality
62 * is careful not to write settings in response to notify signals as a
63 * result of modifications that it makes to widgets.
65 * When creating a GSettings instance, you have to specify a schema
66 * that describes the keys in your settings and their types and default
67 * values, as well as some other information.
69 * Normally, a schema has as fixed path that determines where the settings
70 * are stored in the conceptual global tree of settings. However, schemas
71 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
72 * useful e.g. when the schema describes an 'account', and you want to be
73 * able to store a arbitrary number of accounts.
75 * Unlike other configuration systems (like GConf), GSettings does not
76 * restrict keys to basic types like strings and numbers. GSettings stores
77 * values as #GVariant, and allows any #GVariantType for keys. Key names
78 * are restricted to lowercase characters, numbers and '-'. Furthermore,
79 * the names must begin with a lowercase character, must not end
80 * with a '-', and must not contain consecutive dashes.
82 * Similar to GConf, the default values in GSettings schemas can be
83 * localized, but the localized values are stored in gettext catalogs
84 * and looked up with the domain that is specified in the
85 * <tag class="attribute">gettext-domain</tag> attribute of the
86 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
87 * elements and the category that is specified in the l10n attribute of the
88 * <tag class="starttag">key</tag> element.
90 * GSettings uses schemas in a compact binary form that is created
91 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
92 * utility. The input is a schema description in an XML format that can be
93 * described by the following DTD:
94 * |[<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>]|
96 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
98 * At runtime, schemas are identified by their id (as specified
99 * in the <tag class="attribute">id</tag> attribute of the
100 * <tag class="starttag">schema</tag> element). The
101 * convention for schema ids is to use a dotted name, similar in
102 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
103 * if the settings are for a specific service that owns a D-Bus bus name,
104 * the D-Bus bus name and schema id should match. For schemas which deal
105 * with settings not associated with one named application, the id should
106 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
108 * In addition to #GVariant types, keys can have types that have enumerated
109 * types. These can be described by a <tag class="starttag">choice</tag>,
110 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
111 * <xref linkend="schema-enumerated"/>. The underlying type of
112 * such a key is string, but you can use g_settings_get_enum(),
113 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
114 * access the numeric values corresponding to the string value of enum
117 * <example id="schema-default-values"><title>Default values</title>
118 * <programlisting><![CDATA[
120 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
122 * <key name="greeting" type="s">
123 * <default l10n="messages">"Hello, earthlings"</default>
124 * <summary>A greeting</summary>
126 * Greeting of the invading martians
130 * <key name="box" type="(ii)">
131 * <default>(20,30)</default>
136 * ]]></programlisting></example>
138 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
139 * <programlisting><![CDATA[
142 * <enum id="org.gtk.Test.myenum">
143 * <value nick="first" value="1"/>
144 * <value nick="second" value="2"/>
147 * <flags id="org.gtk.Test.myflags">
148 * <value nick="flag1" value="1"/>
149 * <value nick="flag2" value="2"/>
150 * <value nick="flag3" value="4"/>
153 * <schema id="org.gtk.Test">
155 * <key name="key-with-range" type="i">
156 * <range min="1" max="100"/>
157 * <default>10</default>
160 * <key name="key-with-choices" type="s">
162 * <choice value='Elisabeth'/>
163 * <choice value='Annabeth'/>
164 * <choice value='Joe'/>
167 * <alias value='Anna' target='Annabeth'/>
168 * <alias value='Beth' target='Elisabeth'/>
170 * <default>'Joe'</default>
173 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
174 * <default>'first'</default>
177 * <key name='flags-key' flags='org.gtk.Test.myflags'>
178 * <default>["flag1",flag2"]</default>
182 * ]]></programlisting></example>
185 * <title>Vendor overrides</title>
187 * Default values are defined in the schemas that get installed by
188 * an application. Sometimes, it is necessary for a vendor or distributor
189 * to adjust these defaults. Since patching the XML source for the schema
190 * is inconvenient and error-prone,
191 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
192 * so-called 'vendor override' files. These are keyfiles in the same
193 * directory as the XML schema sources which can override default values.
194 * The schema id serves as the group name in the key file, and the values
195 * are expected in serialized GVariant form, as in the following example:
196 * <informalexample><programlisting>
200 * </programlisting></informalexample>
203 * glib-compile-schemas expects schema files to have the extension
204 * <filename>.gschema.override</filename>
209 * <title>Binding</title>
211 * A very convenient feature of GSettings lets you bind #GObject properties
212 * directly to settings, using g_settings_bind(). Once a GObject property
213 * has been bound to a setting, changes on either side are automatically
214 * propagated to the other side. GSettings handles details like
215 * mapping between GObject and GVariant types, and preventing infinite
219 * This makes it very easy to hook up a preferences dialog to the
220 * underlying settings. To make this even more convenient, GSettings
221 * looks for a boolean property with the name "sensitivity" and
222 * automatically binds it to the writability of the bound setting.
223 * If this 'magic' gets in the way, it can be suppressed with the
224 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
229 struct _GSettingsPrivate
231 /* where the signals go... */
232 GMainContext *main_context;
234 GSettingsBackend *backend;
235 GSettingsSchema *schema;
239 GDelayedSettingsBackend *delayed;
254 SIGNAL_WRITABLE_CHANGE_EVENT,
255 SIGNAL_WRITABLE_CHANGED,
261 static guint g_settings_signals[N_SIGNALS];
263 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
267 g_settings_real_change_event (GSettings *settings,
274 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
276 for (i = 0; i < n_keys; i++)
277 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
278 keys[i], g_quark_to_string (keys[i]));
284 g_settings_real_writable_change_event (GSettings *settings,
287 const GQuark *keys = &key;
292 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
294 for (i = 0; i < n_keys; i++)
295 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
296 keys[i], g_quark_to_string (keys[i]));
302 settings_backend_changed (GObject *target,
303 GSettingsBackend *backend,
307 GSettings *settings = G_SETTINGS (target);
308 gboolean ignore_this;
311 /* We used to assert here:
313 * settings->priv->backend == backend
315 * but it could be the case that a notification is queued for delivery
316 * while someone calls g_settings_delay() (which changes the backend).
318 * Since the delay backend would just pass that straight through
319 * anyway, it doesn't make sense to try to detect this case.
320 * Therefore, we just accept it.
323 for (i = 0; key[i] == settings->priv->path[i]; i++);
325 if (settings->priv->path[i] == '\0' &&
326 g_settings_schema_has_key (settings->priv->schema, key + i))
330 quark = g_quark_from_string (key + i);
331 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
332 0, &quark, 1, &ignore_this);
337 settings_backend_path_changed (GObject *target,
338 GSettingsBackend *backend,
342 GSettings *settings = G_SETTINGS (target);
343 gboolean ignore_this;
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 (GObject *target,
352 GSettingsBackend *backend,
354 const gchar * const *items,
357 GSettings *settings = G_SETTINGS (target);
358 gboolean ignore_this;
361 for (i = 0; settings->priv->path[i] &&
362 settings->priv->path[i] == path[i]; i++);
369 for (j = 0; items[j]; j++)
371 const gchar *item = items[j];
374 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
376 if (settings->priv->path[i + k] == '\0' &&
377 g_settings_schema_has_key (settings->priv->schema, item + k))
378 quarks[l++] = g_quark_from_string (item + k);
380 /* "256 quarks ought to be enough for anybody!"
381 * If this bites you, I'm sorry. Please file a bug.
387 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
388 0, quarks, l, &ignore_this);
393 settings_backend_writable_changed (GObject *target,
394 GSettingsBackend *backend,
397 GSettings *settings = G_SETTINGS (target);
398 gboolean ignore_this;
401 for (i = 0; key[i] == settings->priv->path[i]; i++);
403 if (settings->priv->path[i] == '\0' &&
404 g_settings_schema_has_key (settings->priv->schema, key + i))
405 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
406 0, g_quark_from_string (key + i), &ignore_this);
410 settings_backend_path_writable_changed (GObject *target,
411 GSettingsBackend *backend,
414 GSettings *settings = G_SETTINGS (target);
415 gboolean ignore_this;
417 if (g_str_has_prefix (settings->priv->path, path))
418 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
419 0, (GQuark) 0, &ignore_this);
422 /* Properties, Construction, Destruction {{{1 */
424 g_settings_set_property (GObject *object,
429 GSettings *settings = G_SETTINGS (object);
434 g_assert (settings->priv->schema_name == NULL);
435 settings->priv->schema_name = g_value_dup_string (value);
439 settings->priv->path = g_value_dup_string (value);
443 settings->priv->backend = g_value_dup_object (value);
447 g_assert_not_reached ();
452 g_settings_get_property (GObject *object,
457 GSettings *settings = G_SETTINGS (object);
462 g_value_set_string (value, settings->priv->schema_name);
466 g_value_set_object (value, settings->priv->backend);
470 g_value_set_string (value, settings->priv->path);
473 case PROP_HAS_UNAPPLIED:
474 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
477 case PROP_DELAY_APPLY:
478 g_value_set_boolean (value, settings->priv->delayed != NULL);
482 g_assert_not_reached ();
486 static const GSettingsListenerVTable listener_vtable = {
487 settings_backend_changed,
488 settings_backend_path_changed,
489 settings_backend_keys_changed,
490 settings_backend_writable_changed,
491 settings_backend_path_writable_changed
495 g_settings_constructed (GObject *object)
497 GSettings *settings = G_SETTINGS (object);
498 const gchar *schema_path;
500 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
501 schema_path = g_settings_schema_get_path (settings->priv->schema);
503 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
504 g_error ("settings object created with schema '%s' and path '%s', but "
505 "path '%s' is specified by schema",
506 settings->priv->schema_name, settings->priv->path, schema_path);
508 if (settings->priv->path == NULL)
510 if (schema_path == NULL)
511 g_error ("attempting to create schema '%s' without a path",
512 settings->priv->schema_name);
514 settings->priv->path = g_strdup (schema_path);
517 if (settings->priv->backend == NULL)
518 settings->priv->backend = g_settings_backend_get_default ();
520 g_settings_backend_watch (settings->priv->backend,
521 &listener_vtable, G_OBJECT (settings),
522 settings->priv->main_context);
523 g_settings_backend_subscribe (settings->priv->backend,
524 settings->priv->path);
528 g_settings_finalize (GObject *object)
530 GSettings *settings = G_SETTINGS (object);
532 g_settings_backend_unsubscribe (settings->priv->backend,
533 settings->priv->path);
534 g_main_context_unref (settings->priv->main_context);
535 g_object_unref (settings->priv->backend);
536 g_settings_schema_unref (settings->priv->schema);
537 g_free (settings->priv->schema_name);
538 g_free (settings->priv->path);
540 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
544 g_settings_init (GSettings *settings)
546 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
550 settings->priv->main_context = g_main_context_ref_thread_default ();
554 g_settings_class_init (GSettingsClass *class)
556 GObjectClass *object_class = G_OBJECT_CLASS (class);
558 class->writable_change_event = g_settings_real_writable_change_event;
559 class->change_event = g_settings_real_change_event;
561 object_class->set_property = g_settings_set_property;
562 object_class->get_property = g_settings_get_property;
563 object_class->constructed = g_settings_constructed;
564 object_class->finalize = g_settings_finalize;
566 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
569 * GSettings::changed:
570 * @settings: the object on which the signal was emitted
571 * @key: the name of the key that changed
573 * The "changed" signal is emitted when a key has potentially changed.
574 * You should call one of the g_settings_get() calls to check the new
577 * This signal supports detailed connections. You can connect to the
578 * detailed signal "changed::x" in order to only receive callbacks
579 * when key "x" changes.
581 g_settings_signals[SIGNAL_CHANGED] =
582 g_signal_new ("changed", G_TYPE_SETTINGS,
583 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
584 G_STRUCT_OFFSET (GSettingsClass, changed),
585 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
586 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
589 * GSettings::change-event:
590 * @settings: the object on which the signal was emitted
591 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
592 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
593 * @n_keys: the length of the @keys array, or 0
594 * @returns: %TRUE to stop other handlers from being invoked for the
595 * event. FALSE to propagate the event further.
597 * The "change-event" signal is emitted once per change event that
598 * affects this settings object. You should connect to this signal
599 * only if you are interested in viewing groups of changes before they
600 * are split out into multiple emissions of the "changed" signal.
601 * For most use cases it is more appropriate to use the "changed" signal.
603 * In the event that the change event applies to one or more specified
604 * keys, @keys will be an array of #GQuark of length @n_keys. In the
605 * event that the change event applies to the #GSettings object as a
606 * whole (ie: potentially every key has been changed) then @keys will
607 * be %NULL and @n_keys will be 0.
609 * The default handler for this signal invokes the "changed" signal
610 * for each affected key. If any other connected handler returns
611 * %TRUE then this default functionality will be suppressed.
613 g_settings_signals[SIGNAL_CHANGE_EVENT] =
614 g_signal_new ("change-event", G_TYPE_SETTINGS,
616 G_STRUCT_OFFSET (GSettingsClass, change_event),
617 g_signal_accumulator_true_handled, NULL,
619 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
622 * GSettings::writable-changed:
623 * @settings: the object on which the signal was emitted
626 * The "writable-changed" signal is emitted when the writability of a
627 * key has potentially changed. You should call
628 * g_settings_is_writable() in order to determine the new status.
630 * This signal supports detailed connections. You can connect to the
631 * detailed signal "writable-changed::x" in order to only receive
632 * callbacks when the writability of "x" changes.
634 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
635 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
636 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
637 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
638 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
639 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
642 * GSettings::writable-change-event:
643 * @settings: the object on which the signal was emitted
644 * @key: the quark of the key, or 0
645 * @returns: %TRUE to stop other handlers from being invoked for the
646 * event. FALSE to propagate the event further.
648 * The "writable-change-event" signal is emitted once per writability
649 * change event that affects this settings object. You should connect
650 * to this signal if you are interested in viewing groups of changes
651 * before they are split out into multiple emissions of the
652 * "writable-changed" signal. For most use cases it is more
653 * appropriate to use the "writable-changed" signal.
655 * In the event that the writability change applies only to a single
656 * key, @key will be set to the #GQuark for that key. In the event
657 * that the writability change affects the entire settings object,
660 * The default handler for this signal invokes the "writable-changed"
661 * and "changed" signals for each affected key. This is done because
662 * changes in writability might also imply changes in value (if for
663 * example, a new mandatory setting is introduced). If any other
664 * connected handler returns %TRUE then this default functionality
665 * will be suppressed.
667 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
668 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
670 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
671 g_signal_accumulator_true_handled, NULL,
672 NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
677 * The name of the context that the settings are stored in.
679 g_object_class_install_property (object_class, PROP_BACKEND,
680 g_param_spec_object ("backend",
681 P_("GSettingsBackend"),
682 P_("The GSettingsBackend for this settings object"),
683 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
684 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
689 * The name of the schema that describes the types of keys
690 * for this #GSettings object.
692 g_object_class_install_property (object_class, PROP_SCHEMA,
693 g_param_spec_string ("schema",
695 P_("The name of the schema for this settings object"),
697 G_PARAM_CONSTRUCT_ONLY |
698 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
703 * The path within the backend where the settings are stored.
705 g_object_class_install_property (object_class, PROP_PATH,
706 g_param_spec_string ("path",
708 P_("The path within the backend where the settings are"),
710 G_PARAM_CONSTRUCT_ONLY |
711 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
714 * GSettings:has-unapplied:
716 * If this property is %TRUE, the #GSettings object has outstanding
717 * changes that will be applied when g_settings_apply() is called.
719 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
720 g_param_spec_boolean ("has-unapplied",
721 P_("Has unapplied changes"),
722 P_("TRUE if there are outstanding changes to apply()"),
724 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
727 * GSettings:delay-apply:
729 * Whether the #GSettings object is in 'delay-apply' mode. See
730 * g_settings_delay() for details.
734 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
735 g_param_spec_boolean ("delay-apply",
736 P_("Delay-apply mode"),
737 P_("Whether this settings object is in 'delay-apply' mode"),
739 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
742 /* Construction (new, new_with_path, etc.) {{{1 */
745 * @schema: the name of the schema
746 * @returns: a new #GSettings object
748 * Creates a new #GSettings object with a given schema.
750 * Signals on the newly created #GSettings object will be dispatched
751 * via the thread-default #GMainContext in effect at the time of the
752 * call to g_settings_new(). The new #GSettings will hold a reference
753 * on the context. See g_main_context_push_thread_default().
758 g_settings_new (const gchar *schema)
760 g_return_val_if_fail (schema != NULL, NULL);
762 return g_object_new (G_TYPE_SETTINGS,
768 * g_settings_new_with_path:
769 * @schema: the name of the schema
770 * @path: the path to use
771 * @returns: a new #GSettings object
773 * Creates a new #GSettings object with a given schema and path.
775 * You only need to do this if you want to directly create a settings
776 * object with a schema that doesn't have a specified path of its own.
779 * It is a programmer error to call this function for a schema that
780 * has an explicitly specified path.
785 g_settings_new_with_path (const gchar *schema,
788 g_return_val_if_fail (schema != NULL, NULL);
789 g_return_val_if_fail (path != NULL, NULL);
791 return g_object_new (G_TYPE_SETTINGS,
798 * g_settings_new_with_backend:
799 * @schema: the name of the schema
800 * @backend: the #GSettingsBackend to use
801 * @returns: a new #GSettings object
803 * Creates a new #GSettings object with a given schema and backend.
805 * Creating a #GSettings object with a different backend allows accessing
806 * settings from a database other than the usual one. For example, it may make
807 * sense to pass a backend corresponding to the "defaults" settings database on
808 * the system to get a settings object that modifies the system default
809 * settings instead of the settings for this user.
814 g_settings_new_with_backend (const gchar *schema,
815 GSettingsBackend *backend)
817 g_return_val_if_fail (schema != NULL, NULL);
818 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
820 return g_object_new (G_TYPE_SETTINGS,
827 * g_settings_new_with_backend_and_path:
828 * @schema: the name of the schema
829 * @backend: the #GSettingsBackend to use
830 * @path: the path to use
831 * @returns: a new #GSettings object
833 * Creates a new #GSettings object with a given schema, backend and
836 * This is a mix of g_settings_new_with_backend() and
837 * g_settings_new_with_path().
842 g_settings_new_with_backend_and_path (const gchar *schema,
843 GSettingsBackend *backend,
846 g_return_val_if_fail (schema != NULL, NULL);
847 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
848 g_return_val_if_fail (path != NULL, NULL);
850 return g_object_new (G_TYPE_SETTINGS,
857 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
860 GSettingsSchema *schema;
866 const guint32 *strinfo;
867 gsize strinfo_length;
869 const gchar *unparsed;
872 const GVariantType *type;
873 GVariant *minimum, *maximum;
874 GVariant *default_value;
875 } GSettingsSchemaKey;
878 endian_fixup (GVariant **value)
880 #if G_BYTE_ORDER == G_BIG_ENDIAN
883 tmp = g_variant_byteswap (*value);
884 g_variant_unref (*value);
890 g_settings_schema_key_init (GSettingsSchemaKey *key,
891 GSettingsSchema *schema,
898 memset (key, 0, sizeof *key);
900 iter = g_settings_schema_get_value (schema, name);
902 key->schema = g_settings_schema_ref (schema);
903 key->default_value = g_variant_iter_next_value (iter);
904 endian_fixup (&key->default_value);
905 key->type = g_variant_get_type (key->default_value);
906 key->name = g_intern_string (name);
908 while (g_variant_iter_next (iter, "(y*)", &code, &data))
913 /* translation requested */
914 g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
918 /* enumerated types... */
924 key->is_flags = TRUE;
928 /* ..., choices, aliases */
929 key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
933 g_variant_get (data, "(**)", &key->minimum, &key->maximum);
934 endian_fixup (&key->minimum);
935 endian_fixup (&key->maximum);
939 g_warning ("unknown schema extension '%c'", code);
943 g_variant_unref (data);
946 g_variant_iter_free (iter);
950 g_settings_schema_key_clear (GSettingsSchemaKey *key)
953 g_variant_unref (key->minimum);
956 g_variant_unref (key->maximum);
958 g_variant_unref (key->default_value);
960 g_settings_schema_unref (key->schema);
964 g_settings_write_to_backend (GSettings *settings,
965 GSettingsSchemaKey *key,
971 path = g_strconcat (settings->priv->path, key->name, NULL);
972 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
979 g_settings_schema_key_type_check (GSettingsSchemaKey *key,
982 g_return_val_if_fail (value != NULL, FALSE);
984 return g_variant_is_of_type (value, key->type);
988 g_settings_schema_key_range_check (GSettingsSchemaKey *key,
991 if (key->minimum == NULL && key->strinfo == NULL)
994 if (g_variant_is_container (value))
1000 g_variant_iter_init (&iter, value);
1001 while (ok && (child = g_variant_iter_next_value (&iter)))
1003 ok = g_settings_schema_key_range_check (key, child);
1004 g_variant_unref (child);
1012 return g_variant_compare (key->minimum, value) <= 0 &&
1013 g_variant_compare (value, key->maximum) <= 0;
1016 return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1017 g_variant_get_string (value, NULL));
1021 g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1024 const gchar *target;
1026 if (g_settings_schema_key_range_check (key, value))
1027 return g_variant_ref (value);
1029 if (key->strinfo == NULL)
1032 if (g_variant_is_container (value))
1034 GVariantBuilder builder;
1038 g_variant_iter_init (&iter, value);
1039 g_variant_builder_init (&builder, g_variant_get_type (value));
1041 while ((child = g_variant_iter_next_value (&iter)))
1045 fixed = g_settings_schema_key_range_fixup (key, child);
1046 g_variant_unref (child);
1050 g_variant_builder_clear (&builder);
1054 g_variant_builder_add_value (&builder, fixed);
1055 g_variant_unref (fixed);
1058 return g_variant_ref_sink (g_variant_builder_end (&builder));
1061 target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1062 g_variant_get_string (value, NULL));
1063 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1067 g_settings_read_from_backend (GSettings *settings,
1068 GSettingsSchemaKey *key)
1074 path = g_strconcat (settings->priv->path, key->name, NULL);
1075 value = g_settings_backend_read (settings->priv->backend, path, key->type, FALSE);
1080 fixup = g_settings_schema_key_range_fixup (key, value);
1081 g_variant_unref (value);
1090 g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1092 const gchar *translated;
1093 GError *error = NULL;
1094 const gchar *domain;
1097 domain = g_settings_schema_get_gettext_domain (key->schema);
1099 if (key->lc_char == '\0')
1100 /* translation not requested for this key */
1103 if (key->lc_char == 't')
1104 translated = g_dcgettext (domain, key->unparsed, LC_TIME);
1106 translated = g_dgettext (domain, key->unparsed);
1108 if (translated == key->unparsed)
1109 /* the default value was not translated */
1112 /* try to parse the translation of the unparsed default */
1113 value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1117 g_warning ("Failed to parse translated string `%s' for "
1118 "key `%s' in schema `%s': %s", key->unparsed, key->name,
1119 g_settings_schema_get_name (key->schema), error->message);
1120 g_warning ("Using untranslated default instead.");
1121 g_error_free (error);
1124 else if (!g_settings_schema_key_range_check (key, value))
1126 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1127 "is outside of valid range", key->unparsed, key->name,
1128 g_settings_schema_get_name (key->schema));
1129 g_variant_unref (value);
1137 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1143 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1144 g_variant_get_string (value, NULL),
1147 /* 'value' can only come from the backend after being filtered for validity,
1148 * from the translation after being filtered for validity, or from the schema
1149 * itself (which the schema compiler checks for validity). If this assertion
1150 * fails then it's really a bug in GSettings or the schema compiler...
1152 g_assert (it_worked);
1158 g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1161 const gchar *string;
1163 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1168 return g_variant_new_string (string);
1172 g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1180 g_variant_iter_init (&iter, value);
1181 while (g_variant_iter_next (&iter, "&s", &flag))
1186 it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1187 /* as in g_settings_to_enum() */
1188 g_assert (it_worked);
1190 result |= flag_value;
1197 g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1200 GVariantBuilder builder;
1203 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1205 for (i = 0; i < 32; i++)
1206 if (value & (1u << i))
1208 const gchar *string;
1210 string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1214 g_variant_builder_clear (&builder);
1218 g_variant_builder_add (&builder, "s", string);
1221 return g_variant_builder_end (&builder);
1224 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1226 * g_settings_get_value:
1227 * @settings: a #GSettings object
1228 * @key: the key to get the value for
1229 * @returns: a new #GVariant
1231 * Gets the value that is stored in @settings for @key.
1233 * It is a programmer error to give a @key that isn't contained in the
1234 * schema for @settings.
1239 g_settings_get_value (GSettings *settings,
1242 GSettingsSchemaKey skey;
1245 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1246 g_return_val_if_fail (key != NULL, NULL);
1248 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1249 value = g_settings_read_from_backend (settings, &skey);
1252 value = g_settings_schema_key_get_translated_default (&skey);
1255 value = g_variant_ref (skey.default_value);
1257 g_settings_schema_key_clear (&skey);
1263 * g_settings_get_enum:
1264 * @settings: a #GSettings object
1265 * @key: the key to get the value for
1266 * @returns: the enum value
1268 * Gets the value that is stored in @settings for @key and converts it
1269 * to the enum value that it represents.
1271 * In order to use this function the type of the value must be a string
1272 * and it must be marked in the schema file as an enumerated type.
1274 * It is a programmer error to give a @key that isn't contained in the
1275 * schema for @settings or is not marked as an enumerated type.
1277 * If the value stored in the configuration database is not a valid
1278 * value for the enumerated type then this function will return the
1284 g_settings_get_enum (GSettings *settings,
1287 GSettingsSchemaKey skey;
1291 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1292 g_return_val_if_fail (key != NULL, -1);
1294 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1298 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1299 "associated with an enumerated type", skey.name);
1300 g_settings_schema_key_clear (&skey);
1304 value = g_settings_read_from_backend (settings, &skey);
1307 value = g_settings_schema_key_get_translated_default (&skey);
1310 value = g_variant_ref (skey.default_value);
1312 result = g_settings_schema_key_to_enum (&skey, value);
1313 g_settings_schema_key_clear (&skey);
1314 g_variant_unref (value);
1320 * g_settings_set_enum:
1321 * @settings: a #GSettings object
1322 * @key: a key, within @settings
1323 * @value: an enumerated value
1324 * @returns: %TRUE, if the set succeeds
1326 * Looks up the enumerated type nick for @value and writes it to @key,
1329 * It is a programmer error to give a @key that isn't contained in the
1330 * schema for @settings or is not marked as an enumerated type, or for
1331 * @value not to be a valid value for the named type.
1333 * After performing the write, accessing @key directly with
1334 * g_settings_get_string() will return the 'nick' associated with
1338 g_settings_set_enum (GSettings *settings,
1342 GSettingsSchemaKey skey;
1346 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1347 g_return_val_if_fail (key != NULL, FALSE);
1349 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1353 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1354 "associated with an enumerated type", skey.name);
1358 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1360 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1361 "in schema `%s'. Doing nothing.", value, skey.name,
1362 g_settings_schema_get_name (skey.schema));
1363 g_settings_schema_key_clear (&skey);
1367 success = g_settings_write_to_backend (settings, &skey, variant);
1368 g_settings_schema_key_clear (&skey);
1374 * g_settings_get_flags:
1375 * @settings: a #GSettings object
1376 * @key: the key to get the value for
1377 * @returns: the flags value
1379 * Gets the value that is stored in @settings for @key and converts it
1380 * to the flags value that it represents.
1382 * In order to use this function the type of the value must be an array
1383 * of strings and it must be marked in the schema file as an flags type.
1385 * It is a programmer error to give a @key that isn't contained in the
1386 * schema for @settings or is not marked as a flags type.
1388 * If the value stored in the configuration database is not a valid
1389 * value for the flags type then this function will return the default
1395 g_settings_get_flags (GSettings *settings,
1398 GSettingsSchemaKey skey;
1402 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1403 g_return_val_if_fail (key != NULL, -1);
1405 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1409 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1410 "associated with a flags type", skey.name);
1411 g_settings_schema_key_clear (&skey);
1415 value = g_settings_read_from_backend (settings, &skey);
1418 value = g_settings_schema_key_get_translated_default (&skey);
1421 value = g_variant_ref (skey.default_value);
1423 result = g_settings_schema_key_to_flags (&skey, value);
1424 g_settings_schema_key_clear (&skey);
1425 g_variant_unref (value);
1431 * g_settings_set_flags:
1432 * @settings: a #GSettings object
1433 * @key: a key, within @settings
1434 * @value: a flags value
1435 * @returns: %TRUE, if the set succeeds
1437 * Looks up the flags type nicks for the bits specified by @value, puts
1438 * them in an array of strings and writes the array to @key, within
1441 * It is a programmer error to give a @key that isn't contained in the
1442 * schema for @settings or is not marked as a flags type, or for @value
1443 * to contain any bits that are not value for the named type.
1445 * After performing the write, accessing @key directly with
1446 * g_settings_get_strv() will return an array of 'nicks'; one for each
1450 g_settings_set_flags (GSettings *settings,
1454 GSettingsSchemaKey skey;
1458 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1459 g_return_val_if_fail (key != NULL, FALSE);
1461 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1465 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1466 "associated with a flags type", skey.name);
1470 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1472 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1473 "for key `%s' in schema `%s'. Doing nothing.",
1474 value, skey.name, g_settings_schema_get_name (skey.schema));
1475 g_settings_schema_key_clear (&skey);
1479 success = g_settings_write_to_backend (settings, &skey, variant);
1480 g_settings_schema_key_clear (&skey);
1486 * g_settings_set_value:
1487 * @settings: a #GSettings object
1488 * @key: the name of the key to set
1489 * @value: a #GVariant of the correct type
1490 * @returns: %TRUE if setting the key succeeded,
1491 * %FALSE if the key was not writable
1493 * Sets @key in @settings to @value.
1495 * It is a programmer error to give a @key that isn't contained in the
1496 * schema for @settings or for @value to have the incorrect type, per
1499 * If @value is floating then this function consumes the reference.
1504 g_settings_set_value (GSettings *settings,
1508 GSettingsSchemaKey skey;
1510 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1511 g_return_val_if_fail (key != NULL, FALSE);
1513 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1515 if (!g_settings_schema_key_type_check (&skey, value))
1517 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1519 settings->priv->schema_name,
1520 g_variant_type_peek_string (skey.type),
1521 g_variant_get_type_string (value));
1526 if (!g_settings_schema_key_range_check (&skey, value))
1528 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1529 "is outside of valid range",
1531 settings->priv->schema_name);
1536 g_settings_schema_key_clear (&skey);
1538 return g_settings_write_to_backend (settings, &skey, value);
1543 * @settings: a #GSettings object
1544 * @key: the key to get the value for
1545 * @format: a #GVariant format string
1546 * @...: arguments as per @format
1548 * Gets the value that is stored at @key in @settings.
1550 * A convenience function that combines g_settings_get_value() with
1553 * It is a programmer error to give a @key that isn't contained in the
1554 * schema for @settings or for the #GVariantType of @format to mismatch
1555 * the type given in the schema.
1560 g_settings_get (GSettings *settings,
1562 const gchar *format,
1568 value = g_settings_get_value (settings, key);
1570 va_start (ap, format);
1571 g_variant_get_va (value, format, NULL, &ap);
1574 g_variant_unref (value);
1579 * @settings: a #GSettings object
1580 * @key: the name of the key to set
1581 * @format: a #GVariant format string
1582 * @...: arguments as per @format
1583 * @returns: %TRUE if setting the key succeeded,
1584 * %FALSE if the key was not writable
1586 * Sets @key in @settings to @value.
1588 * A convenience function that combines g_settings_set_value() with
1591 * It is a programmer error to give a @key that isn't contained in the
1592 * schema for @settings or for the #GVariantType of @format to mismatch
1593 * the type given in the schema.
1598 g_settings_set (GSettings *settings,
1600 const gchar *format,
1606 va_start (ap, format);
1607 value = g_variant_new_va (format, NULL, &ap);
1610 return g_settings_set_value (settings, key, value);
1614 * g_settings_get_mapped:
1615 * @settings: a #GSettings object
1616 * @key: the key to get the value for
1617 * @mapping: (scope call): the function to map the value in the
1618 * settings database to the value used by the application
1619 * @user_data: user data for @mapping
1620 * @returns: (transfer full): the result, which may be %NULL
1622 * Gets the value that is stored at @key in @settings, subject to
1623 * application-level validation/mapping.
1625 * You should use this function when the application needs to perform
1626 * some processing on the value of the key (for example, parsing). The
1627 * @mapping function performs that processing. If the function
1628 * indicates that the processing was unsuccessful (due to a parse error,
1629 * for example) then the mapping is tried again with another value.
1631 * This allows a robust 'fall back to defaults' behaviour to be
1632 * implemented somewhat automatically.
1634 * The first value that is tried is the user's setting for the key. If
1635 * the mapping function fails to map this value, other values may be
1636 * tried in an unspecified order (system or site defaults, translated
1637 * schema default values, untranslated schema default values, etc).
1639 * If the mapping function fails for all possible values, one additional
1640 * attempt is made: the mapping function is called with a %NULL value.
1641 * If the mapping function still indicates failure at this point then
1642 * the application will be aborted.
1644 * The result parameter for the @mapping function is pointed to a
1645 * #gpointer which is initially set to %NULL. The same pointer is given
1646 * to each invocation of @mapping. The final value of that #gpointer is
1647 * what is returned by this function. %NULL is valid; it is returned
1648 * just as any other value would be.
1651 g_settings_get_mapped (GSettings *settings,
1653 GSettingsGetMapping mapping,
1656 gpointer result = NULL;
1657 GSettingsSchemaKey skey;
1661 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1662 g_return_val_if_fail (key != NULL, NULL);
1663 g_return_val_if_fail (mapping != NULL, NULL);
1665 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1667 if ((value = g_settings_read_from_backend (settings, &skey)))
1669 okay = mapping (value, &result, user_data);
1670 g_variant_unref (value);
1671 if (okay) goto okay;
1674 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1676 okay = mapping (value, &result, user_data);
1677 g_variant_unref (value);
1678 if (okay) goto okay;
1681 if (mapping (skey.default_value, &result, user_data))
1684 if (!mapping (NULL, &result, user_data))
1685 g_error ("The mapping function given to g_settings_get_mapped() for key "
1686 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1687 key, settings->priv->schema_name);
1690 g_settings_schema_key_clear (&skey);
1695 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1697 * g_settings_get_string:
1698 * @settings: a #GSettings object
1699 * @key: the key to get the value for
1700 * @returns: a newly-allocated string
1702 * Gets the value that is stored at @key in @settings.
1704 * A convenience variant of g_settings_get() for strings.
1706 * It is a programmer error to give a @key that isn't specified as
1707 * having a string type in the schema for @settings.
1712 g_settings_get_string (GSettings *settings,
1718 value = g_settings_get_value (settings, key);
1719 result = g_variant_dup_string (value, NULL);
1720 g_variant_unref (value);
1726 * g_settings_set_string:
1727 * @settings: a #GSettings object
1728 * @key: the name of the key to set
1729 * @value: the value to set it to
1730 * @returns: %TRUE if setting the key succeeded,
1731 * %FALSE if the key was not writable
1733 * Sets @key in @settings to @value.
1735 * A convenience variant of g_settings_set() for strings.
1737 * It is a programmer error to give a @key that isn't specified as
1738 * having a string type in the schema for @settings.
1743 g_settings_set_string (GSettings *settings,
1747 return g_settings_set_value (settings, key, g_variant_new_string (value));
1751 * g_settings_get_int:
1752 * @settings: a #GSettings object
1753 * @key: the key to get the value for
1754 * @returns: an integer
1756 * Gets the value that is stored at @key in @settings.
1758 * A convenience variant of g_settings_get() for 32-bit integers.
1760 * It is a programmer error to give a @key that isn't specified as
1761 * having a int32 type in the schema for @settings.
1766 g_settings_get_int (GSettings *settings,
1772 value = g_settings_get_value (settings, key);
1773 result = g_variant_get_int32 (value);
1774 g_variant_unref (value);
1780 * g_settings_set_int:
1781 * @settings: a #GSettings object
1782 * @key: the name of the key to set
1783 * @value: the value to set it to
1784 * @returns: %TRUE if setting the key succeeded,
1785 * %FALSE if the key was not writable
1787 * Sets @key in @settings to @value.
1789 * A convenience variant of g_settings_set() for 32-bit integers.
1791 * It is a programmer error to give a @key that isn't specified as
1792 * having a int32 type in the schema for @settings.
1797 g_settings_set_int (GSettings *settings,
1801 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1805 * g_settings_get_uint:
1806 * @settings: a #GSettings object
1807 * @key: the key to get the value for
1808 * @returns: an unsigned integer
1810 * Gets the value that is stored at @key in @settings.
1812 * A convenience variant of g_settings_get() for 32-bit unsigned
1815 * It is a programmer error to give a @key that isn't specified as
1816 * having a uint32 type in the schema for @settings.
1821 g_settings_get_uint (GSettings *settings,
1827 value = g_settings_get_value (settings, key);
1828 result = g_variant_get_uint32 (value);
1829 g_variant_unref (value);
1835 * g_settings_set_uint:
1836 * @settings: a #GSettings object
1837 * @key: the name of the key to set
1838 * @value: the value to set it to
1839 * @returns: %TRUE if setting the key succeeded,
1840 * %FALSE if the key was not writable
1842 * Sets @key in @settings to @value.
1844 * A convenience variant of g_settings_set() for 32-bit unsigned
1847 * It is a programmer error to give a @key that isn't specified as
1848 * having a uint32 type in the schema for @settings.
1853 g_settings_set_uint (GSettings *settings,
1857 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1861 * g_settings_get_double:
1862 * @settings: a #GSettings object
1863 * @key: the key to get the value for
1864 * @returns: a double
1866 * Gets the value that is stored at @key in @settings.
1868 * A convenience variant of g_settings_get() for doubles.
1870 * It is a programmer error to give a @key that isn't specified as
1871 * having a 'double' type in the schema for @settings.
1876 g_settings_get_double (GSettings *settings,
1882 value = g_settings_get_value (settings, key);
1883 result = g_variant_get_double (value);
1884 g_variant_unref (value);
1890 * g_settings_set_double:
1891 * @settings: a #GSettings object
1892 * @key: the name of the key to set
1893 * @value: the value to set it to
1894 * @returns: %TRUE if setting the key succeeded,
1895 * %FALSE if the key was not writable
1897 * Sets @key in @settings to @value.
1899 * A convenience variant of g_settings_set() for doubles.
1901 * It is a programmer error to give a @key that isn't specified as
1902 * having a 'double' type in the schema for @settings.
1907 g_settings_set_double (GSettings *settings,
1911 return g_settings_set_value (settings, key, g_variant_new_double (value));
1915 * g_settings_get_boolean:
1916 * @settings: a #GSettings object
1917 * @key: the key to get the value for
1918 * @returns: a boolean
1920 * Gets the value that is stored at @key in @settings.
1922 * A convenience variant of g_settings_get() for booleans.
1924 * It is a programmer error to give a @key that isn't specified as
1925 * having a boolean type in the schema for @settings.
1930 g_settings_get_boolean (GSettings *settings,
1936 value = g_settings_get_value (settings, key);
1937 result = g_variant_get_boolean (value);
1938 g_variant_unref (value);
1944 * g_settings_set_boolean:
1945 * @settings: a #GSettings object
1946 * @key: the name of the key to set
1947 * @value: the value to set it to
1948 * @returns: %TRUE if setting the key succeeded,
1949 * %FALSE if the key was not writable
1951 * Sets @key in @settings to @value.
1953 * A convenience variant of g_settings_set() for booleans.
1955 * It is a programmer error to give a @key that isn't specified as
1956 * having a boolean type in the schema for @settings.
1961 g_settings_set_boolean (GSettings *settings,
1965 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1969 * g_settings_get_strv:
1970 * @settings: a #GSettings object
1971 * @key: the key to get the value for
1972 * @returns: (array zero-terminated=1) (transfer full): a
1973 * newly-allocated, %NULL-terminated array of strings, the value that
1974 * is stored at @key in @settings.
1976 * A convenience variant of g_settings_get() for string arrays.
1978 * It is a programmer error to give a @key that isn't specified as
1979 * having an array of strings type in the schema for @settings.
1984 g_settings_get_strv (GSettings *settings,
1990 value = g_settings_get_value (settings, key);
1991 result = g_variant_dup_strv (value, NULL);
1992 g_variant_unref (value);
1998 * g_settings_set_strv:
1999 * @settings: a #GSettings object
2000 * @key: the name of the key to set
2001 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
2002 * @returns: %TRUE if setting the key succeeded,
2003 * %FALSE if the key was not writable
2005 * Sets @key in @settings to @value.
2007 * A convenience variant of g_settings_set() for string arrays. If
2008 * @value is %NULL, then @key is set to be the empty array.
2010 * It is a programmer error to give a @key that isn't specified as
2011 * having an array of strings type in the schema for @settings.
2016 g_settings_set_strv (GSettings *settings,
2018 const gchar * const *value)
2023 array = g_variant_new_strv (value, -1);
2025 array = g_variant_new_strv (NULL, 0);
2027 return g_settings_set_value (settings, key, array);
2030 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2033 * @settings: a #GSettings object
2035 * Changes the #GSettings object into 'delay-apply' mode. In this
2036 * mode, changes to @settings are not immediately propagated to the
2037 * backend, but kept locally until g_settings_apply() is called.
2042 g_settings_delay (GSettings *settings)
2044 g_return_if_fail (G_IS_SETTINGS (settings));
2046 if (settings->priv->delayed)
2049 settings->priv->delayed =
2050 g_delayed_settings_backend_new (settings->priv->backend,
2052 settings->priv->main_context);
2053 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2054 g_object_unref (settings->priv->backend);
2056 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2057 g_settings_backend_watch (settings->priv->backend,
2058 &listener_vtable, G_OBJECT (settings),
2059 settings->priv->main_context);
2061 g_object_notify (G_OBJECT (settings), "delay-apply");
2066 * @settings: a #GSettings instance
2068 * Applies any changes that have been made to the settings. This
2069 * function does nothing unless @settings is in 'delay-apply' mode;
2070 * see g_settings_delay(). In the normal case settings are always
2071 * applied immediately.
2074 g_settings_apply (GSettings *settings)
2076 if (settings->priv->delayed)
2078 GDelayedSettingsBackend *delayed;
2080 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2081 g_delayed_settings_backend_apply (delayed);
2086 * g_settings_revert:
2087 * @settings: a #GSettings instance
2089 * Reverts all non-applied changes to the settings. This function
2090 * does nothing unless @settings is in 'delay-apply' mode; see
2091 * g_settings_delay(). In the normal case settings are always applied
2094 * Change notifications will be emitted for affected keys.
2097 g_settings_revert (GSettings *settings)
2099 if (settings->priv->delayed)
2101 GDelayedSettingsBackend *delayed;
2103 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2104 g_delayed_settings_backend_revert (delayed);
2109 * g_settings_get_has_unapplied:
2110 * @settings: a #GSettings object
2111 * @returns: %TRUE if @settings has unapplied changes
2113 * Returns whether the #GSettings object has any unapplied
2114 * changes. This can only be the case if it is in 'delayed-apply' mode.
2119 g_settings_get_has_unapplied (GSettings *settings)
2121 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2123 return settings->priv->delayed &&
2124 g_delayed_settings_backend_get_has_unapplied (
2125 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2128 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2131 * @settings: a #GSettings object
2132 * @key: the name of a key
2134 * Resets @key to its default value.
2136 * This call resets the key, as much as possible, to its default value.
2137 * That might the value specified in the schema or the one set by the
2141 g_settings_reset (GSettings *settings,
2146 path = g_strconcat (settings->priv->path, key, NULL);
2147 g_settings_backend_reset (settings->priv->backend, path, NULL);
2154 * Ensures that all pending operations for the given are complete for
2155 * the default backend.
2157 * Writes made to a #GSettings are handled asynchronously. For this
2158 * reason, it is very unlikely that the changes have it to disk by the
2159 * time g_settings_set() returns.
2161 * This call will block until all of the writes have made it to the
2162 * backend. Since the mainloop is not running, no change notifications
2163 * will be dispatched during this call (but some may be queued by the
2164 * time the call is done).
2167 g_settings_sync (void)
2169 g_settings_backend_sync_default ();
2173 * g_settings_is_writable:
2174 * @settings: a #GSettings object
2175 * @name: the name of a key
2176 * @returns: %TRUE if the key @name is writable
2178 * Finds out if a key can be written or not
2183 g_settings_is_writable (GSettings *settings,
2189 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2191 path = g_strconcat (settings->priv->path, name, NULL);
2192 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2199 * g_settings_get_child:
2200 * @settings: a #GSettings object
2201 * @name: the name of the 'child' schema
2202 * @returns: (transfer full): a 'child' settings object
2204 * Creates a 'child' settings object which has a base path of
2205 * <replaceable>base-path</replaceable>/@name, where
2206 * <replaceable>base-path</replaceable> is the base path of @settings.
2208 * The schema for the child settings object must have been declared
2209 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2214 g_settings_get_child (GSettings *settings,
2217 const gchar *child_schema;
2222 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2224 child_name = g_strconcat (name, "/", NULL);
2225 child_schema = g_settings_schema_get_string (settings->priv->schema,
2227 if (child_schema == NULL)
2228 g_error ("Schema '%s' has no child '%s'",
2229 settings->priv->schema_name, name);
2231 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2232 child = g_object_new (G_TYPE_SETTINGS,
2233 "schema", child_schema,
2236 g_free (child_path);
2237 g_free (child_name);
2243 * g_settings_list_keys:
2244 * @settings: a #GSettings object
2245 * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2247 * Introspects the list of keys on @settings.
2249 * You should probably not be calling this function from "normal" code
2250 * (since you should already know what keys are in your schema). This
2251 * function is intended for introspection reasons.
2253 * You should free the return value with g_strfreev() when you are done
2257 g_settings_list_keys (GSettings *settings)
2264 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2265 strv = g_new (gchar *, n_keys + 1);
2266 for (i = j = 0; i < n_keys; i++)
2268 const gchar *key = g_quark_to_string (keys[i]);
2270 if (!g_str_has_suffix (key, "/"))
2271 strv[j++] = g_strdup (key);
2279 * g_settings_list_children:
2280 * @settings: a #GSettings object
2281 * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2283 * Gets the list of children on @settings.
2285 * The list is exactly the list of strings for which it is not an error
2286 * to call g_settings_get_child().
2288 * For GSettings objects that are lists, this value can change at any
2289 * time and you should connect to the "children-changed" signal to watch
2290 * for those changes. Note that there is a race condition here: you may
2291 * request a child after listing it only for it to have been destroyed
2292 * in the meantime. For this reason, g_settings_get_child() may return
2293 * %NULL even for a child that was listed by this function.
2295 * For GSettings objects that are not lists, you should probably not be
2296 * calling this function from "normal" code (since you should already
2297 * know what children are in your schema). This function may still be
2298 * useful there for introspection reasons, however.
2300 * You should free the return value with g_strfreev() when you are done
2304 g_settings_list_children (GSettings *settings)
2311 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2312 strv = g_new (gchar *, n_keys + 1);
2313 for (i = j = 0; i < n_keys; i++)
2315 const gchar *key = g_quark_to_string (keys[i]);
2317 if (g_str_has_suffix (key, "/"))
2319 gint length = strlen (key);
2321 strv[j] = g_memdup (key, length);
2322 strv[j][length - 1] = '\0';
2332 * g_settings_get_range:
2333 * @settings: a #GSettings
2334 * @key: the key to query the range of
2335 * @returns: a #GVariant describing the range
2337 * Queries the range of a key.
2339 * This function will return a #GVariant that fully describes the range
2340 * of values that are valid for @key.
2342 * The type of #GVariant returned is <literal>(sv)</literal>. The
2343 * string describes the type of range restriction in effect. The type
2344 * and meaning of the value contained in the variant depends on the
2347 * If the string is <literal>'type'</literal> then the variant contains
2348 * an empty array. The element type of that empty array is the expected
2349 * type of value and all values of that type are valid.
2351 * If the string is <literal>'enum'</literal> then the variant contains
2352 * an array enumerating the possible values. Each item in the array is
2353 * a possible valid value and no other values are valid.
2355 * If the string is <literal>'flags'</literal> then the variant contains
2356 * an array. Each item in the array is a value that may appear zero or
2357 * one times in an array to be used as the value for this key. For
2358 * example, if the variant contained the array <literal>['x',
2359 * 'y']</literal> then the valid values for the key would be
2360 * <literal>[]</literal>, <literal>['x']</literal>,
2361 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2362 * <literal>['y', 'x']</literal>.
2364 * Finally, if the string is <literal>'range'</literal> then the variant
2365 * contains a pair of like-typed values -- the minimum and maximum
2366 * permissible values for this key.
2368 * This information should not be used by normal programs. It is
2369 * considered to be a hint for introspection purposes. Normal programs
2370 * should already know what is permitted by their own schema. The
2371 * format may change in any way in the future -- but particularly, new
2372 * forms may be added to the possibilities described above.
2374 * It is a programmer error to give a @key that isn't contained in the
2375 * schema for @settings.
2377 * You should free the returned value with g_variant_unref() when it is
2383 g_settings_get_range (GSettings *settings,
2386 GSettingsSchemaKey skey;
2390 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2394 range = g_variant_new ("(**)", skey.minimum, skey.maximum);
2397 else if (skey.strinfo)
2399 range = strinfo_enumerate (skey.strinfo, skey.strinfo_length);
2400 type = skey.is_flags ? "flags" : "enum";
2404 range = g_variant_new_array (skey.type, NULL, 0);
2408 g_settings_schema_key_clear (&skey);
2410 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2414 * g_settings_range_check:
2415 * @settings: a #GSettings
2416 * @key: the key to check
2417 * @value: the value to check
2418 * @returns: %TRUE if @value is valid for @key
2420 * Checks if the given @value is of the correct type and within the
2421 * permitted range for @key.
2423 * This API is not intended to be used by normal programs -- they should
2424 * already know what is permitted by their own schemas. This API is
2425 * meant to be used by programs such as editors or commandline tools.
2427 * It is a programmer error to give a @key that isn't contained in the
2428 * schema for @settings.
2433 g_settings_range_check (GSettings *settings,
2437 GSettingsSchemaKey skey;
2440 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2441 good = g_settings_schema_key_type_check (&skey, value) &&
2442 g_settings_schema_key_range_check (&skey, value);
2443 g_settings_schema_key_clear (&skey);
2451 GSettingsSchemaKey key;
2452 GSettings *settings;
2455 GSettingsBindGetMapping get_mapping;
2456 GSettingsBindSetMapping set_mapping;
2458 GDestroyNotify destroy;
2460 guint writable_handler_id;
2461 guint property_handler_id;
2462 const GParamSpec *property;
2463 guint key_handler_id;
2465 /* prevent recursion */
2470 g_settings_binding_free (gpointer data)
2472 GSettingsBinding *binding = data;
2474 g_assert (!binding->running);
2476 if (binding->writable_handler_id)
2477 g_signal_handler_disconnect (binding->settings,
2478 binding->writable_handler_id);
2480 if (binding->key_handler_id)
2481 g_signal_handler_disconnect (binding->settings,
2482 binding->key_handler_id);
2484 if (g_signal_handler_is_connected (binding->object,
2485 binding->property_handler_id))
2486 g_signal_handler_disconnect (binding->object,
2487 binding->property_handler_id);
2489 g_settings_schema_key_clear (&binding->key);
2491 if (binding->destroy)
2492 binding->destroy (binding->user_data);
2494 g_object_unref (binding->settings);
2496 g_slice_free (GSettingsBinding, binding);
2500 g_settings_binding_quark (const char *property)
2505 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2506 quark = g_quark_from_string (tmp);
2513 g_settings_binding_key_changed (GSettings *settings,
2517 GSettingsBinding *binding = user_data;
2518 GValue value = G_VALUE_INIT;
2521 g_assert (settings == binding->settings);
2522 g_assert (key == binding->key.name);
2524 if (binding->running)
2527 binding->running = TRUE;
2529 g_value_init (&value, binding->property->value_type);
2531 variant = g_settings_read_from_backend (binding->settings, &binding->key);
2532 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2534 /* silently ignore errors in the user's config database */
2535 g_variant_unref (variant);
2539 if (variant == NULL)
2541 variant = g_settings_schema_key_get_translated_default (&binding->key);
2543 !binding->get_mapping (&value, variant, binding->user_data))
2545 /* flag translation errors with a warning */
2546 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2547 "was rejected by the binding mapping function",
2548 binding->key.unparsed, binding->key.name,
2549 g_settings_schema_get_name (binding->key.schema));
2550 g_variant_unref (variant);
2555 if (variant == NULL)
2557 variant = g_variant_ref (binding->key.default_value);
2558 if (!binding->get_mapping (&value, variant, binding->user_data))
2559 g_error ("The schema default value for key `%s' in schema `%s' "
2560 "was rejected by the binding mapping function.",
2561 binding->key.name, g_settings_schema_get_name (binding->key.schema));
2564 g_object_set_property (binding->object, binding->property->name, &value);
2565 g_variant_unref (variant);
2566 g_value_unset (&value);
2568 binding->running = FALSE;
2572 g_settings_binding_property_changed (GObject *object,
2573 const GParamSpec *pspec,
2576 GSettingsBinding *binding = user_data;
2577 GValue value = G_VALUE_INIT;
2580 g_assert (object == binding->object);
2581 g_assert (pspec == binding->property);
2583 if (binding->running)
2586 binding->running = TRUE;
2588 g_value_init (&value, pspec->value_type);
2589 g_object_get_property (object, pspec->name, &value);
2590 if ((variant = binding->set_mapping (&value, binding->key.type,
2591 binding->user_data)))
2593 g_variant_take_ref (variant);
2595 if (!g_settings_schema_key_type_check (&binding->key, variant))
2597 g_critical ("binding mapping function for key `%s' returned "
2598 "GVariant of type `%s' when type `%s' was requested",
2599 binding->key.name, g_variant_get_type_string (variant),
2600 g_variant_type_dup_string (binding->key.type));
2604 if (!g_settings_schema_key_range_check (&binding->key, variant))
2606 g_critical ("GObject property `%s' on a `%s' object is out of "
2607 "schema-specified range for key `%s' of `%s': %s",
2608 binding->property->name, g_type_name (binding->property->owner_type),
2609 binding->key.name, g_settings_schema_get_name (binding->key.schema),
2610 g_variant_print (variant, TRUE));
2614 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2615 g_variant_unref (variant);
2617 g_value_unset (&value);
2619 binding->running = FALSE;
2623 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2627 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2632 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2633 const GVariantType *expected_type,
2636 return g_variant_new_boolean (!g_value_get_boolean (value));
2641 * @settings: a #GSettings object
2642 * @key: the key to bind
2643 * @object: (type GObject.Object): a #GObject
2644 * @property: the name of the property to bind
2645 * @flags: flags for the binding
2647 * Create a binding between the @key in the @settings object
2648 * and the property @property of @object.
2650 * The binding uses the default GIO mapping functions to map
2651 * between the settings and property values. These functions
2652 * handle booleans, numeric types and string types in a
2653 * straightforward way. Use g_settings_bind_with_mapping() if
2654 * you need a custom mapping, or map between types that are not
2655 * supported by the default mapping functions.
2657 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2658 * function also establishes a binding between the writability of
2659 * @key and the "sensitive" property of @object (if @object has
2660 * a boolean property by that name). See g_settings_bind_writable()
2661 * for more details about writable bindings.
2663 * Note that the lifecycle of the binding is tied to the object,
2664 * and that you can have only one binding per object property.
2665 * If you bind the same property twice on the same object, the second
2666 * binding overrides the first one.
2671 g_settings_bind (GSettings *settings,
2674 const gchar *property,
2675 GSettingsBindFlags flags)
2677 GSettingsBindGetMapping get_mapping = NULL;
2678 GSettingsBindSetMapping set_mapping = NULL;
2680 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2682 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2683 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2685 /* can't pass this flag to g_settings_bind_with_mapping() */
2686 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2689 g_settings_bind_with_mapping (settings, key, object, property, flags,
2690 get_mapping, set_mapping, NULL, NULL);
2694 * g_settings_bind_with_mapping: (skip)
2695 * @settings: a #GSettings object
2696 * @key: the key to bind
2697 * @object: (type GObject.Object): a #GObject
2698 * @property: the name of the property to bind
2699 * @flags: flags for the binding
2700 * @get_mapping: a function that gets called to convert values
2701 * from @settings to @object, or %NULL to use the default GIO mapping
2702 * @set_mapping: a function that gets called to convert values
2703 * from @object to @settings, or %NULL to use the default GIO mapping
2704 * @user_data: data that gets passed to @get_mapping and @set_mapping
2705 * @destroy: #GDestroyNotify function for @user_data
2707 * Create a binding between the @key in the @settings object
2708 * and the property @property of @object.
2710 * The binding uses the provided mapping functions to map between
2711 * settings and property values.
2713 * Note that the lifecycle of the binding is tied to the object,
2714 * and that you can have only one binding per object property.
2715 * If you bind the same property twice on the same object, the second
2716 * binding overrides the first one.
2721 g_settings_bind_with_mapping (GSettings *settings,
2724 const gchar *property,
2725 GSettingsBindFlags flags,
2726 GSettingsBindGetMapping get_mapping,
2727 GSettingsBindSetMapping set_mapping,
2729 GDestroyNotify destroy)
2731 GSettingsBinding *binding;
2732 GObjectClass *objectclass;
2733 gchar *detailed_signal;
2734 GQuark binding_quark;
2736 g_return_if_fail (G_IS_SETTINGS (settings));
2737 g_return_if_fail (key != NULL);
2738 g_return_if_fail (G_IS_OBJECT (object));
2739 g_return_if_fail (property != NULL);
2740 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2742 objectclass = G_OBJECT_GET_CLASS (object);
2744 binding = g_slice_new0 (GSettingsBinding);
2745 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2746 binding->settings = g_object_ref (settings);
2747 binding->object = object;
2748 binding->property = g_object_class_find_property (objectclass, property);
2749 binding->user_data = user_data;
2750 binding->destroy = destroy;
2751 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2752 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2754 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2755 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2757 if (binding->property == NULL)
2759 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2760 property, G_OBJECT_TYPE_NAME (object));
2764 if ((flags & G_SETTINGS_BIND_GET) &&
2765 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2767 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2768 "writable", property, G_OBJECT_TYPE_NAME (object));
2771 if ((flags & G_SETTINGS_BIND_SET) &&
2772 (binding->property->flags & G_PARAM_READABLE) == 0)
2774 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2775 "readable", property, G_OBJECT_TYPE_NAME (object));
2779 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2781 /* g_settings_bind_invert_boolean_get_mapping() is a private
2782 * function, so if we are here it means that g_settings_bind() was
2783 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2785 * Ensure that both sides are boolean.
2788 if (binding->property->value_type != G_TYPE_BOOLEAN)
2790 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2791 "was specified, but property `%s' on type `%s' has "
2792 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2793 g_type_name ((binding->property->value_type)));
2797 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2799 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2800 "was specified, but key `%s' on schema `%s' has "
2801 "type `%s'", key, settings->priv->schema_name,
2802 g_variant_type_dup_string (binding->key.type));
2808 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2809 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2810 !g_settings_mapping_is_compatible (binding->property->value_type,
2813 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2814 "'%s' which is not compatible with type '%s' of key '%s' "
2815 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2816 g_type_name (binding->property->value_type),
2817 g_variant_type_dup_string (binding->key.type), key,
2818 settings->priv->schema_name);
2822 if ((flags & G_SETTINGS_BIND_SET) &&
2823 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2825 GParamSpec *sensitive;
2827 sensitive = g_object_class_find_property (objectclass, "sensitive");
2829 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2830 (sensitive->flags & G_PARAM_WRITABLE))
2831 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2834 if (flags & G_SETTINGS_BIND_SET)
2836 detailed_signal = g_strdup_printf ("notify::%s", property);
2837 binding->property_handler_id =
2838 g_signal_connect (object, detailed_signal,
2839 G_CALLBACK (g_settings_binding_property_changed),
2841 g_free (detailed_signal);
2843 if (~flags & G_SETTINGS_BIND_GET)
2844 g_settings_binding_property_changed (object,
2849 if (flags & G_SETTINGS_BIND_GET)
2851 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2853 detailed_signal = g_strdup_printf ("changed::%s", key);
2854 binding->key_handler_id =
2855 g_signal_connect (settings, detailed_signal,
2856 G_CALLBACK (g_settings_binding_key_changed),
2858 g_free (detailed_signal);
2861 g_settings_binding_key_changed (settings, binding->key.name, binding);
2864 binding_quark = g_settings_binding_quark (property);
2865 g_object_set_qdata_full (object, binding_quark,
2866 binding, g_settings_binding_free);
2869 /* Writability binding {{{1 */
2872 GSettings *settings;
2875 const gchar *property;
2878 } GSettingsWritableBinding;
2881 g_settings_writable_binding_free (gpointer data)
2883 GSettingsWritableBinding *binding = data;
2885 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2886 g_object_unref (binding->settings);
2887 g_slice_free (GSettingsWritableBinding, binding);
2891 g_settings_binding_writable_changed (GSettings *settings,
2895 GSettingsWritableBinding *binding = user_data;
2898 g_assert (settings == binding->settings);
2899 g_assert (key == binding->key);
2901 writable = g_settings_is_writable (settings, key);
2903 if (binding->inverted)
2904 writable = !writable;
2906 g_object_set (binding->object, binding->property, writable, NULL);
2910 * g_settings_bind_writable:
2911 * @settings: a #GSettings object
2912 * @key: the key to bind
2913 * @object: (type GObject.Object):a #GObject
2914 * @property: the name of a boolean property to bind
2915 * @inverted: whether to 'invert' the value
2917 * Create a binding between the writability of @key in the
2918 * @settings object and the property @property of @object.
2919 * The property must be boolean; "sensitive" or "visible"
2920 * properties of widgets are the most likely candidates.
2922 * Writable bindings are always uni-directional; changes of the
2923 * writability of the setting will be propagated to the object
2924 * property, not the other way.
2926 * When the @inverted argument is %TRUE, the binding inverts the
2927 * value as it passes from the setting to the object, i.e. @property
2928 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2931 * Note that the lifecycle of the binding is tied to the object,
2932 * and that you can have only one binding per object property.
2933 * If you bind the same property twice on the same object, the second
2934 * binding overrides the first one.
2939 g_settings_bind_writable (GSettings *settings,
2942 const gchar *property,
2945 GSettingsWritableBinding *binding;
2946 gchar *detailed_signal;
2949 g_return_if_fail (G_IS_SETTINGS (settings));
2951 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2954 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2955 property, G_OBJECT_TYPE_NAME (object));
2958 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2960 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2961 property, G_OBJECT_TYPE_NAME (object));
2965 binding = g_slice_new (GSettingsWritableBinding);
2966 binding->settings = g_object_ref (settings);
2967 binding->object = object;
2968 binding->key = g_intern_string (key);
2969 binding->property = g_intern_string (property);
2970 binding->inverted = inverted;
2972 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2973 binding->handler_id =
2974 g_signal_connect (settings, detailed_signal,
2975 G_CALLBACK (g_settings_binding_writable_changed),
2977 g_free (detailed_signal);
2979 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2980 binding, g_settings_writable_binding_free);
2982 g_settings_binding_writable_changed (settings, binding->key, binding);
2986 * g_settings_unbind:
2987 * @object: the object
2988 * @property: the property whose binding is removed
2990 * Removes an existing binding for @property on @object.
2992 * Note that bindings are automatically removed when the
2993 * object is finalized, so it is rarely necessary to call this
2999 g_settings_unbind (gpointer object,
3000 const gchar *property)
3002 GQuark binding_quark;
3004 binding_quark = g_settings_binding_quark (property);
3005 g_object_set_qdata (object, binding_quark, NULL);
3010 /* vim:set foldmethod=marker: */