1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the ActiveQt framework of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef QAXSCRIPT_H
42 #define QAXSCRIPT_H
43 
44 #include <ActiveQt/qaxobject.h>
45 
46 struct IActiveScript;
47 
48 QT_BEGIN_HEADER
49 
50 QT_BEGIN_NAMESPACE
51 
52 QT_MODULE(ActiveQt)
53 
54 #ifndef QT_NO_WIN_ACTIVEQT
55 
56 class QAxBase;
57 class QAxScript;
58 class QAxScriptSite;
59 class QAxScriptEngine;
60 class QAxScriptManager;
61 class QAxScriptManagerPrivate;
62 
63 class QAxScriptEngine : public QAxObject
64 {
65 public:
66     enum State {
67         Uninitialized = 0,
68         Initialized = 5,
69         Started = 1,
70         Connected = 2,
71         Disconnected = 3,
72         Closed = 4
73     };
74 
75     QAxScriptEngine(const QString &language, QAxScript *script);
76     ~QAxScriptEngine();
77 
78     bool isValid() const;
79     bool hasIntrospection() const;
80 
81     QString scriptLanguage() const;
82 
83     State state() const;
84     void setState(State st);
85 
86     void addItem(const QString &name);
87 
88     long queryInterface(const QUuid &, void**) const;
89 
90 protected:
91     bool initialize(IUnknown** ptr);
92 
93 private:
94     QAxScript *script_code;
95     IActiveScript *engine;
96 
97     QString script_language;
98 };
99 
100 class QAxScript : public QObject
101 {
102     Q_OBJECT
103 
104 public:
105     enum FunctionFlags {
106         FunctionNames = 0,
107         FunctionSignatures
108     };
109 
110     QAxScript(const QString &name, QAxScriptManager *manager);
111     ~QAxScript();
112 
113     bool load(const QString &code, const QString &language = QString());
114 
115     QStringList functions(FunctionFlags = FunctionNames) const;
116 
117     QString scriptCode() const;
118     QString scriptName() const;
119     QAxScriptEngine *scriptEngine() const;
120 
121     QVariant call(const QString &function, const QVariant &v1 = QVariant(),
122 					   const QVariant &v2 = QVariant(),
123                                            const QVariant &v3 = QVariant(),
124                                            const QVariant &v4 = QVariant(),
125                                            const QVariant &v5 = QVariant(),
126                                            const QVariant &v6 = QVariant(),
127                                            const QVariant &v7 = QVariant(),
128                                            const QVariant &v8 = QVariant());
129     QVariant call(const QString &function, QList<QVariant> &arguments);
130 
131 Q_SIGNALS:
132     void entered();
133     void finished();
134     void finished(const QVariant &result);
135     void finished(int code, const QString &source,const QString &description, const QString &help);
136     void stateChanged(int state);
137     void error(int code, const QString &description, int sourcePosition, const QString &sourceText);
138 
139 private:
140     friend class QAxScriptSite;
141     friend class QAxScriptEngine;
142 
143     void updateObjects();
144     QAxBase *findObject(const QString &name);
145 
146     QString script_name;
147     QString script_code;
148     QAxScriptManager *script_manager;
149     QAxScriptEngine *script_engine;
150     QAxScriptSite *script_site;
151 };
152 
153 class QAxScriptManager : public QObject
154 {
155     Q_OBJECT
156 
157 public:
158     QAxScriptManager(QObject *parent = 0);
159     ~QAxScriptManager();
160 
161     void addObject(QAxBase *object);
162     void addObject(QObject *object);
163 
164     QStringList functions(QAxScript::FunctionFlags = QAxScript::FunctionNames) const;
165     QStringList scriptNames() const;
166     QAxScript *script(const QString &name) const;
167 
168     QAxScript* load(const QString &code, const QString &name, const QString &language);
169     QAxScript* load(const QString &file, const QString &name);
170 
171     QVariant call(const QString &function, const QVariant &v1 = QVariant(),
172 					   const QVariant &v2 = QVariant(),
173                                            const QVariant &v3 = QVariant(),
174                                            const QVariant &v4 = QVariant(),
175                                            const QVariant &v5 = QVariant(),
176                                            const QVariant &v6 = QVariant(),
177                                            const QVariant &v7 = QVariant(),
178                                            const QVariant &v8 = QVariant());
179     QVariant call(const QString &function, QList<QVariant> &arguments);
180 
181     static bool registerEngine(const QString &name, const QString &extension, const QString &code = QString());
182     static QString scriptFileFilter();
183 
184 Q_SIGNALS:
185     void error(QAxScript *script, int code, const QString &description, int sourcePosition, const QString &sourceText);
186 
187 private Q_SLOTS:
188     void objectDestroyed(QObject *o);
189     void scriptError(int code, const QString &description, int sourcePosition, const QString &sourceText);
190 
191 private:
192     friend class QAxScript;
193     QAxScriptManagerPrivate *d;
194 
195     void updateScript(QAxScript*);
196     QAxScript *scriptForFunction(const QString &function) const;
197 };
198 
199 
200 // QAxScript inlines
201 
scriptCode()202 inline QString QAxScript::scriptCode() const
203 {
204     return script_code;
205 }
206 
scriptName()207 inline QString QAxScript::scriptName() const
208 {
209     return script_name;
210 }
211 
scriptEngine()212 inline QAxScriptEngine *QAxScript::scriptEngine() const
213 {
214     return script_engine;
215 }
216 
217 // QAxScriptEngine inlines
218 
isValid()219 inline bool QAxScriptEngine::isValid() const
220 {
221     return engine != 0;
222 }
223 
scriptLanguage()224 inline QString QAxScriptEngine::scriptLanguage() const
225 {
226     return script_language;
227 }
228 
229 // QAxScriptManager inlines
230 
231 extern QAxBase *qax_create_object_wrapper(QObject*);
232 
addObject(QObject * object)233 inline void QAxScriptManager::addObject(QObject *object)
234 {
235     QAxBase *wrapper = qax_create_object_wrapper(object);
236     if (!wrapper) {
237         qWarning("QAxScriptMananger::addObject: Class %s not exposed through the QAxFactory",
238             object->metaObject()->className());
239         Q_ASSERT(wrapper);
240     }
241     addObject(wrapper);
242 }
243 
244 QT_END_NAMESPACE
245 #endif // QT_NO_WIN_ACTIVEQT
246 
247 QT_END_HEADER
248 
249 #endif // QAXSCRIPT_H
250