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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
26 #include "gsettings.h"
28 #include "gdelayedsettingsbackend.h"
29 #include "gsettingsbackendinternal.h"
30 #include "gsettings-mapping.h"
31 #include "gsettingsschema-internal.h"
38 * @short_description: High-level API for application settings
41 * The #GSettings class provides a convenient API for storing and retrieving
42 * application settings.
44 * Reads and writes can be considered to be non-blocking. Reading
45 * settings with #GSettings is typically extremely fast: on
46 * approximately the same order of magnitude (but slower than) a
47 * #GHashTable lookup. Writing settings is also extremely fast in terms
48 * of time to return to your application, but can be extremely expensive
49 * for other threads and other processes. Many settings backends
50 * (including dconf) have lazy initialisation which means in the common
51 * case of the user using their computer without modifying any settings
52 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
53 * even need to be started in this case. For this reason, you should
54 * only ever modify #GSettings keys in response to explicit user action.
55 * Particular care should be paid to ensure that modifications are not
56 * made during startup -- for example, when setting the initial value
57 * of preferences widgets. The built-in g_settings_bind() functionality
58 * is careful not to write settings in response to notify signals as a
59 * result of modifications that it makes to widgets.
61 * When creating a GSettings instance, you have to specify a schema
62 * that describes the keys in your settings and their types and default
63 * values, as well as some other information.
65 * Normally, a schema has as fixed path that determines where the settings
66 * are stored in the conceptual global tree of settings. However, schemas
67 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
68 * useful e.g. when the schema describes an 'account', and you want to be
69 * able to store a arbitrary number of accounts.
71 * Paths must start with and end with a forward slash character ('/')
72 * and must not contain two sequential slash characters. Paths should
73 * be chosen based on a domain name associated with the program or
74 * library to which the settings belong. Examples of paths are
75 * "/org/gtk/settings/file-chooser/" and "/ca/desrt/dconf-editor/".
76 * Paths should not start with "/apps/", "/desktop/" or "/system/" as
77 * they often did in GConf.
79 * Unlike other configuration systems (like GConf), GSettings does not
80 * restrict keys to basic types like strings and numbers. GSettings stores
81 * values as #GVariant, and allows any #GVariantType for keys. Key names
82 * are restricted to lowercase characters, numbers and '-'. Furthermore,
83 * the names must begin with a lowercase character, must not end
84 * with a '-', and must not contain consecutive dashes.
86 * GSettings supports change notification. The primary mechanism to
87 * watch for changes is to connect to the "changed" signal. You can
88 * optionally watch for changes on only a single key by using a signal
89 * detail. Signals are only guaranteed to be emitted for a given key
90 * after you have read the value of that key while a signal handler was
91 * connected for that key. Signals may or may not be emitted in the
92 * case that the key "changed" to the value that you had previously
93 * read. Signals may be reported in additional cases as well and the
94 * "changed" signal should really be treated as "may have changed".
96 * Similar to GConf, the default values in GSettings schemas can be
97 * localized, but the localized values are stored in gettext catalogs
98 * and looked up with the domain that is specified in the
99 * gettext-domain attribute of the <schemalist> or <schema>
100 * elements and the category that is specified in the l10n attribute of
103 * GSettings uses schemas in a compact binary form that is created
104 * by the [glib-compile-schemas][glib-compile-schemas]
105 * utility. The input is a schema description in an XML format.
107 * A DTD for the gschema XML format can be found here:
108 * [gschema.dtd](https://git.gnome.org/browse/glib/tree/gio/gschema.dtd)
110 * The [glib-compile-schemas][glib-compile-schemas] tool expects schema
111 * files to have the extension `.gschema.xml`.
113 * At runtime, schemas are identified by their id (as specified in the
114 * id attribute of the <schema> element). The convention for schema
115 * ids is to use a dotted name, similar in style to a D-Bus bus name,
116 * e.g. "org.gnome.SessionManager". In particular, if the settings are
117 * for a specific service that owns a D-Bus bus name, the D-Bus bus name
118 * and schema id should match. For schemas which deal with settings not
119 * associated with one named application, the id should not use
120 * StudlyCaps, e.g. "org.gnome.font-rendering".
122 * In addition to #GVariant types, keys can have types that have
123 * enumerated types. These can be described by a <choice>,
124 * <enum> or <flags> element, as seen in the
125 * [example][schema-enumerated]. The underlying type of such a key
126 * is string, but you can use g_settings_get_enum(), g_settings_set_enum(),
127 * g_settings_get_flags(), g_settings_set_flags() access the numeric values
128 * corresponding to the string value of enum and flags keys.
130 * An example for default value:
133 * <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
135 * <key name="greeting" type="s">
136 * <default l10n="messages">"Hello, earthlings"</default>
137 * <summary>A greeting</summary>
139 * Greeting of the invading martians
143 * <key name="box" type="(ii)">
144 * <default>(20,30)</default>
151 * An example for ranges, choices and enumerated types:
155 * <enum id="org.gtk.Test.myenum">
156 * <value nick="first" value="1"/>
157 * <value nick="second" value="2"/>
160 * <flags id="org.gtk.Test.myflags">
161 * <value nick="flag1" value="1"/>
162 * <value nick="flag2" value="2"/>
163 * <value nick="flag3" value="4"/>
166 * <schema id="org.gtk.Test">
168 * <key name="key-with-range" type="i">
169 * <range min="1" max="100"/>
170 * <default>10</default>
173 * <key name="key-with-choices" type="s">
175 * <choice value='Elisabeth'/>
176 * <choice value='Annabeth'/>
177 * <choice value='Joe'/>
180 * <alias value='Anna' target='Annabeth'/>
181 * <alias value='Beth' target='Elisabeth'/>
183 * <default>'Joe'</default>
186 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
187 * <default>'first'</default>
190 * <key name='flags-key' flags='org.gtk.Test.myflags'>
191 * <default>["flag1",flag2"]</default>
197 * ## Vendor overrides
199 * Default values are defined in the schemas that get installed by
200 * an application. Sometimes, it is necessary for a vendor or distributor
201 * to adjust these defaults. Since patching the XML source for the schema
202 * is inconvenient and error-prone,
203 * [glib-compile-schemas][glib-compile-schemas] reads so-called vendor
204 * override' files. These are keyfiles in the same directory as the XML
205 * schema sources which can override default values. The schema id serves
206 * as the group name in the key file, and the values are expected in
207 * serialized GVariant form, as in the following example:
214 * glib-compile-schemas expects schema files to have the extension
215 * `.gschema.override`.
219 * A very convenient feature of GSettings lets you bind #GObject properties
220 * directly to settings, using g_settings_bind(). Once a GObject property
221 * has been bound to a setting, changes on either side are automatically
222 * propagated to the other side. GSettings handles details like mapping
223 * between GObject and GVariant types, and preventing infinite cycles.
225 * This makes it very easy to hook up a preferences dialog to the
226 * underlying settings. To make this even more convenient, GSettings
227 * looks for a boolean property with the name "sensitivity" and
228 * automatically binds it to the writability of the bound setting.
229 * If this 'magic' gets in the way, it can be suppressed with the
230 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
233 struct _GSettingsPrivate
235 /* where the signals go... */
236 GMainContext *main_context;
238 GSettingsBackend *backend;
239 GSettingsSchema *schema;
242 gboolean is_subscribed;
244 GDelayedSettingsBackend *delayed;
260 SIGNAL_WRITABLE_CHANGE_EVENT,
261 SIGNAL_WRITABLE_CHANGED,
267 static guint g_settings_signals[N_SIGNALS];
269 G_DEFINE_TYPE_WITH_PRIVATE (GSettings, g_settings, G_TYPE_OBJECT)
273 g_settings_real_change_event (GSettings *settings,
280 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
282 for (i = 0; i < n_keys; i++)
284 const gchar *key = g_quark_to_string (keys[i]);
286 if (g_str_has_suffix (key, "/"))
289 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED], keys[i], key);
296 g_settings_real_writable_change_event (GSettings *settings,
299 const GQuark *keys = &key;
304 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
306 for (i = 0; i < n_keys; i++)
308 const gchar *key = g_quark_to_string (keys[i]);
310 if (g_str_has_suffix (key, "/"))
313 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keys[i], key);
320 g_settings_has_signal_handlers (GSettings *settings,
323 GSettingsClass *class = G_SETTINGS_GET_CLASS (settings);
326 if (class->change_event != g_settings_real_change_event ||
327 class->writable_change_event != g_settings_real_writable_change_event)
330 keyq = g_quark_from_string (key);
332 if (g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT], 0, TRUE) ||
333 g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], 0, TRUE) ||
334 g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keyq, TRUE) ||
335 g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_CHANGE_EVENT], 0, TRUE) ||
336 g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_CHANGED], 0, TRUE) ||
337 g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_CHANGED], keyq, TRUE))
340 /* None of that? Then surely nobody is watching.... */
346 settings_backend_changed (GObject *target,
347 GSettingsBackend *backend,
351 GSettings *settings = G_SETTINGS (target);
352 gboolean ignore_this;
355 /* We used to assert here:
357 * settings->priv->backend == backend
359 * but it could be the case that a notification is queued for delivery
360 * while someone calls g_settings_delay() (which changes the backend).
362 * Since the delay backend would just pass that straight through
363 * anyway, it doesn't make sense to try to detect this case.
364 * Therefore, we just accept it.
367 for (i = 0; key[i] == settings->priv->path[i]; i++);
369 if (settings->priv->path[i] == '\0' &&
370 g_settings_schema_has_key (settings->priv->schema, key + i))
374 quark = g_quark_from_string (key + i);
375 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
376 0, &quark, 1, &ignore_this);
381 settings_backend_path_changed (GObject *target,
382 GSettingsBackend *backend,
386 GSettings *settings = G_SETTINGS (target);
387 gboolean ignore_this;
389 if (g_str_has_prefix (settings->priv->path, path))
390 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
391 0, NULL, 0, &ignore_this);
395 settings_backend_keys_changed (GObject *target,
396 GSettingsBackend *backend,
399 const gchar * const *items)
401 GSettings *settings = G_SETTINGS (target);
402 gboolean ignore_this;
405 for (i = 0; settings->priv->path[i] &&
406 settings->priv->path[i] == path[i]; i++);
413 for (j = 0; items[j]; j++)
415 const gchar *item = items[j];
418 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
420 if (settings->priv->path[i + k] == '\0' &&
421 g_settings_schema_has_key (settings->priv->schema, item + k))
422 quarks[l++] = g_quark_from_string (item + k);
424 /* "256 quarks ought to be enough for anybody!"
425 * If this bites you, I'm sorry. Please file a bug.
431 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
432 0, quarks, l, &ignore_this);
437 settings_backend_writable_changed (GObject *target,
438 GSettingsBackend *backend,
441 GSettings *settings = G_SETTINGS (target);
442 gboolean ignore_this;
445 for (i = 0; key[i] == settings->priv->path[i]; i++);
447 if (settings->priv->path[i] == '\0' &&
448 g_settings_schema_has_key (settings->priv->schema, key + i))
449 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
450 0, g_quark_from_string (key + i), &ignore_this);
454 settings_backend_path_writable_changed (GObject *target,
455 GSettingsBackend *backend,
458 GSettings *settings = G_SETTINGS (target);
459 gboolean ignore_this;
461 if (g_str_has_prefix (settings->priv->path, path))
462 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
463 0, (GQuark) 0, &ignore_this);
466 /* Properties, Construction, Destruction {{{1 */
468 g_settings_set_property (GObject *object,
473 GSettings *settings = G_SETTINGS (object);
479 GSettingsSchema *schema;
481 schema = g_value_dup_boxed (value);
483 /* we receive a set_property() call for "settings-schema" even
484 * if it was not specified (ie: with NULL value). ->schema
485 * could already be set at this point (ie: via "schema-id").
486 * check for NULL to avoid clobbering the existing value.
490 g_assert (settings->priv->schema == NULL);
491 settings->priv->schema = schema;
498 const gchar *schema_id;
500 schema_id = g_value_get_string (value);
502 /* we receive a set_property() call for both "schema" and
503 * "schema-id", even if they are not set. Hopefully only one of
506 if (schema_id != NULL)
508 GSettingsSchemaSource *default_source;
510 g_assert (settings->priv->schema == NULL);
511 default_source = g_settings_schema_source_get_default ();
513 if (default_source == NULL)
514 g_error ("No GSettings schemas are installed on the system");
516 settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
518 if (settings->priv->schema == NULL)
519 g_error ("Settings schema '%s' is not installed\n", schema_id);
525 settings->priv->path = g_value_dup_string (value);
529 settings->priv->backend = g_value_dup_object (value);
533 g_assert_not_reached ();
538 g_settings_get_property (GObject *object,
543 GSettings *settings = G_SETTINGS (object);
548 g_value_set_boxed (value, settings->priv->schema);
552 g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
556 g_value_set_object (value, settings->priv->backend);
560 g_value_set_string (value, settings->priv->path);
563 case PROP_HAS_UNAPPLIED:
564 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
567 case PROP_DELAY_APPLY:
568 g_value_set_boolean (value, settings->priv->delayed != NULL);
572 g_assert_not_reached ();
576 static const GSettingsListenerVTable listener_vtable = {
577 settings_backend_changed,
578 settings_backend_path_changed,
579 settings_backend_keys_changed,
580 settings_backend_writable_changed,
581 settings_backend_path_writable_changed
585 g_settings_constructed (GObject *object)
587 GSettings *settings = G_SETTINGS (object);
588 const gchar *schema_path;
590 schema_path = g_settings_schema_get_path (settings->priv->schema);
592 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
593 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
594 g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
596 if (settings->priv->path == NULL)
598 if (schema_path == NULL)
599 g_error ("attempting to create schema '%s' without a path",
600 g_settings_schema_get_id (settings->priv->schema));
602 settings->priv->path = g_strdup (schema_path);
605 if (settings->priv->backend == NULL)
606 settings->priv->backend = g_settings_backend_get_default ();
608 g_settings_backend_watch (settings->priv->backend,
609 &listener_vtable, G_OBJECT (settings),
610 settings->priv->main_context);
614 g_settings_finalize (GObject *object)
616 GSettings *settings = G_SETTINGS (object);
618 if (settings->priv->is_subscribed)
619 g_settings_backend_unsubscribe (settings->priv->backend,
620 settings->priv->path);
622 g_main_context_unref (settings->priv->main_context);
623 g_object_unref (settings->priv->backend);
624 g_settings_schema_unref (settings->priv->schema);
625 g_free (settings->priv->path);
627 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
631 g_settings_init (GSettings *settings)
633 settings->priv = g_settings_get_instance_private (settings);
634 settings->priv->main_context = g_main_context_ref_thread_default ();
638 g_settings_class_init (GSettingsClass *class)
640 GObjectClass *object_class = G_OBJECT_CLASS (class);
642 class->writable_change_event = g_settings_real_writable_change_event;
643 class->change_event = g_settings_real_change_event;
645 object_class->set_property = g_settings_set_property;
646 object_class->get_property = g_settings_get_property;
647 object_class->constructed = g_settings_constructed;
648 object_class->finalize = g_settings_finalize;
651 * GSettings::changed:
652 * @settings: the object on which the signal was emitted
653 * @key: the name of the key that changed
655 * The "changed" signal is emitted when a key has potentially changed.
656 * You should call one of the g_settings_get() calls to check the new
659 * This signal supports detailed connections. You can connect to the
660 * detailed signal "changed::x" in order to only receive callbacks
661 * when key "x" changes.
663 g_settings_signals[SIGNAL_CHANGED] =
664 g_signal_new ("changed", G_TYPE_SETTINGS,
665 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
666 G_STRUCT_OFFSET (GSettingsClass, changed),
667 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
668 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
671 * GSettings::change-event:
672 * @settings: the object on which the signal was emitted
673 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
674 * an array of #GQuarks for the changed keys, or %NULL
675 * @n_keys: the length of the @keys array, or 0
677 * The "change-event" signal is emitted once per change event that
678 * affects this settings object. You should connect to this signal
679 * only if you are interested in viewing groups of changes before they
680 * are split out into multiple emissions of the "changed" signal.
681 * For most use cases it is more appropriate to use the "changed" signal.
683 * In the event that the change event applies to one or more specified
684 * keys, @keys will be an array of #GQuark of length @n_keys. In the
685 * event that the change event applies to the #GSettings object as a
686 * whole (ie: potentially every key has been changed) then @keys will
687 * be %NULL and @n_keys will be 0.
689 * The default handler for this signal invokes the "changed" signal
690 * for each affected key. If any other connected handler returns
691 * %TRUE then this default functionality will be suppressed.
693 * Returns: %TRUE to stop other handlers from being invoked for the
694 * event. FALSE to propagate the event further.
696 g_settings_signals[SIGNAL_CHANGE_EVENT] =
697 g_signal_new ("change-event", G_TYPE_SETTINGS,
699 G_STRUCT_OFFSET (GSettingsClass, change_event),
700 g_signal_accumulator_true_handled, NULL,
702 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
705 * GSettings::writable-changed:
706 * @settings: the object on which the signal was emitted
709 * The "writable-changed" signal is emitted when the writability of a
710 * key has potentially changed. You should call
711 * g_settings_is_writable() in order to determine the new status.
713 * This signal supports detailed connections. You can connect to the
714 * detailed signal "writable-changed::x" in order to only receive
715 * callbacks when the writability of "x" changes.
717 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
718 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
719 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
720 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
721 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
722 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
725 * GSettings::writable-change-event:
726 * @settings: the object on which the signal was emitted
727 * @key: the quark of the key, or 0
729 * The "writable-change-event" signal is emitted once per writability
730 * change event that affects this settings object. You should connect
731 * to this signal if you are interested in viewing groups of changes
732 * before they are split out into multiple emissions of the
733 * "writable-changed" signal. For most use cases it is more
734 * appropriate to use the "writable-changed" signal.
736 * In the event that the writability change applies only to a single
737 * key, @key will be set to the #GQuark for that key. In the event
738 * that the writability change affects the entire settings object,
741 * The default handler for this signal invokes the "writable-changed"
742 * and "changed" signals for each affected key. This is done because
743 * changes in writability might also imply changes in value (if for
744 * example, a new mandatory setting is introduced). If any other
745 * connected handler returns %TRUE then this default functionality
746 * will be suppressed.
748 * Returns: %TRUE to stop other handlers from being invoked for the
749 * event. FALSE to propagate the event further.
751 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
752 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
754 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
755 g_signal_accumulator_true_handled, NULL,
756 NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
761 * The name of the context that the settings are stored in.
763 g_object_class_install_property (object_class, PROP_BACKEND,
764 g_param_spec_object ("backend",
765 P_("GSettingsBackend"),
766 P_("The GSettingsBackend for this settings object"),
767 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
768 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
771 * GSettings:settings-schema:
773 * The #GSettingsSchema describing the types of keys for this
776 * Ideally, this property would be called 'schema'. #GSettingsSchema
777 * has only existed since version 2.32, however, and before then the
778 * 'schema' property was used to refer to the ID of the schema rather
779 * than the schema itself. Take care.
781 g_object_class_install_property (object_class, PROP_SCHEMA,
782 g_param_spec_boxed ("settings-schema",
784 P_("The GSettingsSchema for this settings object"),
785 G_TYPE_SETTINGS_SCHEMA,
786 G_PARAM_CONSTRUCT_ONLY |
787 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
792 * The name of the schema that describes the types of keys
793 * for this #GSettings object.
795 * The type of this property is *not* #GSettingsSchema.
796 * #GSettingsSchema has only existed since version 2.32 and
797 * unfortunately this name was used in previous versions to refer to
798 * the schema ID rather than the schema itself. Take care to use the
799 * 'settings-schema' property if you wish to pass in a
802 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
803 * version, this property may instead refer to a #GSettingsSchema.
805 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
806 g_param_spec_string ("schema",
808 P_("The name of the schema for this settings object"),
810 G_PARAM_CONSTRUCT_ONLY |
811 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814 * GSettings:schema-id:
816 * The name of the schema that describes the types of keys
817 * for this #GSettings object.
819 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
820 g_param_spec_string ("schema-id",
822 P_("The name of the schema for this settings object"),
824 G_PARAM_CONSTRUCT_ONLY |
825 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
830 * The path within the backend where the settings are stored.
832 g_object_class_install_property (object_class, PROP_PATH,
833 g_param_spec_string ("path",
835 P_("The path within the backend where the settings are"),
837 G_PARAM_CONSTRUCT_ONLY |
838 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
841 * GSettings:has-unapplied:
843 * If this property is %TRUE, the #GSettings object has outstanding
844 * changes that will be applied when g_settings_apply() is called.
846 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
847 g_param_spec_boolean ("has-unapplied",
848 P_("Has unapplied changes"),
849 P_("TRUE if there are outstanding changes to apply()"),
851 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
854 * GSettings:delay-apply:
856 * Whether the #GSettings object is in 'delay-apply' mode. See
857 * g_settings_delay() for details.
861 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
862 g_param_spec_boolean ("delay-apply",
863 P_("Delay-apply mode"),
864 P_("Whether this settings object is in 'delay-apply' mode"),
866 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
869 /* Construction (new, new_with_path, etc.) {{{1 */
872 * @schema_id: the id of the schema
874 * Creates a new #GSettings object with the schema specified by
877 * Signals on the newly created #GSettings object will be dispatched
878 * via the thread-default #GMainContext in effect at the time of the
879 * call to g_settings_new(). The new #GSettings will hold a reference
880 * on the context. See g_main_context_push_thread_default().
882 * Returns: a new #GSettings object
887 g_settings_new (const gchar *schema_id)
889 g_return_val_if_fail (schema_id != NULL, NULL);
891 return g_object_new (G_TYPE_SETTINGS,
892 "schema-id", schema_id,
897 path_is_valid (const gchar *path)
905 if (!g_str_has_suffix (path, "/"))
908 return strstr (path, "//") == NULL;
912 * g_settings_new_with_path:
913 * @schema_id: the id of the schema
914 * @path: the path to use
916 * Creates a new #GSettings object with the relocatable schema specified
917 * by @schema_id and a given path.
919 * You only need to do this if you want to directly create a settings
920 * object with a schema that doesn't have a specified path of its own.
923 * It is a programmer error to call this function for a schema that
924 * has an explicitly specified path.
926 * It is a programmer error if @path is not a valid path. A valid path
927 * begins and ends with '/' and does not contain two consecutive '/'
930 * Returns: a new #GSettings object
935 g_settings_new_with_path (const gchar *schema_id,
938 g_return_val_if_fail (schema_id != NULL, NULL);
939 g_return_val_if_fail (path_is_valid (path), NULL);
941 return g_object_new (G_TYPE_SETTINGS,
942 "schema-id", schema_id,
948 * g_settings_new_with_backend:
949 * @schema_id: the id of the schema
950 * @backend: the #GSettingsBackend to use
952 * Creates a new #GSettings object with the schema specified by
953 * @schema_id and a given #GSettingsBackend.
955 * Creating a #GSettings object with a different backend allows accessing
956 * settings from a database other than the usual one. For example, it may make
957 * sense to pass a backend corresponding to the "defaults" settings database on
958 * the system to get a settings object that modifies the system default
959 * settings instead of the settings for this user.
961 * Returns: a new #GSettings object
966 g_settings_new_with_backend (const gchar *schema_id,
967 GSettingsBackend *backend)
969 g_return_val_if_fail (schema_id != NULL, NULL);
970 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
972 return g_object_new (G_TYPE_SETTINGS,
973 "schema-id", schema_id,
979 * g_settings_new_with_backend_and_path:
980 * @schema_id: the id of the schema
981 * @backend: the #GSettingsBackend to use
982 * @path: the path to use
984 * Creates a new #GSettings object with the schema specified by
985 * @schema_id and a given #GSettingsBackend and path.
987 * This is a mix of g_settings_new_with_backend() and
988 * g_settings_new_with_path().
990 * Returns: a new #GSettings object
995 g_settings_new_with_backend_and_path (const gchar *schema_id,
996 GSettingsBackend *backend,
999 g_return_val_if_fail (schema_id != NULL, NULL);
1000 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
1001 g_return_val_if_fail (path_is_valid (path), NULL);
1003 return g_object_new (G_TYPE_SETTINGS,
1004 "schema-id", schema_id,
1011 * g_settings_new_full:
1012 * @schema: a #GSettingsSchema
1013 * @backend: (allow-none): a #GSettingsBackend
1014 * @path: (allow-none): the path to use
1016 * Creates a new #GSettings object with a given schema, backend and
1019 * It should be extremely rare that you ever want to use this function.
1020 * It is made available for advanced use-cases (such as plugin systems
1021 * that want to provide access to schemas loaded from custom locations,
1024 * At the most basic level, a #GSettings object is a pure composition of
1025 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
1026 * backend, and a #GMainContext to which signals are dispatched.
1028 * This constructor therefore gives you full control over constructing
1029 * #GSettings instances. The first 4 parameters are given directly as
1030 * @schema, @backend and @path, and the main context is taken from the
1031 * thread-default (as per g_settings_new()).
1033 * If @backend is %NULL then the default backend is used.
1035 * If @path is %NULL then the path from the schema is used. It is an
1036 * error f @path is %NULL and the schema has no path of its own or if
1037 * @path is non-%NULL and not equal to the path that the schema does
1040 * Returns: a new #GSettings object
1045 g_settings_new_full (GSettingsSchema *schema,
1046 GSettingsBackend *backend,
1049 g_return_val_if_fail (schema != NULL, NULL);
1050 g_return_val_if_fail (backend == NULL || G_IS_SETTINGS_BACKEND (backend), NULL);
1051 g_return_val_if_fail (path == NULL || path_is_valid (path), NULL);
1053 return g_object_new (G_TYPE_SETTINGS,
1054 "settings-schema", schema,
1060 /* Internal read/write utilities {{{1 */
1062 g_settings_write_to_backend (GSettings *settings,
1063 GSettingsSchemaKey *key,
1069 path = g_strconcat (settings->priv->path, key->name, NULL);
1070 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1077 g_settings_read_from_backend (GSettings *settings,
1078 GSettingsSchemaKey *key,
1079 gboolean user_value_only,
1080 gboolean default_value)
1086 /* If we are not yet watching for changes, consider doing it now... */
1087 if (!settings->priv->is_subscribed && g_settings_has_signal_handlers (settings, key->name))
1089 g_settings_backend_subscribe (settings->priv->backend, settings->priv->path);
1090 settings->priv->is_subscribed = TRUE;
1093 path = g_strconcat (settings->priv->path, key->name, NULL);
1094 if (user_value_only)
1095 value = g_settings_backend_read_user_value (settings->priv->backend, path, key->type);
1097 value = g_settings_backend_read (settings->priv->backend, path, key->type, default_value);
1102 fixup = g_settings_schema_key_range_fixup (key, value);
1103 g_variant_unref (value);
1111 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1113 * g_settings_get_value:
1114 * @settings: a #GSettings object
1115 * @key: the key to get the value for
1117 * Gets the value that is stored in @settings for @key.
1119 * It is a programmer error to give a @key that isn't contained in the
1120 * schema for @settings.
1122 * Returns: a new #GVariant
1127 g_settings_get_value (GSettings *settings,
1130 GSettingsSchemaKey skey;
1133 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1134 g_return_val_if_fail (key != NULL, NULL);
1136 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1137 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1140 value = g_settings_schema_key_get_translated_default (&skey);
1143 value = g_variant_ref (skey.default_value);
1145 g_settings_schema_key_clear (&skey);
1151 * g_settings_get_user_value:
1152 * @settings: a #GSettings object
1153 * @key: the key to get the user value for
1155 * Checks the "user value" of a key, if there is one.
1157 * The user value of a key is the last value that was set by the user.
1159 * After calling g_settings_reset() this function should always return
1160 * %NULL (assuming something is not wrong with the system
1163 * It is possible that g_settings_get_value() will return a different
1164 * value than this function. This can happen in the case that the user
1165 * set a value for a key that was subsequently locked down by the system
1166 * administrator -- this function will return the user's old value.
1168 * This function may be useful for adding a "reset" option to a UI or
1169 * for providing indication that a particular value has been changed.
1171 * It is a programmer error to give a @key that isn't contained in the
1172 * schema for @settings.
1174 * Returns: (allow-none) (transfer full): the user's value, if set
1179 g_settings_get_user_value (GSettings *settings,
1182 GSettingsSchemaKey skey;
1185 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1186 g_return_val_if_fail (key != NULL, NULL);
1188 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1189 value = g_settings_read_from_backend (settings, &skey, TRUE, FALSE);
1190 g_settings_schema_key_clear (&skey);
1196 * g_settings_get_default_value:
1197 * @settings: a #GSettings object
1198 * @key: the key to get the default value for
1200 * Gets the "default value" of a key.
1202 * This is the value that would be read if g_settings_reset() were to be
1203 * called on the key.
1205 * Note that this may be a different value than returned by
1206 * g_settings_schema_key_get_default_value() if the system administrator
1207 * has provided a default value.
1209 * Comparing the return values of g_settings_get_default_value() and
1210 * g_settings_get_value() is not sufficient for determining if a value
1211 * has been set because the user may have explicitly set the value to
1212 * something that happens to be equal to the default. The difference
1213 * here is that if the default changes in the future, the user's key
1214 * will still be set.
1216 * This function may be useful for adding an indication to a UI of what
1217 * the default value was before the user set it.
1219 * It is a programmer error to give a @key that isn't contained in the
1220 * schema for @settings.
1222 * Returns: (allow-none) (transfer full): the default value
1227 g_settings_get_default_value (GSettings *settings,
1230 GSettingsSchemaKey skey;
1233 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1234 g_return_val_if_fail (key != NULL, NULL);
1236 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1237 value = g_settings_read_from_backend (settings, &skey, FALSE, TRUE);
1240 value = g_settings_schema_key_get_translated_default (&skey);
1243 value = g_variant_ref (skey.default_value);
1245 g_settings_schema_key_clear (&skey);
1251 * g_settings_get_enum:
1252 * @settings: a #GSettings object
1253 * @key: the key to get the value for
1255 * Gets the value that is stored in @settings for @key and converts it
1256 * to the enum value that it represents.
1258 * In order to use this function the type of the value must be a string
1259 * and it must be marked in the schema file as an enumerated type.
1261 * It is a programmer error to give a @key that isn't contained in the
1262 * schema for @settings or is not marked as an enumerated type.
1264 * If the value stored in the configuration database is not a valid
1265 * value for the enumerated type then this function will return the
1268 * Returns: the enum value
1273 g_settings_get_enum (GSettings *settings,
1276 GSettingsSchemaKey skey;
1280 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1281 g_return_val_if_fail (key != NULL, -1);
1283 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1287 g_critical ("g_settings_get_enum() called on key '%s' which is not "
1288 "associated with an enumerated type", skey.name);
1289 g_settings_schema_key_clear (&skey);
1293 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1296 value = g_settings_schema_key_get_translated_default (&skey);
1299 value = g_variant_ref (skey.default_value);
1301 result = g_settings_schema_key_to_enum (&skey, value);
1302 g_settings_schema_key_clear (&skey);
1303 g_variant_unref (value);
1309 * g_settings_set_enum:
1310 * @settings: a #GSettings object
1311 * @key: a key, within @settings
1312 * @value: an enumerated value
1314 * Looks up the enumerated type nick for @value and writes it to @key,
1317 * It is a programmer error to give a @key that isn't contained in the
1318 * schema for @settings or is not marked as an enumerated type, or for
1319 * @value not to be a valid value for the named type.
1321 * After performing the write, accessing @key directly with
1322 * g_settings_get_string() will return the 'nick' associated with
1325 * Returns: %TRUE, if the set succeeds
1328 g_settings_set_enum (GSettings *settings,
1332 GSettingsSchemaKey skey;
1336 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1337 g_return_val_if_fail (key != NULL, FALSE);
1339 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1343 g_critical ("g_settings_set_enum() called on key '%s' which is not "
1344 "associated with an enumerated type", skey.name);
1348 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1350 g_critical ("g_settings_set_enum(): invalid enum value %d for key '%s' "
1351 "in schema '%s'. Doing nothing.", value, skey.name,
1352 g_settings_schema_get_id (skey.schema));
1353 g_settings_schema_key_clear (&skey);
1357 success = g_settings_write_to_backend (settings, &skey, variant);
1358 g_settings_schema_key_clear (&skey);
1364 * g_settings_get_flags:
1365 * @settings: a #GSettings object
1366 * @key: the key to get the value for
1368 * Gets the value that is stored in @settings for @key and converts it
1369 * to the flags value that it represents.
1371 * In order to use this function the type of the value must be an array
1372 * of strings and it must be marked in the schema file as an flags type.
1374 * It is a programmer error to give a @key that isn't contained in the
1375 * schema for @settings or is not marked as a flags type.
1377 * If the value stored in the configuration database is not a valid
1378 * value for the flags type then this function will return the default
1381 * Returns: the flags value
1386 g_settings_get_flags (GSettings *settings,
1389 GSettingsSchemaKey skey;
1393 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1394 g_return_val_if_fail (key != NULL, -1);
1396 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1400 g_critical ("g_settings_get_flags() called on key '%s' which is not "
1401 "associated with a flags type", skey.name);
1402 g_settings_schema_key_clear (&skey);
1406 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1409 value = g_settings_schema_key_get_translated_default (&skey);
1412 value = g_variant_ref (skey.default_value);
1414 result = g_settings_schema_key_to_flags (&skey, value);
1415 g_settings_schema_key_clear (&skey);
1416 g_variant_unref (value);
1422 * g_settings_set_flags:
1423 * @settings: a #GSettings object
1424 * @key: a key, within @settings
1425 * @value: a flags value
1427 * Looks up the flags type nicks for the bits specified by @value, puts
1428 * them in an array of strings and writes the array to @key, within
1431 * It is a programmer error to give a @key that isn't contained in the
1432 * schema for @settings or is not marked as a flags type, or for @value
1433 * to contain any bits that are not value for the named type.
1435 * After performing the write, accessing @key directly with
1436 * g_settings_get_strv() will return an array of 'nicks'; one for each
1439 * Returns: %TRUE, if the set succeeds
1442 g_settings_set_flags (GSettings *settings,
1446 GSettingsSchemaKey skey;
1450 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1451 g_return_val_if_fail (key != NULL, FALSE);
1453 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1457 g_critical ("g_settings_set_flags() called on key '%s' which is not "
1458 "associated with a flags type", skey.name);
1462 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1464 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1465 "for key '%s' in schema '%s'. Doing nothing.",
1466 value, skey.name, g_settings_schema_get_id (skey.schema));
1467 g_settings_schema_key_clear (&skey);
1471 success = g_settings_write_to_backend (settings, &skey, variant);
1472 g_settings_schema_key_clear (&skey);
1478 * g_settings_set_value:
1479 * @settings: a #GSettings object
1480 * @key: the name of the key to set
1481 * @value: a #GVariant of the correct type
1483 * Sets @key in @settings to @value.
1485 * It is a programmer error to give a @key that isn't contained in the
1486 * schema for @settings or for @value to have the incorrect type, per
1489 * If @value is floating then this function consumes the reference.
1491 * Returns: %TRUE if setting the key succeeded,
1492 * %FALSE if the key was not writable
1497 g_settings_set_value (GSettings *settings,
1501 GSettingsSchemaKey skey;
1504 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1505 g_return_val_if_fail (key != NULL, FALSE);
1507 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1509 if (!g_settings_schema_key_type_check (&skey, value))
1511 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1513 g_settings_schema_get_id (settings->priv->schema),
1514 g_variant_type_peek_string (skey.type),
1515 g_variant_get_type_string (value));
1520 if (!g_settings_schema_key_range_check (&skey, value))
1522 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1523 "is outside of valid range",
1525 g_settings_schema_get_id (settings->priv->schema));
1530 success = g_settings_write_to_backend (settings, &skey, value);
1531 g_settings_schema_key_clear (&skey);
1538 * @settings: a #GSettings object
1539 * @key: the key to get the value for
1540 * @format: a #GVariant format string
1541 * @...: arguments as per @format
1543 * Gets the value that is stored at @key in @settings.
1545 * A convenience function that combines g_settings_get_value() with
1548 * It is a programmer error to give a @key that isn't contained in the
1549 * schema for @settings or for the #GVariantType of @format to mismatch
1550 * the type given in the schema.
1555 g_settings_get (GSettings *settings,
1557 const gchar *format,
1563 value = g_settings_get_value (settings, key);
1565 if (strchr (format, '&'))
1567 g_critical ("%s: the format string may not contain '&' (key '%s' from schema '%s'). "
1568 "This call will probably stop working with a future version of glib.",
1569 G_STRFUNC, key, g_settings_schema_get_id (settings->priv->schema));
1572 va_start (ap, format);
1573 g_variant_get_va (value, format, NULL, &ap);
1576 g_variant_unref (value);
1581 * @settings: a #GSettings object
1582 * @key: the name of the key to set
1583 * @format: a #GVariant format string
1584 * @...: arguments as per @format
1586 * Sets @key in @settings to @value.
1588 * A convenience function that combines g_settings_set_value() with
1591 * It is a programmer error to give a @key that isn't contained in the
1592 * schema for @settings or for the #GVariantType of @format to mismatch
1593 * the type given in the schema.
1595 * Returns: %TRUE if setting the key succeeded,
1596 * %FALSE if the key was not writable
1601 g_settings_set (GSettings *settings,
1603 const gchar *format,
1609 va_start (ap, format);
1610 value = g_variant_new_va (format, NULL, &ap);
1613 return g_settings_set_value (settings, key, value);
1617 * g_settings_get_mapped:
1618 * @settings: a #GSettings object
1619 * @key: the key to get the value for
1620 * @mapping: (scope call): the function to map the value in the
1621 * settings database to the value used by the application
1622 * @user_data: user data for @mapping
1624 * Gets the value that is stored at @key in @settings, subject to
1625 * application-level validation/mapping.
1627 * You should use this function when the application needs to perform
1628 * some processing on the value of the key (for example, parsing). The
1629 * @mapping function performs that processing. If the function
1630 * indicates that the processing was unsuccessful (due to a parse error,
1631 * for example) then the mapping is tried again with another value.
1633 * This allows a robust 'fall back to defaults' behaviour to be
1634 * implemented somewhat automatically.
1636 * The first value that is tried is the user's setting for the key. If
1637 * the mapping function fails to map this value, other values may be
1638 * tried in an unspecified order (system or site defaults, translated
1639 * schema default values, untranslated schema default values, etc).
1641 * If the mapping function fails for all possible values, one additional
1642 * attempt is made: the mapping function is called with a %NULL value.
1643 * If the mapping function still indicates failure at this point then
1644 * the application will be aborted.
1646 * The result parameter for the @mapping function is pointed to a
1647 * #gpointer which is initially set to %NULL. The same pointer is given
1648 * to each invocation of @mapping. The final value of that #gpointer is
1649 * what is returned by this function. %NULL is valid; it is returned
1650 * just as any other value would be.
1652 * Returns: (transfer full): the result, which may be %NULL
1655 g_settings_get_mapped (GSettings *settings,
1657 GSettingsGetMapping mapping,
1660 gpointer result = NULL;
1661 GSettingsSchemaKey skey;
1665 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1666 g_return_val_if_fail (key != NULL, NULL);
1667 g_return_val_if_fail (mapping != NULL, NULL);
1669 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1671 if ((value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE)))
1673 okay = mapping (value, &result, user_data);
1674 g_variant_unref (value);
1675 if (okay) goto okay;
1678 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1680 okay = mapping (value, &result, user_data);
1681 g_variant_unref (value);
1682 if (okay) goto okay;
1685 if (mapping (skey.default_value, &result, user_data))
1688 if (!mapping (NULL, &result, user_data))
1689 g_error ("The mapping function given to g_settings_get_mapped() for key "
1690 "'%s' in schema '%s' returned FALSE when given a NULL value.",
1691 key, g_settings_schema_get_id (settings->priv->schema));
1694 g_settings_schema_key_clear (&skey);
1699 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1701 * g_settings_get_string:
1702 * @settings: a #GSettings object
1703 * @key: the key to get the value for
1705 * Gets the value that is stored at @key in @settings.
1707 * A convenience variant of g_settings_get() for strings.
1709 * It is a programmer error to give a @key that isn't specified as
1710 * having a string type in the schema for @settings.
1712 * Returns: a newly-allocated string
1717 g_settings_get_string (GSettings *settings,
1723 value = g_settings_get_value (settings, key);
1724 result = g_variant_dup_string (value, NULL);
1725 g_variant_unref (value);
1731 * g_settings_set_string:
1732 * @settings: a #GSettings object
1733 * @key: the name of the key to set
1734 * @value: the value to set it to
1736 * Sets @key in @settings to @value.
1738 * A convenience variant of g_settings_set() for strings.
1740 * It is a programmer error to give a @key that isn't specified as
1741 * having a string type in the schema for @settings.
1743 * Returns: %TRUE if setting the key succeeded,
1744 * %FALSE if the key was not writable
1749 g_settings_set_string (GSettings *settings,
1753 return g_settings_set_value (settings, key, g_variant_new_string (value));
1757 * g_settings_get_int:
1758 * @settings: a #GSettings object
1759 * @key: the key to get the value for
1761 * Gets the value that is stored at @key in @settings.
1763 * A convenience variant of g_settings_get() for 32-bit integers.
1765 * It is a programmer error to give a @key that isn't specified as
1766 * having a int32 type in the schema for @settings.
1768 * Returns: an integer
1773 g_settings_get_int (GSettings *settings,
1779 value = g_settings_get_value (settings, key);
1780 result = g_variant_get_int32 (value);
1781 g_variant_unref (value);
1787 * g_settings_set_int:
1788 * @settings: a #GSettings object
1789 * @key: the name of the key to set
1790 * @value: the value to set it to
1792 * Sets @key in @settings to @value.
1794 * A convenience variant of g_settings_set() for 32-bit integers.
1796 * It is a programmer error to give a @key that isn't specified as
1797 * having a int32 type in the schema for @settings.
1799 * Returns: %TRUE if setting the key succeeded,
1800 * %FALSE if the key was not writable
1805 g_settings_set_int (GSettings *settings,
1809 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1813 * g_settings_get_uint:
1814 * @settings: a #GSettings object
1815 * @key: the key to get the value for
1817 * Gets the value that is stored at @key in @settings.
1819 * A convenience variant of g_settings_get() for 32-bit unsigned
1822 * It is a programmer error to give a @key that isn't specified as
1823 * having a uint32 type in the schema for @settings.
1825 * Returns: an unsigned integer
1830 g_settings_get_uint (GSettings *settings,
1836 value = g_settings_get_value (settings, key);
1837 result = g_variant_get_uint32 (value);
1838 g_variant_unref (value);
1844 * g_settings_set_uint:
1845 * @settings: a #GSettings object
1846 * @key: the name of the key to set
1847 * @value: the value to set it to
1849 * Sets @key in @settings to @value.
1851 * A convenience variant of g_settings_set() for 32-bit unsigned
1854 * It is a programmer error to give a @key that isn't specified as
1855 * having a uint32 type in the schema for @settings.
1857 * Returns: %TRUE if setting the key succeeded,
1858 * %FALSE if the key was not writable
1863 g_settings_set_uint (GSettings *settings,
1867 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1871 * g_settings_get_double:
1872 * @settings: a #GSettings object
1873 * @key: the key to get the value for
1875 * Gets the value that is stored at @key in @settings.
1877 * A convenience variant of g_settings_get() for doubles.
1879 * It is a programmer error to give a @key that isn't specified as
1880 * having a 'double' type in the schema for @settings.
1887 g_settings_get_double (GSettings *settings,
1893 value = g_settings_get_value (settings, key);
1894 result = g_variant_get_double (value);
1895 g_variant_unref (value);
1901 * g_settings_set_double:
1902 * @settings: a #GSettings object
1903 * @key: the name of the key to set
1904 * @value: the value to set it to
1906 * Sets @key in @settings to @value.
1908 * A convenience variant of g_settings_set() for doubles.
1910 * It is a programmer error to give a @key that isn't specified as
1911 * having a 'double' type in the schema for @settings.
1913 * Returns: %TRUE if setting the key succeeded,
1914 * %FALSE if the key was not writable
1919 g_settings_set_double (GSettings *settings,
1923 return g_settings_set_value (settings, key, g_variant_new_double (value));
1927 * g_settings_get_boolean:
1928 * @settings: a #GSettings object
1929 * @key: the key to get the value for
1931 * Gets the value that is stored at @key in @settings.
1933 * A convenience variant of g_settings_get() for booleans.
1935 * It is a programmer error to give a @key that isn't specified as
1936 * having a boolean type in the schema for @settings.
1938 * Returns: a boolean
1943 g_settings_get_boolean (GSettings *settings,
1949 value = g_settings_get_value (settings, key);
1950 result = g_variant_get_boolean (value);
1951 g_variant_unref (value);
1957 * g_settings_set_boolean:
1958 * @settings: a #GSettings object
1959 * @key: the name of the key to set
1960 * @value: the value to set it to
1962 * Sets @key in @settings to @value.
1964 * A convenience variant of g_settings_set() for booleans.
1966 * It is a programmer error to give a @key that isn't specified as
1967 * having a boolean type in the schema for @settings.
1969 * Returns: %TRUE if setting the key succeeded,
1970 * %FALSE if the key was not writable
1975 g_settings_set_boolean (GSettings *settings,
1979 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1983 * g_settings_get_strv:
1984 * @settings: a #GSettings object
1985 * @key: the key to get the value for
1987 * A convenience variant of g_settings_get() for string arrays.
1989 * It is a programmer error to give a @key that isn't specified as
1990 * having an array of strings type in the schema for @settings.
1992 * Returns: (array zero-terminated=1) (transfer full): a
1993 * newly-allocated, %NULL-terminated array of strings, the value that
1994 * is stored at @key in @settings.
1999 g_settings_get_strv (GSettings *settings,
2005 value = g_settings_get_value (settings, key);
2006 result = g_variant_dup_strv (value, NULL);
2007 g_variant_unref (value);
2013 * g_settings_set_strv:
2014 * @settings: a #GSettings object
2015 * @key: the name of the key to set
2016 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
2018 * Sets @key in @settings to @value.
2020 * A convenience variant of g_settings_set() for string arrays. If
2021 * @value is %NULL, then @key is set to be the empty array.
2023 * It is a programmer error to give a @key that isn't specified as
2024 * having an array of strings type in the schema for @settings.
2026 * Returns: %TRUE if setting the key succeeded,
2027 * %FALSE if the key was not writable
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
2128 * Returns whether the #GSettings object has any unapplied
2129 * changes. This can only be the case if it is in 'delayed-apply' mode.
2131 * Returns: %TRUE if @settings has unapplied changes
2136 g_settings_get_has_unapplied (GSettings *settings)
2138 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2140 return settings->priv->delayed &&
2141 g_delayed_settings_backend_get_has_unapplied (
2142 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2145 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2148 * @settings: a #GSettings object
2149 * @key: the name of a key
2151 * Resets @key to its default value.
2153 * This call resets the key, as much as possible, to its default value.
2154 * That might the value specified in the schema or the one set by the
2158 g_settings_reset (GSettings *settings,
2163 path = g_strconcat (settings->priv->path, key, NULL);
2164 g_settings_backend_reset (settings->priv->backend, path, NULL);
2171 * Ensures that all pending operations for the given are complete for
2172 * the default backend.
2174 * Writes made to a #GSettings are handled asynchronously. For this
2175 * reason, it is very unlikely that the changes have it to disk by the
2176 * time g_settings_set() returns.
2178 * This call will block until all of the writes have made it to the
2179 * backend. Since the mainloop is not running, no change notifications
2180 * will be dispatched during this call (but some may be queued by the
2181 * time the call is done).
2184 g_settings_sync (void)
2186 g_settings_backend_sync_default ();
2190 * g_settings_is_writable:
2191 * @settings: a #GSettings object
2192 * @name: the name of a key
2194 * Finds out if a key can be written or not
2196 * Returns: %TRUE if the key @name is writable
2201 g_settings_is_writable (GSettings *settings,
2207 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2209 path = g_strconcat (settings->priv->path, name, NULL);
2210 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2217 * g_settings_get_child:
2218 * @settings: a #GSettings object
2219 * @name: the name of the child schema
2221 * Creates a child settings object which has a base path of
2222 * `base-path/@name`, where `base-path` is the base path of
2225 * The schema for the child settings object must have been declared
2226 * in the schema of @settings using a <child> element.
2228 * Returns: (transfer full): a 'child' settings object
2233 g_settings_get_child (GSettings *settings,
2236 const gchar *child_schema;
2241 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2243 child_name = g_strconcat (name, "/", NULL);
2244 child_schema = g_settings_schema_get_string (settings->priv->schema,
2246 if (child_schema == NULL)
2247 g_error ("Schema '%s' has no child '%s'",
2248 g_settings_schema_get_id (settings->priv->schema), name);
2250 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2251 child = g_object_new (G_TYPE_SETTINGS,
2252 "backend", settings->priv->backend,
2253 "schema-id", child_schema,
2256 g_free (child_path);
2257 g_free (child_name);
2263 * g_settings_list_keys:
2264 * @settings: a #GSettings object
2266 * Introspects the list of keys on @settings.
2268 * You should probably not be calling this function from "normal" code
2269 * (since you should already know what keys are in your schema). This
2270 * function is intended for introspection reasons.
2272 * You should free the return value with g_strfreev() when you are done
2275 * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2278 g_settings_list_keys (GSettings *settings)
2285 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2286 strv = g_new (gchar *, n_keys + 1);
2287 for (i = j = 0; i < n_keys; i++)
2289 const gchar *key = g_quark_to_string (keys[i]);
2291 if (!g_str_has_suffix (key, "/"))
2292 strv[j++] = g_strdup (key);
2300 * g_settings_list_children:
2301 * @settings: a #GSettings object
2303 * Gets the list of children on @settings.
2305 * The list is exactly the list of strings for which it is not an error
2306 * to call g_settings_get_child().
2308 * For GSettings objects that are lists, this value can change at any
2309 * time and you should connect to the "children-changed" signal to watch
2310 * for those changes. Note that there is a race condition here: you may
2311 * request a child after listing it only for it to have been destroyed
2312 * in the meantime. For this reason, g_settings_get_child() may return
2313 * %NULL even for a child that was listed by this function.
2315 * For GSettings objects that are not lists, you should probably not be
2316 * calling this function from "normal" code (since you should already
2317 * know what children are in your schema). This function may still be
2318 * useful there for introspection reasons, however.
2320 * You should free the return value with g_strfreev() when you are done
2323 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2326 g_settings_list_children (GSettings *settings)
2333 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2334 strv = g_new (gchar *, n_keys + 1);
2335 for (i = j = 0; i < n_keys; i++)
2337 const gchar *key = g_quark_to_string (keys[i]);
2339 if (g_str_has_suffix (key, "/"))
2341 gint length = strlen (key);
2343 strv[j] = g_memdup (key, length);
2344 strv[j][length - 1] = '\0';
2354 * g_settings_get_range:
2355 * @settings: a #GSettings
2356 * @key: the key to query the range of
2358 * Queries the range of a key.
2362 * Deprecated:2.40:Use g_settings_schema_key_get_range() instead.
2365 g_settings_get_range (GSettings *settings,
2368 GSettingsSchemaKey skey;
2371 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2372 range = g_settings_schema_key_get_range (&skey);
2373 g_settings_schema_key_clear (&skey);
2379 * g_settings_range_check:
2380 * @settings: a #GSettings
2381 * @key: the key to check
2382 * @value: the value to check
2384 * Checks if the given @value is of the correct type and within the
2385 * permitted range for @key.
2387 * Returns: %TRUE if @value is valid for @key
2391 * Deprecated:2.40:Use g_settings_schema_key_range_check() instead.
2394 g_settings_range_check (GSettings *settings,
2398 GSettingsSchemaKey skey;
2401 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2402 good = g_settings_schema_key_range_check (&skey, value);
2403 g_settings_schema_key_clear (&skey);
2411 GSettingsSchemaKey key;
2412 GSettings *settings;
2415 GSettingsBindGetMapping get_mapping;
2416 GSettingsBindSetMapping set_mapping;
2418 GDestroyNotify destroy;
2420 guint writable_handler_id;
2421 guint property_handler_id;
2422 const GParamSpec *property;
2423 guint key_handler_id;
2425 /* prevent recursion */
2430 g_settings_binding_free (gpointer data)
2432 GSettingsBinding *binding = data;
2434 g_assert (!binding->running);
2436 if (binding->writable_handler_id)
2437 g_signal_handler_disconnect (binding->settings,
2438 binding->writable_handler_id);
2440 if (binding->key_handler_id)
2441 g_signal_handler_disconnect (binding->settings,
2442 binding->key_handler_id);
2444 if (g_signal_handler_is_connected (binding->object,
2445 binding->property_handler_id))
2446 g_signal_handler_disconnect (binding->object,
2447 binding->property_handler_id);
2449 g_settings_schema_key_clear (&binding->key);
2451 if (binding->destroy)
2452 binding->destroy (binding->user_data);
2454 g_object_unref (binding->settings);
2456 g_slice_free (GSettingsBinding, binding);
2460 g_settings_binding_quark (const char *property)
2465 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2466 quark = g_quark_from_string (tmp);
2473 g_settings_binding_key_changed (GSettings *settings,
2477 GSettingsBinding *binding = user_data;
2478 GValue value = G_VALUE_INIT;
2481 g_assert (settings == binding->settings);
2482 g_assert (key == binding->key.name);
2484 if (binding->running)
2487 binding->running = TRUE;
2489 g_value_init (&value, binding->property->value_type);
2491 variant = g_settings_read_from_backend (binding->settings, &binding->key, FALSE, FALSE);
2492 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2494 /* silently ignore errors in the user's config database */
2495 g_variant_unref (variant);
2499 if (variant == NULL)
2501 variant = g_settings_schema_key_get_translated_default (&binding->key);
2503 !binding->get_mapping (&value, variant, binding->user_data))
2505 /* flag translation errors with a warning */
2506 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
2507 "was rejected by the binding mapping function",
2508 binding->key.unparsed, binding->key.name,
2509 g_settings_schema_get_id (binding->key.schema));
2510 g_variant_unref (variant);
2515 if (variant == NULL)
2517 variant = g_variant_ref (binding->key.default_value);
2518 if (!binding->get_mapping (&value, variant, binding->user_data))
2519 g_error ("The schema default value for key '%s' in schema '%s' "
2520 "was rejected by the binding mapping function.",
2521 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2524 g_object_set_property (binding->object, binding->property->name, &value);
2525 g_variant_unref (variant);
2526 g_value_unset (&value);
2528 binding->running = FALSE;
2532 g_settings_binding_property_changed (GObject *object,
2533 const GParamSpec *pspec,
2536 GSettingsBinding *binding = user_data;
2537 GValue value = G_VALUE_INIT;
2540 g_assert (object == binding->object);
2541 g_assert (pspec == binding->property);
2543 if (binding->running)
2546 binding->running = TRUE;
2548 g_value_init (&value, pspec->value_type);
2549 g_object_get_property (object, pspec->name, &value);
2550 if ((variant = binding->set_mapping (&value, binding->key.type,
2551 binding->user_data)))
2553 g_variant_take_ref (variant);
2555 if (!g_settings_schema_key_type_check (&binding->key, variant))
2557 g_critical ("binding mapping function for key '%s' returned "
2558 "GVariant of type '%s' when type '%s' was requested",
2559 binding->key.name, g_variant_get_type_string (variant),
2560 g_variant_type_dup_string (binding->key.type));
2564 if (!g_settings_schema_key_range_check (&binding->key, variant))
2566 g_critical ("GObject property '%s' on a '%s' object is out of "
2567 "schema-specified range for key '%s' of '%s': %s",
2568 binding->property->name, g_type_name (binding->property->owner_type),
2569 binding->key.name, g_settings_schema_get_id (binding->key.schema),
2570 g_variant_print (variant, TRUE));
2574 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2575 g_variant_unref (variant);
2577 g_value_unset (&value);
2579 binding->running = FALSE;
2583 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2587 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2592 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2593 const GVariantType *expected_type,
2596 return g_variant_new_boolean (!g_value_get_boolean (value));
2601 * @settings: a #GSettings object
2602 * @key: the key to bind
2603 * @object: (type GObject.Object): a #GObject
2604 * @property: the name of the property to bind
2605 * @flags: flags for the binding
2607 * Create a binding between the @key in the @settings object
2608 * and the property @property of @object.
2610 * The binding uses the default GIO mapping functions to map
2611 * between the settings and property values. These functions
2612 * handle booleans, numeric types and string types in a
2613 * straightforward way. Use g_settings_bind_with_mapping() if
2614 * you need a custom mapping, or map between types that are not
2615 * supported by the default mapping functions.
2617 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2618 * function also establishes a binding between the writability of
2619 * @key and the "sensitive" property of @object (if @object has
2620 * a boolean property by that name). See g_settings_bind_writable()
2621 * for more details about writable bindings.
2623 * Note that the lifecycle of the binding is tied to the object,
2624 * and that you can have only one binding per object property.
2625 * If you bind the same property twice on the same object, the second
2626 * binding overrides the first one.
2631 g_settings_bind (GSettings *settings,
2634 const gchar *property,
2635 GSettingsBindFlags flags)
2637 GSettingsBindGetMapping get_mapping = NULL;
2638 GSettingsBindSetMapping set_mapping = NULL;
2640 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2642 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2643 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2645 /* can't pass this flag to g_settings_bind_with_mapping() */
2646 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2649 g_settings_bind_with_mapping (settings, key, object, property, flags,
2650 get_mapping, set_mapping, NULL, NULL);
2654 * g_settings_bind_with_mapping: (skip)
2655 * @settings: a #GSettings object
2656 * @key: the key to bind
2657 * @object: (type GObject.Object): a #GObject
2658 * @property: the name of the property to bind
2659 * @flags: flags for the binding
2660 * @get_mapping: a function that gets called to convert values
2661 * from @settings to @object, or %NULL to use the default GIO mapping
2662 * @set_mapping: a function that gets called to convert values
2663 * from @object to @settings, or %NULL to use the default GIO mapping
2664 * @user_data: data that gets passed to @get_mapping and @set_mapping
2665 * @destroy: #GDestroyNotify function for @user_data
2667 * Create a binding between the @key in the @settings object
2668 * and the property @property of @object.
2670 * The binding uses the provided mapping functions to map between
2671 * settings and property values.
2673 * Note that the lifecycle of the binding is tied to the object,
2674 * and that you can have only one binding per object property.
2675 * If you bind the same property twice on the same object, the second
2676 * binding overrides the first one.
2681 g_settings_bind_with_mapping (GSettings *settings,
2684 const gchar *property,
2685 GSettingsBindFlags flags,
2686 GSettingsBindGetMapping get_mapping,
2687 GSettingsBindSetMapping set_mapping,
2689 GDestroyNotify destroy)
2691 GSettingsBinding *binding;
2692 GObjectClass *objectclass;
2693 gchar *detailed_signal;
2694 GQuark binding_quark;
2696 g_return_if_fail (G_IS_SETTINGS (settings));
2697 g_return_if_fail (key != NULL);
2698 g_return_if_fail (G_IS_OBJECT (object));
2699 g_return_if_fail (property != NULL);
2700 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2702 objectclass = G_OBJECT_GET_CLASS (object);
2704 binding = g_slice_new0 (GSettingsBinding);
2705 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2706 binding->settings = g_object_ref (settings);
2707 binding->object = object;
2708 binding->property = g_object_class_find_property (objectclass, property);
2709 binding->user_data = user_data;
2710 binding->destroy = destroy;
2711 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2712 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2714 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2715 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2717 if (binding->property == NULL)
2719 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2720 property, G_OBJECT_TYPE_NAME (object));
2724 if ((flags & G_SETTINGS_BIND_GET) &&
2725 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2727 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2728 "writable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2731 if ((flags & G_SETTINGS_BIND_SET) &&
2732 (binding->property->flags & G_PARAM_READABLE) == 0)
2734 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2735 "readable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2739 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2741 /* g_settings_bind_invert_boolean_get_mapping() is a private
2742 * function, so if we are here it means that g_settings_bind() was
2743 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2745 * Ensure that both sides are boolean.
2748 if (binding->property->value_type != G_TYPE_BOOLEAN)
2750 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2751 "was specified, but property '%s' on type '%s' has "
2752 "type '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2753 g_type_name ((binding->property->value_type)));
2757 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2759 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2760 "was specified, but key '%s' on schema '%s' has "
2761 "type '%s'", key, g_settings_schema_get_id (settings->priv->schema),
2762 g_variant_type_dup_string (binding->key.type));
2768 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2769 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2770 !g_settings_mapping_is_compatible (binding->property->value_type,
2773 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2774 "'%s' which is not compatible with type '%s' of key '%s' "
2775 "on schema '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2776 g_type_name (binding->property->value_type),
2777 g_variant_type_dup_string (binding->key.type), key,
2778 g_settings_schema_get_id (settings->priv->schema));
2782 if ((flags & G_SETTINGS_BIND_SET) &&
2783 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2785 GParamSpec *sensitive;
2787 sensitive = g_object_class_find_property (objectclass, "sensitive");
2789 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2790 (sensitive->flags & G_PARAM_WRITABLE))
2791 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2794 if (flags & G_SETTINGS_BIND_SET)
2796 detailed_signal = g_strdup_printf ("notify::%s", binding->property->name);
2797 binding->property_handler_id =
2798 g_signal_connect (object, detailed_signal,
2799 G_CALLBACK (g_settings_binding_property_changed),
2801 g_free (detailed_signal);
2803 if (~flags & G_SETTINGS_BIND_GET)
2804 g_settings_binding_property_changed (object,
2809 if (flags & G_SETTINGS_BIND_GET)
2811 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2813 detailed_signal = g_strdup_printf ("changed::%s", key);
2814 binding->key_handler_id =
2815 g_signal_connect (settings, detailed_signal,
2816 G_CALLBACK (g_settings_binding_key_changed),
2818 g_free (detailed_signal);
2821 g_settings_binding_key_changed (settings, binding->key.name, binding);
2824 binding_quark = g_settings_binding_quark (binding->property->name);
2825 g_object_set_qdata_full (object, binding_quark,
2826 binding, g_settings_binding_free);
2829 /* Writability binding {{{1 */
2832 GSettings *settings;
2835 const gchar *property;
2838 } GSettingsWritableBinding;
2841 g_settings_writable_binding_free (gpointer data)
2843 GSettingsWritableBinding *binding = data;
2845 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2846 g_object_unref (binding->settings);
2847 g_slice_free (GSettingsWritableBinding, binding);
2851 g_settings_binding_writable_changed (GSettings *settings,
2855 GSettingsWritableBinding *binding = user_data;
2858 g_assert (settings == binding->settings);
2859 g_assert (key == binding->key);
2861 writable = g_settings_is_writable (settings, key);
2863 if (binding->inverted)
2864 writable = !writable;
2866 g_object_set (binding->object, binding->property, writable, NULL);
2870 * g_settings_bind_writable:
2871 * @settings: a #GSettings object
2872 * @key: the key to bind
2873 * @object: (type GObject.Object):a #GObject
2874 * @property: the name of a boolean property to bind
2875 * @inverted: whether to 'invert' the value
2877 * Create a binding between the writability of @key in the
2878 * @settings object and the property @property of @object.
2879 * The property must be boolean; "sensitive" or "visible"
2880 * properties of widgets are the most likely candidates.
2882 * Writable bindings are always uni-directional; changes of the
2883 * writability of the setting will be propagated to the object
2884 * property, not the other way.
2886 * When the @inverted argument is %TRUE, the binding inverts the
2887 * value as it passes from the setting to the object, i.e. @property
2888 * will be set to %TRUE if the key is not writable.
2890 * Note that the lifecycle of the binding is tied to the object,
2891 * and that you can have only one binding per object property.
2892 * If you bind the same property twice on the same object, the second
2893 * binding overrides the first one.
2898 g_settings_bind_writable (GSettings *settings,
2901 const gchar *property,
2904 GSettingsWritableBinding *binding;
2905 gchar *detailed_signal;
2908 g_return_if_fail (G_IS_SETTINGS (settings));
2910 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2913 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2914 property, G_OBJECT_TYPE_NAME (object));
2917 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2919 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2920 property, G_OBJECT_TYPE_NAME (object));
2924 binding = g_slice_new (GSettingsWritableBinding);
2925 binding->settings = g_object_ref (settings);
2926 binding->object = object;
2927 binding->key = g_intern_string (key);
2928 binding->property = g_intern_string (property);
2929 binding->inverted = inverted;
2931 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2932 binding->handler_id =
2933 g_signal_connect (settings, detailed_signal,
2934 G_CALLBACK (g_settings_binding_writable_changed),
2936 g_free (detailed_signal);
2938 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2939 binding, g_settings_writable_binding_free);
2941 g_settings_binding_writable_changed (settings, binding->key, binding);
2945 * g_settings_unbind:
2946 * @object: the object
2947 * @property: the property whose binding is removed
2949 * Removes an existing binding for @property on @object.
2951 * Note that bindings are automatically removed when the
2952 * object is finalized, so it is rarely necessary to call this
2958 g_settings_unbind (gpointer object,
2959 const gchar *property)
2961 GQuark binding_quark;
2963 binding_quark = g_settings_binding_quark (property);
2964 g_object_set_qdata (object, binding_quark, NULL);
2971 GObject parent_instance;
2973 GSettingsSchemaKey key;
2974 GSettings *settings;
2977 typedef GObjectClass GSettingsActionClass;
2979 static GType g_settings_action_get_type (void);
2980 static void g_settings_action_iface_init (GActionInterface *iface);
2981 G_DEFINE_TYPE_WITH_CODE (GSettingsAction, g_settings_action, G_TYPE_OBJECT,
2982 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_settings_action_iface_init))
2988 ACTION_PROP_PARAMETER_TYPE,
2989 ACTION_PROP_ENABLED,
2990 ACTION_PROP_STATE_TYPE,
2994 static const gchar *
2995 g_settings_action_get_name (GAction *action)
2997 GSettingsAction *gsa = (GSettingsAction *) action;
2999 return gsa->key.name;
3002 static const GVariantType *
3003 g_settings_action_get_parameter_type (GAction *action)
3005 GSettingsAction *gsa = (GSettingsAction *) action;
3006 const GVariantType *type;
3008 type = g_variant_get_type (gsa->key.default_value);
3009 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
3016 g_settings_action_get_enabled (GAction *action)
3018 GSettingsAction *gsa = (GSettingsAction *) action;
3020 return g_settings_is_writable (gsa->settings, gsa->key.name);
3023 static const GVariantType *
3024 g_settings_action_get_state_type (GAction *action)
3026 GSettingsAction *gsa = (GSettingsAction *) action;
3028 return g_variant_get_type (gsa->key.default_value);
3032 g_settings_action_get_state (GAction *action)
3034 GSettingsAction *gsa = (GSettingsAction *) action;
3037 value = g_settings_read_from_backend (gsa->settings, &gsa->key, FALSE, FALSE);
3040 value = g_settings_schema_key_get_translated_default (&gsa->key);
3043 value = g_variant_ref (gsa->key.default_value);
3049 g_settings_action_get_state_hint (GAction *action)
3051 GSettingsAction *gsa = (GSettingsAction *) action;
3053 /* no point in reimplementing this... */
3054 return g_settings_schema_key_get_range (&gsa->key);
3058 g_settings_action_change_state (GAction *action,
3061 GSettingsAction *gsa = (GSettingsAction *) action;
3063 if (g_settings_schema_key_type_check (&gsa->key, value) && g_settings_schema_key_range_check (&gsa->key, value))
3064 g_settings_write_to_backend (gsa->settings, &gsa->key, value);
3068 g_settings_action_activate (GAction *action,
3069 GVariant *parameter)
3071 GSettingsAction *gsa = (GSettingsAction *) action;
3073 if (g_variant_is_of_type (gsa->key.default_value, G_VARIANT_TYPE_BOOLEAN))
3077 if (parameter != NULL)
3080 old = g_settings_action_get_state (action);
3081 parameter = g_variant_new_boolean (!g_variant_get_boolean (old));
3082 g_variant_unref (old);
3085 g_action_change_state (action, parameter);
3089 g_settings_action_get_property (GObject *object, guint prop_id,
3090 GValue *value, GParamSpec *pspec)
3092 GAction *action = G_ACTION (object);
3096 case ACTION_PROP_NAME:
3097 g_value_set_string (value, g_settings_action_get_name (action));
3100 case ACTION_PROP_PARAMETER_TYPE:
3101 g_value_set_boxed (value, g_settings_action_get_parameter_type (action));
3104 case ACTION_PROP_ENABLED:
3105 g_value_set_boolean (value, g_settings_action_get_enabled (action));
3108 case ACTION_PROP_STATE_TYPE:
3109 g_value_set_boxed (value, g_settings_action_get_state_type (action));
3112 case ACTION_PROP_STATE:
3113 g_value_set_variant (value, g_settings_action_get_state (action));
3117 g_assert_not_reached ();
3122 g_settings_action_finalize (GObject *object)
3124 GSettingsAction *gsa = (GSettingsAction *) object;
3126 g_signal_handlers_disconnect_by_data (gsa->settings, gsa);
3127 g_object_unref (gsa->settings);
3129 G_OBJECT_CLASS (g_settings_action_parent_class)
3130 ->finalize (object);
3134 g_settings_action_init (GSettingsAction *gsa)
3139 g_settings_action_iface_init (GActionInterface *iface)
3141 iface->get_name = g_settings_action_get_name;
3142 iface->get_parameter_type = g_settings_action_get_parameter_type;
3143 iface->get_enabled = g_settings_action_get_enabled;
3144 iface->get_state_type = g_settings_action_get_state_type;
3145 iface->get_state = g_settings_action_get_state;
3146 iface->get_state_hint = g_settings_action_get_state_hint;
3147 iface->change_state = g_settings_action_change_state;
3148 iface->activate = g_settings_action_activate;
3152 g_settings_action_class_init (GSettingsActionClass *class)
3154 class->get_property = g_settings_action_get_property;
3155 class->finalize = g_settings_action_finalize;
3157 g_object_class_override_property (class, ACTION_PROP_NAME, "name");
3158 g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE, "parameter-type");
3159 g_object_class_override_property (class, ACTION_PROP_ENABLED, "enabled");
3160 g_object_class_override_property (class, ACTION_PROP_STATE_TYPE, "state-type");
3161 g_object_class_override_property (class, ACTION_PROP_STATE, "state");
3165 g_settings_action_changed (GSettings *settings,
3169 g_object_notify (user_data, "state");
3173 g_settings_action_enabled_changed (GSettings *settings,
3177 g_object_notify (user_data, "enabled");
3181 * g_settings_create_action:
3182 * @settings: a #GSettings
3183 * @key: the name of a key in @settings
3185 * Creates a #GAction corresponding to a given #GSettings key.
3187 * The action has the same name as the key.
3189 * The value of the key becomes the state of the action and the action
3190 * is enabled when the key is writable. Changing the state of the
3191 * action results in the key being written to. Changes to the value or
3192 * writability of the key cause appropriate change notifications to be
3193 * emitted for the action.
3195 * For boolean-valued keys, action activations take no parameter and
3196 * result in the toggling of the value. For all other types,
3197 * activations take the new value for the key (which must have the
3200 * Returns: (transfer full): a new #GAction
3205 g_settings_create_action (GSettings *settings,
3208 GSettingsAction *gsa;
3209 gchar *detailed_signal;
3211 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
3212 g_return_val_if_fail (key != NULL, NULL);
3214 gsa = g_object_new (g_settings_action_get_type (), NULL);
3215 gsa->settings = g_object_ref (settings);
3216 g_settings_schema_key_init (&gsa->key, settings->priv->schema, key);
3218 detailed_signal = g_strdup_printf ("changed::%s", key);
3219 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_changed), gsa);
3220 g_free (detailed_signal);
3221 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3222 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_enabled_changed), gsa);
3223 g_free (detailed_signal);
3225 return G_ACTION (gsa);
3230 /* vim:set foldmethod=marker: */