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;
76 QVarLengthArray<int, 4> inputTypes;
77 QVarLengthArray<int, 4> outputTypes;
92 QMap<QByteArray, Method> signals_;
93 QMap<QByteArray, Method> methods;
94 QMap<QByteArray, Property> properties;
96 const QDBusIntrospection::Interface *data;
99 Type findType(const QByteArray &signature,
100 const QDBusIntrospection::Annotations &annotations,
101 const char *direction = "Out", int id = -1);
105 void parseProperties();
107 static int aggregateParameterCount(const QMap<QByteArray, Method> &map);
110 static const int intsPerProperty = 2;
111 static const int intsPerMethod = 2;
113 struct QDBusMetaObjectPrivate : public QMetaObjectPrivate
115 int propertyDBusData;
119 QDBusMetaObjectGenerator::QDBusMetaObjectGenerator(const QString &interfaceName,
120 const QDBusIntrospection::Interface *parsedData)
121 : data(parsedData), interface(interfaceName)
125 parseSignals(); // call parseSignals first so that slots override signals
130 Q_DBUS_EXPORT bool qt_dbus_metaobject_skip_annotations = false;
132 QDBusMetaObjectGenerator::Type
133 QDBusMetaObjectGenerator::findType(const QByteArray &signature,
134 const QDBusIntrospection::Annotations &annotations,
135 const char *direction, int id)
137 struct QDBusRawTypeHandler {
138 static void destroy(void *)
140 qFatal("Cannot destroy placeholder type QDBusRawType");
143 static void *create(const void *)
145 qFatal("Cannot create placeholder type QDBusRawType");
149 static void destruct(void *)
151 qFatal("Cannot destruct placeholder type QDBusRawType");
154 static void *construct(void *, const void *)
156 qFatal("Cannot construct placeholder type QDBusRawType");
162 result.id = QVariant::Invalid;
164 int type = QDBusMetaType::signatureToType(signature);
165 if (type == QVariant::Invalid && !qt_dbus_metaobject_skip_annotations) {
166 // it's not a type normally handled by our meta type system
167 // it must contain an annotation
168 QString annotationName = QString::fromLatin1("org.qtproject.QtDBus.QtTypeName");
170 annotationName += QString::fromLatin1(".%1%2")
171 .arg(QLatin1String(direction))
174 // extract from annotations:
175 QByteArray typeName = annotations.value(annotationName).toLatin1();
177 // verify that it's a valid one
178 if (typeName.isEmpty()) {
179 // try the old annotation from Qt 4
180 annotationName = QString::fromLatin1("com.trolltech.QtDBus.QtTypeName");
182 annotationName += QString::fromLatin1(".%1%2")
183 .arg(QLatin1String(direction))
185 typeName = annotations.value(annotationName).toLatin1();
188 if (!typeName.isEmpty()) {
190 type = QMetaType::type(typeName);
193 if (type == QVariant::Invalid || signature != QDBusMetaType::typeToSignature(type)) {
194 // type is still unknown or doesn't match back to the signature that it
195 // was expected to, so synthesize a fake type
196 typeName = "QDBusRawType<0x" + signature.toHex() + ">*";
197 type = QMetaType::registerType(typeName, QDBusRawTypeHandler::destroy,
198 QDBusRawTypeHandler::create,
199 QDBusRawTypeHandler::destruct,
200 QDBusRawTypeHandler::construct,
202 QMetaType::MovableType,
206 result.name = typeName;
207 } else if (type == QVariant::Invalid) {
208 // this case is used only by the qdbus command-line tool
209 // invalid, let's create an impossible type that contains the signature
211 if (signature == "av") {
212 result.name = "QVariantList";
213 type = QVariant::List;
214 } else if (signature == "a{sv}") {
215 result.name = "QVariantMap";
216 type = QVariant::Map;
218 result.name = "QDBusRawType::" + signature;
222 result.name = QMetaType::typeName(type);
226 return result; // success
229 void QDBusMetaObjectGenerator::parseMethods()
233 // Add cloned methods when the remote object has return types
236 QDBusIntrospection::Methods::ConstIterator method_it = data->methods.constBegin();
237 QDBusIntrospection::Methods::ConstIterator method_end = data->methods.constEnd();
238 for ( ; method_it != method_end; ++method_it) {
239 const QDBusIntrospection::Method &m = *method_it;
242 mm.name = m.name.toLatin1();
243 QByteArray prototype = mm.name;
248 // build the input argument list
249 for (int i = 0; i < m.inputArgs.count(); ++i) {
250 const QDBusIntrospection::Argument &arg = m.inputArgs.at(i);
252 Type type = findType(arg.type.toLatin1(), m.annotations, "In", i);
253 if (type.id == QVariant::Invalid) {
258 mm.inputTypes.append(type.id);
260 mm.parameterNames.append(arg.name.toLatin1());
262 prototype.append(type.name);
263 prototype.append(',');
267 // build the output argument list:
268 for (int i = 0; i < m.outputArgs.count(); ++i) {
269 const QDBusIntrospection::Argument &arg = m.outputArgs.at(i);
271 Type type = findType(arg.type.toLatin1(), m.annotations, "Out", i);
272 if (type.id == QVariant::Invalid) {
277 mm.outputTypes.append(type.id);
280 // non-const ref parameter
281 mm.parameterNames.append(arg.name.toLatin1());
283 prototype.append(type.name);
284 prototype.append("&,");
289 // convert the last commas:
290 if (!mm.parameterNames.isEmpty())
291 prototype[prototype.length() - 1] = ')';
293 prototype.append(')');
295 // check the async tag
296 if (m.annotations.value(QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true"))
297 mm.tag = "Q_NOREPLY";
300 mm.flags = AccessPublic | MethodSlot | MethodScriptable;
303 methods.insert(QMetaObject::normalizedSignature(prototype), mm);
307 void QDBusMetaObjectGenerator::parseSignals()
309 QDBusIntrospection::Signals::ConstIterator signal_it = data->signals_.constBegin();
310 QDBusIntrospection::Signals::ConstIterator signal_end = data->signals_.constEnd();
311 for ( ; signal_it != signal_end; ++signal_it) {
312 const QDBusIntrospection::Signal &s = *signal_it;
315 mm.name = s.name.toLatin1();
316 QByteArray prototype = mm.name;
321 // build the output argument list
322 for (int i = 0; i < s.outputArgs.count(); ++i) {
323 const QDBusIntrospection::Argument &arg = s.outputArgs.at(i);
325 Type type = findType(arg.type.toLatin1(), s.annotations, "Out", i);
326 if (type.id == QVariant::Invalid) {
331 mm.inputTypes.append(type.id);
333 mm.parameterNames.append(arg.name.toLatin1());
335 prototype.append(type.name);
336 prototype.append(',');
340 // convert the last commas:
341 if (!mm.parameterNames.isEmpty())
342 prototype[prototype.length() - 1] = ')';
344 prototype.append(')');
347 mm.flags = AccessProtected | MethodSignal | MethodScriptable;
350 signals_.insert(QMetaObject::normalizedSignature(prototype), mm);
354 void QDBusMetaObjectGenerator::parseProperties()
356 QDBusIntrospection::Properties::ConstIterator prop_it = data->properties.constBegin();
357 QDBusIntrospection::Properties::ConstIterator prop_end = data->properties.constEnd();
358 for ( ; prop_it != prop_end; ++prop_it) {
359 const QDBusIntrospection::Property &p = *prop_it;
361 Type type = findType(p.type.toLatin1(), p.annotations);
362 if (type.id == QVariant::Invalid)
365 QByteArray name = p.name.toLatin1();
366 mp.signature = p.type.toLatin1();
368 mp.typeName = type.name;
371 mp.flags = StdCppSet | Scriptable | Stored | Designable;
372 if (p.access != QDBusIntrospection::Property::Write)
373 mp.flags |= Readable;
374 if (p.access != QDBusIntrospection::Property::Read)
375 mp.flags |= Writable;
378 properties.insert(name, mp);
382 // Returns the sum of all parameters (including return type) for the given
383 // \a map of methods. This is needed for calculating the size of the methods'
384 // parameter type/name meta-data.
385 int QDBusMetaObjectGenerator::aggregateParameterCount(const QMap<QByteArray, Method> &map)
388 QMap<QByteArray, Method>::const_iterator it;
389 for (it = map.constBegin(); it != map.constEnd(); ++it) {
390 const Method &m = it.value();
391 sum += m.inputTypes.size() + qMax(1, m.outputTypes.size());
396 void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj)
398 // this code here is mostly copied from qaxbase.cpp
399 // with a few modifications to make it cleaner
401 QString className = interface;
402 className.replace(QLatin1Char('.'), QLatin1String("::"));
403 if (className.isEmpty())
404 className = QLatin1String("QDBusInterface");
406 QVarLengthArray<int> idata;
407 idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(int));
409 int methodParametersDataSize =
410 ((aggregateParameterCount(signals_)
411 + aggregateParameterCount(methods)) * 2) // types and parameter names
412 - signals_.count() // return "parameters" don't have names
413 - methods.count(); // ditto
415 QDBusMetaObjectPrivate *header = reinterpret_cast<QDBusMetaObjectPrivate *>(idata.data());
416 Q_STATIC_ASSERT_X(QMetaObjectPrivate::OutputRevision == 7, "QtDBus meta-object generator should generate the same version as moc");
417 header->revision = QMetaObjectPrivate::OutputRevision;
418 header->className = 0;
419 header->classInfoCount = 0;
420 header->classInfoData = 0;
421 header->methodCount = signals_.count() + methods.count();
422 header->methodData = idata.size();
423 header->propertyCount = properties.count();
424 header->propertyData = header->methodData + header->methodCount * 5 + methodParametersDataSize;
425 header->enumeratorCount = 0;
426 header->enumeratorData = 0;
427 header->constructorCount = 0;
428 header->constructorData = 0;
429 header->flags = RequiresVariantMetaObject;
430 header->signalCount = signals_.count();
431 // These are specific to QDBusMetaObject:
432 header->propertyDBusData = header->propertyData + header->propertyCount * 3;
433 header->methodDBusData = header->propertyDBusData + header->propertyCount * intsPerProperty;
435 int data_size = idata.size() +
436 (header->methodCount * (5+intsPerMethod)) + methodParametersDataSize +
437 (header->propertyCount * (3+intsPerProperty));
438 foreach (const Method &mm, signals_)
439 data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count();
440 foreach (const Method &mm, methods)
441 data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count();
442 idata.resize(data_size + 1);
444 QMetaStringTable strings;
445 strings.enter(className.toLatin1());
447 int offset = header->methodData;
448 int parametersOffset = offset + header->methodCount * 5;
449 int signatureOffset = header->methodDBusData;
450 int typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod;
451 idata[typeidOffset++] = 0; // eod
454 for (int x = 0; x < 2; ++x) {
455 // Signals must be added before other methods, to match moc.
456 QMap<QByteArray, Method> &map = (x == 0) ? signals_ : methods;
457 for (QMap<QByteArray, Method>::ConstIterator it = map.constBegin();
458 it != map.constEnd(); ++it) {
459 const Method &mm = it.value();
461 int argc = mm.inputTypes.size() + qMax(0, mm.outputTypes.size() - 1);
463 idata[offset++] = strings.enter(mm.name);
464 idata[offset++] = argc;
465 idata[offset++] = parametersOffset;
466 idata[offset++] = strings.enter(mm.tag);
467 idata[offset++] = mm.flags;
470 for (int i = -1; i < argc; ++i) {
473 if (i < 0) { // Return type
474 if (!mm.outputTypes.isEmpty())
475 type = mm.outputTypes.first();
477 type = QMetaType::Void;
478 } else if (i < mm.inputTypes.size()) {
479 type = mm.inputTypes.at(i);
481 Q_ASSERT(mm.outputTypes.size() > 1);
482 type = mm.outputTypes.at(i - mm.inputTypes.size() + 1);
483 // Output parameters are references; type id not available
484 typeName = QMetaType::typeName(type);
485 typeName.append('&');
487 Q_ASSERT(type != QMetaType::UnknownType);
489 if (!typeName.isEmpty())
490 typeInfo = IsUnresolvedType | strings.enter(typeName);
493 idata[parametersOffset++] = typeInfo;
496 for (int i = 0; i < argc; ++i)
497 idata[parametersOffset++] = strings.enter(mm.parameterNames.at(i));
499 idata[signatureOffset++] = typeidOffset;
500 idata[typeidOffset++] = mm.inputTypes.count();
501 memcpy(idata.data() + typeidOffset, mm.inputTypes.data(), mm.inputTypes.count() * sizeof(int));
502 typeidOffset += mm.inputTypes.count();
504 idata[signatureOffset++] = typeidOffset;
505 idata[typeidOffset++] = mm.outputTypes.count();
506 memcpy(idata.data() + typeidOffset, mm.outputTypes.data(), mm.outputTypes.count() * sizeof(int));
507 typeidOffset += mm.outputTypes.count();
511 Q_ASSERT(offset == header->methodData + header->methodCount * 5);
512 Q_ASSERT(parametersOffset = header->propertyData);
513 Q_ASSERT(signatureOffset == header->methodDBusData + header->methodCount * intsPerMethod);
514 Q_ASSERT(typeidOffset == idata.size());
515 offset += methodParametersDataSize;
516 Q_ASSERT(offset == header->propertyData);
519 signatureOffset = header->propertyDBusData;
520 for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin();
521 it != properties.constEnd(); ++it) {
522 const Property &mp = it.value();
524 // form is name, typeinfo, flags
525 idata[offset++] = strings.enter(it.key()); // name
526 Q_ASSERT(mp.type != QMetaType::UnknownType);
527 idata[offset++] = mp.type;
528 idata[offset++] = mp.flags;
530 idata[signatureOffset++] = strings.enter(mp.signature);
531 idata[signatureOffset++] = mp.type;
534 Q_ASSERT(offset == header->propertyDBusData);
535 Q_ASSERT(signatureOffset == header->methodDBusData);
537 char *string_data = new char[strings.blobSize()];
538 strings.writeBlob(string_data);
540 uint *uint_data = new uint[idata.size()];
541 memcpy(uint_data, idata.data(), idata.size() * sizeof(int));
543 // put the metaobject together
544 obj->d.data = uint_data;
545 obj->d.relatedMetaObjects = 0;
546 obj->d.static_metacall = 0;
547 obj->d.extradata = 0;
548 obj->d.stringdata = reinterpret_cast<const QByteArrayData *>(string_data);
549 obj->d.superdata = &QDBusAbstractInterface::staticMetaObject;
553 void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface)
556 QString tmp(interface);
557 tmp.replace(QLatin1Char('.'), QLatin1String("::"));
558 QByteArray name(tmp.toLatin1());
560 QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate;
561 memset(header, 0, sizeof *header);
562 header->revision = 1;
563 // leave the rest with 0
565 char *stringdata = new char[name.length() + 1];
566 stringdata[name.length()] = '\0';
568 d.data = reinterpret_cast<uint*>(header);
569 d.relatedMetaObjects = 0;
570 d.static_metacall = 0;
572 d.stringdata = stringdata;
573 d.superdata = &QDBusAbstractInterface::staticMetaObject;
579 // class QDBusMetaObject
581 QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, const QString &xml,
582 QHash<QString, QDBusMetaObject *> &cache,
585 error = QDBusError();
586 QDBusIntrospection::Interfaces parsed = QDBusIntrospection::parseInterfaces(xml);
588 QDBusMetaObject *we = 0;
589 QDBusIntrospection::Interfaces::ConstIterator it = parsed.constBegin();
590 QDBusIntrospection::Interfaces::ConstIterator end = parsed.constEnd();
591 for ( ; it != end; ++it) {
592 // check if it's in the cache
593 bool us = it.key() == interface;
595 QDBusMetaObject *obj = cache.value(it.key(), 0);
596 if ( !obj && ( us || !interface.startsWith( QLatin1String("local.") ) ) ) {
597 // not in cache; create
598 obj = new QDBusMetaObject;
599 QDBusMetaObjectGenerator generator(it.key(), it.value().constData());
600 generator.write(obj);
602 if ( (obj->cached = !it.key().startsWith( QLatin1String("local.") )) )
604 cache.insert(it.key(), obj);
619 if (parsed.isEmpty()) {
620 // object didn't return introspection
621 we = new QDBusMetaObject;
622 QDBusMetaObjectGenerator generator(interface, 0);
626 } else if (interface.isEmpty()) {
627 // merge all interfaces
628 it = parsed.constBegin();
629 QDBusIntrospection::Interface merged = *it.value().constData();
631 for (++it; it != end; ++it) {
632 merged.annotations.unite(it.value()->annotations);
633 merged.methods.unite(it.value()->methods);
634 merged.signals_.unite(it.value()->signals_);
635 merged.properties.unite(it.value()->properties);
638 merged.name = QLatin1String("local.Merged");
639 merged.introspection.clear();
641 we = new QDBusMetaObject;
642 QDBusMetaObjectGenerator generator(merged.name, &merged);
649 error = QDBusError(QDBusError::UnknownInterface,
650 QString::fromLatin1("Interface '%1' was not found")
655 QDBusMetaObject::QDBusMetaObject()
659 static inline const QDBusMetaObjectPrivate *priv(const uint* data)
661 return reinterpret_cast<const QDBusMetaObjectPrivate *>(data);
664 const int *QDBusMetaObject::inputTypesForMethod(int id) const
666 //id -= methodOffset();
667 if (id >= 0 && id < priv(d.data)->methodCount) {
668 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
669 return reinterpret_cast<const int*>(d.data + d.data[handle]);
674 const int *QDBusMetaObject::outputTypesForMethod(int id) const
676 //id -= methodOffset();
677 if (id >= 0 && id < priv(d.data)->methodCount) {
678 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
679 return reinterpret_cast<const int*>(d.data + d.data[handle + 1]);
684 int QDBusMetaObject::propertyMetaType(int id) const
686 //id -= propertyOffset();
687 if (id >= 0 && id < priv(d.data)->propertyCount) {
688 int handle = priv(d.data)->propertyDBusData + id*intsPerProperty;
689 return d.data[handle + 1];