|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtDBus module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qdbusargument_p.h" |
|
43 #include <stdlib.h> |
|
44 |
|
45 QT_BEGIN_NAMESPACE |
|
46 |
|
47 template <typename T> |
|
48 static inline T qIterGet(DBusMessageIter *it) |
|
49 { |
|
50 T t; |
|
51 q_dbus_message_iter_get_basic(it, &t); |
|
52 q_dbus_message_iter_next(it); |
|
53 return t; |
|
54 } |
|
55 |
|
56 QDBusDemarshaller::~QDBusDemarshaller() |
|
57 { |
|
58 } |
|
59 |
|
60 inline QString QDBusDemarshaller::currentSignature() |
|
61 { |
|
62 char *sig = q_dbus_message_iter_get_signature(&iterator); |
|
63 QString retval = QString::fromUtf8(sig); |
|
64 q_dbus_free(sig); |
|
65 |
|
66 return retval; |
|
67 } |
|
68 |
|
69 inline uchar QDBusDemarshaller::toByte() |
|
70 { |
|
71 return qIterGet<uchar>(&iterator); |
|
72 } |
|
73 |
|
74 inline bool QDBusDemarshaller::toBool() |
|
75 { |
|
76 return bool(qIterGet<dbus_bool_t>(&iterator)); |
|
77 } |
|
78 |
|
79 inline ushort QDBusDemarshaller::toUShort() |
|
80 { |
|
81 return qIterGet<dbus_uint16_t>(&iterator); |
|
82 } |
|
83 |
|
84 inline short QDBusDemarshaller::toShort() |
|
85 { |
|
86 return qIterGet<dbus_int16_t>(&iterator); |
|
87 } |
|
88 |
|
89 inline int QDBusDemarshaller::toInt() |
|
90 { |
|
91 return qIterGet<dbus_int32_t>(&iterator); |
|
92 } |
|
93 |
|
94 inline uint QDBusDemarshaller::toUInt() |
|
95 { |
|
96 return qIterGet<dbus_uint32_t>(&iterator); |
|
97 } |
|
98 |
|
99 inline qlonglong QDBusDemarshaller::toLongLong() |
|
100 { |
|
101 return qIterGet<qlonglong>(&iterator); |
|
102 } |
|
103 |
|
104 inline qulonglong QDBusDemarshaller::toULongLong() |
|
105 { |
|
106 return qIterGet<qulonglong>(&iterator); |
|
107 } |
|
108 |
|
109 inline double QDBusDemarshaller::toDouble() |
|
110 { |
|
111 return qIterGet<double>(&iterator); |
|
112 } |
|
113 |
|
114 inline QString QDBusDemarshaller::toString() |
|
115 { |
|
116 return QString::fromUtf8(qIterGet<char *>(&iterator)); |
|
117 } |
|
118 |
|
119 inline QDBusObjectPath QDBusDemarshaller::toObjectPath() |
|
120 { |
|
121 return QDBusObjectPath(QString::fromUtf8(qIterGet<char *>(&iterator))); |
|
122 } |
|
123 |
|
124 inline QDBusSignature QDBusDemarshaller::toSignature() |
|
125 { |
|
126 return QDBusSignature(QString::fromUtf8(qIterGet<char *>(&iterator))); |
|
127 } |
|
128 |
|
129 inline QDBusVariant QDBusDemarshaller::toVariant() |
|
130 { |
|
131 QDBusDemarshaller sub; |
|
132 sub.message = q_dbus_message_ref(message); |
|
133 q_dbus_message_iter_recurse(&iterator, &sub.iterator); |
|
134 q_dbus_message_iter_next(&iterator); |
|
135 |
|
136 return QDBusVariant( sub.toVariantInternal() ); |
|
137 } |
|
138 |
|
139 QDBusArgument::ElementType QDBusDemarshaller::currentType() |
|
140 { |
|
141 switch (q_dbus_message_iter_get_arg_type(&iterator)) { |
|
142 case DBUS_TYPE_BYTE: |
|
143 case DBUS_TYPE_INT16: |
|
144 case DBUS_TYPE_UINT16: |
|
145 case DBUS_TYPE_INT32: |
|
146 case DBUS_TYPE_UINT32: |
|
147 case DBUS_TYPE_INT64: |
|
148 case DBUS_TYPE_UINT64: |
|
149 case DBUS_TYPE_BOOLEAN: |
|
150 case DBUS_TYPE_DOUBLE: |
|
151 case DBUS_TYPE_STRING: |
|
152 case DBUS_TYPE_OBJECT_PATH: |
|
153 case DBUS_TYPE_SIGNATURE: |
|
154 return QDBusArgument::BasicType; |
|
155 |
|
156 case DBUS_TYPE_VARIANT: |
|
157 return QDBusArgument::VariantType; |
|
158 |
|
159 case DBUS_TYPE_ARRAY: |
|
160 switch (q_dbus_message_iter_get_element_type(&iterator)) { |
|
161 case DBUS_TYPE_BYTE: |
|
162 case DBUS_TYPE_STRING: |
|
163 // QByteArray and QStringList |
|
164 return QDBusArgument::BasicType; |
|
165 case DBUS_TYPE_DICT_ENTRY: |
|
166 return QDBusArgument::MapType; |
|
167 default: |
|
168 return QDBusArgument::ArrayType; |
|
169 } |
|
170 |
|
171 case DBUS_TYPE_STRUCT: |
|
172 return QDBusArgument::StructureType; |
|
173 case DBUS_TYPE_DICT_ENTRY: |
|
174 return QDBusArgument::MapEntryType; |
|
175 |
|
176 case DBUS_TYPE_INVALID: |
|
177 return QDBusArgument::UnknownType; |
|
178 |
|
179 default: |
|
180 qWarning("QDBusDemarshaller: Found unknown D-Bus type %d '%c'", |
|
181 q_dbus_message_iter_get_arg_type(&iterator), |
|
182 q_dbus_message_iter_get_arg_type(&iterator)); |
|
183 } |
|
184 return QDBusArgument::UnknownType; |
|
185 } |
|
186 |
|
187 QVariant QDBusDemarshaller::toVariantInternal() |
|
188 { |
|
189 switch (q_dbus_message_iter_get_arg_type(&iterator)) { |
|
190 case DBUS_TYPE_BYTE: |
|
191 return qVariantFromValue(toByte()); |
|
192 case DBUS_TYPE_INT16: |
|
193 return qVariantFromValue(toShort()); |
|
194 case DBUS_TYPE_UINT16: |
|
195 return qVariantFromValue(toUShort()); |
|
196 case DBUS_TYPE_INT32: |
|
197 return toInt(); |
|
198 case DBUS_TYPE_UINT32: |
|
199 return toUInt(); |
|
200 case DBUS_TYPE_DOUBLE: |
|
201 return toDouble(); |
|
202 case DBUS_TYPE_BOOLEAN: |
|
203 return toBool(); |
|
204 case DBUS_TYPE_INT64: |
|
205 return toLongLong(); |
|
206 case DBUS_TYPE_UINT64: |
|
207 return toULongLong(); |
|
208 case DBUS_TYPE_STRING: |
|
209 return toString(); |
|
210 case DBUS_TYPE_OBJECT_PATH: |
|
211 return qVariantFromValue(toObjectPath()); |
|
212 case DBUS_TYPE_SIGNATURE: |
|
213 return qVariantFromValue(toSignature()); |
|
214 case DBUS_TYPE_VARIANT: |
|
215 return qVariantFromValue(toVariant()); |
|
216 |
|
217 case DBUS_TYPE_ARRAY: |
|
218 switch (q_dbus_message_iter_get_element_type(&iterator)) { |
|
219 case DBUS_TYPE_BYTE: |
|
220 // QByteArray |
|
221 return toByteArray(); |
|
222 case DBUS_TYPE_STRING: |
|
223 return toStringList(); |
|
224 case DBUS_TYPE_DICT_ENTRY: |
|
225 return qVariantFromValue(duplicate()); |
|
226 |
|
227 default: |
|
228 return qVariantFromValue(duplicate()); |
|
229 } |
|
230 |
|
231 case DBUS_TYPE_STRUCT: |
|
232 return qVariantFromValue(duplicate()); |
|
233 |
|
234 default: |
|
235 qWarning("QDBusDemarshaller: Found unknown D-Bus type %d '%c'", |
|
236 q_dbus_message_iter_get_arg_type(&iterator), |
|
237 q_dbus_message_iter_get_arg_type(&iterator)); |
|
238 return QVariant(); |
|
239 break; |
|
240 }; |
|
241 } |
|
242 |
|
243 QStringList QDBusDemarshaller::toStringList() |
|
244 { |
|
245 QStringList list; |
|
246 |
|
247 QDBusDemarshaller sub; |
|
248 q_dbus_message_iter_recurse(&iterator, &sub.iterator); |
|
249 q_dbus_message_iter_next(&iterator); |
|
250 while (!sub.atEnd()) |
|
251 list.append(sub.toString()); |
|
252 |
|
253 return list; |
|
254 } |
|
255 |
|
256 QByteArray QDBusDemarshaller::toByteArray() |
|
257 { |
|
258 DBusMessageIter sub; |
|
259 q_dbus_message_iter_recurse(&iterator, &sub); |
|
260 q_dbus_message_iter_next(&iterator); |
|
261 int len; |
|
262 char* data; |
|
263 q_dbus_message_iter_get_fixed_array(&sub,&data,&len); |
|
264 return QByteArray(data,len); |
|
265 } |
|
266 |
|
267 bool QDBusDemarshaller::atEnd() |
|
268 { |
|
269 // dbus_message_iter_has_next is broken if the list has one single element |
|
270 return q_dbus_message_iter_get_arg_type(&iterator) == DBUS_TYPE_INVALID; |
|
271 } |
|
272 |
|
273 inline QDBusDemarshaller *QDBusDemarshaller::beginStructure() |
|
274 { |
|
275 return beginCommon(); |
|
276 } |
|
277 |
|
278 inline QDBusDemarshaller *QDBusDemarshaller::beginArray() |
|
279 { |
|
280 return beginCommon(); |
|
281 } |
|
282 |
|
283 inline QDBusDemarshaller *QDBusDemarshaller::beginMap() |
|
284 { |
|
285 return beginCommon(); |
|
286 } |
|
287 |
|
288 inline QDBusDemarshaller *QDBusDemarshaller::beginMapEntry() |
|
289 { |
|
290 return beginCommon(); |
|
291 } |
|
292 |
|
293 QDBusDemarshaller *QDBusDemarshaller::beginCommon() |
|
294 { |
|
295 QDBusDemarshaller *d = new QDBusDemarshaller; |
|
296 d->parent = this; |
|
297 d->message = q_dbus_message_ref(message); |
|
298 |
|
299 // recurse |
|
300 q_dbus_message_iter_recurse(&iterator, &d->iterator); |
|
301 q_dbus_message_iter_next(&iterator); |
|
302 return d; |
|
303 } |
|
304 |
|
305 inline QDBusDemarshaller *QDBusDemarshaller::endStructure() |
|
306 { |
|
307 return endCommon(); |
|
308 } |
|
309 |
|
310 inline QDBusDemarshaller *QDBusDemarshaller::endArray() |
|
311 { |
|
312 return endCommon(); |
|
313 } |
|
314 |
|
315 inline QDBusDemarshaller *QDBusDemarshaller::endMap() |
|
316 { |
|
317 return endCommon(); |
|
318 } |
|
319 |
|
320 inline QDBusDemarshaller *QDBusDemarshaller::endMapEntry() |
|
321 { |
|
322 return endCommon(); |
|
323 } |
|
324 |
|
325 QDBusDemarshaller *QDBusDemarshaller::endCommon() |
|
326 { |
|
327 QDBusDemarshaller *retval = parent; |
|
328 delete this; |
|
329 return retval; |
|
330 } |
|
331 |
|
332 QDBusArgument QDBusDemarshaller::duplicate() |
|
333 { |
|
334 QDBusDemarshaller *d = new QDBusDemarshaller; |
|
335 d->iterator = iterator; |
|
336 d->message = q_dbus_message_ref(message); |
|
337 |
|
338 q_dbus_message_iter_next(&iterator); |
|
339 return QDBusArgumentPrivate::create(d); |
|
340 } |
|
341 |
|
342 QT_END_NAMESPACE |