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 plugins 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 "main.h"
|
---|
43 | #include <QDebug>
|
---|
44 | #include <QMetaMethod>
|
---|
45 | #include <QScriptExtensionPlugin>
|
---|
46 |
|
---|
47 | #ifndef QT_NO_DBUS
|
---|
48 |
|
---|
49 | QT_USE_NAMESPACE
|
---|
50 |
|
---|
51 | static QScriptValue setupDBusInterface(QScriptEngine *engine, QDBusAbstractInterface *iface);
|
---|
52 |
|
---|
53 | static QScriptValue do_dbus_call(QScriptContext *context, QScriptEngine *engine)
|
---|
54 | {
|
---|
55 | int firstArgument = 0;
|
---|
56 | QString functionName = context->callee().property(QLatin1String("functionName")).toString();
|
---|
57 | if (functionName.isEmpty()) {
|
---|
58 | functionName = context->argument(0).toString();
|
---|
59 | ++firstArgument;
|
---|
60 | }
|
---|
61 |
|
---|
62 | QScriptValue thisObject = context->thisObject();
|
---|
63 | QDBusAbstractInterface *iface = qobject_cast<QDBusAbstractInterface *>(thisObject.toQObject());
|
---|
64 | if (!iface)
|
---|
65 | return QScriptValue();
|
---|
66 |
|
---|
67 | QDBusMessage msg = QDBusMessage::createMethodCall(iface->service(),
|
---|
68 | iface->path(),
|
---|
69 | iface->interface(),
|
---|
70 | functionName);
|
---|
71 |
|
---|
72 | QList<QVariant> args;
|
---|
73 | for (int i = firstArgument; i < context->argumentCount(); ++i) {
|
---|
74 | args.append(context->argument(i).toVariant());
|
---|
75 | }
|
---|
76 | msg.setArguments(args);
|
---|
77 |
|
---|
78 | msg = iface->connection().call(msg);
|
---|
79 |
|
---|
80 | QScriptValue returnValue = engine->nullValue();
|
---|
81 | args = msg.arguments();
|
---|
82 | if (args.count() != 1)
|
---|
83 | return returnValue;
|
---|
84 |
|
---|
85 | QVariant variant = args.first();
|
---|
86 | if (variant.type() == QVariant::UserType
|
---|
87 | && variant.userType() == qMetaTypeId<QDBusObjectPath>()) {
|
---|
88 | QDBusObjectPath path = qvariant_cast<QDBusObjectPath>(variant);
|
---|
89 |
|
---|
90 | QDBusInterface *returnedIface = new QDBusInterface(iface->service(),
|
---|
91 | path.path(),
|
---|
92 | /*interface*/QString(),
|
---|
93 | iface->connection(),
|
---|
94 | engine);
|
---|
95 | returnValue = setupDBusInterface(engine, returnedIface);
|
---|
96 | } else {
|
---|
97 | returnValue = engine->newVariant(variant);
|
---|
98 | }
|
---|
99 |
|
---|
100 | return returnValue;
|
---|
101 | }
|
---|
102 |
|
---|
103 | static QScriptValue setupDBusInterface(QScriptEngine *engine, QDBusAbstractInterface *iface)
|
---|
104 | {
|
---|
105 | QScriptValue v = engine->newQObject(iface);
|
---|
106 |
|
---|
107 | if (!qobject_cast<QDBusConnectionInterface *>(iface)) {
|
---|
108 | const QMetaObject *mo = iface->metaObject();
|
---|
109 | for (int i = 0; i < mo->methodCount(); ++i) {
|
---|
110 | const QMetaMethod method = mo->method(i);
|
---|
111 | const QByteArray signature = method.signature();
|
---|
112 | //qDebug() << "signature" << signature;
|
---|
113 | int parenIndex = signature.indexOf('(');
|
---|
114 | if (parenIndex == -1)
|
---|
115 | continue;
|
---|
116 | const QByteArray name = signature.left(parenIndex);
|
---|
117 | if (name.isEmpty())
|
---|
118 | continue;
|
---|
119 |
|
---|
120 | // don't try to override properties
|
---|
121 | if (mo->indexOfProperty(name) != -1)
|
---|
122 | continue;
|
---|
123 |
|
---|
124 | QScriptValue callWrapper = engine->newFunction(do_dbus_call);
|
---|
125 | const QString nameString = QString::fromAscii(name);
|
---|
126 | callWrapper.setProperty(QLatin1String("functionName"), QScriptValue(engine, nameString));
|
---|
127 | v.setProperty(nameString, callWrapper);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | v.setProperty(QLatin1String("service"), QScriptValue(engine, iface->service()), QScriptValue::ReadOnly);
|
---|
132 | v.setProperty(QLatin1String("path"), QScriptValue(engine, iface->path()), QScriptValue::ReadOnly);
|
---|
133 | v.setProperty(QLatin1String("interface"), QScriptValue(engine, iface->interface()), QScriptValue::ReadOnly);
|
---|
134 | v.setProperty(QLatin1String("isValid"), QScriptValue(engine, iface->isValid()), QScriptValue::ReadOnly);
|
---|
135 | v.setProperty(QLatin1String("connection"), engine->newQObject(new QScriptDBusConnection(iface->connection(), engine)), QScriptValue::ReadOnly);
|
---|
136 |
|
---|
137 | return v;
|
---|
138 | }
|
---|
139 |
|
---|
140 | QDBusConnectionConstructor::QDBusConnectionConstructor(QScriptEngine *engine, QScriptValue extensionObject)
|
---|
141 | : QObject(engine)
|
---|
142 | {
|
---|
143 | QScriptValue ctor = engine->newQObject(this);
|
---|
144 |
|
---|
145 | QScriptValue proto = engine->newQMetaObject(&QDBusConnection::staticMetaObject);
|
---|
146 | proto.setPrototype(engine->globalObject().property(QLatin1String("Function")).property(QLatin1String("prototype")));
|
---|
147 | ctor.setProperty(QLatin1String("prototype"), proto);
|
---|
148 |
|
---|
149 | extensionObject.setProperty(QLatin1String("QDBusConnection"), ctor);
|
---|
150 | }
|
---|
151 |
|
---|
152 | QScriptValue QDBusConnectionConstructor::sessionBus() const
|
---|
153 | {
|
---|
154 | return engine()->newQObject(new QScriptDBusConnection(QDBusConnection::sessionBus(), engine()));
|
---|
155 | }
|
---|
156 |
|
---|
157 | QScriptValue QDBusConnectionConstructor::systemBus() const
|
---|
158 | {
|
---|
159 | return engine()->newQObject(new QScriptDBusConnection(QDBusConnection::systemBus(), engine()));
|
---|
160 | }
|
---|
161 |
|
---|
162 | QObject *QDBusConnectionConstructor::qscript_call(const QString &name)
|
---|
163 | {
|
---|
164 | return new QScriptDBusConnection(QDBusConnection(name), this);
|
---|
165 | }
|
---|
166 |
|
---|
167 | void QDBusConnectionConstructor::disconnectFromBus(const QString &name)
|
---|
168 | {
|
---|
169 | QDBusConnection::disconnectFromBus(name);
|
---|
170 | }
|
---|
171 |
|
---|
172 | QDBusConnection QDBusConnectionConstructor::connectToBus(const QString &address, const QString &name)
|
---|
173 | {
|
---|
174 | return QDBusConnection::connectToBus(address, name);
|
---|
175 | }
|
---|
176 |
|
---|
177 | QDBusConnection QDBusConnectionConstructor::connectToBus(QDBusConnection::BusType type, const QString &name)
|
---|
178 | {
|
---|
179 | return QDBusConnection::connectToBus(type, name);
|
---|
180 | }
|
---|
181 |
|
---|
182 | QScriptDBusConnection::QScriptDBusConnection(const QDBusConnection &conn, QObject *parent)
|
---|
183 | : QObject(parent), connection(conn)
|
---|
184 | {
|
---|
185 | }
|
---|
186 |
|
---|
187 | QScriptValue QScriptDBusConnection::dbusInterface() const
|
---|
188 | {
|
---|
189 | QDBusConnectionInterface *iface = connection.interface();
|
---|
190 | if (!iface)
|
---|
191 | return engine()->nullValue();
|
---|
192 | return setupDBusInterface(engine(), iface);
|
---|
193 | }
|
---|
194 |
|
---|
195 | QScriptDBusInterfaceConstructor::QScriptDBusInterfaceConstructor(QScriptEngine *engine, QScriptValue extensionObject)
|
---|
196 | {
|
---|
197 | QScriptValue ctorValue = engine->newQObject(this);
|
---|
198 | QScriptValue klass = engine->newQMetaObject(metaObject(), ctorValue);
|
---|
199 | extensionObject.setProperty(QLatin1String("QDBusInterface"), klass);
|
---|
200 | }
|
---|
201 |
|
---|
202 | QScriptValue QScriptDBusInterfaceConstructor::qscript_call(const QString &service, const QString &path, const QString &interface,
|
---|
203 | const QScriptValue &conn)
|
---|
204 | {
|
---|
205 | QDBusConnection connection = QDBusConnection::sessionBus();
|
---|
206 |
|
---|
207 | QScriptDBusConnection *connWrapper = qobject_cast<QScriptDBusConnection *>(conn.toQObject());
|
---|
208 | if (connWrapper)
|
---|
209 | connection = connWrapper->dbusConnection();
|
---|
210 |
|
---|
211 | return setupDBusInterface(engine(), new QDBusInterface(service, path, interface, connection, engine()));
|
---|
212 | }
|
---|
213 |
|
---|
214 | QScriptDBusMessageConstructor::QScriptDBusMessageConstructor(QScriptEngine *engine, QScriptValue extensionObject)
|
---|
215 | : QObject(engine)
|
---|
216 | {
|
---|
217 | proto = engine->newQMetaObject(metaObject(), engine->newQObject(this));
|
---|
218 |
|
---|
219 | proto.setProperty(QLatin1String("createReply"), engine->newFunction(createReply));
|
---|
220 | proto.setProperty(QLatin1String("createErrorReply"), engine->newFunction(createErrorReply));
|
---|
221 |
|
---|
222 | extensionObject.setProperty(QLatin1String("QDBusMessage"), proto);
|
---|
223 | engine->setDefaultPrototype(qMetaTypeId<QDBusMessage>(), proto);
|
---|
224 | }
|
---|
225 |
|
---|
226 | QDBusMessage QScriptDBusMessageConstructor::createSignal(const QString &path, const QString &interface, const QString &name)
|
---|
227 | {
|
---|
228 | return QDBusMessage::createSignal(path, interface, name);
|
---|
229 | }
|
---|
230 |
|
---|
231 | QDBusMessage QScriptDBusMessageConstructor::createMethodCall(const QString &destination, const QString &path, const QString &interface, const QString &method)
|
---|
232 | {
|
---|
233 | return QDBusMessage::createMethodCall(destination, path, interface, method);
|
---|
234 | }
|
---|
235 |
|
---|
236 | QDBusMessage QScriptDBusMessageConstructor::createError(const QString &name, const QString &msg)
|
---|
237 | {
|
---|
238 | return QDBusMessage::createError(name, msg);
|
---|
239 | }
|
---|
240 |
|
---|
241 | static QScriptValue messageToScriptValue(QScriptEngine *engine, const QDBusMessage &message)
|
---|
242 | {
|
---|
243 | QScriptValue v = engine->newVariant(QVariant::fromValue(message));
|
---|
244 | v.setProperty(QLatin1String("service"), QScriptValue(engine, message.service()), QScriptValue::ReadOnly);
|
---|
245 | v.setProperty(QLatin1String("path"), QScriptValue(engine, message.path()), QScriptValue::ReadOnly);
|
---|
246 | v.setProperty(QLatin1String("interface"), QScriptValue(engine, message.interface()), QScriptValue::ReadOnly);
|
---|
247 | v.setProperty(QLatin1String("member"), QScriptValue(engine, message.member()), QScriptValue::ReadOnly);
|
---|
|
---|