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 "qdbusmetaobject_p.h"
44 #include <QtCore/qbytearray.h>
45 #include <QtCore/qhash.h>
46 #include <QtCore/qstring.h>
47 #include <QtCore/qvarlengtharray.h>
49 #include "qdbusutil_p.h"
50 #include "qdbuserror.h"
51 #include "qdbusmetatype.h"
52 #include "qdbusargument.h"
53 #include "qdbusintrospection_p.h"
54 #include "qdbusabstractinterface_p.h"
56 #include <private/qmetaobject_p.h>
57 #include <private/qmetaobjectbuilder_p.h>
63 class QDBusMetaObjectGenerator
66 QDBusMetaObjectGenerator(const QString &interface,
67 const QDBusIntrospection::Interface *parsedData);
68 void write(QDBusMetaObject *obj);
69 void writeWithoutXml(QDBusMetaObject *obj);
73 QList<QByteArray> parameterNames;
77 QVarLengthArray<int, 4> inputTypes;
78 QVarLengthArray<int, 4> outputTypes;
93 QMap<QByteArray, Method> signals_;
94 QMap<QByteArray, Method> methods;
95 QMap<QByteArray, Property> properties;
97 const QDBusIntrospection::Interface *data;
100 Type findType(const QByteArray &signature,
101 const QDBusIntrospection::Annotations &annotations,
102 const char *direction = "Out", int id = -1);
106 void parseProperties();
108 static int aggregateParameterCount(const QMap<QByteArray, Method> &map);
111 static const int intsPerProperty = 2;
112 static const int intsPerMethod = 2;
114 struct QDBusMetaObjectPrivate : public QMetaObjectPrivate
116 int propertyDBusData;
120 QDBusMetaObjectGenerator::QDBusMetaObjectGenerator(const QString &interfaceName,
121 const QDBusIntrospection::Interface *parsedData)
122 : data(parsedData), interface(interfaceName)
126 parseSignals(); // call parseSignals first so that slots override signals
131 Q_DBUS_EXPORT bool qt_dbus_metaobject_skip_annotations = false;
133 QDBusMetaObjectGenerator::Type
134 QDBusMetaObjectGenerator::findType(const QByteArray &signature,
135 const QDBusIntrospection::Annotations &annotations,
136 const char *direction, int id)
138 struct QDBusRawTypeHandler {
139 static void destroy(void *)
141 qFatal("Cannot destroy placeholder type QDBusRawType");
144 static void *create(const void *)
146 qFatal("Cannot create placeholder type QDBusRawType");
150 static void destruct(void *)
152 qFatal("Cannot destruct placeholder type QDBusRawType");
155 static void *construct(void *, const void *)
157 qFatal("Cannot construct placeholder type QDBusRawType");
163 result.id = QVariant::Invalid;
165 int type = QDBusMetaType::signatureToType(signature);
166 if (type == QVariant::Invalid && !qt_dbus_metaobject_skip_annotations) {
167 // it's not a type normally handled by our meta type system
168 // it must contain an annotation
169 QString annotationName = QString::fromLatin1("com.trolltech.QtDBus.QtTypeName");
171 annotationName += QString::fromLatin1(".%1%2")
172 .arg(QLatin1String(direction))
175 // extract from annotations:
176 QByteArray typeName = annotations.value(annotationName).toLatin1();
178 // verify that it's a valid one
179 if (!typeName.isEmpty()) {
181 type = QMetaType::type(typeName);
184 if (type == QVariant::Invalid || signature != QDBusMetaType::typeToSignature(type)) {
185 // type is still unknown or doesn't match back to the signature that it
186 // was expected to, so synthesize a fake type
187 typeName = "QDBusRawType<0x" + signature.toHex() + ">*";
188 type = QMetaType::registerType(typeName, QDBusRawTypeHandler::destroy,
189 QDBusRawTypeHandler::create,
190 QDBusRawTypeHandler::destruct,
191 QDBusRawTypeHandler::construct,
193 QMetaType::MovableType);
196 result.name = typeName;
197 } else if (type == QVariant::Invalid) {
198 // this case is used only by the qdbus command-line tool
199 // invalid, let's create an impossible type that contains the signature
201 if (signature == "av") {
202 result.name = "QVariantList";
203 type = QVariant::List;
204 } else if (signature == "a{sv}") {
205 result.name = "QVariantMap";
206 type = QVariant::Map;
208 result.name = "QDBusRawType::" + signature;
212 result.name = QVariant::typeToName( QVariant::Type(type) );
216 return result; // success
219 void QDBusMetaObjectGenerator::parseMethods()
223 // Add cloned methods when the remote object has return types
226 QDBusIntrospection::Methods::ConstIterator method_it = data->methods.constBegin();
227 QDBusIntrospection::Methods::ConstIterator method_end = data->methods.constEnd();
228 for ( ; method_it != method_end; ++method_it) {
229 const QDBusIntrospection::Method &m = *method_it;
232 mm.name = m.name.toLatin1();
233 QByteArray prototype = mm.name;
238 // build the input argument list
239 for (int i = 0; i < m.inputArgs.count(); ++i) {
240 const QDBusIntrospection::Argument &arg = m.inputArgs.at(i);
242 Type type = findType(arg.type.toLatin1(), m.annotations, "In", i);
243 if (type.id == QVariant::Invalid) {
248 mm.inputTypes.append(type.id);
250 mm.parameterNames.append(arg.name.toLatin1());
252 prototype.append(type.name);
253 prototype.append(',');
257 // build the output argument list:
258 for (int i = 0; i < m.outputArgs.count(); ++i) {
259 const QDBusIntrospection::Argument &arg = m.outputArgs.at(i);
261 Type type = findType(arg.type.toLatin1(), m.annotations, "Out", i);
262 if (type.id == QVariant::Invalid) {
267 mm.outputTypes.append(type.id);
271 mm.typeName = type.name;
273 // non-const ref parameter
274 mm.parameterNames.append(arg.name.toLatin1());
276 prototype.append(type.name);
277 prototype.append("&,");
282 // convert the last commas:
283 if (!mm.parameterNames.isEmpty())
284 prototype[prototype.length() - 1] = ')';
286 prototype.append(')');
288 // check the async tag
289 if (m.annotations.value(QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true"))
290 mm.tag = "Q_NOREPLY";
293 mm.flags = AccessPublic | MethodSlot | MethodScriptable;
296 methods.insert(QMetaObject::normalizedSignature(prototype), mm);
300 void QDBusMetaObjectGenerator::parseSignals()
302 QDBusIntrospection::Signals::ConstIterator signal_it = data->signals_.constBegin();
303 QDBusIntrospection::Signals::ConstIterator signal_end = data->signals_.constEnd();
304 for ( ; signal_it != signal_end; ++signal_it) {
305 const QDBusIntrospection::Signal &s = *signal_it;
308 mm.name = s.name.toLatin1();
309 QByteArray prototype = mm.name;
314 // build the output argument list
315 for (int i = 0; i < s.outputArgs.count(); ++i) {
316 const QDBusIntrospection::Argument &arg = s.outputArgs.at(i);
318 Type type = findType(arg.type.toLatin1(), s.annotations, "Out", i);
319 if (type.id == QVariant::Invalid) {
324 mm.inputTypes.append(type.id);
326 mm.parameterNames.append(arg.name.toLatin1());
328 prototype.append(type.name);
329 prototype.append(',');
333 // convert the last commas:
334 if (!mm.parameterNames.isEmpty())
335 prototype[prototype.length() - 1] = ')';
337 prototype.append(')');
340 mm.flags = AccessProtected | MethodSignal | MethodScriptable;
343 signals_.insert(QMetaObject::normalizedSignature(prototype), mm);
347 void QDBusMetaObjectGenerator::parseProperties()
349 QDBusIntrospection::Properties::ConstIterator prop_it = data->properties.constBegin();
350 QDBusIntrospection::Properties::ConstIterator prop_end = data->properties.constEnd();
351 for ( ; prop_it != prop_end; ++prop_it) {
352 const QDBusIntrospection::Property &p = *prop_it;
354 Type type = findType(p.type.toLatin1(), p.annotations);
355 if (type.id == QVariant::Invalid)
358 QByteArray name = p.name.toLatin1();
359 mp.signature = p.type.toLatin1();
361 mp.typeName = type.name;
364 mp.flags = StdCppSet | Scriptable | Stored | Designable;
365 if (p.access != QDBusIntrospection::Property::Write)
366 mp.flags |= Readable;
367 if (p.access != QDBusIntrospection::Property::Read)
368 mp.flags |= Writable;
371 properties.insert(name, mp);
375 // Returns the sum of all parameters (including return type) for the given
376 // \a map of methods. This is needed for calculating the size of the methods'
377 // parameter type/name meta-data.
378 int QDBusMetaObjectGenerator::aggregateParameterCount(const QMap<QByteArray, Method> &map)
381 QMap<QByteArray, Method>::const_iterator it;
382 for (it = map.constBegin(); it != map.constEnd(); ++it) {
383 const Method &m = it.value();
384 sum += m.inputTypes.size() + qMax(1, m.outputTypes.size());
389 void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj)
391 // this code here is mostly copied from qaxbase.cpp
392 // with a few modifications to make it cleaner
394 QString className = interface;
395 className.replace(QLatin1Char('.'), QLatin1String("::"));
396 if (className.isEmpty())
397 className = QLatin1String("QDBusInterface");
399 QVarLengthArray<int> idata;
400 idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(int));
402 int methodParametersDataSize =
403 ((aggregateParameterCount(signals_)
404 + aggregateParameterCount(methods)) * 2) // types and parameter names
405 - signals_.count() // return "parameters" don't have names
406 - methods.count(); // ditto
408 QDBusMetaObjectPrivate *header = reinterpret_cast<QDBusMetaObjectPrivate *>(idata.data());
409 Q_STATIC_ASSERT_X(QMetaObjectPrivate::OutputRevision == 7, "QtDBus meta-object generator should generate the same version as moc");
410 header->revision = QMetaObjectPrivate::OutputRevision;
411 header->className = 0;
412 header->classInfoCount = 0;
413 header->classInfoData = 0;
414 header->methodCount = signals_.count() + methods.count();
415 header->methodData = idata.size();
416 header->propertyCount = properties.count();
417 header->propertyData = header->methodData + header->methodCount * 5 + methodParametersDataSize;
418 header->enumeratorCount = 0;
419 header->enumeratorData = 0;
420 header->constructorCount = 0;
421 header->constructorData = 0;
422 header->flags = RequiresVariantMetaObject;
423 header->signalCount = signals_.count();
424 // These are specific to QDBusMetaObject:
425 header->propertyDBusData = header->propertyData + header->propertyCount * 3;
426 header->methodDBusData = header->propertyDBusData + header->propertyCount * intsPerProperty;
428 int data_size = idata.size() +
429 (header->methodCount * (5+intsPerMethod)) + methodParametersDataSize +
430 (header->propertyCount * (3+intsPerProperty));
431 foreach (const Method &mm, signals_)
432 data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count();
433 foreach (const Method &mm, methods)
434 data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count();
435 idata.resize(data_size + 1);
437 QMetaStringTable strings;
438 strings.enter(className.toLatin1());
440 int offset = header->methodData;
441 int parametersOffset = offset + header->methodCount * 5;
442 int signatureOffset = header->methodDBusData;
443 int typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod;
444 idata[typeidOffset++] = 0; // eod
447 for (int x = 0; x < 2; ++x) {
448 // Signals must be added before other methods, to match moc.
449 QMap<QByteArray, Method> &map = (x == 0) ? signals_ : methods;
450 for (QMap<QByteArray, Method>::ConstIterator it = map.constBegin();
451 it != map.constEnd(); ++it) {
452 const Method &mm = it.value();
454 int argc = mm.inputTypes.size() + qMax(0, mm.outputTypes.size() - 1);
456 idata[offset++] = strings.enter(mm.name);
457 idata[offset++] = argc;
458 idata[offset++] = parametersOffset;
459 idata[offset++] = strings.enter(mm.tag);
460 idata[offset++] = mm.flags;
463 for (int i = -1; i < argc; ++i) {
466 if (i < 0) { // Return type
467 if (!mm.outputTypes.isEmpty())
468 type = mm.outputTypes.first();
470 type = QMetaType::Void;
471 } else if (i < mm.inputTypes.size()) {
472 type = mm.inputTypes.at(i);
474 Q_ASSERT(mm.outputTypes.size() > 1);
475 type = mm.outputTypes.at(i - mm.inputTypes.size() + 1);
476 // Output parameters are references; type id not available
477 typeName = QMetaType::typeName(type);
478 typeName.append('&');
480 Q_ASSERT(type || (i < 0));
482 if (!typeName.isEmpty())
483 typeInfo = IsUnresolvedType | strings.enter(typeName);
486 idata[parametersOffset++] = typeInfo;
489 for (int i = 0; i < argc; ++i)
490 idata[parametersOffset++] = strings.enter(mm.parameterNames.at(i));
492 idata[signatureOffset++] = typeidOffset;
493 idata[typeidOffset++] = mm.inputTypes.count();
494 memcpy(idata.data() + typeidOffset, mm.inputTypes.data(), mm.inputTypes.count() * sizeof(int));
495 typeidOffset += mm.inputTypes.count();
497 idata[signatureOffset++] = typeidOffset;
498 idata[typeidOffset++] = mm.outputTypes.count();
499 memcpy(idata.data() + typeidOffset, mm.outputTypes.data(), mm.outputTypes.count() * sizeof(int));
500 typeidOffset += mm.outputTypes.count();
504 Q_ASSERT(offset == header->methodData + header->methodCount * 5);
505 Q_ASSERT(parametersOffset = header->propertyData);
506 Q_ASSERT(signatureOffset == header->methodDBusData + header->methodCount * intsPerMethod);
507 Q_ASSERT(typeidOffset == idata.size());
508 offset += methodParametersDataSize;
509 Q_ASSERT(offset == header->propertyData);
512 signatureOffset = header->propertyDBusData;
513 for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin();
514 it != properties.constEnd(); ++it) {
515 const Property &mp = it.value();
517 // form is name, typeinfo, flags
518 idata[offset++] = strings.enter(it.key()); // name
519 Q_ASSERT(mp.type != 0);
520 idata[offset++] = mp.type;
521 idata[offset++] = mp.flags;
523 idata[signatureOffset++] = strings.enter(mp.signature);
524 idata[signatureOffset++] = mp.type;
527 Q_ASSERT(offset == header->propertyDBusData);
528 Q_ASSERT(signatureOffset == header->methodDBusData);
530 char *string_data = new char[strings.blobSize()];
531 strings.writeBlob(string_data);
533 uint *uint_data = new uint[idata.size()];
534 memcpy(uint_data, idata.data(), idata.size() * sizeof(int));
536 // put the metaobject together
537 obj->d.data = uint_data;
538 obj->d.extradata = 0;
539 obj->d.stringdata = reinterpret_cast<const QByteArrayData *>(string_data);
540 obj->d.superdata = &QDBusAbstractInterface::staticMetaObject;
544 void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface)
547 QString tmp(interface);
548 tmp.replace(QLatin1Char('.'), QLatin1String("::"));
549 QByteArray name(tmp.toLatin1());
551 QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate;
552 memset(header, 0, sizeof *header);
553 header->revision = 1;
554 // leave the rest with 0
556 char *stringdata = new char[name.length() + 1];
557 stringdata[name.length()] = '\0';
559 d.data = reinterpret_cast<uint*>(header);
561 d.stringdata = stringdata;
562 d.superdata = &QDBusAbstractInterface::staticMetaObject;
568 // class QDBusMetaObject
570 QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, const QString &xml,
571 QHash<QString, QDBusMetaObject *> &cache,
574 error = QDBusError();
575 QDBusIntrospection::Interfaces parsed = QDBusIntrospection::parseInterfaces(xml);
577 QDBusMetaObject *we = 0;
578 QDBusIntrospection::Interfaces::ConstIterator it = parsed.constBegin();
579 QDBusIntrospection::Interfaces::ConstIterator end = parsed.constEnd();
580 for ( ; it != end; ++it) {
581 // check if it's in the cache
582 bool us = it.key() == interface;
584 QDBusMetaObject *obj = cache.value(it.key(), 0);
585 if ( !obj && ( us || !interface.startsWith( QLatin1String("local.") ) ) ) {
586 // not in cache; create
587 obj = new QDBusMetaObject;
588 QDBusMetaObjectGenerator generator(it.key(), it.value().constData());
589 generator.write(obj);
591 if ( (obj->cached = !it.key().startsWith( QLatin1String("local.") )) )
593 cache.insert(it.key(), obj);
608 if (parsed.isEmpty()) {
609 // object didn't return introspection
610 we = new QDBusMetaObject;
611 QDBusMetaObjectGenerator generator(interface, 0);
615 } else if (interface.isEmpty()) {
616 // merge all interfaces
617 it = parsed.constBegin();
618 QDBusIntrospection::Interface merged = *it.value().constData();
620 for (++it; it != end; ++it) {
621 merged.annotations.unite(it.value()->annotations);
622 merged.methods.unite(it.value()->methods);
623 merged.signals_.unite(it.value()->signals_);
624 merged.properties.unite(it.value()->properties);
627 merged.name = QLatin1String("local.Merged");
628 merged.introspection.clear();
630 we = new QDBusMetaObject;
631 QDBusMetaObjectGenerator generator(merged.name, &merged);
638 error = QDBusError(QDBusError::UnknownInterface,
639 QString::fromLatin1("Interface '%1' was not found")
644 QDBusMetaObject::QDBusMetaObject()
648 static inline const QDBusMetaObjectPrivate *priv(const uint* data)
650 return reinterpret_cast<const QDBusMetaObjectPrivate *>(data);
653 const int *QDBusMetaObject::inputTypesForMethod(int id) const
655 //id -= methodOffset();
656 if (id >= 0 && id < priv(d.data)->methodCount) {
657 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
658 return reinterpret_cast<const int*>(d.data + d.data[handle]);
663 const int *QDBusMetaObject::outputTypesForMethod(int id) const
665 //id -= methodOffset();
666 if (id >= 0 && id < priv(d.data)->methodCount) {
667 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
668 return reinterpret_cast<const int*>(d.data + d.data[handle + 1]);
673 int QDBusMetaObject::propertyMetaType(int id) const
675 //id -= propertyOffset();
676 if (id >= 0 && id < priv(d.data)->propertyCount) {
677 int handle = priv(d.data)->propertyDBusData + id*intsPerProperty;
678 return d.data[handle + 1];