gdelayedsettingsbackend.c 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "gdelayedsettingsbackend.h"
#include "gsettingsbackendinternal.h"

#include <string.h>


struct _GDelayedSettingsBackendPrivate
{
  GSettingsBackend *backend;
33
  GMutex lock;
34
  GTree *delayed;
35 36

  GMainContext *owner_context;
37 38 39
  gpointer owner;
};

40 41 42
G_DEFINE_TYPE_WITH_PRIVATE (GDelayedSettingsBackend,
                            g_delayed_settings_backend,
                            G_TYPE_SETTINGS_BACKEND)
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static gboolean
invoke_notify_unapplied (gpointer data)
{
  g_object_notify (data, "has-unapplied");
  g_object_unref (data);

  return FALSE;
}

static void
g_delayed_settings_backend_notify_unapplied (GDelayedSettingsBackend *delayed)
{
  GMainContext *target_context;
  GObject *target;

59
  g_mutex_lock (&delayed->priv->lock);
60 61 62 63 64 65 66 67 68 69
  if (delayed->priv->owner)
    {
      target_context = delayed->priv->owner_context;
      target = g_object_ref (delayed->priv->owner);
    }
  else
    {
      target_context = NULL;
      target = NULL;
    }
70
  g_mutex_unlock (&delayed->priv->lock);
71 72

  if (target != NULL)
73
    g_main_context_invoke (target_context, invoke_notify_unapplied, target);
74 75 76
}


77 78 79
static GVariant *
g_delayed_settings_backend_read (GSettingsBackend   *backend,
                                 const gchar        *key,
80 81
                                 const GVariantType *expected_type,
                                 gboolean            default_value)
82 83
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
84
  gpointer result = NULL;
85

86 87
  if (!default_value)
    {
88
      g_mutex_lock (&delayed->priv->lock);
89 90 91 92 93 94 95 96
      if (g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result))
        {
          /* NULL in the tree means we should consult the default value */
          if (result != NULL)
            g_variant_ref (result);
          else
            default_value = TRUE;
        }
97
      g_mutex_unlock (&delayed->priv->lock);
98
    }
99

100 101 102 103 104
  if (result == NULL)
    result = g_settings_backend_read (delayed->priv->backend, key,
                                      expected_type, default_value);

  return result;
105
}
106

107 108 109 110 111 112 113 114 115
static gboolean
g_delayed_settings_backend_write (GSettingsBackend *backend,
                                  const gchar      *key,
                                  GVariant         *value,
                                  gpointer          origin_tag)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
  gboolean was_empty;

116
  g_mutex_lock (&delayed->priv->lock);
117 118 119
  was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
  g_tree_insert (delayed->priv->delayed, g_strdup (key),
                 g_variant_ref_sink (value));
120
  g_mutex_unlock (&delayed->priv->lock);
121

122 123
  g_settings_backend_changed (backend, key, origin_tag);

124 125
  if (was_empty)
    g_delayed_settings_backend_notify_unapplied (delayed);
126 127 128 129 130 131 132 133 134 135 136 137 138 139

  return TRUE;
}

static gboolean
add_to_tree (gpointer key,
             gpointer value,
             gpointer user_data)
{
  g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
  return FALSE;
}

static gboolean
140
g_delayed_settings_backend_write_tree (GSettingsBackend *backend,
141 142 143 144 145 146
                                       GTree            *tree,
                                       gpointer          origin_tag)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
  gboolean was_empty;

147
  g_mutex_lock (&delayed->priv->lock);
148 149 150
  was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;

  g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
151
  g_mutex_unlock (&delayed->priv->lock);
152 153 154

  g_settings_backend_changed_tree (backend, tree, origin_tag);

155 156
  if (was_empty)
    g_delayed_settings_backend_notify_unapplied (delayed);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

  return TRUE;
}

static gboolean
g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
                                         const gchar      *name)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);

  return g_settings_backend_get_writable (delayed->priv->backend, name);
}

static void
g_delayed_settings_backend_reset (GSettingsBackend *backend,
                                  const gchar      *key,
                                  gpointer          origin_tag)
{
175 176 177
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
  gboolean was_empty;

178
  g_mutex_lock (&delayed->priv->lock);
179 180
  was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
  g_tree_insert (delayed->priv->delayed, g_strdup (key), NULL);
181
  g_mutex_unlock (&delayed->priv->lock);
182 183 184

  if (was_empty)
    g_delayed_settings_backend_notify_unapplied (delayed);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
}

