summaryrefslogtreecommitdiff
path: root/gsettings/dconfsettingsbackend.c
blob: cdf0550933a25dce7a69fd9e9ec4780da99f9f67 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
 * Copyright © 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>
 */

#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>
#include <dconf-engine.h>
#include <gio/gio.h>

#include <string.h>

#include "dconfcontext.h"

typedef GSettingsBackendClass DConfSettingsBackendClass;
typedef struct _Outstanding Outstanding;

typedef struct
{
  GSettingsBackend backend;

  GDBusConnection *session_bus;
  GDBusConnection *system_bus;
  guint session_subscription;
  guint system_subscription;

  Outstanding *outstanding;
  gchar *anti_expose_tag;

  DConfEngine *engine;
  GMutex lock;
  GCond sync_cond;
  gint sync_waiters;
} DConfSettingsBackend;

static GType dconf_settings_backend_get_type (void);
G_DEFINE_TYPE (DConfSettingsBackend,
               dconf_settings_backend,
               G_TYPE_SETTINGS_BACKEND)

static void
dconf_settings_backend_signal (GDBusConnection *connection,
                               const gchar     *sender_name,
                               const gchar     *object_path,
                               const gchar     *interface_name,
                               const gchar     *signal_name,
                               GVariant        *parameters,
                               gpointer         user_data)
{
  DConfSettingsBackend *dcsb = user_data;
  const gchar *anti_expose;
  const gchar **rels;
  const gchar *path;
  gchar bus_type;

  if (connection == dcsb->session_bus)
    {
      anti_expose = dcsb->anti_expose_tag;
      bus_type = 'e';
    }

  else if (connection == dcsb->system_bus)
    {
      anti_expose = NULL;
      bus_type = 'y';
    }

  else
    g_assert_not_reached ();

  if (dconf_engine_decode_notify (dcsb->engine, anti_expose,
                                  &path, &rels, bus_type,
                                  sender_name, interface_name,
                                  signal_name, parameters))
    {
      GSettingsBackend *backend = G_SETTINGS_BACKEND (dcsb);

      if (!g_str_has_suffix (path, "/"))
        g_settings_backend_changed (backend, path, NULL);

      else if (rels[0] == NULL)
        g_settings_backend_path_changed (backend, path, NULL);

      else
        g_settings_backend_keys_changed (backend, path, rels, NULL);

      g_free (rels);
    }

  if (dconf_engine_decode_writability_notify (&path, interface_name,
                                              signal_name, parameters))
    {
      GSettingsBackend *backend = G_SETTINGS_BACKEND (dcsb);

      if (g_str_has_suffix (path, "/"))
        {
          g_settings_backend_path_writable_changed (backend, path);
          g_settings_backend_path_changed (backend, path, NULL);
        }

      else
        {
          g_settings_backend_writable_changed (backend, path);
          g_settings_backend_changed (backend, path, NULL);
        }
    }
}

static void
dconf_settings_backend_send (DConfSettingsBackend *dcsb,
                             DConfEngineMessage   *dcem,
                             GAsyncReadyCallback   callback,
                             gpointer              user_data)
{
  GDBusConnection *connection;
  gint i;

  for (i = 0; i < dcem->n_messages; i++)
    {
      GError *error = NULL;

      switch (dcem->bus_types[i])
        {
        case 'e':
          if (dcsb->session_bus == NULL && callback)
            {
              dcsb->session_bus =
                g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);

              if (dcsb->session_bus != NULL)
                dcsb->session_subscription =
                  g_dbus_connection_signal_subscribe (dcsb->session_bus, NULL,
                                                      "ca.desrt.dconf.Writer",
                                                      NULL, NULL, NULL,
                                                      G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
                                                      dconf_settings_backend_signal,
                                                      dcsb, NULL);
            }
          connection = dcsb->session_bus;
          break;

        case 'y':
          if (dcsb->system_bus == NULL && callback)
            {
              dcsb->system_bus =
                g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);

              if (dcsb->system_bus != NULL)
                dcsb->system_subscription =
                  g_dbus_connection_signal_subscribe (dcsb->system_bus, NULL,
                                                      "ca.desrt.dconf.Writer",
                                                      NULL, NULL, NULL,
                                                      G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
                                                      dconf_settings_backend_signal,
                                                      dcsb, NULL);
            }
          connection = dcsb->system_bus;
          break;

        default:
          g_assert_not_reached ();
        }

      if (connection == NULL && callback != NULL)
        {
          g_assert (error != NULL);

          g_warning ("%s", error->message);
          g_error_free (error);

          callback (NULL, NULL, user_data);
        }

      if (connection != NULL)
        g_dbus_connection_call (connection,
                                dcem->bus_name,
                                dcem->object_path,
                                dcem->interface_name,
                                dcem->method_name,
                                dcem->parameters[i],
                                dcem->reply_type,
                                0, 120000, NULL, callback, user_data);
    }
}

