summaryrefslogtreecommitdiff
path: root/gio/src/dbusconnection.ccg
blob: 67dceee86e0998a8a334c8d7e51df79b0e8f724b (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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* Copyright (C) 2010 The giomm Development Team
 *
 * 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.1 of the License, 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, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <glibmm/exceptionhandler.h>
#include <giomm/actiongroup.h>
#include <giomm/dbusauthobserver.h>
#include <giomm/dbusintrospection.h>
#include <giomm/dbusmethodinvocation.h>
#include <giomm/dbuserror.h>
#include <giomm/menumodel.h>

#ifdef G_OS_UNIX
#include <giomm/unixfdlist.h>
#endif

#include <giomm/slot_async.h>
#include <mutex>

namespace
{

extern "C" {

static void
DBusConnection_Signal_giomm_callback(GDBusConnection* connection, const char* sender_name,
  const char* object_path, const char* interface_name, const char* signal_name,
  GVariant* parameters, void* user_data)
{
  Gio::DBus::Connection::SlotSignal* the_slot =
    static_cast<Gio::DBus::Connection::SlotSignal*>(user_data);

  try
  {
    (*the_slot)(Glib::wrap(connection, true), (sender_name ? sender_name : ""),
      (object_path ? object_path : ""), (interface_name ? interface_name : ""),
      (signal_name ? signal_name : ""), Glib::VariantContainerBase(parameters, true));
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
}

static void
DBusConnection_Signal_giomm_callback_destroy(void* data)
{
  delete static_cast<Gio::DBus::Connection::SlotSignal*>(data);
}

static GDBusMessage*
DBusConnection_Message_Filter_giomm_callback(
  GDBusConnection* connection, GDBusMessage* message, gboolean incoming, void* user_data)
{
  Gio::DBus::Connection::SlotMessageFilter* the_slot =
    static_cast<Gio::DBus::Connection::SlotMessageFilter*>(user_data);

  try
  {
    auto result = (*the_slot)(
      Glib::wrap(connection, true), Glib::wrap(message, true), static_cast<bool>(incoming));
    return (result) ? result->gobj_copy() : nullptr;
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }

  return message;
}

static void
DBusConnection_Message_Filter_giomm_callback_destroy(void* data)
{
  delete static_cast<Gio::DBus::Connection::SlotMessageFilter*>(data);
}

} // extern "C"

// Glib::wrap(GDBusConnection* object, bool take_copy) must be protected by a
// mutex while a C++ wrapper is being constructed. The same GDBusConnection
// instance can be used in more than one thread. The wrap() function can be
// called simultaneously in different threads for the same GDBusConnection
// instance before it has been given a C++ wrapper.
// https://gitlab.gnome.org/GNOME/glibmm/issues/56
std::mutex wrap_mutex;

} // anonymous namespace

namespace Gio
{

namespace DBus
{

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, const SlotAsyncReady& slot,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init_async(slot, cancellable);
}

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init_async(slot, cancellable);
}

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, const SlotAsyncReady& slot, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init_async(slot);
}

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const SlotAsyncReady& slot, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init_async(slot);
}

Connection::Connection(const std::string& address, const Glib::RefPtr<AuthObserver>& observer,
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init_async(slot, cancellable);
}

Connection::Connection(const std::string& address, const SlotAsyncReady& slot,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init_async(slot, cancellable);
}

Connection::Connection(const std::string& address, const Glib::RefPtr<AuthObserver>& observer,
  const SlotAsyncReady& slot, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init_async(slot);
}

Connection::Connection(
  const std::string& address, const SlotAsyncReady& slot, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init_async(slot);
}

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, const Glib::RefPtr<Cancellable>& cancellable,
  ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init(cancellable);
}

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init(cancellable);
}

Connection::Connection(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init();
}

Connection::Connection(
  const Glib::RefPtr<IOStream>& stream, const std::string& guid, ConnectionFlags flags)
: _CONSTRUCT("stream", Glib::unwrap(stream), "guid", Glib::c_str_or_nullptr(guid), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init();
}

Connection::Connection(const std::string& address, const Glib::RefPtr<AuthObserver>& observer,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init(cancellable);
}

