| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 21 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 22 | ** products derived from this software without specific prior written
|
|---|
| 23 | ** permission.
|
|---|
| 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 | ** $QT_END_LICENSE$
|
|---|
| 37 | **
|
|---|
| 38 | ****************************************************************************/
|
|---|
| 39 |
|
|---|
| 40 | #ifndef QAXSCRIPT_H
|
|---|
| 41 | #define QAXSCRIPT_H
|
|---|
| 42 |
|
|---|
| 43 | #include <ActiveQt/qaxobject.h>
|
|---|
| 44 |
|
|---|
| 45 | struct IActiveScript;
|
|---|
| 46 |
|
|---|
| 47 | QT_BEGIN_HEADER
|
|---|
| 48 |
|
|---|
| 49 | QT_BEGIN_NAMESPACE
|
|---|
| 50 |
|
|---|
| 51 | QT_MODULE(ActiveQt)
|
|---|
| 52 |
|
|---|
| 53 | #ifndef QT_NO_WIN_ACTIVEQT
|
|---|
| 54 |
|
|---|
| 55 | class QAxBase;
|
|---|
| 56 | class QAxScript;
|
|---|
| 57 | class QAxScriptSite;
|
|---|
| 58 | class QAxScriptEngine;
|
|---|
| 59 | class QAxScriptManager;
|
|---|
| 60 | class QAxScriptManagerPrivate;
|
|---|
| 61 |
|
|---|
| 62 | class QAxScriptEngine : public QAxObject
|
|---|
| 63 | {
|
|---|
| 64 | public:
|
|---|
| 65 | enum State {
|
|---|
| 66 | Uninitialized = 0,
|
|---|
| 67 | Initialized = 5,
|
|---|
| 68 | Started = 1,
|
|---|
| 69 | Connected = 2,
|
|---|
| 70 | Disconnected = 3,
|
|---|
| 71 | Closed = 4
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | QAxScriptEngine(const QString &language, QAxScript *script);
|
|---|
| 75 | ~QAxScriptEngine();
|
|---|
| 76 |
|
|---|
| 77 | bool isValid() const;
|
|---|
| 78 | bool hasIntrospection() const;
|
|---|
| 79 |
|
|---|
| 80 | QString scriptLanguage() const;
|
|---|
| 81 |
|
|---|
| 82 | State state() const;
|
|---|
| 83 | void setState(State st);
|
|---|
| 84 |
|
|---|
| 85 | void addItem(const QString &name);
|
|---|
| 86 |
|
|---|
| 87 | long queryInterface(const QUuid &, void**) const;
|
|---|
| 88 |
|
|---|
| 89 | protected:
|
|---|
| 90 | bool initialize(IUnknown** ptr);
|
|---|
| 91 |
|
|---|
| 92 | private:
|
|---|
| 93 | QAxScript *script_code;
|
|---|
| 94 | IActiveScript *engine;
|
|---|
| 95 |
|
|---|
| 96 | QString script_language;
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | class QAxScript : public QObject
|
|---|
| 100 | {
|
|---|
| 101 | Q_OBJECT
|
|---|
| 102 |
|
|---|
| 103 | public:
|
|---|
| 104 | enum FunctionFlags {
|
|---|
| 105 | FunctionNames = 0,
|
|---|
| 106 | FunctionSignatures
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | QAxScript(const QString &name, QAxScriptManager *manager);
|
|---|
| 110 | ~QAxScript();
|
|---|
| 111 |
|
|---|
| 112 | bool load(const QString &code, const QString &language = QString());
|
|---|
| 113 |
|
|---|
| 114 | QStringList functions(FunctionFlags = FunctionNames) const;
|
|---|
| 115 |
|
|---|
| 116 | QString scriptCode() const;
|
|---|
| 117 | QString scriptName() const;
|
|---|
| 118 | QAxScriptEngine *scriptEngine() const;
|
|---|
| 119 |
|
|---|
| 120 | QVariant call(const QString &function, const QVariant &v1 = QVariant(),
|
|---|
| 121 | const QVariant &v2 = QVariant(),
|
|---|
| 122 | const QVariant &v3 = QVariant(),
|
|---|
| 123 | const QVariant &v4 = QVariant(),
|
|---|
| 124 | const QVariant &v5 = QVariant(),
|
|---|
| 125 | const QVariant &v6 = QVariant(),
|
|---|
| 126 | const QVariant &v7 = QVariant(),
|
|---|
| 127 | const QVariant &v8 = QVariant());
|
|---|
| 128 | QVariant call(const QString &function, QList<QVariant> &arguments);
|
|---|
| 129 |
|
|---|
| 130 | Q_SIGNALS:
|
|---|
| 131 | void entered();
|
|---|
| 132 | void finished();
|
|---|
| 133 | void finished(const QVariant &result);
|
|---|
| 134 | void finished(int code, const QString &source,const QString &description, const QString &help);
|
|---|
| 135 | void stateChanged(int state);
|
|---|
| 136 | void error(int code, const QString &description, int sourcePosition, const QString &sourceText);
|
|---|
| 137 |
|
|---|
| 138 | private:
|
|---|
| 139 | friend class QAxScriptSite;
|
|---|
| 140 | friend class QAxScriptEngine;
|
|---|
| 141 |
|
|---|
| 142 | void updateObjects();
|
|---|
| 143 | QAxBase *findObject(const QString &name);
|
|---|
| 144 |
|
|---|
| 145 | QString script_name;
|
|---|
| 146 | QString script_code;
|
|---|
| 147 | QAxScriptManager *script_manager;
|
|---|
| 148 | QAxScriptEngine *script_engine;
|
|---|
| 149 | QAxScriptSite *script_site;
|
|---|
| 150 | };
|
|---|
| 151 |
|
|---|
| 152 | class QAxScriptManager : public QObject
|
|---|
| 153 | {
|
|---|
| 154 | Q_OBJECT
|
|---|
| 155 |
|
|---|
| 156 | public:
|
|---|
| 157 | QAxScriptManager(QObject *parent = 0);
|
|---|
| 158 | ~QAxScriptManager();
|
|---|
| 159 |
|
|---|
| 160 | void addObject(QAxBase *object);
|
|---|
| 161 | void addObject(QObject *object);
|
|---|
| 162 |
|
|---|
| 163 | QStringList functions(QAxScript::FunctionFlags = QAxScript::FunctionNames) const;
|
|---|
| 164 | QStringList scriptNames() const;
|
|---|
| 165 | QAxScript *script(const QString &name) const;
|
|---|
| 166 |
|
|---|
| 167 | QAxScript* load(const QString &code, const QString &name, const QString &language);
|
|---|
| 168 | QAxScript* load(const QString &file, const QString &name);
|
|---|
| 169 |
|
|---|
| 170 | QVariant call(const QString &function, const QVariant &v1 = QVariant(),
|
|---|
| 171 | const QVariant &v2 = QVariant(),
|
|---|
| 172 | const QVariant &v3 = QVariant(),
|
|---|
| 173 | const QVariant &v4 = QVariant(),
|
|---|
| 174 | const QVariant &v5 = QVariant(),
|
|---|
| 175 | const QVariant &v6 = QVariant(),
|
|---|
| 176 | const QVariant &v7 = QVariant(),
|
|---|
| 177 | const QVariant &v8 = QVariant());
|
|---|
| 178 | QVariant call(const QString &function, QList<QVariant> &arguments);
|
|---|
| 179 |
|
|---|
| 180 | static bool registerEngine(const QString &name, const QString &extension, const QString &code = QString());
|
|---|
| 181 | static QString scriptFileFilter();
|
|---|
| 182 |
|
|---|
| 183 | Q_SIGNALS:
|
|---|
| 184 | void error(QAxScript *script, int code, const QString &description, int sourcePosition, const QString &sourceText);
|
|---|
| 185 |
|
|---|
| 186 | private Q_SLOTS:
|
|---|
| 187 | void objectDestroyed(QObject *o);
|
|---|
| 188 | void scriptError(int code, const QString &description, int sourcePosition, const QString &sourceText);
|
|---|
| 189 |
|
|---|
| 190 | private:
|
|---|
| 191 | friend class QAxScript;
|
|---|
| 192 | QAxScriptManagerPrivate *d;
|
|---|
| 193 |
|
|---|
| 194 | void updateScript(QAxScript*);
|
|---|
| 195 | QAxScript *scriptForFunction(const QString &function) const;
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | // QAxScript inlines
|
|---|
| 200 |
|
|---|
| 201 | inline QString QAxScript::scriptCode() const
|
|---|
| 202 | {
|
|---|
| 203 | return script_code;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | inline QString QAxScript::scriptName() const
|
|---|
| 207 | {
|
|---|
| 208 | return script_name;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | inline QAxScriptEngine *QAxScript::scriptEngine() const
|
|---|
| 212 | {
|
|---|
| 213 | return script_engine;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | // QAxScriptEngine inlines
|
|---|
| 217 |
|
|---|
| 218 | inline bool QAxScriptEngine::isValid() const
|
|---|
| 219 | {
|
|---|
| 220 | return engine != 0;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | inline QString QAxScriptEngine::scriptLanguage() const
|
|---|
| 224 | {
|
|---|
| 225 | return script_language;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | // QAxScriptManager inlines
|
|---|
| 229 |
|
|---|
| 230 | extern QAxBase *qax_create_object_wrapper(QObject*);
|
|---|
| 231 |
|
|---|
| 232 | inline void QAxScriptManager::addObject(QObject *object)
|
|---|
| 233 | {
|
|---|
| 234 | QAxBase *wrapper = qax_create_object_wrapper(object);
|
|---|
| 235 | if (!wrapper) {
|
|---|
| 236 | qWarning("QAxScriptMananger::addObject: Class %s not exposed through the QAxFactory",
|
|---|
| 237 | object->metaObject()->className());
|
|---|
| 238 | Q_ASSERT(wrapper);
|
|---|
| 239 | }
|
|---|
| 240 | addObject(wrapper);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | QT_END_NAMESPACE
|
|---|
| 244 | #endif // QT_NO_WIN_ACTIVEQT
|
|---|
| 245 |
|
|---|
| 246 | QT_END_HEADER
|
|---|
| 247 |
|
|---|
| 248 | #endif // QAXSCRIPT_H
|
|---|