static GVariant *
dconf_settings_backend_send_finish (GObject      *source,
                                    GAsyncResult *result)
{
  GError *error = NULL;
  GVariant *reply;

  if (source == NULL)
    return NULL;

  reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
                                         result, &error);

  if (reply == NULL)
    {
      /* This should only be hit in the case that something is seriously
       * wrong with the installation (ie: the service can't be started,
       * etc).  Bug #641768 requests some notification of these kinds of
       * situations in the context of the gsettings(1) commandline tool,
       * so a g_warning() is appropriate here.
       */

      g_warning ("%s", error->message);
      g_error_free (error);
    }

  return reply;
}

struct _Outstanding
{
  Outstanding *next;

  DConfSettingsBackend *dcsb;
  DConfEngineMessage    dcem;

  gchar    *set_key;
  GVariant *set_value;
  GTree    *tree;
};

static void
dconf_settings_backend_outstanding_returned (GObject      *source,
                                             GAsyncResult *result,
                                             gpointer      user_data)
{
  Outstanding *outstanding = user_data;
  DConfSettingsBackend *dcsb;
  GVariant *reply;

  dcsb = outstanding->dcsb;

  /* One way or another we no longer need this hooked into the list.
   */
  g_mutex_lock (&dcsb->lock);
  {
    Outstanding **tmp;

    for (tmp = &dcsb->outstanding; tmp; tmp = &(*tmp)->next)
      if (*tmp == outstanding)
        {
          *tmp = outstanding->next;
          break;
        }

    if (dcsb->outstanding == NULL && dcsb->sync_waiters)
      g_cond_broadcast (&dcsb->sync_cond);
  }
  g_mutex_unlock (&dcsb->lock);

  reply = dconf_settings_backend_send_finish (source, result);

  if (reply)
    {
      /* Success.
       *
       * We want to ensure that we don't emit an extra change
       * notification signal when we see the signal that the service is
       * about to send, so store the tag so we know to ignore it when
       * the signal comes.
       *
       * No thread safety issue here since this variable is only
       * accessed from the worker thread.
       */
      g_free (dcsb->anti_expose_tag);
      g_variant_get_child (reply, 0, "s", &dcsb->anti_expose_tag);
      g_variant_unref (reply);
    }
  else
    {
      /* An error of some kind.
       *
       * We already removed the outstanding entry from the list, so the
       * unmodified database is now visible to the client.  Change
       * notify so that they see it.
       */
      if (outstanding->set_key)
        g_settings_backend_changed (G_SETTINGS_BACKEND (dcsb),
                                    outstanding->set_key, NULL);

      else
        g_settings_backend_changed_tree (G_SETTINGS_BACKEND (dcsb),
                                         outstanding->tree, NULL);
    }

  dconf_engine_message_destroy (&outstanding->dcem);
  g_object_unref (outstanding->dcsb);
  g_free (outstanding->set_key);

  if (outstanding->set_value)
    g_variant_unref (outstanding->set_value);

  if (outstanding->tree)
    g_tree_unref (outstanding->tree);

  g_slice_free (Outstanding, outstanding);
}

static gboolean
dconf_settings_backend_send_outstanding (gpointer data)
{
  Outstanding *outstanding = data;

  dconf_settings_backend_send (outstanding->dcsb,
                               &outstanding->dcem,
                               dconf_settings_backend_outstanding_returned,
                               outstanding);

  return FALSE;
}

static void
dconf_settings_backend_queue (DConfSettingsBackend *dcsb,
                              DConfEngineMessage   *dcem,
                              const gchar          *set_key,
                              GVariant             *set_value,
                              GTree                *tree)
{
  Outstanding *outstanding;

  outstanding = g_slice_new (Outstanding);
  outstanding->dcsb = g_object_ref (dcsb);
  outstanding->dcem = *dcem;

  outstanding->set_key = g_strdup (set_key);
  outstanding->set_value = set_value ? g_variant_ref_sink (set_value) : NULL;
  outstanding->tree = tree ? g_tree_ref (tree) : NULL;

  g_mutex_lock (&dcsb->lock);
  outstanding->next = dcsb->outstanding;
  dcsb->outstanding = outstanding;
  g_mutex_unlock (&dcsb->lock);

  g_main_context_invoke (dconf_context_get (),
                         dconf_settings_backend_send_outstanding,
                         outstanding);
}

