summaryrefslogtreecommitdiff
path: root/client/dconf-client.c
blob: 88b8189216c236821c0ab72211496e6991fe015f (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
/*
 * 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>
 */

#include <dconf-engine.h>
#include "dconf-client.h"
#include <string.h>

struct _DConfClient
{
  GObject parent_instance;

  GDBusConnection *session_bus;
  GDBusConnection *system_bus;

  DConfEngine *engine;
  gboolean will_write;

  DConfWatchFunc watch_func;
  gpointer user_data;
  GDestroyNotify notify;
};

G_DEFINE_TYPE (DConfClient, dconf_client, G_TYPE_OBJECT)

static GBusType
dconf_client_bus_type (DConfEngineMessage *dcem)
{
  switch (dcem->bus_type)
    {
    case 'e':
      return G_BUS_TYPE_SESSION;

    case 'y':
      return G_BUS_TYPE_SYSTEM;

    default:
      g_assert_not_reached ();
    }
}

typedef struct
{
  GSimpleAsyncResult *simple;
  GCancellable *cancellable;
  DConfEngineMessage dcem;
  GError *error;
} DConfClientAsyncOp;

static DConfClientAsyncOp *
dconf_client_async_op_new (DConfClient         *client,
                           gpointer             source_tag,
                           GCancellable        *cancellable,
                           GAsyncReadyCallback  callback,
                           gpointer             user_data)
{
  DConfClientAsyncOp *op;

  op = g_slice_new (DConfClientAsyncOp);
  op->simple = g_simple_async_result_new (G_OBJECT (client), callback,
                                          user_data, source_tag);
  if (cancellable)
    op->cancellable = g_object_ref (cancellable);
  else
    op->cancellable = NULL;

  op->error = NULL;

  return op;
}

static void
dconf_client_async_op_complete (DConfClientAsyncOp *op,
                                gboolean            in_idle)
{
  if (op->error != NULL)
    {
      g_assert (!g_simple_async_result_get_op_res_gpointer (op->simple));
      g_simple_async_result_set_from_error (op->simple, op->error);
      g_error_free (op->error);
    }

  else
    g_assert (g_simple_async_result_get_op_res_gpointer (op->simple) ||
              op->dcem.body == NULL);

  if (op->cancellable)
    g_object_unref (op->cancellable);

  if (op->dcem.body)
    g_variant_unref (op->dcem.body);

  if (in_idle)
    g_simple_async_result_complete_in_idle (op->simple);
  else
    g_simple_async_result_complete (op->simple);

  g_object_unref (op->simple);

  g_slice_free (DConfClientAsyncOp, op);
}

static gboolean
dconf_client_interpret_reply (DConfEngineMessage  *dcem,
                              GDBusMessage        *reply,
                              gchar              **tag,
                              GError             **error)
{
  gboolean success;

  if (reply == NULL)
    /* error will already have been set */
    return FALSE;

  
  success = dconf_engine_interpret_reply (dcem,
                                          g_dbus_message_get_sender (reply),
                                          g_dbus_message_get_body (reply),
                                          tag, error);
  g_object_unref (reply);

  return success;
}


static void
dconf_client_async_op_call_done (GObject      *object,
                                 GAsyncResult *result,
                                 gpointer      user_data)
{
  DConfClientAsyncOp *op = user_data;
  GDBusMessage *reply;

  reply = g_dbus_connection_send_message_with_reply_finish (G_DBUS_CONNECTION (object),
                                                            result, &op->error);

  if (reply != NULL)
    {
      gchar *tag;

      if (dconf_client_interpret_reply (&op->dcem, reply, &tag, &op->error))
        g_simple_async_result_set_op_res_gpointer (op->simple, tag, g_free);

      g_object_unref (reply);
    }

  dconf_client_async_op_complete (op, FALSE);
}

static void
dconf_client_async_op_get_bus_done (GObject      *no_object,
                                    GAsyncResult *result,
                                    gpointer      user_data)
{
  DConfClientAsyncOp *op = user_data;
  GDBusConnection *connection;

  if ((connection = g_bus_get_finish (result, &op->error)) && op->dcem.body)
    g_dbus_connection_call (connection, op->dcem.destination,
                            op->dcem.object_path, op->dcem.interface,
                            op->dcem.method, op->dcem.body,
                            op->dcem.reply_type, 0, -1, op->cancellable,
                            dconf_client_async_op_call_done, op);

  else
    dconf_client_async_op_complete (op, FALSE);
}

static void
dconf_client_async_op_run (DConfClientAsyncOp *op)
{
  if (op->error)
    dconf_client_async_op_complete (op, TRUE);
  else
    g_bus_get (dconf_client_bus_type (&op->dcem), op->cancellable,
               dconf_client_async_op_get_bus_done, op);
}

static gboolean
dconf_client_async_op_finish (gpointer       client,
                              GAsyncResult  *result,
                              gpointer       source_tag,
                              gchar        **tag,
                              GError       **error)
{
  GSimpleAsyncResult *simple;

  g_return_val_if_fail (DCONF_IS_CLIENT (client), FALSE);
  g_return_val_if_fail (g_simple_async_result_is_valid (result, client,
                                                        source_tag), FALSE);
  simple = G_SIMPLE_ASYNC_RESULT (result);

  if (g_simple_async_result_propagate_error (simple, error))
    return FALSE;

  if (tag)
    *tag = g_strdup (g_simple_async_result_get_op_res_gpointer (simple));

  return TRUE;
}

static void
dconf_client_finalize (GObject *object)
{
  DConfClient *client = DCONF_CLIENT (object);

  if (client->notify)
    client->notify (client->user_data);

  G_OBJECT_CLASS (dconf_client_parent_class)
    ->finalize (object);
}

static void
dconf_client_init (DConfClient *client)
{
}

static void
dconf_client_class_init (DConfClientClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = dconf_client_finalize;
}

/**
 * dconf_client_new:
 * @context: the context string (must by %NULL for now)
 * @watch_func: the function to call when changes occur
 * @user_data: the user_data to pass to @watch_func
 * @notify: the function to free @user_data when no longer needed
 * @returns: a new #DConfClient
 *
 * Creates a new #DConfClient for the given context.
 **/
DConfClient *
dconf_client_new (const gchar          *context,
                  gboolean              will_write,
                  DConfWatchFunc        watch_func,
                  gpointer              user_data,
                  GDestroyNotify        notify)
{
  DConfClient *client = g_object_new (DCONF_TYPE_CLIENT, NULL);

  client->engine = dconf_engine_new (context);
  client->will_write = will_write;
  client->watch_func = watch_func;
  client->user_data = user_data;
  client->notify = notify;

  return client;
}

/**
 * dconf_client_read:
 * @client: a #DConfClient
 * @key: a valid dconf key
 * @returns: the value corresponding to @key, or %NULL if there is none
 *
 * Reads the value named by @key from dconf.  If no such value exists,
 * %NULL is returned.
 **/
GVariant *
dconf_client_read (DConfClient   *client,
                   const gchar   *key)
{
  return dconf_engine_read (client->engine, key, DCONF_READ_NORMAL);
}

/**
 * dconf_client_read_default:
 * @client: a #DConfClient
 * @key: a valid dconf key
 * @returns: the default value corresponding to @key, or %NULL if there
 *           is none
 *
 * Reads the value named by @key from any existing default/mandatory
 * databases but ignoring any value set by the user.  The result is as
 * if the named key had just been reset.
 **/
GVariant *
dconf_client_read_default (DConfClient *client,
                           const gchar *key)
{
  return dconf_engine_read (client->engine, key, DCONF_READ_RESET);
}

/**
 * dconf_client_read_no_default:
 * @client: a #DConfClient
 * @key: a valid dconf key
 * @returns: the user value corresponding to @key, or %NULL if there is
 *           none
 *
 * Reads the value named by @key as set by the user, ignoring any
 * default/mandatory databases.  Normal applications will never want to
 * do this, but it may be useful for administrative or configuration
 * tweaking utilities to have access to this information.
 *
 * Note that in the case of mandatory keys, the result of
 * dconf_client_read_no_default() with a fallback to
 * dconf_client_read_default() is not necessarily the same as the result
 * of a dconf_client_read().  This is because the user may have set a
 * value before the key became marked as mandatory, in which case this
 * call will see the user's (otherwise inaccessible) key.
 **/
GVariant *
dconf_client_read_no_default (DConfClient *client,
                              const gchar *key)
{
  return dconf_engine_read (client->engine, key, DCONF_READ_SET);
}

static GDBusMessage *
dconf_client_create_call (DConfEngineMessage *dcem)
{
  GDBusMessage *message;

  message = g_dbus_message_new_method_call (dcem->destination,
                                            dcem->object_path,
                                            dcem->interface,
                                            dcem->method);
  g_dbus_message_set_body (message, dcem->body);

  return message;
}

static gboolean
dconf_client_call_sync (DConfClient          *client,
                        DConfEngineMessage   *dcem,
                        gchar               **tag,
                        GCancellable         *cancellable,
                        GError              **error)
{
  GDBusConnection *connection;

  connection = g_bus_get_sync (dconf_client_bus_type (dcem),
                               cancellable, error);

  if (connection == NULL)
    return FALSE;

  if (dcem->body)
    {
      GDBusMessage *message, *reply;

      message = dconf_client_create_call (dcem);
      reply = g_dbus_connection_send_message_with_reply_sync (connection,
                                                              message,
                                                              -1, NULL,
                                                              cancellable,
                                                              error);
      g_object_unref (message);

      return dconf_client_interpret_reply (dcem, reply, tag, error);
    }

  return TRUE;
}

/**
 * dconf_client_write:
 * @client: a #DConfClient
 * @key: a dconf key
 * @value (allow-none): a #GVariant, or %NULL
 * @sequence: (out) (allow-none): the sequence number of this write
 * @cancellable: a #GCancellable, or %NULL
 * @error: a pointer to a #GError, or %NULL
 * @returns: %TRUE if the write is successful
 *
 * Write a value to the given @key, or reset @key to its default value.
 *
 * If @value is %NULL then @key is reset to its default value (which may
 * be completely unset), otherwise @value becomes the new value.
 *
 * If @sequence is non-%NULL then it is set to the sequence number of
 * this write.  The sequence number is unique to this process.
 **/
gboolean
dconf_client_write (DConfClient   *client,
                    const gchar   *key,
                    GVariant      *value,
                    gchar        **tag,
                    GCancellable  *cancellable,
                    GError       **error)
{
  DConfEngineMessage dcem;

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

  return dconf_client_call_sync (client, &dcem, tag, cancellable, error);
}

/**
 * dconf_client_write_async:
 * @client: a #DConfClient
 * @key: a dconf key
 * @value (allow-none): a #GVariant, or %NULL
 * @cancellable: a #GCancellable, or %NULL
 * @callback: the function to call when complete
 * @user_data: the user data for @callback
 *
 * Writes a value to the given @key, or reset @key to its default value.
 *
 * This is the asynchronous version of dconf_client_write().  You should
 * call dconf_client_write_finish() from @callback to collect the
 * result.
 **/
void
dconf_client_write_async (DConfClient          *client,
                          const gchar          *key,
                          GVariant             *value,
                          GCancellable         *cancellable,
                          GAsyncReadyCallback   callback,
                          gpointer              user_data)
{
  DConfClientAsyncOp *op;

  op = dconf_client_async_op_new (client, dconf_client_write_async,
                                  cancellable, callback, user_data);
  dconf_engine_write (client->engine, &op->dcem, key, value, &op->error);
  dconf_client_async_op_run (op);
}

/**
 * dconf_client_write_finish:
 * @client: a #DConfClient
 * @sequence: (out) (allow-none): the sequence number of this write
 * @error: a pointer to a #GError, or %NULL
 *
 * Collects the result from a prior call to dconf_client_write_async().
 **/
gboolean
dconf_client_write_finish (DConfClient   *client,
                           GAsyncResult  *result,
                           gchar        **tag,
                           GError       **error)
{
  return dconf_client_async_op_finish (client, result,
                                       dconf_client_write_async,
                                       tag, error);
}

/**
 * dconf_client_list:
 * @client: a #DConfClient
 * @dir: a dconf dir
 * @length: the number of items that were returned
 * @returns: (array length=length): the paths located directly below @dir
 *
 * Lists the keys and dirs located directly below @dir.
 *
 * You should free the return result with g_strfreev() when it is no
 * longer needed.
 **/
gchar **
dconf_client_list (DConfClient    *client,
                   const gchar    *prefix,
                   gsize          *length)
{
  return dconf_engine_list (client->engine, prefix, NULL, length);
}

/**
 * dconf_client_set_locked:
 * @client: a #DConfClient
 * @path: a dconf path
 * @locked: %TRUE to lock, %FALSE to unlock
 * @cancellable: a #GCancellable, or %NULL
 * @error: a pointer to a #GError, or %NULL
 * @returns: %TRUE if setting the lock was successful
 *
 * Marks a dconf path as being locked.
 *
 * Locks do not affect writes to this #DConfClient.  You can still write
 * to a key that is marked as being locked without problems.
 *
 * Locks are only effective when they are set on a database that is
 * being used as the source of default/mandatory values.  In that case,
 * the lock will prevent writes from occuring to the database that has
 * this database as its defaults.
 **/
gboolean
dconf_client_set_locked (DConfClient   *client,
                         const gchar   *path,
                         gboolean       locked,
                         GCancellable  *cancellable,
                         GError       **error)
{
  DConfEngineMessage dcem;

  dconf_engine_set_locked (client->engine, &dcem, path, locked);

  return dconf_client_call_sync (client, &dcem, NULL, cancellable, error);
}

gboolean
dconf_client_is_writable (DConfClient  *client,
                          const gchar  *path,
                          GError      **error)
{
  DConfEngineMessage dcem;

  if (!dconf_engine_is_writable (client->engine, &dcem, path, error))
    return FALSE;

  return dconf_client_call_sync (client, &dcem, NULL, NULL, error);
}

gboolean
dconf_client_write_many (DConfClient          *client,
                         const gchar          *prefix,
                         const gchar * const  *rels,
                         GVariant            **values,
                         gchar               **tag,
                         GCancellable         *cancellable,
                         GError              **error)
{
  DConfEngineMessage dcem;

  if (!dconf_engine_write_many (client->engine, &dcem, prefix, rels, values, error))
    return FALSE;

  return dconf_client_call_sync (client, &dcem, tag, cancellable, error);
}

void
dconf_client_write_many_async (DConfClient          *client,
                               const gchar          *prefix,
                               const gchar * const  *rels,
                               GVariant            **values,
                               GCancellable         *cancellable,
                               GAsyncReadyCallback   callback,
                               gpointer              user_data)
{
  DConfClientAsyncOp *op;

  op = dconf_client_async_op_new (client, dconf_client_write_async,
                                  cancellable, callback, user_data);
  dconf_engine_write_many (client->engine, &op->dcem, prefix,
                           rels, values, &op->error);
  dconf_client_async_op_run (op);
}

gboolean
dconf_client_write_many_finish (DConfClient   *client,
                                GAsyncResult  *result,
                                gchar        **tag,
                                GError       **error)
{
  return dconf_client_async_op_finish (client, result,
                                       dconf_client_write_many_async,
                                       tag, error);
}

#if 0
gboolean                dconf_client_watch                              (DConfClient          *client,
                                                                         const gchar          *name,
                                                                         GError              **error);
void                    dconf_client_watch_async                        (DConfClient          *client,
                                                                         const gchar          *name,
                                                                         GAsyncReadyCallback   callback,
                                                                         gpointer              user_data);
gboolean                dconf_client_watch_finish                       (DConfClient          *client,
                                                                         GAsyncResult         *result,
                                                                         gpointer              user_data);
gboolean                dconf_client_unwatch                            (DConfClient          *client,
                                                                         const gchar          *name,
                                                                         GError              **error);
void                    dconf_client_unwatch_async                      (DConfClient          *client,
                                                                         const gchar          *name,
                                                                         GAsyncReadyCallback   callback,
                                                                         gpointer              user_data);
gboolean                dconf_client_unwatch_finish                     (DConfClient          *client,
                                                                         GAsyncResult         *result,
                                                                         gpointer              user_data);

#endif