7b8a67f34373da99cefa9d0376d30bf8ffa0109e
[profile/ivi/qtbase.git] / src / dbus / qdbusmetaobject.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDBus module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdbusmetaobject_p.h"
43
44 #include <QtCore/qbytearray.h>
45 #include <QtCore/qhash.h>
46 #include <QtCore/qstring.h>
47 #include <QtCore/qvarlengtharray.h>
48
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"
55
56 #include <private/qmetaobject_p.h>
57 #include <private/qmetaobjectbuilder_p.h>
58
59 #ifndef QT_NO_DBUS
60
61 QT_BEGIN_NAMESPACE
62
63 class QDBusMetaObjectGenerator
64 {
65 public:
66     QDBusMetaObjectGenerator(const QString &interface,
67                              const QDBusIntrospection::Interface *parsedData);
68     void write(QDBusMetaObject *obj);
69     void writeWithoutXml(QDBusMetaObject *obj);
70
71 private:
72     struct Method {
73         QList<QByteArray> parameterNames;
74         QByteArray typeName;
75         QByteArray tag;
76         QByteArray name;
77         QVarLengthArray<int, 4> inputTypes;
78         QVarLengthArray<int, 4> outputTypes;
79         int flags;
80     };
81     
82     struct Property {
83         QByteArray typeName;
84         QByteArray signature;
85         int type;
86         int flags;
87     };
88     struct Type {
89         int id;
90         QByteArray name;
91     };
92
93     QMap<QByteArray, Method> signals_;
94     QMap<QByteArray, Method> methods;
95     QMap<QByteArray, Property> properties;
96     
97     const QDBusIntrospection::Interface *data;
98     QString interface;
99
100     Type findType(const QByteArray &signature,
101                   const QDBusIntrospection::Annotations &annotations,
102                   const char *direction = "Out", int id = -1);
103     
104     void parseMethods();
105     void parseSignals();
106     void parseProperties();
107
108     static int aggregateParameterCount(const QMap<QByteArray, Method> &map);
109 };
110
111 static const int intsPerProperty = 2;
112 static const int intsPerMethod = 2;
113
114 struct QDBusMetaObjectPrivate : public QMetaObjectPrivate
115 {
116     int propertyDBusData;
117     int methodDBusData;
118 };
119
120 QDBusMetaObjectGenerator::QDBusMetaObjectGenerator(const QString &interfaceName,
121                                                    const QDBusIntrospection::Interface *parsedData)
122     : data(parsedData), interface(interfaceName)
123 {
124     if (data) {
125         parseProperties();
126         parseSignals();             // call parseSignals first so that slots override signals
127         parseMethods();
128     }
129 }
130
131 Q_DBUS_EXPORT bool qt_dbus_metaobject_skip_annotations = false;
132
133 QDBusMetaObjectGenerator::Type
134 QDBusMetaObjectGenerator::findType(const QByteArray &signature,
135                                    const QDBusIntrospection::Annotations &annotations,
136                                    const char *direction, int id)
137 {
138     struct QDBusRawTypeHandler {
139         static void destroy(void *)
140         {
141             qFatal("Cannot destroy placeholder type QDBusRawType");
142         }
143
144         static void *create(const void *)
145         {
146             qFatal("Cannot create placeholder type QDBusRawType");
147             return 0;
148         }
149
150         static void destruct(void *)
151         {
152             qFatal("Cannot destruct placeholder type QDBusRawType");
153         }
154
155         static void *construct(void *, const void *)
156         {
157             qFatal("Cannot construct placeholder type QDBusRawType");
158             return 0;
159         }
160     };
161
162     Type result;
163     result.id = QVariant::Invalid;
164
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");
170         if (id >= 0)
171             annotationName += QString::fromLatin1(".%1%2")
172                               .arg(QLatin1String(direction))
173                               .arg(id);
174
175         // extract from annotations:
176         QByteArray typeName = annotations.value(annotationName).toLatin1();
177
178         // verify that it's a valid one
179         if (!typeName.isEmpty()) {
180             // type name found
181             type = QMetaType::type(typeName);
182         }
183
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,
192                                            sizeof(void *),
193                                            QMetaType::MovableType);
194         }
195
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
200
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;
207         } else {
208             result.name = "QDBusRawType::" + signature;
209             type = -1;
210         }
211     } else {
212         result.name = QVariant::typeToName( QVariant::Type(type) );
213     }
214
215     result.id = type;
216     return result;              // success
217 }
218
219 void QDBusMetaObjectGenerator::parseMethods()
220 {
221     //
222     // TODO:
223     //  Add cloned methods when the remote object has return types
224     //
225
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;
230         Method mm;
231
232         mm.name = m.name.toLatin1();
233         QByteArray prototype = mm.name;
234         prototype += '(';
235
236         bool ok = true;
237
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);
241
242             Type type = findType(arg.type.toLatin1(), m.annotations, "In", i);
243             if (type.id == QVariant::Invalid) {
244                 ok = false;
245                 break;
246             }
247
248             mm.inputTypes.append(type.id);
249
250             mm.parameterNames.append(arg.name.toLatin1());
251             
252             prototype.append(type.name);
253             prototype.append(',');
254         }
255         if (!ok) continue;
256
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);
260
261             Type type = findType(arg.type.toLatin1(), m.annotations, "Out", i);
262             if (type.id == QVariant::Invalid) {
263                 ok = false;
264                 break;
265             }
266
267             mm.outputTypes.append(type.id);
268
269             if (i == 0) {
270                 // return value
271                 mm.typeName = type.name;
272             } else {
273                 // non-const ref parameter
274                 mm.parameterNames.append(arg.name.toLatin1());
275
276                 prototype.append(type.name);
277                 prototype.append("&,");
278             }
279         }
280         if (!ok) continue;
281
282         // convert the last commas:
283         if (!mm.parameterNames.isEmpty())
284             prototype[prototype.length() - 1] = ')';
285         else
286             prototype.append(')');
287
288         // check the async tag
289         if (m.annotations.value(QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true"))
290             mm.tag = "Q_NOREPLY";
291
292         // meta method flags
293         mm.flags = AccessPublic | MethodSlot | MethodScriptable;
294
295         // add
296         methods.insert(QMetaObject::normalizedSignature(prototype), mm);
297     }
298 }
299
300 void QDBusMetaObjectGenerator::parseSignals()
301 {
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;
306         Method mm;
307
308         mm.name = s.name.toLatin1();
309         QByteArray prototype = mm.name;
310         prototype += '(';
311
312         bool ok = true;
313
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);
317
318             Type type = findType(arg.type.toLatin1(), s.annotations, "Out", i);
319             if (type.id == QVariant::Invalid) {
320                 ok = false;
321                 break;
322             }
323
324             mm.inputTypes.append(type.id);
325
326             mm.parameterNames.append(arg.name.toLatin1());
327             
328             prototype.append(type.name);
329             prototype.append(',');
330         }
331         if (!ok) continue;
332
333         // convert the last commas:
334         if (!mm.parameterNames.isEmpty())
335             prototype[prototype.length() - 1] = ')';
336         else
337             prototype.append(')');
338
339         // meta method flags
340         mm.flags = AccessProtected | MethodSignal | MethodScriptable;
341
342         // add
343         signals_.insert(QMetaObject::normalizedSignature(prototype), mm);
344     }
345 }
346
347 void QDBusMetaObjectGenerator::parseProperties()
348 {
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;
353         Property mp;
354         Type type = findType(p.type.toLatin1(), p.annotations);
355         if (type.id == QVariant::Invalid)
356             continue;
357         
358         QByteArray name = p.name.toLatin1();
359         mp.signature = p.type.toLatin1();
360         mp.type = type.id;
361         mp.typeName = type.name;
362
363         // build the flags:
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;
369
370         // add the property:
371         properties.insert(name, mp);
372     }
373 }
374
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)
379 {
380     int sum = 0;
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());
385     }
386     return sum;
387 }
388
389 void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj)
390 {
391     // this code here is mostly copied from qaxbase.cpp
392     // with a few modifications to make it cleaner
393
394     QString className = interface;
395     className.replace(QLatin1Char('.'), QLatin1String("::"));
396     if (className.isEmpty())
397         className = QLatin1String("QDBusInterface");
398
399     QVarLengthArray<int> idata;
400     idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(int));
401
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
407
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;
427
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);
436
437     QMetaStringTable strings;
438     strings.enter(className.toLatin1());
439
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
445
446     // add each method:
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();
453
454             int argc = mm.inputTypes.size() + qMax(0, mm.outputTypes.size() - 1);
455
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;
461
462             // Parameter types
463             for (int i = -1; i < argc; ++i) {
464                 int type;
465                 QByteArray typeName;
466                 if (i < 0) { // Return type
467                     if (!mm.outputTypes.isEmpty())
468                         type = mm.outputTypes.first();
469                     else
470                         type = QMetaType::Void;
471                 } else if (i < mm.inputTypes.size()) {
472                     type = mm.inputTypes.at(i);
473                 } else {
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('&');
479                 }
480                 Q_ASSERT(type || (i < 0));
481                 int typeInfo;
482                 if (!typeName.isEmpty())
483                     typeInfo = IsUnresolvedType | strings.enter(typeName);
484                 else
485                     typeInfo = type;
486                 idata[parametersOffset++] = typeInfo;
487             }
488             // Parameter names
489             for (int i = 0; i < argc; ++i)
490                 idata[parametersOffset++] = strings.enter(mm.parameterNames.at(i));
491
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();
496
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();
501         }
502     }
503
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);
510
511     // add each property
512     signatureOffset = header->propertyDBusData;
513     for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin();
514          it != properties.constEnd(); ++it) {
515         const Property &mp = it.value();
516
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;
522
523         idata[signatureOffset++] = strings.enter(mp.signature);
524         idata[signatureOffset++] = mp.type;
525     }
526
527     Q_ASSERT(offset == header->propertyDBusData);
528     Q_ASSERT(signatureOffset == header->methodDBusData);
529
530     char *string_data = new char[strings.blobSize()];
531     strings.writeBlob(string_data);
532
533     uint *uint_data = new uint[idata.size()];
534     memcpy(uint_data, idata.data(), idata.size() * sizeof(int));
535
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;
541 }
542
543 #if 0
544 void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface)
545 {
546     // no XML definition
547     QString tmp(interface);
548     tmp.replace(QLatin1Char('.'), QLatin1String("::"));
549     QByteArray name(tmp.toLatin1());
550
551     QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate;
552     memset(header, 0, sizeof *header);
553     header->revision = 1;
554     // leave the rest with 0
555
556     char *stringdata = new char[name.length() + 1];
557     stringdata[name.length()] = '\0';
558     
559     d.data = reinterpret_cast<uint*>(header);
560     d.extradata = 0;
561     d.stringdata = stringdata;
562     d.superdata = &QDBusAbstractInterface::staticMetaObject;
563     cached = false;
564 }
565 #endif
566
567 /////////
568 // class QDBusMetaObject
569
570 QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, const QString &xml,
571                                                    QHash<QString, QDBusMetaObject *> &cache,
572                                                    QDBusError &error)
573 {
574     error = QDBusError();
575     QDBusIntrospection::Interfaces parsed = QDBusIntrospection::parseInterfaces(xml);
576
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;
583
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);
590
591             if ( (obj->cached = !it.key().startsWith( QLatin1String("local.") )) )
592                 // cache it
593                 cache.insert(it.key(), obj);
594             else if (!us)
595                 delete obj;
596
597         }
598
599         if (us)
600             // it's us
601             we = obj;
602     }
603
604     if (we)
605         return we;
606     // still nothing?
607     
608     if (parsed.isEmpty()) {
609         // object didn't return introspection
610         we = new QDBusMetaObject;
611         QDBusMetaObjectGenerator generator(interface, 0);
612         generator.write(we);
613         we->cached = false;
614         return we;
615     } else if (interface.isEmpty()) {
616         // merge all interfaces
617         it = parsed.constBegin();
618         QDBusIntrospection::Interface merged = *it.value().constData();
619  
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);
625         }
626
627         merged.name = QLatin1String("local.Merged");
628         merged.introspection.clear();
629
630         we = new QDBusMetaObject;
631         QDBusMetaObjectGenerator generator(merged.name, &merged);
632         generator.write(we);
633         we->cached = false;
634         return we;
635     }
636
637     // mark as an error
638     error = QDBusError(QDBusError::UnknownInterface,
639         QString::fromLatin1("Interface '%1' was not found")
640                        .arg(interface));
641     return 0;
642 }
643
644 QDBusMetaObject::QDBusMetaObject()
645 {
646 }
647
648 static inline const QDBusMetaObjectPrivate *priv(const uint* data)
649 {
650     return reinterpret_cast<const QDBusMetaObjectPrivate *>(data);
651 }
652
653 const int *QDBusMetaObject::inputTypesForMethod(int id) const
654 {
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]);
659     }
660     return 0;
661 }
662
663 const int *QDBusMetaObject::outputTypesForMethod(int id) const
664 {
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]);
669     }
670     return 0;
671 }
672
673 int QDBusMetaObject::propertyMetaType(int id) const
674 {
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];
679     }
680     return 0;
681 }
682
683 QT_END_NAMESPACE
684
685 #endif // QT_NO_DBUS