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>
27 #include "gsettings.h"
29 #include "gdelayedsettingsbackend.h"
30 #include "gsettingsbackendinternal.h"
31 #include "gsettings-mapping.h"
32 #include "gio-marshal.h"
33 #include "gsettingsschema.h"
41 * @short_description: a high-level API for application settings
43 * The #GSettings class provides a convenient API for storing and retrieving
44 * application settings.
46 * When creating a GSettings instance, you have to specify a schema
47 * that describes the keys in your settings and their types and default
48 * values, as well as some other information.
50 * Normally, a schema has as fixed path that determines where the settings
51 * are stored in the conceptual global tree of settings. However, schemas
52 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
53 * useful e.g. when the schema describes an 'account', and you want to be
54 * able to store a arbitrary number of accounts.
56 * Unlike other configuration systems (like GConf), GSettings does not
57 * restrict keys to basic types like strings and numbers. GSettings stores
58 * values as #GVariant, and allows any #GVariantType for keys. Key names
59 * are restricted to lowercase characters, numbers and '-'. Furthermore,
60 * the names must begin with a lowercase character, must not end
61 * with a '-', and must not contain consecutive dashes. Key names can
62 * be up to 32 characters long.
64 * Similar to GConf, the default values in GSettings schemas can be
65 * localized, but the localized values are stored in gettext catalogs
66 * and looked up with the domain that is specified in the
67 * <tag class="attribute">gettext-domain</tag> attribute of the
68 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
69 * elements and the category that is specified in the l10n attribute of the
70 * <tag class="starttag">key</tag> element.
72 * GSettings uses schemas in a compact binary form that is created
73 * by the gschema-compile utility. The input is a schema description in
74 * an XML format that can be described by the following DTD:
76 * <!ELEMENT schemalist (schema*) >
77 * <!ATTLIST schemalist gettext-domain #IMPLIED >
79 * <!ELEMENT schema (key|child)* >
80 * <!ATTLIST schema id CDATA #REQUIRED
82 * gettext-domain CDATA #IMPLIED >
84 * <!ELEMENT key (default|summary?|description?|range?|choices?) >
85 * <!-- name can only contain lowercase letters, numbers and '-' -->
86 * <!-- type must be a GVariant type string -->
87 * <!ATTLIST key name CDATA #REQUIRED
88 * type CDATA #REQUIRED >
90 * <!-- the default value is specified a a serialized GVariant,
91 * i.e. you have to include the quotes when specifying a string -->
92 * <!ELEMENT default (#PCDATA) >
93 * <!-- the presence of the l10n attribute marks a default value for
94 * translation, its value is the gettext category to use -->
95 * <!-- if context is present, it specifies msgctxt to use -->
96 * <!ATTLIST default l10n (messages|time) #IMPLIED
97 * context CDATA #IMPLIED >
99 * <!ELEMENT summary (#PCDATA) >
100 * <!ELEMENT description (#PCDATA) >
102 * <!ELEMENT range (min,max) >
103 * <!ELEMENT min (#PCDATA) >
104 * <!ELEMENT max (#PCDATA) >
106 * <!ELEMENT choices (choice+) >
107 * <!ELEMENT choice (alias?) >
108 * <!ATTLIST choice value CDATA #REQUIRED >
109 * <!ELEMENT choice (alias?) >
110 * <!ELEMENT alias EMPTY >
111 * <!ATTLIST alias value CDATA #REQUIRED >
113 * <!ELEMENT child EMPTY >
114 * <!ATTLIST child name CDATA #REQUIRED
115 * schema CDATA #REQUIRED >
119 * At runtime, schemas are identified by their id (as specified
120 * in the <tag class="attribute">id</tag> attribute of the
121 * <tag class="starttag">schema</tag> element). The
122 * convention for schema ids is to use a dotted name, similar in
123 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
126 * <title>Binding</title>
128 * A very convenient feature of GSettings lets you bind #GObject properties
129 * directly to settings, using g_settings_bind(). Once a GObject property
130 * has been bound to a setting, changes on either side are automatically
131 * propagated to the other side. GSettings handles details like
132 * mapping between GObject and GVariant types, and preventing infinite
136 * This makes it very easy to hook up a preferences dialog to the
137 * underlying settings. To make this even more convenient, GSettings
138 * looks for a boolean property with the name "sensitivity" and
139 * automatically binds it to the writability of the bound setting.
140 * If this 'magic' gets in the way, it can be suppressed with the
141 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
146 struct _GSettingsPrivate
148 GSettingsBackend *backend;
149 GSettingsSchema *schema;
154 GDelayedSettingsBackend *delayed;
169 SIGNAL_WRITABLE_CHANGE_EVENT,
170 SIGNAL_WRITABLE_CHANGED,
176 static guint g_settings_signals[N_SIGNALS];
178 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
181 g_settings_real_change_event (GSettings *settings,
188 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
190 for (i = 0; i < n_keys; i++)
191 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
192 keys[i], g_quark_to_string (keys[i]));
198 g_settings_real_writable_change_event (GSettings *settings,
201 const GQuark *keys = &key;
206 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
208 for (i = 0; i < n_keys; i++)
210 const gchar *string = g_quark_to_string (keys[i]);
212 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
214 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
222 settings_backend_changed (GSettingsBackend *backend,
227 GSettings *settings = G_SETTINGS (user_data);
228 gboolean ignore_this;
231 g_assert (settings->priv->backend == backend);
233 for (i = 0; key[i] == settings->priv->path[i]; i++);
235 if (settings->priv->path[i] == '\0' &&
236 g_settings_schema_has_key (settings->priv->schema, key + i))
240 quark = g_quark_from_string (key + i);
241 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
242 0, &quark, 1, &ignore_this);
247 settings_backend_path_changed (GSettingsBackend *backend,
252 GSettings *settings = G_SETTINGS (user_data);
253 gboolean ignore_this;
255 g_assert (settings->priv->backend == backend);
257 if (g_str_has_prefix (settings->priv->path, path))
258 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
259 0, NULL, 0, &ignore_this);
263 settings_backend_keys_changed (GSettingsBackend *backend,
265 const gchar * const *items,
269 GSettings *settings = G_SETTINGS (user_data);
270 gboolean ignore_this;
273 g_assert (settings->priv->backend == backend);
275 for (i = 0; settings->priv->path[i] &&
276 settings->priv->path[i] == path[i]; i++);
283 for (j = 0; items[j]; j++)
285 const gchar *item = items[j];
288 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
290 if (settings->priv->path[i + k] == '\0' &&
291 g_settings_schema_has_key (settings->priv->schema, item + k))
292 quarks[l++] = g_quark_from_string (item + k);
294 /* "256 quarks ought to be enough for anybody!"
295 * If this bites you, I'm sorry. Please file a bug.
301 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
302 0, quarks, l, &ignore_this);
307 settings_backend_writable_changed (GSettingsBackend *backend,
311 GSettings *settings = G_SETTINGS (user_data);
312 gboolean ignore_this;
315 g_assert (settings->priv->backend == backend);
317 for (i = 0; key[i] == settings->priv->path[i]; i++);
319 if (settings->priv->path[i] == '\0' &&
320 g_settings_schema_has_key (settings->priv->schema, key + i))
321 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
322 0, g_quark_from_string (key + i), &ignore_this);
326 settings_backend_path_writable_changed (GSettingsBackend *backend,
330 GSettings *settings = G_SETTINGS (user_data);
331 gboolean ignore_this;
333 g_assert (settings->priv->backend == backend);
335 if (g_str_has_prefix (settings->priv->path, path))
336 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
337 0, (GQuark) 0, &ignore_this);
341 g_settings_constructed (GObject *object)
343 GSettings *settings = G_SETTINGS (object);
344 const gchar *schema_path;
346 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
347 schema_path = g_settings_schema_get_path (settings->priv->schema);
349 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
350 g_error ("settings object created with schema '%s' and path '%s', but "
351 "path '%s' is specified by schema",
352 settings->priv->schema_name, settings->priv->path, schema_path);
354 if (settings->priv->path == NULL)
356 if (schema_path == NULL)
357 g_error ("attempting to create schema '%s' without a path",
358 settings->priv->schema_name);
360 settings->priv->path = g_strdup (schema_path);
363 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
364 g_settings_backend_watch (settings->priv->backend,
365 settings_backend_changed,
366 settings_backend_path_changed,
367 settings_backend_keys_changed,
368 settings_backend_writable_changed,
369 settings_backend_path_writable_changed,
371 g_settings_backend_subscribe (settings->priv->backend,
372 settings->priv->path);
376 g_settings_init (GSettings *settings)
378 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
385 * @settings: a #GSettings object
387 * Changes the #GSettings object into 'delay-apply' mode. In this
388 * mode, changes to @settings are not immediately propagated to the
389 * backend, but kept locally until g_settings_apply() is called.
394 g_settings_delay (GSettings *settings)
396 g_return_if_fail (G_IS_SETTINGS (settings));
398 if (settings->priv->delayed)
401 settings->priv->delayed =
402 g_delayed_settings_backend_new (settings->priv->backend, settings);
403 g_settings_backend_unwatch (settings->priv->backend, settings);
404 g_object_unref (settings->priv->backend);
406 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
407 g_settings_backend_watch (settings->priv->backend,
408 settings_backend_changed,
409 settings_backend_path_changed,
410 settings_backend_keys_changed,
411 settings_backend_writable_changed,
412 settings_backend_path_writable_changed,
418 * @settings: a #GSettings instance
420 * Applies any changes that have been made to the settings. This
421 * function does nothing unless @settings is in 'delay-apply' mode;
422 * see g_settings_set_delay_apply(). In the normal case settings are
423 * always applied immediately.
426 g_settings_apply (GSettings *settings)
428 if (settings->priv->delayed)
430 GDelayedSettingsBackend *delayed;
432 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
433 g_delayed_settings_backend_apply (delayed);
439 * @settings: a #GSettings instance
441 * Reverts all non-applied changes to the settings. This function
442 * does nothing unless @settings is in 'delay-apply' mode; see
443 * g_settings_set_delay_apply(). In the normal case settings are
444 * always applied immediately.
446 * Change notifications will be emitted for affected keys.
449 g_settings_revert (GSettings *settings)
451 if (settings->priv->delayed)
453 GDelayedSettingsBackend *delayed;
455 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
456 g_delayed_settings_backend_revert (delayed);
461 g_settings_set_property (GObject *object,
466 GSettings *settings = G_SETTINGS (object);
471 g_assert (settings->priv->schema_name == NULL);
472 settings->priv->schema_name = g_value_dup_string (value);
476 settings->priv->path = g_value_dup_string (value);
480 settings->priv->context = g_value_dup_string (value);
484 g_assert_not_reached ();
489 * g_settings_get_has_unapplied:
490 * @settings: a #GSettings object
491 * @returns: %TRUE if @settings has unapplied changes
493 * Returns whether the #GSettings object has any unapplied
494 * changes. This can only be the case if it is in 'delayed-apply' mode.
499 g_settings_get_has_unapplied (GSettings *settings)
501 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
503 return settings->priv->delayed &&
504 g_delayed_settings_backend_get_has_unapplied (
505 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
509 g_settings_get_property (GObject *object,
514 GSettings *settings = G_SETTINGS (object);
519 g_value_set_object (value, settings->priv->schema);
522 case PROP_HAS_UNAPPLIED:
523 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
527 g_assert_not_reached ();
532 g_settings_finalize (GObject *object)
534 GSettings *settings = G_SETTINGS (object);
536 g_settings_backend_unwatch (settings->priv->backend, settings);
537 g_settings_backend_unsubscribe (settings->priv->backend,
538 settings->priv->path);
539 g_object_unref (settings->priv->backend);
540 g_object_unref (settings->priv->schema);
541 g_free (settings->priv->schema_name);
542 g_free (settings->priv->path);
546 g_settings_class_init (GSettingsClass *class)
548 GObjectClass *object_class = G_OBJECT_CLASS (class);
550 class->writable_change_event = g_settings_real_writable_change_event;
551 class->change_event = g_settings_real_change_event;
553 object_class->set_property = g_settings_set_property;
554 object_class->get_property = g_settings_get_property;
555 object_class->constructed = g_settings_constructed;
556 object_class->finalize = g_settings_finalize;
558 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
561 * GSettings::changed:
562 * @settings: the object on which the signal was emitted
563 * @key: the name of the key that changed
565 * The "changed" signal is emitted when a key has potentially changed.
566 * You should call one of the g_settings_get() calls to check the new
569 * This signal supports detailed connections. You can connect to the
570 * detailed signal "changed::x" in order to only receive callbacks
571 * when key "x" changes.
573 g_settings_signals[SIGNAL_CHANGED] =
574 g_signal_new ("changed", G_TYPE_SETTINGS,
575 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
576 G_STRUCT_OFFSET (GSettingsClass, changed),
577 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
578 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
581 * GSettings::change-event:
582 * @settings: the object on which the signal was emitted
583 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
584 * @n_keys: the length of the @keys array, or 0
585 * @returns: %TRUE to stop other handlers from being invoked for the
586 * event. FALSE to propagate the event further.
588 * The "change-event" signal is emitted once per change event that
589 * affects this settings object. You should connect to this signal
590 * only if you are interested in viewing groups of changes before they
591 * are split out into multiple emissions of the "changed" signal.
592 * For most use cases it is more appropriate to use the "changed" signal.
594 * In the event that the change event applies to one or more specified
595 * keys, @keys will be an array of #GQuark of length @n_keys. In the
596 * event that the change event applies to the #GSettings object as a
597 * whole (ie: potentially every key has been changed) then @keys will
598 * be %NULL and @n_keys will be 0.
600 * The default handler for this signal invokes the "changed" signal
601 * for each affected key. If any other connected handler returns
602 * %TRUE then this default functionality will be supressed.
604 g_settings_signals[SIGNAL_CHANGE_EVENT] =
605 g_signal_new ("change-event", G_TYPE_SETTINGS,
607 G_STRUCT_OFFSET (GSettingsClass, change_event),
608 g_signal_accumulator_true_handled, NULL,
609 _gio_marshal_BOOL__POINTER_INT,
610 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
613 * GSettings::writable-changed:
614 * @settings: the object on which the signal was emitted
617 * The "writable-changed" signal is emitted when the writability of a
618 * key has potentially changed. You should call
619 * g_settings_is_writable() in order to determine the new status.
621 * This signal supports detailed connections. You can connect to the
622 * detailed signal "writable-changed::x" in order to only receive
623 * callbacks when the writability of "x" changes.
625 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
626 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
627 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
628 G_STRUCT_OFFSET (GSettingsClass, changed),
629 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
630 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
633 * GSettings::writable-change-event:
634 * @settings: the object on which the signal was emitted
635 * @key: the quark of the key, or 0
636 * @returns: %TRUE to stop other handlers from being invoked for the
637 * event. FALSE to propagate the event further.
639 * The "writable-change-event" signal is emitted once per writability
640 * change event that affects this settings object. You should connect
641 * to this signal if you are interested in viewing groups of changes
642 * before they are split out into multiple emissions of the
643 * "writable-changed" signal. For most use cases it is more
644 * appropriate to use the "writable-changed" signal.
646 * In the event that the writability change applies only to a single
647 * key, @key will be set to the #GQuark for that key. In the event
648 * that the writability change affects the entire settings object,
651 * The default handler for this signal invokes the "writable-changed"
652 * and "changed" signals for each affected key. This is done because
653 * changes in writability might also imply changes in value (if for
654 * example, a new mandatory setting is introduced). If any other
655 * connected handler returns %TRUE then this default functionality
658 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
659 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
661 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
662 g_signal_accumulator_true_handled, NULL,
663 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
668 * The name of the context that the settings are stored in.
670 g_object_class_install_property (object_class, PROP_CONTEXT,
671 g_param_spec_string ("context",
673 P_("The name of the context for this settings object"),
674 "", G_PARAM_CONSTRUCT_ONLY |
675 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
680 * The name of the schema that describes the types of keys
681 * for this #GSettings object.
683 g_object_class_install_property (object_class, PROP_SCHEMA,
684 g_param_spec_string ("schema",
686 P_("The name of the schema for this settings object"),
688 G_PARAM_CONSTRUCT_ONLY |
689 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
694 * The path within the backend where the settings are stored.
696 g_object_class_install_property (object_class, PROP_PATH,
697 g_param_spec_string ("path",
699 P_("The path within the backend where the settings are"),
701 G_PARAM_CONSTRUCT_ONLY |
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
705 * GSettings:has-unapplied:
707 * If this property is %TRUE, the #GSettings object has outstanding
708 * changes that will be applied when g_settings_apply() is called.
710 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
711 g_param_spec_boolean ("has-unapplied",
712 P_("Has unapplied changes"),
713 P_("TRUE if there are outstanding changes to apply()"),
715 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
720 * g_settings_get_value:
721 * @settings: a #GSettings object
722 * @key: the key to get the value for
723 * @returns: a new #GVariant
725 * Gets the value that is stored in @settings for @key.
727 * It is a programmer error to give a @key that isn't valid for
733 g_settings_get_value (GSettings *settings,
736 const gchar *unparsed = NULL;
737 GVariant *value, *options;
738 const GVariantType *type;
739 gchar lc_char = '\0';
743 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
745 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
747 if G_UNLIKELY (sval == NULL)
748 g_error ("schema '%s' does not contain a key named '%s'",
749 settings->priv->schema_name, key);
751 path = g_strconcat (settings->priv->path, key, NULL);
752 type = g_variant_get_type (sval);
753 value = g_settings_backend_read (settings->priv->backend, path, type);
760 GVariant *option_value;
762 g_variant_iter_init (&iter, options);
763 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
765 if (strcmp (option, "l10n") == 0)
766 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
768 g_warning ("unknown schema extension '%s'", option);
772 if (value && !g_variant_is_of_type (value, type))
774 g_variant_unref (value);
778 if (value == NULL && lc_char != '\0')
779 /* we will need to translate the schema default value */
781 const gchar *translated;
782 GError *error = NULL;
786 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
789 lc_category = LC_TIME;
791 lc_category = LC_MESSAGES;
793 translated = dcgettext (domain, unparsed, lc_category);
795 if (translated != unparsed)
796 /* it was translated, so we need to re-parse it */
798 value = g_variant_parse (g_variant_get_type (sval),
799 translated, NULL, NULL, &error);
803 g_warning ("Failed to parse translated string `%s' for "
804 "key `%s' in schema `%s': %s", unparsed, key,
805 settings->priv->schema_name, error->message);
806 g_warning ("Using untranslated default instead.");
807 g_error_free (error);
813 /* either translation failed or there was none to do.
814 * use the pre-compiled default.
816 value = g_variant_ref (sval);
818 g_variant_unref (sval);
824 * g_settings_set_value:
825 * @settings: a #GSettings object
826 * @key: the name of the key to set
827 * @value: a #GVariant of the correct type
828 * @returns: %TRUE if setting the key succeeded,
829 * %FALSE if the key was not writable
831 * Sets @key in @settings to @value.
833 * It is a programmer error to give a @key that isn't valid for
834 * @settings. It is a programmer error to give a @value of the
837 * If @value is floating then this function consumes the reference.
842 g_settings_set_value (GSettings *settings,
846 gboolean correct_type;
851 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
853 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
854 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
855 g_variant_unref (sval);
857 g_return_val_if_fail (correct_type, FALSE);
859 path = g_strconcat (settings->priv->path, key, NULL);
860 result = g_settings_backend_write (settings->priv->backend,
869 * @settings: a #GSettings object
870 * @key: the key to get the value for
871 * @format: a #GVariant format string
872 * @...: arguments as per @format
874 * Gets the value that is stored at @key in @settings.
876 * A convenience function that combines g_settings_get_value() with
879 * It is a programmer error to pass a @key that isn't valid for
880 * @settings or a @format_string that doesn't match the type of @key according
881 * to the schema of @settings.
886 g_settings_get (GSettings *settings,
894 value = g_settings_get_value (settings, key);
896 va_start (ap, format);
897 g_variant_get_va (value, format, NULL, &ap);
900 g_variant_unref (value);
905 * @settings: a #GSettings object
906 * @key: the name of the key to set
907 * @format: a #GVariant format string
908 * @...: arguments as per @format
909 * @returns: %TRUE if setting the key succeeded,
910 * %FALSE if the key was not writable
912 * Sets @key in @settings to @value.
914 * A convenience function that combines g_settings_set_value() with
917 * It is a programmer error to pass a @key that isn't valid for
918 * @settings or a @format that doesn't match the type of @key according
919 * to the schema of @settings.
924 g_settings_set (GSettings *settings,
932 va_start (ap, format);
933 value = g_variant_new_va (format, NULL, &ap);
936 return g_settings_set_value (settings, key, value);
940 * g_settings_is_writable:
941 * @settings: a #GSettings object
942 * @name: the name of a key
943 * @returns: %TRUE if the key @name is writable
945 * Finds out if a key can be written or not
950 g_settings_is_writable (GSettings *settings,
956 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
958 path = g_strconcat (settings->priv->path, name, NULL);
959 writable = g_settings_backend_get_writable (settings->priv->backend, path);
966 * g_settings_get_child:
967 * @settings: a #GSettings object
968 * @name: the name of the 'child' schema
969 * @returns: a 'child' settings object
971 * Creates a 'child' settings object which has a base path of
972 * <replaceable>base-path</replaceable>/@name", where
973 * <replaceable>base-path</replaceable> is the base path of @settings.
975 * The schema for the child settings object must have been declared
976 * in the schema of @settings using a <tag class="starttag">child</tag> element.
981 g_settings_get_child (GSettings *settings,
984 GVariant *child_schema;
989 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
991 child_name = g_strconcat (name, "/", NULL);
992 child_schema = g_settings_schema_get_value (settings->priv->schema,
994 if (child_schema == NULL ||
995 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
996 g_error ("Schema '%s' has no child '%s'",
997 settings->priv->schema_name, name);
999 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1000 child = g_object_new (G_TYPE_SETTINGS,
1001 "schema", g_variant_get_string (child_schema, NULL),
1004 g_variant_unref (child_schema);
1005 g_free (child_path);
1006 g_free (child_name);
1013 * @schema: the name of the schema
1014 * @returns: a new #GSettings object
1016 * Creates a new #GSettings object with a given schema.
1021 g_settings_new (const gchar *schema)
1023 return g_object_new (G_TYPE_SETTINGS,
1029 * g_settings_new_with_path:
1030 * @schema: the name of the schema
1031 * @path: the path to use
1032 * @returns: a new #GSettings object
1034 * Creates a new #GSettings object with a given schema and path.
1036 * You only need to do this if you want to directly create a settings
1037 * object with a schema that doesn't have a specified path of its own.
1038 * That's quite rare.
1040 * It is a programmer error to call this function for a schema that
1041 * has an explicitly specified path.
1046 g_settings_new_with_path (const gchar *schema,
1049 return g_object_new (G_TYPE_SETTINGS,
1056 * g_settings_new_with_context:
1057 * @schema: the name of the schema
1058 * @context: the context to use
1059 * @returns: a new #GSettings object
1061 * Creates a new #GSettings object with a given schema and context.
1063 * Creating settings objects with a context allow accessing settings
1064 * from a database other than the usual one. For example, it may make
1065 * sense to specify "defaults" in order to get a settings object that
1066 * modifies the system default settings instead of the settings for this
1069 * It is a programmer error to call this function for an unsupported
1070 * context. Use g_settings_supports_context() to determine if a context
1071 * is supported if you are unsure.
1076 g_settings_new_with_context (const gchar *schema,
1077 const gchar *context)
1079 return g_object_new (G_TYPE_SETTINGS,
1086 * g_settings_new_with_context_and_path:
1087 * @schema: the name of the schema
1088 * @path: the path to use
1089 * @returns: a new #GSettings object
1091 * Creates a new #GSettings object with a given schema, context and
1094 * This is a mix of g_settings_new_with_context() and
1095 * g_settings_new_with_path().
1100 g_settings_new_with_context_and_path (const gchar *schema,
1101 const gchar *context,
1104 return g_object_new (G_TYPE_SETTINGS,
1113 GSettings *settings;
1116 GSettingsBindGetMapping get_mapping;
1117 GSettingsBindSetMapping set_mapping;
1119 GDestroyNotify destroy;
1121 guint writable_handler_id;
1122 guint property_handler_id;
1123 const GParamSpec *property;
1124 guint key_handler_id;
1128 /* prevent recursion */
1133 g_settings_binding_free (gpointer data)
1135 GSettingsBinding *binding = data;
1137 g_assert (!binding->running);
1139 if (binding->writable_handler_id)
1140 g_signal_handler_disconnect (binding->settings,
1141 binding->writable_handler_id);
1143 if (binding->key_handler_id)
1144 g_signal_handler_disconnect (binding->settings,
1145 binding->key_handler_id);
1147 if (g_signal_handler_is_connected (binding->object,
1148 binding->property_handler_id))
1149 g_signal_handler_disconnect (binding->object,
1150 binding->property_handler_id);
1152 g_variant_type_free (binding->type);
1153 g_object_unref (binding->settings);
1155 if (binding->destroy)
1156 binding->destroy (binding->user_data);
1158 g_slice_free (GSettingsBinding, binding);
1162 g_settings_binding_quark (const char *property)
1167 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1168 quark = g_quark_from_string (tmp);
1175 g_settings_binding_key_changed (GSettings *settings,
1179 GSettingsBinding *binding = user_data;
1180 GValue value = { 0, };
1183 g_assert (settings == binding->settings);
1184 g_assert (key == binding->key);
1186 if (binding->running)
1189 binding->running = TRUE;
1191 g_value_init (&value, binding->property->value_type);
1192 variant = g_settings_get_value (settings, key);
1193 if (binding->get_mapping (&value, variant, binding->user_data))
1194 g_object_set_property (binding->object,
1195 binding->property->name,
1197 g_value_unset (&value);
1199 binding->running = FALSE;
1203 g_settings_binding_property_changed (GObject *object,
1204 const GParamSpec *pspec,
1207 GSettingsBinding *binding = user_data;
1208 GValue value = { 0, };
1211 g_assert (object == binding->object);
1212 g_assert (pspec == binding->property);
1214 if (binding->running)
1217 binding->running = TRUE;
1219 g_value_init (&value, pspec->value_type);
1220 g_object_get_property (object, pspec->name, &value);
1221 if ((variant = binding->set_mapping (&value, binding->type,
1222 binding->user_data)))
1224 g_settings_set_value (binding->settings,
1226 g_variant_ref_sink (variant));
1227 g_variant_unref (variant);
1229 g_value_unset (&value);
1231 binding->running = FALSE;
1236 * @settings: a #GSettings object
1237 * @key: the key to bind
1238 * @object: a #GObject
1239 * @property: the name of the property to bind
1240 * @flags: flags for the binding
1242 * Create a binding between the @key in the @settings object
1243 * and the property @property of @object.
1245 * The binding uses the default GIO mapping functions to map
1246 * between the settings and property values. These functions
1247 * handle booleans, numeric types and string types in a
1248 * straightforward way. Use g_settings_bind_with_mapping() if
1249 * you need a custom mapping, or map between types that are not
1250 * supported by the default mapping functions.
1252 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1253 * function also establishes a binding between the writability of
1254 * @key and the "sensitive" property of @object (if @object has
1255 * a boolean property by that name). See g_settings_bind_writable()
1256 * for more details about writable bindings.
1258 * Note that the lifecycle of the binding is tied to the object,
1259 * and that you can have only one binding per object property.
1260 * If you bind the same property twice on the same object, the second
1261 * binding overrides the first one.
1266 g_settings_bind (GSettings *settings,
1269 const gchar *property,
1270 GSettingsBindFlags flags)
1272 g_settings_bind_with_mapping (settings, key, object, property,
1273 flags, NULL, NULL, NULL, NULL);
1277 * g_settings_bind_with_mapping:
1278 * @settings: a #GSettings object
1279 * @key: the key to bind
1280 * @object: a #GObject
1281 * @property: the name of the property to bind
1282 * @flags: flags for the binding
1283 * @get_mapping: a function that gets called to convert values
1284 * from @settings to @object, or %NULL to use the default GIO mapping
1285 * @set_mapping: a function that gets called to convert values
1286 * from @object to @settings, or %NULL to use the default GIO mapping
1287 * @user_data: data that gets passed to @get_mapping and @set_mapping
1288 * @destroy: #GDestroyNotify function for @user_data
1290 * Create a binding between the @key in the @settings object
1291 * and the property @property of @object.
1293 * The binding uses the provided mapping functions to map between
1294 * settings and property values.
1296 * Note that the lifecycle of the binding is tied to the object,
1297 * and that you can have only one binding per object property.
1298 * If you bind the same property twice on the same object, the second
1299 * binding overrides the first one.
1304 g_settings_bind_with_mapping (GSettings *settings,
1307 const gchar *property,
1308 GSettingsBindFlags flags,
1309 GSettingsBindGetMapping get_mapping,
1310 GSettingsBindSetMapping set_mapping,
1312 GDestroyNotify destroy)
1314 GSettingsBinding *binding;
1315 GObjectClass *objectclass;
1316 gchar *detailed_signal;
1317 GQuark binding_quark;
1319 g_return_if_fail (G_IS_SETTINGS (settings));
1321 objectclass = G_OBJECT_GET_CLASS (object);
1323 binding = g_slice_new0 (GSettingsBinding);
1324 binding->settings = g_object_ref (settings);
1325 binding->object = object;
1326 binding->key = g_intern_string (key);
1327 binding->property = g_object_class_find_property (objectclass, property);
1328 binding->user_data = user_data;
1329 binding->destroy = destroy;
1330 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1331 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1333 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1334 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1336 if (binding->property == NULL)
1338 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1339 property, G_OBJECT_TYPE_NAME (object));
1343 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1345 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1346 property, G_OBJECT_TYPE_NAME (object));
1349 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1351 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1352 property, G_OBJECT_TYPE_NAME (object));
1359 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1360 binding->type = g_variant_type_copy (g_variant_get_type (value));
1362 g_variant_unref (value);
1365 if (binding->type == NULL)
1367 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1368 key, settings->priv->schema_name);
1372 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1373 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1374 !g_settings_mapping_is_compatible (binding->property->value_type,
1377 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1378 "'%s' which is not compatible with type '%s' of key '%s'"
1379 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1380 g_type_name (binding->property->value_type),
1381 g_variant_type_dup_string (binding->type), key,
1382 settings->priv->schema_name);
1386 if ((flags & G_SETTINGS_BIND_SET) &&
1387 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1389 GParamSpec *sensitive;
1391 sensitive = g_object_class_find_property (objectclass, "sensitive");
1393 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1394 (sensitive->flags & G_PARAM_WRITABLE))
1395 g_settings_bind_writable (settings, binding->key,
1396 object, "sensitive", FALSE);
1399 if (flags & G_SETTINGS_BIND_SET)
1401 detailed_signal = g_strdup_printf ("notify::%s", property);
1402 binding->property_handler_id =
1403 g_signal_connect (object, detailed_signal,
1404 G_CALLBACK (g_settings_binding_property_changed),
1406 g_free (detailed_signal);
1408 if (~flags & G_SETTINGS_BIND_GET)
1409 g_settings_binding_property_changed (object,
1414 if (flags & G_SETTINGS_BIND_GET)
1416 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1418 detailed_signal = g_strdup_printf ("changed::%s", key);
1419 binding->key_handler_id =
1420 g_signal_connect (settings, detailed_signal,
1421 G_CALLBACK (g_settings_binding_key_changed),
1423 g_free (detailed_signal);
1426 g_settings_binding_key_changed (settings, binding->key, binding);
1429 binding_quark = g_settings_binding_quark (property);
1430 g_object_set_qdata_full (object, binding_quark,
1431 binding, g_settings_binding_free);
1436 GSettings *settings;
1439 const gchar *property;
1442 } GSettingsWritableBinding;
1445 g_settings_writable_binding_free (gpointer data)
1447 GSettingsWritableBinding *binding = data;
1449 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1450 g_object_unref (binding->settings);
1454 g_settings_binding_writable_changed (GSettings *settings,
1458 GSettingsWritableBinding *binding = user_data;
1461 g_assert (settings == binding->settings);
1462 g_assert (key == binding->key);
1464 writable = g_settings_is_writable (settings, key);
1466 if (binding->inverted)
1467 writable = !writable;
1469 g_object_set (binding->object, binding->property, writable, NULL);
1473 * g_settings_bind_writable:
1474 * @settings: a #GSettings object
1475 * @key: the key to bind
1476 * @object: a #GObject
1477 * @property: the name of a boolean property to bind
1478 * @inverted: whether to 'invert' the value
1480 * Create a binding between the writability of @key in the
1481 * @settings object and the property @property of @object.
1482 * The property must be boolean; "sensitive" or "visible"
1483 * properties of widgets are the most likely candidates.
1485 * Writable bindings are always uni-directional; changes of the
1486 * writability of the setting will be propagated to the object
1487 * property, not the other way.
1489 * When the @inverted argument is %TRUE, the binding inverts the
1490 * value as it passes from the setting to the object, i.e. @property
1491 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1494 * Note that the lifecycle of the binding is tied to the object,
1495 * and that you can have only one binding per object property.
1496 * If you bind the same property twice on the same object, the second
1497 * binding overrides the first one.
1502 g_settings_bind_writable (GSettings *settings,
1505 const gchar *property,
1508 GSettingsWritableBinding *binding;
1509 gchar *detailed_signal;
1512 g_return_if_fail (G_IS_SETTINGS (settings));
1514 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1517 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1518 property, G_OBJECT_TYPE_NAME (object));
1521 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1523 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1524 property, G_OBJECT_TYPE_NAME (object));
1528 binding = g_slice_new (GSettingsWritableBinding);
1529 binding->settings = g_object_ref (settings);
1530 binding->object = object;
1531 binding->property = g_intern_string (property);
1532 binding->inverted = inverted;
1534 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1535 binding->handler_id =
1536 g_signal_connect (settings, detailed_signal,
1537 G_CALLBACK (g_settings_binding_writable_changed),
1539 g_free (detailed_signal);
1541 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1542 binding, g_settings_writable_binding_free);
1544 g_settings_binding_writable_changed (settings, binding->key, binding);
1548 * g_settings_unbind:
1549 * @object: the object
1550 * @property: the property whose binding is removed
1552 * Removes an existing binding for @property on @object.
1554 * Note that bindings are automatically removed when the
1555 * object is finalized, so it is rarely necessary to call this
1561 g_settings_unbind (gpointer object,
1562 const gchar *property)
1564 GQuark binding_quark;
1566 binding_quark = g_settings_binding_quark (property);
1567 g_object_set_qdata (object, binding_quark, NULL);
1571 * g_settings_get_string:
1572 * @settings: a #GSettings object
1573 * @key: the key to get the value for
1574 * @returns: a newly-allocated string
1576 * Gets the value that is stored at @key in @settings.
1578 * A convenience variant of g_settings_get() for strings.
1580 * It is a programmer error to pass a @key that isn't valid for
1581 * @settings or is not of type string.
1586 g_settings_get_string (GSettings *settings,
1592 value = g_settings_get_value (settings, key);
1593 result = g_variant_dup_string (value, NULL);
1594 g_variant_unref (value);
1600 * g_settings_set_string:
1601 * @settings: a #GSettings object
1602 * @key: the name of the key to set
1603 * @value: the value to set it to
1604 * @returns: %TRUE if setting the key succeeded,
1605 * %FALSE if the key was not writable
1607 * Sets @key in @settings to @value.
1609 * A convenience variant of g_settings_set() for strings.
1611 * It is a programmer error to pass a @key that isn't valid for
1612 * @settings or is not of type string.
1617 g_settings_set_string (GSettings *settings,
1621 return g_settings_set_value (settings, key, g_variant_new_string (value));
1625 * g_settings_get_int:
1626 * @settings: a #GSettings object
1627 * @key: the key to get the value for
1628 * @returns: an integer
1630 * Gets the value that is stored at @key in @settings.
1632 * A convenience variant of g_settings_get() for 32-bit integers.
1634 * It is a programmer error to pass a @key that isn't valid for
1635 * @settings or is not of type int32.
1640 g_settings_get_int (GSettings *settings,
1646 value = g_settings_get_value (settings, key);
1647 result = g_variant_get_int32 (value);
1648 g_variant_unref (value);
1654 * g_settings_set_int:
1655 * @settings: a #GSettings object
1656 * @key: the name of the key to set
1657 * @value: the value to set it to
1658 * @returns: %TRUE if setting the key succeeded,
1659 * %FALSE if the key was not writable
1661 * Sets @key in @settings to @value.
1663 * A convenience variant of g_settings_set() for 32-bit integers.
1665 * It is a programmer error to pass a @key that isn't valid for
1666 * @settings or is not of type int32.
1671 g_settings_set_int (GSettings *settings,
1675 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1679 * g_settings_get_double:
1680 * @settings: a #GSettings object
1681 * @key: the key to get the value for
1682 * @returns: a double
1684 * Gets the value that is stored at @key in @settings.
1686 * A convenience variant of g_settings_get() for doubles.
1688 * It is a programmer error to pass a @key that isn't valid for
1689 * @settings or is not of type double.
1694 g_settings_get_double (GSettings *settings,
1700 value = g_settings_get_value (settings, key);
1701 result = g_variant_get_double (value);
1702 g_variant_unref (value);
1708 * g_settings_set_double:
1709 * @settings: a #GSettings object
1710 * @key: the name of the key to set
1711 * @value: the value to set it to
1712 * @returns: %TRUE if setting the key succeeded,
1713 * %FALSE if the key was not writable
1715 * Sets @key in @settings to @value.
1717 * A convenience variant of g_settings_set() for doubles.
1719 * It is a programmer error to pass a @key that isn't valid for
1720 * @settings or is not of type double.
1725 g_settings_set_double (GSettings *settings,
1729 return g_settings_set_value (settings, key, g_variant_new_double (value));
1733 * g_settings_get_boolean:
1734 * @settings: a #GSettings object
1735 * @key: the key to get the value for
1736 * @returns: a boolean
1738 * Gets the value that is stored at @key in @settings.
1740 * A convenience variant of g_settings_get() for booleans.
1742 * It is a programmer error to pass a @key that isn't valid for
1743 * @settings or is not of type boolean.
1748 g_settings_get_boolean (GSettings *settings,
1754 value = g_settings_get_value (settings, key);
1755 result = g_variant_get_boolean (value);
1756 g_variant_unref (value);
1762 * g_settings_set_boolean:
1763 * @settings: a #GSettings object
1764 * @key: the name of the key to set
1765 * @value: the value to set it to
1766 * @returns: %TRUE if setting the key succeeded,
1767 * %FALSE if the key was not writable
1769 * Sets @key in @settings to @value.
1771 * A convenience variant of g_settings_set() for booleans.
1773 * It is a programmer error to pass a @key that isn't valid for
1774 * @settings or is not of type boolean.
1779 g_settings_set_boolean (GSettings *settings,
1783 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1787 * g_settings_get_strv:
1788 * @settings: a #GSettings object
1789 * @key: the key to get the value for
1790 * @returns: a newly-allocated, %NULL-terminated array of strings
1792 * Gets the value that is stored at @key in @settings.
1794 * A convenience variant of g_settings_get() for string arrays.
1796 * It is a programmer error to pass a @key that isn't valid for
1797 * @settings or is not of type 'string array'.
1802 g_settings_get_strv (GSettings *settings,
1809 value = g_settings_get_value (settings, key);
1810 result = g_variant_dup_strv (value, length);
1811 g_variant_unref (value);
1817 * g_settings_set_strv:
1818 * @settings: a #GSettings object
1819 * @key: the name of the key to set
1820 * @value: the value to set it to
1821 * @returns: %TRUE if setting the key succeeded,
1822 * %FALSE if the key was not writable
1824 * Sets @key in @settings to @value.
1826 * A convenience variant of g_settings_set() for string arrays.
1828 * It is a programmer error to pass a @key that isn't valid for
1829 * @settings or is not of type 'string array'.
1834 g_settings_set_strv (GSettings *settings,
1836 const gchar * const *value,
1839 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1842 #define __G_SETTINGS_C__
1843 #include "gioaliasdef.c"