Do not expand autoptr macros when running introspection
[glib.git] / gio / gsettings.c
blob7b0b6a1629fed8bb258801cbffd0e8d3f671f51f
1 /*
2 * Copyright © 2009, 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 /* Prelude {{{1 */
21 #include "config.h"
23 #include <glib.h>
24 #include <glibintl.h>
26 #include "gsettings.h"
28 #include "gdelayedsettingsbackend.h"
29 #include "gsettingsbackendinternal.h"
30 #include "gsettings-mapping.h"
31 #include "gsettingsschema-internal.h"
32 #include "gaction.h"
34 #include "strinfo.c"
36 /**
37 * SECTION:gsettings
38 * @short_description: High-level API for application settings
39 * @include: gio/gio.h
41 * The #GSettings class provides a convenient API for storing and retrieving
42 * application settings.
44 * Reads and writes can be considered to be non-blocking. Reading
45 * settings with #GSettings is typically extremely fast: on
46 * approximately the same order of magnitude (but slower than) a
47 * #GHashTable lookup. Writing settings is also extremely fast in terms
48 * of time to return to your application, but can be extremely expensive
49 * for other threads and other processes. Many settings backends
50 * (including dconf) have lazy initialisation which means in the common
51 * case of the user using their computer without modifying any settings
52 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
53 * even need to be started in this case. For this reason, you should
54 * only ever modify #GSettings keys in response to explicit user action.
55 * Particular care should be paid to ensure that modifications are not
56 * made during startup -- for example, when setting the initial value
57 * of preferences widgets. The built-in g_settings_bind() functionality
58 * is careful not to write settings in response to notify signals as a
59 * result of modifications that it makes to widgets.
61 * When creating a GSettings instance, you have to specify a schema
62 * that describes the keys in your settings and their types and default
63 * values, as well as some other information.
65 * Normally, a schema has as fixed path that determines where the settings
66 * are stored in the conceptual global tree of settings. However, schemas
67 * can also be '[relocatable][gsettings-relocatable]', i.e. not equipped with
68 * a fixed path. This is
69 * useful e.g. when the schema describes an 'account', and you want to be
70 * able to store a arbitrary number of accounts.
72 * Paths must start with and end with a forward slash character ('/')
73 * and must not contain two sequential slash characters. Paths should
74 * be chosen based on a domain name associated with the program or
75 * library to which the settings belong. Examples of paths are
76 * "/org/gtk/settings/file-chooser/" and "/ca/desrt/dconf-editor/".
77 * Paths should not start with "/apps/", "/desktop/" or "/system/" as
78 * they often did in GConf.
80 * Unlike other configuration systems (like GConf), GSettings does not
81 * restrict keys to basic types like strings and numbers. GSettings stores
82 * values as #GVariant, and allows any #GVariantType for keys. Key names
83 * are restricted to lowercase characters, numbers and '-'. Furthermore,
84 * the names must begin with a lowercase character, must not end
85 * with a '-', and must not contain consecutive dashes.
87 * Similar to GConf, the default values in GSettings schemas can be
88 * localized, but the localized values are stored in gettext catalogs
89 * and looked up with the domain that is specified in the
90 * `gettext-domain` attribute of the <schemalist> or <schema>
91 * elements and the category that is specified in the `l10n` attribute of
92 * the <default> element. The string which is translated includes all text in
93 * the <default> element, including any surrounding quotation marks.
95 * The `l10n` attribute must be set to `messages` or `time`, and sets the
96 * [locale category for
97 * translation](https://www.gnu.org/software/gettext/manual/html_node/Aspects.html#index-locale-categories-1).
98 * The `messages` category should be used by default; use `time` for
99 * translatable date or time formats. A translation comment can be added as an
100 * XML comment immediately above the <default> element — it is recommended to
101 * add these comments to aid translators understand the meaning and
102 * implications of the default value. An optional translation `context`
103 * attribute can be set on the <default> element to disambiguate multiple
104 * defaults which use the same string.
106 * For example:
107 * |[
108 * <!-- Translators: A list of words which are not allowed to be typed, in
109 * GVariant serialization syntax.
110 * See: https://developer.gnome.org/glib/stable/gvariant-text.html -->
111 * <default l10n='messages' context='Banned words'>['bad', 'words']</default>
112 * ]|
114 * Translations of default values must remain syntactically valid serialized
115 * #GVariants (e.g. retaining any surrounding quotation marks) or runtime
116 * errors will occur.
118 * GSettings uses schemas in a compact binary form that is created
119 * by the [glib-compile-schemas][glib-compile-schemas]
120 * utility. The input is a schema description in an XML format.
122 * A DTD for the gschema XML format can be found here:
123 * [gschema.dtd](https://git.gnome.org/browse/glib/tree/gio/gschema.dtd)
125 * The [glib-compile-schemas][glib-compile-schemas] tool expects schema
126 * files to have the extension `.gschema.xml`.
128 * At runtime, schemas are identified by their id (as specified in the
129 * id attribute of the <schema> element). The convention for schema
130 * ids is to use a dotted name, similar in style to a D-Bus bus name,
131 * e.g. "org.gnome.SessionManager". In particular, if the settings are
132 * for a specific service that owns a D-Bus bus name, the D-Bus bus name
133 * and schema id should match. For schemas which deal with settings not
134 * associated with one named application, the id should not use
135 * StudlyCaps, e.g. "org.gnome.font-rendering".
137 * In addition to #GVariant types, keys can have types that have
138 * enumerated types. These can be described by a <choice>,
139 * <enum> or <flags> element, as seen in the
140 * [example][schema-enumerated]. The underlying type of such a key
141 * is string, but you can use g_settings_get_enum(), g_settings_set_enum(),
142 * g_settings_get_flags(), g_settings_set_flags() access the numeric values
143 * corresponding to the string value of enum and flags keys.
145 * An example for default value:
146 * |[
147 * <schemalist>
148 * <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
150 * <key name="greeting" type="s">
151 * <default l10n="messages">"Hello, earthlings"</default>
152 * <summary>A greeting</summary>
153 * <description>
154 * Greeting of the invading martians
155 * </description>
156 * </key>
158 * <key name="box" type="(ii)">
159 * <default>(20,30)</default>
160 * </key>
162 * </schema>
163 * </schemalist>
164 * ]|
166 * An example for ranges, choices and enumerated types:
167 * |[
168 * <schemalist>
170 * <enum id="org.gtk.Test.myenum">
171 * <value nick="first" value="1"/>
172 * <value nick="second" value="2"/>
173 * </enum>
175 * <flags id="org.gtk.Test.myflags">
176 * <value nick="flag1" value="1"/>
177 * <value nick="flag2" value="2"/>
178 * <value nick="flag3" value="4"/>
179 * </flags>
181 * <schema id="org.gtk.Test">
183 * <key name="key-with-range" type="i">
184 * <range min="1" max="100"/>
185 * <default>10</default>
186 * </key>
188 * <key name="key-with-choices" type="s">
189 * <choices>
190 * <choice value='Elisabeth'/>
191 * <choice value='Annabeth'/>
192 * <choice value='Joe'/>
193 * </choices>
194 * <aliases>
195 * <alias value='Anna' target='Annabeth'/>
196 * <alias value='Beth' target='Elisabeth'/>
197 * </aliases>
198 * <default>'Joe'</default>
199 * </key>
201 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
202 * <default>'first'</default>
203 * </key>
205 * <key name='flags-key' flags='org.gtk.Test.myflags'>
206 * <default>["flag1","flag2"]</default>
207 * </key>
208 * </schema>
209 * </schemalist>
210 * ]|
212 * ## Vendor overrides
214 * Default values are defined in the schemas that get installed by
215 * an application. Sometimes, it is necessary for a vendor or distributor
216 * to adjust these defaults. Since patching the XML source for the schema
217 * is inconvenient and error-prone,
218 * [glib-compile-schemas][glib-compile-schemas] reads so-called vendor
219 * override' files. These are keyfiles in the same directory as the XML
220 * schema sources which can override default values. The schema id serves
221 * as the group name in the key file, and the values are expected in
222 * serialized GVariant form, as in the following example:
223 * |[
224 * [org.gtk.Example]
225 * key1='string'
226 * key2=1.5
227 * ]|
229 * glib-compile-schemas expects schema files to have the extension
230 * `.gschema.override`.
232 * ## Binding
234 * A very convenient feature of GSettings lets you bind #GObject properties
235 * directly to settings, using g_settings_bind(). Once a GObject property
236 * has been bound to a setting, changes on either side are automatically
237 * propagated to the other side. GSettings handles details like mapping
238 * between GObject and GVariant types, and preventing infinite cycles.
240 * This makes it very easy to hook up a preferences dialog to the
241 * underlying settings. To make this even more convenient, GSettings
242 * looks for a boolean property with the name "sensitivity" and
243 * automatically binds it to the writability of the bound setting.
244 * If this 'magic' gets in the way, it can be suppressed with the
245 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
247 * ## Relocatable schemas # {#gsettings-relocatable}
249 * A relocatable schema is one with no `path` attribute specified on its
250 * <schema> element. By using g_settings_new_with_path(), a #GSettings object
251 * can be instantiated for a relocatable schema, assigning a path to the
252 * instance. Paths passed to g_settings_new_with_path() will typically be
253 * constructed dynamically from a constant prefix plus some form of instance
254 * identifier; but they must still be valid GSettings paths. Paths could also
255 * be constant and used with a globally installed schema originating from a
256 * dependency library.
258 * For example, a relocatable schema could be used to store geometry information
259 * for different windows in an application. If the schema ID was
260 * `org.foo.MyApp.Window`, it could be instantiated for paths
261 * `/org/foo/MyApp/main/`, `/org/foo/MyApp/document-1/`,
262 * `/org/foo/MyApp/document-2/`, etc. If any of the paths are well-known
263 * they can be specified as <child> elements in the parent schema, e.g.:
264 * |[
265 * <schema id="org.foo.MyApp" path="/org/foo/MyApp/">
266 * <child name="main" schema="org.foo.MyApp.Window"/>
267 * </schema>
268 * ]|
270 * ## Build system integration # {#gsettings-build-system}
272 * GSettings comes with autotools integration to simplify compiling and
273 * installing schemas. To add GSettings support to an application, add the
274 * following to your `configure.ac`:
275 * |[
276 * GLIB_GSETTINGS
277 * ]|
279 * In the appropriate `Makefile.am`, use the following snippet to compile and
280 * install the named schema:
281 * |[
282 * gsettings_SCHEMAS = org.foo.MyApp.gschema.xml
283 * EXTRA_DIST = $(gsettings_SCHEMAS)
285 * @GSETTINGS_RULES@
286 * ]|
288 * No changes are needed to the build system to mark a schema XML file for
289 * translation. Assuming it sets the `gettext-domain` attribute, a schema may
290 * be marked for translation by adding it to `POTFILES.in`, assuming gettext
291 * 0.19 is in use (the preferred method for translation):
292 * |[
293 * data/org.foo.MyApp.gschema.xml
294 * ]|
296 * Alternatively, if intltool 0.50.1 is in use:
297 * |[
298 * [type: gettext/gsettings]data/org.foo.MyApp.gschema.xml
299 * ]|
301 * GSettings will use gettext to look up translations for the <summary> and
302 * <description> elements, and also any <default> elements which have a `l10n`
303 * attribute set. Translations must not be included in the `.gschema.xml` file
304 * by the build system, for example by using intltool XML rules with a
305 * `.gschema.xml.in` template.
307 * If an enumerated type defined in a C header file is to be used in a GSettings
308 * schema, it can either be defined manually using an <enum> element in the
309 * schema XML, or it can be extracted automatically from the C header. This
310 * approach is preferred, as it ensures the two representations are always
311 * synchronised. To do so, add the following to the relevant `Makefile.am`:
312 * |[
313 * gsettings_ENUM_NAMESPACE = org.foo.MyApp
314 * gsettings_ENUM_FILES = my-app-enums.h my-app-misc.h
315 * ]|
317 * `gsettings_ENUM_NAMESPACE` specifies the schema namespace for the enum files,
318 * which are specified in `gsettings_ENUM_FILES`. This will generate a
319 * `org.foo.MyApp.enums.xml` file containing the extracted enums, which will be
320 * automatically included in the schema compilation, install and uninstall
321 * rules. It should not be committed to version control or included in
322 * `EXTRA_DIST`.
326 * GSettings:
328 * #GSettings is an opaque data structure and can only be accessed
329 * using the following functions.
332 struct _GSettingsPrivate
334 /* where the signals go... */
335 GMainContext *main_context;
337 GSettingsBackend *backend;
338 GSettingsSchema *schema;
339 gchar *path;
341 GDelayedSettingsBackend *delayed;
344 enum
346 PROP_0,
347 PROP_SCHEMA,
348 PROP_SCHEMA_ID,
349 PROP_BACKEND,
350 PROP_PATH,
351 PROP_HAS_UNAPPLIED,
352 PROP_DELAY_APPLY
355 enum
357 SIGNAL_WRITABLE_CHANGE_EVENT,
358 SIGNAL_WRITABLE_CHANGED,
359 SIGNAL_CHANGE_EVENT,
360 SIGNAL_CHANGED,
361 N_SIGNALS
364 static guint g_settings_signals[N_SIGNALS];
366 G_DEFINE_TYPE_WITH_PRIVATE (GSettings, g_settings, G_TYPE_OBJECT)
368 /* Signals {{{1 */
369 static gboolean
370 g_settings_real_change_event (GSettings *settings,
371 const GQuark *keys,
372 gint n_keys)
374 gint i;
376 if (keys == NULL)
377 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
379 for (i = 0; i < n_keys; i++)
381 const gchar *key = g_quark_to_string (keys[i]);
383 if (g_str_has_suffix (key, "/"))
384 continue;
386 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED], keys[i], key);
389 return FALSE;
392 static gboolean
393 g_settings_real_writable_change_event (GSettings *settings,
394 GQuark key)
396 const GQuark *keys = &key;
397 gint n_keys = 1;
398 gint i;
400 if (key == 0)
401 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
403 for (i = 0; i < n_keys; i++)
405 const gchar *key = g_quark_to_string (keys[i]);
407 if (g_str_has_suffix (key, "/"))
408 continue;
410 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keys[i], key);
413 return FALSE;
416 static void
417 settings_backend_changed (GObject *target,
418 GSettingsBackend *backend,
419 const gchar *key,
420 gpointer origin_tag)
422 GSettings *settings = G_SETTINGS (target);
423 gboolean ignore_this;
424 gint i;
426 /* We used to assert here:
428 * settings->priv->backend == backend
430 * but it could be the case that a notification is queued for delivery
431 * while someone calls g_settings_delay() (which changes the backend).
433 * Since the delay backend would just pass that straight through
434 * anyway, it doesn't make sense to try to detect this case.
435 * Therefore, we just accept it.
438 for (i = 0; key[i] == settings->priv->path[i]; i++);
440 if (settings->priv->path[i] == '\0' &&
441 g_settings_schema_has_key (settings->priv->schema, key + i))
443 GQuark quark;
445 quark = g_quark_from_string (key + i);
446 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
447 0, &quark, 1, &ignore_this);
451 static void
452 settings_backend_path_changed (GObject *target,
453 GSettingsBackend *backend,
454 const gchar *path,
455 gpointer origin_tag)
457 GSettings *settings = G_SETTINGS (target);
458 gboolean ignore_this;
460 if (g_str_has_prefix (settings->priv->path, path))
461 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
462 0, NULL, 0, &ignore_this);
465 static void
466 settings_backend_keys_changed (GObject *target,
467 GSettingsBackend *backend,
468 const gchar *path,
469 gpointer origin_tag,
470 const gchar * const *items)
472 GSettings *settings = G_SETTINGS (target);
473 gboolean ignore_this;
474 gint i;
476 for (i = 0; settings->priv->path[i] &&
477 settings->priv->path[i] == path[i]; i++);
479 if (path[i] == '\0')
481 GQuark quarks[256];
482 gint j, l = 0;
484 for (j = 0; items[j]; j++)
486 const gchar *item = items[j];
487 gint k;
489 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
491 if (settings->priv->path[i + k] == '\0' &&
492 g_settings_schema_has_key (settings->priv->schema, item + k))
493 quarks[l++] = g_quark_from_string (item + k);
495 /* "256 quarks ought to be enough for anybody!"
496 * If this bites you, I'm sorry. Please file a bug.
498 g_assert (l < 256);
501 if (l > 0)
502 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
503 0, quarks, l, &ignore_this);
507 static void
508 settings_backend_writable_changed (GObject *target,
509 GSettingsBackend *backend,
510 const gchar *key)
512 GSettings *settings = G_SETTINGS (target);
513 gboolean ignore_this;
514 gint i;
516 for (i = 0; key[i] == settings->priv->path[i]; i++);
518 if (settings->priv->path[i] == '\0' &&
519 g_settings_schema_has_key (settings->priv->schema, key + i))
520 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
521 0, g_quark_from_string (key + i), &ignore_this);
524 static void
525 settings_backend_path_writable_changed (GObject *target,
526 GSettingsBackend *backend,
527 const gchar *path)
529 GSettings *settings = G_SETTINGS (target);
530 gboolean ignore_this;
532 if (g_str_has_prefix (settings->priv->path, path))
533 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
534 0, (GQuark) 0, &ignore_this);
537 /* Properties, Construction, Destruction {{{1 */
538 static void
539 g_settings_set_property (GObject *object,
540 guint prop_id,
541 const GValue *value,
542 GParamSpec *pspec)
544 GSettings *settings = G_SETTINGS (object);
546 switch (prop_id)
548 case PROP_SCHEMA:
550 GSettingsSchema *schema;
552 schema = g_value_dup_boxed (value);
554 /* we receive a set_property() call for "settings-schema" even
555 * if it was not specified (ie: with NULL value). ->schema
556 * could already be set at this point (ie: via "schema-id").
557 * check for NULL to avoid clobbering the existing value.
559 if (schema != NULL)
561 g_assert (settings->priv->schema == NULL);
562 settings->priv->schema = schema;
565 break;
567 case PROP_SCHEMA_ID:
569 const gchar *schema_id;
571 schema_id = g_value_get_string (value);
573 /* we receive a set_property() call for both "schema" and
574 * "schema-id", even if they are not set. Hopefully only one of
575 * them is non-NULL.
577 if (schema_id != NULL)
579 GSettingsSchemaSource *default_source;
581 g_assert (settings->priv->schema == NULL);
582 default_source = g_settings_schema_source_get_default ();
584 if (default_source == NULL)
585 g_error ("No GSettings schemas are installed on the system");
587 settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
589 if (settings->priv->schema == NULL)
590 g_error ("Settings schema '%s' is not installed\n", schema_id);
593 break;
595 case PROP_PATH:
596 settings->priv->path = g_value_dup_string (value);
597 break;
599 case PROP_BACKEND:
600 settings->priv->backend = g_value_dup_object (value);
601 break;
603 default:
604 g_assert_not_reached ();
608 static void
609 g_settings_get_property (GObject *object,
610 guint prop_id,
611 GValue *value,
612 GParamSpec *pspec)
614 GSettings *settings = G_SETTINGS (object);
616 switch (prop_id)
618 case PROP_SCHEMA:
619 g_value_set_boxed (value, settings->priv->schema);
620 break;
622 case PROP_SCHEMA_ID:
623 g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
624 break;
626 case PROP_BACKEND:
627 g_value_set_object (value, settings->priv->backend);
628 break;
630 case PROP_PATH:
631 g_value_set_string (value, settings->priv->path);
632 break;
634 case PROP_HAS_UNAPPLIED:
635 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
636 break;
638 case PROP_DELAY_APPLY:
639 g_value_set_boolean (value, settings->priv->delayed != NULL);
640 break;
642 default:
643 g_assert_not_reached ();
647 static const GSettingsListenerVTable listener_vtable = {
648 settings_backend_changed,
649 settings_backend_path_changed,
650 settings_backend_keys_changed,
651 settings_backend_writable_changed,
652 settings_backend_path_writable_changed
655 static void
656 g_settings_constructed (GObject *object)
658 GSettings *settings = G_SETTINGS (object);
659 const gchar *schema_path;
661 schema_path = g_settings_schema_get_path (settings->priv->schema);
663 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
664 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
665 g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
667 if (settings->priv->path == NULL)
669 if (schema_path == NULL)
670 g_error ("attempting to create schema '%s' without a path",
671 g_settings_schema_get_id (settings->priv->schema));
673 settings->priv->path = g_strdup (schema_path);
676 if (settings->priv->backend == NULL)
677 settings->priv->backend = g_settings_backend_get_default ();
679 g_settings_backend_watch (settings->priv->backend,
680 &listener_vtable, G_OBJECT (settings),
681 settings->priv->main_context);
682 g_settings_backend_subscribe (settings->priv->backend,
683 settings->priv->path);
686 static void
687 g_settings_finalize (GObject *object)
689 GSettings *settings = G_SETTINGS (object);
691 g_settings_backend_unsubscribe (settings->priv->backend,
692 settings->priv->path);
693 g_main_context_unref (settings->priv->main_context);
694 g_object_unref (settings->priv->backend);
695 g_settings_schema_unref (settings->priv->schema);
696 g_free (settings->priv->path);
698 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
701 static void
702 g_settings_init (GSettings *settings)
704 settings->priv = g_settings_get_instance_private (settings);
705 settings->priv->main_context = g_main_context_ref_thread_default ();
708 static void
709 g_settings_class_init (GSettingsClass *class)
711 GObjectClass *object_class = G_OBJECT_CLASS (class);
713 class->writable_change_event = g_settings_real_writable_change_event;
714 class->change_event = g_settings_real_change_event;
716 object_class->set_property = g_settings_set_property;
717 object_class->get_property = g_settings_get_property;
718 object_class->constructed = g_settings_constructed;
719 object_class->finalize = g_settings_finalize;
722 * GSettings::changed:
723 * @settings: the object on which the signal was emitted
724 * @key: the name of the key that changed
726 * The "changed" signal is emitted when a key has potentially changed.
727 * You should call one of the g_settings_get() calls to check the new
728 * value.
730 * This signal supports detailed connections. You can connect to the
731 * detailed signal "changed::x" in order to only receive callbacks
732 * when key "x" changes.
734 * Note that @settings only emits this signal if you have read @key at
735 * least once while a signal handler was already connected for @key.
737 g_settings_signals[SIGNAL_CHANGED] =
738 g_signal_new (I_("changed"), G_TYPE_SETTINGS,
739 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
740 G_STRUCT_OFFSET (GSettingsClass, changed),
741 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
742 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
745 * GSettings::change-event:
746 * @settings: the object on which the signal was emitted
747 * @keys: (array length=n_keys) (element-type GQuark) (nullable):
748 * an array of #GQuarks for the changed keys, or %NULL
749 * @n_keys: the length of the @keys array, or 0
751 * The "change-event" signal is emitted once per change event that
752 * affects this settings object. You should connect to this signal
753 * only if you are interested in viewing groups of changes before they
754 * are split out into multiple emissions of the "changed" signal.
755 * For most use cases it is more appropriate to use the "changed" signal.
757 * In the event that the change event applies to one or more specified
758 * keys, @keys will be an array of #GQuark of length @n_keys. In the
759 * event that the change event applies to the #GSettings object as a
760 * whole (ie: potentially every key has been changed) then @keys will
761 * be %NULL and @n_keys will be 0.
763 * The default handler for this signal invokes the "changed" signal
764 * for each affected key. If any other connected handler returns
765 * %TRUE then this default functionality will be suppressed.
767 * Returns: %TRUE to stop other handlers from being invoked for the
768 * event. FALSE to propagate the event further.
770 g_settings_signals[SIGNAL_CHANGE_EVENT] =
771 g_signal_new (I_("change-event"), G_TYPE_SETTINGS,
772 G_SIGNAL_RUN_LAST,
773 G_STRUCT_OFFSET (GSettingsClass, change_event),
774 g_signal_accumulator_true_handled, NULL,
775 NULL,
776 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
779 * GSettings::writable-changed:
780 * @settings: the object on which the signal was emitted
781 * @key: the key
783 * The "writable-changed" signal is emitted when the writability of a
784 * key has potentially changed. You should call
785 * g_settings_is_writable() in order to determine the new status.
787 * This signal supports detailed connections. You can connect to the
788 * detailed signal "writable-changed::x" in order to only receive
789 * callbacks when the writability of "x" changes.
791 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
792 g_signal_new (I_("writable-changed"), G_TYPE_SETTINGS,
793 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
794 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
795 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
796 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
799 * GSettings::writable-change-event:
800 * @settings: the object on which the signal was emitted
801 * @key: the quark of the key, or 0
803 * The "writable-change-event" signal is emitted once per writability
804 * change event that affects this settings object. You should connect
805 * to this signal if you are interested in viewing groups of changes
806 * before they are split out into multiple emissions of the
807 * "writable-changed" signal. For most use cases it is more
808 * appropriate to use the "writable-changed" signal.
810 * In the event that the writability change applies only to a single
811 * key, @key will be set to the #GQuark for that key. In the event
812 * that the writability change affects the entire settings object,
813 * @key will be 0.
815 * The default handler for this signal invokes the "writable-changed"
816 * and "changed" signals for each affected key. This is done because
817 * changes in writability might also imply changes in value (if for
818 * example, a new mandatory setting is introduced). If any other
819 * connected handler returns %TRUE then this default functionality
820 * will be suppressed.
822 * Returns: %TRUE to stop other handlers from being invoked for the
823 * event. FALSE to propagate the event further.
825 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
826 g_signal_new (I_("writable-change-event"), G_TYPE_SETTINGS,
827 G_SIGNAL_RUN_LAST,
828 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
829 g_signal_accumulator_true_handled, NULL,
830 NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
833 * GSettings:backend:
835 * The name of the context that the settings are stored in.
837 g_object_class_install_property (object_class, PROP_BACKEND,
838 g_param_spec_object ("backend",
839 P_("GSettingsBackend"),
840 P_("The GSettingsBackend for this settings object"),
841 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
842 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
845 * GSettings:settings-schema:
847 * The #GSettingsSchema describing the types of keys for this
848 * #GSettings object.
850 * Ideally, this property would be called 'schema'. #GSettingsSchema
851 * has only existed since version 2.32, however, and before then the
852 * 'schema' property was used to refer to the ID of the schema rather
853 * than the schema itself. Take care.
855 g_object_class_install_property (object_class, PROP_SCHEMA,
856 g_param_spec_boxed ("settings-schema",
857 P_("schema"),
858 P_("The GSettingsSchema for this settings object"),
859 G_TYPE_SETTINGS_SCHEMA,
860 G_PARAM_CONSTRUCT_ONLY |
861 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
864 * GSettings:schema:
866 * The name of the schema that describes the types of keys
867 * for this #GSettings object.
869 * The type of this property is *not* #GSettingsSchema.
870 * #GSettingsSchema has only existed since version 2.32 and
871 * unfortunately this name was used in previous versions to refer to
872 * the schema ID rather than the schema itself. Take care to use the
873 * 'settings-schema' property if you wish to pass in a
874 * #GSettingsSchema.
876 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
877 * version, this property may instead refer to a #GSettingsSchema.
879 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
880 g_param_spec_string ("schema",
881 P_("Schema name"),
882 P_("The name of the schema for this settings object"),
883 NULL,
884 G_PARAM_CONSTRUCT_ONLY |
885 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
888 * GSettings:schema-id:
890 * The name of the schema that describes the types of keys
891 * for this #GSettings object.
893 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
894 g_param_spec_string ("schema-id",
895 P_("Schema name"),
896 P_("The name of the schema for this settings object"),
897 NULL,
898 G_PARAM_CONSTRUCT_ONLY |
899 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
902 * GSettings:path:
904 * The path within the backend where the settings are stored.
906 g_object_class_install_property (object_class, PROP_PATH,
907 g_param_spec_string ("path",
908 P_("Base path"),
909 P_("The path within the backend where the settings are"),
910 NULL,
911 G_PARAM_CONSTRUCT_ONLY |
912 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
915 * GSettings:has-unapplied:
917 * If this property is %TRUE, the #GSettings object has outstanding
918 * changes that will be applied when g_settings_apply() is called.
920 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
921 g_param_spec_boolean ("has-unapplied",
922 P_("Has unapplied changes"),
923 P_("TRUE if there are outstanding changes to apply()"),
924 FALSE,
925 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
928 * GSettings:delay-apply:
930 * Whether the #GSettings object is in 'delay-apply' mode. See
931 * g_settings_delay() for details.
933 * Since: 2.28
935 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
936 g_param_spec_boolean ("delay-apply",
937 P_("Delay-apply mode"),
938 P_("Whether this settings object is in “delay-apply” mode"),
939 FALSE,
940 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
943 /* Construction (new, new_with_path, etc.) {{{1 */
945 * g_settings_new:
946 * @schema_id: the id of the schema
948 * Creates a new #GSettings object with the schema specified by
949 * @schema_id.
951 * Signals on the newly created #GSettings object will be dispatched
952 * via the thread-default #GMainContext in effect at the time of the
953 * call to g_settings_new(). The new #GSettings will hold a reference
954 * on the context. See g_main_context_push_thread_default().
956 * Returns: a new #GSettings object
958 * Since: 2.26
960 GSettings *
961 g_settings_new (const gchar *schema_id)
963 g_return_val_if_fail (schema_id != NULL, NULL);
965 return g_object_new (G_TYPE_SETTINGS,
966 "schema-id", schema_id,
967 NULL);
970 static gboolean
971 path_is_valid (const gchar *path)
973 if (!path)
974 return FALSE;
976 if (path[0] != '/')
977 return FALSE;
979 if (!g_str_has_suffix (path, "/"))
980 return FALSE;
982 return strstr (path, "//") == NULL;
986 * g_settings_new_with_path:
987 * @schema_id: the id of the schema
988 * @path: the path to use
990 * Creates a new #GSettings object with the relocatable schema specified
991 * by @schema_id and a given path.
993 * You only need to do this if you want to directly create a settings
994 * object with a schema that doesn't have a specified path of its own.
995 * That's quite rare.
997 * It is a programmer error to call this function for a schema that
998 * has an explicitly specified path.
1000 * It is a programmer error if @path is not a valid path. A valid path
1001 * begins and ends with '/' and does not contain two consecutive '/'
1002 * characters.
1004 * Returns: a new #GSettings object
1006 * Since: 2.26
1008 GSettings *
1009 g_settings_new_with_path (const gchar *schema_id,
1010 const gchar *path)
1012 g_return_val_if_fail (schema_id != NULL, NULL);
1013 g_return_val_if_fail (path_is_valid (path), NULL);
1015 return g_object_new (G_TYPE_SETTINGS,
1016 "schema-id", schema_id,
1017 "path", path,
1018 NULL);
1022 * g_settings_new_with_backend:
1023 * @schema_id: the id of the schema
1024 * @backend: the #GSettingsBackend to use
1026 * Creates a new #GSettings object with the schema specified by
1027 * @schema_id and a given #GSettingsBackend.
1029 * Creating a #GSettings object with a different backend allows accessing
1030 * settings from a database other than the usual one. For example, it may make
1031 * sense to pass a backend corresponding to the "defaults" settings database on
1032 * the system to get a settings object that modifies the system default
1033 * settings instead of the settings for this user.
1035 * Returns: a new #GSettings object
1037 * Since: 2.26
1039 GSettings *
1040 g_settings_new_with_backend (const gchar *schema_id,
1041 GSettingsBackend *backend)
1043 g_return_val_if_fail (schema_id != NULL, NULL);
1044 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
1046 return g_object_new (G_TYPE_SETTINGS,
1047 "schema-id", schema_id,
1048 "backend", backend,
1049 NULL);
1053 * g_settings_new_with_backend_and_path:
1054 * @schema_id: the id of the schema
1055 * @backend: the #GSettingsBackend to use
1056 * @path: the path to use
1058 * Creates a new #GSettings object with the schema specified by
1059 * @schema_id and a given #GSettingsBackend and path.
1061 * This is a mix of g_settings_new_with_backend() and
1062 * g_settings_new_with_path().
1064 * Returns: a new #GSettings object
1066 * Since: 2.26
1068 GSettings *
1069 g_settings_new_with_backend_and_path (const gchar *schema_id,
1070 GSettingsBackend *backend,
1071 const gchar *path)
1073 g_return_val_if_fail (schema_id != NULL, NULL);
1074 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
1075 g_return_val_if_fail (path_is_valid (path), NULL);
1077 return g_object_new (G_TYPE_SETTINGS,
1078 "schema-id", schema_id,
1079 "backend", backend,
1080 "path", path,
1081 NULL);
1085 * g_settings_new_full:
1086 * @schema: a #GSettingsSchema
1087 * @backend: (nullable): a #GSettingsBackend
1088 * @path: (nullable): the path to use
1090 * Creates a new #GSettings object with a given schema, backend and
1091 * path.
1093 * It should be extremely rare that you ever want to use this function.
1094 * It is made available for advanced use-cases (such as plugin systems
1095 * that want to provide access to schemas loaded from custom locations,
1096 * etc).
1098 * At the most basic level, a #GSettings object is a pure composition of
1099 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
1100 * backend, and a #GMainContext to which signals are dispatched.
1102 * This constructor therefore gives you full control over constructing
1103 * #GSettings instances. The first 3 parameters are given directly as
1104 * @schema, @backend and @path, and the main context is taken from the
1105 * thread-default (as per g_settings_new()).
1107 * If @backend is %NULL then the default backend is used.
1109 * If @path is %NULL then the path from the schema is used. It is an
1110 * error if @path is %NULL and the schema has no path of its own or if
1111 * @path is non-%NULL and not equal to the path that the schema does
1112 * have.
1114 * Returns: a new #GSettings object
1116 * Since: 2.32
1118 GSettings *
1119 g_settings_new_full (GSettingsSchema *schema,
1120 GSettingsBackend *backend,
1121 const gchar *path)
1123 g_return_val_if_fail (schema != NULL, NULL);
1124 g_return_val_if_fail (backend == NULL || G_IS_SETTINGS_BACKEND (backend), NULL);
1125 g_return_val_if_fail (path == NULL || path_is_valid (path), NULL);
1127 return g_object_new (G_TYPE_SETTINGS,
1128 "settings-schema", schema,
1129 "backend", backend,
1130 "path", path,
1131 NULL);
1134 /* Internal read/write utilities {{{1 */
1135 static gboolean
1136 g_settings_write_to_backend (GSettings *settings,
1137 GSettingsSchemaKey *key,
1138 GVariant *value)
1140 gboolean success;
1141 gchar *path;
1143 path = g_strconcat (settings->priv->path, key->name, NULL);
1144 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1145 g_free (path);
1147 return success;
1150 static GVariant *
1151 g_settings_read_from_backend (GSettings *settings,
1152 GSettingsSchemaKey *key,
1153 gboolean user_value_only,
1154 gboolean default_value)
1156 GVariant *value;
1157 GVariant *fixup;
1158 gchar *path;
1160 path = g_strconcat (settings->priv->path, key->name, NULL);
1161 if (user_value_only)
1162 value = g_settings_backend_read_user_value (settings->priv->backend, path, key->type);
1163 else
1164 value = g_settings_backend_read (settings->priv->backend, path, key->type, default_value);
1165 g_free (path);
1167 if (value != NULL)
1169 fixup = g_settings_schema_key_range_fixup (key, value);
1170 g_variant_unref (value);
1172 else
1173 fixup = NULL;
1175 return fixup;
1178 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1180 * g_settings_get_value:
1181 * @settings: a #GSettings object
1182 * @key: the key to get the value for
1184 * Gets the value that is stored in @settings for @key.
1186 * It is a programmer error to give a @key that isn't contained in the
1187 * schema for @settings.
1189 * Returns: a new #GVariant
1191 * Since: 2.26
1193 GVariant *
1194 g_settings_get_value (GSettings *settings,
1195 const gchar *key)
1197 GSettingsSchemaKey skey;
1198 GVariant *value;
1200 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1201 g_return_val_if_fail (key != NULL, NULL);
1203 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1204 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1206 if (value == NULL)
1207 value = g_settings_schema_key_get_translated_default (&skey);
1209 if (value == NULL)
1210 value = g_variant_ref (skey.default_value);
1212 g_settings_schema_key_clear (&skey);
1214 return value;
1218 * g_settings_get_user_value:
1219 * @settings: a #GSettings object
1220 * @key: the key to get the user value for
1222 * Checks the "user value" of a key, if there is one.
1224 * The user value of a key is the last value that was set by the user.
1226 * After calling g_settings_reset() this function should always return
1227 * %NULL (assuming something is not wrong with the system
1228 * configuration).
1230 * It is possible that g_settings_get_value() will return a different
1231 * value than this function. This can happen in the case that the user
1232 * set a value for a key that was subsequently locked down by the system
1233 * administrator -- this function will return the user's old value.
1235 * This function may be useful for adding a "reset" option to a UI or
1236 * for providing indication that a particular value has been changed.
1238 * It is a programmer error to give a @key that isn't contained in the
1239 * schema for @settings.
1241 * Returns: (nullable) (transfer full): the user's value, if set
1243 * Since: 2.40
1245 GVariant *
1246 g_settings_get_user_value (GSettings *settings,
1247 const gchar *key)
1249 GSettingsSchemaKey skey;
1250 GVariant *value;
1252 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1253 g_return_val_if_fail (key != NULL, NULL);
1255 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1256 value = g_settings_read_from_backend (settings, &skey, TRUE, FALSE);
1257 g_settings_schema_key_clear (&skey);
1259 return value;
1263 * g_settings_get_default_value:
1264 * @settings: a #GSettings object
1265 * @key: the key to get the default value for
1267 * Gets the "default value" of a key.
1269 * This is the value that would be read if g_settings_reset() were to be
1270 * called on the key.
1272 * Note that this may be a different value than returned by
1273 * g_settings_schema_key_get_default_value() if the system administrator
1274 * has provided a default value.
1276 * Comparing the return values of g_settings_get_default_value() and
1277 * g_settings_get_value() is not sufficient for determining if a value
1278 * has been set because the user may have explicitly set the value to
1279 * something that happens to be equal to the default. The difference
1280 * here is that if the default changes in the future, the user's key
1281 * will still be set.
1283 * This function may be useful for adding an indication to a UI of what
1284 * the default value was before the user set it.
1286 * It is a programmer error to give a @key that isn't contained in the
1287 * schema for @settings.
1289 * Returns: (nullable) (transfer full): the default value
1291 * Since: 2.40
1293 GVariant *
1294 g_settings_get_default_value (GSettings *settings,
1295 const gchar *key)
1297 GSettingsSchemaKey skey;
1298 GVariant *value;
1300 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1301 g_return_val_if_fail (key != NULL, NULL);
1303 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1304 value = g_settings_read_from_backend (settings, &skey, FALSE, TRUE);
1306 if (value == NULL)
1307 value = g_settings_schema_key_get_translated_default (&skey);
1309 if (value == NULL)
1310 value = g_variant_ref (skey.default_value);
1312 g_settings_schema_key_clear (&skey);
1314 return value;
1318 * g_settings_get_enum:
1319 * @settings: a #GSettings object
1320 * @key: the key to get the value for
1322 * Gets the value that is stored in @settings for @key and converts it
1323 * to the enum value that it represents.
1325 * In order to use this function the type of the value must be a string
1326 * and it must be marked in the schema file as an enumerated type.
1328 * It is a programmer error to give a @key that isn't contained in the
1329 * schema for @settings or is not marked as an enumerated type.
1331 * If the value stored in the configuration database is not a valid
1332 * value for the enumerated type then this function will return the
1333 * default value.
1335 * Returns: the enum value
1337 * Since: 2.26
1339 gint
1340 g_settings_get_enum (GSettings *settings,
1341 const gchar *key)
1343 GSettingsSchemaKey skey;
1344 GVariant *value;
1345 gint result;
1347 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1348 g_return_val_if_fail (key != NULL, -1);
1350 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1352 if (!skey.is_enum)
1354 g_critical ("g_settings_get_enum() called on key '%s' which is not "
1355 "associated with an enumerated type", skey.name);
1356 g_settings_schema_key_clear (&skey);
1357 return -1;
1360 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1362 if (value == NULL)
1363 value = g_settings_schema_key_get_translated_default (&skey);
1365 if (value == NULL)
1366 value = g_variant_ref (skey.default_value);
1368 result = g_settings_schema_key_to_enum (&skey, value);
1369 g_settings_schema_key_clear (&skey);
1370 g_variant_unref (value);
1372 return result;
1376 * g_settings_set_enum:
1377 * @settings: a #GSettings object
1378 * @key: a key, within @settings
1379 * @value: an enumerated value
1381 * Looks up the enumerated type nick for @value and writes it to @key,
1382 * within @settings.
1384 * It is a programmer error to give a @key that isn't contained in the
1385 * schema for @settings or is not marked as an enumerated type, or for
1386 * @value not to be a valid value for the named type.
1388 * After performing the write, accessing @key directly with
1389 * g_settings_get_string() will return the 'nick' associated with
1390 * @value.
1392 * Returns: %TRUE, if the set succeeds
1394 gboolean
1395 g_settings_set_enum (GSettings *settings,
1396 const gchar *key,
1397 gint value)
1399 GSettingsSchemaKey skey;
1400 GVariant *variant;
1401 gboolean success;
1403 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1404 g_return_val_if_fail (key != NULL, FALSE);
1406 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1408 if (!skey.is_enum)
1410 g_critical ("g_settings_set_enum() called on key '%s' which is not "
1411 "associated with an enumerated type", skey.name);
1412 return FALSE;
1415 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1417 g_critical ("g_settings_set_enum(): invalid enum value %d for key '%s' "
1418 "in schema '%s'. Doing nothing.", value, skey.name,
1419 g_settings_schema_get_id (skey.schema));
1420 g_settings_schema_key_clear (&skey);
1421 return FALSE;
1424 success = g_settings_write_to_backend (settings, &skey, variant);
1425 g_settings_schema_key_clear (&skey);
1427 return success;
1431 * g_settings_get_flags:
1432 * @settings: a #GSettings object
1433 * @key: the key to get the value for
1435 * Gets the value that is stored in @settings for @key and converts it
1436 * to the flags value that it represents.
1438 * In order to use this function the type of the value must be an array
1439 * of strings and it must be marked in the schema file as an flags type.
1441 * It is a programmer error to give a @key that isn't contained in the
1442 * schema for @settings or is not marked as a flags type.
1444 * If the value stored in the configuration database is not a valid
1445 * value for the flags type then this function will return the default
1446 * value.
1448 * Returns: the flags value
1450 * Since: 2.26
1452 guint
1453 g_settings_get_flags (GSettings *settings,
1454 const gchar *key)
1456 GSettingsSchemaKey skey;
1457 GVariant *value;
1458 guint result;
1460 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1461 g_return_val_if_fail (key != NULL, -1);
1463 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1465 if (!skey.is_flags)
1467 g_critical ("g_settings_get_flags() called on key '%s' which is not "
1468 "associated with a flags type", skey.name);
1469 g_settings_schema_key_clear (&skey);
1470 return -1;
1473 value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1475 if (value == NULL)
1476 value = g_settings_schema_key_get_translated_default (&skey);
1478 if (value == NULL)
1479 value = g_variant_ref (skey.default_value);
1481 result = g_settings_schema_key_to_flags (&skey, value);
1482 g_settings_schema_key_clear (&skey);
1483 g_variant_unref (value);
1485 return result;
1489 * g_settings_set_flags:
1490 * @settings: a #GSettings object
1491 * @key: a key, within @settings
1492 * @value: a flags value
1494 * Looks up the flags type nicks for the bits specified by @value, puts
1495 * them in an array of strings and writes the array to @key, within
1496 * @settings.
1498 * It is a programmer error to give a @key that isn't contained in the
1499 * schema for @settings or is not marked as a flags type, or for @value
1500 * to contain any bits that are not value for the named type.
1502 * After performing the write, accessing @key directly with
1503 * g_settings_get_strv() will return an array of 'nicks'; one for each
1504 * bit in @value.
1506 * Returns: %TRUE, if the set succeeds
1508 gboolean
1509 g_settings_set_flags (GSettings *settings,
1510 const gchar *key,
1511 guint value)
1513 GSettingsSchemaKey skey;
1514 GVariant *variant;
1515 gboolean success;
1517 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1518 g_return_val_if_fail (key != NULL, FALSE);
1520 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1522 if (!skey.is_flags)
1524 g_critical ("g_settings_set_flags() called on key '%s' which is not "
1525 "associated with a flags type", skey.name);
1526 return FALSE;
1529 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1531 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1532 "for key '%s' in schema '%s'. Doing nothing.",
1533 value, skey.name, g_settings_schema_get_id (skey.schema));
1534 g_settings_schema_key_clear (&skey);
1535 return FALSE;
1538 success = g_settings_write_to_backend (settings, &skey, variant);
1539 g_settings_schema_key_clear (&skey);
1541 return success;
1545 * g_settings_set_value:
1546 * @settings: a #GSettings object
1547 * @key: the name of the key to set
1548 * @value: a #GVariant of the correct type
1550 * Sets @key in @settings to @value.
1552 * It is a programmer error to give a @key that isn't contained in the
1553 * schema for @settings or for @value to have the incorrect type, per
1554 * the schema.
1556 * If @value is floating then this function consumes the reference.
1558 * Returns: %TRUE if setting the key succeeded,
1559 * %FALSE if the key was not writable
1561 * Since: 2.26
1563 gboolean
1564 g_settings_set_value (GSettings *settings,
1565 const gchar *key,
1566 GVariant *value)
1568 GSettingsSchemaKey skey;
1569 gboolean success;
1571 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1572 g_return_val_if_fail (key != NULL, FALSE);
1574 g_variant_ref_sink (value);
1575 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1577 if (!g_settings_schema_key_type_check (&skey, value))
1579 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1580 key,
1581 g_settings_schema_get_id (settings->priv->schema),
1582 g_variant_type_peek_string (skey.type),
1583 g_variant_get_type_string (value));
1584 success = FALSE;
1586 else if (!g_settings_schema_key_range_check (&skey, value))
1588 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1589 "is outside of valid range",
1590 key,
1591 g_settings_schema_get_id (settings->priv->schema));
1592 success = FALSE;
1594 else
1596 success = g_settings_write_to_backend (settings, &skey, value);
1599 g_settings_schema_key_clear (&skey);
1600 g_variant_unref (value);
1602 return success;
1606 * g_settings_get:
1607 * @settings: a #GSettings object
1608 * @key: the key to get the value for
1609 * @format: a #GVariant format string
1610 * @...: arguments as per @format
1612 * Gets the value that is stored at @key in @settings.
1614 * A convenience function that combines g_settings_get_value() with
1615 * g_variant_get().
1617 * It is a programmer error to give a @key that isn't contained in the
1618 * schema for @settings or for the #GVariantType of @format to mismatch
1619 * the type given in the schema.
1621 * Since: 2.26
1623 void
1624 g_settings_get (GSettings *settings,
1625 const gchar *key,
1626 const gchar *format,
1627 ...)
1629 GVariant *value;
1630 va_list ap;
1632 value = g_settings_get_value (settings, key);
1634 if (strchr (format, '&'))
1636 g_critical ("%s: the format string may not contain '&' (key '%s' from schema '%s'). "
1637 "This call will probably stop working with a future version of glib.",
1638 G_STRFUNC, key, g_settings_schema_get_id (settings->priv->schema));
1641 va_start (ap, format);
1642 g_variant_get_va (value, format, NULL, &ap);
1643 va_end (ap);
1645 g_variant_unref (value);
1649 * g_settings_set:
1650 * @settings: a #GSettings object
1651 * @key: the name of the key to set
1652 * @format: a #GVariant format string
1653 * @...: arguments as per @format
1655 * Sets @key in @settings to @value.
1657 * A convenience function that combines g_settings_set_value() with
1658 * g_variant_new().
1660 * It is a programmer error to give a @key that isn't contained in the
1661 * schema for @settings or for the #GVariantType of @format to mismatch
1662 * the type given in the schema.
1664 * Returns: %TRUE if setting the key succeeded,
1665 * %FALSE if the key was not writable
1667 * Since: 2.26
1669 gboolean
1670 g_settings_set (GSettings *settings,
1671 const gchar *key,
1672 const gchar *format,
1673 ...)
1675 GVariant *value;
1676 va_list ap;
1678 va_start (ap, format);
1679 value = g_variant_new_va (format, NULL, &ap);
1680 va_end (ap);
1682 return g_settings_set_value (settings, key, value);
1686 * g_settings_get_mapped:
1687 * @settings: a #GSettings object
1688 * @key: the key to get the value for
1689 * @mapping: (scope call): the function to map the value in the
1690 * settings database to the value used by the application
1691 * @user_data: user data for @mapping
1693 * Gets the value that is stored at @key in @settings, subject to
1694 * application-level validation/mapping.
1696 * You should use this function when the application needs to perform
1697 * some processing on the value of the key (for example, parsing). The
1698 * @mapping function performs that processing. If the function
1699 * indicates that the processing was unsuccessful (due to a parse error,
1700 * for example) then the mapping is tried again with another value.
1702 * This allows a robust 'fall back to defaults' behaviour to be
1703 * implemented somewhat automatically.
1705 * The first value that is tried is the user's setting for the key. If
1706 * the mapping function fails to map this value, other values may be
1707 * tried in an unspecified order (system or site defaults, translated
1708 * schema default values, untranslated schema default values, etc).
1710 * If the mapping function fails for all possible values, one additional
1711 * attempt is made: the mapping function is called with a %NULL value.
1712 * If the mapping function still indicates failure at this point then
1713 * the application will be aborted.
1715 * The result parameter for the @mapping function is pointed to a
1716 * #gpointer which is initially set to %NULL. The same pointer is given
1717 * to each invocation of @mapping. The final value of that #gpointer is
1718 * what is returned by this function. %NULL is valid; it is returned
1719 * just as any other value would be.
1721 * Returns: (transfer full): the result, which may be %NULL
1723 gpointer
1724 g_settings_get_mapped (GSettings *settings,
1725 const gchar *key,
1726 GSettingsGetMapping mapping,
1727 gpointer user_data)
1729 gpointer result = NULL;
1730 GSettingsSchemaKey skey;
1731 GVariant *value;
1732 gboolean okay;
1734 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1735 g_return_val_if_fail (key != NULL, NULL);
1736 g_return_val_if_fail (mapping != NULL, NULL);
1738 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1740 if ((value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE)))
1742 okay = mapping (value, &result, user_data);
1743 g_variant_unref (value);
1744 if (okay) goto okay;
1747 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1749 okay = mapping (value, &result, user_data);
1750 g_variant_unref (value);
1751 if (okay) goto okay;
1754 if (mapping (skey.default_value, &result, user_data))
1755 goto okay;
1757 if (!mapping (NULL, &result, user_data))
1758 g_error ("The mapping function given to g_settings_get_mapped() for key "
1759 "'%s' in schema '%s' returned FALSE when given a NULL value.",
1760 key, g_settings_schema_get_id (settings->priv->schema));
1762 okay:
1763 g_settings_schema_key_clear (&skey);
1765 return result;
1768 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1770 * g_settings_get_string:
1771 * @settings: a #GSettings object
1772 * @key: the key to get the value for
1774 * Gets the value that is stored at @key in @settings.
1776 * A convenience variant of g_settings_get() for strings.
1778 * It is a programmer error to give a @key that isn't specified as
1779 * having a string type in the schema for @settings.
1781 * Returns: a newly-allocated string
1783 * Since: 2.26
1785 gchar *
1786 g_settings_get_string (GSettings *settings,
1787 const gchar *key)
1789 GVariant *value;
1790 gchar *result;
1792 value = g_settings_get_value (settings, key);
1793 result = g_variant_dup_string (value, NULL);
1794 g_variant_unref (value);
1796 return result;
1800 * g_settings_set_string:
1801 * @settings: a #GSettings object
1802 * @key: the name of the key to set
1803 * @value: the value to set it to
1805 * Sets @key in @settings to @value.
1807 * A convenience variant of g_settings_set() for strings.
1809 * It is a programmer error to give a @key that isn't specified as
1810 * having a string type in the schema for @settings.
1812 * Returns: %TRUE if setting the key succeeded,
1813 * %FALSE if the key was not writable
1815 * Since: 2.26
1817 gboolean
1818 g_settings_set_string (GSettings *settings,
1819 const gchar *key,
1820 const gchar *value)
1822 return g_settings_set_value (settings, key, g_variant_new_string (value));
1826 * g_settings_get_int:
1827 * @settings: a #GSettings object
1828 * @key: the key to get the value for
1830 * Gets the value that is stored at @key in @settings.
1832 * A convenience variant of g_settings_get() for 32-bit integers.
1834 * It is a programmer error to give a @key that isn't specified as
1835 * having a int32 type in the schema for @settings.
1837 * Returns: an integer
1839 * Since: 2.26
1841 gint
1842 g_settings_get_int (GSettings *settings,
1843 const gchar *key)
1845 GVariant *value;
1846 gint result;
1848 value = g_settings_get_value (settings, key);
1849 result = g_variant_get_int32 (value);
1850 g_variant_unref (value);
1852 return result;
1856 * g_settings_set_int:
1857 * @settings: a #GSettings object
1858 * @key: the name of the key to set
1859 * @value: the value to set it to
1861 * Sets @key in @settings to @value.
1863 * A convenience variant of g_settings_set() for 32-bit integers.
1865 * It is a programmer error to give a @key that isn't specified as
1866 * having a int32 type in the schema for @settings.
1868 * Returns: %TRUE if setting the key succeeded,
1869 * %FALSE if the key was not writable
1871 * Since: 2.26
1873 gboolean
1874 g_settings_set_int (GSettings *settings,
1875 const gchar *key,
1876 gint value)
1878 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1882 * g_settings_get_int64:
1883 * @settings: a #GSettings object
1884 * @key: the key to get the value for
1886 * Gets the value that is stored at @key in @settings.
1888 * A convenience variant of g_settings_get() for 64-bit integers.
1890 * It is a programmer error to give a @key that isn't specified as
1891 * having a int64 type in the schema for @settings.
1893 * Returns: a 64-bit integer
1895 * Since: 2.50
1897 gint64
1898 g_settings_get_int64 (GSettings *settings,
1899 const gchar *key)
1901 GVariant *value;
1902 gint64 result;
1904 value = g_settings_get_value (settings, key);
1905 result = g_variant_get_int64 (value);
1906 g_variant_unref (value);
1908 return result;
1912 * g_settings_set_int64:
1913 * @settings: a #GSettings object
1914 * @key: the name of the key to set
1915 * @value: the value to set it to
1917 * Sets @key in @settings to @value.
1919 * A convenience variant of g_settings_set() for 64-bit integers.
1921 * It is a programmer error to give a @key that isn't specified as
1922 * having a int64 type in the schema for @settings.
1924 * Returns: %TRUE if setting the key succeeded,
1925 * %FALSE if the key was not writable
1927 * Since: 2.50
1929 gboolean
1930 g_settings_set_int64 (GSettings *settings,
1931 const gchar *key,
1932 gint64 value)
1934 return g_settings_set_value (settings, key, g_variant_new_int64 (value));
1938 * g_settings_get_uint:
1939 * @settings: a #GSettings object
1940 * @key: the key to get the value for
1942 * Gets the value that is stored at @key in @settings.
1944 * A convenience variant of g_settings_get() for 32-bit unsigned
1945 * integers.
1947 * It is a programmer error to give a @key that isn't specified as
1948 * having a uint32 type in the schema for @settings.
1950 * Returns: an unsigned integer
1952 * Since: 2.30
1954 guint
1955 g_settings_get_uint (GSettings *settings,
1956 const gchar *key)
1958 GVariant *value;
1959 guint result;
1961 value = g_settings_get_value (settings, key);
1962 result = g_variant_get_uint32 (value);
1963 g_variant_unref (value);
1965 return result;
1969 * g_settings_set_uint:
1970 * @settings: a #GSettings object
1971 * @key: the name of the key to set
1972 * @value: the value to set it to
1974 * Sets @key in @settings to @value.
1976 * A convenience variant of g_settings_set() for 32-bit unsigned
1977 * integers.
1979 * It is a programmer error to give a @key that isn't specified as
1980 * having a uint32 type in the schema for @settings.
1982 * Returns: %TRUE if setting the key succeeded,
1983 * %FALSE if the key was not writable
1985 * Since: 2.30
1987 gboolean
1988 g_settings_set_uint (GSettings *settings,
1989 const gchar *key,
1990 guint value)
1992 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1996 * g_settings_get_uint64:
1997 * @settings: a #GSettings object
1998 * @key: the key to get the value for
2000 * Gets the value that is stored at @key in @settings.
2002 * A convenience variant of g_settings_get() for 64-bit unsigned
2003 * integers.
2005 * It is a programmer error to give a @key that isn't specified as
2006 * having a uint64 type in the schema for @settings.
2008 * Returns: a 64-bit unsigned integer
2010 * Since: 2.50
2012 guint64
2013 g_settings_get_uint64 (GSettings *settings,
2014 const gchar *key)
2016 GVariant *value;
2017 guint64 result;
2019 value = g_settings_get_value (settings, key);
2020 result = g_variant_get_uint64 (value);
2021 g_variant_unref (value);
2023 return result;
2027 * g_settings_set_uint64:
2028 * @settings: a #GSettings object
2029 * @key: the name of the key to set
2030 * @value: the value to set it to
2032 * Sets @key in @settings to @value.
2034 * A convenience variant of g_settings_set() for 64-bit unsigned
2035 * integers.
2037 * It is a programmer error to give a @key that isn't specified as
2038 * having a uint64 type in the schema for @settings.
2040 * Returns: %TRUE if setting the key succeeded,
2041 * %FALSE if the key was not writable
2043 * Since: 2.50
2045 gboolean
2046 g_settings_set_uint64 (GSettings *settings,
2047 const gchar *key,
2048 guint64 value)
2050 return g_settings_set_value (settings, key, g_variant_new_uint64 (value));
2054 * g_settings_get_double:
2055 * @settings: a #GSettings object
2056 * @key: the key to get the value for
2058 * Gets the value that is stored at @key in @settings.
2060 * A convenience variant of g_settings_get() for doubles.
2062 * It is a programmer error to give a @key that isn't specified as
2063 * having a 'double' type in the schema for @settings.
2065 * Returns: a double
2067 * Since: 2.26
2069 gdouble
2070 g_settings_get_double (GSettings *settings,
2071 const gchar *key)
2073 GVariant *value;
2074 gdouble result;
2076 value = g_settings_get_value (settings, key);
2077 result = g_variant_get_double (value);
2078 g_variant_unref (value);
2080 return result;
2084 * g_settings_set_double:
2085 * @settings: a #GSettings object
2086 * @key: the name of the key to set
2087 * @value: the value to set it to
2089 * Sets @key in @settings to @value.
2091 * A convenience variant of g_settings_set() for doubles.
2093 * It is a programmer error to give a @key that isn't specified as
2094 * having a 'double' type in the schema for @settings.
2096 * Returns: %TRUE if setting the key succeeded,
2097 * %FALSE if the key was not writable
2099 * Since: 2.26
2101 gboolean
2102 g_settings_set_double (GSettings *settings,
2103 const gchar *key,
2104 gdouble value)
2106 return g_settings_set_value (settings, key, g_variant_new_double (value));
2110 * g_settings_get_boolean:
2111 * @settings: a #GSettings object
2112 * @key: the key to get the value for
2114 * Gets the value that is stored at @key in @settings.
2116 * A convenience variant of g_settings_get() for booleans.
2118 * It is a programmer error to give a @key that isn't specified as
2119 * having a boolean type in the schema for @settings.
2121 * Returns: a boolean
2123 * Since: 2.26
2125 gboolean
2126 g_settings_get_boolean (GSettings *settings,
2127 const gchar *key)
2129 GVariant *value;
2130 gboolean result;
2132 value = g_settings_get_value (settings, key);
2133 result = g_variant_get_boolean (value);
2134 g_variant_unref (value);
2136 return result;
2140 * g_settings_set_boolean:
2141 * @settings: a #GSettings object
2142 * @key: the name of the key to set
2143 * @value: the value to set it to
2145 * Sets @key in @settings to @value.
2147 * A convenience variant of g_settings_set() for booleans.
2149 * It is a programmer error to give a @key that isn't specified as
2150 * having a boolean type in the schema for @settings.
2152 * Returns: %TRUE if setting the key succeeded,
2153 * %FALSE if the key was not writable
2155 * Since: 2.26
2157 gboolean
2158 g_settings_set_boolean (GSettings *settings,
2159 const gchar *key,
2160 gboolean value)
2162 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
2166 * g_settings_get_strv:
2167 * @settings: a #GSettings object
2168 * @key: the key to get the value for
2170 * A convenience variant of g_settings_get() for string arrays.
2172 * It is a programmer error to give a @key that isn't specified as
2173 * having an array of strings type in the schema for @settings.
2175 * Returns: (array zero-terminated=1) (transfer full): a
2176 * newly-allocated, %NULL-terminated array of strings, the value that
2177 * is stored at @key in @settings.
2179 * Since: 2.26
2181 gchar **
2182 g_settings_get_strv (GSettings *settings,
2183 const gchar *key)
2185 GVariant *value;
2186 gchar **result;
2188 value = g_settings_get_value (settings, key);
2189 result = g_variant_dup_strv (value, NULL);
2190 g_variant_unref (value);
2192 return result;
2196 * g_settings_set_strv:
2197 * @settings: a #GSettings object
2198 * @key: the name of the key to set
2199 * @value: (nullable) (array zero-terminated=1): the value to set it to, or %NULL
2201 * Sets @key in @settings to @value.
2203 * A convenience variant of g_settings_set() for string arrays. If
2204 * @value is %NULL, then @key is set to be the empty array.
2206 * It is a programmer error to give a @key that isn't specified as
2207 * having an array of strings type in the schema for @settings.
2209 * Returns: %TRUE if setting the key succeeded,
2210 * %FALSE if the key was not writable
2212 * Since: 2.26
2214 gboolean
2215 g_settings_set_strv (GSettings *settings,
2216 const gchar *key,
2217 const gchar * const *value)
2219 GVariant *array;
2221 if (value != NULL)
2222 array = g_variant_new_strv (value, -1);
2223 else
2224 array = g_variant_new_strv (NULL, 0);
2226 return g_settings_set_value (settings, key, array);
2229 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2231 * g_settings_delay:
2232 * @settings: a #GSettings object
2234 * Changes the #GSettings object into 'delay-apply' mode. In this
2235 * mode, changes to @settings are not immediately propagated to the
2236 * backend, but kept locally until g_settings_apply() is called.
2238 * Since: 2.26
2240 void
2241 g_settings_delay (GSettings *settings)
2243 g_return_if_fail (G_IS_SETTINGS (settings));
2245 if (settings->priv->delayed)
2246 return;
2248 settings->priv->delayed =
2249 g_delayed_settings_backend_new (settings->priv->backend,
2250 settings,
2251 settings->priv->main_context);
2252 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2253 g_object_unref (settings->priv->backend);
2255 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2256 g_settings_backend_watch (settings->priv->backend,
2257 &listener_vtable, G_OBJECT (settings),
2258 settings->priv->main_context);
2260 g_object_notify (G_OBJECT (settings), "delay-apply");
2264 * g_settings_apply:
2265 * @settings: a #GSettings instance
2267 * Applies any changes that have been made to the settings. This
2268 * function does nothing unless @settings is in 'delay-apply' mode;
2269 * see g_settings_delay(). In the normal case settings are always
2270 * applied immediately.
2272 void
2273 g_settings_apply (GSettings *settings)
2275 if (settings->priv->delayed)
2277 GDelayedSettingsBackend *delayed;
2279 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2280 g_delayed_settings_backend_apply (delayed);
2285 * g_settings_revert:
2286 * @settings: a #GSettings instance
2288 * Reverts all non-applied changes to the settings. This function
2289 * does nothing unless @settings is in 'delay-apply' mode; see
2290 * g_settings_delay(). In the normal case settings are always applied
2291 * immediately.
2293 * Change notifications will be emitted for affected keys.
2295 void
2296 g_settings_revert (GSettings *settings)
2298 if (settings->priv->delayed)
2300 GDelayedSettingsBackend *delayed;
2302 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2303 g_delayed_settings_backend_revert (delayed);
2308 * g_settings_get_has_unapplied:
2309 * @settings: a #GSettings object
2311 * Returns whether the #GSettings object has any unapplied
2312 * changes. This can only be the case if it is in 'delayed-apply' mode.
2314 * Returns: %TRUE if @settings has unapplied changes
2316 * Since: 2.26
2318 gboolean
2319 g_settings_get_has_unapplied (GSettings *settings)
2321 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2323 return settings->priv->delayed &&
2324 g_delayed_settings_backend_get_has_unapplied (
2325 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2328 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2330 * g_settings_reset:
2331 * @settings: a #GSettings object
2332 * @key: the name of a key
2334 * Resets @key to its default value.
2336 * This call resets the key, as much as possible, to its default value.
2337 * That might the value specified in the schema or the one set by the
2338 * administrator.
2340 void
2341 g_settings_reset (GSettings *settings,
2342 const gchar *key)
2344 gchar *path;
2346 g_return_if_fail (G_IS_SETTINGS (settings));
2347 g_return_if_fail (key != NULL);
2349 path = g_strconcat (settings->priv->path, key, NULL);
2350 g_settings_backend_reset (settings->priv->backend, path, NULL);
2351 g_free (path);
2355 * g_settings_sync:
2357 * Ensures that all pending operations are complete for the default backend.
2359 * Writes made to a #GSettings are handled asynchronously. For this
2360 * reason, it is very unlikely that the changes have it to disk by the
2361 * time g_settings_set() returns.
2363 * This call will block until all of the writes have made it to the
2364 * backend. Since the mainloop is not running, no change notifications
2365 * will be dispatched during this call (but some may be queued by the
2366 * time the call is done).
2368 void
2369 g_settings_sync (void)
2371 g_settings_backend_sync_default ();
2375 * g_settings_is_writable:
2376 * @settings: a #GSettings object
2377 * @name: the name of a key
2379 * Finds out if a key can be written or not
2381 * Returns: %TRUE if the key @name is writable
2383 * Since: 2.26
2385 gboolean
2386 g_settings_is_writable (GSettings *settings,
2387 const gchar *name)
2389 gboolean writable;
2390 gchar *path;
2392 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2394 path = g_strconcat (settings->priv->path, name, NULL);
2395 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2396 g_free (path);
2398 return writable;
2402 * g_settings_get_child:
2403 * @settings: a #GSettings object
2404 * @name: the name of the child schema
2406 * Creates a child settings object which has a base path of
2407 * `base-path/@name`, where `base-path` is the base path of
2408 * @settings.
2410 * The schema for the child settings object must have been declared
2411 * in the schema of @settings using a <child> element.
2413 * Returns: (transfer full): a 'child' settings object
2415 * Since: 2.26
2417 GSettings *
2418 g_settings_get_child (GSettings *settings,
2419 const gchar *name)
2421 const gchar *child_schema;
2422 gchar *child_path;
2423 gchar *child_name;
2424 GSettings *child;
2426 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2428 child_name = g_strconcat (name, "/", NULL);
2429 child_schema = g_settings_schema_get_string (settings->priv->schema,
2430 child_name);
2431 if (child_schema == NULL)
2432 g_error ("Schema '%s' has no child '%s'",
2433 g_settings_schema_get_id (settings->priv->schema), name);
2435 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2436 child = g_object_new (G_TYPE_SETTINGS,
2437 "backend", settings->priv->backend,
2438 "schema-id", child_schema,
2439 "path", child_path,
2440 NULL);
2441 g_free (child_path);
2442 g_free (child_name);
2444 return child;
2448 * g_settings_list_keys:
2449 * @settings: a #GSettings object
2451 * Introspects the list of keys on @settings.
2453 * You should probably not be calling this function from "normal" code
2454 * (since you should already know what keys are in your schema). This
2455 * function is intended for introspection reasons.
2457 * You should free the return value with g_strfreev() when you are done
2458 * with it.
2460 * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2462 gchar **
2463 g_settings_list_keys (GSettings *settings)
2465 return g_settings_schema_list_keys (settings->priv->schema);
2469 * g_settings_list_children:
2470 * @settings: a #GSettings object
2472 * Gets the list of children on @settings.
2474 * The list is exactly the list of strings for which it is not an error
2475 * to call g_settings_get_child().
2477 * For GSettings objects that are lists, this value can change at any
2478 * time and you should connect to the "children-changed" signal to watch
2479 * for those changes. Note that there is a race condition here: you may
2480 * request a child after listing it only for it to have been destroyed
2481 * in the meantime. For this reason, g_settings_get_child() may return
2482 * %NULL even for a child that was listed by this function.
2484 * For GSettings objects that are not lists, you should probably not be
2485 * calling this function from "normal" code (since you should already
2486 * know what children are in your schema). This function may still be
2487 * useful there for introspection reasons, however.
2489 * You should free the return value with g_strfreev() when you are done
2490 * with it.
2492 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2494 gchar **
2495 g_settings_list_children (GSettings *settings)
2497 return g_settings_schema_list_children (settings->priv->schema);
2501 * g_settings_get_range:
2502 * @settings: a #GSettings
2503 * @key: the key to query the range of
2505 * Queries the range of a key.
2507 * Since: 2.28
2509 * Deprecated:2.40:Use g_settings_schema_key_get_range() instead.
2511 GVariant *
2512 g_settings_get_range (GSettings *settings,
2513 const gchar *key)
2515 GSettingsSchemaKey skey;
2516 GVariant *range;
2518 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2519 range = g_settings_schema_key_get_range (&skey);
2520 g_settings_schema_key_clear (&skey);
2522 return range;
2526 * g_settings_range_check:
2527 * @settings: a #GSettings
2528 * @key: the key to check
2529 * @value: the value to check
2531 * Checks if the given @value is of the correct type and within the
2532 * permitted range for @key.
2534 * Returns: %TRUE if @value is valid for @key
2536 * Since: 2.28
2538 * Deprecated:2.40:Use g_settings_schema_key_range_check() instead.
2540 gboolean
2541 g_settings_range_check (GSettings *settings,
2542 const gchar *key,
2543 GVariant *value)
2545 GSettingsSchemaKey skey;
2546 gboolean good;
2548 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2549 good = g_settings_schema_key_range_check (&skey, value);
2550 g_settings_schema_key_clear (&skey);
2552 return good;
2555 /* Binding {{{1 */
2556 typedef struct
2558 GSettingsSchemaKey key;
2559 GSettings *settings;
2560 GObject *object;
2562 GSettingsBindGetMapping get_mapping;
2563 GSettingsBindSetMapping set_mapping;
2564 gpointer user_data;
2565 GDestroyNotify destroy;
2567 guint writable_handler_id;
2568 guint property_handler_id;
2569 const GParamSpec *property;
2570 guint key_handler_id;
2572 /* prevent recursion */
2573 gboolean running;
2574 } GSettingsBinding;
2576 static void
2577 g_settings_binding_free (gpointer data)
2579 GSettingsBinding *binding = data;
2581 g_assert (!binding->running);
2583 if (binding->writable_handler_id)
2584 g_signal_handler_disconnect (binding->settings,
2585 binding->writable_handler_id);
2587 if (binding->key_handler_id)
2588 g_signal_handler_disconnect (binding->settings,
2589 binding->key_handler_id);
2591 if (g_signal_handler_is_connected (binding->object,
2592 binding->property_handler_id))
2593 g_signal_handler_disconnect (binding->object,
2594 binding->property_handler_id);
2596 g_settings_schema_key_clear (&binding->key);
2598 if (binding->destroy)
2599 binding->destroy (binding->user_data);
2601 g_object_unref (binding->settings);
2603 g_slice_free (GSettingsBinding, binding);
2606 static GQuark
2607 g_settings_binding_quark (const char *property)
2609 GQuark quark;
2610 gchar *tmp;
2612 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2613 quark = g_quark_from_string (tmp);
2614 g_free (tmp);
2616 return quark;
2619 static void
2620 g_settings_binding_key_changed (GSettings *settings,
2621 const gchar *key,
2622 gpointer user_data)
2624 GSettingsBinding *binding = user_data;
2625 GValue value = G_VALUE_INIT;
2626 GVariant *variant;
2628 g_assert (settings == binding->settings);
2629 g_assert (key == binding->key.name);
2631 if (binding->running)
2632 return;
2634 binding->running = TRUE;
2636 g_value_init (&value, binding->property->value_type);
2638 variant = g_settings_read_from_backend (binding->settings, &binding->key, FALSE, FALSE);
2639 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2641 /* silently ignore errors in the user's config database */
2642 g_variant_unref (variant);
2643 variant = NULL;
2646 if (variant == NULL)
2648 variant = g_settings_schema_key_get_translated_default (&binding->key);
2649 if (variant &&
2650 !binding->get_mapping (&value, variant, binding->user_data))
2652 /* flag translation errors with a warning */
2653 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
2654 "was rejected by the binding mapping function",
2655 binding->key.unparsed, binding->key.name,
2656 g_settings_schema_get_id (binding->key.schema));
2657 g_variant_unref (variant);
2658 variant = NULL;
2662 if (variant == NULL)
2664 variant = g_variant_ref (binding->key.default_value);
2665 if (!binding->get_mapping (&value, variant, binding->user_data))
2666 g_error ("The schema default value for key '%s' in schema '%s' "
2667 "was rejected by the binding mapping function.",
2668 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2671 g_object_set_property (binding->object, binding->property->name, &value);
2672 g_variant_unref (variant);
2673 g_value_unset (&value);
2675 binding->running = FALSE;
2678 static void
2679 g_settings_binding_property_changed (GObject *object,
2680 const GParamSpec *pspec,
2681 gpointer user_data)
2683 GSettingsBinding *binding = user_data;
2684 GValue value = G_VALUE_INIT;
2685 GVariant *variant;
2687 g_assert (object == binding->object);
2688 g_assert (pspec == binding->property);
2690 if (binding->running)
2691 return;
2693 binding->running = TRUE;
2695 g_value_init (&value, pspec->value_type);
2696 g_object_get_property (object, pspec->name, &value);
2697 if ((variant = binding->set_mapping (&value, binding->key.type,
2698 binding->user_data)))
2700 g_variant_take_ref (variant);
2702 if (!g_settings_schema_key_type_check (&binding->key, variant))
2704 g_critical ("binding mapping function for key '%s' returned "
2705 "GVariant of type '%s' when type '%s' was requested",
2706 binding->key.name, g_variant_get_type_string (variant),
2707 g_variant_type_dup_string (binding->key.type));
2708 return;
2711 if (!g_settings_schema_key_range_check (&binding->key, variant))
2713 g_critical ("GObject property '%s' on a '%s' object is out of "
2714 "schema-specified range for key '%s' of '%s': %s",
2715 binding->property->name, g_type_name (binding->property->owner_type),
2716 binding->key.name, g_settings_schema_get_id (binding->key.schema),
2717 g_variant_print (variant, TRUE));
2718 return;
2721 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2722 g_variant_unref (variant);
2724 g_value_unset (&value);
2726 binding->running = FALSE;
2729 static gboolean
2730 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2731 GVariant *variant,
2732 gpointer user_data)
2734 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2735 return TRUE;
2738 static GVariant *
2739 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2740 const GVariantType *expected_type,
2741 gpointer user_data)
2743 return g_variant_new_boolean (!g_value_get_boolean (value));
2747 * g_settings_bind:
2748 * @settings: a #GSettings object
2749 * @key: the key to bind
2750 * @object: (type GObject.Object): a #GObject
2751 * @property: the name of the property to bind
2752 * @flags: flags for the binding
2754 * Create a binding between the @key in the @settings object
2755 * and the property @property of @object.
2757 * The binding uses the default GIO mapping functions to map
2758 * between the settings and property values. These functions
2759 * handle booleans, numeric types and string types in a
2760 * straightforward way. Use g_settings_bind_with_mapping() if
2761 * you need a custom mapping, or map between types that are not
2762 * supported by the default mapping functions.
2764 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2765 * function also establishes a binding between the writability of
2766 * @key and the "sensitive" property of @object (if @object has
2767 * a boolean property by that name). See g_settings_bind_writable()
2768 * for more details about writable bindings.
2770 * Note that the lifecycle of the binding is tied to @object,
2771 * and that you can have only one binding per object property.
2772 * If you bind the same property twice on the same object, the second
2773 * binding overrides the first one.
2775 * Since: 2.26
2777 void
2778 g_settings_bind (GSettings *settings,
2779 const gchar *key,
2780 gpointer object,
2781 const gchar *property,
2782 GSettingsBindFlags flags)
2784 GSettingsBindGetMapping get_mapping = NULL;
2785 GSettingsBindSetMapping set_mapping = NULL;
2787 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2789 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2790 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2792 /* can't pass this flag to g_settings_bind_with_mapping() */
2793 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2796 g_settings_bind_with_mapping (settings, key, object, property, flags,
2797 get_mapping, set_mapping, NULL, NULL);
2801 * g_settings_bind_with_mapping: (skip)
2802 * @settings: a #GSettings object
2803 * @key: the key to bind
2804 * @object: (type GObject.Object): a #GObject
2805 * @property: the name of the property to bind
2806 * @flags: flags for the binding
2807 * @get_mapping: a function that gets called to convert values
2808 * from @settings to @object, or %NULL to use the default GIO mapping
2809 * @set_mapping: a function that gets called to convert values
2810 * from @object to @settings, or %NULL to use the default GIO mapping
2811 * @user_data: data that gets passed to @get_mapping and @set_mapping
2812 * @destroy: #GDestroyNotify function for @user_data
2814 * Create a binding between the @key in the @settings object
2815 * and the property @property of @object.
2817 * The binding uses the provided mapping functions to map between
2818 * settings and property values.
2820 * Note that the lifecycle of the binding is tied to @object,
2821 * and that you can have only one binding per object property.
2822 * If you bind the same property twice on the same object, the second
2823 * binding overrides the first one.
2825 * Since: 2.26
2827 void
2828 g_settings_bind_with_mapping (GSettings *settings,
2829 const gchar *key,
2830 gpointer object,
2831 const gchar *property,
2832 GSettingsBindFlags flags,
2833 GSettingsBindGetMapping get_mapping,
2834 GSettingsBindSetMapping set_mapping,
2835 gpointer user_data,
2836 GDestroyNotify destroy)
2838 GSettingsBinding *binding;
2839 GObjectClass *objectclass;
2840 gchar *detailed_signal;
2841 GQuark binding_quark;
2843 g_return_if_fail (G_IS_SETTINGS (settings));
2844 g_return_if_fail (key != NULL);
2845 g_return_if_fail (G_IS_OBJECT (object));
2846 g_return_if_fail (property != NULL);
2847 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2849 objectclass = G_OBJECT_GET_CLASS (object);
2851 binding = g_slice_new0 (GSettingsBinding);
2852 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2853 binding->settings = g_object_ref (settings);
2854 binding->object = object;
2855 binding->property = g_object_class_find_property (objectclass, property);
2856 binding->user_data = user_data;
2857 binding->destroy = destroy;
2858 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2859 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2861 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2862 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2864 if (binding->property == NULL)
2866 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2867 property, G_OBJECT_TYPE_NAME (object));
2868 return;
2871 if ((flags & G_SETTINGS_BIND_GET) &&
2872 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2874 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2875 "writable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2876 return;
2878 if ((flags & G_SETTINGS_BIND_SET) &&
2879 (binding->property->flags & G_PARAM_READABLE) == 0)
2881 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2882 "readable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2883 return;
2886 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2888 /* g_settings_bind_invert_boolean_get_mapping() is a private
2889 * function, so if we are here it means that g_settings_bind() was
2890 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2892 * Ensure that both sides are boolean.
2895 if (binding->property->value_type != G_TYPE_BOOLEAN)
2897 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2898 "was specified, but property '%s' on type '%s' has "
2899 "type '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2900 g_type_name ((binding->property->value_type)));
2901 return;
2904 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2906 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2907 "was specified, but key '%s' on schema '%s' has "
2908 "type '%s'", key, g_settings_schema_get_id (settings->priv->schema),
2909 g_variant_type_dup_string (binding->key.type));
2910 return;
2915 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2916 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2917 !g_settings_mapping_is_compatible (binding->property->value_type,
2918 binding->key.type))
2920 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2921 "'%s' which is not compatible with type '%s' of key '%s' "
2922 "on schema '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2923 g_type_name (binding->property->value_type),
2924 g_variant_type_dup_string (binding->key.type), key,
2925 g_settings_schema_get_id (settings->priv->schema));
2926 return;
2929 if ((flags & G_SETTINGS_BIND_SET) &&
2930 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2932 GParamSpec *sensitive;
2934 sensitive = g_object_class_find_property (objectclass, "sensitive");
2936 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2937 (sensitive->flags & G_PARAM_WRITABLE))
2938 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2941 if (flags & G_SETTINGS_BIND_SET)
2943 detailed_signal = g_strdup_printf ("notify::%s", binding->property->name);
2944 binding->property_handler_id =
2945 g_signal_connect (object, detailed_signal,
2946 G_CALLBACK (g_settings_binding_property_changed),
2947 binding);
2948 g_free (detailed_signal);
2950 if (~flags & G_SETTINGS_BIND_GET)
2951 g_settings_binding_property_changed (object,
2952 binding->property,
2953 binding);
2956 if (flags & G_SETTINGS_BIND_GET)
2958 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2960 detailed_signal = g_strdup_printf ("changed::%s", key);
2961 binding->key_handler_id =
2962 g_signal_connect (settings, detailed_signal,
2963 G_CALLBACK (g_settings_binding_key_changed),
2964 binding);
2965 g_free (detailed_signal);
2968 g_settings_binding_key_changed (settings, binding->key.name, binding);
2971 binding_quark = g_settings_binding_quark (binding->property->name);
2972 g_object_set_qdata_full (object, binding_quark,
2973 binding, g_settings_binding_free);
2976 /* Writability binding {{{1 */
2977 typedef struct
2979 GSettings *settings;
2980 gpointer object;
2981 const gchar *key;
2982 const gchar *property;
2983 gboolean inverted;
2984 gulong handler_id;
2985 } GSettingsWritableBinding;
2987 static void
2988 g_settings_writable_binding_free (gpointer data)
2990 GSettingsWritableBinding *binding = data;
2992 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2993 g_object_unref (binding->settings);
2994 g_slice_free (GSettingsWritableBinding, binding);
2997 static void
2998 g_settings_binding_writable_changed (GSettings *settings,
2999 const gchar *key,
3000 gpointer user_data)
3002 GSettingsWritableBinding *binding = user_data;
3003 gboolean writable;
3005 g_assert (settings == binding->settings);
3006 g_assert (key == binding->key);
3008 writable = g_settings_is_writable (settings, key);
3010 if (binding->inverted)
3011 writable = !writable;
3013 g_object_set (binding->object, binding->property, writable, NULL);
3017 * g_settings_bind_writable:
3018 * @settings: a #GSettings object
3019 * @key: the key to bind
3020 * @object: (type GObject.Object):a #GObject
3021 * @property: the name of a boolean property to bind
3022 * @inverted: whether to 'invert' the value
3024 * Create a binding between the writability of @key in the
3025 * @settings object and the property @property of @object.
3026 * The property must be boolean; "sensitive" or "visible"
3027 * properties of widgets are the most likely candidates.
3029 * Writable bindings are always uni-directional; changes of the
3030 * writability of the setting will be propagated to the object
3031 * property, not the other way.
3033 * When the @inverted argument is %TRUE, the binding inverts the
3034 * value as it passes from the setting to the object, i.e. @property
3035 * will be set to %TRUE if the key is not writable.
3037 * Note that the lifecycle of the binding is tied to @object,
3038 * and that you can have only one binding per object property.
3039 * If you bind the same property twice on the same object, the second
3040 * binding overrides the first one.
3042 * Since: 2.26
3044 void
3045 g_settings_bind_writable (GSettings *settings,
3046 const gchar *key,
3047 gpointer object,
3048 const gchar *property,
3049 gboolean inverted)
3051 GSettingsWritableBinding *binding;
3052 gchar *detailed_signal;
3053 GParamSpec *pspec;
3055 g_return_if_fail (G_IS_SETTINGS (settings));
3057 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
3058 if (pspec == NULL)
3060 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
3061 property, G_OBJECT_TYPE_NAME (object));
3062 return;
3064 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
3066 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
3067 property, G_OBJECT_TYPE_NAME (object));
3068 return;
3071 binding = g_slice_new (GSettingsWritableBinding);
3072 binding->settings = g_object_ref (settings);
3073 binding->object = object;
3074 binding->key = g_intern_string (key);
3075 binding->property = g_intern_string (property);
3076 binding->inverted = inverted;
3078 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3079 binding->handler_id =
3080 g_signal_connect (settings, detailed_signal,
3081 G_CALLBACK (g_settings_binding_writable_changed),
3082 binding);
3083 g_free (detailed_signal);
3085 g_object_set_qdata_full (object, g_settings_binding_quark (property),
3086 binding, g_settings_writable_binding_free);
3088 g_settings_binding_writable_changed (settings, binding->key, binding);
3092 * g_settings_unbind:
3093 * @object: (type GObject.Object): the object
3094 * @property: the property whose binding is removed
3096 * Removes an existing binding for @property on @object.
3098 * Note that bindings are automatically removed when the
3099 * object is finalized, so it is rarely necessary to call this
3100 * function.
3102 * Since: 2.26
3104 void
3105 g_settings_unbind (gpointer object,
3106 const gchar *property)
3108 GQuark binding_quark;
3110 binding_quark = g_settings_binding_quark (property);
3111 g_object_set_qdata (object, binding_quark, NULL);
3114 /* GAction {{{1 */
3116 typedef struct
3118 GObject parent_instance;
3120 GSettingsSchemaKey key;
3121 GSettings *settings;
3122 } GSettingsAction;
3124 typedef GObjectClass GSettingsActionClass;
3126 static GType g_settings_action_get_type (void);
3127 static void g_settings_action_iface_init (GActionInterface *iface);
3128 G_DEFINE_TYPE_WITH_CODE (GSettingsAction, g_settings_action, G_TYPE_OBJECT,
3129 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_settings_action_iface_init))
3131 enum
3133 ACTION_PROP_0,
3134 ACTION_PROP_NAME,
3135 ACTION_PROP_PARAMETER_TYPE,
3136 ACTION_PROP_ENABLED,
3137 ACTION_PROP_STATE_TYPE,
3138 ACTION_PROP_STATE
3141 static const gchar *
3142 g_settings_action_get_name (GAction *action)
3144 GSettingsAction *gsa = (GSettingsAction *) action;
3146 return gsa->key.name;
3149 static const GVariantType *
3150 g_settings_action_get_parameter_type (GAction *action)
3152 GSettingsAction *gsa = (GSettingsAction *) action;
3153 const GVariantType *type;
3155 type = g_variant_get_type (gsa->key.default_value);
3156 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
3157 type = NULL;
3159 return type;
3162 static gboolean
3163 g_settings_action_get_enabled (GAction *action)
3165 GSettingsAction *gsa = (GSettingsAction *) action;
3167 return g_settings_is_writable (gsa->settings, gsa->key.name);
3170 static const GVariantType *
3171 g_settings_action_get_state_type (GAction *action)
3173 GSettingsAction *gsa = (GSettingsAction *) action;
3175 return g_variant_get_type (gsa->key.default_value);
3178 static GVariant *
3179 g_settings_action_get_state (GAction *action)
3181 GSettingsAction *gsa = (GSettingsAction *) action;
3182 GVariant *value;
3184 value = g_settings_read_from_backend (gsa->settings, &gsa->key, FALSE, FALSE);
3186 if (value == NULL)
3187 value = g_settings_schema_key_get_translated_default (&gsa->key);
3189 if (value == NULL)
3190 value = g_variant_ref (gsa->key.default_value);
3192 return value;
3195 static GVariant *
3196 g_settings_action_get_state_hint (GAction *action)
3198 GSettingsAction *gsa = (GSettingsAction *) action;
3200 /* no point in reimplementing this... */
3201 return g_settings_schema_key_get_range (&gsa->key);
3204 static void
3205 g_settings_action_change_state (GAction *action,
3206 GVariant *value)
3208 GSettingsAction *gsa = (GSettingsAction *) action;
3210 if (g_settings_schema_key_type_check (&gsa->key, value) && g_settings_schema_key_range_check (&gsa->key, value))
3211 g_settings_write_to_backend (gsa->settings, &gsa->key, value);
3214 static void
3215 g_settings_action_activate (GAction *action,
3216 GVariant *parameter)
3218 GSettingsAction *gsa = (GSettingsAction *) action;
3220 if (g_variant_is_of_type (gsa->key.default_value, G_VARIANT_TYPE_BOOLEAN))
3222 GVariant *old;
3224 if (parameter != NULL)
3225 return;
3227 old = g_settings_action_get_state (action);
3228 parameter = g_variant_new_boolean (!g_variant_get_boolean (old));
3229 g_variant_unref (old);
3232 g_action_change_state (action, parameter);
3235 static void
3236 g_settings_action_get_property (GObject *object, guint prop_id,
3237 GValue *value, GParamSpec *pspec)
3239 GAction *action = G_ACTION (object);
3241 switch (prop_id)
3243 case ACTION_PROP_NAME:
3244 g_value_set_string (value, g_settings_action_get_name (action));
3245 break;
3247 case ACTION_PROP_PARAMETER_TYPE:
3248 g_value_set_boxed (value, g_settings_action_get_parameter_type (action));
3249 break;
3251 case ACTION_PROP_ENABLED:
3252 g_value_set_boolean (value, g_settings_action_get_enabled (action));
3253 break;
3255 case ACTION_PROP_STATE_TYPE:
3256 g_value_set_boxed (value, g_settings_action_get_state_type (action));
3257 break;
3259 case ACTION_PROP_STATE:
3260 g_value_set_variant (value, g_settings_action_get_state (action));
3261 break;
3263 default:
3264 g_assert_not_reached ();
3268 static void
3269 g_settings_action_finalize (GObject *object)
3271 GSettingsAction *gsa = (GSettingsAction *) object;
3273 g_signal_handlers_disconnect_by_data (gsa->settings, gsa);
3274 g_object_unref (gsa->settings);
3275 g_settings_schema_key_clear (&gsa->key);
3277 G_OBJECT_CLASS (g_settings_action_parent_class)
3278 ->finalize (object);
3281 static void
3282 g_settings_action_init (GSettingsAction *gsa)
3286 static void
3287 g_settings_action_iface_init (GActionInterface *iface)
3289 iface->get_name = g_settings_action_get_name;
3290 iface->get_parameter_type = g_settings_action_get_parameter_type;
3291 iface->get_enabled = g_settings_action_get_enabled;
3292 iface->get_state_type = g_settings_action_get_state_type;
3293 iface->get_state = g_settings_action_get_state;
3294 iface->get_state_hint = g_settings_action_get_state_hint;
3295 iface->change_state = g_settings_action_change_state;
3296 iface->activate = g_settings_action_activate;
3299 static void
3300 g_settings_action_class_init (GSettingsActionClass *class)
3302 class->get_property = g_settings_action_get_property;
3303 class->finalize = g_settings_action_finalize;
3305 g_object_class_override_property (class, ACTION_PROP_NAME, "name");
3306 g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE, "parameter-type");
3307 g_object_class_override_property (class, ACTION_PROP_ENABLED, "enabled");
3308 g_object_class_override_property (class, ACTION_PROP_STATE_TYPE, "state-type");
3309 g_object_class_override_property (class, ACTION_PROP_STATE, "state");
3312 static void
3313 g_settings_action_changed (GSettings *settings,
3314 const gchar *key,
3315 gpointer user_data)
3317 g_object_notify (user_data, "state");
3320 static void
3321 g_settings_action_enabled_changed (GSettings *settings,
3322 const gchar *key,
3323 gpointer user_data)
3325 g_object_notify (user_data, "enabled");
3329 * g_settings_create_action:
3330 * @settings: a #GSettings
3331 * @key: the name of a key in @settings
3333 * Creates a #GAction corresponding to a given #GSettings key.
3335 * The action has the same name as the key.
3337 * The value of the key becomes the state of the action and the action
3338 * is enabled when the key is writable. Changing the state of the
3339 * action results in the key being written to. Changes to the value or
3340 * writability of the key cause appropriate change notifications to be
3341 * emitted for the action.
3343 * For boolean-valued keys, action activations take no parameter and
3344 * result in the toggling of the value. For all other types,
3345 * activations take the new value for the key (which must have the
3346 * correct type).
3348 * Returns: (transfer full): a new #GAction
3350 * Since: 2.32
3352 GAction *
3353 g_settings_create_action (GSettings *settings,
3354 const gchar *key)
3356 GSettingsAction *gsa;
3357 gchar *detailed_signal;
3359 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
3360 g_return_val_if_fail (key != NULL, NULL);
3362 gsa = g_object_new (g_settings_action_get_type (), NULL);
3363 gsa->settings = g_object_ref (settings);
3364 g_settings_schema_key_init (&gsa->key, settings->priv->schema, key);
3366 detailed_signal = g_strdup_printf ("changed::%s", key);
3367 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_changed), gsa);
3368 g_free (detailed_signal);
3369 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3370 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_enabled_changed), gsa);
3371 g_free (detailed_signal);
3373 return G_ACTION (gsa);
3376 /* Epilogue {{{1 */
3378 /* vim:set foldmethod=marker: */