summaryrefslogtreecommitdiffstats
path: root/src/tqdbusproxy.cpp
blob: 7ca6118252a40dddc63e344b2951c8f5f2181002 (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
/* qdbusproxy.cpp DBUS Object proxy
 *
 * Copyright (C) 2005 Kevin Krammer <kevin.krammer@gmx.at>
 *
 * 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 "tqdbuserror.h"
#include "tqdbusconnection.h"
#include "tqdbusmessage.h"
#include "tqdbusproxy.h"

class TQT_DBusProxy::Private
{
public:
    Private() : canSend(false) {}
    ~Private() {}

    void checkCanSend()
    {
        canSend = !path.isEmpty() && !service.isEmpty() && !interface.isEmpty();
    }

public:
    TQT_DBusConnection connection;

    TQString service;
    TQString path;
    TQString interface;
    bool canSend;

    TQT_DBusError error;
};

TQT_DBusProxy::TQT_DBusProxy(TQObject* parent, const char* name)
    : TQObject(parent, (name ? name : "TQT_DBusProxy")),
      d(new Private())
{
}

TQT_DBusProxy::TQT_DBusProxy(const TQT_DBusConnection& connection,
                       TQObject* parent, const char* name)
    : TQObject(parent, (name ? name : "TQT_DBusProxy")),
      d(new Private())
{
    setConnection(connection);
}

TQT_DBusProxy::TQT_DBusProxy(const TQString& service, const TQString& path,
                       const TQString& interface, const TQT_DBusConnection& connection,
                       TQObject* parent, const char* name)
    : TQObject(parent, (name ? name : "TQT_DBusProxy")),
      d(new Private())
{
    setConnection(connection);

    d->service = service;
    d->path = path;
    d->interface = interface;
    d->checkCanSend();
}

TQT_DBusProxy::~TQT_DBusProxy()
{
    delete d;
}

bool TQT_DBusProxy::setConnection(const TQT_DBusConnection& connection)
{
    d->connection.disconnect(this, TQT_SLOT(handleDBusSignal(const TQT_DBusMessage&)));

    d->connection = connection;

    return d->connection.connect(this, TQT_SLOT(handleDBusSignal(const TQT_DBusMessage&)));
}

const TQT_DBusConnection& TQT_DBusProxy::connection() const
{
    return d->connection;
}

void TQT_DBusProxy::setService(const TQString& service)
{
    d->service = service;
    d->checkCanSend();
}

TQString TQT_DBusProxy::service() const
{
    return d->service;
}

void TQT_DBusProxy::setPath(const TQString& path)
{
    d->path = path;
    d->checkCanSend();
}

TQString TQT_DBusProxy::path() const
{
    return d->path;
}

void TQT_DBusProxy::setInterface(const TQString& interface)
{
    d->interface = interface;
    d->checkCanSend();
}

TQString TQT_DBusProxy::interface() const
{
    return d->interface;
}

bool TQT_DBusProxy::canSend() const
{
    return d->canSend && d->connection.isConnected();
}

bool TQT_DBusProxy::send(const TQString& method, const TQValueList<TQT_DBusData>& params) const
{
    if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
        return false;

    TQT_DBusMessage message = TQT_DBusMessage::methodCall(d->service, d->path,
                                                    d->interface, method);
    message += params;

    return d->connection.send(message);
}

TQT_DBusMessage TQT_DBusProxy::sendWithReply(const TQString& method,
                                       const TQValueList<TQT_DBusData>& params,
                                       TQT_DBusError* error) const
{
    if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
        return TQT_DBusMessage();

    TQT_DBusMessage message = TQT_DBusMessage::methodCall(d->service, d->path,
                                                    d->interface, method);
    message += params;

    TQT_DBusMessage reply = d->connection.sendWithReply(message, &d->error);

    if (error)
        *error = d->error;

    return reply;
}

int TQT_DBusProxy::sendWithAsyncReply(const TQString& method, const TQValueList<TQT_DBusData>& params)
{
    if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
        return 0;

    TQT_DBusMessage message = TQT_DBusMessage::methodCall(d->service, d->path,
                                                    d->interface, method);
    message += params;

    return d->connection.sendWithAsyncReply(message, this,
                   TQT_SLOT(handleAsyncReply(const TQT_DBusMessage&)));
}

TQT_DBusError TQT_DBusProxy::lastError() const
{
    return d->error;
}

void TQT_DBusProxy::handleDBusSignal(const TQT_DBusMessage& message)
{
    if (!d->path.isEmpty() && d->path != message.path())
        return;

    // only filter by service name if the name is a unique name
    // because signals are always coming from a connection's unique name
    // and filtering by a generic name would reject all signals
    if (d->service.startsWith(":") && d->service != message.sender())
        return;

    if (!d->interface.isEmpty() && d->interface != message.interface())
        return;

    emit dbusSignal(message);
}

void TQT_DBusProxy::handleAsyncReply(const TQT_DBusMessage& message)
{
    d->error = message.error();

    emit asyncReply(message.replySerialNumber(), message);
}

#include "tqdbusproxy.moc"