source: trunk/src/dbus/qdbusabstractadaptor.cpp@ 637

Last change on this file since 637 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 13.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtDBus module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
56QT_BEGIN_NAMESPACE
57
58QDBusAdaptorConnector *qDBusFindAdaptorConnector(QObject *obj)
59{
60 if (!obj)
61 return 0;
62 const QObjectList &children = obj->children();
63 QObjectList::ConstIterator it = children.constBegin();
64 QObjectList::ConstIterator end = children.constEnd();
65 for ( ; it != end; ++it) {
66 QDBusAdaptorConnector *connector = qobject_cast<QDBusAdaptorConnector *>(*it);
67 if (connector) {
68 connector->polish();
69 return connector;
70 }
71 }
72 return 0;
73}
74
75QDBusAdaptorConnector *qDBusFindAdaptorConnector(QDBusAbstractAdaptor *adaptor)
76{
77 return qDBusFindAdaptorConnector(adaptor->parent());
78}
79
80QDBusAdaptorConnector *qDBusCreateAdaptorConnector(QObject *obj)
81{
82 QDBusAdaptorConnector *connector = qDBusFindAdaptorConnector(obj);
83 if (connector)
84 return connector;
85 return new QDBusAdaptorConnector(obj);
86}
87
88QString QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(QDBusAbstractAdaptor *adaptor)
89{
90 return adaptor->d_func()->xml;
91}
92
93void QDBusAbstractAdaptorPrivate::saveIntrospectionXml(QDBusAbstractAdaptor *adaptor,
94 const QString &xml)
95{
96 adaptor->d_func()->xml = xml;
97}
98
99/*!
100 \class QDBusAbstractAdaptor
101 \inmodule QtDBus
102 \since 4.2
103
104 \brief The QDBusAbstractAdaptor class is the base class of D-Bus adaptor classes.
105
106 The QDBusAbstractAdaptor class is the starting point for all objects intending to provide
107 interfaces to the external world using D-Bus. This is accomplished by attaching a one or more
108 classes derived from QDBusAbstractAdaptor to a normal QObject and then registering that QObject
109 with QDBusConnection::registerObject. QDBusAbstractAdaptor objects are intended to be
110 light-weight wrappers, mostly just relaying calls into the real object (its parent) and the
111 signals from it.
112
113 Each QDBusAbstractAdaptor-derived class should define the D-Bus interface it is implementing
114 using the Q_CLASSINFO macro in the class definition. Note that only one interface can be
115 exposed in this way.
116
117 QDBusAbstractAdaptor uses the standard QObject mechanism of signals, slots and properties to
118 determine what signals, methods and properties to export to the bus. Any signal emitted by
119 QDBusAbstractAdaptor-derived classes will be automatically be relayed through any D-Bus
120 connections the object is registered on.
121
122 Classes derived from QDBusAbstractAdaptor must be created on the heap using the \a new operator
123 and must not be deleted by the user (they will be deleted automatically when the object they are
124 connected to is also deleted).
125
126 \sa {usingadaptors.html}{Using adaptors}, QDBusConnection
127*/
128
129/*!
130 Constructs a QDBusAbstractAdaptor with \a obj as the parent object.
131*/
132QDBusAbstractAdaptor::QDBusAbstractAdaptor(QObject* obj)
133 : QObject(*new QDBusAbstractAdaptorPrivate, obj)
134{
135 QDBusAdaptorConnector *connector = qDBusCreateAdaptorConnector(obj);
136
137 connector->waitingForPolish = true;
138 QMetaObject::invokeMethod(connector, "polish", Qt::QueuedConnection);
139}
140
141/*!
142 Destroys the adaptor.
143
144 \warning Adaptors are destroyed automatically when the real object they refer to is
145 destroyed. Do not delete the adaptors yourself.
146*/
147QDBusAbstractAdaptor::~QDBusAbstractAdaptor()
148{
149}
150
151/*!
152 Toggles automatic signal relaying from the real object (see object()).
153
154 Automatic signal relaying consists of signal-to-signal connection of the signals on the parent
155 that have the exact same method signatue in both classes.
156
157 If \a enable is set to true, connect the signals; if set to false, disconnect all signals.
158*/
159void QDBusAbstractAdaptor::setAutoRelaySignals(bool enable)
160{
161 const QMetaObject *us = metaObject();
162 const QMetaObject *them = parent()->metaObject();
163 bool connected = false;
164 for (int idx = staticMetaObject.methodCount(); idx < us->methodCount(); ++idx) {
165 QMetaMethod mm = us->method(idx);
166
167 if (mm.methodType() != QMetaMethod::Signal)
168 continue;
169
170 // try to connect/disconnect to a signal on the parent that has the same method signature
171 QByteArray sig = QMetaObject::normalizedSignature(mm.signature());
172 if (them->indexOfSignal(sig) == -1)
173 continue;
174 sig.prepend(QSIGNAL_CODE + '0');
175 parent()->disconnect(sig, this, sig);
176 if (enable)
177 connected = connect(parent(), sig, sig) || connected;
178 }
179 d_func()->autoRelaySignals = connected;
180}
181
182/*!
183 Returns true if automatic signal relaying from the real object (see object()) is enabled,
184 otherwiser returns false.
185
186 \sa setAutoRelaySignals()
187*/
188bool QDBusAbstractAdaptor::autoRelaySignals() const
189{
190 return d_func()->autoRelaySignals;
191}
192
193QDBusAdaptorConnector::QDBusAdaptorConnector(QObject *obj)
194 : QObject(obj), waitingForPolish(false)
195{
196}
197
198QDBusAdaptorConnector::~QDBusAdaptorConnector()
199{
200}
201
202void QDBusAdaptorConnector::addAdaptor(QDBusAbstractAdaptor *adaptor)
203{
204 // find the interface name
205 const QMetaObject *mo = adaptor->metaObject();
206 int ciid = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
207 if (ciid != -1) {
208 QMetaClassInfo mci = mo->classInfo(ciid);
209 if (*mci.value()) {
210 // find out if this interface exists first
211 const char *interface = mci.value();
212 AdaptorMap::Iterator it = qLowerBound(adaptors.begin(), adaptors.end(),
213 QByteArray(interface));
214 if (it != adaptors.end() && qstrcmp(interface, it->interface) == 0) {
215 // exists. Replace it (though it's probably the same)
216 if (it->adaptor != adaptor) {
217 // reconnect the signals
218 disconnectAllSignals(it->adaptor);
219 connectAllSignals(adaptor);
220 }
221 it->adaptor = adaptor;
222 } else {
223 // create a new one
224 AdaptorData entry;
225 entry.interface = interface;