1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QDBUSCONNECTION_H
6#define QDBUSCONNECTION_H
7
8#include <QtDBus/qtdbusglobal.h>
9#include <QtCore/qobjectdefs.h>
10#include <QtCore/qstring.h>
11
12#ifndef QT_NO_DBUS
13
14#ifdef interface
15# undef interface
16#endif
17
18QT_BEGIN_NAMESPACE
19
20
21namespace QDBus
22{
23 enum CallMode {
24 NoBlock,
25 Block,
26 BlockWithGui,
27 AutoDetect
28 };
29}
30
31class QDBusAbstractInterfacePrivate;
32class QDBusInterface;
33class QDBusError;
34class QDBusMessage;
35class QDBusPendingCall;
36class QDBusConnectionInterface;
37class QDBusVirtualObject;
38class QObject;
39
40class QDBusConnectionPrivate;
41class Q_DBUS_EXPORT QDBusConnection
42{
43 Q_GADGET
44 Q_MOC_INCLUDE(<QtDBus/qdbuspendingcall.h>)
45
46public:
47 enum BusType { SessionBus, SystemBus, ActivationBus };
48 Q_ENUM(BusType)
49 enum RegisterOption {
50 ExportAdaptors = 0x01,
51
52 ExportScriptableSlots = 0x10,
53 ExportScriptableSignals = 0x20,
54 ExportScriptableProperties = 0x40,
55 ExportScriptableInvokables = 0x80,
56 ExportScriptableContents = 0xf0,
57
58 ExportNonScriptableSlots = 0x100,
59 ExportNonScriptableSignals = 0x200,
60 ExportNonScriptableProperties = 0x400,
61 ExportNonScriptableInvokables = 0x800,
62 ExportNonScriptableContents = 0xf00,
63
64 ExportAllSlots = ExportScriptableSlots|ExportNonScriptableSlots,
65 ExportAllSignals = ExportScriptableSignals|ExportNonScriptableSignals,
66 ExportAllProperties = ExportScriptableProperties|ExportNonScriptableProperties,
67 ExportAllInvokables = ExportScriptableInvokables|ExportNonScriptableInvokables,
68 ExportAllContents = ExportScriptableContents|ExportNonScriptableContents,
69
70#ifndef Q_QDOC
71 // Qt 4.2 had a misspelling here
72 ExportAllSignal = ExportAllSignals,
73#endif
74 ExportChildObjects = 0x1000
75 // Reserved = 0xff000000
76 };
77 Q_DECLARE_FLAGS(RegisterOptions, RegisterOption)
78 Q_FLAG(RegisterOptions)
79
80 enum UnregisterMode {
81 UnregisterNode,
82 UnregisterTree
83 };
84 Q_ENUM(UnregisterMode)
85
86 enum VirtualObjectRegisterOption {
87 SingleNode = 0x0,
88 SubPath = 0x1
89 // Reserved = 0xff000000
90 };
91 Q_DECLARE_FLAGS(VirtualObjectRegisterOptions, VirtualObjectRegisterOption)
92
93 enum ConnectionCapability {
94 UnixFileDescriptorPassing = 0x0001
95 };
96 Q_DECLARE_FLAGS(ConnectionCapabilities, ConnectionCapability)
97
98 explicit QDBusConnection(const QString &name);
99 QDBusConnection(const QDBusConnection &other);
100 QDBusConnection(QDBusConnection &&other) noexcept : d(other.d) { other.d = nullptr; }
101 QDBusConnection &operator=(QDBusConnection &&other) noexcept { swap(other); return *this; }
102 QDBusConnection &operator=(const QDBusConnection &other);
103 ~QDBusConnection();
104
105 void swap(QDBusConnection &other) noexcept { qt_ptr_swap(lhs&: d, rhs&: other.d); }
106
107 bool isConnected() const;
108 QString baseService() const;
109 QDBusError lastError() const;
110 QString name() const;
111 ConnectionCapabilities connectionCapabilities() const;
112
113 bool send(const QDBusMessage &message) const;
114 bool callWithCallback(const QDBusMessage &message, QObject *receiver,
115 const char *returnMethod, const char *errorMethod,
116 int timeout = -1) const;
117 bool callWithCallback(const QDBusMessage &message, QObject *receiver,
118 const char *slot, int timeout = -1) const;
119 QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode = QDBus::Block,
120 int timeout = -1) const;
121 QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout = -1) const;
122
123 bool connect(const QString &service, const QString &path, const QString &interface,
124 const QString &name, QObject *receiver, const char *slot);
125 bool connect(const QString &service, const QString &path, const QString &interface,
126 const QString &name, const QString& signature,
127 QObject *receiver, const char *slot);
128 bool connect(const QString &service, const QString &path, const QString &interface,
129 const QString &name, const QStringList &argumentMatch, const QString& signature,
130 QObject *receiver, const char *slot);
131
132 bool disconnect(const QString &service, const QString &path, const QString &interface,
133 const QString &name, QObject *receiver, const char *slot);
134 bool disconnect(const QString &service, const QString &path, const QString &interface,
135 const QString &name, const QString& signature,
136 QObject *receiver, const char *slot);
137 bool disconnect(const QString &service, const QString &path, const QString &interface,
138 const QString &name, const QStringList &argumentMatch, const QString& signature,
139 QObject *receiver, const char *slot);
140
141 bool registerObject(const QString &path, QObject *object,
142 RegisterOptions options = ExportAdaptors);
143 bool registerObject(const QString &path, const QString &interface, QObject *object,
144 RegisterOptions options = ExportAdaptors);
145 void unregisterObject(const QString &path, UnregisterMode mode = UnregisterNode);
146 QObject *objectRegisteredAt(const QString &path) const;
147
148 bool registerVirtualObject(const QString &path, QDBusVirtualObject *object,
149 VirtualObjectRegisterOption options = SingleNode);
150
151 bool registerService(const QString &serviceName);
152 bool unregisterService(const QString &serviceName);
153
154 QDBusConnectionInterface *interface() const;
155
156 void *internalPointer() const;
157
158 static QDBusConnection connectToBus(BusType type, const QString &name);
159 static QDBusConnection connectToBus(const QString &address, const QString &name);
160 static QDBusConnection connectToPeer(const QString &address, const QString &name);
161 static void disconnectFromBus(const QString &name);
162 static void disconnectFromPeer(const QString &name);
163
164 static QByteArray localMachineId();
165
166 static QDBusConnection sessionBus();
167 static QDBusConnection systemBus();
168
169protected:
170 explicit QDBusConnection(QDBusConnectionPrivate *dd);
171
172private:
173 friend class QDBusConnectionPrivate;
174 QDBusConnectionPrivate *d;
175};
176Q_DECLARE_SHARED(QDBusConnection)
177
178Q_DECLARE_OPERATORS_FOR_FLAGS(QDBusConnection::RegisterOptions)
179Q_DECLARE_OPERATORS_FOR_FLAGS(QDBusConnection::VirtualObjectRegisterOptions)
180Q_DECLARE_OPERATORS_FOR_FLAGS(QDBusConnection::ConnectionCapabilities)
181
182QT_END_NAMESPACE
183
184#endif // QT_NO_DBUS
185#endif
186

source code of qtbase/src/dbus/qdbusconnection.h