-
Notifications
You must be signed in to change notification settings - Fork 0
/
qdbusconnection.cpp
1289 lines (1089 loc) · 46.3 KB
/
qdbusconnection.cpp
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtDBus module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdbusconnection.h"
#include "qdbusconnection_p.h"
#include <qdebug.h>
#include <qcoreapplication.h>
#include <qstringlist.h>
#include <qtimer.h>
#include <qthread.h>
#include <QtCore/private/qlocking_p.h>
#include "qdbusconnectioninterface.h"
#include "qdbuserror.h"
#include "qdbusmessage.h"
#include "qdbusmessage_p.h"
#include "qdbusinterface_p.h"
#include "qdbusutil_p.h"
#include "qdbusconnectionmanager_p.h"
#include "qdbuspendingcall_p.h"
#include "qdbusthreaddebug_p.h"
#include <algorithm>
#ifdef interface
#undef interface
#endif
#ifndef QT_NO_DBUS
QT_BEGIN_NAMESPACE
#ifdef Q_OS_WIN
static void preventDllUnload();
#endif
Q_GLOBAL_STATIC(QDBusConnectionManager, _q_manager)
QDBusConnectionPrivate *QDBusConnectionManager::busConnection(QDBusConnection::BusType type)
{
static_assert(int(QDBusConnection::SessionBus) + int(QDBusConnection::SystemBus) == 1);
Q_ASSERT(type == QDBusConnection::SessionBus || type == QDBusConnection::SystemBus);
if (!qdbus_loadLibDBus())
return nullptr;
// we'll start in suspended delivery mode if we're in the main thread
// (the event loop will resume delivery)
bool suspendedDelivery = qApp && qApp->thread() == QThread::currentThread();
const auto locker = qt_scoped_lock(defaultBusMutex);
if (defaultBuses[type])
return defaultBuses[type];
QString name = QStringLiteral("qt_default_session_bus");
if (type == QDBusConnection::SystemBus)
name = QStringLiteral("qt_default_system_bus");
return defaultBuses[type] = connectToBus(type, name, suspendedDelivery);
}
QDBusConnectionPrivate *QDBusConnectionManager::connection(const QString &name) const
{
return connectionHash.value(name, nullptr);
}
void QDBusConnectionManager::removeConnection(const QString &name)
{
QDBusConnectionPrivate *d = nullptr;
d = connectionHash.take(name);
if (d && !d->ref.deref())
d->deleteLater();
// Static objects may be keeping the connection open.
// However, it is harmless to have outstanding references to a connection that is
// closing as long as those references will be soon dropped without being used.
// ### Output a warning if connections are being used after they have been removed.
}
QDBusConnectionManager::QDBusConnectionManager()
{
connect(this, &QDBusConnectionManager::connectionRequested,
this, &QDBusConnectionManager::executeConnectionRequest, Qt::BlockingQueuedConnection);
connect(this, &QDBusConnectionManager::serverRequested,
this, &QDBusConnectionManager::createServer, Qt::BlockingQueuedConnection);
moveToThread(this); // ugly, don't do this in other projects
#ifdef Q_OS_WIN
// prevent the library from being unloaded on Windows. See comments in the function.
preventDllUnload();
#endif
defaultBuses[0] = defaultBuses[1] = nullptr;
start();
}
QDBusConnectionManager::~QDBusConnectionManager()
{
quit();
wait();
}
QDBusConnectionManager* QDBusConnectionManager::instance()
{
return _q_manager();
}
Q_DBUS_EXPORT void qDBusBindToApplication();
void qDBusBindToApplication()
{
}
void QDBusConnectionManager::setConnection(const QString &name, QDBusConnectionPrivate *c)
{
connectionHash[name] = c;
c->name = name;
}
void QDBusConnectionManager::run()
{
exec();
// cleanup:
const auto locker = qt_scoped_lock(mutex);
for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
it != connectionHash.constEnd(); ++it) {
QDBusConnectionPrivate *d = it.value();
if (!d->ref.deref()) {
delete d;
} else {
d->closeConnection();
d->moveToThread(nullptr); // allow it to be deleted in another thread
}
}
connectionHash.clear();
// allow deletion from any thread without warning
moveToThread(nullptr);
}
QDBusConnectionPrivate *QDBusConnectionManager::connectToBus(QDBusConnection::BusType type, const QString &name,
bool suspendedDelivery)
{
ConnectionRequestData data;
data.type = ConnectionRequestData::ConnectToStandardBus;
data.busType = type;
data.name = &name;
data.suspendedDelivery = suspendedDelivery;
emit connectionRequested(&data);
if (suspendedDelivery && data.result->connection) {
data.result->ref.ref();
QDBusConnectionDispatchEnabler *o = new QDBusConnectionDispatchEnabler(data.result);
QTimer::singleShot(0, o, SLOT(execute()));
o->moveToThread(qApp->thread()); // qApp was checked in the caller
}
return data.result;
}
QDBusConnectionPrivate *QDBusConnectionManager::connectToBus(const QString &address, const QString &name)
{
ConnectionRequestData data;
data.type = ConnectionRequestData::ConnectToBusByAddress;
data.busAddress = &address;
data.name = &name;
data.suspendedDelivery = false;
emit connectionRequested(&data);
return data.result;
}
QDBusConnectionPrivate *QDBusConnectionManager::connectToPeer(const QString &address, const QString &name)
{
ConnectionRequestData data;
data.type = ConnectionRequestData::ConnectToPeerByAddress;
data.busAddress = &address;
data.name = &name;
data.suspendedDelivery = false;
emit connectionRequested(&data);
return data.result;
}
void QDBusConnectionManager::executeConnectionRequest(QDBusConnectionManager::ConnectionRequestData *data)
{
const auto locker = qt_scoped_lock(mutex);
const QString &name = *data->name;
QDBusConnectionPrivate *&d = data->result;
// check if the connection exists by name
d = connection(name);
if (d || name.isEmpty())
return;
d = new QDBusConnectionPrivate;
DBusConnection *c = nullptr;
QDBusErrorInternal error;
switch (data->type) {
case ConnectionRequestData::ConnectToStandardBus:
switch (data->busType) {
case QDBusConnection::SystemBus:
c = q_dbus_bus_get_private(DBUS_BUS_SYSTEM, error);
break;
case QDBusConnection::SessionBus:
c = q_dbus_bus_get_private(DBUS_BUS_SESSION, error);
break;
case QDBusConnection::ActivationBus:
c = q_dbus_bus_get_private(DBUS_BUS_STARTER, error);
break;
}
break;
case ConnectionRequestData::ConnectToBusByAddress:
case ConnectionRequestData::ConnectToPeerByAddress:
c = q_dbus_connection_open_private(data->busAddress->toUtf8().constData(), error);
if (c && data->type == ConnectionRequestData::ConnectToBusByAddress) {
// register on the bus
if (!q_dbus_bus_register(c, error)) {
q_dbus_connection_unref(c);
c = nullptr;
}
}
break;
}
setConnection(name, d);
if (data->type == ConnectionRequestData::ConnectToPeerByAddress) {
d->setPeer(c, error);
} else {
// create the bus service
// will lock in QDBusConnectionPrivate::connectRelay()
d->setConnection(c, error);
d->createBusService();
if (c && data->suspendedDelivery)
d->setDispatchEnabled(false);
}
}
void QDBusConnectionManager::createServer(const QString &address, void *server)
{
QDBusErrorInternal error;
QDBusConnectionPrivate *d = new QDBusConnectionPrivate;
d->setServer(static_cast<QDBusServer *>(server),
q_dbus_server_listen(address.toUtf8().constData(), error), error);
}
/*!
\class QDBusConnection
\inmodule QtDBus
\since 4.2
\brief The QDBusConnection class represents a connection to the D-Bus bus daemon.
This class is the initial point in a D-Bus session. Using it, you
can get access to remote objects, interfaces; connect remote
signals to your object's slots; register objects, etc.
D-Bus connections are created using the connectToBus() function,
which opens a connection to the server daemon and does the initial
handshaking, associating that connection with a name. Further
attempts to connect using the same name will return the same
connection.
The connection is then torn down using the disconnectFromBus()
function.
Once disconnected, calling connectToBus() will not reestablish a
connection, you must create a new QDBusConnection instance.
As a convenience for the two most common connection types, the
sessionBus() and systemBus() functions return open connections to
the session server daemon and the system server daemon,
respectively. Those connections are opened when first used and are
closed when the QCoreApplication destructor is run.
D-Bus also supports peer-to-peer connections, without the need for
a bus server daemon. Using this facility, two applications can
talk to each other and exchange messages. This can be achieved by
passing an address to connectToBus() function, which was opened by
another D-Bus application using QDBusServer.
*/
/*!
\enum QDBusConnection::BusType
Specifies the type of the bus connection. The valid bus types are:
\value SessionBus the session bus, associated with the running desktop session
\value SystemBus the system bus, used to communicate with system-wide processes
\value ActivationBus the activation bus, the "alias" for the bus that started the
service
On the Session Bus, one can find other applications by the same user that are sharing the same
desktop session (hence the name). On the System Bus, however, processes shared for the whole
system are usually found.
*/
/*!
\enum QDBusConnection::RegisterOption
Specifies the options for registering objects with the connection. The possible values are:
\value ExportAdaptors export the contents of adaptors found in this object
\value ExportScriptableSlots export this object's scriptable slots
\value ExportScriptableSignals export this object's scriptable signals
\value ExportScriptableProperties export this object's scriptable properties
\value ExportScriptableInvokables export this object's scriptable invokables
\value ExportScriptableContents shorthand form for ExportScriptableSlots |
ExportScriptableSignals |
ExportScriptableProperties
\value ExportNonScriptableSlots export this object's non-scriptable slots
\value ExportNonScriptableSignals export this object's non-scriptable signals
\value ExportNonScriptableProperties export this object's non-scriptable properties
\value ExportNonScriptableInvokables export this object's non-scriptable invokables
\value ExportNonScriptableContents shorthand form for ExportNonScriptableSlots |
ExportNonScriptableSignals |
ExportNonScriptableProperties
\value ExportAllSlots export all of this object's slots
\value ExportAllSignals export all of this object's signals
\value ExportAllProperties export all of this object's properties
\value ExportAllInvokables export all of this object's invokables
\value ExportAllContents export all of this object's contents
\value ExportChildObjects export this object's child objects
\sa registerObject(), QDBusAbstractAdaptor, {usingadaptors.html}{Using adaptors}
*/
/*!
\internal
\since 4.8
\enum QDBusConnection::VirtualObjectRegisterOption
Specifies the options for registering virtual objects with the connection. The possible values are:
\value SingleNode register a virtual object to handle one path only
\value SubPath register a virtual object so that it handles all sub paths
\sa registerVirtualObject(), QDBusVirtualObject
*/
/*!
\enum QDBusConnection::UnregisterMode
The mode for unregistering an object path:
\value UnregisterNode unregister this node only: do not unregister child objects
\value UnregisterTree unregister this node and all its sub-tree
Note, however, if this object was registered with the ExportChildObjects option, UnregisterNode
will unregister the child objects too.
*/
/*!
\since 4.8
\enum QDBusConnection::ConnectionCapability
This enum describes the available capabilities for a D-Bus connection.
\value UnixFileDescriptorPassing enables passing of Unix file descriptors to other processes
(see QDBusUnixFileDescriptor)
\sa connectionCapabilities()
*/
/*!
Creates a QDBusConnection object attached to the connection with name \a name.
This does not open the connection. You have to call connectToBus() to open it.
*/
QDBusConnection::QDBusConnection(const QString &name)
{
if (name.isEmpty() || _q_manager.isDestroyed()) {
d = nullptr;
} else {
const auto locker = qt_scoped_lock(_q_manager()->mutex);
d = _q_manager()->connection(name);
if (d)
d->ref.ref();
}
}
/*!
Creates a copy of the \a other connection.
*/
QDBusConnection::QDBusConnection(const QDBusConnection &other)
{
d = other.d;
if (d)
d->ref.ref();
}
/*!
\internal
Creates a connection object with the given \a dd as private object.
*/
QDBusConnection::QDBusConnection(QDBusConnectionPrivate *dd)
{
d = dd;
if (d)
d->ref.ref();
}
/*!
Disposes of this object. This does not close the connection: you
have to call disconnectFromBus() to do that.
*/
QDBusConnection::~QDBusConnection()
{
if (d && !d->ref.deref())
d->deleteLater();
}
/*!
Creates a copy of the connection \a other in this object. Note
that the connection this object referenced before the copy, is not
spontaneously disconnected.
\sa disconnectFromBus()
*/
QDBusConnection &QDBusConnection::operator=(const QDBusConnection &other)
{
if (other.d)
other.d->ref.ref();
if (d && !d->ref.deref())
d->deleteLater();
d = other.d;
return *this;
}
/*!
Opens a connection of type \a type to one of the known busses and
associate with it the connection name \a name. Returns a
QDBusConnection object associated with that connection.
*/
QDBusConnection QDBusConnection::connectToBus(BusType type, const QString &name)
{
if (_q_manager.isDestroyed() || !qdbus_loadLibDBus()) {
QDBusConnectionPrivate *d = nullptr;
return QDBusConnection(d);
}
return QDBusConnection(_q_manager()->connectToBus(type, name, false));
}
/*!
Opens a connection to a private bus on address \a address and associate with it the
connection name \a name. Returns a QDBusConnection object associated with that connection.
*/
QDBusConnection QDBusConnection::connectToBus(const QString &address,
const QString &name)
{
if (_q_manager.isDestroyed() || !qdbus_loadLibDBus()) {
QDBusConnectionPrivate *d = nullptr;
return QDBusConnection(d);
}
return QDBusConnection(_q_manager()->connectToBus(address, name));
}
/*!
\since 4.8
Opens a peer-to-peer connection on address \a address and associate with it the
connection name \a name. Returns a QDBusConnection object associated with that connection.
*/
QDBusConnection QDBusConnection::connectToPeer(const QString &address,
const QString &name)
{
if (_q_manager.isDestroyed() || !qdbus_loadLibDBus()) {
QDBusConnectionPrivate *d = nullptr;
return QDBusConnection(d);
}
return QDBusConnection(_q_manager()->connectToPeer(address, name));
}
/*!
Closes the bus connection of name \a name.
Note that if there are still QDBusConnection objects associated
with the same connection, the connection will not be closed until
all references are dropped. However, no further references can be
created using the QDBusConnection constructor.
*/
void QDBusConnection::disconnectFromBus(const QString &name)
{
if (_q_manager()) {
const auto locker = qt_scoped_lock(_q_manager()->mutex);
QDBusConnectionPrivate *d = _q_manager()->connection(name);
if (d && d->mode != QDBusConnectionPrivate::ClientMode)
return;
_q_manager()->removeConnection(name);
}
}
/*!
\since 4.8
Closes the peer connection of name \a name.
Note that if there are still QDBusConnection objects associated
with the same connection, the connection will not be closed until
all references are dropped. However, no further references can be
created using the QDBusConnection constructor.
*/
void QDBusConnection::disconnectFromPeer(const QString &name)
{
if (_q_manager()) {
const auto locker = qt_scoped_lock(_q_manager()->mutex);
QDBusConnectionPrivate *d = _q_manager()->connection(name);
if (d && d->mode != QDBusConnectionPrivate::PeerMode)
return;
_q_manager()->removeConnection(name);
}
}
/*!
Sends the \a message over this connection, without waiting for a
reply. This is suitable for errors, signals, and return values as
well as calls whose return values are not necessary.
Returns \c true if the message was queued successfully, false otherwise.
*/
bool QDBusConnection::send(const QDBusMessage &message) const
{
if (!d || !d->connection) {
QDBusError err = QDBusError(QDBusError::Disconnected,
QDBusUtil::disconnectedErrorMessage());
if (d)
d->lastError = err;
return false;
}
return d->send(message);
}
/*!
Sends the \a message over this connection and returns immediately.
When the reply is received, the method \a returnMethod is called in
the \a receiver object. If an error occurs, the method \a errorMethod
will be called instead.
If no reply is received within \a timeout milliseconds, an automatic
error will be delivered indicating the expiration of the call.
The default \a timeout is -1, which will be replaced with an
implementation-defined value that is suitable for inter-process
communications (generally, 25 seconds).
This function is suitable for method calls only. It is guaranteed
that the slot will be called exactly once with the reply, as long
as the parameter types match and no error occurs.
Returns \c true if the message was sent, or false if the message could
not be sent.
*/
bool QDBusConnection::callWithCallback(const QDBusMessage &message, QObject *receiver,
const char *returnMethod, const char *errorMethod,
int timeout) const
{
if (!d || !d->connection) {
QDBusError err = QDBusError(QDBusError::Disconnected,
QDBusUtil::disconnectedErrorMessage());
if (d)
d->lastError = err;
return false;
}
return d->sendWithReplyAsync(message, receiver, returnMethod, errorMethod, timeout) != nullptr;
}
/*!
\overload
\deprecated
Sends the \a message over this connection and returns immediately.
When the reply is received, the method \a returnMethod is called in
the \a receiver object.
This function is suitable for method calls only. It is guaranteed
that the slot will be called exactly once with the reply, as long
as the parameter types match and no error occurs.
This function is dangerous because it cannot report errors, including
the expiration of the timeout.
Returns \c true if the message was sent, or false if the message could
not be sent.
*/
bool QDBusConnection::callWithCallback(const QDBusMessage &message, QObject *receiver,
const char *returnMethod, int timeout) const
{
return callWithCallback(message, receiver, returnMethod, nullptr, timeout);
}
/*!
Sends the \a message over this connection and blocks, waiting for
a reply, for at most \a timeout milliseconds. This function is
suitable for method calls only. It returns the reply message as
its return value, which will be either of type
QDBusMessage::ReplyMessage or QDBusMessage::ErrorMessage.
If no reply is received within \a timeout milliseconds, an automatic
error will be delivered indicating the expiration of the call.
The default \a timeout is -1, which will be replaced with an
implementation-defined value that is suitable for inter-process
communications (generally, 25 seconds).
See the QDBusInterface::call() function for a more friendly way
of placing calls.
\warning If \a mode is QDBus::BlockWithGui, this function will
reenter the Qt event loop in order to wait for the
reply. During the wait, it may deliver signals and other
method calls to your application. Therefore, it must be
prepared to handle a reentrancy whenever a call is
placed with call().
*/
QDBusMessage QDBusConnection::call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const
{
if (!d || !d->connection) {
QDBusError err = QDBusError(QDBusError::Disconnected,
QDBusUtil::disconnectedErrorMessage());
if (d)
d->lastError = err;
return QDBusMessage::createError(err);
}
if (mode != QDBus::NoBlock)
return d->sendWithReply(message, mode, timeout);
d->send(message);
QDBusMessage retval;
retval << QVariant(); // add one argument (to avoid .at(0) problems)
return retval;
}
/*!
\since 4.5
Sends the \a message over this connection and returns
immediately. This function is suitable for method calls only. It
returns an object of type QDBusPendingCall which can be used to
track the status of the reply.
If no reply is received within \a timeout milliseconds, an automatic
error will be delivered indicating the expiration of the call. The
default \a timeout is -1, which will be replaced with an
implementation-defined value that is suitable for inter-process
communications (generally, 25 seconds). This timeout is also the
upper limit for waiting in QDBusPendingCall::waitForFinished().
See the QDBusInterface::asyncCall() function for a more friendly way
of placing calls.
*/
QDBusPendingCall QDBusConnection::asyncCall(const QDBusMessage &message, int timeout) const
{
if (!d || !d->connection) {
return QDBusPendingCall(nullptr); // null pointer -> disconnected
}
QDBusPendingCallPrivate *priv = d->sendWithReplyAsync(message, nullptr, nullptr, nullptr, timeout);
return QDBusPendingCall(priv);
}
/*!
Connects the signal specified by the \a service, \a path, \a interface and \a name parameters to
the slot \a slot in object \a receiver. The arguments \a service and \a path can be empty,
denoting a connection to any signal of the (\a interface, \a name) pair, from any remote
application.
Returns \c true if the connection was successful.
\warning The signal will only be delivered to the slot if the parameters match. This verification
can be done only when the signal is received, not at connection time.
*/
bool QDBusConnection::connect(const QString &service, const QString &path, const QString& interface,
const QString &name, QObject *receiver, const char *slot)
{
return connect(service, path, interface, name, QStringList(), QString(), receiver, slot);
}
/*!
\overload
Connects the signal to the slot \a slot in object \a
receiver. Unlike the previous connect() overload, this function
allows one to specify the parameter signature to be connected
using the \a signature variable. The function will then verify
that this signature can be delivered to the slot specified by \a
slot and return false otherwise.
Returns \c true if the connection was successful.
\note This function verifies that the signal signature matches the
slot's parameters, but it does not verify that the actual
signal exists with the given signature in the remote
service.
*/
bool QDBusConnection::connect(const QString &service, const QString &path, const QString& interface,
const QString &name, const QString &signature,
QObject *receiver, const char *slot)
{
return connect(service, path, interface, name, QStringList(), signature, receiver, slot);
}
/*!
\overload
\since 4.6
Connects the signal to the slot \a slot in object \a
receiver. Unlike the previous connect() overload, this function
allows one to specify the parameter signature to be connected
using the \a signature variable. The function will then verify
that this signature can be delivered to the slot specified by \a
slot and return false otherwise.
The \a argumentMatch parameter lists the string parameters to be matched,
in sequential order. Note that, to match an empty string, you need to
pass a QString that is empty but not null (i.e., QString("")). A null
QString skips matching at that position.
Returns \c true if the connection was successful.
\note This function verifies that the signal signature matches the
slot's parameters, but it does not verify that the actual
signal exists with the given signature in the remote
service.
*/
bool QDBusConnection::connect(const QString &service, const QString &path, const QString& interface,
const QString &name, const QStringList &argumentMatch, const QString &signature,
QObject *receiver, const char *slot)
{
if (!receiver || !slot || !d || !d->connection)
return false;
if (interface.isEmpty() && name.isEmpty())
return false;
if (!interface.isEmpty() && !QDBusUtil::isValidInterfaceName(interface)) {
#ifndef QT_NO_DEBUG
qWarning("QDBusConnection::connect: interface name '%s' is not valid", interface.toLatin1().constData());
#endif
return false;
}
if (!service.isEmpty() && !QDBusUtil::isValidBusName(service)) {
#ifndef QT_NO_DEBUG
qWarning("QDBusConnection::connect: service name '%s' is not valid", service.toLatin1().constData());
#endif
return false;
}
if (!path.isEmpty() && !QDBusUtil::isValidObjectPath(path)) {
#ifndef QT_NO_DEBUG
qWarning("QDBusConnection::connect: object path '%s' is not valid", path.toLatin1().constData());
#endif
return false;
}
return d->connectSignal(service, path, interface, name, argumentMatch, signature, receiver, slot);
}
/*!
Disconnects the signal specified by the \a service, \a path, \a interface
and \a name parameters from the slot \a slot in object \a receiver. The
arguments must be the same as passed to the connect() function.
Returns \c true if the disconnection was successful.
*/
bool QDBusConnection::disconnect(const QString &service, const QString &path, const QString &interface,
const QString &name, QObject *receiver, const char *slot)
{
return disconnect(service, path, interface, name, QStringList(), QString(), receiver, slot);
}
/*!
\overload
Disconnects the signal specified by the \a service, \a path, \a
interface, \a name, and \a signature parameters from the slot \a slot in
object \a receiver. The arguments must be the same as passed to the
connect() function.
Returns \c true if the disconnection was successful.
*/
bool QDBusConnection::disconnect(const QString &service, const QString &path, const QString& interface,
const QString &name, const QString &signature,
QObject *receiver, const char *slot)
{
return disconnect(service, path, interface, name, QStringList(), signature, receiver, slot);
}
/*!
\overload
\since 4.6
Disconnects the signal specified by the \a service, \a path, \a
interface, \a name, \a argumentMatch, and \a signature parameters from
the slot \a slot in object \a receiver. The arguments must be the same as
passed to the connect() function.
Returns \c true if the disconnection was successful.
*/
bool QDBusConnection::disconnect(const QString &service, const QString &path, const QString& interface,
const QString &name, const QStringList &argumentMatch, const QString &signature,
QObject *receiver, const char *slot)
{
if (!receiver || !slot || !d || !d->connection)
return false;
if (!interface.isEmpty() && !QDBusUtil::isValidInterfaceName(interface))
return false;
if (interface.isEmpty() && name.isEmpty())
return false;
return d->disconnectSignal(service, path, interface, name, argumentMatch, signature, receiver, slot);
}
/*!
Registers the object \a object at path \a path and returns \c true if
the registration was successful. The \a options parameter
specifies how much of the object \a object will be exposed through
D-Bus.
This function does not replace existing objects: if there is already an object registered at
path \a path, this function will return false. Use unregisterObject() to unregister it first.
The ExportChildObjects flag exports child objects on D-Bus based on the
path of the registered objects and the QObject::objectName of the child.
Therefore, it is important for the child object to have an object name.
You cannot register an object as a child object of an object that
was registered with ExportChildObjects.
*/
bool QDBusConnection::registerObject(const QString &path, QObject *object, RegisterOptions options)
{
return registerObject(path, QString(), object, options);
}
/*!
\overload
\since 5.5
Registers the object \a object at path \a path with interface name \a interface
and returns \c true if the registration was successful. The \a options parameter
specifies how much of the object \a object will be exposed through
D-Bus.
This function does not replace existing objects: if there is already an object registered at
path \a path, this function will return false. Use unregisterObject() to unregister it first.
The ExportChildObjects flag exports child objects on D-Bus based on the
path of the registered objects and the QObject::objectName of the child.
Therefore, it is important for the child object to have an object name.
You cannot register an object as a child object of an object that
was registered with ExportChildObjects.
*/
bool QDBusConnection::registerObject(const QString &path, const QString &interface, QObject *object, RegisterOptions options)
{
Q_ASSERT_X(QDBusUtil::isValidObjectPath(path), "QDBusConnection::registerObject",
"Invalid object path given");
if (!d || !d->connection || !object || !options || !QDBusUtil::isValidObjectPath(path))
return false;
auto pathComponents = QStringView{path}.split(u'/');
if (pathComponents.constLast().isEmpty())
pathComponents.removeLast();
QDBusWriteLocker locker(RegisterObjectAction, d);
// lower-bound search for where this object should enter in the tree
QDBusConnectionPrivate::ObjectTreeNode *node = &d->rootNode;
int i = 1;
while (node) {
if (pathComponents.count() == i) {
// this node exists
// consider it free if there's no object here and the user is not trying to
// replace the object sub-tree
if (node->obj)
return false;
if (options & QDBusConnectionPrivate::VirtualObject) {
if (options & SubPath && !node->children.isEmpty())
return false;
} else {
if ((options & ExportChildObjects && !node->children.isEmpty()))
return false;
}
// we can add the object here
node->obj = object;
node->flags = options;
node->interfaceName = interface;
d->registerObject(node);
//qDebug("REGISTERED FOR %s", path.toLocal8Bit().constData());
return true;
}
// if a virtual object occupies this path, return false
if (node->obj && (node->flags & QDBusConnectionPrivate::VirtualObject) && (node->flags & QDBusConnection::SubPath)) {
//qDebug("Cannot register object at %s because QDBusVirtualObject handles all sub-paths.",
// qPrintable(path));
return false;
}
// find the position where we'd insert the node
QDBusConnectionPrivate::ObjectTreeNode::DataList::Iterator it =
std::lower_bound(node->children.begin(), node->children.end(), pathComponents.at(i));
if (it != node->children.end() && it->name == pathComponents.at(i)) {
// match: this node exists
node = &(*it);
// are we allowed to go deeper?
if (node->flags & ExportChildObjects) {
// we're not
//qDebug("Cannot register object at %s because %s exports its own child objects",
// qPrintable(path), qPrintable(pathComponents.at(i)));
return false;
}
} else {
// add entry
it = node->children.insert(it, pathComponents.at(i).toString());
node = &(*it);
}
// iterate
++i;
}
Q_ASSERT_X(false, "QDBusConnection::registerObject", "The impossible happened");
return false;
}
/*!
\internal
\since 4.8
Registers a QDBusTreeNode for a path. It can handle a path including all child paths, thus
handling multiple DBus nodes.
To unregister a QDBusTreeNode use the unregisterObject() function with its path.
*/
bool QDBusConnection::registerVirtualObject(const QString &path, QDBusVirtualObject *treeNode,
VirtualObjectRegisterOption options)
{
int opts = options | QDBusConnectionPrivate::VirtualObject;
return registerObject(path, (QObject*) treeNode, (RegisterOptions) opts);
}
/*!
Unregisters an object that was registered with the registerObject() at the object path given by
\a path and, if \a mode is QDBusConnection::UnregisterTree, all of its sub-objects too.
Note that you cannot unregister objects that were not registered with registerObject().
*/
void QDBusConnection::unregisterObject(const QString &path, UnregisterMode mode)
{
if (!d || !d->connection || !QDBusUtil::isValidObjectPath(path))
return;
QDBusWriteLocker locker(UnregisterObjectAction, d);
d->unregisterObject(path, mode);
}
/*!
Return the object that was registered with the registerObject() at the object path given by
\a path.
*/
QObject *QDBusConnection::objectRegisteredAt(const QString &path) const
{
Q_ASSERT_X(QDBusUtil::isValidObjectPath(path), "QDBusConnection::registeredObject",
"Invalid object path given");
if (!d || !d->connection || !QDBusUtil::isValidObjectPath(path))
return nullptr;