3 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
5 * Licensed under the Academic Free License version 2.1
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "qdbusmessage.h"
25 #include <QtCore/qdebug.h>
26 #include <QtCore/qstringlist.h>
28 #include <dbus/dbus.h>
30 #include "qdbusmarshall.h"
31 #include "qdbusmessage_p.h"
33 QDBusMessagePrivate::QDBusMessagePrivate(QDBusMessage *qq)
34 : msg(0), reply(0), q(qq), type(DBUS_MESSAGE_TYPE_INVALID), timeout(-1), ref(1)
38 QDBusMessagePrivate::~QDBusMessagePrivate()
41 dbus_message_unref(msg);
43 dbus_message_unref(reply);
49 QDBusMessage QDBusMessage::signal(const QString &path, const QString &interface,
53 message.d->type = DBUS_MESSAGE_TYPE_SIGNAL;
54 message.d->path = path;
55 message.d->interface = interface;
56 message.d->name = name;
61 QDBusMessage QDBusMessage::methodCall(const QString &service, const QString &path,
62 const QString &interface, const QString &method)
65 message.d->type = DBUS_MESSAGE_TYPE_METHOD_CALL;
66 message.d->service = service;
67 message.d->path = path;
68 message.d->interface = interface;
69 message.d->method = method;
74 QDBusMessage QDBusMessage::methodReply(const QDBusMessage &other)
76 Q_ASSERT(other.d->msg);
79 message.d->type = DBUS_MESSAGE_TYPE_METHOD_RETURN;
80 message.d->reply = dbus_message_ref(other.d->msg);
85 QDBusMessage::QDBusMessage()
87 d = new QDBusMessagePrivate(this);
90 QDBusMessage::QDBusMessage(const QDBusMessage &other)
91 : QList<QVariant>(other)
97 QDBusMessage::~QDBusMessage()
103 QDBusMessage &QDBusMessage::operator=(const QDBusMessage &other)
105 QList<QVariant>::operator=(other);
106 qAtomicAssign(d, other.d);
110 DBusMessage *QDBusMessage::toDBusMessage() const
112 DBusMessage *msg = 0;
114 case DBUS_MESSAGE_TYPE_METHOD_CALL:
115 msg = dbus_message_new_method_call(d->service.toUtf8().constData(),
116 d->path.toUtf8().constData(), d->interface.toUtf8().constData(),
117 d->method.toUtf8().constData());
119 case DBUS_MESSAGE_TYPE_SIGNAL:
120 msg = dbus_message_new_signal(d->path.toUtf8().constData(),
121 d->interface.toUtf8().constData(), d->name.toUtf8().constData());
123 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
124 msg = dbus_message_new_method_return(d->reply);
130 QDBusMarshall::listToMessage(*this, msg);
134 QDBusMessage QDBusMessage::fromDBusMessage(DBusMessage *dmsg)
136 QDBusMessage message;
140 message.d->type = dbus_message_get_type(dmsg);
141 message.d->path = QString::fromUtf8(dbus_message_get_path(dmsg));
142 message.d->interface = QString::fromUtf8(dbus_message_get_interface(dmsg));
143 message.d->name = QString::fromUtf8(dbus_message_get_member(dmsg));
144 message.d->sender = QString::fromUtf8(dbus_message_get_sender(dmsg));
145 message.d->msg = dbus_message_ref(dmsg);
147 QDBusMarshall::messageToList(message, dmsg);
152 QString QDBusMessage::path() const
157 QString QDBusMessage::interface() const
162 QString QDBusMessage::name() const
167 QString QDBusMessage::sender() const
172 int QDBusMessage::timeout() const
177 void QDBusMessage::setTimeout(int ms)
183 Returns the unique serial number assigned to this message
184 or 0 if the message was not sent yet.
186 int QDBusMessage::serialNumber() const
190 return dbus_message_get_serial(d->msg);
194 Returns the unique serial number assigned to the message
195 that triggered this reply message.
197 If this message is not a reply to another message, 0
201 int QDBusMessage::replySerialNumber() const
205 return dbus_message_get_reply_serial(d->msg);
208 QDBusMessage::MessageType QDBusMessage::type() const
211 case DBUS_MESSAGE_TYPE_METHOD_CALL:
212 return MethodCallMessage;
213 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
215 case DBUS_MESSAGE_TYPE_ERROR:
217 case DBUS_MESSAGE_TYPE_SIGNAL:
218 return SignalMessage;
220 return InvalidMessage;
225 QDebug operator<<(QDebug dbg, const QDBusMessage &msg)
227 dbg.nospace() << "QDBusMessage(" << msg.path() << ", " << msg.interface() << ", "
228 << msg.name() << ", " << msg.sender() << ", "
229 << static_cast<QList<QVariant> >(msg) << ")";