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 <link linkend="glib-compile-schemas">glib-compile-schemas</link>
74 * utility. The input is a schema description in an XML format that can be
75 * described by the following DTD:
77 * <!ELEMENT schemalist (schema*) >
78 * <!ATTLIST schemalist gettext-domain #IMPLIED >
80 * <!ELEMENT schema (key|child)* >
81 * <!ATTLIST schema id CDATA #REQUIRED
83 * gettext-domain CDATA #IMPLIED >
85 * <!ELEMENT key (default|summary?|description?|range?|choices?) >
86 * <!-- name can only contain lowercase letters, numbers and '-' -->
87 * <!-- type must be a GVariant type string -->
88 * <!ATTLIST key name CDATA #REQUIRED
89 * type CDATA #REQUIRED >
91 * <!-- the default value is specified a a serialized GVariant,
92 * i.e. you have to include the quotes when specifying a string -->
93 * <!ELEMENT default (#PCDATA) >
94 * <!-- the presence of the l10n attribute marks a default value for
95 * translation, its value is the gettext category to use -->
96 * <!-- if context is present, it specifies msgctxt to use -->
97 * <!ATTLIST default l10n (messages|time) #IMPLIED
98 * context CDATA #IMPLIED >
100 * <!ELEMENT summary (#PCDATA) >
101 * <!ELEMENT description (#PCDATA) >
103 * <!ELEMENT range EMPTY >
104 * <!ATTLIST range min CDATA #REQUIRED
105 * max CDATA #REQUIRED >
107 * <!ELEMENT choices (choice+) >
108 * <!ELEMENT choice (alias?) >
109 * <!ATTLIST choice value CDATA #REQUIRED >
110 * <!ELEMENT choice (alias?) >
111 * <!ELEMENT alias EMPTY >
112 * <!ATTLIST alias value CDATA #REQUIRED >
114 * <!ELEMENT child EMPTY >
115 * <!ATTLIST child name CDATA #REQUIRED
116 * schema CDATA #REQUIRED >
120 * At runtime, schemas are identified by their id (as specified
121 * in the <tag class="attribute">id</tag> attribute of the
122 * <tag class="starttag">schema</tag> element). The
123 * convention for schema ids is to use a dotted name, similar in
124 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
127 * <title>Binding</title>
129 * A very convenient feature of GSettings lets you bind #GObject properties
130 * directly to settings, using g_settings_bind(). Once a GObject property
131 * has been bound to a setting, changes on either side are automatically
132 * propagated to the other side. GSettings handles details like
133 * mapping between GObject and GVariant types, and preventing infinite
137 * This makes it very easy to hook up a preferences dialog to the
138 * underlying settings. To make this even more convenient, GSettings
139 * looks for a boolean property with the name "sensitivity" and
140 * automatically binds it to the writability of the bound setting.
141 * If this 'magic' gets in the way, it can be suppressed with the
142 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
147 struct _GSettingsPrivate
149 /* where the signals go... */
150 GMainContext *main_context;
152 GSettingsBackend *backend;
153 GSettingsSchema *schema;
158 GDelayedSettingsBackend *delayed;
173 SIGNAL_WRITABLE_CHANGE_EVENT,
174 SIGNAL_WRITABLE_CHANGED,
180 static guint g_settings_signals[N_SIGNALS];
182 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
185 g_settings_real_change_event (GSettings *settings,
192 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
194 for (i = 0; i < n_keys; i++)
195 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
196 keys[i], g_quark_to_string (keys[i]));
202 g_settings_real_writable_change_event (GSettings *settings,
205 const GQuark *keys = &key;
210 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
212 for (i = 0; i < n_keys; i++)
214 const gchar *string = g_quark_to_string (keys[i]);
216 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
218 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
226 settings_backend_changed (GSettingsBackend *backend,
231 GSettings *settings = G_SETTINGS (target);
232 gboolean ignore_this;
235 g_assert (settings->priv->backend == backend);
237 for (i = 0; key[i] == settings->priv->path[i]; i++);
239 if (settings->priv->path[i] == '\0' &&
240 g_settings_schema_has_key (settings->priv->schema, key + i))
244 quark = g_quark_from_string (key + i);
245 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
246 0, &quark, 1, &ignore_this);
251 settings_backend_path_changed (GSettingsBackend *backend,
256 GSettings *settings = G_SETTINGS (target);
257 gboolean ignore_this;
259 g_assert (settings->priv->backend == backend);
261 if (g_str_has_prefix (settings->priv->path, path))
262 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
263 0, NULL, 0, &ignore_this);
267 settings_backend_keys_changed (GSettingsBackend *backend,
270 const gchar * const *items,
273 GSettings *settings = G_SETTINGS (target);
274 gboolean ignore_this;
277 g_assert (settings->priv->backend == backend);
279 for (i = 0; settings->priv->path[i] &&
280 settings->priv->path[i] == path[i]; i++);
287 for (j = 0; items[j]; j++)
289 const gchar *item = items[j];
292 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
294 if (settings->priv->path[i + k] == '\0' &&
295 g_settings_schema_has_key (settings->priv->schema, item + k))
296 quarks[l++] = g_quark_from_string (item + k);
298 /* "256 quarks ought to be enough for anybody!"
299 * If this bites you, I'm sorry. Please file a bug.
305 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
306 0, quarks, l, &ignore_this);
311 settings_backend_writable_changed (GSettingsBackend *backend,
315 GSettings *settings = G_SETTINGS (target);
316 gboolean ignore_this;
319 g_assert (settings->priv->backend == backend);
321 for (i = 0; key[i] == settings->priv->path[i]; i++);
323 if (settings->priv->path[i] == '\0' &&
324 g_settings_schema_has_key (settings->priv->schema, key + i))
325 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
326 0, g_quark_from_string (key + i), &ignore_this);
330 settings_backend_path_writable_changed (GSettingsBackend *backend,
334 GSettings *settings = G_SETTINGS (target);
335 gboolean ignore_this;
337 g_assert (settings->priv->backend == backend);
339 if (g_str_has_prefix (settings->priv->path, path))
340 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
341 0, (GQuark) 0, &ignore_this);
345 g_settings_constructed (GObject *object)
347 GSettings *settings = G_SETTINGS (object);
348 const gchar *schema_path;
350 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
351 schema_path = g_settings_schema_get_path (settings->priv->schema);
353 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
354 g_error ("settings object created with schema '%s' and path '%s', but "
355 "path '%s' is specified by schema",
356 settings->priv->schema_name, settings->priv->path, schema_path);
358 if (settings->priv->path == NULL)
360 if (schema_path == NULL)
361 g_error ("attempting to create schema '%s' without a path",
362 settings->priv->schema_name);
364 settings->priv->path = g_strdup (schema_path);
367 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
368 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
369 settings->priv->main_context,
370 settings_backend_changed,
371 settings_backend_path_changed,
372 settings_backend_keys_changed,
373 settings_backend_writable_changed,
374 settings_backend_path_writable_changed);
375 g_settings_backend_subscribe (settings->priv->backend,
376 settings->priv->path);
380 g_settings_init (GSettings *settings)
382 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
386 settings->priv->main_context = g_main_context_get_thread_default ();
388 if (settings->priv->main_context == NULL)
389 settings->priv->main_context = g_main_context_default ();
391 g_main_context_ref (settings->priv->main_context);
396 * @settings: a #GSettings object
398 * Changes the #GSettings object into 'delay-apply' mode. In this
399 * mode, changes to @settings are not immediately propagated to the
400 * backend, but kept locally until g_settings_apply() is called.
405 g_settings_delay (GSettings *settings)
407 g_return_if_fail (G_IS_SETTINGS (settings));
409 if (settings->priv->delayed)
412 settings->priv->delayed =
413 g_delayed_settings_backend_new (settings->priv->backend,
415 settings->priv->main_context);
416 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
417 g_object_unref (settings->priv->backend);
419 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
420 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
421 settings->priv->main_context,
422 settings_backend_changed,
423 settings_backend_path_changed,
424 settings_backend_keys_changed,
425 settings_backend_writable_changed,
426 settings_backend_path_writable_changed);
431 * @settings: a #GSettings instance
433 * Applies any changes that have been made to the settings. This
434 * function does nothing unless @settings is in 'delay-apply' mode;
435 * see g_settings_set_delay_apply(). In the normal case settings are
436 * always applied immediately.
439 g_settings_apply (GSettings *settings)
441 if (settings->priv->delayed)
443 GDelayedSettingsBackend *delayed;
445 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
446 g_delayed_settings_backend_apply (delayed);
452 * @settings: a #GSettings instance
454 * Reverts all non-applied changes to the settings. This function
455 * does nothing unless @settings is in 'delay-apply' mode; see
456 * g_settings_set_delay_apply(). In the normal case settings are
457 * always applied immediately.
459 * Change notifications will be emitted for affected keys.
462 g_settings_revert (GSettings *settings)
464 if (settings->priv->delayed)
466 GDelayedSettingsBackend *delayed;
468 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
469 g_delayed_settings_backend_revert (delayed);
474 g_settings_set_property (GObject *object,
479 GSettings *settings = G_SETTINGS (object);
484 g_assert (settings->priv->schema_name == NULL);
485 settings->priv->schema_name = g_value_dup_string (value);
489 settings->priv->path = g_value_dup_string (value);
493 settings->priv->context = g_value_dup_string (value);
497 g_assert_not_reached ();
502 * g_settings_get_has_unapplied:
503 * @settings: a #GSettings object
504 * @returns: %TRUE if @settings has unapplied changes
506 * Returns whether the #GSettings object has any unapplied
507 * changes. This can only be the case if it is in 'delayed-apply' mode.
512 g_settings_get_has_unapplied (GSettings *settings)
514 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
516 return settings->priv->delayed &&
517 g_delayed_settings_backend_get_has_unapplied (
518 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
522 g_settings_get_property (GObject *object,
527 GSettings *settings = G_SETTINGS (object);
532 g_value_set_object (value, settings->priv->schema);
535 case PROP_HAS_UNAPPLIED:
536 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
540 g_assert_not_reached ();
545 g_settings_finalize (GObject *object)
547 GSettings *settings = G_SETTINGS (object);
549 g_settings_backend_unsubscribe (settings->priv->backend,
550 settings->priv->path);
551 g_main_context_unref (settings->priv->main_context);
552 g_object_unref (settings->priv->backend);
553 g_object_unref (settings->priv->schema);
554 g_free (settings->priv->schema_name);
555 g_free (settings->priv->path);
559 g_settings_class_init (GSettingsClass *class)
561 GObjectClass *object_class = G_OBJECT_CLASS (class);
563 class->writable_change_event = g_settings_real_writable_change_event;
564 class->change_event = g_settings_real_change_event;
566 object_class->set_property = g_settings_set_property;
567 object_class->get_property = g_settings_get_property;
568 object_class->constructed = g_settings_constructed;
569 object_class->finalize = g_settings_finalize;
571 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
574 * GSettings::changed:
575 * @settings: the object on which the signal was emitted
576 * @key: the name of the key that changed
578 * The "changed" signal is emitted when a key has potentially changed.
579 * You should call one of the g_settings_get() calls to check the new
582 * This signal supports detailed connections. You can connect to the
583 * detailed signal "changed::x" in order to only receive callbacks
584 * when key "x" changes.
586 g_settings_signals[SIGNAL_CHANGED] =
587 g_signal_new ("changed", G_TYPE_SETTINGS,
588 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
589 G_STRUCT_OFFSET (GSettingsClass, changed),
590 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
591 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
594 * GSettings::change-event:
595 * @settings: the object on which the signal was emitted
596 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
597 * @n_keys: the length of the @keys array, or 0
598 * @returns: %TRUE to stop other handlers from being invoked for the
599 * event. FALSE to propagate the event further.
601 * The "change-event" signal is emitted once per change event that
602 * affects this settings object. You should connect to this signal
603 * only if you are interested in viewing groups of changes before they
604 * are split out into multiple emissions of the "changed" signal.
605 * For most use cases it is more appropriate to use the "changed" signal.
607 * In the event that the change event applies to one or more specified
608 * keys, @keys will be an array of #GQuark of length @n_keys. In the
609 * event that the change event applies to the #GSettings object as a
610 * whole (ie: potentially every key has been changed) then @keys will
611 * be %NULL and @n_keys will be 0.
613 * The default handler for this signal invokes the "changed" signal
614 * for each affected key. If any other connected handler returns
615 * %TRUE then this default functionality will be supressed.
617 g_settings_signals[SIGNAL_CHANGE_EVENT] =
618 g_signal_new ("change-event", G_TYPE_SETTINGS,
620 G_STRUCT_OFFSET (GSettingsClass, change_event),
621 g_signal_accumulator_true_handled, NULL,
622 _gio_marshal_BOOL__POINTER_INT,
623 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
626 * GSettings::writable-changed:
627 * @settings: the object on which the signal was emitted
630 * The "writable-changed" signal is emitted when the writability of a
631 * key has potentially changed. You should call
632 * g_settings_is_writable() in order to determine the new status.
634 * This signal supports detailed connections. You can connect to the
635 * detailed signal "writable-changed::x" in order to only receive
636 * callbacks when the writability of "x" changes.
638 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
639 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
640 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
641 G_STRUCT_OFFSET (GSettingsClass, changed),
642 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
643 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
646 * GSettings::writable-change-event:
647 * @settings: the object on which the signal was emitted
648 * @key: the quark of the key, or 0
649 * @returns: %TRUE to stop other handlers from being invoked for the
650 * event. FALSE to propagate the event further.
652 * The "writable-change-event" signal is emitted once per writability
653 * change event that affects this settings object. You should connect
654 * to this signal if you are interested in viewing groups of changes
655 * before they are split out into multiple emissions of the
656 * "writable-changed" signal. For most use cases it is more
657 * appropriate to use the "writable-changed" signal.
659 * In the event that the writability change applies only to a single
660 * key, @key will be set to the #GQuark for that key. In the event
661 * that the writability change affects the entire settings object,
664 * The default handler for this signal invokes the "writable-changed"
665 * and "changed" signals for each affected key. This is done because
666 * changes in writability might also imply changes in value (if for
667 * example, a new mandatory setting is introduced). If any other
668 * connected handler returns %TRUE then this default functionality
671 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
672 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
674 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
675 g_signal_accumulator_true_handled, NULL,
676 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
681 * The name of the context that the settings are stored in.
683 g_object_class_install_property (object_class, PROP_CONTEXT,
684 g_param_spec_string ("context",
686 P_("The name of the context for this settings object"),
687 "", G_PARAM_CONSTRUCT_ONLY |
688 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
693 * The name of the schema that describes the types of keys
694 * for this #GSettings object.
696 g_object_class_install_property (object_class, PROP_SCHEMA,
697 g_param_spec_string ("schema",
699 P_("The name of the schema for this settings object"),
701 G_PARAM_CONSTRUCT_ONLY |
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
707 * The path within the backend where the settings are stored.
709 g_object_class_install_property (object_class, PROP_PATH,
710 g_param_spec_string ("path",
712 P_("The path within the backend where the settings are"),
714 G_PARAM_CONSTRUCT_ONLY |
715 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
718 * GSettings:has-unapplied:
720 * If this property is %TRUE, the #GSettings object has outstanding
721 * changes that will be applied when g_settings_apply() is called.
723 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
724 g_param_spec_boolean ("has-unapplied",
725 P_("Has unapplied changes"),
726 P_("TRUE if there are outstanding changes to apply()"),
728 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
733 * g_settings_get_value:
734 * @settings: a #GSettings object
735 * @key: the key to get the value for
736 * @returns: a new #GVariant
738 * Gets the value that is stored in @settings for @key.
740 * It is a programmer error to give a @key that isn't valid for
746 g_settings_get_value (GSettings *settings,
749 const gchar *unparsed = NULL;
750 GVariant *value, *options;
751 const GVariantType *type;
752 gchar lc_char = '\0';
756 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
758 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
760 if G_UNLIKELY (sval == NULL)
761 g_error ("schema '%s' does not contain a key named '%s'",
762 settings->priv->schema_name, key);
764 path = g_strconcat (settings->priv->path, key, NULL);
765 type = g_variant_get_type (sval);
766 value = g_settings_backend_read (settings->priv->backend, path, type, FALSE);
773 GVariant *option_value;
775 g_variant_iter_init (&iter, options);
776 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
778 if (strcmp (option, "l10n") == 0)
779 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
781 g_warning ("unknown schema extension '%s'", option);
785 if (value && !g_variant_is_of_type (value, type))
787 g_variant_unref (value);
791 if (value == NULL && lc_char != '\0')
792 /* we will need to translate the schema default value */
794 const gchar *translated;
795 GError *error = NULL;
798 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
801 translated = g_dcgettext (domain, unparsed, LC_TIME);
803 translated = g_dgettext (domain, unparsed);
805 if (translated != unparsed)
806 /* it was translated, so we need to re-parse it */
808 value = g_variant_parse (g_variant_get_type (sval),
809 translated, NULL, NULL, &error);
813 g_warning ("Failed to parse translated string `%s' for "
814 "key `%s' in schema `%s': %s", unparsed, key,
815 settings->priv->schema_name, error->message);
816 g_warning ("Using untranslated default instead.");
817 g_error_free (error);
823 /* either translation failed or there was none to do.
824 * use the pre-compiled default.
826 value = g_variant_ref (sval);
828 g_variant_unref (sval);
834 * g_settings_set_value:
835 * @settings: a #GSettings object
836 * @key: the name of the key to set
837 * @value: a #GVariant of the correct type
838 * @returns: %TRUE if setting the key succeeded,
839 * %FALSE if the key was not writable
841 * Sets @key in @settings to @value.
843 * It is a programmer error to give a @key that isn't valid for
844 * @settings. It is a programmer error to give a @value of the
847 * If @value is floating then this function consumes the reference.
852 g_settings_set_value (GSettings *settings,
856 gboolean correct_type;
861 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
863 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
864 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
865 g_variant_unref (sval);
867 g_return_val_if_fail (correct_type, FALSE);
869 path = g_strconcat (settings->priv->path, key, NULL);
870 result = g_settings_backend_write (settings->priv->backend,
879 * @settings: a #GSettings object
880 * @key: the key to get the value for
881 * @format: a #GVariant format string
882 * @...: arguments as per @format
884 * Gets the value that is stored at @key in @settings.
886 * A convenience function that combines g_settings_get_value() with
889 * It is a programmer error to pass a @key that isn't valid for
890 * @settings or a @format_string that doesn't match the type of @key according
891 * to the schema of @settings.
896 g_settings_get (GSettings *settings,
904 value = g_settings_get_value (settings, key);
906 va_start (ap, format);
907 g_variant_get_va (value, format, NULL, &ap);
910 g_variant_unref (value);
915 * @settings: a #GSettings object
916 * @key: the name of the key to set
917 * @format: a #GVariant format string
918 * @...: arguments as per @format
919 * @returns: %TRUE if setting the key succeeded,
920 * %FALSE if the key was not writable
922 * Sets @key in @settings to @value.
924 * A convenience function that combines g_settings_set_value() with
927 * It is a programmer error to pass a @key that isn't valid for
928 * @settings or a @format that doesn't match the type of @key according
929 * to the schema of @settings.
934 g_settings_set (GSettings *settings,
942 va_start (ap, format);
943 value = g_variant_new_va (format, NULL, &ap);
946 return g_settings_set_value (settings, key, value);
950 * g_settings_is_writable:
951 * @settings: a #GSettings object
952 * @name: the name of a key
953 * @returns: %TRUE if the key @name is writable
955 * Finds out if a key can be written or not
960 g_settings_is_writable (GSettings *settings,
966 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
968 path = g_strconcat (settings->priv->path, name, NULL);
969 writable = g_settings_backend_get_writable (settings->priv->backend, path);
976 * g_settings_get_child:
977 * @settings: a #GSettings object
978 * @name: the name of the 'child' schema
979 * @returns: a 'child' settings object
981 * Creates a 'child' settings object which has a base path of
982 * <replaceable>base-path</replaceable>/@name", where
983 * <replaceable>base-path</replaceable> is the base path of @settings.
985 * The schema for the child settings object must have been declared
986 * in the schema of @settings using a <tag class="starttag">child</tag> element.
991 g_settings_get_child (GSettings *settings,
994 GVariant *child_schema;
999 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1001 child_name = g_strconcat (name, "/", NULL);
1002 child_schema = g_settings_schema_get_value (settings->priv->schema,
1004 if (child_schema == NULL ||
1005 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
1006 g_error ("Schema '%s' has no child '%s'",
1007 settings->priv->schema_name, name);
1009 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1010 child = g_object_new (G_TYPE_SETTINGS,
1011 "schema", g_variant_get_string (child_schema, NULL),
1014 g_variant_unref (child_schema);
1015 g_free (child_path);
1016 g_free (child_name);
1023 * @schema: the name of the schema
1024 * @returns: a new #GSettings object
1026 * Creates a new #GSettings object with a given schema.
1028 * Signals on the newly created #GSettings object will be dispatched
1029 * via the thread-default #GMainContext in effect at the time of the
1030 * call to g_settings_new(). The new #GSettings will hold a reference
1031 * on the context. See g_main_context_push_thread_default().
1036 g_settings_new (const gchar *schema)
1038 return g_object_new (G_TYPE_SETTINGS,
1044 * g_settings_new_with_path:
1045 * @schema: the name of the schema
1046 * @path: the path to use
1047 * @returns: a new #GSettings object
1049 * Creates a new #GSettings object with a given schema and path.
1051 * You only need to do this if you want to directly create a settings
1052 * object with a schema that doesn't have a specified path of its own.
1053 * That's quite rare.
1055 * It is a programmer error to call this function for a schema that
1056 * has an explicitly specified path.
1061 g_settings_new_with_path (const gchar *schema,
1064 return g_object_new (G_TYPE_SETTINGS,
1071 * g_settings_new_with_context:
1072 * @schema: the name of the schema
1073 * @context: the context to use
1074 * @returns: a new #GSettings object
1076 * Creates a new #GSettings object with a given schema and context.
1078 * Creating settings objects with a context allow accessing settings
1079 * from a database other than the usual one. For example, it may make
1080 * sense to specify "defaults" in order to get a settings object that
1081 * modifies the system default settings instead of the settings for this
1084 * It is a programmer error to call this function for an unsupported
1085 * context. Use g_settings_supports_context() to determine if a context
1086 * is supported if you are unsure.
1091 g_settings_new_with_context (const gchar *schema,
1092 const gchar *context)
1094 return g_object_new (G_TYPE_SETTINGS,
1101 * g_settings_new_with_context_and_path:
1102 * @schema: the name of the schema
1103 * @path: the path to use
1104 * @returns: a new #GSettings object
1106 * Creates a new #GSettings object with a given schema, context and
1109 * This is a mix of g_settings_new_with_context() and
1110 * g_settings_new_with_path().
1115 g_settings_new_with_context_and_path (const gchar *schema,
1116 const gchar *context,
1119 return g_object_new (G_TYPE_SETTINGS,
1128 GSettings *settings;
1131 GSettingsBindGetMapping get_mapping;
1132 GSettingsBindSetMapping set_mapping;
1134 GDestroyNotify destroy;
1136 guint writable_handler_id;
1137 guint property_handler_id;
1138 const GParamSpec *property;
1139 guint key_handler_id;
1143 /* prevent recursion */
1148 g_settings_binding_free (gpointer data)
1150 GSettingsBinding *binding = data;
1152 g_assert (!binding->running);
1154 if (binding->writable_handler_id)
1155 g_signal_handler_disconnect (binding->settings,
1156 binding->writable_handler_id);
1158 if (binding->key_handler_id)
1159 g_signal_handler_disconnect (binding->settings,
1160 binding->key_handler_id);
1162 if (g_signal_handler_is_connected (binding->object,
1163 binding->property_handler_id))
1164 g_signal_handler_disconnect (binding->object,
1165 binding->property_handler_id);
1167 g_variant_type_free (binding->type);
1168 g_object_unref (binding->settings);
1170 if (binding->destroy)
1171 binding->destroy (binding->user_data);
1173 g_slice_free (GSettingsBinding, binding);
1177 g_settings_binding_quark (const char *property)
1182 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1183 quark = g_quark_from_string (tmp);
1190 g_settings_binding_key_changed (GSettings *settings,
1194 GSettingsBinding *binding = user_data;
1195 GValue value = { 0, };
1198 g_assert (settings == binding->settings);
1199 g_assert (key == binding->key);
1201 if (binding->running)
1204 binding->running = TRUE;
1206 g_value_init (&value, binding->property->value_type);
1207 variant = g_settings_get_value (settings, key);
1208 if (binding->get_mapping (&value, variant, binding->user_data))
1209 g_object_set_property (binding->object,
1210 binding->property->name,
1212 g_value_unset (&value);
1214 binding->running = FALSE;
1218 g_settings_binding_property_changed (GObject *object,
1219 const GParamSpec *pspec,
1222 GSettingsBinding *binding = user_data;
1223 GValue value = { 0, };
1226 g_assert (object == binding->object);
1227 g_assert (pspec == binding->property);
1229 if (binding->running)
1232 binding->running = TRUE;
1234 g_value_init (&value, pspec->value_type);
1235 g_object_get_property (object, pspec->name, &value);
1236 if ((variant = binding->set_mapping (&value, binding->type,
1237 binding->user_data)))
1239 g_settings_set_value (binding->settings,
1241 g_variant_ref_sink (variant));
1242 g_variant_unref (variant);
1244 g_value_unset (&value);
1246 binding->running = FALSE;
1251 * @settings: a #GSettings object
1252 * @key: the key to bind
1253 * @object: a #GObject
1254 * @property: the name of the property to bind
1255 * @flags: flags for the binding
1257 * Create a binding between the @key in the @settings object
1258 * and the property @property of @object.
1260 * The binding uses the default GIO mapping functions to map
1261 * between the settings and property values. These functions
1262 * handle booleans, numeric types and string types in a
1263 * straightforward way. Use g_settings_bind_with_mapping() if
1264 * you need a custom mapping, or map between types that are not
1265 * supported by the default mapping functions.
1267 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1268 * function also establishes a binding between the writability of
1269 * @key and the "sensitive" property of @object (if @object has
1270 * a boolean property by that name). See g_settings_bind_writable()
1271 * for more details about writable bindings.
1273 * Note that the lifecycle of the binding is tied to the object,
1274 * and that you can have only one binding per object property.
1275 * If you bind the same property twice on the same object, the second
1276 * binding overrides the first one.
1281 g_settings_bind (GSettings *settings,
1284 const gchar *property,
1285 GSettingsBindFlags flags)
1287 g_settings_bind_with_mapping (settings, key, object, property,
1288 flags, NULL, NULL, NULL, NULL);
1292 * g_settings_bind_with_mapping:
1293 * @settings: a #GSettings object
1294 * @key: the key to bind
1295 * @object: a #GObject
1296 * @property: the name of the property to bind
1297 * @flags: flags for the binding
1298 * @get_mapping: a function that gets called to convert values
1299 * from @settings to @object, or %NULL to use the default GIO mapping
1300 * @set_mapping: a function that gets called to convert values
1301 * from @object to @settings, or %NULL to use the default GIO mapping
1302 * @user_data: data that gets passed to @get_mapping and @set_mapping
1303 * @destroy: #GDestroyNotify function for @user_data
1305 * Create a binding between the @key in the @settings object
1306 * and the property @property of @object.
1308 * The binding uses the provided mapping functions to map between
1309 * settings and property values.
1311 * Note that the lifecycle of the binding is tied to the object,
1312 * and that you can have only one binding per object property.
1313 * If you bind the same property twice on the same object, the second
1314 * binding overrides the first one.
1319 g_settings_bind_with_mapping (GSettings *settings,
1322 const gchar *property,
1323 GSettingsBindFlags flags,
1324 GSettingsBindGetMapping get_mapping,
1325 GSettingsBindSetMapping set_mapping,
1327 GDestroyNotify destroy)
1329 GSettingsBinding *binding;
1330 GObjectClass *objectclass;
1331 gchar *detailed_signal;
1332 GQuark binding_quark;
1334 g_return_if_fail (G_IS_SETTINGS (settings));
1336 objectclass = G_OBJECT_GET_CLASS (object);
1338 binding = g_slice_new0 (GSettingsBinding);
1339 binding->settings = g_object_ref (settings);
1340 binding->object = object;
1341 binding->key = g_intern_string (key);
1342 binding->property = g_object_class_find_property (objectclass, property);
1343 binding->user_data = user_data;
1344 binding->destroy = destroy;
1345 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1346 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1348 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1349 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1351 if (binding->property == NULL)
1353 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1354 property, G_OBJECT_TYPE_NAME (object));
1358 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1360 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1361 property, G_OBJECT_TYPE_NAME (object));
1364 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1366 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1367 property, G_OBJECT_TYPE_NAME (object));
1374 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1375 binding->type = g_variant_type_copy (g_variant_get_type (value));
1377 g_variant_unref (value);
1380 if (binding->type == NULL)
1382 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1383 key, settings->priv->schema_name);
1387 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1388 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1389 !g_settings_mapping_is_compatible (binding->property->value_type,
1392 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1393 "'%s' which is not compatible with type '%s' of key '%s'"
1394 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1395 g_type_name (binding->property->value_type),
1396 g_variant_type_dup_string (binding->type), key,
1397 settings->priv->schema_name);
1401 if ((flags & G_SETTINGS_BIND_SET) &&
1402 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1404 GParamSpec *sensitive;
1406 sensitive = g_object_class_find_property (objectclass, "sensitive");
1408 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1409 (sensitive->flags & G_PARAM_WRITABLE))
1410 g_settings_bind_writable (settings, binding->key,
1411 object, "sensitive", FALSE);
1414 if (flags & G_SETTINGS_BIND_SET)
1416 detailed_signal = g_strdup_printf ("notify::%s", property);
1417 binding->property_handler_id =
1418 g_signal_connect (object, detailed_signal,
1419 G_CALLBACK (g_settings_binding_property_changed),
1421 g_free (detailed_signal);
1423 if (~flags & G_SETTINGS_BIND_GET)
1424 g_settings_binding_property_changed (object,
1429 if (flags & G_SETTINGS_BIND_GET)
1431 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1433 detailed_signal = g_strdup_printf ("changed::%s", key);
1434 binding->key_handler_id =
1435 g_signal_connect (settings, detailed_signal,
1436 G_CALLBACK (g_settings_binding_key_changed),
1438 g_free (detailed_signal);
1441 g_settings_binding_key_changed (settings, binding->key, binding);
1444 binding_quark = g_settings_binding_quark (property);
1445 g_object_set_qdata_full (object, binding_quark,
1446 binding, g_settings_binding_free);
1451 GSettings *settings;
1454 const gchar *property;
1457 } GSettingsWritableBinding;
1460 g_settings_writable_binding_free (gpointer data)
1462 GSettingsWritableBinding *binding = data;
1464 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1465 g_object_unref (binding->settings);
1466 g_slice_free (GSettingsWritableBinding, binding);
1470 g_settings_binding_writable_changed (GSettings *settings,
1474 GSettingsWritableBinding *binding = user_data;
1477 g_assert (settings == binding->settings);
1478 g_assert (key == binding->key);
1480 writable = g_settings_is_writable (settings, key);
1482 if (binding->inverted)
1483 writable = !writable;
1485 g_object_set (binding->object, binding->property, writable, NULL);
1489 * g_settings_bind_writable:
1490 * @settings: a #GSettings object
1491 * @key: the key to bind
1492 * @object: a #GObject
1493 * @property: the name of a boolean property to bind
1494 * @inverted: whether to 'invert' the value
1496 * Create a binding between the writability of @key in the
1497 * @settings object and the property @property of @object.
1498 * The property must be boolean; "sensitive" or "visible"
1499 * properties of widgets are the most likely candidates.
1501 * Writable bindings are always uni-directional; changes of the
1502 * writability of the setting will be propagated to the object
1503 * property, not the other way.
1505 * When the @inverted argument is %TRUE, the binding inverts the
1506 * value as it passes from the setting to the object, i.e. @property
1507 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1510 * Note that the lifecycle of the binding is tied to the object,
1511 * and that you can have only one binding per object property.
1512 * If you bind the same property twice on the same object, the second
1513 * binding overrides the first one.
1518 g_settings_bind_writable (GSettings *settings,
1521 const gchar *property,
1524 GSettingsWritableBinding *binding;
1525 gchar *detailed_signal;
1528 g_return_if_fail (G_IS_SETTINGS (settings));
1530 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1533 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1534 property, G_OBJECT_TYPE_NAME (object));
1537 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1539 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1540 property, G_OBJECT_TYPE_NAME (object));
1544 binding = g_slice_new (GSettingsWritableBinding);
1545 binding->settings = g_object_ref (settings);
1546 binding->object = object;
1547 binding->key = g_intern_string (key);
1548 binding->property = g_intern_string (property);
1549 binding->inverted = inverted;
1551 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1552 binding->handler_id =
1553 g_signal_connect (settings, detailed_signal,
1554 G_CALLBACK (g_settings_binding_writable_changed),
1556 g_free (detailed_signal);
1558 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1559 binding, g_settings_writable_binding_free);
1561 g_settings_binding_writable_changed (settings, binding->key, binding);
1565 * g_settings_unbind:
1566 * @object: the object
1567 * @property: the property whose binding is removed
1569 * Removes an existing binding for @property on @object.
1571 * Note that bindings are automatically removed when the
1572 * object is finalized, so it is rarely necessary to call this
1578 g_settings_unbind (gpointer object,
1579 const gchar *property)
1581 GQuark binding_quark;
1583 binding_quark = g_settings_binding_quark (property);
1584 g_object_set_qdata (object, binding_quark, NULL);
1588 * g_settings_get_string:
1589 * @settings: a #GSettings object
1590 * @key: the key to get the value for
1591 * @returns: a newly-allocated string
1593 * Gets the value that is stored at @key in @settings.
1595 * A convenience variant of g_settings_get() for strings.
1597 * It is a programmer error to pass a @key that isn't valid for
1598 * @settings or is not of type string.
1603 g_settings_get_string (GSettings *settings,
1609 value = g_settings_get_value (settings, key);
1610 result = g_variant_dup_string (value, NULL);
1611 g_variant_unref (value);
1617 * g_settings_set_string:
1618 * @settings: a #GSettings object
1619 * @key: the name of the key to set
1620 * @value: the value to set it to
1621 * @returns: %TRUE if setting the key succeeded,
1622 * %FALSE if the key was not writable
1624 * Sets @key in @settings to @value.
1626 * A convenience variant of g_settings_set() for strings.
1628 * It is a programmer error to pass a @key that isn't valid for
1629 * @settings or is not of type string.
1634 g_settings_set_string (GSettings *settings,
1638 return g_settings_set_value (settings, key, g_variant_new_string (value));
1642 * g_settings_get_int:
1643 * @settings: a #GSettings object
1644 * @key: the key to get the value for
1645 * @returns: an integer
1647 * Gets the value that is stored at @key in @settings.
1649 * A convenience variant of g_settings_get() for 32-bit integers.
1651 * It is a programmer error to pass a @key that isn't valid for
1652 * @settings or is not of type int32.
1657 g_settings_get_int (GSettings *settings,
1663 value = g_settings_get_value (settings, key);
1664 result = g_variant_get_int32 (value);
1665 g_variant_unref (value);
1671 * g_settings_set_int:
1672 * @settings: a #GSettings object
1673 * @key: the name of the key to set
1674 * @value: the value to set it to
1675 * @returns: %TRUE if setting the key succeeded,
1676 * %FALSE if the key was not writable
1678 * Sets @key in @settings to @value.
1680 * A convenience variant of g_settings_set() for 32-bit integers.
1682 * It is a programmer error to pass a @key that isn't valid for
1683 * @settings or is not of type int32.
1688 g_settings_set_int (GSettings *settings,
1692 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1696 * g_settings_get_double:
1697 * @settings: a #GSettings object
1698 * @key: the key to get the value for
1699 * @returns: a double
1701 * Gets the value that is stored at @key in @settings.
1703 * A convenience variant of g_settings_get() for doubles.
1705 * It is a programmer error to pass a @key that isn't valid for
1706 * @settings or is not of type double.
1711 g_settings_get_double (GSettings *settings,
1717 value = g_settings_get_value (settings, key);
1718 result = g_variant_get_double (value);
1719 g_variant_unref (value);
1725 * g_settings_set_double:
1726 * @settings: a #GSettings object
1727 * @key: the name of the key to set
1728 * @value: the value to set it to
1729 * @returns: %TRUE if setting the key succeeded,
1730 * %FALSE if the key was not writable
1732 * Sets @key in @settings to @value.
1734 * A convenience variant of g_settings_set() for doubles.
1736 * It is a programmer error to pass a @key that isn't valid for
1737 * @settings or is not of type double.
1742 g_settings_set_double (GSettings *settings,
1746 return g_settings_set_value (settings, key, g_variant_new_double (value));
1750 * g_settings_get_boolean:
1751 * @settings: a #GSettings object
1752 * @key: the key to get the value for
1753 * @returns: a boolean
1755 * Gets the value that is stored at @key in @settings.
1757 * A convenience variant of g_settings_get() for booleans.
1759 * It is a programmer error to pass a @key that isn't valid for
1760 * @settings or is not of type boolean.
1765 g_settings_get_boolean (GSettings *settings,
1771 value = g_settings_get_value (settings, key);
1772 result = g_variant_get_boolean (value);
1773 g_variant_unref (value);
1779 * g_settings_set_boolean:
1780 * @settings: a #GSettings object
1781 * @key: the name of the key to set
1782 * @value: the value to set it to
1783 * @returns: %TRUE if setting the key succeeded,
1784 * %FALSE if the key was not writable
1786 * Sets @key in @settings to @value.
1788 * A convenience variant of g_settings_set() for booleans.
1790 * It is a programmer error to pass a @key that isn't valid for
1791 * @settings or is not of type boolean.
1796 g_settings_set_boolean (GSettings *settings,
1800 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1804 * g_settings_get_strv:
1805 * @settings: a #GSettings object
1806 * @key: the key to get the value for
1807 * @returns: a newly-allocated, %NULL-terminated array of strings
1809 * Gets the value that is stored at @key in @settings.
1811 * A convenience variant of g_settings_get() for string arrays.
1813 * It is a programmer error to pass a @key that isn't valid for
1814 * @settings or is not of type 'string array'.
1819 g_settings_get_strv (GSettings *settings,
1825 value = g_settings_get_value (settings, key);
1826 result = g_variant_dup_strv (value, NULL);
1827 g_variant_unref (value);
1833 * g_settings_set_strv:
1834 * @settings: a #GSettings object
1835 * @key: the name of the key to set
1836 * @value: (allow-none): the value to set it to, or %NULL
1837 * @returns: %TRUE if setting the key succeeded,
1838 * %FALSE if the key was not writable
1840 * Sets @key in @settings to @value.
1842 * A convenience variant of g_settings_set() for string arrays. If
1843 * @value is %NULL, then @key is set to be the empty array.
1845 * It is a programmer error to pass a @key that isn't valid for
1846 * @settings or is not of type 'string array'.
1851 g_settings_set_strv (GSettings *settings,
1853 const gchar * const *value)
1858 array = g_variant_new_strv (value, -1);
1860 array = g_variant_new_strv (NULL, 0);
1862 return g_settings_set_value (settings, key, array);
1865 #define __G_SETTINGS_C__
1866 #include "gioaliasdef.c"