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 (user_data);
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 (user_data);
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,
269 const gchar * const *items,
273 GSettings *settings = G_SETTINGS (user_data);
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 (user_data);
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 (user_data);
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,
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,
376 g_settings_backend_subscribe (settings->priv->backend,
377 settings->priv->path);
381 g_settings_init (GSettings *settings)
383 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
387 settings->priv->main_context = g_main_context_get_thread_default ();
389 if (settings->priv->main_context == NULL)
390 settings->priv->main_context = g_main_context_default ();
392 g_main_context_ref (settings->priv->main_context);
397 * @settings: a #GSettings object
399 * Changes the #GSettings object into 'delay-apply' mode. In this
400 * mode, changes to @settings are not immediately propagated to the
401 * backend, but kept locally until g_settings_apply() is called.
406 g_settings_delay (GSettings *settings)
408 g_return_if_fail (G_IS_SETTINGS (settings));
410 if (settings->priv->delayed)
413 settings->priv->delayed =
414 g_delayed_settings_backend_new (settings->priv->backend, settings);
415 g_settings_backend_unwatch (settings->priv->backend, settings);
416 g_object_unref (settings->priv->backend);
418 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
419 g_settings_backend_watch (settings->priv->backend,
420 settings->priv->main_context,
421 settings_backend_changed,
422 settings_backend_path_changed,
423 settings_backend_keys_changed,
424 settings_backend_writable_changed,
425 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_unwatch (settings->priv->backend, settings);
550 g_settings_backend_unsubscribe (settings->priv->backend,
551 settings->priv->path);
552 g_main_context_unref (settings->priv->main_context);
553 g_object_unref (settings->priv->backend);
554 g_object_unref (settings->priv->schema);
555 g_free (settings->priv->schema_name);
556 g_free (settings->priv->path);
560 g_settings_class_init (GSettingsClass *class)
562 GObjectClass *object_class = G_OBJECT_CLASS (class);
564 class->writable_change_event = g_settings_real_writable_change_event;
565 class->change_event = g_settings_real_change_event;
567 object_class->set_property = g_settings_set_property;
568 object_class->get_property = g_settings_get_property;
569 object_class->constructed = g_settings_constructed;
570 object_class->finalize = g_settings_finalize;
572 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
575 * GSettings::changed:
576 * @settings: the object on which the signal was emitted
577 * @key: the name of the key that changed
579 * The "changed" signal is emitted when a key has potentially changed.
580 * You should call one of the g_settings_get() calls to check the new
583 * This signal supports detailed connections. You can connect to the
584 * detailed signal "changed::x" in order to only receive callbacks
585 * when key "x" changes.
587 g_settings_signals[SIGNAL_CHANGED] =
588 g_signal_new ("changed", G_TYPE_SETTINGS,
589 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
590 G_STRUCT_OFFSET (GSettingsClass, changed),
591 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
592 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
595 * GSettings::change-event:
596 * @settings: the object on which the signal was emitted
597 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
598 * @n_keys: the length of the @keys array, or 0
599 * @returns: %TRUE to stop other handlers from being invoked for the
600 * event. FALSE to propagate the event further.
602 * The "change-event" signal is emitted once per change event that
603 * affects this settings object. You should connect to this signal
604 * only if you are interested in viewing groups of changes before they
605 * are split out into multiple emissions of the "changed" signal.
606 * For most use cases it is more appropriate to use the "changed" signal.
608 * In the event that the change event applies to one or more specified
609 * keys, @keys will be an array of #GQuark of length @n_keys. In the
610 * event that the change event applies to the #GSettings object as a
611 * whole (ie: potentially every key has been changed) then @keys will
612 * be %NULL and @n_keys will be 0.
614 * The default handler for this signal invokes the "changed" signal
615 * for each affected key. If any other connected handler returns
616 * %TRUE then this default functionality will be supressed.
618 g_settings_signals[SIGNAL_CHANGE_EVENT] =
619 g_signal_new ("change-event", G_TYPE_SETTINGS,
621 G_STRUCT_OFFSET (GSettingsClass, change_event),
622 g_signal_accumulator_true_handled, NULL,
623 _gio_marshal_BOOL__POINTER_INT,
624 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
627 * GSettings::writable-changed:
628 * @settings: the object on which the signal was emitted
631 * The "writable-changed" signal is emitted when the writability of a
632 * key has potentially changed. You should call
633 * g_settings_is_writable() in order to determine the new status.
635 * This signal supports detailed connections. You can connect to the
636 * detailed signal "writable-changed::x" in order to only receive
637 * callbacks when the writability of "x" changes.
639 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
640 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
641 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
642 G_STRUCT_OFFSET (GSettingsClass, changed),
643 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
644 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
647 * GSettings::writable-change-event:
648 * @settings: the object on which the signal was emitted
649 * @key: the quark of the key, or 0
650 * @returns: %TRUE to stop other handlers from being invoked for the
651 * event. FALSE to propagate the event further.
653 * The "writable-change-event" signal is emitted once per writability
654 * change event that affects this settings object. You should connect
655 * to this signal if you are interested in viewing groups of changes
656 * before they are split out into multiple emissions of the
657 * "writable-changed" signal. For most use cases it is more
658 * appropriate to use the "writable-changed" signal.
660 * In the event that the writability change applies only to a single
661 * key, @key will be set to the #GQuark for that key. In the event
662 * that the writability change affects the entire settings object,
665 * The default handler for this signal invokes the "writable-changed"
666 * and "changed" signals for each affected key. This is done because
667 * changes in writability might also imply changes in value (if for
668 * example, a new mandatory setting is introduced). If any other
669 * connected handler returns %TRUE then this default functionality
672 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
673 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
675 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
676 g_signal_accumulator_true_handled, NULL,
677 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
682 * The name of the context that the settings are stored in.
684 g_object_class_install_property (object_class, PROP_CONTEXT,
685 g_param_spec_string ("context",
687 P_("The name of the context for this settings object"),
688 "", G_PARAM_CONSTRUCT_ONLY |
689 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
694 * The name of the schema that describes the types of keys
695 * for this #GSettings object.
697 g_object_class_install_property (object_class, PROP_SCHEMA,
698 g_param_spec_string ("schema",
700 P_("The name of the schema for this settings object"),
702 G_PARAM_CONSTRUCT_ONLY |
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
708 * The path within the backend where the settings are stored.
710 g_object_class_install_property (object_class, PROP_PATH,
711 g_param_spec_string ("path",
713 P_("The path within the backend where the settings are"),
715 G_PARAM_CONSTRUCT_ONLY |
716 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719 * GSettings:has-unapplied:
721 * If this property is %TRUE, the #GSettings object has outstanding
722 * changes that will be applied when g_settings_apply() is called.
724 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
725 g_param_spec_boolean ("has-unapplied",
726 P_("Has unapplied changes"),
727 P_("TRUE if there are outstanding changes to apply()"),
729 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
734 * g_settings_get_value:
735 * @settings: a #GSettings object
736 * @key: the key to get the value for
737 * @returns: a new #GVariant
739 * Gets the value that is stored in @settings for @key.
741 * It is a programmer error to give a @key that isn't valid for
747 g_settings_get_value (GSettings *settings,
750 const gchar *unparsed = NULL;
751 GVariant *value, *options;
752 const GVariantType *type;
753 gchar lc_char = '\0';
757 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
759 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
761 if G_UNLIKELY (sval == NULL)
762 g_error ("schema '%s' does not contain a key named '%s'",
763 settings->priv->schema_name, key);
765 path = g_strconcat (settings->priv->path, key, NULL);
766 type = g_variant_get_type (sval);
767 value = g_settings_backend_read (settings->priv->backend, path, type, FALSE);
774 GVariant *option_value;
776 g_variant_iter_init (&iter, options);
777 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
779 if (strcmp (option, "l10n") == 0)
780 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
782 g_warning ("unknown schema extension '%s'", option);
786 if (value && !g_variant_is_of_type (value, type))
788 g_variant_unref (value);
792 if (value == NULL && lc_char != '\0')
793 /* we will need to translate the schema default value */
795 const gchar *translated;
796 GError *error = NULL;
800 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
803 lc_category = LC_TIME;
805 lc_category = LC_MESSAGES;
807 translated = dcgettext (domain, unparsed, lc_category);
809 if (translated != unparsed)
810 /* it was translated, so we need to re-parse it */
812 value = g_variant_parse (g_variant_get_type (sval),
813 translated, NULL, NULL, &error);
817 g_warning ("Failed to parse translated string `%s' for "
818 "key `%s' in schema `%s': %s", unparsed, key,
819 settings->priv->schema_name, error->message);
820 g_warning ("Using untranslated default instead.");
821 g_error_free (error);
827 /* either translation failed or there was none to do.
828 * use the pre-compiled default.
830 value = g_variant_ref (sval);
832 g_variant_unref (sval);
838 * g_settings_set_value:
839 * @settings: a #GSettings object
840 * @key: the name of the key to set
841 * @value: a #GVariant of the correct type
842 * @returns: %TRUE if setting the key succeeded,
843 * %FALSE if the key was not writable
845 * Sets @key in @settings to @value.
847 * It is a programmer error to give a @key that isn't valid for
848 * @settings. It is a programmer error to give a @value of the
851 * If @value is floating then this function consumes the reference.
856 g_settings_set_value (GSettings *settings,
860 gboolean correct_type;
865 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
867 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
868 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
869 g_variant_unref (sval);
871 g_return_val_if_fail (correct_type, FALSE);
873 path = g_strconcat (settings->priv->path, key, NULL);
874 result = g_settings_backend_write (settings->priv->backend,
883 * @settings: a #GSettings object
884 * @key: the key to get the value for
885 * @format: a #GVariant format string
886 * @...: arguments as per @format
888 * Gets the value that is stored at @key in @settings.
890 * A convenience function that combines g_settings_get_value() with
893 * It is a programmer error to pass a @key that isn't valid for
894 * @settings or a @format_string that doesn't match the type of @key according
895 * to the schema of @settings.
900 g_settings_get (GSettings *settings,
908 value = g_settings_get_value (settings, key);
910 va_start (ap, format);
911 g_variant_get_va (value, format, NULL, &ap);
914 g_variant_unref (value);
919 * @settings: a #GSettings object
920 * @key: the name of the key to set
921 * @format: a #GVariant format string
922 * @...: arguments as per @format
923 * @returns: %TRUE if setting the key succeeded,
924 * %FALSE if the key was not writable
926 * Sets @key in @settings to @value.
928 * A convenience function that combines g_settings_set_value() with
931 * It is a programmer error to pass a @key that isn't valid for
932 * @settings or a @format that doesn't match the type of @key according
933 * to the schema of @settings.
938 g_settings_set (GSettings *settings,
946 va_start (ap, format);
947 value = g_variant_new_va (format, NULL, &ap);
950 return g_settings_set_value (settings, key, value);
954 * g_settings_is_writable:
955 * @settings: a #GSettings object
956 * @name: the name of a key
957 * @returns: %TRUE if the key @name is writable
959 * Finds out if a key can be written or not
964 g_settings_is_writable (GSettings *settings,
970 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
972 path = g_strconcat (settings->priv->path, name, NULL);
973 writable = g_settings_backend_get_writable (settings->priv->backend, path);
980 * g_settings_get_child:
981 * @settings: a #GSettings object
982 * @name: the name of the 'child' schema
983 * @returns: a 'child' settings object
985 * Creates a 'child' settings object which has a base path of
986 * <replaceable>base-path</replaceable>/@name", where
987 * <replaceable>base-path</replaceable> is the base path of @settings.
989 * The schema for the child settings object must have been declared
990 * in the schema of @settings using a <tag class="starttag">child</tag> element.
995 g_settings_get_child (GSettings *settings,
998 GVariant *child_schema;
1003 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1005 child_name = g_strconcat (name, "/", NULL);
1006 child_schema = g_settings_schema_get_value (settings->priv->schema,
1008 if (child_schema == NULL ||
1009 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
1010 g_error ("Schema '%s' has no child '%s'",
1011 settings->priv->schema_name, name);
1013 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1014 child = g_object_new (G_TYPE_SETTINGS,
1015 "schema", g_variant_get_string (child_schema, NULL),
1018 g_variant_unref (child_schema);
1019 g_free (child_path);
1020 g_free (child_name);
1027 * @schema: the name of the schema
1028 * @returns: a new #GSettings object
1030 * Creates a new #GSettings object with a given schema.
1032 * Signals on the newly created #GSettings object will be dispatched
1033 * via the thread-default #GMainContext in effect at the time of the
1034 * call to g_settings_new(). The new #GSettings will hold a reference
1035 * on the context. See g_main_context_push_thread_default().
1040 g_settings_new (const gchar *schema)
1042 return g_object_new (G_TYPE_SETTINGS,
1048 * g_settings_new_with_path:
1049 * @schema: the name of the schema
1050 * @path: the path to use
1051 * @returns: a new #GSettings object
1053 * Creates a new #GSettings object with a given schema and path.
1055 * You only need to do this if you want to directly create a settings
1056 * object with a schema that doesn't have a specified path of its own.
1057 * That's quite rare.
1059 * It is a programmer error to call this function for a schema that
1060 * has an explicitly specified path.
1065 g_settings_new_with_path (const gchar *schema,
1068 return g_object_new (G_TYPE_SETTINGS,
1075 * g_settings_new_with_context:
1076 * @schema: the name of the schema
1077 * @context: the context to use
1078 * @returns: a new #GSettings object
1080 * Creates a new #GSettings object with a given schema and context.
1082 * Creating settings objects with a context allow accessing settings
1083 * from a database other than the usual one. For example, it may make
1084 * sense to specify "defaults" in order to get a settings object that
1085 * modifies the system default settings instead of the settings for this
1088 * It is a programmer error to call this function for an unsupported
1089 * context. Use g_settings_supports_context() to determine if a context
1090 * is supported if you are unsure.
1095 g_settings_new_with_context (const gchar *schema,
1096 const gchar *context)
1098 return g_object_new (G_TYPE_SETTINGS,
1105 * g_settings_new_with_context_and_path:
1106 * @schema: the name of the schema
1107 * @path: the path to use
1108 * @returns: a new #GSettings object
1110 * Creates a new #GSettings object with a given schema, context and
1113 * This is a mix of g_settings_new_with_context() and
1114 * g_settings_new_with_path().
1119 g_settings_new_with_context_and_path (const gchar *schema,
1120 const gchar *context,
1123 return g_object_new (G_TYPE_SETTINGS,
1132 GSettings *settings;
1135 GSettingsBindGetMapping get_mapping;
1136 GSettingsBindSetMapping set_mapping;
1138 GDestroyNotify destroy;
1140 guint writable_handler_id;
1141 guint property_handler_id;
1142 const GParamSpec *property;
1143 guint key_handler_id;
1147 /* prevent recursion */
1152 g_settings_binding_free (gpointer data)
1154 GSettingsBinding *binding = data;
1156 g_assert (!binding->running);
1158 if (binding->writable_handler_id)
1159 g_signal_handler_disconnect (binding->settings,
1160 binding->writable_handler_id);
1162 if (binding->key_handler_id)
1163 g_signal_handler_disconnect (binding->settings,
1164 binding->key_handler_id);
1166 if (g_signal_handler_is_connected (binding->object,
1167 binding->property_handler_id))
1168 g_signal_handler_disconnect (binding->object,
1169 binding->property_handler_id);
1171 g_variant_type_free (binding->type);
1172 g_object_unref (binding->settings);
1174 if (binding->destroy)
1175 binding->destroy (binding->user_data);
1177 g_slice_free (GSettingsBinding, binding);
1181 g_settings_binding_quark (const char *property)
1186 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1187 quark = g_quark_from_string (tmp);
1194 g_settings_binding_key_changed (GSettings *settings,
1198 GSettingsBinding *binding = user_data;
1199 GValue value = { 0, };
1202 g_assert (settings == binding->settings);
1203 g_assert (key == binding->key);
1205 if (binding->running)
1208 binding->running = TRUE;
1210 g_value_init (&value, binding->property->value_type);
1211 variant = g_settings_get_value (settings, key);
1212 if (binding->get_mapping (&value, variant, binding->user_data))
1213 g_object_set_property (binding->object,
1214 binding->property->name,
1216 g_value_unset (&value);
1218 binding->running = FALSE;
1222 g_settings_binding_property_changed (GObject *object,
1223 const GParamSpec *pspec,
1226 GSettingsBinding *binding = user_data;
1227 GValue value = { 0, };
1230 g_assert (object == binding->object);
1231 g_assert (pspec == binding->property);
1233 if (binding->running)
1236 binding->running = TRUE;
1238 g_value_init (&value, pspec->value_type);
1239 g_object_get_property (object, pspec->name, &value);
1240 if ((variant = binding->set_mapping (&value, binding->type,
1241 binding->user_data)))
1243 g_settings_set_value (binding->settings,
1245 g_variant_ref_sink (variant));
1246 g_variant_unref (variant);
1248 g_value_unset (&value);
1250 binding->running = FALSE;
1255 * @settings: a #GSettings object
1256 * @key: the key to bind
1257 * @object: a #GObject
1258 * @property: the name of the property to bind
1259 * @flags: flags for the binding
1261 * Create a binding between the @key in the @settings object
1262 * and the property @property of @object.
1264 * The binding uses the default GIO mapping functions to map
1265 * between the settings and property values. These functions
1266 * handle booleans, numeric types and string types in a
1267 * straightforward way. Use g_settings_bind_with_mapping() if
1268 * you need a custom mapping, or map between types that are not
1269 * supported by the default mapping functions.
1271 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1272 * function also establishes a binding between the writability of
1273 * @key and the "sensitive" property of @object (if @object has
1274 * a boolean property by that name). See g_settings_bind_writable()
1275 * for more details about writable bindings.
1277 * Note that the lifecycle of the binding is tied to the object,
1278 * and that you can have only one binding per object property.
1279 * If you bind the same property twice on the same object, the second
1280 * binding overrides the first one.
1285 g_settings_bind (GSettings *settings,
1288 const gchar *property,
1289 GSettingsBindFlags flags)
1291 g_settings_bind_with_mapping (settings, key, object, property,
1292 flags, NULL, NULL, NULL, NULL);
1296 * g_settings_bind_with_mapping:
1297 * @settings: a #GSettings object
1298 * @key: the key to bind
1299 * @object: a #GObject
1300 * @property: the name of the property to bind
1301 * @flags: flags for the binding
1302 * @get_mapping: a function that gets called to convert values
1303 * from @settings to @object, or %NULL to use the default GIO mapping
1304 * @set_mapping: a function that gets called to convert values
1305 * from @object to @settings, or %NULL to use the default GIO mapping
1306 * @user_data: data that gets passed to @get_mapping and @set_mapping
1307 * @destroy: #GDestroyNotify function for @user_data
1309 * Create a binding between the @key in the @settings object
1310 * and the property @property of @object.
1312 * The binding uses the provided mapping functions to map between
1313 * settings and property values.
1315 * Note that the lifecycle of the binding is tied to the object,
1316 * and that you can have only one binding per object property.
1317 * If you bind the same property twice on the same object, the second
1318 * binding overrides the first one.
1323 g_settings_bind_with_mapping (GSettings *settings,
1326 const gchar *property,
1327 GSettingsBindFlags flags,
1328 GSettingsBindGetMapping get_mapping,
1329 GSettingsBindSetMapping set_mapping,
1331 GDestroyNotify destroy)
1333 GSettingsBinding *binding;
1334 GObjectClass *objectclass;
1335 gchar *detailed_signal;
1336 GQuark binding_quark;
1338 g_return_if_fail (G_IS_SETTINGS (settings));
1340 objectclass = G_OBJECT_GET_CLASS (object);
1342 binding = g_slice_new0 (GSettingsBinding);
1343 binding->settings = g_object_ref (settings);
1344 binding->object = object;
1345 binding->key = g_intern_string (key);
1346 binding->property = g_object_class_find_property (objectclass, property);
1347 binding->user_data = user_data;
1348 binding->destroy = destroy;
1349 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1350 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1352 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1353 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1355 if (binding->property == NULL)
1357 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1358 property, G_OBJECT_TYPE_NAME (object));
1362 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1364 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1365 property, G_OBJECT_TYPE_NAME (object));
1368 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1370 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1371 property, G_OBJECT_TYPE_NAME (object));
1378 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1379 binding->type = g_variant_type_copy (g_variant_get_type (value));
1381 g_variant_unref (value);
1384 if (binding->type == NULL)
1386 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1387 key, settings->priv->schema_name);
1391 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1392 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1393 !g_settings_mapping_is_compatible (binding->property->value_type,
1396 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1397 "'%s' which is not compatible with type '%s' of key '%s'"
1398 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1399 g_type_name (binding->property->value_type),
1400 g_variant_type_dup_string (binding->type), key,
1401 settings->priv->schema_name);
1405 if ((flags & G_SETTINGS_BIND_SET) &&
1406 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1408 GParamSpec *sensitive;
1410 sensitive = g_object_class_find_property (objectclass, "sensitive");
1412 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1413 (sensitive->flags & G_PARAM_WRITABLE))
1414 g_settings_bind_writable (settings, binding->key,
1415 object, "sensitive", FALSE);
1418 if (flags & G_SETTINGS_BIND_SET)
1420 detailed_signal = g_strdup_printf ("notify::%s", property);
1421 binding->property_handler_id =
1422 g_signal_connect (object, detailed_signal,
1423 G_CALLBACK (g_settings_binding_property_changed),
1425 g_free (detailed_signal);
1427 if (~flags & G_SETTINGS_BIND_GET)
1428 g_settings_binding_property_changed (object,
1433 if (flags & G_SETTINGS_BIND_GET)
1435 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1437 detailed_signal = g_strdup_printf ("changed::%s", key);
1438 binding->key_handler_id =
1439 g_signal_connect (settings, detailed_signal,
1440 G_CALLBACK (g_settings_binding_key_changed),
1442 g_free (detailed_signal);
1445 g_settings_binding_key_changed (settings, binding->key, binding);
1448 binding_quark = g_settings_binding_quark (property);
1449 g_object_set_qdata_full (object, binding_quark,
1450 binding, g_settings_binding_free);
1455 GSettings *settings;
1458 const gchar *property;
1461 } GSettingsWritableBinding;
1464 g_settings_writable_binding_free (gpointer data)
1466 GSettingsWritableBinding *binding = data;
1468 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1469 g_object_unref (binding->settings);
1470 g_slice_free (GSettingsWritableBinding, binding);
1474 g_settings_binding_writable_changed (GSettings *settings,
1478 GSettingsWritableBinding *binding = user_data;
1481 g_assert (settings == binding->settings);
1482 g_assert (key == binding->key);
1484 writable = g_settings_is_writable (settings, key);
1486 if (binding->inverted)
1487 writable = !writable;
1489 g_object_set (binding->object, binding->property, writable, NULL);
1493 * g_settings_bind_writable:
1494 * @settings: a #GSettings object
1495 * @key: the key to bind
1496 * @object: a #GObject
1497 * @property: the name of a boolean property to bind
1498 * @inverted: whether to 'invert' the value
1500 * Create a binding between the writability of @key in the
1501 * @settings object and the property @property of @object.
1502 * The property must be boolean; "sensitive" or "visible"
1503 * properties of widgets are the most likely candidates.
1505 * Writable bindings are always uni-directional; changes of the
1506 * writability of the setting will be propagated to the object
1507 * property, not the other way.
1509 * When the @inverted argument is %TRUE, the binding inverts the
1510 * value as it passes from the setting to the object, i.e. @property
1511 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1514 * Note that the lifecycle of the binding is tied to the object,
1515 * and that you can have only one binding per object property.
1516 * If you bind the same property twice on the same object, the second
1517 * binding overrides the first one.
1522 g_settings_bind_writable (GSettings *settings,
1525 const gchar *property,
1528 GSettingsWritableBinding *binding;
1529 gchar *detailed_signal;
1532 g_return_if_fail (G_IS_SETTINGS (settings));
1534 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1537 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1538 property, G_OBJECT_TYPE_NAME (object));
1541 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1543 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1544 property, G_OBJECT_TYPE_NAME (object));
1548 binding = g_slice_new (GSettingsWritableBinding);
1549 binding->settings = g_object_ref (settings);
1550 binding->object = object;
1551 binding->key = g_intern_string (key);
1552 binding->property = g_intern_string (property);
1553 binding->inverted = inverted;
1555 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1556 binding->handler_id =
1557 g_signal_connect (settings, detailed_signal,
1558 G_CALLBACK (g_settings_binding_writable_changed),
1560 g_free (detailed_signal);
1562 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1563 binding, g_settings_writable_binding_free);
1565 g_settings_binding_writable_changed (settings, binding->key, binding);
1569 * g_settings_unbind:
1570 * @object: the object
1571 * @property: the property whose binding is removed
1573 * Removes an existing binding for @property on @object.
1575 * Note that bindings are automatically removed when the
1576 * object is finalized, so it is rarely necessary to call this
1582 g_settings_unbind (gpointer object,
1583 const gchar *property)
1585 GQuark binding_quark;
1587 binding_quark = g_settings_binding_quark (property);
1588 g_object_set_qdata (object, binding_quark, NULL);
1592 * g_settings_get_string:
1593 * @settings: a #GSettings object
1594 * @key: the key to get the value for
1595 * @returns: a newly-allocated string
1597 * Gets the value that is stored at @key in @settings.
1599 * A convenience variant of g_settings_get() for strings.
1601 * It is a programmer error to pass a @key that isn't valid for
1602 * @settings or is not of type string.
1607 g_settings_get_string (GSettings *settings,
1613 value = g_settings_get_value (settings, key);
1614 result = g_variant_dup_string (value, NULL);
1615 g_variant_unref (value);
1621 * g_settings_set_string:
1622 * @settings: a #GSettings object
1623 * @key: the name of the key to set
1624 * @value: the value to set it to
1625 * @returns: %TRUE if setting the key succeeded,
1626 * %FALSE if the key was not writable
1628 * Sets @key in @settings to @value.
1630 * A convenience variant of g_settings_set() for strings.
1632 * It is a programmer error to pass a @key that isn't valid for
1633 * @settings or is not of type string.
1638 g_settings_set_string (GSettings *settings,
1642 return g_settings_set_value (settings, key, g_variant_new_string (value));
1646 * g_settings_get_int:
1647 * @settings: a #GSettings object
1648 * @key: the key to get the value for
1649 * @returns: an integer
1651 * Gets the value that is stored at @key in @settings.
1653 * A convenience variant of g_settings_get() for 32-bit integers.
1655 * It is a programmer error to pass a @key that isn't valid for
1656 * @settings or is not of type int32.
1661 g_settings_get_int (GSettings *settings,
1667 value = g_settings_get_value (settings, key);
1668 result = g_variant_get_int32 (value);
1669 g_variant_unref (value);
1675 * g_settings_set_int:
1676 * @settings: a #GSettings object
1677 * @key: the name of the key to set
1678 * @value: the value to set it to
1679 * @returns: %TRUE if setting the key succeeded,
1680 * %FALSE if the key was not writable
1682 * Sets @key in @settings to @value.
1684 * A convenience variant of g_settings_set() for 32-bit integers.
1686 * It is a programmer error to pass a @key that isn't valid for
1687 * @settings or is not of type int32.
1692 g_settings_set_int (GSettings *settings,
1696 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1700 * g_settings_get_double:
1701 * @settings: a #GSettings object
1702 * @key: the key to get the value for
1703 * @returns: a double
1705 * Gets the value that is stored at @key in @settings.
1707 * A convenience variant of g_settings_get() for doubles.
1709 * It is a programmer error to pass a @key that isn't valid for
1710 * @settings or is not of type double.
1715 g_settings_get_double (GSettings *settings,
1721 value = g_settings_get_value (settings, key);
1722 result = g_variant_get_double (value);
1723 g_variant_unref (value);
1729 * g_settings_set_double:
1730 * @settings: a #GSettings object
1731 * @key: the name of the key to set
1732 * @value: the value to set it to
1733 * @returns: %TRUE if setting the key succeeded,
1734 * %FALSE if the key was not writable
1736 * Sets @key in @settings to @value.
1738 * A convenience variant of g_settings_set() for doubles.
1740 * It is a programmer error to pass a @key that isn't valid for
1741 * @settings or is not of type double.
1746 g_settings_set_double (GSettings *settings,
1750 return g_settings_set_value (settings, key, g_variant_new_double (value));
1754 * g_settings_get_boolean:
1755 * @settings: a #GSettings object
1756 * @key: the key to get the value for
1757 * @returns: a boolean
1759 * Gets the value that is stored at @key in @settings.
1761 * A convenience variant of g_settings_get() for booleans.
1763 * It is a programmer error to pass a @key that isn't valid for
1764 * @settings or is not of type boolean.
1769 g_settings_get_boolean (GSettings *settings,
1775 value = g_settings_get_value (settings, key);
1776 result = g_variant_get_boolean (value);
1777 g_variant_unref (value);
1783 * g_settings_set_boolean:
1784 * @settings: a #GSettings object
1785 * @key: the name of the key to set
1786 * @value: the value to set it to
1787 * @returns: %TRUE if setting the key succeeded,
1788 * %FALSE if the key was not writable
1790 * Sets @key in @settings to @value.
1792 * A convenience variant of g_settings_set() for booleans.
1794 * It is a programmer error to pass a @key that isn't valid for
1795 * @settings or is not of type boolean.
1800 g_settings_set_boolean (GSettings *settings,
1804 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1808 * g_settings_get_strv:
1809 * @settings: a #GSettings object
1810 * @key: the key to get the value for
1811 * @length: return location for the length of the result, or %NULL
1812 * @returns: a newly-allocated, %NULL-terminated array of strings
1814 * Gets the value that is stored at @key in @settings.
1816 * A convenience variant of g_settings_get() for string arrays.
1818 * It is a programmer error to pass a @key that isn't valid for
1819 * @settings or is not of type 'string array'.
1824 g_settings_get_strv (GSettings *settings,
1831 value = g_settings_get_value (settings, key);
1832 result = g_variant_dup_strv (value, length);
1833 g_variant_unref (value);
1839 * g_settings_set_strv:
1840 * @settings: a #GSettings object
1841 * @key: the name of the key to set
1842 * @value: the value to set it to
1843 * @length: the length of the @value array, or -1
1844 * @returns: %TRUE if setting the key succeeded,
1845 * %FALSE if the key was not writable
1847 * Sets @key in @settings to @value.
1849 * A convenience variant of g_settings_set() for string arrays.
1851 * It is a programmer error to pass a @key that isn't valid for
1852 * @settings or is not of type 'string array'.
1857 g_settings_set_strv (GSettings *settings,
1859 const gchar * const *value,
1862 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1865 #define __G_SETTINGS_C__
1866 #include "gioaliasdef.c"