1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDBus module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdbusargument_p.h"
43 #include "qdbusconnection.h"
44 #include "qdbusmetatype_p.h"
45 #include "qdbusutil_p.h"
51 static void qIterAppend(DBusMessageIter *it, QByteArray *ba, int type, const void *arg)
56 q_dbus_message_iter_append_basic(it, type, arg);
59 QDBusMarshaller::~QDBusMarshaller()
64 inline QString QDBusMarshaller::currentSignature()
67 return QString::fromUtf8(q_dbus_message_get_signature(message));
71 inline void QDBusMarshaller::append(uchar arg)
73 qIterAppend(&iterator, ba, DBUS_TYPE_BYTE, &arg);
76 inline void QDBusMarshaller::append(bool arg)
78 dbus_bool_t cast = arg;
79 qIterAppend(&iterator, ba, DBUS_TYPE_BOOLEAN, &cast);
82 inline void QDBusMarshaller::append(short arg)
84 qIterAppend(&iterator, ba, DBUS_TYPE_INT16, &arg);
87 inline void QDBusMarshaller::append(ushort arg)
89 qIterAppend(&iterator, ba, DBUS_TYPE_UINT16, &arg);
92 inline void QDBusMarshaller::append(int arg)
94 qIterAppend(&iterator, ba, DBUS_TYPE_INT32, &arg);
97 inline void QDBusMarshaller::append(uint arg)
99 qIterAppend(&iterator, ba, DBUS_TYPE_UINT32, &arg);
102 inline void QDBusMarshaller::append(qlonglong arg)
104 qIterAppend(&iterator, ba, DBUS_TYPE_INT64, &arg);
107 inline void QDBusMarshaller::append(qulonglong arg)
109 qIterAppend(&iterator, ba, DBUS_TYPE_UINT64, &arg);
112 inline void QDBusMarshaller::append(double arg)
114 qIterAppend(&iterator, ba, DBUS_TYPE_DOUBLE, &arg);
117 void QDBusMarshaller::append(const QString &arg)
119 QByteArray data = arg.toUtf8();
120 const char *cdata = data.constData();
121 qIterAppend(&iterator, ba, DBUS_TYPE_STRING, &cdata);
124 inline void QDBusMarshaller::append(const QDBusObjectPath &arg)
126 QByteArray data = arg.path().toUtf8();
127 if (!ba && data.isEmpty()) {
128 error(QLatin1String("Invalid object path passed in arguments"));
130 const char *cdata = data.constData();
131 qIterAppend(&iterator, ba, DBUS_TYPE_OBJECT_PATH, &cdata);
135 inline void QDBusMarshaller::append(const QDBusSignature &arg)
137 QByteArray data = arg.signature().toUtf8();
138 if (!ba && data.isEmpty()) {
139 error(QLatin1String("Invalid signature passed in arguments"));
141 const char *cdata = data.constData();
142 qIterAppend(&iterator, ba, DBUS_TYPE_SIGNATURE, &cdata);
146 inline void QDBusMarshaller::append(const QDBusUnixFileDescriptor &arg)
148 int fd = arg.fileDescriptor();
149 if (!ba && fd == -1) {
150 error(QLatin1String("Invalid file descriptor passed in arguments"));
152 qIterAppend(&iterator, ba, DBUS_TYPE_UNIX_FD, &fd);
156 inline void QDBusMarshaller::append(const QByteArray &arg)
159 *ba += DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
163 const char* cdata = arg.constData();
164 DBusMessageIter subiterator;
165 q_dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING,
167 q_dbus_message_iter_append_fixed_array(&subiterator, DBUS_TYPE_BYTE, &cdata, arg.length());
168 q_dbus_message_iter_close_container(&iterator, &subiterator);
171 inline bool QDBusMarshaller::append(const QDBusVariant &arg)
174 *ba += DBUS_TYPE_VARIANT_AS_STRING;
178 const QVariant &value = arg.variant();
179 int id = value.userType();
180 if (id == QVariant::Invalid) {
181 qWarning("QDBusMarshaller: cannot add a null QDBusVariant");
182 error(QLatin1String("Variant containing QVariant::Invalid passed in arguments"));
186 QByteArray tmpSignature;
187 const char *signature = 0;
188 if (id == QDBusMetaTypeId::argument) {
189 // take the signature from the QDBusArgument object we're marshalling
191 qvariant_cast<QDBusArgument>(value).currentSignature().toLatin1();
192 signature = tmpSignature.constData();
194 // take the signatuer from the metatype we're marshalling
195 signature = QDBusMetaType::typeToSignature(id);
198 qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
199 "Use qDBusRegisterMetaType to register it",
200 QMetaType::typeName(id), id);
201 error(QString::fromLatin1("Unregistered type %1 passed in arguments")
202 .arg(QLatin1String(QMetaType::typeName(id))));
206 QDBusMarshaller sub(capabilities);
207 open(sub, DBUS_TYPE_VARIANT, signature);
208 bool isOk = sub.appendVariantInternal(value);
209 // don't call sub.close(): it auto-closes
214 inline void QDBusMarshaller::append(const QStringList &arg)
217 *ba += DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
221 QDBusMarshaller sub(capabilities);
222 open(sub, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING);
223 QStringList::ConstIterator it = arg.constBegin();
224 QStringList::ConstIterator end = arg.constEnd();
225 for ( ; it != end; ++it)
227 // don't call sub.close(): it auto-closes
230 inline QDBusMarshaller *QDBusMarshaller::beginStructure()
232 return beginCommon(DBUS_TYPE_STRUCT, 0);
235 inline QDBusMarshaller *QDBusMarshaller::beginArray(int id)
237 const char *signature = QDBusMetaType::typeToSignature( QVariant::Type(id) );
239 qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
240 "Use qDBusRegisterMetaType to register it",
241 QMetaType::typeName(id), id);
242 error(QString::fromLatin1("Unregistered type %1 passed in arguments")
243 .arg(QLatin1String(QMetaType::typeName(id))));
247 return beginCommon(DBUS_TYPE_ARRAY, signature);
250 inline QDBusMarshaller *QDBusMarshaller::beginMap(int kid, int vid)
252 const char *ksignature = QDBusMetaType::typeToSignature( QVariant::Type(kid) );
254 qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
255 "Use qDBusRegisterMetaType to register it",
256 QMetaType::typeName(kid), kid);
257 error(QString::fromLatin1("Unregistered type %1 passed in arguments")
258 .arg(QLatin1String(QMetaType::typeName(kid))));
261 if (ksignature[1] != 0 || !QDBusUtil::isValidBasicType(*ksignature)) {
262 qWarning("QDBusMarshaller: type '%s' (%d) cannot be used as the key type in a D-BUS map.",
263 QMetaType::typeName(kid), kid);
264 error(QString::fromLatin1("Type %1 passed in arguments cannot be used as a key in a map")
265 .arg(QLatin1String(QMetaType::typeName(kid))));
269 const char *vsignature = QDBusMetaType::typeToSignature( QVariant::Type(vid) );
271 const char *typeName = QMetaType::typeName(vid);
272 qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
273 "Use qDBusRegisterMetaType to register it",
275 error(QString::fromLatin1("Unregistered type %1 passed in arguments")
276 .arg(QLatin1String(typeName)));
280 QByteArray signature;
281 signature = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;
282 signature += ksignature;
283 signature += vsignature;
284 signature += DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
285 return beginCommon(DBUS_TYPE_ARRAY, signature);
288 inline QDBusMarshaller *QDBusMarshaller::beginMapEntry()
290 return beginCommon(DBUS_TYPE_DICT_ENTRY, 0);
293 void QDBusMarshaller::open(QDBusMarshaller &sub, int code, const char *signature)
298 sub.capabilities = capabilities;
302 case DBUS_TYPE_ARRAY:
307 case DBUS_TYPE_DICT_ENTRY:
311 case DBUS_TYPE_STRUCT:
312 *ba += DBUS_STRUCT_BEGIN_CHAR;
313 sub.closeCode = DBUS_STRUCT_END_CHAR;
317 q_dbus_message_iter_open_container(&iterator, code, signature, &sub.iterator);
320 QDBusMarshaller *QDBusMarshaller::beginCommon(int code, const char *signature)
322 QDBusMarshaller *d = new QDBusMarshaller(capabilities);
323 open(*d, code, signature);
327 inline QDBusMarshaller *QDBusMarshaller::endStructure()
328 { return endCommon(); }
330 inline QDBusMarshaller *QDBusMarshaller::endArray()
331 { return endCommon(); }
333 inline QDBusMarshaller *QDBusMarshaller::endMap()
334 { return endCommon(); }
336 inline QDBusMarshaller *QDBusMarshaller::endMapEntry()
337 { return endCommon(); }
339 QDBusMarshaller *QDBusMarshaller::endCommon()
341 QDBusMarshaller *retval = parent;
346 void QDBusMarshaller::close()
352 q_dbus_message_iter_close_container(&parent->iterator, &iterator);
356 void QDBusMarshaller::error(const QString &msg)
365 bool QDBusMarshaller::appendVariantInternal(const QVariant &arg)
367 int id = arg.userType();
368 if (id == QVariant::Invalid) {
369 qWarning("QDBusMarshaller: cannot add an invalid QVariant");
370 error(QLatin1String("Variant containing QVariant::Invalid passed in arguments"));
374 // intercept QDBusArgument parameters here
375 if (id == QDBusMetaTypeId::argument) {
376 QDBusArgument dbusargument = qvariant_cast<QDBusArgument>(arg);
377 QDBusArgumentPrivate *d = QDBusArgumentPrivate::d(dbusargument);
379 return false; // can't append this one...
381 QDBusDemarshaller demarshaller(capabilities);
382 demarshaller.message = q_dbus_message_ref(d->message);
384 if (d->direction == Demarshalling) {
385 // it's demarshalling; just copy
386 demarshaller.iterator = static_cast<QDBusDemarshaller *>(d)->iterator;
388 // it's marshalling; start over
389 if (!q_dbus_message_iter_init(demarshaller.message, &demarshaller.iterator))
390 return false; // error!
393 return appendCrossMarshalling(&demarshaller);
396 const char *signature = QDBusMetaType::typeToSignature( QVariant::Type(id) );
398 qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
399 "Use qDBusRegisterMetaType to register it",
400 QMetaType::typeName(id), id);
401 error(QString::fromLatin1("Unregistered type %1 passed in arguments")
402 .arg(QLatin1String(QMetaType::typeName(id))));
406 switch (*signature) {
409 case DBUS_TYPE_INT16:
410 case DBUS_TYPE_UINT16:
411 case DBUS_TYPE_INT32:
412 case DBUS_TYPE_UINT32:
413 case DBUS_TYPE_INT64:
414 case DBUS_TYPE_UINT64:
415 case DBUS_TYPE_DOUBLE:
416 qIterAppend(&iterator, ba, *signature, arg.constData());
418 case DBUS_TYPE_BOOLEAN:
419 append( arg.toBool() );
423 append( qvariant_cast<uchar>(arg) );
425 case DBUS_TYPE_BOOLEAN:
426 append( arg.toBool() );
428 case DBUS_TYPE_INT16:
429 append( qvariant_cast<short>(arg) );
431 case DBUS_TYPE_UINT16:
432 append( qvariant_cast<ushort>(arg) );
434 case DBUS_TYPE_INT32:
435 append( static_cast<dbus_int32_t>(arg.toInt()) );
437 case DBUS_TYPE_UINT32:
438 append( static_cast<dbus_uint32_t>(arg.toUInt()) );
440 case DBUS_TYPE_INT64:
441 append( arg.toLongLong() );
443 case DBUS_TYPE_UINT64:
444 append( arg.toULongLong() );
446 case DBUS_TYPE_DOUBLE:
447 append( arg.toDouble() );
451 case DBUS_TYPE_STRING:
452 append( arg.toString() );
454 case DBUS_TYPE_OBJECT_PATH:
455 append( qvariant_cast<QDBusObjectPath>(arg) );
457 case DBUS_TYPE_SIGNATURE:
458 append( qvariant_cast<QDBusSignature>(arg) );
462 case DBUS_TYPE_VARIANT:
464 return append( qvariant_cast<QDBusVariant>(arg) );
466 case DBUS_TYPE_ARRAY:
467 // could be many things
468 // find out what kind of array it is
469 switch (arg.type()) {
470 case QVariant::StringList:
471 append( arg.toStringList() );
474 case QVariant::ByteArray:
475 append( arg.toByteArray() );
483 case DBUS_TYPE_STRUCT:
484 case DBUS_STRUCT_BEGIN_CHAR:
485 return appendRegisteredType( arg );
487 case DBUS_TYPE_DICT_ENTRY:
488 case DBUS_DICT_ENTRY_BEGIN_CHAR:
489 qFatal("QDBusMarshaller::appendVariantInternal got a DICT_ENTRY!");
492 case DBUS_TYPE_UNIX_FD:
493 if (capabilities & QDBusConnection::UnixFileDescriptorPassing || ba) {
494 append(qvariant_cast<QDBusUnixFileDescriptor>(arg));
500 qWarning("QDBusMarshaller::appendVariantInternal: Found unknown D-BUS type '%s'",
508 bool QDBusMarshaller::appendRegisteredType(const QVariant &arg)
510 ref.ref(); // reference up
511 QDBusArgument self(QDBusArgumentPrivate::create(this));
512 return QDBusMetaType::marshall(self, arg.userType(), arg.constData());
515 bool QDBusMarshaller::appendCrossMarshalling(QDBusDemarshaller *demarshaller)
517 int code = q_dbus_message_iter_get_arg_type(&demarshaller->iterator);
518 if (QDBusUtil::isValidBasicType(code)) {
520 // do exactly like the D-BUS docs suggest
521 // (see apidocs for q_dbus_message_iter_get_basic)
524 q_dbus_message_iter_get_basic(&demarshaller->iterator, &value);
525 q_dbus_message_iter_next(&demarshaller->iterator);
526 q_dbus_message_iter_append_basic(&iterator, code, &value);
530 if (code == DBUS_TYPE_ARRAY) {
531 int element = q_dbus_message_iter_get_element_type(&demarshaller->iterator);
532 if (QDBusUtil::isValidFixedType(element) && element != DBUS_TYPE_UNIX_FD) {
533 // another optimization: fixed size arrays
534 // code is exactly like QDBusDemarshaller::toByteArray
536 q_dbus_message_iter_recurse(&demarshaller->iterator, &sub);
537 q_dbus_message_iter_next(&demarshaller->iterator);
540 q_dbus_message_iter_get_fixed_array(&sub,&data,&len);
542 char signature[2] = { char(element), 0 };
543 q_dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, signature, &sub);
544 q_dbus_message_iter_append_fixed_array(&sub, element, &data, len);
545 q_dbus_message_iter_close_container(&iterator, &sub);
551 // We have to recurse
552 QDBusDemarshaller *drecursed = demarshaller->beginCommon();
554 QDBusMarshaller mrecursed(capabilities); // create on the stack makes it autoclose
555 QByteArray subSignature;
557 if (code == DBUS_TYPE_VARIANT || code == DBUS_TYPE_ARRAY) {
558 subSignature = drecursed->currentSignature().toLatin1();
559 if (!subSignature.isEmpty())
560 sig = subSignature.constData();
562 open(mrecursed, code, sig);
564 while (!drecursed->atEnd()) {
565 if (!mrecursed.appendCrossMarshalling(drecursed)) {