| 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 char *typeName) |
| 131 | { |
| 132 | struct QDBusRawTypeHandler { |
| 133 | static void destruct(void *) |
| 134 | { |
| 135 | qFatal(msg: "Cannot destruct placeholder type QDBusRawType" ); |
| 136 | } |
| 137 | |
| 138 | static void *construct(void *, const void *) |
| 139 | { |
| 140 | qFatal(msg: "Cannot construct placeholder type QDBusRawType" ); |
| 141 | return nullptr; |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | return QMetaType::registerNormalizedType(normalizedTypeName: typeName, |
| 146 | destructor: QDBusRawTypeHandler::destruct, |
| 147 | constructor: QDBusRawTypeHandler::construct, |
| 148 | size: sizeof(void *), |
| 149 | flags: QMetaType::MovableType, |
| 150 | metaObject: nullptr); |
| 151 | } |
| 152 | |
| 153 | Q_DBUS_EXPORT bool qt_dbus_metaobject_skip_annotations = false; |
| 154 | |
| 155 | QDBusMetaObjectGenerator::Type |
| 156 | QDBusMetaObjectGenerator::findType(const QByteArray &signature, |
| 157 | const QDBusIntrospection::Annotations &annotations, |
| 158 | const char *direction, int id) |
| 159 | { |
| 160 | Type result; |
| 161 | result.id = QMetaType::UnknownType; |
| 162 | |
| 163 | int type = QDBusMetaType::signatureToType(signature); |
| 164 | if (type == QMetaType::UnknownType && !qt_dbus_metaobject_skip_annotations) { |
| 165 | // it's not a type normally handled by our meta type system |
| 166 | // it must contain an annotation |
| 167 | QString annotationName = QString::fromLatin1(str: "org.qtproject.QtDBus.QtTypeName" ); |
| 168 | if (id >= 0) |
| 169 | annotationName += QString::fromLatin1(str: ".%1%2" ) |
| 170 | .arg(a: QLatin1String(direction)) |
| 171 | .arg(a: id); |
| 172 | |
| 173 | // extract from annotations: |
| 174 | QByteArray typeName = annotations.value(akey: annotationName).toLatin1(); |
| 175 | |
| 176 | // verify that it's a valid one |
| 177 | if (typeName.isEmpty()) { |
| 178 | // try the old annotation from Qt 4 |
| 179 | annotationName = QString::fromLatin1(str: "com.trolltech.QtDBus.QtTypeName" ); |
| 180 | if (id >= 0) |
| 181 | annotationName += QString::fromLatin1(str: ".%1%2" ) |
| 182 | .arg(a: QLatin1String(direction)) |
| 183 | .arg(a: id); |
| 184 | typeName = annotations.value(akey: annotationName).toLatin1(); |
| 185 | } |
| 186 | |
| 187 | if (!typeName.isEmpty()) { |
| 188 | // type name found |
| 189 | type = QMetaType::type(typeName); |
| 190 | } |
| 191 | |
| 192 | if (type == QMetaType::UnknownType || signature != QDBusMetaType::typeToSignature(type)) { |
| 193 | // type is still unknown or doesn't match back to the signature that it |
| 194 | // was expected to, so synthesize a fake type |
| 195 | typeName = "QDBusRawType<0x" + signature.toHex() + ">*" ; |
| 196 | type = registerComplexDBusType(typeName); |
| 197 | } |
| 198 | |
| 199 | result.name = typeName; |
| 200 | } else if (type == QMetaType::UnknownType) { |
| 201 | // this case is used only by the qdbus command-line tool |
| 202 | // invalid, let's create an impossible type that contains the signature |
| 203 | |
| 204 | if (signature == "av" ) { |
| 205 | result.name = "QVariantList" ; |
| 206 | type = QMetaType::QVariantList; |
| 207 | } else if (signature == "a{sv}" ) { |
| 208 | result.name = "QVariantMap" ; |
| 209 | type = QMetaType::QVariantMap; |
| 210 | } else if (signature == "a{ss}" ) { |
| 211 | result.name = "QMap<QString,QString>" ; |
| 212 | type = qMetaTypeId<QMap<QString, QString> >(); |
| 213 | } else { |
| 214 | result.name = "{D-Bus type \"" + signature + "\"}" ; |
| 215 | type = registerComplexDBusType(typeName: result.name); |
| 216 | } |
| 217 | } else { |
| 218 | result.name = QMetaType::typeName(type); |
| 219 | } |
| 220 | |
| 221 | result.id = type; |
| 222 | return result; // success |
| 223 | } |
| 224 | |
| 225 | void QDBusMetaObjectGenerator::parseMethods() |
| 226 | { |
| 227 | // |
| 228 | // TODO: |
| 229 | // Add cloned methods when the remote object has return types |
| 230 | // |
| 231 | |
| 232 | QDBusIntrospection::Methods::ConstIterator method_it = data->methods.constBegin(); |
| 233 | QDBusIntrospection::Methods::ConstIterator method_end = data->methods.constEnd(); |
| 234 | for ( ; method_it != method_end; ++method_it) { |
| 235 | const QDBusIntrospection::Method &m = *method_it; |
| 236 | Method mm; |
| 237 | |
| 238 | mm.name = m.name.toLatin1(); |
| 239 | QByteArray prototype = mm.name; |
| 240 | prototype += '('; |
| 241 | |
| 242 | bool ok = true; |
| 243 | |
| 244 | // build the input argument list |
| 245 | for (int i = 0; i < m.inputArgs.count(); ++i) { |
| 246 | const QDBusIntrospection::Argument &arg = m.inputArgs.at(i); |
| 247 | |
| 248 | Type type = findType(signature: arg.type.toLatin1(), annotations: m.annotations, direction: "In" , id: i); |
| 249 | if (type.id == QMetaType::UnknownType) { |
| 250 | ok = false; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | mm.inputTypes.append(t: type.id); |
| 255 | |
| 256 | mm.parameterNames.append(t: arg.name.toLatin1()); |
| 257 | |
| 258 | prototype.append(a: type.name); |
| 259 | prototype.append(c: ','); |
| 260 | } |
| 261 | if (!ok) continue; |
| 262 | |
| 263 | // build the output argument list: |
| 264 | for (int i = 0; i < m.outputArgs.count(); ++i) { |
| 265 | const QDBusIntrospection::Argument &arg = m.outputArgs.at(i); |
| 266 | |
| 267 | Type type = findType(signature: arg.type.toLatin1(), annotations: m.annotations, direction: "Out" , id: i); |
| 268 | if (type.id == QMetaType::UnknownType) { |
| 269 | ok = false; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | mm.outputTypes.append(t: type.id); |
| 274 | |
| 275 | if (i == 0 && type.id == -1) { |
| 276 | mm.rawReturnType = type.name; |
| 277 | } |
| 278 | if (i != 0) { |
| 279 | // non-const ref parameter |
| 280 | mm.parameterNames.append(t: arg.name.toLatin1()); |
| 281 | |
| 282 | prototype.append(a: type.name); |
| 283 | prototype.append(s: "&," ); |
| 284 | } |
| 285 | } |
| 286 | if (!ok) continue; |
| 287 | |
| 288 | // convert the last commas: |
| 289 | if (!mm.parameterNames.isEmpty()) |
| 290 | prototype[prototype.length() - 1] = ')'; |
| 291 | else |
| 292 | prototype.append(c: ')'); |
| 293 | |
| 294 | // check the async tag |
| 295 | if (m.annotations.value(akey: QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true" )) |
| 296 | mm.tag = "Q_NOREPLY" ; |
| 297 | |
| 298 | // meta method flags |
| 299 | mm.flags = AccessPublic | MethodSlot | MethodScriptable; |
| 300 | |
| 301 | // add |
| 302 | methods.insert(akey: QMetaObject::normalizedSignature(method: prototype), avalue: mm); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void QDBusMetaObjectGenerator::parseSignals() |
| 307 | { |
| 308 | QDBusIntrospection::Signals::ConstIterator signal_it = data->signals_.constBegin(); |
| 309 | QDBusIntrospection::Signals::ConstIterator signal_end = data->signals_.constEnd(); |
| 310 | for ( ; signal_it != signal_end; ++signal_it) { |
| 311 | const QDBusIntrospection::Signal &s = *signal_it; |
| 312 | Method mm; |
| 313 | |
| 314 | mm.name = s.name.toLatin1(); |
| 315 | QByteArray prototype = mm.name; |
| 316 | prototype += '('; |
| 317 | |
| 318 | bool ok = true; |
| 319 | |
| 320 | // build the output argument list |
| 321 | for (int i = 0; i < s.outputArgs.count(); ++i) { |
| 322 | const QDBusIntrospection::Argument &arg = s.outputArgs.at(i); |
| 323 | |
| 324 | Type type = findType(signature: arg.type.toLatin1(), annotations: s.annotations, direction: "Out" , id: i); |
| 325 | if (type.id == QMetaType::UnknownType) { |
| 326 | ok = false; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | mm.inputTypes.append(t: type.id); |
| 331 | |
| 332 | mm.parameterNames.append(t: arg.name.toLatin1()); |
| 333 | |
| 334 | prototype.append(a: type.name); |
| 335 | prototype.append(c: ','); |
| 336 | } |
| 337 | if (!ok) continue; |
| 338 | |
| 339 | // convert the last commas: |
| 340 | if (!mm.parameterNames.isEmpty()) |
| 341 | prototype[prototype.length() - 1] = ')'; |
| 342 | else |
| 343 | prototype.append(c: ')'); |
| 344 | |
| 345 | // meta method flags |
| 346 | mm.flags = AccessPublic | MethodSignal | MethodScriptable; |
| 347 | |
| 348 | // add |
| 349 | signals_.insert(akey: QMetaObject::normalizedSignature(method: prototype), avalue: mm); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void QDBusMetaObjectGenerator::parseProperties() |
| 354 | { |
| 355 | QDBusIntrospection::Properties::ConstIterator prop_it = data->properties.constBegin(); |
| 356 | QDBusIntrospection::Properties::ConstIterator prop_end = data->properties.constEnd(); |
| 357 | for ( ; prop_it != prop_end; ++prop_it) { |
| 358 | const QDBusIntrospection::Property &p = *prop_it; |
| 359 | Property mp; |
| 360 | Type type = findType(signature: p.type.toLatin1(), annotations: p.annotations); |
| 361 | if (type.id == QMetaType::UnknownType) |
| 362 | continue; |
| 363 | |
| 364 | QByteArray name = p.name.toLatin1(); |
| 365 | mp.signature = p.type.toLatin1(); |
| 366 | mp.type = type.id; |
| 367 | mp.typeName = type.name; |
| 368 | |
| 369 | // build the flags: |
| 370 | mp.flags = StdCppSet | Scriptable | Stored | Designable; |
| 371 | if (p.access != QDBusIntrospection::Property::Write) |
| 372 | mp.flags |= Readable; |
| 373 | if (p.access != QDBusIntrospection::Property::Read) |
| 374 | mp.flags |= Writable; |
| 375 | |
| 376 | // add the property: |
| 377 | properties.insert(akey: name, avalue: mp); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Returns the sum of all parameters (including return type) for the given |
| 382 | // \a map of methods. This is needed for calculating the size of the methods' |
| 383 | // parameter type/name meta-data. |
| 384 | int QDBusMetaObjectGenerator::aggregateParameterCount(const QMap<QByteArray, Method> &map) |
| 385 | { |
| 386 | int sum = 0; |
| 387 | QMap<QByteArray, Method>::const_iterator it; |
| 388 | for (it = map.constBegin(); it != map.constEnd(); ++it) { |
| 389 | const Method &m = it.value(); |
| 390 | sum += m.inputTypes.size() + qMax(a: 1, b: m.outputTypes.size()); |
| 391 | } |
| 392 | return sum; |
| 393 | } |
| 394 | |
| 395 | void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) |
| 396 | { |
| 397 | // this code here is mostly copied from qaxbase.cpp |
| 398 | // with a few modifications to make it cleaner |
| 399 | |
| 400 | QString className = interface; |
| 401 | className.replace(c: QLatin1Char('.'), after: QLatin1String("::" )); |
| 402 | if (className.isEmpty()) |
| 403 | className = QLatin1String("QDBusInterface" ); |
| 404 | |
| 405 | QVarLengthArray<int> idata; |
| 406 | idata.resize(asize: sizeof(QDBusMetaObjectPrivate) / sizeof(int)); |
| 407 | |
| 408 | int methodParametersDataSize = |
| 409 | ((aggregateParameterCount(map: signals_) |
| 410 | + aggregateParameterCount(map: methods)) * 2) // types and parameter names |
| 411 | - signals_.count() // return "parameters" don't have names |
| 412 | - methods.count(); // ditto |
| 413 | |
| 414 | QDBusMetaObjectPrivate * = reinterpret_cast<QDBusMetaObjectPrivate *>(idata.data()); |
| 415 | Q_STATIC_ASSERT_X(QMetaObjectPrivate::OutputRevision == 8, "QtDBus meta-object generator should generate the same version as moc" ); |
| 416 | header->revision = QMetaObjectPrivate::OutputRevision; |
| 417 | header->className = 0; |
| 418 | header->classInfoCount = 0; |
| 419 | header->classInfoData = 0; |
| 420 | header->methodCount = signals_.count() + methods.count(); |
| 421 | header->methodData = idata.size(); |
| 422 | header->propertyCount = properties.count(); |
| 423 | header->propertyData = header->methodData + header->methodCount * 5 + methodParametersDataSize; |
| 424 | header->enumeratorCount = 0; |
| 425 | header->enumeratorData = 0; |
| 426 | header->constructorCount = 0; |
| 427 | header->constructorData = 0; |
| 428 | header->flags = RequiresVariantMetaObject; |
| 429 | header->signalCount = signals_.count(); |
| 430 | // These are specific to QDBusMetaObject: |
| 431 | header->propertyDBusData = header->propertyData + header->propertyCount * 3; |
| 432 | header->methodDBusData = header->propertyDBusData + header->propertyCount * intsPerProperty; |
| 433 | |
| 434 | int data_size = idata.size() + |
| 435 | (header->methodCount * (5+intsPerMethod)) + methodParametersDataSize + |
| 436 | (header->propertyCount * (3+intsPerProperty)); |
| 437 | for (const Method &mm : qAsConst(t&: signals_)) |
| 438 | data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); |
| 439 | for (const Method &mm : qAsConst(t&: methods)) |
| 440 | data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); |
| 441 | idata.resize(asize: data_size + 1); |
| 442 | |
| 443 | QMetaStringTable strings(className.toLatin1()); |
| 444 | |
| 445 | int offset = header->methodData; |
| 446 | int parametersOffset = offset + header->methodCount * 5; |
| 447 | int signatureOffset = header->methodDBusData; |
| 448 | int typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod; |
| 449 | idata[typeidOffset++] = 0; // eod |
| 450 | |
| 451 | // add each method: |
| 452 | for (int x = 0; x < 2; ++x) { |
| 453 | // Signals must be added before other methods, to match moc. |
| 454 | QMap<QByteArray, Method> &map = (x == 0) ? signals_ : methods; |
| 455 | for (QMap<QByteArray, Method>::ConstIterator it = map.constBegin(); |
| 456 | it != map.constEnd(); ++it) { |
| 457 | const Method &mm = it.value(); |
| 458 | |
| 459 | int argc = mm.inputTypes.size() + qMax(a: 0, b: mm.outputTypes.size() - 1); |
| 460 | |
| 461 | idata[offset++] = strings.enter(value: mm.name); |
| 462 | idata[offset++] = argc; |
| 463 | idata[offset++] = parametersOffset; |
| 464 | idata[offset++] = strings.enter(value: mm.tag); |
| 465 | idata[offset++] = mm.flags; |
| 466 | |
| 467 | // Parameter types |
| 468 | for (int i = -1; i < argc; ++i) { |
| 469 | int type; |
| 470 | QByteArray typeName; |
| 471 | if (i < 0) { // Return type |
| 472 | if (!mm.outputTypes.isEmpty()) { |
| 473 | type = mm.outputTypes.first(); |
| 474 | if (type == -1) { |
| 475 | type = IsUnresolvedType | strings.enter(value: mm.rawReturnType); |
| 476 | } |
| 477 | } else { |
| 478 | type = QMetaType::Void; |
| 479 | } |
| 480 | } else if (i < mm.inputTypes.size()) { |
| 481 | type = mm.inputTypes.at(idx: i); |
| 482 | } else { |
| 483 | Q_ASSERT(mm.outputTypes.size() > 1); |
| 484 | type = mm.outputTypes.at(idx: i - mm.inputTypes.size() + 1); |
| 485 | // Output parameters are references; type id not available |
| 486 | typeName = QMetaType::typeName(type); |
| 487 | typeName.append(c: '&'); |
| 488 | } |
| 489 | Q_ASSERT(type != QMetaType::UnknownType); |
| 490 | int typeInfo; |
| 491 | if (!typeName.isEmpty()) |
| 492 | typeInfo = IsUnresolvedType | strings.enter(value: typeName); |
| 493 | else |
| 494 | typeInfo = type; |
| 495 | idata[parametersOffset++] = typeInfo; |
| 496 | } |
| 497 | // Parameter names |
| 498 | for (int i = 0; i < argc; ++i) |
| 499 | idata[parametersOffset++] = strings.enter(value: mm.parameterNames.at(i)); |
| 500 | |
| 501 | idata[signatureOffset++] = typeidOffset; |
| 502 | idata[typeidOffset++] = mm.inputTypes.count(); |
| 503 | memcpy(dest: idata.data() + typeidOffset, src: mm.inputTypes.data(), n: mm.inputTypes.count() * sizeof(int)); |
| 504 | typeidOffset += mm.inputTypes.count(); |
| 505 | |
| 506 | idata[signatureOffset++] = typeidOffset; |
| 507 | idata[typeidOffset++] = mm.outputTypes.count(); |
| 508 | memcpy(dest: idata.data() + typeidOffset, src: mm.outputTypes.data(), n: mm.outputTypes.count() * sizeof(int)); |
| 509 | typeidOffset += mm.outputTypes.count(); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | Q_ASSERT(offset == header->methodData + header->methodCount * 5); |
| 514 | Q_ASSERT(parametersOffset == header->propertyData); |
| 515 | Q_ASSERT(signatureOffset == header->methodDBusData + header->methodCount * intsPerMethod); |
| 516 | Q_ASSERT(typeidOffset == idata.size()); |
| 517 | offset += methodParametersDataSize; |
| 518 | Q_ASSERT(offset == header->propertyData); |
| 519 | |
| 520 | // add each property |
| 521 | signatureOffset = header->propertyDBusData; |
| 522 | for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin(); |
| 523 | it != properties.constEnd(); ++it) { |
| 524 | const Property &mp = it.value(); |
| 525 | |
| 526 | // form is name, typeinfo, flags |
| 527 | idata[offset++] = strings.enter(value: it.key()); // name |
| 528 | Q_ASSERT(mp.type != QMetaType::UnknownType); |
| 529 | idata[offset++] = mp.type; |
| 530 | idata[offset++] = mp.flags; |
| 531 | |
| 532 | idata[signatureOffset++] = strings.enter(value: mp.signature); |
| 533 | idata[signatureOffset++] = mp.type; |
| 534 | } |
| 535 | |
| 536 | Q_ASSERT(offset == header->propertyDBusData); |
| 537 | Q_ASSERT(signatureOffset == header->methodDBusData); |
| 538 | |
| 539 | char *string_data = new char[strings.blobSize()]; |
| 540 | strings.writeBlob(out: string_data); |
| 541 | |
| 542 | uint *uint_data = new uint[idata.size()]; |
| 543 | memcpy(dest: uint_data, src: idata.data(), n: idata.size() * sizeof(int)); |
| 544 | |
| 545 | // put the metaobject together |
| 546 | obj->d.data = uint_data; |
| 547 | obj->d.relatedMetaObjects = nullptr; |
| 548 | obj->d.static_metacall = nullptr; |
| 549 | obj->d.extradata = nullptr; |
| 550 | obj->d.stringdata = reinterpret_cast<const QByteArrayData *>(string_data); |
| 551 | obj->d.superdata = &QDBusAbstractInterface::staticMetaObject; |
| 552 | } |
| 553 | |
| 554 | #if 0 |
| 555 | void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface) |
| 556 | { |
| 557 | // no XML definition |
| 558 | QString tmp(interface); |
| 559 | tmp.replace(QLatin1Char('.'), QLatin1String("::" )); |
| 560 | QByteArray name(tmp.toLatin1()); |
| 561 | |
| 562 | QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate; |
| 563 | memset(header, 0, sizeof *header); |
| 564 | header->revision = 1; |
| 565 | // leave the rest with 0 |
| 566 | |
| 567 | char *stringdata = new char[name.length() + 1]; |
| 568 | stringdata[name.length()] = '\0'; |
| 569 | |
| 570 | d.data = reinterpret_cast<uint*>(header); |
| 571 | d.relatedMetaObjects = 0; |
| 572 | d.static_metacall = 0; |
| 573 | d.extradata = 0; |
| 574 | d.stringdata = stringdata; |
| 575 | d.superdata = &QDBusAbstractInterface::staticMetaObject; |
| 576 | cached = false; |
| 577 | } |
| 578 | #endif |
| 579 | |
| 580 | ///////// |
| 581 | // class QDBusMetaObject |
| 582 | |
| 583 | QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, const QString &xml, |
| 584 | QHash<QString, QDBusMetaObject *> &cache, |
| 585 | QDBusError &error) |
| 586 | { |
| 587 | error = QDBusError(); |
| 588 | QDBusIntrospection::Interfaces parsed = QDBusIntrospection::parseInterfaces(xml); |
| 589 | |
| 590 | QDBusMetaObject *we = nullptr; |
| 591 | QDBusIntrospection::Interfaces::ConstIterator it = parsed.constBegin(); |
| 592 | QDBusIntrospection::Interfaces::ConstIterator end = parsed.constEnd(); |
| 593 | for ( ; it != end; ++it) { |
| 594 | // check if it's in the cache |
| 595 | bool us = it.key() == interface; |
| 596 | |
| 597 | QDBusMetaObject *obj = cache.value(akey: it.key(), adefaultValue: 0); |
| 598 | if ( !obj && ( us || !interface.startsWith( s: QLatin1String("local." ) ) ) ) { |
| 599 | // not in cache; create |
| 600 | obj = new QDBusMetaObject; |
| 601 | QDBusMetaObjectGenerator generator(it.key(), it.value().constData()); |
| 602 | generator.write(obj); |
| 603 | |
| 604 | if ( (obj->cached = !it.key().startsWith( s: QLatin1String("local." ) )) ) |
| 605 | // cache it |
| 606 | cache.insert(akey: it.key(), avalue: obj); |
| 607 | else if (!us) |
| 608 | delete obj; |
| 609 | |
| 610 | } |
| 611 | |
| 612 | if (us) |
| 613 | // it's us |
| 614 | we = obj; |
| 615 | } |
| 616 | |
| 617 | if (we) |
| 618 | return we; |
| 619 | // still nothing? |
| 620 | |
| 621 | if (parsed.isEmpty()) { |
| 622 | // object didn't return introspection |
| 623 | we = new QDBusMetaObject; |
| 624 | QDBusMetaObjectGenerator generator(interface, nullptr); |
| 625 | generator.write(obj: we); |
| 626 | we->cached = false; |
| 627 | return we; |
| 628 | } else if (interface.isEmpty()) { |
| 629 | // merge all interfaces |
| 630 | it = parsed.constBegin(); |
| 631 | QDBusIntrospection::Interface merged = *it.value().constData(); |
| 632 | |
| 633 | for (++it; it != end; ++it) { |
| 634 | merged.annotations.insert(map: it.value()->annotations); |
| 635 | merged.methods.unite(other: it.value()->methods); |
| 636 | merged.signals_.unite(other: it.value()->signals_); |
| 637 | merged.properties.insert(map: it.value()->properties); |
| 638 | } |
| 639 | |
| 640 | merged.name = QLatin1String("local.Merged" ); |
| 641 | merged.introspection.clear(); |
| 642 | |
| 643 | we = new QDBusMetaObject; |
| 644 | QDBusMetaObjectGenerator generator(merged.name, &merged); |
| 645 | generator.write(obj: we); |
| 646 | we->cached = false; |
| 647 | return we; |
| 648 | } |
| 649 | |
| 650 | // mark as an error |
| 651 | error = QDBusError(QDBusError::UnknownInterface, |
| 652 | QLatin1String("Interface '%1' was not found" ) |
| 653 | .arg(args: interface)); |
| 654 | return nullptr; |
| 655 | } |
| 656 | |
| 657 | QDBusMetaObject::QDBusMetaObject() |
| 658 | { |
| 659 | } |
| 660 | |
| 661 | static inline const QDBusMetaObjectPrivate *priv(const uint* data) |
| 662 | { |
| 663 | return reinterpret_cast<const QDBusMetaObjectPrivate *>(data); |
| 664 | } |
| 665 | |
| 666 | const int *QDBusMetaObject::inputTypesForMethod(int id) const |
| 667 | { |
| 668 | //id -= methodOffset(); |
| 669 | if (id >= 0 && id < priv(data: d.data)->methodCount) { |
| 670 | int handle = priv(data: d.data)->methodDBusData + id*intsPerMethod; |
| 671 | return reinterpret_cast<const int*>(d.data + d.data[handle]); |
| 672 | } |
| 673 | return nullptr; |
| 674 | } |
| 675 | |
| 676 | const int *QDBusMetaObject::outputTypesForMethod(int id) const |
| 677 | { |
| 678 | //id -= methodOffset(); |
| 679 | if (id >= 0 && id < priv(data: d.data)->methodCount) { |
| 680 | int handle = priv(data: d.data)->methodDBusData + id*intsPerMethod; |
| 681 | return reinterpret_cast<const int*>(d.data + d.data[handle + 1]); |
| 682 | } |
| 683 | return nullptr; |
| 684 | } |
| 685 | |
| 686 | int QDBusMetaObject::propertyMetaType(int id) const |
| 687 | { |
| 688 | //id -= propertyOffset(); |
| 689 | if (id >= 0 && id < priv(data: d.data)->propertyCount) { |
| 690 | int handle = priv(data: d.data)->propertyDBusData + id*intsPerProperty; |
| 691 | return d.data[handle + 1]; |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | QT_END_NAMESPACE |
| 697 | |
| 698 | #endif // QT_NO_DBUS |
| 699 | |