Connection::Connection(
  const std::string& address, const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init(cancellable);
}

Connection::Connection(
  const std::string& address, const Glib::RefPtr<AuthObserver>& observer, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer", Glib::unwrap(observer))
{
  init();
}

Connection::Connection(const std::string& address, ConnectionFlags flags)
: _CONSTRUCT("address", Glib::c_str_or_nullptr(address), "flags",
    static_cast<GDBusConnectionFlags>(flags), "authentication-observer",
    static_cast<GDBusAuthObserver*>(nullptr))
{
  init();
}

// static
void
Connection::create(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, const SlotAsyncReady& slot,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().

  Connection(stream, guid, observer, slot, cancellable, flags);
}

// static
void
Connection::create(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().

  Connection(stream, guid, slot, cancellable, flags);
}

// static
void
Connection::create(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, const SlotAsyncReady& slot, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().
  Connection(stream, guid, observer, slot, flags);
}

// static
void
Connection::create(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const SlotAsyncReady& slot, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().
  Connection(stream, guid, slot, flags);
}

// static
Glib::RefPtr<Connection>
Connection::create_sync(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, const Glib::RefPtr<Cancellable>& cancellable,
  ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(stream, guid, observer, cancellable, flags));
}

// static
Glib::RefPtr<Connection>
Connection::create_sync(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(stream, guid, cancellable, flags));
}

// static
Glib::RefPtr<Connection>
Connection::create_sync(const Glib::RefPtr<IOStream>& stream, const std::string& guid,
  const Glib::RefPtr<AuthObserver>& observer, ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(stream, guid, observer, flags));
}

// static
Glib::RefPtr<Connection>
Connection::create_sync(
  const Glib::RefPtr<IOStream>& stream, const std::string& guid, ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(stream, guid, flags));
}

// static
void
Connection::create_for_address(const std::string& address,
  const Glib::RefPtr<AuthObserver>& observer, const SlotAsyncReady& slot,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().
  Connection(address, observer, slot, cancellable, flags);
}

// static
void
Connection::create_for_address(const std::string& address, const SlotAsyncReady& slot,
  const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().
  Connection(address, slot, cancellable, flags);
}

// static
void
Connection::create_for_address(const std::string& address,
  const Glib::RefPtr<AuthObserver>& observer, const SlotAsyncReady& slot, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().
  Connection(address, observer, slot, flags);
}

// static
void
Connection::create_for_address(
  const std::string& address, const SlotAsyncReady& slot, ConnectionFlags flags)
{
  // Note that this does not return anything, because it is async - see
  // create_finish().
  Connection(address, slot, flags);
}

// static
Glib::RefPtr<Connection>
Connection::create_for_address_sync(const std::string& address,
  const Glib::RefPtr<AuthObserver>& observer, const Glib::RefPtr<Cancellable>& cancellable,
  ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(address, observer, cancellable, flags));
}

// static
Glib::RefPtr<Connection>
Connection::create_for_address_sync(
  const std::string& address, const Glib::RefPtr<Cancellable>& cancellable, ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(address, cancellable, flags));
}

// static
Glib::RefPtr<Connection>
Connection::create_for_address_sync(
  const std::string& address, const Glib::RefPtr<AuthObserver>& observer, ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(address, observer, flags));
}

// static
Glib::RefPtr<Connection>
Connection::create_for_address_sync(const std::string& address, ConnectionFlags flags)
{
  return Glib::make_refptr_for_instance<Connection>(new Connection(address, flags));
}

// static
void
Connection::get(
  BusType bus_type, const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable)
{
  auto slot_copy = new SlotAsyncReady(slot);

  g_bus_get(static_cast<GBusType>(bus_type), Glib::unwrap(cancellable), &SignalProxy_async_callback,
    slot_copy);
}

// static
void
Connection::get(BusType bus_type, const SlotAsyncReady& slot)
{
  auto slot_copy = new SlotAsyncReady(slot);

  g_bus_get(static_cast<GBusType>(bus_type), nullptr, &SignalProxy_async_callback, slot_copy);
}

void
Connection::close()
{
  g_dbus_connection_close(gobj(), nullptr, nullptr, nullptr);
}

