gsettings.c 98.4 KB
Newer Older
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2009, 2010 Codethink Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
16 17 18 19
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

20
/* Prelude {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
21
#include "config.h"
22

Allison Karlitskaya's avatar
Allison Karlitskaya committed
23 24 25 26 27 28 29 30
#include <glib.h>
#include <glibintl.h>

#include "gsettings.h"

#include "gdelayedsettingsbackend.h"
#include "gsettingsbackendinternal.h"
#include "gsettings-mapping.h"
31
#include "gsettingsschema-internal.h"
32
#include "gaction.h"
Allison Karlitskaya's avatar
Allison Karlitskaya committed
33

34 35
#include "strinfo.c"

Allison Karlitskaya's avatar
Allison Karlitskaya committed
36 37
/**
 * SECTION:gsettings
Matthias Clasen's avatar
Matthias Clasen committed
38
 * @short_description: High-level API for application settings
39
 * @include: gio/gio.h
Allison Karlitskaya's avatar
Allison Karlitskaya committed
40 41 42 43
 *
 * The #GSettings class provides a convenient API for storing and retrieving
 * application settings.
 *
44 45 46 47 48
 * Reads and writes can be considered to be non-blocking.  Reading
 * settings with #GSettings is typically extremely fast: on
 * approximately the same order of magnitude (but slower than) a
 * #GHashTable lookup.  Writing settings is also extremely fast in terms
 * of time to return to your application, but can be extremely expensive
49
 * for other threads and other processes.  Many settings backends
50 51 52 53 54 55
 * (including dconf) have lazy initialisation which means in the common
 * case of the user using their computer without modifying any settings
 * a lot of work can be avoided.  For dconf, the D-Bus service doesn't
 * even need to be started in this case.  For this reason, you should
 * only ever modify #GSettings keys in response to explicit user action.
 * Particular care should be paid to ensure that modifications are not
56
 * made during startup -- for example, when setting the initial value
57
 * of preferences widgets.  The built-in g_settings_bind() functionality
58 59 60
 * is careful not to write settings in response to notify signals as a
 * result of modifications that it makes to widgets.
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
61 62 63 64 65 66 67 68 69 70
 * When creating a GSettings instance, you have to specify a schema
 * that describes the keys in your settings and their types and default
 * values, as well as some other information.
 *
 * Normally, a schema has as fixed path that determines where the settings
 * are stored in the conceptual global tree of settings. However, schemas
 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
 * useful e.g. when the schema describes an 'account', and you want to be
 * able to store a arbitrary number of accounts.
 *
71 72 73 74 75 76 77 78
 * Paths must start with and end with a forward slash character ('/')
 * and must not contain two sequential slash characters.  Paths should
 * be chosen based on a domain name associated with the program or
 * library to which the settings belong.  Examples of paths are
 * "/org/gtk/settings/file-chooser/" and "/ca/desrt/dconf-editor/".
 * Paths should not start with "/apps/", "/desktop/" or "/system/" as
 * they often did in GConf.
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
79 80 81 82 83
 * Unlike other configuration systems (like GConf), GSettings does not
 * restrict keys to basic types like strings and numbers. GSettings stores
 * values as #GVariant, and allows any #GVariantType for keys. Key names
 * are restricted to lowercase characters, numbers and '-'. Furthermore,
 * the names must begin with a lowercase character, must not end
84
 * with a '-', and must not contain consecutive dashes.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
85
 *
86 87 88 89 90 91 92 93 94 95
 * GSettings supports change notification.  The primary mechanism to
 * watch for changes is to connect to the "changed" signal.  You can
 * optionally watch for changes on only a single key by using a signal
 * detail.  Signals are only guaranteed to be emitted for a given key
 * after you have read the value of that key while a signal handler was
 * connected for that key.  Signals may or may not be emitted in the
 * case that the key "changed" to the value that you had previously
 * read.  Signals may be reported in additional cases as well and the
 * "changed" signal should really be treated as "may have changed".
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
96 97
 * Similar to GConf, the default values in GSettings schemas can be
 * localized, but the localized values are stored in gettext catalogs
98
 * and looked up with the domain that is specified in the
99
 * gettext-domain attribute of the <schemalist> or <schema>
100
 * elements and the category that is specified in the l10n attribute of
101
 * the <key> element.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
102 103
 *
 * GSettings uses schemas in a compact binary form that is created
104
 * by the [glib-compile-schemas][glib-compile-schemas]
105
 * utility. The input is a schema description in an XML format.
106
 *
107 108
 * A DTD for the gschema XML format can be found here:
 * [gschema.dtd](https://git.gnome.org/browse/glib/tree/gio/gschema.dtd)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
109
 *
110 111
 * The [glib-compile-schemas][glib-compile-schemas] tool expects schema
 * files to have the extension `.gschema.xml`.
112
 *
113
 * At runtime, schemas are identified by their id (as specified in the
114
 * id attribute of the <schema> element). The convention for schema
115 116 117 118 119 120 121 122
 * ids is to use a dotted name, similar in style to a D-Bus bus name,
 * e.g. "org.gnome.SessionManager". In particular, if the settings are
 * for a specific service that owns a D-Bus bus name, the D-Bus bus name
 * and schema id should match. For schemas which deal with settings not
 * associated with one named application, the id should not use
 * StudlyCaps, e.g. "org.gnome.font-rendering".
 *
 * In addition to #GVariant types, keys can have types that have
123 124
 * enumerated types. These can be described by a <choice>,
 * <enum> or <flags> element, as seen in the
125 126 127 128
 * [example][schema-enumerated]. The underlying type of such a key
 * is string, but you can use g_settings_get_enum(), g_settings_set_enum(),
 * g_settings_get_flags(), g_settings_set_flags() access the numeric values
 * corresponding to the string value of enum and flags keys.
129
 *
130 131
 * An example for default value:
 * |[
132
 * <schemalist>
133
 *   <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
134
 *
135 136 137 138 139 140 141
 *     <key name="greeting" type="s">
 *       <default l10n="messages">"Hello, earthlings"</default>
 *       <summary>A greeting</summary>
 *       <description>
 *         Greeting of the invading martians
 *       </description>
 *     </key>
142
 *
143 144 145
 *     <key name="box" type="(ii)">
 *       <default>(20,30)</default>
 *     </key>
146
 *
147 148
 *   </schema>
 * </schemalist>
149
 * ]|
150
 *
151 152
 * An example for ranges, choices and enumerated types:
 * |[
153
 * <schemalist>
154
 *
155
 *   <enum id="org.gtk.Test.myenum">
156 157 158
 *     <value nick="first" value="1"/>
 *     <value nick="second" value="2"/>
 *   </enum>
159
 *
160
 *   <flags id="org.gtk.Test.myflags">
161 162 163
 *     <value nick="flag1" value="1"/>
 *     <value nick="flag2" value="2"/>
 *     <value nick="flag3" value="4"/>
164
 *   </flags>
165
 *
166
 *   <schema id="org.gtk.Test">
167
 *
168 169 170 171
 *     <key name="key-with-range" type="i">
 *       <range min="1" max="100"/>
 *       <default>10</default>
 *     </key>
172
 *
173 174 175 176 177 178 179
 *     <key name="key-with-choices" type="s">
 *       <choices>
 *         <choice value='Elisabeth'/>
 *         <choice value='Annabeth'/>
 *         <choice value='Joe'/>
 *       </choices>
 *       <aliases>
180 181
 *         <alias value='Anna' target='Annabeth'/>
 *         <alias value='Beth' target='Elisabeth'/>
182 183 184
 *       </aliases>
 *       <default>'Joe'</default>
 *     </key>
185
 *
186
 *     <key name='enumerated-key' enum='org.gtk.Test.myenum'>
187 188
 *       <default>'first'</default>
 *     </key>
189
 *
190
 *     <key name='flags-key' flags='org.gtk.Test.myflags'>
191
 *       <default>["flag1","flag2"]</default>
192
 *     </key>
193 194
 *   </schema>
 * </schemalist>
195
 * ]|
196
 *
197 198 199 200 201 202
 * ## Vendor overrides
 *
 * Default values are defined in the schemas that get installed by
 * an application. Sometimes, it is necessary for a vendor or distributor
 * to adjust these defaults. Since patching the XML source for the schema
 * is inconvenient and error-prone,
203 204 205 206 207
 * [glib-compile-schemas][glib-compile-schemas] reads so-called vendor
 * override' files. These are keyfiles in the same directory as the XML
 * schema sources which can override default values. The schema id serves
 * as the group name in the key file, and the values are expected in
 * serialized GVariant form, as in the following example:
208
 * |[
209 210 211
 *     [org.gtk.Example]
 *     key1='string'
 *     key2=1.5
212 213 214
 * ]|
 *
 * glib-compile-schemas expects schema files to have the extension
215
 * `.gschema.override`.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
 *
 * ## Binding
 *
 * A very convenient feature of GSettings lets you bind #GObject properties
 * directly to settings, using g_settings_bind(). Once a GObject property
 * has been bound to a setting, changes on either side are automatically
 * propagated to the other side. GSettings handles details like mapping
 * between GObject and GVariant types, and preventing infinite cycles.
 *
 * This makes it very easy to hook up a preferences dialog to the
 * underlying settings. To make this even more convenient, GSettings
 * looks for a boolean property with the name "sensitivity" and
 * automatically binds it to the writability of the bound setting.
 * If this 'magic' gets in the way, it can be suppressed with the
 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
232 233 234

struct _GSettingsPrivate
{
235 236 237
  /* where the signals go... */
  GMainContext *main_context;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
