1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/* -*- mode: C++ -*-
*
* Copyright (C) 2006 Trolltech AS. All rights reserved.
* Author: Thiago Macieira <thiago.macieira@trolltech.com>
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "qdbusconnection_p.h"
#include <dbus/dbus.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qmetaobject.h>
#include "qdbusstandardinterfaces.h"
#include "qdbusabstractadaptor.h"
#include "qdbusabstractadaptor_p.h"
#include "qdbusinterface_p.h" // for ANNOTATION_NO_WAIT
#include "qdbusmessage.h"
#include "qdbustype.h"
#include "qdbusvariant.h"
// implement the D-Bus org.freedesktop.DBus.Introspectable interface
// we do that by analysing the metaObject of all the adaptor interfaces
static inline QString dbusMemberName(const char *qtMemberName)
{
QString retval = QLatin1String(qtMemberName);
if (!retval.isEmpty())
retval[0] = retval[0].toUpper();
return retval;
}
static QString generateInterfaceXml(const QMetaObject *mo, int flags, int methodOffset, int propOffset)
{
QString retval;
// start with properties:
if (flags & QDBusConnection::ExportProperties) {
for (int i = propOffset; i < mo->propertyCount(); ++i) {
static const char *accessvalues[] = {0, "read", "write", "readwrite"};
QMetaProperty mp = mo->property(i);
if (!mp.isScriptable() && (flags & QDBusConnection::ExportNonScriptableProperties) !=
QDBusConnection::ExportNonScriptableProperties)
continue;
int access = 0;
if (mp.isReadable())
access |= 1;
if (mp.isWritable())
access |= 2;
int typeId = qDBusNameToTypeId(mp.typeName());
if (!typeId)
continue;
retval += QString(QLatin1String(" <property name=\"%1\" type=\"%2\" access=\"%3\" />\n"))
.arg(dbusMemberName(mp.name()))
.arg(QLatin1String(QDBusType::dbusSignature( QVariant::Type(typeId) )))
.arg(QLatin1String( accessvalues[access] ));
}
}
// now add methods:
for (int i = methodOffset; i < mo->methodCount(); ++i) {
QMetaMethod mm = mo->method(i);
QByteArray signature = mm.signature();
int paren = signature.indexOf('(');
bool isSignal;
if (mm.methodType() == QMetaMethod::Signal)
// adding a signal
isSignal = true;
else if (mm.methodType() == QMetaMethod::Slot && mm.access() == QMetaMethod::Public)
isSignal = false;
else
continue; // neither signal nor public slot
if ((isSignal && !(flags & QDBusConnection::ExportSignals)) ||
(!isSignal && !(flags & QDBusConnection::ExportSlots)))
continue;
QString xml = QString(QLatin1String(" <%1 name=\"%2\">\n"))
.arg(isSignal ? QLatin1String("signal") : QLatin1String("method"))
.arg(dbusMemberName(signature.left(paren)));
// check the return type first
int typeId = qDBusNameToTypeId(mm.typeName());
if (typeId)
xml += QString(QLatin1String(" <arg type=\"%1\" direction=\"out\"/>\n"))
.arg(QLatin1String(QDBusType::dbusSignature( QVariant::Type(typeId) )));
else if (*mm.typeName())
continue; // wasn't a valid type
QList<QByteArray> names = mm.parameterNames();
QList<int> types;
int inputCount = qDBusParametersForMethod(QMetaObject::normalizedSignature(signature),
types);
if (inputCount == -1)
continue; // invalid form
if (isSignal && inputCount + 1 != types.count())
continue; // signal with output arguments?
if (isSignal && types.at(inputCount) == QDBusConnectionPrivate::messageMetaType)
continue; // signal with QDBusMessage argument?
int j;
bool isScriptable = mm.attributes() & QMetaMethod::Scriptable;
for (j = 1; j < types.count(); ++j) {
// input parameter for a slot or output for a signal
if (types.at(j) == QDBusConnectionPrivate::messageMetaType) {
isScriptable = true;
continue;
}
QString name;
if (!names.at(j - 1).isEmpty())
name = QString(QLatin1String("name=\"%1\" ")).arg(QLatin1String(names.at(j - 1)));
bool isOutput = isSignal || j > inputCount;
xml += QString(QLatin1String(" <arg %1type=\"%2\" direction=\"%3\"/>\n"))
.arg(name)
.arg(QLatin1String(QDBusType::dbusSignature( QVariant::Type(types.at(j)) )))
.arg(isOutput ? QLatin1String("out") : QLatin1String("in"));
}
if (!isScriptable &&
!(flags & (QDBusConnection::ExportNonScriptableSlots | QDBusConnection::ExportNonScriptableSignals)))
continue;
if (qDBusCheckAsyncTag(mm.tag()))
// add the no-reply annotation
xml += QLatin1String(" <annotation name=\"" ANNOTATION_NO_WAIT "\""
" value=\"true\"/>\n");
retval += xml;
retval += QString(QLatin1String(" </%1>\n"))
.arg(isSignal ? QLatin1String("signal") : QLatin1String("method"));
}
return retval;
}
static QString generateMetaObjectXml(QString interface, const QMetaObject *mo, const QMetaObject *base,
int flags)
{
if (interface.isEmpty()) {
// generate the interface name from the meta object
int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
if (idx >= mo->classInfoOffset()) {
interface = QLatin1String(mo->classInfo(idx).value());
} else {
interface = QLatin1String(mo->className());
interface.replace(QLatin1String("::"), QLatin1String("."));
if (interface.startsWith( QLatin1String("QDBus") )) {
interface.prepend( QLatin1String("com.trolltech.QtDBus.") );
} else if (interface.startsWith( QLatin1Char('Q') )) {
// assume it's Qt
interface.prepend( QLatin1String("com.trolltech.Qt.") );
} else if (!QCoreApplication::instance() ||
QCoreApplication::instance()->applicationName().isEmpty()) {
interface.prepend( QLatin1String("local.") );
} else {
interface.prepend(QLatin1Char('.')).prepend( QCoreApplication::instance()->applicationName() );
QStringList domainName = QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'));
foreach (const QString &part, domainName)
interface.prepend(QLatin1Char('.')).prepend(part);
}
}
}
QString xml;
int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTROSPECTION);
if (idx >= mo->classInfoOffset())
xml = QString::fromUtf8(mo->classInfo(idx).value());
else
xml = generateInterfaceXml(mo, flags, base->methodCount(), base->propertyCount());
return QString(QLatin1String(" <interface name=\"%1\">\n%2 </interface>"))
.arg(interface, xml);
}
static QString generateSubObjectXml(QObject *object)
{
QString retval;
foreach (QObject *child, object->children()) {
QString name = child->objectName();
if (!name.isEmpty())
retval += QString(QLatin1String(" <node name=\"%1\"/>\n"))
.arg(name);
}
return retval;
}
void qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode *node,
const QDBusMessage &msg)
{
// object may be null
QString xml_data(QLatin1String(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE));
xml_data += QLatin1String("<node>\n");
if (node->obj) {
xml_data += QLatin1String( QDBusIntrospectableInterface::staticIntrospectionData() );
xml_data += QLatin1String( QDBusPropertiesInterface::staticIntrospectionData() );
if (node->flags & QDBusConnection::ExportContents) {
const QMetaObject *mo = node->obj->metaObject();
for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
xml_data += generateMetaObjectXml(QString(), mo, &QObject::staticMetaObject,
node->flags);
}
// does this object have adaptors?
QDBusAdaptorConnector *connector;
if (node->flags & QDBusConnection::ExportAdaptors &&
(connector = qDBusFindAdaptorConnector(node->obj))) {
// trasverse every adaptor in this object
QDBusAdaptorConnector::AdaptorMap::ConstIterator it = connector->adaptors.constBegin();
QDBusAdaptorConnector::AdaptorMap::ConstIterator end = connector->adaptors.constEnd();
for ( ; it != end; ++it) {
// add the interface:
QString ifaceXml = QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(it->adaptor);
if (ifaceXml.isEmpty()) {
// add the interface's contents:
ifaceXml += generateMetaObjectXml(it->interface, it->metaObject,
&QDBusAbstractAdaptor::staticMetaObject,
QDBusConnection::ExportNonScriptableContents);
QDBusAbstractAdaptorPrivate::saveIntrospectionXml(it->adaptor, ifaceXml);
}
xml_data += ifaceXml;
}
}
}
if (node->flags & QDBusConnection::ExportChildObjects) {
xml_data += generateSubObjectXml(node->obj);
} else {
// generate from the object tree
foreach (const QDBusConnectionPrivate::ObjectTreeNode::Data &entry, node->children) {
if (entry.node && (entry.node->obj || !entry.node->children.isEmpty()))
xml_data += QString(QLatin1String(" <node name=\"%1\"/>\n"))
.arg(entry.name);
}
}
xml_data += QLatin1String("</node>\n");
// now send it
QDBusMessage reply = QDBusMessage::methodReply(msg);
reply << xml_data;
msg.connection().send(reply);
}
// implement the D-Bus interface org.freedesktop.DBus.Properties
static void sendPropertyError(const QDBusMessage &msg, const QString &interface_name)
{
QDBusMessage error = QDBusMessage::error(msg, QLatin1String(DBUS_ERROR_INVALID_ARGS),
QString::fromLatin1("Interface %1 was not found in object %2")
.arg(interface_name)
.arg(msg.path()));
msg.connection().send(error);
}
void qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode *node, const QDBusMessage &msg)
{
Q_ASSERT(msg.count() == 2);
QString interface_name = msg.at(0).toString();
QByteArray property_name = msg.at(1).toString().toUtf8();
QDBusAdaptorConnector *connector;
QVariant value;
if (node->flags & QDBusConnection::ExportAdaptors &&
(connector = qDBusFindAdaptorConnector(node->obj))) {
// find the class that implements interface_name
QDBusAdaptorConnector::AdaptorMap::ConstIterator it;
it = qLowerBound(connector->adaptors.constBegin(), connector->adaptors.constEnd(),
interface_name);
if (it != connector->adaptors.end() && it->interface == interface_name)
value = it->adaptor->property(property_name);
}
if (!value.isValid() && node->flags & QDBusConnection::ExportProperties) {
// try the object itself
int pidx = node->obj->metaObject()->indexOfProperty(property_name);
if (pidx != -1) {
QMetaProperty mp = node->obj->metaObject()->property(pidx);
if (mp.isScriptable() || (node->flags & QDBusConnection::ExportNonScriptableProperties) ==
QDBusConnection::ExportNonScriptableProperties)
value = mp.read(node->obj);
}
}
if (!value.isValid()) {
// the property was not found
sendPropertyError(msg, interface_name);
return;
}
QDBusMessage reply = QDBusMessage::methodReply(msg);
reply.setSignature(QLatin1String("v"));
reply << value;
msg.connection().send(reply);
}
void qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode *node, const QDBusMessage &msg)
{
Q_ASSERT(msg.count() == 3);
QString interface_name = msg.at(0).toString();
QByteArray property_name = msg.at(1).toString().toUtf8();
QVariant value = msg.at(2).value<QDBusVariant>();
QDBusAdaptorConnector *connector;
if (node->flags & QDBusConnection::ExportAdaptors &&
(connector = qDBusFindAdaptorConnector(node->obj))) {
// find the class that implements interface_name
QDBusAdaptorConnector::AdaptorMap::ConstIterator it;
it = qLowerBound(connector->adaptors.constBegin(), connector->adaptors.constEnd(),
interface_name);
if (it != connector->adaptors.end() && it->interface == interface_name)
if (it->adaptor->setProperty(property_name, value)) {
msg.connection().send(QDBusMessage::methodReply(msg));
return;
}
}
if (!value.isValid() && node->flags & QDBusConnection::ExportProperties) {
// try the object itself
int pidx = node->obj->metaObject()->indexOfProperty(property_name);
if (pidx != -1) {
QMetaProperty mp = node->obj->metaObject()->property(pidx);
if (mp.isScriptable() || (node->flags & QDBusConnection::ExportNonScriptableProperties) ==
QDBusConnection::ExportNonScriptableProperties) {
if (mp.write(node->obj, value)) {
msg.connection().send(QDBusMessage::methodReply(msg));
return;
}
}
}
}
// the property was not found or not written to
sendPropertyError(msg, interface_name);
}
|