static gboolean
dconf_settings_backend_scan_outstanding_tree (GTree       *tree,
                                              const gchar *key,
                                              gsize        key_length,
                                              gpointer    *value)
{
  gchar *mykey;

  mykey = g_alloca (key_length + 1);
  memcpy (mykey, key, key_length + 1);

  while (!g_tree_lookup_extended (tree, mykey, NULL, value) &&
         --key_length)
    {
      while (mykey[key_length - 1] != '/')
        key_length--;

      mykey[key_length] = '\0';
    }

  return key_length != 0;
}

static gboolean
dconf_settings_backend_scan_outstanding (DConfSettingsBackend  *backend,
                                         const gchar           *key,
                                         GVariant             **value)
{
  gboolean found = FALSE;
  Outstanding *node;
  gsize length;

  length = strlen (key);

  if G_LIKELY (backend->outstanding == NULL)
    return FALSE;

  g_mutex_lock (&backend->lock);

  for (node = backend->outstanding; node; node = node->next)
    {
      if (node->set_key)
        {
          if (strcmp (key, node->set_key) == 0)
            {
              if (node->set_value != NULL)
                *value = g_variant_ref (node->set_value);
              else
                *value = NULL;

              found = TRUE;
              break;
            }
        }

      else
        {
          gpointer result;

          if (dconf_settings_backend_scan_outstanding_tree (node->tree, key,
                                                            length, &result))
            {
              if (result)
                *value = g_variant_ref (result);
              else
                *value = NULL;

              found = TRUE;
              break;
            }
        }
    }

  g_mutex_unlock (&backend->lock);

  return found;
}

static GVariant *
dconf_settings_backend_read (GSettingsBackend   *backend,
                             const gchar        *key,
                             const GVariantType *expected_type,
                             gboolean            default_value)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  if (!default_value)
    {
      GVariant *value;

      if (dconf_settings_backend_scan_outstanding (dcsb, key, &value))
        return value;

      return dconf_engine_read (dcsb->engine, key);
    }
  else
    return dconf_engine_read_default (dcsb->engine, key);
}

static gboolean
dconf_settings_backend_write (GSettingsBackend *backend,
                              const gchar      *key,
                              GVariant         *value,
                              gpointer          origin_tag)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
  DConfEngineMessage dcem;

  if (!dconf_engine_write (dcsb->engine, key, value, &dcem, NULL))
    return FALSE;

  dconf_settings_backend_queue (dcsb, &dcem, key, value, NULL);
  g_settings_backend_changed (backend, key, origin_tag);

  return TRUE;
}

static gboolean
dconf_settings_backend_write_tree (GSettingsBackend *backend,
                                   GTree            *tree,
                                   gpointer          origin_tag)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
  DConfEngineMessage dcem;
  const gchar **keys;
  GVariant **values;
  gboolean success;
  gchar *prefix;

  g_settings_backend_flatten_tree (tree, &prefix, &keys, &values);

  if ((success = dconf_engine_write_many (dcsb->engine, prefix,
                                          keys, values, &dcem, NULL)))
    {
      dconf_settings_backend_queue (dcsb, &dcem, NULL, NULL, tree);
      g_settings_backend_keys_changed (backend, prefix, keys, origin_tag);
    }

  g_free (prefix);
  g_free (values);
  g_free (keys);

  return success;
}

static void
dconf_settings_backend_reset (GSettingsBackend *backend,
                              const gchar      *key,
                              gpointer          origin_tag)
{
  dconf_settings_backend_write (backend, key, NULL, origin_tag);
}

static gboolean
dconf_settings_backend_get_writable (GSettingsBackend *backend,
                                     const gchar      *name)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  return dconf_engine_is_writable (dcsb->engine, name);
}

typedef struct
{
  DConfSettingsBackend *dcsb;
  guint64 state;
  gchar *name;
  gint outstanding;
} OutstandingWatch;