238 239 240 241
  GSettingsBackend *backend;
  GSettingsSchema *schema;
  gchar *path;

242 243
  gboolean is_subscribed;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
244 245 246 247 248 249
  GDelayedSettingsBackend *delayed;
};

enum
{
  PROP_0,
250
  PROP_SCHEMA,
251
  PROP_SCHEMA_ID,
252
  PROP_BACKEND,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
253 254
  PROP_PATH,
  PROP_HAS_UNAPPLIED,
255
  PROP_DELAY_APPLY
Allison Karlitskaya's avatar
Allison Karlitskaya committed
256 257 258 259 260 261 262 263 264 265 266 267 268
};

enum
{
  SIGNAL_WRITABLE_CHANGE_EVENT,
  SIGNAL_WRITABLE_CHANGED,
  SIGNAL_CHANGE_EVENT,
  SIGNAL_CHANGED,
  N_SIGNALS
};

static guint g_settings_signals[N_SIGNALS];

269
G_DEFINE_TYPE_WITH_PRIVATE (GSettings, g_settings, G_TYPE_OBJECT)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
270

271
/* Signals {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
272 273 274 275 276 277 278 279 280 281 282
static gboolean
g_settings_real_change_event (GSettings    *settings,
                              const GQuark *keys,
                              gint          n_keys)
{
  gint i;

  if (keys == NULL)
    keys = g_settings_schema_list (settings->priv->schema, &n_keys);

  for (i = 0; i < n_keys; i++)
283 284 285 286 287 288 289 290
    {
      const gchar *key = g_quark_to_string (keys[i]);

      if (g_str_has_suffix (key, "/"))
        continue;

      g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED], keys[i], key);
    }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

  return FALSE;
}

static gboolean
g_settings_real_writable_change_event (GSettings *settings,
                                       GQuark     key)
{
  const GQuark *keys = &key;
  gint n_keys = 1;
  gint i;

  if (key == 0)
    keys = g_settings_schema_list (settings->priv->schema, &n_keys);

  for (i = 0; i < n_keys; i++)
307 308 309 310 311 312 313 314
    {
      const gchar *key = g_quark_to_string (keys[i]);

      if (g_str_has_suffix (key, "/"))
        continue;

      g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keys[i], key);
    }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
315 316 317 318

  return FALSE;
}

319
static gboolean
320 321
g_settings_has_signal_handlers (GSettings   *settings,
                                const gchar *key)
322 323
{
  GSettingsClass *class = G_SETTINGS_GET_CLASS (settings);
324
  GQuark keyq;
325 326 327 328 329

  if (class->change_event != g_settings_real_change_event ||
      class->writable_change_event != g_settings_real_writable_change_event)
    return TRUE;

330 331
  keyq = g_quark_from_string (key);

332 333
  if (g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT], 0, TRUE) ||
      g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], 0, TRUE) ||
334
      g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keyq, TRUE) ||
335
      g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_CHANGE_EVENT], 0, TRUE) ||
336 337
      g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_CHANGED], 0, TRUE) ||
      g_signal_has_handler_pending (settings, g_settings_signals[SIGNAL_CHANGED], keyq, TRUE))
338 339 340 341 342 343 344
    return TRUE;

  /* None of that?  Then surely nobody is watching.... */
  return FALSE;
}


