| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtDBus module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qdbusmetaobject_p.h" |
| 42 | |
| 43 | #include <QtCore/qbytearray.h> |
| 44 | #include <QtCore/qhash.h> |
| 45 | #include <QtCore/qstring.h> |
| 46 | #include <QtCore/qvarlengtharray.h> |
| 47 | |
| 48 | #include "qdbusutil_p.h" |
| 49 | #include "qdbuserror.h" |
| 50 | #include "qdbusmetatype.h" |
| 51 | #include "qdbusargument.h" |
| 52 | #include "qdbusintrospection_p.h" |
| 53 | #include "qdbusabstractinterface_p.h" |
| 54 | |
| 55 | #include <private/qmetaobject_p.h> |
| 56 | #include <private/qmetaobjectbuilder_p.h> |
| 57 | |
| 58 | #ifndef QT_NO_DBUS |
| 59 | |
| 60 | QT_BEGIN_NAMESPACE |
| 61 | |
| 62 | class QDBusMetaObjectGenerator |
| 63 | { |
| 64 | public: |
| 65 | QDBusMetaObjectGenerator(const QString &interface, |
| 66 | const QDBusIntrospection::Interface *parsedData); |
| 67 | void write(QDBusMetaObject *obj); |
| 68 | void writeWithoutXml(QDBusMetaObject *obj); |
| 69 | |
| 70 | private: |
| 71 | struct Method { |
| 72 | QList<QByteArray> parameterNames; |
| 73 | QByteArray tag; |
| 74 | QByteArray name; |
| 75 | QVarLengthArray<int, 4> inputTypes; |
| 76 | QVarLengthArray<int, 4> outputTypes; |
| 77 | QByteArray rawReturnType; |
| 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 | QMap<QByteArray, Method> signals_; |
| 93 | QMap<QByteArray, Method> methods; |
| 94 | QMap<QByteArray, Property> properties; |
| 95 | |
| 96 | const QDBusIntrospection::Interface *data; |
| 97 | QString interface; |
| 98 | |
| 99 | Type findType(const QByteArray &signature, |
| 100 | const QDBusIntrospection::Annotations &annotations, |
| 101 | const char *direction = "Out" , int id = -1); |
| 102 | |
| 103 | void parseMethods(); |
| 104 | void parseSignals(); |
| 105 | void parseProperties(); |
| 106 | |
| 107 | static int aggregateParameterCount(const QMap<QByteArray, Method> &map); |
| 108 | }; |
| 109 | |
| 110 | static const int intsPerProperty = 2; |
| 111 | static const int intsPerMethod = 2; |
| 112 | |
| 113 | struct QDBusMetaObjectPrivate : public QMetaObjectPrivate |
| 114 | { |
| 115 | int propertyDBusData; |
| 116 | int methodDBusData; |
| 117 | }; |
| 118 | |
| 119 | QDBusMetaObjectGenerator::QDBusMetaObjectGenerator(const QString &interfaceName, |
| 120 | const QDBusIntrospection::Interface *parsedData) |
| 121 | : data(parsedData), interface(interfaceName) |
| 122 | { |
| 123 | if (data) { |
| 124 | parseProperties(); |
| 125 | parseSignals(); // call parseSignals first so that slots override signals |
| 126 | parseMethods(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static int registerComplexDBusType(const QByteArray &typeName) |
| 131 | { |
| 132 | struct QDBusRawTypeHandler : QtPrivate::QMetaTypeInterface |
| 133 | { |
| 134 | const QByteArray name; |
| 135 | QDBusRawTypeHandler(const QByteArray &name) |
| 136 | : QtPrivate::QMetaTypeInterface { |
| 137 | 0, sizeof(void *), sizeof(void *), QMetaType::RelocatableType, 0, nullptr, |
| 138 | name.constData(), |
| 139 | nullptr, nullptr, nullptr, nullptr, |
| 140 | nullptr, nullptr, nullptr, |
| 141 | nullptr, nullptr, nullptr |
| 142 | }, |
| 143 | name(name) |
| 144 | {} |
| 145 | }; |
| 146 | |
| 147 | static QBasicMutex mutex; |
| 148 | static struct Hash : QHash<QByteArray, QMetaType> |
| 149 | { |
| 150 | ~Hash() |
| 151 | { |
| 152 | for (QMetaType entry : *this) |
| 153 | QMetaType::unregisterMetaType(std::move(entry)); |
| 154 | } |
| 155 | } hash; |
| 156 | QMutexLocker lock(&mutex); |
| 157 | QMetaType &metatype = hash[typeName]; |
| 158 | if (!metatype.isValid()) |
| 159 | metatype = QMetaType(new QDBusRawTypeHandler(typeName)); |
| 160 | return metatype.id(); |
| 161 | } |
| 162 | |
| 163 | Q_DBUS_EXPORT bool qt_dbus_metaobject_skip_annotations = false; |
| 164 | |
| 165 | QDBusMetaObjectGenerator::Type |
| 166 | QDBusMetaObjectGenerator::findType(const QByteArray &signature, |
| 167 | const QDBusIntrospection::Annotations &annotations, |
| 168 | const char *direction, int id) |
| 169 | { |
| 170 | Type result; |
| 171 | result.id = QMetaType::UnknownType; |
| 172 | |
| 173 | int type = QDBusMetaType::signatureToMetaType(signature).id(); |
| 174 | if (type == QMetaType::UnknownType && !qt_dbus_metaobject_skip_annotations) { |
| 175 | // it's not a type normally handled by our meta type system |
| 176 | // it must contain an annotation |
| 177 | QString annotationName = QString::fromLatin1("org.qtproject.QtDBus.QtTypeName" ); |
| 178 | if (id >= 0) |
| 179 | annotationName += QString::fromLatin1(".%1%2" ) |
| 180 | .arg(QLatin1String(direction)) |
| 181 | .arg(id); |
| 182 | |
| 183 | // extract from annotations: |
| 184 | QByteArray typeName = annotations.value(annotationName).toLatin1(); |
| 185 | |
| 186 | // verify that it's a valid one |
| 187 | if (typeName.isEmpty()) { |
| 188 | // try the old annotation from Qt 4 |
| 189 | annotationName = QString::fromLatin1("com.trolltech.QtDBus.QtTypeName" ); |
| 190 | if (id >= 0) |
| 191 | annotationName += QString::fromLatin1(".%1%2" ) |
| 192 | .arg(QLatin1String(direction)) |
| 193 | .arg(id); |
| 194 | typeName = annotations.value(annotationName).toLatin1(); |
| 195 | } |
| 196 | |
| 197 | if (!typeName.isEmpty()) { |
| 198 | // type name found |
| 199 | type = QMetaType::fromName(typeName).id(); |
| 200 | } |
| 201 | |
| 202 | if (type == QMetaType::UnknownType || signature != QDBusMetaType::typeToSignature(QMetaType(type))) { |
| 203 | // type is still unknown or doesn't match back to the signature that it |
| 204 | // was expected to, so synthesize a fake type |
| 205 | typeName = "QDBusRawType<0x" + signature.toHex() + ">*" ; |
| 206 | type = registerComplexDBusType(typeName); |
| 207 | } |
| 208 | |
| 209 | result.name = typeName; |
| 210 | } else if (type == QMetaType::UnknownType) { |
| 211 | // this case is used only by the qdbus command-line tool |
| 212 | // invalid, let's create an impossible type that contains the signature |
| 213 | |
| 214 | if (signature == "av" ) { |
| 215 | result.name = "QVariantList" ; |
| 216 | type = QMetaType::QVariantList; |
| 217 | } else if (signature == "a{sv}" ) { |
| 218 | result.name = "QVariantMap" ; |
| 219 | type = QMetaType::QVariantMap; |
| 220 | } else if (signature == "a{ss}" ) { |
| 221 | result.name = "QMap<QString,QString>" ; |
| 222 | type = qMetaTypeId<QMap<QString, QString> >(); |
| 223 | } else { |
| 224 | result.name = "{D-Bus type \"" + signature + "\"}" ; |
| 225 | type = registerComplexDBusType(result.name); |
| 226 | } |
| 227 | } else { |
| 228 | result.name = QMetaType(type).name(); |
| 229 | } |
| 230 | |
| 231 | result.id = type; |
| 232 | return result; // success |
| 233 | } |
| 234 | |
| 235 | void QDBusMetaObjectGenerator::parseMethods() |
| 236 | { |
| 237 | // |
| 238 | // TODO: |
| 239 | // Add cloned methods when the remote object has return types |
| 240 | // |
| 241 | |
| 242 | QDBusIntrospection::Methods::ConstIterator method_it = data->methods.constBegin(); |
| 243 | QDBusIntrospection::Methods::ConstIterator method_end = data->methods.constEnd(); |
| 244 | for ( ; method_it != method_end; ++method_it) { |
| 245 | const QDBusIntrospection::Method &m = *method_it; |
| 246 | Method mm; |
| 247 | |
| 248 | mm.name = m.name.toLatin1(); |
| 249 | QByteArray prototype = mm.name; |
| 250 | prototype += '('; |
| 251 | |
| 252 | bool ok = true; |
| 253 | |
| 254 | // build the input argument list |
| 255 | for (int i = 0; i < m.inputArgs.count(); ++i) { |
| 256 | const QDBusIntrospection::Argument &arg = m.inputArgs.at(i); |
| 257 | |
| 258 | Type type = findType(arg.type.toLatin1(), m.annotations, "In" , i); |
| 259 | if (type.id == QMetaType::UnknownType) { |
| 260 | ok = false; |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | mm.inputTypes.append(type.id); |
| 265 | |
| 266 | mm.parameterNames.append(arg.name.toLatin1()); |
| 267 | |
| 268 | prototype.append(type.name); |
| 269 | prototype.append(','); |
| 270 | } |
| 271 | if (!ok) continue; |
| 272 | |
| 273 | // build the output argument list: |
| 274 | for (int i = 0; i < m.outputArgs.count(); ++i) { |
| 275 | const QDBusIntrospection::Argument &arg = m.outputArgs.at(i); |
| 276 | |
| 277 | Type type = findType(arg.type.toLatin1(), m.annotations, "Out" , i); |
| 278 | if (type.id == QMetaType::UnknownType) { |
| 279 | ok = false; |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | mm.outputTypes.append(type.id); |
| 284 | |
| 285 | if (i == 0 && type.id == -1) { |
| 286 | mm.rawReturnType = type.name; |
| 287 | } |
| 288 | if (i != 0) { |
| 289 | // non-const ref parameter |
| 290 | mm.parameterNames.append(arg.name.toLatin1()); |
| 291 | |
| 292 | prototype.append(type.name); |
| 293 | prototype.append("&," ); |
| 294 | } |
| 295 | } |
| 296 | if (!ok) continue; |
| 297 | |
| 298 | // convert the last commas: |
| 299 | if (!mm.parameterNames.isEmpty()) |
| 300 | prototype[prototype.length() - 1] = ')'; |
| 301 | else |
| 302 | prototype.append(')'); |
| 303 | |
| 304 | // check the async tag |
| 305 | if (m.annotations.value(QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true" )) |
| 306 | mm.tag = "Q_NOREPLY" ; |
| 307 | |
| 308 | // meta method flags |
| 309 | mm.flags = AccessPublic | MethodSlot | MethodScriptable; |
| 310 | |
| 311 | // add |
| 312 | methods.insert(QMetaObject::normalizedSignature(prototype), mm); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void QDBusMetaObjectGenerator::parseSignals() |
| 317 | { |
| 318 | QDBusIntrospection::Signals::ConstIterator signal_it = data->signals_.constBegin(); |
| 319 | QDBusIntrospection::Signals::ConstIterator signal_end = data->signals_.constEnd(); |
| 320 | for ( ; signal_it != signal_end; ++signal_it) { |
| 321 | const QDBusIntrospection::Signal &s = *signal_it; |
| 322 | Method mm; |
| 323 | |
| 324 | mm.name = s.name.toLatin1(); |
| 325 | QByteArray prototype = mm.name; |
| 326 | prototype += '('; |
| 327 | |
| 328 | bool ok = true; |
| 329 | |
| 330 | // build the output argument list |
| 331 | for (int i = 0; i < s.outputArgs.count(); ++i) { |
| 332 | const QDBusIntrospection::Argument &arg = s.outputArgs.at(i); |
| 333 | |
| 334 | Type type = findType(arg.type.toLatin1(), s.annotations, "Out" , i); |
| 335 | if (type.id == QMetaType::UnknownType) { |
| 336 | ok = false; |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | mm.inputTypes.append(type.id); |
| 341 | |
| 342 | mm.parameterNames.append(arg.name.toLatin1()); |
| 343 | |
| 344 | prototype.append(type.name); |
| 345 | prototype.append(','); |
| 346 | } |
| 347 | if (!ok) continue; |
| 348 | |
| 349 | // convert the last commas: |
| 350 | if (!mm.parameterNames.isEmpty()) |
| 351 | prototype[prototype.length() - 1] = ')'; |
| 352 | else |
| 353 | prototype.append(')'); |
| 354 | |
| 355 | // meta method flags |
| 356 | mm.flags = AccessPublic | MethodSignal | MethodScriptable; |
| 357 | |
| 358 | // add |
| 359 | signals_.insert(QMetaObject::normalizedSignature(prototype), mm); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void QDBusMetaObjectGenerator::parseProperties() |
| 364 | { |
| 365 | QDBusIntrospection::Properties::ConstIterator prop_it = data->properties.constBegin(); |
| 366 | QDBusIntrospection::Properties::ConstIterator prop_end = data->properties.constEnd(); |
| 367 | for ( ; prop_it != prop_end; ++prop_it) { |
| 368 | const QDBusIntrospection::Property &p = *prop_it; |
| 369 | Property mp; |
| 370 | Type type = findType(p.type.toLatin1(), p.annotations); |
| 371 | if (type.id == QMetaType::UnknownType) |
| 372 | continue; |
| 373 | |
| 374 | QByteArray name = p.name.toLatin1(); |
| 375 | mp.signature = p.type.toLatin1(); |
| 376 | mp.type = type.id; |
| 377 | mp.typeName = type.name; |
| 378 | |
| 379 | // build the flags: |
| 380 | mp.flags = StdCppSet | Scriptable | Stored | Designable; |
| 381 | if (p.access != QDBusIntrospection::Property::Write) |
| 382 | mp.flags |= Readable; |
| 383 | if (p.access != QDBusIntrospection::Property::Read) |
| 384 | mp.flags |= Writable; |
| 385 | |
| 386 | // add the property: |
| 387 | properties.insert(name, mp); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Returns the sum of all parameters (including return type) for the given |
| 392 | // \a map of methods. This is needed for calculating the size of the methods' |
| 393 | // parameter type/name meta-data. |
| 394 | int QDBusMetaObjectGenerator::aggregateParameterCount(const QMap<QByteArray, Method> &map) |
| 395 | { |
| 396 | int sum = 0; |
| 397 | QMap<QByteArray, Method>::const_iterator it; |
| 398 | for (it = map.constBegin(); it != map.constEnd(); ++it) { |
| 399 | const Method &m = it.value(); |
| 400 | sum += m.inputTypes.size() + qMax(qsizetype(1), m.outputTypes.size()); |
| 401 | } |
| 402 | return sum; |
| 403 | } |
| 404 | |
| 405 | void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) |
| 406 | { |
| 407 | // this code here is mostly copied from qaxbase.cpp |
| 408 | // with a few modifications to make it cleaner |
| 409 | |
| 410 | QString className = interface; |
| 411 | className.replace(QLatin1Char('.'), QLatin1String("::" )); |
| 412 | if (className.isEmpty()) |
| 413 | className = QLatin1String("QDBusInterface" ); |
| 414 | |
| 415 | QVarLengthArray<int> idata; |
| 416 | idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(int)); |
| 417 | |
| 418 | int methodParametersDataSize = |
| 419 | ((aggregateParameterCount(signals_) |
| 420 | + aggregateParameterCount(methods)) * 2) // types and parameter names |
| 421 | - signals_.count() // return "parameters" don't have names |
| 422 | - methods.count(); // ditto |
| 423 | |
| 424 | QDBusMetaObjectPrivate * = reinterpret_cast<QDBusMetaObjectPrivate *>(idata.data()); |
| 425 | static_assert(QMetaObjectPrivate::OutputRevision == 9, "QtDBus meta-object generator should generate the same version as moc" ); |
| 426 | header->revision = QMetaObjectPrivate::OutputRevision; |
| 427 | header->className = 0; |
| 428 | header->classInfoCount = 0; |
| 429 | header->classInfoData = 0; |
| 430 | header->methodCount = signals_.count() + methods.count(); |
| 431 | header->methodData = idata.size(); |
| 432 | header->propertyCount = properties.count(); |
| 433 | header->propertyData = header->methodData + header->methodCount * QMetaObjectPrivate::IntsPerMethod + methodParametersDataSize; |
| 434 | header->enumeratorCount = 0; |
| 435 | header->enumeratorData = 0; |
| 436 | header->constructorCount = 0; |
| 437 | header->constructorData = 0; |
| 438 | header->flags = RequiresVariantMetaObject; |
| 439 | header->signalCount = signals_.count(); |
| 440 | // These are specific to QDBusMetaObject: |
| 441 | header->propertyDBusData = header->propertyData + header->propertyCount * QMetaObjectPrivate::IntsPerProperty; |
| 442 | header->methodDBusData = header->propertyDBusData + header->propertyCount * intsPerProperty; |
| 443 | |
| 444 | int data_size = idata.size() + |
| 445 | (header->methodCount * (QMetaObjectPrivate::IntsPerMethod+intsPerMethod)) + methodParametersDataSize + |
| 446 | (header->propertyCount * (QMetaObjectPrivate::IntsPerProperty+intsPerProperty)); |
| 447 | for (const Method &mm : qAsConst(signals_)) |
| 448 | data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); |
| 449 | for (const Method &mm : qAsConst(methods)) |
| 450 | data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); |
| 451 | idata.resize(data_size + 1); |
| 452 | |
| 453 | QMetaStringTable strings(className.toLatin1()); |
| 454 | |
| 455 | int offset = header->methodData; |
| 456 | int parametersOffset = offset + header->methodCount * QMetaObjectPrivate::IntsPerMethod; |
| 457 | int signatureOffset = header->methodDBusData; |
| 458 | int typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod; |
| 459 | idata[typeidOffset++] = 0; // eod |
| 460 | |
| 461 | int totalMetaTypeCount = properties.count(); |
| 462 | for (const auto& methodContainer: {signals_, methods}) { |
| 463 | for (const auto& method: methodContainer) { |
| 464 | int argc = method.inputTypes.size() + qMax(qsizetype(0), method.outputTypes.size() - 1); |
| 465 | totalMetaTypeCount += argc + 1; |
| 466 | } |
| 467 | } |
| 468 | QMetaType *metaTypes = new QMetaType[totalMetaTypeCount]; |
| 469 | int propertyId = 0; |
| 470 | |
| 471 | // add each method: |
| 472 | int currentMethodMetaTypeOffset = properties.count(); |
| 473 | for (int x = 0; x < 2; ++x) { |
| 474 | // Signals must be added before other methods, to match moc. |
| 475 | QMap<QByteArray, Method> &map = (x == 0) ? signals_ : methods; |
| 476 | for (QMap<QByteArray, Method>::ConstIterator it = map.constBegin(); |
| 477 | it != map.constEnd(); ++it) { |
| 478 | const Method &mm = it.value(); |
| 479 | |
| 480 | int argc = mm.inputTypes.size() + qMax(qsizetype(0), mm.outputTypes.size() - 1); |
| 481 | |
| 482 | idata[offset++] = strings.enter(mm.name); |
| 483 | idata[offset++] = argc; |
| 484 | idata[offset++] = parametersOffset; |
| 485 | idata[offset++] = strings.enter(mm.tag); |
| 486 | idata[offset++] = mm.flags; |
| 487 | idata[offset++] = currentMethodMetaTypeOffset; |
| 488 | |
| 489 | // Parameter types |
| 490 | for (int i = -1; i < argc; ++i) { |
| 491 | int type; |
| 492 | QByteArray typeName; |
| 493 | if (i < 0) { // Return type |
| 494 | if (!mm.outputTypes.isEmpty()) { |
| 495 | type = mm.outputTypes.first(); |
| 496 | if (type == -1) { |
| 497 | type = IsUnresolvedType | strings.enter(mm.rawReturnType); |
| 498 | } |
| 499 | } else { |
| 500 | type = QMetaType::Void; |
| 501 | } |
| 502 | } else if (i < mm.inputTypes.size()) { |
| 503 | type = mm.inputTypes.at(i); |
| 504 | } else { |
| 505 | Q_ASSERT(mm.outputTypes.size() > 1); |
| 506 | type = mm.outputTypes.at(i - mm.inputTypes.size() + 1); |
| 507 | // Output parameters are references; type id not available |
| 508 | typeName = QMetaType(type).name(); |
| 509 | typeName.append('&'); |
| 510 | } |
| 511 | Q_ASSERT(type != QMetaType::UnknownType); |
| 512 | int typeInfo; |
| 513 | if (!typeName.isEmpty()) |
| 514 | typeInfo = IsUnresolvedType | strings.enter(typeName); |
| 515 | else |
| 516 | typeInfo = type; |
| 517 | metaTypes[currentMethodMetaTypeOffset++] = QMetaType (type); |
| 518 | idata[parametersOffset++] = typeInfo; |
| 519 | } |
| 520 | // Parameter names |
| 521 | for (int i = 0; i < argc; ++i) |
| 522 | idata[parametersOffset++] = strings.enter(mm.parameterNames.at(i)); |
| 523 | |
| 524 | idata[signatureOffset++] = typeidOffset; |
| 525 | idata[typeidOffset++] = mm.inputTypes.count(); |
| 526 | memcpy(idata.data() + typeidOffset, mm.inputTypes.data(), mm.inputTypes.count() * sizeof(int)); |
| 527 | typeidOffset += mm.inputTypes.count(); |
| 528 | |
| 529 | idata[signatureOffset++] = typeidOffset; |
| 530 | idata[typeidOffset++] = mm.outputTypes.count(); |
| 531 | memcpy(idata.data() + typeidOffset, mm.outputTypes.data(), mm.outputTypes.count() * sizeof(int)); |
| 532 | typeidOffset += mm.outputTypes.count(); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | Q_ASSERT(offset == header->methodData + header->methodCount * QMetaObjectPrivate::IntsPerMethod); |
| 537 | Q_ASSERT(parametersOffset == header->propertyData); |
| 538 | Q_ASSERT(signatureOffset == header->methodDBusData + header->methodCount * intsPerMethod); |
| 539 | Q_ASSERT(typeidOffset == idata.size()); |
| 540 | offset += methodParametersDataSize; |
| 541 | Q_ASSERT(offset == header->propertyData); |
| 542 | |
| 543 | // add each property |
| 544 | signatureOffset = header->propertyDBusData; |
| 545 | for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin(); |
| 546 | it != properties.constEnd(); ++it) { |
| 547 | const Property &mp = it.value(); |
| 548 | |
| 549 | // form is name, typeinfo, flags |
| 550 | idata[offset++] = strings.enter(it.key()); // name |
| 551 | Q_ASSERT(mp.type != QMetaType::UnknownType); |
| 552 | idata[offset++] = mp.type; |
| 553 | idata[offset++] = mp.flags; |
| 554 | idata[offset++] = -1; // notify index |
| 555 | idata[offset++] = 0; // revision |
| 556 | |
| 557 | idata[signatureOffset++] = strings.enter(mp.signature); |
| 558 | idata[signatureOffset++] = mp.type; |
| 559 | |
| 560 | metaTypes[propertyId++] = QMetaType(mp.type); |
| 561 | } |
| 562 | |
| 563 | Q_ASSERT(offset == header->propertyDBusData); |
| 564 | Q_ASSERT(signatureOffset == header->methodDBusData); |
| 565 | |
| 566 | char *string_data = new char[strings.blobSize()]; |
| 567 | strings.writeBlob(string_data); |
| 568 | |
| 569 | uint *uint_data = new uint[idata.size()]; |
| 570 | memcpy(uint_data, idata.data(), idata.size() * sizeof(int)); |
| 571 | |
| 572 | // put the metaobject together |
| 573 | obj->d.data = uint_data; |
| 574 | obj->d.relatedMetaObjects = nullptr; |
| 575 | obj->d.static_metacall = nullptr; |
| 576 | obj->d.extradata = nullptr; |
| 577 | obj->d.stringdata = reinterpret_cast<const uint *>(string_data); |
| 578 | obj->d.superdata = &QDBusAbstractInterface::staticMetaObject; |
| 579 | obj->d.metaTypes = reinterpret_cast<QtPrivate::QMetaTypeInterface *const *>(metaTypes); |
| 580 | } |
| 581 | |
| 582 | #if 0 |
| 583 | void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface) |
| 584 | { |
| 585 | // no XML definition |
| 586 | QString tmp(interface); |
| 587 | tmp.replace(QLatin1Char('.'), QLatin1String("::" )); |
| 588 | QByteArray name(tmp.toLatin1()); |
| 589 | |
| 590 | QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate; |
| 591 | memset(header, 0, sizeof *header); |
| 592 | header->revision = 1; |
| 593 | // leave the rest with 0 |
| 594 | |
| 595 | char *stringdata = new char[name.length() + 1]; |
| 596 | stringdata[name.length()] = '\0'; |
| 597 | |
| 598 | d.data = reinterpret_cast<uint*>(header); |
| 599 | d.relatedMetaObjects = 0; |
| 600 | d.static_metacall = 0; |
| 601 | d.extradata = 0; |
| 602 | d.stringdata = stringdata; |
| 603 | d.superdata = &QDBusAbstractInterface::staticMetaObject; |
| 604 | cached = false; |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | ///////// |
| 609 | // class QDBusMetaObject |
| 610 | |
| 611 | QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, const QString &xml, |
| 612 | QHash<QString, QDBusMetaObject *> &cache, |
| 613 | QDBusError &error) |
| 614 | { |
| 615 | error = QDBusError(); |
| 616 | QDBusIntrospection::Interfaces parsed = QDBusIntrospection::parseInterfaces(xml); |
| 617 | |
| 618 | QDBusMetaObject *we = nullptr; |
| 619 | QDBusIntrospection::Interfaces::ConstIterator it = parsed.constBegin(); |
| 620 | QDBusIntrospection::Interfaces::ConstIterator end = parsed.constEnd(); |
| 621 | for ( ; it != end; ++it) { |
| 622 | // check if it's in the cache |
| 623 | bool us = it.key() == interface; |
| 624 | |
| 625 | QDBusMetaObject *obj = cache.value(it.key(), 0); |
| 626 | if ( !obj && ( us || !interface.startsWith( QLatin1String("local." ) ) ) ) { |
| 627 | // not in cache; create |
| 628 | obj = new QDBusMetaObject; |
| 629 | QDBusMetaObjectGenerator generator(it.key(), it.value().constData()); |
| 630 | generator.write(obj); |
| 631 | |
| 632 | if ( (obj->cached = !it.key().startsWith( QLatin1String("local." ) )) ) |
| 633 | // cache it |
| 634 | cache.insert(it.key(), obj); |
| 635 | else if (!us) |
| 636 | delete obj; |
| 637 | |
| 638 | } |
| 639 | |
| 640 | if (us) |
| 641 | // it's us |
| 642 | we = obj; |
| 643 | } |
| 644 | |
| 645 | if (we) |
| 646 | return we; |
| 647 | // still nothing? |
| 648 | |
| 649 | if (parsed.isEmpty()) { |
| 650 | // object didn't return introspection |
| 651 | we = new QDBusMetaObject; |
| 652 | QDBusMetaObjectGenerator generator(interface, nullptr); |
| 653 | generator.write(we); |
| 654 | we->cached = false; |
| 655 | return we; |
| 656 | } else if (interface.isEmpty()) { |
| 657 | // merge all interfaces |
| 658 | it = parsed.constBegin(); |
| 659 | QDBusIntrospection::Interface merged = *it.value().constData(); |
| 660 | |
| 661 | for (++it; it != end; ++it) { |
| 662 | merged.annotations.insert(it.value()->annotations); |
| 663 | merged.methods.unite(it.value()->methods); |
| 664 | merged.signals_.unite(it.value()->signals_); |
| 665 | merged.properties.insert(it.value()->properties); |
| 666 | } |
| 667 | |
| 668 | merged.name = QLatin1String("local.Merged" ); |
| 669 | merged.introspection.clear(); |
| 670 | |
| 671 | we = new QDBusMetaObject; |
| 672 | QDBusMetaObjectGenerator generator(merged.name, &merged); |
| 673 | generator.write(we); |
| 674 | we->cached = false; |
| 675 | return we; |
| 676 | } |
| 677 | |
| 678 | // mark as an error |
| 679 | error = QDBusError(QDBusError::UnknownInterface, |
| 680 | QLatin1String("Interface '%1' was not found" ) |
| 681 | .arg(interface)); |
| 682 | return nullptr; |
| 683 | } |
| 684 | |
| 685 | QDBusMetaObject::QDBusMetaObject() |
| 686 | { |
| 687 | } |
| 688 | |
| 689 | static inline const QDBusMetaObjectPrivate *priv(const uint* data) |
| 690 | { |
| 691 | return reinterpret_cast<const QDBusMetaObjectPrivate *>(data); |
| 692 | } |
| 693 | |
| 694 | const int *QDBusMetaObject::inputTypesForMethod(int id) const |
| 695 | { |
| 696 | //id -= methodOffset(); |
| 697 | if (id >= 0 && id < priv(d.data)->methodCount) { |
| 698 | int handle = priv(d.data)->methodDBusData + id*intsPerMethod; |
| 699 | return reinterpret_cast<const int*>(d.data + d.data[handle]); |
| 700 | } |
| 701 | return nullptr; |
| 702 | } |
| 703 | |
| 704 | const int *QDBusMetaObject::outputTypesForMethod(int id) const |
| 705 | { |
| 706 | //id -= methodOffset(); |
| 707 | if (id >= 0 && id < priv(d.data)->methodCount) { |
| 708 | int handle = priv(d.data)->methodDBusData + id*intsPerMethod; |
| 709 | return reinterpret_cast<const int*>(d.data + d.data[handle + 1]); |
| 710 | } |
| 711 | return nullptr; |
| 712 | } |
| 713 | |
| 714 | int QDBusMetaObject::propertyMetaType(int id) const |
| 715 | { |
| 716 | //id -= propertyOffset(); |
| 717 | if (id >= 0 && id < priv(d.data)->propertyCount) { |
| 718 | int handle = priv(d.data)->propertyDBusData + id*intsPerProperty; |
| 719 | return d.data[handle + 1]; |
| 720 | } |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | QT_END_NAMESPACE |
| 725 | |
| 726 | #endif // QT_NO_DBUS |
| 727 | |