qdbusmessage.cpp

00001 /* qdbusmessage.cpp
00002  *
00003  * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
00004  *
00005  * Licensed under the Academic Free License version 2.1
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 #include "qdbusmessage.h"
00024 
00025 #include <QtCore/qdebug.h>
00026 #include <QtCore/qstringlist.h>
00027 
00028 #include <dbus/dbus.h>
00029 
00030 #include "qdbusmarshall.h"
00031 #include "qdbusmessage_p.h"
00032 
00033 QDBusMessagePrivate::QDBusMessagePrivate(QDBusMessage *qq)
00034     : msg(0), reply(0), q(qq), type(DBUS_MESSAGE_TYPE_INVALID), timeout(-1), ref(1)
00035 {
00036 }
00037 
00038 QDBusMessagePrivate::~QDBusMessagePrivate()
00039 {
00040     if (msg)
00041         dbus_message_unref(msg);
00042     if (reply)
00043         dbus_message_unref(reply);
00044 }
00045 
00047 
00048 
00049 QDBusMessage QDBusMessage::signal(const QString &path, const QString &interface,
00050                                   const QString &name)
00051 {
00052     QDBusMessage message;
00053     message.d->type = DBUS_MESSAGE_TYPE_SIGNAL;
00054     message.d->path = path;
00055     message.d->interface = interface;
00056     message.d->name = name;
00057 
00058     return message;
00059 }
00060 
00061 QDBusMessage QDBusMessage::methodCall(const QString &service, const QString &path,
00062                                       const QString &interface, const QString &method)
00063 {
00064     QDBusMessage message;
00065     message.d->type = DBUS_MESSAGE_TYPE_METHOD_CALL;
00066     message.d->service = service;
00067     message.d->path = path;
00068     message.d->interface = interface;
00069     message.d->method = method;
00070 
00071     return message;
00072 }
00073 
00074 QDBusMessage QDBusMessage::methodReply(const QDBusMessage &other)
00075 {
00076     Q_ASSERT(other.d->msg);
00077 
00078     QDBusMessage message;
00079     message.d->type = DBUS_MESSAGE_TYPE_METHOD_RETURN;
00080     message.d->reply = dbus_message_ref(other.d->msg);
00081 
00082     return message;
00083 }
00084 
00085 QDBusMessage::QDBusMessage()
00086 {
00087     d = new QDBusMessagePrivate(this);
00088 }
00089 
00090 QDBusMessage::QDBusMessage(const QDBusMessage &other)
00091     : QList<QVariant>(other)
00092 {
00093     d = other.d;
00094     d->ref.ref();
00095 }
00096 
00097 QDBusMessage::~QDBusMessage()
00098 {
00099     if (!d->ref.deref())
00100         delete d;
00101 }
00102 
00103 QDBusMessage &QDBusMessage::operator=(const QDBusMessage &other)
00104 {
00105     QList<QVariant>::operator=(other);
00106     qAtomicAssign(d, other.d);
00107     return *this;
00108 }
00109 
00110 DBusMessage *QDBusMessage::toDBusMessage() const
00111 {
00112     DBusMessage *msg = 0;
00113     switch (d->type) {
00114     case DBUS_MESSAGE_TYPE_METHOD_CALL:
00115         msg = dbus_message_new_method_call(d->service.toUtf8().constData(),
00116                 d->path.toUtf8().constData(), d->interface.toUtf8().constData(),
00117                 d->method.toUtf8().constData());
00118         break;
00119     case DBUS_MESSAGE_TYPE_SIGNAL:
00120         msg = dbus_message_new_signal(d->path.toUtf8().constData(),
00121                 d->interface.toUtf8().constData(), d->name.toUtf8().constData());
00122         break;
00123     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
00124         msg = dbus_message_new_method_return(d->reply);
00125         break;
00126     }
00127     if (!msg)
00128         return 0;
00129 
00130     QDBusMarshall::listToMessage(*this, msg);
00131     return msg;
00132 }
00133 
00134 QDBusMessage QDBusMessage::fromDBusMessage(DBusMessage *dmsg)
00135 {
00136     QDBusMessage message;
00137     if (!dmsg)
00138         return message;
00139 
00140     message.d->type = dbus_message_get_type(dmsg);
00141     message.d->path = QString::fromUtf8(dbus_message_get_path(dmsg));
00142     message.d->interface = QString::fromUtf8(dbus_message_get_interface(dmsg));
00143     message.d->name = QString::fromUtf8(dbus_message_get_member(dmsg));
00144     message.d->sender = QString::fromUtf8(dbus_message_get_sender(dmsg));
00145     message.d->msg = dbus_message_ref(dmsg);
00146 
00147     QDBusMarshall::messageToList(message, dmsg);
00148 
00149     return message;
00150 }
00151 
00152 QString QDBusMessage::path() const
00153 {
00154     return d->path;
00155 }
00156 
00157 QString QDBusMessage::interface() const
00158 {
00159     return d->interface;
00160 }
00161 
00162 QString QDBusMessage::name() const
00163 {
00164     return d->name;
00165 }
00166 
00167 QString QDBusMessage::sender() const
00168 {
00169     return d->sender;
00170 }
00171 
00172 int QDBusMessage::timeout() const
00173 {
00174     return d->timeout;
00175 }
00176 
00177 void QDBusMessage::setTimeout(int ms)
00178 {
00179     d->timeout = ms;
00180 }
00181 
00186 int QDBusMessage::serialNumber() const
00187 {
00188     if (!d->msg)
00189         return 0;
00190     return dbus_message_get_serial(d->msg);
00191 }
00192 
00201 int QDBusMessage::replySerialNumber() const
00202 {
00203     if (!d->msg)
00204         return 0;
00205     return dbus_message_get_reply_serial(d->msg);
00206 }
00207 
00208 QDBusMessage::MessageType QDBusMessage::type() const
00209 {
00210     switch (d->type) {
00211     case DBUS_MESSAGE_TYPE_METHOD_CALL:
00212         return MethodCallMessage;
00213     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
00214         return ReplyMessage;
00215     case DBUS_MESSAGE_TYPE_ERROR:
00216         return ErrorMessage;
00217     case DBUS_MESSAGE_TYPE_SIGNAL:
00218         return SignalMessage;
00219     default:
00220         return InvalidMessage;
00221     }
00222 }
00223 
00224 #ifndef QT_NO_DEBUG
00225 QDebug operator<<(QDebug dbg, const QDBusMessage &msg)
00226 {
00227     dbg.nospace() << "QDBusMessage(" << msg.path() << ", " << msg.interface() << ", "
00228                   << msg.name() << ", " << msg.sender() << ", "
00229                   << static_cast<QList<QVariant> >(msg) << ")";
00230     return dbg.space();
00231 }
00232 #endif
00233 

Generated on Wed Feb 27 10:13:38 2008 for D-BUS by  doxygen 1.4.6