| 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 QtDeclarative 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 | #ifndef QDECLARATIVEENGINE_P_H
|
|---|
| 43 | #define QDECLARATIVEENGINE_P_H
|
|---|
| 44 |
|
|---|
| 45 | //
|
|---|
| 46 | // W A R N I N G
|
|---|
| 47 | // -------------
|
|---|
| 48 | //
|
|---|
| 49 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 50 | // implementation detail. This header file may change from version to
|
|---|
| 51 | // version without notice, or even be removed.
|
|---|
| 52 | //
|
|---|
| 53 | // We mean it.
|
|---|
| 54 | //
|
|---|
| 55 |
|
|---|
| 56 | #include "qdeclarativeengine.h"
|
|---|
| 57 |
|
|---|
| 58 | #include "private/qdeclarativetypeloader_p.h"
|
|---|
| 59 | #include "private/qdeclarativeimport_p.h"
|
|---|
| 60 | #include "private/qpodvector_p.h"
|
|---|
| 61 | #include "qdeclarative.h"
|
|---|
| 62 | #include "private/qdeclarativevaluetype_p.h"
|
|---|
| 63 | #include "qdeclarativecontext.h"
|
|---|
| 64 | #include "private/qdeclarativecontext_p.h"
|
|---|
| 65 | #include "qdeclarativeexpression.h"
|
|---|
| 66 | #include "qdeclarativeimageprovider.h"
|
|---|
| 67 | #include "private/qdeclarativeproperty_p.h"
|
|---|
| 68 | #include "private/qdeclarativepropertycache_p.h"
|
|---|
| 69 | #include "private/qdeclarativeobjectscriptclass_p.h"
|
|---|
| 70 | #include "private/qdeclarativecontextscriptclass_p.h"
|
|---|
| 71 | #include "private/qdeclarativevaluetypescriptclass_p.h"
|
|---|
| 72 | #include "private/qdeclarativemetatype_p.h"
|
|---|
| 73 | #include "private/qdeclarativedirparser_p.h"
|
|---|
| 74 |
|
|---|
| 75 | #include <QtScript/QScriptClass>
|
|---|
| 76 | #include <QtScript/QScriptValue>
|
|---|
| 77 | #include <QtScript/QScriptString>
|
|---|
| 78 | #include <QtCore/qstring.h>
|
|---|
| 79 | #include <QtCore/qlist.h>
|
|---|
| 80 | #include <QtCore/qpair.h>
|
|---|
| 81 | #include <QtCore/qstack.h>
|
|---|
| 82 | #include <QtCore/qmutex.h>
|
|---|
| 83 | #include <QtScript/qscriptengine.h>
|
|---|
| 84 |
|
|---|
| 85 | #include <private/qobject_p.h>
|
|---|
| 86 |
|
|---|
| 87 | QT_BEGIN_NAMESPACE
|
|---|
| 88 |
|
|---|
| 89 | class QDeclarativeContext;
|
|---|
| 90 | class QDeclarativeEngine;
|
|---|
| 91 | class QDeclarativeContextPrivate;
|
|---|
| 92 | class QDeclarativeExpression;
|
|---|
| 93 | class QDeclarativeContextScriptClass;
|
|---|
| 94 | class QDeclarativeImportDatabase;
|
|---|
| 95 | class QDeclarativeObjectScriptClass;
|
|---|
| 96 | class QDeclarativeTypeNameScriptClass;
|
|---|
| 97 | class QDeclarativeValueTypeScriptClass;
|
|---|
| 98 | class QScriptEngineDebugger;
|
|---|
| 99 | class QNetworkReply;
|
|---|
| 100 | class QNetworkAccessManager;
|
|---|
| 101 | class QDeclarativeNetworkAccessManagerFactory;
|
|---|
| 102 | class QDeclarativeAbstractBinding;
|
|---|
| 103 | class QScriptDeclarativeClass;
|
|---|
| 104 | class QDeclarativeTypeNameScriptClass;
|
|---|
| 105 | class QDeclarativeTypeNameCache;
|
|---|
| 106 | class QDeclarativeComponentAttached;
|
|---|
| 107 | class QDeclarativeListScriptClass;
|
|---|
| 108 | class QDeclarativeCleanup;
|
|---|
| 109 | class QDeclarativeDelayedError;
|
|---|
| 110 | class QDeclarativeWorkerScriptEngine;
|
|---|
| 111 | class QDeclarativeGlobalScriptClass;
|
|---|
| 112 | class QDir;
|
|---|
| 113 |
|
|---|
| 114 | class QDeclarativeScriptEngine : public QScriptEngine
|
|---|
| 115 | {
|
|---|
| 116 | public:
|
|---|
| 117 | QDeclarativeScriptEngine(QDeclarativeEnginePrivate *priv);
|
|---|
| 118 | virtual ~QDeclarativeScriptEngine();
|
|---|
| 119 |
|
|---|
| 120 | QUrl resolvedUrl(QScriptContext *context, const QUrl& url); // resolved against p's context, or baseUrl if no p
|
|---|
| 121 | static QScriptValue resolvedUrl(QScriptContext *ctxt, QScriptEngine *engine);
|
|---|
| 122 |
|
|---|
| 123 | static QDeclarativeScriptEngine *get(QScriptEngine* e) { return static_cast<QDeclarativeScriptEngine*>(e); }
|
|---|
| 124 |
|
|---|
| 125 | QDeclarativeEnginePrivate *p;
|
|---|
| 126 |
|
|---|
| 127 | // User by SQL API
|
|---|
| 128 | QScriptClass *sqlQueryClass;
|
|---|
| 129 | QString offlineStoragePath;
|
|---|
| 130 |
|
|---|
| 131 | // Used by DOM Core 3 API
|
|---|
| 132 | QScriptClass *namedNodeMapClass;
|
|---|
| 133 | QScriptClass *nodeListClass;
|
|---|
| 134 |
|
|---|
| 135 | QUrl baseUrl;
|
|---|
| 136 |
|
|---|
| 137 | virtual QNetworkAccessManager *networkAccessManager();
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| 140 | class Q_AUTOTEST_EXPORT QDeclarativeEnginePrivate : public QObjectPrivate
|
|---|
| 141 | {
|
|---|
| 142 | Q_DECLARE_PUBLIC(QDeclarativeEngine)
|
|---|
| 143 | public:
|
|---|
| 144 | QDeclarativeEnginePrivate(QDeclarativeEngine *);
|
|---|
| 145 | ~QDeclarativeEnginePrivate();
|
|---|
| 146 |
|
|---|
| 147 | void init();
|
|---|
| 148 |
|
|---|
| 149 | struct CapturedProperty {
|
|---|
| 150 | CapturedProperty(QObject *o, int c, int n)
|
|---|
| 151 | : object(o), coreIndex(c), notifier(0), notifyIndex(n) {}
|
|---|
| 152 | CapturedProperty(QDeclarativeNotifier *n)
|
|---|
| 153 | : object(0), coreIndex(-1), notifier(n), notifyIndex(-1) {}
|
|---|
| 154 |
|
|---|
| 155 | QObject *object;
|
|---|
| 156 | int coreIndex;
|
|---|
| 157 | QDeclarativeNotifier *notifier;
|
|---|
| 158 | int notifyIndex;
|
|---|
| 159 | };
|
|---|
| 160 | bool captureProperties;
|
|---|
| 161 | QPODVector<CapturedProperty> capturedProperties;
|
|---|
| 162 |
|
|---|
| 163 | QDeclarativeContext *rootContext;
|
|---|
| 164 | bool isDebugging;
|
|---|
| 165 |
|
|---|
| 166 | bool outputWarningsToStdErr;
|
|---|
| 167 |
|
|---|
| 168 | QDeclarativeContextScriptClass *contextClass;
|
|---|
| 169 | QDeclarativeContextData *sharedContext;
|
|---|
| 170 | QObject *sharedScope;
|
|---|
| 171 | QDeclarativeObjectScriptClass *objectClass;
|
|---|
| 172 | QDeclarativeValueTypeScriptClass *valueTypeClass;
|
|---|
| 173 | QDeclarativeTypeNameScriptClass *typeNameClass;
|
|---|
| 174 | QDeclarativeListScriptClass *listClass;
|
|---|
| 175 | // Global script class
|
|---|
| 176 | QDeclarativeGlobalScriptClass *globalClass;
|
|---|
| 177 |
|
|---|
| 178 | // Registered cleanup handlers
|
|---|
| 179 | QDeclarativeCleanup *cleanup;
|
|---|
| 180 |
|
|---|
| 181 | // Bindings that have had errors during startup
|
|---|
| 182 | QDeclarativeDelayedError *erroredBindings;
|
|---|
| 183 | int inProgressCreations;
|
|---|
| 184 |
|
|---|
| 185 | QDeclarativeScriptEngine scriptEngine;
|
|---|
| 186 |
|
|---|
| 187 | QDeclarativeWorkerScriptEngine *getWorkerScriptEngine();
|
|---|
| 188 | QDeclarativeWorkerScriptEngine *workerScriptEngine;
|
|---|
| 189 |
|
|---|
| 190 | QUrl baseUrl;
|
|---|
| 191 |
|
|---|
| 192 | template<class T>
|
|---|
| 193 | struct SimpleList {
|
|---|
| 194 | SimpleList()
|
|---|
| 195 | : count(0), values(0) {}
|
|---|
| 196 | SimpleList(int r)
|
|---|
| 197 | : count(0), values(new T*[r]) {}
|
|---|
| 198 |
|
|---|
| 199 | int count;
|
|---|
| 200 | T **values;
|
|---|
| 201 |
|
|---|
| 202 | void append(T *v) {
|
|---|
| 203 | values[count++] = v;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | T *at(int idx) const {
|
|---|
| 207 | return values[idx];
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void clear() {
|
|---|
| 211 | delete [] values;
|
|---|
| 212 | }
|
|---|
| 213 | };
|
|---|
| 214 |
|
|---|
| 215 | static void clear(SimpleList<QDeclarativeAbstractBinding> &);
|
|---|
| 216 | static void clear(SimpleList<QDeclarativeParserStatus> &);
|
|---|
| 217 |
|
|---|
| 218 | QList<SimpleList<QDeclarativeAbstractBinding> > bindValues;
|
|---|
| 219 | QList<SimpleList<QDeclarativeParserStatus> > parserStatus;
|
|---|
| 220 | QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus;
|
|---|
| 221 | QDeclarativeComponentAttached *componentAttached;
|
|---|
| 222 |
|
|---|
| 223 | void registerFinalizedParserStatusObject(QObject *obj, int index) {
|
|---|
| 224 | finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index));
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | bool inBeginCreate;
|
|---|
| 228 |
|
|---|
| 229 | QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const;
|
|---|
| 230 | QNetworkAccessManager *getNetworkAccessManager() const;
|
|---|
| 231 | mutable QNetworkAccessManager *networkAccessManager;
|
|---|
| 232 | mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory;
|
|---|
| 233 |
|
|---|
| 234 | QHash<QString,QSharedPointer<QDeclarativeImageProvider> > imageProviders;
|
|---|
| 235 | QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url);
|
|---|
| 236 | QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
|
|---|
| 237 | QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
|
|---|
| 238 |
|
|---|
| 239 | mutable QMutex mutex;
|
|---|
| 240 |
|
|---|
| 241 | QDeclarativeTypeLoader typeLoader;
|
|---|
| 242 | QDeclarativeImportDatabase importDatabase;
|
|---|
| 243 |
|
|---|
| 244 | QString offlineStoragePath;
|
|---|
| 245 |
|
|---|
| 246 | mutable quint32 uniqueId;
|
|---|
| 247 | quint32 getUniqueId() const {
|
|---|
| 248 | return uniqueId++;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | QDeclarativeValueTypeFactory valueTypes;
|
|---|
| 252 |
|
|---|
| 253 | QHash<const QMetaObject *, QDeclarativePropertyCache *> propertyCache;
|
|---|
| 254 | inline QDeclarativePropertyCache *cache(QObject *obj);
|
|---|
| 255 | inline QDeclarativePropertyCache *cache(const QMetaObject *);
|
|---|
| 256 |
|
|---|
| 257 | void registerCompositeType(QDeclarativeCompiledData *);
|
|---|
| 258 |
|
|---|
| 259 | bool isQObject(int);
|
|---|
| 260 | QObject *toQObject(const QVariant &, bool *ok = 0) const;
|
|---|
| 261 | QDeclarativeMetaType::TypeCategory typeCategory(int) const;
|
|---|
| 262 | bool isList(int) const;
|
|---|
| 263 | int listType(int) const;
|
|---|
| 264 | const QMetaObject *rawMetaObjectForType(int) const;
|
|---|
| 265 | const QMetaObject *metaObjectForType(int) const;
|
|---|
| 266 | QHash<int, int> m_qmlLists;
|
|---|
| 267 | QHash<int, QDeclarativeCompiledData *> m_compositeTypes;
|
|---|
| 268 |
|
|---|
| 269 | QHash<QString, QScriptValue> m_sharedScriptImports;
|
|---|
| 270 |
|
|---|
| 271 | QScriptValue scriptValueFromVariant(const QVariant &);
|
|---|
| 272 | QVariant scriptValueToVariant(const QScriptValue &, int hint = QVariant::Invalid);
|
|---|
| 273 |
|
|---|
| 274 | void sendQuit();
|
|---|
| 275 | void warning(const QDeclarativeError &);
|
|---|
| 276 | void warning(const QList<QDeclarativeError> &);
|
|---|
| 277 | static void warning(QDeclarativeEngine *, const QDeclarativeError &);
|
|---|
| 278 | static void warning(QDeclarativeEngine *, const QList<QDeclarativeError> &);
|
|---|
| 279 | static void warning(QDeclarativeEnginePrivate *, const QDeclarativeError &);
|
|---|
| 280 | static void warning(QDeclarativeEnginePrivate *, const QList<QDeclarativeError> &);
|
|---|
| 281 |
|
|---|
| 282 | static QScriptValue qmlScriptObject(QObject*, QDeclarativeEngine*);
|
|---|
| 283 |
|
|---|
| 284 | static QScriptValue createComponent(QScriptContext*, QScriptEngine*);
|
|---|
| 285 | static QScriptValue createQmlObject(QScriptContext*, QScriptEngine*);
|
|---|
| 286 | static QScriptValue isQtObject(QScriptContext*, QScriptEngine*);
|
|---|
| 287 | static QScriptValue vector3d(QScriptContext*, QScriptEngine*);
|
|---|
| 288 | static QScriptValue rgba(QScriptContext*, QScriptEngine*);
|
|---|
| 289 | static QScriptValue hsla(QScriptContext*, QScriptEngine*);
|
|---|
| 290 | static QScriptValue point(QScriptContext*, QScriptEngine*);
|
|---|
| 291 | static QScriptValue size(QScriptContext*, QScriptEngine*);
|
|---|
| 292 | static QScriptValue rect(QScriptContext*, QScriptEngine*);
|
|---|
| 293 |
|
|---|
| 294 | static QScriptValue lighter(QScriptContext*, QScriptEngine*);
|
|---|
| 295 | static QScriptValue darker(QScriptContext*, QScriptEngine*);
|
|---|
| 296 | static QScriptValue tint(QScriptContext*, QScriptEngine*);
|
|---|
| 297 |
|
|---|
| 298 | static QScriptValue desktopOpenUrl(QScriptContext*, QScriptEngine*);
|
|---|
| 299 | static QScriptValue fontFamilies(QScriptContext*, QScriptEngine*);
|
|---|
| 300 | static QScriptValue md5(QScriptContext*, QScriptEngine*);
|
|---|
| 301 | static QScriptValue btoa(QScriptContext*, QScriptEngine*);
|
|---|
| 302 | static QScriptValue atob(QScriptContext*, QScriptEngine*);
|
|---|
| 303 | static QScriptValue consoleLog(QScriptContext*, QScriptEngine*);
|
|---|
| 304 | static QScriptValue quit(QScriptContext*, QScriptEngine*);
|
|---|
| 305 |
|
|---|
| 306 | #ifndef QT_NO_DATESTRING
|
|---|
| 307 | static QScriptValue formatDate(QScriptContext*, QScriptEngine*);
|
|---|
| 308 | static QScriptValue formatTime(QScriptContext*, QScriptEngine*);
|
|---|
| 309 | static QScriptValue formatDateTime(QScriptContext*, QScriptEngine*);
|
|---|
| 310 | #endif
|
|---|
| 311 | static QScriptEngine *getScriptEngine(QDeclarativeEngine *e) { return &e->d_func()->scriptEngine; }
|
|---|
| 312 | static QDeclarativeEngine *getEngine(QScriptEngine *e) { return static_cast<QDeclarativeScriptEngine*>(e)->p->q_func(); }
|
|---|
| 313 | static QDeclarativeEnginePrivate *get(QDeclarativeEngine *e) { return e->d_func(); }
|
|---|
| 314 | static QDeclarativeEnginePrivate *get(QDeclarativeContext *c) { return (c && c->engine()) ? QDeclarativeEnginePrivate::get(c->engine()) : 0; }
|
|---|
| 315 | static QDeclarativeEnginePrivate *get(QDeclarativeContextData *c) { return (c && c->engine) ? QDeclarativeEnginePrivate::get(c->engine) : 0; }
|
|---|
| 316 | static QDeclarativeEnginePrivate *get(QScriptEngine *e) { return static_cast<QDeclarativeScriptEngine*>(e)->p; }
|
|---|
| 317 | static QDeclarativeEngine *get(QDeclarativeEnginePrivate *p) { return p->q_func(); }
|
|---|
| 318 | QDeclarativeContextData *getContext(QScriptContext *);
|
|---|
| 319 | QUrl getUrl(QScriptContext *);
|
|---|
| 320 |
|
|---|
| 321 | static QString urlToLocalFileOrQrc(const QUrl& url);
|
|---|
| 322 |
|
|---|
| 323 | static void defineModule();
|
|---|
| 324 |
|
|---|
| 325 | static bool qml_debugging_enabled;
|
|---|
| 326 | };
|
|---|
| 327 |
|
|---|
| 328 | /*!
|
|---|
| 329 | Returns a QDeclarativePropertyCache for \a obj if one is available.
|
|---|
| 330 |
|
|---|
| 331 | If \a obj is null, being deleted or contains a dynamic meta object 0
|
|---|
| 332 | is returned.
|
|---|
| 333 | */
|
|---|
| 334 | QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QObject *obj)
|
|---|
| 335 | {
|
|---|
| 336 | Q_Q(QDeclarativeEngine);
|
|---|
| 337 | if (!obj || QObjectPrivate::get(obj)->metaObject || QObjectPrivate::get(obj)->wasDeleted)
|
|---|
| 338 | return 0;
|
|---|
| 339 |
|
|---|
| 340 | const QMetaObject *mo = obj->metaObject();
|
|---|
| 341 | QDeclarativePropertyCache *rv = propertyCache.value(mo);
|
|---|
| 342 | if (!rv) {
|
|---|
| 343 | rv = new QDeclarativePropertyCache(q, mo);
|
|---|
| 344 | propertyCache.insert(mo, rv);
|
|---|
| 345 | }
|
|---|
| 346 | return rv;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /*!
|
|---|
| 350 | Returns a QDeclarativePropertyCache for \a metaObject.
|
|---|
| 351 |
|
|---|
| 352 | As the cache is persisted for the life of the engine, \a metaObject must be
|
|---|
| 353 | a static "compile time" meta-object, or a meta-object that is otherwise known to
|
|---|
| 354 | exist for the lifetime of the QDeclarativeEngine.
|
|---|
| 355 | */
|
|---|
| 356 | QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(const QMetaObject *metaObject)
|
|---|
| 357 | {
|
|---|
| 358 | Q_Q(QDeclarativeEngine);
|
|---|
| 359 | Q_ASSERT(metaObject);
|
|---|
| 360 |
|
|---|
| 361 | QDeclarativePropertyCache *rv = propertyCache.value(metaObject);
|
|---|
| 362 | if (!rv) {
|
|---|
| 363 | rv = new QDeclarativePropertyCache(q, metaObject);
|
|---|
| 364 | propertyCache.insert(metaObject, rv);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | return rv;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | QT_END_NAMESPACE
|
|---|
| 371 |
|
|---|
| 372 | #endif // QDECLARATIVEENGINE_P_H
|
|---|