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

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 13.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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
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,