static void
g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
                                      const char       *name)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);

  g_settings_backend_subscribe (delayed->priv->backend, name);
}

static void
g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
                                        const char       *name)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);

  g_settings_backend_unsubscribe (delayed->priv->backend, name);
}

205 206 207 208 209 210 211 212 213 214
static GPermission *
g_delayed_settings_backend_get_permission (GSettingsBackend *backend,
                                           const gchar      *path)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);

  return g_settings_backend_get_permission (delayed->priv->backend, path);
}


215 216 217 218
/* method calls */
gboolean
g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
{
219 220
  /* we don't need to lock for this... */

221 222 223 224 225 226 227 228
  return g_tree_nnodes (delayed->priv->delayed) > 0;
}

void
g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
{
  if (g_tree_nnodes (delayed->priv->delayed) > 0)
    {
229
      gboolean success;
230 231
      GTree *tmp;

232
      g_mutex_lock (&delayed->priv->lock);
233 234
      tmp = delayed->priv->delayed;
      delayed->priv->delayed = g_settings_backend_create_tree ();
235
      success = g_settings_backend_write_tree (delayed->priv->backend,
236
                                               tmp, delayed->priv);
237
      g_mutex_unlock (&delayed->priv->lock);
238 239 240 241 242

      if (!success)
        g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
                                         tmp, NULL);

243 244
      g_tree_unref (tmp);

245
      g_delayed_settings_backend_notify_unapplied (delayed);
246 247 248 249 250 251 252 253 254 255
    }
}

void
g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
{
  if (g_tree_nnodes (delayed->priv->delayed) > 0)
    {
      GTree *tmp;

256
      g_mutex_lock (&delayed->priv->lock);
257 258
      tmp = delayed->priv->delayed;
      delayed->priv->delayed = g_settings_backend_create_tree ();
259
      g_mutex_unlock (&delayed->priv->lock);
260 261 262
      g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed), tmp, NULL);
      g_tree_unref (tmp);

263
      g_delayed_settings_backend_notify_unapplied (delayed);
264 265 266 267 268
    }
}

/* change notification */
static void
269 270
delayed_backend_changed (GObject          *target,
                         GSettingsBackend *backend,
271
                         const gchar      *key,
272
                         gpointer          origin_tag)
273
{
274
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
275 276 277 278 279 280 281

  if (origin_tag != delayed->priv)
    g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
                                key, origin_tag);
}

static void
282 283
delayed_backend_keys_changed (GObject             *target,
                              GSettingsBackend    *backend,
284 285
                              const gchar         *path,
                              const gchar * const *items,
286
                              gpointer             origin_tag)
287
{
288
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
289 290 291 292 293 294 295

  if (origin_tag != delayed->priv)
    g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
                                     path, items, origin_tag);
}

static void
296 297
delayed_backend_path_changed (GObject          *target,
                              GSettingsBackend *backend,
298 299
                              const gchar      *path,
                              gpointer          origin_tag)
300
{
301
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
302 303 304 305 306 307 308

  if (origin_tag != delayed->priv)
    g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
                                     path, origin_tag);
}

static void
309 310
delayed_backend_writable_changed (GObject          *target,
                                  GSettingsBackend *backend,
311
                                  const gchar      *key)
