menu threaded test: run the mainloop after export
[glib.git] / gio / gsettings.c
blob5898fdd0c157f1769284dabf04b048ad1bac138a
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"
35 #include "strinfo.c"
37 /**
38 * SECTION:gsettings
39 * @short_description: High-level API for application settings
41 * The #GSettings class provides a convenient API for storing and retrieving
42 * application settings.
44 * Reads and writes can be considered to be non-blocking. Reading
45 * settings with #GSettings is typically extremely fast: on
46 * approximately the same order of magnitude (but slower than) a
47 * #GHashTable lookup. Writing settings is also extremely fast in terms
48 * of time to return to your application, but can be extremely expensive
49 * for other threads and other processes. Many settings backends
50 * (including dconf) have lazy initialisation which means in the common
51 * case of the user using their computer without modifying any settings
52 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
53 * even need to be started in this case. For this reason, you should
54 * only ever modify #GSettings keys in response to explicit user action.
55 * Particular care should be paid to ensure that modifications are not
56 * made during startup -- for example, when setting the initial value
57 * of preferences widgets. The built-in g_settings_bind() functionality
58 * is careful not to write settings in response to notify signals as a
59 * result of modifications that it makes to widgets.
61 * When creating a GSettings instance, you have to specify a schema
62 * that describes the keys in your settings and their types and default
63 * values, as well as some other information.
65 * Normally, a schema has as fixed path that determines where the settings
66 * are stored in the conceptual global tree of settings. However, schemas
67 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
68 * useful e.g. when the schema describes an 'account', and you want to be
69 * able to store a arbitrary number of accounts.
71 * Unlike other configuration systems (like GConf), GSettings does not
72 * restrict keys to basic types like strings and numbers. GSettings stores
73 * values as #GVariant, and allows any #GVariantType for keys. Key names
74 * are restricted to lowercase characters, numbers and '-'. Furthermore,
75 * the names must begin with a lowercase character, must not end
76 * with a '-', and must not contain consecutive dashes.
78 * Similar to GConf, the default values in GSettings schemas can be
79 * localized, but the localized values are stored in gettext catalogs
80 * and looked up with the domain that is specified in the
81 * <tag class="attribute">gettext-domain</tag> attribute of the
82 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
83 * elements and the category that is specified in the l10n attribute of the
84 * <tag class="starttag">key</tag> element.
86 * GSettings uses schemas in a compact binary form that is created
87 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
88 * utility. The input is a schema description in an XML format that can be
89 * described by the following DTD:
90 * |[<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>]|
92 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
94 * At runtime, schemas are identified by their id (as specified
95 * in the <tag class="attribute">id</tag> attribute of the
96 * <tag class="starttag">schema</tag> element). The
97 * convention for schema ids is to use a dotted name, similar in
98 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
99 * if the settings are for a specific service that owns a D-Bus bus name,
100 * the D-Bus bus name and schema id should match. For schemas which deal
101 * with settings not associated with one named application, the id should
102 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
104 * In addition to #GVariant types, keys can have types that have enumerated
105 * types. These can be described by a <tag class="starttag">choice</tag>,
106 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
107 * <xref linkend="schema-enumerated"/>. The underlying type of
108 * such a key is string, but you can use g_settings_get_enum(),
109 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
110 * access the numeric values corresponding to the string value of enum
111 * and flags keys.
113 * <example id="schema-default-values"><title>Default values</title>
114 * <programlisting><![CDATA[
115 * <schemalist>
116 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
118 * <key name="greeting" type="s">
119 * <default l10n="messages">"Hello, earthlings"</default>
120 * <summary>A greeting</summary>
121 * <description>
122 * Greeting of the invading martians
123 * </description>
124 * </key>
126 * <key name="box" type="(ii)">
127 * <default>(20,30)</default>
128 * </key>
130 * </schema>
131 * </schemalist>
132 * ]]></programlisting></example>
134 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
135 * <programlisting><![CDATA[
136 * <schemalist>
138 * <enum id="org.gtk.Test.myenum">
139 * <value nick="first" value="1"/>
140 * <value nick="second" value="2"/>
141 * </enum>
143 * <flags id="org.gtk.Test.myflags">
144 * <value nick="flag1" value="1"/>
145 * <value nick="flag2" value="2"/>
146 * <value nick="flag3" value="4"/>
147 * </flags>
149 * <schema id="org.gtk.Test">
151 * <key name="key-with-range" type="i">
152 * <range min="1" max="100"/>
153 * <default>10</default>
154 * </key>
156 * <key name="key-with-choices" type="s">
157 * <choices>
158 * <choice value='Elisabeth'/>
159 * <choice value='Annabeth'/>
160 * <choice value='Joe'/>
161 * </choices>
162 * <aliases>
163 * <alias value='Anna' target='Annabeth'/>
164 * <alias value='Beth' target='Elisabeth'/>
165 * </aliases>
166 * <default>'Joe'</default>
167 * </key>
169 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
170 * <default>'first'</default>
171 * </key>
173 * <key name='flags-key' flags='org.gtk.Test.myflags'>
174 * <default>["flag1",flag2"]</default>
175 * </key>
176 * </schema>
177 * </schemalist>
178 * ]]></programlisting></example>
180 * <refsect2>
181 * <title>Vendor overrides</title>
182 * <para>
183 * Default values are defined in the schemas that get installed by
184 * an application. Sometimes, it is necessary for a vendor or distributor
185 * to adjust these defaults. Since patching the XML source for the schema
186 * is inconvenient and error-prone,
187 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
188 * so-called 'vendor override' files. These are keyfiles in the same
189 * directory as the XML schema sources which can override default values.
190 * The schema id serves as the group name in the key file, and the values
191 * are expected in serialized GVariant form, as in the following example:
192 * <informalexample><programlisting>
193 * [org.gtk.Example]
194 * key1='string'
195 * key2=1.5
196 * </programlisting></informalexample>
197 * </para>
198 * <para>
199 * glib-compile-schemas expects schema files to have the extension
200 * <filename>.gschema.override</filename>
201 * </para>
202 * </refsect2>
204 * <refsect2>
205 * <title>Binding</title>
206 * <para>
207 * A very convenient feature of GSettings lets you bind #GObject properties
208 * directly to settings, using g_settings_bind(). Once a GObject property
209 * has been bound to a setting, changes on either side are automatically
210 * propagated to the other side. GSettings handles details like
211 * mapping between GObject and GVariant types, and preventing infinite
212 * cycles.
213 * </para>
214 * <para>
215 * This makes it very easy to hook up a preferences dialog to the
216 * underlying settings. To make this even more convenient, GSettings
217 * looks for a boolean property with the name "sensitivity" and
218 * automatically binds it to the writability of the bound setting.
219 * If this 'magic' gets in the way, it can be suppressed with the
220 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
221 * </para>
222 * </refsect2>
225 struct _GSettingsPrivate
227 /* where the signals go... */
228 GMainContext *main_context;
230 GSettingsBackend *backend;
231 GSettingsSchema *schema;
232 gchar *path;
234 GDelayedSettingsBackend *delayed;
237 enum
239 PROP_0,
240 PROP_SCHEMA,
241 PROP_SCHEMA_ID,
242 PROP_BACKEND,
243 PROP_PATH,
244 PROP_HAS_UNAPPLIED,
245 PROP_DELAY_APPLY
248 enum
250 SIGNAL_WRITABLE_CHANGE_EVENT,
251 SIGNAL_WRITABLE_CHANGED,
252 SIGNAL_CHANGE_EVENT,
253 SIGNAL_CHANGED,
254 N_SIGNALS
257 static guint g_settings_signals[N_SIGNALS];
259 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
261 /* Signals {{{1 */
262 static gboolean
263 g_settings_real_change_event (GSettings *settings,
264 const GQuark *keys,
265 gint n_keys)
267 gint i;
269 if (keys == NULL)
270 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
272 for (i = 0; i < n_keys; i++)
273 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
274 keys[i], g_quark_to_string (keys[i]));
276 return FALSE;
279 static gboolean
280 g_settings_real_writable_change_event (GSettings *settings,
281 GQuark key)
283 const GQuark *keys = &key;
284 gint n_keys = 1;
285 gint i;
287 if (key == 0)
288 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
290 for (i = 0; i < n_keys; i++)
291 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
292 keys[i], g_quark_to_string (keys[i]));
294 return FALSE;
297 static void
298 settings_backend_changed (GObject *target,
299 GSettingsBackend *backend,
300 const gchar *key,
301 gpointer origin_tag)
303 GSettings *settings = G_SETTINGS (target);
304 gboolean ignore_this;
305 gint i;
307 /* We used to assert here:
309 * settings->priv->backend == backend
311 * but it could be the case that a notification is queued for delivery
312 * while someone calls g_settings_delay() (which changes the backend).
314 * Since the delay backend would just pass that straight through
315 * anyway, it doesn't make sense to try to detect this case.
316 * Therefore, we just accept it.
319 for (i = 0; key[i] == settings->priv->path[i]; i++);
321 if (settings->priv->path[i] == '\0' &&
322 g_settings_schema_has_key (settings->priv->schema, key + i))
324 GQuark quark;
326 quark = g_quark_from_string (key + i);
327 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
328 0, &quark, 1, &ignore_this);
332 static void
333 settings_backend_path_changed (GObject *target,
334 GSettingsBackend *backend,
335 const gchar *path,
336 gpointer origin_tag)
338 GSettings *settings = G_SETTINGS (target);
339 gboolean ignore_this;
341 if (g_str_has_prefix (settings->priv->path, path))
342 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
343 0, NULL, 0, &ignore_this);
346 static void
347 settings_backend_keys_changed (GObject *target,
348 GSettingsBackend *backend,
349 const gchar *path,
350 const gchar * const *items,
351 gpointer origin_tag)
353 GSettings *settings = G_SETTINGS (target);
354 gboolean ignore_this;
355 gint i;
357 for (i = 0; settings->priv->path[i] &&
358 settings->priv->path[i] == path[i]; i++);
360 if (path[i] == '\0')
362 GQuark quarks[256];
363 gint j, l = 0;
365 for (j = 0; items[j]; j++)
367 const gchar *item = items[j];
368 gint k;
370 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
372 if (settings->priv->path[i + k] == '\0' &&
373 g_settings_schema_has_key (settings->priv->schema, item + k))
374 quarks[l++] = g_quark_from_string (item + k);
376 /* "256 quarks ought to be enough for anybody!"
377 * If this bites you, I'm sorry. Please file a bug.
379 g_assert (l < 256);
382 if (l > 0)
383 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
384 0, quarks, l, &ignore_this);
388 static void
389 settings_backend_writable_changed (GObject *target,
390 GSettingsBackend *backend,
391 const gchar *key)
393 GSettings *settings = G_SETTINGS (target);
394 gboolean ignore_this;
395 gint i;
397 for (i = 0; key[i] == settings->priv->path[i]; i++);
399 if (settings->priv->path[i] == '\0' &&
400 g_settings_schema_has_key (settings->priv->schema, key + i))
401 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
402 0, g_quark_from_string (key + i), &ignore_this);
405 static void
406 settings_backend_path_writable_changed (GObject *target,
407 GSettingsBackend *backend,
408 const gchar *path)
410 GSettings *settings = G_SETTINGS (target);
411 gboolean ignore_this;
413 if (g_str_has_prefix (settings->priv->path, path))
414 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
415 0, (GQuark) 0, &ignore_this);
418 /* Properties, Construction, Destruction {{{1 */
419 static void
420 g_settings_set_property (GObject *object,
421 guint prop_id,
422 const GValue *value,
423 GParamSpec *pspec)
425 GSettings *settings = G_SETTINGS (object);
427 switch (prop_id)
429 case PROP_SCHEMA:
431 GSettingsSchema *schema;
433 schema = g_value_dup_boxed (value);
435 /* we receive a set_property() call for "settings-schema" even
436 * if it was not specified (ie: with NULL value). ->schema
437 * could already be set at this point (ie: via "schema-id").
438 * check for NULL to avoid clobbering the existing value.
440 if (schema != NULL)
442 g_assert (settings->priv->schema == NULL);
443 settings->priv->schema = schema;
446 break;
448 case PROP_SCHEMA_ID:
450 const gchar *schema_id;
452 schema_id = g_value_get_string (value);
454 /* we receive a set_property() call for both "schema" and
455 * "schema-id", even if they are not set. Hopefully only one of
456 * them is non-NULL.
458 if (schema_id != NULL)
460 GSettingsSchemaSource *default_source;
462 g_assert (settings->priv->schema == NULL);
463 default_source = g_settings_schema_source_get_default ();
465 if (default_source == NULL)
466 g_error ("No GSettings schemas are installed on the system");
468 settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
470 if (settings->priv->schema == NULL)
471 g_error ("Settings schema '%s' is not installed\n", schema_id);
474 break;
476 case PROP_PATH:
477 settings->priv->path = g_value_dup_string (value);
478 break;
480 case PROP_BACKEND:
481 settings->priv->backend = g_value_dup_object (value);
482 break;
484 default:
485 g_assert_not_reached ();
489 static void
490 g_settings_get_property (GObject *object,
491 guint prop_id,
492 GValue *value,
493 GParamSpec *pspec)
495 GSettings *settings = G_SETTINGS (object);
497 switch (prop_id)
499 case PROP_SCHEMA:
500 g_value_set_boxed (value, settings->priv->schema);
501 break;
503 case PROP_SCHEMA_ID:
504 g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
505 break;
507 case PROP_BACKEND:
508 g_value_set_object (value, settings->priv->backend);
509 break;
511 case PROP_PATH:
512 g_value_set_string (value, settings->priv->path);
513 break;
515 case PROP_HAS_UNAPPLIED:
516 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
517 break;
519 case PROP_DELAY_APPLY:
520 g_value_set_boolean (value, settings->priv->delayed != NULL);
521 break;
523 default:
524 g_assert_not_reached ();
528 static const GSettingsListenerVTable listener_vtable = {
529 settings_backend_changed,
530 settings_backend_path_changed,
531 settings_backend_keys_changed,
532 settings_backend_writable_changed,
533 settings_backend_path_writable_changed
536 static void
537 g_settings_constructed (GObject *object)
539 GSettings *settings = G_SETTINGS (object);
540 const gchar *schema_path;
542 schema_path = g_settings_schema_get_path (settings->priv->schema);
544 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
545 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
546 g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
548 if (settings->priv->path == NULL)
550 if (schema_path == NULL)
551 g_error ("attempting to create schema '%s' without a path",
552 g_settings_schema_get_id (settings->priv->schema));
554 settings->priv->path = g_strdup (schema_path);
557 if (settings->priv->backend == NULL)
558 settings->priv->backend = g_settings_backend_get_default ();
560 g_settings_backend_watch (settings->priv->backend,
561 &listener_vtable, G_OBJECT (settings),
562 settings->priv->main_context);
563 g_settings_backend_subscribe (settings->priv->backend,
564 settings->priv->path);
567 static void
568 g_settings_finalize (GObject *object)
570 GSettings *settings = G_SETTINGS (object);
572 g_settings_backend_unsubscribe (settings->priv->backend,
573 settings->priv->path);
574 g_main_context_unref (settings->priv->main_context);
575 g_object_unref (settings->priv->backend);
576 g_settings_schema_unref (settings->priv->schema);
577 g_free (settings->priv->path);
579 G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
582 static void
583 g_settings_init (GSettings *settings)
585 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
586 G_TYPE_SETTINGS,
587 GSettingsPrivate);
589 settings->priv->main_context = g_main_context_ref_thread_default ();
592 static void
593 g_settings_class_init (GSettingsClass *class)
595 GObjectClass *object_class = G_OBJECT_CLASS (class);
597 class->writable_change_event = g_settings_real_writable_change_event;
598 class->change_event = g_settings_real_change_event;
600 object_class->set_property = g_settings_set_property;
601 object_class->get_property = g_settings_get_property;
602 object_class->constructed = g_settings_constructed;
603 object_class->finalize = g_settings_finalize;
605 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
608 * GSettings::changed:
609 * @settings: the object on which the signal was emitted
610 * @key: the name of the key that changed
612 * The "changed" signal is emitted when a key has potentially changed.
613 * You should call one of the g_settings_get() calls to check the new
614 * value.
616 * This signal supports detailed connections. You can connect to the
617 * detailed signal "changed::x" in order to only receive callbacks
618 * when key "x" changes.
620 g_settings_signals[SIGNAL_CHANGED] =
621 g_signal_new ("changed", G_TYPE_SETTINGS,
622 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
623 G_STRUCT_OFFSET (GSettingsClass, changed),
624 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
625 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
628 * GSettings::change-event:
629 * @settings: the object on which the signal was emitted
630 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
631 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
632 * @n_keys: the length of the @keys array, or 0
634 * The "change-event" signal is emitted once per change event that
635 * affects this settings object. You should connect to this signal
636 * only if you are interested in viewing groups of changes before they
637 * are split out into multiple emissions of the "changed" signal.
638 * For most use cases it is more appropriate to use the "changed" signal.
640 * In the event that the change event applies to one or more specified
641 * keys, @keys will be an array of #GQuark of length @n_keys. In the
642 * event that the change event applies to the #GSettings object as a
643 * whole (ie: potentially every key has been changed) then @keys will
644 * be %NULL and @n_keys will be 0.
646 * The default handler for this signal invokes the "changed" signal
647 * for each affected key. If any other connected handler returns
648 * %TRUE then this default functionality will be suppressed.
650 * Returns: %TRUE to stop other handlers from being invoked for the
651 * event. FALSE to propagate the event further.
653 g_settings_signals[SIGNAL_CHANGE_EVENT] =
654 g_signal_new ("change-event", G_TYPE_SETTINGS,
655 G_SIGNAL_RUN_LAST,
656 G_STRUCT_OFFSET (GSettingsClass, change_event),
657 g_signal_accumulator_true_handled, NULL,
658 NULL,
659 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
662 * GSettings::writable-changed:
663 * @settings: the object on which the signal was emitted
664 * @key: the key
666 * The "writable-changed" signal is emitted when the writability of a
667 * key has potentially changed. You should call
668 * g_settings_is_writable() in order to determine the new status.
670 * This signal supports detailed connections. You can connect to the
671 * detailed signal "writable-changed::x" in order to only receive
672 * callbacks when the writability of "x" changes.
674 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
675 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
676 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
677 G_STRUCT_OFFSET (GSettingsClass, writable_changed),
678 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
679 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
682 * GSettings::writable-change-event:
683 * @settings: the object on which the signal was emitted
684 * @key: the quark of the key, or 0
686 * The "writable-change-event" signal is emitted once per writability
687 * change event that affects this settings object. You should connect
688 * to this signal if you are interested in viewing groups of changes
689 * before they are split out into multiple emissions of the
690 * "writable-changed" signal. For most use cases it is more
691 * appropriate to use the "writable-changed" signal.
693 * In the event that the writability change applies only to a single
694 * key, @key will be set to the #GQuark for that key. In the event
695 * that the writability change affects the entire settings object,
696 * @key will be 0.
698 * The default handler for this signal invokes the "writable-changed"
699 * and "changed" signals for each affected key. This is done because
700 * changes in writability might also imply changes in value (if for
701 * example, a new mandatory setting is introduced). If any other
702 * connected handler returns %TRUE then this default functionality
703 * will be suppressed.
705 * Returns: %TRUE to stop other handlers from being invoked for the
706 * event. FALSE to propagate the event further.
708 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
709 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
710 G_SIGNAL_RUN_LAST,
711 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
712 g_signal_accumulator_true_handled, NULL,
713 NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
716 * GSettings:context:
718 * The name of the context that the settings are stored in.
720 g_object_class_install_property (object_class, PROP_BACKEND,
721 g_param_spec_object ("backend",
722 P_("GSettingsBackend"),
723 P_("The GSettingsBackend for this settings object"),
724 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
725 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
728 * GSettings:settings-schema:
730 * The #GSettingsSchema describing the types of keys for this
731 * #GSettings object.
733 * Ideally, this property would be called 'schema'. #GSettingsSchema
734 * has only existed since version 2.32, however, and before then the
735 * 'schema' property was used to refer to the ID of the schema rather
736 * than the schema itself. Take care.
738 g_object_class_install_property (object_class, PROP_SCHEMA,
739 g_param_spec_boxed ("settings-schema",
740 P_("schema"),
741 P_("The GSettingsSchema for this settings object"),
742 G_TYPE_SETTINGS_SCHEMA,
743 G_PARAM_CONSTRUCT_ONLY |
744 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
747 * GSettings:schema:
749 * The name of the schema that describes the types of keys
750 * for this #GSettings object.
752 * The type of this property is *not* #GSettingsSchema.
753 * #GSettingsSchema has only existed since version 2.32 and
754 * unfortunately this name was used in previous versions to refer to
755 * the schema ID rather than the schema itself. Take care to use the
756 * 'settings-schema' property if you wish to pass in a
757 * #GSettingsSchema.
759 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
760 * version, this property may instead refer to a #GSettingsSchema.
762 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
763 g_param_spec_string ("schema",
764 P_("Schema name"),
765 P_("The name of the schema for this settings object"),
766 NULL,
767 G_PARAM_CONSTRUCT_ONLY |
768 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
771 * GSettings:schema-id:
773 * The name of the schema that describes the types of keys
774 * for this #GSettings object.
776 g_object_class_install_property (object_class, PROP_SCHEMA_ID,
777 g_param_spec_string ("schema-id",
778 P_("Schema name"),
779 P_("The name of the schema for this settings object"),
780 NULL,
781 G_PARAM_CONSTRUCT_ONLY |
782 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
785 * GSettings:path:
787 * The path within the backend where the settings are stored.
789 g_object_class_install_property (object_class, PROP_PATH,
790 g_param_spec_string ("path",
791 P_("Base path"),
792 P_("The path within the backend where the settings are"),
793 NULL,
794 G_PARAM_CONSTRUCT_ONLY |
795 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
798 * GSettings:has-unapplied:
800 * If this property is %TRUE, the #GSettings object has outstanding
801 * changes that will be applied when g_settings_apply() is called.
803 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
804 g_param_spec_boolean ("has-unapplied",
805 P_("Has unapplied changes"),
806 P_("TRUE if there are outstanding changes to apply()"),
807 FALSE,
808 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
811 * GSettings:delay-apply:
813 * Whether the #GSettings object is in 'delay-apply' mode. See
814 * g_settings_delay() for details.
816 * Since: 2.28
818 g_object_class_install_property (object_class, PROP_DELAY_APPLY,
819 g_param_spec_boolean ("delay-apply",
820 P_("Delay-apply mode"),
821 P_("Whether this settings object is in 'delay-apply' mode"),
822 FALSE,
823 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
826 /* Construction (new, new_with_path, etc.) {{{1 */
828 * g_settings_new:
829 * @schema_id: the id of the schema
831 * Creates a new #GSettings object with the schema specified by
832 * @schema_id.
834 * Signals on the newly created #GSettings object will be dispatched
835 * via the thread-default #GMainContext in effect at the time of the
836 * call to g_settings_new(). The new #GSettings will hold a reference
837 * on the context. See g_main_context_push_thread_default().
839 * Returns: a new #GSettings object
841 * Since: 2.26
843 GSettings *
844 g_settings_new (const gchar *schema_id)
846 g_return_val_if_fail (schema_id != NULL, NULL);
848 return g_object_new (G_TYPE_SETTINGS,
849 "schema-id", schema_id,
850 NULL);
854 * g_settings_new_with_path:
855 * @schema_id: the id of the schema
856 * @path: the path to use
858 * Creates a new #GSettings object with the relocatable schema specified
859 * by @schema_id and a given path.
861 * You only need to do this if you want to directly create a settings
862 * object with a schema that doesn't have a specified path of its own.
863 * That's quite rare.
865 * It is a programmer error to call this function for a schema that
866 * has an explicitly specified path.
868 * Returns: a new #GSettings object
870 * Since: 2.26
872 GSettings *
873 g_settings_new_with_path (const gchar *schema_id,
874 const gchar *path)
876 g_return_val_if_fail (schema_id != NULL, NULL);
877 g_return_val_if_fail (path != NULL, NULL);
879 return g_object_new (G_TYPE_SETTINGS,
880 "schema-id", schema_id,
881 "path", path,
882 NULL);
886 * g_settings_new_with_backend:
887 * @schema_id: the id of the schema
888 * @backend: the #GSettingsBackend to use
890 * Creates a new #GSettings object with the schema specified by
891 * @schema_id and a given #GSettingsBackend.
893 * Creating a #GSettings object with a different backend allows accessing
894 * settings from a database other than the usual one. For example, it may make
895 * sense to pass a backend corresponding to the "defaults" settings database on
896 * the system to get a settings object that modifies the system default
897 * settings instead of the settings for this user.
899 * Returns: a new #GSettings object
901 * Since: 2.26
903 GSettings *
904 g_settings_new_with_backend (const gchar *schema_id,
905 GSettingsBackend *backend)
907 g_return_val_if_fail (schema_id != NULL, NULL);
908 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
910 return g_object_new (G_TYPE_SETTINGS,
911 "schema-id", schema_id,
912 "backend", backend,
913 NULL);
917 * g_settings_new_with_backend_and_path:
918 * @schema_id: the id of the schema
919 * @backend: the #GSettingsBackend to use
920 * @path: the path to use
922 * Creates a new #GSettings object with the schema specified by
923 * @schema_id and a given #GSettingsBackend and path.
925 * This is a mix of g_settings_new_with_backend() and
926 * g_settings_new_with_path().
928 * Returns: a new #GSettings object
930 * Since: 2.26
932 GSettings *
933 g_settings_new_with_backend_and_path (const gchar *schema_id,
934 GSettingsBackend *backend,
935 const gchar *path)
937 g_return_val_if_fail (schema_id != NULL, NULL);
938 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
939 g_return_val_if_fail (path != NULL, NULL);
941 return g_object_new (G_TYPE_SETTINGS,
942 "schema-id", schema_id,
943 "backend", backend,
944 "path", path,
945 NULL);
949 * g_settings_new_full:
950 * @schema: a #GSettingsSchema
951 * @backend: (allow-none): a #GSettingsBackend
952 * @path: (allow-none): the path to use
954 * Creates a new #GSettings object with a given schema, backend and
955 * path.
957 * It should be extremely rare that you ever want to use this function.
958 * It is made available for advanced use-cases (such as plugin systems
959 * that want to provide access to schemas loaded from custom locations,
960 * etc).
962 * At the most basic level, a #GSettings object is a pure composition of
963 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
964 * backend, and a #GMainContext to which signals are dispatched.
966 * This constructor therefore gives you full control over constructing
967 * #GSettings instances. The first 4 parameters are given directly as
968 * @schema, @backend and @path, and the main context is taken from the
969 * thread-default (as per g_settings_new()).
971 * If @backend is %NULL then the default backend is used.
973 * If @path is %NULL then the path from the schema is used. It is an
974 * error f @path is %NULL and the schema has no path of its own or if
975 * @path is non-%NULL and not equal to the path that the schema does
976 * have.
978 * Returns: a new #GSettings object
980 * Since: 2.32
982 GSettings *
983 g_settings_new_full (GSettingsSchema *schema,
984 GSettingsBackend *backend,
985 const gchar *path)
987 return g_object_new (G_TYPE_SETTINGS,
988 "settings-schema", schema,
989 "backend", backend,
990 "path", path,
991 NULL);
994 /* Internal read/write utilities {{{1 */
995 static gboolean
996 g_settings_write_to_backend (GSettings *settings,
997 GSettingsSchemaKey *key,
998 GVariant *value)
1000 gboolean success;
1001 gchar *path;
1003 path = g_strconcat (settings->priv->path, key->name, NULL);
1004 success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1005 g_free (path);
1007 return success;
1010 static GVariant *
1011 g_settings_read_from_backend (GSettings *settings,
1012 GSettingsSchemaKey *key)
1014 GVariant *value;
1015 GVariant *fixup;
1016 gchar *path;
1018 path = g_strconcat (settings->priv->path, key->name, NULL);
1019 value = g_settings_backend_read (settings->priv->backend, path, key->type, FALSE);
1020 g_free (path);
1022 if (value != NULL)
1024 fixup = g_settings_schema_key_range_fixup (key, value);
1025 g_variant_unref (value);
1027 else
1028 fixup = NULL;
1030 return fixup;
1033 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1035 * g_settings_get_value:
1036 * @settings: a #GSettings object
1037 * @key: the key to get the value for
1039 * Gets the value that is stored in @settings for @key.
1041 * It is a programmer error to give a @key that isn't contained in the
1042 * schema for @settings.
1044 * Returns: a new #GVariant
1046 * Since: 2.26
1048 GVariant *
1049 g_settings_get_value (GSettings *settings,
1050 const gchar *key)
1052 GSettingsSchemaKey skey;
1053 GVariant *value;
1055 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1056 g_return_val_if_fail (key != NULL, NULL);
1058 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1059 value = g_settings_read_from_backend (settings, &skey);
1061 if (value == NULL)
1062 value = g_settings_schema_key_get_translated_default (&skey);
1064 if (value == NULL)
1065 value = g_variant_ref (skey.default_value);
1067 g_settings_schema_key_clear (&skey);
1069 return value;
1073 * g_settings_get_enum:
1074 * @settings: a #GSettings object
1075 * @key: the key to get the value for
1077 * Gets the value that is stored in @settings for @key and converts it
1078 * to the enum value that it represents.
1080 * In order to use this function the type of the value must be a string
1081 * and it must be marked in the schema file as an enumerated type.
1083 * It is a programmer error to give a @key that isn't contained in the
1084 * schema for @settings or is not marked as an enumerated type.
1086 * If the value stored in the configuration database is not a valid
1087 * value for the enumerated type then this function will return the
1088 * default value.
1090 * Returns: the enum value
1092 * Since: 2.26
1094 gint
1095 g_settings_get_enum (GSettings *settings,
1096 const gchar *key)
1098 GSettingsSchemaKey skey;
1099 GVariant *value;
1100 gint result;
1102 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1103 g_return_val_if_fail (key != NULL, -1);
1105 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1107 if (!skey.is_enum)
1109 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1110 "associated with an enumerated type", skey.name);
1111 g_settings_schema_key_clear (&skey);
1112 return -1;
1115 value = g_settings_read_from_backend (settings, &skey);
1117 if (value == NULL)
1118 value = g_settings_schema_key_get_translated_default (&skey);
1120 if (value == NULL)
1121 value = g_variant_ref (skey.default_value);
1123 result = g_settings_schema_key_to_enum (&skey, value);
1124 g_settings_schema_key_clear (&skey);
1125 g_variant_unref (value);
1127 return result;
1131 * g_settings_set_enum:
1132 * @settings: a #GSettings object
1133 * @key: a key, within @settings
1134 * @value: an enumerated value
1136 * Looks up the enumerated type nick for @value and writes it to @key,
1137 * within @settings.
1139 * It is a programmer error to give a @key that isn't contained in the
1140 * schema for @settings or is not marked as an enumerated type, or for
1141 * @value not to be a valid value for the named type.
1143 * After performing the write, accessing @key directly with
1144 * g_settings_get_string() will return the 'nick' associated with
1145 * @value.
1147 * Returns: %TRUE, if the set succeeds
1149 gboolean
1150 g_settings_set_enum (GSettings *settings,
1151 const gchar *key,
1152 gint value)
1154 GSettingsSchemaKey skey;
1155 GVariant *variant;
1156 gboolean success;
1158 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1159 g_return_val_if_fail (key != NULL, FALSE);
1161 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1163 if (!skey.is_enum)
1165 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1166 "associated with an enumerated type", skey.name);
1167 return FALSE;
1170 if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1172 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1173 "in schema `%s'. Doing nothing.", value, skey.name,
1174 g_settings_schema_get_id (skey.schema));
1175 g_settings_schema_key_clear (&skey);
1176 return FALSE;
1179 success = g_settings_write_to_backend (settings, &skey, variant);
1180 g_settings_schema_key_clear (&skey);
1182 return success;
1186 * g_settings_get_flags:
1187 * @settings: a #GSettings object
1188 * @key: the key to get the value for
1190 * Gets the value that is stored in @settings for @key and converts it
1191 * to the flags value that it represents.
1193 * In order to use this function the type of the value must be an array
1194 * of strings and it must be marked in the schema file as an flags type.
1196 * It is a programmer error to give a @key that isn't contained in the
1197 * schema for @settings or is not marked as a flags type.
1199 * If the value stored in the configuration database is not a valid
1200 * value for the flags type then this function will return the default
1201 * value.
1203 * Returns: the flags value
1205 * Since: 2.26
1207 guint
1208 g_settings_get_flags (GSettings *settings,
1209 const gchar *key)
1211 GSettingsSchemaKey skey;
1212 GVariant *value;
1213 guint result;
1215 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1216 g_return_val_if_fail (key != NULL, -1);
1218 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1220 if (!skey.is_flags)
1222 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1223 "associated with a flags type", skey.name);
1224 g_settings_schema_key_clear (&skey);
1225 return -1;
1228 value = g_settings_read_from_backend (settings, &skey);
1230 if (value == NULL)
1231 value = g_settings_schema_key_get_translated_default (&skey);
1233 if (value == NULL)
1234 value = g_variant_ref (skey.default_value);
1236 result = g_settings_schema_key_to_flags (&skey, value);
1237 g_settings_schema_key_clear (&skey);
1238 g_variant_unref (value);
1240 return result;
1244 * g_settings_set_flags:
1245 * @settings: a #GSettings object
1246 * @key: a key, within @settings
1247 * @value: a flags value
1249 * Looks up the flags type nicks for the bits specified by @value, puts
1250 * them in an array of strings and writes the array to @key, within
1251 * @settings.
1253 * It is a programmer error to give a @key that isn't contained in the
1254 * schema for @settings or is not marked as a flags type, or for @value
1255 * to contain any bits that are not value for the named type.
1257 * After performing the write, accessing @key directly with
1258 * g_settings_get_strv() will return an array of 'nicks'; one for each
1259 * bit in @value.
1261 * Returns: %TRUE, if the set succeeds
1263 gboolean
1264 g_settings_set_flags (GSettings *settings,
1265 const gchar *key,
1266 guint value)
1268 GSettingsSchemaKey skey;
1269 GVariant *variant;
1270 gboolean success;
1272 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1273 g_return_val_if_fail (key != NULL, FALSE);
1275 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1277 if (!skey.is_flags)
1279 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1280 "associated with a flags type", skey.name);
1281 return FALSE;
1284 if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1286 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1287 "for key `%s' in schema `%s'. Doing nothing.",
1288 value, skey.name, g_settings_schema_get_id (skey.schema));
1289 g_settings_schema_key_clear (&skey);
1290 return FALSE;
1293 success = g_settings_write_to_backend (settings, &skey, variant);
1294 g_settings_schema_key_clear (&skey);
1296 return success;
1300 * g_settings_set_value:
1301 * @settings: a #GSettings object
1302 * @key: the name of the key to set
1303 * @value: a #GVariant of the correct type
1305 * Sets @key in @settings to @value.
1307 * It is a programmer error to give a @key that isn't contained in the
1308 * schema for @settings or for @value to have the incorrect type, per
1309 * the schema.
1311 * If @value is floating then this function consumes the reference.
1313 * Returns: %TRUE if setting the key succeeded,
1314 * %FALSE if the key was not writable
1316 * Since: 2.26
1318 gboolean
1319 g_settings_set_value (GSettings *settings,
1320 const gchar *key,
1321 GVariant *value)
1323 GSettingsSchemaKey skey;
1325 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1326 g_return_val_if_fail (key != NULL, FALSE);
1328 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1330 if (!g_settings_schema_key_type_check (&skey, value))
1332 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1333 key,
1334 g_settings_schema_get_id (settings->priv->schema),
1335 g_variant_type_peek_string (skey.type),
1336 g_variant_get_type_string (value));
1338 return FALSE;
1341 if (!g_settings_schema_key_range_check (&skey, value))
1343 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1344 "is outside of valid range",
1345 key,
1346 g_settings_schema_get_id (settings->priv->schema));
1348 return FALSE;
1351 g_settings_schema_key_clear (&skey);
1353 return g_settings_write_to_backend (settings, &skey, value);
1357 * g_settings_get:
1358 * @settings: a #GSettings object
1359 * @key: the key to get the value for
1360 * @format: a #GVariant format string
1361 * @...: arguments as per @format
1363 * Gets the value that is stored at @key in @settings.
1365 * A convenience function that combines g_settings_get_value() with
1366 * g_variant_get().
1368 * It is a programmer error to give a @key that isn't contained in the
1369 * schema for @settings or for the #GVariantType of @format to mismatch
1370 * the type given in the schema.
1372 * Since: 2.26
1374 void
1375 g_settings_get (GSettings *settings,
1376 const gchar *key,
1377 const gchar *format,
1378 ...)
1380 GVariant *value;
1381 va_list ap;
1383 value = g_settings_get_value (settings, key);
1385 va_start (ap, format);
1386 g_variant_get_va (value, format, NULL, &ap);
1387 va_end (ap);
1389 g_variant_unref (value);
1393 * g_settings_set:
1394 * @settings: a #GSettings object
1395 * @key: the name of the key to set
1396 * @format: a #GVariant format string
1397 * @...: arguments as per @format
1399 * Sets @key in @settings to @value.
1401 * A convenience function that combines g_settings_set_value() with
1402 * g_variant_new().
1404 * It is a programmer error to give a @key that isn't contained in the
1405 * schema for @settings or for the #GVariantType of @format to mismatch
1406 * the type given in the schema.
1408 * Returns: %TRUE if setting the key succeeded,
1409 * %FALSE if the key was not writable
1411 * Since: 2.26
1413 gboolean
1414 g_settings_set (GSettings *settings,
1415 const gchar *key,
1416 const gchar *format,
1417 ...)
1419 GVariant *value;
1420 va_list ap;
1422 va_start (ap, format);
1423 value = g_variant_new_va (format, NULL, &ap);
1424 va_end (ap);
1426 return g_settings_set_value (settings, key, value);
1430 * g_settings_get_mapped:
1431 * @settings: a #GSettings object
1432 * @key: the key to get the value for
1433 * @mapping: (scope call): the function to map the value in the
1434 * settings database to the value used by the application
1435 * @user_data: user data for @mapping
1437 * Gets the value that is stored at @key in @settings, subject to
1438 * application-level validation/mapping.
1440 * You should use this function when the application needs to perform
1441 * some processing on the value of the key (for example, parsing). The
1442 * @mapping function performs that processing. If the function
1443 * indicates that the processing was unsuccessful (due to a parse error,
1444 * for example) then the mapping is tried again with another value.
1446 * This allows a robust 'fall back to defaults' behaviour to be
1447 * implemented somewhat automatically.
1449 * The first value that is tried is the user's setting for the key. If
1450 * the mapping function fails to map this value, other values may be
1451 * tried in an unspecified order (system or site defaults, translated
1452 * schema default values, untranslated schema default values, etc).
1454 * If the mapping function fails for all possible values, one additional
1455 * attempt is made: the mapping function is called with a %NULL value.
1456 * If the mapping function still indicates failure at this point then
1457 * the application will be aborted.
1459 * The result parameter for the @mapping function is pointed to a
1460 * #gpointer which is initially set to %NULL. The same pointer is given
1461 * to each invocation of @mapping. The final value of that #gpointer is
1462 * what is returned by this function. %NULL is valid; it is returned
1463 * just as any other value would be.
1465 * Returns: (transfer full): the result, which may be %NULL
1467 gpointer
1468 g_settings_get_mapped (GSettings *settings,
1469 const gchar *key,
1470 GSettingsGetMapping mapping,
1471 gpointer user_data)
1473 gpointer result = NULL;
1474 GSettingsSchemaKey skey;
1475 GVariant *value;
1476 gboolean okay;
1478 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1479 g_return_val_if_fail (key != NULL, NULL);
1480 g_return_val_if_fail (mapping != NULL, NULL);
1482 g_settings_schema_key_init (&skey, settings->priv->schema, key);
1484 if ((value = g_settings_read_from_backend (settings, &skey)))
1486 okay = mapping (value, &result, user_data);
1487 g_variant_unref (value);
1488 if (okay) goto okay;
1491 if ((value = g_settings_schema_key_get_translated_default (&skey)))
1493 okay = mapping (value, &result, user_data);
1494 g_variant_unref (value);
1495 if (okay) goto okay;
1498 if (mapping (skey.default_value, &result, user_data))
1499 goto okay;
1501 if (!mapping (NULL, &result, user_data))
1502 g_error ("The mapping function given to g_settings_get_mapped() for key "
1503 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1504 key, g_settings_schema_get_id (settings->priv->schema));
1506 okay:
1507 g_settings_schema_key_clear (&skey);
1509 return result;
1512 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1514 * g_settings_get_string:
1515 * @settings: a #GSettings object
1516 * @key: the key to get the value for
1518 * Gets the value that is stored at @key in @settings.
1520 * A convenience variant of g_settings_get() for strings.
1522 * It is a programmer error to give a @key that isn't specified as
1523 * having a string type in the schema for @settings.
1525 * Returns: a newly-allocated string
1527 * Since: 2.26
1529 gchar *
1530 g_settings_get_string (GSettings *settings,
1531 const gchar *key)
1533 GVariant *value;
1534 gchar *result;
1536 value = g_settings_get_value (settings, key);
1537 result = g_variant_dup_string (value, NULL);
1538 g_variant_unref (value);
1540 return result;
1544 * g_settings_set_string:
1545 * @settings: a #GSettings object
1546 * @key: the name of the key to set
1547 * @value: the value to set it to
1549 * Sets @key in @settings to @value.
1551 * A convenience variant of g_settings_set() for strings.
1553 * It is a programmer error to give a @key that isn't specified as
1554 * having a string type in the schema for @settings.
1556 * Returns: %TRUE if setting the key succeeded,
1557 * %FALSE if the key was not writable
1559 * Since: 2.26
1561 gboolean
1562 g_settings_set_string (GSettings *settings,
1563 const gchar *key,
1564 const gchar *value)
1566 return g_settings_set_value (settings, key, g_variant_new_string (value));
1570 * g_settings_get_int:
1571 * @settings: a #GSettings object
1572 * @key: the key to get the value for
1574 * Gets the value that is stored at @key in @settings.
1576 * A convenience variant of g_settings_get() for 32-bit integers.
1578 * It is a programmer error to give a @key that isn't specified as
1579 * having a int32 type in the schema for @settings.
1581 * Returns: an integer
1583 * Since: 2.26
1585 gint
1586 g_settings_get_int (GSettings *settings,
1587 const gchar *key)
1589 GVariant *value;
1590 gint result;
1592 value = g_settings_get_value (settings, key);
1593 result = g_variant_get_int32 (value);
1594 g_variant_unref (value);
1596 return result;
1600 * g_settings_set_int:
1601 * @settings: a #GSettings object
1602 * @key: the name of the key to set
1603 * @value: the value to set it to
1605 * Sets @key in @settings to @value.
1607 * A convenience variant of g_settings_set() for 32-bit integers.
1609 * It is a programmer error to give a @key that isn't specified as
1610 * having a int32 type in the schema for @settings.
1612 * Returns: %TRUE if setting the key succeeded,
1613 * %FALSE if the key was not writable
1615 * Since: 2.26
1617 gboolean
1618 g_settings_set_int (GSettings *settings,
1619 const gchar *key,
1620 gint value)
1622 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1626 * g_settings_get_uint:
1627 * @settings: a #GSettings object
1628 * @key: the key to get the value for
1630 * Gets the value that is stored at @key in @settings.
1632 * A convenience variant of g_settings_get() for 32-bit unsigned
1633 * integers.
1635 * It is a programmer error to give a @key that isn't specified as
1636 * having a uint32 type in the schema for @settings.
1638 * Returns: an unsigned integer
1640 * Since: 2.30
1642 guint
1643 g_settings_get_uint (GSettings *settings,
1644 const gchar *key)
1646 GVariant *value;
1647 guint result;
1649 value = g_settings_get_value (settings, key);
1650 result = g_variant_get_uint32 (value);
1651 g_variant_unref (value);
1653 return result;
1657 * g_settings_set_uint:
1658 * @settings: a #GSettings object
1659 * @key: the name of the key to set
1660 * @value: the value to set it to
1662 * Sets @key in @settings to @value.
1664 * A convenience variant of g_settings_set() for 32-bit unsigned
1665 * integers.
1667 * It is a programmer error to give a @key that isn't specified as
1668 * having a uint32 type in the schema for @settings.
1670 * Returns: %TRUE if setting the key succeeded,
1671 * %FALSE if the key was not writable
1673 * Since: 2.30
1675 gboolean
1676 g_settings_set_uint (GSettings *settings,
1677 const gchar *key,
1678 guint value)
1680 return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1684 * g_settings_get_double:
1685 * @settings: a #GSettings object
1686 * @key: the key to get the value for
1688 * Gets the value that is stored at @key in @settings.
1690 * A convenience variant of g_settings_get() for doubles.
1692 * It is a programmer error to give a @key that isn't specified as
1693 * having a 'double' type in the schema for @settings.
1695 * Returns: a double
1697 * Since: 2.26
1699 gdouble
1700 g_settings_get_double (GSettings *settings,
1701 const gchar *key)
1703 GVariant *value;
1704 gdouble result;
1706 value = g_settings_get_value (settings, key);
1707 result = g_variant_get_double (value);
1708 g_variant_unref (value);
1710 return result;
1714 * g_settings_set_double:
1715 * @settings: a #GSettings object
1716 * @key: the name of the key to set
1717 * @value: the value to set it to
1719 * Sets @key in @settings to @value.
1721 * A convenience variant of g_settings_set() for doubles.
1723 * It is a programmer error to give a @key that isn't specified as
1724 * having a 'double' type in the schema for @settings.
1726 * Returns: %TRUE if setting the key succeeded,
1727 * %FALSE if the key was not writable
1729 * Since: 2.26
1731 gboolean
1732 g_settings_set_double (GSettings *settings,
1733 const gchar *key,
1734 gdouble value)
1736 return g_settings_set_value (settings, key, g_variant_new_double (value));
1740 * g_settings_get_boolean:
1741 * @settings: a #GSettings object
1742 * @key: the key to get the value for
1744 * Gets the value that is stored at @key in @settings.
1746 * A convenience variant of g_settings_get() for booleans.
1748 * It is a programmer error to give a @key that isn't specified as
1749 * having a boolean type in the schema for @settings.
1751 * Returns: a boolean
1753 * Since: 2.26
1755 gboolean
1756 g_settings_get_boolean (GSettings *settings,
1757 const gchar *key)
1759 GVariant *value;
1760 gboolean result;
1762 value = g_settings_get_value (settings, key);
1763 result = g_variant_get_boolean (value);
1764 g_variant_unref (value);
1766 return result;
1770 * g_settings_set_boolean:
1771 * @settings: a #GSettings object
1772 * @key: the name of the key to set
1773 * @value: the value to set it to
1775 * Sets @key in @settings to @value.
1777 * A convenience variant of g_settings_set() for booleans.
1779 * It is a programmer error to give a @key that isn't specified as
1780 * having a boolean type in the schema for @settings.
1782 * Returns: %TRUE if setting the key succeeded,
1783 * %FALSE if the key was not writable
1785 * Since: 2.26
1787 gboolean
1788 g_settings_set_boolean (GSettings *settings,
1789 const gchar *key,
1790 gboolean value)
1792 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1796 * g_settings_get_strv:
1797 * @settings: a #GSettings object
1798 * @key: the key to get the value for
1800 * A convenience variant of g_settings_get() for string arrays.
1802 * It is a programmer error to give a @key that isn't specified as
1803 * having an array of strings type in the schema for @settings.
1805 * Returns: (array zero-terminated=1) (transfer full): a
1806 * newly-allocated, %NULL-terminated array of strings, the value that
1807 * is stored at @key in @settings.
1809 * Since: 2.26
1811 gchar **
1812 g_settings_get_strv (GSettings *settings,
1813 const gchar *key)
1815 GVariant *value;
1816 gchar **result;
1818 value = g_settings_get_value (settings, key);
1819 result = g_variant_dup_strv (value, NULL);
1820 g_variant_unref (value);
1822 return result;
1826 * g_settings_set_strv:
1827 * @settings: a #GSettings object
1828 * @key: the name of the key to set
1829 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1831 * Sets @key in @settings to @value.
1833 * A convenience variant of g_settings_set() for string arrays. If
1834 * @value is %NULL, then @key is set to be the empty array.
1836 * It is a programmer error to give a @key that isn't specified as
1837 * having an array of strings type in the schema for @settings.
1839 * Returns: %TRUE if setting the key succeeded,
1840 * %FALSE if the key was not writable
1842 * Since: 2.26
1844 gboolean
1845 g_settings_set_strv (GSettings *settings,
1846 const gchar *key,
1847 const gchar * const *value)
1849 GVariant *array;
1851 if (value != NULL)
1852 array = g_variant_new_strv (value, -1);
1853 else
1854 array = g_variant_new_strv (NULL, 0);
1856 return g_settings_set_value (settings, key, array);
1859 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1861 * g_settings_delay:
1862 * @settings: a #GSettings object
1864 * Changes the #GSettings object into 'delay-apply' mode. In this
1865 * mode, changes to @settings are not immediately propagated to the
1866 * backend, but kept locally until g_settings_apply() is called.
1868 * Since: 2.26
1870 void
1871 g_settings_delay (GSettings *settings)
1873 g_return_if_fail (G_IS_SETTINGS (settings));
1875 if (settings->priv->delayed)
1876 return;
1878 settings->priv->delayed =
1879 g_delayed_settings_backend_new (settings->priv->backend,
1880 settings,
1881 settings->priv->main_context);
1882 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1883 g_object_unref (settings->priv->backend);
1885 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1886 g_settings_backend_watch (settings->priv->backend,
1887 &listener_vtable, G_OBJECT (settings),
1888 settings->priv->main_context);
1890 g_object_notify (G_OBJECT (settings), "delay-apply");
1894 * g_settings_apply:
1895 * @settings: a #GSettings instance
1897 * Applies any changes that have been made to the settings. This
1898 * function does nothing unless @settings is in 'delay-apply' mode;
1899 * see g_settings_delay(). In the normal case settings are always
1900 * applied immediately.
1902 void
1903 g_settings_apply (GSettings *settings)
1905 if (settings->priv->delayed)
1907 GDelayedSettingsBackend *delayed;
1909 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1910 g_delayed_settings_backend_apply (delayed);
1915 * g_settings_revert:
1916 * @settings: a #GSettings instance
1918 * Reverts all non-applied changes to the settings. This function
1919 * does nothing unless @settings is in 'delay-apply' mode; see
1920 * g_settings_delay(). In the normal case settings are always applied
1921 * immediately.
1923 * Change notifications will be emitted for affected keys.
1925 void
1926 g_settings_revert (GSettings *settings)
1928 if (settings->priv->delayed)
1930 GDelayedSettingsBackend *delayed;
1932 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1933 g_delayed_settings_backend_revert (delayed);
1938 * g_settings_get_has_unapplied:
1939 * @settings: a #GSettings object
1941 * Returns whether the #GSettings object has any unapplied
1942 * changes. This can only be the case if it is in 'delayed-apply' mode.
1944 * Returns: %TRUE if @settings has unapplied changes
1946 * Since: 2.26
1948 gboolean
1949 g_settings_get_has_unapplied (GSettings *settings)
1951 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1953 return settings->priv->delayed &&
1954 g_delayed_settings_backend_get_has_unapplied (
1955 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1958 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
1960 * g_settings_reset:
1961 * @settings: a #GSettings object
1962 * @key: the name of a key
1964 * Resets @key to its default value.
1966 * This call resets the key, as much as possible, to its default value.
1967 * That might the value specified in the schema or the one set by the
1968 * administrator.
1970 void
1971 g_settings_reset (GSettings *settings,
1972 const gchar *key)
1974 gchar *path;
1976 path = g_strconcat (settings->priv->path, key, NULL);
1977 g_settings_backend_reset (settings->priv->backend, path, NULL);
1978 g_free (path);
1982 * g_settings_sync:
1984 * Ensures that all pending operations for the given are complete for
1985 * the default backend.
1987 * Writes made to a #GSettings are handled asynchronously. For this
1988 * reason, it is very unlikely that the changes have it to disk by the
1989 * time g_settings_set() returns.
1991 * This call will block until all of the writes have made it to the
1992 * backend. Since the mainloop is not running, no change notifications
1993 * will be dispatched during this call (but some may be queued by the
1994 * time the call is done).
1996 void
1997 g_settings_sync (void)
1999 g_settings_backend_sync_default ();
2003 * g_settings_is_writable:
2004 * @settings: a #GSettings object
2005 * @name: the name of a key
2007 * Finds out if a key can be written or not
2009 * Returns: %TRUE if the key @name is writable
2011 * Since: 2.26
2013 gboolean
2014 g_settings_is_writable (GSettings *settings,
2015 const gchar *name)
2017 gboolean writable;
2018 gchar *path;
2020 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2022 path = g_strconcat (settings->priv->path, name, NULL);
2023 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2024 g_free (path);
2026 return writable;
2030 * g_settings_get_child:
2031 * @settings: a #GSettings object
2032 * @name: the name of the 'child' schema
2034 * Creates a 'child' settings object which has a base path of
2035 * <replaceable>base-path</replaceable>/@name, where
2036 * <replaceable>base-path</replaceable> is the base path of @settings.
2038 * The schema for the child settings object must have been declared
2039 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2041 * Returns: (transfer full): a 'child' settings object
2043 * Since: 2.26
2045 GSettings *
2046 g_settings_get_child (GSettings *settings,
2047 const gchar *name)
2049 const gchar *child_schema;
2050 gchar *child_path;
2051 gchar *child_name;
2052 GSettings *child;
2054 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2056 child_name = g_strconcat (name, "/", NULL);
2057 child_schema = g_settings_schema_get_string (settings->priv->schema,
2058 child_name);
2059 if (child_schema == NULL)
2060 g_error ("Schema '%s' has no child '%s'",
2061 g_settings_schema_get_id (settings->priv->schema), name);
2063 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2064 child = g_object_new (G_TYPE_SETTINGS,
2065 "schema-id", child_schema,
2066 "path", child_path,
2067 NULL);
2068 g_free (child_path);
2069 g_free (child_name);
2071 return child;
2075 * g_settings_list_keys:
2076 * @settings: a #GSettings object
2078 * Introspects the list of keys on @settings.
2080 * You should probably not be calling this function from "normal" code
2081 * (since you should already know what keys are in your schema). This
2082 * function is intended for introspection reasons.
2084 * You should free the return value with g_strfreev() when you are done
2085 * with it.
2087 * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2089 gchar **
2090 g_settings_list_keys (GSettings *settings)
2092 const GQuark *keys;
2093 gchar **strv;
2094 gint n_keys;
2095 gint i, j;
2097 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2098 strv = g_new (gchar *, n_keys + 1);
2099 for (i = j = 0; i < n_keys; i++)
2101 const gchar *key = g_quark_to_string (keys[i]);
2103 if (!g_str_has_suffix (key, "/"))
2104 strv[j++] = g_strdup (key);
2106 strv[j] = NULL;
2108 return strv;
2112 * g_settings_list_children:
2113 * @settings: a #GSettings object
2115 * Gets the list of children on @settings.
2117 * The list is exactly the list of strings for which it is not an error
2118 * to call g_settings_get_child().
2120 * For GSettings objects that are lists, this value can change at any
2121 * time and you should connect to the "children-changed" signal to watch
2122 * for those changes. Note that there is a race condition here: you may
2123 * request a child after listing it only for it to have been destroyed
2124 * in the meantime. For this reason, g_settings_get_child() may return
2125 * %NULL even for a child that was listed by this function.
2127 * For GSettings objects that are not lists, you should probably not be
2128 * calling this function from "normal" code (since you should already
2129 * know what children are in your schema). This function may still be
2130 * useful there for introspection reasons, however.
2132 * You should free the return value with g_strfreev() when you are done
2133 * with it.
2135 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2137 gchar **
2138 g_settings_list_children (GSettings *settings)
2140 const GQuark *keys;
2141 gchar **strv;
2142 gint n_keys;
2143 gint i, j;
2145 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2146 strv = g_new (gchar *, n_keys + 1);
2147 for (i = j = 0; i < n_keys; i++)
2149 const gchar *key = g_quark_to_string (keys[i]);
2151 if (g_str_has_suffix (key, "/"))
2153 gint length = strlen (key);
2155 strv[j] = g_memdup (key, length);
2156 strv[j][length - 1] = '\0';
2157 j++;
2160 strv[j] = NULL;
2162 return strv;
2166 * g_settings_get_range:
2167 * @settings: a #GSettings
2168 * @key: the key to query the range of
2170 * Queries the range of a key.
2172 * This function will return a #GVariant that fully describes the range
2173 * of values that are valid for @key.
2175 * The type of #GVariant returned is <literal>(sv)</literal>. The
2176 * string describes the type of range restriction in effect. The type
2177 * and meaning of the value contained in the variant depends on the
2178 * string.
2180 * If the string is <literal>'type'</literal> then the variant contains
2181 * an empty array. The element type of that empty array is the expected
2182 * type of value and all values of that type are valid.
2184 * If the string is <literal>'enum'</literal> then the variant contains
2185 * an array enumerating the possible values. Each item in the array is
2186 * a possible valid value and no other values are valid.
2188 * If the string is <literal>'flags'</literal> then the variant contains
2189 * an array. Each item in the array is a value that may appear zero or
2190 * one times in an array to be used as the value for this key. For
2191 * example, if the variant contained the array <literal>['x',
2192 * 'y']</literal> then the valid values for the key would be
2193 * <literal>[]</literal>, <literal>['x']</literal>,
2194 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2195 * <literal>['y', 'x']</literal>.
2197 * Finally, if the string is <literal>'range'</literal> then the variant
2198 * contains a pair of like-typed values -- the minimum and maximum
2199 * permissible values for this key.
2201 * This information should not be used by normal programs. It is
2202 * considered to be a hint for introspection purposes. Normal programs
2203 * should already know what is permitted by their own schema. The
2204 * format may change in any way in the future -- but particularly, new
2205 * forms may be added to the possibilities described above.
2207 * It is a programmer error to give a @key that isn't contained in the
2208 * schema for @settings.
2210 * You should free the returned value with g_variant_unref() when it is
2211 * no longer needed.
2213 * Returns: a #GVariant describing the range
2215 * Since: 2.28
2217 GVariant *
2218 g_settings_get_range (GSettings *settings,
2219 const gchar *key)
2221 GSettingsSchemaKey skey;
2222 const gchar *type;
2223 GVariant *range;
2225 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2227 if (skey.minimum)
2229 range = g_variant_new ("(**)", skey.minimum, skey.maximum);
2230 type = "range";
2232 else if (skey.strinfo)
2234 range = strinfo_enumerate (skey.strinfo, skey.strinfo_length);
2235 type = skey.is_flags ? "flags" : "enum";
2237 else
2239 range = g_variant_new_array (skey.type, NULL, 0);
2240 type = "type";
2243 g_settings_schema_key_clear (&skey);
2245 return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2249 * g_settings_range_check:
2250 * @settings: a #GSettings
2251 * @key: the key to check
2252 * @value: the value to check
2254 * Checks if the given @value is of the correct type and within the
2255 * permitted range for @key.
2257 * This API is not intended to be used by normal programs -- they should
2258 * already know what is permitted by their own schemas. This API is
2259 * meant to be used by programs such as editors or commandline tools.
2261 * It is a programmer error to give a @key that isn't contained in the
2262 * schema for @settings.
2264 * Returns: %TRUE if @value is valid for @key
2266 * Since: 2.28
2268 gboolean
2269 g_settings_range_check (GSettings *settings,
2270 const gchar *key,
2271 GVariant *value)
2273 GSettingsSchemaKey skey;
2274 gboolean good;
2276 g_settings_schema_key_init (&skey, settings->priv->schema, key);
2277 good = g_settings_schema_key_type_check (&skey, value) &&
2278 g_settings_schema_key_range_check (&skey, value);
2279 g_settings_schema_key_clear (&skey);
2281 return good;
2284 /* Binding {{{1 */
2285 typedef struct
2287 GSettingsSchemaKey key;
2288 GSettings *settings;
2289 GObject *object;
2291 GSettingsBindGetMapping get_mapping;
2292 GSettingsBindSetMapping set_mapping;
2293 gpointer user_data;
2294 GDestroyNotify destroy;
2296 guint writable_handler_id;
2297 guint property_handler_id;
2298 const GParamSpec *property;
2299 guint key_handler_id;
2301 /* prevent recursion */
2302 gboolean running;
2303 } GSettingsBinding;
2305 static void
2306 g_settings_binding_free (gpointer data)
2308 GSettingsBinding *binding = data;
2310 g_assert (!binding->running);
2312 if (binding->writable_handler_id)
2313 g_signal_handler_disconnect (binding->settings,
2314 binding->writable_handler_id);
2316 if (binding->key_handler_id)
2317 g_signal_handler_disconnect (binding->settings,
2318 binding->key_handler_id);
2320 if (g_signal_handler_is_connected (binding->object,
2321 binding->property_handler_id))
2322 g_signal_handler_disconnect (binding->object,
2323 binding->property_handler_id);
2325 g_settings_schema_key_clear (&binding->key);
2327 if (binding->destroy)
2328 binding->destroy (binding->user_data);
2330 g_object_unref (binding->settings);
2332 g_slice_free (GSettingsBinding, binding);
2335 static GQuark
2336 g_settings_binding_quark (const char *property)
2338 GQuark quark;
2339 gchar *tmp;
2341 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2342 quark = g_quark_from_string (tmp);
2343 g_free (tmp);
2345 return quark;
2348 static void
2349 g_settings_binding_key_changed (GSettings *settings,
2350 const gchar *key,
2351 gpointer user_data)
2353 GSettingsBinding *binding = user_data;
2354 GValue value = G_VALUE_INIT;
2355 GVariant *variant;
2357 g_assert (settings == binding->settings);
2358 g_assert (key == binding->key.name);
2360 if (binding->running)
2361 return;
2363 binding->running = TRUE;
2365 g_value_init (&value, binding->property->value_type);
2367 variant = g_settings_read_from_backend (binding->settings, &binding->key);
2368 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2370 /* silently ignore errors in the user's config database */
2371 g_variant_unref (variant);
2372 variant = NULL;
2375 if (variant == NULL)
2377 variant = g_settings_schema_key_get_translated_default (&binding->key);
2378 if (variant &&
2379 !binding->get_mapping (&value, variant, binding->user_data))
2381 /* flag translation errors with a warning */
2382 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2383 "was rejected by the binding mapping function",
2384 binding->key.unparsed, binding->key.name,
2385 g_settings_schema_get_id (binding->key.schema));
2386 g_variant_unref (variant);
2387 variant = NULL;
2391 if (variant == NULL)
2393 variant = g_variant_ref (binding->key.default_value);
2394 if (!binding->get_mapping (&value, variant, binding->user_data))
2395 g_error ("The schema default value for key `%s' in schema `%s' "
2396 "was rejected by the binding mapping function.",
2397 binding->key.name, g_settings_schema_get_id (binding->key.schema));
2400 g_object_set_property (binding->object, binding->property->name, &value);
2401 g_variant_unref (variant);
2402 g_value_unset (&value);
2404 binding->running = FALSE;
2407 static void
2408 g_settings_binding_property_changed (GObject *object,
2409 const GParamSpec *pspec,
2410 gpointer user_data)
2412 GSettingsBinding *binding = user_data;
2413 GValue value = G_VALUE_INIT;
2414 GVariant *variant;
2416 g_assert (object == binding->object);
2417 g_assert (pspec == binding->property);
2419 if (binding->running)
2420 return;
2422 binding->running = TRUE;
2424 g_value_init (&value, pspec->value_type);
2425 g_object_get_property (object, pspec->name, &value);
2426 if ((variant = binding->set_mapping (&value, binding->key.type,
2427 binding->user_data)))
2429 g_variant_take_ref (variant);
2431 if (!g_settings_schema_key_type_check (&binding->key, variant))
2433 g_critical ("binding mapping function for key `%s' returned "
2434 "GVariant of type `%s' when type `%s' was requested",
2435 binding->key.name, g_variant_get_type_string (variant),
2436 g_variant_type_dup_string (binding->key.type));
2437 return;
2440 if (!g_settings_schema_key_range_check (&binding->key, variant))
2442 g_critical ("GObject property `%s' on a `%s' object is out of "
2443 "schema-specified range for key `%s' of `%s': %s",
2444 binding->property->name, g_type_name (binding->property->owner_type),
2445 binding->key.name, g_settings_schema_get_id (binding->key.schema),
2446 g_variant_print (variant, TRUE));
2447 return;
2450 g_settings_write_to_backend (binding->settings, &binding->key, variant);
2451 g_variant_unref (variant);
2453 g_value_unset (&value);
2455 binding->running = FALSE;
2458 static gboolean
2459 g_settings_bind_invert_boolean_get_mapping (GValue *value,
2460 GVariant *variant,
2461 gpointer user_data)
2463 g_value_set_boolean (value, !g_variant_get_boolean (variant));
2464 return TRUE;
2467 static GVariant *
2468 g_settings_bind_invert_boolean_set_mapping (const GValue *value,
2469 const GVariantType *expected_type,
2470 gpointer user_data)
2472 return g_variant_new_boolean (!g_value_get_boolean (value));
2476 * g_settings_bind:
2477 * @settings: a #GSettings object
2478 * @key: the key to bind
2479 * @object: (type GObject.Object): a #GObject
2480 * @property: the name of the property to bind
2481 * @flags: flags for the binding
2483 * Create a binding between the @key in the @settings object
2484 * and the property @property of @object.
2486 * The binding uses the default GIO mapping functions to map
2487 * between the settings and property values. These functions
2488 * handle booleans, numeric types and string types in a
2489 * straightforward way. Use g_settings_bind_with_mapping() if
2490 * you need a custom mapping, or map between types that are not
2491 * supported by the default mapping functions.
2493 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2494 * function also establishes a binding between the writability of
2495 * @key and the "sensitive" property of @object (if @object has
2496 * a boolean property by that name). See g_settings_bind_writable()
2497 * for more details about writable bindings.
2499 * Note that the lifecycle of the binding is tied to the object,
2500 * and that you can have only one binding per object property.
2501 * If you bind the same property twice on the same object, the second
2502 * binding overrides the first one.
2504 * Since: 2.26
2506 void
2507 g_settings_bind (GSettings *settings,
2508 const gchar *key,
2509 gpointer object,
2510 const gchar *property,
2511 GSettingsBindFlags flags)
2513 GSettingsBindGetMapping get_mapping = NULL;
2514 GSettingsBindSetMapping set_mapping = NULL;
2516 if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2518 get_mapping = g_settings_bind_invert_boolean_get_mapping;
2519 set_mapping = g_settings_bind_invert_boolean_set_mapping;
2521 /* can't pass this flag to g_settings_bind_with_mapping() */
2522 flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2525 g_settings_bind_with_mapping (settings, key, object, property, flags,
2526 get_mapping, set_mapping, NULL, NULL);
2530 * g_settings_bind_with_mapping: (skip)
2531 * @settings: a #GSettings object
2532 * @key: the key to bind
2533 * @object: (type GObject.Object): a #GObject
2534 * @property: the name of the property to bind
2535 * @flags: flags for the binding
2536 * @get_mapping: a function that gets called to convert values
2537 * from @settings to @object, or %NULL to use the default GIO mapping
2538 * @set_mapping: a function that gets called to convert values
2539 * from @object to @settings, or %NULL to use the default GIO mapping
2540 * @user_data: data that gets passed to @get_mapping and @set_mapping
2541 * @destroy: #GDestroyNotify function for @user_data
2543 * Create a binding between the @key in the @settings object
2544 * and the property @property of @object.
2546 * The binding uses the provided mapping functions to map between
2547 * settings and property values.
2549 * Note that the lifecycle of the binding is tied to the object,
2550 * and that you can have only one binding per object property.
2551 * If you bind the same property twice on the same object, the second
2552 * binding overrides the first one.
2554 * Since: 2.26
2556 void
2557 g_settings_bind_with_mapping (GSettings *settings,
2558 const gchar *key,
2559 gpointer object,
2560 const gchar *property,
2561 GSettingsBindFlags flags,
2562 GSettingsBindGetMapping get_mapping,
2563 GSettingsBindSetMapping set_mapping,
2564 gpointer user_data,
2565 GDestroyNotify destroy)
2567 GSettingsBinding *binding;
2568 GObjectClass *objectclass;
2569 gchar *detailed_signal;
2570 GQuark binding_quark;
2572 g_return_if_fail (G_IS_SETTINGS (settings));
2573 g_return_if_fail (key != NULL);
2574 g_return_if_fail (G_IS_OBJECT (object));
2575 g_return_if_fail (property != NULL);
2576 g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2578 objectclass = G_OBJECT_GET_CLASS (object);
2580 binding = g_slice_new0 (GSettingsBinding);
2581 g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2582 binding->settings = g_object_ref (settings);
2583 binding->object = object;
2584 binding->property = g_object_class_find_property (objectclass, property);
2585 binding->user_data = user_data;
2586 binding->destroy = destroy;
2587 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2588 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2590 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2591 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2593 if (binding->property == NULL)
2595 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2596 property, G_OBJECT_TYPE_NAME (object));
2597 return;
2600 if ((flags & G_SETTINGS_BIND_GET) &&
2601 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2603 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2604 "writable", property, G_OBJECT_TYPE_NAME (object));
2605 return;
2607 if ((flags & G_SETTINGS_BIND_SET) &&
2608 (binding->property->flags & G_PARAM_READABLE) == 0)
2610 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2611 "readable", property, G_OBJECT_TYPE_NAME (object));
2612 return;
2615 if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2617 /* g_settings_bind_invert_boolean_get_mapping() is a private
2618 * function, so if we are here it means that g_settings_bind() was
2619 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2621 * Ensure that both sides are boolean.
2624 if (binding->property->value_type != G_TYPE_BOOLEAN)
2626 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2627 "was specified, but property `%s' on type `%s' has "
2628 "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2629 g_type_name ((binding->property->value_type)));
2630 return;
2633 if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2635 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2636 "was specified, but key `%s' on schema `%s' has "
2637 "type `%s'", key, g_settings_schema_get_id (settings->priv->schema),
2638 g_variant_type_dup_string (binding->key.type));
2639 return;
2644 else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2645 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2646 !g_settings_mapping_is_compatible (binding->property->value_type,
2647 binding->key.type))
2649 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2650 "'%s' which is not compatible with type '%s' of key '%s' "
2651 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2652 g_type_name (binding->property->value_type),
2653 g_variant_type_dup_string (binding->key.type), key,
2654 g_settings_schema_get_id (settings->priv->schema));
2655 return;
2658 if ((flags & G_SETTINGS_BIND_SET) &&
2659 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2661 GParamSpec *sensitive;
2663 sensitive = g_object_class_find_property (objectclass, "sensitive");
2665 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2666 (sensitive->flags & G_PARAM_WRITABLE))
2667 g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2670 if (flags & G_SETTINGS_BIND_SET)
2672 detailed_signal = g_strdup_printf ("notify::%s", property);
2673 binding->property_handler_id =
2674 g_signal_connect (object, detailed_signal,
2675 G_CALLBACK (g_settings_binding_property_changed),
2676 binding);
2677 g_free (detailed_signal);
2679 if (~flags & G_SETTINGS_BIND_GET)
2680 g_settings_binding_property_changed (object,
2681 binding->property,
2682 binding);
2685 if (flags & G_SETTINGS_BIND_GET)
2687 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2689 detailed_signal = g_strdup_printf ("changed::%s", key);
2690 binding->key_handler_id =
2691 g_signal_connect (settings, detailed_signal,
2692 G_CALLBACK (g_settings_binding_key_changed),
2693 binding);
2694 g_free (detailed_signal);
2697 g_settings_binding_key_changed (settings, binding->key.name, binding);
2700 binding_quark = g_settings_binding_quark (property);
2701 g_object_set_qdata_full (object, binding_quark,
2702 binding, g_settings_binding_free);
2705 /* Writability binding {{{1 */
2706 typedef struct
2708 GSettings *settings;
2709 gpointer object;
2710 const gchar *key;
2711 const gchar *property;
2712 gboolean inverted;
2713 gulong handler_id;
2714 } GSettingsWritableBinding;
2716 static void
2717 g_settings_writable_binding_free (gpointer data)
2719 GSettingsWritableBinding *binding = data;
2721 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2722 g_object_unref (binding->settings);
2723 g_slice_free (GSettingsWritableBinding, binding);
2726 static void
2727 g_settings_binding_writable_changed (GSettings *settings,
2728 const gchar *key,
2729 gpointer user_data)
2731 GSettingsWritableBinding *binding = user_data;
2732 gboolean writable;
2734 g_assert (settings == binding->settings);
2735 g_assert (key == binding->key);
2737 writable = g_settings_is_writable (settings, key);
2739 if (binding->inverted)
2740 writable = !writable;
2742 g_object_set (binding->object, binding->property, writable, NULL);
2746 * g_settings_bind_writable:
2747 * @settings: a #GSettings object
2748 * @key: the key to bind
2749 * @object: (type GObject.Object):a #GObject
2750 * @property: the name of a boolean property to bind
2751 * @inverted: whether to 'invert' the value
2753 * Create a binding between the writability of @key in the
2754 * @settings object and the property @property of @object.
2755 * The property must be boolean; "sensitive" or "visible"
2756 * properties of widgets are the most likely candidates.
2758 * Writable bindings are always uni-directional; changes of the
2759 * writability of the setting will be propagated to the object
2760 * property, not the other way.
2762 * When the @inverted argument is %TRUE, the binding inverts the
2763 * value as it passes from the setting to the object, i.e. @property
2764 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2765 * writable.
2767 * Note that the lifecycle of the binding is tied to the object,
2768 * and that you can have only one binding per object property.
2769 * If you bind the same property twice on the same object, the second
2770 * binding overrides the first one.
2772 * Since: 2.26
2774 void
2775 g_settings_bind_writable (GSettings *settings,
2776 const gchar *key,
2777 gpointer object,
2778 const gchar *property,
2779 gboolean inverted)
2781 GSettingsWritableBinding *binding;
2782 gchar *detailed_signal;
2783 GParamSpec *pspec;
2785 g_return_if_fail (G_IS_SETTINGS (settings));
2787 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2788 if (pspec == NULL)
2790 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2791 property, G_OBJECT_TYPE_NAME (object));
2792 return;
2794 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2796 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2797 property, G_OBJECT_TYPE_NAME (object));
2798 return;
2801 binding = g_slice_new (GSettingsWritableBinding);
2802 binding->settings = g_object_ref (settings);
2803 binding->object = object;
2804 binding->key = g_intern_string (key);
2805 binding->property = g_intern_string (property);
2806 binding->inverted = inverted;
2808 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2809 binding->handler_id =
2810 g_signal_connect (settings, detailed_signal,
2811 G_CALLBACK (g_settings_binding_writable_changed),
2812 binding);
2813 g_free (detailed_signal);
2815 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2816 binding, g_settings_writable_binding_free);
2818 g_settings_binding_writable_changed (settings, binding->key, binding);
2822 * g_settings_unbind:
2823 * @object: the object
2824 * @property: the property whose binding is removed
2826 * Removes an existing binding for @property on @object.
2828 * Note that bindings are automatically removed when the
2829 * object is finalized, so it is rarely necessary to call this
2830 * function.
2832 * Since: 2.26
2834 void
2835 g_settings_unbind (gpointer object,
2836 const gchar *property)
2838 GQuark binding_quark;
2840 binding_quark = g_settings_binding_quark (property);
2841 g_object_set_qdata (object, binding_quark, NULL);
2844 /* Epilogue {{{1 */
2846 /* vim:set foldmethod=marker: */