2 * Copyright © 2009, 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "gsettings.h"
32 #include "gdelayedsettingsbackend.h"
33 #include "gsettingsbackendinternal.h"
34 #include "gsettings-mapping.h"
35 #include "gio-marshal.h"
36 #include "gsettingsschema.h"
45 * @short_description: High-level API for application settings
47 * The #GSettings class provides a convenient API for storing and retrieving
48 * application settings.
50 * Reads and writes can be considered to be non-blocking. Reading
51 * settings with #GSettings is typically extremely fast: on
52 * approximately the same order of magnitude (but slower than) a
53 * #GHashTable lookup. Writing settings is also extremely fast in terms
54 * of time to return to your application, but can be extremely expensive
55 * for other threads and other processes. Many settings backends
56 * (including dconf) have lazy initialisation which means in the common
57 * case of the user using their computer without modifying any settings
58 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
59 * even need to be started in this case. For this reason, you should
60 * only ever modify #GSettings keys in response to explicit user action.
61 * Particular care should be paid to ensure that modifications are not
62 * made during startup -- for example, when settings the initial value
63 * of preferences widgets. The built-in g_settings_bind() functionality
64 * is careful not to write settings in response to notify signals as a
65 * result of modifications that it makes to widgets.
67 * When creating a GSettings instance, you have to specify a schema
68 * that describes the keys in your settings and their types and default
69 * values, as well as some other information.
71 * Normally, a schema has as fixed path that determines where the settings
72 * are stored in the conceptual global tree of settings. However, schemas
73 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
74 * useful e.g. when the schema describes an 'account', and you want to be
75 * able to store a arbitrary number of accounts.
77 * Unlike other configuration systems (like GConf), GSettings does not
78 * restrict keys to basic types like strings and numbers. GSettings stores
79 * values as #GVariant, and allows any #GVariantType for keys. Key names
80 * are restricted to lowercase characters, numbers and '-'. Furthermore,
81 * the names must begin with a lowercase character, must not end
82 * with a '-', and must not contain consecutive dashes. Key names can
83 * be up to 32 characters long.
85 * Similar to GConf, the default values in GSettings schemas can be
86 * localized, but the localized values are stored in gettext catalogs
87 * and looked up with the domain that is specified in the
88 * <tag class="attribute">gettext-domain</tag> attribute of the
89 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
90 * elements and the category that is specified in the l10n attribute of the
91 * <tag class="starttag">key</tag> element.
93 * GSettings uses schemas in a compact binary form that is created
94 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
95 * utility. The input is a schema description in an XML format that can be
96 * described by the following DTD:
97 * |[<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>]|
99 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
101 * At runtime, schemas are identified by their id (as specified
102 * in the <tag class="attribute">id</tag> attribute of the
103 * <tag class="starttag">schema</tag> element). The
104 * convention for schema ids is to use a dotted name, similar in
105 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
106 * if the settings are for a specific service that owns a D-Bus bus name,
107 * the D-Bus bus name and schema id should match. For schemas which deal
108 * with settings not associated with one named application, the id should
109 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
111 * In addition to #GVariant types, keys can have types that have enumerated
112 * types. These can be described by a <tag class="starttag">choice</tag>,
113 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
114 * <xref linkend="schema-enumerated"/>. The underlying type of
115 * such a key is string, but you can use g_settings_get_enum(),
116 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
117 * access the numeric values corresponding to the string value of enum
120 * <example id="schema-default-values"><title>Default values</title>
121 * <programlisting><![CDATA[
123 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
125 * <key name="greeting" type="s">
126 * <default l10n="messages">"Hello, earthlings"</default>
127 * <summary>A greeting</summary>
129 * Greeting of the invading martians
133 * <key name="box" type="(ii)">
134 * <default>(20,30)</default>
139 * ]]></programlisting></example>
141 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
142 * <programlisting><![CDATA[
146 * <value nick="first" value="1"/>
147 * <value nick="second" value="2"/>
150 * <enum id="myflags">
151 * <value nick="flag1" value="1"/>
152 * <value nick="flag2" value="2"/>
153 * <value nick="flag3" value="4"/>
156 * <schema id="org.gtk.Test">
158 * <key name="key-with-range" type="i">
159 * <range min="1" max="100"/>
160 * <default>10</default>
163 * <key name="key-with-choices" type="s">
165 * <choice value='Elisabeth'/>
166 * <choice value='Annabeth'/>
167 * <choice value='Joe'/>
170 * <alias value='Anna' target='Annabeth'/>
171 * <alias value='Beth' target='Elisabeth'/>
173 * <default>'Joe'</default>
176 * <key name='enumerated-key' enum='myenum'>
177 * <default>'first'</default>
180 * <key name='flags-key' flags='myflags'>
181 * <default>["flag1",flag2"]</default>
185 * ]]></programlisting></example>
188 * <title>Vendor overrides</title>
190 * Default values are defined in the schemas that get installed by
191 * an application. Sometimes, it is necessary for a vendor or distributor
192 * to adjust these defaults. Since patching the XML source for the schema
193 * is inconvenient and error-prone,
194 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
195 * so-called 'vendor override' files. These are keyfiles in the same
196 * directory as the XML schema sources which can override default values.
197 * The schema id serves as the group name in the key file, and the values
198 * are expected in serialized GVariant form, as in the following example:
199 * <informalexample><programlisting>
203 * </programlisting></informalexample>
206 * glib-compile-schemas expects schema files to have the extension
207 * <filename>.gschema.override</filename>
212 * <title>Binding</title>
214 * A very convenient feature of GSettings lets you bind #GObject properties
215 * directly to settings, using g_settings_bind(). Once a GObject property
216 * has been bound to a setting, changes on either side are automatically
217 * propagated to the other side. GSettings handles details like
218 * mapping between GObject and GVariant types, and preventing infinite
222 * This makes it very easy to hook up a preferences dialog to the
223 * underlying settings. To make this even more convenient, GSettings
224 * looks for a boolean property with the name "sensitivity" and
225 * automatically binds it to the writability of the bound setting.
226 * If this 'magic' gets in the way, it can be suppressed with the
227 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
232 struct _GSettingsPrivate
234 /* where the signals go... */
235 GMainContext *main_context;
237 GSettingsBackend *backend;
238 GSettingsSchema *schema;
242 GDelayedSettingsBackend *delayed;
257 SIGNAL_WRITABLE_CHANGE_EVENT,
258 SIGNAL_WRITABLE_CHANGED,
264 static guint g_settings_signals[N_SIGNALS];
266 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
270 g_settings_real_change_event (GSettings *settings,
277 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
279 for (i = 0; i < n_keys; i++)
280 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
281 keys[i], g_quark_to_string (keys[i]));
287 g_settings_real_writable_change_event (GSettings *settings,
290 const GQuark *keys = &key;
295 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
297 for (i = 0; i < n_keys; i++)
298 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
299 keys[i], g_quark_to_string (keys[i]));
305 settings_backend_changed (GObject *target,
306 GSettingsBackend *backend,
310 GSettings *settings = G_SETTINGS (target);
311 gboolean ignore_this;
314 /* We used to assert here:
316 * settings->priv->backend == backend
318 * but it could be the case that a notification is queued for delivery
319 * while someone calls g_settings_delay() (which changes the backend).
321 * Since the delay backend would just pass that straight through
322 * anyway, it doesn't make sense to try to detect this case.
323 * Therefore, we just accept it.
326 for (i = 0; key[i] == settings->priv->path[i]; i++);
328 if (settings->priv->path[i] == '\0' &&
329 g_settings_schema_has_key (settings->priv->schema, key + i))
333 quark = g_quark_from_string (key + i);
334 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
335 0, &quark, 1, &ignore_this);
340 settings_backend_path_changed (GObject *target,
341 GSettingsBackend *backend,
345 GSettings *settings = G_SETTINGS (target);
346 gboolean ignore_this;
348 if (g_str_has_prefix (settings->priv->path, path))
349 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
350 0, NULL, 0, &ignore_this);
354 settings_backend_keys_changed (GObject *target,
355 GSettingsBackend *backend,
357 const gchar * const *items,
360 GSettings *settings = G_SETTINGS (target);
361 gboolean ignore_this;
364 for (i = 0; settings->priv->path[i] &&
365 settings->priv->path[i] == path[i]; i++);
372 for (j = 0; items[j]; j++)
374 const gchar *item = items[j];
377 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
379 if (settings->priv->path[i + k] == '\0' &&
380 g_settings_schema_has_key (settings->priv->schema, item + k))
381 quarks[l++] = g_quark_from_string (item + k);
383 /* "256 quarks ought to be enough for anybody!"
384 * If this bites you, I'm sorry. Please file a bug.
390 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
391 0, quarks, l, &ignore_this);
396 settings_backend_writable_changed (GObject *target,
397 GSettingsBackend *backend,
400 GSettings *settings = G_SETTINGS (target);
401 gboolean ignore_this;
404 for (i = 0; key[i] == settings->priv->path[i]; i++);
406 if (settings->priv->path[i] == '\0' &&
407 g_settings_schema_has_key (settings->priv->schema, key + i))
408 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
409 0, g_quark_from_string (key + i), &ignore_this);
413 settings_backend_path_writable_changed (GObject *target,
414 GSettingsBackend *backend,
417 GSettings *settings = G_SETTINGS (target);
418 gboolean ignore_this;
420 if (g_str_has_prefix (settings->priv->path, path))
421 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
422 0, (GQuark) 0, &ignore_this);
425 /* Properties, Construction, Destruction {{{1 */
427 g_settings_set_property (GObject *object,
432 GSettings *settings = G_SETTINGS (object);
437 g_assert (settings->priv->schema_name == NULL);
438 settings->priv->schema_name = g_value_dup_string (value);
442 settings->priv->path = g_value_dup_string (value);
446 settings->priv->backend = g_value_dup_object (value);
450 g_assert_not_reached ();
455 g_settings_get_property (GObject *object,
460 GSettings *settings = G_SETTINGS (object);
465 g_value_set_string (value, settings->priv->schema_name);
469 g_value_set_object (value, settings->priv->backend);
473 g_value_set_string (value, settings->priv->path);
476 case PROP_HAS_UNAPPLIED:
477 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
480 case PROP_DELAY_APPLY:
481 g_value_set_boolean (value, settings->priv->delayed != NULL);
485 g_assert_not_reached ();
489 static const GSettingsListenerVTable listener_vtable = {
490 settings_backend_changed,
491 settings_backend_path_changed,
492 settings_backend_keys_changed,
493 settings_backend_writable_changed,
494 settings_backend_path_writable_changed
498 g_settings_constructed (GObject *object)
500 GSettings *settings = G_SETTINGS (object);
501 const gchar *schema_path;
503 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
504 schema_path = g_settings_schema_get_path (settings->priv->schema);
506 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
507 g_error ("settings object created with schema '%s' and path '%s', but "
508 "path '%s' is specified by schema",
509 settings->priv->schema_name, settings->priv->path, schema_path);
511 if (settings->priv->path == NULL)
513 if (schema_path == NULL)
514 g_error ("attempting to create schema '%s' without a path",
515 settings->priv->schema_name);
517 settings->priv->path = g_strdup (schema_path);
520 if (settings->priv->backend == NULL)
521 settings->priv->backend = g_settings_backend_get_default ();
523 g_settings_backend_watch (settings->priv->backend,
524 &listener_vtable, G_OBJECT (settings),
525 settings->priv->main_context);
526 g_settings_backend_subscribe (settings->priv->backend,
527 settings->priv->path);
531 g_settings_finalize (GObject *object)
533 GSettings *settings = G_SETTINGS (object);
535 g_settings_backend_unsubscribe (settings->priv->backend,
536 settings->priv->path);
537 g_main_context_unref (settings->priv->main_context);
538 g_object_unref (settings->priv->backend);
539 g_object_unref (settings->priv->schema);
540 g_free (settings->priv->schema_name);
541 g_free (settings->priv->path);
543 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
547 g_settings_init (GSettings *settings)
549 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
553 settings->priv->main_context = g_main_context_get_thread_default ();
555 if (settings->priv->main_context == NULL)
556 settings->priv->main_context = g_main_context_default ();
558 g_main_context_ref (settings->priv->main_context);
562 g_settings_class_init (GSettingsClass *class)
564 GObjectClass *object_class = G_OBJECT_CLASS (class);
566 class->writable_change_event = g_settings_real_writable_change_event;
567 class->change_event = g_settings_real_change_event;
569 object_class->set_property = g_settings_set_property;
570 object_class->get_property = g_settings_get_property;
571 object_class->constructed = g_settings_constructed;
572 object_class->finalize = g_settings_finalize;
574 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
577 * GSettings::changed:
578 * @settings: the object on which the signal was emitted
579 * @key: the name of the key that changed
581 * The "changed" signal is emitted when a key has potentially changed.
582 * You should call one of the g_settings_get() calls to check the new
585 * This signal supports detailed connections. You can connect to the
586 * detailed signal "changed::x" in order to only receive callbacks
587 * when key "x" changes.
589 g_settings_signals[SIGNAL_CHANGED] =
590 g_signal_new ("changed", G_TYPE_SETTINGS,
591 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
592 G_STRUCT_OFFSET (GSettingsClass, changed),
593 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
594 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
597 * GSettings::change-event:
598 * @settings: the object on which the signal was emitted
599 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
600 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
601 * @n_keys: the length of the @keys array, or 0
602 * @returns: %TRUE to stop other handlers from being invoked for the
603 * event. FALSE to propagate the event further.
605 * The "change-event" signal is emitted once per change event that
606 * affects this settings object. You should connect to this signal
607 * only if you are interested in viewing groups of changes before they
608 * are split out into multiple emissions of the "changed" signal.
609 * For most use cases it is more appropriate to use the "changed" signal.
611 * In the event that the change event applies to one or more specified
612 * keys, @keys will be an array of #GQuark of length @n_keys. In the
613 * event that the change event applies to the #GSettings object as a
614 * whole (ie: potentially every key has been changed) then @keys will
615 * be %NULL and @n_keys will be 0.
617 * The default handler for this signal invokes the "changed" signal
618 * for each affected key. If any other connected handler returns
619 * %TRUE then this default functionality will be supressed.
621 g_settings_signals[SIGNAL_CHANGE_EVENT] =
622 g_signal_new ("change-event", G_TYPE_SETTINGS,
624 G_STRUCT_OFFSET (GSettingsClass, change_event),
625 g_signal_accumulator_true_handled, NULL,
626 _gio_marshal_BOOL__POINTER_INT,
627 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
630 * GSettings::writable-changed:
631 * @settings: the object on which the signal was emitted
634 * The "writable-changed" signal is emitted when the writability of a
635 * key has potentially changed. You should call
636 * g_settings_is_writable() in order to determine the new status.
638 * This signal supports detailed connections. You can connect to the
639 * detailed signal "writable-changed::x" in order to only receive
640 * callbacks when the writability of "x" changes.
642 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
643 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
644 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
645 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
646 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
647 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
650 * GSettings::writable-change-event:
651 * @settings: the object on which the signal was emitted
652 * @key: the quark of the key, or 0
653 * @returns: %TRUE to stop other handlers from being invoked for the
654 * event. FALSE to propagate the event further.
656 * The "writable-change-event" signal is emitted once per writability
657 * change event that affects this settings object. You should connect
658 * to this signal if you are interested in viewing groups of changes
659 * before they are split out into multiple emissions of the
660 * "writable-changed" signal. For most use cases it is more
661 * appropriate to use the "writable-changed" signal.
663 * In the event that the writability change applies only to a single
664 * key, @key will be set to the #GQuark for that key. In the event
665 * that the writability change affects the entire settings object,
668 * The default handler for this signal invokes the "writable-changed"
669 * and "changed" signals for each affected key. This is done because
670 * changes in writability might also imply changes in value (if for
671 * example, a new mandatory setting is introduced). If any other
672 * connected handler returns %TRUE then this default functionality
675 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
676 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
678 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
679 g_signal_accumulator_true_handled, NULL,
680 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
685 * The name of the context that the settings are stored in.
687 g_object_class_install_property (object_class, PROP_BACKEND,
688 g_param_spec_object ("backend",
689 P_("GSettingsBackend"),
690 P_("The GSettingsBackend for this settings object"),
691 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
697 * The name of the schema that describes the types of keys
698 * for this #GSettings object.
700 g_object_class_install_property (object_class, PROP_SCHEMA,
701 g_param_spec_string ("schema",
703 P_("The name of the schema for this settings object"),
705 G_PARAM_CONSTRUCT_ONLY |
706 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
711 * The path within the backend where the settings are stored.
713 g_object_class_install_property (object_class, PROP_PATH,
714 g_param_spec_string ("path",
716 P_("The path within the backend where the settings are"),
718 G_PARAM_CONSTRUCT_ONLY |
719 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
722 * GSettings:has-unapplied:
724 * If this property is %TRUE, the #GSettings object has outstanding
725 * changes that will be applied when g_settings_apply() is called.
727 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
728 g_param_spec_boolean ("has-unapplied",
729 P_("Has unapplied changes"),
730 P_("TRUE if there are outstanding changes to apply()"),
732 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
735 * GSettings:delay-apply:
737 * Whether the #GSettings object is in 'delay-apply' mode. See
738 * g_settings_delay() for details.
742 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
743 g_param_spec_boolean ("delay-apply",
744 P_("Delay-apply mode"),
745 P_("Whether this settings object is in 'delay-apply' mode"),
747 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
750 /* Construction (new, new_with_path, etc.) {{{1 */
753 * @schema: the name of the schema
754 * @returns: a new #GSettings object
756 * Creates a new #GSettings object with a given schema.
758 * Signals on the newly created #GSettings object will be dispatched
759 * via the thread-default #GMainContext in effect at the time of the
760 * call to g_settings_new(). The new #GSettings will hold a reference
761 * on the context. See g_main_context_push_thread_default().
766 g_settings_new (const gchar *schema)
768 g_return_val_if_fail (schema != NULL, NULL);
770 return g_object_new (G_TYPE_SETTINGS,
776 * g_settings_new_with_path:
777 * @schema: the name of the schema
778 * @path: the path to use
779 * @returns: a new #GSettings object
781 * Creates a new #GSettings object with a given schema and path.
783 * You only need to do this if you want to directly create a settings
784 * object with a schema that doesn't have a specified path of its own.
787 * It is a programmer error to call this function for a schema that
788 * has an explicitly specified path.
793 g_settings_new_with_path (const gchar *schema,
796 g_return_val_if_fail (schema != NULL, NULL);
797 g_return_val_if_fail (path != NULL, NULL);
799 return g_object_new (G_TYPE_SETTINGS,
806 * g_settings_new_with_backend:
807 * @schema: the name of the schema
808 * @backend: the #GSettingsBackend to use
809 * @returns: a new #GSettings object
811 * Creates a new #GSettings object with a given schema and backend.
813 * Creating settings objects with an different backend allows accessing settings
814 * from a database other than the usual one. For example, it may make
815 * sense to pass a backend corresponding to the "defaults" settings database on
816 * the system to get a settings object that modifies the system default
817 * settings instead of the settings for this user.
822 g_settings_new_with_backend (const gchar *schema,
823 GSettingsBackend *backend)
825 g_return_val_if_fail (schema != NULL, NULL);
826 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
828 return g_object_new (G_TYPE_SETTINGS,
835 * g_settings_new_with_backend_and_path:
836 * @schema: the name of the schema
837 * @backend: the #GSettingsBackend to use
838 * @path: the path to use
839 * @returns: a new #GSettings object
841 * Creates a new #GSettings object with a given schema, backend and
844 * This is a mix of g_settings_new_with_backend() and
845 * g_settings_new_with_path().
850 g_settings_new_with_backend_and_path (const gchar *schema,
851 GSettingsBackend *backend,
854 g_return_val_if_fail (schema != NULL, NULL);
855 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
856 g_return_val_if_fail (path != NULL, NULL);
858 return g_object_new (G_TYPE_SETTINGS,
865 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
871 GSettingsSchema *schema;
876 const guint32 *strinfo;
877 gsize strinfo_length;
879 const gchar *unparsed;
882 const GVariantType *type;
883 GVariant *minimum, *maximum;
884 GVariant *default_value;
888 endian_fixup (GVariant **value)
890 #if G_BYTE_ORDER == G_BIG_ENDIAN
893 tmp = g_variant_byteswap (*value);
894 g_variant_unref (*value);
900 g_settings_get_key_info (GSettingsKeyInfo *info,
908 memset (info, 0, sizeof *info);
910 iter = g_settings_schema_get_value (settings->priv->schema, key);
912 info->default_value = g_variant_iter_next_value (iter);
913 endian_fixup (&info->default_value);
914 info->type = g_variant_get_type (info->default_value);
915 info->settings = g_object_ref (settings);
916 info->key = g_intern_string (key);
918 while (g_variant_iter_next (iter, "(y*)", &code, &data))
923 /* translation requested */
924 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
928 /* enumerated types... */
929 info->is_enum = TRUE;
934 info->is_flags = TRUE;
938 /* ..., choices, aliases */
939 info->strinfo = g_variant_get_fixed_array (data,
940 &info->strinfo_length,
945 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
946 endian_fixup (&info->minimum);
947 endian_fixup (&info->maximum);
951 g_warning ("unknown schema extension '%c'", code);
955 g_variant_unref (data);
958 g_variant_iter_free (iter);
962 g_settings_free_key_info (GSettingsKeyInfo *info)
965 g_variant_unref (info->minimum);
968 g_variant_unref (info->maximum);
970 g_variant_unref (info->default_value);
971 g_object_unref (info->settings);
975 g_settings_write_to_backend (GSettingsKeyInfo *info,
981 path = g_strconcat (info->settings->priv->path, info->key, NULL);
982 success = g_settings_backend_write (info->settings->priv->backend,
990 g_settings_type_check (GSettingsKeyInfo *info,
993 g_return_val_if_fail (value != NULL, FALSE);
995 return g_variant_is_of_type (value, info->type);
999 g_settings_key_info_range_check (GSettingsKeyInfo *info,
1002 if (info->minimum == NULL && info->strinfo == NULL)
1005 if (g_variant_is_container (value))
1011 g_variant_iter_init (&iter, value);
1012 while (ok && (child = g_variant_iter_next_value (&iter)))
1014 ok = g_settings_key_info_range_check (info, child);
1015 g_variant_unref (child);
1023 return g_variant_compare (info->minimum, value) <= 0 &&
1024 g_variant_compare (value, info->maximum) <= 0;
1027 return strinfo_is_string_valid (info->strinfo,
1028 info->strinfo_length,
1029 g_variant_get_string (value, NULL));
1033 g_settings_range_fixup (GSettingsKeyInfo *info,
1036 const gchar *target;
1038 if (g_settings_key_info_range_check (info, value))
1039 return g_variant_ref (value);
1041 if (info->strinfo == NULL)
1044 if (g_variant_is_container (value))
1046 GVariantBuilder builder;
1050 g_variant_iter_init (&iter, value);
1051 g_variant_builder_init (&builder, g_variant_get_type (value));
1053 while ((child = g_variant_iter_next_value (&iter)))
1057 fixed = g_settings_range_fixup (info, child);
1058 g_variant_unref (child);
1062 g_variant_builder_clear (&builder);
1066 g_variant_builder_add_value (&builder, fixed);
1067 g_variant_unref (fixed);
1070 return g_variant_ref_sink (g_variant_builder_end (&builder));
1073 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1074 g_variant_get_string (value, NULL));
1075 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1079 g_settings_read_from_backend (GSettingsKeyInfo *info)
1085 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1086 value = g_settings_backend_read (info->settings->priv->backend,
1087 path, info->type, FALSE);
1092 fixup = g_settings_range_fixup (info, value);
1093 g_variant_unref (value);
1102 g_settings_get_translated_default (GSettingsKeyInfo *info)
1104 const gchar *translated;
1105 GError *error = NULL;
1106 const gchar *domain;
1109 if (info->lc_char == '\0')
1110 /* translation not requested for this key */
1113 domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
1115 if (info->lc_char == 't')
1116 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1118 translated = g_dgettext (domain, info->unparsed);
1120 if (translated == info->unparsed)
1121 /* the default value was not translated */
1124 /* try to parse the translation of the unparsed default */
1125 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1129 g_warning ("Failed to parse translated string `%s' for "
1130 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1131 info->settings->priv->schema_name, error->message);
1132 g_warning ("Using untranslated default instead.");
1133 g_error_free (error);
1136 else if (!g_settings_key_info_range_check (info, value))
1138 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1139 "is outside of valid range", info->unparsed, info->key,
1140 info->settings->priv->schema_name);
1141 g_variant_unref (value);
1149 g_settings_to_enum (GSettingsKeyInfo *info,
1155 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1156 g_variant_get_string (value, NULL),
1159 /* 'value' can only come from the backend after being filtered for validity,
1160 * from the translation after being filtered for validity, or from the schema
1161 * itself (which the schema compiler checks for validity). If this assertion
1162 * fails then it's really a bug in GSettings or the schema compiler...
1164 g_assert (it_worked);
1170 g_settings_from_enum (GSettingsKeyInfo *info,
1173 const gchar *string;
1175 string = strinfo_string_from_enum (info->strinfo,
1176 info->strinfo_length,
1182 return g_variant_new_string (string);
1186 g_settings_to_flags (GSettingsKeyInfo *info,
1194 g_variant_iter_init (&iter, value);
1195 while (g_variant_iter_next (&iter, "&s", &flag))
1200 it_worked = strinfo_enum_from_string (info->strinfo,
1201 info->strinfo_length,
1203 /* as in g_settings_to_enum() */
1204 g_assert (it_worked);
1206 result |= flag_value;
1213 g_settings_from_flags (GSettingsKeyInfo *info,
1216 GVariantBuilder builder;
1219 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1221 for (i = 0; i < 32; i++)
1222 if (value & (1u << i))
1224 const gchar *string;
1226 string = strinfo_string_from_enum (info->strinfo,
1227 info->strinfo_length,
1232 g_variant_builder_clear (&builder);
1236 g_variant_builder_add (&builder, "s", string);
1239 return g_variant_builder_end (&builder);
1242 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1244 * g_settings_get_value:
1245 * @settings: a #GSettings object
1246 * @key: the key to get the value for
1247 * @returns: a new #GVariant
1249 * Gets the value that is stored in @settings for @key.
1251 * It is a programmer error to give a @key that isn't contained in the
1252 * schema for @settings.
1257 g_settings_get_value (GSettings *settings,
1260 GSettingsKeyInfo info;
1263 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1264 g_return_val_if_fail (key != NULL, NULL);
1266 g_settings_get_key_info (&info, settings, key);
1267 value = g_settings_read_from_backend (&info);
1270 value = g_settings_get_translated_default (&info);
1273 value = g_variant_ref (info.default_value);
1275 g_settings_free_key_info (&info);
1281 * g_settings_get_enum:
1282 * @settings: a #GSettings object
1283 * @key: the key to get the value for
1284 * @returns: the enum value
1286 * Gets the value that is stored in @settings for @key and converts it
1287 * to the enum value that it represents.
1289 * In order to use this function the type of the value must be a string
1290 * and it must be marked in the schema file as an enumerated type.
1292 * It is a programmer error to give a @key that isn't contained in the
1293 * schema for @settings or is not marked as an enumerated type.
1295 * If the value stored in the configuration database is not a valid
1296 * value for the enumerated type then this function will return the
1302 g_settings_get_enum (GSettings *settings,
1305 GSettingsKeyInfo info;
1309 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1310 g_return_val_if_fail (key != NULL, -1);
1312 g_settings_get_key_info (&info, settings, key);
1316 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1317 "associated with an enumerated type", info.key);
1318 g_settings_free_key_info (&info);
1322 value = g_settings_read_from_backend (&info);
1325 value = g_settings_get_translated_default (&info);
1328 value = g_variant_ref (info.default_value);
1330 result = g_settings_to_enum (&info, value);
1331 g_settings_free_key_info (&info);
1332 g_variant_unref (value);
1338 * g_settings_set_enum:
1339 * @settings: a #GSettings object
1340 * @key: a key, within @settings
1341 * @value: an enumerated value
1342 * @returns: %TRUE, if the set succeeds
1344 * Looks up the enumerated type nick for @value and writes it to @key,
1347 * It is a programmer error to give a @key that isn't contained in the
1348 * schema for @settings or is not marked as an enumerated type, or for
1349 * @value not to be a valid value for the named type.
1351 * After performing the write, accessing @key directly with
1352 * g_settings_get_string() will return the 'nick' associated with
1356 g_settings_set_enum (GSettings *settings,
1360 GSettingsKeyInfo info;
1364 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1365 g_return_val_if_fail (key != NULL, FALSE);
1367 g_settings_get_key_info (&info, settings, key);
1371 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1372 "associated with an enumerated type", info.key);
1376 if (!(variant = g_settings_from_enum (&info, value)))
1378 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1379 "in schema `%s'. Doing nothing.", value, info.key,
1380 info.settings->priv->schema_name);
1381 g_settings_free_key_info (&info);
1385 success = g_settings_write_to_backend (&info, variant);
1386 g_settings_free_key_info (&info);
1392 * g_settings_get_flags:
1393 * @settings: a #GSettings object
1394 * @key: the key to get the value for
1395 * @returns: the flags value
1397 * Gets the value that is stored in @settings for @key and converts it
1398 * to the flags value that it represents.
1400 * In order to use this function the type of the value must be an array
1401 * of strings and it must be marked in the schema file as an flags type.
1403 * It is a programmer error to give a @key that isn't contained in the
1404 * schema for @settings or is not marked as a flags type.
1406 * If the value stored in the configuration database is not a valid
1407 * value for the flags type then this function will return the default
1413 g_settings_get_flags (GSettings *settings,
1416 GSettingsKeyInfo info;
1420 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1421 g_return_val_if_fail (key != NULL, -1);
1423 g_settings_get_key_info (&info, settings, key);
1427 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1428 "associated with a flags type", info.key);
1429 g_settings_free_key_info (&info);
1433 value = g_settings_read_from_backend (&info);
1436 value = g_settings_get_translated_default (&info);
1439 value = g_variant_ref (info.default_value);
1441 result = g_settings_to_flags (&info, value);
1442 g_settings_free_key_info (&info);
1443 g_variant_unref (value);
1449 * g_settings_set_flags:
1450 * @settings: a #GSettings object
1451 * @key: a key, within @settings
1452 * @value: a flags value
1453 * @returns: %TRUE, if the set succeeds
1455 * Looks up the flags type nicks for the bits specified by @value, puts
1456 * them in an array of strings and writes the array to @key, withing
1459 * It is a programmer error to give a @key that isn't contained in the
1460 * schema for @settings or is not marked as a flags type, or for @value
1461 * to contain any bits that are not value for the named type.
1463 * After performing the write, accessing @key directly with
1464 * g_settings_get_strv() will return an array of 'nicks'; one for each
1468 g_settings_set_flags (GSettings *settings,
1472 GSettingsKeyInfo info;
1476 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1477 g_return_val_if_fail (key != NULL, FALSE);
1479 g_settings_get_key_info (&info, settings, key);
1483 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1484 "associated with a flags type", info.key);
1488 if (!(variant = g_settings_from_flags (&info, value)))
1490 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1491 "for key `%s' in schema `%s'. Doing nothing.",
1492 value, info.key, info.settings->priv->schema_name);
1493 g_settings_free_key_info (&info);
1497 success = g_settings_write_to_backend (&info, variant);
1498 g_settings_free_key_info (&info);
1504 * g_settings_set_value:
1505 * @settings: a #GSettings object
1506 * @key: the name of the key to set
1507 * @value: a #GVariant of the correct type
1508 * @returns: %TRUE if setting the key succeeded,
1509 * %FALSE if the key was not writable
1511 * Sets @key in @settings to @value.
1513 * It is a programmer error to give a @key that isn't contained in the
1514 * schema for @settings or for @value to have the incorrect type, per
1517 * If @value is floating then this function consumes the reference.
1522 g_settings_set_value (GSettings *settings,
1526 GSettingsKeyInfo info;
1528 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1529 g_return_val_if_fail (key != NULL, FALSE);
1531 g_settings_get_key_info (&info, settings, key);
1533 if (!g_settings_type_check (&info, value))
1535 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1537 settings->priv->schema_name,
1538 g_variant_type_peek_string (info.type),
1539 g_variant_get_type_string (value));
1544 if (!g_settings_key_info_range_check (&info, value))
1546 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1547 "is outside of valid range",
1549 settings->priv->schema_name);
1554 g_settings_free_key_info (&info);
1556 return g_settings_write_to_backend (&info, value);
1561 * @settings: a #GSettings object
1562 * @key: the key to get the value for
1563 * @format: a #GVariant format string
1564 * @...: arguments as per @format
1566 * Gets the value that is stored at @key in @settings.
1568 * A convenience function that combines g_settings_get_value() with
1571 * It is a programmer error to give a @key that isn't contained in the
1572 * schema for @settings or for the #GVariantType of @format to mismatch
1573 * the type given in the schema.
1578 g_settings_get (GSettings *settings,
1580 const gchar *format,
1586 value = g_settings_get_value (settings, key);
1588 va_start (ap, format);
1589 g_variant_get_va (value, format, NULL, &ap);
1592 g_variant_unref (value);
1597 * @settings: a #GSettings object
1598 * @key: the name of the key to set
1599 * @format: a #GVariant format string
1600 * @...: arguments as per @format
1601 * @returns: %TRUE if setting the key succeeded,
1602 * %FALSE if the key was not writable
1604 * Sets @key in @settings to @value.
1606 * A convenience function that combines g_settings_set_value() with
1609 * It is a programmer error to give a @key that isn't contained in the
1610 * schema for @settings or for the #GVariantType of @format to mismatch
1611 * the type given in the schema.
1616 g_settings_set (GSettings *settings,
1618 const gchar *format,
1624 va_start (ap, format);
1625 value = g_variant_new_va (format, NULL, &ap);
1628 return g_settings_set_value (settings, key, value);
1632 * g_settings_get_mapped:
1633 * @settings: a #GSettings object
1634 * @key: the key to get the value for
1635 * @mapping: (scope call): the function to map the value in the
1636 * settings database to the value used by the application
1637 * @user_data: user data for @mapping
1638 * @returns: (transfer full): the result, which may be %NULL
1640 * Gets the value that is stored at @key in @settings, subject to
1641 * application-level validation/mapping.
1643 * You should use this function when the application needs to perform
1644 * some processing on the value of the key (for example, parsing). The
1645 * @mapping function performs that processing. If the function
1646 * indicates that the processing was unsuccessful (due to a parse error,
1647 * for example) then the mapping is tried again with another value.
1649 * This allows a robust 'fall back to defaults' behaviour to be
1650 * implemented somewhat automatically.
1652 * The first value that is tried is the user's setting for the key. If
1653 * the mapping function fails to map this value, other values may be
1654 * tried in an unspecified order (system or site defaults, translated
1655 * schema default values, untranslated schema default values, etc).
1657 * If the mapping function fails for all possible values, one additional
1658 * attempt is made: the mapping function is called with a %NULL value.
1659 * If the mapping function still indicates failure at this point then
1660 * the application will be aborted.
1662 * The result parameter for the @mapping function is pointed to a
1663 * #gpointer which is initially set to %NULL. The same pointer is given
1664 * to each invocation of @mapping. The final value of that #gpointer is
1665 * what is returned by this function. %NULL is valid; it is returned
1666 * just as any other value would be.
1669 g_settings_get_mapped (GSettings *settings,
1671 GSettingsGetMapping mapping,
1674 gpointer result = NULL;
1675 GSettingsKeyInfo info;
1679 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1680 g_return_val_if_fail (key != NULL, NULL);
1681 g_return_val_if_fail (mapping != NULL, NULL);
1683 g_settings_get_key_info (&info, settings, key);
1685 if ((value = g_settings_read_from_backend (&info)))
1687 okay = mapping (value, &result, user_data);
1688 g_variant_unref (value);
1689 if (okay) goto okay;
1692 if ((value = g_settings_get_translated_default (&info)))
1694 okay = mapping (value, &result, user_data);
1695 g_variant_unref (value);
1696 if (okay) goto okay;
1699 if (mapping (info.default_value, &result, user_data))
1702 if (!mapping (NULL, &result, user_data))
1703 g_error ("The mapping function given to g_settings_get_mapped() for key "
1704 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1705 key, settings->priv->schema_name);
1708 g_settings_free_key_info (&info);
1713 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1715 * g_settings_get_string:
1716 * @settings: a #GSettings object
1717 * @key: the key to get the value for
1718 * @returns: a newly-allocated string
1720 * Gets the value that is stored at @key in @settings.
1722 * A convenience variant of g_settings_get() for strings.
1724 * It is a programmer error to give a @key that isn't specified as
1725 * having a string type in the schema for @settings.
1730 g_settings_get_string (GSettings *settings,
1736 value = g_settings_get_value (settings, key);
1737 result = g_variant_dup_string (value, NULL);
1738 g_variant_unref (value);
1744 * g_settings_set_string:
1745 * @settings: a #GSettings object
1746 * @key: the name of the key to set
1747 * @value: the value to set it to
1748 * @returns: %TRUE if setting the key succeeded,
1749 * %FALSE if the key was not writable
1751 * Sets @key in @settings to @value.
1753 * A convenience variant of g_settings_set() for strings.
1755 * It is a programmer error to give a @key that isn't specified as
1756 * having a string type in the schema for @settings.
1761 g_settings_set_string (GSettings *settings,
1765 return g_settings_set_value (settings, key, g_variant_new_string (value));
1769 * g_settings_get_int:
1770 * @settings: a #GSettings object
1771 * @key: the key to get the value for
1772 * @returns: an integer
1774 * Gets the value that is stored at @key in @settings.
1776 * A convenience variant of g_settings_get() for 32-bit integers.
1778 * It is a programmer error to give a @key that isn't specified as
1779 * having a int32 type in the schema for @settings.
1784 g_settings_get_int (GSettings *settings,
1790 value = g_settings_get_value (settings, key);
1791 result = g_variant_get_int32 (value);
1792 g_variant_unref (value);
1798 * g_settings_set_int:
1799 * @settings: a #GSettings object
1800 * @key: the name of the key to set
1801 * @value: the value to set it to
1802 * @returns: %TRUE if setting the key succeeded,
1803 * %FALSE if the key was not writable
1805 * Sets @key in @settings to @value.
1807 * A convenience variant of g_settings_set() for 32-bit integers.
1809 * It is a programmer error to give a @key that isn't specified as
1810 * having a int32 type in the schema for @settings.
1815 g_settings_set_int (GSettings *settings,
1819 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1823 * g_settings_get_uint:
1824 * @settings: a #GSettings object
1825 * @key: the key to get the value for
1826 * @returns: an unsigned integer
1828 * Gets the value that is stored at @key in @settings.
1830 * A convenience variant of g_settings_get() for 32-bit unsigned
1833 * It is a programmer error to give a @key that isn't specified as
1834 * having a uint32 type in the schema for @settings.
1839 g_settings_get_uint (GSettings *settings,
1845 value = g_settings_get_value (settings, key);
1846 result = g_variant_get_uint32 (value);
1847 g_variant_unref (value);
1853 * g_settings_set_uint:
1854 * @settings: a #GSettings object
1855 * @key: the name of the key to set
1856 * @value: the value to set it to
1857 * @returns: %TRUE if setting the key succeeded,
1858 * %FALSE if the key was not writable
1860 * Sets @key in @settings to @value.
1862 * A convenience variant of g_settings_set() for 32-bit unsigned
1865 * It is a programmer error to give a @key that isn't specified as
1866 * having a uint32 type in the schema for @settings.
1871 g_settings_set_uint (GSettings *settings,
1875 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1879 * g_settings_get_double:
1880 * @settings: a #GSettings object
1881 * @key: the key to get the value for
1882 * @returns: a double
1884 * Gets the value that is stored at @key in @settings.
1886 * A convenience variant of g_settings_get() for doubles.
1888 * It is a programmer error to give a @key that isn't specified as
1889 * having a 'double' type in the schema for @settings.
1894 g_settings_get_double (GSettings *settings,
1900 value = g_settings_get_value (settings, key);
1901 result = g_variant_get_double (value);
1902 g_variant_unref (value);
1908 * g_settings_set_double:
1909 * @settings: a #GSettings object
1910 * @key: the name of the key to set
1911 * @value: the value to set it to
1912 * @returns: %TRUE if setting the key succeeded,
1913 * %FALSE if the key was not writable
1915 * Sets @key in @settings to @value.
1917 * A convenience variant of g_settings_set() for doubles.
1919 * It is a programmer error to give a @key that isn't specified as
1920 * having a 'double' type in the schema for @settings.
1925 g_settings_set_double (GSettings *settings,
1929 return g_settings_set_value (settings, key, g_variant_new_double (value));
1933 * g_settings_get_boolean:
1934 * @settings: a #GSettings object
1935 * @key: the key to get the value for
1936 * @returns: a boolean
1938 * Gets the value that is stored at @key in @settings.
1940 * A convenience variant of g_settings_get() for booleans.
1942 * It is a programmer error to give a @key that isn't specified as
1943 * having a boolean type in the schema for @settings.
1948 g_settings_get_boolean (GSettings *settings,
1954 value = g_settings_get_value (settings, key);
1955 result = g_variant_get_boolean (value);
1956 g_variant_unref (value);
1962 * g_settings_set_boolean:
1963 * @settings: a #GSettings object
1964 * @key: the name of the key to set
1965 * @value: the value to set it to
1966 * @returns: %TRUE if setting the key succeeded,
1967 * %FALSE if the key was not writable
1969 * Sets @key in @settings to @value.
1971 * A convenience variant of g_settings_set() for booleans.
1973 * It is a programmer error to give a @key that isn't specified as
1974 * having a boolean type in the schema for @settings.
1979 g_settings_set_boolean (GSettings *settings,
1983 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1987 * g_settings_get_strv:
1988 * @settings: a #GSettings object
1989 * @key: the key to get the value for
1990 * @returns: (array zero-terminated=1) (transfer full): a
1991 * newly-allocated, %NULL-terminated array of strings, the value that
1992 * is stored at @key in @settings.
1994 * A convenience variant of g_settings_get() for string arrays.
1996 * It is a programmer error to give a @key that isn't specified as
1997 * having an array of strings type in the schema for @settings.
2002 g_settings_get_strv (GSettings *settings,
2008 value = g_settings_get_value (settings, key);
2009 result = g_variant_dup_strv (value, NULL);
2010 g_variant_unref (value);
2016 * g_settings_set_strv:
2017 * @settings: a #GSettings object
2018 * @key: the name of the key to set
2019 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
2020 * @returns: %TRUE if setting the key succeeded,
2021 * %FALSE if the key was not writable
2023 * Sets @key in @settings to @value.
2025 * A convenience variant of g_settings_set() for string arrays. If
2026 * @value is %NULL, then @key is set to be the empty array.
2028 * It is a programmer error to give a @key that isn't specified as
2029 * having an array of strings type in the schema for @settings.
2034 g_settings_set_strv (GSettings *settings,
2036 const gchar * const *value)
2041 array = g_variant_new_strv (value, -1);
2043 array = g_variant_new_strv (NULL, 0);
2045 return g_settings_set_value (settings, key, array);
2048 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2051 * @settings: a #GSettings object
2053 * Changes the #GSettings object into 'delay-apply' mode. In this
2054 * mode, changes to @settings are not immediately propagated to the
2055 * backend, but kept locally until g_settings_apply() is called.
2060 g_settings_delay (GSettings *settings)
2062 g_return_if_fail (G_IS_SETTINGS (settings));
2064 if (settings->priv->delayed)
2067 settings->priv->delayed =
2068 g_delayed_settings_backend_new (settings->priv->backend,
2070 settings->priv->main_context);
2071 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2072 g_object_unref (settings->priv->backend);
2074 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2075 g_settings_backend_watch (settings->priv->backend,
2076 &listener_vtable, G_OBJECT (settings),
2077 settings->priv->main_context);
2079 g_object_notify (G_OBJECT (settings), "delay-apply");
2084 * @settings: a #GSettings instance
2086 * Applies any changes that have been made to the settings. This
2087 * function does nothing unless @settings is in 'delay-apply' mode;
2088 * see g_settings_delay(). In the normal case settings are always
2089 * applied immediately.
2092 g_settings_apply (GSettings *settings)
2094 if (settings->priv->delayed)
2096 GDelayedSettingsBackend *delayed;
2098 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2099 g_delayed_settings_backend_apply (delayed);
2104 * g_settings_revert:
2105 * @settings: a #GSettings instance
2107 * Reverts all non-applied changes to the settings. This function
2108 * does nothing unless @settings is in 'delay-apply' mode; see
2109 * g_settings_delay(). In the normal case settings are always applied
2112 * Change notifications will be emitted for affected keys.
2115 g_settings_revert (GSettings *settings)
2117 if (settings->priv->delayed)
2119 GDelayedSettingsBackend *delayed;
2121 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2122 g_delayed_settings_backend_revert (delayed);
2127 * g_settings_get_has_unapplied:
2128 * @settings: a #GSettings object
2129 * @returns: %TRUE if @settings has unapplied changes
2131 * Returns whether the #GSettings object has any unapplied
2132 * changes. This can only be the case if it is in 'delayed-apply' mode.
2137 g_settings_get_has_unapplied (GSettings *settings)
2139 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2141 return settings->priv->delayed &&
2142 g_delayed_settings_backend_get_has_unapplied (
2143 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2146 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2149 * @settings: a #GSettings object
2150 * @key: the name of a key
2152 * Resets @key to its default value.
2154 * This call resets the key, as much as possible, to its default value.
2155 * That might the value specified in the schema or the one set by the
2159 g_settings_reset (GSettings *settings,
2164 path = g_strconcat (settings->priv->path, key, NULL);
2165 g_settings_backend_reset (settings->priv->backend, path, NULL);
2172 * Ensures that all pending operations for the given are complete for
2173 * the default backend.
2175 * Writes made to a #GSettings are handled asynchronously. For this
2176 * reason, it is very unlikely that the changes have it to disk by the
2177 * time g_settings_set() returns.
2179 * This call will block until all of the writes have made it to the
2180 * backend. Since the mainloop is not running, no change notifications
2181 * will be dispatched during this call (but some may be queued by the
2182 * time the call is done).
2185 g_settings_sync (void)
2187 g_settings_backend_sync_default ();
2191 * g_settings_is_writable:
2192 * @settings: a #GSettings object
2193 * @name: the name of a key
2194 * @returns: %TRUE if the key @name is writable
2196 * Finds out if a key can be written or not
2201 g_settings_is_writable (GSettings *settings,
2207 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2209 path = g_strconcat (settings->priv->path, name, NULL);
2210 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2217 * g_settings_get_child:
2218 * @settings: a #GSettings object
2219 * @name: the name of the 'child' schema
2220 * @returns: (transfer full): a 'child' settings object
2222 * Creates a 'child' settings object which has a base path of
2223 * <replaceable>base-path</replaceable>/@name, where
2224 * <replaceable>base-path</replaceable> is the base path of @settings.
2226 * The schema for the child settings object must have been declared
2227 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2232 g_settings_get_child (GSettings *settings,
2235 const gchar *child_schema;
2240 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2242 child_name = g_strconcat (name, "/", NULL);
2243 child_schema = g_settings_schema_get_string (settings->priv->schema,
2245 if (child_schema == NULL)
2246 g_error ("Schema '%s' has no child '%s'",
2247 settings->priv->schema_name, name);
2249 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2250 child = g_object_new (G_TYPE_SETTINGS,
2251 "schema", child_schema,
2254 g_free (child_path);
2255 g_free (child_name);
2261 * g_settings_list_keys:
2262 * @settings: a #GSettings object
2263 * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2265 * Introspects the list of keys on @settings.
2267 * You should probably not be calling this function from "normal" code
2268 * (since you should already know what keys are in your schema). This
2269 * function is intended for introspection reasons.
2271 * You should free the return value with g_strfreev() when you are done
2275 g_settings_list_keys (GSettings *settings)
2282 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2283 strv = g_new (gchar *, n_keys + 1);
2284 for (i = j = 0; i < n_keys; i++)
2286 const gchar *key = g_quark_to_string (keys[i]);
2288 if (!g_str_has_suffix (key, "/"))
2289 strv[j++] = g_strdup (key);
2297 * g_settings_list_children:
2298 * @settings: a #GSettings object
2299 * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2301 * Gets the list of children on @settings.
2303 * The list is exactly the list of strings for which it is not an error
2304 * to call g_settings_get_child().
2306 * For GSettings objects that are lists, this value can change at any
2307 * time and you should connect to the "children-changed" signal to watch
2308 * for those changes. Note that there is a race condition here: you may
2309 * request a child after listing it only for it to have been destroyed
2310 * in the meantime. For this reason, g_settings_get_child() may return
2311 * %NULL even for a child that was listed by this function.
2313 * For GSettings objects that are not lists, you should probably not be
2314 * calling this function from "normal" code (since you should already
2315 * know what children are in your schema). This function may still be
2316 * useful there for introspection reasons, however.
2318 * You should free the return value with g_strfreev() when you are done
2322 g_settings_list_children (GSettings *settings)
2329 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2330 strv = g_new (gchar *, n_keys + 1);
2331 for (i = j = 0; i < n_keys; i++)
2333 const gchar *key = g_quark_to_string (keys[i]);
2335 if (g_str_has_suffix (key, "/"))
2337 gint length = strlen (key);
2339 strv[j] = g_memdup (key, length);
2340 strv[j][length - 1] = '\0';
2350 * g_settings_get_range:
2351 * @settings: a #GSettings
2352 * @key: the key to query the range of
2353 * @returns: a #GVariant describing the range
2355 * Queries the range of a key.
2357 * This function will return a #GVariant that fully describes the range
2358 * of values that are valid for @key.
2360 * The type of #GVariant returned is <literal>(sv)</literal>. The
2361 * string describes the type of range restriction in effect. The type
2362 * and meaning of the value contained in the variant depends on the
2365 * If the string is <literal>'type'</literal> then the variant contains
2366 * an empty array. The element type of that empty array is the expected
2367 * type of value and all values of that type are valid.
2369 * If the string is <literal>'enum'</literal> then the variant contains
2370 * an array enumerating the possible values. Each item in the array is
2371 * a possible valid value and no other values are valid.
2373 * If the string is <literal>'flags'</literal> then the variant contains
2374 * an array. Each item in the array is a value that may appear zero or
2375 * one times in an array to be used as the value for this key. For
2376 * example, if the variant contained the array <literal>['x',
2377 * 'y']</literal> then the valid values for the key would be
2378 * <literal>[]</literal>, <literal>['x']</literal>,
2379 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2380 * <literal>['y', 'x']</literal>.
2382 * Finally, if the string is <literal>'range'</literal> then the variant
2383 * contains a pair of like-typed values -- the minimum and maximum
2384 * permissible values for this key.
2386 * This information should not be used by normal programs. It is
2387 * considered to be a hint for introspection purposes. Normal programs
2388 * should already know what is permitted by their own schema. The
2389 * format may change in any way in the future -- but particularly, new
2390 * forms may be added to the possibilities described above.
2392 * It is a programmer error to give a @key that isn't contained in the
2393 * schema for @settings.
2395 * You should free the returned value with g_variant_unref() when it is
2401 g_settings_get_range (GSettings *settings,
2404 GSettingsKeyInfo info;
2408 g_settings_get_key_info (&info, settings, key);
2412 range = g_variant_new ("(**)", info.minimum, info.maximum);
2415 else if (info.strinfo)
2417 range = strinfo_enumerate (info.strinfo, info.strinfo_length);
2418 type = info.is_flags ? "flags" : "enum";
2422 range = g_variant_new_array (info.type, NULL, 0);
2426 g_settings_free_key_info (&info);
2428 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2432 * g_settings_range_check:
2433 * @settings: a #GSettings
2434 * @key: the key to check
2435 * @value: the value to check
2436 * @returns: %TRUE if @value is valid for @key
2438 * Checks if the given @value is of the correct type and within the
2439 * permitted range for @key.
2441 * This API is not intended to be used by normal programs -- they should
2442 * already know what is permitted by their own schemas. This API is
2443 * meant to be used by programs such as editors or commandline tools.
2445 * It is a programmer error to give a @key that isn't contained in the
2446 * schema for @settings.
2451 g_settings_range_check (GSettings *settings,
2455 GSettingsKeyInfo info;
2458 g_settings_get_key_info (&info, settings, key);
2459 good = g_settings_type_check (&info, value) &&
2460 g_settings_key_info_range_check (&info, value);
2461 g_settings_free_key_info (&info);
2469 GSettingsKeyInfo info;
2472 GSettingsBindGetMapping get_mapping;
2473 GSettingsBindSetMapping set_mapping;
2475 GDestroyNotify destroy;
2477 guint writable_handler_id;
2478 guint property_handler_id;
2479 const GParamSpec *property;
2480 guint key_handler_id;
2482 /* prevent recursion */
2487 g_settings_binding_free (gpointer data)
2489 GSettingsBinding *binding = data;
2491 g_assert (!binding->running);
2493 if (binding->writable_handler_id)
2494 g_signal_handler_disconnect (binding->info.settings,
2495 binding->writable_handler_id);
2497 if (binding->key_handler_id)
2498 g_signal_handler_disconnect (binding->info.settings,
2499 binding->key_handler_id);
2501 if (g_signal_handler_is_connected (binding->object,
2502 binding->property_handler_id))
2503 g_signal_handler_disconnect (binding->object,
2504 binding->property_handler_id);
2506 g_settings_free_key_info (&binding->info);
2508 if (binding->destroy)
2509 binding->destroy (binding->user_data);
2511 g_slice_free (GSettingsBinding, binding);
2515 g_settings_binding_quark (const char *property)
2520 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2521 quark = g_quark_from_string (tmp);
2528 g_settings_binding_key_changed (GSettings *settings,
2532 GSettingsBinding *binding = user_data;
2533 GValue value = { 0, };
2536 g_assert (settings == binding->info.settings);
2537 g_assert (key == binding->info.key);
2539 if (binding->running)
2542 binding->running = TRUE;
2544 g_value_init (&value, binding->property->value_type);
2546 variant = g_settings_read_from_backend (&binding->info);
2547 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2549 /* silently ignore errors in the user's config database */
2550 g_variant_unref (variant);
2554 if (variant == NULL)
2556 variant = g_settings_get_translated_default (&binding->info);
2558 !binding->get_mapping (&value, variant, binding->user_data))
2560 /* flag translation errors with a warning */
2561 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2562 "was rejected by the binding mapping function",
2563 binding->info.unparsed, binding->info.key,
2564 binding->info.settings->priv->schema_name);
2565 g_variant_unref (variant);
2570 if (variant == NULL)
2572 variant = g_variant_ref (binding->info.default_value);
2573 if (!binding->get_mapping (&value, variant, binding->user_data))
2574 g_error ("The schema default value for key `%s' in schema `%s' "
2575 "was rejected by the binding mapping function.",
2577 binding->info.settings->priv->schema_name);
2580 g_object_set_property (binding->object, binding->property->name, &value);
2581 g_variant_unref (variant);
2582 g_value_unset (&value);
2584 binding->running = FALSE;
2588 g_settings_binding_property_changed (GObject *object,
2589 const GParamSpec *pspec,
2592 GSettingsBinding *binding = user_data;
2593 GValue value = { 0, };
2596 g_assert (object == binding->object);
2597 g_assert (pspec == binding->property);
2599 if (binding->running)
2602 binding->running = TRUE;
2604 g_value_init (&value, pspec->value_type);
2605 g_object_get_property (object, pspec->name, &value);
2606 if ((variant = binding->set_mapping (&value, binding->info.type,
2607 binding->user_data)))
2609 if (g_variant_is_floating (variant))
2610 g_variant_ref_sink (variant);
2612 if (!g_settings_type_check (&binding->info, variant))
2614 g_critical ("binding mapping function for key `%s' returned "
2615 "GVariant of type `%s' when type `%s' was requested",
2616 binding->info.key, g_variant_get_type_string (variant),
2617 g_variant_type_dup_string (binding->info.type));
2621 if (!g_settings_key_info_range_check (&binding->info, variant))
2623 g_critical ("GObject property `%s' on a `%s' object is out of "
2624 "schema-specified range for key `%s' of `%s': %s",
2625 binding->property->name,
2626 g_type_name (binding->property->owner_type),
2628 binding->info.settings->priv->schema_name,
2629 g_variant_print (variant, TRUE));
2633 g_settings_write_to_backend (&binding->info, variant);
2634 g_variant_unref (variant);
2636 g_value_unset (&value);
2638 binding->running = FALSE;
2642 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2646 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2651 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2652 const GVariantType *expected_type,
2655 return g_variant_new_boolean (!g_value_get_boolean (value));
2660 * @settings: a #GSettings object
2661 * @key: the key to bind
2662 * @object: (type GObject.Object): a #GObject
2663 * @property: the name of the property to bind
2664 * @flags: flags for the binding
2666 * Create a binding between the @key in the @settings object
2667 * and the property @property of @object.
2669 * The binding uses the default GIO mapping functions to map
2670 * between the settings and property values. These functions
2671 * handle booleans, numeric types and string types in a
2672 * straightforward way. Use g_settings_bind_with_mapping() if
2673 * you need a custom mapping, or map between types that are not
2674 * supported by the default mapping functions.
2676 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2677 * function also establishes a binding between the writability of
2678 * @key and the "sensitive" property of @object (if @object has
2679 * a boolean property by that name). See g_settings_bind_writable()
2680 * for more details about writable bindings.
2682 * Note that the lifecycle of the binding is tied to the object,
2683 * and that you can have only one binding per object property.
2684 * If you bind the same property twice on the same object, the second
2685 * binding overrides the first one.
2690 g_settings_bind (GSettings *settings,
2693 const gchar *property,
2694 GSettingsBindFlags flags)
2696 GSettingsBindGetMapping get_mapping = NULL;
2697 GSettingsBindSetMapping set_mapping = NULL;
2699 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2701 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2702 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2704 /* can't pass this flag to g_settings_bind_with_mapping() */
2705 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2708 g_settings_bind_with_mapping (settings, key, object, property, flags,
2709 get_mapping, set_mapping, NULL, NULL);
2713 * g_settings_bind_with_mapping: (skip)
2714 * @settings: a #GSettings object
2715 * @key: the key to bind
2716 * @object: (type GObject.Object): a #GObject
2717 * @property: the name of the property to bind
2718 * @flags: flags for the binding
2719 * @get_mapping: a function that gets called to convert values
2720 * from @settings to @object, or %NULL to use the default GIO mapping
2721 * @set_mapping: a function that gets called to convert values
2722 * from @object to @settings, or %NULL to use the default GIO mapping
2723 * @user_data: data that gets passed to @get_mapping and @set_mapping
2724 * @destroy: #GDestroyNotify function for @user_data
2726 * Create a binding between the @key in the @settings object
2727 * and the property @property of @object.
2729 * The binding uses the provided mapping functions to map between
2730 * settings and property values.
2732 * Note that the lifecycle of the binding is tied to the object,
2733 * and that you can have only one binding per object property.
2734 * If you bind the same property twice on the same object, the second
2735 * binding overrides the first one.
2740 g_settings_bind_with_mapping (GSettings *settings,
2743 const gchar *property,
2744 GSettingsBindFlags flags,
2745 GSettingsBindGetMapping get_mapping,
2746 GSettingsBindSetMapping set_mapping,
2748 GDestroyNotify destroy)
2750 GSettingsBinding *binding;
2751 GObjectClass *objectclass;
2752 gchar *detailed_signal;
2753 GQuark binding_quark;
2755 g_return_if_fail (G_IS_SETTINGS (settings));
2756 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2758 objectclass = G_OBJECT_GET_CLASS (object);
2760 binding = g_slice_new0 (GSettingsBinding);
2761 g_settings_get_key_info (&binding->info, settings, key);
2762 binding->object = object;
2763 binding->property = g_object_class_find_property (objectclass, property);
2764 binding->user_data = user_data;
2765 binding->destroy = destroy;
2766 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2767 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2769 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2770 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2772 if (binding->property == NULL)
2774 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2775 property, G_OBJECT_TYPE_NAME (object));
2779 if ((flags & G_SETTINGS_BIND_GET) &&
2780 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2782 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2783 "writable", property, G_OBJECT_TYPE_NAME (object));
2786 if ((flags & G_SETTINGS_BIND_SET) &&
2787 (binding->property->flags & G_PARAM_READABLE) == 0)
2789 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2790 "readable", property, G_OBJECT_TYPE_NAME (object));
2794 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2796 /* g_settings_bind_invert_boolean_get_mapping() is a private
2797 * function, so if we are here it means that g_settings_bind() was
2798 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2800 * Ensure that both sides are boolean.
2803 if (binding->property->value_type != G_TYPE_BOOLEAN)
2805 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2806 "was specified, but property `%s' on type `%s' has "
2807 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2808 g_type_name ((binding->property->value_type)));
2812 if (!g_variant_type_equal (binding->info.type, G_VARIANT_TYPE_BOOLEAN))
2814 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2815 "was specified, but key `%s' on schema `%s' has "
2816 "type `%s'", key, settings->priv->schema_name,
2817 g_variant_type_dup_string (binding->info.type));
2823 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2824 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2825 !g_settings_mapping_is_compatible (binding->property->value_type,
2826 binding->info.type))
2828 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2829 "'%s' which is not compatible with type '%s' of key '%s' "
2830 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2831 g_type_name (binding->property->value_type),
2832 g_variant_type_dup_string (binding->info.type), key,
2833 settings->priv->schema_name);
2837 if ((flags & G_SETTINGS_BIND_SET) &&
2838 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2840 GParamSpec *sensitive;
2842 sensitive = g_object_class_find_property (objectclass, "sensitive");
2844 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2845 (sensitive->flags & G_PARAM_WRITABLE))
2846 g_settings_bind_writable (settings, binding->info.key,
2847 object, "sensitive", FALSE);
2850 if (flags & G_SETTINGS_BIND_SET)
2852 detailed_signal = g_strdup_printf ("notify::%s", property);
2853 binding->property_handler_id =
2854 g_signal_connect (object, detailed_signal,
2855 G_CALLBACK (g_settings_binding_property_changed),
2857 g_free (detailed_signal);
2859 if (~flags & G_SETTINGS_BIND_GET)
2860 g_settings_binding_property_changed (object,
2865 if (flags & G_SETTINGS_BIND_GET)
2867 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2869 detailed_signal = g_strdup_printf ("changed::%s", key);
2870 binding->key_handler_id =
2871 g_signal_connect (settings, detailed_signal,
2872 G_CALLBACK (g_settings_binding_key_changed),
2874 g_free (detailed_signal);
2877 g_settings_binding_key_changed (settings, binding->info.key, binding);
2880 binding_quark = g_settings_binding_quark (property);
2881 g_object_set_qdata_full (object, binding_quark,
2882 binding, g_settings_binding_free);
2885 /* Writability binding {{{1 */
2888 GSettings *settings;
2891 const gchar *property;
2894 } GSettingsWritableBinding;
2897 g_settings_writable_binding_free (gpointer data)
2899 GSettingsWritableBinding *binding = data;
2901 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2902 g_object_unref (binding->settings);
2903 g_slice_free (GSettingsWritableBinding, binding);
2907 g_settings_binding_writable_changed (GSettings *settings,
2911 GSettingsWritableBinding *binding = user_data;
2914 g_assert (settings == binding->settings);
2915 g_assert (key == binding->key);
2917 writable = g_settings_is_writable (settings, key);
2919 if (binding->inverted)
2920 writable = !writable;
2922 g_object_set (binding->object, binding->property, writable, NULL);
2926 * g_settings_bind_writable:
2927 * @settings: a #GSettings object
2928 * @key: the key to bind
2929 * @object: (type GObject.Object):a #GObject
2930 * @property: the name of a boolean property to bind
2931 * @inverted: whether to 'invert' the value
2933 * Create a binding between the writability of @key in the
2934 * @settings object and the property @property of @object.
2935 * The property must be boolean; "sensitive" or "visible"
2936 * properties of widgets are the most likely candidates.
2938 * Writable bindings are always uni-directional; changes of the
2939 * writability of the setting will be propagated to the object
2940 * property, not the other way.
2942 * When the @inverted argument is %TRUE, the binding inverts the
2943 * value as it passes from the setting to the object, i.e. @property
2944 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2947 * Note that the lifecycle of the binding is tied to the object,
2948 * and that you can have only one binding per object property.
2949 * If you bind the same property twice on the same object, the second
2950 * binding overrides the first one.
2955 g_settings_bind_writable (GSettings *settings,
2958 const gchar *property,
2961 GSettingsWritableBinding *binding;
2962 gchar *detailed_signal;
2965 g_return_if_fail (G_IS_SETTINGS (settings));
2967 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2970 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2971 property, G_OBJECT_TYPE_NAME (object));
2974 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2976 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2977 property, G_OBJECT_TYPE_NAME (object));
2981 binding = g_slice_new (GSettingsWritableBinding);
2982 binding->settings = g_object_ref (settings);
2983 binding->object = object;
2984 binding->key = g_intern_string (key);
2985 binding->property = g_intern_string (property);
2986 binding->inverted = inverted;
2988 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2989 binding->handler_id =
2990 g_signal_connect (settings, detailed_signal,
2991 G_CALLBACK (g_settings_binding_writable_changed),
2993 g_free (detailed_signal);
2995 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2996 binding, g_settings_writable_binding_free);
2998 g_settings_binding_writable_changed (settings, binding->key, binding);
3002 * g_settings_unbind:
3003 * @object: the object
3004 * @property: the property whose binding is removed
3006 * Removes an existing binding for @property on @object.
3008 * Note that bindings are automatically removed when the
3009 * object is finalized, so it is rarely necessary to call this
3015 g_settings_unbind (gpointer object,
3016 const gchar *property)
3018 GQuark binding_quark;
3020 binding_quark = g_settings_binding_quark (property);
3021 g_object_set_qdata (object, binding_quark, NULL);
3026 /* vim:set foldmethod=marker: */