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 g_assert (settings->priv->backend == backend);
316 for (i = 0; key[i] == settings->priv->path[i]; i++);
318 if (settings->priv->path[i] == '\0' &&
319 g_settings_schema_has_key (settings->priv->schema, key + i))
323 quark = g_quark_from_string (key + i);
324 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
325 0, &quark, 1, &ignore_this);
330 settings_backend_path_changed (GObject *target,
331 GSettingsBackend *backend,
335 GSettings *settings = G_SETTINGS (target);
336 gboolean ignore_this;
338 g_assert (settings->priv->backend == backend);
340 if (g_str_has_prefix (settings->priv->path, path))
341 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
342 0, NULL, 0, &ignore_this);
346 settings_backend_keys_changed (GObject *target,
347 GSettingsBackend *backend,
349 const gchar * const *items,
352 GSettings *settings = G_SETTINGS (target);
353 gboolean ignore_this;
356 g_assert (settings->priv->backend == backend);
358 for (i = 0; settings->priv->path[i] &&
359 settings->priv->path[i] == path[i]; i++);
366 for (j = 0; items[j]; j++)
368 const gchar *item = items[j];
371 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
373 if (settings->priv->path[i + k] == '\0' &&
374 g_settings_schema_has_key (settings->priv->schema, item + k))
375 quarks[l++] = g_quark_from_string (item + k);
377 /* "256 quarks ought to be enough for anybody!"
378 * If this bites you, I'm sorry. Please file a bug.
384 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
385 0, quarks, l, &ignore_this);
390 settings_backend_writable_changed (GObject *target,
391 GSettingsBackend *backend,
394 GSettings *settings = G_SETTINGS (target);
395 gboolean ignore_this;
398 g_assert (settings->priv->backend == backend);
400 for (i = 0; key[i] == settings->priv->path[i]; i++);
402 if (settings->priv->path[i] == '\0' &&
403 g_settings_schema_has_key (settings->priv->schema, key + i))
404 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
405 0, g_quark_from_string (key + i), &ignore_this);
409 settings_backend_path_writable_changed (GObject *target,
410 GSettingsBackend *backend,
413 GSettings *settings = G_SETTINGS (target);
414 gboolean ignore_this;
416 g_assert (settings->priv->backend == backend);
418 if (g_str_has_prefix (settings->priv->path, path))
419 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
420 0, (GQuark) 0, &ignore_this);
423 /* Properties, Construction, Destruction {{{1 */
425 g_settings_set_property (GObject *object,
430 GSettings *settings = G_SETTINGS (object);
435 g_assert (settings->priv->schema_name == NULL);
436 settings->priv->schema_name = g_value_dup_string (value);
440 settings->priv->path = g_value_dup_string (value);
444 settings->priv->backend = g_value_dup_object (value);
448 g_assert_not_reached ();
453 g_settings_get_property (GObject *object,
458 GSettings *settings = G_SETTINGS (object);
463 g_value_set_string (value, settings->priv->schema_name);
467 g_value_set_object (value, settings->priv->backend);
471 g_value_set_string (value, settings->priv->path);
474 case PROP_HAS_UNAPPLIED:
475 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
478 case PROP_DELAY_APPLY:
479 g_value_set_boolean (value, settings->priv->delayed != NULL);
483 g_assert_not_reached ();
487 static const GSettingsListenerVTable listener_vtable = {
488 settings_backend_changed,
489 settings_backend_path_changed,
490 settings_backend_keys_changed,
491 settings_backend_writable_changed,
492 settings_backend_path_writable_changed
496 g_settings_constructed (GObject *object)
498 GSettings *settings = G_SETTINGS (object);
499 const gchar *schema_path;
501 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
502 schema_path = g_settings_schema_get_path (settings->priv->schema);
504 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
505 g_error ("settings object created with schema '%s' and path '%s', but "
506 "path '%s' is specified by schema",
507 settings->priv->schema_name, settings->priv->path, schema_path);
509 if (settings->priv->path == NULL)
511 if (schema_path == NULL)
512 g_error ("attempting to create schema '%s' without a path",
513 settings->priv->schema_name);
515 settings->priv->path = g_strdup (schema_path);
518 if (settings->priv->backend == NULL)
519 settings->priv->backend = g_settings_backend_get_default ();
521 g_settings_backend_watch (settings->priv->backend,
522 &listener_vtable, G_OBJECT (settings),
523 settings->priv->main_context);
524 g_settings_backend_subscribe (settings->priv->backend,
525 settings->priv->path);
529 g_settings_finalize (GObject *object)
531 GSettings *settings = G_SETTINGS (object);
533 g_settings_backend_unsubscribe (settings->priv->backend,
534 settings->priv->path);
535 g_main_context_unref (settings->priv->main_context);
536 g_object_unref (settings->priv->backend);
537 g_object_unref (settings->priv->schema);
538 g_free (settings->priv->schema_name);
539 g_free (settings->priv->path);
541 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
545 g_settings_init (GSettings *settings)
547 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
551 settings->priv->main_context = g_main_context_get_thread_default ();
553 if (settings->priv->main_context == NULL)
554 settings->priv->main_context = g_main_context_default ();
556 g_main_context_ref (settings->priv->main_context);
560 g_settings_class_init (GSettingsClass *class)
562 GObjectClass *object_class = G_OBJECT_CLASS (class);
564 class->writable_change_event = g_settings_real_writable_change_event;
565 class->change_event = g_settings_real_change_event;
567 object_class->set_property = g_settings_set_property;
568 object_class->get_property = g_settings_get_property;
569 object_class->constructed = g_settings_constructed;
570 object_class->finalize = g_settings_finalize;
572 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
575 * GSettings::changed:
576 * @settings: the object on which the signal was emitted
577 * @key: the name of the key that changed
579 * The "changed" signal is emitted when a key has potentially changed.
580 * You should call one of the g_settings_get() calls to check the new
583 * This signal supports detailed connections. You can connect to the
584 * detailed signal "changed::x" in order to only receive callbacks
585 * when key "x" changes.
587 g_settings_signals[SIGNAL_CHANGED] =
588 g_signal_new ("changed", G_TYPE_SETTINGS,
589 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
590 G_STRUCT_OFFSET (GSettingsClass, changed),
591 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
592 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
595 * GSettings::change-event:
596 * @settings: the object on which the signal was emitted
597 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
598 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
599 * @n_keys: the length of the @keys array, or 0
600 * @returns: %TRUE to stop other handlers from being invoked for the
601 * event. FALSE to propagate the event further.
603 * The "change-event" signal is emitted once per change event that
604 * affects this settings object. You should connect to this signal
605 * only if you are interested in viewing groups of changes before they
606 * are split out into multiple emissions of the "changed" signal.
607 * For most use cases it is more appropriate to use the "changed" signal.
609 * In the event that the change event applies to one or more specified
610 * keys, @keys will be an array of #GQuark of length @n_keys. In the
611 * event that the change event applies to the #GSettings object as a
612 * whole (ie: potentially every key has been changed) then @keys will
613 * be %NULL and @n_keys will be 0.
615 * The default handler for this signal invokes the "changed" signal
616 * for each affected key. If any other connected handler returns
617 * %TRUE then this default functionality will be supressed.
619 g_settings_signals[SIGNAL_CHANGE_EVENT] =
620 g_signal_new ("change-event", G_TYPE_SETTINGS,
622 G_STRUCT_OFFSET (GSettingsClass, change_event),
623 g_signal_accumulator_true_handled, NULL,
624 _gio_marshal_BOOL__POINTER_INT,
625 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
628 * GSettings::writable-changed:
629 * @settings: the object on which the signal was emitted
632 * The "writable-changed" signal is emitted when the writability of a
633 * key has potentially changed. You should call
634 * g_settings_is_writable() in order to determine the new status.
636 * This signal supports detailed connections. You can connect to the
637 * detailed signal "writable-changed::x" in order to only receive
638 * callbacks when the writability of "x" changes.
640 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
641 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
642 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
643 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
644 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
645 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
648 * GSettings::writable-change-event:
649 * @settings: the object on which the signal was emitted
650 * @key: the quark of the key, or 0
651 * @returns: %TRUE to stop other handlers from being invoked for the
652 * event. FALSE to propagate the event further.
654 * The "writable-change-event" signal is emitted once per writability
655 * change event that affects this settings object. You should connect
656 * to this signal if you are interested in viewing groups of changes
657 * before they are split out into multiple emissions of the
658 * "writable-changed" signal. For most use cases it is more
659 * appropriate to use the "writable-changed" signal.
661 * In the event that the writability change applies only to a single
662 * key, @key will be set to the #GQuark for that key. In the event
663 * that the writability change affects the entire settings object,
666 * The default handler for this signal invokes the "writable-changed"
667 * and "changed" signals for each affected key. This is done because
668 * changes in writability might also imply changes in value (if for
669 * example, a new mandatory setting is introduced). If any other
670 * connected handler returns %TRUE then this default functionality
673 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
674 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
676 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
677 g_signal_accumulator_true_handled, NULL,
678 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
683 * The name of the context that the settings are stored in.
685 g_object_class_install_property (object_class, PROP_BACKEND,
686 g_param_spec_object ("backend",
687 P_("GSettingsBackend"),
688 P_("The GSettingsBackend for this settings object"),
689 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
690 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695 * The name of the schema that describes the types of keys
696 * for this #GSettings object.
698 g_object_class_install_property (object_class, PROP_SCHEMA,
699 g_param_spec_string ("schema",
701 P_("The name of the schema for this settings object"),
703 G_PARAM_CONSTRUCT_ONLY |
704 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
709 * The path within the backend where the settings are stored.
711 g_object_class_install_property (object_class, PROP_PATH,
712 g_param_spec_string ("path",
714 P_("The path within the backend where the settings are"),
716 G_PARAM_CONSTRUCT_ONLY |
717 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
720 * GSettings:has-unapplied:
722 * If this property is %TRUE, the #GSettings object has outstanding
723 * changes that will be applied when g_settings_apply() is called.
725 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
726 g_param_spec_boolean ("has-unapplied",
727 P_("Has unapplied changes"),
728 P_("TRUE if there are outstanding changes to apply()"),
730 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
733 * GSettings:delay-apply:
735 * Whether the #GSettings object is in 'delay-apply' mode. See
736 * g_settings_delay() for details.
740 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
741 g_param_spec_boolean ("delay-apply",
742 P_("Delay-apply mode"),
743 P_("Whether this settings object is in 'delay-apply' mode"),
745 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
748 /* Construction (new, new_with_path, etc.) {{{1 */
751 * @schema: the name of the schema
752 * @returns: a new #GSettings object
754 * Creates a new #GSettings object with a given schema.
756 * Signals on the newly created #GSettings object will be dispatched
757 * via the thread-default #GMainContext in effect at the time of the
758 * call to g_settings_new(). The new #GSettings will hold a reference
759 * on the context. See g_main_context_push_thread_default().
764 g_settings_new (const gchar *schema)
766 g_return_val_if_fail (schema != NULL, NULL);
768 return g_object_new (G_TYPE_SETTINGS,
774 * g_settings_new_with_path:
775 * @schema: the name of the schema
776 * @path: the path to use
777 * @returns: a new #GSettings object
779 * Creates a new #GSettings object with a given schema and path.
781 * You only need to do this if you want to directly create a settings
782 * object with a schema that doesn't have a specified path of its own.
785 * It is a programmer error to call this function for a schema that
786 * has an explicitly specified path.
791 g_settings_new_with_path (const gchar *schema,
794 g_return_val_if_fail (schema != NULL, NULL);
795 g_return_val_if_fail (path != NULL, NULL);
797 return g_object_new (G_TYPE_SETTINGS,
804 * g_settings_new_with_backend:
805 * @schema: the name of the schema
806 * @backend: the #GSettingsBackend to use
807 * @returns: a new #GSettings object
809 * Creates a new #GSettings object with a given schema and backend.
811 * Creating settings objects with an different backend allows accessing settings
812 * from a database other than the usual one. For example, it may make
813 * sense to pass a backend corresponding to the "defaults" settings database on
814 * the system to get a settings object that modifies the system default
815 * settings instead of the settings for this user.
820 g_settings_new_with_backend (const gchar *schema,
821 GSettingsBackend *backend)
823 g_return_val_if_fail (schema != NULL, NULL);
824 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
826 return g_object_new (G_TYPE_SETTINGS,
833 * g_settings_new_with_backend_and_path:
834 * @schema: the name of the schema
835 * @backend: the #GSettingsBackend to use
836 * @path: the path to use
837 * @returns: a new #GSettings object
839 * Creates a new #GSettings object with a given schema, backend and
842 * This is a mix of g_settings_new_with_backend() and
843 * g_settings_new_with_path().
848 g_settings_new_with_backend_and_path (const gchar *schema,
849 GSettingsBackend *backend,
852 g_return_val_if_fail (schema != NULL, NULL);
853 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
854 g_return_val_if_fail (path != NULL, NULL);
856 return g_object_new (G_TYPE_SETTINGS,
863 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
869 GSettingsSchema *schema;
874 const guint32 *strinfo;
875 gsize strinfo_length;
877 const gchar *unparsed;
880 const GVariantType *type;
881 GVariant *minimum, *maximum;
882 GVariant *default_value;
886 endian_fixup (GVariant **value)
888 #if G_BYTE_ORDER == G_BIG_ENDIAN
891 tmp = g_variant_byteswap (*value);
892 g_variant_unref (*value);
898 g_settings_get_key_info (GSettingsKeyInfo *info,
906 memset (info, 0, sizeof *info);
908 iter = g_settings_schema_get_value (settings->priv->schema, key);
910 info->default_value = g_variant_iter_next_value (iter);
911 endian_fixup (&info->default_value);
912 info->type = g_variant_get_type (info->default_value);
913 info->settings = g_object_ref (settings);
914 info->key = g_intern_string (key);
916 while (g_variant_iter_next (iter, "(y*)", &code, &data))
921 /* translation requested */
922 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
926 /* enumerated types... */
927 info->is_enum = TRUE;
932 info->is_flags = TRUE;
936 /* ..., choices, aliases */
937 info->strinfo = g_variant_get_fixed_array (data,
938 &info->strinfo_length,
943 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
944 endian_fixup (&info->minimum);
945 endian_fixup (&info->maximum);
949 g_warning ("unknown schema extension '%c'", code);
953 g_variant_unref (data);
956 g_variant_iter_free (iter);
960 g_settings_free_key_info (GSettingsKeyInfo *info)
963 g_variant_unref (info->minimum);
966 g_variant_unref (info->maximum);
968 g_variant_unref (info->default_value);
969 g_object_unref (info->settings);
973 g_settings_write_to_backend (GSettingsKeyInfo *info,
979 path = g_strconcat (info->settings->priv->path, info->key, NULL);
980 success = g_settings_backend_write (info->settings->priv->backend,
988 g_settings_type_check (GSettingsKeyInfo *info,
991 g_return_val_if_fail (value != NULL, FALSE);
993 return g_variant_is_of_type (value, info->type);
997 g_settings_key_info_range_check (GSettingsKeyInfo *info,
1000 if (info->minimum == NULL && info->strinfo == NULL)
1003 if (g_variant_is_container (value))
1009 g_variant_iter_init (&iter, value);
1010 while (ok && (child = g_variant_iter_next_value (&iter)))
1012 ok = g_settings_key_info_range_check (info, child);
1013 g_variant_unref (child);
1021 return g_variant_compare (info->minimum, value) <= 0 &&
1022 g_variant_compare (value, info->maximum) <= 0;
1025 return strinfo_is_string_valid (info->strinfo,
1026 info->strinfo_length,
1027 g_variant_get_string (value, NULL));
1031 g_settings_range_fixup (GSettingsKeyInfo *info,
1034 const gchar *target;
1036 if (g_settings_key_info_range_check (info, value))
1037 return g_variant_ref (value);
1039 if (info->strinfo == NULL)
1042 if (g_variant_is_container (value))
1044 GVariantBuilder builder;
1048 g_variant_iter_init (&iter, value);
1049 g_variant_builder_init (&builder, g_variant_get_type (value));
1051 while ((child = g_variant_iter_next_value (&iter)))
1055 fixed = g_settings_range_fixup (info, child);
1056 g_variant_unref (child);
1060 g_variant_builder_clear (&builder);
1064 g_variant_builder_add_value (&builder, fixed);
1065 g_variant_unref (fixed);
1068 return g_variant_ref_sink (g_variant_builder_end (&builder));
1071 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1072 g_variant_get_string (value, NULL));
1073 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1077 g_settings_read_from_backend (GSettingsKeyInfo *info)
1083 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1084 value = g_settings_backend_read (info->settings->priv->backend,
1085 path, info->type, FALSE);
1090 fixup = g_settings_range_fixup (info, value);
1091 g_variant_unref (value);
1100 g_settings_get_translated_default (GSettingsKeyInfo *info)
1102 const gchar *translated;
1103 GError *error = NULL;
1104 const gchar *domain;
1107 if (info->lc_char == '\0')
1108 /* translation not requested for this key */
1111 domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
1113 if (info->lc_char == 't')
1114 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1116 translated = g_dgettext (domain, info->unparsed);
1118 if (translated == info->unparsed)
1119 /* the default value was not translated */
1122 /* try to parse the translation of the unparsed default */
1123 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1127 g_warning ("Failed to parse translated string `%s' for "
1128 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1129 info->settings->priv->schema_name, error->message);
1130 g_warning ("Using untranslated default instead.");
1131 g_error_free (error);
1134 else if (!g_settings_key_info_range_check (info, value))
1136 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1137 "is outside of valid range", info->unparsed, info->key,
1138 info->settings->priv->schema_name);
1139 g_variant_unref (value);
1147 g_settings_to_enum (GSettingsKeyInfo *info,
1153 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1154 g_variant_get_string (value, NULL),
1157 /* 'value' can only come from the backend after being filtered for validity,
1158 * from the translation after being filtered for validity, or from the schema
1159 * itself (which the schema compiler checks for validity). If this assertion
1160 * fails then it's really a bug in GSettings or the schema compiler...
1162 g_assert (it_worked);
1168 g_settings_from_enum (GSettingsKeyInfo *info,
1171 const gchar *string;
1173 string = strinfo_string_from_enum (info->strinfo,
1174 info->strinfo_length,
1180 return g_variant_new_string (string);
1184 g_settings_to_flags (GSettingsKeyInfo *info,
1192 g_variant_iter_init (&iter, value);
1193 while (g_variant_iter_next (&iter, "&s", &flag))
1198 it_worked = strinfo_enum_from_string (info->strinfo,
1199 info->strinfo_length,
1201 /* as in g_settings_to_enum() */
1202 g_assert (it_worked);
1204 result |= flag_value;
1211 g_settings_from_flags (GSettingsKeyInfo *info,
1214 GVariantBuilder builder;
1217 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1219 for (i = 0; i < 32; i++)
1220 if (value & (1u << i))
1222 const gchar *string;
1224 string = strinfo_string_from_enum (info->strinfo,
1225 info->strinfo_length,
1230 g_variant_builder_clear (&builder);
1234 g_variant_builder_add (&builder, "s", string);
1237 return g_variant_builder_end (&builder);
1240 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1242 * g_settings_get_value:
1243 * @settings: a #GSettings object
1244 * @key: the key to get the value for
1245 * @returns: a new #GVariant
1247 * Gets the value that is stored in @settings for @key.
1249 * It is a programmer error to give a @key that isn't contained in the
1250 * schema for @settings.
1255 g_settings_get_value (GSettings *settings,
1258 GSettingsKeyInfo info;
1261 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1262 g_return_val_if_fail (key != NULL, NULL);
1264 g_settings_get_key_info (&info, settings, key);
1265 value = g_settings_read_from_backend (&info);
1268 value = g_settings_get_translated_default (&info);
1271 value = g_variant_ref (info.default_value);
1273 g_settings_free_key_info (&info);
1279 * g_settings_get_enum:
1280 * @settings: a #GSettings object
1281 * @key: the key to get the value for
1282 * @returns: the enum value
1284 * Gets the value that is stored in @settings for @key and converts it
1285 * to the enum value that it represents.
1287 * In order to use this function the type of the value must be a string
1288 * and it must be marked in the schema file as an enumerated type.
1290 * It is a programmer error to give a @key that isn't contained in the
1291 * schema for @settings or is not marked as an enumerated type.
1293 * If the value stored in the configuration database is not a valid
1294 * value for the enumerated type then this function will return the
1300 g_settings_get_enum (GSettings *settings,
1303 GSettingsKeyInfo info;
1307 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1308 g_return_val_if_fail (key != NULL, -1);
1310 g_settings_get_key_info (&info, settings, key);
1314 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1315 "associated with an enumerated type", info.key);
1316 g_settings_free_key_info (&info);
1320 value = g_settings_read_from_backend (&info);
1323 value = g_settings_get_translated_default (&info);
1326 value = g_variant_ref (info.default_value);
1328 result = g_settings_to_enum (&info, value);
1329 g_settings_free_key_info (&info);
1330 g_variant_unref (value);
1336 * g_settings_set_enum:
1337 * @settings: a #GSettings object
1338 * @key: a key, within @settings
1339 * @value: an enumerated value
1340 * @returns: %TRUE, if the set succeeds
1342 * Looks up the enumerated type nick for @value and writes it to @key,
1345 * It is a programmer error to give a @key that isn't contained in the
1346 * schema for @settings or is not marked as an enumerated type, or for
1347 * @value not to be a valid value for the named type.
1349 * After performing the write, accessing @key directly with
1350 * g_settings_get_string() will return the 'nick' associated with
1354 g_settings_set_enum (GSettings *settings,
1358 GSettingsKeyInfo info;
1362 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1363 g_return_val_if_fail (key != NULL, FALSE);
1365 g_settings_get_key_info (&info, settings, key);
1369 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1370 "associated with an enumerated type", info.key);
1374 if (!(variant = g_settings_from_enum (&info, value)))
1376 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1377 "in schema `%s'. Doing nothing.", value, info.key,
1378 info.settings->priv->schema_name);
1379 g_settings_free_key_info (&info);
1383 success = g_settings_write_to_backend (&info, variant);
1384 g_settings_free_key_info (&info);
1390 * g_settings_get_flags:
1391 * @settings: a #GSettings object
1392 * @key: the key to get the value for
1393 * @returns: the flags value
1395 * Gets the value that is stored in @settings for @key and converts it
1396 * to the flags value that it represents.
1398 * In order to use this function the type of the value must be an array
1399 * of strings and it must be marked in the schema file as an flags type.
1401 * It is a programmer error to give a @key that isn't contained in the
1402 * schema for @settings or is not marked as a flags type.
1404 * If the value stored in the configuration database is not a valid
1405 * value for the flags type then this function will return the default
1411 g_settings_get_flags (GSettings *settings,
1414 GSettingsKeyInfo info;
1418 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1419 g_return_val_if_fail (key != NULL, -1);
1421 g_settings_get_key_info (&info, settings, key);
1425 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1426 "associated with a flags type", info.key);
1427 g_settings_free_key_info (&info);
1431 value = g_settings_read_from_backend (&info);
1434 value = g_settings_get_translated_default (&info);
1437 value = g_variant_ref (info.default_value);
1439 result = g_settings_to_flags (&info, value);
1440 g_settings_free_key_info (&info);
1441 g_variant_unref (value);
1447 * g_settings_set_flags:
1448 * @settings: a #GSettings object
1449 * @key: a key, within @settings
1450 * @value: a flags value
1451 * @returns: %TRUE, if the set succeeds
1453 * Looks up the flags type nicks for the bits specified by @value, puts
1454 * them in an array of strings and writes the array to @key, withing
1457 * It is a programmer error to give a @key that isn't contained in the
1458 * schema for @settings or is not marked as a flags type, or for @value
1459 * to contain any bits that are not value for the named type.
1461 * After performing the write, accessing @key directly with
1462 * g_settings_get_strv() will return an array of 'nicks'; one for each
1466 g_settings_set_flags (GSettings *settings,
1470 GSettingsKeyInfo info;
1474 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1475 g_return_val_if_fail (key != NULL, FALSE);
1477 g_settings_get_key_info (&info, settings, key);
1481 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1482 "associated with a flags type", info.key);
1486 if (!(variant = g_settings_from_flags (&info, value)))
1488 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1489 "for key `%s' in schema `%s'. Doing nothing.",
1490 value, info.key, info.settings->priv->schema_name);
1491 g_settings_free_key_info (&info);
1495 success = g_settings_write_to_backend (&info, variant);
1496 g_settings_free_key_info (&info);
1502 * g_settings_set_value:
1503 * @settings: a #GSettings object
1504 * @key: the name of the key to set
1505 * @value: a #GVariant of the correct type
1506 * @returns: %TRUE if setting the key succeeded,
1507 * %FALSE if the key was not writable
1509 * Sets @key in @settings to @value.
1511 * It is a programmer error to give a @key that isn't contained in the
1512 * schema for @settings or for @value to have the incorrect type, per
1515 * If @value is floating then this function consumes the reference.
1520 g_settings_set_value (GSettings *settings,
1524 GSettingsKeyInfo info;
1526 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1527 g_return_val_if_fail (key != NULL, FALSE);
1529 g_settings_get_key_info (&info, settings, key);
1531 if (!g_settings_type_check (&info, value))
1533 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1535 settings->priv->schema_name,
1536 g_variant_type_peek_string (info.type),
1537 g_variant_get_type_string (value));
1542 if (!g_settings_key_info_range_check (&info, value))
1544 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1545 "is outside of valid range",
1547 settings->priv->schema_name);
1552 g_settings_free_key_info (&info);
1554 return g_settings_write_to_backend (&info, value);
1559 * @settings: a #GSettings object
1560 * @key: the key to get the value for
1561 * @format: a #GVariant format string
1562 * @...: arguments as per @format
1564 * Gets the value that is stored at @key in @settings.
1566 * A convenience function that combines g_settings_get_value() with
1569 * It is a programmer error to give a @key that isn't contained in the
1570 * schema for @settings or for the #GVariantType of @format to mismatch
1571 * the type given in the schema.
1576 g_settings_get (GSettings *settings,
1578 const gchar *format,
1584 value = g_settings_get_value (settings, key);
1586 va_start (ap, format);
1587 g_variant_get_va (value, format, NULL, &ap);
1590 g_variant_unref (value);
1595 * @settings: a #GSettings object
1596 * @key: the name of the key to set
1597 * @format: a #GVariant format string
1598 * @...: arguments as per @format
1599 * @returns: %TRUE if setting the key succeeded,
1600 * %FALSE if the key was not writable
1602 * Sets @key in @settings to @value.
1604 * A convenience function that combines g_settings_set_value() with
1607 * It is a programmer error to give a @key that isn't contained in the
1608 * schema for @settings or for the #GVariantType of @format to mismatch
1609 * the type given in the schema.
1614 g_settings_set (GSettings *settings,
1616 const gchar *format,
1622 va_start (ap, format);
1623 value = g_variant_new_va (format, NULL, &ap);
1626 return g_settings_set_value (settings, key, value);
1630 * g_settings_get_mapped:
1631 * @settings: a #GSettings object
1632 * @key: the key to get the value for
1633 * @mapping: (scope call): the function to map the value in the
1634 * settings database to the value used by the application
1635 * @user_data: user data for @mapping
1636 * @returns: (transfer full): the result, which may be %NULL
1638 * Gets the value that is stored at @key in @settings, subject to
1639 * application-level validation/mapping.
1641 * You should use this function when the application needs to perform
1642 * some processing on the value of the key (for example, parsing). The
1643 * @mapping function performs that processing. If the function
1644 * indicates that the processing was unsuccessful (due to a parse error,
1645 * for example) then the mapping is tried again with another value.
1647 * This allows a robust 'fall back to defaults' behaviour to be
1648 * implemented somewhat automatically.
1650 * The first value that is tried is the user's setting for the key. If
1651 * the mapping function fails to map this value, other values may be
1652 * tried in an unspecified order (system or site defaults, translated
1653 * schema default values, untranslated schema default values, etc).
1655 * If the mapping function fails for all possible values, one additional
1656 * attempt is made: the mapping function is called with a %NULL value.
1657 * If the mapping function still indicates failure at this point then
1658 * the application will be aborted.
1660 * The result parameter for the @mapping function is pointed to a
1661 * #gpointer which is initially set to %NULL. The same pointer is given
1662 * to each invocation of @mapping. The final value of that #gpointer is
1663 * what is returned by this function. %NULL is valid; it is returned
1664 * just as any other value would be.
1667 g_settings_get_mapped (GSettings *settings,
1669 GSettingsGetMapping mapping,
1672 gpointer result = NULL;
1673 GSettingsKeyInfo info;
1677 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1678 g_return_val_if_fail (key != NULL, NULL);
1679 g_return_val_if_fail (mapping != NULL, NULL);
1681 g_settings_get_key_info (&info, settings, key);
1683 if ((value = g_settings_read_from_backend (&info)))
1685 okay = mapping (value, &result, user_data);
1686 g_variant_unref (value);
1687 if (okay) goto okay;
1690 if ((value = g_settings_get_translated_default (&info)))
1692 okay = mapping (value, &result, user_data);
1693 g_variant_unref (value);
1694 if (okay) goto okay;
1697 if (mapping (info.default_value, &result, user_data))
1700 if (!mapping (NULL, &result, user_data))
1701 g_error ("The mapping function given to g_settings_get_mapped() for key "
1702 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1703 key, settings->priv->schema_name);
1706 g_settings_free_key_info (&info);
1711 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1713 * g_settings_get_string:
1714 * @settings: a #GSettings object
1715 * @key: the key to get the value for
1716 * @returns: a newly-allocated string
1718 * Gets the value that is stored at @key in @settings.
1720 * A convenience variant of g_settings_get() for strings.
1722 * It is a programmer error to give a @key that isn't specified as
1723 * having a string type in the schema for @settings.
1728 g_settings_get_string (GSettings *settings,
1734 value = g_settings_get_value (settings, key);
1735 result = g_variant_dup_string (value, NULL);
1736 g_variant_unref (value);
1742 * g_settings_set_string:
1743 * @settings: a #GSettings object
1744 * @key: the name of the key to set
1745 * @value: the value to set it to
1746 * @returns: %TRUE if setting the key succeeded,
1747 * %FALSE if the key was not writable
1749 * Sets @key in @settings to @value.
1751 * A convenience variant of g_settings_set() for strings.
1753 * It is a programmer error to give a @key that isn't specified as
1754 * having a string type in the schema for @settings.
1759 g_settings_set_string (GSettings *settings,
1763 return g_settings_set_value (settings, key, g_variant_new_string (value));
1767 * g_settings_get_int:
1768 * @settings: a #GSettings object
1769 * @key: the key to get the value for
1770 * @returns: an integer
1772 * Gets the value that is stored at @key in @settings.
1774 * A convenience variant of g_settings_get() for 32-bit integers.
1776 * It is a programmer error to give a @key that isn't specified as
1777 * having a int32 type in the schema for @settings.
1782 g_settings_get_int (GSettings *settings,
1788 value = g_settings_get_value (settings, key);
1789 result = g_variant_get_int32 (value);
1790 g_variant_unref (value);
1796 * g_settings_set_int:
1797 * @settings: a #GSettings object
1798 * @key: the name of the key to set
1799 * @value: the value to set it to
1800 * @returns: %TRUE if setting the key succeeded,
1801 * %FALSE if the key was not writable
1803 * Sets @key in @settings to @value.
1805 * A convenience variant of g_settings_set() for 32-bit integers.
1807 * It is a programmer error to give a @key that isn't specified as
1808 * having a int32 type in the schema for @settings.
1813 g_settings_set_int (GSettings *settings,
1817 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1821 * g_settings_get_double:
1822 * @settings: a #GSettings object
1823 * @key: the key to get the value for
1824 * @returns: a double
1826 * Gets the value that is stored at @key in @settings.
1828 * A convenience variant of g_settings_get() for doubles.
1830 * It is a programmer error to give a @key that isn't specified as
1831 * having a 'double' type in the schema for @settings.
1836 g_settings_get_double (GSettings *settings,
1842 value = g_settings_get_value (settings, key);
1843 result = g_variant_get_double (value);
1844 g_variant_unref (value);
1850 * g_settings_set_double:
1851 * @settings: a #GSettings object
1852 * @key: the name of the key to set
1853 * @value: the value to set it to
1854 * @returns: %TRUE if setting the key succeeded,
1855 * %FALSE if the key was not writable
1857 * Sets @key in @settings to @value.
1859 * A convenience variant of g_settings_set() for doubles.
1861 * It is a programmer error to give a @key that isn't specified as
1862 * having a 'double' type in the schema for @settings.
1867 g_settings_set_double (GSettings *settings,
1871 return g_settings_set_value (settings, key, g_variant_new_double (value));
1875 * g_settings_get_boolean:
1876 * @settings: a #GSettings object
1877 * @key: the key to get the value for
1878 * @returns: a boolean
1880 * Gets the value that is stored at @key in @settings.
1882 * A convenience variant of g_settings_get() for booleans.
1884 * It is a programmer error to give a @key that isn't specified as
1885 * having a boolean type in the schema for @settings.
1890 g_settings_get_boolean (GSettings *settings,
1896 value = g_settings_get_value (settings, key);
1897 result = g_variant_get_boolean (value);
1898 g_variant_unref (value);
1904 * g_settings_set_boolean:
1905 * @settings: a #GSettings object
1906 * @key: the name of the key to set
1907 * @value: the value to set it to
1908 * @returns: %TRUE if setting the key succeeded,
1909 * %FALSE if the key was not writable
1911 * Sets @key in @settings to @value.
1913 * A convenience variant of g_settings_set() for booleans.
1915 * It is a programmer error to give a @key that isn't specified as
1916 * having a boolean type in the schema for @settings.
1921 g_settings_set_boolean (GSettings *settings,
1925 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1929 * g_settings_get_strv:
1930 * @settings: a #GSettings object
1931 * @key: the key to get the value for
1932 * @returns: (array zero-terminated=1) (transfer full): a
1933 * newly-allocated, %NULL-terminated array of strings, the value that
1934 * is stored at @key in @settings.
1936 * A convenience variant of g_settings_get() for string arrays.
1938 * It is a programmer error to give a @key that isn't specified as
1939 * having an array of strings type in the schema for @settings.
1944 g_settings_get_strv (GSettings *settings,
1950 value = g_settings_get_value (settings, key);
1951 result = g_variant_dup_strv (value, NULL);
1952 g_variant_unref (value);
1958 * g_settings_set_strv:
1959 * @settings: a #GSettings object
1960 * @key: the name of the key to set
1961 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1962 * @returns: %TRUE if setting the key succeeded,
1963 * %FALSE if the key was not writable
1965 * Sets @key in @settings to @value.
1967 * A convenience variant of g_settings_set() for string arrays. If
1968 * @value is %NULL, then @key is set to be the empty array.
1970 * It is a programmer error to give a @key that isn't specified as
1971 * having an array of strings type in the schema for @settings.
1976 g_settings_set_strv (GSettings *settings,
1978 const gchar * const *value)
1983 array = g_variant_new_strv (value, -1);
1985 array = g_variant_new_strv (NULL, 0);
1987 return g_settings_set_value (settings, key, array);
1990 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1993 * @settings: a #GSettings object
1995 * Changes the #GSettings object into 'delay-apply' mode. In this
1996 * mode, changes to @settings are not immediately propagated to the
1997 * backend, but kept locally until g_settings_apply() is called.
2002 g_settings_delay (GSettings *settings)
2004 g_return_if_fail (G_IS_SETTINGS (settings));
2006 if (settings->priv->delayed)
2009 settings->priv->delayed =
2010 g_delayed_settings_backend_new (settings->priv->backend,
2012 settings->priv->main_context);
2013 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2014 g_object_unref (settings->priv->backend);
2016 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2017 g_settings_backend_watch (settings->priv->backend,
2018 &listener_vtable, G_OBJECT (settings),
2019 settings->priv->main_context);
2021 g_object_notify (G_OBJECT (settings), "delay-apply");
2026 * @settings: a #GSettings instance
2028 * Applies any changes that have been made to the settings. This
2029 * function does nothing unless @settings is in 'delay-apply' mode;
2030 * see g_settings_delay(). In the normal case settings are always
2031 * applied immediately.
2034 g_settings_apply (GSettings *settings)
2036 if (settings->priv->delayed)
2038 GDelayedSettingsBackend *delayed;
2040 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2041 g_delayed_settings_backend_apply (delayed);
2046 * g_settings_revert:
2047 * @settings: a #GSettings instance
2049 * Reverts all non-applied changes to the settings. This function
2050 * does nothing unless @settings is in 'delay-apply' mode; see
2051 * g_settings_delay(). In the normal case settings are always applied
2054 * Change notifications will be emitted for affected keys.
2057 g_settings_revert (GSettings *settings)
2059 if (settings->priv->delayed)
2061 GDelayedSettingsBackend *delayed;
2063 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2064 g_delayed_settings_backend_revert (delayed);
2069 * g_settings_get_has_unapplied:
2070 * @settings: a #GSettings object
2071 * @returns: %TRUE if @settings has unapplied changes
2073 * Returns whether the #GSettings object has any unapplied
2074 * changes. This can only be the case if it is in 'delayed-apply' mode.
2079 g_settings_get_has_unapplied (GSettings *settings)
2081 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2083 return settings->priv->delayed &&
2084 g_delayed_settings_backend_get_has_unapplied (
2085 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2088 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2091 * @settings: a #GSettings object
2092 * @key: the name of a key
2094 * Resets @key to its default value.
2096 * This call resets the key, as much as possible, to its default value.
2097 * That might the value specified in the schema or the one set by the
2101 g_settings_reset (GSettings *settings,
2106 path = g_strconcat (settings->priv->path, key, NULL);
2107 g_settings_backend_reset (settings->priv->backend, path, NULL);
2114 * Ensures that all pending operations for the given are complete for
2115 * the default backend.
2117 * Writes made to a #GSettings are handled asynchronously. For this
2118 * reason, it is very unlikely that the changes have it to disk by the
2119 * time g_settings_set() returns.
2121 * This call will block until all of the writes have made it to the
2122 * backend. Since the mainloop is not running, no change notifications
2123 * will be dispatched during this call (but some may be queued by the
2124 * time the call is done).
2127 g_settings_sync (void)
2129 g_settings_backend_sync_default ();
2133 * g_settings_is_writable:
2134 * @settings: a #GSettings object
2135 * @name: the name of a key
2136 * @returns: %TRUE if the key @name is writable
2138 * Finds out if a key can be written or not
2143 g_settings_is_writable (GSettings *settings,
2149 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2151 path = g_strconcat (settings->priv->path, name, NULL);
2152 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2159 * g_settings_get_child:
2160 * @settings: a #GSettings object
2161 * @name: the name of the 'child' schema
2162 * @returns: (transfer full): a 'child' settings object
2164 * Creates a 'child' settings object which has a base path of
2165 * <replaceable>base-path</replaceable>/@name, where
2166 * <replaceable>base-path</replaceable> is the base path of @settings.
2168 * The schema for the child settings object must have been declared
2169 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2174 g_settings_get_child (GSettings *settings,
2177 const gchar *child_schema;
2182 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2184 child_name = g_strconcat (name, "/", NULL);
2185 child_schema = g_settings_schema_get_string (settings->priv->schema,
2187 if (child_schema == NULL)
2188 g_error ("Schema '%s' has no child '%s'",
2189 settings->priv->schema_name, name);
2191 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2192 child = g_object_new (G_TYPE_SETTINGS,
2193 "schema", child_schema,
2196 g_free (child_path);
2197 g_free (child_name);
2203 * g_settings_list_keys:
2204 * @settings: a #GSettings object
2205 * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2207 * Introspects the list of keys on @settings.
2209 * You should probably not be calling this function from "normal" code
2210 * (since you should already know what keys are in your schema). This
2211 * function is intended for introspection reasons.
2213 * You should free the return value with g_strfreev() when you are done
2217 g_settings_list_keys (GSettings *settings)
2224 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2225 strv = g_new (gchar *, n_keys + 1);
2226 for (i = j = 0; i < n_keys; i++)
2228 const gchar *key = g_quark_to_string (keys[i]);
2230 if (!g_str_has_suffix (key, "/"))
2231 strv[j++] = g_strdup (key);
2239 * g_settings_list_children:
2240 * @settings: a #GSettings object
2241 * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2243 * Gets the list of children on @settings.
2245 * The list is exactly the list of strings for which it is not an error
2246 * to call g_settings_get_child().
2248 * For GSettings objects that are lists, this value can change at any
2249 * time and you should connect to the "children-changed" signal to watch
2250 * for those changes. Note that there is a race condition here: you may
2251 * request a child after listing it only for it to have been destroyed
2252 * in the meantime. For this reason, g_settings_get_child() may return
2253 * %NULL even for a child that was listed by this function.
2255 * For GSettings objects that are not lists, you should probably not be
2256 * calling this function from "normal" code (since you should already
2257 * know what children are in your schema). This function may still be
2258 * useful there for introspection reasons, however.
2260 * You should free the return value with g_strfreev() when you are done
2264 g_settings_list_children (GSettings *settings)
2271 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2272 strv = g_new (gchar *, n_keys + 1);
2273 for (i = j = 0; i < n_keys; i++)
2275 const gchar *key = g_quark_to_string (keys[i]);
2277 if (g_str_has_suffix (key, "/"))
2279 gint length = strlen (key);
2281 strv[j] = g_memdup (key, length);
2282 strv[j][length - 1] = '\0';
2292 * g_settings_get_range:
2293 * @settings: a #GSettings
2294 * @key: the key to query the range of
2295 * @returns: a #GVariant describing the range
2297 * Queries the range of a key.
2299 * This function will return a #GVariant that fully describes the range
2300 * of values that are valid for @key.
2302 * The type of #GVariant returned is <literal>(sv)</literal>. The
2303 * string describes the type of range restriction in effect. The type
2304 * and meaning of the value contained in the variant depends on the
2307 * If the string is <literal>'type'</literal> then the variant contains
2308 * an empty array. The element type of that empty array is the expected
2309 * type of value and all values of that type are valid.
2311 * If the string is <literal>'enum'</literal> then the variant contains
2312 * an array enumerating the possible values. Each item in the array is
2313 * a possible valid value and no other values are valid.
2315 * If the string is <literal>'flags'</literal> then the variant contains
2316 * an array. Each item in the array is a value that may appear zero or
2317 * one times in an array to be used as the value for this key. For
2318 * example, if the variant contained the array <literal>['x',
2319 * 'y']</literal> then the valid values for the key would be
2320 * <literal>[]</literal>, <literal>['x']</literal>,
2321 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2322 * <literal>['y', 'x']</literal>.
2324 * Finally, if the string is <literal>'range'</literal> then the variant
2325 * contains a pair of like-typed values -- the minimum and maximum
2326 * permissible values for this key.
2328 * This information should not be used by normal programs. It is
2329 * considered to be a hint for introspection purposes. Normal programs
2330 * should already know what is permitted by their own schema. The
2331 * format may change in any way in the future -- but particularly, new
2332 * forms may be added to the possibilities described above.
2334 * It is a programmer error to give a @key that isn't contained in the
2335 * schema for @settings.
2337 * You should free the returned value with g_variant_unref() when it is
2343 g_settings_get_range (GSettings *settings,
2346 GSettingsKeyInfo info;
2350 g_settings_get_key_info (&info, settings, key);
2354 range = g_variant_new ("(**)", info.minimum, info.maximum);
2357 else if (info.strinfo)
2359 range = strinfo_enumerate (info.strinfo, info.strinfo_length);
2360 type = info.is_flags ? "flags" : "enum";
2364 range = g_variant_new_array (info.type, NULL, 0);
2368 g_settings_free_key_info (&info);
2370 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2374 * g_settings_range_check:
2375 * @settings: a #GSettings
2376 * @key: the key to check
2377 * @value: the value to check
2378 * @returns: %TRUE if @value is valid for @key
2380 * Checks if the given @value is of the correct type and within the
2381 * permitted range for @key.
2383 * This API is not intended to be used by normal programs -- they should
2384 * already know what is permitted by their own schemas. This API is
2385 * meant to be used by programs such as editors or commandline tools.
2387 * It is a programmer error to give a @key that isn't contained in the
2388 * schema for @settings.
2393 g_settings_range_check (GSettings *settings,
2397 GSettingsKeyInfo info;
2400 g_settings_get_key_info (&info, settings, key);
2401 good = g_settings_type_check (&info, value) &&
2402 g_settings_key_info_range_check (&info, value);
2403 g_settings_free_key_info (&info);
2411 GSettingsKeyInfo info;
2414 GSettingsBindGetMapping get_mapping;
2415 GSettingsBindSetMapping set_mapping;
2417 GDestroyNotify destroy;
2419 guint writable_handler_id;
2420 guint property_handler_id;
2421 const GParamSpec *property;
2422 guint key_handler_id;
2424 /* prevent recursion */
2429 g_settings_binding_free (gpointer data)
2431 GSettingsBinding *binding = data;
2433 g_assert (!binding->running);
2435 if (binding->writable_handler_id)
2436 g_signal_handler_disconnect (binding->info.settings,
2437 binding->writable_handler_id);
2439 if (binding->key_handler_id)
2440 g_signal_handler_disconnect (binding->info.settings,
2441 binding->key_handler_id);
2443 if (g_signal_handler_is_connected (binding->object,
2444 binding->property_handler_id))
2445 g_signal_handler_disconnect (binding->object,
2446 binding->property_handler_id);
2448 g_settings_free_key_info (&binding->info);
2450 if (binding->destroy)
2451 binding->destroy (binding->user_data);
2453 g_slice_free (GSettingsBinding, binding);
2457 g_settings_binding_quark (const char *property)
2462 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2463 quark = g_quark_from_string (tmp);
2470 g_settings_binding_key_changed (GSettings *settings,
2474 GSettingsBinding *binding = user_data;
2475 GValue value = { 0, };
2478 g_assert (settings == binding->info.settings);
2479 g_assert (key == binding->info.key);
2481 if (binding->running)
2484 binding->running = TRUE;
2486 g_value_init (&value, binding->property->value_type);
2488 variant = g_settings_read_from_backend (&binding->info);
2489 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2491 /* silently ignore errors in the user's config database */
2492 g_variant_unref (variant);
2496 if (variant == NULL)
2498 variant = g_settings_get_translated_default (&binding->info);
2500 !binding->get_mapping (&value, variant, binding->user_data))
2502 /* flag translation errors with a warning */
2503 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2504 "was rejected by the binding mapping function",
2505 binding->info.unparsed, binding->info.key,
2506 binding->info.settings->priv->schema_name);
2507 g_variant_unref (variant);
2512 if (variant == NULL)
2514 variant = g_variant_ref (binding->info.default_value);
2515 if (!binding->get_mapping (&value, variant, binding->user_data))
2516 g_error ("The schema default value for key `%s' in schema `%s' "
2517 "was rejected by the binding mapping function.",
2519 binding->info.settings->priv->schema_name);
2522 g_object_set_property (binding->object, binding->property->name, &value);
2523 g_variant_unref (variant);
2524 g_value_unset (&value);
2526 binding->running = FALSE;
2530 g_settings_binding_property_changed (GObject *object,
2531 const GParamSpec *pspec,
2534 GSettingsBinding *binding = user_data;
2535 GValue value = { 0, };
2538 g_assert (object == binding->object);
2539 g_assert (pspec == binding->property);
2541 if (binding->running)
2544 binding->running = TRUE;
2546 g_value_init (&value, pspec->value_type);
2547 g_object_get_property (object, pspec->name, &value);
2548 if ((variant = binding->set_mapping (&value, binding->info.type,
2549 binding->user_data)))
2551 if (g_variant_is_floating (variant))
2552 g_variant_ref_sink (variant);
2554 if (!g_settings_type_check (&binding->info, variant))
2556 g_critical ("binding mapping function for key `%s' returned "
2557 "GVariant of type `%s' when type `%s' was requested",
2558 binding->info.key, g_variant_get_type_string (variant),
2559 g_variant_type_dup_string (binding->info.type));
2563 if (!g_settings_key_info_range_check (&binding->info, variant))
2565 g_critical ("GObject property `%s' on a `%s' object is out of "
2566 "schema-specified range for key `%s' of `%s': %s",
2567 binding->property->name,
2568 g_type_name (binding->property->owner_type),
2570 binding->info.settings->priv->schema_name,
2571 g_variant_print (variant, TRUE));
2575 g_settings_write_to_backend (&binding->info, variant);
2576 g_variant_unref (variant);
2578 g_value_unset (&value);
2580 binding->running = FALSE;
2584 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2588 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2593 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2594 const GVariantType *expected_type,
2597 return g_variant_new_boolean (!g_value_get_boolean (value));
2602 * @settings: a #GSettings object
2603 * @key: the key to bind
2604 * @object: (type GObject.Object): a #GObject
2605 * @property: the name of the property to bind
2606 * @flags: flags for the binding
2608 * Create a binding between the @key in the @settings object
2609 * and the property @property of @object.
2611 * The binding uses the default GIO mapping functions to map
2612 * between the settings and property values. These functions
2613 * handle booleans, numeric types and string types in a
2614 * straightforward way. Use g_settings_bind_with_mapping() if
2615 * you need a custom mapping, or map between types that are not
2616 * supported by the default mapping functions.
2618 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2619 * function also establishes a binding between the writability of
2620 * @key and the "sensitive" property of @object (if @object has
2621 * a boolean property by that name). See g_settings_bind_writable()
2622 * for more details about writable bindings.
2624 * Note that the lifecycle of the binding is tied to the object,
2625 * and that you can have only one binding per object property.
2626 * If you bind the same property twice on the same object, the second
2627 * binding overrides the first one.
2632 g_settings_bind (GSettings *settings,
2635 const gchar *property,
2636 GSettingsBindFlags flags)
2638 GSettingsBindGetMapping get_mapping = NULL;
2639 GSettingsBindSetMapping set_mapping = NULL;
2641 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2643 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2644 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2646 /* can't pass this flag to g_settings_bind_with_mapping() */
2647 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2650 g_settings_bind_with_mapping (settings, key, object, property, flags,
2651 get_mapping, set_mapping, NULL, NULL);
2655 * g_settings_bind_with_mapping: (skip)
2656 * @settings: a #GSettings object
2657 * @key: the key to bind
2658 * @object: (type GObject.Object): a #GObject
2659 * @property: the name of the property to bind
2660 * @flags: flags for the binding
2661 * @get_mapping: a function that gets called to convert values
2662 * from @settings to @object, or %NULL to use the default GIO mapping
2663 * @set_mapping: a function that gets called to convert values
2664 * from @object to @settings, or %NULL to use the default GIO mapping
2665 * @user_data: data that gets passed to @get_mapping and @set_mapping
2666 * @destroy: #GDestroyNotify function for @user_data
2668 * Create a binding between the @key in the @settings object
2669 * and the property @property of @object.
2671 * The binding uses the provided mapping functions to map between
2672 * settings and property values.
2674 * Note that the lifecycle of the binding is tied to the object,
2675 * and that you can have only one binding per object property.
2676 * If you bind the same property twice on the same object, the second
2677 * binding overrides the first one.
2682 g_settings_bind_with_mapping (GSettings *settings,
2685 const gchar *property,
2686 GSettingsBindFlags flags,
2687 GSettingsBindGetMapping get_mapping,
2688 GSettingsBindSetMapping set_mapping,
2690 GDestroyNotify destroy)
2692 GSettingsBinding *binding;
2693 GObjectClass *objectclass;
2694 gchar *detailed_signal;
2695 GQuark binding_quark;
2697 g_return_if_fail (G_IS_SETTINGS (settings));
2698 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2700 objectclass = G_OBJECT_GET_CLASS (object);
2702 binding = g_slice_new0 (GSettingsBinding);
2703 g_settings_get_key_info (&binding->info, settings, key);
2704 binding->object = object;
2705 binding->property = g_object_class_find_property (objectclass, property);
2706 binding->user_data = user_data;
2707 binding->destroy = destroy;
2708 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2709 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2711 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2712 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2714 if (binding->property == NULL)
2716 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2717 property, G_OBJECT_TYPE_NAME (object));
2721 if ((flags & G_SETTINGS_BIND_GET) &&
2722 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2724 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2725 "writable", property, G_OBJECT_TYPE_NAME (object));
2728 if ((flags & G_SETTINGS_BIND_SET) &&
2729 (binding->property->flags & G_PARAM_READABLE) == 0)
2731 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2732 "readable", property, G_OBJECT_TYPE_NAME (object));
2736 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2738 /* g_settings_bind_invert_boolean_get_mapping() is a private
2739 * function, so if we are here it means that g_settings_bind() was
2740 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2742 * Ensure that both sides are boolean.
2745 if (binding->property->value_type != G_TYPE_BOOLEAN)
2747 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2748 "was specified, but property `%s' on type `%s' has "
2749 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2750 g_type_name ((binding->property->value_type)));
2754 if (!g_variant_type_equal (binding->info.type, G_VARIANT_TYPE_BOOLEAN))
2756 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2757 "was specified, but key `%s' on schema `%s' has "
2758 "type `%s'", key, settings->priv->schema_name,
2759 g_variant_type_dup_string (binding->info.type));
2765 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2766 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2767 !g_settings_mapping_is_compatible (binding->property->value_type,
2768 binding->info.type))
2770 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2771 "'%s' which is not compatible with type '%s' of key '%s' "
2772 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2773 g_type_name (binding->property->value_type),
2774 g_variant_type_dup_string (binding->info.type), key,
2775 settings->priv->schema_name);
2779 if ((flags & G_SETTINGS_BIND_SET) &&
2780 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2782 GParamSpec *sensitive;
2784 sensitive = g_object_class_find_property (objectclass, "sensitive");
2786 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2787 (sensitive->flags & G_PARAM_WRITABLE))
2788 g_settings_bind_writable (settings, binding->info.key,
2789 object, "sensitive", FALSE);
2792 if (flags & G_SETTINGS_BIND_SET)
2794 detailed_signal = g_strdup_printf ("notify::%s", property);
2795 binding->property_handler_id =
2796 g_signal_connect (object, detailed_signal,
2797 G_CALLBACK (g_settings_binding_property_changed),
2799 g_free (detailed_signal);
2801 if (~flags & G_SETTINGS_BIND_GET)
2802 g_settings_binding_property_changed (object,
2807 if (flags & G_SETTINGS_BIND_GET)
2809 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2811 detailed_signal = g_strdup_printf ("changed::%s", key);
2812 binding->key_handler_id =
2813 g_signal_connect (settings, detailed_signal,
2814 G_CALLBACK (g_settings_binding_key_changed),
2816 g_free (detailed_signal);
2819 g_settings_binding_key_changed (settings, binding->info.key, binding);
2822 binding_quark = g_settings_binding_quark (property);
2823 g_object_set_qdata_full (object, binding_quark,
2824 binding, g_settings_binding_free);
2827 /* Writability binding {{{1 */
2830 GSettings *settings;
2833 const gchar *property;
2836 } GSettingsWritableBinding;
2839 g_settings_writable_binding_free (gpointer data)
2841 GSettingsWritableBinding *binding = data;
2843 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2844 g_object_unref (binding->settings);
2845 g_slice_free (GSettingsWritableBinding, binding);
2849 g_settings_binding_writable_changed (GSettings *settings,
2853 GSettingsWritableBinding *binding = user_data;
2856 g_assert (settings == binding->settings);
2857 g_assert (key == binding->key);
2859 writable = g_settings_is_writable (settings, key);
2861 if (binding->inverted)
2862 writable = !writable;
2864 g_object_set (binding->object, binding->property, writable, NULL);
2868 * g_settings_bind_writable:
2869 * @settings: a #GSettings object
2870 * @key: the key to bind
2871 * @object: (type GObject.Object):a #GObject
2872 * @property: the name of a boolean property to bind
2873 * @inverted: whether to 'invert' the value
2875 * Create a binding between the writability of @key in the
2876 * @settings object and the property @property of @object.
2877 * The property must be boolean; "sensitive" or "visible"
2878 * properties of widgets are the most likely candidates.
2880 * Writable bindings are always uni-directional; changes of the
2881 * writability of the setting will be propagated to the object
2882 * property, not the other way.
2884 * When the @inverted argument is %TRUE, the binding inverts the
2885 * value as it passes from the setting to the object, i.e. @property
2886 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2889 * Note that the lifecycle of the binding is tied to the object,
2890 * and that you can have only one binding per object property.
2891 * If you bind the same property twice on the same object, the second
2892 * binding overrides the first one.
2897 g_settings_bind_writable (GSettings *settings,
2900 const gchar *property,
2903 GSettingsWritableBinding *binding;
2904 gchar *detailed_signal;
2907 g_return_if_fail (G_IS_SETTINGS (settings));
2909 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2912 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2913 property, G_OBJECT_TYPE_NAME (object));
2916 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2918 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2919 property, G_OBJECT_TYPE_NAME (object));
2923 binding = g_slice_new (GSettingsWritableBinding);
2924 binding->settings = g_object_ref (settings);
2925 binding->object = object;
2926 binding->key = g_intern_string (key);
2927 binding->property = g_intern_string (property);
2928 binding->inverted = inverted;
2930 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2931 binding->handler_id =
2932 g_signal_connect (settings, detailed_signal,
2933 G_CALLBACK (g_settings_binding_writable_changed),
2935 g_free (detailed_signal);
2937 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2938 binding, g_settings_writable_binding_free);
2940 g_settings_binding_writable_changed (settings, binding->key, binding);
2944 * g_settings_unbind:
2945 * @object: the object
2946 * @property: the property whose binding is removed
2948 * Removes an existing binding for @property on @object.
2950 * Note that bindings are automatically removed when the
2951 * object is finalized, so it is rarely necessary to call this
2957 g_settings_unbind (gpointer object,
2958 const gchar *property)
2960 GQuark binding_quark;
2962 binding_quark = g_settings_binding_quark (property);
2963 g_object_set_qdata (object, binding_quark, NULL);
2968 /* vim:set foldmethod=marker: */