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