1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QDBUSBINDING_H
41 #define QDBUSBINDING_H
42 
43 #include <QtDBus/QtDBus>
44 #include <QtScript/qscriptable.h>
45 #include <QtScript/qscriptengine.h>
46 
47 #ifndef QT_NO_DBUS
48 
49 class QDBusConnectionConstructor : public QObject,
50                                    public QScriptable
51 {
52     Q_OBJECT
53     Q_PROPERTY(QScriptValue sessionBus READ sessionBus)
54     Q_PROPERTY(QScriptValue systemBus READ systemBus)
55 
56 public:
57     QDBusConnectionConstructor(QScriptEngine *engine, QScriptValue extensionObject);
58 
59     QScriptValue sessionBus() const;
60     QScriptValue systemBus() const;
61 
62 public Q_SLOTS:
63     QObject *qscript_call(const QString &name);
64 
65     void disconnectFromBus(const QString &name);
66     QDBusConnection connectToBus(const QString &address, const QString &name);
67     QDBusConnection connectToBus(QDBusConnection::BusType type, const QString &name);
68 };
69 
70 class QScriptDBusConnection : public QObject,
71                               public QScriptable
72 {
73     Q_OBJECT
74     Q_PROPERTY(QString baseService READ baseService)
75     Q_PROPERTY(bool isConnected READ isConnected)
76     Q_PROPERTY(QScriptValue dbusInterface READ dbusInterface)
77 public:
78     QScriptDBusConnection(const QDBusConnection &conn, QObject *parent);
79 
baseService()80     inline QString baseService() const { return connection.baseService(); }
isConnected()81     inline bool isConnected() const { return connection.isConnected(); }
82     QScriptValue dbusInterface() const;
83 
dbusConnection()84     inline QDBusConnection dbusConnection() const { return connection; }
85 
86 public Q_SLOTS:
send(const QDBusMessage & message)87     inline bool send(const QDBusMessage &message) const
88     { return connection.send(message); }
89     inline QDBusMessage call(const QDBusMessage &message, int callMode = QDBus::Block, int timeout = -1) const
90     { return connection.call(message, QDBus::CallMode(callMode), timeout); }
91 
registerService(const QString & serviceName)92     inline bool registerService(const QString &serviceName)
93     { return connection.registerService(serviceName); }
unregisterService(const QString & serviceName)94     inline bool unregisterService(const QString &serviceName)
95     { return connection.unregisterService(serviceName); }
96 
lastError()97     inline QDBusError lastError() const
98     { return connection.lastError(); }
99 
100     inline void unregisterObject(const QString &path, QDBusConnection::UnregisterMode mode = QDBusConnection::UnregisterNode)
101     { return connection.unregisterObject(path, mode); }
objectRegisteredAt(const QString & path)102     inline QObject *objectRegisteredAt(const QString &path) const
103     { return connection.objectRegisteredAt(path); }
104 
105 #if 0
106     bool callWithCallback(const QDBusMessage &message, QObject *receiver,
107                           const char *slot, int timeout = -1) const;
108 
109     bool connect(const QString &service, const QString &path, const QString &interface,
110                  const QString &name, QObject *receiver, const char *slot);
111     bool disconnect(const QString &service, const QString &path, const QString &interface,
112                     const QString &name, QObject *receiver, const char *slot);
113 
114     bool connect(const QString &service, const QString &path, const QString &interface,
115                  const QString &name, const QString& signature,
116                  QObject *receiver, const char *slot);
117     bool disconnect(const QString &service, const QString &path, const QString &interface,
118                     const QString &name, const QString& signature,
119                     QObject *receiver, const char *slot);
120 
121     bool registerObject(const QString &path, QObject *object,
122                         RegisterOptions options = ExportAdaptors);
123 
124 #endif
125 
126 private:
127     QDBusConnection connection;
128 };
129 
Q_DECLARE_METATYPE(QScriptDBusConnection *)130 Q_DECLARE_METATYPE(QScriptDBusConnection*)
131 
132 class QScriptDBusInterfaceConstructor : public QObject,
133                                         public QScriptable
134 {
135     Q_OBJECT
136 public:
137     QScriptDBusInterfaceConstructor(QScriptEngine *engine, QScriptValue extensionObject);
138 
139 public Q_SLOTS:
140     QScriptValue qscript_call(const QString &service, const QString &path, const QString &interface = QString(),
141                               const QScriptValue &conn = QScriptValue());
142 };
143 
Q_DECLARE_METATYPE(QDBusMessage)144 Q_DECLARE_METATYPE(QDBusMessage)
145 
146 class QScriptDBusMessageConstructor : public QObject, public QScriptable
147 {
148     Q_OBJECT
149     Q_ENUMS(MessageType)
150 public:
151     enum MessageType {
152         InvalidMessage = QDBusMessage::InvalidMessage,
153         MethodCallMessage = QDBusMessage::MethodCallMessage,
154         ReplyMessage = QDBusMessage::ReplyMessage,
155         ErrorMessage = QDBusMessage::ErrorMessage,
156         SignalMessage = QDBusMessage::SignalMessage
157     };
158 
159     QScriptDBusMessageConstructor(QScriptEngine *engine, QScriptValue extensionObject);
160 
161     QScriptValue protoType() const { return proto; }
162 
163 public Q_SLOTS:
164     QDBusMessage createSignal(const QString &path, const QString &interface, const QString &name);
165     QDBusMessage createMethodCall(const QString &destination, const QString &path, const QString &interface, const QString &method);
166     QDBusMessage createError(const QString &name, const QString &msg);
167 
168 public:
169     static QScriptValue createReply(QScriptContext *context, QScriptEngine *engine);
170     static QScriptValue createErrorReply(QScriptContext *context, QScriptEngine *engine);
171 
172 private:
173     QScriptValue proto;
174 };
175 
176 #endif // QT_NO_DBUS
177 #endif // QDBUSBINDING_H
178