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 "gio-marshal.h"
35 #include "gsettingsschema.h"
44 * @short_description: High-level API for application settings
46 * The #GSettings class provides a convenient API for storing and retrieving
47 * application settings.
49 * Reads and writes can be considered to be non-blocking. Reading
50 * settings with #GSettings is typically extremely fast: on
51 * approximately the same order of magnitude (but slower than) a
52 * #GHashTable lookup. Writing settings is also extremely fast in terms
53 * of time to return to your application, but can be extremely expensive
54 * for other threads and other processes. Many settings backends
55 * (including dconf) have lazy initialisation which means in the common
56 * case of the user using their computer without modifying any settings
57 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
58 * even need to be started in this case. For this reason, you should
59 * only ever modify #GSettings keys in response to explicit user action.
60 * Particular care should be paid to ensure that modifications are not
61 * made during startup -- for example, when setting the initial value
62 * of preferences widgets. The built-in g_settings_bind() functionality
63 * is careful not to write settings in response to notify signals as a
64 * result of modifications that it makes to widgets.
66 * When creating a GSettings instance, you have to specify a schema
67 * that describes the keys in your settings and their types and default
68 * values, as well as some other information.
70 * Normally, a schema has as fixed path that determines where the settings
71 * are stored in the conceptual global tree of settings. However, schemas
72 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
73 * useful e.g. when the schema describes an 'account', and you want to be
74 * able to store a arbitrary number of accounts.
76 * Unlike other configuration systems (like GConf), GSettings does not
77 * restrict keys to basic types like strings and numbers. GSettings stores
78 * values as #GVariant, and allows any #GVariantType for keys. Key names
79 * are restricted to lowercase characters, numbers and '-'. Furthermore,
80 * the names must begin with a lowercase character, must not end
81 * with a '-', and must not contain consecutive dashes. Key names can
82 * be up to 32 characters long.
84 * Similar to GConf, the default values in GSettings schemas can be
85 * localized, but the localized values are stored in gettext catalogs
86 * and looked up with the domain that is specified in the
87 * <tag class="attribute">gettext-domain</tag> attribute of the
88 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
89 * elements and the category that is specified in the l10n attribute of the
90 * <tag class="starttag">key</tag> element.
92 * GSettings uses schemas in a compact binary form that is created
93 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
94 * utility. The input is a schema description in an XML format that can be
95 * described by the following DTD:
96 * |[<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>]|
98 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
100 * At runtime, schemas are identified by their id (as specified
101 * in the <tag class="attribute">id</tag> attribute of the
102 * <tag class="starttag">schema</tag> element). The
103 * convention for schema ids is to use a dotted name, similar in
104 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
105 * if the settings are for a specific service that owns a D-Bus bus name,
106 * the D-Bus bus name and schema id should match. For schemas which deal
107 * with settings not associated with one named application, the id should
108 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
110 * In addition to #GVariant types, keys can have types that have enumerated
111 * types. These can be described by a <tag class="starttag">choice</tag>,
112 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
113 * <xref linkend="schema-enumerated"/>. The underlying type of
114 * such a key is string, but you can use g_settings_get_enum(),
115 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
116 * access the numeric values corresponding to the string value of enum
119 * <example id="schema-default-values"><title>Default values</title>
120 * <programlisting><![CDATA[
122 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
124 * <key name="greeting" type="s">
125 * <default l10n="messages">"Hello, earthlings"</default>
126 * <summary>A greeting</summary>
128 * Greeting of the invading martians
132 * <key name="box" type="(ii)">
133 * <default>(20,30)</default>
138 * ]]></programlisting></example>
140 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
141 * <programlisting><![CDATA[
145 * <value nick="first" value="1"/>
146 * <value nick="second" value="2"/>
149 * <enum id="myflags">
150 * <value nick="flag1" value="1"/>
151 * <value nick="flag2" value="2"/>
152 * <value nick="flag3" value="4"/>
155 * <schema id="org.gtk.Test">
157 * <key name="key-with-range" type="i">
158 * <range min="1" max="100"/>
159 * <default>10</default>
162 * <key name="key-with-choices" type="s">
164 * <choice value='Elisabeth'/>
165 * <choice value='Annabeth'/>
166 * <choice value='Joe'/>
169 * <alias value='Anna' target='Annabeth'/>
170 * <alias value='Beth' target='Elisabeth'/>
172 * <default>'Joe'</default>
175 * <key name='enumerated-key' enum='myenum'>
176 * <default>'first'</default>
179 * <key name='flags-key' flags='myflags'>
180 * <default>["flag1",flag2"]</default>
184 * ]]></programlisting></example>
187 * <title>Vendor overrides</title>
189 * Default values are defined in the schemas that get installed by
190 * an application. Sometimes, it is necessary for a vendor or distributor
191 * to adjust these defaults. Since patching the XML source for the schema
192 * is inconvenient and error-prone,
193 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
194 * so-called 'vendor override' files. These are keyfiles in the same
195 * directory as the XML schema sources which can override default values.
196 * The schema id serves as the group name in the key file, and the values
197 * are expected in serialized GVariant form, as in the following example:
198 * <informalexample><programlisting>
202 * </programlisting></informalexample>
205 * glib-compile-schemas expects schema files to have the extension
206 * <filename>.gschema.override</filename>
211 * <title>Binding</title>
213 * A very convenient feature of GSettings lets you bind #GObject properties
214 * directly to settings, using g_settings_bind(). Once a GObject property
215 * has been bound to a setting, changes on either side are automatically
216 * propagated to the other side. GSettings handles details like
217 * mapping between GObject and GVariant types, and preventing infinite
221 * This makes it very easy to hook up a preferences dialog to the
222 * underlying settings. To make this even more convenient, GSettings
223 * looks for a boolean property with the name "sensitivity" and
224 * automatically binds it to the writability of the bound setting.
225 * If this 'magic' gets in the way, it can be suppressed with the
226 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
231 struct _GSettingsPrivate
233 /* where the signals go... */
234 GMainContext *main_context;
236 GSettingsBackend *backend;
237 GSettingsSchema *schema;
241 GDelayedSettingsBackend *delayed;
256 SIGNAL_WRITABLE_CHANGE_EVENT,
257 SIGNAL_WRITABLE_CHANGED,
263 static guint g_settings_signals[N_SIGNALS];
265 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
269 g_settings_real_change_event (GSettings *settings,
276 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
278 for (i = 0; i < n_keys; i++)
279 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
280 keys[i], g_quark_to_string (keys[i]));
286 g_settings_real_writable_change_event (GSettings *settings,
289 const GQuark *keys = &key;
294 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
296 for (i = 0; i < n_keys; i++)
297 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
298 keys[i], g_quark_to_string (keys[i]));
304 settings_backend_changed (GObject *target,
305 GSettingsBackend *backend,
309 GSettings *settings = G_SETTINGS (target);
310 gboolean ignore_this;
313 /* We used to assert here:
315 * settings->priv->backend == backend
317 * but it could be the case that a notification is queued for delivery
318 * while someone calls g_settings_delay() (which changes the backend).
320 * Since the delay backend would just pass that straight through
321 * anyway, it doesn't make sense to try to detect this case.
322 * Therefore, we just accept it.
325 for (i = 0; key[i] == settings->priv->path[i]; i++);
327 if (settings->priv->path[i] == '\0' &&
328 g_settings_schema_has_key (settings->priv->schema, key + i))
332 quark = g_quark_from_string (key + i);
333 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
334 0, &quark, 1, &ignore_this);
339 settings_backend_path_changed (GObject *target,
340 GSettingsBackend *backend,
344 GSettings *settings = G_SETTINGS (target);
345 gboolean ignore_this;
347 if (g_str_has_prefix (settings->priv->path, path))
348 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
349 0, NULL, 0, &ignore_this);
353 settings_backend_keys_changed (GObject *target,
354 GSettingsBackend *backend,
356 const gchar * const *items,
359 GSettings *settings = G_SETTINGS (target);
360 gboolean ignore_this;
363 for (i = 0; settings->priv->path[i] &&
364 settings->priv->path[i] == path[i]; i++);
371 for (j = 0; items[j]; j++)
373 const gchar *item = items[j];
376 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
378 if (settings->priv->path[i + k] == '\0' &&
379 g_settings_schema_has_key (settings->priv->schema, item + k))
380 quarks[l++] = g_quark_from_string (item + k);
382 /* "256 quarks ought to be enough for anybody!"
383 * If this bites you, I'm sorry. Please file a bug.
389 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
390 0, quarks, l, &ignore_this);
395 settings_backend_writable_changed (GObject *target,
396 GSettingsBackend *backend,
399 GSettings *settings = G_SETTINGS (target);
400 gboolean ignore_this;
403 for (i = 0; key[i] == settings->priv->path[i]; i++);
405 if (settings->priv->path[i] == '\0' &&
406 g_settings_schema_has_key (settings->priv->schema, key + i))
407 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
408 0, g_quark_from_string (key + i), &ignore_this);
412 settings_backend_path_writable_changed (GObject *target,
413 GSettingsBackend *backend,
416 GSettings *settings = G_SETTINGS (target);
417 gboolean ignore_this;
419 if (g_str_has_prefix (settings->priv->path, path))
420 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
421 0, (GQuark) 0, &ignore_this);
424 /* Properties, Construction, Destruction {{{1 */
426 g_settings_set_property (GObject *object,
431 GSettings *settings = G_SETTINGS (object);
436 g_assert (settings->priv->schema_name == NULL);
437 settings->priv->schema_name = g_value_dup_string (value);
441 settings->priv->path = g_value_dup_string (value);
445 settings->priv->backend = g_value_dup_object (value);
449 g_assert_not_reached ();
454 g_settings_get_property (GObject *object,
459 GSettings *settings = G_SETTINGS (object);
464 g_value_set_string (value, settings->priv->schema_name);
468 g_value_set_object (value, settings->priv->backend);
472 g_value_set_string (value, settings->priv->path);
475 case PROP_HAS_UNAPPLIED:
476 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
479 case PROP_DELAY_APPLY:
480 g_value_set_boolean (value, settings->priv->delayed != NULL);
484 g_assert_not_reached ();
488 static const GSettingsListenerVTable listener_vtable = {
489 settings_backend_changed,
490 settings_backend_path_changed,
491 settings_backend_keys_changed,
492 settings_backend_writable_changed,
493 settings_backend_path_writable_changed
497 g_settings_constructed (GObject *object)
499 GSettings *settings = G_SETTINGS (object);
500 const gchar *schema_path;
502 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
503 schema_path = g_settings_schema_get_path (settings->priv->schema);
505 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
506 g_error ("settings object created with schema '%s' and path '%s', but "
507 "path '%s' is specified by schema",
508 settings->priv->schema_name, settings->priv->path, schema_path);
510 if (settings->priv->path == NULL)
512 if (schema_path == NULL)
513 g_error ("attempting to create schema '%s' without a path",
514 settings->priv->schema_name);
516 settings->priv->path = g_strdup (schema_path);
519 if (settings->priv->backend == NULL)
520 settings->priv->backend = g_settings_backend_get_default ();
522 g_settings_backend_watch (settings->priv->backend,
523 &listener_vtable, G_OBJECT (settings),
524 settings->priv->main_context);
525 g_settings_backend_subscribe (settings->priv->backend,
526 settings->priv->path);
530 g_settings_finalize (GObject *object)
532 GSettings *settings = G_SETTINGS (object);
534 g_settings_backend_unsubscribe (settings->priv->backend,
535 settings->priv->path);
536 g_main_context_unref (settings->priv->main_context);
537 g_object_unref (settings->priv->backend);
538 g_object_unref (settings->priv->schema);
539 g_free (settings->priv->schema_name);
540 g_free (settings->priv->path);
542 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
546 g_settings_init (GSettings *settings)
548 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
552 settings->priv->main_context = g_main_context_get_thread_default ();
554 if (settings->priv->main_context == NULL)
555 settings->priv->main_context = g_main_context_default ();
557 g_main_context_ref (settings->priv->main_context);
561 g_settings_class_init (GSettingsClass *class)
563 GObjectClass *object_class = G_OBJECT_CLASS (class);
565 class->writable_change_event = g_settings_real_writable_change_event;
566 class->change_event = g_settings_real_change_event;
568 object_class->set_property = g_settings_set_property;
569 object_class->get_property = g_settings_get_property;
570 object_class->constructed = g_settings_constructed;
571 object_class->finalize = g_settings_finalize;
573 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
576 * GSettings::changed:
577 * @settings: the object on which the signal was emitted
578 * @key: the name of the key that changed
580 * The "changed" signal is emitted when a key has potentially changed.
581 * You should call one of the g_settings_get() calls to check the new
584 * This signal supports detailed connections. You can connect to the
585 * detailed signal "changed::x" in order to only receive callbacks
586 * when key "x" changes.
588 g_settings_signals[SIGNAL_CHANGED] =
589 g_signal_new ("changed", G_TYPE_SETTINGS,
590 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
591 G_STRUCT_OFFSET (GSettingsClass, changed),
592 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
593 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
596 * GSettings::change-event:
597 * @settings: the object on which the signal was emitted
598 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
599 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
600 * @n_keys: the length of the @keys array, or 0
601 * @returns: %TRUE to stop other handlers from being invoked for the
602 * event. FALSE to propagate the event further.
604 * The "change-event" signal is emitted once per change event that
605 * affects this settings object. You should connect to this signal
606 * only if you are interested in viewing groups of changes before they
607 * are split out into multiple emissions of the "changed" signal.
608 * For most use cases it is more appropriate to use the "changed" signal.
610 * In the event that the change event applies to one or more specified
611 * keys, @keys will be an array of #GQuark of length @n_keys. In the
612 * event that the change event applies to the #GSettings object as a
613 * whole (ie: potentially every key has been changed) then @keys will
614 * be %NULL and @n_keys will be 0.
616 * The default handler for this signal invokes the "changed" signal
617 * for each affected key. If any other connected handler returns
618 * %TRUE then this default functionality will be supressed.
620 g_settings_signals[SIGNAL_CHANGE_EVENT] =
621 g_signal_new ("change-event", G_TYPE_SETTINGS,
623 G_STRUCT_OFFSET (GSettingsClass, change_event),
624 g_signal_accumulator_true_handled, NULL,
625 _gio_marshal_BOOL__POINTER_INT,
626 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
629 * GSettings::writable-changed:
630 * @settings: the object on which the signal was emitted
633 * The "writable-changed" signal is emitted when the writability of a
634 * key has potentially changed. You should call
635 * g_settings_is_writable() in order to determine the new status.
637 * This signal supports detailed connections. You can connect to the
638 * detailed signal "writable-changed::x" in order to only receive
639 * callbacks when the writability of "x" changes.
641 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
642 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
643 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
644 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
645 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
646 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
649 * GSettings::writable-change-event:
650 * @settings: the object on which the signal was emitted
651 * @key: the quark of the key, or 0
652 * @returns: %TRUE to stop other handlers from being invoked for the
653 * event. FALSE to propagate the event further.
655 * The "writable-change-event" signal is emitted once per writability
656 * change event that affects this settings object. You should connect
657 * to this signal if you are interested in viewing groups of changes
658 * before they are split out into multiple emissions of the
659 * "writable-changed" signal. For most use cases it is more
660 * appropriate to use the "writable-changed" signal.
662 * In the event that the writability change applies only to a single
663 * key, @key will be set to the #GQuark for that key. In the event
664 * that the writability change affects the entire settings object,
667 * The default handler for this signal invokes the "writable-changed"
668 * and "changed" signals for each affected key. This is done because
669 * changes in writability might also imply changes in value (if for
670 * example, a new mandatory setting is introduced). If any other
671 * connected handler returns %TRUE then this default functionality
674 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
675 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
677 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
678 g_signal_accumulator_true_handled, NULL,
679 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
684 * The name of the context that the settings are stored in.
686 g_object_class_install_property (object_class, PROP_BACKEND,
687 g_param_spec_object ("backend",
688 P_("GSettingsBackend"),
689 P_("The GSettingsBackend for this settings object"),
690 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
691 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
696 * The name of the schema that describes the types of keys
697 * for this #GSettings object.
699 g_object_class_install_property (object_class, PROP_SCHEMA,
700 g_param_spec_string ("schema",
702 P_("The name of the schema for this settings object"),
704 G_PARAM_CONSTRUCT_ONLY |
705 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
710 * The path within the backend where the settings are stored.
712 g_object_class_install_property (object_class, PROP_PATH,
713 g_param_spec_string ("path",
715 P_("The path within the backend where the settings are"),
717 G_PARAM_CONSTRUCT_ONLY |
718 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
721 * GSettings:has-unapplied:
723 * If this property is %TRUE, the #GSettings object has outstanding
724 * changes that will be applied when g_settings_apply() is called.
726 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
727 g_param_spec_boolean ("has-unapplied",
728 P_("Has unapplied changes"),
729 P_("TRUE if there are outstanding changes to apply()"),
731 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
734 * GSettings:delay-apply:
736 * Whether the #GSettings object is in 'delay-apply' mode. See
737 * g_settings_delay() for details.
741 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
742 g_param_spec_boolean ("delay-apply",
743 P_("Delay-apply mode"),
744 P_("Whether this settings object is in 'delay-apply' mode"),
746 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
749 /* Construction (new, new_with_path, etc.) {{{1 */
752 * @schema: the name of the schema
753 * @returns: a new #GSettings object
755 * Creates a new #GSettings object with a given schema.
757 * Signals on the newly created #GSettings object will be dispatched
758 * via the thread-default #GMainContext in effect at the time of the
759 * call to g_settings_new(). The new #GSettings will hold a reference
760 * on the context. See g_main_context_push_thread_default().
765 g_settings_new (const gchar *schema)
767 g_return_val_if_fail (schema != NULL, NULL);
769 return g_object_new (G_TYPE_SETTINGS,
775 * g_settings_new_with_path:
776 * @schema: the name of the schema
777 * @path: the path to use
778 * @returns: a new #GSettings object
780 * Creates a new #GSettings object with a given schema and path.
782 * You only need to do this if you want to directly create a settings
783 * object with a schema that doesn't have a specified path of its own.
786 * It is a programmer error to call this function for a schema that
787 * has an explicitly specified path.
792 g_settings_new_with_path (const gchar *schema,
795 g_return_val_if_fail (schema != NULL, NULL);
796 g_return_val_if_fail (path != NULL, NULL);
798 return g_object_new (G_TYPE_SETTINGS,
805 * g_settings_new_with_backend:
806 * @schema: the name of the schema
807 * @backend: the #GSettingsBackend to use
808 * @returns: a new #GSettings object
810 * Creates a new #GSettings object with a given schema and backend.
812 * Creating a #GSettings object with a different backend allows accessing
813 * settings from a database other than the usual one. For example, it may make
814 * sense to pass a backend corresponding to the "defaults" settings database on
815 * the system to get a settings object that modifies the system default
816 * settings instead of the settings for this user.
821 g_settings_new_with_backend (const gchar *schema,
822 GSettingsBackend *backend)
824 g_return_val_if_fail (schema != NULL, NULL);
825 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
827 return g_object_new (G_TYPE_SETTINGS,
834 * g_settings_new_with_backend_and_path:
835 * @schema: the name of the schema
836 * @backend: the #GSettingsBackend to use
837 * @path: the path to use
838 * @returns: a new #GSettings object
840 * Creates a new #GSettings object with a given schema, backend and
843 * This is a mix of g_settings_new_with_backend() and
844 * g_settings_new_with_path().
849 g_settings_new_with_backend_and_path (const gchar *schema,
850 GSettingsBackend *backend,
853 g_return_val_if_fail (schema != NULL, NULL);
854 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
855 g_return_val_if_fail (path != NULL, NULL);
857 return g_object_new (G_TYPE_SETTINGS,
864 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
870 GSettingsSchema *schema;
875 const guint32 *strinfo;
876 gsize strinfo_length;
878 const gchar *unparsed;
881 const GVariantType *type;
882 GVariant *minimum, *maximum;
883 GVariant *default_value;
887 endian_fixup (GVariant **value)
889 #if G_BYTE_ORDER == G_BIG_ENDIAN
892 tmp = g_variant_byteswap (*value);
893 g_variant_unref (*value);
899 g_settings_get_key_info (GSettingsKeyInfo *info,
907 memset (info, 0, sizeof *info);
909 iter = g_settings_schema_get_value (settings->priv->schema, key);
911 info->default_value = g_variant_iter_next_value (iter);
912 endian_fixup (&info->default_value);
913 info->type = g_variant_get_type (info->default_value);
914 info->settings = g_object_ref (settings);
915 info->key = g_intern_string (key);
917 while (g_variant_iter_next (iter, "(y*)", &code, &data))
922 /* translation requested */
923 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
927 /* enumerated types... */
928 info->is_enum = TRUE;
933 info->is_flags = TRUE;
937 /* ..., choices, aliases */
938 info->strinfo = g_variant_get_fixed_array (data,
939 &info->strinfo_length,
944 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
945 endian_fixup (&info->minimum);
946 endian_fixup (&info->maximum);
950 g_warning ("unknown schema extension '%c'", code);
954 g_variant_unref (data);
957 g_variant_iter_free (iter);
961 g_settings_free_key_info (GSettingsKeyInfo *info)
964 g_variant_unref (info->minimum);
967 g_variant_unref (info->maximum);
969 g_variant_unref (info->default_value);
970 g_object_unref (info->settings);
974 g_settings_write_to_backend (GSettingsKeyInfo *info,
980 path = g_strconcat (info->settings->priv->path, info->key, NULL);
981 success = g_settings_backend_write (info->settings->priv->backend,
989 g_settings_type_check (GSettingsKeyInfo *info,
992 g_return_val_if_fail (value != NULL, FALSE);
994 return g_variant_is_of_type (value, info->type);
998 g_settings_key_info_range_check (GSettingsKeyInfo *info,
1001 if (info->minimum == NULL && info->strinfo == NULL)
1004 if (g_variant_is_container (value))
1010 g_variant_iter_init (&iter, value);
1011 while (ok && (child = g_variant_iter_next_value (&iter)))
1013 ok = g_settings_key_info_range_check (info, child);
1014 g_variant_unref (child);
1022 return g_variant_compare (info->minimum, value) <= 0 &&
1023 g_variant_compare (value, info->maximum) <= 0;
1026 return strinfo_is_string_valid (info->strinfo,
1027 info->strinfo_length,
1028 g_variant_get_string (value, NULL));
1032 g_settings_range_fixup (GSettingsKeyInfo *info,
1035 const gchar *target;
1037 if (g_settings_key_info_range_check (info, value))
1038 return g_variant_ref (value);
1040 if (info->strinfo == NULL)
1043 if (g_variant_is_container (value))
1045 GVariantBuilder builder;
1049 g_variant_iter_init (&iter, value);
1050 g_variant_builder_init (&builder, g_variant_get_type (value));
1052 while ((child = g_variant_iter_next_value (&iter)))
1056 fixed = g_settings_range_fixup (info, child);
1057 g_variant_unref (child);
1061 g_variant_builder_clear (&builder);
1065 g_variant_builder_add_value (&builder, fixed);
1066 g_variant_unref (fixed);
1069 return g_variant_ref_sink (g_variant_builder_end (&builder));
1072 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1073 g_variant_get_string (value, NULL));
1074 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1078 g_settings_read_from_backend (GSettingsKeyInfo *info)
1084 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1085 value = g_settings_backend_read (info->settings->priv->backend,
1086 path, info->type, FALSE);
1091 fixup = g_settings_range_fixup (info, value);
1092 g_variant_unref (value);
1101 g_settings_get_translated_default (GSettingsKeyInfo *info)
1103 const gchar *translated;
1104 GError *error = NULL;
1105 const gchar *domain;
1108 if (info->lc_char == '\0')
1109 /* translation not requested for this key */
1112 domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
1114 if (info->lc_char == 't')
1115 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1117 translated = g_dgettext (domain, info->unparsed);
1119 if (translated == info->unparsed)
1120 /* the default value was not translated */
1123 /* try to parse the translation of the unparsed default */
1124 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1128 g_warning ("Failed to parse translated string `%s' for "
1129 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1130 info->settings->priv->schema_name, error->message);
1131 g_warning ("Using untranslated default instead.");
1132 g_error_free (error);
1135 else if (!g_settings_key_info_range_check (info, value))
1137 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1138 "is outside of valid range", info->unparsed, info->key,
1139 info->settings->priv->schema_name);
1140 g_variant_unref (value);
1148 g_settings_to_enum (GSettingsKeyInfo *info,
1154 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1155 g_variant_get_string (value, NULL),
1158 /* 'value' can only come from the backend after being filtered for validity,
1159 * from the translation after being filtered for validity, or from the schema
1160 * itself (which the schema compiler checks for validity). If this assertion
1161 * fails then it's really a bug in GSettings or the schema compiler...
1163 g_assert (it_worked);
1169 g_settings_from_enum (GSettingsKeyInfo *info,
1172 const gchar *string;
1174 string = strinfo_string_from_enum (info->strinfo,
1175 info->strinfo_length,
1181 return g_variant_new_string (string);
1185 g_settings_to_flags (GSettingsKeyInfo *info,
1193 g_variant_iter_init (&iter, value);
1194 while (g_variant_iter_next (&iter, "&s", &flag))
1199 it_worked = strinfo_enum_from_string (info->strinfo,
1200 info->strinfo_length,
1202 /* as in g_settings_to_enum() */
1203 g_assert (it_worked);
1205 result |= flag_value;
1212 g_settings_from_flags (GSettingsKeyInfo *info,
1215 GVariantBuilder builder;
1218 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1220 for (i = 0; i < 32; i++)
1221 if (value & (1u << i))
1223 const gchar *string;
1225 string = strinfo_string_from_enum (info->strinfo,
1226 info->strinfo_length,
1231 g_variant_builder_clear (&builder);
1235 g_variant_builder_add (&builder, "s", string);
1238 return g_variant_builder_end (&builder);
1241 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1243 * g_settings_get_value:
1244 * @settings: a #GSettings object
1245 * @key: the key to get the value for
1246 * @returns: a new #GVariant
1248 * Gets the value that is stored in @settings for @key.
1250 * It is a programmer error to give a @key that isn't contained in the
1251 * schema for @settings.
1256 g_settings_get_value (GSettings *settings,
1259 GSettingsKeyInfo info;
1262 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1263 g_return_val_if_fail (key != NULL, NULL);
1265 g_settings_get_key_info (&info, settings, key);
1266 value = g_settings_read_from_backend (&info);
1269 value = g_settings_get_translated_default (&info);
1272 value = g_variant_ref (info.default_value);
1274 g_settings_free_key_info (&info);
1280 * g_settings_get_enum:
1281 * @settings: a #GSettings object
1282 * @key: the key to get the value for
1283 * @returns: the enum value
1285 * Gets the value that is stored in @settings for @key and converts it
1286 * to the enum value that it represents.
1288 * In order to use this function the type of the value must be a string
1289 * and it must be marked in the schema file as an enumerated type.
1291 * It is a programmer error to give a @key that isn't contained in the
1292 * schema for @settings or is not marked as an enumerated type.
1294 * If the value stored in the configuration database is not a valid
1295 * value for the enumerated type then this function will return the
1301 g_settings_get_enum (GSettings *settings,
1304 GSettingsKeyInfo info;
1308 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1309 g_return_val_if_fail (key != NULL, -1);
1311 g_settings_get_key_info (&info, settings, key);
1315 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1316 "associated with an enumerated type", info.key);
1317 g_settings_free_key_info (&info);
1321 value = g_settings_read_from_backend (&info);
1324 value = g_settings_get_translated_default (&info);
1327 value = g_variant_ref (info.default_value);
1329 result = g_settings_to_enum (&info, value);
1330 g_settings_free_key_info (&info);
1331 g_variant_unref (value);
1337 * g_settings_set_enum:
1338 * @settings: a #GSettings object
1339 * @key: a key, within @settings
1340 * @value: an enumerated value
1341 * @returns: %TRUE, if the set succeeds
1343 * Looks up the enumerated type nick for @value and writes it to @key,
1346 * It is a programmer error to give a @key that isn't contained in the
1347 * schema for @settings or is not marked as an enumerated type, or for
1348 * @value not to be a valid value for the named type.
1350 * After performing the write, accessing @key directly with
1351 * g_settings_get_string() will return the 'nick' associated with
1355 g_settings_set_enum (GSettings *settings,
1359 GSettingsKeyInfo info;
1363 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1364 g_return_val_if_fail (key != NULL, FALSE);
1366 g_settings_get_key_info (&info, settings, key);
1370 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1371 "associated with an enumerated type", info.key);
1375 if (!(variant = g_settings_from_enum (&info, value)))
1377 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1378 "in schema `%s'. Doing nothing.", value, info.key,
1379 info.settings->priv->schema_name);
1380 g_settings_free_key_info (&info);
1384 success = g_settings_write_to_backend (&info, variant);
1385 g_settings_free_key_info (&info);
1391 * g_settings_get_flags:
1392 * @settings: a #GSettings object
1393 * @key: the key to get the value for
1394 * @returns: the flags value
1396 * Gets the value that is stored in @settings for @key and converts it
1397 * to the flags value that it represents.
1399 * In order to use this function the type of the value must be an array
1400 * of strings and it must be marked in the schema file as an flags type.
1402 * It is a programmer error to give a @key that isn't contained in the
1403 * schema for @settings or is not marked as a flags type.
1405 * If the value stored in the configuration database is not a valid
1406 * value for the flags type then this function will return the default
1412 g_settings_get_flags (GSettings *settings,
1415 GSettingsKeyInfo info;
1419 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1420 g_return_val_if_fail (key != NULL, -1);
1422 g_settings_get_key_info (&info, settings, key);
1426 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1427 "associated with a flags type", info.key);
1428 g_settings_free_key_info (&info);
1432 value = g_settings_read_from_backend (&info);
1435 value = g_settings_get_translated_default (&info);
1438 value = g_variant_ref (info.default_value);
1440 result = g_settings_to_flags (&info, value);
1441 g_settings_free_key_info (&info);
1442 g_variant_unref (value);
1448 * g_settings_set_flags:
1449 * @settings: a #GSettings object
1450 * @key: a key, within @settings
1451 * @value: a flags value
1452 * @returns: %TRUE, if the set succeeds
1454 * Looks up the flags type nicks for the bits specified by @value, puts
1455 * them in an array of strings and writes the array to @key, withing
1458 * It is a programmer error to give a @key that isn't contained in the
1459 * schema for @settings or is not marked as a flags type, or for @value
1460 * to contain any bits that are not value for the named type.
1462 * After performing the write, accessing @key directly with
1463 * g_settings_get_strv() will return an array of 'nicks'; one for each
1467 g_settings_set_flags (GSettings *settings,
1471 GSettingsKeyInfo info;
1475 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1476 g_return_val_if_fail (key != NULL, FALSE);
1478 g_settings_get_key_info (&info, settings, key);
1482 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1483 "associated with a flags type", info.key);
1487 if (!(variant = g_settings_from_flags (&info, value)))
1489 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1490 "for key `%s' in schema `%s'. Doing nothing.",
1491 value, info.key, info.settings->priv->schema_name);
1492 g_settings_free_key_info (&info);
1496 success = g_settings_write_to_backend (&info, variant);
1497 g_settings_free_key_info (&info);
1503 * g_settings_set_value:
1504 * @settings: a #GSettings object
1505 * @key: the name of the key to set
1506 * @value: a #GVariant of the correct type
1507 * @returns: %TRUE if setting the key succeeded,
1508 * %FALSE if the key was not writable
1510 * Sets @key in @settings to @value.
1512 * It is a programmer error to give a @key that isn't contained in the
1513 * schema for @settings or for @value to have the incorrect type, per
1516 * If @value is floating then this function consumes the reference.
1521 g_settings_set_value (GSettings *settings,
1525 GSettingsKeyInfo info;
1527 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1528 g_return_val_if_fail (key != NULL, FALSE);
1530 g_settings_get_key_info (&info, settings, key);
1532 if (!g_settings_type_check (&info, value))
1534 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1536 settings->priv->schema_name,
1537 g_variant_type_peek_string (info.type),
1538 g_variant_get_type_string (value));
1543 if (!g_settings_key_info_range_check (&info, value))
1545 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1546 "is outside of valid range",
1548 settings->priv->schema_name);
1553 g_settings_free_key_info (&info);
1555 return g_settings_write_to_backend (&info, value);
1560 * @settings: a #GSettings object
1561 * @key: the key to get the value for
1562 * @format: a #GVariant format string
1563 * @...: arguments as per @format
1565 * Gets the value that is stored at @key in @settings.
1567 * A convenience function that combines g_settings_get_value() with
1570 * It is a programmer error to give a @key that isn't contained in the
1571 * schema for @settings or for the #GVariantType of @format to mismatch
1572 * the type given in the schema.
1577 g_settings_get (GSettings *settings,
1579 const gchar *format,
1585 value = g_settings_get_value (settings, key);
1587 va_start (ap, format);
1588 g_variant_get_va (value, format, NULL, &ap);
1591 g_variant_unref (value);
1596 * @settings: a #GSettings object
1597 * @key: the name of the key to set
1598 * @format: a #GVariant format string
1599 * @...: arguments as per @format
1600 * @returns: %TRUE if setting the key succeeded,
1601 * %FALSE if the key was not writable
1603 * Sets @key in @settings to @value.
1605 * A convenience function that combines g_settings_set_value() with
1608 * It is a programmer error to give a @key that isn't contained in the
1609 * schema for @settings or for the #GVariantType of @format to mismatch
1610 * the type given in the schema.
1615 g_settings_set (GSettings *settings,
1617 const gchar *format,
1623 va_start (ap, format);
1624 value = g_variant_new_va (format, NULL, &ap);
1627 return g_settings_set_value (settings, key, value);
1631 * g_settings_get_mapped:
1632 * @settings: a #GSettings object
1633 * @key: the key to get the value for
1634 * @mapping: (scope call): the function to map the value in the
1635 * settings database to the value used by the application
1636 * @user_data: user data for @mapping
1637 * @returns: (transfer full): the result, which may be %NULL
1639 * Gets the value that is stored at @key in @settings, subject to
1640 * application-level validation/mapping.
1642 * You should use this function when the application needs to perform
1643 * some processing on the value of the key (for example, parsing). The
1644 * @mapping function performs that processing. If the function
1645 * indicates that the processing was unsuccessful (due to a parse error,
1646 * for example) then the mapping is tried again with another value.
1648 * This allows a robust 'fall back to defaults' behaviour to be
1649 * implemented somewhat automatically.
1651 * The first value that is tried is the user's setting for the key. If
1652 * the mapping function fails to map this value, other values may be
1653 * tried in an unspecified order (system or site defaults, translated
1654 * schema default values, untranslated schema default values, etc).
1656 * If the mapping function fails for all possible values, one additional
1657 * attempt is made: the mapping function is called with a %NULL value.
1658 * If the mapping function still indicates failure at this point then
1659 * the application will be aborted.
1661 * The result parameter for the @mapping function is pointed to a
1662 * #gpointer which is initially set to %NULL. The same pointer is given
1663 * to each invocation of @mapping. The final value of that #gpointer is
1664 * what is returned by this function. %NULL is valid; it is returned
1665 * just as any other value would be.
1668 g_settings_get_mapped (GSettings *settings,
1670 GSettingsGetMapping mapping,
1673 gpointer result = NULL;
1674 GSettingsKeyInfo info;
1678 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1679 g_return_val_if_fail (key != NULL, NULL);
1680 g_return_val_if_fail (mapping != NULL, NULL);
1682 g_settings_get_key_info (&info, settings, key);
1684 if ((value = g_settings_read_from_backend (&info)))
1686 okay = mapping (value, &result, user_data);
1687 g_variant_unref (value);
1688 if (okay) goto okay;
1691 if ((value = g_settings_get_translated_default (&info)))
1693 okay = mapping (value, &result, user_data);
1694 g_variant_unref (value);
1695 if (okay) goto okay;
1698 if (mapping (info.default_value, &result, user_data))
1701 if (!mapping (NULL, &result, user_data))
1702 g_error ("The mapping function given to g_settings_get_mapped() for key "
1703 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1704 key, settings->priv->schema_name);
1707 g_settings_free_key_info (&info);
1712 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1714 * g_settings_get_string:
1715 * @settings: a #GSettings object
1716 * @key: the key to get the value for
1717 * @returns: a newly-allocated string
1719 * Gets the value that is stored at @key in @settings.
1721 * A convenience variant of g_settings_get() for strings.
1723 * It is a programmer error to give a @key that isn't specified as
1724 * having a string type in the schema for @settings.
1729 g_settings_get_string (GSettings *settings,
1735 value = g_settings_get_value (settings, key);
1736 result = g_variant_dup_string (value, NULL);
1737 g_variant_unref (value);
1743 * g_settings_set_string:
1744 * @settings: a #GSettings object
1745 * @key: the name of the key to set
1746 * @value: the value to set it to
1747 * @returns: %TRUE if setting the key succeeded,
1748 * %FALSE if the key was not writable
1750 * Sets @key in @settings to @value.
1752 * A convenience variant of g_settings_set() for strings.
1754 * It is a programmer error to give a @key that isn't specified as
1755 * having a string type in the schema for @settings.
1760 g_settings_set_string (GSettings *settings,
1764 return g_settings_set_value (settings, key, g_variant_new_string (value));
1768 * g_settings_get_int:
1769 * @settings: a #GSettings object
1770 * @key: the key to get the value for
1771 * @returns: an integer
1773 * Gets the value that is stored at @key in @settings.
1775 * A convenience variant of g_settings_get() for 32-bit integers.
1777 * It is a programmer error to give a @key that isn't specified as
1778 * having a int32 type in the schema for @settings.
1783 g_settings_get_int (GSettings *settings,
1789 value = g_settings_get_value (settings, key);
1790 result = g_variant_get_int32 (value);
1791 g_variant_unref (value);
1797 * g_settings_set_int:
1798 * @settings: a #GSettings object
1799 * @key: the name of the key to set
1800 * @value: the value to set it to
1801 * @returns: %TRUE if setting the key succeeded,
1802 * %FALSE if the key was not writable
1804 * Sets @key in @settings to @value.
1806 * A convenience variant of g_settings_set() for 32-bit integers.
1808 * It is a programmer error to give a @key that isn't specified as
1809 * having a int32 type in the schema for @settings.
1814 g_settings_set_int (GSettings *settings,
1818 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1822 * g_settings_get_uint:
1823 * @settings: a #GSettings object
1824 * @key: the key to get the value for
1825 * @returns: an unsigned integer
1827 * Gets the value that is stored at @key in @settings.
1829 * A convenience variant of g_settings_get() for 32-bit unsigned
1832 * It is a programmer error to give a @key that isn't specified as
1833 * having a uint32 type in the schema for @settings.
1838 g_settings_get_uint (GSettings *settings,
1844 value = g_settings_get_value (settings, key);
1845 result = g_variant_get_uint32 (value);
1846 g_variant_unref (value);
1852 * g_settings_set_uint:
1853 * @settings: a #GSettings object
1854 * @key: the name of the key to set
1855 * @value: the value to set it to
1856 * @returns: %TRUE if setting the key succeeded,
1857 * %FALSE if the key was not writable
1859 * Sets @key in @settings to @value.
1861 * A convenience variant of g_settings_set() for 32-bit unsigned
1864 * It is a programmer error to give a @key that isn't specified as
1865 * having a uint32 type in the schema for @settings.
1870 g_settings_set_uint (GSettings *settings,
1874 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1878 * g_settings_get_double:
1879 * @settings: a #GSettings object
1880 * @key: the key to get the value for
1881 * @returns: a double
1883 * Gets the value that is stored at @key in @settings.
1885 * A convenience variant of g_settings_get() for doubles.
1887 * It is a programmer error to give a @key that isn't specified as
1888 * having a 'double' type in the schema for @settings.
1893 g_settings_get_double (GSettings *settings,
1899 value = g_settings_get_value (settings, key);
1900 result = g_variant_get_double (value);
1901 g_variant_unref (value);
1907 * g_settings_set_double:
1908 * @settings: a #GSettings object
1909 * @key: the name of the key to set
1910 * @value: the value to set it to
1911 * @returns: %TRUE if setting the key succeeded,
1912 * %FALSE if the key was not writable
1914 * Sets @key in @settings to @value.
1916 * A convenience variant of g_settings_set() for doubles.
1918 * It is a programmer error to give a @key that isn't specified as
1919 * having a 'double' type in the schema for @settings.
1924 g_settings_set_double (GSettings *settings,
1928 return g_settings_set_value (settings, key, g_variant_new_double (value));
1932 * g_settings_get_boolean:
1933 * @settings: a #GSettings object
1934 * @key: the key to get the value for
1935 * @returns: a boolean
1937 * Gets the value that is stored at @key in @settings.
1939 * A convenience variant of g_settings_get() for booleans.
1941 * It is a programmer error to give a @key that isn't specified as
1942 * having a boolean type in the schema for @settings.
1947 g_settings_get_boolean (GSettings *settings,
1953 value = g_settings_get_value (settings, key);
1954 result = g_variant_get_boolean (value);
1955 g_variant_unref (value);
1961 * g_settings_set_boolean:
1962 * @settings: a #GSettings object
1963 * @key: the name of the key to set
1964 * @value: the value to set it to
1965 * @returns: %TRUE if setting the key succeeded,
1966 * %FALSE if the key was not writable
1968 * Sets @key in @settings to @value.
1970 * A convenience variant of g_settings_set() for booleans.
1972 * It is a programmer error to give a @key that isn't specified as
1973 * having a boolean type in the schema for @settings.
1978 g_settings_set_boolean (GSettings *settings,
1982 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1986 * g_settings_get_strv:
1987 * @settings: a #GSettings object
1988 * @key: the key to get the value for
1989 * @returns: (array zero-terminated=1) (transfer full): a
1990 * newly-allocated, %NULL-terminated array of strings, the value that
1991 * is stored at @key in @settings.
1993 * A convenience variant of g_settings_get() for string arrays.
1995 * It is a programmer error to give a @key that isn't specified as
1996 * having an array of strings type in the schema for @settings.
2001 g_settings_get_strv (GSettings *settings,
2007 value = g_settings_get_value (settings, key);
2008 result = g_variant_dup_strv (value, NULL);
2009 g_variant_unref (value);
2015 * g_settings_set_strv:
2016 * @settings: a #GSettings object
2017 * @key: the name of the key to set
2018 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
2019 * @returns: %TRUE if setting the key succeeded,
2020 * %FALSE if the key was not writable
2022 * Sets @key in @settings to @value.
2024 * A convenience variant of g_settings_set() for string arrays. If
2025 * @value is %NULL, then @key is set to be the empty array.
2027 * It is a programmer error to give a @key that isn't specified as
2028 * having an array of strings type in the schema for @settings.
2033 g_settings_set_strv (GSettings *settings,
2035 const gchar * const *value)
2040 array = g_variant_new_strv (value, -1);
2042 array = g_variant_new_strv (NULL, 0);
2044 return g_settings_set_value (settings, key, array);
2047 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2050 * @settings: a #GSettings object
2052 * Changes the #GSettings object into 'delay-apply' mode. In this
2053 * mode, changes to @settings are not immediately propagated to the
2054 * backend, but kept locally until g_settings_apply() is called.
2059 g_settings_delay (GSettings *settings)
2061 g_return_if_fail (G_IS_SETTINGS (settings));
2063 if (settings->priv->delayed)
2066 settings->priv->delayed =
2067 g_delayed_settings_backend_new (settings->priv->backend,
2069 settings->priv->main_context);
2070 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2071 g_object_unref (settings->priv->backend);
2073 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2074 g_settings_backend_watch (settings->priv->backend,
2075 &listener_vtable, G_OBJECT (settings),
2076 settings->priv->main_context);
2078 g_object_notify (G_OBJECT (settings), "delay-apply");
2083 * @settings: a #GSettings instance
2085 * Applies any changes that have been made to the settings. This
2086 * function does nothing unless @settings is in 'delay-apply' mode;
2087 * see g_settings_delay(). In the normal case settings are always
2088 * applied immediately.
2091 g_settings_apply (GSettings *settings)
2093 if (settings->priv->delayed)
2095 GDelayedSettingsBackend *delayed;
2097 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2098 g_delayed_settings_backend_apply (delayed);
2103 * g_settings_revert:
2104 * @settings: a #GSettings instance
2106 * Reverts all non-applied changes to the settings. This function
2107 * does nothing unless @settings is in 'delay-apply' mode; see
2108 * g_settings_delay(). In the normal case settings are always applied
2111 * Change notifications will be emitted for affected keys.
2114 g_settings_revert (GSettings *settings)
2116 if (settings->priv->delayed)
2118 GDelayedSettingsBackend *delayed;
2120 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2121 g_delayed_settings_backend_revert (delayed);
2126 * g_settings_get_has_unapplied:
2127 * @settings: a #GSettings object
2128 * @returns: %TRUE if @settings has unapplied changes
2130 * Returns whether the #GSettings object has any unapplied
2131 * changes. This can only be the case if it is in 'delayed-apply' mode.
2136 g_settings_get_has_unapplied (GSettings *settings)
2138 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2140 return settings->priv->delayed &&
2141 g_delayed_settings_backend_get_has_unapplied (
2142 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2145 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2148 * @settings: a #GSettings object
2149 * @key: the name of a key
2151 * Resets @key to its default value.
2153 * This call resets the key, as much as possible, to its default value.
2154 * That might the value specified in the schema or the one set by the
2158 g_settings_reset (GSettings *settings,
2163 path = g_strconcat (settings->priv->path, key, NULL);
2164 g_settings_backend_reset (settings->priv->backend, path, NULL);
2171 * Ensures that all pending operations for the given are complete for
2172 * the default backend.
2174 * Writes made to a #GSettings are handled asynchronously. For this
2175 * reason, it is very unlikely that the changes have it to disk by the
2176 * time g_settings_set() returns.
2178 * This call will block until all of the writes have made it to the
2179 * backend. Since the mainloop is not running, no change notifications
2180 * will be dispatched during this call (but some may be queued by the
2181 * time the call is done).
2184 g_settings_sync (void)
2186 g_settings_backend_sync_default ();
2190 * g_settings_is_writable:
2191 * @settings: a #GSettings object
2192 * @name: the name of a key
2193 * @returns: %TRUE if the key @name is writable
2195 * Finds out if a key can be written or not
2200 g_settings_is_writable (GSettings *settings,
2206 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2208 path = g_strconcat (settings->priv->path, name, NULL);
2209 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2216 * g_settings_get_child:
2217 * @settings: a #GSettings object
2218 * @name: the name of the 'child' schema
2219 * @returns: (transfer full): a 'child' settings object
2221 * Creates a 'child' settings object which has a base path of
2222 * <replaceable>base-path</replaceable>/@name, where
2223 * <replaceable>base-path</replaceable> is the base path of @settings.
2225 * The schema for the child settings object must have been declared
2226 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2231 g_settings_get_child (GSettings *settings,
2234 const gchar *child_schema;
2239 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2241 child_name = g_strconcat (name, "/", NULL);
2242 child_schema = g_settings_schema_get_string (settings->priv->schema,
2244 if (child_schema == NULL)
2245 g_error ("Schema '%s' has no child '%s'",
2246 settings->priv->schema_name, name);
2248 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2249 child = g_object_new (G_TYPE_SETTINGS,
2250 "schema", child_schema,
2253 g_free (child_path);
2254 g_free (child_name);
2260 * g_settings_list_keys:
2261 * @settings: a #GSettings object
2262 * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2264 * Introspects the list of keys on @settings.
2266 * You should probably not be calling this function from "normal" code
2267 * (since you should already know what keys are in your schema). This
2268 * function is intended for introspection reasons.
2270 * You should free the return value with g_strfreev() when you are done
2274 g_settings_list_keys (GSettings *settings)
2281 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2282 strv = g_new (gchar *, n_keys + 1);
2283 for (i = j = 0; i < n_keys; i++)
2285 const gchar *key = g_quark_to_string (keys[i]);
2287 if (!g_str_has_suffix (key, "/"))
2288 strv[j++] = g_strdup (key);
2296 * g_settings_list_children:
2297 * @settings: a #GSettings object
2298 * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2300 * Gets the list of children on @settings.
2302 * The list is exactly the list of strings for which it is not an error
2303 * to call g_settings_get_child().
2305 * For GSettings objects that are lists, this value can change at any
2306 * time and you should connect to the "children-changed" signal to watch
2307 * for those changes. Note that there is a race condition here: you may
2308 * request a child after listing it only for it to have been destroyed
2309 * in the meantime. For this reason, g_settings_get_child() may return
2310 * %NULL even for a child that was listed by this function.
2312 * For GSettings objects that are not lists, you should probably not be
2313 * calling this function from "normal" code (since you should already
2314 * know what children are in your schema). This function may still be
2315 * useful there for introspection reasons, however.
2317 * You should free the return value with g_strfreev() when you are done
2321 g_settings_list_children (GSettings *settings)
2328 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2329 strv = g_new (gchar *, n_keys + 1);
2330 for (i = j = 0; i < n_keys; i++)
2332 const gchar *key = g_quark_to_string (keys[i]);
2334 if (g_str_has_suffix (key, "/"))
2336 gint length = strlen (key);
2338 strv[j] = g_memdup (key, length);
2339 strv[j][length - 1] = '\0';
2349 * g_settings_get_range:
2350 * @settings: a #GSettings
2351 * @key: the key to query the range of
2352 * @returns: a #GVariant describing the range
2354 * Queries the range of a key.
2356 * This function will return a #GVariant that fully describes the range
2357 * of values that are valid for @key.
2359 * The type of #GVariant returned is <literal>(sv)</literal>. The
2360 * string describes the type of range restriction in effect. The type
2361 * and meaning of the value contained in the variant depends on the
2364 * If the string is <literal>'type'</literal> then the variant contains
2365 * an empty array. The element type of that empty array is the expected
2366 * type of value and all values of that type are valid.
2368 * If the string is <literal>'enum'</literal> then the variant contains
2369 * an array enumerating the possible values. Each item in the array is
2370 * a possible valid value and no other values are valid.
2372 * If the string is <literal>'flags'</literal> then the variant contains
2373 * an array. Each item in the array is a value that may appear zero or
2374 * one times in an array to be used as the value for this key. For
2375 * example, if the variant contained the array <literal>['x',
2376 * 'y']</literal> then the valid values for the key would be
2377 * <literal>[]</literal>, <literal>['x']</literal>,
2378 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2379 * <literal>['y', 'x']</literal>.
2381 * Finally, if the string is <literal>'range'</literal> then the variant
2382 * contains a pair of like-typed values -- the minimum and maximum
2383 * permissible values for this key.
2385 * This information should not be used by normal programs. It is
2386 * considered to be a hint for introspection purposes. Normal programs
2387 * should already know what is permitted by their own schema. The
2388 * format may change in any way in the future -- but particularly, new
2389 * forms may be added to the possibilities described above.
2391 * It is a programmer error to give a @key that isn't contained in the
2392 * schema for @settings.
2394 * You should free the returned value with g_variant_unref() when it is
2400 g_settings_get_range (GSettings *settings,
2403 GSettingsKeyInfo info;
2407 g_settings_get_key_info (&info, settings, key);
2411 range = g_variant_new ("(**)", info.minimum, info.maximum);
2414 else if (info.strinfo)
2416 range = strinfo_enumerate (info.strinfo, info.strinfo_length);
2417 type = info.is_flags ? "flags" : "enum";
2421 range = g_variant_new_array (info.type, NULL, 0);
2425 g_settings_free_key_info (&info);
2427 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2431 * g_settings_range_check:
2432 * @settings: a #GSettings
2433 * @key: the key to check
2434 * @value: the value to check
2435 * @returns: %TRUE if @value is valid for @key
2437 * Checks if the given @value is of the correct type and within the
2438 * permitted range for @key.
2440 * This API is not intended to be used by normal programs -- they should
2441 * already know what is permitted by their own schemas. This API is
2442 * meant to be used by programs such as editors or commandline tools.
2444 * It is a programmer error to give a @key that isn't contained in the
2445 * schema for @settings.
2450 g_settings_range_check (GSettings *settings,
2454 GSettingsKeyInfo info;
2457 g_settings_get_key_info (&info, settings, key);
2458 good = g_settings_type_check (&info, value) &&
2459 g_settings_key_info_range_check (&info, value);
2460 g_settings_free_key_info (&info);
2468 GSettingsKeyInfo info;
2471 GSettingsBindGetMapping get_mapping;
2472 GSettingsBindSetMapping set_mapping;
2474 GDestroyNotify destroy;
2476 guint writable_handler_id;
2477 guint property_handler_id;
2478 const GParamSpec *property;
2479 guint key_handler_id;
2481 /* prevent recursion */
2486 g_settings_binding_free (gpointer data)
2488 GSettingsBinding *binding = data;
2490 g_assert (!binding->running);
2492 if (binding->writable_handler_id)
2493 g_signal_handler_disconnect (binding->info.settings,
2494 binding->writable_handler_id);
2496 if (binding->key_handler_id)
2497 g_signal_handler_disconnect (binding->info.settings,
2498 binding->key_handler_id);
2500 if (g_signal_handler_is_connected (binding->object,
2501 binding->property_handler_id))
2502 g_signal_handler_disconnect (binding->object,
2503 binding->property_handler_id);
2505 g_settings_free_key_info (&binding->info);
2507 if (binding->destroy)
2508 binding->destroy (binding->user_data);
2510 g_slice_free (GSettingsBinding, binding);
2514 g_settings_binding_quark (const char *property)
2519 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2520 quark = g_quark_from_string (tmp);
2527 g_settings_binding_key_changed (GSettings *settings,
2531 GSettingsBinding *binding = user_data;
2532 GValue value = { 0, };
2535 g_assert (settings == binding->info.settings);
2536 g_assert (key == binding->info.key);
2538 if (binding->running)
2541 binding->running = TRUE;
2543 g_value_init (&value, binding->property->value_type);
2545 variant = g_settings_read_from_backend (&binding->info);
2546 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2548 /* silently ignore errors in the user's config database */
2549 g_variant_unref (variant);
2553 if (variant == NULL)
2555 variant = g_settings_get_translated_default (&binding->info);
2557 !binding->get_mapping (&value, variant, binding->user_data))
2559 /* flag translation errors with a warning */
2560 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2561 "was rejected by the binding mapping function",
2562 binding->info.unparsed, binding->info.key,
2563 binding->info.settings->priv->schema_name);
2564 g_variant_unref (variant);
2569 if (variant == NULL)
2571 variant = g_variant_ref (binding->info.default_value);
2572 if (!binding->get_mapping (&value, variant, binding->user_data))
2573 g_error ("The schema default value for key `%s' in schema `%s' "
2574 "was rejected by the binding mapping function.",
2576 binding->info.settings->priv->schema_name);
2579 g_object_set_property (binding->object, binding->property->name, &value);
2580 g_variant_unref (variant);
2581 g_value_unset (&value);
2583 binding->running = FALSE;
2587 g_settings_binding_property_changed (GObject *object,
2588 const GParamSpec *pspec,
2591 GSettingsBinding *binding = user_data;
2592 GValue value = { 0, };
2595 g_assert (object == binding->object);
2596 g_assert (pspec == binding->property);
2598 if (binding->running)
2601 binding->running = TRUE;
2603 g_value_init (&value, pspec->value_type);
2604 g_object_get_property (object, pspec->name, &value);
2605 if ((variant = binding->set_mapping (&value, binding->info.type,
2606 binding->user_data)))
2608 if (g_variant_is_floating (variant))
2609 g_variant_ref_sink (variant);
2611 if (!g_settings_type_check (&binding->info, variant))
2613 g_critical ("binding mapping function for key `%s' returned "
2614 "GVariant of type `%s' when type `%s' was requested",
2615 binding->info.key, g_variant_get_type_string (variant),
2616 g_variant_type_dup_string (binding->info.type));
2620 if (!g_settings_key_info_range_check (&binding->info, variant))
2622 g_critical ("GObject property `%s' on a `%s' object is out of "
2623 "schema-specified range for key `%s' of `%s': %s",
2624 binding->property->name,
2625 g_type_name (binding->property->owner_type),
2627 binding->info.settings->priv->schema_name,
2628 g_variant_print (variant, TRUE));
2632 g_settings_write_to_backend (&binding->info, variant);
2633 g_variant_unref (variant);
2635 g_value_unset (&value);
2637 binding->running = FALSE;
2641 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2645 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2650 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2651 const GVariantType *expected_type,
2654 return g_variant_new_boolean (!g_value_get_boolean (value));
2659 * @settings: a #GSettings object
2660 * @key: the key to bind
2661 * @object: (type GObject.Object): a #GObject
2662 * @property: the name of the property to bind
2663 * @flags: flags for the binding
2665 * Create a binding between the @key in the @settings object
2666 * and the property @property of @object.
2668 * The binding uses the default GIO mapping functions to map
2669 * between the settings and property values. These functions
2670 * handle booleans, numeric types and string types in a
2671 * straightforward way. Use g_settings_bind_with_mapping() if
2672 * you need a custom mapping, or map between types that are not
2673 * supported by the default mapping functions.
2675 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2676 * function also establishes a binding between the writability of
2677 * @key and the "sensitive" property of @object (if @object has
2678 * a boolean property by that name). See g_settings_bind_writable()
2679 * for more details about writable bindings.
2681 * Note that the lifecycle of the binding is tied to the object,
2682 * and that you can have only one binding per object property.
2683 * If you bind the same property twice on the same object, the second
2684 * binding overrides the first one.
2689 g_settings_bind (GSettings *settings,
2692 const gchar *property,
2693 GSettingsBindFlags flags)
2695 GSettingsBindGetMapping get_mapping = NULL;
2696 GSettingsBindSetMapping set_mapping = NULL;
2698 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2700 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2701 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2703 /* can't pass this flag to g_settings_bind_with_mapping() */
2704 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2707 g_settings_bind_with_mapping (settings, key, object, property, flags,
2708 get_mapping, set_mapping, NULL, NULL);
2712 * g_settings_bind_with_mapping: (skip)
2713 * @settings: a #GSettings object
2714 * @key: the key to bind
2715 * @object: (type GObject.Object): a #GObject
2716 * @property: the name of the property to bind
2717 * @flags: flags for the binding
2718 * @get_mapping: a function that gets called to convert values
2719 * from @settings to @object, or %NULL to use the default GIO mapping
2720 * @set_mapping: a function that gets called to convert values
2721 * from @object to @settings, or %NULL to use the default GIO mapping
2722 * @user_data: data that gets passed to @get_mapping and @set_mapping
2723 * @destroy: #GDestroyNotify function for @user_data
2725 * Create a binding between the @key in the @settings object
2726 * and the property @property of @object.
2728 * The binding uses the provided mapping functions to map between
2729 * settings and property values.
2731 * Note that the lifecycle of the binding is tied to the object,
2732 * and that you can have only one binding per object property.
2733 * If you bind the same property twice on the same object, the second
2734 * binding overrides the first one.
2739 g_settings_bind_with_mapping (GSettings *settings,
2742 const gchar *property,
2743 GSettingsBindFlags flags,
2744 GSettingsBindGetMapping get_mapping,
2745 GSettingsBindSetMapping set_mapping,
2747 GDestroyNotify destroy)
2749 GSettingsBinding *binding;
2750 GObjectClass *objectclass;
2751 gchar *detailed_signal;
2752 GQuark binding_quark;
2754 g_return_if_fail (G_IS_SETTINGS (settings));
2755 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2757 objectclass = G_OBJECT_GET_CLASS (object);
2759 binding = g_slice_new0 (GSettingsBinding);
2760 g_settings_get_key_info (&binding->info, settings, key);
2761 binding->object = object;
2762 binding->property = g_object_class_find_property (objectclass, property);
2763 binding->user_data = user_data;
2764 binding->destroy = destroy;
2765 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2766 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2768 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2769 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2771 if (binding->property == NULL)
2773 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2774 property, G_OBJECT_TYPE_NAME (object));
2778 if ((flags & G_SETTINGS_BIND_GET) &&
2779 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2781 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2782 "writable", property, G_OBJECT_TYPE_NAME (object));
2785 if ((flags & G_SETTINGS_BIND_SET) &&
2786 (binding->property->flags & G_PARAM_READABLE) == 0)
2788 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2789 "readable", property, G_OBJECT_TYPE_NAME (object));
2793 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2795 /* g_settings_bind_invert_boolean_get_mapping() is a private
2796 * function, so if we are here it means that g_settings_bind() was
2797 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2799 * Ensure that both sides are boolean.
2802 if (binding->property->value_type != G_TYPE_BOOLEAN)
2804 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2805 "was specified, but property `%s' on type `%s' has "
2806 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2807 g_type_name ((binding->property->value_type)));
2811 if (!g_variant_type_equal (binding->info.type, G_VARIANT_TYPE_BOOLEAN))
2813 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2814 "was specified, but key `%s' on schema `%s' has "
2815 "type `%s'", key, settings->priv->schema_name,
2816 g_variant_type_dup_string (binding->info.type));
2822 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2823 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2824 !g_settings_mapping_is_compatible (binding->property->value_type,
2825 binding->info.type))
2827 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2828 "'%s' which is not compatible with type '%s' of key '%s' "
2829 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2830 g_type_name (binding->property->value_type),
2831 g_variant_type_dup_string (binding->info.type), key,
2832 settings->priv->schema_name);
2836 if ((flags & G_SETTINGS_BIND_SET) &&
2837 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2839 GParamSpec *sensitive;
2841 sensitive = g_object_class_find_property (objectclass, "sensitive");
2843 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2844 (sensitive->flags & G_PARAM_WRITABLE))
2845 g_settings_bind_writable (settings, binding->info.key,
2846 object, "sensitive", FALSE);
2849 if (flags & G_SETTINGS_BIND_SET)
2851 detailed_signal = g_strdup_printf ("notify::%s", property);
2852 binding->property_handler_id =
2853 g_signal_connect (object, detailed_signal,
2854 G_CALLBACK (g_settings_binding_property_changed),
2856 g_free (detailed_signal);
2858 if (~flags & G_SETTINGS_BIND_GET)
2859 g_settings_binding_property_changed (object,
2864 if (flags & G_SETTINGS_BIND_GET)
2866 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2868 detailed_signal = g_strdup_printf ("changed::%s", key);
2869 binding->key_handler_id =
2870 g_signal_connect (settings, detailed_signal,
2871 G_CALLBACK (g_settings_binding_key_changed),
2873 g_free (detailed_signal);
2876 g_settings_binding_key_changed (settings, binding->info.key, binding);
2879 binding_quark = g_settings_binding_quark (property);
2880 g_object_set_qdata_full (object, binding_quark,
2881 binding, g_settings_binding_free);
2884 /* Writability binding {{{1 */
2887 GSettings *settings;
2890 const gchar *property;
2893 } GSettingsWritableBinding;
2896 g_settings_writable_binding_free (gpointer data)
2898 GSettingsWritableBinding *binding = data;
2900 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2901 g_object_unref (binding->settings);
2902 g_slice_free (GSettingsWritableBinding, binding);
2906 g_settings_binding_writable_changed (GSettings *settings,
2910 GSettingsWritableBinding *binding = user_data;
2913 g_assert (settings == binding->settings);
2914 g_assert (key == binding->key);
2916 writable = g_settings_is_writable (settings, key);
2918 if (binding->inverted)
2919 writable = !writable;
2921 g_object_set (binding->object, binding->property, writable, NULL);
2925 * g_settings_bind_writable:
2926 * @settings: a #GSettings object
2927 * @key: the key to bind
2928 * @object: (type GObject.Object):a #GObject
2929 * @property: the name of a boolean property to bind
2930 * @inverted: whether to 'invert' the value
2932 * Create a binding between the writability of @key in the
2933 * @settings object and the property @property of @object.
2934 * The property must be boolean; "sensitive" or "visible"
2935 * properties of widgets are the most likely candidates.
2937 * Writable bindings are always uni-directional; changes of the
2938 * writability of the setting will be propagated to the object
2939 * property, not the other way.
2941 * When the @inverted argument is %TRUE, the binding inverts the
2942 * value as it passes from the setting to the object, i.e. @property
2943 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2946 * Note that the lifecycle of the binding is tied to the object,
2947 * and that you can have only one binding per object property.
2948 * If you bind the same property twice on the same object, the second
2949 * binding overrides the first one.
2954 g_settings_bind_writable (GSettings *settings,
2957 const gchar *property,
2960 GSettingsWritableBinding *binding;
2961 gchar *detailed_signal;
2964 g_return_if_fail (G_IS_SETTINGS (settings));
2966 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2969 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2970 property, G_OBJECT_TYPE_NAME (object));
2973 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2975 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2976 property, G_OBJECT_TYPE_NAME (object));
2980 binding = g_slice_new (GSettingsWritableBinding);
2981 binding->settings = g_object_ref (settings);
2982 binding->object = object;
2983 binding->key = g_intern_string (key);
2984 binding->property = g_intern_string (property);
2985 binding->inverted = inverted;
2987 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2988 binding->handler_id =
2989 g_signal_connect (settings, detailed_signal,
2990 G_CALLBACK (g_settings_binding_writable_changed),
2992 g_free (detailed_signal);
2994 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2995 binding, g_settings_writable_binding_free);
2997 g_settings_binding_writable_changed (settings, binding->key, binding);
3001 * g_settings_unbind:
3002 * @object: the object
3003 * @property: the property whose binding is removed
3005 * Removes an existing binding for @property on @object.
3007 * Note that bindings are automatically removed when the
3008 * object is finalized, so it is rarely necessary to call this
3014 g_settings_unbind (gpointer object,
3015 const gchar *property)
3017 GQuark binding_quark;
3019 binding_quark = g_settings_binding_quark (property);
3020 g_object_set_qdata (object, binding_quark, NULL);
3025 /* vim:set foldmethod=marker: */