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>
28 #include "gsettings.h"
30 #include "gdelayedsettingsbackend.h"
31 #include "gsettingsbackendinternal.h"
32 #include "gsettings-mapping.h"
33 #include "gsettingsschema-internal.h"
40 * @short_description: High-level API for application settings
42 * The #GSettings class provides a convenient API for storing and retrieving
43 * application settings.
45 * Reads and writes can be considered to be non-blocking. Reading
46 * settings with #GSettings is typically extremely fast: on
47 * approximately the same order of magnitude (but slower than) a
48 * #GHashTable lookup. Writing settings is also extremely fast in terms
49 * of time to return to your application, but can be extremely expensive
50 * for other threads and other processes. Many settings backends
51 * (including dconf) have lazy initialisation which means in the common
52 * case of the user using their computer without modifying any settings
53 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
54 * even need to be started in this case. For this reason, you should
55 * only ever modify #GSettings keys in response to explicit user action.
56 * Particular care should be paid to ensure that modifications are not
57 * made during startup -- for example, when setting the initial value
58 * of preferences widgets. The built-in g_settings_bind() functionality
59 * is careful not to write settings in response to notify signals as a
60 * result of modifications that it makes to widgets.
62 * When creating a GSettings instance, you have to specify a schema
63 * that describes the keys in your settings and their types and default
64 * values, as well as some other information.
66 * Normally, a schema has as fixed path that determines where the settings
67 * are stored in the conceptual global tree of settings. However, schemas
68 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
69 * useful e.g. when the schema describes an 'account', and you want to be
70 * able to store a arbitrary number of accounts.
72 * Paths must start with and end with a forward slash character ('/')
73 * and must not contain two sequential slash characters. Paths should
74 * be chosen based on a domain name associated with the program or
75 * library to which the settings belong. Examples of paths are
76 * "/org/gtk/settings/file-chooser/" and "/ca/desrt/dconf-editor/".
77 * Paths should not start with "/apps/", "/desktop/" or "/system/" as
78 * they often did in GConf.
80 * Unlike other configuration systems (like GConf), GSettings does not
81 * restrict keys to basic types like strings and numbers. GSettings stores
82 * values as #GVariant, and allows any #GVariantType for keys. Key names
83 * are restricted to lowercase characters, numbers and '-'. Furthermore,
84 * the names must begin with a lowercase character, must not end
85 * with a '-', and must not contain consecutive dashes.
87 * Similar to GConf, the default values in GSettings schemas can be
88 * localized, but the localized values are stored in gettext catalogs
89 * and looked up with the domain that is specified in the
90 * <tag class="attribute">gettext-domain</tag> attribute of the
91 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
92 * elements and the category that is specified in the l10n attribute of the
93 * <tag class="starttag">key</tag> element.
95 * GSettings uses schemas in a compact binary form that is created
96 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
97 * utility. The input is a schema description in an XML format that can be
98 * described by the following DTD:
99 * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/gschema.dtd"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
101 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
103 * At runtime, schemas are identified by their id (as specified
104 * in the <tag class="attribute">id</tag> attribute of the
105 * <tag class="starttag">schema</tag> element). The
106 * convention for schema ids is to use a dotted name, similar in
107 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
108 * if the settings are for a specific service that owns a D-Bus bus name,
109 * the D-Bus bus name and schema id should match. For schemas which deal
110 * with settings not associated with one named application, the id should
111 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
113 * In addition to #GVariant types, keys can have types that have enumerated
114 * types. These can be described by a <tag class="starttag">choice</tag>,
115 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
116 * <xref linkend="schema-enumerated"/>. The underlying type of
117 * such a key is string, but you can use g_settings_get_enum(),
118 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
119 * access the numeric values corresponding to the string value of enum
122 * <example id="schema-default-values"><title>Default values</title>
123 * <programlisting><![CDATA[
125 * <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
127 * <key name="greeting" type="s">
128 * <default l10n="messages">"Hello, earthlings"</default>
129 * <summary>A greeting</summary>
131 * Greeting of the invading martians
135 * <key name="box" type="(ii)">
136 * <default>(20,30)</default>
141 * ]]></programlisting></example>
143 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
144 * <programlisting><![CDATA[
147 * <enum id="org.gtk.Test.myenum">
148 * <value nick="first" value="1"/>
149 * <value nick="second" value="2"/>
152 * <flags id="org.gtk.Test.myflags">
153 * <value nick="flag1" value="1"/>
154 * <value nick="flag2" value="2"/>
155 * <value nick="flag3" value="4"/>
158 * <schema id="org.gtk.Test">
160 * <key name="key-with-range" type="i">
161 * <range min="1" max="100"/>
162 * <default>10</default>
165 * <key name="key-with-choices" type="s">
167 * <choice value='Elisabeth'/>
168 * <choice value='Annabeth'/>
169 * <choice value='Joe'/>
172 * <alias value='Anna' target='Annabeth'/>
173 * <alias value='Beth' target='Elisabeth'/>
175 * <default>'Joe'</default>
178 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
179 * <default>'first'</default>
182 * <key name='flags-key' flags='org.gtk.Test.myflags'>
183 * <default>["flag1",flag2"]</default>
187 * ]]></programlisting></example>
190 * <title>Vendor overrides</title>
192 * Default values are defined in the schemas that get installed by
193 * an application. Sometimes, it is necessary for a vendor or distributor
194 * to adjust these defaults. Since patching the XML source for the schema
195 * is inconvenient and error-prone,
196 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
197 * so-called 'vendor override' files. These are keyfiles in the same
198 * directory as the XML schema sources which can override default values.
199 * The schema id serves as the group name in the key file, and the values
200 * are expected in serialized GVariant form, as in the following example:
201 * <informalexample><programlisting>
205 * </programlisting></informalexample>
208 * glib-compile-schemas expects schema files to have the extension
209 * <filename>.gschema.override</filename>
214 * <title>Binding</title>
216 * A very convenient feature of GSettings lets you bind #GObject properties
217 * directly to settings, using g_settings_bind(). Once a GObject property
218 * has been bound to a setting, changes on either side are automatically
219 * propagated to the other side. GSettings handles details like
220 * mapping between GObject and GVariant types, and preventing infinite
224 * This makes it very easy to hook up a preferences dialog to the
225 * underlying settings. To make this even more convenient, GSettings
226 * looks for a boolean property with the name "sensitivity" and
227 * automatically binds it to the writability of the bound setting.
228 * If this 'magic' gets in the way, it can be suppressed with the
229 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
234 struct _GSettingsPrivate
236 /* where the signals go... */
237 GMainContext
*main_context
;
239 GSettingsBackend
*backend
;
240 GSettingsSchema
*schema
;
243 GDelayedSettingsBackend
*delayed
;
259 SIGNAL_WRITABLE_CHANGE_EVENT
,
260 SIGNAL_WRITABLE_CHANGED
,
266 static guint g_settings_signals
[N_SIGNALS
];
268 G_DEFINE_TYPE_WITH_PRIVATE (GSettings
, g_settings
, G_TYPE_OBJECT
)
272 g_settings_real_change_event (GSettings
*settings
,
279 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
281 for (i
= 0; i
< n_keys
; i
++)
283 const gchar
*key
= g_quark_to_string (keys
[i
]);
285 if (g_str_has_suffix (key
, "/"))
288 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGED
], keys
[i
], key
);
295 g_settings_real_writable_change_event (GSettings
*settings
,
298 const GQuark
*keys
= &key
;
303 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
305 for (i
= 0; i
< n_keys
; i
++)
307 const gchar
*key
= g_quark_to_string (keys
[i
]);
309 if (g_str_has_suffix (key
, "/"))
312 g_signal_emit (settings
, g_settings_signals
[SIGNAL_WRITABLE_CHANGED
], keys
[i
], key
);
319 settings_backend_changed (GObject
*target
,
320 GSettingsBackend
*backend
,
324 GSettings
*settings
= G_SETTINGS (target
);
325 gboolean ignore_this
;
328 /* We used to assert here:
330 * settings->priv->backend == backend
332 * but it could be the case that a notification is queued for delivery
333 * while someone calls g_settings_delay() (which changes the backend).
335 * Since the delay backend would just pass that straight through
336 * anyway, it doesn't make sense to try to detect this case.
337 * Therefore, we just accept it.
340 for (i
= 0; key
[i
] == settings
->priv
->path
[i
]; i
++);
342 if (settings
->priv
->path
[i
] == '\0' &&
343 g_settings_schema_has_key (settings
->priv
->schema
, key
+ i
))
347 quark
= g_quark_from_string (key
+ i
);
348 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGE_EVENT
],
349 0, &quark
, 1, &ignore_this
);
354 settings_backend_path_changed (GObject
*target
,
355 GSettingsBackend
*backend
,
359 GSettings
*settings
= G_SETTINGS (target
);
360 gboolean ignore_this
;
362 if (g_str_has_prefix (settings
->priv
->path
, path
))
363 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGE_EVENT
],
364 0, NULL
, 0, &ignore_this
);
368 settings_backend_keys_changed (GObject
*target
,
369 GSettingsBackend
*backend
,
371 const gchar
* const *items
,
374 GSettings
*settings
= G_SETTINGS (target
);
375 gboolean ignore_this
;
378 for (i
= 0; settings
->priv
->path
[i
] &&
379 settings
->priv
->path
[i
] == path
[i
]; i
++);
386 for (j
= 0; items
[j
]; j
++)
388 const gchar
*item
= items
[j
];
391 for (k
= 0; item
[k
] == settings
->priv
->path
[i
+ k
]; k
++);
393 if (settings
->priv
->path
[i
+ k
] == '\0' &&
394 g_settings_schema_has_key (settings
->priv
->schema
, item
+ k
))
395 quarks
[l
++] = g_quark_from_string (item
+ k
);
397 /* "256 quarks ought to be enough for anybody!"
398 * If this bites you, I'm sorry. Please file a bug.
404 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGE_EVENT
],
405 0, quarks
, l
, &ignore_this
);
410 settings_backend_writable_changed (GObject
*target
,
411 GSettingsBackend
*backend
,
414 GSettings
*settings
= G_SETTINGS (target
);
415 gboolean ignore_this
;
418 for (i
= 0; key
[i
] == settings
->priv
->path
[i
]; i
++);
420 if (settings
->priv
->path
[i
] == '\0' &&
421 g_settings_schema_has_key (settings
->priv
->schema
, key
+ i
))
422 g_signal_emit (settings
, g_settings_signals
[SIGNAL_WRITABLE_CHANGE_EVENT
],
423 0, g_quark_from_string (key
+ i
), &ignore_this
);
427 settings_backend_path_writable_changed (GObject
*target
,
428 GSettingsBackend
*backend
,
431 GSettings
*settings
= G_SETTINGS (target
);
432 gboolean ignore_this
;
434 if (g_str_has_prefix (settings
->priv
->path
, path
))
435 g_signal_emit (settings
, g_settings_signals
[SIGNAL_WRITABLE_CHANGE_EVENT
],
436 0, (GQuark
) 0, &ignore_this
);
439 /* Properties, Construction, Destruction {{{1 */
441 g_settings_set_property (GObject
*object
,
446 GSettings
*settings
= G_SETTINGS (object
);
452 GSettingsSchema
*schema
;
454 schema
= g_value_dup_boxed (value
);
456 /* we receive a set_property() call for "settings-schema" even
457 * if it was not specified (ie: with NULL value). ->schema
458 * could already be set at this point (ie: via "schema-id").
459 * check for NULL to avoid clobbering the existing value.
463 g_assert (settings
->priv
->schema
== NULL
);
464 settings
->priv
->schema
= schema
;
471 const gchar
*schema_id
;
473 schema_id
= g_value_get_string (value
);
475 /* we receive a set_property() call for both "schema" and
476 * "schema-id", even if they are not set. Hopefully only one of
479 if (schema_id
!= NULL
)
481 GSettingsSchemaSource
*default_source
;
483 g_assert (settings
->priv
->schema
== NULL
);
484 default_source
= g_settings_schema_source_get_default ();
486 if (default_source
== NULL
)
487 g_error ("No GSettings schemas are installed on the system");
489 settings
->priv
->schema
= g_settings_schema_source_lookup (default_source
, schema_id
, TRUE
);
491 if (settings
->priv
->schema
== NULL
)
492 g_error ("Settings schema '%s' is not installed\n", schema_id
);
498 settings
->priv
->path
= g_value_dup_string (value
);
502 settings
->priv
->backend
= g_value_dup_object (value
);
506 g_assert_not_reached ();
511 g_settings_get_property (GObject
*object
,
516 GSettings
*settings
= G_SETTINGS (object
);
521 g_value_set_boxed (value
, settings
->priv
->schema
);
525 g_value_set_string (value
, g_settings_schema_get_id (settings
->priv
->schema
));
529 g_value_set_object (value
, settings
->priv
->backend
);
533 g_value_set_string (value
, settings
->priv
->path
);
536 case PROP_HAS_UNAPPLIED
:
537 g_value_set_boolean (value
, g_settings_get_has_unapplied (settings
));
540 case PROP_DELAY_APPLY
:
541 g_value_set_boolean (value
, settings
->priv
->delayed
!= NULL
);
545 g_assert_not_reached ();
549 static const GSettingsListenerVTable listener_vtable
= {
550 settings_backend_changed
,
551 settings_backend_path_changed
,
552 settings_backend_keys_changed
,
553 settings_backend_writable_changed
,
554 settings_backend_path_writable_changed
558 g_settings_constructed (GObject
*object
)
560 GSettings
*settings
= G_SETTINGS (object
);
561 const gchar
*schema_path
;
563 schema_path
= g_settings_schema_get_path (settings
->priv
->schema
);
565 if (settings
->priv
->path
&& schema_path
&& strcmp (settings
->priv
->path
, schema_path
) != 0)
566 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
567 g_settings_schema_get_id (settings
->priv
->schema
), settings
->priv
->path
, schema_path
);
569 if (settings
->priv
->path
== NULL
)
571 if (schema_path
== NULL
)
572 g_error ("attempting to create schema '%s' without a path",
573 g_settings_schema_get_id (settings
->priv
->schema
));
575 settings
->priv
->path
= g_strdup (schema_path
);
578 if (settings
->priv
->backend
== NULL
)
579 settings
->priv
->backend
= g_settings_backend_get_default ();
581 g_settings_backend_watch (settings
->priv
->backend
,
582 &listener_vtable
, G_OBJECT (settings
),
583 settings
->priv
->main_context
);
584 g_settings_backend_subscribe (settings
->priv
->backend
,
585 settings
->priv
->path
);
589 g_settings_finalize (GObject
*object
)
591 GSettings
*settings
= G_SETTINGS (object
);
593 g_settings_backend_unsubscribe (settings
->priv
->backend
,
594 settings
->priv
->path
);
595 g_main_context_unref (settings
->priv
->main_context
);
596 g_object_unref (settings
->priv
->backend
);
597 g_settings_schema_unref (settings
->priv
->schema
);
598 g_free (settings
->priv
->path
);
600 G_OBJECT_CLASS (g_settings_parent_class
)->finalize (object
);
604 g_settings_init (GSettings
*settings
)
606 settings
->priv
= g_settings_get_instance_private (settings
);
607 settings
->priv
->main_context
= g_main_context_ref_thread_default ();
611 g_settings_class_init (GSettingsClass
*class)
613 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
615 class->writable_change_event
= g_settings_real_writable_change_event
;
616 class->change_event
= g_settings_real_change_event
;
618 object_class
->set_property
= g_settings_set_property
;
619 object_class
->get_property
= g_settings_get_property
;
620 object_class
->constructed
= g_settings_constructed
;
621 object_class
->finalize
= g_settings_finalize
;
624 * GSettings::changed:
625 * @settings: the object on which the signal was emitted
626 * @key: the name of the key that changed
628 * The "changed" signal is emitted when a key has potentially changed.
629 * You should call one of the g_settings_get() calls to check the new
632 * This signal supports detailed connections. You can connect to the
633 * detailed signal "changed::x" in order to only receive callbacks
634 * when key "x" changes.
636 g_settings_signals
[SIGNAL_CHANGED
] =
637 g_signal_new ("changed", G_TYPE_SETTINGS
,
638 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
639 G_STRUCT_OFFSET (GSettingsClass
, changed
),
640 NULL
, NULL
, g_cclosure_marshal_VOID__STRING
, G_TYPE_NONE
,
641 1, G_TYPE_STRING
| G_SIGNAL_TYPE_STATIC_SCOPE
);
644 * GSettings::change-event:
645 * @settings: the object on which the signal was emitted
646 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
647 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
648 * @n_keys: the length of the @keys array, or 0
650 * The "change-event" signal is emitted once per change event that
651 * affects this settings object. You should connect to this signal
652 * only if you are interested in viewing groups of changes before they
653 * are split out into multiple emissions of the "changed" signal.
654 * For most use cases it is more appropriate to use the "changed" signal.
656 * In the event that the change event applies to one or more specified
657 * keys, @keys will be an array of #GQuark of length @n_keys. In the
658 * event that the change event applies to the #GSettings object as a
659 * whole (ie: potentially every key has been changed) then @keys will
660 * be %NULL and @n_keys will be 0.
662 * The default handler for this signal invokes the "changed" signal
663 * for each affected key. If any other connected handler returns
664 * %TRUE then this default functionality will be suppressed.
666 * Returns: %TRUE to stop other handlers from being invoked for the
667 * event. FALSE to propagate the event further.
669 g_settings_signals
[SIGNAL_CHANGE_EVENT
] =
670 g_signal_new ("change-event", G_TYPE_SETTINGS
,
672 G_STRUCT_OFFSET (GSettingsClass
, change_event
),
673 g_signal_accumulator_true_handled
, NULL
,
675 G_TYPE_BOOLEAN
, 2, G_TYPE_POINTER
, G_TYPE_INT
);
678 * GSettings::writable-changed:
679 * @settings: the object on which the signal was emitted
682 * The "writable-changed" signal is emitted when the writability of a
683 * key has potentially changed. You should call
684 * g_settings_is_writable() in order to determine the new status.
686 * This signal supports detailed connections. You can connect to the
687 * detailed signal "writable-changed::x" in order to only receive
688 * callbacks when the writability of "x" changes.
690 g_settings_signals
[SIGNAL_WRITABLE_CHANGED
] =
691 g_signal_new ("writable-changed", G_TYPE_SETTINGS
,
692 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
693 G_STRUCT_OFFSET (GSettingsClass
, writable_changed
),
694 NULL
, NULL
, g_cclosure_marshal_VOID__STRING
, G_TYPE_NONE
,
695 1, G_TYPE_STRING
| G_SIGNAL_TYPE_STATIC_SCOPE
);
698 * GSettings::writable-change-event:
699 * @settings: the object on which the signal was emitted
700 * @key: the quark of the key, or 0
702 * The "writable-change-event" signal is emitted once per writability
703 * change event that affects this settings object. You should connect
704 * to this signal if you are interested in viewing groups of changes
705 * before they are split out into multiple emissions of the
706 * "writable-changed" signal. For most use cases it is more
707 * appropriate to use the "writable-changed" signal.
709 * In the event that the writability change applies only to a single
710 * key, @key will be set to the #GQuark for that key. In the event
711 * that the writability change affects the entire settings object,
714 * The default handler for this signal invokes the "writable-changed"
715 * and "changed" signals for each affected key. This is done because
716 * changes in writability might also imply changes in value (if for
717 * example, a new mandatory setting is introduced). If any other
718 * connected handler returns %TRUE then this default functionality
719 * will be suppressed.
721 * Returns: %TRUE to stop other handlers from being invoked for the
722 * event. FALSE to propagate the event further.
724 g_settings_signals
[SIGNAL_WRITABLE_CHANGE_EVENT
] =
725 g_signal_new ("writable-change-event", G_TYPE_SETTINGS
,
727 G_STRUCT_OFFSET (GSettingsClass
, writable_change_event
),
728 g_signal_accumulator_true_handled
, NULL
,
729 NULL
, G_TYPE_BOOLEAN
, 1, G_TYPE_UINT
);
734 * The name of the context that the settings are stored in.
736 g_object_class_install_property (object_class
, PROP_BACKEND
,
737 g_param_spec_object ("backend",
738 P_("GSettingsBackend"),
739 P_("The GSettingsBackend for this settings object"),
740 G_TYPE_SETTINGS_BACKEND
, G_PARAM_CONSTRUCT_ONLY
|
741 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
744 * GSettings:settings-schema:
746 * The #GSettingsSchema describing the types of keys for this
749 * Ideally, this property would be called 'schema'. #GSettingsSchema
750 * has only existed since version 2.32, however, and before then the
751 * 'schema' property was used to refer to the ID of the schema rather
752 * than the schema itself. Take care.
754 g_object_class_install_property (object_class
, PROP_SCHEMA
,
755 g_param_spec_boxed ("settings-schema",
757 P_("The GSettingsSchema for this settings object"),
758 G_TYPE_SETTINGS_SCHEMA
,
759 G_PARAM_CONSTRUCT_ONLY
|
760 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
765 * The name of the schema that describes the types of keys
766 * for this #GSettings object.
768 * The type of this property is *not* #GSettingsSchema.
769 * #GSettingsSchema has only existed since version 2.32 and
770 * unfortunately this name was used in previous versions to refer to
771 * the schema ID rather than the schema itself. Take care to use the
772 * 'settings-schema' property if you wish to pass in a
775 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
776 * version, this property may instead refer to a #GSettingsSchema.
778 g_object_class_install_property (object_class
, PROP_SCHEMA_ID
,
779 g_param_spec_string ("schema",
781 P_("The name of the schema for this settings object"),
783 G_PARAM_CONSTRUCT_ONLY
|
784 G_PARAM_DEPRECATED
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
787 * GSettings:schema-id:
789 * The name of the schema that describes the types of keys
790 * for this #GSettings object.
792 g_object_class_install_property (object_class
, PROP_SCHEMA_ID
,
793 g_param_spec_string ("schema-id",
795 P_("The name of the schema for this settings object"),
797 G_PARAM_CONSTRUCT_ONLY
|
798 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
803 * The path within the backend where the settings are stored.
805 g_object_class_install_property (object_class
, PROP_PATH
,
806 g_param_spec_string ("path",
808 P_("The path within the backend where the settings are"),
810 G_PARAM_CONSTRUCT_ONLY
|
811 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
814 * GSettings:has-unapplied:
816 * If this property is %TRUE, the #GSettings object has outstanding
817 * changes that will be applied when g_settings_apply() is called.
819 g_object_class_install_property (object_class
, PROP_HAS_UNAPPLIED
,
820 g_param_spec_boolean ("has-unapplied",
821 P_("Has unapplied changes"),
822 P_("TRUE if there are outstanding changes to apply()"),
824 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
));
827 * GSettings:delay-apply:
829 * Whether the #GSettings object is in 'delay-apply' mode. See
830 * g_settings_delay() for details.
834 g_object_class_install_property (object_class
, PROP_DELAY_APPLY
,
835 g_param_spec_boolean ("delay-apply",
836 P_("Delay-apply mode"),
837 P_("Whether this settings object is in 'delay-apply' mode"),
839 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
));
842 /* Construction (new, new_with_path, etc.) {{{1 */
845 * @schema_id: the id of the schema
847 * Creates a new #GSettings object with the schema specified by
850 * Signals on the newly created #GSettings object will be dispatched
851 * via the thread-default #GMainContext in effect at the time of the
852 * call to g_settings_new(). The new #GSettings will hold a reference
853 * on the context. See g_main_context_push_thread_default().
855 * Returns: a new #GSettings object
860 g_settings_new (const gchar
*schema_id
)
862 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
864 return g_object_new (G_TYPE_SETTINGS
,
865 "schema-id", schema_id
,
870 path_is_valid (const gchar
*path
)
878 if (!g_str_has_suffix (path
, "/"))
881 return strstr (path
, "//") == NULL
;
885 * g_settings_new_with_path:
886 * @schema_id: the id of the schema
887 * @path: the path to use
889 * Creates a new #GSettings object with the relocatable schema specified
890 * by @schema_id and a given path.
892 * You only need to do this if you want to directly create a settings
893 * object with a schema that doesn't have a specified path of its own.
896 * It is a programmer error to call this function for a schema that
897 * has an explicitly specified path.
899 * It is a programmer error if @path is not a valid path. A valid path
900 * begins and ends with '/' and does not contain two consecutive '/'
903 * Returns: a new #GSettings object
908 g_settings_new_with_path (const gchar
*schema_id
,
911 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
912 g_return_val_if_fail (path_is_valid (path
), NULL
);
914 return g_object_new (G_TYPE_SETTINGS
,
915 "schema-id", schema_id
,
921 * g_settings_new_with_backend:
922 * @schema_id: the id of the schema
923 * @backend: the #GSettingsBackend to use
925 * Creates a new #GSettings object with the schema specified by
926 * @schema_id and a given #GSettingsBackend.
928 * Creating a #GSettings object with a different backend allows accessing
929 * settings from a database other than the usual one. For example, it may make
930 * sense to pass a backend corresponding to the "defaults" settings database on
931 * the system to get a settings object that modifies the system default
932 * settings instead of the settings for this user.
934 * Returns: a new #GSettings object
939 g_settings_new_with_backend (const gchar
*schema_id
,
940 GSettingsBackend
*backend
)
942 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
943 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend
), NULL
);
945 return g_object_new (G_TYPE_SETTINGS
,
946 "schema-id", schema_id
,
952 * g_settings_new_with_backend_and_path:
953 * @schema_id: the id of the schema
954 * @backend: the #GSettingsBackend to use
955 * @path: the path to use
957 * Creates a new #GSettings object with the schema specified by
958 * @schema_id and a given #GSettingsBackend and path.
960 * This is a mix of g_settings_new_with_backend() and
961 * g_settings_new_with_path().
963 * Returns: a new #GSettings object
968 g_settings_new_with_backend_and_path (const gchar
*schema_id
,
969 GSettingsBackend
*backend
,
972 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
973 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend
), NULL
);
974 g_return_val_if_fail (path_is_valid (path
), NULL
);
976 return g_object_new (G_TYPE_SETTINGS
,
977 "schema-id", schema_id
,
984 * g_settings_new_full:
985 * @schema: a #GSettingsSchema
986 * @backend: (allow-none): a #GSettingsBackend
987 * @path: (allow-none): the path to use
989 * Creates a new #GSettings object with a given schema, backend and
992 * It should be extremely rare that you ever want to use this function.
993 * It is made available for advanced use-cases (such as plugin systems
994 * that want to provide access to schemas loaded from custom locations,
997 * At the most basic level, a #GSettings object is a pure composition of
998 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
999 * backend, and a #GMainContext to which signals are dispatched.
1001 * This constructor therefore gives you full control over constructing
1002 * #GSettings instances. The first 4 parameters are given directly as
1003 * @schema, @backend and @path, and the main context is taken from the
1004 * thread-default (as per g_settings_new()).
1006 * If @backend is %NULL then the default backend is used.
1008 * If @path is %NULL then the path from the schema is used. It is an
1009 * error f @path is %NULL and the schema has no path of its own or if
1010 * @path is non-%NULL and not equal to the path that the schema does
1013 * Returns: a new #GSettings object
1018 g_settings_new_full (GSettingsSchema
*schema
,
1019 GSettingsBackend
*backend
,
1022 g_return_val_if_fail (schema
!= NULL
, NULL
);
1023 g_return_val_if_fail (backend
== NULL
|| G_IS_SETTINGS_BACKEND (backend
), NULL
);
1024 g_return_val_if_fail (path
== NULL
|| path_is_valid (path
), NULL
);
1026 return g_object_new (G_TYPE_SETTINGS
,
1027 "settings-schema", schema
,
1033 /* Internal read/write utilities {{{1 */
1035 g_settings_write_to_backend (GSettings
*settings
,
1036 GSettingsSchemaKey
*key
,
1042 path
= g_strconcat (settings
->priv
->path
, key
->name
, NULL
);
1043 success
= g_settings_backend_write (settings
->priv
->backend
, path
, value
, NULL
);
1050 g_settings_read_from_backend (GSettings
*settings
,
1051 GSettingsSchemaKey
*key
)
1057 path
= g_strconcat (settings
->priv
->path
, key
->name
, NULL
);
1058 value
= g_settings_backend_read (settings
->priv
->backend
, path
, key
->type
, FALSE
);
1063 fixup
= g_settings_schema_key_range_fixup (key
, value
);
1064 g_variant_unref (value
);
1072 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1074 * g_settings_get_value:
1075 * @settings: a #GSettings object
1076 * @key: the key to get the value for
1078 * Gets the value that is stored in @settings for @key.
1080 * It is a programmer error to give a @key that isn't contained in the
1081 * schema for @settings.
1083 * Returns: a new #GVariant
1088 g_settings_get_value (GSettings
*settings
,
1091 GSettingsSchemaKey skey
;
1094 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
1095 g_return_val_if_fail (key
!= NULL
, NULL
);
1097 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1098 value
= g_settings_read_from_backend (settings
, &skey
);
1101 value
= g_settings_schema_key_get_translated_default (&skey
);
1104 value
= g_variant_ref (skey
.default_value
);
1106 g_settings_schema_key_clear (&skey
);
1112 * g_settings_get_enum:
1113 * @settings: a #GSettings object
1114 * @key: the key to get the value for
1116 * Gets the value that is stored in @settings for @key and converts it
1117 * to the enum value that it represents.
1119 * In order to use this function the type of the value must be a string
1120 * and it must be marked in the schema file as an enumerated type.
1122 * It is a programmer error to give a @key that isn't contained in the
1123 * schema for @settings or is not marked as an enumerated type.
1125 * If the value stored in the configuration database is not a valid
1126 * value for the enumerated type then this function will return the
1129 * Returns: the enum value
1134 g_settings_get_enum (GSettings
*settings
,
1137 GSettingsSchemaKey skey
;
1141 g_return_val_if_fail (G_IS_SETTINGS (settings
), -1);
1142 g_return_val_if_fail (key
!= NULL
, -1);
1144 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1148 g_critical ("g_settings_get_enum() called on key '%s' which is not "
1149 "associated with an enumerated type", skey
.name
);
1150 g_settings_schema_key_clear (&skey
);
1154 value
= g_settings_read_from_backend (settings
, &skey
);
1157 value
= g_settings_schema_key_get_translated_default (&skey
);
1160 value
= g_variant_ref (skey
.default_value
);
1162 result
= g_settings_schema_key_to_enum (&skey
, value
);
1163 g_settings_schema_key_clear (&skey
);
1164 g_variant_unref (value
);
1170 * g_settings_set_enum:
1171 * @settings: a #GSettings object
1172 * @key: a key, within @settings
1173 * @value: an enumerated value
1175 * Looks up the enumerated type nick for @value and writes it to @key,
1178 * It is a programmer error to give a @key that isn't contained in the
1179 * schema for @settings or is not marked as an enumerated type, or for
1180 * @value not to be a valid value for the named type.
1182 * After performing the write, accessing @key directly with
1183 * g_settings_get_string() will return the 'nick' associated with
1186 * Returns: %TRUE, if the set succeeds
1189 g_settings_set_enum (GSettings
*settings
,
1193 GSettingsSchemaKey skey
;
1197 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1198 g_return_val_if_fail (key
!= NULL
, FALSE
);
1200 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1204 g_critical ("g_settings_set_enum() called on key '%s' which is not "
1205 "associated with an enumerated type", skey
.name
);
1209 if (!(variant
= g_settings_schema_key_from_enum (&skey
, value
)))
1211 g_critical ("g_settings_set_enum(): invalid enum value %d for key '%s' "
1212 "in schema '%s'. Doing nothing.", value
, skey
.name
,
1213 g_settings_schema_get_id (skey
.schema
));
1214 g_settings_schema_key_clear (&skey
);
1218 success
= g_settings_write_to_backend (settings
, &skey
, variant
);
1219 g_settings_schema_key_clear (&skey
);
1225 * g_settings_get_flags:
1226 * @settings: a #GSettings object
1227 * @key: the key to get the value for
1229 * Gets the value that is stored in @settings for @key and converts it
1230 * to the flags value that it represents.
1232 * In order to use this function the type of the value must be an array
1233 * of strings and it must be marked in the schema file as an flags type.
1235 * It is a programmer error to give a @key that isn't contained in the
1236 * schema for @settings or is not marked as a flags type.
1238 * If the value stored in the configuration database is not a valid
1239 * value for the flags type then this function will return the default
1242 * Returns: the flags value
1247 g_settings_get_flags (GSettings
*settings
,
1250 GSettingsSchemaKey skey
;
1254 g_return_val_if_fail (G_IS_SETTINGS (settings
), -1);
1255 g_return_val_if_fail (key
!= NULL
, -1);
1257 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1261 g_critical ("g_settings_get_flags() called on key '%s' which is not "
1262 "associated with a flags type", skey
.name
);
1263 g_settings_schema_key_clear (&skey
);
1267 value
= g_settings_read_from_backend (settings
, &skey
);
1270 value
= g_settings_schema_key_get_translated_default (&skey
);
1273 value
= g_variant_ref (skey
.default_value
);
1275 result
= g_settings_schema_key_to_flags (&skey
, value
);
1276 g_settings_schema_key_clear (&skey
);
1277 g_variant_unref (value
);
1283 * g_settings_set_flags:
1284 * @settings: a #GSettings object
1285 * @key: a key, within @settings
1286 * @value: a flags value
1288 * Looks up the flags type nicks for the bits specified by @value, puts
1289 * them in an array of strings and writes the array to @key, within
1292 * It is a programmer error to give a @key that isn't contained in the
1293 * schema for @settings or is not marked as a flags type, or for @value
1294 * to contain any bits that are not value for the named type.
1296 * After performing the write, accessing @key directly with
1297 * g_settings_get_strv() will return an array of 'nicks'; one for each
1300 * Returns: %TRUE, if the set succeeds
1303 g_settings_set_flags (GSettings
*settings
,
1307 GSettingsSchemaKey skey
;
1311 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1312 g_return_val_if_fail (key
!= NULL
, FALSE
);
1314 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1318 g_critical ("g_settings_set_flags() called on key '%s' which is not "
1319 "associated with a flags type", skey
.name
);
1323 if (!(variant
= g_settings_schema_key_from_flags (&skey
, value
)))
1325 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1326 "for key '%s' in schema '%s'. Doing nothing.",
1327 value
, skey
.name
, g_settings_schema_get_id (skey
.schema
));
1328 g_settings_schema_key_clear (&skey
);
1332 success
= g_settings_write_to_backend (settings
, &skey
, variant
);
1333 g_settings_schema_key_clear (&skey
);
1339 * g_settings_set_value:
1340 * @settings: a #GSettings object
1341 * @key: the name of the key to set
1342 * @value: a #GVariant of the correct type
1344 * Sets @key in @settings to @value.
1346 * It is a programmer error to give a @key that isn't contained in the
1347 * schema for @settings or for @value to have the incorrect type, per
1350 * If @value is floating then this function consumes the reference.
1352 * Returns: %TRUE if setting the key succeeded,
1353 * %FALSE if the key was not writable
1358 g_settings_set_value (GSettings
*settings
,
1362 GSettingsSchemaKey skey
;
1365 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1366 g_return_val_if_fail (key
!= NULL
, FALSE
);
1368 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1370 if (!g_settings_schema_key_type_check (&skey
, value
))
1372 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1374 g_settings_schema_get_id (settings
->priv
->schema
),
1375 g_variant_type_peek_string (skey
.type
),
1376 g_variant_get_type_string (value
));
1381 if (!g_settings_schema_key_range_check (&skey
, value
))
1383 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1384 "is outside of valid range",
1386 g_settings_schema_get_id (settings
->priv
->schema
));
1391 success
= g_settings_write_to_backend (settings
, &skey
, value
);
1392 g_settings_schema_key_clear (&skey
);
1399 * @settings: a #GSettings object
1400 * @key: the key to get the value for
1401 * @format: a #GVariant format string
1402 * @...: arguments as per @format
1404 * Gets the value that is stored at @key in @settings.
1406 * A convenience function that combines g_settings_get_value() with
1409 * It is a programmer error to give a @key that isn't contained in the
1410 * schema for @settings or for the #GVariantType of @format to mismatch
1411 * the type given in the schema.
1416 g_settings_get (GSettings
*settings
,
1418 const gchar
*format
,
1424 value
= g_settings_get_value (settings
, key
);
1426 va_start (ap
, format
);
1427 g_variant_get_va (value
, format
, NULL
, &ap
);
1430 g_variant_unref (value
);
1435 * @settings: a #GSettings object
1436 * @key: the name of the key to set
1437 * @format: a #GVariant format string
1438 * @...: arguments as per @format
1440 * Sets @key in @settings to @value.
1442 * A convenience function that combines g_settings_set_value() with
1445 * It is a programmer error to give a @key that isn't contained in the
1446 * schema for @settings or for the #GVariantType of @format to mismatch
1447 * the type given in the schema.
1449 * Returns: %TRUE if setting the key succeeded,
1450 * %FALSE if the key was not writable
1455 g_settings_set (GSettings
*settings
,
1457 const gchar
*format
,
1463 va_start (ap
, format
);
1464 value
= g_variant_new_va (format
, NULL
, &ap
);
1467 return g_settings_set_value (settings
, key
, value
);
1471 * g_settings_get_mapped:
1472 * @settings: a #GSettings object
1473 * @key: the key to get the value for
1474 * @mapping: (scope call): the function to map the value in the
1475 * settings database to the value used by the application
1476 * @user_data: user data for @mapping
1478 * Gets the value that is stored at @key in @settings, subject to
1479 * application-level validation/mapping.
1481 * You should use this function when the application needs to perform
1482 * some processing on the value of the key (for example, parsing). The
1483 * @mapping function performs that processing. If the function
1484 * indicates that the processing was unsuccessful (due to a parse error,
1485 * for example) then the mapping is tried again with another value.
1487 * This allows a robust 'fall back to defaults' behaviour to be
1488 * implemented somewhat automatically.
1490 * The first value that is tried is the user's setting for the key. If
1491 * the mapping function fails to map this value, other values may be
1492 * tried in an unspecified order (system or site defaults, translated
1493 * schema default values, untranslated schema default values, etc).
1495 * If the mapping function fails for all possible values, one additional
1496 * attempt is made: the mapping function is called with a %NULL value.
1497 * If the mapping function still indicates failure at this point then
1498 * the application will be aborted.
1500 * The result parameter for the @mapping function is pointed to a
1501 * #gpointer which is initially set to %NULL. The same pointer is given
1502 * to each invocation of @mapping. The final value of that #gpointer is
1503 * what is returned by this function. %NULL is valid; it is returned
1504 * just as any other value would be.
1506 * Returns: (transfer full): the result, which may be %NULL
1509 g_settings_get_mapped (GSettings
*settings
,
1511 GSettingsGetMapping mapping
,
1514 gpointer result
= NULL
;
1515 GSettingsSchemaKey skey
;
1519 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
1520 g_return_val_if_fail (key
!= NULL
, NULL
);
1521 g_return_val_if_fail (mapping
!= NULL
, NULL
);
1523 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1525 if ((value
= g_settings_read_from_backend (settings
, &skey
)))
1527 okay
= mapping (value
, &result
, user_data
);
1528 g_variant_unref (value
);
1529 if (okay
) goto okay
;
1532 if ((value
= g_settings_schema_key_get_translated_default (&skey
)))
1534 okay
= mapping (value
, &result
, user_data
);
1535 g_variant_unref (value
);
1536 if (okay
) goto okay
;
1539 if (mapping (skey
.default_value
, &result
, user_data
))
1542 if (!mapping (NULL
, &result
, user_data
))
1543 g_error ("The mapping function given to g_settings_get_mapped() for key "
1544 "'%s' in schema '%s' returned FALSE when given a NULL value.",
1545 key
, g_settings_schema_get_id (settings
->priv
->schema
));
1548 g_settings_schema_key_clear (&skey
);
1553 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1555 * g_settings_get_string:
1556 * @settings: a #GSettings object
1557 * @key: the key to get the value for
1559 * Gets the value that is stored at @key in @settings.
1561 * A convenience variant of g_settings_get() for strings.
1563 * It is a programmer error to give a @key that isn't specified as
1564 * having a string type in the schema for @settings.
1566 * Returns: a newly-allocated string
1571 g_settings_get_string (GSettings
*settings
,
1577 value
= g_settings_get_value (settings
, key
);
1578 result
= g_variant_dup_string (value
, NULL
);
1579 g_variant_unref (value
);
1585 * g_settings_set_string:
1586 * @settings: a #GSettings object
1587 * @key: the name of the key to set
1588 * @value: the value to set it to
1590 * Sets @key in @settings to @value.
1592 * A convenience variant of g_settings_set() for strings.
1594 * It is a programmer error to give a @key that isn't specified as
1595 * having a string type in the schema for @settings.
1597 * Returns: %TRUE if setting the key succeeded,
1598 * %FALSE if the key was not writable
1603 g_settings_set_string (GSettings
*settings
,
1607 return g_settings_set_value (settings
, key
, g_variant_new_string (value
));
1611 * g_settings_get_int:
1612 * @settings: a #GSettings object
1613 * @key: the key to get the value for
1615 * Gets the value that is stored at @key in @settings.
1617 * A convenience variant of g_settings_get() for 32-bit integers.
1619 * It is a programmer error to give a @key that isn't specified as
1620 * having a int32 type in the schema for @settings.
1622 * Returns: an integer
1627 g_settings_get_int (GSettings
*settings
,
1633 value
= g_settings_get_value (settings
, key
);
1634 result
= g_variant_get_int32 (value
);
1635 g_variant_unref (value
);
1641 * g_settings_set_int:
1642 * @settings: a #GSettings object
1643 * @key: the name of the key to set
1644 * @value: the value to set it to
1646 * Sets @key in @settings to @value.
1648 * A convenience variant of g_settings_set() for 32-bit integers.
1650 * It is a programmer error to give a @key that isn't specified as
1651 * having a int32 type in the schema for @settings.
1653 * Returns: %TRUE if setting the key succeeded,
1654 * %FALSE if the key was not writable
1659 g_settings_set_int (GSettings
*settings
,
1663 return g_settings_set_value (settings
, key
, g_variant_new_int32 (value
));
1667 * g_settings_get_uint:
1668 * @settings: a #GSettings object
1669 * @key: the key to get the value for
1671 * Gets the value that is stored at @key in @settings.
1673 * A convenience variant of g_settings_get() for 32-bit unsigned
1676 * It is a programmer error to give a @key that isn't specified as
1677 * having a uint32 type in the schema for @settings.
1679 * Returns: an unsigned integer
1684 g_settings_get_uint (GSettings
*settings
,
1690 value
= g_settings_get_value (settings
, key
);
1691 result
= g_variant_get_uint32 (value
);
1692 g_variant_unref (value
);
1698 * g_settings_set_uint:
1699 * @settings: a #GSettings object
1700 * @key: the name of the key to set
1701 * @value: the value to set it to
1703 * Sets @key in @settings to @value.
1705 * A convenience variant of g_settings_set() for 32-bit unsigned
1708 * It is a programmer error to give a @key that isn't specified as
1709 * having a uint32 type in the schema for @settings.
1711 * Returns: %TRUE if setting the key succeeded,
1712 * %FALSE if the key was not writable
1717 g_settings_set_uint (GSettings
*settings
,
1721 return g_settings_set_value (settings
, key
, g_variant_new_uint32 (value
));
1725 * g_settings_get_double:
1726 * @settings: a #GSettings object
1727 * @key: the key to get the value for
1729 * Gets the value that is stored at @key in @settings.
1731 * A convenience variant of g_settings_get() for doubles.
1733 * It is a programmer error to give a @key that isn't specified as
1734 * having a 'double' type in the schema for @settings.
1741 g_settings_get_double (GSettings
*settings
,
1747 value
= g_settings_get_value (settings
, key
);
1748 result
= g_variant_get_double (value
);
1749 g_variant_unref (value
);
1755 * g_settings_set_double:
1756 * @settings: a #GSettings object
1757 * @key: the name of the key to set
1758 * @value: the value to set it to
1760 * Sets @key in @settings to @value.
1762 * A convenience variant of g_settings_set() for doubles.
1764 * It is a programmer error to give a @key that isn't specified as
1765 * having a 'double' type in the schema for @settings.
1767 * Returns: %TRUE if setting the key succeeded,
1768 * %FALSE if the key was not writable
1773 g_settings_set_double (GSettings
*settings
,
1777 return g_settings_set_value (settings
, key
, g_variant_new_double (value
));
1781 * g_settings_get_boolean:
1782 * @settings: a #GSettings object
1783 * @key: the key to get the value for
1785 * Gets the value that is stored at @key in @settings.
1787 * A convenience variant of g_settings_get() for booleans.
1789 * It is a programmer error to give a @key that isn't specified as
1790 * having a boolean type in the schema for @settings.
1792 * Returns: a boolean
1797 g_settings_get_boolean (GSettings
*settings
,
1803 value
= g_settings_get_value (settings
, key
);
1804 result
= g_variant_get_boolean (value
);
1805 g_variant_unref (value
);
1811 * g_settings_set_boolean:
1812 * @settings: a #GSettings object
1813 * @key: the name of the key to set
1814 * @value: the value to set it to
1816 * Sets @key in @settings to @value.
1818 * A convenience variant of g_settings_set() for booleans.
1820 * It is a programmer error to give a @key that isn't specified as
1821 * having a boolean type in the schema for @settings.
1823 * Returns: %TRUE if setting the key succeeded,
1824 * %FALSE if the key was not writable
1829 g_settings_set_boolean (GSettings
*settings
,
1833 return g_settings_set_value (settings
, key
, g_variant_new_boolean (value
));
1837 * g_settings_get_strv:
1838 * @settings: a #GSettings object
1839 * @key: the key to get the value for
1841 * A convenience variant of g_settings_get() for string arrays.
1843 * It is a programmer error to give a @key that isn't specified as
1844 * having an array of strings type in the schema for @settings.
1846 * Returns: (array zero-terminated=1) (transfer full): a
1847 * newly-allocated, %NULL-terminated array of strings, the value that
1848 * is stored at @key in @settings.
1853 g_settings_get_strv (GSettings
*settings
,
1859 value
= g_settings_get_value (settings
, key
);
1860 result
= g_variant_dup_strv (value
, NULL
);
1861 g_variant_unref (value
);
1867 * g_settings_set_strv:
1868 * @settings: a #GSettings object
1869 * @key: the name of the key to set
1870 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1872 * Sets @key in @settings to @value.
1874 * A convenience variant of g_settings_set() for string arrays. If
1875 * @value is %NULL, then @key is set to be the empty array.
1877 * It is a programmer error to give a @key that isn't specified as
1878 * having an array of strings type in the schema for @settings.
1880 * Returns: %TRUE if setting the key succeeded,
1881 * %FALSE if the key was not writable
1886 g_settings_set_strv (GSettings
*settings
,
1888 const gchar
* const *value
)
1893 array
= g_variant_new_strv (value
, -1);
1895 array
= g_variant_new_strv (NULL
, 0);
1897 return g_settings_set_value (settings
, key
, array
);
1900 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1903 * @settings: a #GSettings object
1905 * Changes the #GSettings object into 'delay-apply' mode. In this
1906 * mode, changes to @settings are not immediately propagated to the
1907 * backend, but kept locally until g_settings_apply() is called.
1912 g_settings_delay (GSettings
*settings
)
1914 g_return_if_fail (G_IS_SETTINGS (settings
));
1916 if (settings
->priv
->delayed
)
1919 settings
->priv
->delayed
=
1920 g_delayed_settings_backend_new (settings
->priv
->backend
,
1922 settings
->priv
->main_context
);
1923 g_settings_backend_unwatch (settings
->priv
->backend
, G_OBJECT (settings
));
1924 g_object_unref (settings
->priv
->backend
);
1926 settings
->priv
->backend
= G_SETTINGS_BACKEND (settings
->priv
->delayed
);
1927 g_settings_backend_watch (settings
->priv
->backend
,
1928 &listener_vtable
, G_OBJECT (settings
),
1929 settings
->priv
->main_context
);
1931 g_object_notify (G_OBJECT (settings
), "delay-apply");
1936 * @settings: a #GSettings instance
1938 * Applies any changes that have been made to the settings. This
1939 * function does nothing unless @settings is in 'delay-apply' mode;
1940 * see g_settings_delay(). In the normal case settings are always
1941 * applied immediately.
1944 g_settings_apply (GSettings
*settings
)
1946 if (settings
->priv
->delayed
)
1948 GDelayedSettingsBackend
*delayed
;
1950 delayed
= G_DELAYED_SETTINGS_BACKEND (settings
->priv
->backend
);
1951 g_delayed_settings_backend_apply (delayed
);
1956 * g_settings_revert:
1957 * @settings: a #GSettings instance
1959 * Reverts all non-applied changes to the settings. This function
1960 * does nothing unless @settings is in 'delay-apply' mode; see
1961 * g_settings_delay(). In the normal case settings are always applied
1964 * Change notifications will be emitted for affected keys.
1967 g_settings_revert (GSettings
*settings
)
1969 if (settings
->priv
->delayed
)
1971 GDelayedSettingsBackend
*delayed
;
1973 delayed
= G_DELAYED_SETTINGS_BACKEND (settings
->priv
->backend
);
1974 g_delayed_settings_backend_revert (delayed
);
1979 * g_settings_get_has_unapplied:
1980 * @settings: a #GSettings object
1982 * Returns whether the #GSettings object has any unapplied
1983 * changes. This can only be the case if it is in 'delayed-apply' mode.
1985 * Returns: %TRUE if @settings has unapplied changes
1990 g_settings_get_has_unapplied (GSettings
*settings
)
1992 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1994 return settings
->priv
->delayed
&&
1995 g_delayed_settings_backend_get_has_unapplied (
1996 G_DELAYED_SETTINGS_BACKEND (settings
->priv
->backend
));
1999 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2002 * @settings: a #GSettings object
2003 * @key: the name of a key
2005 * Resets @key to its default value.
2007 * This call resets the key, as much as possible, to its default value.
2008 * That might the value specified in the schema or the one set by the
2012 g_settings_reset (GSettings
*settings
,
2017 path
= g_strconcat (settings
->priv
->path
, key
, NULL
);
2018 g_settings_backend_reset (settings
->priv
->backend
, path
, NULL
);
2025 * Ensures that all pending operations for the given are complete for
2026 * the default backend.
2028 * Writes made to a #GSettings are handled asynchronously. For this
2029 * reason, it is very unlikely that the changes have it to disk by the
2030 * time g_settings_set() returns.
2032 * This call will block until all of the writes have made it to the
2033 * backend. Since the mainloop is not running, no change notifications
2034 * will be dispatched during this call (but some may be queued by the
2035 * time the call is done).
2038 g_settings_sync (void)
2040 g_settings_backend_sync_default ();
2044 * g_settings_is_writable:
2045 * @settings: a #GSettings object
2046 * @name: the name of a key
2048 * Finds out if a key can be written or not
2050 * Returns: %TRUE if the key @name is writable
2055 g_settings_is_writable (GSettings
*settings
,
2061 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
2063 path
= g_strconcat (settings
->priv
->path
, name
, NULL
);
2064 writable
= g_settings_backend_get_writable (settings
->priv
->backend
, path
);
2071 * g_settings_get_child:
2072 * @settings: a #GSettings object
2073 * @name: the name of the 'child' schema
2075 * Creates a 'child' settings object which has a base path of
2076 * <replaceable>base-path</replaceable>/@name, where
2077 * <replaceable>base-path</replaceable> is the base path of @settings.
2079 * The schema for the child settings object must have been declared
2080 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2082 * Returns: (transfer full): a 'child' settings object
2087 g_settings_get_child (GSettings
*settings
,
2090 const gchar
*child_schema
;
2095 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
2097 child_name
= g_strconcat (name
, "/", NULL
);
2098 child_schema
= g_settings_schema_get_string (settings
->priv
->schema
,
2100 if (child_schema
== NULL
)
2101 g_error ("Schema '%s' has no child '%s'",
2102 g_settings_schema_get_id (settings
->priv
->schema
), name
);
2104 child_path
= g_strconcat (settings
->priv
->path
, child_name
, NULL
);
2105 child
= g_object_new (G_TYPE_SETTINGS
,
2106 "schema-id", child_schema
,
2109 g_free (child_path
);
2110 g_free (child_name
);
2116 * g_settings_list_keys:
2117 * @settings: a #GSettings object
2119 * Introspects the list of keys on @settings.
2121 * You should probably not be calling this function from "normal" code
2122 * (since you should already know what keys are in your schema). This
2123 * function is intended for introspection reasons.
2125 * You should free the return value with g_strfreev() when you are done
2128 * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2131 g_settings_list_keys (GSettings
*settings
)
2138 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
2139 strv
= g_new (gchar
*, n_keys
+ 1);
2140 for (i
= j
= 0; i
< n_keys
; i
++)
2142 const gchar
*key
= g_quark_to_string (keys
[i
]);
2144 if (!g_str_has_suffix (key
, "/"))
2145 strv
[j
++] = g_strdup (key
);
2153 * g_settings_list_children:
2154 * @settings: a #GSettings object
2156 * Gets the list of children on @settings.
2158 * The list is exactly the list of strings for which it is not an error
2159 * to call g_settings_get_child().
2161 * For GSettings objects that are lists, this value can change at any
2162 * time and you should connect to the "children-changed" signal to watch
2163 * for those changes. Note that there is a race condition here: you may
2164 * request a child after listing it only for it to have been destroyed
2165 * in the meantime. For this reason, g_settings_get_child() may return
2166 * %NULL even for a child that was listed by this function.
2168 * For GSettings objects that are not lists, you should probably not be
2169 * calling this function from "normal" code (since you should already
2170 * know what children are in your schema). This function may still be
2171 * useful there for introspection reasons, however.
2173 * You should free the return value with g_strfreev() when you are done
2176 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2179 g_settings_list_children (GSettings
*settings
)
2186 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
2187 strv
= g_new (gchar
*, n_keys
+ 1);
2188 for (i
= j
= 0; i
< n_keys
; i
++)
2190 const gchar
*key
= g_quark_to_string (keys
[i
]);
2192 if (g_str_has_suffix (key
, "/"))
2194 gint length
= strlen (key
);
2196 strv
[j
] = g_memdup (key
, length
);
2197 strv
[j
][length
- 1] = '\0';
2207 * g_settings_get_range:
2208 * @settings: a #GSettings
2209 * @key: the key to query the range of
2211 * Queries the range of a key.
2213 * This function will return a #GVariant that fully describes the range
2214 * of values that are valid for @key.
2216 * The type of #GVariant returned is <literal>(sv)</literal>. The
2217 * string describes the type of range restriction in effect. The type
2218 * and meaning of the value contained in the variant depends on the
2221 * If the string is <literal>'type'</literal> then the variant contains
2222 * an empty array. The element type of that empty array is the expected
2223 * type of value and all values of that type are valid.
2225 * If the string is <literal>'enum'</literal> then the variant contains
2226 * an array enumerating the possible values. Each item in the array is
2227 * a possible valid value and no other values are valid.
2229 * If the string is <literal>'flags'</literal> then the variant contains
2230 * an array. Each item in the array is a value that may appear zero or
2231 * one times in an array to be used as the value for this key. For
2232 * example, if the variant contained the array <literal>['x',
2233 * 'y']</literal> then the valid values for the key would be
2234 * <literal>[]</literal>, <literal>['x']</literal>,
2235 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2236 * <literal>['y', 'x']</literal>.
2238 * Finally, if the string is <literal>'range'</literal> then the variant
2239 * contains a pair of like-typed values -- the minimum and maximum
2240 * permissible values for this key.
2242 * This information should not be used by normal programs. It is
2243 * considered to be a hint for introspection purposes. Normal programs
2244 * should already know what is permitted by their own schema. The
2245 * format may change in any way in the future -- but particularly, new
2246 * forms may be added to the possibilities described above.
2248 * It is a programmer error to give a @key that isn't contained in the
2249 * schema for @settings.
2251 * You should free the returned value with g_variant_unref() when it is
2254 * Returns: a #GVariant describing the range
2259 g_settings_get_range (GSettings
*settings
,
2262 GSettingsSchemaKey skey
;
2266 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
2270 range
= g_variant_new ("(**)", skey
.minimum
, skey
.maximum
);
2273 else if (skey
.strinfo
)
2275 range
= strinfo_enumerate (skey
.strinfo
, skey
.strinfo_length
);
2276 type
= skey
.is_flags
? "flags" : "enum";
2280 range
= g_variant_new_array (skey
.type
, NULL
, 0);
2284 g_settings_schema_key_clear (&skey
);
2286 return g_variant_ref_sink (g_variant_new ("(sv)", type
, range
));
2290 * g_settings_range_check:
2291 * @settings: a #GSettings
2292 * @key: the key to check
2293 * @value: the value to check
2295 * Checks if the given @value is of the correct type and within the
2296 * permitted range for @key.
2298 * This API is not intended to be used by normal programs -- they should
2299 * already know what is permitted by their own schemas. This API is
2300 * meant to be used by programs such as editors or commandline tools.
2302 * It is a programmer error to give a @key that isn't contained in the
2303 * schema for @settings.
2305 * Returns: %TRUE if @value is valid for @key
2310 g_settings_range_check (GSettings
*settings
,
2314 GSettingsSchemaKey skey
;
2317 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
2318 good
= g_settings_schema_key_type_check (&skey
, value
) &&
2319 g_settings_schema_key_range_check (&skey
, value
);
2320 g_settings_schema_key_clear (&skey
);
2328 GSettingsSchemaKey key
;
2329 GSettings
*settings
;
2332 GSettingsBindGetMapping get_mapping
;
2333 GSettingsBindSetMapping set_mapping
;
2335 GDestroyNotify destroy
;
2337 guint writable_handler_id
;
2338 guint property_handler_id
;
2339 const GParamSpec
*property
;
2340 guint key_handler_id
;
2342 /* prevent recursion */
2347 g_settings_binding_free (gpointer data
)
2349 GSettingsBinding
*binding
= data
;
2351 g_assert (!binding
->running
);
2353 if (binding
->writable_handler_id
)
2354 g_signal_handler_disconnect (binding
->settings
,
2355 binding
->writable_handler_id
);
2357 if (binding
->key_handler_id
)
2358 g_signal_handler_disconnect (binding
->settings
,
2359 binding
->key_handler_id
);
2361 if (g_signal_handler_is_connected (binding
->object
,
2362 binding
->property_handler_id
))
2363 g_signal_handler_disconnect (binding
->object
,
2364 binding
->property_handler_id
);
2366 g_settings_schema_key_clear (&binding
->key
);
2368 if (binding
->destroy
)
2369 binding
->destroy (binding
->user_data
);
2371 g_object_unref (binding
->settings
);
2373 g_slice_free (GSettingsBinding
, binding
);
2377 g_settings_binding_quark (const char *property
)
2382 tmp
= g_strdup_printf ("gsettingsbinding-%s", property
);
2383 quark
= g_quark_from_string (tmp
);
2390 g_settings_binding_key_changed (GSettings
*settings
,
2394 GSettingsBinding
*binding
= user_data
;
2395 GValue value
= G_VALUE_INIT
;
2398 g_assert (settings
== binding
->settings
);
2399 g_assert (key
== binding
->key
.name
);
2401 if (binding
->running
)
2404 binding
->running
= TRUE
;
2406 g_value_init (&value
, binding
->property
->value_type
);
2408 variant
= g_settings_read_from_backend (binding
->settings
, &binding
->key
);
2409 if (variant
&& !binding
->get_mapping (&value
, variant
, binding
->user_data
))
2411 /* silently ignore errors in the user's config database */
2412 g_variant_unref (variant
);
2416 if (variant
== NULL
)
2418 variant
= g_settings_schema_key_get_translated_default (&binding
->key
);
2420 !binding
->get_mapping (&value
, variant
, binding
->user_data
))
2422 /* flag translation errors with a warning */
2423 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
2424 "was rejected by the binding mapping function",
2425 binding
->key
.unparsed
, binding
->key
.name
,
2426 g_settings_schema_get_id (binding
->key
.schema
));
2427 g_variant_unref (variant
);
2432 if (variant
== NULL
)
2434 variant
= g_variant_ref (binding
->key
.default_value
);
2435 if (!binding
->get_mapping (&value
, variant
, binding
->user_data
))
2436 g_error ("The schema default value for key '%s' in schema '%s' "
2437 "was rejected by the binding mapping function.",
2438 binding
->key
.name
, g_settings_schema_get_id (binding
->key
.schema
));
2441 g_object_set_property (binding
->object
, binding
->property
->name
, &value
);
2442 g_variant_unref (variant
);
2443 g_value_unset (&value
);
2445 binding
->running
= FALSE
;
2449 g_settings_binding_property_changed (GObject
*object
,
2450 const GParamSpec
*pspec
,
2453 GSettingsBinding
*binding
= user_data
;
2454 GValue value
= G_VALUE_INIT
;
2457 g_assert (object
== binding
->object
);
2458 g_assert (pspec
== binding
->property
);
2460 if (binding
->running
)
2463 binding
->running
= TRUE
;
2465 g_value_init (&value
, pspec
->value_type
);
2466 g_object_get_property (object
, pspec
->name
, &value
);
2467 if ((variant
= binding
->set_mapping (&value
, binding
->key
.type
,
2468 binding
->user_data
)))
2470 g_variant_take_ref (variant
);
2472 if (!g_settings_schema_key_type_check (&binding
->key
, variant
))
2474 g_critical ("binding mapping function for key '%s' returned "
2475 "GVariant of type '%s' when type '%s' was requested",
2476 binding
->key
.name
, g_variant_get_type_string (variant
),
2477 g_variant_type_dup_string (binding
->key
.type
));
2481 if (!g_settings_schema_key_range_check (&binding
->key
, variant
))
2483 g_critical ("GObject property '%s' on a '%s' object is out of "
2484 "schema-specified range for key '%s' of '%s': %s",
2485 binding
->property
->name
, g_type_name (binding
->property
->owner_type
),
2486 binding
->key
.name
, g_settings_schema_get_id (binding
->key
.schema
),
2487 g_variant_print (variant
, TRUE
));
2491 g_settings_write_to_backend (binding
->settings
, &binding
->key
, variant
);
2492 g_variant_unref (variant
);
2494 g_value_unset (&value
);
2496 binding
->running
= FALSE
;
2500 g_settings_bind_invert_boolean_get_mapping (GValue
*value
,
2504 g_value_set_boolean (value
, !g_variant_get_boolean (variant
));
2509 g_settings_bind_invert_boolean_set_mapping (const GValue
*value
,
2510 const GVariantType
*expected_type
,
2513 return g_variant_new_boolean (!g_value_get_boolean (value
));
2518 * @settings: a #GSettings object
2519 * @key: the key to bind
2520 * @object: (type GObject.Object): a #GObject
2521 * @property: the name of the property to bind
2522 * @flags: flags for the binding
2524 * Create a binding between the @key in the @settings object
2525 * and the property @property of @object.
2527 * The binding uses the default GIO mapping functions to map
2528 * between the settings and property values. These functions
2529 * handle booleans, numeric types and string types in a
2530 * straightforward way. Use g_settings_bind_with_mapping() if
2531 * you need a custom mapping, or map between types that are not
2532 * supported by the default mapping functions.
2534 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2535 * function also establishes a binding between the writability of
2536 * @key and the "sensitive" property of @object (if @object has
2537 * a boolean property by that name). See g_settings_bind_writable()
2538 * for more details about writable bindings.
2540 * Note that the lifecycle of the binding is tied to the object,
2541 * and that you can have only one binding per object property.
2542 * If you bind the same property twice on the same object, the second
2543 * binding overrides the first one.
2548 g_settings_bind (GSettings
*settings
,
2551 const gchar
*property
,
2552 GSettingsBindFlags flags
)
2554 GSettingsBindGetMapping get_mapping
= NULL
;
2555 GSettingsBindSetMapping set_mapping
= NULL
;
2557 if (flags
& G_SETTINGS_BIND_INVERT_BOOLEAN
)
2559 get_mapping
= g_settings_bind_invert_boolean_get_mapping
;
2560 set_mapping
= g_settings_bind_invert_boolean_set_mapping
;
2562 /* can't pass this flag to g_settings_bind_with_mapping() */
2563 flags
&= ~G_SETTINGS_BIND_INVERT_BOOLEAN
;
2566 g_settings_bind_with_mapping (settings
, key
, object
, property
, flags
,
2567 get_mapping
, set_mapping
, NULL
, NULL
);
2571 * g_settings_bind_with_mapping: (skip)
2572 * @settings: a #GSettings object
2573 * @key: the key to bind
2574 * @object: (type GObject.Object): a #GObject
2575 * @property: the name of the property to bind
2576 * @flags: flags for the binding
2577 * @get_mapping: a function that gets called to convert values
2578 * from @settings to @object, or %NULL to use the default GIO mapping
2579 * @set_mapping: a function that gets called to convert values
2580 * from @object to @settings, or %NULL to use the default GIO mapping
2581 * @user_data: data that gets passed to @get_mapping and @set_mapping
2582 * @destroy: #GDestroyNotify function for @user_data
2584 * Create a binding between the @key in the @settings object
2585 * and the property @property of @object.
2587 * The binding uses the provided mapping functions to map between
2588 * settings and property values.
2590 * Note that the lifecycle of the binding is tied to the object,
2591 * and that you can have only one binding per object property.
2592 * If you bind the same property twice on the same object, the second
2593 * binding overrides the first one.
2598 g_settings_bind_with_mapping (GSettings
*settings
,
2601 const gchar
*property
,
2602 GSettingsBindFlags flags
,
2603 GSettingsBindGetMapping get_mapping
,
2604 GSettingsBindSetMapping set_mapping
,
2606 GDestroyNotify destroy
)
2608 GSettingsBinding
*binding
;
2609 GObjectClass
*objectclass
;
2610 gchar
*detailed_signal
;
2611 GQuark binding_quark
;
2613 g_return_if_fail (G_IS_SETTINGS (settings
));
2614 g_return_if_fail (key
!= NULL
);
2615 g_return_if_fail (G_IS_OBJECT (object
));
2616 g_return_if_fail (property
!= NULL
);
2617 g_return_if_fail (~flags
& G_SETTINGS_BIND_INVERT_BOOLEAN
);
2619 objectclass
= G_OBJECT_GET_CLASS (object
);
2621 binding
= g_slice_new0 (GSettingsBinding
);
2622 g_settings_schema_key_init (&binding
->key
, settings
->priv
->schema
, key
);
2623 binding
->settings
= g_object_ref (settings
);
2624 binding
->object
= object
;
2625 binding
->property
= g_object_class_find_property (objectclass
, property
);
2626 binding
->user_data
= user_data
;
2627 binding
->destroy
= destroy
;
2628 binding
->get_mapping
= get_mapping
? get_mapping
: g_settings_get_mapping
;
2629 binding
->set_mapping
= set_mapping
? set_mapping
: g_settings_set_mapping
;
2631 if (!(flags
& (G_SETTINGS_BIND_GET
| G_SETTINGS_BIND_SET
)))
2632 flags
|= G_SETTINGS_BIND_GET
| G_SETTINGS_BIND_SET
;
2634 if (binding
->property
== NULL
)
2636 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2637 property
, G_OBJECT_TYPE_NAME (object
));
2641 if ((flags
& G_SETTINGS_BIND_GET
) &&
2642 (binding
->property
->flags
& G_PARAM_WRITABLE
) == 0)
2644 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2645 "writable", binding
->property
->name
, G_OBJECT_TYPE_NAME (object
));
2648 if ((flags
& G_SETTINGS_BIND_SET
) &&
2649 (binding
->property
->flags
& G_PARAM_READABLE
) == 0)
2651 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2652 "readable", binding
->property
->name
, G_OBJECT_TYPE_NAME (object
));
2656 if (get_mapping
== g_settings_bind_invert_boolean_get_mapping
)
2658 /* g_settings_bind_invert_boolean_get_mapping() is a private
2659 * function, so if we are here it means that g_settings_bind() was
2660 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2662 * Ensure that both sides are boolean.
2665 if (binding
->property
->value_type
!= G_TYPE_BOOLEAN
)
2667 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2668 "was specified, but property '%s' on type '%s' has "
2669 "type '%s'", binding
->property
->name
, G_OBJECT_TYPE_NAME (object
),
2670 g_type_name ((binding
->property
->value_type
)));
2674 if (!g_variant_type_equal (binding
->key
.type
, G_VARIANT_TYPE_BOOLEAN
))
2676 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2677 "was specified, but key '%s' on schema '%s' has "
2678 "type '%s'", key
, g_settings_schema_get_id (settings
->priv
->schema
),
2679 g_variant_type_dup_string (binding
->key
.type
));
2685 else if (((get_mapping
== NULL
&& (flags
& G_SETTINGS_BIND_GET
)) ||
2686 (set_mapping
== NULL
&& (flags
& G_SETTINGS_BIND_SET
))) &&
2687 !g_settings_mapping_is_compatible (binding
->property
->value_type
,
2690 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2691 "'%s' which is not compatible with type '%s' of key '%s' "
2692 "on schema '%s'", binding
->property
->name
, G_OBJECT_TYPE_NAME (object
),
2693 g_type_name (binding
->property
->value_type
),
2694 g_variant_type_dup_string (binding
->key
.type
), key
,
2695 g_settings_schema_get_id (settings
->priv
->schema
));
2699 if ((flags
& G_SETTINGS_BIND_SET
) &&
2700 (~flags
& G_SETTINGS_BIND_NO_SENSITIVITY
))
2702 GParamSpec
*sensitive
;
2704 sensitive
= g_object_class_find_property (objectclass
, "sensitive");
2706 if (sensitive
&& sensitive
->value_type
== G_TYPE_BOOLEAN
&&
2707 (sensitive
->flags
& G_PARAM_WRITABLE
))
2708 g_settings_bind_writable (settings
, binding
->key
.name
, object
, "sensitive", FALSE
);
2711 if (flags
& G_SETTINGS_BIND_SET
)
2713 detailed_signal
= g_strdup_printf ("notify::%s", binding
->property
->name
);
2714 binding
->property_handler_id
=
2715 g_signal_connect (object
, detailed_signal
,
2716 G_CALLBACK (g_settings_binding_property_changed
),
2718 g_free (detailed_signal
);
2720 if (~flags
& G_SETTINGS_BIND_GET
)
2721 g_settings_binding_property_changed (object
,
2726 if (flags
& G_SETTINGS_BIND_GET
)
2728 if (~flags
& G_SETTINGS_BIND_GET_NO_CHANGES
)
2730 detailed_signal
= g_strdup_printf ("changed::%s", key
);
2731 binding
->key_handler_id
=
2732 g_signal_connect (settings
, detailed_signal
,
2733 G_CALLBACK (g_settings_binding_key_changed
),
2735 g_free (detailed_signal
);
2738 g_settings_binding_key_changed (settings
, binding
->key
.name
, binding
);
2741 binding_quark
= g_settings_binding_quark (binding
->property
->name
);
2742 g_object_set_qdata_full (object
, binding_quark
,
2743 binding
, g_settings_binding_free
);
2746 /* Writability binding {{{1 */
2749 GSettings
*settings
;
2752 const gchar
*property
;
2755 } GSettingsWritableBinding
;
2758 g_settings_writable_binding_free (gpointer data
)
2760 GSettingsWritableBinding
*binding
= data
;
2762 g_signal_handler_disconnect (binding
->settings
, binding
->handler_id
);
2763 g_object_unref (binding
->settings
);
2764 g_slice_free (GSettingsWritableBinding
, binding
);
2768 g_settings_binding_writable_changed (GSettings
*settings
,
2772 GSettingsWritableBinding
*binding
= user_data
;
2775 g_assert (settings
== binding
->settings
);
2776 g_assert (key
== binding
->key
);
2778 writable
= g_settings_is_writable (settings
, key
);
2780 if (binding
->inverted
)
2781 writable
= !writable
;
2783 g_object_set (binding
->object
, binding
->property
, writable
, NULL
);
2787 * g_settings_bind_writable:
2788 * @settings: a #GSettings object
2789 * @key: the key to bind
2790 * @object: (type GObject.Object):a #GObject
2791 * @property: the name of a boolean property to bind
2792 * @inverted: whether to 'invert' the value
2794 * Create a binding between the writability of @key in the
2795 * @settings object and the property @property of @object.
2796 * The property must be boolean; "sensitive" or "visible"
2797 * properties of widgets are the most likely candidates.
2799 * Writable bindings are always uni-directional; changes of the
2800 * writability of the setting will be propagated to the object
2801 * property, not the other way.
2803 * When the @inverted argument is %TRUE, the binding inverts the
2804 * value as it passes from the setting to the object, i.e. @property
2805 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2808 * Note that the lifecycle of the binding is tied to the object,
2809 * and that you can have only one binding per object property.
2810 * If you bind the same property twice on the same object, the second
2811 * binding overrides the first one.
2816 g_settings_bind_writable (GSettings
*settings
,
2819 const gchar
*property
,
2822 GSettingsWritableBinding
*binding
;
2823 gchar
*detailed_signal
;
2826 g_return_if_fail (G_IS_SETTINGS (settings
));
2828 pspec
= g_object_class_find_property (G_OBJECT_GET_CLASS (object
), property
);
2831 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2832 property
, G_OBJECT_TYPE_NAME (object
));
2835 if ((pspec
->flags
& G_PARAM_WRITABLE
) == 0)
2837 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2838 property
, G_OBJECT_TYPE_NAME (object
));
2842 binding
= g_slice_new (GSettingsWritableBinding
);
2843 binding
->settings
= g_object_ref (settings
);
2844 binding
->object
= object
;
2845 binding
->key
= g_intern_string (key
);
2846 binding
->property
= g_intern_string (property
);
2847 binding
->inverted
= inverted
;
2849 detailed_signal
= g_strdup_printf ("writable-changed::%s", key
);
2850 binding
->handler_id
=
2851 g_signal_connect (settings
, detailed_signal
,
2852 G_CALLBACK (g_settings_binding_writable_changed
),
2854 g_free (detailed_signal
);
2856 g_object_set_qdata_full (object
, g_settings_binding_quark (property
),
2857 binding
, g_settings_writable_binding_free
);
2859 g_settings_binding_writable_changed (settings
, binding
->key
, binding
);
2863 * g_settings_unbind:
2864 * @object: the object
2865 * @property: the property whose binding is removed
2867 * Removes an existing binding for @property on @object.
2869 * Note that bindings are automatically removed when the
2870 * object is finalized, so it is rarely necessary to call this
2876 g_settings_unbind (gpointer object
,
2877 const gchar
*property
)
2879 GQuark binding_quark
;
2881 binding_quark
= g_settings_binding_quark (property
);
2882 g_object_set_qdata (object
, binding_quark
, NULL
);
2889 GObject parent_instance
;
2891 GSettingsSchemaKey key
;
2892 GSettings
*settings
;
2895 typedef GObjectClass GSettingsActionClass
;
2897 static GType
g_settings_action_get_type (void);
2898 static void g_settings_action_iface_init (GActionInterface
*iface
);
2899 G_DEFINE_TYPE_WITH_CODE (GSettingsAction
, g_settings_action
, G_TYPE_OBJECT
,
2900 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION
, g_settings_action_iface_init
))
2906 ACTION_PROP_PARAMETER_TYPE
,
2907 ACTION_PROP_ENABLED
,
2908 ACTION_PROP_STATE_TYPE
,
2912 static const gchar
*
2913 g_settings_action_get_name (GAction
*action
)
2915 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2917 return gsa
->key
.name
;
2920 static const GVariantType
*
2921 g_settings_action_get_parameter_type (GAction
*action
)
2923 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2924 const GVariantType
*type
;
2926 type
= g_variant_get_type (gsa
->key
.default_value
);
2927 if (g_variant_type_equal (type
, G_VARIANT_TYPE_BOOLEAN
))
2934 g_settings_action_get_enabled (GAction
*action
)
2936 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2938 return g_settings_is_writable (gsa
->settings
, gsa
->key
.name
);
2941 static const GVariantType
*
2942 g_settings_action_get_state_type (GAction
*action
)
2944 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2946 return g_variant_get_type (gsa
->key
.default_value
);
2950 g_settings_action_get_state (GAction
*action
)
2952 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2955 value
= g_settings_read_from_backend (gsa
->settings
, &gsa
->key
);
2958 value
= g_settings_schema_key_get_translated_default (&gsa
->key
);
2961 value
= g_variant_ref (gsa
->key
.default_value
);
2967 g_settings_action_get_state_hint (GAction
*action
)
2969 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2971 /* no point in reimplementing this... */
2972 return g_settings_get_range (gsa
->settings
, gsa
->key
.name
);
2976 g_settings_action_change_state (GAction
*action
,
2979 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2981 if (g_settings_schema_key_type_check (&gsa
->key
, value
) && g_settings_schema_key_range_check (&gsa
->key
, value
))
2982 g_settings_write_to_backend (gsa
->settings
, &gsa
->key
, value
);
2986 g_settings_action_activate (GAction
*action
,
2987 GVariant
*parameter
)
2989 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2991 if (g_variant_is_of_type (gsa
->key
.default_value
, G_VARIANT_TYPE_BOOLEAN
))
2995 if (parameter
!= NULL
)
2998 old
= g_settings_action_get_state (action
);
2999 parameter
= g_variant_new_boolean (!g_variant_get_boolean (old
));
3000 g_variant_unref (old
);
3003 g_action_change_state (action
, parameter
);
3007 g_settings_action_get_property (GObject
*object
, guint prop_id
,
3008 GValue
*value
, GParamSpec
*pspec
)
3010 GAction
*action
= G_ACTION (object
);
3014 case ACTION_PROP_NAME
:
3015 g_value_set_string (value
, g_settings_action_get_name (action
));
3018 case ACTION_PROP_PARAMETER_TYPE
:
3019 g_value_set_boxed (value
, g_settings_action_get_parameter_type (action
));
3022 case ACTION_PROP_ENABLED
:
3023 g_value_set_boolean (value
, g_settings_action_get_enabled (action
));
3026 case ACTION_PROP_STATE_TYPE
:
3027 g_value_set_boxed (value
, g_settings_action_get_state_type (action
));
3030 case ACTION_PROP_STATE
:
3031 g_value_set_variant (value
, g_settings_action_get_state (action
));
3035 g_assert_not_reached ();
3040 g_settings_action_finalize (GObject
*object
)
3042 GSettingsAction
*gsa
= (GSettingsAction
*) object
;
3044 g_signal_handlers_disconnect_by_data (gsa
->settings
, gsa
);
3045 g_object_unref (gsa
->settings
);
3047 G_OBJECT_CLASS (g_settings_action_parent_class
)
3048 ->finalize (object
);
3052 g_settings_action_init (GSettingsAction
*gsa
)
3057 g_settings_action_iface_init (GActionInterface
*iface
)
3059 iface
->get_name
= g_settings_action_get_name
;
3060 iface
->get_parameter_type
= g_settings_action_get_parameter_type
;
3061 iface
->get_enabled
= g_settings_action_get_enabled
;
3062 iface
->get_state_type
= g_settings_action_get_state_type
;
3063 iface
->get_state
= g_settings_action_get_state
;
3064 iface
->get_state_hint
= g_settings_action_get_state_hint
;
3065 iface
->change_state
= g_settings_action_change_state
;
3066 iface
->activate
= g_settings_action_activate
;
3070 g_settings_action_class_init (GSettingsActionClass
*class)
3072 class->get_property
= g_settings_action_get_property
;
3073 class->finalize
= g_settings_action_finalize
;
3075 g_object_class_override_property (class, ACTION_PROP_NAME
, "name");
3076 g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE
, "parameter-type");
3077 g_object_class_override_property (class, ACTION_PROP_ENABLED
, "enabled");
3078 g_object_class_override_property (class, ACTION_PROP_STATE_TYPE
, "state-type");
3079 g_object_class_override_property (class, ACTION_PROP_STATE
, "state");
3083 g_settings_action_changed (GSettings
*settings
,
3087 g_object_notify (user_data
, "state");
3091 g_settings_action_enabled_changed (GSettings
*settings
,
3095 g_object_notify (user_data
, "enabled");
3099 * g_settings_create_action:
3100 * @settings: a #GSettings
3101 * @key: the name of a key in @settings
3103 * Creates a #GAction corresponding to a given #GSettings key.
3105 * The action has the same name as the key.
3107 * The value of the key becomes the state of the action and the action
3108 * is enabled when the key is writable. Changing the state of the
3109 * action results in the key being written to. Changes to the value or
3110 * writability of the key cause appropriate change notifications to be
3111 * emitted for the action.
3113 * For boolean-valued keys, action activations take no parameter and
3114 * result in the toggling of the value. For all other types,
3115 * activations take the new value for the key (which must have the
3118 * Returns: (transfer full): a new #GAction
3123 g_settings_create_action (GSettings
*settings
,
3126 GSettingsAction
*gsa
;
3127 gchar
*detailed_signal
;
3129 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
3130 g_return_val_if_fail (key
!= NULL
, NULL
);
3132 gsa
= g_object_new (g_settings_action_get_type (), NULL
);
3133 gsa
->settings
= g_object_ref (settings
);
3134 g_settings_schema_key_init (&gsa
->key
, settings
->priv
->schema
, key
);
3136 detailed_signal
= g_strdup_printf ("changed::%s", key
);
3137 g_signal_connect (settings
, detailed_signal
, G_CALLBACK (g_settings_action_changed
), gsa
);
3138 g_free (detailed_signal
);
3139 detailed_signal
= g_strdup_printf ("writable-changed::%s", key
);
3140 g_signal_connect (settings
, detailed_signal
, G_CALLBACK (g_settings_action_enabled_changed
), gsa
);
3141 g_free (detailed_signal
);
3143 return G_ACTION (gsa
);
3148 /* vim:set foldmethod=marker: */