static OutstandingWatch *
outstanding_watch_new (DConfSettingsBackend *dcsb,
                       const gchar          *name)
{
  OutstandingWatch *watch;

  watch = g_slice_new (OutstandingWatch);
  watch->dcsb = g_object_ref (dcsb);
  watch->state = dconf_engine_get_state (dcsb->engine);
  watch->outstanding = 0;
  watch->name = g_strdup (name);

  return watch;
}

static void
outstanding_watch_free (OutstandingWatch *watch)
{
  if (--watch->outstanding == 0)
    {
      g_object_unref (watch->dcsb);
      g_free (watch->name);

      g_slice_free (OutstandingWatch, watch);
    }
}

static void
add_match_done (GObject      *source,
                GAsyncResult *result,
                gpointer      user_data)
{
  OutstandingWatch *watch = user_data;
  GError *error = NULL;
  GVariant *reply;

  /* couldn't connect to DBus */
  if (source == NULL)
    {
      outstanding_watch_free (watch);
      return;
    }

  reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
                                         result, &error);

  if (reply == NULL)
    {
      g_critical ("DBus AddMatch for dconf path '%s': %s",
                  watch->name, error->message);
      outstanding_watch_free (watch);
      g_error_free (error);

      return;
    }

  else
    g_variant_unref (reply); /* it is just an empty tuple */

  /* In the normal case we can just free everything and be done.
   *
   * There is a fleeting chance, however, that the database has changed
   * in the meantime.  In that case we can only assume that the subject
   * of this watch changed in that time period and emit a signal to that
   * effect.
   */
  if (dconf_engine_get_state (watch->dcsb->engine) != watch->state)
    g_settings_backend_path_changed (G_SETTINGS_BACKEND (watch->dcsb),
                                     watch->name, NULL);

  outstanding_watch_free (watch);
}

static gboolean
dconf_settings_backend_subscribe_context_func (gpointer data)
{
  OutstandingWatch *watch = data;
  DConfEngineMessage dcem;

  dconf_engine_watch (watch->dcsb->engine, watch->name, &dcem);
  watch->outstanding = dcem.n_messages;

  dconf_settings_backend_send (watch->dcsb, &dcem, add_match_done, watch);
  dconf_engine_message_destroy (&dcem);

  return FALSE;
}

static void
dconf_settings_backend_subscribe (GSettingsBackend *backend,
                                  const gchar      *name)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  g_main_context_invoke (dconf_context_get (),
                         dconf_settings_backend_subscribe_context_func,
                         outstanding_watch_new (dcsb, name));
}

static void
dconf_settings_backend_unsubscribe (GSettingsBackend *backend,
                                    const gchar      *name)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
  DConfEngineMessage dcem;

  dconf_engine_unwatch (dcsb->engine, name, &dcem);
  dconf_settings_backend_send (dcsb, &dcem, NULL, NULL);
  dconf_engine_message_destroy (&dcem);
}

static void
dconf_settings_backend_sync (GSettingsBackend *backend)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  if (!dcsb->outstanding)
    return;

  g_mutex_lock (&dcsb->lock);

  dcsb->sync_waiters++;
  while (dcsb->outstanding)
    g_cond_wait (&dcsb->sync_cond, &dcsb->lock);
  dcsb->sync_waiters--;

  g_mutex_unlock (&dcsb->lock);
}

static void
dconf_settings_backend_init (DConfSettingsBackend *dcsb)
{
  dcsb->engine = dconf_engine_new (NULL);
  g_mutex_init (&dcsb->lock);
  g_cond_init (&dcsb->sync_cond);
}

static void
dconf_settings_backend_class_init (GSettingsBackendClass *class)
{
  class->read = dconf_settings_backend_read;
  class->write = dconf_settings_backend_write;
  class->write_tree = dconf_settings_backend_write_tree;
  class->reset = dconf_settings_backend_reset;
  class->get_writable = dconf_settings_backend_get_writable;
  class->subscribe = dconf_settings_backend_subscribe;
  class->unsubscribe = dconf_settings_backend_unsubscribe;
  class->sync = dconf_settings_backend_sync;
}

void
g_io_module_load (GIOModule *module)
{
  g_type_module_use (G_TYPE_MODULE (module));
  g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
                                  dconf_settings_backend_get_type (),
                                  "dconf", 100);
}

void
g_io_module_unload (GIOModule *module)
{
  g_assert_not_reached ();
}

gchar **
g_io_module_query (void)
{
  return g_strsplit (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME, "!", 0);
}