DBus-1-TQt 1.0
tqdbusmessage.cpp
Go to the documentation of this file.
1/* qdbusmessage.cpp
2 *
3 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
4 * Copyright (C) 2005 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 "tqdbusmessage.h"
26
27#include <tqstringlist.h>
28
29#include <dbus/dbus.h>
30
31#include "tqdbusmarshall.h"
32#include "tqdbusmessage_p.h"
33
35 : msg(0), reply(0), q(qq), type(DBUS_MESSAGE_TYPE_INVALID), timeout(-1), ref(1)
36{
37}
38
40{
41 if (msg)
42 dbus_message_unref(msg);
43 if (reply)
44 dbus_message_unref(reply);
45}
46
48
49
50TQT_DBusMessage TQT_DBusMessage::signal(const TQString &path, const TQString &interface,
51 const TQString &member)
52{
53 TQT_DBusMessage message;
54 message.d->type = DBUS_MESSAGE_TYPE_SIGNAL;
55 message.d->path = path;
56 message.d->interface = interface;
57 message.d->member = member;
58
59 return message;
60}
61
62TQT_DBusMessage TQT_DBusMessage::methodCall(const TQString &service, const TQString &path,
63 const TQString &interface, const TQString &method)
64{
65 TQT_DBusMessage message;
66 message.d->type = DBUS_MESSAGE_TYPE_METHOD_CALL;
67 message.d->service = service;
68 message.d->path = path;
69 message.d->interface = interface;
70 message.d->member = method;
71
72 return message;
73}
74
76{
77 Q_ASSERT(other.d->msg);
78
79 TQT_DBusMessage message;
80 message.d->type = DBUS_MESSAGE_TYPE_METHOD_RETURN;
81 message.d->reply = dbus_message_ref(other.d->msg);
82
83 return message;
84}
85
87{
88 Q_ASSERT(other.d->msg);
89
90 TQT_DBusMessage message;
91 if (!error.isValid())
92 {
93 tqWarning("TQT_DBusMessage: error passed to methodError() is not valid!");
94 return message;
95 }
96
97 message.d->type = DBUS_MESSAGE_TYPE_ERROR;
98 message.d->reply = dbus_message_ref(other.d->msg);
99 message.d->error = error;
100
101 return message;
102}
103
105{
106 d = new TQT_DBusMessagePrivate(this);
107}
108
110 : TQValueList<TQT_DBusData>(other)
111{
112 d = other.d;
113 d->ref.ref();
114}
115
117{
118 if (!d->ref.deref())
119 delete d;
120}
121
123{
125 // FIXME-QT4 qAtomicAssign(d, other.d);
126 if (other.d) other.d->ref.ref();
128 d = other.d;
129 if (old && !old->ref.deref())
130 delete old;
131 return *this;
132}
133
135{
136 DBusMessage *msg = 0;
137 switch (d->type) {
138 case DBUS_MESSAGE_TYPE_METHOD_CALL:
139 msg = dbus_message_new_method_call(d->service.utf8().data(),
140 d->path.utf8().data(), d->interface.utf8().data(),
141 d->member.utf8().data());
142 break;
143 case DBUS_MESSAGE_TYPE_SIGNAL:
144 msg = dbus_message_new_signal(d->path.utf8().data(),
145 d->interface.utf8().data(), d->member.utf8().data());
146 break;
147 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
148 msg = dbus_message_new_method_return(d->reply);
149 break;
150 case DBUS_MESSAGE_TYPE_ERROR:
151 msg = dbus_message_new_error(d->reply, d->error.name().utf8().data(),
152 d->error.message().utf8().data());
153 break;
154 }
155 if (!msg)
156 return 0;
157
159 return msg;
160}
161
163{
164 TQT_DBusMessage message;
165 if (!dmsg)
166 return message;
167
168 message.d->type = dbus_message_get_type(dmsg);
169 message.d->path = TQString::fromUtf8(dbus_message_get_path(dmsg));
170 message.d->interface = TQString::fromUtf8(dbus_message_get_interface(dmsg));
171 message.d->member = TQString::fromUtf8(dbus_message_get_member(dmsg));
172 message.d->sender = TQString::fromUtf8(dbus_message_get_sender(dmsg));
173 message.d->msg = dbus_message_ref(dmsg);
174
175 DBusError dbusError;
176 dbus_error_init(&dbusError);
177 if (dbus_set_error_from_message(&dbusError, dmsg))
178 {
179 message.d->error = TQT_DBusError(&dbusError);
180 }
181
182 TQT_DBusMarshall::messageToList(message, dmsg);
183
184 return message;
185}
186
187TQString TQT_DBusMessage::path() const
188{
189 return d->path;
190}
191
193{
194 return d->interface;
195}
196
198{
199 return d->member;
200}
201
203{
204 return d->sender;
205}
206
208{
209 return d->error;
210}
211
213{
214 return d->timeout;
215}
216
218{
219 d->timeout = ms;
220}
221
227{
228 if (!d->msg)
229 return 0;
230 return dbus_message_get_serial(d->msg);
231}
232
242{
243 if (!d->msg)
244 return 0;
245 return dbus_message_get_reply_serial(d->msg);
246}
247
249{
250 switch (d->type) {
251 case DBUS_MESSAGE_TYPE_METHOD_CALL:
252 return MethodCallMessage;
253 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
254 return ReplyMessage;
255 case DBUS_MESSAGE_TYPE_ERROR:
256 return ErrorMessage;
257 case DBUS_MESSAGE_TYPE_SIGNAL:
258 return SignalMessage;
259 default:
260 return InvalidMessage;
261 }
262}
263
bool deref()
Class for accurately representing D-Bus data types.
Definition: tqdbusdata.h:59
Class for transporting D-Bus errors.
Definition: tqdbuserror.h:41
TQString name() const
Returns the D-Bus error name.
Definition: tqdbuserror.h:282
TQString message() const
Returns a string describing the error.
Definition: tqdbuserror.h:295
bool isValid() const
Returns whether the error object is valid.
static void messageToList(TQValueList< TQT_DBusData > &list, DBusMessage *message)
static void listToMessage(const TQValueList< TQT_DBusData > &list, DBusMessage *message)
A message converts and transports data over D-Bus.
TQT_DBusMessagePrivate * d
static TQT_DBusMessage methodError(const TQT_DBusMessage &other, const TQT_DBusError &error)
Creates a message for replying to a D-Bus method call.
static TQT_DBusMessage methodCall(const TQString &service, const TQString &path, const TQString &interface, const TQString &method)
Creates a message for sending a D-Bus method call.
TQT_DBusError error() const
Returns the error of an error message.
TQT_DBusMessage & operator=(const TQT_DBusMessage &other)
Creates a shallow copy of the given message.
TQString interface() const
Returns the message's interface name.
TQT_DBusMessage()
Creates an empty and invalid message.
void setTimeout(int ms)
Sets the message's timeout.
static TQT_DBusMessage methodReply(const TQT_DBusMessage &other)
Creates a message for replying to a D-Bus method call.
TQString sender() const
Returns the name of the message sender.
int timeout() const
Returns the message's timeout.
TQString member() const
Returns the message's member name.
~TQT_DBusMessage()
Destroys a message.
static TQT_DBusMessage signal(const TQString &path, const TQString &interface, const TQString &member)
Creates a message for sending a D-Bus signal.
int replySerialNumber() const
Returns the message's reply serial number.
MessageType
D-Bus message types.
int serialNumber() const
Returns the message's serial number.
MessageType type() const
Returns which kind of message this is.
TQString path() const
Returns the message's object path.
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.
TQT_DBusMessagePrivate(TQT_DBusMessage *qq)