| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include <qbytearray.h> |
| 5 | #include <qcommandlineparser.h> |
| 6 | #include <qcoreapplication.h> |
| 7 | #include <qdebug.h> |
| 8 | #include <qfile.h> |
| 9 | #include <qfileinfo.h> |
| 10 | #include <qloggingcategory.h> |
| 11 | #include <qstring.h> |
| 12 | #include <qstringlist.h> |
| 13 | #include <qtextstream.h> |
| 14 | #include <qset.h> |
| 15 | |
| 16 | #include <qdbusmetatype.h> |
| 17 | #include <private/qdbusintrospection_p.h> |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | #define PROGRAMNAME "qdbusxml2cpp" |
| 23 | #define PROGRAMVERSION "0.8" |
| 24 | #define PROGRAMCOPYRIGHT QT_COPYRIGHT |
| 25 | |
| 26 | #define ANNOTATION_NO_WAIT "org.freedesktop.DBus.Method.NoReply" |
| 27 | |
| 28 | using namespace Qt::StringLiterals; |
| 29 | |
| 30 | class QDBusXmlToCpp final |
| 31 | { |
| 32 | public: |
| 33 | int run(const QCoreApplication &app); |
| 34 | |
| 35 | private: |
| 36 | class DiagnosticsReporter final : public QDBusIntrospection::DiagnosticsReporter |
| 37 | { |
| 38 | public: |
| 39 | void setFileName(const QString &fileName) { m_fileName = fileName; } |
| 40 | bool hadErrors() const { return m_hadErrors; } |
| 41 | |
| 42 | void warning(const QDBusIntrospection::SourceLocation &location, const char *msg, |
| 43 | ...) override; |
| 44 | void error(const QDBusIntrospection::SourceLocation &location, const char *msg, |
| 45 | ...) override; |
| 46 | void note(const QDBusIntrospection::SourceLocation &location, const char *msg, ...) |
| 47 | Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
| 48 | |
| 49 | private: |
| 50 | QString m_fileName; |
| 51 | bool m_hadErrors = false; |
| 52 | |
| 53 | void report(const QDBusIntrospection::SourceLocation &location, const char *msg, va_list ap, |
| 54 | const char *severity); |
| 55 | }; |
| 56 | |
| 57 | enum ClassType { Proxy, Adaptor }; |
| 58 | |
| 59 | void writeAdaptor(const QString &filename, const QDBusIntrospection::Interfaces &interfaces); |
| 60 | void writeProxy(const QString &filename, const QDBusIntrospection::Interfaces &interfaces); |
| 61 | |
| 62 | QDBusIntrospection::Interfaces readInput(); |
| 63 | void cleanInterfaces(QDBusIntrospection::Interfaces &interfaces); |
| 64 | QTextStream &writeHeader(QTextStream &ts, bool changesWillBeLost); |
| 65 | QString classNameForInterface(const QString &interface, ClassType classType); |
| 66 | QByteArray qtTypeName(const QDBusIntrospection::SourceLocation &location, |
| 67 | const QString &signature, |
| 68 | const QDBusIntrospection::Annotations &annotations, |
| 69 | qsizetype paramId = -1, const char *direction = "Out" ); |
| 70 | void |
| 71 | writeArgList(QTextStream &ts, const QStringList &argNames, |
| 72 | const QDBusIntrospection::Annotations &annotations, |
| 73 | const QDBusIntrospection::Arguments &inputArgs, |
| 74 | const QDBusIntrospection::Arguments &outputArgs = QDBusIntrospection::Arguments()); |
| 75 | void writeSignalArgList(QTextStream &ts, const QStringList &argNames, |
| 76 | const QDBusIntrospection::Annotations &annotations, |
| 77 | const QDBusIntrospection::Arguments &outputArgs); |
| 78 | QString propertyGetter(const QDBusIntrospection::Property &property); |
| 79 | QString propertySetter(const QDBusIntrospection::Property &property); |
| 80 | |
| 81 | QString globalClassName; |
| 82 | QString parentClassName; |
| 83 | QString customNamespace; |
| 84 | QString inputFile; |
| 85 | bool skipNamespaces = false; |
| 86 | bool includeMocs = false; |
| 87 | QString commandLine; |
| 88 | QStringList includes; |
| 89 | QStringList globalIncludes; |
| 90 | QStringList wantedInterfaces; |
| 91 | |
| 92 | DiagnosticsReporter reporter; |
| 93 | }; |
| 94 | |
| 95 | static const char includeList[] = |
| 96 | "#include <QtCore/QByteArray>\n" |
| 97 | "#include <QtCore/QList>\n" |
| 98 | "#include <QtCore/QMap>\n" |
| 99 | "#include <QtCore/QString>\n" |
| 100 | "#include <QtCore/QStringList>\n" |
| 101 | "#include <QtCore/QVariant>\n" ; |
| 102 | |
| 103 | static const char forwardDeclarations[] = |
| 104 | "#include <QtCore/qcontainerfwd.h>\n" ; |
| 105 | |
| 106 | void QDBusXmlToCpp::DiagnosticsReporter::warning(const QDBusIntrospection::SourceLocation &location, |
| 107 | const char *msg, ...) |
| 108 | { |
| 109 | va_list ap; |
| 110 | va_start(ap, msg); |
| 111 | report(location, msg, ap, severity: "warning" ); |
| 112 | va_end(ap); |
| 113 | } |
| 114 | |
| 115 | void QDBusXmlToCpp::DiagnosticsReporter::error(const QDBusIntrospection::SourceLocation &location, |
| 116 | const char *msg, ...) |
| 117 | { |
| 118 | va_list ap; |
| 119 | va_start(ap, msg); |
| 120 | report(location, msg, ap, severity: "error" ); |
| 121 | va_end(ap); |
| 122 | m_hadErrors = true; |
| 123 | } |
| 124 | |
| 125 | void QDBusXmlToCpp::DiagnosticsReporter::note(const QDBusIntrospection::SourceLocation &location, |
| 126 | const char *msg, ...) |
| 127 | { |
| 128 | va_list ap; |
| 129 | va_start(ap, msg); |
| 130 | report(location, msg, ap, severity: "note" ); |
| 131 | va_end(ap); |
| 132 | m_hadErrors = true; |
| 133 | } |
| 134 | |
| 135 | void QDBusXmlToCpp::DiagnosticsReporter::report(const QDBusIntrospection::SourceLocation &location, |
| 136 | const char *msg, va_list ap, const char *severity) |
| 137 | { |
| 138 | fprintf(stderr, format: "%s:%lld:%lld: %s: " , qPrintable(m_fileName), |
| 139 | (long long int)location.lineNumber, (long long int)location.columnNumber + 1, severity); |
| 140 | vfprintf(stderr, format: msg, arg: ap); |
| 141 | } |
| 142 | |
| 143 | QDBusIntrospection::Interfaces QDBusXmlToCpp::readInput() |
| 144 | { |
| 145 | QFile input(inputFile); |
| 146 | if (inputFile.isEmpty() || inputFile == "-"_L1 ) { |
| 147 | reporter.setFileName("<standard input>"_L1 ); |
| 148 | if (!input.open(stdin, ioFlags: QIODevice::ReadOnly)) { |
| 149 | fprintf(stderr, PROGRAMNAME ": could not open standard input: %s\n" , |
| 150 | qPrintable(input.errorString())); |
| 151 | exit(status: 1); |
| 152 | } |
| 153 | } else { |
| 154 | reporter.setFileName(inputFile); |
| 155 | if (!input.open(flags: QIODevice::ReadOnly)) { |
| 156 | fprintf(stderr, PROGRAMNAME ": could not open input file '%s': %s\n" , |
| 157 | qPrintable(inputFile), qPrintable(input.errorString())); |
| 158 | exit(status: 1); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | QByteArray data = input.readAll(); |
| 163 | auto interfaces = QDBusIntrospection::parseInterfaces(xml: QString::fromUtf8(ba: data), reporter: &reporter); |
| 164 | if (reporter.hadErrors()) |
| 165 | exit(status: 1); |
| 166 | |
| 167 | return interfaces; |
| 168 | } |
| 169 | |
| 170 | void QDBusXmlToCpp::cleanInterfaces(QDBusIntrospection::Interfaces &interfaces) |
| 171 | { |
| 172 | if (!wantedInterfaces.isEmpty()) { |
| 173 | QDBusIntrospection::Interfaces::Iterator it = interfaces.begin(); |
| 174 | while (it != interfaces.end()) |
| 175 | if (!wantedInterfaces.contains(str: it.key())) |
| 176 | it = interfaces.erase(it); |
| 177 | else |
| 178 | ++it; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static bool isSupportedSuffix(QStringView suffix) |
| 183 | { |
| 184 | const QLatin1StringView candidates[] = { |
| 185 | "h"_L1 , |
| 186 | "cpp"_L1 , |
| 187 | "cc"_L1 |
| 188 | }; |
| 189 | |
| 190 | for (auto candidate : candidates) |
| 191 | if (suffix == candidate) |
| 192 | return true; |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | // produce a header name from the file name |
| 198 | static QString (const QString &name) |
| 199 | { |
| 200 | QStringList parts = name.split(sep: u':'); |
| 201 | QString retval = parts.front(); |
| 202 | |
| 203 | if (retval.isEmpty() || retval == "-"_L1 ) |
| 204 | return retval; |
| 205 | |
| 206 | QFileInfo {retval}; |
| 207 | if (!isSupportedSuffix(suffix: header.suffix())) |
| 208 | retval.append(s: ".h"_L1 ); |
| 209 | |
| 210 | return retval; |
| 211 | } |
| 212 | |
| 213 | // produce a cpp name from the file name |
| 214 | static QString cpp(const QString &name) |
| 215 | { |
| 216 | QStringList parts = name.split(sep: u':'); |
| 217 | QString retval = parts.back(); |
| 218 | |
| 219 | if (retval.isEmpty() || retval == "-"_L1 ) |
| 220 | return retval; |
| 221 | |
| 222 | QFileInfo source{retval}; |
| 223 | if (!isSupportedSuffix(suffix: source.suffix())) |
| 224 | retval.append(s: ".cpp"_L1 ); |
| 225 | |
| 226 | return retval; |
| 227 | } |
| 228 | |
| 229 | // produce a moc name from the file name |
| 230 | static QString moc(const QString &name) |
| 231 | { |
| 232 | QString retval; |
| 233 | const QStringList fileNames = name.split(sep: u':'); |
| 234 | |
| 235 | if (fileNames.size() == 1) { |
| 236 | QFileInfo fi{fileNames.front()}; |
| 237 | if (isSupportedSuffix(suffix: fi.suffix())) { |
| 238 | // Generates a file that contains the header and the implementation: include "filename.moc" |
| 239 | retval += fi.completeBaseName(); |
| 240 | retval += ".moc"_L1 ; |
| 241 | } else { |
| 242 | // Separate source and header files are generated: include "moc_filename.cpp" |
| 243 | retval += "moc_"_L1 ; |
| 244 | retval += fi.fileName(); |
| 245 | retval += ".cpp"_L1 ; |
| 246 | } |
| 247 | } else { |
| 248 | QString = fileNames.front(); |
| 249 | QString sourceName = fileNames.back(); |
| 250 | |
| 251 | if (sourceName.isEmpty() || sourceName == "-"_L1 ) { |
| 252 | // If only a header is generated, don't include anything |
| 253 | } else if (headerName.isEmpty() || headerName == "-"_L1 ) { |
| 254 | // If only source file is generated: include "moc_sourcename.cpp" |
| 255 | QFileInfo source{sourceName}; |
| 256 | |
| 257 | retval += "moc_"_L1 ; |
| 258 | retval += source.completeBaseName(); |
| 259 | retval += ".cpp"_L1 ; |
| 260 | |
| 261 | fprintf(stderr, format: "warning: no header name is provided, assuming it to be \"%s\"\n" , |
| 262 | qPrintable(source.completeBaseName() + ".h"_L1 )); |
| 263 | } else { |
| 264 | // Both source and header generated: include "moc_headername.cpp" |
| 265 | QFileInfo {headerName}; |
| 266 | |
| 267 | retval += "moc_"_L1 ; |
| 268 | retval += header.completeBaseName(); |
| 269 | retval += ".cpp"_L1 ; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return retval; |
| 274 | } |
| 275 | |
| 276 | QTextStream &QDBusXmlToCpp::(QTextStream &ts, bool changesWillBeLost) |
| 277 | { |
| 278 | ts << "/*\n" |
| 279 | " * This file was generated by " PROGRAMNAME " version " PROGRAMVERSION "\n" |
| 280 | " * Source file was " << QFileInfo(inputFile).fileName() << "\n" |
| 281 | " *\n" |
| 282 | " * " PROGRAMNAME " is " PROGRAMCOPYRIGHT "\n" |
| 283 | " *\n" |
| 284 | " * This is an auto-generated file.\n" ; |
| 285 | |
| 286 | if (changesWillBeLost) |
| 287 | ts << " * Do not edit! All changes made to it will be lost.\n" ; |
| 288 | else |
| 289 | ts << " * This file may have been hand-edited. Look for HAND-EDIT comments\n" |
| 290 | " * before re-generating it.\n" ; |
| 291 | |
| 292 | ts << " */\n\n" ; |
| 293 | |
| 294 | return ts; |
| 295 | } |
| 296 | |
| 297 | QString QDBusXmlToCpp::classNameForInterface(const QString &interface, |
| 298 | QDBusXmlToCpp::ClassType classType) |
| 299 | { |
| 300 | if (!globalClassName.isEmpty()) |
| 301 | return globalClassName; |
| 302 | |
| 303 | const auto parts = QStringView{interface}.split(sep: u'.'); |
| 304 | |
| 305 | QString retval; |
| 306 | if (classType == Proxy) { |
| 307 | for (const auto &part : parts) { |
| 308 | retval += part[0].toUpper(); |
| 309 | retval += part.mid(pos: 1); |
| 310 | } |
| 311 | } else { |
| 312 | retval += parts.last()[0].toUpper() + parts.last().mid(pos: 1); |
| 313 | } |
| 314 | |
| 315 | if (classType == Proxy) |
| 316 | retval += "Interface"_L1 ; |
| 317 | else |
| 318 | retval += "Adaptor"_L1 ; |
| 319 | |
| 320 | return retval; |
| 321 | } |
| 322 | |
| 323 | QByteArray QDBusXmlToCpp::qtTypeName(const QDBusIntrospection::SourceLocation &location, |
| 324 | const QString &signature, |
| 325 | const QDBusIntrospection::Annotations &annotations, |
| 326 | qsizetype paramId, const char *direction) |
| 327 | { |
| 328 | int type = QDBusMetaType::signatureToMetaType(signature: signature.toLatin1()).id(); |
| 329 | if (type == QMetaType::UnknownType) { |
| 330 | QString annotationName = u"org.qtproject.QtDBus.QtTypeName"_s ; |
| 331 | if (paramId >= 0) |
| 332 | annotationName += ".%1%2"_L1 .arg(args: QLatin1StringView(direction)).arg(a: paramId); |
| 333 | auto annotation = annotations.value(key: annotationName); |
| 334 | QString qttype = annotation.value; |
| 335 | if (!qttype.isEmpty()) |
| 336 | return std::move(qttype).toLatin1(); |
| 337 | |
| 338 | QString oldAnnotationName = u"com.trolltech.QtDBus.QtTypeName"_s ; |
| 339 | if (paramId >= 0) |
| 340 | oldAnnotationName += ".%1%2"_L1 .arg(args: QLatin1StringView(direction)).arg(a: paramId); |
| 341 | annotation = annotations.value(key: oldAnnotationName); |
| 342 | qttype = annotation.value; |
| 343 | |
| 344 | if (qttype.isEmpty()) { |
| 345 | reporter.error(location, msg: "unknown type `%s'\n" , qPrintable(signature)); |
| 346 | reporter.note(location, msg: "you should add <annotation name=\"%s\" value=\"<type>\"/>\n" , |
| 347 | qPrintable(annotationName)); |
| 348 | |
| 349 | exit(status: 1); |
| 350 | } |
| 351 | |
| 352 | reporter.warning(location: annotation.location, msg: "deprecated annotation '%s' found\n" , |
| 353 | qPrintable(oldAnnotationName)); |
| 354 | reporter.note(location: annotation.location, msg: "suggest updating to '%s'\n" , |
| 355 | qPrintable(annotationName)); |
| 356 | return std::move(qttype).toLatin1(); |
| 357 | } |
| 358 | |
| 359 | return QMetaType(type).name(); |
| 360 | } |
| 361 | |
| 362 | static QString nonConstRefArg(const QByteArray &arg) |
| 363 | { |
| 364 | return QLatin1StringView(arg) + " &"_L1 ; |
| 365 | } |
| 366 | |
| 367 | static QString templateArg(const QByteArray &arg) |
| 368 | { |
| 369 | if (!arg.endsWith(c: '>')) |
| 370 | return QLatin1StringView(arg); |
| 371 | |
| 372 | return QLatin1StringView(arg) + " "_L1 ; |
| 373 | } |
| 374 | |
| 375 | static QString constRefArg(const QByteArray &arg) |
| 376 | { |
| 377 | if (!arg.startsWith(c: 'Q')) |
| 378 | return QLatin1StringView(arg) + " "_L1 ; |
| 379 | else |
| 380 | return "const %1 &"_L1 .arg(args: QLatin1StringView(arg)); |
| 381 | } |
| 382 | |
| 383 | static QStringList makeArgNames(const QDBusIntrospection::Arguments &inputArgs, |
| 384 | const QDBusIntrospection::Arguments &outputArgs = |
| 385 | QDBusIntrospection::Arguments()) |
| 386 | { |
| 387 | QStringList retval; |
| 388 | const qsizetype numInputArgs = inputArgs.size(); |
| 389 | const qsizetype numOutputArgs = outputArgs.size(); |
| 390 | retval.reserve(asize: numInputArgs + numOutputArgs); |
| 391 | for (qsizetype i = 0; i < numInputArgs; ++i) { |
| 392 | const QDBusIntrospection::Argument &arg = inputArgs.at(i); |
| 393 | QString name = arg.name; |
| 394 | if (name.isEmpty()) |
| 395 | name = u"in%1"_s .arg(a: i); |
| 396 | else |
| 397 | name.replace(before: u'-', after: u'_'); |
| 398 | while (retval.contains(str: name)) |
| 399 | name += "_"_L1 ; |
| 400 | retval << name; |
| 401 | } |
| 402 | for (qsizetype i = 0; i < numOutputArgs; ++i) { |
| 403 | const QDBusIntrospection::Argument &arg = outputArgs.at(i); |
| 404 | QString name = arg.name; |
| 405 | if (name.isEmpty()) |
| 406 | name = u"out%1"_s .arg(a: i); |
| 407 | else |
| 408 | name.replace(before: u'-', after: u'_'); |
| 409 | while (retval.contains(str: name)) |
| 410 | name += "_"_L1 ; |
| 411 | retval << name; |
| 412 | } |
| 413 | return retval; |
| 414 | } |
| 415 | |
| 416 | void QDBusXmlToCpp::writeArgList(QTextStream &ts, const QStringList &argNames, |
| 417 | const QDBusIntrospection::Annotations &annotations, |
| 418 | const QDBusIntrospection::Arguments &inputArgs, |
| 419 | const QDBusIntrospection::Arguments &outputArgs) |
| 420 | { |
| 421 | // input args: |
| 422 | bool first = true; |
| 423 | qsizetype argPos = 0; |
| 424 | for (qsizetype i = 0; i < inputArgs.size(); ++i) { |
| 425 | const QDBusIntrospection::Argument &arg = inputArgs.at(i); |
| 426 | QString type = constRefArg(arg: qtTypeName(location: arg.location, signature: arg.type, annotations, paramId: i, direction: "In" )); |
| 427 | |
| 428 | if (!first) |
| 429 | ts << ", " ; |
| 430 | ts << type << argNames.at(i: argPos++); |
| 431 | first = false; |
| 432 | } |
| 433 | |
| 434 | argPos++; |
| 435 | |
| 436 | // output args |
| 437 | // yes, starting from 1 |
| 438 | for (qsizetype i = 1; i < outputArgs.size(); ++i) { |
| 439 | const QDBusIntrospection::Argument &arg = outputArgs.at(i); |
| 440 | |
| 441 | if (!first) |
| 442 | ts << ", " ; |
| 443 | ts << nonConstRefArg(arg: qtTypeName(location: arg.location, signature: arg.type, annotations, paramId: i, direction: "Out" )) |
| 444 | << argNames.at(i: argPos++); |
| 445 | first = false; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void QDBusXmlToCpp::writeSignalArgList(QTextStream &ts, const QStringList &argNames, |
| 450 | const QDBusIntrospection::Annotations &annotations, |
| 451 | const QDBusIntrospection::Arguments &outputArgs) |
| 452 | { |
| 453 | bool first = true; |
| 454 | qsizetype argPos = 0; |
| 455 | for (qsizetype i = 0; i < outputArgs.size(); ++i) { |
| 456 | const QDBusIntrospection::Argument &arg = outputArgs.at(i); |
| 457 | QString type = constRefArg(arg: qtTypeName(location: arg.location, signature: arg.type, annotations, paramId: i, direction: "Out" )); |
| 458 | |
| 459 | if (!first) |
| 460 | ts << ", " ; |
| 461 | ts << type << argNames.at(i: argPos++); |
| 462 | first = false; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | QString QDBusXmlToCpp::propertyGetter(const QDBusIntrospection::Property &property) |
| 467 | { |
| 468 | auto annotation = property.annotations.value(key: "org.qtproject.QtDBus.PropertyGetter"_L1 ); |
| 469 | if (!annotation.value.isEmpty()) |
| 470 | return annotation.value; |
| 471 | |
| 472 | annotation = property.annotations.value(key: "com.trolltech.QtDBus.propertyGetter"_L1 ); |
| 473 | if (!annotation.value.isEmpty()) { |
| 474 | reporter.warning(location: annotation.location, |
| 475 | msg: "deprecated annotation 'com.trolltech.QtDBus.propertyGetter' found\n" ); |
| 476 | reporter.note(location: annotation.location, |
| 477 | msg: "suggest updating to 'org.qtproject.QtDBus.PropertyGetter'\n" ); |
| 478 | return annotation.value; |
| 479 | } |
| 480 | |
| 481 | QString getter = property.name; |
| 482 | getter[0] = getter[0].toLower(); |
| 483 | return getter; |
| 484 | } |
| 485 | |
| 486 | QString QDBusXmlToCpp::propertySetter(const QDBusIntrospection::Property &property) |
| 487 | { |
| 488 | auto annotation = property.annotations.value(key: "org.qtproject.QtDBus.PropertySetter"_L1 ); |
| 489 | if (!annotation.value.isEmpty()) |
| 490 | return annotation.value; |
| 491 | |
| 492 | annotation = property.annotations.value(key: "com.trolltech.QtDBus.propertySetter"_L1 ); |
| 493 | if (!annotation.value.isEmpty()) { |
| 494 | reporter.warning(location: annotation.location, |
| 495 | msg: "deprecated annotation 'com.trolltech.QtDBus.propertySetter' found\n" ); |
| 496 | reporter.note(location: annotation.location, |
| 497 | msg: "suggest updating to 'org.qtproject.QtDBus.PropertySetter'\n" ); |
| 498 | return annotation.value; |
| 499 | } |
| 500 | |
| 501 | QString setter = "set"_L1 + property.name; |
| 502 | setter[3] = setter[3].toUpper(); |
| 503 | return setter; |
| 504 | } |
| 505 | |
| 506 | static QString methodName(const QDBusIntrospection::Method &method) |
| 507 | { |
| 508 | QString name = method.annotations.value(key: u"org.qtproject.QtDBus.MethodName"_s ).value; |
| 509 | if (!name.isEmpty()) |
| 510 | return name; |
| 511 | |
| 512 | return method.name; |
| 513 | } |
| 514 | |
| 515 | static QString stringify(const QString &data) |
| 516 | { |
| 517 | QString retval; |
| 518 | qsizetype i; |
| 519 | for (i = 0; i < data.size(); ++i) { |
| 520 | retval += u'\"'; |
| 521 | for ( ; i < data.size() && data[i] != u'\n' && data[i] != u'\r'; ++i) |
| 522 | if (data[i] == u'\"') |
| 523 | retval += "\\\""_L1 ; |
| 524 | else |
| 525 | retval += data[i]; |
| 526 | if (i+1 < data.size() && data[i] == u'\r' && data[i+1] == u'\n') |
| 527 | i++; |
| 528 | retval += "\\n\"\n"_L1 ; |
| 529 | } |
| 530 | return retval; |
| 531 | } |
| 532 | |
| 533 | static bool openFile(const QString &fileName, QFile &file) |
| 534 | { |
| 535 | if (fileName.isEmpty()) |
| 536 | return false; |
| 537 | |
| 538 | bool isOk = false; |
| 539 | if (fileName == "-"_L1 ) { |
| 540 | isOk = file.open(stdout, ioFlags: QIODevice::WriteOnly | QIODevice::Text); |
| 541 | } else { |
| 542 | file.setFileName(fileName); |
| 543 | isOk = file.open(flags: QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); |
| 544 | } |
| 545 | |
| 546 | if (!isOk) |
| 547 | fprintf(stderr, format: "%s: Unable to open '%s': %s\n" , |
| 548 | PROGRAMNAME, qPrintable(fileName), qPrintable(file.errorString())); |
| 549 | return isOk; |
| 550 | } |
| 551 | |
| 552 | void QDBusXmlToCpp::writeProxy(const QString &filename, |
| 553 | const QDBusIntrospection::Interfaces &interfaces) |
| 554 | { |
| 555 | // open the file |
| 556 | QString = header(name: filename); |
| 557 | QByteArray ; |
| 558 | QTextStream hs(&headerData); |
| 559 | |
| 560 | QString cppName = cpp(name: filename); |
| 561 | QByteArray cppData; |
| 562 | QTextStream cs(&cppData); |
| 563 | |
| 564 | // write the header: |
| 565 | writeHeader(ts&: hs, changesWillBeLost: true); |
| 566 | if (cppName != headerName) |
| 567 | writeHeader(ts&: cs, changesWillBeLost: false); |
| 568 | |
| 569 | // include guards: |
| 570 | QString includeGuard; |
| 571 | if (!headerName.isEmpty() && headerName != "-"_L1 ) { |
| 572 | includeGuard = headerName.toUpper().replace(before: u'.', after: u'_'); |
| 573 | qsizetype pos = includeGuard.lastIndexOf(c: u'/'); |
| 574 | if (pos != -1) |
| 575 | includeGuard = includeGuard.mid(position: pos + 1); |
| 576 | } else { |
| 577 | includeGuard = u"QDBUSXML2CPP_PROXY"_s ; |
| 578 | } |
| 579 | |
| 580 | hs << "#ifndef " << includeGuard << "\n" |
| 581 | "#define " << includeGuard << "\n\n" ; |
| 582 | |
| 583 | // include our stuff: |
| 584 | hs << "#include <QtCore/QObject>\n" |
| 585 | << includeList; |
| 586 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
| 587 | hs << "#include <QtDBus/QtDBus>\n" ; |
| 588 | #else |
| 589 | hs << "#include <QtDBus/QDBusAbstractInterface>\n" |
| 590 | "#include <QtDBus/QDBusPendingReply>\n" ; |
| 591 | #endif |
| 592 | |
| 593 | for (const QString &include : std::as_const(t&: includes)) { |
| 594 | hs << "#include \"" << include << "\"\n" ; |
| 595 | if (headerName.isEmpty()) |
| 596 | cs << "#include \"" << include << "\"\n" ; |
| 597 | } |
| 598 | |
| 599 | for (const QString &include : std::as_const(t&: globalIncludes)) { |
| 600 | hs << "#include <" << include << ">\n" ; |
| 601 | if (headerName.isEmpty()) |
| 602 | cs << "#include <" << include << ">\n" ; |
| 603 | } |
| 604 | |
| 605 | hs << "\n" ; |
| 606 | |
| 607 | if (cppName != headerName) { |
| 608 | if (!headerName.isEmpty() && headerName != "-"_L1 ) |
| 609 | cs << "#include \"" << headerName << "\"\n\n" ; |
| 610 | } |
| 611 | |
| 612 | if (!customNamespace.isEmpty()) { |
| 613 | hs << "namespace " << customNamespace << " {\n" |
| 614 | "\n" ; |
| 615 | cs << "namespace " << customNamespace << " {\n" |
| 616 | "\n" ; |
| 617 | } |
| 618 | |
| 619 | for (const QDBusIntrospection::Interface *interface : interfaces) { |
| 620 | QString className = classNameForInterface(interface: interface->name, classType: Proxy); |
| 621 | |
| 622 | // comment: |
| 623 | hs << "/*\n" |
| 624 | " * Proxy class for interface " << interface->name << "\n" |
| 625 | " */\n" ; |
| 626 | cs << "/*\n" |
| 627 | " * Implementation of interface class " << className << "\n" |
| 628 | " */\n\n" ; |
| 629 | |
| 630 | // class header: |
| 631 | hs << "class " << className << ": public QDBusAbstractInterface\n" |
| 632 | "{\n" |
| 633 | " Q_OBJECT\n" ; |
| 634 | |
| 635 | // the interface name |
| 636 | hs << "public:\n" |
| 637 | " static inline const char *staticInterfaceName()\n" |
| 638 | " { return \"" << interface->name << "\"; }\n\n" ; |
| 639 | |
| 640 | // constructors/destructors: |
| 641 | hs << "public:\n" |
| 642 | " " << className << "(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = nullptr);\n\n" |
| 643 | " ~" << className << "();\n\n" ; |
| 644 | cs << className << "::" << className << "(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent)\n" |
| 645 | " : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent)\n" |
| 646 | "{\n" |
| 647 | "}\n\n" |
| 648 | << className << "::~" << className << "()\n" |
| 649 | "{\n" |
| 650 | "}\n\n" ; |
| 651 | |
| 652 | // properties: |
| 653 | for (const QDBusIntrospection::Property &property : interface->properties) { |
| 654 | QByteArray type = qtTypeName(location: property.location, signature: property.type, annotations: property.annotations); |
| 655 | QString getter = propertyGetter(property); |
| 656 | QString setter = propertySetter(property); |
| 657 | |
| 658 | hs << " Q_PROPERTY(" << type << " " << property.name; |
| 659 | |
| 660 | // getter: |
| 661 | if (property.access != QDBusIntrospection::Property::Write) |
| 662 | // it's readable |
| 663 | hs << " READ " << getter; |
| 664 | |
| 665 | // setter |
| 666 | if (property.access != QDBusIntrospection::Property::Read) |
| 667 | // it's writeable |
| 668 | hs << " WRITE " << setter; |
| 669 | |
| 670 | hs << ")\n" ; |
| 671 | |
| 672 | // getter: |
| 673 | if (property.access != QDBusIntrospection::Property::Write) { |
| 674 | hs << " inline " << type << " " << getter << "() const\n" |
| 675 | " { return qvariant_cast< " << type << " >(property(\"" |
| 676 | << property.name << "\")); }\n" ; |
| 677 | } |
| 678 | |
| 679 | // setter: |
| 680 | if (property.access != QDBusIntrospection::Property::Read) { |
| 681 | hs << " inline void " << setter << "(" << constRefArg(arg: type) << "value)\n" |
| 682 | " { setProperty(\"" << property.name |
| 683 | << "\", QVariant::fromValue(value)); }\n" ; |
| 684 | } |
| 685 | |
| 686 | hs << "\n" ; |
| 687 | } |
| 688 | |
| 689 | // methods: |
| 690 | hs << "public Q_SLOTS: // METHODS\n" ; |
| 691 | for (const QDBusIntrospection::Method &method : interface->methods) { |
| 692 | bool isDeprecated = method.annotations.value(key: "org.freedesktop.DBus.Deprecated"_L1 ).value |
| 693 | == "true"_L1 ; |
| 694 | bool isNoReply = method.annotations.value(ANNOTATION_NO_WAIT ""_L1 ).value == "true"_L1 ; |
| 695 | if (isNoReply && !method.outputArgs.isEmpty()) { |
| 696 | reporter.warning(location: method.location, |
| 697 | msg: "method %s in interface %s is marked 'no-reply' but has output " |
| 698 | "arguments.\n" , |
| 699 | qPrintable(method.name), qPrintable(interface->name)); |
| 700 | continue; |
| 701 | } |
| 702 | |
| 703 | if (isDeprecated) |
| 704 | hs << " Q_DECL_DEPRECATED " ; |
| 705 | else |
| 706 | hs << " " ; |
| 707 | |
| 708 | if (isNoReply) { |
| 709 | hs << "Q_NOREPLY inline void " ; |
| 710 | } else { |
| 711 | hs << "inline QDBusPendingReply<" ; |
| 712 | for (qsizetype i = 0; i < method.outputArgs.size(); ++i) |
| 713 | hs << (i > 0 ? ", " : "" ) |
| 714 | << templateArg(arg: qtTypeName(location: method.outputArgs.at(i).location, |
| 715 | signature: method.outputArgs.at(i).type, annotations: method.annotations, |
| 716 | paramId: i, direction: "Out" )); |
| 717 | hs << "> " ; |
| 718 | } |
| 719 | |
| 720 | hs << methodName(method) << "(" ; |
| 721 | |
| 722 | QStringList argNames = makeArgNames(inputArgs: method.inputArgs); |
| 723 | writeArgList(ts&: hs, argNames, annotations: method.annotations, inputArgs: method.inputArgs); |
| 724 | |
| 725 | hs << ")\n" |
| 726 | " {\n" |
| 727 | " QList<QVariant> argumentList;\n" ; |
| 728 | |
| 729 | if (!method.inputArgs.isEmpty()) { |
| 730 | hs << " argumentList" ; |
| 731 | for (qsizetype argPos = 0; argPos < method.inputArgs.size(); ++argPos) |
| 732 | hs << " << QVariant::fromValue(" << argNames.at(i: argPos) << ')'; |
| 733 | hs << ";\n" ; |
| 734 | } |
| 735 | |
| 736 | if (isNoReply) |
| 737 | hs << " callWithArgumentList(QDBus::NoBlock, " |
| 738 | "QStringLiteral(\"" << method.name << "\"), argumentList);\n" ; |
| 739 | else |
| 740 | hs << " return asyncCallWithArgumentList(QStringLiteral(\"" |
| 741 | << method.name << "\"), argumentList);\n" ; |
| 742 | |
| 743 | // close the function: |
| 744 | hs << " }\n" ; |
| 745 | |
| 746 | if (method.outputArgs.size() > 1) { |
| 747 | // generate the old-form QDBusReply methods with multiple incoming parameters |
| 748 | hs << (isDeprecated ? " Q_DECL_DEPRECATED " : " " ) << "inline QDBusReply<" |
| 749 | << templateArg(arg: qtTypeName(location: method.outputArgs.first().location, |
| 750 | signature: method.outputArgs.first().type, annotations: method.annotations, paramId: 0, |
| 751 | direction: "Out" )) |
| 752 | << "> " ; |
| 753 | hs << method.name << "(" ; |
| 754 | |
| 755 | QStringList argNames = makeArgNames(inputArgs: method.inputArgs, outputArgs: method.outputArgs); |
| 756 | writeArgList(ts&: hs, argNames, annotations: method.annotations, inputArgs: method.inputArgs, outputArgs: method.outputArgs); |
| 757 | |
| 758 | hs << ")\n" |
| 759 | " {\n" |
| 760 | " QList<QVariant> argumentList;\n" ; |
| 761 | |
| 762 | qsizetype argPos = 0; |
| 763 | if (!method.inputArgs.isEmpty()) { |
| 764 | hs << " argumentList" ; |
| 765 | for (argPos = 0; argPos < method.inputArgs.size(); ++argPos) |
| 766 | hs << " << QVariant::fromValue(" << argNames.at(i: argPos) << ')'; |
| 767 | hs << ";\n" ; |
| 768 | } |
| 769 | |
| 770 | hs << " QDBusMessage reply = callWithArgumentList(QDBus::Block, " |
| 771 | "QStringLiteral(\"" << method.name << "\"), argumentList);\n" ; |
| 772 | |
| 773 | argPos++; |
| 774 | hs << " if (reply.type() == QDBusMessage::ReplyMessage && reply.arguments().size() == " |
| 775 | << method.outputArgs.size() << ") {\n" ; |
| 776 | |
| 777 | // yes, starting from 1 |
| 778 | for (qsizetype i = 1; i < method.outputArgs.size(); ++i) |
| 779 | hs << " " << argNames.at(i: argPos++) << " = qdbus_cast<" |
| 780 | << templateArg(arg: qtTypeName(location: method.outputArgs.at(i).location, |
| 781 | signature: method.outputArgs.at(i).type, annotations: method.annotations, |
| 782 | paramId: i, direction: "Out" )) |
| 783 | << ">(reply.arguments().at(" << i << "));\n" ; |
| 784 | hs << " }\n" |
| 785 | " return reply;\n" |
| 786 | " }\n" ; |
| 787 | } |
| 788 | |
| 789 | hs << "\n" ; |
| 790 | } |
| 791 | |
| 792 | hs << "Q_SIGNALS: // SIGNALS\n" ; |
| 793 | for (const QDBusIntrospection::Signal &signal : interface->signals_) { |
| 794 | hs << " " ; |
| 795 | if (signal.annotations.value(key: "org.freedesktop.DBus.Deprecated"_L1 ).value == "true"_L1 ) |
| 796 | hs << "Q_DECL_DEPRECATED " ; |
| 797 | |
| 798 | hs << "void " << signal.name << "(" ; |
| 799 | |
| 800 | QStringList argNames = makeArgNames(inputArgs: signal.outputArgs); |
| 801 | writeSignalArgList(ts&: hs, argNames, annotations: signal.annotations, outputArgs: signal.outputArgs); |
| 802 | |
| 803 | hs << ");\n" ; // finished for header |
| 804 | } |
| 805 | |
| 806 | // close the class: |
| 807 | hs << "};\n\n" ; |
| 808 | } |
| 809 | |
| 810 | if (!customNamespace.isEmpty()) { |
| 811 | hs << "} // end of namespace " << customNamespace << "\n" |
| 812 | << "\n" ; |
| 813 | cs << "} // end of namespace " << customNamespace << "\n" ; |
| 814 | } |
| 815 | |
| 816 | if (!skipNamespaces) { |
| 817 | QStringList last; |
| 818 | QDBusIntrospection::Interfaces::ConstIterator it = interfaces.constBegin(); |
| 819 | do |
| 820 | { |
| 821 | QStringList current; |
| 822 | QString name; |
| 823 | if (it != interfaces.constEnd()) { |
| 824 | current = it->constData()->name.split(sep: u'.'); |
| 825 | name = current.takeLast(); |
| 826 | } |
| 827 | |
| 828 | qsizetype i = 0; |
| 829 | while (i < current.size() && i < last.size() && current.at(i) == last.at(i)) |
| 830 | ++i; |
| 831 | |
| 832 | // i parts matched |
| 833 | // close last.arguments().size() - i namespaces: |
| 834 | for (qsizetype j = i; j < last.size(); ++j) |
| 835 | hs << QString((last.size() - j - 1 + i) * 2, u' ') << "}\n" ; |
| 836 | |
| 837 | // open current.arguments().size() - i namespaces |
| 838 | for (qsizetype j = i; j < current.size(); ++j) |
| 839 | hs << QString(j * 2, u' ') << "namespace " << current.at(i: j) << " {\n" ; |
| 840 | |
| 841 | // add this class: |
| 842 | if (!name.isEmpty()) { |
| 843 | hs << QString(current.size() * 2, u' ') |
| 844 | << "using " << name << " = " << (customNamespace.isEmpty() ? "" : "::" ) |
| 845 | << customNamespace << "::" << classNameForInterface(interface: it->constData()->name, classType: Proxy) |
| 846 | << ";\n" ; |
| 847 | } |
| 848 | |
| 849 | if (it == interfaces.constEnd()) |
| 850 | break; |
| 851 | ++it; |
| 852 | last = current; |
| 853 | } while (true); |
| 854 | |
| 855 | hs << "\n" ; |
| 856 | } |
| 857 | |
| 858 | // close the include guard |
| 859 | hs << "#endif\n" ; |
| 860 | |
| 861 | QString mocName = moc(name: filename); |
| 862 | if (includeMocs && !mocName.isEmpty()) |
| 863 | cs << "\n" |
| 864 | "#include \"" << mocName << "\"\n" ; |
| 865 | |
| 866 | cs.flush(); |
| 867 | hs.flush(); |
| 868 | |
| 869 | QFile file; |
| 870 | const bool = openFile(fileName: headerName, file); |
| 871 | if (headerOpen) |
| 872 | file.write(data: headerData); |
| 873 | |
| 874 | if (headerName == cppName) { |
| 875 | if (headerOpen) |
| 876 | file.write(data: cppData); |
| 877 | } else { |
| 878 | QFile cppFile; |
| 879 | if (openFile(fileName: cppName, file&: cppFile)) |
| 880 | cppFile.write(data: cppData); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | void QDBusXmlToCpp::writeAdaptor(const QString &filename, |
| 885 | const QDBusIntrospection::Interfaces &interfaces) |
| 886 | { |
| 887 | // open the file |
| 888 | QString = header(name: filename); |
| 889 | QByteArray ; |
| 890 | QTextStream hs(&headerData); |
| 891 | |
| 892 | QString cppName = cpp(name: filename); |
| 893 | QByteArray cppData; |
| 894 | QTextStream cs(&cppData); |
| 895 | |
| 896 | // write the headers |
| 897 | writeHeader(ts&: hs, changesWillBeLost: false); |
| 898 | if (cppName != headerName) |
| 899 | writeHeader(ts&: cs, changesWillBeLost: true); |
| 900 | |
| 901 | // include guards: |
| 902 | QString includeGuard; |
| 903 | if (!headerName.isEmpty() && headerName != "-"_L1 ) { |
| 904 | includeGuard = headerName.toUpper().replace(before: u'.', after: u'_'); |
| 905 | qsizetype pos = includeGuard.lastIndexOf(c: u'/'); |
| 906 | if (pos != -1) |
| 907 | includeGuard = includeGuard.mid(position: pos + 1); |
| 908 | } else { |
| 909 | includeGuard = u"QDBUSXML2CPP_ADAPTOR"_s ; |
| 910 | } |
| 911 | |
| 912 | hs << "#ifndef " << includeGuard << "\n" |
| 913 | "#define " << includeGuard << "\n\n" ; |
| 914 | |
| 915 | // include our stuff: |
| 916 | hs << "#include <QtCore/QObject>\n" ; |
| 917 | if (cppName == headerName) |
| 918 | hs << "#include <QtCore/QMetaObject>\n" |
| 919 | "#include <QtCore/QVariant>\n" ; |
| 920 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
| 921 | hs << "#include <QtDBus/QtDBus>\n" ; |
| 922 | #else |
| 923 | hs << "#include <QtDBus/QDBusAbstractAdaptor>\n" |
| 924 | "#include <QtDBus/QDBusObjectPath>\n" ; |
| 925 | #endif |
| 926 | |
| 927 | for (const QString &include : std::as_const(t&: includes)) { |
| 928 | hs << "#include \"" << include << "\"\n" ; |
| 929 | if (headerName.isEmpty()) |
| 930 | cs << "#include \"" << include << "\"\n" ; |
| 931 | } |
| 932 | |
| 933 | for (const QString &include : std::as_const(t&: globalIncludes)) { |
| 934 | hs << "#include <" << include << ">\n" ; |
| 935 | if (headerName.isEmpty()) |
| 936 | cs << "#include <" << include << ">\n" ; |
| 937 | } |
| 938 | |
| 939 | if (cppName != headerName) { |
| 940 | if (!headerName.isEmpty() && headerName != "-"_L1 ) |
| 941 | cs << "#include \"" << headerName << "\"\n" ; |
| 942 | |
| 943 | cs << "#include <QtCore/QMetaObject>\n" |
| 944 | << includeList |
| 945 | << "\n" ; |
| 946 | hs << forwardDeclarations; |
| 947 | } else { |
| 948 | hs << includeList; |
| 949 | } |
| 950 | |
| 951 | hs << "\n" ; |
| 952 | |
| 953 | QString parent = parentClassName; |
| 954 | if (parentClassName.isEmpty()) |
| 955 | parent = u"QObject"_s ; |
| 956 | |
| 957 | if (!customNamespace.isEmpty()) { |
| 958 | hs << "namespace " << customNamespace << " {\n" |
| 959 | "\n" ; |
| 960 | cs << "namespace " << customNamespace << " {\n" |
| 961 | "\n" ; |
| 962 | } |
| 963 | |
| 964 | for (const QDBusIntrospection::Interface *interface : interfaces) { |
| 965 | QString className = classNameForInterface(interface: interface->name, classType: Adaptor); |
| 966 | |
| 967 | // comment: |
| 968 | hs << "/*\n" |
| 969 | " * Adaptor class for interface " << interface->name << "\n" |
| 970 | " */\n" ; |
| 971 | cs << "/*\n" |
| 972 | " * Implementation of adaptor class " << className << "\n" |
| 973 | " */\n\n" ; |
| 974 | |
| 975 | // class header: |
| 976 | hs << "class " << className << ": public QDBusAbstractAdaptor\n" |
| 977 | "{\n" |
| 978 | " Q_OBJECT\n" |
| 979 | " Q_CLASSINFO(\"D-Bus Interface\", \"" << interface->name << "\")\n" |
| 980 | " Q_CLASSINFO(\"D-Bus Introspection\", \"\"\n" |
| 981 | << stringify(data: interface->introspection) |
| 982 | << " \"\")\n" |
| 983 | "public:\n" |
| 984 | " " << className << "(" << parent << " *parent);\n" |
| 985 | " ~" << className << "() override;\n\n" ; |
| 986 | |
| 987 | if (!parentClassName.isEmpty()) |
| 988 | hs << " inline " << parent << " *parent() const\n" |
| 989 | " { return static_cast<" << parent << " *>(QObject::parent()); }\n\n" ; |
| 990 | |
| 991 | // constructor/destructor |
| 992 | cs << className << "::" << className << "(" << parent << " *parent)\n" |
| 993 | " : QDBusAbstractAdaptor(parent)\n" |
| 994 | "{\n" |
| 995 | " // constructor\n" |
| 996 | " setAutoRelaySignals(true);\n" |
| 997 | "}\n\n" |
| 998 | << className << "::~" << className << "()\n" |
| 999 | "{\n" |
| 1000 | " // destructor\n" |
| 1001 | "}\n\n" ; |
| 1002 | |
| 1003 | hs << "public: // PROPERTIES\n" ; |
| 1004 | for (const QDBusIntrospection::Property &property : interface->properties) { |
| 1005 | QByteArray type = qtTypeName(location: property.location, signature: property.type, annotations: property.annotations); |
| 1006 | QString constRefType = constRefArg(arg: type); |
| 1007 | QString getter = propertyGetter(property); |
| 1008 | QString setter = propertySetter(property); |
| 1009 | |
| 1010 | hs << " Q_PROPERTY(" << type << " " << property.name; |
| 1011 | if (property.access != QDBusIntrospection::Property::Write) |
| 1012 | hs << " READ " << getter; |
| 1013 | if (property.access != QDBusIntrospection::Property::Read) |
| 1014 | hs << " WRITE " << setter; |
| 1015 | hs << ")\n" ; |
| 1016 | |
| 1017 | // getter: |
| 1018 | if (property.access != QDBusIntrospection::Property::Write) { |
| 1019 | hs << " " << type << " " << getter << "() const;\n" ; |
| 1020 | cs << type << " " |
| 1021 | << className << "::" << getter << "() const\n" |
| 1022 | "{\n" |
| 1023 | " // get the value of property " << property.name << "\n" |
| 1024 | " return qvariant_cast< " << type <<" >(parent()->property(\"" << property.name << "\"));\n" |
| 1025 | "}\n\n" ; |
| 1026 | } |
| 1027 | |
| 1028 | // setter |
| 1029 | if (property.access != QDBusIntrospection::Property::Read) { |
| 1030 | hs << " void " << setter << "(" << constRefType << "value);\n" ; |
| 1031 | cs << "void " << className << "::" << setter << "(" << constRefType << "value)\n" |
| 1032 | "{\n" |
| 1033 | " // set the value of property " << property.name << "\n" |
| 1034 | " parent()->setProperty(\"" << property.name << "\", QVariant::fromValue(value" ; |
| 1035 | if (constRefType.contains(s: "QDBusVariant"_L1 )) |
| 1036 | cs << ".variant()" ; |
| 1037 | cs << "));\n" |
| 1038 | "}\n\n" ; |
| 1039 | } |
| 1040 | |
| 1041 | hs << "\n" ; |
| 1042 | } |
| 1043 | |
| 1044 | hs << "public Q_SLOTS: // METHODS\n" ; |
| 1045 | for (const QDBusIntrospection::Method &method : interface->methods) { |
| 1046 | bool isNoReply = method.annotations.value(ANNOTATION_NO_WAIT ""_L1 ).value == "true"_L1 ; |
| 1047 | if (isNoReply && !method.outputArgs.isEmpty()) { |
| 1048 | reporter.warning(location: method.location, |
| 1049 | msg: "method %s in interface %s is marked 'no-reply' but has output " |
| 1050 | "arguments.\n" , |
| 1051 | qPrintable(method.name), qPrintable(interface->name)); |
| 1052 | continue; |
| 1053 | } |
| 1054 | |
| 1055 | hs << " " ; |
| 1056 | QByteArray returnType; |
| 1057 | if (isNoReply) { |
| 1058 | hs << "Q_NOREPLY void " ; |
| 1059 | cs << "void " ; |
| 1060 | } else if (method.outputArgs.isEmpty()) { |
| 1061 | hs << "void " ; |
| 1062 | cs << "void " ; |
| 1063 | } else { |
| 1064 | returnType = |
| 1065 | qtTypeName(location: method.outputArgs.first().location, |
| 1066 | signature: method.outputArgs.first().type, annotations: method.annotations, paramId: 0, direction: "Out" ); |
| 1067 | hs << returnType << " " ; |
| 1068 | cs << returnType << " " ; |
| 1069 | } |
| 1070 | |
| 1071 | QString name = methodName(method); |
| 1072 | hs << name << "(" ; |
| 1073 | cs << className << "::" << name << "(" ; |
| 1074 | |
| 1075 | QStringList argNames = makeArgNames(inputArgs: method.inputArgs, outputArgs: method.outputArgs); |
| 1076 | writeArgList(ts&: hs, argNames, annotations: method.annotations, inputArgs: method.inputArgs, outputArgs: method.outputArgs); |
| 1077 | writeArgList(ts&: cs, argNames, annotations: method.annotations, inputArgs: method.inputArgs, outputArgs: method.outputArgs); |
| 1078 | |
| 1079 | hs << ");\n" ; // finished for header |
| 1080 | cs << ")\n" |
| 1081 | "{\n" |
| 1082 | " // handle method call " << interface->name << "." << methodName(method) << "\n" ; |
| 1083 | |
| 1084 | // make the call |
| 1085 | bool usingInvokeMethod = false; |
| 1086 | if (parentClassName.isEmpty() && method.inputArgs.size() <= 10 |
| 1087 | && method.outputArgs.size() <= 1) |
| 1088 | usingInvokeMethod = true; |
| 1089 | |
| 1090 | if (usingInvokeMethod) { |
| 1091 | // we are using QMetaObject::invokeMethod |
| 1092 | if (!returnType.isEmpty()) { |
| 1093 | cs << " " << returnType << " " << argNames.at(i: method.inputArgs.size()) |
| 1094 | << "{};\n" ; |
| 1095 | } |
| 1096 | |
| 1097 | static const char invoke[] = " QMetaObject::invokeMethod(parent(), \"" ; |
| 1098 | cs << invoke << name << "\"" ; |
| 1099 | |
| 1100 | if (!method.outputArgs.isEmpty()) |
| 1101 | cs << ", Q_RETURN_ARG(" |
| 1102 | << qtTypeName(location: method.outputArgs.at(i: 0).location, signature: method.outputArgs.at(i: 0).type, |
| 1103 | annotations: method.annotations, paramId: 0, direction: "Out" ) |
| 1104 | << ", " << argNames.at(i: method.inputArgs.size()) << ")" ; |
| 1105 | |
| 1106 | for (qsizetype i = 0; i < method.inputArgs.size(); ++i) |
| 1107 | cs << ", Q_ARG(" |
| 1108 | << qtTypeName(location: method.inputArgs.at(i).location, signature: method.inputArgs.at(i).type, |
| 1109 | annotations: method.annotations, paramId: i, direction: "In" ) |
| 1110 | << ", " << argNames.at(i) << ")" ; |
| 1111 | |
| 1112 | cs << ");\n" ; |
| 1113 | |
| 1114 | if (!returnType.isEmpty()) |
| 1115 | cs << " return " << argNames.at(i: method.inputArgs.size()) << ";\n" ; |
| 1116 | } else { |
| 1117 | if (parentClassName.isEmpty()) |
| 1118 | cs << " //" ; |
| 1119 | else |
| 1120 | cs << " " ; |
| 1121 | |
| 1122 | if (!method.outputArgs.isEmpty()) |
| 1123 | cs << "return " ; |
| 1124 | |
| 1125 | if (parentClassName.isEmpty()) |
| 1126 | cs << "static_cast<YourObjectType *>(parent())->" ; |
| 1127 | else |
| 1128 | cs << "parent()->" ; |
| 1129 | cs << name << "(" ; |
| 1130 | |
| 1131 | qsizetype argPos = 0; |
| 1132 | bool first = true; |
| 1133 | for (qsizetype i = 0; i < method.inputArgs.size(); ++i) { |
| 1134 | cs << (first ? "" : ", " ) << argNames.at(i: argPos++); |
| 1135 | first = false; |
| 1136 | } |
| 1137 | ++argPos; // skip retval, if any |
| 1138 | for (qsizetype i = 1; i < method.outputArgs.size(); ++i) { |
| 1139 | cs << (first ? "" : ", " ) << argNames.at(i: argPos++); |
| 1140 | first = false; |
| 1141 | } |
| 1142 | |
| 1143 | cs << ");\n" ; |
| 1144 | } |
| 1145 | cs << "}\n\n" ; |
| 1146 | } |
| 1147 | |
| 1148 | hs << "Q_SIGNALS: // SIGNALS\n" ; |
| 1149 | for (const QDBusIntrospection::Signal &signal : interface->signals_) { |
| 1150 | hs << " void " << signal.name << "(" ; |
| 1151 | |
| 1152 | QStringList argNames = makeArgNames(inputArgs: signal.outputArgs); |
| 1153 | writeSignalArgList(ts&: hs, argNames, annotations: signal.annotations, outputArgs: signal.outputArgs); |
| 1154 | |
| 1155 | hs << ");\n" ; // finished for header |
| 1156 | } |
| 1157 | |
| 1158 | // close the class: |
| 1159 | hs << "};\n\n" ; |
| 1160 | } |
| 1161 | |
| 1162 | if (!customNamespace.isEmpty()) { |
| 1163 | hs << "} // end of namespace " << customNamespace << "\n" |
| 1164 | << "\n" ; |
| 1165 | cs << "} // end of namespace " << customNamespace << "\n" ; |
| 1166 | } |
| 1167 | |
| 1168 | // close the include guard |
| 1169 | hs << "#endif\n" ; |
| 1170 | |
| 1171 | QString mocName = moc(name: filename); |
| 1172 | if (includeMocs && !mocName.isEmpty()) |
| 1173 | cs << "\n" |
| 1174 | "#include \"" << mocName << "\"\n" ; |
| 1175 | |
| 1176 | cs.flush(); |
| 1177 | hs.flush(); |
| 1178 | |
| 1179 | QFile file; |
| 1180 | const bool = openFile(fileName: headerName, file); |
| 1181 | if (headerOpen) |
| 1182 | file.write(data: headerData); |
| 1183 | |
| 1184 | if (headerName == cppName) { |
| 1185 | if (headerOpen) |
| 1186 | file.write(data: cppData); |
| 1187 | } else { |
| 1188 | QFile cppFile; |
| 1189 | if (openFile(fileName: cppName, file&: cppFile)) |
| 1190 | cppFile.write(data: cppData); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | int QDBusXmlToCpp::run(const QCoreApplication &app) |
| 1195 | { |
| 1196 | QCommandLineParser parser; |
| 1197 | parser.setApplicationDescription( |
| 1198 | "Produces the C++ code to implement the interfaces defined in the input file.\n\n" |
| 1199 | "If the file name given to the options -a and -p does not end in .cpp or .h, the\n" |
| 1200 | "program will automatically append the suffixes and produce both files.\n" |
| 1201 | "You can also use a colon (:) to separate the header name from the source file\n" |
| 1202 | "name, as in '-a filename_p.h:filename.cpp'.\n\n" |
| 1203 | "If you pass a dash (-) as the argument to either -p or -a, the output is written\n" |
| 1204 | "to the standard output."_L1 ); |
| 1205 | |
| 1206 | parser.addHelpOption(); |
| 1207 | parser.addVersionOption(); |
| 1208 | parser.addPositionalArgument(name: u"xml-or-xml-file"_s , description: u"XML file to use."_s ); |
| 1209 | parser.addPositionalArgument(name: u"interfaces"_s , description: u"List of interfaces to use."_s , |
| 1210 | syntax: u"[interfaces ...]"_s ); |
| 1211 | |
| 1212 | QCommandLineOption adapterCodeOption(QStringList{u"a"_s , u"adaptor"_s }, |
| 1213 | u"Write the adaptor code to <filename>"_s , u"filename"_s ); |
| 1214 | parser.addOption(commandLineOption: adapterCodeOption); |
| 1215 | |
| 1216 | QCommandLineOption classNameOption(QStringList{u"c"_s , u"classname"_s }, |
| 1217 | u"Use <classname> as the class name for the generated classes. " |
| 1218 | u"This option can only be used when processing a single interface."_s , |
| 1219 | u"classname"_s ); |
| 1220 | parser.addOption(commandLineOption: classNameOption); |
| 1221 | |
| 1222 | QCommandLineOption namespaceOption(QStringList{u"namespace"_s }, |
| 1223 | u"Put all generated classes into the namespace <namespace>. "_s , u"namespace"_s ); |
| 1224 | parser.addOption(commandLineOption: namespaceOption); |
| 1225 | |
| 1226 | QCommandLineOption addIncludeOption(QStringList{u"i"_s , u"include"_s }, |
| 1227 | u"Add #include \"filename\" to the output"_s , u"filename"_s ); |
| 1228 | parser.addOption(commandLineOption: addIncludeOption); |
| 1229 | |
| 1230 | QCommandLineOption addGlobalIncludeOption(QStringList{u"I"_s , u"global-include"_s }, |
| 1231 | u"Add #include <filename> to the output"_s , u"filename"_s ); |
| 1232 | parser.addOption(commandLineOption: addGlobalIncludeOption); |
| 1233 | |
| 1234 | QCommandLineOption adapterParentOption(u"l"_s , |
| 1235 | u"When generating an adaptor, use <classname> as the parent class"_s , u"classname"_s ); |
| 1236 | parser.addOption(commandLineOption: adapterParentOption); |
| 1237 | |
| 1238 | QCommandLineOption mocIncludeOption(QStringList{u"m"_s , u"moc"_s }, |
| 1239 | u"Generate #include \"filename.moc\" statements in the .cpp files"_s ); |
| 1240 | parser.addOption(commandLineOption: mocIncludeOption); |
| 1241 | |
| 1242 | QCommandLineOption noNamespaceOption(QStringList{u"N"_s , u"no-namespaces"_s }, |
| 1243 | u"Do not export the generated class into the D-Bus specific namespace"_s ); |
| 1244 | parser.addOption(commandLineOption: noNamespaceOption); |
| 1245 | |
| 1246 | QCommandLineOption proxyCodeOption(QStringList{u"p"_s , u"proxy"_s }, |
| 1247 | u"Write the proxy code to <filename>"_s , u"filename"_s ); |
| 1248 | parser.addOption(commandLineOption: proxyCodeOption); |
| 1249 | |
| 1250 | QCommandLineOption verboseOption(QStringList{u"V"_s , u"verbose"_s }, |
| 1251 | u"Be verbose."_s ); |
| 1252 | parser.addOption(commandLineOption: verboseOption); |
| 1253 | |
| 1254 | parser.process(app); |
| 1255 | |
| 1256 | QString adaptorFile = parser.value(option: adapterCodeOption); |
| 1257 | globalClassName = parser.value(option: classNameOption); |
| 1258 | includes = parser.values(option: addIncludeOption); |
| 1259 | globalIncludes = parser.values(option: addGlobalIncludeOption); |
| 1260 | parentClassName = parser.value(option: adapterParentOption); |
| 1261 | customNamespace = parser.value(option: namespaceOption); |
| 1262 | includeMocs = parser.isSet(option: mocIncludeOption); |
| 1263 | skipNamespaces = parser.isSet(option: noNamespaceOption); |
| 1264 | QString proxyFile = parser.value(option: proxyCodeOption); |
| 1265 | bool verbose = parser.isSet(option: verboseOption); |
| 1266 | |
| 1267 | wantedInterfaces = parser.positionalArguments(); |
| 1268 | if (!wantedInterfaces.isEmpty()) { |
| 1269 | inputFile = wantedInterfaces.takeFirst(); |
| 1270 | |
| 1271 | QFileInfo inputInfo(inputFile); |
| 1272 | if (!inputInfo.exists() || !inputInfo.isFile() || !inputInfo.isReadable()) { |
| 1273 | qCritical(msg: "Error: Input %s is not a file or cannot be accessed\n" , qPrintable(inputFile)); |
| 1274 | return 1; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | if (verbose) |
| 1279 | QLoggingCategory::setFilterRules(u"dbus.parser.debug=true"_s ); |
| 1280 | |
| 1281 | QDBusIntrospection::Interfaces interfaces = readInput(); |
| 1282 | cleanInterfaces(interfaces); |
| 1283 | |
| 1284 | if (!globalClassName.isEmpty() && interfaces.count() != 1) { |
| 1285 | qCritical(msg: "Option -c/--classname can only be used with a single interface.\n" ); |
| 1286 | return 1; |
| 1287 | } |
| 1288 | |
| 1289 | QStringList args = app.arguments(); |
| 1290 | args.removeFirst(); |
| 1291 | commandLine = PROGRAMNAME " "_L1 + args.join(sep: u' '); |
| 1292 | |
| 1293 | if (!proxyFile.isEmpty() || adaptorFile.isEmpty()) |
| 1294 | writeProxy(filename: proxyFile, interfaces); |
| 1295 | |
| 1296 | if (!adaptorFile.isEmpty()) |
| 1297 | writeAdaptor(filename: adaptorFile, interfaces); |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | int main(int argc, char **argv) |
| 1303 | { |
| 1304 | QCoreApplication app(argc, argv); |
| 1305 | QCoreApplication::setApplicationName(QStringLiteral(PROGRAMNAME)); |
| 1306 | QCoreApplication::setApplicationVersion(QStringLiteral(PROGRAMVERSION)); |
| 1307 | |
| 1308 | return QDBusXmlToCpp().run(app); |
| 1309 | } |
| 1310 | |