Add private _g_bus_get_singleton_if_exists() function
[platform/upstream/glib.git] / gio / gsettings.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 /* Prelude {{{1 */
23 #include "config.h"
24
25 #include <glib.h>
26 #include <glibintl.h>
27
28 #include "gsettings.h"
29
30 #include "gdelayedsettingsbackend.h"
31 #include "gsettingsbackendinternal.h"
32 #include "gsettings-mapping.h"
33 #include "gsettingsschema-internal.h"
34 #include "gaction.h"
35
36 #include "strinfo.c"
37
38 /**
39  * SECTION:gsettings
40  * @short_description: High-level API for application settings
41  *
42  * The #GSettings class provides a convenient API for storing and retrieving
43  * application settings.
44  *
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.
61  *
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.
65  *
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.
71  *
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.
79  *
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.
86  *
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.
94  *
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>]|
100  *
101  * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
102  *
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".
112  *
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
120  * and flags keys.
121  *
122  * <example id="schema-default-values"><title>Default values</title>
123  * <programlisting><![CDATA[
124  * <schemalist>
125  *   <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
126  *
127  *     <key name="greeting" type="s">
128  *       <default l10n="messages">"Hello, earthlings"</default>
129  *       <summary>A greeting</summary>
130  *       <description>
131  *         Greeting of the invading martians
132  *       </description>
133  *     </key>
134  *
135  *     <key name="box" type="(ii)">
136  *       <default>(20,30)</default>
137  *     </key>
138  *
139  *   </schema>
140  * </schemalist>
141  * ]]></programlisting></example>
142  *
143  * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
144  * <programlisting><![CDATA[
145  * <schemalist>
146  *
147  *   <enum id="org.gtk.Test.myenum">
148  *     <value nick="first" value="1"/>
149  *     <value nick="second" value="2"/>
150  *   </enum>
151  *
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"/>
156  *   </flags>
157  *
158  *   <schema id="org.gtk.Test">
159  *
160  *     <key name="key-with-range" type="i">
161  *       <range min="1" max="100"/>
162  *       <default>10</default>
163  *     </key>
164  *
165  *     <key name="key-with-choices" type="s">
166  *       <choices>
167  *         <choice value='Elisabeth'/>
168  *         <choice value='Annabeth'/>
169  *         <choice value='Joe'/>
170  *       </choices>
171  *       <aliases>
172  *         <alias value='Anna' target='Annabeth'/>
173  *         <alias value='Beth' target='Elisabeth'/>
174  *       </aliases>
175  *       <default>'Joe'</default>
176  *     </key>
177  *
178  *     <key name='enumerated-key' enum='org.gtk.Test.myenum'>
179  *       <default>'first'</default>
180  *     </key>
181  *
182  *     <key name='flags-key' flags='org.gtk.Test.myflags'>
183  *       <default>["flag1",flag2"]</default>
184  *     </key>
185  *   </schema>
186  * </schemalist>
187  * ]]></programlisting></example>
188  *
189  * <refsect2>
190  *   <title>Vendor overrides</title>
191  *   <para>
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>
202  *     [org.gtk.Example]
203  *     key1='string'
204  *     key2=1.5
205  *     </programlisting></informalexample>
206  *   </para>
207  *   <para>
208  *     glib-compile-schemas expects schema files to have the extension
209  *     <filename>.gschema.override</filename>
210  *   </para>
211  * </refsect2>
212  *
213  * <refsect2>
214  *   <title>Binding</title>
215  *   <para>
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
221  *     cycles.
222  *   </para>
223  *   <para>
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.
230  *   </para>
231  * </refsect2>
232  **/
233
234 struct _GSettingsPrivate
235 {
236   /* where the signals go... */
237   GMainContext *main_context;
238
239   GSettingsBackend *backend;
240   GSettingsSchema *schema;
241   gchar *path;
242
243   GDelayedSettingsBackend *delayed;
244 };
245
246 enum
247 {
248   PROP_0,
249   PROP_SCHEMA,
250   PROP_SCHEMA_ID,
251   PROP_BACKEND,
252   PROP_PATH,
253   PROP_HAS_UNAPPLIED,
254   PROP_DELAY_APPLY
255 };
256
257 enum
258 {
259   SIGNAL_WRITABLE_CHANGE_EVENT,
260   SIGNAL_WRITABLE_CHANGED,
261   SIGNAL_CHANGE_EVENT,
262   SIGNAL_CHANGED,
263   N_SIGNALS
264 };
265
266 static guint g_settings_signals[N_SIGNALS];
267
268 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
269
270 /* Signals {{{1 */
271 static gboolean
272 g_settings_real_change_event (GSettings    *settings,
273                               const GQuark *keys,
274                               gint          n_keys)
275 {
276   gint i;
277
278   if (keys == NULL)
279     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
280
281   for (i = 0; i < n_keys; i++)
282     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
283                    keys[i], g_quark_to_string (keys[i]));
284
285   return FALSE;
286 }
287
288 static gboolean
289 g_settings_real_writable_change_event (GSettings *settings,
290                                        GQuark     key)
291 {
292   const GQuark *keys = &key;
293   gint n_keys = 1;
294   gint i;
295
296   if (key == 0)
297     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
298
299   for (i = 0; i < n_keys; i++)
300     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
301                    keys[i], g_quark_to_string (keys[i]));
302
303   return FALSE;
304 }
305
306 static void
307 settings_backend_changed (GObject             *target,
308                           GSettingsBackend    *backend,
309                           const gchar         *key,
310                           gpointer             origin_tag)
311 {
312   GSettings *settings = G_SETTINGS (target);
313   gboolean ignore_this;
314   gint i;
315
316   /* We used to assert here:
317    *
318    *   settings->priv->backend == backend
319    *
320    * but it could be the case that a notification is queued for delivery
321    * while someone calls g_settings_delay() (which changes the backend).
322    *
323    * Since the delay backend would just pass that straight through
324    * anyway, it doesn't make sense to try to detect this case.
325    * Therefore, we just accept it.
326    */
327
328   for (i = 0; key[i] == settings->priv->path[i]; i++);
329
330   if (settings->priv->path[i] == '\0' &&
331       g_settings_schema_has_key (settings->priv->schema, key + i))
332     {
333       GQuark quark;
334
335       quark = g_quark_from_string (key + i);
336       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
337                      0, &quark, 1, &ignore_this);
338     }
339 }
340
341 static void
342 settings_backend_path_changed (GObject          *target,
343                                GSettingsBackend *backend,
344                                const gchar      *path,
345                                gpointer          origin_tag)
346 {
347   GSettings *settings = G_SETTINGS (target);
348   gboolean ignore_this;
349
350   if (g_str_has_prefix (settings->priv->path, path))
351     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
352                    0, NULL, 0, &ignore_this);
353 }
354
355 static void
356 settings_backend_keys_changed (GObject             *target,
357                                GSettingsBackend    *backend,
358                                const gchar         *path,
359                                const gchar * const *items,
360                                gpointer             origin_tag)
361 {
362   GSettings *settings = G_SETTINGS (target);
363   gboolean ignore_this;
364   gint i;
365
366   for (i = 0; settings->priv->path[i] &&
367               settings->priv->path[i] == path[i]; i++);
368
369   if (path[i] == '\0')
370     {
371       GQuark quarks[256];
372       gint j, l = 0;
373
374       for (j = 0; items[j]; j++)
375          {
376            const gchar *item = items[j];
377            gint k;
378
379            for (k = 0; item[k] == settings->priv->path[i + k]; k++);
380
381            if (settings->priv->path[i + k] == '\0' &&
382                g_settings_schema_has_key (settings->priv->schema, item + k))
383              quarks[l++] = g_quark_from_string (item + k);
384
385            /* "256 quarks ought to be enough for anybody!"
386             * If this bites you, I'm sorry.  Please file a bug.
387             */
388            g_assert (l < 256);
389          }
390
391       if (l > 0)
392         g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
393                        0, quarks, l, &ignore_this);
394     }
395 }
396
397 static void
398 settings_backend_writable_changed (GObject          *target,
399                                    GSettingsBackend *backend,
400                                    const gchar      *key)
401 {
402   GSettings *settings = G_SETTINGS (target);
403   gboolean ignore_this;
404   gint i;
405
406   for (i = 0; key[i] == settings->priv->path[i]; i++);
407
408   if (settings->priv->path[i] == '\0' &&
409       g_settings_schema_has_key (settings->priv->schema, key + i))
410     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
411                    0, g_quark_from_string (key + i), &ignore_this);
412 }
413
414 static void
415 settings_backend_path_writable_changed (GObject          *target,
416                                         GSettingsBackend *backend,
417                                         const gchar      *path)
418 {
419   GSettings *settings = G_SETTINGS (target);
420   gboolean ignore_this;
421
422   if (g_str_has_prefix (settings->priv->path, path))
423     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
424                    0, (GQuark) 0, &ignore_this);
425 }
426
427 /* Properties, Construction, Destruction {{{1 */
428 static void
429 g_settings_set_property (GObject      *object,
430                          guint         prop_id,
431                          const GValue *value,
432                          GParamSpec   *pspec)
433 {
434   GSettings *settings = G_SETTINGS (object);
435
436   switch (prop_id)
437     {
438     case PROP_SCHEMA:
439       {
440         GSettingsSchema *schema;
441
442         schema = g_value_dup_boxed (value);
443
444         /* we receive a set_property() call for "settings-schema" even
445          * if it was not specified (ie: with NULL value).  ->schema
446          * could already be set at this point (ie: via "schema-id").
447          * check for NULL to avoid clobbering the existing value.
448          */
449         if (schema != NULL)
450           {
451             g_assert (settings->priv->schema == NULL);
452             settings->priv->schema = schema;
453           }
454       }
455       break;
456
457     case PROP_SCHEMA_ID:
458       {
459         const gchar *schema_id;
460
461         schema_id = g_value_get_string (value);
462
463         /* we receive a set_property() call for both "schema" and
464          * "schema-id", even if they are not set.  Hopefully only one of
465          * them is non-NULL.
466          */
467         if (schema_id != NULL)
468           {
469             GSettingsSchemaSource *default_source;
470
471             g_assert (settings->priv->schema == NULL);
472             default_source = g_settings_schema_source_get_default ();
473
474             if (default_source == NULL)
475               g_error ("No GSettings schemas are installed on the system");
476
477             settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
478
479             if (settings->priv->schema == NULL)
480               g_error ("Settings schema '%s' is not installed\n", schema_id);
481           }
482       }
483       break;
484
485     case PROP_PATH:
486       settings->priv->path = g_value_dup_string (value);
487       break;
488
489     case PROP_BACKEND:
490       settings->priv->backend = g_value_dup_object (value);
491       break;
492
493     default:
494       g_assert_not_reached ();
495     }
496 }
497
498 static void
499 g_settings_get_property (GObject    *object,
500                          guint       prop_id,
501                          GValue     *value,
502                          GParamSpec *pspec)
503 {
504   GSettings *settings = G_SETTINGS (object);
505
506   switch (prop_id)
507     {
508     case PROP_SCHEMA:
509       g_value_set_boxed (value, settings->priv->schema);
510       break;
511
512      case PROP_SCHEMA_ID:
513       g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
514       break;
515
516      case PROP_BACKEND:
517       g_value_set_object (value, settings->priv->backend);
518       break;
519
520      case PROP_PATH:
521       g_value_set_string (value, settings->priv->path);
522       break;
523
524      case PROP_HAS_UNAPPLIED:
525       g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
526       break;
527
528      case PROP_DELAY_APPLY:
529       g_value_set_boolean (value, settings->priv->delayed != NULL);
530       break;
531
532      default:
533       g_assert_not_reached ();
534     }
535 }
536
537 static const GSettingsListenerVTable listener_vtable = {
538   settings_backend_changed,
539   settings_backend_path_changed,
540   settings_backend_keys_changed,
541   settings_backend_writable_changed,
542   settings_backend_path_writable_changed
543 };
544
545 static void
546 g_settings_constructed (GObject *object)
547 {
548   GSettings *settings = G_SETTINGS (object);
549   const gchar *schema_path;
550
551   schema_path = g_settings_schema_get_path (settings->priv->schema);
552
553   if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
554     g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
555              g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
556
557   if (settings->priv->path == NULL)
558     {
559       if (schema_path == NULL)
560         g_error ("attempting to create schema '%s' without a path",
561                  g_settings_schema_get_id (settings->priv->schema));
562
563       settings->priv->path = g_strdup (schema_path);
564     }
565
566   if (settings->priv->backend == NULL)
567     settings->priv->backend = g_settings_backend_get_default ();
568
569   g_settings_backend_watch (settings->priv->backend,
570                             &listener_vtable, G_OBJECT (settings),
571                             settings->priv->main_context);
572   g_settings_backend_subscribe (settings->priv->backend,
573                                 settings->priv->path);
574 }
575
576 static void
577 g_settings_finalize (GObject *object)
578 {
579   GSettings *settings = G_SETTINGS (object);
580
581   g_settings_backend_unsubscribe (settings->priv->backend,
582                                   settings->priv->path);
583   g_main_context_unref (settings->priv->main_context);
584   g_object_unref (settings->priv->backend);
585   g_settings_schema_unref (settings->priv->schema);
586   g_free (settings->priv->path);
587
588   G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
589 }
590
591 static void
592 g_settings_init (GSettings *settings)
593 {
594   settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
595                                                 G_TYPE_SETTINGS,
596                                                 GSettingsPrivate);
597
598   settings->priv->main_context = g_main_context_ref_thread_default ();
599 }
600
601 static void
602 g_settings_class_init (GSettingsClass *class)
603 {
604   GObjectClass *object_class = G_OBJECT_CLASS (class);
605
606   class->writable_change_event = g_settings_real_writable_change_event;
607   class->change_event = g_settings_real_change_event;
608
609   object_class->set_property = g_settings_set_property;
610   object_class->get_property = g_settings_get_property;
611   object_class->constructed = g_settings_constructed;
612   object_class->finalize = g_settings_finalize;
613
614   g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
615
616   /**
617    * GSettings::changed:
618    * @settings: the object on which the signal was emitted
619    * @key: the name of the key that changed
620    *
621    * The "changed" signal is emitted when a key has potentially changed.
622    * You should call one of the g_settings_get() calls to check the new
623    * value.
624    *
625    * This signal supports detailed connections.  You can connect to the
626    * detailed signal "changed::x" in order to only receive callbacks
627    * when key "x" changes.
628    */
629   g_settings_signals[SIGNAL_CHANGED] =
630     g_signal_new ("changed", G_TYPE_SETTINGS,
631                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
632                   G_STRUCT_OFFSET (GSettingsClass, changed),
633                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
634                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
635
636   /**
637    * GSettings::change-event:
638    * @settings: the object on which the signal was emitted
639    * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
640    *        an array of #GQuark<!-- -->s for the changed keys, or %NULL
641    * @n_keys: the length of the @keys array, or 0
642    *
643    * The "change-event" signal is emitted once per change event that
644    * affects this settings object.  You should connect to this signal
645    * only if you are interested in viewing groups of changes before they
646    * are split out into multiple emissions of the "changed" signal.
647    * For most use cases it is more appropriate to use the "changed" signal.
648    *
649    * In the event that the change event applies to one or more specified
650    * keys, @keys will be an array of #GQuark of length @n_keys.  In the
651    * event that the change event applies to the #GSettings object as a
652    * whole (ie: potentially every key has been changed) then @keys will
653    * be %NULL and @n_keys will be 0.
654    *
655    * The default handler for this signal invokes the "changed" signal
656    * for each affected key.  If any other connected handler returns
657    * %TRUE then this default functionality will be suppressed.
658    *
659    * Returns: %TRUE to stop other handlers from being invoked for the
660    *          event. FALSE to propagate the event further.
661    */
662   g_settings_signals[SIGNAL_CHANGE_EVENT] =
663     g_signal_new ("change-event", G_TYPE_SETTINGS,
664                   G_SIGNAL_RUN_LAST,
665                   G_STRUCT_OFFSET (GSettingsClass, change_event),
666                   g_signal_accumulator_true_handled, NULL,
667                   NULL,
668                   G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
669
670   /**
671    * GSettings::writable-changed:
672    * @settings: the object on which the signal was emitted
673    * @key: the key
674    *
675    * The "writable-changed" signal is emitted when the writability of a
676    * key has potentially changed.  You should call
677    * g_settings_is_writable() in order to determine the new status.
678    *
679    * This signal supports detailed connections.  You can connect to the
680    * detailed signal "writable-changed::x" in order to only receive
681    * callbacks when the writability of "x" changes.
682    */
683   g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
684     g_signal_new ("writable-changed", G_TYPE_SETTINGS,
685                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
686                   G_STRUCT_OFFSET (GSettingsClass, writable_changed),
687                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
688                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
689
690   /**
691    * GSettings::writable-change-event:
692    * @settings: the object on which the signal was emitted
693    * @key: the quark of the key, or 0
694    *
695    * The "writable-change-event" signal is emitted once per writability
696    * change event that affects this settings object.  You should connect
697    * to this signal if you are interested in viewing groups of changes
698    * before they are split out into multiple emissions of the
699    * "writable-changed" signal.  For most use cases it is more
700    * appropriate to use the "writable-changed" signal.
701    *
702    * In the event that the writability change applies only to a single
703    * key, @key will be set to the #GQuark for that key.  In the event
704    * that the writability change affects the entire settings object,
705    * @key will be 0.
706    *
707    * The default handler for this signal invokes the "writable-changed"
708    * and "changed" signals for each affected key.  This is done because
709    * changes in writability might also imply changes in value (if for
710    * example, a new mandatory setting is introduced).  If any other
711    * connected handler returns %TRUE then this default functionality
712    * will be suppressed.
713    *
714    * Returns: %TRUE to stop other handlers from being invoked for the
715    *          event. FALSE to propagate the event further.
716    */
717   g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
718     g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
719                   G_SIGNAL_RUN_LAST,
720                   G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
721                   g_signal_accumulator_true_handled, NULL,
722                   NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
723
724   /**
725    * GSettings:context:
726    *
727    * The name of the context that the settings are stored in.
728    */
729   g_object_class_install_property (object_class, PROP_BACKEND,
730     g_param_spec_object ("backend",
731                          P_("GSettingsBackend"),
732                          P_("The GSettingsBackend for this settings object"),
733                          G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
734                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
735
736   /**
737    * GSettings:settings-schema:
738    *
739    * The #GSettingsSchema describing the types of keys for this
740    * #GSettings object.
741    *
742    * Ideally, this property would be called 'schema'.  #GSettingsSchema
743    * has only existed since version 2.32, however, and before then the
744    * 'schema' property was used to refer to the ID of the schema rather
745    * than the schema itself.  Take care.
746    */
747   g_object_class_install_property (object_class, PROP_SCHEMA,
748     g_param_spec_boxed ("settings-schema",
749                         P_("schema"),
750                         P_("The GSettingsSchema for this settings object"),
751                         G_TYPE_SETTINGS_SCHEMA,
752                         G_PARAM_CONSTRUCT_ONLY |
753                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
754
755   /**
756    * GSettings:schema:
757    *
758    * The name of the schema that describes the types of keys
759    * for this #GSettings object.
760    *
761    * The type of this property is *not* #GSettingsSchema.
762    * #GSettingsSchema has only existed since version 2.32 and
763    * unfortunately this name was used in previous versions to refer to
764    * the schema ID rather than the schema itself.  Take care to use the
765    * 'settings-schema' property if you wish to pass in a
766    * #GSettingsSchema.
767    *
768    * Deprecated:2.32:Use the 'schema-id' property instead.  In a future
769    * version, this property may instead refer to a #GSettingsSchema.
770    */
771   g_object_class_install_property (object_class, PROP_SCHEMA_ID,
772     g_param_spec_string ("schema",
773                          P_("Schema name"),
774                          P_("The name of the schema for this settings object"),
775                          NULL,
776                          G_PARAM_CONSTRUCT_ONLY |
777                          G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
778
779   /**
780    * GSettings:schema-id:
781    *
782    * The name of the schema that describes the types of keys
783    * for this #GSettings object.
784    */
785   g_object_class_install_property (object_class, PROP_SCHEMA_ID,
786     g_param_spec_string ("schema-id",
787                          P_("Schema name"),
788                          P_("The name of the schema for this settings object"),
789                          NULL,
790                          G_PARAM_CONSTRUCT_ONLY |
791                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
792
793    /**
794     * GSettings:path:
795     *
796     * The path within the backend where the settings are stored.
797     */
798    g_object_class_install_property (object_class, PROP_PATH,
799      g_param_spec_string ("path",
800                           P_("Base path"),
801                           P_("The path within the backend where the settings are"),
802                           NULL,
803                           G_PARAM_CONSTRUCT_ONLY |
804                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
805
806    /**
807     * GSettings:has-unapplied:
808     *
809     * If this property is %TRUE, the #GSettings object has outstanding
810     * changes that will be applied when g_settings_apply() is called.
811     */
812    g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
813      g_param_spec_boolean ("has-unapplied",
814                            P_("Has unapplied changes"),
815                            P_("TRUE if there are outstanding changes to apply()"),
816                            FALSE,
817                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
818
819    /**
820     * GSettings:delay-apply:
821     *
822     * Whether the #GSettings object is in 'delay-apply' mode. See
823     * g_settings_delay() for details.
824     *
825     * Since: 2.28
826     */
827    g_object_class_install_property (object_class, PROP_DELAY_APPLY,
828      g_param_spec_boolean ("delay-apply",
829                            P_("Delay-apply mode"),
830                            P_("Whether this settings object is in 'delay-apply' mode"),
831                            FALSE,
832                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
833 }
834
835 /* Construction (new, new_with_path, etc.) {{{1 */
836 /**
837  * g_settings_new:
838  * @schema_id: the id of the schema
839  *
840  * Creates a new #GSettings object with the schema specified by
841  * @schema_id.
842  *
843  * Signals on the newly created #GSettings object will be dispatched
844  * via the thread-default #GMainContext in effect at the time of the
845  * call to g_settings_new().  The new #GSettings will hold a reference
846  * on the context.  See g_main_context_push_thread_default().
847  *
848  * Returns: a new #GSettings object
849  *
850  * Since: 2.26
851  */
852 GSettings *
853 g_settings_new (const gchar *schema_id)
854 {
855   g_return_val_if_fail (schema_id != NULL, NULL);
856
857   return g_object_new (G_TYPE_SETTINGS,
858                        "schema-id", schema_id,
859                        NULL);
860 }
861
862 /**
863  * g_settings_new_with_path:
864  * @schema_id: the id of the schema
865  * @path: the path to use
866  *
867  * Creates a new #GSettings object with the relocatable schema specified
868  * by @schema_id and a given path.
869  *
870  * You only need to do this if you want to directly create a settings
871  * object with a schema that doesn't have a specified path of its own.
872  * That's quite rare.
873  *
874  * It is a programmer error to call this function for a schema that
875  * has an explicitly specified path.
876  *
877  * Returns: a new #GSettings object
878  *
879  * Since: 2.26
880  */
881 GSettings *
882 g_settings_new_with_path (const gchar *schema_id,
883                           const gchar *path)
884 {
885   g_return_val_if_fail (schema_id != NULL, NULL);
886   g_return_val_if_fail (path != NULL, NULL);
887
888   return g_object_new (G_TYPE_SETTINGS,
889                        "schema-id", schema_id,
890                        "path", path,
891                        NULL);
892 }
893
894 /**
895  * g_settings_new_with_backend:
896  * @schema_id: the id of the schema
897  * @backend: the #GSettingsBackend to use
898  *
899  * Creates a new #GSettings object with the schema specified by
900  * @schema_id and a given #GSettingsBackend.
901  *
902  * Creating a #GSettings object with a different backend allows accessing
903  * settings from a database other than the usual one. For example, it may make
904  * sense to pass a backend corresponding to the "defaults" settings database on
905  * the system to get a settings object that modifies the system default
906  * settings instead of the settings for this user.
907  *
908  * Returns: a new #GSettings object
909  *
910  * Since: 2.26
911  */
912 GSettings *
913 g_settings_new_with_backend (const gchar      *schema_id,
914                              GSettingsBackend *backend)
915 {
916   g_return_val_if_fail (schema_id != NULL, NULL);
917   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
918
919   return g_object_new (G_TYPE_SETTINGS,
920                        "schema-id", schema_id,
921                        "backend", backend,
922                        NULL);
923 }
924
925 /**
926  * g_settings_new_with_backend_and_path:
927  * @schema_id: the id of the schema
928  * @backend: the #GSettingsBackend to use
929  * @path: the path to use
930  *
931  * Creates a new #GSettings object with the schema specified by
932  * @schema_id and a given #GSettingsBackend and path.
933  *
934  * This is a mix of g_settings_new_with_backend() and
935  * g_settings_new_with_path().
936  *
937  * Returns: a new #GSettings object
938  *
939  * Since: 2.26
940  */
941 GSettings *
942 g_settings_new_with_backend_and_path (const gchar      *schema_id,
943                                       GSettingsBackend *backend,
944                                       const gchar      *path)
945 {
946   g_return_val_if_fail (schema_id != NULL, NULL);
947   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
948   g_return_val_if_fail (path != NULL, NULL);
949
950   return g_object_new (G_TYPE_SETTINGS,
951                        "schema-id", schema_id,
952                        "backend", backend,
953                        "path", path,
954                        NULL);
955 }
956
957 /**
958  * g_settings_new_full:
959  * @schema: a #GSettingsSchema
960  * @backend: (allow-none): a #GSettingsBackend
961  * @path: (allow-none): the path to use
962  *
963  * Creates a new #GSettings object with a given schema, backend and
964  * path.
965  *
966  * It should be extremely rare that you ever want to use this function.
967  * It is made available for advanced use-cases (such as plugin systems
968  * that want to provide access to schemas loaded from custom locations,
969  * etc).
970  *
971  * At the most basic level, a #GSettings object is a pure composition of
972  * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
973  * backend, and a #GMainContext to which signals are dispatched.
974  *
975  * This constructor therefore gives you full control over constructing
976  * #GSettings instances.  The first 4 parameters are given directly as
977  * @schema, @backend and @path, and the main context is taken from the
978  * thread-default (as per g_settings_new()).
979  *
980  * If @backend is %NULL then the default backend is used.
981  *
982  * If @path is %NULL then the path from the schema is used.  It is an
983  * error f @path is %NULL and the schema has no path of its own or if
984  * @path is non-%NULL and not equal to the path that the schema does
985  * have.
986  *
987  * Returns: a new #GSettings object
988  *
989  * Since: 2.32
990  */
991 GSettings *
992 g_settings_new_full (GSettingsSchema  *schema,
993                      GSettingsBackend *backend,
994                      const gchar      *path)
995 {
996   return g_object_new (G_TYPE_SETTINGS,
997                        "settings-schema", schema,
998                        "backend", backend,
999                        "path", path,
1000                        NULL);
1001 }
1002
1003 /* Internal read/write utilities {{{1 */
1004 static gboolean
1005 g_settings_write_to_backend (GSettings          *settings,
1006                              GSettingsSchemaKey *key,
1007                              GVariant           *value)
1008 {
1009   gboolean success;
1010   gchar *path;
1011
1012   path = g_strconcat (settings->priv->path, key->name, NULL);
1013   success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1014   g_free (path);
1015
1016   return success;
1017 }
1018
1019 static GVariant *
1020 g_settings_read_from_backend (GSettings          *settings,
1021                               GSettingsSchemaKey *key)
1022 {
1023   GVariant *value;
1024   GVariant *fixup;
1025   gchar *path;
1026
1027   path = g_strconcat (settings->priv->path, key->name, NULL);
1028   value = g_settings_backend_read (settings->priv->backend, path, key->type, FALSE);
1029   g_free (path);
1030
1031   if (value != NULL)
1032     {
1033       fixup = g_settings_schema_key_range_fixup (key, value);
1034       g_variant_unref (value);
1035     }
1036   else
1037     fixup = NULL;
1038
1039   return fixup;
1040 }
1041
1042 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1043 /**
1044  * g_settings_get_value:
1045  * @settings: a #GSettings object
1046  * @key: the key to get the value for
1047  *
1048  * Gets the value that is stored in @settings for @key.
1049  *
1050  * It is a programmer error to give a @key that isn't contained in the
1051  * schema for @settings.
1052  *
1053  * Returns: a new #GVariant
1054  *
1055  * Since: 2.26
1056  */
1057 GVariant *
1058 g_settings_get_value (GSettings   *settings,
1059                       const gchar *key)
1060 {
1061   GSettingsSchemaKey skey;
1062   GVariant *value;
1063
1064   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1065   g_return_val_if_fail (key != NULL, NULL);
1066
1067   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1068   value = g_settings_read_from_backend (settings, &skey);
1069
1070   if (value == NULL)
1071     value = g_settings_schema_key_get_translated_default (&skey);
1072
1073   if (value == NULL)
1074     value = g_variant_ref (skey.default_value);
1075
1076   g_settings_schema_key_clear (&skey);
1077
1078   return value;
1079 }
1080
1081 /**
1082  * g_settings_get_enum:
1083  * @settings: a #GSettings object
1084  * @key: the key to get the value for
1085  *
1086  * Gets the value that is stored in @settings for @key and converts it
1087  * to the enum value that it represents.
1088  *
1089  * In order to use this function the type of the value must be a string
1090  * and it must be marked in the schema file as an enumerated type.
1091  *
1092  * It is a programmer error to give a @key that isn't contained in the
1093  * schema for @settings or is not marked as an enumerated type.
1094  *
1095  * If the value stored in the configuration database is not a valid
1096  * value for the enumerated type then this function will return the
1097  * default value.
1098  *
1099  * Returns: the enum value
1100  *
1101  * Since: 2.26
1102  **/
1103 gint
1104 g_settings_get_enum (GSettings   *settings,
1105                      const gchar *key)
1106 {
1107   GSettingsSchemaKey skey;
1108   GVariant *value;
1109   gint result;
1110
1111   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1112   g_return_val_if_fail (key != NULL, -1);
1113
1114   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1115
1116   if (!skey.is_enum)
1117     {
1118       g_critical ("g_settings_get_enum() called on key `%s' which is not "
1119                   "associated with an enumerated type", skey.name);
1120       g_settings_schema_key_clear (&skey);
1121       return -1;
1122     }
1123
1124   value = g_settings_read_from_backend (settings, &skey);
1125
1126   if (value == NULL)
1127     value = g_settings_schema_key_get_translated_default (&skey);
1128
1129   if (value == NULL)
1130     value = g_variant_ref (skey.default_value);
1131
1132   result = g_settings_schema_key_to_enum (&skey, value);
1133   g_settings_schema_key_clear (&skey);
1134   g_variant_unref (value);
1135
1136   return result;
1137 }
1138
1139 /**
1140  * g_settings_set_enum:
1141  * @settings: a #GSettings object
1142  * @key: a key, within @settings
1143  * @value: an enumerated value
1144  *
1145  * Looks up the enumerated type nick for @value and writes it to @key,
1146  * within @settings.
1147  *
1148  * It is a programmer error to give a @key that isn't contained in the
1149  * schema for @settings or is not marked as an enumerated type, or for
1150  * @value not to be a valid value for the named type.
1151  *
1152  * After performing the write, accessing @key directly with
1153  * g_settings_get_string() will return the 'nick' associated with
1154  * @value.
1155  *
1156  * Returns: %TRUE, if the set succeeds
1157  **/
1158 gboolean
1159 g_settings_set_enum (GSettings   *settings,
1160                      const gchar *key,
1161                      gint         value)
1162 {
1163   GSettingsSchemaKey skey;
1164   GVariant *variant;
1165   gboolean success;
1166
1167   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1168   g_return_val_if_fail (key != NULL, FALSE);
1169
1170   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1171
1172   if (!skey.is_enum)
1173     {
1174       g_critical ("g_settings_set_enum() called on key `%s' which is not "
1175                   "associated with an enumerated type", skey.name);
1176       return FALSE;
1177     }
1178
1179   if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1180     {
1181       g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1182                   "in schema `%s'.  Doing nothing.", value, skey.name,
1183                   g_settings_schema_get_id (skey.schema));
1184       g_settings_schema_key_clear (&skey);
1185       return FALSE;
1186     }
1187
1188   success = g_settings_write_to_backend (settings, &skey, variant);
1189   g_settings_schema_key_clear (&skey);
1190
1191   return success;
1192 }
1193
1194 /**
1195  * g_settings_get_flags:
1196  * @settings: a #GSettings object
1197  * @key: the key to get the value for
1198  *
1199  * Gets the value that is stored in @settings for @key and converts it
1200  * to the flags value that it represents.
1201  *
1202  * In order to use this function the type of the value must be an array
1203  * of strings and it must be marked in the schema file as an flags type.
1204  *
1205  * It is a programmer error to give a @key that isn't contained in the
1206  * schema for @settings or is not marked as a flags type.
1207  *
1208  * If the value stored in the configuration database is not a valid
1209  * value for the flags type then this function will return the default
1210  * value.
1211  *
1212  * Returns: the flags value
1213  *
1214  * Since: 2.26
1215  **/
1216 guint
1217 g_settings_get_flags (GSettings   *settings,
1218                       const gchar *key)
1219 {
1220   GSettingsSchemaKey skey;
1221   GVariant *value;
1222   guint result;
1223
1224   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1225   g_return_val_if_fail (key != NULL, -1);
1226
1227   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1228
1229   if (!skey.is_flags)
1230     {
1231       g_critical ("g_settings_get_flags() called on key `%s' which is not "
1232                   "associated with a flags type", skey.name);
1233       g_settings_schema_key_clear (&skey);
1234       return -1;
1235     }
1236
1237   value = g_settings_read_from_backend (settings, &skey);
1238
1239   if (value == NULL)
1240     value = g_settings_schema_key_get_translated_default (&skey);
1241
1242   if (value == NULL)
1243     value = g_variant_ref (skey.default_value);
1244
1245   result = g_settings_schema_key_to_flags (&skey, value);
1246   g_settings_schema_key_clear (&skey);
1247   g_variant_unref (value);
1248
1249   return result;
1250 }
1251
1252 /**
1253  * g_settings_set_flags:
1254  * @settings: a #GSettings object
1255  * @key: a key, within @settings
1256  * @value: a flags value
1257  *
1258  * Looks up the flags type nicks for the bits specified by @value, puts
1259  * them in an array of strings and writes the array to @key, within
1260  * @settings.
1261  *
1262  * It is a programmer error to give a @key that isn't contained in the
1263  * schema for @settings or is not marked as a flags type, or for @value
1264  * to contain any bits that are not value for the named type.
1265  *
1266  * After performing the write, accessing @key directly with
1267  * g_settings_get_strv() will return an array of 'nicks'; one for each
1268  * bit in @value.
1269  *
1270  * Returns: %TRUE, if the set succeeds
1271  **/
1272 gboolean
1273 g_settings_set_flags (GSettings   *settings,
1274                       const gchar *key,
1275                       guint        value)
1276 {
1277   GSettingsSchemaKey skey;
1278   GVariant *variant;
1279   gboolean success;
1280
1281   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1282   g_return_val_if_fail (key != NULL, FALSE);
1283
1284   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1285
1286   if (!skey.is_flags)
1287     {
1288       g_critical ("g_settings_set_flags() called on key `%s' which is not "
1289                   "associated with a flags type", skey.name);
1290       return FALSE;
1291     }
1292
1293   if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1294     {
1295       g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1296                   "for key `%s' in schema `%s'.  Doing nothing.",
1297                   value, skey.name, g_settings_schema_get_id (skey.schema));
1298       g_settings_schema_key_clear (&skey);
1299       return FALSE;
1300     }
1301
1302   success = g_settings_write_to_backend (settings, &skey, variant);
1303   g_settings_schema_key_clear (&skey);
1304
1305   return success;
1306 }
1307
1308 /**
1309  * g_settings_set_value:
1310  * @settings: a #GSettings object
1311  * @key: the name of the key to set
1312  * @value: a #GVariant of the correct type
1313  *
1314  * Sets @key in @settings to @value.
1315  *
1316  * It is a programmer error to give a @key that isn't contained in the
1317  * schema for @settings or for @value to have the incorrect type, per
1318  * the schema.
1319  *
1320  * If @value is floating then this function consumes the reference.
1321  *
1322  * Returns: %TRUE if setting the key succeeded,
1323  *     %FALSE if the key was not writable
1324  *
1325  * Since: 2.26
1326  **/
1327 gboolean
1328 g_settings_set_value (GSettings   *settings,
1329                       const gchar *key,
1330                       GVariant    *value)
1331 {
1332   GSettingsSchemaKey skey;
1333   gboolean success;
1334
1335   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1336   g_return_val_if_fail (key != NULL, FALSE);
1337
1338   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1339
1340   if (!g_settings_schema_key_type_check (&skey, value))
1341     {
1342       g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1343                   key,
1344                   g_settings_schema_get_id (settings->priv->schema),
1345                   g_variant_type_peek_string (skey.type),
1346                   g_variant_get_type_string (value));
1347
1348         return FALSE;
1349       }
1350
1351   if (!g_settings_schema_key_range_check (&skey, value))
1352     {
1353       g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1354                  "is outside of valid range",
1355                  key,
1356                  g_settings_schema_get_id (settings->priv->schema));
1357
1358         return FALSE;
1359     }
1360
1361   success = g_settings_write_to_backend (settings, &skey, value);
1362   g_settings_schema_key_clear (&skey);
1363
1364   return success;
1365 }
1366
1367 /**
1368  * g_settings_get:
1369  * @settings: a #GSettings object
1370  * @key: the key to get the value for
1371  * @format: a #GVariant format string
1372  * @...: arguments as per @format
1373  *
1374  * Gets the value that is stored at @key in @settings.
1375  *
1376  * A convenience function that combines g_settings_get_value() with
1377  * g_variant_get().
1378  *
1379  * It is a programmer error to give a @key that isn't contained in the
1380  * schema for @settings or for the #GVariantType of @format to mismatch
1381  * the type given in the schema.
1382  *
1383  * Since: 2.26
1384  */
1385 void
1386 g_settings_get (GSettings   *settings,
1387                 const gchar *key,
1388                 const gchar *format,
1389                 ...)
1390 {
1391   GVariant *value;
1392   va_list ap;
1393
1394   value = g_settings_get_value (settings, key);
1395
1396   va_start (ap, format);
1397   g_variant_get_va (value, format, NULL, &ap);
1398   va_end (ap);
1399
1400   g_variant_unref (value);
1401 }
1402
1403 /**
1404  * g_settings_set:
1405  * @settings: a #GSettings object
1406  * @key: the name of the key to set
1407  * @format: a #GVariant format string
1408  * @...: arguments as per @format
1409  *
1410  * Sets @key in @settings to @value.
1411  *
1412  * A convenience function that combines g_settings_set_value() with
1413  * g_variant_new().
1414  *
1415  * It is a programmer error to give a @key that isn't contained in the
1416  * schema for @settings or for the #GVariantType of @format to mismatch
1417  * the type given in the schema.
1418  *
1419  * Returns: %TRUE if setting the key succeeded,
1420  *     %FALSE if the key was not writable
1421  *
1422  * Since: 2.26
1423  */
1424 gboolean
1425 g_settings_set (GSettings   *settings,
1426                 const gchar *key,
1427                 const gchar *format,
1428                 ...)
1429 {
1430   GVariant *value;
1431   va_list ap;
1432
1433   va_start (ap, format);
1434   value = g_variant_new_va (format, NULL, &ap);
1435   va_end (ap);
1436
1437   return g_settings_set_value (settings, key, value);
1438 }
1439
1440 /**
1441  * g_settings_get_mapped:
1442  * @settings: a #GSettings object
1443  * @key: the key to get the value for
1444  * @mapping: (scope call): the function to map the value in the
1445  *           settings database to the value used by the application
1446  * @user_data: user data for @mapping
1447  *
1448  * Gets the value that is stored at @key in @settings, subject to
1449  * application-level validation/mapping.
1450  *
1451  * You should use this function when the application needs to perform
1452  * some processing on the value of the key (for example, parsing).  The
1453  * @mapping function performs that processing.  If the function
1454  * indicates that the processing was unsuccessful (due to a parse error,
1455  * for example) then the mapping is tried again with another value.
1456  *
1457  * This allows a robust 'fall back to defaults' behaviour to be
1458  * implemented somewhat automatically.
1459  *
1460  * The first value that is tried is the user's setting for the key.  If
1461  * the mapping function fails to map this value, other values may be
1462  * tried in an unspecified order (system or site defaults, translated
1463  * schema default values, untranslated schema default values, etc).
1464  *
1465  * If the mapping function fails for all possible values, one additional
1466  * attempt is made: the mapping function is called with a %NULL value.
1467  * If the mapping function still indicates failure at this point then
1468  * the application will be aborted.
1469  *
1470  * The result parameter for the @mapping function is pointed to a
1471  * #gpointer which is initially set to %NULL.  The same pointer is given
1472  * to each invocation of @mapping.  The final value of that #gpointer is
1473  * what is returned by this function.  %NULL is valid; it is returned
1474  * just as any other value would be.
1475  *
1476  * Returns: (transfer full): the result, which may be %NULL
1477  **/
1478 gpointer
1479 g_settings_get_mapped (GSettings           *settings,
1480                        const gchar         *key,
1481                        GSettingsGetMapping  mapping,
1482                        gpointer             user_data)
1483 {
1484   gpointer result = NULL;
1485   GSettingsSchemaKey skey;
1486   GVariant *value;
1487   gboolean okay;
1488
1489   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1490   g_return_val_if_fail (key != NULL, NULL);
1491   g_return_val_if_fail (mapping != NULL, NULL);
1492
1493   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1494
1495   if ((value = g_settings_read_from_backend (settings, &skey)))
1496     {
1497       okay = mapping (value, &result, user_data);
1498       g_variant_unref (value);
1499       if (okay) goto okay;
1500     }
1501
1502   if ((value = g_settings_schema_key_get_translated_default (&skey)))
1503     {
1504       okay = mapping (value, &result, user_data);
1505       g_variant_unref (value);
1506       if (okay) goto okay;
1507     }
1508
1509   if (mapping (skey.default_value, &result, user_data))
1510     goto okay;
1511
1512   if (!mapping (NULL, &result, user_data))
1513     g_error ("The mapping function given to g_settings_get_mapped() for key "
1514              "`%s' in schema `%s' returned FALSE when given a NULL value.",
1515              key, g_settings_schema_get_id (settings->priv->schema));
1516
1517  okay:
1518   g_settings_schema_key_clear (&skey);
1519
1520   return result;
1521 }
1522
1523 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1524 /**
1525  * g_settings_get_string:
1526  * @settings: a #GSettings object
1527  * @key: the key to get the value for
1528  *
1529  * Gets the value that is stored at @key in @settings.
1530  *
1531  * A convenience variant of g_settings_get() for strings.
1532  *
1533  * It is a programmer error to give a @key that isn't specified as
1534  * having a string type in the schema for @settings.
1535  *
1536  * Returns: a newly-allocated string
1537  *
1538  * Since: 2.26
1539  */
1540 gchar *
1541 g_settings_get_string (GSettings   *settings,
1542                        const gchar *key)
1543 {
1544   GVariant *value;
1545   gchar *result;
1546
1547   value = g_settings_get_value (settings, key);
1548   result = g_variant_dup_string (value, NULL);
1549   g_variant_unref (value);
1550
1551   return result;
1552 }
1553
1554 /**
1555  * g_settings_set_string:
1556  * @settings: a #GSettings object
1557  * @key: the name of the key to set
1558  * @value: the value to set it to
1559  *
1560  * Sets @key in @settings to @value.
1561  *
1562  * A convenience variant of g_settings_set() for strings.
1563  *
1564  * It is a programmer error to give a @key that isn't specified as
1565  * having a string type in the schema for @settings.
1566  *
1567  * Returns: %TRUE if setting the key succeeded,
1568  *     %FALSE if the key was not writable
1569  *
1570  * Since: 2.26
1571  */
1572 gboolean
1573 g_settings_set_string (GSettings   *settings,
1574                        const gchar *key,
1575                        const gchar *value)
1576 {
1577   return g_settings_set_value (settings, key, g_variant_new_string (value));
1578 }
1579
1580 /**
1581  * g_settings_get_int:
1582  * @settings: a #GSettings object
1583  * @key: the key to get the value for
1584  *
1585  * Gets the value that is stored at @key in @settings.
1586  *
1587  * A convenience variant of g_settings_get() for 32-bit integers.
1588  *
1589  * It is a programmer error to give a @key that isn't specified as
1590  * having a int32 type in the schema for @settings.
1591  *
1592  * Returns: an integer
1593  *
1594  * Since: 2.26
1595  */
1596 gint
1597 g_settings_get_int (GSettings   *settings,
1598                     const gchar *key)
1599 {
1600   GVariant *value;
1601   gint result;
1602
1603   value = g_settings_get_value (settings, key);
1604   result = g_variant_get_int32 (value);
1605   g_variant_unref (value);
1606
1607   return result;
1608 }
1609
1610 /**
1611  * g_settings_set_int:
1612  * @settings: a #GSettings object
1613  * @key: the name of the key to set
1614  * @value: the value to set it to
1615  *
1616  * Sets @key in @settings to @value.
1617  *
1618  * A convenience variant of g_settings_set() for 32-bit integers.
1619  *
1620  * It is a programmer error to give a @key that isn't specified as
1621  * having a int32 type in the schema for @settings.
1622  *
1623  * Returns: %TRUE if setting the key succeeded,
1624  *     %FALSE if the key was not writable
1625  *
1626  * Since: 2.26
1627  */
1628 gboolean
1629 g_settings_set_int (GSettings   *settings,
1630                     const gchar *key,
1631                     gint         value)
1632 {
1633   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1634 }
1635
1636 /**
1637  * g_settings_get_uint:
1638  * @settings: a #GSettings object
1639  * @key: the key to get the value for
1640  *
1641  * Gets the value that is stored at @key in @settings.
1642  *
1643  * A convenience variant of g_settings_get() for 32-bit unsigned
1644  * integers.
1645  *
1646  * It is a programmer error to give a @key that isn't specified as
1647  * having a uint32 type in the schema for @settings.
1648  *
1649  * Returns: an unsigned integer
1650  *
1651  * Since: 2.30
1652  */
1653 guint
1654 g_settings_get_uint (GSettings   *settings,
1655                      const gchar *key)
1656 {
1657   GVariant *value;
1658   guint result;
1659
1660   value = g_settings_get_value (settings, key);
1661   result = g_variant_get_uint32 (value);
1662   g_variant_unref (value);
1663
1664   return result;
1665 }
1666
1667 /**
1668  * g_settings_set_uint:
1669  * @settings: a #GSettings object
1670  * @key: the name of the key to set
1671  * @value: the value to set it to
1672  *
1673  * Sets @key in @settings to @value.
1674  *
1675  * A convenience variant of g_settings_set() for 32-bit unsigned
1676  * integers.
1677  *
1678  * It is a programmer error to give a @key that isn't specified as
1679  * having a uint32 type in the schema for @settings.
1680  *
1681  * Returns: %TRUE if setting the key succeeded,
1682  *     %FALSE if the key was not writable
1683  *
1684  * Since: 2.30
1685  */
1686 gboolean
1687 g_settings_set_uint (GSettings   *settings,
1688                      const gchar *key,
1689                      guint        value)
1690 {
1691   return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1692 }
1693
1694 /**
1695  * g_settings_get_double:
1696  * @settings: a #GSettings object
1697  * @key: the key to get the value for
1698  *
1699  * Gets the value that is stored at @key in @settings.
1700  *
1701  * A convenience variant of g_settings_get() for doubles.
1702  *
1703  * It is a programmer error to give a @key that isn't specified as
1704  * having a 'double' type in the schema for @settings.
1705  *
1706  * Returns: a double
1707  *
1708  * Since: 2.26
1709  */
1710 gdouble
1711 g_settings_get_double (GSettings   *settings,
1712                        const gchar *key)
1713 {
1714   GVariant *value;
1715   gdouble result;
1716
1717   value = g_settings_get_value (settings, key);
1718   result = g_variant_get_double (value);
1719   g_variant_unref (value);
1720
1721   return result;
1722 }
1723
1724 /**
1725  * g_settings_set_double:
1726  * @settings: a #GSettings object
1727  * @key: the name of the key to set
1728  * @value: the value to set it to
1729  *
1730  * Sets @key in @settings to @value.
1731  *
1732  * A convenience variant of g_settings_set() for doubles.
1733  *
1734  * It is a programmer error to give a @key that isn't specified as
1735  * having a 'double' type in the schema for @settings.
1736  *
1737  * Returns: %TRUE if setting the key succeeded,
1738  *     %FALSE if the key was not writable
1739  *
1740  * Since: 2.26
1741  */
1742 gboolean
1743 g_settings_set_double (GSettings   *settings,
1744                        const gchar *key,
1745                        gdouble      value)
1746 {
1747   return g_settings_set_value (settings, key, g_variant_new_double (value));
1748 }
1749
1750 /**
1751  * g_settings_get_boolean:
1752  * @settings: a #GSettings object
1753  * @key: the key to get the value for
1754  *
1755  * Gets the value that is stored at @key in @settings.
1756  *
1757  * A convenience variant of g_settings_get() for booleans.
1758  *
1759  * It is a programmer error to give a @key that isn't specified as
1760  * having a boolean type in the schema for @settings.
1761  *
1762  * Returns: a boolean
1763  *
1764  * Since: 2.26
1765  */
1766 gboolean
1767 g_settings_get_boolean (GSettings  *settings,
1768                        const gchar *key)
1769 {
1770   GVariant *value;
1771   gboolean result;
1772
1773   value = g_settings_get_value (settings, key);
1774   result = g_variant_get_boolean (value);
1775   g_variant_unref (value);
1776
1777   return result;
1778 }
1779
1780 /**
1781  * g_settings_set_boolean:
1782  * @settings: a #GSettings object
1783  * @key: the name of the key to set
1784  * @value: the value to set it to
1785  *
1786  * Sets @key in @settings to @value.
1787  *
1788  * A convenience variant of g_settings_set() for booleans.
1789  *
1790  * It is a programmer error to give a @key that isn't specified as
1791  * having a boolean type in the schema for @settings.
1792  *
1793  * Returns: %TRUE if setting the key succeeded,
1794  *     %FALSE if the key was not writable
1795  *
1796  * Since: 2.26
1797  */
1798 gboolean
1799 g_settings_set_boolean (GSettings  *settings,
1800                        const gchar *key,
1801                        gboolean     value)
1802 {
1803   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1804 }
1805
1806 /**
1807  * g_settings_get_strv:
1808  * @settings: a #GSettings object
1809  * @key: the key to get the value for
1810  *
1811  * A convenience variant of g_settings_get() for string arrays.
1812  *
1813  * It is a programmer error to give a @key that isn't specified as
1814  * having an array of strings type in the schema for @settings.
1815  *
1816  * Returns: (array zero-terminated=1) (transfer full): a
1817  * newly-allocated, %NULL-terminated array of strings, the value that
1818  * is stored at @key in @settings.
1819  *
1820  * Since: 2.26
1821  */
1822 gchar **
1823 g_settings_get_strv (GSettings   *settings,
1824                      const gchar *key)
1825 {
1826   GVariant *value;
1827   gchar **result;
1828
1829   value = g_settings_get_value (settings, key);
1830   result = g_variant_dup_strv (value, NULL);
1831   g_variant_unref (value);
1832
1833   return result;
1834 }
1835
1836 /**
1837  * g_settings_set_strv:
1838  * @settings: a #GSettings object
1839  * @key: the name of the key to set
1840  * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1841  *
1842  * Sets @key in @settings to @value.
1843  *
1844  * A convenience variant of g_settings_set() for string arrays.  If
1845  * @value is %NULL, then @key is set to be the empty array.
1846  *
1847  * It is a programmer error to give a @key that isn't specified as
1848  * having an array of strings type in the schema for @settings.
1849  *
1850  * Returns: %TRUE if setting the key succeeded,
1851  *     %FALSE if the key was not writable
1852  *
1853  * Since: 2.26
1854  */
1855 gboolean
1856 g_settings_set_strv (GSettings           *settings,
1857                      const gchar         *key,
1858                      const gchar * const *value)
1859 {
1860   GVariant *array;
1861
1862   if (value != NULL)
1863     array = g_variant_new_strv (value, -1);
1864   else
1865     array = g_variant_new_strv (NULL, 0);
1866
1867   return g_settings_set_value (settings, key, array);
1868 }
1869
1870 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1871 /**
1872  * g_settings_delay:
1873  * @settings: a #GSettings object
1874  *
1875  * Changes the #GSettings object into 'delay-apply' mode. In this
1876  * mode, changes to @settings are not immediately propagated to the
1877  * backend, but kept locally until g_settings_apply() is called.
1878  *
1879  * Since: 2.26
1880  */
1881 void
1882 g_settings_delay (GSettings *settings)
1883 {
1884   g_return_if_fail (G_IS_SETTINGS (settings));
1885
1886   if (settings->priv->delayed)
1887     return;
1888
1889   settings->priv->delayed =
1890     g_delayed_settings_backend_new (settings->priv->backend,
1891                                     settings,
1892                                     settings->priv->main_context);
1893   g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1894   g_object_unref (settings->priv->backend);
1895
1896   settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1897   g_settings_backend_watch (settings->priv->backend,
1898                             &listener_vtable, G_OBJECT (settings),
1899                             settings->priv->main_context);
1900
1901   g_object_notify (G_OBJECT (settings), "delay-apply");
1902 }
1903
1904 /**
1905  * g_settings_apply:
1906  * @settings: a #GSettings instance
1907  *
1908  * Applies any changes that have been made to the settings.  This
1909  * function does nothing unless @settings is in 'delay-apply' mode;
1910  * see g_settings_delay().  In the normal case settings are always
1911  * applied immediately.
1912  **/
1913 void
1914 g_settings_apply (GSettings *settings)
1915 {
1916   if (settings->priv->delayed)
1917     {
1918       GDelayedSettingsBackend *delayed;
1919
1920       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1921       g_delayed_settings_backend_apply (delayed);
1922     }
1923 }
1924
1925 /**
1926  * g_settings_revert:
1927  * @settings: a #GSettings instance
1928  *
1929  * Reverts all non-applied changes to the settings.  This function
1930  * does nothing unless @settings is in 'delay-apply' mode; see
1931  * g_settings_delay().  In the normal case settings are always applied
1932  * immediately.
1933  *
1934  * Change notifications will be emitted for affected keys.
1935  **/
1936 void
1937 g_settings_revert (GSettings *settings)
1938 {
1939   if (settings->priv->delayed)
1940     {
1941       GDelayedSettingsBackend *delayed;
1942
1943       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1944       g_delayed_settings_backend_revert (delayed);
1945     }
1946 }
1947
1948 /**
1949  * g_settings_get_has_unapplied:
1950  * @settings: a #GSettings object
1951  *
1952  * Returns whether the #GSettings object has any unapplied
1953  * changes.  This can only be the case if it is in 'delayed-apply' mode.
1954  *
1955  * Returns: %TRUE if @settings has unapplied changes
1956  *
1957  * Since: 2.26
1958  */
1959 gboolean
1960 g_settings_get_has_unapplied (GSettings *settings)
1961 {
1962   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1963
1964   return settings->priv->delayed &&
1965          g_delayed_settings_backend_get_has_unapplied (
1966            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1967 }
1968
1969 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
1970 /**
1971  * g_settings_reset:
1972  * @settings: a #GSettings object
1973  * @key: the name of a key
1974  *
1975  * Resets @key to its default value.
1976  *
1977  * This call resets the key, as much as possible, to its default value.
1978  * That might the value specified in the schema or the one set by the
1979  * administrator.
1980  **/
1981 void
1982 g_settings_reset (GSettings *settings,
1983                   const gchar *key)
1984 {
1985   gchar *path;
1986
1987   path = g_strconcat (settings->priv->path, key, NULL);
1988   g_settings_backend_reset (settings->priv->backend, path, NULL);
1989   g_free (path);
1990 }
1991
1992 /**
1993  * g_settings_sync:
1994  *
1995  * Ensures that all pending operations for the given are complete for
1996  * the default backend.
1997  *
1998  * Writes made to a #GSettings are handled asynchronously.  For this
1999  * reason, it is very unlikely that the changes have it to disk by the
2000  * time g_settings_set() returns.
2001  *
2002  * This call will block until all of the writes have made it to the
2003  * backend.  Since the mainloop is not running, no change notifications
2004  * will be dispatched during this call (but some may be queued by the
2005  * time the call is done).
2006  **/
2007 void
2008 g_settings_sync (void)
2009 {
2010   g_settings_backend_sync_default ();
2011 }
2012
2013 /**
2014  * g_settings_is_writable:
2015  * @settings: a #GSettings object
2016  * @name: the name of a key
2017  *
2018  * Finds out if a key can be written or not
2019  *
2020  * Returns: %TRUE if the key @name is writable
2021  *
2022  * Since: 2.26
2023  */
2024 gboolean
2025 g_settings_is_writable (GSettings   *settings,
2026                         const gchar *name)
2027 {
2028   gboolean writable;
2029   gchar *path;
2030
2031   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2032
2033   path = g_strconcat (settings->priv->path, name, NULL);
2034   writable = g_settings_backend_get_writable (settings->priv->backend, path);
2035   g_free (path);
2036
2037   return writable;
2038 }
2039
2040 /**
2041  * g_settings_get_child:
2042  * @settings: a #GSettings object
2043  * @name: the name of the 'child' schema
2044  *
2045  * Creates a 'child' settings object which has a base path of
2046  * <replaceable>base-path</replaceable>/@name, where
2047  * <replaceable>base-path</replaceable> is the base path of @settings.
2048  *
2049  * The schema for the child settings object must have been declared
2050  * in the schema of @settings using a <tag class="starttag">child</tag> element.
2051  *
2052  * Returns: (transfer full): a 'child' settings object
2053  *
2054  * Since: 2.26
2055  */
2056 GSettings *
2057 g_settings_get_child (GSettings   *settings,
2058                       const gchar *name)
2059 {
2060   const gchar *child_schema;
2061   gchar *child_path;
2062   gchar *child_name;
2063   GSettings *child;
2064
2065   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2066
2067   child_name = g_strconcat (name, "/", NULL);
2068   child_schema = g_settings_schema_get_string (settings->priv->schema,
2069                                                child_name);
2070   if (child_schema == NULL)
2071     g_error ("Schema '%s' has no child '%s'",
2072              g_settings_schema_get_id (settings->priv->schema), name);
2073
2074   child_path = g_strconcat (settings->priv->path, child_name, NULL);
2075   child = g_object_new (G_TYPE_SETTINGS,
2076                         "schema-id", child_schema,
2077                         "path", child_path,
2078                         NULL);
2079   g_free (child_path);
2080   g_free (child_name);
2081
2082   return child;
2083 }
2084
2085 /**
2086  * g_settings_list_keys:
2087  * @settings: a #GSettings object
2088  *
2089  * Introspects the list of keys on @settings.
2090  *
2091  * You should probably not be calling this function from "normal" code
2092  * (since you should already know what keys are in your schema).  This
2093  * function is intended for introspection reasons.
2094  *
2095  * You should free the return value with g_strfreev() when you are done
2096  * with it.
2097  *
2098  * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2099  */
2100 gchar **
2101 g_settings_list_keys (GSettings *settings)
2102 {
2103   const GQuark *keys;
2104   gchar **strv;
2105   gint n_keys;
2106   gint i, j;
2107
2108   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2109   strv = g_new (gchar *, n_keys + 1);
2110   for (i = j = 0; i < n_keys; i++)
2111     {
2112       const gchar *key = g_quark_to_string (keys[i]);
2113
2114       if (!g_str_has_suffix (key, "/"))
2115         strv[j++] = g_strdup (key);
2116     }
2117   strv[j] = NULL;
2118
2119   return strv;
2120 }
2121
2122 /**
2123  * g_settings_list_children:
2124  * @settings: a #GSettings object
2125  *
2126  * Gets the list of children on @settings.
2127  *
2128  * The list is exactly the list of strings for which it is not an error
2129  * to call g_settings_get_child().
2130  *
2131  * For GSettings objects that are lists, this value can change at any
2132  * time and you should connect to the "children-changed" signal to watch
2133  * for those changes.  Note that there is a race condition here: you may
2134  * request a child after listing it only for it to have been destroyed
2135  * in the meantime.  For this reason, g_settings_get_child() may return
2136  * %NULL even for a child that was listed by this function.
2137  *
2138  * For GSettings objects that are not lists, you should probably not be
2139  * calling this function from "normal" code (since you should already
2140  * know what children are in your schema).  This function may still be
2141  * useful there for introspection reasons, however.
2142  *
2143  * You should free the return value with g_strfreev() when you are done
2144  * with it.
2145  *
2146  * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2147  */
2148 gchar **
2149 g_settings_list_children (GSettings *settings)
2150 {
2151   const GQuark *keys;
2152   gchar **strv;
2153   gint n_keys;
2154   gint i, j;
2155
2156   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2157   strv = g_new (gchar *, n_keys + 1);
2158   for (i = j = 0; i < n_keys; i++)
2159     {
2160       const gchar *key = g_quark_to_string (keys[i]);
2161
2162       if (g_str_has_suffix (key, "/"))
2163         {
2164           gint length = strlen (key);
2165
2166           strv[j] = g_memdup (key, length);
2167           strv[j][length - 1] = '\0';
2168           j++;
2169         }
2170     }
2171   strv[j] = NULL;
2172
2173   return strv;
2174 }
2175
2176 /**
2177  * g_settings_get_range:
2178  * @settings: a #GSettings
2179  * @key: the key to query the range of
2180  *
2181  * Queries the range of a key.
2182  *
2183  * This function will return a #GVariant that fully describes the range
2184  * of values that are valid for @key.
2185  *
2186  * The type of #GVariant returned is <literal>(sv)</literal>.  The
2187  * string describes the type of range restriction in effect.  The type
2188  * and meaning of the value contained in the variant depends on the
2189  * string.
2190  *
2191  * If the string is <literal>'type'</literal> then the variant contains
2192  * an empty array.  The element type of that empty array is the expected
2193  * type of value and all values of that type are valid.
2194  *
2195  * If the string is <literal>'enum'</literal> then the variant contains
2196  * an array enumerating the possible values.  Each item in the array is
2197  * a possible valid value and no other values are valid.
2198  *
2199  * If the string is <literal>'flags'</literal> then the variant contains
2200  * an array.  Each item in the array is a value that may appear zero or
2201  * one times in an array to be used as the value for this key.  For
2202  * example, if the variant contained the array <literal>['x',
2203  * 'y']</literal> then the valid values for the key would be
2204  * <literal>[]</literal>, <literal>['x']</literal>,
2205  * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2206  * <literal>['y', 'x']</literal>.
2207  *
2208  * Finally, if the string is <literal>'range'</literal> then the variant
2209  * contains a pair of like-typed values -- the minimum and maximum
2210  * permissible values for this key.
2211  *
2212  * This information should not be used by normal programs.  It is
2213  * considered to be a hint for introspection purposes.  Normal programs
2214  * should already know what is permitted by their own schema.  The
2215  * format may change in any way in the future -- but particularly, new
2216  * forms may be added to the possibilities described above.
2217  *
2218  * It is a programmer error to give a @key that isn't contained in the
2219  * schema for @settings.
2220  *
2221  * You should free the returned value with g_variant_unref() when it is
2222  * no longer needed.
2223  *
2224  * Returns: a #GVariant describing the range
2225  *
2226  * Since: 2.28
2227  **/
2228 GVariant *
2229 g_settings_get_range (GSettings   *settings,
2230                       const gchar *key)
2231 {
2232   GSettingsSchemaKey skey;
2233   const gchar *type;
2234   GVariant *range;
2235
2236   g_settings_schema_key_init (&skey, settings->priv->schema, key);
2237
2238   if (skey.minimum)
2239     {
2240       range = g_variant_new ("(**)", skey.minimum, skey.maximum);
2241       type = "range";
2242     }
2243   else if (skey.strinfo)
2244     {
2245       range = strinfo_enumerate (skey.strinfo, skey.strinfo_length);
2246       type = skey.is_flags ? "flags" : "enum";
2247     }
2248   else
2249     {
2250       range = g_variant_new_array (skey.type, NULL, 0);
2251       type = "type";
2252     }
2253
2254   g_settings_schema_key_clear (&skey);
2255
2256   return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2257 }
2258
2259 /**
2260  * g_settings_range_check:
2261  * @settings: a #GSettings
2262  * @key: the key to check
2263  * @value: the value to check
2264  *
2265  * Checks if the given @value is of the correct type and within the
2266  * permitted range for @key.
2267  *
2268  * This API is not intended to be used by normal programs -- they should
2269  * already know what is permitted by their own schemas.  This API is
2270  * meant to be used by programs such as editors or commandline tools.
2271  *
2272  * It is a programmer error to give a @key that isn't contained in the
2273  * schema for @settings.
2274  *
2275  * Returns: %TRUE if @value is valid for @key
2276  *
2277  * Since: 2.28
2278  **/
2279 gboolean
2280 g_settings_range_check (GSettings   *settings,
2281                         const gchar *key,
2282                         GVariant    *value)
2283 {
2284   GSettingsSchemaKey skey;
2285   gboolean good;
2286
2287   g_settings_schema_key_init (&skey, settings->priv->schema, key);
2288   good = g_settings_schema_key_type_check (&skey, value) &&
2289          g_settings_schema_key_range_check (&skey, value);
2290   g_settings_schema_key_clear (&skey);
2291
2292   return good;
2293 }
2294
2295 /* Binding {{{1 */
2296 typedef struct
2297 {
2298   GSettingsSchemaKey key;
2299   GSettings *settings;
2300   GObject *object;
2301
2302   GSettingsBindGetMapping get_mapping;
2303   GSettingsBindSetMapping set_mapping;
2304   gpointer user_data;
2305   GDestroyNotify destroy;
2306
2307   guint writable_handler_id;
2308   guint property_handler_id;
2309   const GParamSpec *property;
2310   guint key_handler_id;
2311
2312   /* prevent recursion */
2313   gboolean running;
2314 } GSettingsBinding;
2315
2316 static void
2317 g_settings_binding_free (gpointer data)
2318 {
2319   GSettingsBinding *binding = data;
2320
2321   g_assert (!binding->running);
2322
2323   if (binding->writable_handler_id)
2324     g_signal_handler_disconnect (binding->settings,
2325                                  binding->writable_handler_id);
2326
2327   if (binding->key_handler_id)
2328     g_signal_handler_disconnect (binding->settings,
2329                                  binding->key_handler_id);
2330
2331   if (g_signal_handler_is_connected (binding->object,
2332                                      binding->property_handler_id))
2333   g_signal_handler_disconnect (binding->object,
2334                                binding->property_handler_id);
2335
2336   g_settings_schema_key_clear (&binding->key);
2337
2338   if (binding->destroy)
2339     binding->destroy (binding->user_data);
2340
2341   g_object_unref (binding->settings);
2342
2343   g_slice_free (GSettingsBinding, binding);
2344 }
2345
2346 static GQuark
2347 g_settings_binding_quark (const char *property)
2348 {
2349   GQuark quark;
2350   gchar *tmp;
2351
2352   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2353   quark = g_quark_from_string (tmp);
2354   g_free (tmp);
2355
2356   return quark;
2357 }
2358
2359 static void
2360 g_settings_binding_key_changed (GSettings   *settings,
2361                                 const gchar *key,
2362                                 gpointer     user_data)
2363 {
2364   GSettingsBinding *binding = user_data;
2365   GValue value = G_VALUE_INIT;
2366   GVariant *variant;
2367
2368   g_assert (settings == binding->settings);
2369   g_assert (key == binding->key.name);
2370
2371   if (binding->running)
2372     return;
2373
2374   binding->running = TRUE;
2375
2376   g_value_init (&value, binding->property->value_type);
2377
2378   variant = g_settings_read_from_backend (binding->settings, &binding->key);
2379   if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2380     {
2381       /* silently ignore errors in the user's config database */
2382       g_variant_unref (variant);
2383       variant = NULL;
2384     }
2385
2386   if (variant == NULL)
2387     {
2388       variant = g_settings_schema_key_get_translated_default (&binding->key);
2389       if (variant &&
2390           !binding->get_mapping (&value, variant, binding->user_data))
2391         {
2392           /* flag translation errors with a warning */
2393           g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2394                      "was rejected by the binding mapping function",
2395                      binding->key.unparsed, binding->key.name,
2396                      g_settings_schema_get_id (binding->key.schema));
2397           g_variant_unref (variant);
2398           variant = NULL;
2399         }
2400     }
2401
2402   if (variant == NULL)
2403     {
2404       variant = g_variant_ref (binding->key.default_value);
2405       if (!binding->get_mapping (&value, variant, binding->user_data))
2406         g_error ("The schema default value for key `%s' in schema `%s' "
2407                  "was rejected by the binding mapping function.",
2408                  binding->key.name, g_settings_schema_get_id (binding->key.schema));
2409     }
2410
2411   g_object_set_property (binding->object, binding->property->name, &value);
2412   g_variant_unref (variant);
2413   g_value_unset (&value);
2414
2415   binding->running = FALSE;
2416 }
2417
2418 static void
2419 g_settings_binding_property_changed (GObject          *object,
2420                                      const GParamSpec *pspec,
2421                                      gpointer          user_data)
2422 {
2423   GSettingsBinding *binding = user_data;
2424   GValue value = G_VALUE_INIT;
2425   GVariant *variant;
2426
2427   g_assert (object == binding->object);
2428   g_assert (pspec == binding->property);
2429
2430   if (binding->running)
2431     return;
2432
2433   binding->running = TRUE;
2434
2435   g_value_init (&value, pspec->value_type);
2436   g_object_get_property (object, pspec->name, &value);
2437   if ((variant = binding->set_mapping (&value, binding->key.type,
2438                                        binding->user_data)))
2439     {
2440       g_variant_take_ref (variant);
2441
2442       if (!g_settings_schema_key_type_check (&binding->key, variant))
2443         {
2444           g_critical ("binding mapping function for key `%s' returned "
2445                       "GVariant of type `%s' when type `%s' was requested",
2446                       binding->key.name, g_variant_get_type_string (variant),
2447                       g_variant_type_dup_string (binding->key.type));
2448           return;
2449         }
2450
2451       if (!g_settings_schema_key_range_check (&binding->key, variant))
2452         {
2453           g_critical ("GObject property `%s' on a `%s' object is out of "
2454                       "schema-specified range for key `%s' of `%s': %s",
2455                       binding->property->name, g_type_name (binding->property->owner_type),
2456                       binding->key.name, g_settings_schema_get_id (binding->key.schema),
2457                       g_variant_print (variant, TRUE));
2458           return;
2459         }
2460
2461       g_settings_write_to_backend (binding->settings, &binding->key, variant);
2462       g_variant_unref (variant);
2463     }
2464   g_value_unset (&value);
2465
2466   binding->running = FALSE;
2467 }
2468
2469 static gboolean
2470 g_settings_bind_invert_boolean_get_mapping (GValue   *value,
2471                                             GVariant *variant,
2472                                             gpointer  user_data)
2473 {
2474   g_value_set_boolean (value, !g_variant_get_boolean (variant));
2475   return TRUE;
2476 }
2477
2478 static GVariant *
2479 g_settings_bind_invert_boolean_set_mapping (const GValue       *value,
2480                                             const GVariantType *expected_type,
2481                                             gpointer            user_data)
2482 {
2483   return g_variant_new_boolean (!g_value_get_boolean (value));
2484 }
2485
2486 /**
2487  * g_settings_bind:
2488  * @settings: a #GSettings object
2489  * @key: the key to bind
2490  * @object: (type GObject.Object): a #GObject
2491  * @property: the name of the property to bind
2492  * @flags: flags for the binding
2493  *
2494  * Create a binding between the @key in the @settings object
2495  * and the property @property of @object.
2496  *
2497  * The binding uses the default GIO mapping functions to map
2498  * between the settings and property values. These functions
2499  * handle booleans, numeric types and string types in a
2500  * straightforward way. Use g_settings_bind_with_mapping() if
2501  * you need a custom mapping, or map between types that are not
2502  * supported by the default mapping functions.
2503  *
2504  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2505  * function also establishes a binding between the writability of
2506  * @key and the "sensitive" property of @object (if @object has
2507  * a boolean property by that name). See g_settings_bind_writable()
2508  * for more details about writable bindings.
2509  *
2510  * Note that the lifecycle of the binding is tied to the object,
2511  * and that you can have only one binding per object property.
2512  * If you bind the same property twice on the same object, the second
2513  * binding overrides the first one.
2514  *
2515  * Since: 2.26
2516  */
2517 void
2518 g_settings_bind (GSettings          *settings,
2519                  const gchar        *key,
2520                  gpointer            object,
2521                  const gchar        *property,
2522                  GSettingsBindFlags  flags)
2523 {
2524   GSettingsBindGetMapping get_mapping = NULL;
2525   GSettingsBindSetMapping set_mapping = NULL;
2526
2527   if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2528     {
2529       get_mapping = g_settings_bind_invert_boolean_get_mapping;
2530       set_mapping = g_settings_bind_invert_boolean_set_mapping;
2531
2532       /* can't pass this flag to g_settings_bind_with_mapping() */
2533       flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2534     }
2535
2536   g_settings_bind_with_mapping (settings, key, object, property, flags,
2537                                 get_mapping, set_mapping, NULL, NULL);
2538 }
2539
2540 /**
2541  * g_settings_bind_with_mapping: (skip)
2542  * @settings: a #GSettings object
2543  * @key: the key to bind
2544  * @object: (type GObject.Object): a #GObject
2545  * @property: the name of the property to bind
2546  * @flags: flags for the binding
2547  * @get_mapping: a function that gets called to convert values
2548  *     from @settings to @object, or %NULL to use the default GIO mapping
2549  * @set_mapping: a function that gets called to convert values
2550  *     from @object to @settings, or %NULL to use the default GIO mapping
2551  * @user_data: data that gets passed to @get_mapping and @set_mapping
2552  * @destroy: #GDestroyNotify function for @user_data
2553  *
2554  * Create a binding between the @key in the @settings object
2555  * and the property @property of @object.
2556  *
2557  * The binding uses the provided mapping functions to map between
2558  * settings and property values.
2559  *
2560  * Note that the lifecycle of the binding is tied to the object,
2561  * and that you can have only one binding per object property.
2562  * If you bind the same property twice on the same object, the second
2563  * binding overrides the first one.
2564  *
2565  * Since: 2.26
2566  */
2567 void
2568 g_settings_bind_with_mapping (GSettings               *settings,
2569                               const gchar             *key,
2570                               gpointer                 object,
2571                               const gchar             *property,
2572                               GSettingsBindFlags       flags,
2573                               GSettingsBindGetMapping  get_mapping,
2574                               GSettingsBindSetMapping  set_mapping,
2575                               gpointer                 user_data,
2576                               GDestroyNotify           destroy)
2577 {
2578   GSettingsBinding *binding;
2579   GObjectClass *objectclass;
2580   gchar *detailed_signal;
2581   GQuark binding_quark;
2582
2583   g_return_if_fail (G_IS_SETTINGS (settings));
2584   g_return_if_fail (key != NULL);
2585   g_return_if_fail (G_IS_OBJECT (object));
2586   g_return_if_fail (property != NULL);
2587   g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2588
2589   objectclass = G_OBJECT_GET_CLASS (object);
2590
2591   binding = g_slice_new0 (GSettingsBinding);
2592   g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2593   binding->settings = g_object_ref (settings);
2594   binding->object = object;
2595   binding->property = g_object_class_find_property (objectclass, property);
2596   binding->user_data = user_data;
2597   binding->destroy = destroy;
2598   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2599   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2600
2601   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2602     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2603
2604   if (binding->property == NULL)
2605     {
2606       g_critical ("g_settings_bind: no property '%s' on class '%s'",
2607                   property, G_OBJECT_TYPE_NAME (object));
2608       return;
2609     }
2610
2611   if ((flags & G_SETTINGS_BIND_GET) &&
2612       (binding->property->flags & G_PARAM_WRITABLE) == 0)
2613     {
2614       g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2615                   "writable", property, G_OBJECT_TYPE_NAME (object));
2616       return;
2617     }
2618   if ((flags & G_SETTINGS_BIND_SET) &&
2619       (binding->property->flags & G_PARAM_READABLE) == 0)
2620     {
2621       g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2622                   "readable", property, G_OBJECT_TYPE_NAME (object));
2623       return;
2624     }
2625
2626   if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2627     {
2628       /* g_settings_bind_invert_boolean_get_mapping() is a private
2629        * function, so if we are here it means that g_settings_bind() was
2630        * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2631        *
2632        * Ensure that both sides are boolean.
2633        */
2634
2635       if (binding->property->value_type != G_TYPE_BOOLEAN)
2636         {
2637           g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2638                       "was specified, but property `%s' on type `%s' has "
2639                       "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2640                       g_type_name ((binding->property->value_type)));
2641           return;
2642         }
2643
2644       if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2645         {
2646           g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2647                       "was specified, but key `%s' on schema `%s' has "
2648                       "type `%s'", key, g_settings_schema_get_id (settings->priv->schema),
2649                       g_variant_type_dup_string (binding->key.type));
2650           return;
2651         }
2652
2653     }
2654
2655   else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2656             (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2657            !g_settings_mapping_is_compatible (binding->property->value_type,
2658                                               binding->key.type))
2659     {
2660       g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2661                   "'%s' which is not compatible with type '%s' of key '%s' "
2662                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2663                   g_type_name (binding->property->value_type),
2664                   g_variant_type_dup_string (binding->key.type), key,
2665                   g_settings_schema_get_id (settings->priv->schema));
2666       return;
2667     }
2668
2669   if ((flags & G_SETTINGS_BIND_SET) &&
2670       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2671     {
2672       GParamSpec *sensitive;
2673
2674       sensitive = g_object_class_find_property (objectclass, "sensitive");
2675
2676       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2677           (sensitive->flags & G_PARAM_WRITABLE))
2678         g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2679     }
2680
2681   if (flags & G_SETTINGS_BIND_SET)
2682     {
2683       detailed_signal = g_strdup_printf ("notify::%s", property);
2684       binding->property_handler_id =
2685         g_signal_connect (object, detailed_signal,
2686                           G_CALLBACK (g_settings_binding_property_changed),
2687                           binding);
2688       g_free (detailed_signal);
2689
2690       if (~flags & G_SETTINGS_BIND_GET)
2691         g_settings_binding_property_changed (object,
2692                                              binding->property,
2693                                              binding);
2694     }
2695
2696   if (flags & G_SETTINGS_BIND_GET)
2697     {
2698       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2699         {
2700           detailed_signal = g_strdup_printf ("changed::%s", key);
2701           binding->key_handler_id =
2702             g_signal_connect (settings, detailed_signal,
2703                               G_CALLBACK (g_settings_binding_key_changed),
2704                               binding);
2705           g_free (detailed_signal);
2706         }
2707
2708       g_settings_binding_key_changed (settings, binding->key.name, binding);
2709     }
2710
2711   binding_quark = g_settings_binding_quark (property);
2712   g_object_set_qdata_full (object, binding_quark,
2713                            binding, g_settings_binding_free);
2714 }
2715
2716 /* Writability binding {{{1 */
2717 typedef struct
2718 {
2719   GSettings *settings;
2720   gpointer object;
2721   const gchar *key;
2722   const gchar *property;
2723   gboolean inverted;
2724   gulong handler_id;
2725 } GSettingsWritableBinding;
2726
2727 static void
2728 g_settings_writable_binding_free (gpointer data)
2729 {
2730   GSettingsWritableBinding *binding = data;
2731
2732   g_signal_handler_disconnect (binding->settings, binding->handler_id);
2733   g_object_unref (binding->settings);
2734   g_slice_free (GSettingsWritableBinding, binding);
2735 }
2736
2737 static void
2738 g_settings_binding_writable_changed (GSettings   *settings,
2739                                      const gchar *key,
2740                                      gpointer     user_data)
2741 {
2742   GSettingsWritableBinding *binding = user_data;
2743   gboolean writable;
2744
2745   g_assert (settings == binding->settings);
2746   g_assert (key == binding->key);
2747
2748   writable = g_settings_is_writable (settings, key);
2749
2750   if (binding->inverted)
2751     writable = !writable;
2752
2753   g_object_set (binding->object, binding->property, writable, NULL);
2754 }
2755
2756 /**
2757  * g_settings_bind_writable:
2758  * @settings: a #GSettings object
2759  * @key: the key to bind
2760  * @object: (type GObject.Object):a #GObject
2761  * @property: the name of a boolean property to bind
2762  * @inverted: whether to 'invert' the value
2763  *
2764  * Create a binding between the writability of @key in the
2765  * @settings object and the property @property of @object.
2766  * The property must be boolean; "sensitive" or "visible"
2767  * properties of widgets are the most likely candidates.
2768  *
2769  * Writable bindings are always uni-directional; changes of the
2770  * writability of the setting will be propagated to the object
2771  * property, not the other way.
2772  *
2773  * When the @inverted argument is %TRUE, the binding inverts the
2774  * value as it passes from the setting to the object, i.e. @property
2775  * will be set to %TRUE if the key is <emphasis>not</emphasis>
2776  * writable.
2777  *
2778  * Note that the lifecycle of the binding is tied to the object,
2779  * and that you can have only one binding per object property.
2780  * If you bind the same property twice on the same object, the second
2781  * binding overrides the first one.
2782  *
2783  * Since: 2.26
2784  */
2785 void
2786 g_settings_bind_writable (GSettings   *settings,
2787                           const gchar *key,
2788                           gpointer     object,
2789                           const gchar *property,
2790                           gboolean     inverted)
2791 {
2792   GSettingsWritableBinding *binding;
2793   gchar *detailed_signal;
2794   GParamSpec *pspec;
2795
2796   g_return_if_fail (G_IS_SETTINGS (settings));
2797
2798   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2799   if (pspec == NULL)
2800     {
2801       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2802                   property, G_OBJECT_TYPE_NAME (object));
2803       return;
2804     }
2805   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2806     {
2807       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2808                   property, G_OBJECT_TYPE_NAME (object));
2809       return;
2810     }
2811
2812   binding = g_slice_new (GSettingsWritableBinding);
2813   binding->settings = g_object_ref (settings);
2814   binding->object = object;
2815   binding->key = g_intern_string (key);
2816   binding->property = g_intern_string (property);
2817   binding->inverted = inverted;
2818
2819   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2820   binding->handler_id =
2821     g_signal_connect (settings, detailed_signal,
2822                       G_CALLBACK (g_settings_binding_writable_changed),
2823                       binding);
2824   g_free (detailed_signal);
2825
2826   g_object_set_qdata_full (object, g_settings_binding_quark (property),
2827                            binding, g_settings_writable_binding_free);
2828
2829   g_settings_binding_writable_changed (settings, binding->key, binding);
2830 }
2831
2832 /**
2833  * g_settings_unbind:
2834  * @object: the object
2835  * @property: the property whose binding is removed
2836  *
2837  * Removes an existing binding for @property on @object.
2838  *
2839  * Note that bindings are automatically removed when the
2840  * object is finalized, so it is rarely necessary to call this
2841  * function.
2842  *
2843  * Since: 2.26
2844  */
2845 void
2846 g_settings_unbind (gpointer     object,
2847                    const gchar *property)
2848 {
2849   GQuark binding_quark;
2850
2851   binding_quark = g_settings_binding_quark (property);
2852   g_object_set_qdata (object, binding_quark, NULL);
2853 }
2854
2855 /* GAction {{{1 */
2856
2857 typedef struct
2858 {
2859   GObject parent_instance;
2860
2861   GSettingsSchemaKey key;
2862   GSettings *settings;
2863 } GSettingsAction;
2864
2865 typedef GObjectClass GSettingsActionClass;
2866
2867 static GType g_settings_action_get_type (void);
2868 static void g_settings_action_iface_init (GActionInterface *iface);
2869 G_DEFINE_TYPE_WITH_CODE (GSettingsAction, g_settings_action, G_TYPE_OBJECT,
2870                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_settings_action_iface_init))
2871
2872 enum
2873 {
2874   ACTION_PROP_0,
2875   ACTION_PROP_NAME,
2876   ACTION_PROP_PARAMETER_TYPE,
2877   ACTION_PROP_ENABLED,
2878   ACTION_PROP_STATE_TYPE,
2879   ACTION_PROP_STATE
2880 };
2881
2882 static const gchar *
2883 g_settings_action_get_name (GAction *action)
2884 {
2885   GSettingsAction *gsa = (GSettingsAction *) action;
2886
2887   return gsa->key.name;
2888 }
2889
2890 static const GVariantType *
2891 g_settings_action_get_parameter_type (GAction *action)
2892 {
2893   GSettingsAction *gsa = (GSettingsAction *) action;
2894   const GVariantType *type;
2895
2896   type = g_variant_get_type (gsa->key.default_value);
2897   if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
2898     type = NULL;
2899
2900   return type;
2901 }
2902
2903 static gboolean
2904 g_settings_action_get_enabled (GAction *action)
2905 {
2906   GSettingsAction *gsa = (GSettingsAction *) action;
2907
2908   return g_settings_is_writable (gsa->settings, gsa->key.name);
2909 }
2910
2911 static const GVariantType *
2912 g_settings_action_get_state_type (GAction *action)
2913 {
2914   GSettingsAction *gsa = (GSettingsAction *) action;
2915
2916   return g_variant_get_type (gsa->key.default_value);
2917 }
2918
2919 static GVariant *
2920 g_settings_action_get_state (GAction *action)
2921 {
2922   GSettingsAction *gsa = (GSettingsAction *) action;
2923   GVariant *value;
2924
2925   value = g_settings_read_from_backend (gsa->settings, &gsa->key);
2926
2927   if (value == NULL)
2928     value = g_settings_schema_key_get_translated_default (&gsa->key);
2929
2930   if (value == NULL)
2931     value = g_variant_ref (gsa->key.default_value);
2932
2933   return value;
2934 }
2935
2936 static GVariant *
2937 g_settings_action_get_state_hint (GAction *action)
2938 {
2939   GSettingsAction *gsa = (GSettingsAction *) action;
2940
2941   /* no point in reimplementing this... */
2942   return g_settings_get_range (gsa->settings, gsa->key.name);
2943 }
2944
2945 static void
2946 g_settings_action_change_state (GAction  *action,
2947                                 GVariant *value)
2948 {
2949   GSettingsAction *gsa = (GSettingsAction *) action;
2950
2951   if (g_settings_schema_key_type_check (&gsa->key, value) && g_settings_schema_key_range_check (&gsa->key, value))
2952     g_settings_write_to_backend (gsa->settings, &gsa->key, value);
2953 }
2954
2955 static void
2956 g_settings_action_activate (GAction  *action,
2957                             GVariant *parameter)
2958 {
2959   GSettingsAction *gsa = (GSettingsAction *) action;
2960
2961   if (g_variant_is_of_type (gsa->key.default_value, G_VARIANT_TYPE_BOOLEAN))
2962     {
2963       GVariant *old;
2964
2965       if (parameter != NULL)
2966         return;
2967
2968       old = g_settings_action_get_state (action);
2969       parameter = g_variant_new_boolean (!g_variant_get_boolean (old));
2970       g_variant_unref (old);
2971     }
2972
2973   g_action_change_state (action, parameter);
2974 }
2975
2976 static void
2977 g_settings_action_get_property (GObject *object, guint prop_id,
2978                                 GValue *value, GParamSpec *pspec)
2979 {
2980   GAction *action = G_ACTION (object);
2981
2982   switch (prop_id)
2983     {
2984     case ACTION_PROP_NAME:
2985       g_value_set_string (value, g_settings_action_get_name (action));
2986       break;
2987
2988     case ACTION_PROP_PARAMETER_TYPE:
2989       g_value_set_boxed (value, g_settings_action_get_parameter_type (action));
2990       break;
2991
2992     case ACTION_PROP_ENABLED:
2993       g_value_set_boolean (value, g_settings_action_get_enabled (action));
2994       break;
2995
2996     case ACTION_PROP_STATE_TYPE:
2997       g_value_set_boxed (value, g_settings_action_get_state_type (action));
2998       break;
2999
3000     case ACTION_PROP_STATE:
3001       g_value_set_variant (value, g_settings_action_get_state (action));
3002       break;
3003
3004     default:
3005       g_assert_not_reached ();
3006     }
3007 }
3008
3009 static void
3010 g_settings_action_finalize (GObject *object)
3011 {
3012   GSettingsAction *gsa = (GSettingsAction *) object;
3013
3014   g_signal_handlers_disconnect_by_data (gsa->settings, gsa);
3015   g_object_unref (gsa->settings);
3016
3017   G_OBJECT_CLASS (g_settings_action_parent_class)
3018     ->finalize (object);
3019 }
3020
3021 static void
3022 g_settings_action_init (GSettingsAction *gsa)
3023 {
3024 }
3025
3026 static void
3027 g_settings_action_iface_init (GActionInterface *iface)
3028 {
3029   iface->get_name = g_settings_action_get_name;
3030   iface->get_parameter_type = g_settings_action_get_parameter_type;
3031   iface->get_enabled = g_settings_action_get_enabled;
3032   iface->get_state_type = g_settings_action_get_state_type;
3033   iface->get_state = g_settings_action_get_state;
3034   iface->get_state_hint = g_settings_action_get_state_hint;
3035   iface->change_state = g_settings_action_change_state;
3036   iface->activate = g_settings_action_activate;
3037 }
3038
3039 static void
3040 g_settings_action_class_init (GSettingsActionClass *class)
3041 {
3042   class->get_property = g_settings_action_get_property;
3043   class->finalize = g_settings_action_finalize;
3044
3045   g_object_class_override_property (class, ACTION_PROP_NAME, "name");
3046   g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE, "parameter-type");
3047   g_object_class_override_property (class, ACTION_PROP_ENABLED, "enabled");
3048   g_object_class_override_property (class, ACTION_PROP_STATE_TYPE, "state-type");
3049   g_object_class_override_property (class, ACTION_PROP_STATE, "state");
3050 }
3051
3052 static void
3053 g_settings_action_changed (GSettings   *settings,
3054                            const gchar *key,
3055                            gpointer     user_data)
3056 {
3057   g_object_notify (user_data, "state");
3058 }
3059
3060 static void
3061 g_settings_action_enabled_changed (GSettings   *settings,
3062                                    const gchar *key,
3063                                    gpointer     user_data)
3064 {
3065   g_object_notify (user_data, "enabled");
3066 }
3067
3068 /**
3069  * g_settings_create_action:
3070  * @settings: a #GSettings
3071  * @key: the name of a key in @settings
3072  *
3073  * Creates a #GAction corresponding to a given #GSettings key.
3074  *
3075  * The action has the same name as the key.
3076  *
3077  * The value of the key becomes the state of the action and the action
3078  * is enabled when the key is writable.  Changing the state of the
3079  * action results in the key being written to.  Changes to the value or
3080  * writability of the key cause appropriate change notifications to be
3081  * emitted for the action.
3082  *
3083  * For boolean-valued keys, action activations take no parameter and
3084  * result in the toggling of the value.  For all other types,
3085  * activations take the new value for the key (which must have the
3086  * correct type).
3087  *
3088  * Returns: (transfer full): a new #GAction
3089  *
3090  * Since: 2.32
3091  **/
3092 GAction *
3093 g_settings_create_action (GSettings   *settings,
3094                           const gchar *key)
3095 {
3096   GSettingsAction *gsa;
3097   gchar *detailed_signal;
3098
3099   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
3100   g_return_val_if_fail (key != NULL, NULL);
3101
3102   gsa = g_object_new (g_settings_action_get_type (), NULL);
3103   gsa->settings = g_object_ref (settings);
3104   g_settings_schema_key_init (&gsa->key, settings->priv->schema, key);
3105
3106   detailed_signal = g_strdup_printf ("changed::%s", key);
3107   g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_changed), gsa);
3108   g_free (detailed_signal);
3109   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3110   g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_enabled_changed), gsa);
3111   g_free (detailed_signal);
3112
3113   return G_ACTION (gsa);
3114 }
3115
3116 /* Epilogue {{{1 */
3117
3118 /* vim:set foldmethod=marker: */