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