2 * Copyright © 2009, 2010 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Ryan Lortie <desrt@desrt.ca>
28 #include "gsettings.h"
30 #include "gdelayedsettingsbackend.h"
31 #include "gsettingsbackendinternal.h"
32 #include "gsettings-mapping.h"
33 #include "gsettingsschema-internal.h"
35 #include "gmarshal-internal.h"
41 * @short_description: High-level API for application settings
44 * The #GSettings class provides a convenient API for storing and retrieving
45 * application settings.
47 * Reads and writes can be considered to be non-blocking. Reading
48 * settings with #GSettings is typically extremely fast: on
49 * approximately the same order of magnitude (but slower than) a
50 * #GHashTable lookup. Writing settings is also extremely fast in terms
51 * of time to return to your application, but can be extremely expensive
52 * for other threads and other processes. Many settings backends
53 * (including dconf) have lazy initialisation which means in the common
54 * case of the user using their computer without modifying any settings
55 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
56 * even need to be started in this case. For this reason, you should
57 * only ever modify #GSettings keys in response to explicit user action.
58 * Particular care should be paid to ensure that modifications are not
59 * made during startup -- for example, when setting the initial value
60 * of preferences widgets. The built-in g_settings_bind() functionality
61 * is careful not to write settings in response to notify signals as a
62 * result of modifications that it makes to widgets.
64 * When creating a GSettings instance, you have to specify a schema
65 * that describes the keys in your settings and their types and default
66 * values, as well as some other information.
68 * Normally, a schema has a fixed path that determines where the settings
69 * are stored in the conceptual global tree of settings. However, schemas
70 * can also be '[relocatable][gsettings-relocatable]', i.e. not equipped with
71 * 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 * Paths must start with and end with a forward slash character ('/')
76 * and must not contain two sequential slash characters. Paths should
77 * be chosen based on a domain name associated with the program or
78 * library to which the settings belong. Examples of paths are
79 * "/org/gtk/settings/file-chooser/" and "/ca/desrt/dconf-editor/".
80 * Paths should not start with "/apps/", "/desktop/" or "/system/" as
81 * they often did in GConf.
83 * Unlike other configuration systems (like GConf), GSettings does not
84 * restrict keys to basic types like strings and numbers. GSettings stores
85 * values as #GVariant, and allows any #GVariantType for keys. Key names
86 * are restricted to lowercase characters, numbers and '-'. Furthermore,
87 * the names must begin with a lowercase character, must not end
88 * with a '-', and must not contain consecutive dashes.
90 * Similar to GConf, the default values in GSettings schemas can be
91 * localized, but the localized values are stored in gettext catalogs
92 * and looked up with the domain that is specified in the
93 * `gettext-domain` attribute of the <schemalist> or <schema>
94 * elements and the category that is specified in the `l10n` attribute of
95 * the <default> element. The string which is translated includes all text in
96 * the <default> element, including any surrounding quotation marks.
98 * The `l10n` attribute must be set to `messages` or `time`, and sets the
99 * [locale category for
100 * translation](https://www.gnu.org/software/gettext/manual/html_node/Aspects.html#index-locale-categories-1).
101 * The `messages` category should be used by default; use `time` for
102 * translatable date or time formats. A translation comment can be added as an
103 * XML comment immediately above the <default> element — it is recommended to
104 * add these comments to aid translators understand the meaning and
105 * implications of the default value. An optional translation `context`
106 * attribute can be set on the <default> element to disambiguate multiple
107 * defaults which use the same string.
111 * <!-- Translators: A list of words which are not allowed to be typed, in
112 * GVariant serialization syntax.
113 * See: https://developer.gnome.org/glib/stable/gvariant-text.html -->
114 * <default l10n='messages' context='Banned words'>['bad', 'words']</default>
117 * Translations of default values must remain syntactically valid serialized
118 * #GVariants (e.g. retaining any surrounding quotation marks) or runtime
121 * GSettings uses schemas in a compact binary form that is created
122 * by the [glib-compile-schemas][glib-compile-schemas]
123 * utility. The input is a schema description in an XML format.
125 * A DTD for the gschema XML format can be found here:
126 * [gschema.dtd](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/gschema.dtd)
128 * The [glib-compile-schemas][glib-compile-schemas] tool expects schema
129 * files to have the extension `.gschema.xml`.
131 * At runtime, schemas are identified by their id (as specified in the
132 * id attribute of the <schema> element). The convention for schema
133 * ids is to use a dotted name, similar in style to a D-Bus bus name,
134 * e.g. "org.gnome.SessionManager". In particular, if the settings are
135 * for a specific service that owns a D-Bus bus name, the D-Bus bus name
136 * and schema id should match. For schemas which deal with settings not
137 * associated with one named application, the id should not use
138 * StudlyCaps, e.g. "org.gnome.font-rendering".
140 * In addition to #GVariant types, keys can have types that have
141 * enumerated types. These can be described by a <choice>,
142 * <enum> or <flags> element, as seen in the
143 * [example][schema-enumerated]. The underlying type of such a key
144 * is string, but you can use g_settings_get_enum(), g_settings_set_enum(),
145 * g_settings_get_flags(), g_settings_set_flags() access the numeric values
146 * corresponding to the string value of enum and flags keys.
148 * An example for default value:
151 * <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
153 * <key name="greeting" type="s">
154 * <default l10n="messages">"Hello, earthlings"</default>
155 * <summary>A greeting</summary>
157 * Greeting of the invading martians
161 * <key name="box" type="(ii)">
162 * <default>(20,30)</default>
165 * <key name="empty-string" type="s">
166 * <default>""</default>
167 * <summary>Empty strings have to be provided in GVariant form</summary>
174 * An example for ranges, choices and enumerated types:
178 * <enum id="org.gtk.Test.myenum">
179 * <value nick="first" value="1"/>
180 * <value nick="second" value="2"/>
183 * <flags id="org.gtk.Test.myflags">
184 * <value nick="flag1" value="1"/>
185 * <value nick="flag2" value="2"/>
186 * <value nick="flag3" value="4"/>
189 * <schema id="org.gtk.Test">
191 * <key name="key-with-range" type="i">
192 * <range min="1" max="100"/>
193 * <default>10</default>
196 * <key name="key-with-choices" type="s">
198 * <choice value='Elisabeth'/>
199 * <choice value='Annabeth'/>
200 * <choice value='Joe'/>
203 * <alias value='Anna' target='Annabeth'/>
204 * <alias value='Beth' target='Elisabeth'/>
206 * <default>'Joe'</default>
209 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
210 * <default>'first'</default>
213 * <key name='flags-key' flags='org.gtk.Test.myflags'>
214 * <default>["flag1","flag2"]</default>
220 * ## Vendor overrides
222 * Default values are defined in the schemas that get installed by
223 * an application. Sometimes, it is necessary for a vendor or distributor
224 * to adjust these defaults. Since patching the XML source for the schema
225 * is inconvenient and error-prone,
226 * [glib-compile-schemas][glib-compile-schemas] reads so-called vendor
227 * override' files. These are keyfiles in the same directory as the XML
228 * schema sources which can override default values. The schema id serves
229 * as the group name in the key file, and the values are expected in
230 * serialized GVariant form, as in the following example:
237 * glib-compile-schemas expects schema files to have the extension
238 * `.gschema.override`.
242 * A very convenient feature of GSettings lets you bind #GObject properties
243 * directly to settings, using g_settings_bind(). Once a GObject property
244 * has been bound to a setting, changes on either side are automatically
245 * propagated to the other side. GSettings handles details like mapping
246 * between GObject and GVariant types, and preventing infinite cycles.
248 * This makes it very easy to hook up a preferences dialog to the
249 * underlying settings. To make this even more convenient, GSettings
250 * looks for a boolean property with the name "sensitivity" and
251 * automatically binds it to the writability of the bound setting.
252 * If this 'magic' gets in the way, it can be suppressed with the
253 * %G_SETTINGS_BIND_NO_SENSITIVITY flag.
255 * ## Relocatable schemas # {#gsettings-relocatable}
257 * A relocatable schema is one with no `path` attribute specified on its
258 * <schema> element. By using g_settings_new_with_path(), a #GSettings object
259 * can be instantiated for a relocatable schema, assigning a path to the
260 * instance. Paths passed to g_settings_new_with_path() will typically be
261 * constructed dynamically from a constant prefix plus some form of instance
262 * identifier; but they must still be valid GSettings paths. Paths could also
263 * be constant and used with a globally installed schema originating from a
264 * dependency library.
266 * For example, a relocatable schema could be used to store geometry information
267 * for different windows in an application. If the schema ID was
268 * `org.foo.MyApp.Window`, it could be instantiated for paths
269 * `/org/foo/MyApp/main/`, `/org/foo/MyApp/document-1/`,
270 * `/org/foo/MyApp/document-2/`, etc. If any of the paths are well-known
271 * they can be specified as <child> elements in the parent schema, e.g.:
273 * <schema id="org.foo.MyApp" path="/org/foo/MyApp/">
274 * <child name="main" schema="org.foo.MyApp.Window"/>
278 * ## Build system integration # {#gsettings-build-system}
280 * GSettings comes with autotools integration to simplify compiling and
281 * installing schemas. To add GSettings support to an application, add the
282 * following to your `configure.ac`:
287 * In the appropriate `Makefile.am`, use the following snippet to compile and
288 * install the named schema:
290 * gsettings_SCHEMAS = org.foo.MyApp.gschema.xml
291 * EXTRA_DIST = $(gsettings_SCHEMAS)
296 * No changes are needed to the build system to mark a schema XML file for
297 * translation. Assuming it sets the `gettext-domain` attribute, a schema may
298 * be marked for translation by adding it to `POTFILES.in`, assuming gettext
299 * 0.19 is in use (the preferred method for translation):
301 * data/org.foo.MyApp.gschema.xml
304 * Alternatively, if intltool 0.50.1 is in use:
306 * [type: gettext/gsettings]data/org.foo.MyApp.gschema.xml
309 * GSettings will use gettext to look up translations for the <summary> and
310 * <description> elements, and also any <default> elements which have a `l10n`
311 * attribute set. Translations must not be included in the `.gschema.xml` file
312 * by the build system, for example by using intltool XML rules with a
313 * `.gschema.xml.in` template.
315 * If an enumerated type defined in a C header file is to be used in a GSettings
316 * schema, it can either be defined manually using an <enum> element in the
317 * schema XML, or it can be extracted automatically from the C header. This
318 * approach is preferred, as it ensures the two representations are always
319 * synchronised. To do so, add the following to the relevant `Makefile.am`:
321 * gsettings_ENUM_NAMESPACE = org.foo.MyApp
322 * gsettings_ENUM_FILES = my-app-enums.h my-app-misc.h
325 * `gsettings_ENUM_NAMESPACE` specifies the schema namespace for the enum files,
326 * which are specified in `gsettings_ENUM_FILES`. This will generate a
327 * `org.foo.MyApp.enums.xml` file containing the extracted enums, which will be
328 * automatically included in the schema compilation, install and uninstall
329 * rules. It should not be committed to version control or included in
336 * #GSettings is an opaque data structure and can only be accessed
337 * using the following functions.
340 struct _GSettingsPrivate
342 /* where the signals go... */
343 GMainContext *main_context;
345 GSettingsBackend *backend;
346 GSettingsSchema *schema;
363 SIGNAL_WRITABLE_CHANGE_EVENT,
364 SIGNAL_WRITABLE_CHANGED,
370 static guint g_settings_signals[N_SIGNALS];
372 G_DEFINE_TYPE_WITH_PRIVATE (GSettings, g_settings, G_TYPE_OBJECT)
376 g_settings_real_change_event (GSettings *settings,
383 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
385 for (i = 0; i < n_keys; i++)
387 const gchar *key = g_quark_to_string (keys[i]);
389 if (g_str_has_suffix (key, "/"))
392 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED], keys[i], key);
399 g_settings_real_writable_change_event (GSettings *settings,
402 const GQuark *keys = &key;
407 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
409 for (i = 0; i < n_keys; i++)
411 const gchar *key_name = g_quark_to_string (keys[i]);
413 if (g_str_has_suffix (key_name, "/"))
416 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keys[i], key_name);
423 settings_backend_changed (GObject *target,
424 GSettingsBackend *backend,
428 GSettings *settings = G_SETTINGS (target);
429 gboolean ignore_this;
432 /* We used to assert here:
434 * settings->priv->backend == backend
436 * but it could be the case that a notification is queued for delivery
437 * while someone calls g_settings_delay() (which changes the backend).
439 * Since the delay backend would just pass that straight through
440 * anyway, it doesn't make sense to try to detect this case.
441 * Therefore, we just accept it.
444 for (i = 0; key[i] == settings->priv->path[i]; i++);
446 if (settings->priv->path[i] == '\0' &&
447 g_settings_schema_has_key (settings->priv->schema, key + i))
451 quark = g_quark_from_string (key + i);
452 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
453 0, &quark, 1, &ignore_this);
458 settings_backend_path_changed (GObject *target,
459 GSettingsBackend *backend,
463 GSettings *settings = G_SETTINGS (target);
464 gboolean ignore_this;
466 if (g_str_has_prefix (settings->priv->path, path))
467 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
468 0, NULL, 0, &ignore_this);
472 settings_backend_keys_changed (GObject *target,
473 GSettingsBackend *backend,
476 const gchar * const *items)
478 GSettings *settings = G_SETTINGS (target);
479 gboolean ignore_this;
482 for (i = 0; settings->priv->path[i] &&
483 settings->priv->path[i] == path[i]; i++);
490 for (j = 0; items[j]; j++)
492 const gchar *item = items[j];
495 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
497 if (settings->priv->path[i + k] == '\0' &&
498 g_settings_schema_has_key (settings->priv->schema, item + k))
499 quarks[l++] = g_quark_from_string (item + k);
501 /* "256 quarks ought to be enough for anybody!"
502 * If this bites you, I'm sorry. Please file a bug.
508 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
509 0, quarks, l, &ignore_this);
514 settings_backend_writable_changed (GObject *target,
515 GSettingsBackend *backend,
518 GSettings *settings = G_SETTINGS (target);
519 gboolean ignore_this;
522 for (i = 0; key[i] == settings->priv->path[i]; i++);
524 if (settings->priv->path[i] == '\0' &&
525 g_settings_schema_has_key (settings->priv->schema, key + i))
526 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
527 0, g_quark_from_string (key + i), &ignore_this);
531 settings_backend_path_writable_changed (GObject *target,
532 GSettingsBackend *backend,
535 GSettings *settings = G_SETTINGS (target);
536 gboolean ignore_this;
538 if (g_str_has_prefix (settings->priv->path, path))
539 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
540 0, (GQuark) 0, &ignore_this);
543 /* Properties, Construction, Destruction {{{1 */
545 g_settings_set_property (GObject *object,
550 GSettings *settings = G_SETTINGS (object);
556 GSettingsSchema *schema;
558 schema = g_value_dup_boxed (value);
560 /* we receive a set_property() call for "settings-schema" even
561 * if it was not specified (ie: with NULL value). ->schema
562 * could already be set at this point (ie: via "schema-id").
563 * check for NULL to avoid clobbering the existing value.
567 g_assert (settings->priv->schema == NULL);
568 settings->priv->schema = schema;
575 const gchar *schema_id;
577 schema_id = g_value_get_string (value);
579 /* we receive a set_property() call for both "schema" and
580 * "schema-id", even if they are not set. Hopefully only one of
583 if (schema_id != NULL)
585 GSettingsSchemaSource *default_source;
587 g_assert (settings->priv->schema == NULL);
588 default_source = g_settings_schema_source_get_default ();
590 if (default_source == NULL)
591 g_error ("No GSettings schemas are installed on the system");
593 settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
595 if (settings->priv->schema == NULL)
596 g_error ("Settings schema '%s' is not installed", schema_id);
602 settings->priv->path = g_value_dup_string (value);
606 settings->priv->backend = g_value_dup_object (value);
610 g_assert_not_reached ();
615 g_settings_get_property (GObject *object,
620 GSettings *settings = G_SETTINGS (object);
625 g_value_set_boxed (value, settings->priv->schema);
629 g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
633 g_value_set_object (value, settings->priv->backend);
637 g_value_set_string (value, settings->priv->path);
640 case PROP_HAS_UNAPPLIED:
641 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
644 case PROP_DELAY_APPLY:
645 g_value_set_boolean (value, G_IS_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
649 g_assert_not_reached ();
653 static const GSettingsListenerVTable listener_vtable = {
654 settings_backend_changed,
655 settings_backend_path_changed,
656 settings_backend_keys_changed,
657 settings_backend_writable_changed,
658 settings_backend_path_writable_changed
662 g_settings_constructed (GObject *object)
664 GSettings *settings = G_SETTINGS (object);
665 const gchar *schema_path;
667 schema_path = g_settings_schema_get_path (settings->priv->schema);
669 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
670 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
671 g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
673 if (settings->priv->path == NULL)
675 if (schema_path == NULL)
676 g_error ("attempting to create schema '%s' without a path",
677 g_settings_schema_get_id (settings->priv->schema));
679 settings->priv->path = g_strdup (schema_path);
682 if (settings->priv->backend == NULL)
683 settings->priv->backend = g_settings_backend_get_default ();
685 g_settings_backend_watch (settings->priv->backend,
686 &listener_vtable, G_OBJECT (settings),
687 settings->priv->main_context);
688 g_settings_backend_subscribe (settings->priv->backend,
689 settings->priv->path);
693 g_settings_finalize (GObject *object)
695 GSettings *settings = G_SETTINGS (object);
697 g_settings_backend_unsubscribe (settings->priv->backend,
698 settings->priv->path);
699 g_main_context_unref (settings->priv->main_context);
700 g_object_unref (settings->priv->backend);
701 g_settings_schema_unref (settings->priv->schema);
702 g_free (settings->priv->path);
704 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
708 g_settings_init (GSettings *settings)
710 settings->priv = g_settings_get_instance_private (settings);
711 settings->priv->main_context = g_main_context_ref_thread_default ();
715 g_settings_class_init (GSettingsClass *class)
717 GObjectClass *object_class = G_OBJECT_CLASS (class);
719 class->writable_change_event = g_settings_real_writable_change_event;
720 class->change_event = g_settings_real_change_event;
722 object_class->set_property = g_settings_set_property;
723 object_class->get_property = g_settings_get_property;
724 object_class->constructed = g_settings_constructed;
725 object_class->finalize = g_settings_finalize;
728 * GSettings::changed:
729 * @settings: the object on which the signal was emitted
730 * @key: the name of the key that changed
732 * The "changed" signal is emitted when a key has potentially changed.
733 * You should call one of the g_settings_get() calls to check the new
736 * This signal supports detailed connections. You can connect to the
737 * detailed signal "changed::x" in order to only receive callbacks
738 * when key "x" changes.
740 * Note that @settings only emits this signal if you have read @key at
741 * least once while a signal handler was already connected for @key.
743 g_settings_signals[SIGNAL_CHANGED] =
744 g_signal_new (I_("changed"), G_TYPE_SETTINGS,
745 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
746 G_STRUCT_OFFSET (GSettingsClass, changed),
747 NULL, NULL, NULL, G_TYPE_NONE,
748 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
751 * GSettings::change-event:
752 * @settings: the object on which the signal was emitted
753 * @keys: (array length=n_keys) (element-type GQuark) (nullable):
754 * an array of #GQuarks for the changed keys, or %NULL
755 * @n_keys: the length of the @keys array, or 0
757 * The "change-event" signal is emitted once per change event that
758 * affects this settings object. You should connect to this signal
759 * only if you are interested in viewing groups of changes before they
760 * are split out into multiple emissions of the "changed" signal.
761 * For most use cases it is more appropriate to use the "changed" signal.
763 * In the event that the change event applies to one or more specified
764 * keys, @keys will be an array of #GQuark of length @n_keys. In the
765 * event that the change event applies to the #GSettings object as a
766 * whole (ie: potentially every key has been changed) then @keys will
767 * be %NULL and @n_keys will be 0.
769 * The default handler for this signal invokes the "changed" signal
770 * for each affected key. If any other connected handler returns
771 * %TRUE then this default functionality will be suppressed.
773 * Returns: %TRUE to stop other handlers from being invoked for the
774 * event. FALSE to propagate the event further.
776 g_settings_signals[SIGNAL_CHANGE_EVENT] =
777 g_signal_new (I_("change-event"), G_TYPE_SETTINGS,
779 G_STRUCT_OFFSET (GSettingsClass, change_event),
780 g_signal_accumulator_true_handled, NULL,
781 _g_cclosure_marshal_BOOLEAN__POINTER_INT,
782 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
783 g_signal_set_va_marshaller (g_settings_signals[SIGNAL_CHANGE_EVENT],
784 G_TYPE_FROM_CLASS (class),
785 _g_cclosure_marshal_BOOLEAN__POINTER_INTv);
788 * GSettings::writable-changed:
789 * @settings: the object on which the signal was emitted
792 * The "writable-changed" signal is emitted when the writability of a
793 * key has potentially changed. You should call
794 * g_settings_is_writable() in order to determine the new status.
796 * This signal supports detailed connections. You can connect to the
797 * detailed signal "writable-changed::x" in order to only receive
798 * callbacks when the writability of "x" changes.
800 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
801 g_signal_new (I_("writable-changed"), G_TYPE_SETTINGS,
802 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
803 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
804 NULL, NULL, NULL, G_TYPE_NONE,
805 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
808 * GSettings::writable-change-event:
809 * @settings: the object on which the signal was emitted
810 * @key: the quark of the key, or 0
812 * The "writable-change-event" signal is emitted once per writability
813 * change event that affects this settings object. You should connect
814 * to this signal if you are interested in viewing groups of changes
815 * before they are split out into multiple emissions of the
816 * "writable-changed" signal. For most use cases it is more
817 * appropriate to use the "writable-changed" signal.
819 * In the event that the writability change applies only to a single
820 * key, @key will be set to the #GQuark for that key. In the event
821 * that the writability change affects the entire settings object,
824 * The default handler for this signal invokes the "writable-changed"
825 * and "changed" signals for each affected key. This is done because
826 * changes in writability might also imply changes in value (if for
827 * example, a new mandatory setting is introduced). If any other
828 * connected handler returns %TRUE then this default functionality
829 * will be suppressed.
831 * Returns: %TRUE to stop other handlers from being invoked for the
832 * event. FALSE to propagate the event further.
834 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
835 g_signal_new (I_("writable-change-event"), G_TYPE_SETTINGS,
837 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
838 g_signal_accumulator_true_handled, NULL,
839 _g_cclosure_marshal_BOOLEAN__UINT,
840 G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
841 g_signal_set_va_marshaller (g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
842 G_TYPE_FROM_CLASS (class),
843 _g_cclosure_marshal_BOOLEAN__UINTv);
848 * The name of the context that the settings are stored in.
850 g_object_class_install_property (object_class, PROP_BACKEND,
851 g_param_spec_object ("backend",
852 P_("GSettingsBackend"),
853 P_("The GSettingsBackend for this settings object"),
854 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
855 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
858 * GSettings:settings-schema:
860 * The #GSettingsSchema describing the types of keys for this
863 * Ideally, this property would be called 'schema'. #GSettingsSchema
864 * has only existed since version 2.32, however, and before then the
865 * 'schema' property was used to refer to the ID of the schema rather
866 * than the schema itself. Take care.
868 g_object_class_install_property (object_class, PROP_SCHEMA,
869 g_param_spec_boxed ("settings-schema",
871 P_("The GSettingsSchema for this settings object"),
872 G_TYPE_SETTINGS_SCHEMA,
873 G_PARAM_CONSTRUCT_ONLY |
874 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
879 * The name of the schema that describes the types of keys
880 * for this #GSettings object.
882 * The type of this property is *not* #GSettingsSchema.
883 * #GSettingsSchema has only existed since version 2.32 and
884 * unfortunately this name was used in previous versions to refer to
885 * the schema ID rather than the schema itself. Take care to use the
886 * 'settings-schema' property if you wish to pass in a
889 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
890 * version, this property may instead refer to a #GSettingsSchema.
892 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
893 g_param_spec_string ("schema",
895 P_("The name of the schema for this settings object"),
897 G_PARAM_CONSTRUCT_ONLY |
898 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
901 * GSettings:schema-id:
903 * The name of the schema that describes the types of keys
904 * for this #GSettings object.
906 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
907 g_param_spec_string ("schema-id",
909 P_("The name of the schema for this settings object"),
911 G_PARAM_CONSTRUCT_ONLY |
912 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
917 * The path within the backend where the settings are stored.
919 g_object_class_install_property (object_class, PROP_PATH,
920 g_param_spec_string ("path",
922 P_("The path within the backend where the settings are"),
924 G_PARAM_CONSTRUCT_ONLY |
925 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
928 * GSettings:has-unapplied:
930 * If this property is %TRUE, the #GSettings object has outstanding
931 * changes that will be applied when g_settings_apply() is called.
933 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
934 g_param_spec_boolean ("has-unapplied",
935 P_("Has unapplied changes"),
936 P_("TRUE if there are outstanding changes to apply()"),
938 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
941 * GSettings:delay-apply:
943 * Whether the #GSettings object is in 'delay-apply' mode. See
944 * g_settings_delay() for details.
948 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
949 g_param_spec_boolean ("delay-apply",
950 P_("Delay-apply mode"),
951 P_("Whether this settings object is in “delay-apply” mode"),
953 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
956 /* Construction (new, new_with_path, etc.) {{{1 */
959 * @schema_id: the id of the schema
961 * Creates a new #GSettings object with the schema specified by
964 * It is an error for the schema to not exist: schemas are an
965 * essential part of a program, as they provide type information.
966 * If schemas need to be dynamically loaded (for example, from an
967 * optional runtime dependency), g_settings_schema_source_lookup()
968 * can be used to test for their existence before loading them.
970 * Signals on the newly created #GSettings object will be dispatched
971 * via the thread-default #GMainContext in effect at the time of the
972 * call to g_settings_new(). The new #GSettings will hold a reference
973 * on the context. See g_main_context_push_thread_default().
975 * Returns: (not nullable) (transfer full): a new #GSettings object
980 g_settings_new (const gchar *schema_id)
982 g_return_val_if_fail (schema_id != NULL, NULL);
984 return g_object_new (G_TYPE_SETTINGS,
985 "schema-id", schema_id,
990 path_is_valid (const gchar *path)
998 if (!g_str_has_suffix (path, "/"))
1001 return strstr (path, "//") == NULL;
1005 * g_settings_new_with_path:
1006 * @schema_id: the id of the schema
1007 * @path: the path to use
1009 * Creates a new #GSettings object with the relocatable schema specified
1010 * by @schema_id and a given path.
1012 * You only need to do this if you want to directly create a settings
1013 * object with a schema that doesn't have a specified path of its own.
1014 * That's quite rare.
1016 * It is a programmer error to call this function for a schema that
1017 * has an explicitly specified path.
1019 * It is a programmer error if @path is not a valid path. A valid path
1020 * begins and ends with '/' and does not contain two consecutive '/'
1023 * Returns: (not nullable) (transfer full): a new #GSettings object
1028 g_settings_new_with_path (const gchar *schema_id,
1031 g_return_val_if_fail (schema_id != NULL, NULL);
1032 g_return_val_if_fail (path_is_valid (path), NULL);
1034 return g_object_new (G_TYPE_SETTINGS,
1035 "schema-id", schema_id,
1041 * g_settings_new_with_backend:
1042 * @schema_id: the id of the schema
1043 * @backend: the #GSettingsBackend to use
1045 * Creates a new #GSettings object with the schema specified by
1046 * @schema_id and a given #GSettingsBackend.
1048 * Creating a #GSettings object with a different backend allows accessing
1049 * settings from a database other than the usual one. For example, it may make
1050 * sense to pass a backend corresponding to the "defaults" settings database on
1051 * the system to get a settings object that modifies the system default
1052 * settings instead of the settings for this user.
1054 * Returns: (not nullable) (transfer full): a new #GSettings object
1059 g_settings_new_with_backend (const gchar *schema_id,
1060 GSettingsBackend *backend)
1062 g_return_val_if_fail (schema_id != NULL, NULL);
1063 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
1065 return g_object_new (G_TYPE_SETTINGS,
1066 "schema-id", schema_id,
1072 * g_settings_new_with_backend_and_path:
1073 * @schema_id: the id of the schema
1074 * @backend: the #GSettingsBackend to use
1075 * @path: the path to use
1077 * Creates a new #GSettings object with the schema specified by
1078 * @schema_id and a given #GSettingsBackend and path.
1080 * This is a mix of g_settings_new_with_backend() and
1081 * g_settings_new_with_path().
1083 * Returns: (not nullable) (transfer full): a new #GSettings object
1088 g_settings_new_with_backend_and_path (const gchar *schema_id,
1089 GSettingsBackend *backend,
1092 g_return_val_if_fail (schema_id != NULL, NULL);
1093 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
1094 g_return_val_if_fail (path_is_valid (path), NULL);
1096 return g_object_new (G_TYPE_SETTINGS,
1097 "schema-id", schema_id,
1104 * g_settings_new_full:
1105 * @schema: a #GSettingsSchema
1106 * @backend: (nullable): a #GSettingsBackend
1107 * @path: (nullable): the path to use
1109 * Creates a new #GSettings object with a given schema, backend and
1112 * It should be extremely rare that you ever want to use this function.
1113 * It is made available for advanced use-cases (such as plugin systems
1114 * that want to provide access to schemas loaded from custom locations,
1117 * At the most basic level, a #GSettings object is a pure composition of
1118 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
1119 * backend, and a #GMainContext to which signals are dispatched.
1121 * This constructor therefore gives you full control over constructing
1122 * #GSettings instances. The first 3 parameters are given directly as
1123 * @schema, @backend and @path, and the main context is taken from the
1124 * thread-default (as per g_settings_new()).
1126 * If @backend is %NULL then the default backend is used.
1128 * If @path is %NULL then the path from the schema is used. It is an
1129 * error if @path is %NULL and the schema has no path of its own or if
1130 * @path is non-%NULL and not equal to the path that the schema does
1133 * Returns: (not nullable) (transfer full): a new #GSettings object
1138 g_settings_new_full (GSettingsSchema *schema,
1139 GSettingsBackend *backend,
1142 g_return_val_if_fail (schema != NULL, NULL);
1143 g_return_val_if_fail (backend == NULL || G_IS_SETTINGS_BACKEND (backend), NULL);
1144 g_return_val_if_fail (path == NULL || path_is_valid (path), NULL);
1146 return g_object_new (G_TYPE_SETTINGS,
1147 "settings-schema", schema,
1153 /* Internal read/write utilities {{{1 */
1155 /* @value will be sunk */
1157 g_settings_write_to_backend (GSettings *settings,
1158 GSettingsSchemaKey *key,
1164 path = g_strconcat (settings->priv->path, key->name, NULL);
1165 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1172 g_settings_read_from_backend (GSettings *settings,
1173 GSettingsSchemaKey *key,
1174 gboolean user_value_only,
1175 gboolean default_value)
1181 path = g_strconcat (settings->priv->path, key->name, NULL);
1182 if (user_value_only)
1183 value = g_settings_backend_read_user_value (settings->priv->backend, path, key->type);
1185 value = g_settings_backend_read (settings->priv->backend, path, key->type, default_value);
1190 fixup = g_settings_schema_key_range_fixup (key, value);
1191 g_variant_unref (value);
1199 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1201 * g_settings_get_value:
1202 * @settings: a #GSettings object
1203 * @key: the key to get the value for
1205 * Gets the value that is stored in @settings for @key.
1207 * It is a programmer error to give a @key that isn't contained in the
1208 * schema for @settings.
1210 * Returns: (not nullable) (transfer full): a new #GVariant
1215 g_settings_get_value (GSettings *settings,
1218 GSettingsSchemaKey skey;
1221 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1222 g_return_val_if_fail (key != NULL, NULL);
1224 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1225 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1228 value = g_settings_schema_key_get_default_value (&skey);
1230 g_settings_schema_key_clear (&skey);
1236 * g_settings_get_user_value:
1237 * @settings: a #GSettings object
1238 * @key: the key to get the user value for
1240 * Checks the "user value" of a key, if there is one.
1242 * The user value of a key is the last value that was set by the user.
1244 * After calling g_settings_reset() this function should always return
1245 * %NULL (assuming something is not wrong with the system
1248 * It is possible that g_settings_get_value() will return a different
1249 * value than this function. This can happen in the case that the user
1250 * set a value for a key that was subsequently locked down by the system
1251 * administrator -- this function will return the user's old value.
1253 * This function may be useful for adding a "reset" option to a UI or
1254 * for providing indication that a particular value has been changed.
1256 * It is a programmer error to give a @key that isn't contained in the
1257 * schema for @settings.
1259 * Returns: (nullable) (transfer full): the user's value, if set
1264 g_settings_get_user_value (GSettings *settings,
1267 GSettingsSchemaKey skey;
1270 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1271 g_return_val_if_fail (key != NULL, NULL);
1273 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1274 value = g_settings_read_from_backend (settings, &skey, TRUE, FALSE);
1275 g_settings_schema_key_clear (&skey);
1281 * g_settings_get_default_value:
1282 * @settings: a #GSettings object
1283 * @key: the key to get the default value for
1285 * Gets the "default value" of a key.
1287 * This is the value that would be read if g_settings_reset() were to be
1288 * called on the key.
1290 * Note that this may be a different value than returned by
1291 * g_settings_schema_key_get_default_value() if the system administrator
1292 * has provided a default value.
1294 * Comparing the return values of g_settings_get_default_value() and
1295 * g_settings_get_value() is not sufficient for determining if a value
1296 * has been set because the user may have explicitly set the value to
1297 * something that happens to be equal to the default. The difference
1298 * here is that if the default changes in the future, the user's key
1299 * will still be set.
1301 * This function may be useful for adding an indication to a UI of what
1302 * the default value was before the user set it.
1304 * It is a programmer error to give a @key that isn't contained in the
1305 * schema for @settings.
1307 * Returns: (nullable) (transfer full): the default value
1312 g_settings_get_default_value (GSettings *settings,
1315 GSettingsSchemaKey skey;
1318 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1319 g_return_val_if_fail (key != NULL, NULL);
1321 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1322 value = g_settings_read_from_backend (settings, &skey, FALSE, TRUE);
1325 value = g_settings_schema_key_get_default_value (&skey);
1327 g_settings_schema_key_clear (&skey);
1333 * g_settings_get_enum:
1334 * @settings: a #GSettings object
1335 * @key: the key to get the value for
1337 * Gets the value that is stored in @settings for @key and converts it
1338 * to the enum value that it represents.
1340 * In order to use this function the type of the value must be a string
1341 * and it must be marked in the schema file as an enumerated type.
1343 * It is a programmer error to give a @key that isn't contained in the
1344 * schema for @settings or is not marked as an enumerated type.
1346 * If the value stored in the configuration database is not a valid
1347 * value for the enumerated type then this function will return the
1350 * Returns: the enum value
1355 g_settings_get_enum (GSettings *settings,
1358 GSettingsSchemaKey skey;
1362 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1363 g_return_val_if_fail (key != NULL, -1);
1365 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1369 g_critical ("g_settings_get_enum() called on key '%s' which is not "
1370 "associated with an enumerated type", skey.name);
1371 g_settings_schema_key_clear (&skey);
1375 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1378 value = g_settings_schema_key_get_default_value (&skey);
1380 result = g_settings_schema_key_to_enum (&skey, value);
1381 g_settings_schema_key_clear (&skey);
1382 g_variant_unref (value);
1388 * g_settings_set_enum:
1389 * @settings: a #GSettings object
1390 * @key: a key, within @settings
1391 * @value: an enumerated value
1393 * Looks up the enumerated type nick for @value and writes it to @key,
1396 * It is a programmer error to give a @key that isn't contained in the
1397 * schema for @settings or is not marked as an enumerated type, or for
1398 * @value not to be a valid value for the named type.
1400 * After performing the write, accessing @key directly with
1401 * g_settings_get_string() will return the 'nick' associated with
1404 * Returns: %TRUE, if the set succeeds
1407 g_settings_set_enum (GSettings *settings,
1411 GSettingsSchemaKey skey;
1415 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1416 g_return_val_if_fail (key != NULL, FALSE);
1418 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1422 g_critical ("g_settings_set_enum() called on key '%s' which is not "
1423 "associated with an enumerated type", skey.name);
1427 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1429 g_critical ("g_settings_set_enum(): invalid enum value %d for key '%s' "
1430 "in schema '%s'. Doing nothing.", value, skey.name,
1431 g_settings_schema_get_id (skey.schema));
1432 g_settings_schema_key_clear (&skey);
1436 success = g_settings_write_to_backend (settings, &skey, g_steal_pointer (&variant));
1437 g_settings_schema_key_clear (&skey);
1443 * g_settings_get_flags:
1444 * @settings: a #GSettings object
1445 * @key: the key to get the value for
1447 * Gets the value that is stored in @settings for @key and converts it
1448 * to the flags value that it represents.
1450 * In order to use this function the type of the value must be an array
1451 * of strings and it must be marked in the schema file as a flags type.
1453 * It is a programmer error to give a @key that isn't contained in the
1454 * schema for @settings or is not marked as a flags type.
1456 * If the value stored in the configuration database is not a valid
1457 * value for the flags type then this function will return the default
1460 * Returns: the flags value
1465 g_settings_get_flags (GSettings *settings,
1468 GSettingsSchemaKey skey;
1472 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1473 g_return_val_if_fail (key != NULL, -1);
1475 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1479 g_critical ("g_settings_get_flags() called on key '%s' which is not "
1480 "associated with a flags type", skey.name);
1481 g_settings_schema_key_clear (&skey);
1485 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1488 value = g_settings_schema_key_get_default_value (&skey);
1490 result = g_settings_schema_key_to_flags (&skey, value);
1491 g_settings_schema_key_clear (&skey);
1492 g_variant_unref (value);
1498 * g_settings_set_flags:
1499 * @settings: a #GSettings object
1500 * @key: a key, within @settings
1501 * @value: a flags value
1503 * Looks up the flags type nicks for the bits specified by @value, puts
1504 * them in an array of strings and writes the array to @key, within
1507 * It is a programmer error to give a @key that isn't contained in the
1508 * schema for @settings or is not marked as a flags type, or for @value
1509 * to contain any bits that are not value for the named type.
1511 * After performing the write, accessing @key directly with
1512 * g_settings_get_strv() will return an array of 'nicks'; one for each
1515 * Returns: %TRUE, if the set succeeds
1518 g_settings_set_flags (GSettings *settings,
1522 GSettingsSchemaKey skey;
1526 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1527 g_return_val_if_fail (key != NULL, FALSE);
1529 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1533 g_critical ("g_settings_set_flags() called on key '%s' which is not "
1534 "associated with a flags type", skey.name);
1538 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1540 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1541 "for key '%s' in schema '%s'. Doing nothing.",
1542 value, skey.name, g_settings_schema_get_id (skey.schema));
1543 g_settings_schema_key_clear (&skey);
1547 success = g_settings_write_to_backend (settings, &skey, g_steal_pointer (&variant));
1548 g_settings_schema_key_clear (&skey);
1554 * g_settings_set_value:
1555 * @settings: a #GSettings object
1556 * @key: the name of the key to set
1557 * @value: a #GVariant of the correct type
1559 * Sets @key in @settings to @value.
1561 * It is a programmer error to give a @key that isn't contained in the
1562 * schema for @settings or for @value to have the incorrect type, per
1565 * If @value is floating then this function consumes the reference.
1567 * Returns: %TRUE if setting the key succeeded,
1568 * %FALSE if the key was not writable
1573 g_settings_set_value (GSettings *settings,
1577 GSettingsSchemaKey skey;
1580 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1581 g_return_val_if_fail (key != NULL, FALSE);
1583 g_variant_ref_sink (value);
1584 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1586 if (!g_settings_schema_key_type_check (&skey, value))
1588 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1590 g_settings_schema_get_id (settings->priv->schema),
1591 g_variant_type_peek_string (skey.type),
1592 g_variant_get_type_string (value));
1595 else if (!g_settings_schema_key_range_check (&skey, value))
1597 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1598 "is outside of valid range",
1600 g_settings_schema_get_id (settings->priv->schema));
1605 success = g_settings_write_to_backend (settings, &skey, value);
1608 g_settings_schema_key_clear (&skey);
1609 g_variant_unref (value);
1616 * @settings: a #GSettings object
1617 * @key: the key to get the value for
1618 * @format: a #GVariant format string
1619 * @...: arguments as per @format
1621 * Gets the value that is stored at @key in @settings.
1623 * A convenience function that combines g_settings_get_value() with
1626 * It is a programmer error to give a @key that isn't contained in the
1627 * schema for @settings or for the #GVariantType of @format to mismatch
1628 * the type given in the schema.
1633 g_settings_get (GSettings *settings,
1635 const gchar *format,
1641 value = g_settings_get_value (settings, key);
1643 if (strchr (format, '&'))
1645 g_critical ("%s: the format string may not contain '&' (key '%s' from schema '%s'). "
1646 "This call will probably stop working with a future version of glib.",
1647 G_STRFUNC, key, g_settings_schema_get_id (settings->priv->schema));
1650 va_start (ap, format);
1651 g_variant_get_va (value, format, NULL, &ap);
1654 g_variant_unref (value);
1659 * @settings: a #GSettings object
1660 * @key: the name of the key to set
1661 * @format: a #GVariant format string
1662 * @...: arguments as per @format
1664 * Sets @key in @settings to @value.
1666 * A convenience function that combines g_settings_set_value() with
1669 * It is a programmer error to give a @key that isn't contained in the
1670 * schema for @settings or for the #GVariantType of @format to mismatch
1671 * the type given in the schema.
1673 * Returns: %TRUE if setting the key succeeded,
1674 * %FALSE if the key was not writable
1679 g_settings_set (GSettings *settings,
1681 const gchar *format,
1687 va_start (ap, format);
1688 value = g_variant_new_va (format, NULL, &ap);
1691 return g_settings_set_value (settings, key, g_steal_pointer (&value));
1695 * g_settings_get_mapped:
1696 * @settings: a #GSettings object
1697 * @key: the key to get the value for
1698 * @mapping: (scope call): the function to map the value in the
1699 * settings database to the value used by the application
1700 * @user_data: user data for @mapping
1702 * Gets the value that is stored at @key in @settings, subject to
1703 * application-level validation/mapping.
1705 * You should use this function when the application needs to perform
1706 * some processing on the value of the key (for example, parsing). The
1707 * @mapping function performs that processing. If the function
1708 * indicates that the processing was unsuccessful (due to a parse error,
1709 * for example) then the mapping is tried again with another value.
1711 * This allows a robust 'fall back to defaults' behaviour to be
1712 * implemented somewhat automatically.
1714 * The first value that is tried is the user's setting for the key. If
1715 * the mapping function fails to map this value, other values may be
1716 * tried in an unspecified order (system or site defaults, translated
1717 * schema default values, untranslated schema default values, etc).
1719 * If the mapping function fails for all possible values, one additional
1720 * attempt is made: the mapping function is called with a %NULL value.
1721 * If the mapping function still indicates failure at this point then
1722 * the application will be aborted.
1724 * The result parameter for the @mapping function is pointed to a
1725 * #gpointer which is initially set to %NULL. The same pointer is given
1726 * to each invocation of @mapping. The final value of that #gpointer is
1727 * what is returned by this function. %NULL is valid; it is returned
1728 * just as any other value would be.
1730 * Returns: (nullable) (transfer full): the result, which may be %NULL
1733 g_settings_get_mapped (GSettings *settings,
1735 GSettingsGetMapping mapping,
1738 gpointer result = NULL;
1739 GSettingsSchemaKey skey;
1743 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1744 g_return_val_if_fail (key != NULL, NULL);
1745 g_return_val_if_fail (mapping != NULL, NULL);
1747 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1749 if ((value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE)))
1751 okay = mapping (value, &result, user_data);
1752 g_variant_unref (value);
1753 if (okay) goto okay;
1756 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1758 okay = mapping (value, &result, user_data);
1759 g_variant_unref (value);
1760 if (okay) goto okay;
1763 if ((value = g_settings_schema_key_get_per_desktop_default (&skey)))
1765 okay = mapping (value, &result, user_data);
1766 g_variant_unref (value);
1767 if (okay) goto okay;
1770 if (mapping (skey.default_value, &result, user_data))
1773 if (!mapping (NULL, &result, user_data))
1774 g_error ("The mapping function given to g_settings_get_mapped() for key "
1775 "'%s' in schema '%s' returned FALSE when given a NULL value.",
1776 key, g_settings_schema_get_id (settings->priv->schema));
1779 g_settings_schema_key_clear (&skey);
1784 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1786 * g_settings_get_string:
1787 * @settings: a #GSettings object
1788 * @key: the key to get the value for
1790 * Gets the value that is stored at @key in @settings.
1792 * A convenience variant of g_settings_get() for strings.
1794 * It is a programmer error to give a @key that isn't specified as
1795 * having a string type in the schema for @settings.
1797 * Returns: (not nullable) (transfer full): a newly-allocated string
1802 g_settings_get_string (GSettings *settings,
1808 value = g_settings_get_value (settings, key);
1809 result = g_variant_dup_string (value, NULL);
1810 g_variant_unref (value);
1816 * g_settings_set_string:
1817 * @settings: a #GSettings object
1818 * @key: the name of the key to set
1819 * @value: the value to set it to
1821 * Sets @key in @settings to @value.
1823 * A convenience variant of g_settings_set() for strings.
1825 * It is a programmer error to give a @key that isn't specified as
1826 * having a string type in the schema for @settings.
1828 * Returns: %TRUE if setting the key succeeded,
1829 * %FALSE if the key was not writable
1834 g_settings_set_string (GSettings *settings,
1838 return g_settings_set_value (settings, key, g_variant_new_string (value));
1842 * g_settings_get_int:
1843 * @settings: a #GSettings object
1844 * @key: the key to get the value for
1846 * Gets the value that is stored at @key in @settings.
1848 * A convenience variant of g_settings_get() for 32-bit integers.
1850 * It is a programmer error to give a @key that isn't specified as
1851 * having a int32 type in the schema for @settings.
1853 * Returns: an integer
1858 g_settings_get_int (GSettings *settings,
1864 value = g_settings_get_value (settings, key);
1865 result = g_variant_get_int32 (value);
1866 g_variant_unref (value);
1872 * g_settings_set_int:
1873 * @settings: a #GSettings object
1874 * @key: the name of the key to set
1875 * @value: the value to set it to
1877 * Sets @key in @settings to @value.
1879 * A convenience variant of g_settings_set() for 32-bit integers.
1881 * It is a programmer error to give a @key that isn't specified as
1882 * having a int32 type in the schema for @settings.
1884 * Returns: %TRUE if setting the key succeeded,
1885 * %FALSE if the key was not writable
1890 g_settings_set_int (GSettings *settings,
1894 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1898 * g_settings_get_int64:
1899 * @settings: a #GSettings object
1900 * @key: the key to get the value for
1902 * Gets the value that is stored at @key in @settings.
1904 * A convenience variant of g_settings_get() for 64-bit integers.
1906 * It is a programmer error to give a @key that isn't specified as
1907 * having a int64 type in the schema for @settings.
1909 * Returns: a 64-bit integer
1914 g_settings_get_int64 (GSettings *settings,
1920 value = g_settings_get_value (settings, key);
1921 result = g_variant_get_int64 (value);
1922 g_variant_unref (value);
1928 * g_settings_set_int64:
1929 * @settings: a #GSettings object
1930 * @key: the name of the key to set
1931 * @value: the value to set it to
1933 * Sets @key in @settings to @value.
1935 * A convenience variant of g_settings_set() for 64-bit integers.
1937 * It is a programmer error to give a @key that isn't specified as
1938 * having a int64 type in the schema for @settings.
1940 * Returns: %TRUE if setting the key succeeded,
1941 * %FALSE if the key was not writable
1946 g_settings_set_int64 (GSettings *settings,
1950 return g_settings_set_value (settings, key, g_variant_new_int64 (value));
1954 * g_settings_get_uint:
1955 * @settings: a #GSettings object
1956 * @key: the key to get the value for
1958 * Gets the value that is stored at @key in @settings.
1960 * A convenience variant of g_settings_get() for 32-bit unsigned
1963 * It is a programmer error to give a @key that isn't specified as
1964 * having a uint32 type in the schema for @settings.
1966 * Returns: an unsigned integer
1971 g_settings_get_uint (GSettings *settings,
1977 value = g_settings_get_value (settings, key);
1978 result = g_variant_get_uint32 (value);
1979 g_variant_unref (value);
1985 * g_settings_set_uint:
1986 * @settings: a #GSettings object
1987 * @key: the name of the key to set
1988 * @value: the value to set it to
1990 * Sets @key in @settings to @value.
1992 * A convenience variant of g_settings_set() for 32-bit unsigned
1995 * It is a programmer error to give a @key that isn't specified as
1996 * having a uint32 type in the schema for @settings.
1998 * Returns: %TRUE if setting the key succeeded,
1999 * %FALSE if the key was not writable
2004 g_settings_set_uint (GSettings *settings,
2008 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
2012 * g_settings_get_uint64:
2013 * @settings: a #GSettings object
2014 * @key: the key to get the value for
2016 * Gets the value that is stored at @key in @settings.
2018 * A convenience variant of g_settings_get() for 64-bit unsigned
2021 * It is a programmer error to give a @key that isn't specified as
2022 * having a uint64 type in the schema for @settings.
2024 * Returns: a 64-bit unsigned integer
2029 g_settings_get_uint64 (GSettings *settings,
2035 value = g_settings_get_value (settings, key);
2036 result = g_variant_get_uint64 (value);
2037 g_variant_unref (value);
2043 * g_settings_set_uint64:
2044 * @settings: a #GSettings object
2045 * @key: the name of the key to set
2046 * @value: the value to set it to
2048 * Sets @key in @settings to @value.
2050 * A convenience variant of g_settings_set() for 64-bit unsigned
2053 * It is a programmer error to give a @key that isn't specified as
2054 * having a uint64 type in the schema for @settings.
2056 * Returns: %TRUE if setting the key succeeded,
2057 * %FALSE if the key was not writable
2062 g_settings_set_uint64 (GSettings *settings,
2066 return g_settings_set_value (settings, key, g_variant_new_uint64 (value));
2070 * g_settings_get_double:
2071 * @settings: a #GSettings object
2072 * @key: the key to get the value for
2074 * Gets the value that is stored at @key in @settings.
2076 * A convenience variant of g_settings_get() for doubles.
2078 * It is a programmer error to give a @key that isn't specified as
2079 * having a 'double' type in the schema for @settings.
2086 g_settings_get_double (GSettings *settings,
2092 value = g_settings_get_value (settings, key);
2093 result = g_variant_get_double (value);
2094 g_variant_unref (value);
2100 * g_settings_set_double:
2101 * @settings: a #GSettings object
2102 * @key: the name of the key to set
2103 * @value: the value to set it to
2105 * Sets @key in @settings to @value.
2107 * A convenience variant of g_settings_set() for doubles.
2109 * It is a programmer error to give a @key that isn't specified as
2110 * having a 'double' type in the schema for @settings.
2112 * Returns: %TRUE if setting the key succeeded,
2113 * %FALSE if the key was not writable
2118 g_settings_set_double (GSettings *settings,
2122 return g_settings_set_value (settings, key, g_variant_new_double (value));
2126 * g_settings_get_boolean:
2127 * @settings: a #GSettings object
2128 * @key: the key to get the value for
2130 * Gets the value that is stored at @key in @settings.
2132 * A convenience variant of g_settings_get() for booleans.
2134 * It is a programmer error to give a @key that isn't specified as
2135 * having a boolean type in the schema for @settings.
2137 * Returns: a boolean
2142 g_settings_get_boolean (GSettings *settings,
2148 value = g_settings_get_value (settings, key);
2149 result = g_variant_get_boolean (value);
2150 g_variant_unref (value);
2156 * g_settings_set_boolean:
2157 * @settings: a #GSettings object
2158 * @key: the name of the key to set
2159 * @value: the value to set it to
2161 * Sets @key in @settings to @value.
2163 * A convenience variant of g_settings_set() for booleans.
2165 * It is a programmer error to give a @key that isn't specified as
2166 * having a boolean type in the schema for @settings.
2168 * Returns: %TRUE if setting the key succeeded,
2169 * %FALSE if the key was not writable
2174 g_settings_set_boolean (GSettings *settings,
2178 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
2182 * g_settings_get_strv:
2183 * @settings: a #GSettings object
2184 * @key: the key to get the value for
2186 * A convenience variant of g_settings_get() for string arrays.
2188 * It is a programmer error to give a @key that isn't specified as
2189 * having an array of strings type in the schema for @settings.
2191 * Returns: (array zero-terminated=1) (not nullable) (transfer full): a
2192 * newly-allocated, %NULL-terminated array of strings, the value that
2193 * is stored at @key in @settings.
2198 g_settings_get_strv (GSettings *settings,
2204 value = g_settings_get_value (settings, key);
2205 result = g_variant_dup_strv (value, NULL);
2206 g_variant_unref (value);
2212 * g_settings_set_strv:
2213 * @settings: a #GSettings object
2214 * @key: the name of the key to set
2215 * @value: (nullable) (array zero-terminated=1): the value to set it to, or %NULL
2217 * Sets @key in @settings to @value.
2219 * A convenience variant of g_settings_set() for string arrays. If
2220 * @value is %NULL, then @key is set to be the empty array.
2222 * It is a programmer error to give a @key that isn't specified as
2223 * having an array of strings type in the schema for @settings.
2225 * Returns: %TRUE if setting the key succeeded,
2226 * %FALSE if the key was not writable
2231 g_settings_set_strv (GSettings *settings,
2233 const gchar * const *value)
2238 array = g_variant_new_strv (value, -1);
2240 array = g_variant_new_strv (NULL, 0);
2242 return g_settings_set_value (settings, key, array);
2245 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2248 * @settings: a #GSettings object
2250 * Changes the #GSettings object into 'delay-apply' mode. In this
2251 * mode, changes to @settings are not immediately propagated to the
2252 * backend, but kept locally until g_settings_apply() is called.
2257 g_settings_delay (GSettings *settings)
2259 GDelayedSettingsBackend *delayed = NULL;
2261 g_return_if_fail (G_IS_SETTINGS (settings));
2263 if (G_IS_DELAYED_SETTINGS_BACKEND (settings->priv->backend))
2266 delayed = g_delayed_settings_backend_new (settings->priv->backend,
2268 settings->priv->main_context);
2269 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2270 g_object_unref (settings->priv->backend);
2272 settings->priv->backend = G_SETTINGS_BACKEND (delayed);
2273 g_settings_backend_watch (settings->priv->backend,
2274 &listener_vtable, G_OBJECT (settings),
2275 settings->priv->main_context);
2277 g_object_notify (G_OBJECT (settings), "delay-apply");
2282 * @settings: a #GSettings instance
2284 * Applies any changes that have been made to the settings. This
2285 * function does nothing unless @settings is in 'delay-apply' mode;
2286 * see g_settings_delay(). In the normal case settings are always
2287 * applied immediately.
2290 g_settings_apply (GSettings *settings)
2292 if (G_IS_DELAYED_SETTINGS_BACKEND (settings->priv->backend))
2294 GDelayedSettingsBackend *delayed;
2296 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2297 g_delayed_settings_backend_apply (delayed);
2302 * g_settings_revert:
2303 * @settings: a #GSettings instance
2305 * Reverts all non-applied changes to the settings. This function
2306 * does nothing unless @settings is in 'delay-apply' mode; see
2307 * g_settings_delay(). In the normal case settings are always applied
2310 * Change notifications will be emitted for affected keys.
2313 g_settings_revert (GSettings *settings)
2315 if (G_IS_DELAYED_SETTINGS_BACKEND (settings->priv->backend))
2317 GDelayedSettingsBackend *delayed;
2319 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2320 g_delayed_settings_backend_revert (delayed);
2325 * g_settings_get_has_unapplied:
2326 * @settings: a #GSettings object
2328 * Returns whether the #GSettings object has any unapplied
2329 * changes. This can only be the case if it is in 'delayed-apply' mode.
2331 * Returns: %TRUE if @settings has unapplied changes
2336 g_settings_get_has_unapplied (GSettings *settings)
2338 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2340 return G_IS_DELAYED_SETTINGS_BACKEND (settings->priv->backend) &&
2341 g_delayed_settings_backend_get_has_unapplied (
2342 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2345 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2348 * @settings: a #GSettings object
2349 * @key: the name of a key
2351 * Resets @key to its default value.
2353 * This call resets the key, as much as possible, to its default value.
2354 * That might be the value specified in the schema or the one set by the
2358 g_settings_reset (GSettings *settings,
2363 g_return_if_fail (G_IS_SETTINGS (settings));
2364 g_return_if_fail (key != NULL);
2366 path = g_strconcat (settings->priv->path, key, NULL);
2367 g_settings_backend_reset (settings->priv->backend, path, NULL);
2374 * Ensures that all pending operations are complete for the default backend.
2376 * Writes made to a #GSettings are handled asynchronously. For this
2377 * reason, it is very unlikely that the changes have it to disk by the
2378 * time g_settings_set() returns.
2380 * This call will block until all of the writes have made it to the
2381 * backend. Since the mainloop is not running, no change notifications
2382 * will be dispatched during this call (but some may be queued by the
2383 * time the call is done).
2386 g_settings_sync (void)
2388 g_settings_backend_sync_default ();
2392 * g_settings_is_writable:
2393 * @settings: a #GSettings object
2394 * @name: the name of a key
2396 * Finds out if a key can be written or not
2398 * Returns: %TRUE if the key @name is writable
2403 g_settings_is_writable (GSettings *settings,
2409 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2411 path = g_strconcat (settings->priv->path, name, NULL);
2412 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2419 * g_settings_get_child:
2420 * @settings: a #GSettings object
2421 * @name: the name of the child schema
2423 * Creates a child settings object which has a base path of
2424 * `base-path/@name`, where `base-path` is the base path of
2427 * The schema for the child settings object must have been declared
2428 * in the schema of @settings using a `<child>` element.
2430 * The created child settings object will inherit the #GSettings:delay-apply
2431 * mode from @settings.
2433 * Returns: (not nullable) (transfer full): a 'child' settings object
2438 g_settings_get_child (GSettings *settings,
2441 GSettingsSchema *child_schema;
2445 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2447 child_schema = g_settings_schema_get_child_schema (settings->priv->schema,
2449 if (child_schema == NULL)
2450 g_error ("Schema '%s' has no child '%s' or child schema not found",
2451 g_settings_schema_get_id (settings->priv->schema), name);
2453 child_path = g_strconcat (settings->priv->path, name, "/", NULL);
2454 child = g_settings_new_full (child_schema,
2455 settings->priv->backend,
2457 g_settings_schema_unref (child_schema);
2458 g_free (child_path);
2464 * g_settings_list_keys:
2465 * @settings: a #GSettings object
2467 * Introspects the list of keys on @settings.
2469 * You should probably not be calling this function from "normal" code
2470 * (since you should already know what keys are in your schema). This
2471 * function is intended for introspection reasons.
2473 * You should free the return value with g_strfreev() when you are done
2476 * Returns: (not nullable) (transfer full) (element-type utf8): a list
2477 * of the keys on @settings, in no defined order
2478 * Deprecated: 2.46: Use g_settings_schema_list_keys() instead.
2481 g_settings_list_keys (GSettings *settings)
2483 return g_settings_schema_list_keys (settings->priv->schema);
2487 * g_settings_list_children:
2488 * @settings: a #GSettings object
2490 * Gets the list of children on @settings.
2492 * The list is exactly the list of strings for which it is not an error
2493 * to call g_settings_get_child().
2495 * There is little reason to call this function from "normal" code, since
2496 * you should already know what children are in your schema. This function
2497 * may still be useful there for introspection reasons, however.
2499 * You should free the return value with g_strfreev() when you are done
2502 * Returns: (not nullable) (transfer full) (element-type utf8): a list of the children
2503 * on @settings, in no defined order
2506 g_settings_list_children (GSettings *settings)
2508 return g_settings_schema_list_children (settings->priv->schema);
2512 * g_settings_get_range:
2513 * @settings: a #GSettings
2514 * @key: the key to query the range of
2516 * Queries the range of a key.
2520 * Deprecated:2.40:Use g_settings_schema_key_get_range() instead.
2523 g_settings_get_range (GSettings *settings,
2526 GSettingsSchemaKey skey;
2529 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2530 range = g_settings_schema_key_get_range (&skey);
2531 g_settings_schema_key_clear (&skey);
2537 * g_settings_range_check:
2538 * @settings: a #GSettings
2539 * @key: the key to check
2540 * @value: the value to check
2542 * Checks if the given @value is of the correct type and within the
2543 * permitted range for @key.
2545 * Returns: %TRUE if @value is valid for @key
2549 * Deprecated:2.40:Use g_settings_schema_key_range_check() instead.
2552 g_settings_range_check (GSettings *settings,
2556 GSettingsSchemaKey skey;
2559 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2560 good = g_settings_schema_key_range_check (&skey, value);
2561 g_settings_schema_key_clear (&skey);
2569 GSettingsSchemaKey key;
2570 GSettings *settings;
2573 GSettingsBindGetMapping get_mapping;
2574 GSettingsBindSetMapping set_mapping;
2576 GDestroyNotify destroy;
2578 guint writable_handler_id;
2579 guint property_handler_id;
2580 const GParamSpec *property;
2581 guint key_handler_id;
2583 /* prevent recursion */
2588 g_settings_binding_free (gpointer data)
2590 GSettingsBinding *binding = data;
2592 g_assert (!binding->running);
2594 if (binding->writable_handler_id)
2595 g_signal_handler_disconnect (binding->settings,
2596 binding->writable_handler_id);
2598 if (binding->key_handler_id)
2599 g_signal_handler_disconnect (binding->settings,
2600 binding->key_handler_id);
2602 if (g_signal_handler_is_connected (binding->object,
2603 binding->property_handler_id))
2604 g_signal_handler_disconnect (binding->object,
2605 binding->property_handler_id);
2607 g_settings_schema_key_clear (&binding->key);
2609 if (binding->destroy)
2610 binding->destroy (binding->user_data);
2612 g_object_unref (binding->settings);
2614 g_slice_free (GSettingsBinding, binding);
2618 g_settings_binding_quark (const char *property)
2623 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2624 quark = g_quark_from_string (tmp);
2631 g_settings_binding_key_changed (GSettings *settings,
2635 GSettingsBinding *binding = user_data;
2636 GValue value = G_VALUE_INIT;
2639 g_assert (settings == binding->settings);
2640 g_assert (key == binding->key.name);
2642 if (binding->running)
2645 binding->running = TRUE;
2647 g_value_init (&value, binding->property->value_type);
2649 variant = g_settings_read_from_backend (binding->settings, &binding->key, FALSE, FALSE);
2650 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2652 /* silently ignore errors in the user's config database */
2653 g_variant_unref (variant);
2657 if (variant == NULL)
2659 variant = g_settings_schema_key_get_translated_default (&binding->key);
2661 !binding->get_mapping (&value, variant, binding->user_data))
2663 /* flag translation errors with a warning */
2664 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
2665 "was rejected by the binding mapping function",
2666 binding->key.unparsed, binding->key.name,
2667 g_settings_schema_get_id (binding->key.schema));
2668 g_variant_unref (variant);
2673 if (variant == NULL)
2675 variant = g_settings_schema_key_get_per_desktop_default (&binding->key);
2677 !binding->get_mapping (&value, variant, binding->user_data))
2679 g_error ("Per-desktop default value for key '%s' in schema '%s' "
2680 "was rejected by the binding mapping function.",
2681 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2682 g_variant_unref (variant);
2687 if (variant == NULL)
2689 variant = g_variant_ref (binding->key.default_value);
2690 if (!binding->get_mapping (&value, variant, binding->user_data))
2691 g_error ("The schema default value for key '%s' in schema '%s' "
2692 "was rejected by the binding mapping function.",
2693 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2696 g_object_set_property (binding->object, binding->property->name, &value);
2697 g_variant_unref (variant);
2698 g_value_unset (&value);
2700 binding->running = FALSE;
2704 g_settings_binding_property_changed (GObject *object,
2705 const GParamSpec *pspec,
2708 GSettingsBinding *binding = user_data;
2709 GValue value = G_VALUE_INIT;
2711 gboolean valid = TRUE;
2713 g_assert (object == binding->object);
2714 g_assert (pspec == binding->property);
2716 if (binding->running)
2719 binding->running = TRUE;
2721 g_value_init (&value, pspec->value_type);
2722 g_object_get_property (object, pspec->name, &value);
2723 if ((variant = binding->set_mapping (&value, binding->key.type,
2724 binding->user_data)))
2726 g_variant_take_ref (variant);
2728 if (!g_settings_schema_key_type_check (&binding->key, variant))
2731 type_str = g_variant_type_dup_string (binding->key.type);
2732 g_critical ("binding mapping function for key '%s' returned "
2733 "GVariant of type '%s' when type '%s' was requested",
2734 binding->key.name, g_variant_get_type_string (variant),
2740 if (valid && !g_settings_schema_key_range_check (&binding->key, variant))
2743 variant_str = g_variant_print (variant, TRUE);
2744 g_critical ("GObject property '%s' on a '%s' object is out of "
2745 "schema-specified range for key '%s' of '%s': %s",
2746 binding->property->name, g_type_name (binding->property->owner_type),
2747 binding->key.name, g_settings_schema_get_id (binding->key.schema),
2749 g_free (variant_str);
2755 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2757 g_variant_unref (variant);
2759 g_value_unset (&value);
2761 binding->running = FALSE;
2765 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2769 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2774 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2775 const GVariantType *expected_type,
2778 return g_variant_new_boolean (!g_value_get_boolean (value));
2783 * @settings: a #GSettings object
2784 * @key: the key to bind
2785 * @object: (type GObject.Object): a #GObject
2786 * @property: the name of the property to bind
2787 * @flags: flags for the binding
2789 * Create a binding between the @key in the @settings object
2790 * and the property @property of @object.
2792 * The binding uses the default GIO mapping functions to map
2793 * between the settings and property values. These functions
2794 * handle booleans, numeric types and string types in a
2795 * straightforward way. Use g_settings_bind_with_mapping() if
2796 * you need a custom mapping, or map between types that are not
2797 * supported by the default mapping functions.
2799 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2800 * function also establishes a binding between the writability of
2801 * @key and the "sensitive" property of @object (if @object has
2802 * a boolean property by that name). See g_settings_bind_writable()
2803 * for more details about writable bindings.
2805 * Note that the lifecycle of the binding is tied to @object,
2806 * and that you can have only one binding per object property.
2807 * If you bind the same property twice on the same object, the second
2808 * binding overrides the first one.
2813 g_settings_bind (GSettings *settings,
2816 const gchar *property,
2817 GSettingsBindFlags flags)
2819 GSettingsBindGetMapping get_mapping = NULL;
2820 GSettingsBindSetMapping set_mapping = NULL;
2822 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2824 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2825 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2827 /* can't pass this flag to g_settings_bind_with_mapping() */
2828 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2831 g_settings_bind_with_mapping (settings, key, object, property, flags,
2832 get_mapping, set_mapping, NULL, NULL);
2836 * g_settings_bind_with_mapping: (skip)
2837 * @settings: a #GSettings object
2838 * @key: the key to bind
2839 * @object: (type GObject.Object): a #GObject
2840 * @property: the name of the property to bind
2841 * @flags: flags for the binding
2842 * @get_mapping: a function that gets called to convert values
2843 * from @settings to @object, or %NULL to use the default GIO mapping
2844 * @set_mapping: a function that gets called to convert values
2845 * from @object to @settings, or %NULL to use the default GIO mapping
2846 * @user_data: data that gets passed to @get_mapping and @set_mapping
2847 * @destroy: #GDestroyNotify function for @user_data
2849 * Create a binding between the @key in the @settings object
2850 * and the property @property of @object.
2852 * The binding uses the provided mapping functions to map between
2853 * settings and property values.
2855 * Note that the lifecycle of the binding is tied to @object,
2856 * and that you can have only one binding per object property.
2857 * If you bind the same property twice on the same object, the second
2858 * binding overrides the first one.
2863 g_settings_bind_with_mapping (GSettings *settings,
2866 const gchar *property,
2867 GSettingsBindFlags flags,
2868 GSettingsBindGetMapping get_mapping,
2869 GSettingsBindSetMapping set_mapping,
2871 GDestroyNotify destroy)
2873 GSettingsBinding *binding;
2874 GObjectClass *objectclass;
2875 gchar *detailed_signal;
2876 GQuark binding_quark;
2878 g_return_if_fail (G_IS_SETTINGS (settings));
2879 g_return_if_fail (key != NULL);
2880 g_return_if_fail (G_IS_OBJECT (object));
2881 g_return_if_fail (property != NULL);
2882 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2884 objectclass = G_OBJECT_GET_CLASS (object);
2886 binding = g_slice_new0 (GSettingsBinding);
2887 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2888 binding->settings = g_object_ref (settings);
2889 binding->object = object;
2890 binding->property = g_object_class_find_property (objectclass, property);
2891 binding->user_data = user_data;
2892 binding->destroy = destroy;
2893 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2894 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2896 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2897 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2899 if (binding->property == NULL)
2901 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2902 property, G_OBJECT_TYPE_NAME (object));
2906 if ((flags & G_SETTINGS_BIND_GET) &&
2907 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2909 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2910 "writable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2913 if ((flags & G_SETTINGS_BIND_SET) &&
2914 (binding->property->flags & G_PARAM_READABLE) == 0)
2916 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2917 "readable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2921 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2923 /* g_settings_bind_invert_boolean_get_mapping() is a private
2924 * function, so if we are here it means that g_settings_bind() was
2925 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2927 * Ensure that both sides are boolean.
2930 if (binding->property->value_type != G_TYPE_BOOLEAN)
2932 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2933 "was specified, but property '%s' on type '%s' has "
2934 "type '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2935 g_type_name ((binding->property->value_type)));
2939 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2941 gchar *type_string = g_variant_type_dup_string (binding->key.type);
2942 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2943 "was specified, but key '%s' on schema '%s' has "
2944 "type '%s'", key, g_settings_schema_get_id (settings->priv->schema),
2946 g_free (type_string);
2952 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2953 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2954 !g_settings_mapping_is_compatible (binding->property->value_type,
2957 gchar *type_string = g_variant_type_dup_string (binding->key.type);
2958 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2959 "'%s' which is not compatible with type '%s' of key '%s' "
2960 "on schema '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2961 g_type_name (binding->property->value_type),
2963 g_settings_schema_get_id (settings->priv->schema));
2964 g_free (type_string);
2968 if ((flags & G_SETTINGS_BIND_SET) &&
2969 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2971 GParamSpec *sensitive;
2973 sensitive = g_object_class_find_property (objectclass, "sensitive");
2975 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2976 (sensitive->flags & G_PARAM_WRITABLE))
2977 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2980 if (flags & G_SETTINGS_BIND_SET)
2982 detailed_signal = g_strdup_printf ("notify::%s", binding->property->name);
2983 binding->property_handler_id =
2984 g_signal_connect (object, detailed_signal,
2985 G_CALLBACK (g_settings_binding_property_changed),
2987 g_free (detailed_signal);
2989 if (~flags & G_SETTINGS_BIND_GET)
2990 g_settings_binding_property_changed (object,
2995 if (flags & G_SETTINGS_BIND_GET)
2997 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2999 detailed_signal = g_strdup_printf ("changed::%s", key);
3000 binding->key_handler_id =
3001 g_signal_connect (settings, detailed_signal,
3002 G_CALLBACK (g_settings_binding_key_changed),
3004 g_free (detailed_signal);
3007 g_settings_binding_key_changed (settings, binding->key.name, binding);
3010 binding_quark = g_settings_binding_quark (binding->property->name);
3011 g_object_set_qdata_full (object, binding_quark,
3012 binding, g_settings_binding_free);
3015 /* Writability binding {{{1 */
3018 GSettings *settings;
3021 const gchar *property;
3024 } GSettingsWritableBinding;
3027 g_settings_writable_binding_free (gpointer data)
3029 GSettingsWritableBinding *binding = data;
3031 g_signal_handler_disconnect (binding->settings, binding->handler_id);
3032 g_object_unref (binding->settings);
3033 g_slice_free (GSettingsWritableBinding, binding);
3037 g_settings_binding_writable_changed (GSettings *settings,
3041 GSettingsWritableBinding *binding = user_data;
3044 g_assert (settings == binding->settings);
3045 g_assert (key == binding->key);
3047 writable = g_settings_is_writable (settings, key);
3049 if (binding->inverted)
3050 writable = !writable;
3052 g_object_set (binding->object, binding->property, writable, NULL);
3056 * g_settings_bind_writable:
3057 * @settings: a #GSettings object
3058 * @key: the key to bind
3059 * @object: (type GObject.Object):a #GObject
3060 * @property: the name of a boolean property to bind
3061 * @inverted: whether to 'invert' the value
3063 * Create a binding between the writability of @key in the
3064 * @settings object and the property @property of @object.
3065 * The property must be boolean; "sensitive" or "visible"
3066 * properties of widgets are the most likely candidates.
3068 * Writable bindings are always uni-directional; changes of the
3069 * writability of the setting will be propagated to the object
3070 * property, not the other way.
3072 * When the @inverted argument is %TRUE, the binding inverts the
3073 * value as it passes from the setting to the object, i.e. @property
3074 * will be set to %TRUE if the key is not writable.
3076 * Note that the lifecycle of the binding is tied to @object,
3077 * and that you can have only one binding per object property.
3078 * If you bind the same property twice on the same object, the second
3079 * binding overrides the first one.
3084 g_settings_bind_writable (GSettings *settings,
3087 const gchar *property,
3090 GSettingsWritableBinding *binding;
3091 gchar *detailed_signal;
3094 g_return_if_fail (G_IS_SETTINGS (settings));
3096 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
3099 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
3100 property, G_OBJECT_TYPE_NAME (object));
3103 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
3105 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
3106 property, G_OBJECT_TYPE_NAME (object));
3110 binding = g_slice_new (GSettingsWritableBinding);
3111 binding->settings = g_object_ref (settings);
3112 binding->object = object;
3113 binding->key = g_intern_string (key);
3114 binding->property = g_intern_string (property);
3115 binding->inverted = inverted;
3117 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3118 binding->handler_id =
3119 g_signal_connect (settings, detailed_signal,
3120 G_CALLBACK (g_settings_binding_writable_changed),
3122 g_free (detailed_signal);
3124 g_object_set_qdata_full (object, g_settings_binding_quark (property),
3125 binding, g_settings_writable_binding_free);
3127 g_settings_binding_writable_changed (settings, binding->key, binding);
3131 * g_settings_unbind:
3132 * @object: (type GObject.Object): the object
3133 * @property: the property whose binding is removed
3135 * Removes an existing binding for @property on @object.
3137 * Note that bindings are automatically removed when the
3138 * object is finalized, so it is rarely necessary to call this
3144 g_settings_unbind (gpointer object,
3145 const gchar *property)
3147 GQuark binding_quark;
3149 binding_quark = g_settings_binding_quark (property);
3150 g_object_set_qdata (object, binding_quark, NULL);
3157 GObject parent_instance;
3159 GSettingsSchemaKey key;
3160 GSettings *settings;
3163 typedef GObjectClass GSettingsActionClass;
3165 static GType g_settings_action_get_type (void);
3166 static void g_settings_action_iface_init (GActionInterface *iface);
3167 G_DEFINE_TYPE_WITH_CODE (GSettingsAction, g_settings_action, G_TYPE_OBJECT,
3168 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_settings_action_iface_init))
3174 ACTION_PROP_PARAMETER_TYPE,
3175 ACTION_PROP_ENABLED,
3176 ACTION_PROP_STATE_TYPE,
3180 static const gchar *
3181 g_settings_action_get_name (GAction *action)
3183 GSettingsAction *gsa = (GSettingsAction *) action;
3185 return gsa->key.name;
3188 static const GVariantType *
3189 g_settings_action_get_parameter_type (GAction *action)
3191 GSettingsAction *gsa = (GSettingsAction *) action;
3192 const GVariantType *type;
3194 type = g_variant_get_type (gsa->key.default_value);
3195 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
3202 g_settings_action_get_enabled (GAction *action)
3204 GSettingsAction *gsa = (GSettingsAction *) action;
3206 return g_settings_is_writable (gsa->settings, gsa->key.name);
3209 static const GVariantType *
3210 g_settings_action_get_state_type (GAction *action)
3212 GSettingsAction *gsa = (GSettingsAction *) action;
3214 return g_variant_get_type (gsa->key.default_value);
3218 g_settings_action_get_state (GAction *action)
3220 GSettingsAction *gsa = (GSettingsAction *) action;
3223 value = g_settings_read_from_backend (gsa->settings, &gsa->key, FALSE, FALSE);
3226 value = g_settings_schema_key_get_default_value (&gsa->key);
3232 g_settings_action_get_state_hint (GAction *action)
3234 GSettingsAction *gsa = (GSettingsAction *) action;
3236 /* no point in reimplementing this... */
3237 return g_settings_schema_key_get_range (&gsa->key);
3241 g_settings_action_change_state (GAction *action,
3244 GSettingsAction *gsa = (GSettingsAction *) action;
3246 if (g_settings_schema_key_type_check (&gsa->key, value) && g_settings_schema_key_range_check (&gsa->key, value))
3247 g_settings_write_to_backend (gsa->settings, &gsa->key, value);
3251 g_settings_action_activate (GAction *action,
3252 GVariant *parameter)
3254 GSettingsAction *gsa = (GSettingsAction *) action;
3256 if (g_variant_is_of_type (gsa->key.default_value, G_VARIANT_TYPE_BOOLEAN))
3260 if (parameter != NULL)
3263 old = g_settings_action_get_state (action);
3264 parameter = g_variant_new_boolean (!g_variant_get_boolean (old));
3265 g_variant_unref (old);
3268 g_action_change_state (action, parameter);
3272 g_settings_action_get_property (GObject *object, guint prop_id,
3273 GValue *value, GParamSpec *pspec)
3275 GAction *action = G_ACTION (object);
3279 case ACTION_PROP_NAME:
3280 g_value_set_string (value, g_settings_action_get_name (action));
3283 case ACTION_PROP_PARAMETER_TYPE:
3284 g_value_set_boxed (value, g_settings_action_get_parameter_type (action));
3287 case ACTION_PROP_ENABLED:
3288 g_value_set_boolean (value, g_settings_action_get_enabled (action));
3291 case ACTION_PROP_STATE_TYPE:
3292 g_value_set_boxed (value, g_settings_action_get_state_type (action));
3295 case ACTION_PROP_STATE:
3296 g_value_take_variant (value, g_settings_action_get_state (action));
3300 g_assert_not_reached ();
3305 g_settings_action_finalize (GObject *object)
3307 GSettingsAction *gsa = (GSettingsAction *) object;
3309 g_signal_handlers_disconnect_by_data (gsa->settings, gsa);
3310 g_object_unref (gsa->settings);
3311 g_settings_schema_key_clear (&gsa->key);
3313 G_OBJECT_CLASS (g_settings_action_parent_class)
3314 ->finalize (object);
3318 g_settings_action_init (GSettingsAction *gsa)
3323 g_settings_action_iface_init (GActionInterface *iface)
3325 iface->get_name = g_settings_action_get_name;
3326 iface->get_parameter_type = g_settings_action_get_parameter_type;
3327 iface->get_enabled = g_settings_action_get_enabled;
3328 iface->get_state_type = g_settings_action_get_state_type;
3329 iface->get_state = g_settings_action_get_state;
3330 iface->get_state_hint = g_settings_action_get_state_hint;
3331 iface->change_state = g_settings_action_change_state;
3332 iface->activate = g_settings_action_activate;
3336 g_settings_action_class_init (GSettingsActionClass *class)
3338 class->get_property = g_settings_action_get_property;
3339 class->finalize = g_settings_action_finalize;
3341 g_object_class_override_property (class, ACTION_PROP_NAME, "name");
3342 g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE, "parameter-type");
3343 g_object_class_override_property (class, ACTION_PROP_ENABLED, "enabled");
3344 g_object_class_override_property (class, ACTION_PROP_STATE_TYPE, "state-type");
3345 g_object_class_override_property (class, ACTION_PROP_STATE, "state");
3349 g_settings_action_changed (GSettings *settings,
3353 g_object_notify (user_data, "state");
3357 g_settings_action_enabled_changed (GSettings *settings,
3361 g_object_notify (user_data, "enabled");
3365 * g_settings_create_action:
3366 * @settings: a #GSettings
3367 * @key: the name of a key in @settings
3369 * Creates a #GAction corresponding to a given #GSettings key.
3371 * The action has the same name as the key.
3373 * The value of the key becomes the state of the action and the action
3374 * is enabled when the key is writable. Changing the state of the
3375 * action results in the key being written to. Changes to the value or
3376 * writability of the key cause appropriate change notifications to be
3377 * emitted for the action.
3379 * For boolean-valued keys, action activations take no parameter and
3380 * result in the toggling of the value. For all other types,
3381 * activations take the new value for the key (which must have the
3384 * Returns: (not nullable) (transfer full): a new #GAction
3389 g_settings_create_action (GSettings *settings,
3392 GSettingsAction *gsa;
3393 gchar *detailed_signal;
3395 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
3396 g_return_val_if_fail (key != NULL, NULL);
3398 gsa = g_object_new (g_settings_action_get_type (), NULL);
3399 gsa->settings = g_object_ref (settings);
3400 g_settings_schema_key_init (&gsa->key, settings->priv->schema, key);
3402 detailed_signal = g_strdup_printf ("changed::%s", key);
3403 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_changed), gsa);
3404 g_free (detailed_signal);
3405 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3406 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_enabled_changed), gsa);
3407 g_free (detailed_signal);
3409 return G_ACTION (gsa);
3414 /* vim:set foldmethod=marker: */