Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdbusxmlgenerator.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtCore/qmetaobject.h>
6#include <QtCore/qstringlist.h>
7#include <QtCore/qdebug.h>
8
9#include "qdbusinterface_p.h" // for ANNOTATION_NO_WAIT
10#include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
11#include "qdbusconnection_p.h" // for the flags
13#include "qdbusmetatype.h"
14#include "qdbusutil_p.h"
15
16#ifndef QT_NO_DBUS
17
19
20using namespace Qt::StringLiterals;
21
22extern Q_DBUS_EXPORT QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo,
23 const QMetaObject *base, int flags);
24
25static inline QString typeNameToXml(const char *typeName)
26{
27 // ### copied from qtextdocument.cpp
28 // ### move this into Qt Core at some point
29 const QLatin1StringView plain(typeName);
30 QString rich;
31 rich.reserve(int(plain.size() * 1.1));
32 for (int i = 0; i < plain.size(); ++i) {
33 if (plain.at(i) == u'<')
34 rich += "&lt;"_L1;
35 else if (plain.at(i) == u'>')
36 rich += "&gt;"_L1;
37 else if (plain.at(i) == u'&')
38 rich += "&amp;"_L1;
39 else
40 rich += plain.at(i);
41 }
42 return rich;
43}
44
45static inline QLatin1StringView accessAsString(bool read, bool write)
46{
47 if (read)
48 return write ? "readwrite"_L1 : "read"_L1 ;
49 else
50 return write ? "write"_L1 : ""_L1 ;
51}
52
53// implement the D-Bus org.freedesktop.DBus.Introspectable interface
54// we do that by analysing the metaObject of all the adaptor interfaces
55
56static QString generateInterfaceXml(const QMetaObject *mo, int flags, int methodOffset, int propOffset)
57{
58 QString retval;
59
60 // start with properties:
61 if (flags & (QDBusConnection::ExportScriptableProperties |
62 QDBusConnection::ExportNonScriptableProperties)) {
63 for (int i = propOffset; i < mo->propertyCount(); ++i) {
64
65 QMetaProperty mp = mo->property(i);
66
67 if (!((mp.isScriptable() && (flags & QDBusConnection::ExportScriptableProperties)) ||
68 (!mp.isScriptable() && (flags & QDBusConnection::ExportNonScriptableProperties))))
69 continue;
70
71 QMetaType type = mp.metaType();
72 if (!type.isValid())
73 continue;
74 const char *signature = QDBusMetaType::typeToSignature(type);
75 if (!signature)
76 continue;
77
78 retval += " <property name=\"%1\" type=\"%2\" access=\"%3\""_L1
79 .arg(QLatin1StringView(mp.name()),
80 QLatin1StringView(signature),
81 accessAsString(mp.isReadable(), mp.isWritable()));
82
83 if (!QDBusMetaType::signatureToMetaType(signature).isValid()) {
84 const char *typeName = type.name();
85 retval += ">\n <annotation name=\"org.qtproject.QtDBus.QtTypeName\" value=\"%3\"/>\n </property>\n"_L1
86 .arg(typeNameToXml(typeName));
87 } else {
88 retval += "/>\n"_L1;
89 }
90 }
91 }
92
93 // now add methods:
94 for (int i = methodOffset; i < mo->methodCount(); ++i) {
95 QMetaMethod mm = mo->method(i);
96
97 bool isSignal = false;
98 bool isSlot = false;
99 if (mm.methodType() == QMetaMethod::Signal)
100 // adding a signal
101 isSignal = true;
102 else if (mm.access() == QMetaMethod::Public && mm.methodType() == QMetaMethod::Slot)
103 isSlot = true;
104 else if (mm.access() == QMetaMethod::Public && mm.methodType() == QMetaMethod::Method)
105 ; // invokable, neither signal nor slot
106 else
107 continue; // neither signal nor public method/slot
108
109 if (isSignal && !(flags & (QDBusConnection::ExportScriptableSignals |
110 QDBusConnection::ExportNonScriptableSignals)))
111 continue; // we're not exporting any signals
112 if (!isSignal && (!(flags & (QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportNonScriptableSlots)) &&
113 !(flags & (QDBusConnection::ExportScriptableInvokables | QDBusConnection::ExportNonScriptableInvokables))))
114 continue; // we're not exporting any slots or invokables
115
116 // we want to skip non-scriptable stuff as early as possible to avoid bogus warning
117 // for methods that are not being exported at all
118 bool isScriptable = mm.attributes() & QMetaMethod::Scriptable;
119 if (!isScriptable && !(flags & (isSignal ? QDBusConnection::ExportNonScriptableSignals : QDBusConnection::ExportNonScriptableInvokables | QDBusConnection::ExportNonScriptableSlots)))
120 continue;
121
122 QString xml = QString::asprintf(" <%s name=\"%s\">\n",
123 isSignal ? "signal" : "method", mm.name().constData());
124
125 // check the return type first
126 QMetaType typeId = mm.returnMetaType();
127 if (typeId.isValid() && typeId.id() != QMetaType::Void) {
128 const char *typeName = QDBusMetaType::typeToSignature(typeId);
129 if (typeName) {
130 xml += " <arg type=\"%1\" direction=\"out\"/>\n"_L1
131 .arg(typeNameToXml(typeName));
132
133 // do we need to describe this argument?
134 if (!QDBusMetaType::signatureToMetaType(typeName).isValid())
135 xml += " <annotation name=\"org.qtproject.QtDBus.QtTypeName.Out0\" value=\"%1\"/>\n"_L1
136 .arg(typeNameToXml(QMetaType(typeId).name()));
137 } else {
138 qWarning() << "Unsupported return type" << typeId.id() << typeId.name() << "in method" << mm.name();
139 continue;
140 }
141 }
142 else if (!typeId.isValid()) {
143 qWarning() << "Invalid return type in method" << mm.name();
144 continue; // wasn't a valid type
145 }
146
147 QList<QByteArray> names = mm.parameterNames();
148 QList<QMetaType> types;
149 QString errorMsg;
150 int inputCount = qDBusParametersForMethod(mm, types, errorMsg);
151 if (inputCount == -1) {
152 qWarning() << "Skipped method" << mm.name() << ":" << qPrintable(errorMsg);
153 continue; // invalid form
154 }
155 if (isSignal && inputCount + 1 != types.size())
156 continue; // signal with output arguments?
157 if (isSignal && types.at(inputCount) == QDBusMetaTypeId::message())
158 continue; // signal with QDBusMessage argument?
159 if (isSignal && mm.attributes() & QMetaMethod::Cloned)
160 continue; // cloned signal?
161
162 int j;
163 for (j = 1; j < types.size(); ++j) {
164 // input parameter for a slot or output for a signal
165 if (types.at(j) == QDBusMetaTypeId::message()) {
166 isScriptable = true;
167 continue;
168 }
169
170 QString name;
171 if (!names.at(j - 1).isEmpty())
172 name = "name=\"%1\" "_L1.arg(QLatin1StringView(names.at(j - 1)));
173
174 bool isOutput = isSignal || j > inputCount;
175
176 const char *signature = QDBusMetaType::typeToSignature(types.at(j));
177 xml += QString::asprintf(" <arg %lstype=\"%s\" direction=\"%s\"/>\n",
178 qUtf16Printable(name), signature, isOutput ? "out" : "in");
179
180 // do we need to describe this argument?
181 if (!QDBusMetaType::signatureToMetaType(signature).isValid()) {
182 const char *typeName = QMetaType(types.at(j)).name();
183 xml += QString::fromLatin1(" <annotation name=\"org.qtproject.QtDBus.QtTypeName.%1%2\" value=\"%3\"/>\n")
184 .arg(isOutput ? "Out"_L1 : "In"_L1)
185 .arg(isOutput && !isSignal ? j - inputCount : j - 1)
186 .arg(typeNameToXml(typeName));
187 }
188 }
189
190 int wantedMask;
191 if (isScriptable)
192 wantedMask = isSignal ? QDBusConnection::ExportScriptableSignals
193 : isSlot ? QDBusConnection::ExportScriptableSlots
194 : QDBusConnection::ExportScriptableInvokables;
195 else
196 wantedMask = isSignal ? QDBusConnection::ExportNonScriptableSignals
197 : isSlot ? QDBusConnection::ExportNonScriptableSlots
198 : QDBusConnection::ExportNonScriptableInvokables;
199 if ((flags & wantedMask) != wantedMask)
200 continue;
201
202 if (qDBusCheckAsyncTag(mm.tag()))
203 // add the no-reply annotation
204 xml += " <annotation name=\"" ANNOTATION_NO_WAIT "\" value=\"true\"/>\n"_L1;
205
206 retval += xml;
207 retval += " </%1>\n"_L1.arg(isSignal ? "signal"_L1 : "method"_L1);
208 }
209
210 return retval;
211}
212
213QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo,
214 const QMetaObject *base, int flags)
215{
216 if (interface.isEmpty())
217 // generate the interface name from the meta object
218 interface = qDBusInterfaceFromMetaObject(mo);
219
220 QString xml;
221 int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTROSPECTION);
222 if (idx >= mo->classInfoOffset())
223 return QString::fromUtf8(mo->classInfo(idx).value());
224 else
225 xml = generateInterfaceXml(mo, flags, base->methodCount(), base->propertyCount());
226
227 if (xml.isEmpty())
228 return QString(); // don't add an empty interface
229 return " <interface name=\"%1\">\n%2 </interface>\n"_L1
230 .arg(interface, xml);
231}
232
233QT_END_NAMESPACE
234
235#endif // QT_NO_DBUS
#define QCLASSINFO_DBUS_INTROSPECTION
#define ANNOTATION_NO_WAIT
static QLatin1StringView accessAsString(bool read, bool write)
static QString generateInterfaceXml(const QMetaObject *mo, int flags, int methodOffset, int propOffset)
static QString typeNameToXml(const char *typeName)