2 * Copyright © 2009, 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
29 #include "gsettings.h"
31 #include "gdelayedsettingsbackend.h"
32 #include "gsettingsbackendinternal.h"
33 #include "gsettings-mapping.h"
34 #include "gsettingsschema.h"
43 * @short_description: High-level API for application settings
45 * The #GSettings class provides a convenient API for storing and retrieving
46 * application settings.
48 * Reads and writes can be considered to be non-blocking. Reading
49 * settings with #GSettings is typically extremely fast: on
50 * approximately the same order of magnitude (but slower than) a
51 * #GHashTable lookup. Writing settings is also extremely fast in terms
52 * of time to return to your application, but can be extremely expensive
53 * for other threads and other processes. Many settings backends
54 * (including dconf) have lazy initialisation which means in the common
55 * case of the user using their computer without modifying any settings
56 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
57 * even need to be started in this case. For this reason, you should
58 * only ever modify #GSettings keys in response to explicit user action.
59 * Particular care should be paid to ensure that modifications are not
60 * made during startup -- for example, when setting the initial value
61 * of preferences widgets. The built-in g_settings_bind() functionality
62 * is careful not to write settings in response to notify signals as a
63 * result of modifications that it makes to widgets.
65 * When creating a GSettings instance, you have to specify a schema
66 * that describes the keys in your settings and their types and default
67 * values, as well as some other information.
69 * Normally, a schema has as fixed path that determines where the settings
70 * are stored in the conceptual global tree of settings. However, schemas
71 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
72 * useful e.g. when the schema describes an 'account', and you want to be
73 * able to store a arbitrary number of accounts.
75 * Unlike other configuration systems (like GConf), GSettings does not
76 * restrict keys to basic types like strings and numbers. GSettings stores
77 * values as #GVariant, and allows any #GVariantType for keys. Key names
78 * are restricted to lowercase characters, numbers and '-'. Furthermore,
79 * the names must begin with a lowercase character, must not end
80 * with a '-', and must not contain consecutive dashes. Key names can
81 * be up to 32 characters long.
83 * Similar to GConf, the default values in GSettings schemas can be
84 * localized, but the localized values are stored in gettext catalogs
85 * and looked up with the domain that is specified in the
86 * <tag class="attribute">gettext-domain</tag> attribute of the
87 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
88 * elements and the category that is specified in the l10n attribute of the
89 * <tag class="starttag">key</tag> element.
91 * GSettings uses schemas in a compact binary form that is created
92 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
93 * utility. The input is a schema description in an XML format that can be
94 * described by the following DTD:
95 * |[<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>]|
97 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
99 * At runtime, schemas are identified by their id (as specified
100 * in the <tag class="attribute">id</tag> attribute of the
101 * <tag class="starttag">schema</tag> element). The
102 * convention for schema ids is to use a dotted name, similar in
103 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
104 * if the settings are for a specific service that owns a D-Bus bus name,
105 * the D-Bus bus name and schema id should match. For schemas which deal
106 * with settings not associated with one named application, the id should
107 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
109 * In addition to #GVariant types, keys can have types that have enumerated
110 * types. These can be described by a <tag class="starttag">choice</tag>,
111 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
112 * <xref linkend="schema-enumerated"/>. The underlying type of
113 * such a key is string, but you can use g_settings_get_enum(),
114 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
115 * access the numeric values corresponding to the string value of enum
118 * <example id="schema-default-values"><title>Default values</title>
119 * <programlisting><![CDATA[
121 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
123 * <key name="greeting" type="s">
124 * <default l10n="messages">"Hello, earthlings"</default>
125 * <summary>A greeting</summary>
127 * Greeting of the invading martians
131 * <key name="box" type="(ii)">
132 * <default>(20,30)</default>
137 * ]]></programlisting></example>
139 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
140 * <programlisting><![CDATA[
143 * <enum id="org.gtk.Test.myenum">
144 * <value nick="first" value="1"/>
145 * <value nick="second" value="2"/>
148 * <flags id="org.gtk.Test.myflags">
149 * <value nick="flag1" value="1"/>
150 * <value nick="flag2" value="2"/>
151 * <value nick="flag3" value="4"/>
154 * <schema id="org.gtk.Test">
156 * <key name="key-with-range" type="i">
157 * <range min="1" max="100"/>
158 * <default>10</default>
161 * <key name="key-with-choices" type="s">
163 * <choice value='Elisabeth'/>
164 * <choice value='Annabeth'/>
165 * <choice value='Joe'/>
168 * <alias value='Anna' target='Annabeth'/>
169 * <alias value='Beth' target='Elisabeth'/>
171 * <default>'Joe'</default>
174 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
175 * <default>'first'</default>
178 * <key name='flags-key' flags='org.gtk.Test.myflags'>
179 * <default>["flag1",flag2"]</default>
183 * ]]></programlisting></example>
186 * <title>Vendor overrides</title>
188 * Default values are defined in the schemas that get installed by
189 * an application. Sometimes, it is necessary for a vendor or distributor
190 * to adjust these defaults. Since patching the XML source for the schema
191 * is inconvenient and error-prone,
192 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
193 * so-called 'vendor override' files. These are keyfiles in the same
194 * directory as the XML schema sources which can override default values.
195 * The schema id serves as the group name in the key file, and the values
196 * are expected in serialized GVariant form, as in the following example:
197 * <informalexample><programlisting>
201 * </programlisting></informalexample>
204 * glib-compile-schemas expects schema files to have the extension
205 * <filename>.gschema.override</filename>
210 * <title>Binding</title>
212 * A very convenient feature of GSettings lets you bind #GObject properties
213 * directly to settings, using g_settings_bind(). Once a GObject property
214 * has been bound to a setting, changes on either side are automatically
215 * propagated to the other side. GSettings handles details like
216 * mapping between GObject and GVariant types, and preventing infinite
220 * This makes it very easy to hook up a preferences dialog to the
221 * underlying settings. To make this even more convenient, GSettings
222 * looks for a boolean property with the name "sensitivity" and
223 * automatically binds it to the writability of the bound setting.
224 * If this 'magic' gets in the way, it can be suppressed with the
225 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
230 struct _GSettingsPrivate
232 /* where the signals go... */
233 GMainContext *main_context;
235 GSettingsBackend *backend;
236 GSettingsSchema *schema;
240 GDelayedSettingsBackend *delayed;
255 SIGNAL_WRITABLE_CHANGE_EVENT,
256 SIGNAL_WRITABLE_CHANGED,
262 static guint g_settings_signals[N_SIGNALS];
264 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
268 g_settings_real_change_event (GSettings *settings,
275 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
277 for (i = 0; i < n_keys; i++)
278 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
279 keys[i], g_quark_to_string (keys[i]));
285 g_settings_real_writable_change_event (GSettings *settings,
288 const GQuark *keys = &key;
293 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
295 for (i = 0; i < n_keys; i++)
296 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
297 keys[i], g_quark_to_string (keys[i]));
303 settings_backend_changed (GObject *target,
304 GSettingsBackend *backend,
308 GSettings *settings = G_SETTINGS (target);
309 gboolean ignore_this;
312 /* We used to assert here:
314 * settings->priv->backend == backend
316 * but it could be the case that a notification is queued for delivery
317 * while someone calls g_settings_delay() (which changes the backend).
319 * Since the delay backend would just pass that straight through
320 * anyway, it doesn't make sense to try to detect this case.
321 * Therefore, we just accept it.
324 for (i = 0; key[i] == settings->priv->path[i]; i++);
326 if (settings->priv->path[i] == '\0' &&
327 g_settings_schema_has_key (settings->priv->schema, key + i))
331 quark = g_quark_from_string (key + i);
332 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
333 0, &quark, 1, &ignore_this);
338 settings_backend_path_changed (GObject *target,
339 GSettingsBackend *backend,
343 GSettings *settings = G_SETTINGS (target);
344 gboolean ignore_this;
346 if (g_str_has_prefix (settings->priv->path, path))
347 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
348 0, NULL, 0, &ignore_this);
352 settings_backend_keys_changed (GObject *target,
353 GSettingsBackend *backend,
355 const gchar * const *items,
358 GSettings *settings = G_SETTINGS (target);
359 gboolean ignore_this;
362 for (i = 0; settings->priv->path[i] &&
363 settings->priv->path[i] == path[i]; i++);
370 for (j = 0; items[j]; j++)
372 const gchar *item = items[j];
375 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
377 if (settings->priv->path[i + k] == '\0' &&
378 g_settings_schema_has_key (settings->priv->schema, item + k))
379 quarks[l++] = g_quark_from_string (item + k);
381 /* "256 quarks ought to be enough for anybody!"
382 * If this bites you, I'm sorry. Please file a bug.
388 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
389 0, quarks, l, &ignore_this);
394 settings_backend_writable_changed (GObject *target,
395 GSettingsBackend *backend,
398 GSettings *settings = G_SETTINGS (target);
399 gboolean ignore_this;
402 for (i = 0; key[i] == settings->priv->path[i]; i++);
404 if (settings->priv->path[i] == '\0' &&
405 g_settings_schema_has_key (settings->priv->schema, key + i))
406 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
407 0, g_quark_from_string (key + i), &ignore_this);
411 settings_backend_path_writable_changed (GObject *target,
412 GSettingsBackend *backend,
415 GSettings *settings = G_SETTINGS (target);
416 gboolean ignore_this;
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 g_cclosure_marshal_generic,
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 g_cclosure_marshal_generic, 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 a #GSettings object with a different backend allows accessing
812 * settings 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_uint:
1822 * @settings: a #GSettings object
1823 * @key: the key to get the value for
1824 * @returns: an unsigned integer
1826 * Gets the value that is stored at @key in @settings.
1828 * A convenience variant of g_settings_get() for 32-bit unsigned
1831 * It is a programmer error to give a @key that isn't specified as
1832 * having a uint32 type in the schema for @settings.
1837 g_settings_get_uint (GSettings *settings,
1843 value = g_settings_get_value (settings, key);
1844 result = g_variant_get_uint32 (value);
1845 g_variant_unref (value);
1851 * g_settings_set_uint:
1852 * @settings: a #GSettings object
1853 * @key: the name of the key to set
1854 * @value: the value to set it to
1855 * @returns: %TRUE if setting the key succeeded,
1856 * %FALSE if the key was not writable
1858 * Sets @key in @settings to @value.
1860 * A convenience variant of g_settings_set() for 32-bit unsigned
1863 * It is a programmer error to give a @key that isn't specified as
1864 * having a uint32 type in the schema for @settings.
1869 g_settings_set_uint (GSettings *settings,
1873 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1877 * g_settings_get_double:
1878 * @settings: a #GSettings object
1879 * @key: the key to get the value for
1880 * @returns: a double
1882 * Gets the value that is stored at @key in @settings.
1884 * A convenience variant of g_settings_get() for doubles.
1886 * It is a programmer error to give a @key that isn't specified as
1887 * having a 'double' type in the schema for @settings.
1892 g_settings_get_double (GSettings *settings,
1898 value = g_settings_get_value (settings, key);
1899 result = g_variant_get_double (value);
1900 g_variant_unref (value);
1906 * g_settings_set_double:
1907 * @settings: a #GSettings object
1908 * @key: the name of the key to set
1909 * @value: the value to set it to
1910 * @returns: %TRUE if setting the key succeeded,
1911 * %FALSE if the key was not writable
1913 * Sets @key in @settings to @value.
1915 * A convenience variant of g_settings_set() for doubles.
1917 * It is a programmer error to give a @key that isn't specified as
1918 * having a 'double' type in the schema for @settings.
1923 g_settings_set_double (GSettings *settings,
1927 return g_settings_set_value (settings, key, g_variant_new_double (value));
1931 * g_settings_get_boolean:
1932 * @settings: a #GSettings object
1933 * @key: the key to get the value for
1934 * @returns: a boolean
1936 * Gets the value that is stored at @key in @settings.
1938 * A convenience variant of g_settings_get() for booleans.
1940 * It is a programmer error to give a @key that isn't specified as
1941 * having a boolean type in the schema for @settings.
1946 g_settings_get_boolean (GSettings *settings,
1952 value = g_settings_get_value (settings, key);
1953 result = g_variant_get_boolean (value);
1954 g_variant_unref (value);
1960 * g_settings_set_boolean:
1961 * @settings: a #GSettings object
1962 * @key: the name of the key to set
1963 * @value: the value to set it to
1964 * @returns: %TRUE if setting the key succeeded,
1965 * %FALSE if the key was not writable
1967 * Sets @key in @settings to @value.
1969 * A convenience variant of g_settings_set() for booleans.
1971 * It is a programmer error to give a @key that isn't specified as
1972 * having a boolean type in the schema for @settings.
1977 g_settings_set_boolean (GSettings *settings,
1981 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1985 * g_settings_get_strv:
1986 * @settings: a #GSettings object
1987 * @key: the key to get the value for
1988 * @returns: (array zero-terminated=1) (transfer full): a
1989 * newly-allocated, %NULL-terminated array of strings, the value that
1990 * is stored at @key in @settings.
1992 * A convenience variant of g_settings_get() for string arrays.
1994 * It is a programmer error to give a @key that isn't specified as
1995 * having an array of strings type in the schema for @settings.
2000 g_settings_get_strv (GSettings *settings,
2006 value = g_settings_get_value (settings, key);
2007 result = g_variant_dup_strv (value, NULL);
2008 g_variant_unref (value);
2014 * g_settings_set_strv:
2015 * @settings: a #GSettings object
2016 * @key: the name of the key to set
2017 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
2018 * @returns: %TRUE if setting the key succeeded,
2019 * %FALSE if the key was not writable
2021 * Sets @key in @settings to @value.
2023 * A convenience variant of g_settings_set() for string arrays. If
2024 * @value is %NULL, then @key is set to be the empty array.
2026 * It is a programmer error to give a @key that isn't specified as
2027 * having an array of strings type in the schema for @settings.
2032 g_settings_set_strv (GSettings *settings,
2034 const gchar * const *value)
2039 array = g_variant_new_strv (value, -1);
2041 array = g_variant_new_strv (NULL, 0);
2043 return g_settings_set_value (settings, key, array);
2046 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2049 * @settings: a #GSettings object
2051 * Changes the #GSettings object into 'delay-apply' mode. In this
2052 * mode, changes to @settings are not immediately propagated to the
2053 * backend, but kept locally until g_settings_apply() is called.
2058 g_settings_delay (GSettings *settings)
2060 g_return_if_fail (G_IS_SETTINGS (settings));
2062 if (settings->priv->delayed)
2065 settings->priv->delayed =
2066 g_delayed_settings_backend_new (settings->priv->backend,
2068 settings->priv->main_context);
2069 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2070 g_object_unref (settings->priv->backend);
2072 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2073 g_settings_backend_watch (settings->priv->backend,
2074 &listener_vtable, G_OBJECT (settings),
2075 settings->priv->main_context);
2077 g_object_notify (G_OBJECT (settings), "delay-apply");
2082 * @settings: a #GSettings instance
2084 * Applies any changes that have been made to the settings. This
2085 * function does nothing unless @settings is in 'delay-apply' mode;
2086 * see g_settings_delay(). In the normal case settings are always
2087 * applied immediately.
2090 g_settings_apply (GSettings *settings)
2092 if (settings->priv->delayed)
2094 GDelayedSettingsBackend *delayed;
2096 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2097 g_delayed_settings_backend_apply (delayed);
2102 * g_settings_revert:
2103 * @settings: a #GSettings instance
2105 * Reverts all non-applied changes to the settings. This function
2106 * does nothing unless @settings is in 'delay-apply' mode; see
2107 * g_settings_delay(). In the normal case settings are always applied
2110 * Change notifications will be emitted for affected keys.
2113 g_settings_revert (GSettings *settings)
2115 if (settings->priv->delayed)
2117 GDelayedSettingsBackend *delayed;
2119 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2120 g_delayed_settings_backend_revert (delayed);
2125 * g_settings_get_has_unapplied:
2126 * @settings: a #GSettings object
2127 * @returns: %TRUE if @settings has unapplied changes
2129 * Returns whether the #GSettings object has any unapplied
2130 * changes. This can only be the case if it is in 'delayed-apply' mode.
2135 g_settings_get_has_unapplied (GSettings *settings)
2137 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2139 return settings->priv->delayed &&
2140 g_delayed_settings_backend_get_has_unapplied (
2141 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2144 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2147 * @settings: a #GSettings object
2148 * @key: the name of a key
2150 * Resets @key to its default value.
2152 * This call resets the key, as much as possible, to its default value.
2153 * That might the value specified in the schema or the one set by the
2157 g_settings_reset (GSettings *settings,
2162 path = g_strconcat (settings->priv->path, key, NULL);
2163 g_settings_backend_reset (settings->priv->backend, path, NULL);
2170 * Ensures that all pending operations for the given are complete for
2171 * the default backend.
2173 * Writes made to a #GSettings are handled asynchronously. For this
2174 * reason, it is very unlikely that the changes have it to disk by the
2175 * time g_settings_set() returns.
2177 * This call will block until all of the writes have made it to the
2178 * backend. Since the mainloop is not running, no change notifications
2179 * will be dispatched during this call (but some may be queued by the
2180 * time the call is done).
2183 g_settings_sync (void)
2185 g_settings_backend_sync_default ();
2189 * g_settings_is_writable:
2190 * @settings: a #GSettings object
2191 * @name: the name of a key
2192 * @returns: %TRUE if the key @name is writable
2194 * Finds out if a key can be written or not
2199 g_settings_is_writable (GSettings *settings,
2205 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2207 path = g_strconcat (settings->priv->path, name, NULL);
2208 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2215 * g_settings_get_child:
2216 * @settings: a #GSettings object
2217 * @name: the name of the 'child' schema
2218 * @returns: (transfer full): a 'child' settings object
2220 * Creates a 'child' settings object which has a base path of
2221 * <replaceable>base-path</replaceable>/@name, where
2222 * <replaceable>base-path</replaceable> is the base path of @settings.
2224 * The schema for the child settings object must have been declared
2225 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2230 g_settings_get_child (GSettings *settings,
2233 const gchar *child_schema;
2238 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2240 child_name = g_strconcat (name, "/", NULL);
2241 child_schema = g_settings_schema_get_string (settings->priv->schema,
2243 if (child_schema == NULL)
2244 g_error ("Schema '%s' has no child '%s'",
2245 settings->priv->schema_name, name);
2247 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2248 child = g_object_new (G_TYPE_SETTINGS,
2249 "schema", child_schema,
2252 g_free (child_path);
2253 g_free (child_name);
2259 * g_settings_list_keys:
2260 * @settings: a #GSettings object
2261 * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2263 * Introspects the list of keys on @settings.
2265 * You should probably not be calling this function from "normal" code
2266 * (since you should already know what keys are in your schema). This
2267 * function is intended for introspection reasons.
2269 * You should free the return value with g_strfreev() when you are done
2273 g_settings_list_keys (GSettings *settings)
2280 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2281 strv = g_new (gchar *, n_keys + 1);
2282 for (i = j = 0; i < n_keys; i++)
2284 const gchar *key = g_quark_to_string (keys[i]);
2286 if (!g_str_has_suffix (key, "/"))
2287 strv[j++] = g_strdup (key);
2295 * g_settings_list_children:
2296 * @settings: a #GSettings object
2297 * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2299 * Gets the list of children on @settings.
2301 * The list is exactly the list of strings for which it is not an error
2302 * to call g_settings_get_child().
2304 * For GSettings objects that are lists, this value can change at any
2305 * time and you should connect to the "children-changed" signal to watch
2306 * for those changes. Note that there is a race condition here: you may
2307 * request a child after listing it only for it to have been destroyed
2308 * in the meantime. For this reason, g_settings_get_child() may return
2309 * %NULL even for a child that was listed by this function.
2311 * For GSettings objects that are not lists, you should probably not be
2312 * calling this function from "normal" code (since you should already
2313 * know what children are in your schema). This function may still be
2314 * useful there for introspection reasons, however.
2316 * You should free the return value with g_strfreev() when you are done
2320 g_settings_list_children (GSettings *settings)
2327 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2328 strv = g_new (gchar *, n_keys + 1);
2329 for (i = j = 0; i < n_keys; i++)
2331 const gchar *key = g_quark_to_string (keys[i]);
2333 if (g_str_has_suffix (key, "/"))
2335 gint length = strlen (key);
2337 strv[j] = g_memdup (key, length);
2338 strv[j][length - 1] = '\0';
2348 * g_settings_get_range:
2349 * @settings: a #GSettings
2350 * @key: the key to query the range of
2351 * @returns: a #GVariant describing the range
2353 * Queries the range of a key.
2355 * This function will return a #GVariant that fully describes the range
2356 * of values that are valid for @key.
2358 * The type of #GVariant returned is <literal>(sv)</literal>. The
2359 * string describes the type of range restriction in effect. The type
2360 * and meaning of the value contained in the variant depends on the
2363 * If the string is <literal>'type'</literal> then the variant contains
2364 * an empty array. The element type of that empty array is the expected
2365 * type of value and all values of that type are valid.
2367 * If the string is <literal>'enum'</literal> then the variant contains
2368 * an array enumerating the possible values. Each item in the array is
2369 * a possible valid value and no other values are valid.
2371 * If the string is <literal>'flags'</literal> then the variant contains
2372 * an array. Each item in the array is a value that may appear zero or
2373 * one times in an array to be used as the value for this key. For
2374 * example, if the variant contained the array <literal>['x',
2375 * 'y']</literal> then the valid values for the key would be
2376 * <literal>[]</literal>, <literal>['x']</literal>,
2377 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2378 * <literal>['y', 'x']</literal>.
2380 * Finally, if the string is <literal>'range'</literal> then the variant
2381 * contains a pair of like-typed values -- the minimum and maximum
2382 * permissible values for this key.
2384 * This information should not be used by normal programs. It is
2385 * considered to be a hint for introspection purposes. Normal programs
2386 * should already know what is permitted by their own schema. The
2387 * format may change in any way in the future -- but particularly, new
2388 * forms may be added to the possibilities described above.
2390 * It is a programmer error to give a @key that isn't contained in the
2391 * schema for @settings.
2393 * You should free the returned value with g_variant_unref() when it is
2399 g_settings_get_range (GSettings *settings,
2402 GSettingsKeyInfo info;
2406 g_settings_get_key_info (&info, settings, key);
2410 range = g_variant_new ("(**)", info.minimum, info.maximum);
2413 else if (info.strinfo)
2415 range = strinfo_enumerate (info.strinfo, info.strinfo_length);
2416 type = info.is_flags ? "flags" : "enum";
2420 range = g_variant_new_array (info.type, NULL, 0);
2424 g_settings_free_key_info (&info);
2426 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2430 * g_settings_range_check:
2431 * @settings: a #GSettings
2432 * @key: the key to check
2433 * @value: the value to check
2434 * @returns: %TRUE if @value is valid for @key
2436 * Checks if the given @value is of the correct type and within the
2437 * permitted range for @key.
2439 * This API is not intended to be used by normal programs -- they should
2440 * already know what is permitted by their own schemas. This API is
2441 * meant to be used by programs such as editors or commandline tools.
2443 * It is a programmer error to give a @key that isn't contained in the
2444 * schema for @settings.
2449 g_settings_range_check (GSettings *settings,
2453 GSettingsKeyInfo info;
2456 g_settings_get_key_info (&info, settings, key);
2457 good = g_settings_type_check (&info, value) &&
2458 g_settings_key_info_range_check (&info, value);
2459 g_settings_free_key_info (&info);
2467 GSettingsKeyInfo info;
2470 GSettingsBindGetMapping get_mapping;
2471 GSettingsBindSetMapping set_mapping;
2473 GDestroyNotify destroy;
2475 guint writable_handler_id;
2476 guint property_handler_id;
2477 const GParamSpec *property;
2478 guint key_handler_id;
2480 /* prevent recursion */
2485 g_settings_binding_free (gpointer data)
2487 GSettingsBinding *binding = data;
2489 g_assert (!binding->running);
2491 if (binding->writable_handler_id)
2492 g_signal_handler_disconnect (binding->info.settings,
2493 binding->writable_handler_id);
2495 if (binding->key_handler_id)
2496 g_signal_handler_disconnect (binding->info.settings,
2497 binding->key_handler_id);
2499 if (g_signal_handler_is_connected (binding->object,
2500 binding->property_handler_id))
2501 g_signal_handler_disconnect (binding->object,
2502 binding->property_handler_id);
2504 g_settings_free_key_info (&binding->info);
2506 if (binding->destroy)
2507 binding->destroy (binding->user_data);
2509 g_slice_free (GSettingsBinding, binding);
2513 g_settings_binding_quark (const char *property)
2518 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2519 quark = g_quark_from_string (tmp);
2526 g_settings_binding_key_changed (GSettings *settings,
2530 GSettingsBinding *binding = user_data;
2531 GValue value = { 0, };
2534 g_assert (settings == binding->info.settings);
2535 g_assert (key == binding->info.key);
2537 if (binding->running)
2540 binding->running = TRUE;
2542 g_value_init (&value, binding->property->value_type);
2544 variant = g_settings_read_from_backend (&binding->info);
2545 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2547 /* silently ignore errors in the user's config database */
2548 g_variant_unref (variant);
2552 if (variant == NULL)
2554 variant = g_settings_get_translated_default (&binding->info);
2556 !binding->get_mapping (&value, variant, binding->user_data))
2558 /* flag translation errors with a warning */
2559 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2560 "was rejected by the binding mapping function",
2561 binding->info.unparsed, binding->info.key,
2562 binding->info.settings->priv->schema_name);
2563 g_variant_unref (variant);
2568 if (variant == NULL)
2570 variant = g_variant_ref (binding->info.default_value);
2571 if (!binding->get_mapping (&value, variant, binding->user_data))
2572 g_error ("The schema default value for key `%s' in schema `%s' "
2573 "was rejected by the binding mapping function.",
2575 binding->info.settings->priv->schema_name);
2578 g_object_set_property (binding->object, binding->property->name, &value);
2579 g_variant_unref (variant);
2580 g_value_unset (&value);
2582 binding->running = FALSE;
2586 g_settings_binding_property_changed (GObject *object,
2587 const GParamSpec *pspec,
2590 GSettingsBinding *binding = user_data;
2591 GValue value = { 0, };
2594 g_assert (object == binding->object);
2595 g_assert (pspec == binding->property);
2597 if (binding->running)
2600 binding->running = TRUE;
2602 g_value_init (&value, pspec->value_type);
2603 g_object_get_property (object, pspec->name, &value);
2604 if ((variant = binding->set_mapping (&value, binding->info.type,
2605 binding->user_data)))
2607 g_variant_take_ref (variant);
2609 if (!g_settings_type_check (&binding->info, variant))
2611 g_critical ("binding mapping function for key `%s' returned "
2612 "GVariant of type `%s' when type `%s' was requested",
2613 binding->info.key, g_variant_get_type_string (variant),
2614 g_variant_type_dup_string (binding->info.type));
2618 if (!g_settings_key_info_range_check (&binding->info, variant))
2620 g_critical ("GObject property `%s' on a `%s' object is out of "
2621 "schema-specified range for key `%s' of `%s': %s",
2622 binding->property->name,
2623 g_type_name (binding->property->owner_type),
2625 binding->info.settings->priv->schema_name,
2626 g_variant_print (variant, TRUE));
2630 g_settings_write_to_backend (&binding->info, variant);
2631 g_variant_unref (variant);
2633 g_value_unset (&value);
2635 binding->running = FALSE;
2639 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2643 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2648 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2649 const GVariantType *expected_type,
2652 return g_variant_new_boolean (!g_value_get_boolean (value));
2657 * @settings: a #GSettings object
2658 * @key: the key to bind
2659 * @object: (type GObject.Object): a #GObject
2660 * @property: the name of the property to bind
2661 * @flags: flags for the binding
2663 * Create a binding between the @key in the @settings object
2664 * and the property @property of @object.
2666 * The binding uses the default GIO mapping functions to map
2667 * between the settings and property values. These functions
2668 * handle booleans, numeric types and string types in a
2669 * straightforward way. Use g_settings_bind_with_mapping() if
2670 * you need a custom mapping, or map between types that are not
2671 * supported by the default mapping functions.
2673 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2674 * function also establishes a binding between the writability of
2675 * @key and the "sensitive" property of @object (if @object has
2676 * a boolean property by that name). See g_settings_bind_writable()
2677 * for more details about writable bindings.
2679 * Note that the lifecycle of the binding is tied to the object,
2680 * and that you can have only one binding per object property.
2681 * If you bind the same property twice on the same object, the second
2682 * binding overrides the first one.
2687 g_settings_bind (GSettings *settings,
2690 const gchar *property,
2691 GSettingsBindFlags flags)
2693 GSettingsBindGetMapping get_mapping = NULL;
2694 GSettingsBindSetMapping set_mapping = NULL;
2696 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2698 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2699 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2701 /* can't pass this flag to g_settings_bind_with_mapping() */
2702 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2705 g_settings_bind_with_mapping (settings, key, object, property, flags,
2706 get_mapping, set_mapping, NULL, NULL);
2710 * g_settings_bind_with_mapping: (skip)
2711 * @settings: a #GSettings object
2712 * @key: the key to bind
2713 * @object: (type GObject.Object): a #GObject
2714 * @property: the name of the property to bind
2715 * @flags: flags for the binding
2716 * @get_mapping: a function that gets called to convert values
2717 * from @settings to @object, or %NULL to use the default GIO mapping
2718 * @set_mapping: a function that gets called to convert values
2719 * from @object to @settings, or %NULL to use the default GIO mapping
2720 * @user_data: data that gets passed to @get_mapping and @set_mapping
2721 * @destroy: #GDestroyNotify function for @user_data
2723 * Create a binding between the @key in the @settings object
2724 * and the property @property of @object.
2726 * The binding uses the provided mapping functions to map between
2727 * settings and property values.
2729 * Note that the lifecycle of the binding is tied to the object,
2730 * and that you can have only one binding per object property.
2731 * If you bind the same property twice on the same object, the second
2732 * binding overrides the first one.
2737 g_settings_bind_with_mapping (GSettings *settings,
2740 const gchar *property,
2741 GSettingsBindFlags flags,
2742 GSettingsBindGetMapping get_mapping,
2743 GSettingsBindSetMapping set_mapping,
2745 GDestroyNotify destroy)
2747 GSettingsBinding *binding;
2748 GObjectClass *objectclass;
2749 gchar *detailed_signal;
2750 GQuark binding_quark;
2752 g_return_if_fail (G_IS_SETTINGS (settings));
2753 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2755 objectclass = G_OBJECT_GET_CLASS (object);
2757 binding = g_slice_new0 (GSettingsBinding);
2758 g_settings_get_key_info (&binding->info, settings, key);
2759 binding->object = object;
2760 binding->property = g_object_class_find_property (objectclass, property);
2761 binding->user_data = user_data;
2762 binding->destroy = destroy;
2763 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2764 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2766 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2767 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2769 if (binding->property == NULL)
2771 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2772 property, G_OBJECT_TYPE_NAME (object));
2776 if ((flags & G_SETTINGS_BIND_GET) &&
2777 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2779 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2780 "writable", property, G_OBJECT_TYPE_NAME (object));
2783 if ((flags & G_SETTINGS_BIND_SET) &&
2784 (binding->property->flags & G_PARAM_READABLE) == 0)
2786 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2787 "readable", property, G_OBJECT_TYPE_NAME (object));
2791 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2793 /* g_settings_bind_invert_boolean_get_mapping() is a private
2794 * function, so if we are here it means that g_settings_bind() was
2795 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2797 * Ensure that both sides are boolean.
2800 if (binding->property->value_type != G_TYPE_BOOLEAN)
2802 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2803 "was specified, but property `%s' on type `%s' has "
2804 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2805 g_type_name ((binding->property->value_type)));
2809 if (!g_variant_type_equal (binding->info.type, G_VARIANT_TYPE_BOOLEAN))
2811 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2812 "was specified, but key `%s' on schema `%s' has "
2813 "type `%s'", key, settings->priv->schema_name,
2814 g_variant_type_dup_string (binding->info.type));
2820 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2821 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2822 !g_settings_mapping_is_compatible (binding->property->value_type,
2823 binding->info.type))
2825 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2826 "'%s' which is not compatible with type '%s' of key '%s' "
2827 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2828 g_type_name (binding->property->value_type),
2829 g_variant_type_dup_string (binding->info.type), key,
2830 settings->priv->schema_name);
2834 if ((flags & G_SETTINGS_BIND_SET) &&
2835 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2837 GParamSpec *sensitive;
2839 sensitive = g_object_class_find_property (objectclass, "sensitive");
2841 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2842 (sensitive->flags & G_PARAM_WRITABLE))
2843 g_settings_bind_writable (settings, binding->info.key,
2844 object, "sensitive", FALSE);
2847 if (flags & G_SETTINGS_BIND_SET)
2849 detailed_signal = g_strdup_printf ("notify::%s", property);
2850 binding->property_handler_id =
2851 g_signal_connect (object, detailed_signal,
2852 G_CALLBACK (g_settings_binding_property_changed),
2854 g_free (detailed_signal);
2856 if (~flags & G_SETTINGS_BIND_GET)
2857 g_settings_binding_property_changed (object,
2862 if (flags & G_SETTINGS_BIND_GET)
2864 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2866 detailed_signal = g_strdup_printf ("changed::%s", key);
2867 binding->key_handler_id =
2868 g_signal_connect (settings, detailed_signal,
2869 G_CALLBACK (g_settings_binding_key_changed),
2871 g_free (detailed_signal);
2874 g_settings_binding_key_changed (settings, binding->info.key, binding);
2877 binding_quark = g_settings_binding_quark (property);
2878 g_object_set_qdata_full (object, binding_quark,
2879 binding, g_settings_binding_free);
2882 /* Writability binding {{{1 */
2885 GSettings *settings;
2888 const gchar *property;
2891 } GSettingsWritableBinding;
2894 g_settings_writable_binding_free (gpointer data)
2896 GSettingsWritableBinding *binding = data;
2898 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2899 g_object_unref (binding->settings);
2900 g_slice_free (GSettingsWritableBinding, binding);
2904 g_settings_binding_writable_changed (GSettings *settings,
2908 GSettingsWritableBinding *binding = user_data;
2911 g_assert (settings == binding->settings);
2912 g_assert (key == binding->key);
2914 writable = g_settings_is_writable (settings, key);
2916 if (binding->inverted)
2917 writable = !writable;
2919 g_object_set (binding->object, binding->property, writable, NULL);
2923 * g_settings_bind_writable:
2924 * @settings: a #GSettings object
2925 * @key: the key to bind
2926 * @object: (type GObject.Object):a #GObject
2927 * @property: the name of a boolean property to bind
2928 * @inverted: whether to 'invert' the value
2930 * Create a binding between the writability of @key in the
2931 * @settings object and the property @property of @object.
2932 * The property must be boolean; "sensitive" or "visible"
2933 * properties of widgets are the most likely candidates.
2935 * Writable bindings are always uni-directional; changes of the
2936 * writability of the setting will be propagated to the object
2937 * property, not the other way.
2939 * When the @inverted argument is %TRUE, the binding inverts the
2940 * value as it passes from the setting to the object, i.e. @property
2941 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2944 * Note that the lifecycle of the binding is tied to the object,
2945 * and that you can have only one binding per object property.
2946 * If you bind the same property twice on the same object, the second
2947 * binding overrides the first one.
2952 g_settings_bind_writable (GSettings *settings,
2955 const gchar *property,
2958 GSettingsWritableBinding *binding;
2959 gchar *detailed_signal;
2962 g_return_if_fail (G_IS_SETTINGS (settings));
2964 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2967 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2968 property, G_OBJECT_TYPE_NAME (object));
2971 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2973 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2974 property, G_OBJECT_TYPE_NAME (object));
2978 binding = g_slice_new (GSettingsWritableBinding);
2979 binding->settings = g_object_ref (settings);
2980 binding->object = object;
2981 binding->key = g_intern_string (key);
2982 binding->property = g_intern_string (property);
2983 binding->inverted = inverted;
2985 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2986 binding->handler_id =
2987 g_signal_connect (settings, detailed_signal,
2988 G_CALLBACK (g_settings_binding_writable_changed),
2990 g_free (detailed_signal);
2992 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2993 binding, g_settings_writable_binding_free);
2995 g_settings_binding_writable_changed (settings, binding->key, binding);
2999 * g_settings_unbind:
3000 * @object: the object
3001 * @property: the property whose binding is removed
3003 * Removes an existing binding for @property on @object.
3005 * Note that bindings are automatically removed when the
3006 * object is finalized, so it is rarely necessary to call this
3012 g_settings_unbind (gpointer object,
3013 const gchar *property)
3015 GQuark binding_quark;
3017 binding_quark = g_settings_binding_quark (property);
3018 g_object_set_qdata (object, binding_quark, NULL);
3023 /* vim:set foldmethod=marker: */