void
Connection::close(const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable)
{
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_close(
    gobj(), Glib::unwrap(cancellable), &SignalProxy_async_callback, slot_copy);
}

void
Connection::close(const SlotAsyncReady& slot)
{
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_close(gobj(), nullptr, &SignalProxy_async_callback, slot_copy);
}

void
Connection::flush()
{
  g_dbus_connection_flush(gobj(), nullptr, nullptr, nullptr);
}

void
Connection::flush(const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable)
{
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_flush(
    gobj(), Glib::unwrap(cancellable), &SignalProxy_async_callback, slot_copy);
}

void
Connection::flush(const SlotAsyncReady& slot)
{
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_flush(gobj(), nullptr, &SignalProxy_async_callback, slot_copy);
}

bool
Connection::send_message(const Glib::RefPtr<Message>& message, SendMessageFlags flags)
{
  GError* gerror = nullptr;

  const bool result = g_dbus_connection_send_message(
    gobj(), Glib::unwrap(message), static_cast<GDBusSendMessageFlags>(flags), nullptr, &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return result;
}

void
Connection::send_message_with_reply(const Glib::RefPtr<Message>& message, int timeout_msec,
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable)
{
  auto slot_copy = new SlotAsyncReady(slot);
  volatile guint32 out_serial = 0;
  g_dbus_connection_send_message_with_reply(gobj(), Glib::unwrap(message),
    static_cast<GDBusSendMessageFlags>(message->get_flags()), timeout_msec, &out_serial,
    Glib::unwrap(cancellable), &SignalProxy_async_callback, slot_copy);
  message->set_serial(out_serial);
}

void
Connection::send_message_with_reply(
  const Glib::RefPtr<Message>& message, int timeout_msec, const SlotAsyncReady& slot)
{
  auto slot_copy = new SlotAsyncReady(slot);
  volatile guint32 out_serial = 0;
  g_dbus_connection_send_message_with_reply(gobj(), Glib::unwrap(message),
    static_cast<GDBusSendMessageFlags>(message->get_flags()), timeout_msec, &out_serial, nullptr,
    &SignalProxy_async_callback, slot_copy);
  message->set_serial(out_serial);
}

Glib::RefPtr<Message>
Connection::send_message_with_reply_sync(const Glib::RefPtr<Message>& message,
  const Glib::RefPtr<Cancellable>& cancellable, gint timeout_msec)
{
  volatile guint32 out_serial = 0;
  GError* gerror = nullptr;

  GDBusMessage* result = g_dbus_connection_send_message_with_reply_sync(gobj(),
    Glib::unwrap(message), static_cast<GDBusSendMessageFlags>(message->get_flags()), timeout_msec,
    &out_serial, Glib::unwrap(cancellable), &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);
  message->set_serial(out_serial);
  return Glib::wrap(result);
}

Glib::RefPtr<Message>
Connection::send_message_with_reply_sync(const Glib::RefPtr<Message>& message, gint timeout_msec)
{
  volatile guint32 out_serial = 0;
  GError* gerror = nullptr;

  GDBusMessage* result = g_dbus_connection_send_message_with_reply_sync(gobj(),
    Glib::unwrap(message), static_cast<GDBusSendMessageFlags>(message->get_flags()), timeout_msec,
    &out_serial, nullptr, &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);
  message->set_serial(out_serial);
  return Glib::wrap(result);
}

void
Connection::call(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& method_name, const Glib::VariantContainerBase& parameters,
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable,
  const Glib::ustring& bus_name, int timeout_msec, CallFlags flags,
  const Glib::VariantType& reply_type)
{
  // Create a copy of the slot.
  // A pointer to it will be passed through the callback's data parameter
  // and deleted in the callback.
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_call(gobj(), Glib::c_str_or_nullptr(bus_name), object_path.c_str(),
    interface_name.c_str(), method_name.c_str(), const_cast<GVariant*>(parameters.gobj()),
    reply_type.gobj(), static_cast<GDBusCallFlags>(flags), timeout_msec, Glib::unwrap(cancellable),
    &SignalProxy_async_callback, slot_copy);
}

// Non-cancellable version.
void
Connection::call(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& method_name, const Glib::VariantContainerBase& parameters,
  const SlotAsyncReady& slot, const Glib::ustring& bus_name, int timeout_msec, CallFlags flags,
  const Glib::VariantType& reply_type)
{
  // Create a copy of the slot.
  // A pointer to it will be passed through the callback's data parameter
  // and deleted in the callback.
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_call(gobj(), Glib::c_str_or_nullptr(bus_name), object_path.c_str(),
    interface_name.c_str(), method_name.c_str(), const_cast<GVariant*>(parameters.gobj()),
    reply_type.gobj(), static_cast<GDBusCallFlags>(flags), timeout_msec, nullptr,
    &SignalProxy_async_callback, slot_copy);
}

Glib::VariantContainerBase
Connection::call_sync(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& method_name, const Glib::VariantContainerBase& parameters,
  const Glib::RefPtr<Cancellable>& cancellable, const Glib::ustring& bus_name, int timeout_msec,
  CallFlags flags, const Glib::VariantType& reply_type)
{
  GError* gerror = nullptr;

  GVariant* const gvariant = g_dbus_connection_call_sync(gobj(), Glib::c_str_or_nullptr(bus_name),
    object_path.c_str(), interface_name.c_str(), method_name.c_str(),
    const_cast<GVariant*>(parameters.gobj()), reply_type.gobj(), static_cast<GDBusCallFlags>(flags),
    timeout_msec, Glib::unwrap(cancellable), &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return Glib::VariantContainerBase(gvariant, false); // Dont' take an extra reference.
}

// Non-cancellable version.
Glib::VariantContainerBase
Connection::call_sync(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& method_name, const Glib::VariantContainerBase& parameters,
  const Glib::ustring& bus_name, int timeout_msec, CallFlags flags,
  const Glib::VariantType& reply_type)
{
  GError* gerror = nullptr;

  GVariant* const gvariant =
    g_dbus_connection_call_sync(gobj(), Glib::c_str_or_nullptr(bus_name), object_path.c_str(),
      interface_name.c_str(), method_name.c_str(), const_cast<GVariant*>(parameters.gobj()),
      reply_type.gobj(), static_cast<GDBusCallFlags>(flags), timeout_msec, nullptr, &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return Glib::VariantContainerBase(gvariant, false); // Dont' take an extra reference.
}

#ifdef G_OS_UNIX
// With a UnixFDList.
void
Connection::call(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& method_name, const Glib::VariantContainerBase& parameters,
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable,
  const Glib::RefPtr<UnixFDList>& fd_list, const Glib::ustring& bus_name, int timeout_msec,
  CallFlags flags, const Glib::VariantType& reply_type)
{
  // Create a copy of the slot.
  // A pointer to it will be passed through the callback's data parameter
  // and deleted in the callback.
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_call_with_unix_fd_list(gobj(), Glib::c_str_or_nullptr(bus_name),
    object_path.c_str(), interface_name.c_str(), method_name.c_str(),
    const_cast<GVariant*>(parameters.gobj()), reply_type.gobj(), static_cast<GDBusCallFlags>(flags),
    timeout_msec, Glib::unwrap(fd_list), Glib::unwrap(cancellable), &SignalProxy_async_callback,
    slot_copy);
}

// Non-cancellable version (with a UnixFDList).
void
Connection::call(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& method_name, const Glib::VariantContainerBase& parameters,
  const SlotAsyncReady& slot, const Glib::RefPtr<UnixFDList>& fd_list,
  const Glib::ustring& bus_name, int timeout_msec, CallFlags flags,
  const Glib::VariantType& reply_type)
{
  // Create a copy of the slot.
  // A pointer to it will be passed through the callback's data parameter
  // and deleted in the callback.
  auto slot_copy = new SlotAsyncReady(slot);

  g_dbus_connection_call_with_unix_fd_list(gobj(), Glib::c_str_or_nullptr(bus_name),
    object_path.c_str(), interface_name.c_str(), method_name.c_str(),
    const_cast<GVariant*>(parameters.gobj()), reply_type.gobj(), static_cast<GDBusCallFlags>(flags),
    timeout_msec, Glib::unwrap(fd_list), nullptr, &SignalProxy_async_callback, slot_copy);
}
#endif // G_OS_UNIX

void
Connection::emit_signal(const Glib::ustring& object_path, const Glib::ustring& interface_name,
  const Glib::ustring& signal_name, const Glib::ustring& destination_bus_name,
  const Glib::VariantContainerBase& parameters)
{
  GError* gerror = nullptr;

  // destination_bus_name is checked to see if it is empty so that nullptr can be passed
  // to the C API. This is done because the bus name can be nullptr in the C API,
  // meaning that the signal should be emitted to all the listeners.
  g_dbus_connection_emit_signal(gobj(), Glib::c_str_or_nullptr(destination_bus_name),
    object_path.c_str(), interface_name.c_str(), signal_name.c_str(),
    const_cast<GVariant*>(parameters.gobj()), &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);
}

guint
Connection::signal_subscribe(const SlotSignal& slot, const Glib::ustring& sender,
  const Glib::ustring& interface_name, const Glib::ustring& member,
  const Glib::ustring& object_path, const Glib::ustring& arg0, SignalFlags flags)
{
  auto slot_copy = new SlotSignal(slot);

  return g_dbus_connection_signal_subscribe(gobj(), Glib::c_str_or_nullptr(sender),
    Glib::c_str_or_nullptr(interface_name), Glib::c_str_or_nullptr(member),
    Glib::c_str_or_nullptr(object_path), Glib::c_str_or_nullptr(arg0),
    static_cast<GDBusSignalFlags>(flags), &DBusConnection_Signal_giomm_callback, slot_copy,
    &DBusConnection_Signal_giomm_callback_destroy);
}

guint
Connection::add_filter(const SlotMessageFilter& slot)
{
  auto slot_copy = new SlotMessageFilter(slot);

  return g_dbus_connection_add_filter(gobj(), &DBusConnection_Message_Filter_giomm_callback,
    slot_copy, DBusConnection_Message_Filter_giomm_callback_destroy);
}

guint
Connection::register_object(const Glib::ustring& object_path,
  const Glib::RefPtr<InterfaceInfo>& interface_info, const InterfaceVTable& vtable)
{
  GError* gerror = nullptr;

  const guint result =
    g_dbus_connection_register_object(gobj(), object_path.c_str(), Glib::unwrap(interface_info),
      vtable.gobj(), const_cast<InterfaceVTable*>(&vtable), nullptr, &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return result;
}

guint
Connection::register_object(
  const Glib::ustring& object_path, const Glib::RefPtr<InterfaceInfo>& interface_info)
{
  GError* gerror = nullptr;

  const guint result = g_dbus_connection_register_object(
    gobj(), object_path.c_str(), Glib::unwrap(interface_info), nullptr, nullptr, nullptr, &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return result;
}

guint
Connection::register_subtree(
  const Glib::ustring& object_path, const SubtreeVTable& vtable, SubtreeFlags flags)
{
  GError* gerror = nullptr;

  const guint result =
    g_dbus_connection_register_subtree(gobj(), object_path.c_str(), vtable.gobj(),
      static_cast<GDBusSubtreeFlags>(flags), const_cast<SubtreeVTable*>(&vtable), nullptr, &gerror);

  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return result;
}

} // namespace DBus

} // namespace Gio

namespace Glib
{

Glib::RefPtr<Gio::DBus::Connection> wrap(GDBusConnection* object, bool take_copy)
{
  Glib::ObjectBase* pCppObject = nullptr;
  if (!ObjectBase::_get_current_wrapper((GObject*)object))
  {
    // 'object' does not yet have a C++ wrapper. Construction of the C++ wrapper
    // must be thread-safe. See comments at the definition of wrap_mutex.
    std::lock_guard<std::mutex> lock(wrap_mutex);
    pCppObject = Glib::wrap_auto((GObject*)object, take_copy);
  }
  else
    pCppObject = Glib::wrap_auto((GObject*)object, take_copy);

  return Glib::make_refptr_for_instance<Gio::DBus::Connection>(dynamic_cast<Gio::DBus::Connection*>(pCppObject));
  //We use dynamic_cast<> in case of multiple inheritance.
}

} // namespace Glib