Allison Karlitskaya's avatar
Allison Karlitskaya committed
345
static void
346 347
settings_backend_changed (GObject             *target,
                          GSettingsBackend    *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
348
                          const gchar         *key,
349
                          gpointer             origin_tag)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
350
{
351
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
352 353 354
  gboolean ignore_this;
  gint i;

355 356 357 358 359 360 361 362 363 364 365
  /* We used to assert here:
   *
   *   settings->priv->backend == backend
   *
   * but it could be the case that a notification is queued for delivery
   * while someone calls g_settings_delay() (which changes the backend).
   *
   * Since the delay backend would just pass that straight through
   * anyway, it doesn't make sense to try to detect this case.
   * Therefore, we just accept it.
   */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

  for (i = 0; key[i] == settings->priv->path[i]; i++);

  if (settings->priv->path[i] == '\0' &&
      g_settings_schema_has_key (settings->priv->schema, key + i))
    {
      GQuark quark;

      quark = g_quark_from_string (key + i);
      g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
                     0, &quark, 1, &ignore_this);
    }
}

static void
381 382
settings_backend_path_changed (GObject          *target,
                               GSettingsBackend *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
383
                               const gchar      *path,
384
                               gpointer          origin_tag)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
385
{
386
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
387 388 389 390 391 392 393 394
  gboolean ignore_this;

  if (g_str_has_prefix (settings->priv->path, path))
    g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
                   0, NULL, 0, &ignore_this);
}

static void
395 396
settings_backend_keys_changed (GObject             *target,
                               GSettingsBackend    *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
397
                               const gchar         *path,
398 399
                               gpointer             origin_tag,
                               const gchar * const *items)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
400
{
401
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  gboolean ignore_this;
  gint i;

  for (i = 0; settings->priv->path[i] &&
              settings->priv->path[i] == path[i]; i++);

  if (path[i] == '\0')
    {
      GQuark quarks[256];
      gint j, l = 0;

      for (j = 0; items[j]; j++)
         {
           const gchar *item = items[j];
           gint k;

           for (k = 0; item[k] == settings->priv->path[i + k]; k++);

           if (settings->priv->path[i + k] == '\0' &&
               g_settings_schema_has_key (settings->priv->schema, item + k))
             quarks[l++] = g_quark_from_string (item + k);

           /* "256 quarks ought to be enough for anybody!"
            * If this bites you, I'm sorry.  Please file a bug.
            */
           g_assert (l < 256);
         }

      if (l > 0)
        g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
                       0, quarks, l, &ignore_this);
    }
}

static void
437 438
settings_backend_writable_changed (GObject          *target,
                                   GSettingsBackend *backend,
439
                                   const gchar      *key)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
440
{
441
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
442 443 444 445 446 447 448 449 450 451 452 453
  gboolean ignore_this;
  gint i;

  for (i = 0; key[i] == settings->priv->path[i]; i++);

  if (settings->priv->path[i] == '\0' &&
      g_settings_schema_has_key (settings->priv->schema, key + i))
    g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
                   0, g_quark_from_string (key + i), &ignore_this);
}

static void
454 455
settings_backend_path_writable_changed (GObject          *target,
                                        GSettingsBackend *backend,
456
                                        const gchar      *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
457
{
458
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
459 460 461 462 463 464 465
  gboolean ignore_this;

  if (g_str_has_prefix (settings->priv->path, path))
    g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
                   0, (GQuark) 0, &ignore_this);
}

466
/* Properties, Construction, Destruction {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
467 468 469 470 471 472 473 474 475 476
static void
g_settings_set_property (GObject      *object,
                         guint         prop_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  GSettings *settings = G_SETTINGS (object);

  switch (prop_id)
    {
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    case PROP_SCHEMA:
      {
        GSettingsSchema *schema;

        schema = g_value_dup_boxed (value);

        /* we receive a set_property() call for "settings-schema" even
         * if it was not specified (ie: with NULL value).  ->schema
         * could already be set at this point (ie: via "schema-id").
         * check for NULL to avoid clobbering the existing value.
         */
        if (schema != NULL)
          {
            g_assert (settings->priv->schema == NULL);
            settings->priv->schema = schema;
          }
      }
      break;

496
    case PROP_SCHEMA_ID:
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
      {
        const gchar *schema_id;

        schema_id = g_value_get_string (value);

        /* we receive a set_property() call for both "schema" and
         * "schema-id", even if they are not set.  Hopefully only one of
         * them is non-NULL.
         */
        if (schema_id != NULL)
          {
            GSettingsSchemaSource *default_source;

            g_assert (settings->priv->schema == NULL);
            default_source = g_settings_schema_source_get_default ();

            if (default_source == NULL)
              g_error ("No GSettings schemas are installed on the system");

            settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);

            if (settings->priv->schema == NULL)
              g_error ("Settings schema '%s' is not installed\n", schema_id);
          }
      }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
522 523 524 525 526 527
      break;

    case PROP_PATH:
      settings->priv->path = g_value_dup_string (value);
      break;

528 529
    case PROP_BACKEND:
      settings->priv->backend = g_value_dup_object (value);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
g_settings_get_property (GObject    *object,
                         guint       prop_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  GSettings *settings = G_SETTINGS (object);

  switch (prop_id)
    {
547 548 549 550
    case PROP_SCHEMA:
      g_value_set_boxed (value, settings->priv->schema);
      break;

551
     case PROP_SCHEMA_ID:
552
      g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
553 554
      break;

555 556 557 558
     case PROP_BACKEND:
      g_value_set_object (value, settings->priv->backend);
      break;

559 560 561 562
     case PROP_PATH:
      g_value_set_string (value, settings->priv->path);
      break;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
563 564 565 566
     case PROP_HAS_UNAPPLIED:
      g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
      break;

567 568 569 570
     case PROP_DELAY_APPLY:
      g_value_set_boolean (value, settings->priv->delayed != NULL);
      break;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
571 572 573 574 575
     default:
      g_assert_not_reached ();
    }
}

576 577 578 579 580 581 582 583
static const GSettingsListenerVTable listener_vtable = {
  settings_backend_changed,
  settings_backend_path_changed,
  settings_backend_keys_changed,
  settings_backend_writable_changed,
  settings_backend_path_writable_changed
};

