1/*
2 * BluezQt - Asynchronous BlueZ wrapper library
3 *
4 * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include <QCommandLineParser>
10#include <QCoreApplication>
11#include <QDebug>
12#include <QFile>
13#include <QFileInfo>
14#include <QTextStream>
15
16#include "BluezApiParser.h"
17#include "CppGenerator.h"
18#include "XmlGenerator.h"
19
20int main(int argc, char *argv[])
21{
22 QCoreApplication a(argc, argv);
23 QCoreApplication::setApplicationName(QStringLiteral("bluezapi2qt"));
24 QCoreApplication::setApplicationVersion(QStringLiteral("0.1"));
25
26 // Add command line parsing
27 QCommandLineParser parser;
28 parser.setApplicationDescription(
29 QStringLiteral("Generates D-BUS object introspection XML files out of BlueZ D-Bus API\n"
30 "description files (*-api.txt)."));
31 parser.addHelpOption();
32 parser.addVersionOption();
33 parser.addPositionalArgument(QStringLiteral("<bluez>-api.txt"), QStringLiteral("BlueZ D-Bus API description file to use."));
34 QCommandLineOption deprecatedOption(QStringList() << QStringLiteral("d") << QStringLiteral("deprecated"),
35 QStringLiteral("Generate deprecated methods/properties"));
36 parser.addOption(commandLineOption: deprecatedOption);
37 QCommandLineOption experimentalOption(QStringList() << QStringLiteral("e") << QStringLiteral("experimental"),
38 QStringLiteral("Generate experimental methods/properties"));
39 parser.addOption(commandLineOption: experimentalOption);
40 QCommandLineOption optionalOption(QStringList() << QStringLiteral("o") << QStringLiteral("optional"),
41 QStringLiteral("Generate optional methods/properties"));
42 parser.addOption(commandLineOption: optionalOption);
43 QCommandLineOption xmlOption(QStringList() << QStringLiteral("x") << QStringLiteral("xml"),
44 QStringLiteral("Generate D-Bus object introspection XML files"));
45 parser.addOption(commandLineOption: xmlOption);
46 QCommandLineOption cppOption(QStringList() << QStringLiteral("c") << QStringLiteral("cpp"), QStringLiteral("Generate D-Bus interface adaptor C++ files"));
47 parser.addOption(commandLineOption: cppOption);
48 QCommandLineOption authorOption(QStringList() << QStringLiteral("a") << QStringLiteral("author"),
49 QStringLiteral("Author for copyright header in C++ files"),
50 QStringLiteral("author"));
51 parser.addOption(commandLineOption: authorOption);
52 QCommandLineOption yearOption(QStringList() << QStringLiteral("y") << QStringLiteral("year"),
53 QStringLiteral("Year for copyright header in C++ files"),
54 QStringLiteral("year"));
55 parser.addOption(commandLineOption: yearOption);
56 parser.process(app: a);
57
58 // Open file
59 auto positionalArgs = parser.positionalArguments();
60 if (positionalArgs.isEmpty()) {
61 fputs(qPrintable(parser.helpText()), stderr);
62 return 1;
63 }
64 auto fileName = positionalArgs.takeFirst();
65 QFileInfo inputInfo(fileName);
66 if (!inputInfo.exists() || !inputInfo.isFile() || !inputInfo.isReadable()) {
67 qCritical() << "Cannot open file" << fileName;
68 return 1;
69 }
70
71 QFile file(fileName);
72 if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) {
73 qCritical() << "Cannot open file" << file.fileName();
74 return 1;
75 }
76 QTextStream stream(&file);
77
78 // Parse file
79 BluezApiParser bluezParser;
80 if (!bluezParser.parse(stream)) {
81 qCritical() << "Error parsing file" << file.fileName();
82 return 1;
83 }
84 if (!bluezParser.finalize()) {
85 qCritical() << "Error parsing file" << file.fileName();
86 return 1;
87 }
88
89 if (parser.isSet(option: xmlOption)) {
90 XmlGenerator::Config xmlConfig;
91 xmlConfig.useOptional = parser.isSet(option: optionalOption);
92 xmlConfig.useDeprecated = parser.isSet(option: deprecatedOption);
93 xmlConfig.useExperimental = parser.isSet(option: experimentalOption);
94 XmlGenerator xmlGenerator(xmlConfig);
95 if (!xmlGenerator.generate(parser: bluezParser)) {
96 qCritical() << "Error generating xml";
97 return 1;
98 }
99 }
100
101 if (parser.isSet(option: cppOption)) {
102 CppGenerator::Config cppConfig;
103 cppConfig.author = parser.value(option: authorOption);
104 cppConfig.year = parser.value(option: yearOption);
105 CppGenerator cppGenerator(cppConfig);
106 if (!cppGenerator.generate(parser: bluezParser)) {
107 qCritical() << "Error generating C++ files";
108 return 1;
109 }
110 }
111
112 return 0;
113}
114

source code of bluez-qt/tools/bluezapi2qt/main.cpp