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