584 585 586 587 588 589 590 591 592
static void
g_settings_constructed (GObject *object)
{
  GSettings *settings = G_SETTINGS (object);
  const gchar *schema_path;

  schema_path = g_settings_schema_get_path (settings->priv->schema);

  if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
593 594
    g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
             g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
595 596 597 598

  if (settings->priv->path == NULL)
    {
      if (schema_path == NULL)
599
        g_error ("attempting to create schema '%s' without a path",
600
                 g_settings_schema_get_id (settings->priv->schema));
601 602 603 604

      settings->priv->path = g_strdup (schema_path);
    }

605 606 607
  if (settings->priv->backend == NULL)
    settings->priv->backend = g_settings_backend_get_default ();

608 609 610
  g_settings_backend_watch (settings->priv->backend,
                            &listener_vtable, G_OBJECT (settings),
                            settings->priv->main_context);
611 612
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
613 614 615 616 617
static void
g_settings_finalize (GObject *object)
{
  GSettings *settings = G_SETTINGS (object);

618 619 620 621
  if (settings->priv->is_subscribed)
    g_settings_backend_unsubscribe (settings->priv->backend,
                                    settings->priv->path);

622
  g_main_context_unref (settings->priv->main_context);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
623
  g_object_unref (settings->priv->backend);
624
  g_settings_schema_unref (settings->priv->schema);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
625
  g_free (settings->priv->path);
626 627

  G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
628 629
}

630 631 632
static void
g_settings_init (GSettings *settings)
{
633
  settings->priv = g_settings_get_instance_private (settings);
634
  settings->priv->main_context = g_main_context_ref_thread_default ();
635 636
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
static void
g_settings_class_init (GSettingsClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  class->writable_change_event = g_settings_real_writable_change_event;
  class->change_event = g_settings_real_change_event;

  object_class->set_property = g_settings_set_property;
  object_class->get_property = g_settings_get_property;
  object_class->constructed = g_settings_constructed;
  object_class->finalize = g_settings_finalize;

  /**
   * GSettings::changed:
   * @settings: the object on which the signal was emitted
653
   * @key: the name of the key that changed
Allison Karlitskaya's avatar
Allison Karlitskaya committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
   *
   * The "changed" signal is emitted when a key has potentially changed.
   * You should call one of the g_settings_get() calls to check the new
   * value.
   *
   * This signal supports detailed connections.  You can connect to the
   * detailed signal "changed::x" in order to only receive callbacks
   * when key "x" changes.
   */
  g_settings_signals[SIGNAL_CHANGED] =
    g_signal_new ("changed", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  G_STRUCT_OFFSET (GSettingsClass, changed),
                  NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
                  1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);

  /**
   * GSettings::change-event:
   * @settings: the object on which the signal was emitted
673
   * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
674
   *        an array of #GQuarks for the changed keys, or %NULL
Allison Karlitskaya's avatar
Allison Karlitskaya committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
   * @n_keys: the length of the @keys array, or 0
   *
   * The "change-event" signal is emitted once per change event that
   * affects this settings object.  You should connect to this signal
   * only if you are interested in viewing groups of changes before they
   * are split out into multiple emissions of the "changed" signal.
   * For most use cases it is more appropriate to use the "changed" signal.
   *
   * In the event that the change event applies to one or more specified
   * keys, @keys will be an array of #GQuark of length @n_keys.  In the
   * event that the change event applies to the #GSettings object as a
   * whole (ie: potentially every key has been changed) then @keys will
   * be %NULL and @n_keys will be 0.
   *
   * The default handler for this signal invokes the "changed" signal
   * for each affected key.  If any other connected handler returns
Matthias Clasen's avatar
Matthias Clasen committed
691
   * %TRUE then this default functionality will be suppressed.
692 693 694
   *
   * Returns: %TRUE to stop other handlers from being invoked for the
   *          event. FALSE to propagate the event further.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
695 696 697 698 699 700
   */
  g_settings_signals[SIGNAL_CHANGE_EVENT] =
    g_signal_new ("change-event", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GSettingsClass, change_event),
                  g_signal_accumulator_true_handled, NULL,
701
                  NULL,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
                  G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);

  /**
   * GSettings::writable-changed:
   * @settings: the object on which the signal was emitted
   * @key: the key
   *
   * The "writable-changed" signal is emitted when the writability of a
   * key has potentially changed.  You should call
   * g_settings_is_writable() in order to determine the new status.
   *
   * This signal supports detailed connections.  You can connect to the
   * detailed signal "writable-changed::x" in order to only receive
   * callbacks when the writability of "x" changes.
   */
  g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
    g_signal_new ("writable-changed", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
720
                  G_STRUCT_OFFSET (GSettingsClass, writable_changed),
Allison Karlitskaya's avatar
Allison Karlitskaya committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
                  NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
                  1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);

  /**
   * GSettings::writable-change-event:
   * @settings: the object on which the signal was emitted
   * @key: the quark of the key, or 0
   *
   * The "writable-change-event" signal is emitted once per writability
   * change event that affects this settings object.  You should connect
   * to this signal if you are interested in viewing groups of changes
   * before they are split out into multiple emissions of the
   * "writable-changed" signal.  For most use cases it is more
   * appropriate to use the "writable-changed" signal.
   *
   * In the event that the writability change applies only to a single
   * key, @key will be set to the #GQuark for that key.  In the event
   * that the writability change affects the entire settings object,
   * @key will be 0.
   *
   * The default handler for this signal invokes the "writable-changed"
   * and "changed" signals for each affected key.  This is done because
   * changes in writability might also imply changes in value (if for
   * example, a new mandatory setting is introduced).  If any other
   * connected handler returns %TRUE then this default functionality
Matthias Clasen's avatar
Matthias Clasen committed
746
   * will be suppressed.
747 748 749
   *
   * Returns: %TRUE to stop other handlers from being invoked for the
   *          event. FALSE to propagate the event further.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
750 751 752 753 754 755
   */
  g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
    g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
                  g_signal_accumulator_true_handled, NULL,
756
                  NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
757 758 759 760 761 762

  /**
   * GSettings:context:
   *
   * The name of the context that the settings are stored in.
   */
763 764 765 766 767
  g_object_class_install_property (object_class, PROP_BACKEND,
    g_param_spec_object ("backend",
                         P_("GSettingsBackend"),
                         P_("The GSettingsBackend for this settings object"),
                         G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
Allison Karlitskaya's avatar
Allison Karlitskaya committed
768 769
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  /**
   * GSettings:settings-schema:
   *
   * The #GSettingsSchema describing the types of keys for this
   * #GSettings object.
   *
   * Ideally, this property would be called 'schema'.  #GSettingsSchema
   * has only existed since version 2.32, however, and before then the
   * 'schema' property was used to refer to the ID of the schema rather
   * than the schema itself.  Take care.
   */
  g_object_class_install_property (object_class, PROP_SCHEMA,
    g_param_spec_boxed ("settings-schema",
                        P_("schema"),
                        P_("The GSettingsSchema for this settings object"),
                        G_TYPE_SETTINGS_SCHEMA,
                        G_PARAM_CONSTRUCT_ONLY |
                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
  /**
   * GSettings:schema:
   *
   * The name of the schema that describes the types of keys
   * for this #GSettings object.
   *
   * The type of this property is *not* #GSettingsSchema.
   * #GSettingsSchema has only existed since version 2.32 and
   * unfortunately this name was used in previous versions to refer to
   * the schema ID rather than the schema itself.  Take care to use the
   * 'settings-schema' property if you wish to pass in a
   * #GSettingsSchema.
   *
   * Deprecated:2.32:Use the 'schema-id' property instead.  In a future
   * version, this property may instead refer to a #GSettingsSchema.
   */
  g_object_class_install_property (object_class, PROP_SCHEMA_ID,
    g_param_spec_string ("schema",
                         P_("Schema name"),
                         P_("The name of the schema for this settings object"),
                         NULL,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

813
  /**
814
   * GSettings:schema-id:
815 816 817 818
   *
   * The name of the schema that describes the types of keys
   * for this #GSettings object.
   */
819 820
  g_object_class_install_property (object_class, PROP_SCHEMA_ID,
    g_param_spec_string ("schema-id",
Allison Karlitskaya's avatar
Allison Karlitskaya committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
                         P_("Schema name"),
                         P_("The name of the schema for this settings object"),
                         NULL,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

   /**
    * GSettings:path:
    *
    * The path within the backend where the settings are stored.
    */
   g_object_class_install_property (object_class, PROP_PATH,
     g_param_spec_string ("path",
                          P_("Base path"),
                          P_("The path within the backend where the settings are"),
                          NULL,
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

   /**
    * GSettings:has-unapplied:
    *
    * If this property is %TRUE, the #GSettings object has outstanding
    * changes that will be applied when g_settings_apply() is called.
    */
   g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
     g_param_spec_boolean ("has-unapplied",
                           P_("Has unapplied changes"),
                           P_("TRUE if there are outstanding changes to apply()"),
                           FALSE,
                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

853 854 855 856 857 858 859 860 861 862 863 864 865 866
   /**
    * GSettings:delay-apply:
    *
    * Whether the #GSettings object is in 'delay-apply' mode. See
    * g_settings_delay() for details.
    *
    * Since: 2.28
    */
   g_object_class_install_property (object_class, PROP_DELAY_APPLY,
     g_param_spec_boolean ("delay-apply",
                           P_("Delay-apply mode"),
                           P_("Whether this settings object is in 'delay-apply' mode"),
                           FALSE,
                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
867 868
}

869
/* Construction (new, new_with_path, etc.) {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
870
/**
871
 * g_settings_new:
872
 * @schema_id: the id of the schema
Allison Karlitskaya's avatar
Allison Karlitskaya committed
873
 *
874 875
 * Creates a new #GSettings object with the schema specified by
 * @schema_id.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
876
 *
877 878 879 880
 * Signals on the newly created #GSettings object will be dispatched
 * via the thread-default #GMainContext in effect at the time of the
 * call to g_settings_new().  The new #GSettings will hold a reference
 * on the context.  See g_main_context_push_thread_default().
Allison Karlitskaya's avatar
Allison Karlitskaya committed
881
 *
882 883
 * Returns: a new #GSettings object
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
884 885
 * Since: 2.26
 */
886
GSettings *
887
g_settings_new (const gchar *schema_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
888
{
889
  g_return_val_if_fail (schema_id != NULL, NULL);
890

891
  return g_object_new (G_TYPE_SETTINGS,
892
                       "schema-id", schema_id,
893 894
                       NULL);
}
Allison Karlitskaya's avatar
Allison Karlitskaya committed
895

896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
static gboolean
path_is_valid (const gchar *path)
{
  if (!path)
    return FALSE;

  if (path[0] != '/')
    return FALSE;

  if (!g_str_has_suffix (path, "/"))
    return FALSE;

  return strstr (path, "//") == NULL;
}

911 912
/**
 * g_settings_new_with_path:
913
 * @schema_id: the id of the schema
914 915
 * @path: the path to use
 *
916 917
 * Creates a new #GSettings object with the relocatable schema specified
 * by @schema_id and a given path.
918 919 920 921 922 923 924 925
 *
 * You only need to do this if you want to directly create a settings
 * object with a schema that doesn't have a specified path of its own.
 * That's quite rare.
 *
 * It is a programmer error to call this function for a schema that
 * has an explicitly specified path.
 *
926 927 928 929
 * It is a programmer error if @path is not a valid path.  A valid path
 * begins and ends with '/' and does not contain two consecutive '/'
 * characters.
 *
930 931
 * Returns: a new #GSettings object
 *
932 933 934
 * Since: 2.26
 */
GSettings *
935
g_settings_new_with_path (const gchar *schema_id,
936 937
                          const gchar *path)
{
938
  g_return_val_if_fail (schema_id != NULL, NULL);
939
  g_return_val_if_fail (path_is_valid (path), NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
940

941
  return g_object_new (G_TYPE_SETTINGS,
942
                       "schema-id", schema_id,
943 944 945
                       "path", path,
                       NULL);
}
Allison Karlitskaya's avatar
Allison Karlitskaya committed
946

947
/**
948
 * g_settings_new_with_backend:
949
 * @schema_id: the id of the schema
950
 * @backend: the #GSettingsBackend to use
951
 *
952 953
 * Creates a new #GSettings object with the schema specified by
 * @schema_id and a given #GSettingsBackend.
954
 *
Matthias Clasen's avatar
Matthias Clasen committed
955 956
 * Creating a #GSettings object with a different backend allows accessing
 * settings from a database other than the usual one. For example, it may make
Matthias Clasen's avatar
Matthias Clasen committed
957
 * sense to pass a backend corresponding to the "defaults" settings database on
958 959
 * the system to get a settings object that modifies the system default
 * settings instead of the settings for this user.
960
 *
961 962
 * Returns: a new #GSettings object
 *
963 964 965
 * Since: 2.26
 */
GSettings *
966
g_settings_new_with_backend (const gchar      *schema_id,
967
                             GSettingsBackend *backend)
968
{
969
  g_return_val_if_fail (schema_id != NULL, NULL);
970
  g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
971 972

  return g_object_new (G_TYPE_SETTINGS,
973
                       "schema-id", schema_id,
974
                       "backend", backend,
975 976 977 978
                       NULL);
}

/**
979
 * g_settings_new_with_backend_and_path:
980
 * @schema_id: the id of the schema
981
 * @backend: the #GSettingsBackend to use
982 983
 * @path: the path to use
 *
984 985
 * Creates a new #GSettings object with the schema specified by
 * @schema_id and a given #GSettingsBackend and path.
986
 *
987
 * This is a mix of g_settings_new_with_backend() and
988 989
 * g_settings_new_with_path().
 *
990 991
 * Returns: a new #GSettings object
 *
992 993 994
 * Since: 2.26
 */
GSettings *
995
g_settings_new_with_backend_and_path (const gchar      *schema_id,
996 997
                                      GSettingsBackend *backend,
                                      const gchar      *path)
998
{
999
  g_return_val_if_fail (schema_id != NULL, NULL);
1000
  g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
1001
  g_return_val_if_fail (path_is_valid (path), NULL);
1002 1003

  return g_object_new (G_TYPE_SETTINGS,
1004
                       "schema-id", schema_id,
1005 1006 1007
                       "backend", backend,
                       "path", path,
                       NULL);
1008 1009 1010 1011 1012
}

/**
 * g_settings_new_full:
 * @schema: a #GSettingsSchema
1013 1014
 * @backend: (allow-none): a #GSettingsBackend
 * @path: (allow-none): the path to use
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
 *
 * Creates a new #GSettings object with a given schema, backend and
 * path.
 *
 * It should be extremely rare that you ever want to use this function.
 * It is made available for advanced use-cases (such as plugin systems
 * that want to provide access to schemas loaded from custom locations,
 * etc).
 *
 * At the most basic level, a #GSettings object is a pure composition of
 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
 * backend, and a #GMainContext to which signals are dispatched.
 *
 * This constructor therefore gives you full control over constructing
 * #GSettings instances.  The first 4 parameters are given directly as
 * @schema, @backend and @path, and the main context is taken from the
 * thread-default (as per g_settings_new()).
 *
1033 1034 1035
 * If @backend is %NULL then the default backend is used.
 *
 * If @path is %NULL then the path from the schema is used.  It is an
1036
 * error if @path is %NULL and the schema has no path of its own or if
1037 1038 1039
 * @path is non-%NULL and not equal to the path that the schema does
 * have.
 *
1040 1041
 * Returns: a new #GSettings object
 *
1042 1043 1044 1045 1046 1047 1048
 * Since: 2.32
 */
GSettings *
g_settings_new_full (GSettingsSchema  *schema,
                     GSettingsBackend *backend,
                     const gchar      *path)
{
1049 1050 1051 1052
  g_return_val_if_fail (schema != NULL, NULL);
  g_return_val_if_fail (backend == NULL || G_IS_SETTINGS_BACKEND (backend), NULL);
  g_return_val_if_fail (path == NULL || path_is_valid (path), NULL);

1053 1054 1055
  return g_object_new (G_TYPE_SETTINGS,
                       "settings-schema", schema,
                       "backend", backend,
1056 1057
                       "path", path,
                       NULL);
1058 1059
}

1060
/* Internal read/write utilities {{{1 */
1061
static gboolean
1062 1063 1064
g_settings_write_to_backend (GSettings          *settings,
                             GSettingsSchemaKey *key,
                             GVariant           *value)
1065 1066 1067 1068
{
  gboolean success;
  gchar *path;

1069
  path = g_strconcat (settings->priv->path, key->name, NULL);
1070
  success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1071 1072 1073 1074 1075 1076
  g_free (path);

  return success;
}

static GVariant *
1077
g_settings_read_from_backend (GSettings          *settings,
1078 1079 1080
                              GSettingsSchemaKey *key,
                              gboolean            user_value_only,
                              gboolean            default_value)
1081 1082 1083 1084 1085
{
  GVariant *value;
  GVariant *fixup;
  gchar *path;

1086
  /* If we are not yet watching for changes, consider doing it now... */
1087
  if (!settings->priv->is_subscribed && g_settings_has_signal_handlers (settings, key->name))
1088 1089 1090 1091 1092
    {
      g_settings_backend_subscribe (settings->priv->backend, settings->priv->path);
      settings->priv->is_subscribed = TRUE;
    }

1093
  path = g_strconcat (settings->priv->path, key->name, NULL);
1094 1095 1096 1097
  if (user_value_only)
    value = g_settings_backend_read_user_value (settings->priv->backend, path, key->type);
  else
    value = g_settings_backend_read (settings->priv->backend, path, key->type, default_value);
1098 1099 1100 1101
  g_free (path);

  if (value != NULL)
    {
1102
      fixup = g_settings_schema_key_range_fixup (key, value);
1103 1104 1105 1106 1107 1108 1109 1110
      g_variant_unref (value);
    }
  else
    fixup = NULL;

  return fixup;
}

1111
/* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1112 1113 1114 1115 1116 1117 1118
/**
 * g_settings_get_value:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 *
 * Gets the value that is stored in @settings for @key.
 *
1119 1120
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings.
1121
 *
1122 1123
 * Returns: a new #GVariant
 *
1124 1125 1126 1127 1128 1129
 * Since: 2.26
 */
GVariant *
g_settings_get_value (GSettings   *settings,
                      const gchar *key)
{
1130
  GSettingsSchemaKey skey;
1131 1132 1133 1134 1135
  GVariant *value;

  g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
  g_return_val_if_fail (key != NULL, NULL);

1136
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1137
  value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1138 1139

  if (value == NULL)
1140
    value = g_settings_schema_key_get_translated_default (&skey);
1141

Allison Karlitskaya's avatar
Allison Karlitskaya committed
1142
  if (value == NULL)
1143
    value = g_variant_ref (skey.default_value);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1144

1145
  g_settings_schema_key_clear (&skey);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1146 1147 1148 1149

  return value;
}

1150 1151 1152
/**
 * g_settings_get_user_value:
 * @settings: a #GSettings object
1153
 * @key: the key to get the user value for
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
 *
 * Checks the "user value" of a key, if there is one.
 *
 * The user value of a key is the last value that was set by the user.
 *
 * After calling g_settings_reset() this function should always return
 * %NULL (assuming something is not wrong with the system
 * configuration).
 *
 * It is possible that g_settings_get_value() will return a different
 * value than this function.  This can happen in the case that the user
 * set a value for a key that was subsequently locked down by the system
 * administrator -- this function will return the user's old value.
 *
 * This function may be useful for adding a "reset" option to a UI or
 * for providing indication that a particular value has been changed.
 *
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings.
 *
1174
 * Returns: (allow-none) (transfer full): the user's value, if set
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
 *
 * Since: 2.40
 **/
GVariant *
g_settings_get_user_value (GSettings   *settings,
                           const gchar *key)
{
  GSettingsSchemaKey skey;
  GVariant *value;

  g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
  g_return_val_if_fail (key != NULL, NULL);

  g_settings_schema_key_init (&skey, settings->priv->schema, key);
  value = g_settings_read_from_backend (settings, &skey, TRUE, FALSE);
  g_settings_schema_key_clear (&skey);

  return value;
}

/**
 * g_settings_get_default_value:
 * @settings: a #GSettings object
1198
 * @key: the key to get the default value for
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
 *
 * Gets the "default value" of a key.
 *
 * This is the value that would be read if g_settings_reset() were to be
 * called on the key.
 *
 * Note that this may be a different value than returned by
 * g_settings_schema_key_get_default_value() if the system administrator
 * has provided a default value.
 *
 * Comparing the return values of g_settings_get_default_value() and
 * g_settings_get_value() is not sufficient for determining if a value
 * has been set because the user may have explicitly set the value to
 * something that happens to be equal to the default.  The difference
 * here is that if the default changes in the future, the user's key
 * will still be set.
 *
 * This function may be useful for adding an indication to a UI of what
 * the default value was before the user set it.
 *
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings.
 *
1222
 * Returns: (allow-none) (transfer full): the default value
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
 *
 * Since: 2.40
 **/
GVariant *
g_settings_get_default_value (GSettings   *settings,
                              const gchar *key)
{
  GSettingsSchemaKey skey;
  GVariant *value;

  g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
  g_return_val_if_fail (key != NULL, NULL);

  g_settings_schema_key_init (&skey, settings->priv->schema, key);
  value = g_settings_read_from_backend (settings, &skey, FALSE, TRUE);

  if (value == NULL)
    value = g_settings_schema_key_get_translated_default (&skey);

  if (value == NULL)
    value = g_variant_ref (skey.default_value);

  g_settings_schema_key_clear (&skey);

  return value;
}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
/**
 * g_settings_get_enum:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 *
 * Gets the value that is stored in @settings for @key and converts it
 * to the enum value that it represents.
 *
 * In order to use this function the type of the value must be a string
 * 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
 * schema for @settings or is not marked as an enumerated type.
1263 1264 1265 1266 1267
 *
 * If the value stored in the configuration database is not a valid
 * value for the enumerated type then this function will return the
 * default value.
 *
1268 1269
 * Returns: the enum value
 *
1270 1271 1272 1273 1274 1275
 * Since: 2.26
 **/
gint
g_settings_get_enum (GSettings   *settings,
                     const gchar *key)
{
1276
  GSettingsSchemaKey skey;
1277 1278 1279 1280 1281 1282
  GVariant *value;
  gint result;

  g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
  g_return_val_if_fail (key != NULL, -1);

1283
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1284

1285
  if (!skey.is_enum)
1286
    {
1287
      g_critical ("g_settings_get_enum() called on key '%s' which is not "
1288 1289
                  "associated with an enumerated type", skey.name);
      g_settings_schema_key_clear (&skey);
1290 1291 1292
      return -1;
    }

1293
  value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1294 1295

  if (value == NULL)
1296
    value = g_settings_schema_key_get_translated_default (&skey);
1297 1298

  if (value == NULL)
1299
    value = g_variant_ref (skey.default_value);
1300

1301 1302
  result = g_settings_schema_key_to_enum (&skey, value);
  g_settings_schema_key_clear (&skey);
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
  g_variant_unref (value);

  return result;
}

/**
 * g_settings_set_enum:
 * @settings: a #GSettings object
 * @key: a key, within @settings
 * @value: an enumerated value
 *
 * Looks up the enumerated type nick for @value and writes it to @key,
 * within @settings.
 *
1317 1318 1319
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as an enumerated type, or for
 * @value not to be a valid value for the named type.
1320
 *
1321
 * After performing the write, accessing @key directly with
1322 1323
 * g_settings_get_string() will return the 'nick' associated with
 * @value.
1324 1325
 *
 * Returns: %TRUE, if the set succeeds
1326 1327 1328 1329 1330 1331
 **/
gboolean
g_settings_set_enum (GSettings   *settings,
                     const gchar *key,
                     gint         value)
{
1332
  GSettingsSchemaKey skey;
1333 1334 1335 1336 1337 1338
  GVariant *variant;
  gboolean success;

  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);

1339
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1340

1341
  if (!skey.is_enum)
1342
    {
1343
      g_critical ("g_settings_set_enum() called on key '%s' which is not "
1344
                  "associated with an enumerated type", skey.name);
1345 1346 1347
      return FALSE;
    }

1348
  if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1349
    {
1350 1351
      g_critical ("g_settings_set_enum(): invalid enum value %d for key '%s' "
                  "in schema '%s'.  Doing nothing.", value, skey.name,
1352
                  g_settings_schema_get_id (skey.schema));
1353
      g_settings_schema_key_clear (&skey);
1354 1355 1356
      return FALSE;
    }

1357 1358
  success = g_settings_write_to_backend (settings, &skey, variant);
  g_settings_schema_key_clear (&skey);
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

  return success;
}

/**
 * g_settings_get_flags:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 *
 * Gets the value that is stored in @settings for @key and converts it
 * to the flags value that it represents.
 *
 * In order to use this function the type of the value must be an array
 * of strings and it must be marked in the schema file as an flags type.
 *
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as a flags type.
 *
 * If the value stored in the configuration database is not a valid
 * value for the flags type then this function will return the default
 * value.
 *
1381 1382
 * Returns: the flags value
 *
1383 1384 1385 1386 1387 1388
 * Since: 2.26
 **/
guint
g_settings_get_flags (GSettings   *settings,
                      const gchar *key)
{
1389
  GSettingsSchemaKey skey;
1390 1391 1392 1393 1394 1395
  GVariant *value;
  guint result;

  g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
  g_return_val_if_fail (key != NULL, -1);

1396
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1397

1398
  if (!skey.is_flags)
1399
    {
1400
      g_critical ("g_settings_get_flags() called on key '%s' which is not "
1401 1402
                  "associated with a flags type", skey.name);
      g_settings_schema_key_clear (&skey);
1403 1404 1405
      return -1;
    }

1406
  value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1407 1408

  if (value == NULL)
1409
    value = g_settings_schema_key_get_translated_default (&skey);
1410 1411

  if (value == NULL)
1412
    value = g_variant_ref (skey.default_value);
1413

1414 1415
  result = g_settings_schema_key_to_flags (&skey, value);
  g_settings_schema_key_clear (&skey);
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
  g_variant_unref (value);

  return result;
}

/**
 * g_settings_set_flags:
 * @settings: a #GSettings object
 * @key: a key, within @settings
 * @value: a flags value
 *
 * Looks up the flags type nicks for the bits specified by @value, puts
Matthias Clasen's avatar
Matthias Clasen committed
1428
 * them in an array of strings and writes the array to @key, within
1429 1430 1431 1432 1433 1434 1435 1436 1437
 * @settings.
 *
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as a flags type, or for @value
 * to contain any bits that are not value for the named type.
 *
 * After performing the write, accessing @key directly with
 * g_settings_get_strv() will return an array of 'nicks'; one for each
 * bit in @value.
1438 1439
 *
 * Returns: %TRUE, if the set succeeds
1440 1441 1442 1443 1444 1445
 **/
gboolean
g_settings_set_flags (GSettings   *settings,
                      const gchar *key,
                      guint        value)
{
1446
  GSettingsSchemaKey skey;
1447 1448 1449 1450 1451 1452
  GVariant *variant;
  gboolean success;

  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);

1453
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1454

1455
  if (!skey.is_flags)
1456
    {
1457
      g_critical ("g_settings_set_flags() called on key '%s' which is not "
1458
                  "associated with a flags type", skey.name);
1459 1460 1461
      return FALSE;
    }

1462
  if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1463 1464
    {
      g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1465
                  "for key '%s' in schema '%s'.  Doing nothing.",
1466
                  value, skey.name, g_settings_schema_get_id (skey.schema));
1467
      g_settings_schema_key_clear (&skey);
1468 1469 1470
      return FALSE;
    }

1471 1472
  success = g_settings_write_to_backend (settings, &skey, variant);
  g_settings_schema_key_clear (&skey);
1473 1474 1475 1476

  return success;
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
1477 1478 1479 1480 1481 1482 1483 1484
/**
 * g_settings_set_value:
 * @settings: a #GSettings object
 * @key: the name of the key to set
 * @value: a #GVariant of the correct type
 *
 * Sets @key in @settings to @value.
 *
1485 1486 1487
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or for @value to have the incorrect type, per
 * the schema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1488 1489 1490
 *
 * If @value is floating then this function consumes the reference.
 *
1491 1492 1493
 * Returns: %TRUE if setting the key succeeded,
 *     %FALSE if the key was not writable
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1494 1495 1496 1497 1498 1499 1500
 * Since: 2.26
 **/
gboolean
g_settings_set_value (GSettings   *settings,
                      const gchar *key,
                      GVariant    *value)
{
1501
  GSettingsSchemaKey skey;
1502
  gboolean success;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1503

1504
  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1505
  g_return_val_if_fail (key != NULL, FALSE);
1506

1507
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1508

1509
  if (!g_settings_schema_key_type_check (&skey, value))
1510 1511 1512
    {
      g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
                  key,
1513
                  g_settings_schema_get_id (settings->priv->schema),
1514
                  g_variant_type_peek_string (skey.type),
1515 1516 1517 1518 1519
                  g_variant_get_type_string (value));

        return FALSE;
      }

1520
  if (!g_settings_schema_key_range_check (&skey, value))
1521
    {
Dan Winship's avatar
Dan Winship committed
1522
      g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1523 1524
                 "is outside of valid range",
                 key,
1525
                 g_settings_schema_get_id (settings->priv->schema));
1526 1527 1528 1529

        return FALSE;
    }

1530
  success = g_settings_write_to_backend (settings, &skey, value);
1531
  g_settings_schema_key_clear (&skey);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1532

1533
  return success;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
}

/**
 * g_settings_get:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 * @format: a #GVariant format string
 * @...: arguments as per @format
 *
 * Gets the value that is stored at @key in @settings.
 *
 * A convenience function that combines g_settings_get_value() with
 * g_variant_get().
 *
1548 1549 1550
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or for the #GVariantType of @format to mismatch
 * the type given in the schema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
 *
 * Since: 2.26
 */
void
g_settings_get (GSettings   *settings,
                const gchar *key,
                const gchar *format,
                ...)
{
  GVariant *value;
  va_list ap;

  value = g_settings_get_value (settings, key);

1565
  if (strchr (format, '&'))
1566
    {
1567
      g_critical ("%s: the format string may not contain '&' (key '%s' from schema '%s'). "
1568 1569 1570 1571
                  "This call will probably stop working with a future version of glib.",
                  G_STRFUNC, key, g_settings_schema_get_id (settings->priv->schema));
    }

Allison Karlitskaya's avatar
Allison Karlitskaya committed
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
  va_start (ap, format);
  g_variant_get_va (value, format, NULL, &ap);
  va_end (ap);

  g_variant_unref (value);
}

/**
 * g_settings_set:
 * @settings: a #GSettings object
 * @key: the name of the key to set
 * @format: a #GVariant format string
 * @...: arguments as per @format
 *
 * Sets @key in @settings to @value.
 *
 * A convenience function that combines g_settings_set_value() with
 * g_variant_new().
 *
1591 1592 1593
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or for the #GVariantType of @format to mismatch
 * the type given in the schema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1594
 *
1595 1596 1597
 * Returns: %TRUE if setting the key succeeded,
 *     %FALSE if the key was not writable
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
 * Since: 2.26
 */
gboolean
g_settings_set (GSettings   *settings,
                const gchar *key,
                const gchar *format,
                ...)
{
  GVariant *value;
  va_list ap;

  va_start (ap, format);
  value = g_variant_new_va (format, NULL, &ap);
  va_end (ap);

  return g_settings_set_value (settings, key, value);
}

1616 1617 1618 1619
/**
 * g_settings_get_mapped:
 * @settings: a #GSettings object
 * @key: the key to get the value for
1620 1621
 * @mapping: (scope call): the function to map the value in the
 *           settings database to the value used by the application
1622 1623 1624 1625