DBus-1-TQt 1.0
tqdbusconnection.cpp
Go to the documentation of this file.
1/* qdbusconnection.cpp
2 *
3 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
4 * Copyright (C) 2005-2007 Kevin Krammer <kevin.krammer@gmx.at>
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA.
22 *
23 */
24
25#include <tqapplication.h>
26
27#include "tqdbusconnection.h"
28#include "tqdbuserror.h"
29#include "tqdbusmessage.h"
30#include "tqdbusconnection_p.h"
31
32#include "tqdbusmessage_p.h"
33
34const char *TQT_DBusConnection::default_connection_name = "qt_dbus_default_connection";
35
37{
38public:
41 void bindToApplication();
42 TQT_DBusConnectionPrivate *connection(const TQString &name) const;
43 void removeConnection(const TQString &name);
44 void setConnection(const TQString &name, TQT_DBusConnectionPrivate *c);
45
48 return managerInstance;
49 }
50
51private:
53 // FIXME-QT4 TQHash<TQString, TQT_DBusConnectionPrivate *> connectionHash;
54 typedef TQMap<TQString, TQT_DBusConnectionPrivate*> ConnectionHash;
56
58};
59
60// FIXME-QT4 TQ_GLOBAL_STATIC(TQT_DBusConnectionManager, manager);
64}
65
67{
68 if (name == TQString::fromLatin1(TQT_DBusConnection::default_connection_name))
69 return default_connection;
70
71 ConnectionHash::const_iterator it = connectionHash.find(name);
72
73 return (it != connectionHash.end() ? it.data() : 0);
74}
75
77{
79 if (name == TQString::fromLatin1(TQT_DBusConnection::default_connection_name)) {
82 } else {
83 ConnectionHash::iterator it = connectionHash.find(name);
84 if (it == connectionHash.end())
85 return;
86
87 d = it.data();
88 connectionHash.erase(it);
89 }
90 if (!d->ref.deref())
91 delete d;
92}
93
95{
97 delete default_connection;
99 }
100/* FIXME-QT4
101 for (TQHash<TQString, TQT_DBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
102 it != connectionHash.constEnd(); ++it) {
103 delete it.value();
104 }*/
105 for (ConnectionHash::const_iterator it = connectionHash.constBegin();
106 it != connectionHash.constEnd(); ++it)
107 {
108 delete it.data();
109 }
110 connectionHash.clear();
111}
112
114{
115 if (default_connection) {
117 }
118/* FIXME-QT4
119 for (TQHash<TQString, TQT_DBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
120 it != connectionHash.constEnd(); ++it) {
121 (*it)->bindToApplication();
122 }*/
123 for (ConnectionHash::const_iterator it = connectionHash.constBegin();
124 it != connectionHash.constEnd(); ++it)
125 {
126 it.data()->bindToApplication();
127 }
128}
129
131{
133}
134
136{
137 if (name == TQString::fromLatin1(TQT_DBusConnection::default_connection_name))
139 else
140 connectionHash[name] = c;
141}
142
143
145{
146}
147
149{
150 d = manager()->connection(name);
151 if (d)
152 d->ref.ref();
153}
154
156{
157 d = other.d;
158 if (d)
159 d->ref.ref();
160}
161
163{
164 if (d && !d->ref.deref())
165 delete d;
166}
167
169{
170 if (other.d)
171 other.d->ref.ref();
172/* FIXME-QT4
173 TQT_DBusConnectionPrivate *old = static_cast<TQT_DBusConnectionPrivate *>(
174 q_atomic_set_ptr(&d, other.d));*/
176 d = other.d;
177 if (old && !old->ref.deref())
178 delete old;
179
180 return *this;
181}
182
184{
186}
187
189{
191}
192
194{
195// Q_ASSERT_X(TQCoreApplication::instance(), "TQT_DBusConnection::addConnection",
196// "Cannot create connection without a Q[Core]Application instance");
197
199 if (d)
200 return TQT_DBusConnection(name);
201
203 DBusConnection *c = 0;
204 switch (type) {
205 case SystemBus:
206 c = dbus_bus_get(DBUS_BUS_SYSTEM, &d->error);
207 break;
208 case SessionBus:
209 c = dbus_bus_get(DBUS_BUS_SESSION, &d->error);
210 break;
211 case ActivationBus:
212 c = dbus_bus_get(DBUS_BUS_STARTER, &d->error);
213 break;
214 }
215 d->setConnection(c); //setConnection does the error handling for us
216
217 manager()->setConnection(name, d);
218
219 return TQT_DBusConnection(name);
220}
221
223 const TQString &name)
224{
225// Q_ASSERT_X(TQCoreApplication::instance(), "TQT_DBusConnection::addConnection",
226// "Cannot create connection without a Q[Core]Application instance");
227
229 if (d)
230 return TQT_DBusConnection(name);
231
233 // setConnection does the error handling for us
234 d->setConnection(dbus_connection_open(address.utf8().data(), &d->error));
235
236 manager()->setConnection(name, d);
237
238 return TQT_DBusConnection(name);
239}
240
241void TQT_DBusConnection::closeConnection(const TQString &name)
242{
243 manager()->removeConnection(name);
244}
245
247{
248 DBusTimeout *timeout = timeouts[e->timerId()];
249 dbus_timeout_handle(timeout);
250}
251
253{
254 if (!d || !d->connection)
255 return false;
256
257 DBusMessage *msg = message.toDBusMessage();
258 if (!msg)
259 return false;
260
261 bool isOk = dbus_connection_send(d->connection, msg, 0);
262 dbus_message_unref(msg);
263 return isOk;
264}
265
266int TQT_DBusConnection::sendWithAsyncReply(const TQT_DBusMessage &message, TQObject *receiver,
267 const char *method) const
268{
269 if (!d || !d->connection)
270 return 0;
271
272 return d->sendWithReplyAsync(message, receiver, method);
273}
274
276{
277 if (!d || !d->connection)
279
280 DBusMessage *msg = message.toDBusMessage();
281 if (!msg)
283
284 DBusMessage *reply = dbus_connection_send_with_reply_and_block(d->connection, msg, -1, &d->error);
285
286 if (d->handleError() && error)
287 *error = d->lastError;
288
289 dbus_message_unref(msg);
290
292 if (reply) {
293 dbus_message_unref(reply);
294 }
295
296 bool dbus_error_set = dbus_error_is_set(&d->error);
297 ret.d->error.setDBUSError(dbus_error_set);
298 if (error) error->setDBUSError(dbus_error_set);
299
300 return ret;
301}
302
304{
305 if (!d || !d->connection) return;
306
307 d->flush();
308}
309
311{
312 if (!d || !d->connection) return;
313
314 d->dispatch();
315}
316
318{
319 if (!d || !d->connection) return;
320
322}
323
324bool TQT_DBusConnection::connect(TQObject* object, const char* slot)
325{
326 if (!d || !d->connection || !object || !slot)
327 return false;
328
329 bool ok = object->connect(d, TQ_SIGNAL(dbusSignal(const TQT_DBusMessage&)), slot);
330
331 return ok;
332}
333
334bool TQT_DBusConnection::disconnect(TQObject* object, const char* slot)
335{
336 if (!d || !d->connection || !object || !slot)
337 return false;
338
339 bool ok = d->disconnect(object, slot);
340
341 return ok;
342}
343
345{
346 if (!d || !d->connection || !object || path.isEmpty())
347 return false;
348
349 TQT_DBusConnectionPrivate::ObjectMap::const_iterator it = d->registeredObjects.find(path);
350 if (it != d->registeredObjects.end())
351 return false;
352
353 d->registeredObjects.insert(path, object);
354
355 return true;
356}
357
358void TQT_DBusConnection::unregisterObject(const TQString &path)
359{
360 if (!d || !d->connection || path.isEmpty())
361 return;
362
363 TQT_DBusConnectionPrivate::ObjectMap::iterator it = d->registeredObjects.find(path);
364 if (it == d->registeredObjects.end())
365 return ;
366
367 d->registeredObjects.erase(it);
368}
369
371{
372 return d && d->connection && dbus_connection_get_is_connected(d->connection);
373}
374
376{
377 return d ? d->lastError : TQT_DBusError();
378}
379
381{
382 return d && d->connection ?
383 TQString::fromUtf8(dbus_bus_get_unique_name(d->connection))
384 : TQString();
385}
386
387bool TQT_DBusConnection::requestName(const TQString &name, int modeFlags)
388{
389 Q_ASSERT(modeFlags >= 0);
390
391 if (!d || !d->connection)
392 return false;
393
394 if (modeFlags < 0)
395 return false;
396
397 int dbusFlags = 0;
398 if (modeFlags & AllowReplace)
399 dbusFlags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
400 if (modeFlags & ReplaceExisting)
401 dbusFlags |= DBUS_NAME_FLAG_REPLACE_EXISTING;
402
403 dbus_bus_request_name(d->connection, name.utf8(), dbusFlags, &d->error);
404 bool res = !d->handleError();
405 res &= d->handleUnreadMessages();
406 return res;
407}
408
409#include "tqdbusconnection.moc"
bool deref()
Provides access to a specific D-Bus bus.
bool connect(TQObject *object, const char *slot)
Connects an object to receive D-Bus signals.
void flush() const
Flushes buffered outgoing message.
TQT_DBusMessage sendWithReply(const TQT_DBusMessage &message, TQT_DBusError *error=0) const
Sends a message over the bus and waits for the reply.
int sendWithAsyncReply(const TQT_DBusMessage &message, TQObject *receiver, const char *slot) const
Sends a message over the bus, specifying a receiver object for replies.
TQT_DBusConnectionPrivate * d
bool disconnect(TQObject *object, const char *slot)
Disconnects a given receiver from the D-Bus signal handling.
static const char * default_connection_name
TQString uniqueName() const
Returns the connection identifier assigned at connect.
static TQT_DBusConnection addConnection(BusType type, const TQString &name=default_connection_name)
Add a connection to a bus with a specific bus type.
static TQT_DBusConnection sessionBus()
Gets a connection to the session bus.
TQT_DBusConnection & operator=(const TQT_DBusConnection &other)
Creates a shallow copy of the given connection.
bool send(const TQT_DBusMessage &message) const
Sends a message over the bus.
TQT_DBusConnection()
Creates an empty/disconnected connection handle.
TQT_DBusError lastError() const
Returns the last error seen by the connection.
static void closeConnection(const TQString &name=default_connection_name)
Closes a connection with a given name.
static TQT_DBusConnection systemBus()
Gets a connection to the system bus.
void dispatch() const
Processes buffered inbound messages.
void scheduleDispatch() const
Request a delayed check for inbound buffer processing.
bool registerObject(const TQString &path, TQT_DBusObjectBase *object)
Registers a service object for a given path.
~TQT_DBusConnection()
Destroys the connection handle.
bool isConnected() const
Returns whether the connection is connected to a bus.
bool requestName(const TQString &name, int modeFlags=NoReplace)
Requests to be addressable on the bus by a given name.
void unregisterObject(const TQString &path)
Unregister a service object on a given path.
TQT_DBusConnectionPrivate * connection(const TQString &name) const
TQT_DBusConnectionPrivate * default_connection
static TQT_DBusConnectionManager * instance()
void setConnection(const TQString &name, TQT_DBusConnectionPrivate *c)
void removeConnection(const TQString &name)
static TQT_DBusConnectionManager * managerInstance
TQMap< TQString, TQT_DBusConnectionPrivate * > ConnectionHash
void setConnection(DBusConnection *connection)
void timerEvent(TQTimerEvent *e)
int sendWithReplyAsync(const TQT_DBusMessage &message, TQObject *receiver, const char *method)
Class for transporting D-Bus errors.
Definition: tqdbuserror.h:41
void setDBUSError(bool err) const
Definition: tqdbuserror.h:321
A message converts and transports data over D-Bus.
TQT_DBusMessagePrivate * d
DBusMessage * toDBusMessage() const
Creates a raw D-Bus message from this TQt3-bindings message.
static TQT_DBusMessage fromDBusMessage(DBusMessage *dmsg)
Creates a TQt3-bindings message from the given raw D-Bus message.
Base interface for D-Bus service objects.
Definition: tqdbusobject.h:303
void qDBusBindToApplication()
TQT_DBusConnectionManager * manager()