summaryrefslogtreecommitdiffstats
path: root/tqdbusconnection.cpp
blob: a62735e5e271aaa9f2b3b1b468ea706af1ed6a86 (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
/* qdbusconnection.cpp
 *
 * Copyright (C) 2005 Harald Fernengel <[email protected]>
 * Copyright (C) 2005-2007 Kevin Krammer <[email protected]>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 *
 */

#include <tqapplication.h>

#include "tqdbusconnection.h"
#include "tqdbuserror.h"
#include "tqdbusmessage.h"
#include "tqdbusconnection_p.h"

#include "tqdbusmessage_p.h"

QT_STATIC_CONST_IMPL char *TQT_DBusConnection::default_connection_name = "qt_dbus_default_connection";

class TQT_DBusConnectionManager
{
public:
    TQT_DBusConnectionManager(): default_connection(0) {}
    ~TQT_DBusConnectionManager();
    void bindToApplication();
    TQT_DBusConnectionPrivate *connection(const TQString &name) const;
    void removeConnection(const TQString &name);
    void setConnection(const TQString &name, TQT_DBusConnectionPrivate *c);

    static TQT_DBusConnectionManager* instance() {
        if (managerInstance == 0) managerInstance = new TQT_DBusConnectionManager();
        return managerInstance;
    }

private:
    TQT_DBusConnectionPrivate *default_connection;
    // FIXME-QT4 TQHash<TQString, TQT_DBusConnectionPrivate *> connectionHash;
    typedef TQMap<TQString, TQT_DBusConnectionPrivate*> ConnectionHash;
    ConnectionHash connectionHash;

    static TQT_DBusConnectionManager* managerInstance;
};

// FIXME-QT4 TQ_GLOBAL_STATIC(TQT_DBusConnectionManager, manager);
TQT_DBusConnectionManager* TQT_DBusConnectionManager::managerInstance = 0;
TQT_DBusConnectionManager* manager() {
    return TQT_DBusConnectionManager::instance();
}

TQT_DBusConnectionPrivate *TQT_DBusConnectionManager::connection(const TQString &name) const
{
    if (name == TQString::fromLatin1(TQT_DBusConnection::default_connection_name))
        return default_connection;

    ConnectionHash::const_iterator it = connectionHash.find(name);

    return (it != connectionHash.end() ? it.data() : 0);
}

void TQT_DBusConnectionManager::removeConnection(const TQString &name)
{
    TQT_DBusConnectionPrivate *d = 0;
    if (name == TQString::fromLatin1(TQT_DBusConnection::default_connection_name)) {
        d = default_connection;
        default_connection = 0;
    } else {
        ConnectionHash::iterator it = connectionHash.find(name);
        if (it == connectionHash.end())
            return;

        d = it.data();
        connectionHash.erase(it);
    }
    if (!d->ref.deref())
        delete d;
}

TQT_DBusConnectionManager::~TQT_DBusConnectionManager()
{
    if (default_connection) {
        delete default_connection;
        default_connection = 0;
    }
/* FIXME-QT4
    for (TQHash<TQString, TQT_DBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
         it != connectionHash.constEnd(); ++it) {
             delete it.value();
    }*/
    for (ConnectionHash::const_iterator it = connectionHash.constBegin();
         it != connectionHash.constEnd(); ++it)
    {
        delete it.data();
    }
    connectionHash.clear();
}

void TQT_DBusConnectionManager::bindToApplication()
{
    if (default_connection) {
        default_connection->bindToApplication();
    }
/* FIXME-QT4
    for (TQHash<TQString, TQT_DBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
         it != connectionHash.constEnd(); ++it) {
             (*it)->bindToApplication();
    }*/
    for (ConnectionHash::const_iterator it = connectionHash.constBegin();
         it != connectionHash.constEnd(); ++it)
    {
        it.data()->bindToApplication();
    }
}

void qDBusBindToApplication()
{
    manager()->bindToApplication();
}

void TQT_DBusConnectionManager::setConnection(const TQString &name, TQT_DBusConnectionPrivate *c)
{
    if (name == TQString::fromLatin1(TQT_DBusConnection::default_connection_name))
        default_connection = c;
    else
        connectionHash[name] = c;
}


TQT_DBusConnection::TQT_DBusConnection() : d(0)
{
}

TQT_DBusConnection::TQT_DBusConnection(const TQString &name)
{
    d = manager()->connection(name);
    if (d)
        d->ref.ref();
}

TQT_DBusConnection::TQT_DBusConnection(const TQT_DBusConnection &other)
{
    d = other.d;
    if (d)
        d->ref.ref();
}

TQT_DBusConnection::~TQT_DBusConnection()
{
    if (d && !d->ref.deref())
        delete d;
}

TQT_DBusConnection &TQT_DBusConnection::operator=(const TQT_DBusConnection &other)
{
    if (other.d)
        other.d->ref.ref();
/* FIXME-QT4
    TQT_DBusConnectionPrivate *old = static_cast<TQT_DBusConnectionPrivate *>(
            q_atomic_set_ptr(&d, other.d));*/
    TQT_DBusConnectionPrivate* old = d;
    d = other.d;
    if (old && !old->ref.deref())
        delete old;

    return *this;
}

TQT_DBusConnection TQT_DBusConnection::sessionBus()
{
    return addConnection(TQT_DBusConnection::SessionBus);
}

TQT_DBusConnection TQT_DBusConnection::systemBus()
{
    return addConnection(TQT_DBusConnection::SystemBus);
}

TQT_DBusConnection TQT_DBusConnection::addConnection(BusType type, const TQString &name)
{
//    Q_ASSERT_X(TQCoreApplication::instance(), "TQT_DBusConnection::addConnection",
//               "Cannot create connection without a Q[Core]Application instance");

    TQT_DBusConnectionPrivate *d = manager()->connection(name);
    if (d)
        return TQT_DBusConnection(name);

    d = new TQT_DBusConnectionPrivate;
    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 TQT_DBusConnection(name);
}

TQT_DBusConnection TQT_DBusConnection::addConnection(const TQString &address,
                    const TQString &name)
{
//    Q_ASSERT_X(TQCoreApplication::instance(), "TQT_DBusConnection::addConnection",
//               "Cannot create connection without a Q[Core]Application instance");

    TQT_DBusConnectionPrivate *d = manager()->connection(name);
    if (d)
        return TQT_DBusConnection(name);

    d = new TQT_DBusConnectionPrivate;
    // setConnection does the error handling for us
    d->setConnection(dbus_connection_open(address.utf8().data(), &d->error));

    manager()->setConnection(name, d);

    return TQT_DBusConnection(name);
}

void TQT_DBusConnection::closeConnection(const TQString &name)
{
    manager()->removeConnection(name);
}

void TQT_DBusConnectionPrivate::timerEvent(TQTimerEvent *e)
{
    DBusTimeout *timeout = timeouts[e->timerId()];
    dbus_timeout_handle(timeout);
}

bool TQT_DBusConnection::send(const TQT_DBusMessage &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 TQT_DBusConnection::sendWithAsyncReply(const TQT_DBusMessage &message, TQObject *receiver,
        const char *method) const
{
    if (!d || !d->connection)
        return 0;

    return d->sendWithReplyAsync(message, receiver, method);
}

TQT_DBusMessage TQT_DBusConnection::sendWithReply(const TQT_DBusMessage &message, TQT_DBusError *error) const
{
    if (!d || !d->connection)
        return TQT_DBusMessage::fromDBusMessage(0);

    DBusMessage *msg = message.toDBusMessage();
    if (!msg)
        return TQT_DBusMessage::fromDBusMessage(0);
    DBusMessage *reply = dbus_connection_send_with_reply_and_block(d->connection, msg,
                                                -1, &d->error);

    if (d->handleError() && error)
        *error = d->lastError;

    dbus_message_unref(msg);

    TQT_DBusMessage ret = TQT_DBusMessage::fromDBusMessage(reply);

    bool dbus_error_set = dbus_error_is_set(&d->error);
    ret.d->error.setDBUSError(dbus_error_set);
    if (error) error->setDBUSError(dbus_error_set);

    return ret;
}

void TQT_DBusConnection::flush() const
{
    if (!d || !d->connection) return;

    d->flush();
}

void TQT_DBusConnection::dispatch() const
{
    if (!d || !d->connection) return;

    d->dispatch();
}

void TQT_DBusConnection::scheduleDispatch() const
{
    if (!d || !d->connection) return;

    d->scheduleDispatch();
}

bool TQT_DBusConnection::connect(TQObject* object, const char* slot)
{
    if (!d || !d->connection || !object || !slot)
        return false;

    bool ok = object->connect(d, TQT_SIGNAL(dbusSignal(const TQT_DBusMessage&)), slot);

    return ok;
}

bool TQT_DBusConnection::disconnect(TQObject* object, const char* slot)
{
    if (!d || !d->connection || !object || !slot)
        return false;

    bool ok = d->disconnect(object, slot);

    return ok;
}

bool TQT_DBusConnection::registerObject(const TQString& path, TQT_DBusObjectBase* object)
{
    if (!d || !d->connection || !object || path.isEmpty())
        return false;

    TQT_DBusConnectionPrivate::ObjectMap::const_iterator it = d->registeredObjects.find(path);
    if (it != d->registeredObjects.end())
        return false;

    d->registeredObjects.insert(path, object);

    return true;
}

void TQT_DBusConnection::unregisterObject(const TQString &path)
{
    if (!d || !d->connection || path.isEmpty())
        return;

    TQT_DBusConnectionPrivate::ObjectMap::iterator it = d->registeredObjects.find(path);
    if (it == d->registeredObjects.end())
        return ;

    d->registeredObjects.erase(it);
}

bool TQT_DBusConnection::isConnected( ) const
{
    return d && d->connection && dbus_connection_get_is_connected(d->connection);
}

TQT_DBusError TQT_DBusConnection::lastError() const
{
    return d ? d->lastError : TQT_DBusError();
}

TQString TQT_DBusConnection::uniqueName() const
{
    return d && d->connection ?
            TQString::fromUtf8(dbus_bus_get_unique_name(d->connection))
            : TQString();
}

bool TQT_DBusConnection::requestName(const TQString &name, int modeFlags)
{
    Q_ASSERT(modeFlags >= 0);

    if (!d || !d->connection)
        return false;

    if (modeFlags < 0)
        return false;

    int dbusFlags = 0;
    if (modeFlags & AllowReplace)
        dbusFlags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
    if (modeFlags & ReplaceExisting)
        dbusFlags |= DBUS_NAME_FLAG_REPLACE_EXISTING;

    dbus_bus_request_name(d->connection, name.utf8(), dbusFlags, &d->error);

    return !d->handleError();
}

#include "tqdbusconnection.moc"