source: trunk/src/dbus/qdbusmetaobject.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 22.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
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#ifndef QT_NO_DBUS
57
58QT_BEGIN_NAMESPACE
59
60class QDBusMetaObjectGenerator
61{
62public:
63 QDBusMetaObjectGenerator(const QString &interface,
64 const QDBusIntrospection::Interface *parsedData);
65 void write(QDBusMetaObject *obj);
66 void writeWithoutXml(QDBusMetaObject *obj);
67
68private:
69 struct Method {
70 QByteArray parameters;
71 QByteArray typeName;
72 QByteArray tag;
73 QByteArray name;
74 QByteArray inputSignature;
75 QByteArray outputSignature;
76 QVarLengthArray<int, 4> inputTypes;
77 QVarLengthArray<int, 4> outputTypes;
78 int flags;
79 };
80
81 struct Property {
82 QByteArray typeName;
83 QByteArray signature;
84 int type;
85 int flags;
86 };
87 struct Type {
88 int id;
89 QByteArray name;
90 };
91
92 enum PropertyFlags {
93 Invalid = 0x00000000,
94 Readable = 0x00000001,
95 Writable = 0x00000002,
96 Resettable = 0x00000004,
97 EnumOrFlag = 0x00000008,
98 StdCppSet = 0x00000100,
99 // Override = 0x00000200,
100 Designable = 0x00001000,
101 ResolveDesignable = 0x00002000,
102 Scriptable = 0x00004000,
103 ResolveScriptable = 0x00008000,
104 Stored = 0x00010000,
105 ResolveStored = 0x00020000,
106 Editable = 0x00040000,
107 ResolveEditable = 0x00080000,
108 User = 0x00100000,
109 ResolveUser = 0x00200000
110 };
111
112 enum MethodFlags {
113 AccessPrivate = 0x00,
114 AccessProtected = 0x01,
115 AccessPublic = 0x02,
116 AccessMask = 0x03, //mask
117
118 MethodMethod = 0x00,
119 MethodSignal = 0x04,
120 MethodSlot = 0x08,
121 MethodTypeMask = 0x0c,
122
123 MethodCompatibility = 0x10,
124 MethodCloned = 0x20,
125 MethodScriptable = 0x40
126 };
127
128 QMap<QByteArray, Method> methods;
129 QMap<QByteArray, Property> properties;
130
131 const QDBusIntrospection::Interface *data;
132 QString interface;
133
134 Type findType(const QByteArray &signature,
135 const QDBusIntrospection::Annotations &annotations,
136 const char *direction = "Out", int id = -1);
137
138 void parseMethods();
139 void parseSignals();
140 void parseProperties();
141};
142
143static const int intsPerProperty = 2;
144static const int intsPerMethod = 5;
145
146// ### from kernel/qmetaobject.cpp (Qt 4.1.2):
147struct QDBusMetaObjectPrivate
148{
149 int revision;
150 int className;
151 int classInfoCount, classInfoData;
152 int methodCount, methodData;
153 int propertyCount, propertyData;
154 int enumeratorCount, enumeratorData;
155
156 // this is specific for QDBusMetaObject:
157 int propertyDBusData;
158 int methodDBusData;
159};
160
161QDBusMetaObjectGenerator::QDBusMetaObjectGenerator(const QString &interfaceName,
162 const QDBusIntrospection::Interface *parsedData)
163 : data(parsedData), interface(interfaceName)
164{
165 if (data) {
166 parseProperties();
167 parseSignals(); // call parseSignals first so that slots override signals
168 parseMethods();
169 }
170}
171
172QDBusMetaObjectGenerator::Type
173QDBusMetaObjectGenerator::findType(const QByteArray &signature,
174 const QDBusIntrospection::Annotations &annotations,
175 const char *direction, int id)
176{
177 Type result;
178 result.id = QVariant::Invalid;
179
180 int type = QDBusMetaType::signatureToType(signature);
181 if (type == QVariant::Invalid) {
182 // it's not a type normally handled by our meta type system
183 // it must contain an annotation
184 QString annotationName = QString::fromLatin1("com.trolltech.QtDBus.QtTypeName");
185 if (id >= 0)
186 annotationName += QString::fromLatin1(".%1%2")
187 .arg(QLatin1String(direction))
188 .arg(id);
189
190 // extract from annotations:
191 QByteArray typeName = annotations.value(annotationName).toLatin1();
192
193 // verify that it's a valid one
194 if (typeName.isEmpty())
195 return result; // invalid
196
197 type = QVariant::nameToType(typeName);
198 if (type == QVariant::UserType)
199 type = QMetaType::type(typeName);
200 if (type == QVariant::Invalid || signature != QDBusMetaType::typeToSignature(type))
201 return result; // unknown type is invalid too
202
203 result.name = typeName;
204 } else {
205 result.name = QVariant::typeToName( QVariant::Type(type) );
206 }
207
208 result.id = type;
209 return result; // success
210}
211
212void QDBusMetaObjectGenerator::parseMethods()
213{
214 //
215 // TODO:
216 // Add cloned methods when the remote object has return types
217 //
218
219 QDBusIntrospection::Methods::ConstIterator method_it = data->methods.constBegin();
220 QDBusIntrospection::Methods::ConstIterator method_end = data->methods.constEnd();
221 for ( ; method_it != method_end; ++method_it) {
222 const QDBusIntrospection::Method &m = *method_it;
223 Method mm;
224
225 mm.name = m.name.toLatin1();
226 QByteArray prototype = mm.name;
227 prototype += '(';
228
229 bool ok = true;
230
231 // build the input argument list
232 for (int i = 0; i < m.inputArgs.count(); ++i) {
233 const QDBusIntrospection::Argument &arg = m.inputArgs.at(i);
234
235 Type type = findType(arg.type.toLatin1(), m.annotations, "In", i);
236 if (type.id == QVariant::Invalid) {
237 ok = false;
238 break;
239 }
240
241 mm.inputSignature += arg.type.toLatin1();
242 mm.inputTypes.append(type.id);
243
244 mm.parameters.append(arg.name.toLatin1());
245 mm.parameters.append(',');
246
247 prototype.append(type.name);
248 prototype.append(',');
249 }
250 if (!ok) continue;
251
252 // build the output argument list:
253 for (int i = 0; i < m.outputArgs.count(); ++i) {
254 const QDBusIntrospection::Argument &arg = m.outputArgs.at(i);
255
256 Type type = findType(arg.type.toLatin1(), m.annotations, "Out", i);
257 if (type.id == QVariant::Invalid) {
258 ok = false;
259 break;
260 }
261
262 mm.outputSignature += arg.type.toLatin1();
263 mm.outputTypes.append(type.id);
264
265 if (i == 0) {
266 // return value
267 mm.typeName = type.name;
268 } else {
269 // non-const ref parameter
270 mm.parameters.append(arg.name.toLatin1());
271 mm.parameters.append(',');
272
273 prototype.append(type.name);
274 prototype.append("&,");
275 }
276 }
277 if (!ok) continue;
278
279 // convert the last commas:
280 if (!mm.parameters.isEmpty()) {
281 mm.parameters.truncate(mm.parameters.length() - 1);
282 prototype[prototype.length() - 1] = ')';
283 } else {
284 prototype.append(')');
285 }
286
287 // check the async tag
288 if (m.annotations.value(QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true"))
289 mm.tag = "Q_NOREPLY";
290
291 // meta method flags
292 mm.flags = AccessPublic | MethodSlot | MethodScriptable;
293
294 // add
295 methods.insert(QMetaObject::normalizedSignature(prototype), mm);
296 }
297}
298
299void QDBusMetaObjectGenerator::parseSignals()
300{
301 QDBusIntrospection::Signals::ConstIterator signal_it = data->signals_.constBegin();
302 QDBusIntrospection::Signals::ConstIterator signal_end = data->signals_.constEnd();
303 for ( ; signal_it != signal_end; ++signal_it) {
304 const QDBusIntrospection::Signal &s = *signal_it;
305 Method mm;
306
307 mm.name = s.name.toLatin1();
308 QByteArray prototype = mm.name;
309 prototype += '(';
310
311 bool ok = true;
312
313 // build the output argument list
314 for (int i = 0; i < s.outputArgs.count(); ++i) {
315 const QDBusIntrospection::Argument &arg = s.outputArgs.at(i);
316
317 Type type = findType(arg.type.toLatin1(), s.annotations, "Out", i);
318 if (type.id == QVariant::Invalid) {
319 ok = false;
320 break;
321 }
322
323 mm.inputSignature += arg.type.toLatin1();
324 mm.inputTypes.append(type.id);
325
326 mm.parameters.append(arg.name.toLatin1());
327 mm.parameters.append(',');
328
329 prototype.append(type.name);
330 prototype.append(',');
331 }
332 if (!ok) continue;
333
334 // convert the last commas:
335 if (!mm.parameters.isEmpty()) {
336 mm.parameters.truncate(mm.parameters.length() - 1);
337 prototype[prototype.length() - 1] = ')';
338 } else {
339 prototype.append(')');
340 }
341
342 // meta method flags
343 mm.flags = AccessProtected | MethodSignal | MethodScriptable;
344
345 // add
346 methods.insert(QMetaObject::normalizedSignature(prototype), mm);
347 }
348}
349
350void QDBusMetaObjectGenerator::parseProperties()
351{
352 QDBusIntrospection::Properties::ConstIterator prop_it = data->properties.constBegin();
353 QDBusIntrospection::Properties::ConstIterator prop_end = data->properties.constEnd();
354 for ( ; prop_it != prop_end; ++prop_it) {
355 const QDBusIntrospection::Property &p = *prop_it;
356 Property mp;
357 Type type = findType(p.type.toLatin1(), p.annotations);
358 if (type.id == QVariant::Invalid)
359 continue;
360
361 QByteArray name = p.name.toLatin1();
362 mp.signature = p.type.toLatin1();
363 mp.type = type.id;
364 mp.typeName = type.name;
365
366 // build the flags:
367 mp.flags = StdCppSet | Scriptable | Stored | Designable;
368 if (p.access != QDBusIntrospection::Property::Write)
369 mp.flags |= Readable;
370 if (p.access != QDBusIntrospection::Property::Read)
371 mp.flags |= Writable;
372
373 if (mp.typeName == "QDBusVariant")
374 mp.flags |= 0xff << 24;
375 else if (mp.type < 0xff)
376 // encode the type in the flags
377 mp.flags |= mp.type << 24;
378
379 // add the property:
380 properties.insert(name, mp);
381 }
382}
383
384void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj)
385{
386 // this code here is mostly copied from qaxbase.cpp
387 // with a few modifications to make it cleaner
388
389 QString className = interface;
390 className.replace(QLatin1Char('.'), QLatin1String("::"));
391 if (className.isEmpty())
392 className = QLatin1String("QDBusInterface");
393
394 QVarLengthArray<int> idata;
395 idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(int));
396
397 QDBusMetaObjectPrivate *header = reinterpret_cast<QDBusMetaObjectPrivate *>(idata.data());
398 header->revision = 1;
399 header->className = 0;
400 header->classInfoCount = 0;
401 header->classInfoData = 0;
402 header->methodCount = methods.count();
403 header->methodData = idata.size();
404 header->propertyCount = properties.count();
405 header->propertyData = header->methodData + header->methodCount * 5;
406 header->enumeratorCount = 0;
407 header->enumeratorData = 0;
408 header->propertyDBusData = header->propertyData + header->propertyCount * 3;
409 header->methodDBusData = header->propertyDBusData + header->propertyCount * intsPerProperty;
410
411 int data_size = idata.size() +
412 (header->methodCount * (5+intsPerMethod)) +
413 (header->propertyCount * (3+intsPerProperty));
414 foreach (const Method &mm, methods)
415 data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count();
416 idata.resize(data_size + 1);
417
418 char null('\0');
419 QByteArray stringdata = className.toLatin1();
420 stringdata += null;
421 stringdata.reserve(8192);
422
423 int offset = header->methodData;
424 int signatureOffset = header->methodDBusData;
425 int typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod;
426 idata[typeidOffset++] = 0; // eod
427
428 // add each method:
429 for (QMap<QByteArray, Method>::ConstIterator it = methods.constBegin();
430 it != methods.constEnd(); ++it) {
431 // form "prototype\0parameters\0typeName\0tag\0methodname\0inputSignature\0outputSignature"
432 const Method &mm = it.value();
433
434 idata[offset++] = stringdata.length();
435 stringdata += it.key(); // prototype
436 stringdata += null;
437 idata[offset++] = stringdata.length();
438 stringdata += mm.parameters;
439 stringdata += null;
440 idata[offset++] = stringdata.length();
441 stringdata += mm.typeName;
442 stringdata += null;
443 idata[offset++] = stringdata.length();
444 stringdata += mm.tag;
445 stringdata += null;
446 idata[offset++] = mm.flags;
447
448 idata[signatureOffset++] = stringdata.length();
449 stringdata += mm.name;
450 stringdata += null;
451 idata[signatureOffset++] = stringdata.length();
452 stringdata += mm.inputSignature;
453 stringdata += null;
454 idata[signatureOffset++] = stringdata.length();
455 stringdata += mm.outputSignature;
456 stringdata += null;
457
458 idata[signatureOffset++] = typeidOffset;
459 idata[typeidOffset++] = mm.inputTypes.count();
460 memcpy(idata.data() + typeidOffset, mm.inputTypes.data(), mm.inputTypes.count() * sizeof(int));
461 typeidOffset += mm.inputTypes.count();
462
463 idata[signatureOffset++] = typeidOffset;
464 idata[typeidOffset++] = mm.outputTypes.count();
465 memcpy(idata.data() + typeidOffset, mm.outputTypes.data(), mm.outputTypes.count() * sizeof(int));
466 typeidOffset += mm.outputTypes.count();
467 }
468
469 Q_ASSERT(offset == header->propertyData);
470 Q_ASSERT(signatureOffset == header->methodDBusData + header->methodCount * intsPerMethod);
471 Q_ASSERT(typeidOffset == idata.size());
472
473 // add each property
474 signatureOffset = header->propertyDBusData;
475 for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin();
476 it != properties.constEnd(); ++it) {
477 const Property &mp = it.value();
478
479 // form is "name\0typeName\0signature\0"
480 idata[offset++] = stringdata.length();
481 stringdata += it.key(); // name
482 stringdata += null;
483 idata[offset++] = stringdata.length();
484 stringdata += mp.typeName;
485 stringdata += null;
486 idata[offset++] = mp.flags;
487
488 idata[signatureOffset++] = stringdata.length();
489 stringdata += mp.signature;
490 stringdata += null;
491 idata[signatureOffset++] = mp.type;
492 }
493
494 Q_ASSERT(offset == header->propertyDBusData);
495 Q_ASSERT(signatureOffset == header->methodDBusData);
496
497 char *string_data = new char[stringdata.length()];
498 memcpy(string_data, stringdata, stringdata.length());
499
500 uint *uint_data = new uint[idata.size()];
501 memcpy(uint_data, idata.data(), idata.size() * sizeof(int));
502
503 // put the metaobject together
504 obj->d.data = uint_data;
505 obj->d.extradata = 0;
506 obj->d.stringdata = string_data;
507 obj->d.superdata = &QDBusAbstractInterface::staticMetaObject;
508}
509
510#if 0
511void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface)
512{
513 // no XML definition
514 QString tmp(interface);
515 tmp.replace(QLatin1Char('.'), QLatin1String("::"));
516 QByteArray name(tmp.toLatin1());
517
518 QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate;
519 memset(header, 0, sizeof *header);
520 header->revision = 1;
521 // leave the rest with 0
522
523 char *stringdata = new char[name.length() + 1];
524 stringdata[name.length()] = '\0';
525
526 d.data = reinterpret_cast<uint*>(header);
527 d.extradata = 0;
528 d.stringdata = stringdata;
529 d.superdata = &QDBusAbstractInterface::staticMetaObject;
530 cached = false;
531}
532#endif
533
534/////////
535// class QDBusMetaObject
536
537QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, const QString &xml,
538 QHash<QString, QDBusMetaObject *> &cache,
539 QDBusError &error)
540{
541 error = QDBusError();
542 QDBusIntrospection::Interfaces parsed = QDBusIntrospection::parseInterfaces(xml);
543
544 QDBusMetaObject *we = 0;
545 QDBusIntrospection::Interfaces::ConstIterator it = parsed.constBegin();
546 QDBusIntrospection::Interfaces::ConstIterator end = parsed.constEnd();
547 for ( ; it != end; ++it) {
548 // check if it's in the cache
549 bool us = it.key() == interface;
550
551 QDBusMetaObject *obj = cache.value(it.key(), 0);
552 if ( !obj && ( us || !interface.startsWith( QLatin1String("local.") ) ) ) {
553 // not in cache; create
554 obj = new QDBusMetaObject;
555 QDBusMetaObjectGenerator generator(it.key(), it.value().constData());
556 generator.write(obj);
557
558 if ( (obj->cached = !it.key().startsWith( QLatin1String("local.") )) )
559 // cache it
560 cache.insert(it.key(), obj);
561 else if (!us)
562 delete obj;
563
564 }
565
566 if (us)
567 // it's us
568 we = obj;
569 }
570
571 if (we)
572 return we;
573 // still nothing?
574
575 if (parsed.isEmpty()) {
576 // object didn't return introspection
577 we = new QDBusMetaObject;
578 QDBusMetaObjectGenerator generator(interface, 0);
579 generator.write(we);
580 we->cached = false;
581 return we;
582 } else if (interface.isEmpty()) {
583 // merge all interfaces
584 it = parsed.constBegin();
585 QDBusIntrospection::Interface merged = *it.value().constData();
586
587 for (++it; it != end; ++it) {
588 merged.annotations.unite(it.value()->annotations);
589 merged.methods.unite(it.value()->methods);
590 merged.signals_.unite(it.value()->signals_);
591 merged.properties.unite(it.value()->properties);
592 }
593
594 merged.name = QLatin1String("local.Merged");
595 merged.introspection.clear();
596
597 we = new QDBusMetaObject;
598 QDBusMetaObjectGenerator generator(merged.name, &merged);
599 generator.write(we);
600 we->cached = false;
601 return we;
602 }
603
604 // mark as an error
605 error = QDBusError(QDBusError::UnknownInterface,
606 QString::fromLatin1("Interface '%1' was not found")
607 .arg(interface));
608 return 0;
609}
610
611QDBusMetaObject::QDBusMetaObject()
612{
613}
614
615static inline const QDBusMetaObjectPrivate *priv(const uint* data)
616{
617 return reinterpret_cast<const QDBusMetaObjectPrivate *>(data);
618}
619
620const char *QDBusMetaObject::dbusNameForMethod(int id) const
621{
622 //id -= methodOffset();
623 if (id >= 0 && id < priv(d.data)->methodCount) {
624 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
625 return d.stringdata + d.data[handle];
626 }
627 return 0;
628}
629
630const char *QDBusMetaObject::inputSignatureForMethod(int id) const
631{
632 //id -= methodOffset();
633 if (id >= 0 && id < priv(d.data)->methodCount) {
634 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
635 return d.stringdata + d.data[handle + 1];
636 }
637 return 0;
638}
639
640const char *QDBusMetaObject::outputSignatureForMethod(int id) const
641{
642 //id -= methodOffset();
643 if (id >= 0 && id < priv(d.data)->methodCount) {
644 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
645 return d.stringdata + d.data[handle + 2];
646 }
647 return 0;
648}
649
650const int *QDBusMetaObject::inputTypesForMethod(int id) const
651{
652 //id -= methodOffset();
653 if (id >= 0 && id < priv(d.data)->methodCount) {
654 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
655 return reinterpret_cast<const int*>(d.data + d.data[handle + 3]);
656 }
657 return 0;
658}
659
660const int *QDBusMetaObject::outputTypesForMethod(int id) const
661{
662 //id -= methodOffset();
663 if (id >= 0 && id < priv(d.data)->methodCount) {
664 int handle = priv(d.data)->methodDBusData + id*intsPerMethod;
665 return reinterpret_cast<const int*>(d.data + d.data[handle + 4]);
666 }
667 return 0;
668}
669
670int QDBusMetaObject::propertyMetaType(int id) const
671{
672 //id -= propertyOffset();
673 if (id >= 0 && id < priv(d.data)->propertyCount) {
674 int handle = priv(d.data)->propertyDBusData + id*intsPerProperty;
675 return d.data[handle + 1];
676 }
677 return 0;
678}
679
680QT_END_NAMESPACE
681
682#endif // QT_NO_DBUS
Note: See TracBrowser for help on using the repository browser.