source: trunk/src/dbus/qdbusmessage.cpp@ 240

Last change on this file since 240 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 22.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdbusmessage.h"
43
44#include <qdebug.h>
45#include <qstringlist.h>
46
47#include <qdbus_symbols_p.h>
48
49#include "qdbusargument_p.h"
50#include "qdbuserror.h"
51#include "qdbusmessage_p.h"
52#include "qdbusmetatype.h"
53#include "qdbusconnection_p.h"
54#include "qdbusutil_p.h"
55
56QT_BEGIN_NAMESPACE
57
58static inline const char *data(const QByteArray &arr)
59{
60 return arr.isEmpty() ? 0 : arr.constData();
61}
62
63QDBusMessagePrivate::QDBusMessagePrivate()
64 : msg(0), reply(0), type(DBUS_MESSAGE_TYPE_INVALID),
65 timeout(-1), localReply(0), ref(1), delayedReply(false), localMessage(false)
66{
67}
68
69QDBusMessagePrivate::~QDBusMessagePrivate()
70{
71 if (msg)
72 q_dbus_message_unref(msg);
73 if (reply)
74 q_dbus_message_unref(reply);
75 delete localReply;
76}
77
78/*!
79 \since 4.3
80 Returns the human-readable message associated with the error that was received.
81*/
82QString QDBusMessage::errorMessage() const
83{
84 if (d_ptr->type == ErrorMessage) {
85 if (!d_ptr->message.isEmpty())
86 return d_ptr->message;
87 if (!d_ptr->arguments.isEmpty())
88 return d_ptr->arguments.at(0).toString();
89 }
90 return QString();
91}
92
93/*!
94 \internal
95 Constructs a DBusMessage object from this object. The returned value must be de-referenced
96 with q_dbus_message_unref.
97*/
98DBusMessage *QDBusMessagePrivate::toDBusMessage(const QDBusMessage &message)
99{
100 if (!qdbus_loadLibDBus())
101 return 0;
102
103 DBusMessage *msg = 0;
104 const QDBusMessagePrivate *d_ptr = message.d_ptr;
105
106 switch (d_ptr->type) {
107 case DBUS_MESSAGE_TYPE_INVALID:
108 //qDebug() << "QDBusMessagePrivate::toDBusMessage" << "message is invalid";
109 break;
110 case DBUS_MESSAGE_TYPE_METHOD_CALL:
111 msg = q_dbus_message_new_method_call(data(d_ptr->service.toUtf8()), data(d_ptr->path.toUtf8()),
112 data(d_ptr->interface.toUtf8()), data(d_ptr->name.toUtf8()));
113 break;
114 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
115 msg = q_dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN);
116 if (!d_ptr->localMessage) {
117 q_dbus_message_set_destination(msg, q_dbus_message_get_sender(d_ptr->reply));
118 q_dbus_message_set_reply_serial(msg, q_dbus_message_get_serial(d_ptr->reply));
119 }
120 break;
121 case DBUS_MESSAGE_TYPE_ERROR:
122 msg = q_dbus_message_new(DBUS_MESSAGE_TYPE_ERROR);
123 q_dbus_message_set_error_name(msg, data(d_ptr->name.toUtf8()));
124 if (!d_ptr->localMessage) {
125 q_dbus_message_set_destination(msg, q_dbus_message_get_sender(d_ptr->reply));
126 q_dbus_message_set_reply_serial(msg, q_dbus_message_get_serial(d_ptr->reply));
127 }
128 break;
129 case DBUS_MESSAGE_TYPE_SIGNAL:
130 msg = q_dbus_message_new_signal(data(d_ptr->path.toUtf8()), data(d_ptr->interface.toUtf8()),
131 data(d_ptr->name.toUtf8()));
132 break;
133 default:
134 Q_ASSERT(false);
135 break;
136 }
137#if 0
138 DBusError err;
139 q_dbus_error_init(&err);
140 if (q_dbus_error_is_set(&err)) {
141 QDBusError qe(&err);
142 qDebug() << "QDBusMessagePrivate::toDBusMessage" << qe;
143 }
144#endif
145 if (!msg)
146 return 0;
147
148 QDBusMarshaller marshaller;
149 QVariantList::ConstIterator it = d_ptr->arguments.constBegin();
150 QVariantList::ConstIterator cend = d_ptr->arguments.constEnd();
151 q_dbus_message_iter_init_append(msg, &marshaller.iterator);
152 if (!d_ptr->message.isEmpty())
153 // prepend the error message
154 marshaller.append(d_ptr->message);
155 for ( ; it != cend; ++it)
156 marshaller.appendVariantInternal(*it);
157
158 // check if everything is ok
159 if (marshaller.ok)
160 return msg;
161
162 // not ok;
163 q_dbus_message_unref(msg);
164 Q_ASSERT(false);
165 return 0;
166}
167
168/*
169struct DBusMessage
170{
171 DBusAtomic refcount;
172 DBusHeader header;