|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtDeclarative module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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/qdeclarativeclassfactory_p.h" |
|
59 #include "private/qdeclarativecompositetypemanager_p.h" |
|
60 #include "private/qdeclarativeimport_p.h" |
|
61 #include "private/qpodvector_p.h" |
|
62 #include "qdeclarative.h" |
|
63 #include "private/qdeclarativevaluetype_p.h" |
|
64 #include "qdeclarativecontext.h" |
|
65 #include "private/qdeclarativecontext_p.h" |
|
66 #include "qdeclarativeexpression.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 QDeclarativeExpression *currentExpression; |
|
165 bool isDebugging; |
|
166 |
|
167 bool outputWarningsToStdErr; |
|
168 |
|
169 QDeclarativeContextScriptClass *contextClass; |
|
170 QDeclarativeContextData *sharedContext; |
|
171 QObject *sharedScope; |
|
172 QDeclarativeObjectScriptClass *objectClass; |
|
173 QDeclarativeValueTypeScriptClass *valueTypeClass; |
|
174 QDeclarativeTypeNameScriptClass *typeNameClass; |
|
175 QDeclarativeListScriptClass *listClass; |
|
176 // Global script class |
|
177 QDeclarativeGlobalScriptClass *globalClass; |
|
178 |
|
179 // Registered cleanup handlers |
|
180 QDeclarativeCleanup *cleanup; |
|
181 |
|
182 // Bindings that have had errors during startup |
|
183 QDeclarativeDelayedError *erroredBindings; |
|
184 int inProgressCreations; |
|
185 |
|
186 QDeclarativeScriptEngine scriptEngine; |
|
187 |
|
188 QDeclarativeWorkerScriptEngine *getWorkerScriptEngine(); |
|
189 QDeclarativeWorkerScriptEngine *workerScriptEngine; |
|
190 |
|
191 QUrl baseUrl; |
|
192 |
|
193 template<class T> |
|
194 struct SimpleList { |
|
195 SimpleList() |
|
196 : count(0), values(0) {} |
|
197 SimpleList(int r) |
|
198 : count(0), values(new T*[r]) {} |
|
199 |
|
200 int count; |
|
201 T **values; |
|
202 |
|
203 void append(T *v) { |
|
204 values[count++] = v; |
|
205 } |
|
206 |
|
207 T *at(int idx) const { |
|
208 return values[idx]; |
|
209 } |
|
210 |
|
211 void clear() { |
|
212 delete [] values; |
|
213 } |
|
214 }; |
|
215 |
|
216 static void clear(SimpleList<QDeclarativeAbstractBinding> &); |
|
217 static void clear(SimpleList<QDeclarativeParserStatus> &); |
|
218 |
|
219 QList<SimpleList<QDeclarativeAbstractBinding> > bindValues; |
|
220 QList<SimpleList<QDeclarativeParserStatus> > parserStatus; |
|
221 QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus; |
|
222 QDeclarativeComponentAttached *componentAttached; |
|
223 |
|
224 void registerFinalizedParserStatusObject(QObject *obj, int index) { |
|
225 finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index)); |
|
226 } |
|
227 |
|
228 bool inBeginCreate; |
|
229 |
|
230 QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const; |
|
231 QNetworkAccessManager *getNetworkAccessManager() const; |
|
232 mutable QNetworkAccessManager *networkAccessManager; |
|
233 mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory; |
|
234 |
|
235 QHash<QString,QDeclarativeImageProvider*> imageProviders; |
|
236 QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size); |
|
237 |
|
238 mutable QMutex mutex; |
|
239 |
|
240 QDeclarativeCompositeTypeManager typeManager; |
|
241 QDeclarativeImportDatabase importDatabase; |
|
242 |
|
243 QString offlineStoragePath; |
|
244 |
|
245 mutable quint32 uniqueId; |
|
246 quint32 getUniqueId() const { |
|
247 return uniqueId++; |
|
248 } |
|
249 |
|
250 QDeclarativeValueTypeFactory valueTypes; |
|
251 |
|
252 QHash<const QMetaObject *, QDeclarativePropertyCache *> propertyCache; |
|
253 inline QDeclarativePropertyCache *cache(QObject *obj); |
|
254 inline QDeclarativePropertyCache *cache(const QMetaObject *); |
|
255 |
|
256 void registerCompositeType(QDeclarativeCompiledData *); |
|
257 |
|
258 bool isQObject(int); |
|
259 QObject *toQObject(const QVariant &, bool *ok = 0) const; |
|
260 QDeclarativeMetaType::TypeCategory typeCategory(int) const; |
|
261 bool isList(int) const; |
|
262 int listType(int) const; |
|
263 const QMetaObject *rawMetaObjectForType(int) const; |
|
264 const QMetaObject *metaObjectForType(int) const; |
|
265 QHash<int, int> m_qmlLists; |
|
266 QHash<int, QDeclarativeCompiledData *> m_compositeTypes; |
|
267 |
|
268 QHash<QString, QScriptValue> m_sharedScriptImports; |
|
269 |
|
270 QScriptValue scriptValueFromVariant(const QVariant &); |
|
271 QVariant scriptValueToVariant(const QScriptValue &, int hint = QVariant::Invalid); |
|
272 |
|
273 void sendQuit(); |
|
274 void warning(const QDeclarativeError &); |
|
275 void warning(const QList<QDeclarativeError> &); |
|
276 static void warning(QDeclarativeEngine *, const QDeclarativeError &); |
|
277 static void warning(QDeclarativeEngine *, const QList<QDeclarativeError> &); |
|
278 static void warning(QDeclarativeEnginePrivate *, const QDeclarativeError &); |
|
279 static void warning(QDeclarativeEnginePrivate *, const QList<QDeclarativeError> &); |
|
280 |
|
281 static QScriptValue qmlScriptObject(QObject*, QDeclarativeEngine*); |
|
282 |
|
283 static QScriptValue createComponent(QScriptContext*, QScriptEngine*); |
|
284 static QScriptValue createQmlObject(QScriptContext*, QScriptEngine*); |
|
285 static QScriptValue isQtObject(QScriptContext*, QScriptEngine*); |
|
286 static QScriptValue vector3d(QScriptContext*, QScriptEngine*); |
|
287 static QScriptValue rgba(QScriptContext*, QScriptEngine*); |
|
288 static QScriptValue hsla(QScriptContext*, QScriptEngine*); |
|
289 static QScriptValue point(QScriptContext*, QScriptEngine*); |
|
290 static QScriptValue size(QScriptContext*, QScriptEngine*); |
|
291 static QScriptValue rect(QScriptContext*, QScriptEngine*); |
|
292 |
|
293 static QScriptValue lighter(QScriptContext*, QScriptEngine*); |
|
294 static QScriptValue darker(QScriptContext*, QScriptEngine*); |
|
295 static QScriptValue tint(QScriptContext*, QScriptEngine*); |
|
296 |
|
297 static QScriptValue desktopOpenUrl(QScriptContext*, QScriptEngine*); |
|
298 static QScriptValue fontFamilies(QScriptContext*, QScriptEngine*); |
|
299 static QScriptValue md5(QScriptContext*, QScriptEngine*); |
|
300 static QScriptValue btoa(QScriptContext*, QScriptEngine*); |
|
301 static QScriptValue atob(QScriptContext*, QScriptEngine*); |
|
302 static QScriptValue consoleLog(QScriptContext*, QScriptEngine*); |
|
303 static QScriptValue quit(QScriptContext*, QScriptEngine*); |
|
304 |
|
305 static QScriptValue formatDate(QScriptContext*, QScriptEngine*); |
|
306 static QScriptValue formatTime(QScriptContext*, QScriptEngine*); |
|
307 static QScriptValue formatDateTime(QScriptContext*, QScriptEngine*); |
|
308 |
|
309 static QScriptEngine *getScriptEngine(QDeclarativeEngine *e) { return &e->d_func()->scriptEngine; } |
|
310 static QDeclarativeEngine *getEngine(QScriptEngine *e) { return static_cast<QDeclarativeScriptEngine*>(e)->p->q_func(); } |
|
311 static QDeclarativeEnginePrivate *get(QDeclarativeEngine *e) { return e->d_func(); } |
|
312 static QDeclarativeEnginePrivate *get(QDeclarativeContext *c) { return (c && c->engine()) ? QDeclarativeEnginePrivate::get(c->engine()) : 0; } |
|
313 static QDeclarativeEnginePrivate *get(QDeclarativeContextData *c) { return (c && c->engine) ? QDeclarativeEnginePrivate::get(c->engine) : 0; } |
|
314 static QDeclarativeEnginePrivate *get(QScriptEngine *e) { return static_cast<QDeclarativeScriptEngine*>(e)->p; } |
|
315 static QDeclarativeEngine *get(QDeclarativeEnginePrivate *p) { return p->q_func(); } |
|
316 QDeclarativeContextData *getContext(QScriptContext *); |
|
317 |
|
318 static QString urlToLocalFileOrQrc(const QUrl& url); |
|
319 |
|
320 static void defineModule(); |
|
321 }; |
|
322 |
|
323 /*! |
|
324 Returns a QDeclarativePropertyCache for \a obj if one is available. |
|
325 |
|
326 If \a obj is null, being deleted or contains a dynamic meta object 0 |
|
327 is returned. |
|
328 */ |
|
329 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QObject *obj) |
|
330 { |
|
331 Q_Q(QDeclarativeEngine); |
|
332 if (!obj || QObjectPrivate::get(obj)->metaObject || QObjectPrivate::get(obj)->wasDeleted) |
|
333 return 0; |
|
334 |
|
335 const QMetaObject *mo = obj->metaObject(); |
|
336 QDeclarativePropertyCache *rv = propertyCache.value(mo); |
|
337 if (!rv) { |
|
338 rv = new QDeclarativePropertyCache(q, mo); |
|
339 propertyCache.insert(mo, rv); |
|
340 } |
|
341 return rv; |
|
342 } |
|
343 |
|
344 /*! |
|
345 Returns a QDeclarativePropertyCache for \a metaObject. |
|
346 |
|
347 As the cache is persisted for the life of the engine, \a metaObject must be |
|
348 a static "compile time" meta-object, or a meta-object that is otherwise known to |
|
349 exist for the lifetime of the QDeclarativeEngine. |
|
350 */ |
|
351 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(const QMetaObject *metaObject) |
|
352 { |
|
353 Q_Q(QDeclarativeEngine); |
|
354 Q_ASSERT(metaObject); |
|
355 |
|
356 QDeclarativePropertyCache *rv = propertyCache.value(metaObject); |
|
357 if (!rv) { |
|
358 rv = new QDeclarativePropertyCache(q, metaObject); |
|
359 propertyCache.insert(metaObject, rv); |
|
360 } |
|
361 |
|
362 return rv; |
|
363 } |
|
364 |
|
365 QT_END_NAMESPACE |
|
366 |
|
367 #endif // QDECLARATIVEENGINE_P_H |