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>
30 #include "gsettings.h"
32 #include "gdelayedsettingsbackend.h"
33 #include "gsettingsbackendinternal.h"
34 #include "gsettings-mapping.h"
35 #include "gio-marshal.h"
36 #include "gsettingsschema.h"
45 * @short_description: a high-level API for application settings
47 * The #GSettings class provides a convenient API for storing and retrieving
48 * application settings.
50 * When creating a GSettings instance, you have to specify a schema
51 * that describes the keys in your settings and their types and default
52 * values, as well as some other information.
54 * Normally, a schema has as fixed path that determines where the settings
55 * are stored in the conceptual global tree of settings. However, schemas
56 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
57 * useful e.g. when the schema describes an 'account', and you want to be
58 * able to store a arbitrary number of accounts.
60 * Unlike other configuration systems (like GConf), GSettings does not
61 * restrict keys to basic types like strings and numbers. GSettings stores
62 * values as #GVariant, and allows any #GVariantType for keys. Key names
63 * are restricted to lowercase characters, numbers and '-'. Furthermore,
64 * the names must begin with a lowercase character, must not end
65 * with a '-', and must not contain consecutive dashes. Key names can
66 * be up to 32 characters long.
68 * Similar to GConf, the default values in GSettings schemas can be
69 * localized, but the localized values are stored in gettext catalogs
70 * and looked up with the domain that is specified in the
71 * <tag class="attribute">gettext-domain</tag> attribute of the
72 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
73 * elements and the category that is specified in the l10n attribute of the
74 * <tag class="starttag">key</tag> element.
76 * GSettings uses schemas in a compact binary form that is created
77 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
78 * utility. The input is a schema description in an XML format that can be
79 * described by the following DTD:
81 * <!ELEMENT schemalist (schema|enum)* >
82 * <!ATTLIST schemalist gettext-domain #IMPLIED >
84 * <!ELEMENT schema (key|child)* >
85 * <!ATTLIST schema id CDATA #REQUIRED
87 * gettext-domain CDATA #IMPLIED >
89 * <!-- defines an enumerated type -->
90 * <!-- each value element maps a nick to a numeric value -->
91 * <!ELEMENT enum (value*) >
92 * <!ATTLIST enum id CDATA #REQUIRED >
93 * <!ELEMENT value EMPTY >
94 * <!-- nick must be at least 2 characters long -->
95 * <!-- value must be parsable as a 32-bit integer -->
96 * <!ELEMENT value nick #REQUIRED
99 * <!ELEMENT key (default|summary?|description?|range?|choices?|aliases?) >
100 * <!-- name can only contain lowercase letters, numbers and '-' -->
101 * <!-- type must be a GVariant type string -->
102 * <!-- enum must be the id of an enum that has been defined earlier -->
103 * <!-- exactly one of enum or type must be given -->
104 * <!ATTLIST key name CDATA #REQUIRED
105 * type CDATA #IMPLIED
106 * enum CDATA #IMPLIED >
108 * <!-- the default value is specified a a serialized GVariant,
109 * i.e. you have to include the quotes when specifying a string -->
110 * <!ELEMENT default (#PCDATA) >
111 * <!-- the presence of the l10n attribute marks a default value for
112 * translation, its value is the gettext category to use -->
113 * <!-- if context is present, it specifies msgctxt to use -->
114 * <!ATTLIST default l10n (messages|time) #IMPLIED
115 * context CDATA #IMPLIED >
117 * <!ELEMENT summary (#PCDATA) >
118 * <!ELEMENT description (#PCDATA) >
120 * <!-- range is only allowed for keys with numeric type -->
121 * <!ELEMENT range EMPTY >
122 * <!-- min and max must be parseable as values of the key type and min < max -->
123 * <!ATTLIST range min CDATA #REQUIRED
124 * max CDATA #REQUIRED >
126 * <!-- choices is only allowed for keys with string or string array type -->
127 * <!ELEMENT choices (choice+) >
128 * <!-- each choice element specifies one possible value -->
129 * <!ELEMENT choice EMPTY >
130 * <!ATTLIST choice value CDATA #REQUIRED >
132 * <!-- aliases is only allowed for keys with enumerated type or with choices -->
133 * <!ELEMENT aliases (alias+) >
134 * <!-- each alias element specifies an alias for one of the possible values -->
135 * <!ELEMENT alias EMPTY >
136 * <!ATTLIST alias value CDATA #REQUIRED
137 * target CDATA #REQUIRED >
139 * <!ELEMENT child EMPTY >
140 * <!ATTLIST child name CDATA #REQUIRED
141 * schema CDATA #REQUIRED >
145 * At runtime, schemas are identified by their id (as specified
146 * in the <tag class="attribute">id</tag> attribute of the
147 * <tag class="starttag">schema</tag> element). The
148 * convention for schema ids is to use a dotted name, similar in
149 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
151 * <example><title>Default values</title>
152 * <programlisting><![CDATA[
154 * <schema id="org.gtk.test" path="/tests/" gettext-domain="test">
156 * <key name="greeting" type="s">
157 * <default l10n="messages">"Hello, earthlings"</default>
158 * <summary>A greeting</summary>
160 * Greeting of the invading martians
164 * <key name="box" type="(ii)">
165 * <default>(20,30)</default>
170 * ]]></programlisting></example>
172 * <example><title>Ranges, choices and enumerated types</title>
173 * <programlisting><![CDATA[
177 * <value nick="first" value="1"/>
178 * <value nick="second" value="2"/>
181 * <schema id="org.gtk.test">
183 * <key name="key-with-range" type="i">
184 * <range min="1" max="100"/>
185 * <default>10</default>
188 * <key name="key-with-choices" type="s">
190 * <choice value='Elisabeth'/>
191 * <choice value='Annabeth'/>
192 * <choice value='Joe'/>
195 * <alias value='Anna' target='Annabeth'/>
196 * <alias value='Beth' target='Elisabeth'/>
198 * <default>'Joe'</default>
201 * <key name='enumerated-key' enum='myenum'>
202 * <default>'first'</default>
207 * ]]></programlisting></example>
210 * <title>Binding</title>
212 * A very convenient feature of GSettings lets you bind #GObject properties
213 * directly to settings, using g_settings_bind(). Once a GObject property
214 * has been bound to a setting, changes on either side are automatically
215 * propagated to the other side. GSettings handles details like
216 * mapping between GObject and GVariant types, and preventing infinite
220 * This makes it very easy to hook up a preferences dialog to the
221 * underlying settings. To make this even more convenient, GSettings
222 * looks for a boolean property with the name "sensitivity" and
223 * automatically binds it to the writability of the bound setting.
224 * If this 'magic' gets in the way, it can be suppressed with the
225 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
230 struct _GSettingsPrivate
232 /* where the signals go... */
233 GMainContext *main_context;
235 GSettingsBackend *backend;
236 GSettingsSchema *schema;
240 GDelayedSettingsBackend *delayed;
254 SIGNAL_WRITABLE_CHANGE_EVENT,
255 SIGNAL_WRITABLE_CHANGED,
261 static guint g_settings_signals[N_SIGNALS];
263 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
267 g_settings_real_change_event (GSettings *settings,
274 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
276 for (i = 0; i < n_keys; i++)
277 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
278 keys[i], g_quark_to_string (keys[i]));
284 g_settings_real_writable_change_event (GSettings *settings,
287 const GQuark *keys = &key;
292 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
294 for (i = 0; i < n_keys; i++)
296 const gchar *string = g_quark_to_string (keys[i]);
298 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
300 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
308 settings_backend_changed (GSettingsBackend *backend,
313 GSettings *settings = G_SETTINGS (target);
314 gboolean ignore_this;
317 g_assert (settings->priv->backend == backend);
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))
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);
333 settings_backend_path_changed (GSettingsBackend *backend,
338 GSettings *settings = G_SETTINGS (target);
339 gboolean ignore_this;
341 g_assert (settings->priv->backend == backend);
343 if (g_str_has_prefix (settings->priv->path, path))
344 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
345 0, NULL, 0, &ignore_this);
349 settings_backend_keys_changed (GSettingsBackend *backend,
352 const gchar * const *items,
355 GSettings *settings = G_SETTINGS (target);
356 gboolean ignore_this;
359 g_assert (settings->priv->backend == backend);
361 for (i = 0; settings->priv->path[i] &&
362 settings->priv->path[i] == path[i]; i++);
369 for (j = 0; items[j]; j++)
371 const gchar *item = items[j];
374 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
376 if (settings->priv->path[i + k] == '\0' &&
377 g_settings_schema_has_key (settings->priv->schema, item + k))
378 quarks[l++] = g_quark_from_string (item + k);
380 /* "256 quarks ought to be enough for anybody!"
381 * If this bites you, I'm sorry. Please file a bug.
387 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
388 0, quarks, l, &ignore_this);
393 settings_backend_writable_changed (GSettingsBackend *backend,
397 GSettings *settings = G_SETTINGS (target);
398 gboolean ignore_this;
401 g_assert (settings->priv->backend == backend);
403 for (i = 0; key[i] == settings->priv->path[i]; i++);
405 if (settings->priv->path[i] == '\0' &&
406 g_settings_schema_has_key (settings->priv->schema, key + i))
407 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
408 0, g_quark_from_string (key + i), &ignore_this);
412 settings_backend_path_writable_changed (GSettingsBackend *backend,
416 GSettings *settings = G_SETTINGS (target);
417 gboolean ignore_this;
419 g_assert (settings->priv->backend == backend);
421 if (g_str_has_prefix (settings->priv->path, path))
422 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
423 0, (GQuark) 0, &ignore_this);
426 /* Properties, Construction, Destruction {{{1 */
428 g_settings_set_property (GObject *object,
433 GSettings *settings = G_SETTINGS (object);
438 g_assert (settings->priv->schema_name == NULL);
439 settings->priv->schema_name = g_value_dup_string (value);
443 settings->priv->path = g_value_dup_string (value);
447 settings->priv->backend = g_value_dup_object (value);
451 g_assert_not_reached ();
456 g_settings_get_property (GObject *object,
461 GSettings *settings = G_SETTINGS (object);
466 g_value_set_string (value, settings->priv->schema_name);
469 case PROP_HAS_UNAPPLIED:
470 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
474 g_assert_not_reached ();
479 g_settings_constructed (GObject *object)
481 GSettings *settings = G_SETTINGS (object);
482 const gchar *schema_path;
484 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
485 schema_path = g_settings_schema_get_path (settings->priv->schema);
487 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
488 g_error ("settings object created with schema '%s' and path '%s', but "
489 "path '%s' is specified by schema",
490 settings->priv->schema_name, settings->priv->path, schema_path);
492 if (settings->priv->path == NULL)
494 if (schema_path == NULL)
495 g_error ("attempting to create schema '%s' without a path",
496 settings->priv->schema_name);
498 settings->priv->path = g_strdup (schema_path);
501 if (settings->priv->backend == NULL)
502 settings->priv->backend = g_settings_backend_get_default ();
504 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
505 settings->priv->main_context,
506 settings_backend_changed,
507 settings_backend_path_changed,
508 settings_backend_keys_changed,
509 settings_backend_writable_changed,
510 settings_backend_path_writable_changed);
511 g_settings_backend_subscribe (settings->priv->backend,
512 settings->priv->path);
516 g_settings_finalize (GObject *object)
518 GSettings *settings = G_SETTINGS (object);
520 g_settings_backend_unsubscribe (settings->priv->backend,
521 settings->priv->path);
522 g_main_context_unref (settings->priv->main_context);
523 g_object_unref (settings->priv->backend);
524 g_object_unref (settings->priv->schema);
525 g_free (settings->priv->schema_name);
526 g_free (settings->priv->path);
530 g_settings_init (GSettings *settings)
532 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
536 settings->priv->main_context = g_main_context_get_thread_default ();
538 if (settings->priv->main_context == NULL)
539 settings->priv->main_context = g_main_context_default ();
541 g_main_context_ref (settings->priv->main_context);
545 g_settings_class_init (GSettingsClass *class)
547 GObjectClass *object_class = G_OBJECT_CLASS (class);
549 class->writable_change_event = g_settings_real_writable_change_event;
550 class->change_event = g_settings_real_change_event;
552 object_class->set_property = g_settings_set_property;
553 object_class->get_property = g_settings_get_property;
554 object_class->constructed = g_settings_constructed;
555 object_class->finalize = g_settings_finalize;
557 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
560 * GSettings::changed:
561 * @settings: the object on which the signal was emitted
562 * @key: the name of the key that changed
564 * The "changed" signal is emitted when a key has potentially changed.
565 * You should call one of the g_settings_get() calls to check the new
568 * This signal supports detailed connections. You can connect to the
569 * detailed signal "changed::x" in order to only receive callbacks
570 * when key "x" changes.
572 g_settings_signals[SIGNAL_CHANGED] =
573 g_signal_new ("changed", G_TYPE_SETTINGS,
574 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
575 G_STRUCT_OFFSET (GSettingsClass, changed),
576 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
577 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
580 * GSettings::change-event:
581 * @settings: the object on which the signal was emitted
582 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
583 * @n_keys: the length of the @keys array, or 0
584 * @returns: %TRUE to stop other handlers from being invoked for the
585 * event. FALSE to propagate the event further.
587 * The "change-event" signal is emitted once per change event that
588 * affects this settings object. You should connect to this signal
589 * only if you are interested in viewing groups of changes before they
590 * are split out into multiple emissions of the "changed" signal.
591 * For most use cases it is more appropriate to use the "changed" signal.
593 * In the event that the change event applies to one or more specified
594 * keys, @keys will be an array of #GQuark of length @n_keys. In the
595 * event that the change event applies to the #GSettings object as a
596 * whole (ie: potentially every key has been changed) then @keys will
597 * be %NULL and @n_keys will be 0.
599 * The default handler for this signal invokes the "changed" signal
600 * for each affected key. If any other connected handler returns
601 * %TRUE then this default functionality will be supressed.
603 g_settings_signals[SIGNAL_CHANGE_EVENT] =
604 g_signal_new ("change-event", G_TYPE_SETTINGS,
606 G_STRUCT_OFFSET (GSettingsClass, change_event),
607 g_signal_accumulator_true_handled, NULL,
608 _gio_marshal_BOOL__POINTER_INT,
609 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
612 * GSettings::writable-changed:
613 * @settings: the object on which the signal was emitted
616 * The "writable-changed" signal is emitted when the writability of a
617 * key has potentially changed. You should call
618 * g_settings_is_writable() in order to determine the new status.
620 * This signal supports detailed connections. You can connect to the
621 * detailed signal "writable-changed::x" in order to only receive
622 * callbacks when the writability of "x" changes.
624 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
625 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
626 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
627 G_STRUCT_OFFSET (GSettingsClass, changed),
628 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
629 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
632 * GSettings::writable-change-event:
633 * @settings: the object on which the signal was emitted
634 * @key: the quark of the key, or 0
635 * @returns: %TRUE to stop other handlers from being invoked for the
636 * event. FALSE to propagate the event further.
638 * The "writable-change-event" signal is emitted once per writability
639 * change event that affects this settings object. You should connect
640 * to this signal if you are interested in viewing groups of changes
641 * before they are split out into multiple emissions of the
642 * "writable-changed" signal. For most use cases it is more
643 * appropriate to use the "writable-changed" signal.
645 * In the event that the writability change applies only to a single
646 * key, @key will be set to the #GQuark for that key. In the event
647 * that the writability change affects the entire settings object,
650 * The default handler for this signal invokes the "writable-changed"
651 * and "changed" signals for each affected key. This is done because
652 * changes in writability might also imply changes in value (if for
653 * example, a new mandatory setting is introduced). If any other
654 * connected handler returns %TRUE then this default functionality
657 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
658 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
660 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
661 g_signal_accumulator_true_handled, NULL,
662 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
667 * The name of the context that the settings are stored in.
669 g_object_class_install_property (object_class, PROP_BACKEND,
670 g_param_spec_object ("backend",
671 P_("GSettingsBackend"),
672 P_("The GSettingsBackend for this settings object"),
673 G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
674 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
679 * The name of the schema that describes the types of keys
680 * for this #GSettings object.
682 g_object_class_install_property (object_class, PROP_SCHEMA,
683 g_param_spec_string ("schema",
685 P_("The name of the schema for this settings object"),
687 G_PARAM_CONSTRUCT_ONLY |
688 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
693 * The path within the backend where the settings are stored.
695 g_object_class_install_property (object_class, PROP_PATH,
696 g_param_spec_string ("path",
698 P_("The path within the backend where the settings are"),
700 G_PARAM_CONSTRUCT_ONLY |
701 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
704 * GSettings:has-unapplied:
706 * If this property is %TRUE, the #GSettings object has outstanding
707 * changes that will be applied when g_settings_apply() is called.
709 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
710 g_param_spec_boolean ("has-unapplied",
711 P_("Has unapplied changes"),
712 P_("TRUE if there are outstanding changes to apply()"),
714 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
718 /* Construction (new, new_with_path, etc.) {{{1 */
721 * @schema: the name of the schema
722 * @returns: a new #GSettings object
724 * Creates a new #GSettings object with a given schema.
726 * Signals on the newly created #GSettings object will be dispatched
727 * via the thread-default #GMainContext in effect at the time of the
728 * call to g_settings_new(). The new #GSettings will hold a reference
729 * on the context. See g_main_context_push_thread_default().
734 g_settings_new (const gchar *schema)
736 g_return_val_if_fail (schema != NULL, NULL);
738 return g_object_new (G_TYPE_SETTINGS,
744 * g_settings_new_with_path:
745 * @schema: the name of the schema
746 * @path: the path to use
747 * @returns: a new #GSettings object
749 * Creates a new #GSettings object with a given schema and path.
751 * You only need to do this if you want to directly create a settings
752 * object with a schema that doesn't have a specified path of its own.
755 * It is a programmer error to call this function for a schema that
756 * has an explicitly specified path.
761 g_settings_new_with_path (const gchar *schema,
764 g_return_val_if_fail (schema != NULL, NULL);
765 g_return_val_if_fail (path != NULL, NULL);
767 return g_object_new (G_TYPE_SETTINGS,
774 * g_settings_new_with_backend:
775 * @schema: the name of the schema
776 * @backend: the #GSettingsBackend to use
777 * @returns: a new #GSettings object
779 * Creates a new #GSettings object with a given schema and backend.
781 * Creating settings objects with an different backend allows accessing settings
782 * from a database other than the usual one. For example, it may make
783 * sense to pass a backend corresponding to the "defaults" settings database on
784 * the system to get a settings object that modifies the system default
785 * settings instead of the settings for this user.
790 g_settings_new_with_backend (const gchar *schema,
791 GSettingsBackend *backend)
793 g_return_val_if_fail (schema != NULL, NULL);
794 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
796 return g_object_new (G_TYPE_SETTINGS,
803 * g_settings_new_with_backend_and_path:
804 * @schema: the name of the schema
805 * @backend: the #GSettingsBackend to use
806 * @path: the path to use
807 * @returns: a new #GSettings object
809 * Creates a new #GSettings object with a given schema, backend and
812 * This is a mix of g_settings_new_with_backend() and
813 * g_settings_new_with_path().
818 g_settings_new_with_backend_and_path (const gchar *schema,
819 GSettingsBackend *backend,
822 g_return_val_if_fail (schema != NULL, NULL);
823 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
824 g_return_val_if_fail (path != NULL, NULL);
826 return g_object_new (G_TYPE_SETTINGS,
833 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
839 GSettingsSchema *schema;
844 const guint32 *strinfo;
845 gsize strinfo_length;
847 const gchar *unparsed;
850 const GVariantType *type;
851 GVariant *minimum, *maximum;
852 GVariant *default_value;
856 g_settings_get_key_info (GSettingsKeyInfo *info,
864 memset (info, 0, sizeof *info);
866 iter = g_settings_schema_get_value (settings->priv->schema, key);
868 info->default_value = g_variant_iter_next_value (iter);
869 info->type = g_variant_get_type (info->default_value);
870 info->settings = g_object_ref (settings);
871 info->key = g_intern_string (key);
873 while (g_variant_iter_next (iter, "(y*)", &code, &data))
878 /* translation requested */
879 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
883 /* enumerated types... */
884 info->is_enum = TRUE;
889 info->is_flags = TRUE;
893 /* ..., choices, aliases */
894 info->strinfo = g_variant_get_fixed_array (data,
895 &info->strinfo_length,
900 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
904 g_warning ("unknown schema extension '%c'", code);
908 g_variant_unref (data);
911 g_variant_iter_free (iter);
915 g_settings_free_key_info (GSettingsKeyInfo *info)
918 g_variant_unref (info->minimum);
921 g_variant_unref (info->maximum);
923 g_variant_unref (info->default_value);
924 g_object_unref (info->settings);
928 g_settings_write_to_backend (GSettingsKeyInfo *info,
934 path = g_strconcat (info->settings->priv->path, info->key, NULL);
935 success = g_settings_backend_write (info->settings->priv->backend,
943 g_settings_type_check (GSettingsKeyInfo *info,
946 g_return_val_if_fail (value != NULL, FALSE);
948 return g_variant_is_of_type (value, info->type);
952 g_settings_range_check (GSettingsKeyInfo *info,
955 if (info->minimum == NULL && info->strinfo == NULL)
958 if (g_variant_is_container (value))
964 g_variant_iter_init (&iter, value);
965 while (ok && (child = g_variant_iter_next_value (&iter)))
967 ok = g_settings_range_check (info, child);
968 g_variant_unref (child);
976 return g_variant_compare (info->minimum, value) <= 0 &&
977 g_variant_compare (value, info->maximum) <= 0;
980 return strinfo_is_string_valid (info->strinfo,
981 info->strinfo_length,
982 g_variant_get_string (value, NULL));
986 g_settings_range_fixup (GSettingsKeyInfo *info,
991 if (g_settings_range_check (info, value))
992 return g_variant_ref (value);
994 if (info->strinfo == NULL)
997 if (g_variant_is_container (value))
999 GVariantBuilder builder;
1003 g_variant_iter_init (&iter, value);
1004 g_variant_builder_init (&builder, g_variant_get_type (value));
1006 while ((child = g_variant_iter_next_value (&iter)))
1010 fixed = g_settings_range_fixup (info, child);
1011 g_variant_unref (child);
1015 g_variant_builder_clear (&builder);
1019 g_variant_builder_add_value (&builder, fixed);
1020 g_variant_unref (fixed);
1023 return g_variant_ref_sink (g_variant_builder_end (&builder));
1026 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1027 g_variant_get_string (value, NULL));
1028 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1032 g_settings_read_from_backend (GSettingsKeyInfo *info)
1038 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1039 value = g_settings_backend_read (info->settings->priv->backend,
1040 path, info->type, FALSE);
1045 fixup = g_settings_range_fixup (info, value);
1046 g_variant_unref (value);
1055 g_settings_get_translated_default (GSettingsKeyInfo *info)
1057 const gchar *translated;
1058 GError *error = NULL;
1059 const gchar *domain;
1062 if (info->lc_char == '\0')
1063 /* translation not requested for this key */
1066 domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
1068 if (info->lc_char == 't')
1069 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1071 translated = g_dgettext (domain, info->unparsed);
1073 if (translated == info->unparsed)
1074 /* the default value was not translated */
1077 /* try to parse the translation of the unparsed default */
1078 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1082 g_warning ("Failed to parse translated string `%s' for "
1083 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1084 info->settings->priv->schema_name, error->message);
1085 g_warning ("Using untranslated default instead.");
1086 g_error_free (error);
1089 else if (!g_settings_range_check (info, value))
1091 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1092 "is outside of valid range", info->unparsed, info->key,
1093 info->settings->priv->schema_name);
1094 g_variant_unref (value);
1102 g_settings_to_enum (GSettingsKeyInfo *info,
1108 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1109 g_variant_get_string (value, NULL),
1112 /* 'value' can only come from the backend after being filtered for validity,
1113 * from the translation after being filtered for validity, or from the schema
1114 * itself (which the schema compiler checks for validity). If this assertion
1115 * fails then it's really a bug in GSettings or the schema compiler...
1117 g_assert (it_worked);
1123 g_settings_from_enum (GSettingsKeyInfo *info,
1126 const gchar *string;
1128 string = strinfo_string_from_enum (info->strinfo,
1129 info->strinfo_length,
1135 return g_variant_new_string (string);
1139 g_settings_to_flags (GSettingsKeyInfo *info,
1147 g_variant_iter_init (&iter, value);
1148 while (g_variant_iter_next (&iter, "&s", &flag))
1153 it_worked = strinfo_enum_from_string (info->strinfo,
1154 info->strinfo_length,
1156 /* as in g_settings_to_enum() */
1157 g_assert (it_worked);
1159 result |= flag_value;
1166 g_settings_from_flags (GSettingsKeyInfo *info,
1169 GVariantBuilder builder;
1172 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1174 for (i = 0; i < 32; i++)
1175 if (value & (1u << i))
1177 const gchar *string;
1179 string = strinfo_string_from_enum (info->strinfo,
1180 info->strinfo_length,
1185 g_variant_builder_clear (&builder);
1189 g_variant_builder_add (&builder, "s", string);
1192 return g_variant_builder_end (&builder);
1195 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1197 * g_settings_get_value:
1198 * @settings: a #GSettings object
1199 * @key: the key to get the value for
1200 * @returns: a new #GVariant
1202 * Gets the value that is stored in @settings for @key.
1204 * It is a programmer error to give a @key that isn't contained in the
1205 * schema for @settings.
1210 g_settings_get_value (GSettings *settings,
1213 GSettingsKeyInfo info;
1216 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1217 g_return_val_if_fail (key != NULL, NULL);
1219 g_settings_get_key_info (&info, settings, key);
1220 value = g_settings_read_from_backend (&info);
1223 value = g_settings_get_translated_default (&info);
1226 value = g_variant_ref (info.default_value);
1228 g_settings_free_key_info (&info);
1234 * g_settings_get_enum:
1235 * @settings: a #GSettings object
1236 * @key: the key to get the value for
1237 * @returns: the enum value
1239 * Gets the value that is stored in @settings for @key and converts it
1240 * to the enum value that it represents.
1242 * In order to use this function the type of the value must be a string
1243 * and it must be marked in the schema file as an enumerated type.
1245 * It is a programmer error to give a @key that isn't contained in the
1246 * schema for @settings or is not marked as an enumerated type.
1248 * If the value stored in the configuration database is not a valid
1249 * value for the enumerated type then this function will return the
1255 g_settings_get_enum (GSettings *settings,
1258 GSettingsKeyInfo info;
1262 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1263 g_return_val_if_fail (key != NULL, -1);
1265 g_settings_get_key_info (&info, settings, key);
1269 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1270 "associated with an enumerated type", info.key);
1271 g_settings_free_key_info (&info);
1275 value = g_settings_read_from_backend (&info);
1278 value = g_settings_get_translated_default (&info);
1281 value = g_variant_ref (info.default_value);
1283 result = g_settings_to_enum (&info, value);
1284 g_settings_free_key_info (&info);
1285 g_variant_unref (value);
1291 * g_settings_set_enum:
1292 * @settings: a #GSettings object
1293 * @key: a key, within @settings
1294 * @value: an enumerated value
1295 * @returns: %TRUE, if the set succeeds
1297 * Looks up the enumerated type nick for @value and writes it to @key,
1300 * It is a programmer error to give a @key that isn't contained in the
1301 * schema for @settings or is not marked as an enumerated type, or for
1302 * @value not to be a valid value for the named type.
1304 * After performing the write, accessing @key directly with
1305 * g_settings_get_string() will return the 'nick' associated with
1309 g_settings_set_enum (GSettings *settings,
1313 GSettingsKeyInfo info;
1317 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1318 g_return_val_if_fail (key != NULL, FALSE);
1320 g_settings_get_key_info (&info, settings, key);
1324 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1325 "associated with an enumerated type", info.key);
1329 if (!(variant = g_settings_from_enum (&info, value)))
1331 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1332 "in schema `%s'. Doing nothing.", value, info.key,
1333 info.settings->priv->schema_name);
1334 g_settings_free_key_info (&info);
1338 success = g_settings_write_to_backend (&info, variant);
1339 g_settings_free_key_info (&info);
1345 * g_settings_get_flags:
1346 * @settings: a #GSettings object
1347 * @key: the key to get the value for
1348 * @returns: the flags value
1350 * Gets the value that is stored in @settings for @key and converts it
1351 * to the flags value that it represents.
1353 * In order to use this function the type of the value must be an array
1354 * of strings and it must be marked in the schema file as an flags type.
1356 * It is a programmer error to give a @key that isn't contained in the
1357 * schema for @settings or is not marked as a flags type.
1359 * If the value stored in the configuration database is not a valid
1360 * value for the flags type then this function will return the default
1366 g_settings_get_flags (GSettings *settings,
1369 GSettingsKeyInfo info;
1373 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1374 g_return_val_if_fail (key != NULL, -1);
1376 g_settings_get_key_info (&info, settings, key);
1380 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1381 "associated with a flags type", info.key);
1382 g_settings_free_key_info (&info);
1386 value = g_settings_read_from_backend (&info);
1389 value = g_settings_get_translated_default (&info);
1392 value = g_variant_ref (info.default_value);
1394 result = g_settings_to_flags (&info, value);
1395 g_settings_free_key_info (&info);
1396 g_variant_unref (value);
1402 * g_settings_set_flags:
1403 * @settings: a #GSettings object
1404 * @key: a key, within @settings
1405 * @value: a flags value
1406 * @returns: %TRUE, if the set succeeds
1408 * Looks up the flags type nicks for the bits specified by @value, puts
1409 * them in an array of strings and writes the array to @key, withing
1412 * It is a programmer error to give a @key that isn't contained in the
1413 * schema for @settings or is not marked as a flags type, or for @value
1414 * to contain any bits that are not value for the named type.
1416 * After performing the write, accessing @key directly with
1417 * g_settings_get_strv() will return an array of 'nicks'; one for each
1421 g_settings_set_flags (GSettings *settings,
1425 GSettingsKeyInfo info;
1429 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1430 g_return_val_if_fail (key != NULL, FALSE);
1432 g_settings_get_key_info (&info, settings, key);
1436 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1437 "associated with a flags type", info.key);
1441 if (!(variant = g_settings_from_flags (&info, value)))
1443 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1444 "for key `%s' in schema `%s'. Doing nothing.",
1445 value, info.key, info.settings->priv->schema_name);
1446 g_settings_free_key_info (&info);
1450 success = g_settings_write_to_backend (&info, variant);
1451 g_settings_free_key_info (&info);
1457 * g_settings_set_value:
1458 * @settings: a #GSettings object
1459 * @key: the name of the key to set
1460 * @value: a #GVariant of the correct type
1461 * @returns: %TRUE if setting the key succeeded,
1462 * %FALSE if the key was not writable
1464 * Sets @key in @settings to @value.
1466 * It is a programmer error to give a @key that isn't contained in the
1467 * schema for @settings or for @value to have the incorrect type, per
1470 * If @value is floating then this function consumes the reference.
1475 g_settings_set_value (GSettings *settings,
1479 GSettingsKeyInfo info;
1481 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1482 g_return_val_if_fail (key != NULL, FALSE);
1484 g_settings_get_key_info (&info, settings, key);
1485 g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1486 g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1487 g_settings_free_key_info (&info);
1489 return g_settings_write_to_backend (&info, value);
1494 * @settings: a #GSettings object
1495 * @key: the key to get the value for
1496 * @format: a #GVariant format string
1497 * @...: arguments as per @format
1499 * Gets the value that is stored at @key in @settings.
1501 * A convenience function that combines g_settings_get_value() with
1504 * It is a programmer error to give a @key that isn't contained in the
1505 * schema for @settings or for the #GVariantType of @format to mismatch
1506 * the type given in the schema.
1511 g_settings_get (GSettings *settings,
1513 const gchar *format,
1519 value = g_settings_get_value (settings, key);
1521 va_start (ap, format);
1522 g_variant_get_va (value, format, NULL, &ap);
1525 g_variant_unref (value);
1530 * @settings: a #GSettings object
1531 * @key: the name of the key to set
1532 * @format: a #GVariant format string
1533 * @...: arguments as per @format
1534 * @returns: %TRUE if setting the key succeeded,
1535 * %FALSE if the key was not writable
1537 * Sets @key in @settings to @value.
1539 * A convenience function that combines g_settings_set_value() with
1542 * It is a programmer error to give a @key that isn't contained in the
1543 * schema for @settings or for the #GVariantType of @format to mismatch
1544 * the type given in the schema.
1549 g_settings_set (GSettings *settings,
1551 const gchar *format,
1557 va_start (ap, format);
1558 value = g_variant_new_va (format, NULL, &ap);
1561 return g_settings_set_value (settings, key, value);
1565 * g_settings_get_mapped:
1566 * @settings: a #GSettings object
1567 * @key: the key to get the value for
1568 * @mapping: the function to map the value in the settings database to
1569 * the value used by the application
1570 * @user_data: user data for @mapping
1572 * Gets the value that is stored at @key in @settings, subject to
1573 * application-level validation/mapping.
1575 * You should use this function when the application needs to perform
1576 * some processing on the value of the key (for example, parsing). The
1577 * @mapping function performs that processing. If the function
1578 * indicates that the processing was unsuccessful (due to a parse error,
1579 * for example) then the mapping is tried again with another value.
1581 * This allows a robust 'fall back to defaults' behaviour to be
1582 * implemented somewhat automatically.
1584 * The first value that is tried is the user's setting for the key. If
1585 * the mapping function fails to map this value, other values may be
1586 * tried in an unspecified order (system or site defaults, translated
1587 * schema default values, untranslated schema default values, etc).
1589 * If the mapping function fails for all possible values, one additional
1590 * attempt is made: the mapping function is called with a %NULL value.
1591 * If the mapping function still indicates failure at this point then
1592 * the application will be aborted.
1594 * The result parameter for the @mapping function is pointed to a
1595 * #gpointer which is initially set to %NULL. The same pointer is given
1596 * to each invocation of @mapping. The final value of that #gpointer is
1597 * what is returned by this function. %NULL is valid; it is returned
1598 * just as any other value would be.
1601 g_settings_get_mapped (GSettings *settings,
1603 GSettingsGetMapping mapping,
1606 gpointer result = NULL;
1607 GSettingsKeyInfo info;
1611 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1612 g_return_val_if_fail (key != NULL, NULL);
1613 g_return_val_if_fail (mapping != NULL, NULL);
1615 g_settings_get_key_info (&info, settings, key);
1617 if ((value = g_settings_read_from_backend (&info)))
1619 okay = mapping (value, &result, user_data);
1620 g_variant_unref (value);
1621 if (okay) goto okay;
1624 if ((value = g_settings_get_translated_default (&info)))
1626 okay = mapping (value, &result, user_data);
1627 g_variant_unref (value);
1628 if (okay) goto okay;
1631 if (mapping (info.default_value, &result, user_data))
1634 if (!mapping (NULL, &result, user_data))
1635 g_error ("The mapping function given to g_settings_get_mapped() for key "
1636 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1637 key, settings->priv->schema_name);
1640 g_settings_free_key_info (&info);
1645 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1647 * g_settings_get_string:
1648 * @settings: a #GSettings object
1649 * @key: the key to get the value for
1650 * @returns: a newly-allocated string
1652 * Gets the value that is stored at @key in @settings.
1654 * A convenience variant of g_settings_get() for strings.
1656 * It is a programmer error to give a @key that isn't specified as
1657 * having a string type in the schema for @settings.
1662 g_settings_get_string (GSettings *settings,
1668 value = g_settings_get_value (settings, key);
1669 result = g_variant_dup_string (value, NULL);
1670 g_variant_unref (value);
1676 * g_settings_set_string:
1677 * @settings: a #GSettings object
1678 * @key: the name of the key to set
1679 * @value: the value to set it to
1680 * @returns: %TRUE if setting the key succeeded,
1681 * %FALSE if the key was not writable
1683 * Sets @key in @settings to @value.
1685 * A convenience variant of g_settings_set() for strings.
1687 * It is a programmer error to give a @key that isn't specified as
1688 * having a string type in the schema for @settings.
1693 g_settings_set_string (GSettings *settings,
1697 return g_settings_set_value (settings, key, g_variant_new_string (value));
1701 * g_settings_get_int:
1702 * @settings: a #GSettings object
1703 * @key: the key to get the value for
1704 * @returns: an integer
1706 * Gets the value that is stored at @key in @settings.
1708 * A convenience variant of g_settings_get() for 32-bit integers.
1710 * It is a programmer error to give a @key that isn't specified as
1711 * having a int32 type in the schema for @settings.
1716 g_settings_get_int (GSettings *settings,
1722 value = g_settings_get_value (settings, key);
1723 result = g_variant_get_int32 (value);
1724 g_variant_unref (value);
1730 * g_settings_set_int:
1731 * @settings: a #GSettings object
1732 * @key: the name of the key to set
1733 * @value: the value to set it to
1734 * @returns: %TRUE if setting the key succeeded,
1735 * %FALSE if the key was not writable
1737 * Sets @key in @settings to @value.
1739 * A convenience variant of g_settings_set() for 32-bit integers.
1741 * It is a programmer error to give a @key that isn't specified as
1742 * having a int32 type in the schema for @settings.
1747 g_settings_set_int (GSettings *settings,
1751 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1755 * g_settings_get_double:
1756 * @settings: a #GSettings object
1757 * @key: the key to get the value for
1758 * @returns: a double
1760 * Gets the value that is stored at @key in @settings.
1762 * A convenience variant of g_settings_get() for doubles.
1764 * It is a programmer error to give a @key that isn't specified as
1765 * having a 'double' type in the schema for @settings.
1770 g_settings_get_double (GSettings *settings,
1776 value = g_settings_get_value (settings, key);
1777 result = g_variant_get_double (value);
1778 g_variant_unref (value);
1784 * g_settings_set_double:
1785 * @settings: a #GSettings object
1786 * @key: the name of the key to set
1787 * @value: the value to set it to
1788 * @returns: %TRUE if setting the key succeeded,
1789 * %FALSE if the key was not writable
1791 * Sets @key in @settings to @value.
1793 * A convenience variant of g_settings_set() for doubles.
1795 * It is a programmer error to give a @key that isn't specified as
1796 * having a 'double' type in the schema for @settings.
1801 g_settings_set_double (GSettings *settings,
1805 return g_settings_set_value (settings, key, g_variant_new_double (value));
1809 * g_settings_get_boolean:
1810 * @settings: a #GSettings object
1811 * @key: the key to get the value for
1812 * @returns: a boolean
1814 * Gets the value that is stored at @key in @settings.
1816 * A convenience variant of g_settings_get() for booleans.
1818 * It is a programmer error to give a @key that isn't specified as
1819 * having a boolean type in the schema for @settings.
1824 g_settings_get_boolean (GSettings *settings,
1830 value = g_settings_get_value (settings, key);
1831 result = g_variant_get_boolean (value);
1832 g_variant_unref (value);
1838 * g_settings_set_boolean:
1839 * @settings: a #GSettings object
1840 * @key: the name of the key to set
1841 * @value: the value to set it to
1842 * @returns: %TRUE if setting the key succeeded,
1843 * %FALSE if the key was not writable
1845 * Sets @key in @settings to @value.
1847 * A convenience variant of g_settings_set() for booleans.
1849 * It is a programmer error to give a @key that isn't specified as
1850 * having a boolean type in the schema for @settings.
1855 g_settings_set_boolean (GSettings *settings,
1859 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1863 * g_settings_get_strv:
1864 * @settings: a #GSettings object
1865 * @key: the key to get the value for
1866 * @returns: a newly-allocated, %NULL-terminated array of strings
1868 * Gets the value that is stored at @key in @settings.
1870 * A convenience variant of g_settings_get() for string arrays.
1872 * It is a programmer error to give a @key that isn't specified as
1873 * having an array of strings type in the schema for @settings.
1878 g_settings_get_strv (GSettings *settings,
1884 value = g_settings_get_value (settings, key);
1885 result = g_variant_dup_strv (value, NULL);
1886 g_variant_unref (value);
1892 * g_settings_set_strv:
1893 * @settings: a #GSettings object
1894 * @key: the name of the key to set
1895 * @value: (allow-none): the value to set it to, or %NULL
1896 * @returns: %TRUE if setting the key succeeded,
1897 * %FALSE if the key was not writable
1899 * Sets @key in @settings to @value.
1901 * A convenience variant of g_settings_set() for string arrays. If
1902 * @value is %NULL, then @key is set to be the empty array.
1904 * It is a programmer error to give a @key that isn't specified as
1905 * having an array of strings type in the schema for @settings.
1910 g_settings_set_strv (GSettings *settings,
1912 const gchar * const *value)
1917 array = g_variant_new_strv (value, -1);
1919 array = g_variant_new_strv (NULL, 0);
1921 return g_settings_set_value (settings, key, array);
1924 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1927 * @settings: a #GSettings object
1929 * Changes the #GSettings object into 'delay-apply' mode. In this
1930 * mode, changes to @settings are not immediately propagated to the
1931 * backend, but kept locally until g_settings_apply() is called.
1936 g_settings_delay (GSettings *settings)
1938 g_return_if_fail (G_IS_SETTINGS (settings));
1940 if (settings->priv->delayed)
1943 settings->priv->delayed =
1944 g_delayed_settings_backend_new (settings->priv->backend,
1946 settings->priv->main_context);
1947 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1948 g_object_unref (settings->priv->backend);
1950 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1951 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
1952 settings->priv->main_context,
1953 settings_backend_changed,
1954 settings_backend_path_changed,
1955 settings_backend_keys_changed,
1956 settings_backend_writable_changed,
1957 settings_backend_path_writable_changed);
1962 * @settings: a #GSettings instance
1964 * Applies any changes that have been made to the settings. This
1965 * function does nothing unless @settings is in 'delay-apply' mode;
1966 * see g_settings_delay(). In the normal case settings are always
1967 * applied immediately.
1970 g_settings_apply (GSettings *settings)
1972 if (settings->priv->delayed)
1974 GDelayedSettingsBackend *delayed;
1976 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1977 g_delayed_settings_backend_apply (delayed);
1982 * g_settings_revert:
1983 * @settings: a #GSettings instance
1985 * Reverts all non-applied changes to the settings. This function
1986 * does nothing unless @settings is in 'delay-apply' mode; see
1987 * g_settings_delay(). In the normal case settings are always applied
1990 * Change notifications will be emitted for affected keys.
1993 g_settings_revert (GSettings *settings)
1995 if (settings->priv->delayed)
1997 GDelayedSettingsBackend *delayed;
1999 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2000 g_delayed_settings_backend_revert (delayed);
2005 * g_settings_get_has_unapplied:
2006 * @settings: a #GSettings object
2007 * @returns: %TRUE if @settings has unapplied changes
2009 * Returns whether the #GSettings object has any unapplied
2010 * changes. This can only be the case if it is in 'delayed-apply' mode.
2015 g_settings_get_has_unapplied (GSettings *settings)
2017 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2019 return settings->priv->delayed &&
2020 g_delayed_settings_backend_get_has_unapplied (
2021 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2024 /* Extra API (sync, get_child, is_writable, list_items) {{{1 */
2027 * @context: the context to sync, or %NULL
2029 * Ensures that all pending operations for the given context are
2032 * Writes made to a #GSettings are handled asynchronously. For this
2033 * reason, it is very unlikely that the changes have it to disk by the
2034 * time g_settings_set() returns.
2036 * This call will block until all of the writes have made it to the
2037 * backend. Since the mainloop is not running, no change notifications
2038 * will be dispatched during this call (but some may be queued by the
2039 * time the call is done).
2042 g_settings_sync (void)
2044 g_settings_backend_sync_default ();
2048 * g_settings_is_writable:
2049 * @settings: a #GSettings object
2050 * @name: the name of a key
2051 * @returns: %TRUE if the key @name is writable
2053 * Finds out if a key can be written or not
2058 g_settings_is_writable (GSettings *settings,
2064 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2066 path = g_strconcat (settings->priv->path, name, NULL);
2067 writable = g_settings_backend_get_writable (settings->priv->backend, path);
2074 * g_settings_get_child:
2075 * @settings: a #GSettings object
2076 * @name: the name of the 'child' schema
2077 * @returns: a 'child' settings object
2079 * Creates a 'child' settings object which has a base path of
2080 * <replaceable>base-path</replaceable>/@name", where
2081 * <replaceable>base-path</replaceable> is the base path of @settings.
2083 * The schema for the child settings object must have been declared
2084 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2089 g_settings_get_child (GSettings *settings,
2092 const gchar *child_schema;
2097 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2099 child_name = g_strconcat (name, "/", NULL);
2100 child_schema = g_settings_schema_get_string (settings->priv->schema,
2102 if (child_schema == NULL)
2103 g_error ("Schema '%s' has no child '%s'",
2104 settings->priv->schema_name, name);
2106 child_path = g_strconcat (settings->priv->path, child_name, NULL);
2107 child = g_object_new (G_TYPE_SETTINGS,
2108 "schema", child_schema,
2111 g_free (child_path);
2112 g_free (child_name);
2118 * g_settings_list_items:
2119 * @settings: a #GSettings object
2120 * Returns: a list of the keys on @settings
2122 * Introspects the list of keys and children on @settings.
2124 * The list that is returned is a mix of the keys and children. The
2125 * names of the children are suffixed with '/'. The names of the keys
2128 * You should probably not be calling this function from "normal" code
2129 * (since you should already know what keys are in your schema). This
2130 * function is intended for introspection reasons.
2132 * You should free the return value with g_free() when you are done with
2136 g_settings_list_items (GSettings *settings)
2143 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2144 strv = g_new (const gchar *, n_keys + 1);
2145 for (i = 0; i < n_keys; i++)
2146 strv[i] = g_quark_to_string (keys[i]);
2155 GSettingsKeyInfo info;
2158 GSettingsBindGetMapping get_mapping;
2159 GSettingsBindSetMapping set_mapping;
2161 GDestroyNotify destroy;
2163 guint writable_handler_id;
2164 guint property_handler_id;
2165 const GParamSpec *property;
2166 guint key_handler_id;
2168 /* prevent recursion */
2173 g_settings_binding_free (gpointer data)
2175 GSettingsBinding *binding = data;
2177 g_assert (!binding->running);
2179 if (binding->writable_handler_id)
2180 g_signal_handler_disconnect (binding->info.settings,
2181 binding->writable_handler_id);
2183 if (binding->key_handler_id)
2184 g_signal_handler_disconnect (binding->info.settings,
2185 binding->key_handler_id);
2187 if (g_signal_handler_is_connected (binding->object,
2188 binding->property_handler_id))
2189 g_signal_handler_disconnect (binding->object,
2190 binding->property_handler_id);
2192 g_settings_free_key_info (&binding->info);
2194 if (binding->destroy)
2195 binding->destroy (binding->user_data);
2197 g_slice_free (GSettingsBinding, binding);
2201 g_settings_binding_quark (const char *property)
2206 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2207 quark = g_quark_from_string (tmp);
2214 g_settings_binding_key_changed (GSettings *settings,
2218 GSettingsBinding *binding = user_data;
2219 GValue value = { 0, };
2222 g_assert (settings == binding->info.settings);
2223 g_assert (key == binding->info.key);
2225 if (binding->running)
2228 binding->running = TRUE;
2230 g_value_init (&value, binding->property->value_type);
2232 variant = g_settings_read_from_backend (&binding->info);
2233 if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2235 /* silently ignore errors in the user's config database */
2236 g_variant_unref (variant);
2240 if (variant == NULL)
2242 variant = g_settings_get_translated_default (&binding->info);
2244 !binding->get_mapping (&value, variant, binding->user_data))
2246 /* flag translation errors with a warning */
2247 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2248 "was rejected by the binding mapping function",
2249 binding->info.unparsed, binding->info.key,
2250 binding->info.settings->priv->schema_name);
2251 g_variant_unref (variant);
2256 if (variant == NULL)
2258 variant = g_variant_ref (binding->info.default_value);
2259 if (!binding->get_mapping (&value, variant, binding->user_data))
2260 g_error ("The schema default value for key `%s' in schema `%s' "
2261 "was rejected by the binding mapping function.",
2263 binding->info.settings->priv->schema_name);
2266 g_object_set_property (binding->object, binding->property->name, &value);
2267 g_variant_unref (variant);
2268 g_value_unset (&value);
2270 binding->running = FALSE;
2274 g_settings_binding_property_changed (GObject *object,
2275 const GParamSpec *pspec,
2278 GSettingsBinding *binding = user_data;
2279 GValue value = { 0, };
2282 g_assert (object == binding->object);
2283 g_assert (pspec == binding->property);
2285 if (binding->running)
2288 binding->running = TRUE;
2290 g_value_init (&value, pspec->value_type);
2291 g_object_get_property (object, pspec->name, &value);
2292 if ((variant = binding->set_mapping (&value, binding->info.type,
2293 binding->user_data)))
2295 if (g_variant_is_floating (variant))
2296 g_variant_ref_sink (variant);
2298 if (!g_settings_type_check (&binding->info, variant))
2300 g_critical ("binding mapping function for key `%s' returned "
2301 "GVariant of type `%s' when type `%s' was requested",
2302 binding->info.key, g_variant_get_type_string (variant),
2303 g_variant_type_dup_string (binding->info.type));
2307 if (!g_settings_range_check (&binding->info, variant))
2309 g_critical ("GObject property `%s' on a `%s' object is out of "
2310 "schema-specified range for key `%s' of `%s': %s",
2311 binding->property->name,
2312 g_type_name (binding->property->owner_type),
2314 binding->info.settings->priv->schema_name,
2315 g_variant_print (variant, TRUE));
2319 g_settings_write_to_backend (&binding->info, variant);
2320 g_variant_unref (variant);
2322 g_value_unset (&value);
2324 binding->running = FALSE;
2329 * @settings: a #GSettings object
2330 * @key: the key to bind
2331 * @object: a #GObject
2332 * @property: the name of the property to bind
2333 * @flags: flags for the binding
2335 * Create a binding between the @key in the @settings object
2336 * and the property @property of @object.
2338 * The binding uses the default GIO mapping functions to map
2339 * between the settings and property values. These functions
2340 * handle booleans, numeric types and string types in a
2341 * straightforward way. Use g_settings_bind_with_mapping() if
2342 * you need a custom mapping, or map between types that are not
2343 * supported by the default mapping functions.
2345 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2346 * function also establishes a binding between the writability of
2347 * @key and the "sensitive" property of @object (if @object has
2348 * a boolean property by that name). See g_settings_bind_writable()
2349 * for more details about writable bindings.
2351 * Note that the lifecycle of the binding is tied to the object,
2352 * and that you can have only one binding per object property.
2353 * If you bind the same property twice on the same object, the second
2354 * binding overrides the first one.
2359 g_settings_bind (GSettings *settings,
2362 const gchar *property,
2363 GSettingsBindFlags flags)
2365 g_settings_bind_with_mapping (settings, key, object, property,
2366 flags, NULL, NULL, NULL, NULL);
2370 * g_settings_bind_with_mapping:
2371 * @settings: a #GSettings object
2372 * @key: the key to bind
2373 * @object: a #GObject
2374 * @property: the name of the property to bind
2375 * @flags: flags for the binding
2376 * @get_mapping: a function that gets called to convert values
2377 * from @settings to @object, or %NULL to use the default GIO mapping
2378 * @set_mapping: a function that gets called to convert values
2379 * from @object to @settings, or %NULL to use the default GIO mapping
2380 * @user_data: data that gets passed to @get_mapping and @set_mapping
2381 * @destroy: #GDestroyNotify function for @user_data
2383 * Create a binding between the @key in the @settings object
2384 * and the property @property of @object.
2386 * The binding uses the provided mapping functions to map between
2387 * settings and property values.
2389 * Note that the lifecycle of the binding is tied to the object,
2390 * and that you can have only one binding per object property.
2391 * If you bind the same property twice on the same object, the second
2392 * binding overrides the first one.
2397 g_settings_bind_with_mapping (GSettings *settings,
2400 const gchar *property,
2401 GSettingsBindFlags flags,
2402 GSettingsBindGetMapping get_mapping,
2403 GSettingsBindSetMapping set_mapping,
2405 GDestroyNotify destroy)
2407 GSettingsBinding *binding;
2408 GObjectClass *objectclass;
2409 gchar *detailed_signal;
2410 GQuark binding_quark;
2412 g_return_if_fail (G_IS_SETTINGS (settings));
2414 objectclass = G_OBJECT_GET_CLASS (object);
2416 binding = g_slice_new0 (GSettingsBinding);
2417 g_settings_get_key_info (&binding->info, settings, key);
2418 binding->object = object;
2419 binding->property = g_object_class_find_property (objectclass, property);
2420 binding->user_data = user_data;
2421 binding->destroy = destroy;
2422 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2423 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2425 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2426 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2428 if (binding->property == NULL)
2430 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2431 property, G_OBJECT_TYPE_NAME (object));
2435 if ((flags & G_SETTINGS_BIND_GET) &&
2436 (binding->property->flags & G_PARAM_WRITABLE) == 0)
2438 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2439 "writable", property, G_OBJECT_TYPE_NAME (object));
2442 if ((flags & G_SETTINGS_BIND_SET) &&
2443 (binding->property->flags & G_PARAM_READABLE) == 0)
2445 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2446 "readable", property, G_OBJECT_TYPE_NAME (object));
2450 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2451 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2452 !g_settings_mapping_is_compatible (binding->property->value_type,
2453 binding->info.type))
2455 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2456 "'%s' which is not compatible with type '%s' of key '%s' "
2457 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2458 g_type_name (binding->property->value_type),
2459 g_variant_type_dup_string (binding->info.type), key,
2460 settings->priv->schema_name);
2464 if ((flags & G_SETTINGS_BIND_SET) &&
2465 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2467 GParamSpec *sensitive;
2469 sensitive = g_object_class_find_property (objectclass, "sensitive");
2471 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2472 (sensitive->flags & G_PARAM_WRITABLE))
2473 g_settings_bind_writable (settings, binding->info.key,
2474 object, "sensitive", FALSE);
2477 if (flags & G_SETTINGS_BIND_SET)
2479 detailed_signal = g_strdup_printf ("notify::%s", property);
2480 binding->property_handler_id =
2481 g_signal_connect (object, detailed_signal,
2482 G_CALLBACK (g_settings_binding_property_changed),
2484 g_free (detailed_signal);
2486 if (~flags & G_SETTINGS_BIND_GET)
2487 g_settings_binding_property_changed (object,
2492 if (flags & G_SETTINGS_BIND_GET)
2494 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2496 detailed_signal = g_strdup_printf ("changed::%s", key);
2497 binding->key_handler_id =
2498 g_signal_connect (settings, detailed_signal,
2499 G_CALLBACK (g_settings_binding_key_changed),
2501 g_free (detailed_signal);
2504 g_settings_binding_key_changed (settings, binding->info.key, binding);
2507 binding_quark = g_settings_binding_quark (property);
2508 g_object_set_qdata_full (object, binding_quark,
2509 binding, g_settings_binding_free);
2512 /* Writability binding {{{1 */
2515 GSettings *settings;
2518 const gchar *property;
2521 } GSettingsWritableBinding;
2524 g_settings_writable_binding_free (gpointer data)
2526 GSettingsWritableBinding *binding = data;
2528 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2529 g_object_unref (binding->settings);
2530 g_slice_free (GSettingsWritableBinding, binding);
2534 g_settings_binding_writable_changed (GSettings *settings,
2538 GSettingsWritableBinding *binding = user_data;
2541 g_assert (settings == binding->settings);
2542 g_assert (key == binding->key);
2544 writable = g_settings_is_writable (settings, key);
2546 if (binding->inverted)
2547 writable = !writable;
2549 g_object_set (binding->object, binding->property, writable, NULL);
2553 * g_settings_bind_writable:
2554 * @settings: a #GSettings object
2555 * @key: the key to bind
2556 * @object: a #GObject
2557 * @property: the name of a boolean property to bind
2558 * @inverted: whether to 'invert' the value
2560 * Create a binding between the writability of @key in the
2561 * @settings object and the property @property of @object.
2562 * The property must be boolean; "sensitive" or "visible"
2563 * properties of widgets are the most likely candidates.
2565 * Writable bindings are always uni-directional; changes of the
2566 * writability of the setting will be propagated to the object
2567 * property, not the other way.
2569 * When the @inverted argument is %TRUE, the binding inverts the
2570 * value as it passes from the setting to the object, i.e. @property
2571 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2574 * Note that the lifecycle of the binding is tied to the object,
2575 * and that you can have only one binding per object property.
2576 * If you bind the same property twice on the same object, the second
2577 * binding overrides the first one.
2582 g_settings_bind_writable (GSettings *settings,
2585 const gchar *property,
2588 GSettingsWritableBinding *binding;
2589 gchar *detailed_signal;
2592 g_return_if_fail (G_IS_SETTINGS (settings));
2594 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2597 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2598 property, G_OBJECT_TYPE_NAME (object));
2601 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2603 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2604 property, G_OBJECT_TYPE_NAME (object));
2608 binding = g_slice_new (GSettingsWritableBinding);
2609 binding->settings = g_object_ref (settings);
2610 binding->object = object;
2611 binding->key = g_intern_string (key);
2612 binding->property = g_intern_string (property);
2613 binding->inverted = inverted;
2615 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2616 binding->handler_id =
2617 g_signal_connect (settings, detailed_signal,
2618 G_CALLBACK (g_settings_binding_writable_changed),
2620 g_free (detailed_signal);
2622 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2623 binding, g_settings_writable_binding_free);
2625 g_settings_binding_writable_changed (settings, binding->key, binding);
2629 * g_settings_unbind:
2630 * @object: the object
2631 * @property: the property whose binding is removed
2633 * Removes an existing binding for @property on @object.
2635 * Note that bindings are automatically removed when the
2636 * object is finalized, so it is rarely necessary to call this
2642 g_settings_unbind (gpointer object,
2643 const gchar *property)
2645 GQuark binding_quark;
2647 binding_quark = g_settings_binding_quark (property);
2648 g_object_set_qdata (object, binding_quark, NULL);
2653 /* vim:set foldmethod=marker: */