Improvde #include order consistency
[glib.git] / gio / gsettings.c
blob407a5a00b8fddbac4ac39d27cf25e955f2eb24ae
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 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 /* Prelude {{{1 */
23 #include "config.h"
25 #include <glib.h>
26 #include <glibintl.h>
28 #include "gsettings.h"
30 #include "gdelayedsettingsbackend.h"
31 #include "gsettingsbackendinternal.h"
32 #include "gsettings-mapping.h"
33 #include "gsettingsschema-internal.h"
34 #include "gaction.h"
36 #include "strinfo.c"
38 /**
39 * SECTION:gsettings
40 * @short_description: High-level API for application settings
42 * The #GSettings class provides a convenient API for storing and retrieving
43 * application settings.
45 * Reads and writes can be considered to be non-blocking. Reading
46 * settings with #GSettings is typically extremely fast: on
47 * approximately the same order of magnitude (but slower than) a
48 * #GHashTable lookup. Writing settings is also extremely fast in terms
49 * of time to return to your application, but can be extremely expensive
50 * for other threads and other processes. Many settings backends
51 * (including dconf) have lazy initialisation which means in the common
52 * case of the user using their computer without modifying any settings
53 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
54 * even need to be started in this case. For this reason, you should
55 * only ever modify #GSettings keys in response to explicit user action.
56 * Particular care should be paid to ensure that modifications are not
57 * made during startup -- for example, when setting the initial value
58 * of preferences widgets. The built-in g_settings_bind() functionality
59 * is careful not to write settings in response to notify signals as a
60 * result of modifications that it makes to widgets.
62 * When creating a GSettings instance, you have to specify a schema
63 * that describes the keys in your settings and their types and default
64 * values, as well as some other information.
66 * Normally, a schema has as fixed path that determines where the settings
67 * are stored in the conceptual global tree of settings. However, schemas
68 * can also be 'relocatable', i.e. not equipped with 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 * <tag class="attribute">gettext-domain</tag> attribute of the
91 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
92 * elements and the category that is specified in the l10n attribute of the
93 * <tag class="starttag">key</tag> element.
95 * GSettings uses schemas in a compact binary form that is created
96 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
97 * utility. The input is a schema description in an XML format that can be
98 * described by the following DTD:
99 * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/gschema.dtd"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
101 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
103 * At runtime, schemas are identified by their id (as specified
104 * in the <tag class="attribute">id</tag> attribute of the
105 * <tag class="starttag">schema</tag> element). The
106 * convention for schema ids is to use a dotted name, similar in
107 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
108 * if the settings are for a specific service that owns a D-Bus bus name,
109 * the D-Bus bus name and schema id should match. For schemas which deal
110 * with settings not associated with one named application, the id should
111 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
113 * In addition to #GVariant types, keys can have types that have enumerated
114 * types. These can be described by a <tag class="starttag">choice</tag>,
115 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
116 * <xref linkend="schema-enumerated"/>. The underlying type of
117 * such a key is string, but you can use g_settings_get_enum(),
118 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
119 * access the numeric values corresponding to the string value of enum
120 * and flags keys.
122 * <example id="schema-default-values"><title>Default values</title>
123 * <programlisting><![CDATA[
124 * <schemalist>
125 * <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
127 * <key name="greeting" type="s">
128 * <default l10n="messages">"Hello, earthlings"</default>
129 * <summary>A greeting</summary>
130 * <description>
131 * Greeting of the invading martians
132 * </description>
133 * </key>
135 * <key name="box" type="(ii)">
136 * <default>(20,30)</default>
137 * </key>
139 * </schema>
140 * </schemalist>
141 * ]]></programlisting></example>
143 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
144 * <programlisting><![CDATA[
145 * <schemalist>
147 * <enum id="org.gtk.Test.myenum">
148 * <value nick="first" value="1"/>
149 * <value nick="second" value="2"/>
150 * </enum>
152 * <flags id="org.gtk.Test.myflags">
153 * <value nick="flag1" value="1"/>
154 * <value nick="flag2" value="2"/>
155 * <value nick="flag3" value="4"/>
156 * </flags>
158 * <schema id="org.gtk.Test">
160 * <key name="key-with-range" type="i">
161 * <range min="1" max="100"/>
162 * <default>10</default>
163 * </key>
165 * <key name="key-with-choices" type="s">
166 * <choices>
167 * <choice value='Elisabeth'/>
168 * <choice value='Annabeth'/>
169 * <choice value='Joe'/>
170 * </choices>
171 * <aliases>
172 * <alias value='Anna' target='Annabeth'/>
173 * <alias value='Beth' target='Elisabeth'/>
174 * </aliases>
175 * <default>'Joe'</default>
176 * </key>
178 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
179 * <default>'first'</default>
180 * </key>
182 * <key name='flags-key' flags='org.gtk.Test.myflags'>
183 * <default>["flag1",flag2"]</default>
184 * </key>
185 * </schema>
186 * </schemalist>
187 * ]]></programlisting></example>
189 * <refsect2>
190 * <title>Vendor overrides</title>
191 * <para>
192 * Default values are defined in the schemas that get installed by
193 * an application. Sometimes, it is necessary for a vendor or distributor
194 * to adjust these defaults. Since patching the XML source for the schema
195 * is inconvenient and error-prone,
196 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
197 * so-called 'vendor override' files. These are keyfiles in the same
198 * directory as the XML schema sources which can override default values.
199 * The schema id serves as the group name in the key file, and the values
200 * are expected in serialized GVariant form, as in the following example:
201 * <informalexample><programlisting>
202 * [org.gtk.Example]
203 * key1='string'
204 * key2=1.5
205 * </programlisting></informalexample>
206 * </para>
207 * <para>
208 * glib-compile-schemas expects schema files to have the extension
209 * <filename>.gschema.override</filename>
210 * </para>
211 * </refsect2>
213 * <refsect2>
214 * <title>Binding</title>
215 * <para>
216 * A very convenient feature of GSettings lets you bind #GObject properties
217 * directly to settings, using g_settings_bind(). Once a GObject property
218 * has been bound to a setting, changes on either side are automatically
219 * propagated to the other side. GSettings handles details like
220 * mapping between GObject and GVariant types, and preventing infinite
221 * cycles.
222 * </para>
223 * <para>
224 * This makes it very easy to hook up a preferences dialog to the
225 * underlying settings. To make this even more convenient, GSettings
226 * looks for a boolean property with the name "sensitivity" and
227 * automatically binds it to the writability of the bound setting.
228 * If this 'magic' gets in the way, it can be suppressed with the
229 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
230 * </para>
231 * </refsect2>
234 struct _GSettingsPrivate
236 /* where the signals go... */
237 GMainContext *main_context;
239 GSettingsBackend *backend;
240 GSettingsSchema *schema;
241 gchar *path;
243 GDelayedSettingsBackend *delayed;
246 enum
248 PROP_0,
249 PROP_SCHEMA,
250 PROP_SCHEMA_ID,
251 PROP_BACKEND,
252 PROP_PATH,
253 PROP_HAS_UNAPPLIED,
254 PROP_DELAY_APPLY
257 enum
259 SIGNAL_WRITABLE_CHANGE_EVENT,
260 SIGNAL_WRITABLE_CHANGED,
261 SIGNAL_CHANGE_EVENT,
262 SIGNAL_CHANGED,
263 N_SIGNALS
266 static guint g_settings_signals[N_SIGNALS];
268 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
270 /* Signals {{{1 */
271 static gboolean
272 g_settings_real_change_event (GSettings *settings,
273 const GQuark *keys,
274 gint n_keys)
276 gint i;
278 if (keys == NULL)
279 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
281 for (i = 0; i < n_keys; i++)
283 const gchar *key = g_quark_to_string (keys[i]);
285 if (g_str_has_suffix (key, "/"))
286 continue;
288 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED], keys[i], key);
291 return FALSE;
294 static gboolean
295 g_settings_real_writable_change_event (GSettings *settings,
296 GQuark key)
298 const GQuark *keys = &key;
299 gint n_keys = 1;
300 gint i;
302 if (key == 0)
303 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
305 for (i = 0; i < n_keys; i++)
307 const gchar *key = g_quark_to_string (keys[i]);
309 if (g_str_has_suffix (key, "/"))
310 continue;
312 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keys[i], key);
315 return FALSE;
318 static void
319 settings_backend_changed (GObject *target,
320 GSettingsBackend *backend,
321 const gchar *key,
322 gpointer origin_tag)
324 GSettings *settings = G_SETTINGS (target);
325 gboolean ignore_this;
326 gint i;
328 /* We used to assert here:
330 * settings->priv->backend == backend
332 * but it could be the case that a notification is queued for delivery
333 * while someone calls g_settings_delay() (which changes the backend).
335 * Since the delay backend would just pass that straight through
336 * anyway, it doesn't make sense to try to detect this case.
337 * Therefore, we just accept it.
340 for (i = 0; key[i] == settings->priv->path[i]; i++);
342 if (settings->priv->path[i] == '\0' &&
343 g_settings_schema_has_key (settings->priv->schema, key + i))
345 GQuark quark;
347 quark = g_quark_from_string (key + i);
348 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
349 0, &quark, 1, &ignore_this);
353 static void
354 settings_backend_path_changed (GObject *target,
355 GSettingsBackend *backend,
356 const gchar *path,
357 gpointer origin_tag)
359 GSettings *settings = G_SETTINGS (target);
360 gboolean ignore_this;
362 if (g_str_has_prefix (settings->priv->path, path))
363 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
364 0, NULL, 0, &ignore_this);
367 static void
368 settings_backend_keys_changed (GObject *target,
369 GSettingsBackend *backend,
370 const gchar *path,
371 const gchar * const *items,
372 gpointer origin_tag)
374 GSettings *settings = G_SETTINGS (target);
375 gboolean ignore_this;
376 gint i;
378 for (i = 0; settings->priv->path[i] &&
379 settings->priv->path[i] == path[i]; i++);
381 if (path[i] == '\0')
383 GQuark quarks[256];
384 gint j, l = 0;
386 for (j = 0; items[j]; j++)
388 const gchar *item = items[j];
389 gint k;
391 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
393 if (settings->priv->path[i + k] == '\0' &&
394 g_settings_schema_has_key (settings->priv->schema, item + k))
395 quarks[l++] = g_quark_from_string (item + k);
397 /* "256 quarks ought to be enough for anybody!"
398 * If this bites you, I'm sorry. Please file a bug.
400 g_assert (l < 256);
403 if (l > 0)
404 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
405 0, quarks, l, &ignore_this);
409 static void
410 settings_backend_writable_changed (GObject *target,
411 GSettingsBackend *backend,
412 const gchar *key)
414 GSettings *settings = G_SETTINGS (target);
415 gboolean ignore_this;
416 gint i;
418 for (i = 0; key[i] == settings->priv->path[i]; i++);
420 if (settings->priv->path[i] == '\0' &&
421 g_settings_schema_has_key (settings->priv->schema, key + i))
422 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
423 0, g_quark_from_string (key + i), &ignore_this);
426 static void
427 settings_backend_path_writable_changed (GObject *target,
428 GSettingsBackend *backend,
429 const gchar *path)
431 GSettings *settings = G_SETTINGS (target);
432 gboolean ignore_this;
434 if (g_str_has_prefix (settings->priv->path, path))
435 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
436 0, (GQuark) 0, &ignore_this);
439 /* Properties, Construction, Destruction {{{1 */
440 static void
441 g_settings_set_property (GObject *object,
442 guint prop_id,
443 const GValue *value,
444 GParamSpec *pspec)
446 GSettings *settings = G_SETTINGS (object);
448 switch (prop_id)
450 case PROP_SCHEMA:
452 GSettingsSchema *schema;
454 schema = g_value_dup_boxed (value);
456 /* we receive a set_property() call for "settings-schema" even
457 * if it was not specified (ie: with NULL value). ->schema
458 * could already be set at this point (ie: via "schema-id").
459 * check for NULL to avoid clobbering the existing value.
461 if (schema != NULL)
463 g_assert (settings->priv->schema == NULL);
464 settings->priv->schema = schema;
467 break;
469 case PROP_SCHEMA_ID:
471 const gchar *schema_id;
473 schema_id = g_value_get_string (value);
475 /* we receive a set_property() call for both "schema" and
476 * "schema-id", even if they are not set. Hopefully only one of
477 * them is non-NULL.
479 if (schema_id != NULL)
481 GSettingsSchemaSource *default_source;
483 g_assert (settings->priv->schema == NULL);
484 default_source = g_settings_schema_source_get_default ();
486 if (default_source == NULL)
487 g_error ("No GSettings schemas are installed on the system");
489 settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
491 if (settings->priv->schema == NULL)
492 g_error ("Settings schema '%s' is not installed\n", schema_id);
495 break;
497 case PROP_PATH:
498 settings->priv->path = g_value_dup_string (value);
499 break;
501 case PROP_BACKEND:
502 settings->priv->backend = g_value_dup_object (value);
503 break;
505 default:
506 g_assert_not_reached ();
510 static void
511 g_settings_get_property (GObject *object,
512 guint prop_id,
513 GValue *value,
514 GParamSpec *pspec)
516 GSettings *settings = G_SETTINGS (object);
518 switch (prop_id)
520 case PROP_SCHEMA:
521 g_value_set_boxed (value, settings->priv->schema);
522 break;
524 case PROP_SCHEMA_ID:
525 g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
526 break;
528 case PROP_BACKEND:
529 g_value_set_object (value, settings->priv->backend);
530 break;
532 case PROP_PATH:
533 g_value_set_string (value, settings->priv->path);
534 break;
536 case PROP_HAS_UNAPPLIED:
537 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
538 break;
540 case PROP_DELAY_APPLY:
541 g_value_set_boolean (value, settings->priv->delayed != NULL);
542 break;
544 default:
545 g_assert_not_reached ();
549 static const GSettingsListenerVTable listener_vtable = {
550 settings_backend_changed,
551 settings_backend_path_changed,
552 settings_backend_keys_changed,
553 settings_backend_writable_changed,
554 settings_backend_path_writable_changed
557 static void
558 g_settings_constructed (GObject *object)
560 GSettings *settings = G_SETTINGS (object);
561 const gchar *schema_path;
563 schema_path = g_settings_schema_get_path (settings->priv->schema);
565 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
566 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
567 g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
569 if (settings->priv->path == NULL)
571 if (schema_path == NULL)
572 g_error ("attempting to create schema '%s' without a path",
573 g_settings_schema_get_id (settings->priv->schema));
575 settings->priv->path = g_strdup (schema_path);
578 if (settings->priv->backend == NULL)
579 settings->priv->backend = g_settings_backend_get_default ();
581 g_settings_backend_watch (settings->priv->backend,
582 &listener_vtable, G_OBJECT (settings),
583 settings->priv->main_context);
584 g_settings_backend_subscribe (settings->priv->backend,
585 settings->priv->path);
588 static void
589 g_settings_finalize (GObject *object)
591 GSettings *settings = G_SETTINGS (object);
593 g_settings_backend_unsubscribe (settings->priv->backend,
594 settings->priv->path);
595 g_main_context_unref (settings->priv->main_context);
596 g_object_unref (settings->priv->backend);
597 g_settings_schema_unref (settings->priv->schema);
598 g_free (settings->priv->path);
600 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
603 static void
604 g_settings_init (GSettings *settings)
606 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
607 G_TYPE_SETTINGS,
608 GSettingsPrivate);
610 settings->priv->main_context = g_main_context_ref_thread_default ();
613 static void
614 g_settings_class_init (GSettingsClass *class)
616 GObjectClass *object_class = G_OBJECT_CLASS (class);
618 class->writable_change_event = g_settings_real_writable_change_event;
619 class->change_event = g_settings_real_change_event;
621 object_class->set_property = g_settings_set_property;
622 object_class->get_property = g_settings_get_property;
623 object_class->constructed = g_settings_constructed;
624 object_class->finalize = g_settings_finalize;
626 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
629 * GSettings::changed:
630 * @settings: the object on which the signal was emitted
631 * @key: the name of the key that changed
633 * The "changed" signal is emitted when a key has potentially changed.
634 * You should call one of the g_settings_get() calls to check the new
635 * value.
637 * This signal supports detailed connections. You can connect to the
638 * detailed signal "changed::x" in order to only receive callbacks
639 * when key "x" changes.
641 g_settings_signals[SIGNAL_CHANGED] =
642 g_signal_new ("changed", G_TYPE_SETTINGS,
643 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
644 G_STRUCT_OFFSET (GSettingsClass, changed),
645 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
646 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
649 * GSettings::change-event:
650 * @settings: the object on which the signal was emitted
651 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
652 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
653 * @n_keys: the length of the @keys array, or 0
655 * The "change-event" signal is emitted once per change event that
656 * affects this settings object. You should connect to this signal
657 * only if you are interested in viewing groups of changes before they
658 * are split out into multiple emissions of the "changed" signal.
659 * For most use cases it is more appropriate to use the "changed" signal.
661 * In the event that the change event applies to one or more specified
662 * keys, @keys will be an array of #GQuark of length @n_keys. In the
663 * event that the change event applies to the #GSettings object as a
664 * whole (ie: potentially every key has been changed) then @keys will
665 * be %NULL and @n_keys will be 0.
667 * The default handler for this signal invokes the "changed" signal
668 * for each affected key. If any other connected handler returns
669 * %TRUE then this default functionality will be suppressed.
671 * Returns: %TRUE to stop other handlers from being invoked for the
672 * event. FALSE to propagate the event further.
674 g_settings_signals[SIGNAL_CHANGE_EVENT] =
675 g_signal_new ("change-event", G_TYPE_SETTINGS,
676 G_SIGNAL_RUN_LAST,
677 G_STRUCT_OFFSET (GSettingsClass, change_event),
678 g_signal_accumulator_true_handled, NULL,
679 NULL,
680 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
683 * GSettings::writable-changed:
684 * @settings: the object on which the signal was emitted
685 * @key: the key
687 * The "writable-changed" signal is emitted when the writability of a
688 * key has potentially changed. You should call
689 * g_settings_is_writable() in order to determine the new status.
691 * This signal supports detailed connections. You can connect to the
692 * detailed signal "writable-changed::x" in order to only receive
693 * callbacks when the writability of "x" changes.
695 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
696 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
697 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
698 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
699 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
700 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
703 * GSettings::writable-change-event:
704 * @settings: the object on which the signal was emitted
705 * @key: the quark of the key, or 0
707 * The "writable-change-event" signal is emitted once per writability
708 * change event that affects this settings object. You should connect
709 * to this signal if you are interested in viewing groups of changes
710 * before they are split out into multiple emissions of the
711 * "writable-changed" signal. For most use cases it is more
712 * appropriate to use the "writable-changed" signal.
714 * In the event that the writability change applies only to a single
715 * key, @key will be set to the #GQuark for that key. In the event
716 * that the writability change affects the entire settings object,
717 * @key will be 0.
719 * The default handler for this signal invokes the "writable-changed"
720 * and "changed" signals for each affected key. This is done because
721 * changes in writability might also imply changes in value (if for
722 * example, a new mandatory setting is introduced). If any other
723 * connected handler returns %TRUE then this default functionality
724 * will be suppressed.
726 * Returns: %TRUE to stop other handlers from being invoked for the
727 * event. FALSE to propagate the event further.
729 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
730 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
731 G_SIGNAL_RUN_LAST,
732 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
733 g_signal_accumulator_true_handled, NULL,
734 NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
737 * GSettings:context:
739 * The name of the context that the settings are stored in.
741 g_object_class_install_property (object_class, PROP_BACKEND,
742 g_param_spec_object ("backend",
743 P_("GSettingsBackend"),
744 P_("The GSettingsBackend for this settings object"),
745 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
746 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
749 * GSettings:settings-schema:
751 * The #GSettingsSchema describing the types of keys for this
752 * #GSettings object.
754 * Ideally, this property would be called 'schema'. #GSettingsSchema
755 * has only existed since version 2.32, however, and before then the
756 * 'schema' property was used to refer to the ID of the schema rather
757 * than the schema itself. Take care.
759 g_object_class_install_property (object_class, PROP_SCHEMA,
760 g_param_spec_boxed ("settings-schema",
761 P_("schema"),
762 P_("The GSettingsSchema for this settings object"),
763 G_TYPE_SETTINGS_SCHEMA,
764 G_PARAM_CONSTRUCT_ONLY |
765 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
768 * GSettings:schema:
770 * The name of the schema that describes the types of keys
771 * for this #GSettings object.
773 * The type of this property is *not* #GSettingsSchema.
774 * #GSettingsSchema has only existed since version 2.32 and
775 * unfortunately this name was used in previous versions to refer to
776 * the schema ID rather than the schema itself. Take care to use the
777 * 'settings-schema' property if you wish to pass in a
778 * #GSettingsSchema.
780 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
781 * version, this property may instead refer to a #GSettingsSchema.
783 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
784 g_param_spec_string ("schema",
785 P_("Schema name"),
786 P_("The name of the schema for this settings object"),
787 NULL,
788 G_PARAM_CONSTRUCT_ONLY |
789 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
792 * GSettings:schema-id:
794 * The name of the schema that describes the types of keys
795 * for this #GSettings object.
797 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
798 g_param_spec_string ("schema-id",
799 P_("Schema name"),
800 P_("The name of the schema for this settings object"),
801 NULL,
802 G_PARAM_CONSTRUCT_ONLY |
803 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
806 * GSettings:path:
808 * The path within the backend where the settings are stored.
810 g_object_class_install_property (object_class, PROP_PATH,
811 g_param_spec_string ("path",
812 P_("Base path"),
813 P_("The path within the backend where the settings are"),
814 NULL,
815 G_PARAM_CONSTRUCT_ONLY |
816 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
819 * GSettings:has-unapplied:
821 * If this property is %TRUE, the #GSettings object has outstanding
822 * changes that will be applied when g_settings_apply() is called.
824 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
825 g_param_spec_boolean ("has-unapplied",
826 P_("Has unapplied changes"),
827 P_("TRUE if there are outstanding changes to apply()"),
828 FALSE,
829 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
832 * GSettings:delay-apply:
834 * Whether the #GSettings object is in 'delay-apply' mode. See
835 * g_settings_delay() for details.
837 * Since: 2.28
839 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
840 g_param_spec_boolean ("delay-apply",
841 P_("Delay-apply mode"),
842 P_("Whether this settings object is in 'delay-apply' mode"),
843 FALSE,
844 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
847 /* Construction (new, new_with_path, etc.) {{{1 */
849 * g_settings_new:
850 * @schema_id: the id of the schema
852 * Creates a new #GSettings object with the schema specified by
853 * @schema_id.
855 * Signals on the newly created #GSettings object will be dispatched
856 * via the thread-default #GMainContext in effect at the time of the
857 * call to g_settings_new(). The new #GSettings will hold a reference
858 * on the context. See g_main_context_push_thread_default().
860 * Returns: a new #GSettings object
862 * Since: 2.26
864 GSettings *
865 g_settings_new (const gchar *schema_id)
867 g_return_val_if_fail (schema_id != NULL, NULL);
869 return g_object_new (G_TYPE_SETTINGS,
870 "schema-id", schema_id,
871 NULL);
875 * g_settings_new_with_path:
876 * @schema_id: the id of the schema
877 * @path: the path to use
879 * Creates a new #GSettings object with the relocatable schema specified
880 * by @schema_id and a given path.
882 * You only need to do this if you want to directly create a settings
883 * object with a schema that doesn't have a specified path of its own.
884 * That's quite rare.
886 * It is a programmer error to call this function for a schema that
887 * has an explicitly specified path.
889 * Returns: a new #GSettings object
891 * Since: 2.26
893 GSettings *
894 g_settings_new_with_path (const gchar *schema_id,
895 const gchar *path)
897 g_return_val_if_fail (schema_id != NULL, NULL);
898 g_return_val_if_fail (path != NULL, NULL);
900 return g_object_new (G_TYPE_SETTINGS,
901 "schema-id", schema_id,
902 "path", path,
903 NULL);
907 * g_settings_new_with_backend:
908 * @schema_id: the id of the schema
909 * @backend: the #GSettingsBackend to use
911 * Creates a new #GSettings object with the schema specified by
912 * @schema_id and a given #GSettingsBackend.
914 * Creating a #GSettings object with a different backend allows accessing
915 * settings from a database other than the usual one. For example, it may make
916 * sense to pass a backend corresponding to the "defaults" settings database on
917 * the system to get a settings object that modifies the system default
918 * settings instead of the settings for this user.
920 * Returns: a new #GSettings object
922 * Since: 2.26
924 GSettings *
925 g_settings_new_with_backend (const gchar *schema_id,
926 GSettingsBackend *backend)
928 g_return_val_if_fail (schema_id != NULL, NULL);
929 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
931 return g_object_new (G_TYPE_SETTINGS,
932 "schema-id", schema_id,
933 "backend", backend,
934 NULL);
938 * g_settings_new_with_backend_and_path:
939 * @schema_id: the id of the schema
940 * @backend: the #GSettingsBackend to use
941 * @path: the path to use
943 * Creates a new #GSettings object with the schema specified by
944 * @schema_id and a given #GSettingsBackend and path.
946 * This is a mix of g_settings_new_with_backend() and
947 * g_settings_new_with_path().
949 * Returns: a new #GSettings object
951 * Since: 2.26
953 GSettings *
954 g_settings_new_with_backend_and_path (const gchar *schema_id,
955 GSettingsBackend *backend,
956 const gchar *path)
958 g_return_val_if_fail (schema_id != NULL, NULL);
959 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
960 g_return_val_if_fail (path != NULL, NULL);
962 return g_object_new (G_TYPE_SETTINGS,
963 "schema-id", schema_id,
964 "backend", backend,
965 "path", path,
966 NULL);
970 * g_settings_new_full:
971 * @schema: a #GSettingsSchema
972 * @backend: (allow-none): a #GSettingsBackend
973 * @path: (allow-none): the path to use
975 * Creates a new #GSettings object with a given schema, backend and
976 * path.
978 * It should be extremely rare that you ever want to use this function.
979 * It is made available for advanced use-cases (such as plugin systems
980 * that want to provide access to schemas loaded from custom locations,
981 * etc).
983 * At the most basic level, a #GSettings object is a pure composition of
984 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
985 * backend, and a #GMainContext to which signals are dispatched.
987 * This constructor therefore gives you full control over constructing
988 * #GSettings instances. The first 4 parameters are given directly as
989 * @schema, @backend and @path, and the main context is taken from the
990 * thread-default (as per g_settings_new()).
992 * If @backend is %NULL then the default backend is used.
994 * If @path is %NULL then the path from the schema is used. It is an
995 * error f @path is %NULL and the schema has no path of its own or if
996 * @path is non-%NULL and not equal to the path that the schema does
997 * have.
999 * Returns: a new #GSettings object
1001 * Since: 2.32
1003 GSettings *
1004 g_settings_new_full (GSettingsSchema *schema,
1005 GSettingsBackend *backend,
1006 const gchar *path)
1008 return g_object_new (G_TYPE_SETTINGS,
1009 "settings-schema", schema,
1010 "backend", backend,
1011 "path", path,
1012 NULL);
1015 /* Internal read/write utilities {{{1 */
1016 static gboolean
1017 g_settings_write_to_backend (GSettings *settings,
1018 GSettingsSchemaKey *key,
1019 GVariant *value)
1021 gboolean success;
1022 gchar *path;
1024 path = g_strconcat (settings->priv->path, key->name, NULL);
1025 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1026 g_free (path);
1028 return success;
1031 static GVariant *
1032 g_settings_read_from_backend (GSettings *settings,
1033 GSettingsSchemaKey *key)
1035 GVariant *value;
1036 GVariant *fixup;
1037 gchar *path;
1039 path = g_strconcat (settings->priv->path, key->name, NULL);
1040 value = g_settings_backend_read (settings->priv->backend, path, key->type, FALSE);
1041 g_free (path);
1043 if (value != NULL)
1045 fixup = g_settings_schema_key_range_fixup (key, value);
1046 g_variant_unref (value);
1048 else
1049 fixup = NULL;
1051 return fixup;
1054 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1056 * g_settings_get_value:
1057 * @settings: a #GSettings object
1058 * @key: the key to get the value for
1060 * Gets the value that is stored in @settings for @key.
1062 * It is a programmer error to give a @key that isn't contained in the
1063 * schema for @settings.
1065 * Returns: a new #GVariant
1067 * Since: 2.26
1069 GVariant *
1070 g_settings_get_value (GSettings *settings,
1071 const gchar *key)
1073 GSettingsSchemaKey skey;
1074 GVariant *value;
1076 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1077 g_return_val_if_fail (key != NULL, NULL);
1079 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1080 value = g_settings_read_from_backend (settings, &skey);
1082 if (value == NULL)
1083 value = g_settings_schema_key_get_translated_default (&skey);
1085 if (value == NULL)
1086 value = g_variant_ref (skey.default_value);
1088 g_settings_schema_key_clear (&skey);
1090 return value;
1094 * g_settings_get_enum:
1095 * @settings: a #GSettings object
1096 * @key: the key to get the value for
1098 * Gets the value that is stored in @settings for @key and converts it
1099 * to the enum value that it represents.
1101 * In order to use this function the type of the value must be a string
1102 * and it must be marked in the schema file as an enumerated type.
1104 * It is a programmer error to give a @key that isn't contained in the
1105 * schema for @settings or is not marked as an enumerated type.
1107 * If the value stored in the configuration database is not a valid
1108 * value for the enumerated type then this function will return the
1109 * default value.
1111 * Returns: the enum value
1113 * Since: 2.26
1115 gint
1116 g_settings_get_enum (GSettings *settings,
1117 const gchar *key)
1119 GSettingsSchemaKey skey;
1120 GVariant *value;
1121 gint result;
1123 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1124 g_return_val_if_fail (key != NULL, -1);
1126 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1128 if (!skey.is_enum)
1130 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1131 "associated with an enumerated type", skey.name);
1132 g_settings_schema_key_clear (&skey);
1133 return -1;
1136 value = g_settings_read_from_backend (settings, &skey);
1138 if (value == NULL)
1139 value = g_settings_schema_key_get_translated_default (&skey);
1141 if (value == NULL)
1142 value = g_variant_ref (skey.default_value);
1144 result = g_settings_schema_key_to_enum (&skey, value);
1145 g_settings_schema_key_clear (&skey);
1146 g_variant_unref (value);
1148 return result;
1152 * g_settings_set_enum:
1153 * @settings: a #GSettings object
1154 * @key: a key, within @settings
1155 * @value: an enumerated value
1157 * Looks up the enumerated type nick for @value and writes it to @key,
1158 * within @settings.
1160 * It is a programmer error to give a @key that isn't contained in the
1161 * schema for @settings or is not marked as an enumerated type, or for
1162 * @value not to be a valid value for the named type.
1164 * After performing the write, accessing @key directly with
1165 * g_settings_get_string() will return the 'nick' associated with
1166 * @value.
1168 * Returns: %TRUE, if the set succeeds
1170 gboolean
1171 g_settings_set_enum (GSettings *settings,
1172 const gchar *key,
1173 gint value)
1175 GSettingsSchemaKey skey;
1176 GVariant *variant;
1177 gboolean success;
1179 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1180 g_return_val_if_fail (key != NULL, FALSE);
1182 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1184 if (!skey.is_enum)
1186 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1187 "associated with an enumerated type", skey.name);
1188 return FALSE;
1191 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1193 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1194 "in schema `%s'. Doing nothing.", value, skey.name,
1195 g_settings_schema_get_id (skey.schema));
1196 g_settings_schema_key_clear (&skey);
1197 return FALSE;
1200 success = g_settings_write_to_backend (settings, &skey, variant);
1201 g_settings_schema_key_clear (&skey);
1203 return success;
1207 * g_settings_get_flags:
1208 * @settings: a #GSettings object
1209 * @key: the key to get the value for
1211 * Gets the value that is stored in @settings for @key and converts it
1212 * to the flags value that it represents.
1214 * In order to use this function the type of the value must be an array
1215 * of strings and it must be marked in the schema file as an flags type.
1217 * It is a programmer error to give a @key that isn't contained in the
1218 * schema for @settings or is not marked as a flags type.
1220 * If the value stored in the configuration database is not a valid
1221 * value for the flags type then this function will return the default
1222 * value.
1224 * Returns: the flags value
1226 * Since: 2.26
1228 guint
1229 g_settings_get_flags (GSettings *settings,
1230 const gchar *key)
1232 GSettingsSchemaKey skey;
1233 GVariant *value;
1234 guint result;
1236 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1237 g_return_val_if_fail (key != NULL, -1);
1239 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1241 if (!skey.is_flags)
1243 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1244 "associated with a flags type", skey.name);
1245 g_settings_schema_key_clear (&skey);
1246 return -1;
1249 value = g_settings_read_from_backend (settings, &skey);
1251 if (value == NULL)
1252 value = g_settings_schema_key_get_translated_default (&skey);
1254 if (value == NULL)
1255 value = g_variant_ref (skey.default_value);
1257 result = g_settings_schema_key_to_flags (&skey, value);
1258 g_settings_schema_key_clear (&skey);
1259 g_variant_unref (value);
1261 return result;
1265 * g_settings_set_flags:
1266 * @settings: a #GSettings object
1267 * @key: a key, within @settings
1268 * @value: a flags value
1270 * Looks up the flags type nicks for the bits specified by @value, puts
1271 * them in an array of strings and writes the array to @key, within
1272 * @settings.
1274 * It is a programmer error to give a @key that isn't contained in the
1275 * schema for @settings or is not marked as a flags type, or for @value
1276 * to contain any bits that are not value for the named type.
1278 * After performing the write, accessing @key directly with
1279 * g_settings_get_strv() will return an array of 'nicks'; one for each
1280 * bit in @value.
1282 * Returns: %TRUE, if the set succeeds
1284 gboolean
1285 g_settings_set_flags (GSettings *settings,
1286 const gchar *key,
1287 guint value)
1289 GSettingsSchemaKey skey;
1290 GVariant *variant;
1291 gboolean success;
1293 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1294 g_return_val_if_fail (key != NULL, FALSE);
1296 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1298 if (!skey.is_flags)
1300 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1301 "associated with a flags type", skey.name);
1302 return FALSE;
1305 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1307 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1308 "for key `%s' in schema `%s'. Doing nothing.",
1309 value, skey.name, g_settings_schema_get_id (skey.schema));
1310 g_settings_schema_key_clear (&skey);
1311 return FALSE;
1314 success = g_settings_write_to_backend (settings, &skey, variant);
1315 g_settings_schema_key_clear (&skey);
1317 return success;
1321 * g_settings_set_value:
1322 * @settings: a #GSettings object
1323 * @key: the name of the key to set
1324 * @value: a #GVariant of the correct type
1326 * Sets @key in @settings to @value.
1328 * It is a programmer error to give a @key that isn't contained in the
1329 * schema for @settings or for @value to have the incorrect type, per
1330 * the schema.
1332 * If @value is floating then this function consumes the reference.
1334 * Returns: %TRUE if setting the key succeeded,
1335 * %FALSE if the key was not writable
1337 * Since: 2.26
1339 gboolean
1340 g_settings_set_value (GSettings *settings,
1341 const gchar *key,
1342 GVariant *value)
1344 GSettingsSchemaKey skey;
1345 gboolean success;
1347 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1348 g_return_val_if_fail (key != NULL, FALSE);
1350 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1352 if (!g_settings_schema_key_type_check (&skey, value))
1354 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1355 key,
1356 g_settings_schema_get_id (settings->priv->schema),
1357 g_variant_type_peek_string (skey.type),
1358 g_variant_get_type_string (value));
1360 return FALSE;
1363 if (!g_settings_schema_key_range_check (&skey, value))
1365 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1366 "is outside of valid range",
1367 key,
1368 g_settings_schema_get_id (settings->priv->schema));
1370 return FALSE;
1373 success = g_settings_write_to_backend (settings, &skey, value);
1374 g_settings_schema_key_clear (&skey);
1376 return success;
1380 * g_settings_get:
1381 * @settings: a #GSettings object
1382 * @key: the key to get the value for
1383 * @format: a #GVariant format string
1384 * @...: arguments as per @format
1386 * Gets the value that is stored at @key in @settings.
1388 * A convenience function that combines g_settings_get_value() with
1389 * g_variant_get().
1391 * It is a programmer error to give a @key that isn't contained in the
1392 * schema for @settings or for the #GVariantType of @format to mismatch
1393 * the type given in the schema.
1395 * Since: 2.26
1397 void
1398 g_settings_get (GSettings *settings,
1399 const gchar *key,
1400 const gchar *format,
1401 ...)
1403 GVariant *value;
1404 va_list ap;
1406 value = g_settings_get_value (settings, key);
1408 va_start (ap, format);
1409 g_variant_get_va (value, format, NULL, &ap);
1410 va_end (ap);
1412 g_variant_unref (value);
1416 * g_settings_set:
1417 * @settings: a #GSettings object
1418 * @key: the name of the key to set
1419 * @format: a #GVariant format string
1420 * @...: arguments as per @format
1422 * Sets @key in @settings to @value.
1424 * A convenience function that combines g_settings_set_value() with
1425 * g_variant_new().
1427 * It is a programmer error to give a @key that isn't contained in the
1428 * schema for @settings or for the #GVariantType of @format to mismatch
1429 * the type given in the schema.
1431 * Returns: %TRUE if setting the key succeeded,
1432 * %FALSE if the key was not writable
1434 * Since: 2.26
1436 gboolean
1437 g_settings_set (GSettings *settings,
1438 const gchar *key,
1439 const gchar *format,
1440 ...)
1442 GVariant *value;
1443 va_list ap;
1445 va_start (ap, format);
1446 value = g_variant_new_va (format, NULL, &ap);
1447 va_end (ap);
1449 return g_settings_set_value (settings, key, value);
1453 * g_settings_get_mapped:
1454 * @settings: a #GSettings object
1455 * @key: the key to get the value for
1456 * @mapping: (scope call): the function to map the value in the
1457 * settings database to the value used by the application
1458 * @user_data: user data for @mapping
1460 * Gets the value that is stored at @key in @settings, subject to
1461 * application-level validation/mapping.
1463 * You should use this function when the application needs to perform
1464 * some processing on the value of the key (for example, parsing). The
1465 * @mapping function performs that processing. If the function
1466 * indicates that the processing was unsuccessful (due to a parse error,
1467 * for example) then the mapping is tried again with another value.
1469 * This allows a robust 'fall back to defaults' behaviour to be
1470 * implemented somewhat automatically.
1472 * The first value that is tried is the user's setting for the key. If
1473 * the mapping function fails to map this value, other values may be
1474 * tried in an unspecified order (system or site defaults, translated
1475 * schema default values, untranslated schema default values, etc).
1477 * If the mapping function fails for all possible values, one additional
1478 * attempt is made: the mapping function is called with a %NULL value.
1479 * If the mapping function still indicates failure at this point then
1480 * the application will be aborted.
1482 * The result parameter for the @mapping function is pointed to a
1483 * #gpointer which is initially set to %NULL. The same pointer is given
1484 * to each invocation of @mapping. The final value of that #gpointer is
1485 * what is returned by this function. %NULL is valid; it is returned
1486 * just as any other value would be.
1488 * Returns: (transfer full): the result, which may be %NULL
1490 gpointer
1491 g_settings_get_mapped (GSettings *settings,
1492 const gchar *key,
1493 GSettingsGetMapping mapping,
1494 gpointer user_data)
1496 gpointer result = NULL;
1497 GSettingsSchemaKey skey;
1498 GVariant *value;
1499 gboolean okay;
1501 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1502 g_return_val_if_fail (key != NULL, NULL);
1503 g_return_val_if_fail (mapping != NULL, NULL);
1505 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1507 if ((value = g_settings_read_from_backend (settings, &skey)))
1509 okay = mapping (value, &result, user_data);
1510 g_variant_unref (value);
1511 if (okay) goto okay;
1514 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1516 okay = mapping (value, &result, user_data);
1517 g_variant_unref (value);
1518 if (okay) goto okay;
1521 if (mapping (skey.default_value, &result, user_data))
1522 goto okay;
1524 if (!mapping (NULL, &result, user_data))
1525 g_error ("The mapping function given to g_settings_get_mapped() for key "
1526 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1527 key, g_settings_schema_get_id (settings->priv->schema));
1529 okay:
1530 g_settings_schema_key_clear (&skey);
1532 return result;
1535 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1537 * g_settings_get_string:
1538 * @settings: a #GSettings object
1539 * @key: the key to get the value for
1541 * Gets the value that is stored at @key in @settings.
1543 * A convenience variant of g_settings_get() for strings.
1545 * It is a programmer error to give a @key that isn't specified as
1546 * having a string type in the schema for @settings.
1548 * Returns: a newly-allocated string
1550 * Since: 2.26
1552 gchar *
1553 g_settings_get_string (GSettings *settings,
1554 const gchar *key)
1556 GVariant *value;
1557 gchar *result;
1559 value = g_settings_get_value (settings, key);
1560 result = g_variant_dup_string (value, NULL);
1561 g_variant_unref (value);
1563 return result;
1567 * g_settings_set_string:
1568 * @settings: a #GSettings object
1569 * @key: the name of the key to set
1570 * @value: the value to set it to
1572 * Sets @key in @settings to @value.
1574 * A convenience variant of g_settings_set() for strings.
1576 * It is a programmer error to give a @key that isn't specified as
1577 * having a string type in the schema for @settings.
1579 * Returns: %TRUE if setting the key succeeded,
1580 * %FALSE if the key was not writable
1582 * Since: 2.26
1584 gboolean
1585 g_settings_set_string (GSettings *settings,
1586 const gchar *key,
1587 const gchar *value)
1589 return g_settings_set_value (settings, key, g_variant_new_string (value));
1593 * g_settings_get_int:
1594 * @settings: a #GSettings object
1595 * @key: the key to get the value for
1597 * Gets the value that is stored at @key in @settings.
1599 * A convenience variant of g_settings_get() for 32-bit integers.
1601 * It is a programmer error to give a @key that isn't specified as
1602 * having a int32 type in the schema for @settings.
1604 * Returns: an integer
1606 * Since: 2.26
1608 gint
1609 g_settings_get_int (GSettings *settings,
1610 const gchar *key)
1612 GVariant *value;
1613 gint result;
1615 value = g_settings_get_value (settings, key);
1616 result = g_variant_get_int32 (value);
1617 g_variant_unref (value);
1619 return result;
1623 * g_settings_set_int:
1624 * @settings: a #GSettings object
1625 * @key: the name of the key to set
1626 * @value: the value to set it to
1628 * Sets @key in @settings to @value.
1630 * A convenience variant of g_settings_set() for 32-bit integers.
1632 * It is a programmer error to give a @key that isn't specified as
1633 * having a int32 type in the schema for @settings.
1635 * Returns: %TRUE if setting the key succeeded,
1636 * %FALSE if the key was not writable
1638 * Since: 2.26
1640 gboolean
1641 g_settings_set_int (GSettings *settings,
1642 const gchar *key,
1643 gint value)
1645 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1649 * g_settings_get_uint:
1650 * @settings: a #GSettings object
1651 * @key: the key to get the value for
1653 * Gets the value that is stored at @key in @settings.
1655 * A convenience variant of g_settings_get() for 32-bit unsigned
1656 * integers.
1658 * It is a programmer error to give a @key that isn't specified as
1659 * having a uint32 type in the schema for @settings.
1661 * Returns: an unsigned integer
1663 * Since: 2.30
1665 guint
1666 g_settings_get_uint (GSettings *settings,
1667 const gchar *key)
1669 GVariant *value;
1670 guint result;
1672 value = g_settings_get_value (settings, key);
1673 result = g_variant_get_uint32 (value);
1674 g_variant_unref (value);
1676 return result;
1680 * g_settings_set_uint:
1681 * @settings: a #GSettings object
1682 * @key: the name of the key to set
1683 * @value: the value to set it to
1685 * Sets @key in @settings to @value.
1687 * A convenience variant of g_settings_set() for 32-bit unsigned
1688 * integers.
1690 * It is a programmer error to give a @key that isn't specified as
1691 * having a uint32 type in the schema for @settings.
1693 * Returns: %TRUE if setting the key succeeded,
1694 * %FALSE if the key was not writable
1696 * Since: 2.30
1698 gboolean
1699 g_settings_set_uint (GSettings *settings,
1700 const gchar *key,
1701 guint value)
1703 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1707 * g_settings_get_double:
1708 * @settings: a #GSettings object
1709 * @key: the key to get the value for
1711 * Gets the value that is stored at @key in @settings.
1713 * A convenience variant of g_settings_get() for doubles.
1715 * It is a programmer error to give a @key that isn't specified as
1716 * having a 'double' type in the schema for @settings.
1718 * Returns: a double
1720 * Since: 2.26
1722 gdouble
1723 g_settings_get_double (GSettings *settings,
1724 const gchar *key)
1726 GVariant *value;
1727 gdouble result;
1729 value = g_settings_get_value (settings, key);
1730 result = g_variant_get_double (value);
1731 g_variant_unref (value);
1733 return result;
1737 * g_settings_set_double:
1738 * @settings: a #GSettings object
1739 * @key: the name of the key to set
1740 * @value: the value to set it to
1742 * Sets @key in @settings to @value.
1744 * A convenience variant of g_settings_set() for doubles.
1746 * It is a programmer error to give a @key that isn't specified as
1747 * having a 'double' type in the schema for @settings.
1749 * Returns: %TRUE if setting the key succeeded,
1750 * %FALSE if the key was not writable
1752 * Since: 2.26
1754 gboolean
1755 g_settings_set_double (GSettings *settings,
1756 const gchar *key,
1757 gdouble value)
1759 return g_settings_set_value (settings, key, g_variant_new_double (value));
1763 * g_settings_get_boolean:
1764 * @settings: a #GSettings object
1765 * @key: the key to get the value for
1767 * Gets the value that is stored at @key in @settings.
1769 * A convenience variant of g_settings_get() for booleans.
1771 * It is a programmer error to give a @key that isn't specified as
1772 * having a boolean type in the schema for @settings.
1774 * Returns: a boolean
1776 * Since: 2.26
1778 gboolean
1779 g_settings_get_boolean (GSettings *settings,
1780 const gchar *key)
1782 GVariant *value;
1783 gboolean result;
1785 value = g_settings_get_value (settings, key);
1786 result = g_variant_get_boolean (value);
1787 g_variant_unref (value);
1789 return result;
1793 * g_settings_set_boolean:
1794 * @settings: a #GSettings object
1795 * @key: the name of the key to set
1796 * @value: the value to set it to
1798 * Sets @key in @settings to @value.
1800 * A convenience variant of g_settings_set() for booleans.
1802 * It is a programmer error to give a @key that isn't specified as
1803 * having a boolean type in the schema for @settings.
1805 * Returns: %TRUE if setting the key succeeded,
1806 * %FALSE if the key was not writable
1808 * Since: 2.26
1810 gboolean
1811 g_settings_set_boolean (GSettings *settings,
1812 const gchar *key,
1813 gboolean value)
1815 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1819 * g_settings_get_strv:
1820 * @settings: a #GSettings object
1821 * @key: the key to get the value for
1823 * A convenience variant of g_settings_get() for string arrays.
1825 * It is a programmer error to give a @key that isn't specified as
1826 * having an array of strings type in the schema for @settings.
1828 * Returns: (array zero-terminated=1) (transfer full): a
1829 * newly-allocated, %NULL-terminated array of strings, the value that
1830 * is stored at @key in @settings.
1832 * Since: 2.26
1834 gchar **
1835 g_settings_get_strv (GSettings *settings,
1836 const gchar *key)
1838 GVariant *value;
1839 gchar **result;
1841 value = g_settings_get_value (settings, key);
1842 result = g_variant_dup_strv (value, NULL);
1843 g_variant_unref (value);
1845 return result;
1849 * g_settings_set_strv:
1850 * @settings: a #GSettings object
1851 * @key: the name of the key to set
1852 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1854 * Sets @key in @settings to @value.
1856 * A convenience variant of g_settings_set() for string arrays. If
1857 * @value is %NULL, then @key is set to be the empty array.
1859 * It is a programmer error to give a @key that isn't specified as
1860 * having an array of strings type in the schema for @settings.
1862 * Returns: %TRUE if setting the key succeeded,
1863 * %FALSE if the key was not writable
1865 * Since: 2.26
1867 gboolean
1868 g_settings_set_strv (GSettings *settings,
1869 const gchar *key,
1870 const gchar * const *value)
1872 GVariant *array;
1874 if (value != NULL)
1875 array = g_variant_new_strv (value, -1);
1876 else
1877 array = g_variant_new_strv (NULL, 0);
1879 return g_settings_set_value (settings, key, array);
1882 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1884 * g_settings_delay:
1885 * @settings: a #GSettings object
1887 * Changes the #GSettings object into 'delay-apply' mode. In this
1888 * mode, changes to @settings are not immediately propagated to the
1889 * backend, but kept locally until g_settings_apply() is called.
1891 * Since: 2.26
1893 void
1894 g_settings_delay (GSettings *settings)
1896 g_return_if_fail (G_IS_SETTINGS (settings));
1898 if (settings->priv->delayed)
1899 return;
1901 settings->priv->delayed =
1902 g_delayed_settings_backend_new (settings->priv->backend,
1903 settings,
1904 settings->priv->main_context);
1905 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1906 g_object_unref (settings->priv->backend);
1908 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1909 g_settings_backend_watch (settings->priv->backend,
1910 &listener_vtable, G_OBJECT (settings),
1911 settings->priv->main_context);
1913 g_object_notify (G_OBJECT (settings), "delay-apply");
1917 * g_settings_apply:
1918 * @settings: a #GSettings instance
1920 * Applies any changes that have been made to the settings. This
1921 * function does nothing unless @settings is in 'delay-apply' mode;
1922 * see g_settings_delay(). In the normal case settings are always
1923 * applied immediately.
1925 void
1926 g_settings_apply (GSettings *settings)
1928 if (settings->priv->delayed)
1930 GDelayedSettingsBackend *delayed;
1932 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1933 g_delayed_settings_backend_apply (delayed);
1938 * g_settings_revert:
1939 * @settings: a #GSettings instance
1941 * Reverts all non-applied changes to the settings. This function
1942 * does nothing unless @settings is in 'delay-apply' mode; see
1943 * g_settings_delay(). In the normal case settings are always applied
1944 * immediately.
1946 * Change notifications will be emitted for affected keys.
1948 void
1949 g_settings_revert (GSettings *settings)
1951 if (settings->priv->delayed)
1953 GDelayedSettingsBackend *delayed;
1955 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1956 g_delayed_settings_backend_revert (delayed);
1961 * g_settings_get_has_unapplied:
1962 * @settings: a #GSettings object
1964 * Returns whether the #GSettings object has any unapplied
1965 * changes. This can only be the case if it is in 'delayed-apply' mode.
1967 * Returns: %TRUE if @settings has unapplied changes
1969 * Since: 2.26
1971 gboolean
1972 g_settings_get_has_unapplied (GSettings *settings)
1974 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1976 return settings->priv->delayed &&
1977 g_delayed_settings_backend_get_has_unapplied (
1978 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1981 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
1983 * g_settings_reset:
1984 * @settings: a #GSettings object
1985 * @key: the name of a key
1987 * Resets @key to its default value.
1989 * This call resets the key, as much as possible, to its default value.
1990 * That might the value specified in the schema or the one set by the
1991 * administrator.
1993 void
1994 g_settings_reset (GSettings *settings,
1995 const gchar *key)
1997 gchar *path;
1999 path = g_strconcat (settings->priv->path, key, NULL);
2000 g_settings_backend_reset (settings->priv->backend, path, NULL);
2001 g_free (path);
2005 * g_settings_sync:
2007 * Ensures that all pending operations for the given are complete for
2008 * the default backend.
2010 * Writes made to a #GSettings are handled asynchronously. For this
2011 * reason, it is very unlikely that the changes have it to disk by the
2012 * time g_settings_set() returns.
2014 * This call will block until all of the writes have made it to the
2015 * backend. Since the mainloop is not running, no change notifications
2016 * will be dispatched during this call (but some may be queued by the
2017 * time the call is done).
2019 void
2020 g_settings_sync (void)
2022 g_settings_backend_sync_default ();
2026 * g_settings_is_writable:
2027 * @settings: a #GSettings object
2028 * @name: the name of a key
2030 * Finds out if a key can be written or not
2032 * Returns: %TRUE if the key @name is writable
2034 * Since: 2.26
2036 gboolean
2037 g_settings_is_writable (GSettings *settings,
2038 const gchar *name)
2040 gboolean writable;
2041 gchar *path;
2043 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2045 path = g_strconcat (settings->priv->path, name, NULL);
2046 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2047 g_free (path);
2049 return writable;
2053 * g_settings_get_child:
2054 * @settings: a #GSettings object
2055 * @name: the name of the 'child' schema
2057 * Creates a 'child' settings object which has a base path of
2058 * <replaceable>base-path</replaceable>/@name, where
2059 * <replaceable>base-path</replaceable> is the base path of @settings.
2061 * The schema for the child settings object must have been declared
2062 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2064 * Returns: (transfer full): a 'child' settings object
2066 * Since: 2.26
2068 GSettings *
2069 g_settings_get_child (GSettings *settings,
2070 const gchar *name)
2072 const gchar *child_schema;
2073 gchar *child_path;
2074 gchar *child_name;
2075 GSettings *child;
2077 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2079 child_name = g_strconcat (name, "/", NULL);
2080 child_schema = g_settings_schema_get_string (settings->priv->schema,
2081 child_name);
2082 if (child_schema == NULL)
2083 g_error ("Schema '%s' has no child '%s'",
2084 g_settings_schema_get_id (settings->priv->schema), name);
2086 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2087 child = g_object_new (G_TYPE_SETTINGS,
2088 "schema-id", child_schema,
2089 "path", child_path,
2090 NULL);
2091 g_free (child_path);
2092 g_free (child_name);
2094 return child;
2098 * g_settings_list_keys:
2099 * @settings: a #GSettings object
2101 * Introspects the list of keys on @settings.
2103 * You should probably not be calling this function from "normal" code
2104 * (since you should already know what keys are in your schema). This
2105 * function is intended for introspection reasons.
2107 * You should free the return value with g_strfreev() when you are done
2108 * with it.
2110 * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2112 gchar **
2113 g_settings_list_keys (GSettings *settings)
2115 const GQuark *keys;
2116 gchar **strv;
2117 gint n_keys;
2118 gint i, j;
2120 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2121 strv = g_new (gchar *, n_keys + 1);
2122 for (i = j = 0; i < n_keys; i++)
2124 const gchar *key = g_quark_to_string (keys[i]);
2126 if (!g_str_has_suffix (key, "/"))
2127 strv[j++] = g_strdup (key);
2129 strv[j] = NULL;
2131 return strv;
2135 * g_settings_list_children:
2136 * @settings: a #GSettings object
2138 * Gets the list of children on @settings.
2140 * The list is exactly the list of strings for which it is not an error
2141 * to call g_settings_get_child().
2143 * For GSettings objects that are lists, this value can change at any
2144 * time and you should connect to the "children-changed" signal to watch
2145 * for those changes. Note that there is a race condition here: you may
2146 * request a child after listing it only for it to have been destroyed
2147 * in the meantime. For this reason, g_settings_get_child() may return
2148 * %NULL even for a child that was listed by this function.
2150 * For GSettings objects that are not lists, you should probably not be
2151 * calling this function from "normal" code (since you should already
2152 * know what children are in your schema). This function may still be
2153 * useful there for introspection reasons, however.
2155 * You should free the return value with g_strfreev() when you are done
2156 * with it.
2158 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2160 gchar **
2161 g_settings_list_children (GSettings *settings)
2163 const GQuark *keys;
2164 gchar **strv;
2165 gint n_keys;
2166 gint i, j;
2168 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2169 strv = g_new (gchar *, n_keys + 1);
2170 for (i = j = 0; i < n_keys; i++)
2172 const gchar *key = g_quark_to_string (keys[i]);
2174 if (g_str_has_suffix (key, "/"))
2176 gint length = strlen (key);
2178 strv[j] = g_memdup (key, length);
2179 strv[j][length - 1] = '\0';
2180 j++;
2183 strv[j] = NULL;
2185 return strv;
2189 * g_settings_get_range:
2190 * @settings: a #GSettings
2191 * @key: the key to query the range of
2193 * Queries the range of a key.
2195 * This function will return a #GVariant that fully describes the range
2196 * of values that are valid for @key.
2198 * The type of #GVariant returned is <literal>(sv)</literal>. The
2199 * string describes the type of range restriction in effect. The type
2200 * and meaning of the value contained in the variant depends on the
2201 * string.
2203 * If the string is <literal>'type'</literal> then the variant contains
2204 * an empty array. The element type of that empty array is the expected
2205 * type of value and all values of that type are valid.
2207 * If the string is <literal>'enum'</literal> then the variant contains
2208 * an array enumerating the possible values. Each item in the array is
2209 * a possible valid value and no other values are valid.
2211 * If the string is <literal>'flags'</literal> then the variant contains
2212 * an array. Each item in the array is a value that may appear zero or
2213 * one times in an array to be used as the value for this key. For
2214 * example, if the variant contained the array <literal>['x',
2215 * 'y']</literal> then the valid values for the key would be
2216 * <literal>[]</literal>, <literal>['x']</literal>,
2217 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2218 * <literal>['y', 'x']</literal>.
2220 * Finally, if the string is <literal>'range'</literal> then the variant
2221 * contains a pair of like-typed values -- the minimum and maximum
2222 * permissible values for this key.
2224 * This information should not be used by normal programs. It is
2225 * considered to be a hint for introspection purposes. Normal programs
2226 * should already know what is permitted by their own schema. The
2227 * format may change in any way in the future -- but particularly, new
2228 * forms may be added to the possibilities described above.
2230 * It is a programmer error to give a @key that isn't contained in the
2231 * schema for @settings.
2233 * You should free the returned value with g_variant_unref() when it is
2234 * no longer needed.
2236 * Returns: a #GVariant describing the range
2238 * Since: 2.28
2240 GVariant *
2241 g_settings_get_range (GSettings *settings,
2242 const gchar *key)
2244 GSettingsSchemaKey skey;
2245 const gchar *type;
2246 GVariant *range;
2248 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2250 if (skey.minimum)
2252 range = g_variant_new ("(**)", skey.minimum, skey.maximum);
2253 type = "range";
2255 else if (skey.strinfo)
2257 range = strinfo_enumerate (skey.strinfo, skey.strinfo_length);
2258 type = skey.is_flags ? "flags" : "enum";
2260 else
2262 range = g_variant_new_array (skey.type, NULL, 0);
2263 type = "type";
2266 g_settings_schema_key_clear (&skey);
2268 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2272 * g_settings_range_check:
2273 * @settings: a #GSettings
2274 * @key: the key to check
2275 * @value: the value to check
2277 * Checks if the given @value is of the correct type and within the
2278 * permitted range for @key.
2280 * This API is not intended to be used by normal programs -- they should
2281 * already know what is permitted by their own schemas. This API is
2282 * meant to be used by programs such as editors or commandline tools.
2284 * It is a programmer error to give a @key that isn't contained in the
2285 * schema for @settings.
2287 * Returns: %TRUE if @value is valid for @key
2289 * Since: 2.28
2291 gboolean
2292 g_settings_range_check (GSettings *settings,
2293 const gchar *key,
2294 GVariant *value)
2296 GSettingsSchemaKey skey;
2297 gboolean good;
2299 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2300 good = g_settings_schema_key_type_check (&skey, value) &&
2301 g_settings_schema_key_range_check (&skey, value);
2302 g_settings_schema_key_clear (&skey);
2304 return good;
2307 /* Binding {{{1 */
2308 typedef struct
2310 GSettingsSchemaKey key;
2311 GSettings *settings;
2312 GObject *object;
2314 GSettingsBindGetMapping get_mapping;
2315 GSettingsBindSetMapping set_mapping;
2316 gpointer user_data;
2317 GDestroyNotify destroy;
2319 guint writable_handler_id;
2320 guint property_handler_id;
2321 const GParamSpec *property;
2322 guint key_handler_id;
2324 /* prevent recursion */
2325 gboolean running;
2326 } GSettingsBinding;
2328 static void
2329 g_settings_binding_free (gpointer data)
2331 GSettingsBinding *binding = data;
2333 g_assert (!binding->running);
2335 if (binding->writable_handler_id)
2336 g_signal_handler_disconnect (binding->settings,
2337 binding->writable_handler_id);
2339 if (binding->key_handler_id)
2340 g_signal_handler_disconnect (binding->settings,
2341 binding->key_handler_id);
2343 if (g_signal_handler_is_connected (binding->object,
2344 binding->property_handler_id))
2345 g_signal_handler_disconnect (binding->object,
2346 binding->property_handler_id);
2348 g_settings_schema_key_clear (&binding->key);
2350 if (binding->destroy)
2351 binding->destroy (binding->user_data);
2353 g_object_unref (binding->settings);
2355 g_slice_free (GSettingsBinding, binding);
2358 static GQuark
2359 g_settings_binding_quark (const char *property)
2361 GQuark quark;
2362 gchar *tmp;
2364 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2365 quark = g_quark_from_string (tmp);
2366 g_free (tmp);
2368 return quark;
2371 static void
2372 g_settings_binding_key_changed (GSettings *settings,
2373 const gchar *key,
2374 gpointer user_data)
2376 GSettingsBinding *binding = user_data;
2377 GValue value = G_VALUE_INIT;
2378 GVariant *variant;
2380 g_assert (settings == binding->settings);
2381 g_assert (key == binding->key.name);
2383 if (binding->running)
2384 return;
2386 binding->running = TRUE;
2388 g_value_init (&value, binding->property->value_type);
2390 variant = g_settings_read_from_backend (binding->settings, &binding->key);
2391 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2393 /* silently ignore errors in the user's config database */
2394 g_variant_unref (variant);
2395 variant = NULL;
2398 if (variant == NULL)
2400 variant = g_settings_schema_key_get_translated_default (&binding->key);
2401 if (variant &&
2402 !binding->get_mapping (&value, variant, binding->user_data))
2404 /* flag translation errors with a warning */
2405 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2406 "was rejected by the binding mapping function",
2407 binding->key.unparsed, binding->key.name,
2408 g_settings_schema_get_id (binding->key.schema));
2409 g_variant_unref (variant);
2410 variant = NULL;
2414 if (variant == NULL)
2416 variant = g_variant_ref (binding->key.default_value);
2417 if (!binding->get_mapping (&value, variant, binding->user_data))
2418 g_error ("The schema default value for key `%s' in schema `%s' "
2419 "was rejected by the binding mapping function.",
2420 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2423 g_object_set_property (binding->object, binding->property->name, &value);
2424 g_variant_unref (variant);
2425 g_value_unset (&value);
2427 binding->running = FALSE;
2430 static void
2431 g_settings_binding_property_changed (GObject *object,
2432 const GParamSpec *pspec,
2433 gpointer user_data)
2435 GSettingsBinding *binding = user_data;
2436 GValue value = G_VALUE_INIT;
2437 GVariant *variant;
2439 g_assert (object == binding->object);
2440 g_assert (pspec == binding->property);
2442 if (binding->running)
2443 return;
2445 binding->running = TRUE;
2447 g_value_init (&value, pspec->value_type);
2448 g_object_get_property (object, pspec->name, &value);
2449 if ((variant = binding->set_mapping (&value, binding->key.type,
2450 binding->user_data)))
2452 g_variant_take_ref (variant);
2454 if (!g_settings_schema_key_type_check (&binding->key, variant))
2456 g_critical ("binding mapping function for key `%s' returned "
2457 "GVariant of type `%s' when type `%s' was requested",
2458 binding->key.name, g_variant_get_type_string (variant),
2459 g_variant_type_dup_string (binding->key.type));
2460 return;
2463 if (!g_settings_schema_key_range_check (&binding->key, variant))
2465 g_critical ("GObject property `%s' on a `%s' object is out of "
2466 "schema-specified range for key `%s' of `%s': %s",
2467 binding->property->name, g_type_name (binding->property->owner_type),
2468 binding->key.name, g_settings_schema_get_id (binding->key.schema),
2469 g_variant_print (variant, TRUE));
2470 return;
2473 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2474 g_variant_unref (variant);
2476 g_value_unset (&value);
2478 binding->running = FALSE;
2481 static gboolean
2482 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2483 GVariant *variant,
2484 gpointer user_data)
2486 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2487 return TRUE;
2490 static GVariant *
2491 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2492 const GVariantType *expected_type,
2493 gpointer user_data)
2495 return g_variant_new_boolean (!g_value_get_boolean (value));
2499 * g_settings_bind:
2500 * @settings: a #GSettings object
2501 * @key: the key to bind
2502 * @object: (type GObject.Object): a #GObject
2503 * @property: the name of the property to bind
2504 * @flags: flags for the binding
2506 * Create a binding between the @key in the @settings object
2507 * and the property @property of @object.
2509 * The binding uses the default GIO mapping functions to map
2510 * between the settings and property values. These functions
2511 * handle booleans, numeric types and string types in a
2512 * straightforward way. Use g_settings_bind_with_mapping() if
2513 * you need a custom mapping, or map between types that are not
2514 * supported by the default mapping functions.
2516 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2517 * function also establishes a binding between the writability of
2518 * @key and the "sensitive" property of @object (if @object has
2519 * a boolean property by that name). See g_settings_bind_writable()
2520 * for more details about writable bindings.
2522 * Note that the lifecycle of the binding is tied to the object,
2523 * and that you can have only one binding per object property.
2524 * If you bind the same property twice on the same object, the second
2525 * binding overrides the first one.
2527 * Since: 2.26
2529 void
2530 g_settings_bind (GSettings *settings,
2531 const gchar *key,
2532 gpointer object,
2533 const gchar *property,
2534 GSettingsBindFlags flags)
2536 GSettingsBindGetMapping get_mapping = NULL;
2537 GSettingsBindSetMapping set_mapping = NULL;
2539 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2541 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2542 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2544 /* can't pass this flag to g_settings_bind_with_mapping() */
2545 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2548 g_settings_bind_with_mapping (settings, key, object, property, flags,
2549 get_mapping, set_mapping, NULL, NULL);
2553 * g_settings_bind_with_mapping: (skip)
2554 * @settings: a #GSettings object
2555 * @key: the key to bind
2556 * @object: (type GObject.Object): a #GObject
2557 * @property: the name of the property to bind
2558 * @flags: flags for the binding
2559 * @get_mapping: a function that gets called to convert values
2560 * from @settings to @object, or %NULL to use the default GIO mapping
2561 * @set_mapping: a function that gets called to convert values
2562 * from @object to @settings, or %NULL to use the default GIO mapping
2563 * @user_data: data that gets passed to @get_mapping and @set_mapping
2564 * @destroy: #GDestroyNotify function for @user_data
2566 * Create a binding between the @key in the @settings object
2567 * and the property @property of @object.
2569 * The binding uses the provided mapping functions to map between
2570 * settings and property values.
2572 * Note that the lifecycle of the binding is tied to the object,
2573 * and that you can have only one binding per object property.
2574 * If you bind the same property twice on the same object, the second
2575 * binding overrides the first one.
2577 * Since: 2.26
2579 void
2580 g_settings_bind_with_mapping (GSettings *settings,
2581 const gchar *key,
2582 gpointer object,
2583 const gchar *property,
2584 GSettingsBindFlags flags,
2585 GSettingsBindGetMapping get_mapping,
2586 GSettingsBindSetMapping set_mapping,
2587 gpointer user_data,
2588 GDestroyNotify destroy)
2590 GSettingsBinding *binding;
2591 GObjectClass *objectclass;
2592 gchar *detailed_signal;
2593 GQuark binding_quark;
2595 g_return_if_fail (G_IS_SETTINGS (settings));
2596 g_return_if_fail (key != NULL);
2597 g_return_if_fail (G_IS_OBJECT (object));
2598 g_return_if_fail (property != NULL);
2599 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2601 objectclass = G_OBJECT_GET_CLASS (object);
2603 binding = g_slice_new0 (GSettingsBinding);
2604 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2605 binding->settings = g_object_ref (settings);
2606 binding->object = object;
2607 binding->property = g_object_class_find_property (objectclass, property);
2608 binding->user_data = user_data;
2609 binding->destroy = destroy;
2610 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2611 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2613 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2614 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2616 if (binding->property == NULL)
2618 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2619 property, G_OBJECT_TYPE_NAME (object));
2620 return;
2623 if ((flags & G_SETTINGS_BIND_GET) &&
2624 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2626 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2627 "writable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2628 return;
2630 if ((flags & G_SETTINGS_BIND_SET) &&
2631 (binding->property->flags & G_PARAM_READABLE) == 0)
2633 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2634 "readable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2635 return;
2638 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2640 /* g_settings_bind_invert_boolean_get_mapping() is a private
2641 * function, so if we are here it means that g_settings_bind() was
2642 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2644 * Ensure that both sides are boolean.
2647 if (binding->property->value_type != G_TYPE_BOOLEAN)
2649 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2650 "was specified, but property `%s' on type `%s' has "
2651 "type `%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2652 g_type_name ((binding->property->value_type)));
2653 return;
2656 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2658 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2659 "was specified, but key `%s' on schema `%s' has "
2660 "type `%s'", key, g_settings_schema_get_id (settings->priv->schema),
2661 g_variant_type_dup_string (binding->key.type));
2662 return;
2667 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2668 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2669 !g_settings_mapping_is_compatible (binding->property->value_type,
2670 binding->key.type))
2672 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2673 "'%s' which is not compatible with type '%s' of key '%s' "
2674 "on schema '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2675 g_type_name (binding->property->value_type),
2676 g_variant_type_dup_string (binding->key.type), key,
2677 g_settings_schema_get_id (settings->priv->schema));
2678 return;
2681 if ((flags & G_SETTINGS_BIND_SET) &&
2682 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2684 GParamSpec *sensitive;
2686 sensitive = g_object_class_find_property (objectclass, "sensitive");
2688 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2689 (sensitive->flags & G_PARAM_WRITABLE))
2690 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2693 if (flags & G_SETTINGS_BIND_SET)
2695 detailed_signal = g_strdup_printf ("notify::%s", binding->property->name);
2696 binding->property_handler_id =
2697 g_signal_connect (object, detailed_signal,
2698 G_CALLBACK (g_settings_binding_property_changed),
2699 binding);
2700 g_free (detailed_signal);
2702 if (~flags & G_SETTINGS_BIND_GET)
2703 g_settings_binding_property_changed (object,
2704 binding->property,
2705 binding);
2708 if (flags & G_SETTINGS_BIND_GET)
2710 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2712 detailed_signal = g_strdup_printf ("changed::%s", key);
2713 binding->key_handler_id =
2714 g_signal_connect (settings, detailed_signal,
2715 G_CALLBACK (g_settings_binding_key_changed),
2716 binding);
2717 g_free (detailed_signal);
2720 g_settings_binding_key_changed (settings, binding->key.name, binding);
2723 binding_quark = g_settings_binding_quark (binding->property->name);
2724 g_object_set_qdata_full (object, binding_quark,
2725 binding, g_settings_binding_free);
2728 /* Writability binding {{{1 */
2729 typedef struct
2731 GSettings *settings;
2732 gpointer object;
2733 const gchar *key;
2734 const gchar *property;
2735 gboolean inverted;
2736 gulong handler_id;
2737 } GSettingsWritableBinding;
2739 static void
2740 g_settings_writable_binding_free (gpointer data)
2742 GSettingsWritableBinding *binding = data;
2744 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2745 g_object_unref (binding->settings);
2746 g_slice_free (GSettingsWritableBinding, binding);
2749 static void
2750 g_settings_binding_writable_changed (GSettings *settings,
2751 const gchar *key,
2752 gpointer user_data)
2754 GSettingsWritableBinding *binding = user_data;
2755 gboolean writable;
2757 g_assert (settings == binding->settings);
2758 g_assert (key == binding->key);
2760 writable = g_settings_is_writable (settings, key);
2762 if (binding->inverted)
2763 writable = !writable;
2765 g_object_set (binding->object, binding->property, writable, NULL);
2769 * g_settings_bind_writable:
2770 * @settings: a #GSettings object
2771 * @key: the key to bind
2772 * @object: (type GObject.Object):a #GObject
2773 * @property: the name of a boolean property to bind
2774 * @inverted: whether to 'invert' the value
2776 * Create a binding between the writability of @key in the
2777 * @settings object and the property @property of @object.
2778 * The property must be boolean; "sensitive" or "visible"
2779 * properties of widgets are the most likely candidates.
2781 * Writable bindings are always uni-directional; changes of the
2782 * writability of the setting will be propagated to the object
2783 * property, not the other way.
2785 * When the @inverted argument is %TRUE, the binding inverts the
2786 * value as it passes from the setting to the object, i.e. @property
2787 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2788 * writable.
2790 * Note that the lifecycle of the binding is tied to the object,
2791 * and that you can have only one binding per object property.
2792 * If you bind the same property twice on the same object, the second
2793 * binding overrides the first one.
2795 * Since: 2.26
2797 void
2798 g_settings_bind_writable (GSettings *settings,
2799 const gchar *key,
2800 gpointer object,
2801 const gchar *property,
2802 gboolean inverted)
2804 GSettingsWritableBinding *binding;
2805 gchar *detailed_signal;
2806 GParamSpec *pspec;
2808 g_return_if_fail (G_IS_SETTINGS (settings));
2810 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2811 if (pspec == NULL)
2813 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2814 property, G_OBJECT_TYPE_NAME (object));
2815 return;
2817 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2819 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2820 property, G_OBJECT_TYPE_NAME (object));
2821 return;
2824 binding = g_slice_new (GSettingsWritableBinding);
2825 binding->settings = g_object_ref (settings);
2826 binding->object = object;
2827 binding->key = g_intern_string (key);
2828 binding->property = g_intern_string (property);
2829 binding->inverted = inverted;
2831 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2832 binding->handler_id =
2833 g_signal_connect (settings, detailed_signal,
2834 G_CALLBACK (g_settings_binding_writable_changed),
2835 binding);
2836 g_free (detailed_signal);
2838 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2839 binding, g_settings_writable_binding_free);
2841 g_settings_binding_writable_changed (settings, binding->key, binding);
2845 * g_settings_unbind:
2846 * @object: the object
2847 * @property: the property whose binding is removed
2849 * Removes an existing binding for @property on @object.
2851 * Note that bindings are automatically removed when the
2852 * object is finalized, so it is rarely necessary to call this
2853 * function.
2855 * Since: 2.26
2857 void
2858 g_settings_unbind (gpointer object,
2859 const gchar *property)
2861 GQuark binding_quark;
2863 binding_quark = g_settings_binding_quark (property);
2864 g_object_set_qdata (object, binding_quark, NULL);
2867 /* GAction {{{1 */
2869 typedef struct
2871 GObject parent_instance;
2873 GSettingsSchemaKey key;
2874 GSettings *settings;
2875 } GSettingsAction;
2877 typedef GObjectClass GSettingsActionClass;
2879 static GType g_settings_action_get_type (void);
2880 static void g_settings_action_iface_init (GActionInterface *iface);
2881 G_DEFINE_TYPE_WITH_CODE (GSettingsAction, g_settings_action, G_TYPE_OBJECT,
2882 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_settings_action_iface_init))
2884 enum
2886 ACTION_PROP_0,
2887 ACTION_PROP_NAME,
2888 ACTION_PROP_PARAMETER_TYPE,
2889 ACTION_PROP_ENABLED,
2890 ACTION_PROP_STATE_TYPE,
2891 ACTION_PROP_STATE
2894 static const gchar *
2895 g_settings_action_get_name (GAction *action)
2897 GSettingsAction *gsa = (GSettingsAction *) action;
2899 return gsa->key.name;
2902 static const GVariantType *
2903 g_settings_action_get_parameter_type (GAction *action)
2905 GSettingsAction *gsa = (GSettingsAction *) action;
2906 const GVariantType *type;
2908 type = g_variant_get_type (gsa->key.default_value);
2909 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
2910 type = NULL;
2912 return type;
2915 static gboolean
2916 g_settings_action_get_enabled (GAction *action)
2918 GSettingsAction *gsa = (GSettingsAction *) action;
2920 return g_settings_is_writable (gsa->settings, gsa->key.name);
2923 static const GVariantType *
2924 g_settings_action_get_state_type (GAction *action)
2926 GSettingsAction *gsa = (GSettingsAction *) action;
2928 return g_variant_get_type (gsa->key.default_value);
2931 static GVariant *
2932 g_settings_action_get_state (GAction *action)
2934 GSettingsAction *gsa = (GSettingsAction *) action;
2935 GVariant *value;
2937 value = g_settings_read_from_backend (gsa->settings, &gsa->key);
2939 if (value == NULL)
2940 value = g_settings_schema_key_get_translated_default (&gsa->key);
2942 if (value == NULL)
2943 value = g_variant_ref (gsa->key.default_value);
2945 return value;
2948 static GVariant *
2949 g_settings_action_get_state_hint (GAction *action)
2951 GSettingsAction *gsa = (GSettingsAction *) action;
2953 /* no point in reimplementing this... */
2954 return g_settings_get_range (gsa->settings, gsa->key.name);
2957 static void
2958 g_settings_action_change_state (GAction *action,
2959 GVariant *value)
2961 GSettingsAction *gsa = (GSettingsAction *) action;
2963 if (g_settings_schema_key_type_check (&gsa->key, value) && g_settings_schema_key_range_check (&gsa->key, value))
2964 g_settings_write_to_backend (gsa->settings, &gsa->key, value);
2967 static void
2968 g_settings_action_activate (GAction *action,
2969 GVariant *parameter)
2971 GSettingsAction *gsa = (GSettingsAction *) action;
2973 if (g_variant_is_of_type (gsa->key.default_value, G_VARIANT_TYPE_BOOLEAN))
2975 GVariant *old;
2977 if (parameter != NULL)
2978 return;
2980 old = g_settings_action_get_state (action);
2981 parameter = g_variant_new_boolean (!g_variant_get_boolean (old));
2982 g_variant_unref (old);
2985 g_action_change_state (action, parameter);
2988 static void
2989 g_settings_action_get_property (GObject *object, guint prop_id,
2990 GValue *value, GParamSpec *pspec)
2992 GAction *action = G_ACTION (object);
2994 switch (prop_id)
2996 case ACTION_PROP_NAME:
2997 g_value_set_string (value, g_settings_action_get_name (action));
2998 break;
3000 case ACTION_PROP_PARAMETER_TYPE:
3001 g_value_set_boxed (value, g_settings_action_get_parameter_type (action));
3002 break;
3004 case ACTION_PROP_ENABLED:
3005 g_value_set_boolean (value, g_settings_action_get_enabled (action));
3006 break;
3008 case ACTION_PROP_STATE_TYPE:
3009 g_value_set_boxed (value, g_settings_action_get_state_type (action));
3010 break;
3012 case ACTION_PROP_STATE:
3013 g_value_set_variant (value, g_settings_action_get_state (action));
3014 break;
3016 default:
3017 g_assert_not_reached ();
3021 static void
3022 g_settings_action_finalize (GObject *object)
3024 GSettingsAction *gsa = (GSettingsAction *) object;
3026 g_signal_handlers_disconnect_by_data (gsa->settings, gsa);
3027 g_object_unref (gsa->settings);
3029 G_OBJECT_CLASS (g_settings_action_parent_class)
3030 ->finalize (object);
3033 static void
3034 g_settings_action_init (GSettingsAction *gsa)
3038 static void
3039 g_settings_action_iface_init (GActionInterface *iface)
3041 iface->get_name = g_settings_action_get_name;
3042 iface->get_parameter_type = g_settings_action_get_parameter_type;
3043 iface->get_enabled = g_settings_action_get_enabled;
3044 iface->get_state_type = g_settings_action_get_state_type;
3045 iface->get_state = g_settings_action_get_state;
3046 iface->get_state_hint = g_settings_action_get_state_hint;
3047 iface->change_state = g_settings_action_change_state;
3048 iface->activate = g_settings_action_activate;
3051 static void
3052 g_settings_action_class_init (GSettingsActionClass *class)
3054 class->get_property = g_settings_action_get_property;
3055 class->finalize = g_settings_action_finalize;
3057 g_object_class_override_property (class, ACTION_PROP_NAME, "name");
3058 g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE, "parameter-type");
3059 g_object_class_override_property (class, ACTION_PROP_ENABLED, "enabled");
3060 g_object_class_override_property (class, ACTION_PROP_STATE_TYPE, "state-type");
3061 g_object_class_override_property (class, ACTION_PROP_STATE, "state");
3064 static void
3065 g_settings_action_changed (GSettings *settings,
3066 const gchar *key,
3067 gpointer user_data)
3069 g_object_notify (user_data, "state");
3072 static void
3073 g_settings_action_enabled_changed (GSettings *settings,
3074 const gchar *key,
3075 gpointer user_data)
3077 g_object_notify (user_data, "enabled");
3081 * g_settings_create_action:
3082 * @settings: a #GSettings
3083 * @key: the name of a key in @settings
3085 * Creates a #GAction corresponding to a given #GSettings key.
3087 * The action has the same name as the key.
3089 * The value of the key becomes the state of the action and the action
3090 * is enabled when the key is writable. Changing the state of the
3091 * action results in the key being written to. Changes to the value or
3092 * writability of the key cause appropriate change notifications to be
3093 * emitted for the action.
3095 * For boolean-valued keys, action activations take no parameter and
3096 * result in the toggling of the value. For all other types,
3097 * activations take the new value for the key (which must have the
3098 * correct type).
3100 * Returns: (transfer full): a new #GAction
3102 * Since: 2.32
3104 GAction *
3105 g_settings_create_action (GSettings *settings,
3106 const gchar *key)
3108 GSettingsAction *gsa;
3109 gchar *detailed_signal;
3111 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
3112 g_return_val_if_fail (key != NULL, NULL);
3114 gsa = g_object_new (g_settings_action_get_type (), NULL);
3115 gsa->settings = g_object_ref (settings);
3116 g_settings_schema_key_init (&gsa->key, settings->priv->schema, key);
3118 detailed_signal = g_strdup_printf ("changed::%s", key);
3119 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_changed), gsa);
3120 g_free (detailed_signal);
3121 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3122 g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_enabled_changed), gsa);
3123 g_free (detailed_signal);
3125 return G_ACTION (gsa);
3128 /* Epilogue {{{1 */
3130 /* vim:set foldmethod=marker: */