312
{
313
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
314 315
  gboolean last_one = FALSE;

316
  g_mutex_lock (&delayed->priv->lock);
317

318
  if (g_tree_lookup (delayed->priv->delayed, key) != NULL &&
319 320 321
      !g_settings_backend_get_writable (delayed->priv->backend, key))
    {
      /* drop the key from our changeset if it just became read-only.
322
       * no need to signal since the writable change below implies it.
323 324 325 326 327
       *
       * note that the item in the tree may very well be set to NULL in
       * the case that the user stored a reset.  we intentionally don't
       * drop the key in this case since a reset will always succeed
       * (even against a non-writable key).
328 329
       */
      g_tree_remove (delayed->priv->delayed, key);
330 331

      /* if that was the only key... */
332
      last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
333 334
    }

335
  g_mutex_unlock (&delayed->priv->lock);
336 337 338 339

  if (last_one)
    g_delayed_settings_backend_notify_unapplied (delayed);

340 341 342
  g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* slow method until we get foreach-with-remove in GTree
 */
typedef struct
{
  const gchar *path;
  const gchar **keys;
  gsize index;
} CheckPrefixState;

static gboolean
check_prefix (gpointer key,
              gpointer value,
              gpointer data)
{
  CheckPrefixState *state = data;

  if (g_str_has_prefix (key, state->path))
    state->keys[state->index++] = key;

  return FALSE;
}

365
static void
366 367
delayed_backend_path_writable_changed (GObject          *target,
                                       GSettingsBackend *backend,
368
                                       const gchar      *path)
369
{
370
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
371
  gboolean last_one = FALSE;
372 373
  gsize n_keys;

374
  g_mutex_lock (&delayed->priv->lock);
375

376 377 378 379 380 381 382 383 384
  n_keys = g_tree_nnodes (delayed->priv->delayed);

  if (n_keys > 0)
    {
      CheckPrefixState state = { path, g_new (const gchar *, n_keys) };
      gsize i;

      /* collect a list of possibly-affected keys (ie: matching the path) */
      g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
385

386 387 388
      /* drop the keys that have been affected.
       *
       * don't drop 'reset' keys (see above) */
389
      for (i = 0; i < state.index; i++)
390 391
        if (g_tree_lookup (delayed->priv->delayed, state.keys[i]) != NULL &&
            !g_settings_backend_get_writable (delayed->priv->backend,
392 393
                                              state.keys[i]))
          g_tree_remove (delayed->priv->delayed, state.keys[i]);
394

395
      g_free (state.keys);
396

397
      last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
398
    }
399

400
  g_mutex_unlock (&delayed->priv->lock);
401 402 403 404

  if (last_one)
    g_delayed_settings_backend_notify_unapplied (delayed);

405 406 407 408 409 410 411 412 413
  g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
                                            path);
}

static void
g_delayed_settings_backend_finalize (GObject *object)
{
  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);

414
  g_mutex_clear (&delayed->priv->lock);
415
  g_object_unref (delayed->priv->backend);
416
  g_tree_unref (delayed->priv->delayed);
417 418 419 420 421 422

  /* if our owner is still alive, why are we finalizing? */
  g_assert (delayed->priv->owner == NULL);

  G_OBJECT_CLASS (g_delayed_settings_backend_parent_class)
    ->finalize (object);
423 424 425 426 427 428 429 430 431 432
}

static void
g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
{
  GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  backend_class->read = g_delayed_settings_backend_read;
  backend_class->write = g_delayed_settings_backend_write;
433
  backend_class->write_tree = g_delayed_settings_backend_write_tree;
434 435 436 437
  backend_class->reset = g_delayed_settings_backend_reset;
  backend_class->get_writable = g_delayed_settings_backend_get_writable;
  backend_class->subscribe = g_delayed_settings_backend_subscribe;
  backend_class->unsubscribe = g_delayed_settings_backend_unsubscribe;
438
  backend_class->get_permission = g_delayed_settings_backend_get_permission;
439 440 441 442 443 444 445

  object_class->finalize = g_delayed_settings_backend_finalize;
}

static void
g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
{
446
  delayed->priv = g_delayed_settings_backend_get_instance_private (delayed);
447
  delayed->priv->delayed = g_settings_backend_create_tree ();
448
  g_mutex_init (&delayed->priv->lock);
449 450 451 452 453 454 455 456
}

static void
g_delayed_settings_backend_disown (gpointer  data,
                                   GObject  *where_the_object_was)
{
  GDelayedSettingsBackend *delayed = data;

457
  g_mutex_lock (&delayed->priv->lock);
458 459
  delayed->priv->owner_context = NULL;
  delayed->priv->owner = NULL;
460
  g_mutex_unlock (&delayed->priv->lock);
461 462 463 464
}

GDelayedSettingsBackend *
g_delayed_settings_backend_new (GSettingsBackend *backend,
465 466
                                gpointer          owner,
                                GMainContext     *owner_context)
467
{
468 469 470 471 472 473 474
  static GSettingsListenerVTable vtable = {
    delayed_backend_changed,
    delayed_backend_path_changed,
    delayed_backend_keys_changed,
    delayed_backend_writable_changed,
    delayed_backend_path_writable_changed
  };
475 476 477 478
  GDelayedSettingsBackend *delayed;

  delayed = g_object_new (G_TYPE_DELAYED_SETTINGS_BACKEND, NULL);
  delayed->priv->backend = g_object_ref (backend);
479
  delayed->priv->owner_context = owner_context;
480 481
  delayed->priv->owner = owner;

482 483
  g_object_weak_ref (owner, g_delayed_settings_backend_disown, delayed);

484 485
  g_settings_backend_watch (delayed->priv->backend,
                            &vtable, G_OBJECT (delayed), NULL);
486 487 488

  return delayed;
}