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
|
/* qdbusconnection.cpp
*
* Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QtCore/qdebug.h>
#include <QtCore/qcoreapplication.h>
#include "qdbusconnection.h"
#include "qdbusconnection_p.h"
QT_STATIC_CONST_IMPL char *QDBusConnection::default_connection_name = "qt_dbus_default_connection";
class QDBusConnectionManager
{
public:
QDBusConnectionManager(): default_connection(0) {}
~QDBusConnectionManager();
void bindToApplication();
QDBusConnectionPrivate *connection(const QString &name) const;
void removeConnection(const QString &name);
void setConnection(const QString &name, QDBusConnectionPrivate *c);
private:
QDBusConnectionPrivate *default_connection;
QHash<QString, QDBusConnectionPrivate *> connectionHash;
};
Q_GLOBAL_STATIC(QDBusConnectionManager, manager);
QDBusConnectionPrivate *QDBusConnectionManager::connection(const QString &name) const
{
return name == QLatin1String(QDBusConnection::default_connection_name) ?
default_connection : connectionHash.value(name, 0);
}
void QDBusConnectionManager::removeConnection(const QString &name)
{
QDBusConnectionPrivate *d = 0;
if (name == QLatin1String(QDBusConnection::default_connection_name)) {
d = default_connection;
default_connection = 0;
} else {
d = connectionHash.take(name);
}
if (!d->ref.deref())
delete d;
}
QDBusConnectionManager::~QDBusConnectionManager()
{
if (default_connection) {
delete default_connection;
default_connection = 0;
}
for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
it != connectionHash.constEnd(); ++it) {
delete it.value();
}
connectionHash.clear();
}
void QDBusConnectionManager::bindToApplication()
{
if (default_connection) {
default_connection->bindToApplication();
}
for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
it != connectionHash.constEnd(); ++it) {
(*it)->bindToApplication();
}
}
void qDBusBindToApplication()
{
manager()->bindToApplication();
}
void QDBusConnectionManager::setConnection(const QString &name, QDBusConnectionPrivate *c)
{
if (name == QLatin1String(QDBusConnection::default_connection_name))
default_connection = c;
else
connectionHash[name] = c;
}
QDBusConnection::QDBusConnection(const QString &name)
{
d = manager()->connection(name);
if (d)
d->ref.ref();
}
QDBusConnection::QDBusConnection(const QDBusConnection &other)
{
d = other.d;
if (d)
d->ref.ref();
}
QDBusConnection::~QDBusConnection()
{
if (d && !d->ref.deref())
delete d;
}
QDBusConnection &QDBusConnection::operator=(const QDBusConnection &other)
{
if (other.d)
other.d->ref.ref();
QDBusConnectionPrivate *old = static_cast<QDBusConnectionPrivate *>(
q_atomic_set_ptr(&d, other.d));
if (old && !old->ref.deref())
delete old;
return *this;
}
QDBusConnection QDBusConnection::addConnection(BusType type, const QString &name)
{
// Q_ASSERT_X(QCoreApplication::instance(), "QDBusConnection::addConnection",
// "Cannot create connection without a Q[Core]Application instance");
QDBusConnectionPrivate *d = manager()->connection(name);
if (d)
return QDBusConnection(name);
d = new QDBusConnectionPrivate;
DBusConnection *c = 0;
switch (type) {
case SystemBus:
c = dbus_bus_get(DBUS_BUS_SYSTEM, &d->error);
break;
case SessionBus:
c = dbus_bus_get(DBUS_BUS_SESSION, &d->error);
break;
case ActivationBus:
c = dbus_bus_get(DBUS_BUS_STARTER, &d->error);
break;
}
d->setConnection(c); //setConnection does the error handling for us
manager()->setConnection(name, d);
return QDBusConnection(name);
}
QDBusConnection QDBusConnection::addConnection(const QString &address,
const QString &name)
{
// Q_ASSERT_X(QCoreApplication::instance(), "QDBusConnection::addConnection",
// "Cannot create connection without a Q[Core]Application instance");
QDBusConnectionPrivate *d = manager()->connection(name);
if (d)
return QDBusConnection(name);
d = new QDBusConnectionPrivate;
// setConnection does the error handling for us
d->setConnection(dbus_connection_open(address.toUtf8().constData(), &d->error));
manager()->setConnection(name, d);
return QDBusConnection(name);
}
void QDBusConnection::closeConnection(const QString &name)
{
manager()->removeConnection(name);
}
void QDBusConnectionPrivate::timerEvent(QTimerEvent *e)
{
DBusTimeout *timeout = timeouts.value(e->timerId(), 0);
dbus_timeout_handle(timeout);
}
bool QDBusConnection::send(const QDBusMessage &message) const
{
if (!d || !d->connection)
return false;
DBusMessage *msg = message.toDBusMessage();
if (!msg)
return false;
bool isOk = dbus_connection_send(d->connection, msg, 0);
dbus_message_unref(msg);
return isOk;
}
int QDBusConnection::sendWithReplyAsync(const QDBusMessage &message, QObject *receiver,
const char *method) const
{
if (!d || !d->connection)
return 0;
return d->sendWithReplyAsync(message, receiver, method);
}
QDBusMessage QDBusConnection::sendWithReply(const QDBusMessage &message) const
{
if (!d || !d->connection)
return QDBusMessage::fromDBusMessage(0);
DBusMessage *msg = message.toDBusMessage();
if (!msg)
return QDBusMessage::fromDBusMessage(0);
DBusMessage *reply = dbus_connection_send_with_reply_and_block(d->connection, msg,
-1, &d->error);
d->handleError();
dbus_message_unref(msg);
return QDBusMessage::fromDBusMessage(reply);
}
bool QDBusConnection::connect(const QString &path, const QString &interface,
const QString &name, QObject *receiver, const char *slot)
{
if (!receiver || !slot || !d || !d->connection)
return false;
QDBusConnectionPrivate::SignalHook hook;
hook.interface = interface;
hook.name = name;
hook.obj = QPointer<QObject>(receiver);
if (!hook.setSlot(slot + 1))
return false;
d->signalHooks.insertMulti(path, hook);
d->connect(receiver, SIGNAL(destroyed(QObject*)), SLOT(objectDestroyed(QObject*)));
return true;
}
bool QDBusConnection::registerObject(const QString &path, const QString &interface,
QObject *object)
{
if (!d || !d->connection || !object || path.isEmpty() || interface.isEmpty())
return false;
QDBusConnectionPrivate::ObjectHook hook;
hook.interface = interface;
hook.obj = object;
QDBusConnectionPrivate::ObjectHookHash::iterator it = d->objectHooks.find(path);
while (it != d->objectHooks.end() && it.key() == path) {
if (it.value().interface == interface) {
d->objectHooks.erase(it);
break;
}
++it;
}
d->objectHooks.insert(path, hook);
d->connect(object, SIGNAL(destroyed(QObject*)), SLOT(objectDestroyed(QObject*)));
qDebug("REGISTERED FOR %s", path.toLocal8Bit().constData());
return true; // todo - check for slots etc.
}
void QDBusConnection::unregisterObject(const QString &path)
{
if (!d || !d->connection)
return;
// TODO - check interfaces
d->objectHooks.remove(path);
}
bool QDBusConnection::isConnected( ) const
{
return d && d->connection && dbus_connection_get_is_connected(d->connection);
}
QDBusError QDBusConnection::lastError() const
{
return d ? d->lastError : QDBusError();
}
QString QDBusConnection::baseService() const
{
return d && d->connection ?
QString::fromUtf8(dbus_bus_get_unique_name(d->connection))
: QString();
}
bool QDBusConnection::requestName(const QString &name, NameRequestMode mode)
{
//FIXME: DBUS_NAME_FLAGS_* are bit fields not enumeration
static const int DBusModes[] = { 0, DBUS_NAME_FLAG_ALLOW_REPLACEMENT,
DBUS_NAME_FLAG_REPLACE_EXISTING };
Q_ASSERT(mode == 0 || mode == AllowReplace ||
mode == ReplaceExisting );
DBusError error;
dbus_error_init (&error);
dbus_bus_request_name(d->connection, name.toUtf8(), DBusModes[mode], &error);
if (dbus_error_is_set (&error)) {
qDebug("Error %s\n", error.message);
dbus_error_free (&error);
return false;
}
return true;
}
#include "qdbusconnection.moc"
|