1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdbusabstractadaptor.h"
43
44#include <QtCore/qcoreapplication.h>
45#include <QtCore/qmetaobject.h>
46#include <QtCore/qset.h>
47#include <QtCore/qtimer.h>
48#include <QtCore/qthread.h>
49
50#include "qdbusconnection.h"
51
52#include "qdbusconnection_p.h" // for qDBusParametersForMethod
53#include "qdbusabstractadaptor_p.h"
54#include "qdbusmetatype_p.h"
55
56#ifndef QT_NO_DBUS
57
58QT_BEGIN_NAMESPACE
59
60QDBusAdaptorConnector *qDBusFindAdaptorConnector(QObject *obj)
61{
62 if (!obj)
63 return 0;
64 const QObjectList &children = obj->children();
65 QObjectList::ConstIterator it = children.constBegin();
66 QObjectList::ConstIterator end = children.constEnd();
67 for ( ; it != end; ++it) {
68 QDBusAdaptorConnector *connector = qobject_cast<QDBusAdaptorConnector *>(*it);
69 if (connector) {
70 connector->polish();
71 return connector;
72 }
73 }
74 return 0;
75}
76
77QDBusAdaptorConnector *qDBusFindAdaptorConnector(QDBusAbstractAdaptor *adaptor)
78{
79 return qDBusFindAdaptorConnector(adaptor->parent());
80}
81
82QDBusAdaptorConnector *qDBusCreateAdaptorConnector(QObject *obj)
83{
84 QDBusAdaptorConnector *connector = qDBusFindAdaptorConnector(obj);
85 if (connector)
86 return connector;
87 return new QDBusAdaptorConnector(obj);
88}
89
90QString QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(QDBusAbstractAdaptor *adaptor)
91{
92 return adaptor->d_func()->xml;
93}
94
95void QDBusAbstractAdaptorPrivate::saveIntrospectionXml(QDBusAbstractAdaptor *adaptor,
96 const QString &xml)
97{
98 adaptor->d_func()->xml = xml;
99}
100
101/*!
102 \class QDBusAbstractAdaptor
103 \inmodule QtDBus
104 \since 4.2
105
106 \brief The QDBusAbstractAdaptor class is the base class of D-Bus adaptor classes.
107
108 The QDBusAbstractAdaptor class is the starting point for all objects intending to provide
109 interfaces to the external world using D-Bus. This is accomplished by attaching a one or more
110 classes derived from QDBusAbstractAdaptor to a normal QObject and then registering that QObject
111 with QDBusConnection::registerObject. QDBusAbstractAdaptor objects are intended to be
112 light-weight wrappers, mostly just relaying calls into the real object (its parent) and the
113 signals from it.
114
115 Each QDBusAbstractAdaptor-derived class should define the D-Bus interface it is implementing
116 using the Q_CLASSINFO macro in the class definition. Note that only one interface can be
117 exposed in this way.
118
119 QDBusAbstractAdaptor uses the standard QObject mechanism of signals, slots and properties to
120 determine what signals, methods and properties to export to the bus. Any signal emitted by
121 QDBusAbstractAdaptor-derived classes will be automatically be relayed through any D-Bus
122 connections the object is registered on.
123
124 Classes derived from QDBusAbstractAdaptor must be created on the heap using the \a new operator
125 and must not be deleted by the user (they will be deleted automatically when the object they are
126 connected to is also deleted).
127
128 \sa {usingadaptors.html}{Using adaptors}, QDBusConnection
129*/
130
131/*!
132 Constructs a QDBusAbstractAdaptor with \a obj as the parent object.
133*/
134QDBusAbstractAdaptor::QDBusAbstractAdaptor(QObject* obj)
135 : QObject(*new QDBusAbstractAdaptorPrivate, obj)
136{
137 QDBusAdaptorConnector *connector = qDBusCreateAdaptorConnector(obj);
138
139 connector->waitingForPolish = true;
140 QMetaObject::invokeMethod(connector, "polish", Qt::QueuedConnection);
141}
142
143/*!
144 Destroys the adaptor.
145
146 \warning Adaptors are destroyed automatically when the real object they refer to is
147 destroyed. Do not delete the adaptors yourself.
148*/
149QDBusAbstractAdaptor::~QDBusAbstractAdaptor()
150{
151}
152
153/*!
154 Toggles automatic signal relaying from the real object (see object()).
155
156 Automatic signal relaying consists of signal-to-signal connection of the signals on the parent
157 that have the exact same method signatue in both classes.
158
159 If \a enable is set to true, connect the signals; if set to false, disconnect all signals.
160*/
161void QDBusAbstractAdaptor::setAutoRelaySignals(bool enable)
162{
163 const QMetaObject *us = metaObject();
164 const QMetaObject *them = parent()->metaObject();
165 bool connected = false;
166 for (int idx = staticMetaObject.methodCount(); idx < us->methodCount(); ++idx) {
167 QMetaMethod mm = us->method(idx);
168
169 if (mm.methodType() != QMetaMethod::Signal)
170 continue;
171
172 // try to connect/disconnect to a signal on the parent that has the same method signature
173 QByteArray sig = QMetaObject::normalizedSignature(mm.signature());
174 if (them->indexOfSignal(sig) == -1)
175 continue;
176 sig.prepend(QSIGNAL_CODE + '0');
177 parent()->disconnect(sig, this, sig);
178 if (enable)
179 connected = connect(parent(), sig, sig) || connected;
180 }
181 d_func()->autoRelaySignals = connected;
182}
183
184/*!
185 Returns true if automatic signal relaying from the real object (see object()) is enabled,
186 otherwiser returns false.
187
188 \sa setAutoRelaySignals()
189*/
190bool QDBusAbstractAdaptor::autoRelaySignals() const
191{
192 return d_func()->autoRelaySignals;
193}
194
195QDBusAdaptorConnector::QDBusAdaptorConnector(QObject *obj)
196 : QObject(obj), waitingForPolish(false)
197{
198}
199
200QDBusAdaptorConnector::~QDBusAdaptorConnector()
201{
202}
203
204void QDBusAdaptorConnector::addAdaptor(QDBusAbstractAdaptor *adaptor)
205{
206 // find the interface name
207 const QMetaObject *mo = adaptor->metaObject();
208 int ciid = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
209 if (ciid != -1) {
210 QMetaClassInfo mci = mo->classInfo(ciid);
211 if (*mci.value()) {
212 // find out if this interface exists first
213 const char *interface = mci.value();
214 AdaptorMap::Iterator it = qLowerBound(adaptors.begin(), adaptors.end(),
215 QByteArray(interface));
216 if (it != adaptors.end() && qstrcmp(interface, it->interface) == 0) {
217 // exists. Replace it (though it's probably the same)
218 if (it->adaptor != adaptor) {
219 // reconnect the signals
220 disconnectAllSignals(it->adaptor);
221 connectAllSignals(adaptor);
222 }
223 it->adaptor = adaptor;
224 } else {
225 // create a new one
226 AdaptorData entry;
227 entry.interface = interface;
228 entry.adaptor = adaptor;
229 adaptors << entry;
230
231 // connect the adaptor's signals to our relaySlot slot
232 connectAllSignals(adaptor);
233 }
234 }
235 }
236}
237
238void QDBusAdaptorConnector::disconnectAllSignals(QObject *obj)
239{
240 QMetaObject::disconnect(obj, -1, this, metaObject()->methodOffset());
241}
242
243void QDBusAdaptorConnector::connectAllSignals(QObject *obj)
244{
245 QMetaObject::connect(obj, -1, this, metaObject()->methodOffset(), Qt::DirectConnection);
246}
247
248void QDBusAdaptorConnector::polish()
249{
250 if (!waitingForPolish)
251 return; // avoid working multiple times if multiple adaptors were added
252
253 waitingForPolish = false;
254 const QObjectList &objs = parent()->children();
255 QObjectList::ConstIterator it = objs.constBegin();
256 QObjectList::ConstIterator end = objs.constEnd();
257 for ( ; it != end; ++it) {
258 QDBusAbstractAdaptor *adaptor = qobject_cast<QDBusAbstractAdaptor *>(*it);
259 if (adaptor)
260 addAdaptor(adaptor);
261 }
262
263 // sort the adaptor list
264 qSort(adaptors);
265}
266
267void QDBusAdaptorConnector::relaySlot(void **argv)
268{
269 QObjectPrivate *d = static_cast<QObjectPrivate *>(d_ptr.data());
270 if (Q_LIKELY(d->currentSender)) {
271 relay(d->currentSender->sender, d->currentSender->signal, argv);
272 } else {
273 qWarning("QtDBus: cannot relay signals from parent %s(%p \"%s\") unless they are emitted in the object's thread %s(%p \"%s\"). "
274 "Current thread is %s(%p \"%s\").",
275 parent()->metaObject()->className(), parent(), qPrintable(parent()->objectName()),
276 parent()->thread()->metaObject()->className(), parent()->thread(), qPrintable(parent()->thread()->objectName()),
277 QThread::currentThread()->metaObject()->className(), QThread::currentThread(), qPrintable(QThread::currentThread()->objectName()));
278 }
279}
280
281void QDBusAdaptorConnector::relay(QObject *senderObj, int lastSignalIdx, void **argv)
282{
283 if (lastSignalIdx < QObject::staticMetaObject.methodCount())
284 // QObject signal (destroyed(QObject *)) -- ignore
285 return;
286
287 const QMetaObject *senderMetaObject = senderObj->metaObject();
288 QMetaMethod mm = senderMetaObject->method(lastSignalIdx);
289
290 QObject *realObject = senderObj;
291 if (qobject_cast<QDBusAbstractAdaptor *>(senderObj))
292 // it's an adaptor, so the real object is in fact its parent
293 realObject = realObject->parent();
294
295 // break down the parameter list
296 QList<int> types;
297 int inputCount = qDBusParametersForMethod(mm, types);
298 if (inputCount == -1)
299 // invalid signal signature
300 // qDBusParametersForMethod has already complained
301 return;
302 if (inputCount + 1 != types.count() ||
303 types.at(inputCount) == QDBusMetaTypeId::message) {
304 // invalid signal signature
305 // qDBusParametersForMethod has not yet complained about this one
306 qWarning("QDBusAbstractAdaptor: Cannot relay signal %s::%s",
307 senderMetaObject->className(), mm.signature());
308 return;
309 }
310
311 QVariantList args;
312 for (int i = 1; i < types.count(); ++i)
313 args << QVariant(types.at(i), argv[i]);
314
315 // now emit the signal with all the information
316 emit relaySignal(realObject, senderMetaObject, lastSignalIdx, args);
317}
318
319// our Meta Object
320// modify carefully: this has been hand-edited!
321// the relaySlot slot has local ID 0 (we use this when calling QMetaObject::connect)
322// it also gets called with the void** array
323
324static const uint qt_meta_data_QDBusAdaptorConnector[] = {
325 // content:
326 1, // revision
327 0, // classname
328 0, 0, // classinfo
329 3, 10, // methods
330 0, 0, // properties
331 0, 0, // enums/sets
332
333 // slots: signature, parameters, type, tag, flags
334 106, 22, 22, 22, 0x0a,
335 118, 22, 22, 22, 0x0a,
336
337 // signals: signature, parameters, type, tag, flags
338 47, 23, 22, 22, 0x05,
339
340 0 // eod
341};
342
343static const char qt_meta_stringdata_QDBusAdaptorConnector[] = {
344 "QDBusAdaptorConnector\0\0obj,metaobject,sid,args\0"
345 "relaySignal(QObject*,const QMetaObject*,int,QVariantList)\0\0relaySlot()\0"
346 "polish()\0"
347};
348
349const QMetaObject QDBusAdaptorConnector::staticMetaObject = {
350 { &QObject::staticMetaObject, qt_meta_stringdata_QDBusAdaptorConnector,
351 qt_meta_data_QDBusAdaptorConnector, 0 }
352};
353
354const QMetaObject *QDBusAdaptorConnector::metaObject() const
355{
356 return &staticMetaObject;
357}
358
359void *QDBusAdaptorConnector::qt_metacast(const char *_clname)
360{
361 if (!_clname) return 0;
362 if (!strcmp(_clname, qt_meta_stringdata_QDBusAdaptorConnector))
363 return static_cast<void*>(const_cast<QDBusAdaptorConnector*>(this));
364 return QObject::qt_metacast(_clname);
365}
366
367int QDBusAdaptorConnector::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
368{
369 _id = QObject::qt_metacall(_c, _id, _a);
370 if (_id < 0)
371 return _id;
372 if (_c == QMetaObject::InvokeMetaMethod) {
373 switch (_id) {
374 case 0: relaySlot(_a); break; // HAND EDIT: add the _a parameter
375 case 1: polish(); break;
376 case 2: relaySignal((*reinterpret_cast< QObject*(*)>(_a[1])),(*reinterpret_cast< const QMetaObject*(*)>(_a[2])),(*reinterpret_cast< int(*)>(_a[3])),(*reinterpret_cast< const QVariantList(*)>(_a[4]))); break;
377 }
378 _id -= 3;
379 }
380 return _id;
381}
382
383// SIGNAL 0
384void QDBusAdaptorConnector::relaySignal(QObject * _t1, const QMetaObject * _t2, int _t3, const QVariantList & _t4)
385{
386 void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)), const_cast<void*>(reinterpret_cast<const void*>(&_t2)), const_cast<void*>(reinterpret_cast<const void*>(&_t3)), const_cast<void*>(reinterpret_cast<const void*>(&_t4)) };
387 QMetaObject::activate(this, &staticMetaObject, 2, _a);
388}
389
390QT_END_NAMESPACE
391
392#endif // QT_NO_DBUS
393