Extract all QtQuick 1 elements into a separate library/plugin.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeengine_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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/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/qdeclarativemetatype_p.h"
70 #include "private/qdeclarativedirparser_p.h"
71 #include "private/qintrusivelist_p.h"
72
73 #include <QtCore/qstring.h>
74 #include <QtCore/qlist.h>
75 #include <QtCore/qpair.h>
76 #include <QtCore/qstack.h>
77 #include <QtCore/qmutex.h>
78
79 #include <private/qobject_p.h>
80
81 #include <private/qv8engine_p.h>
82
83 QT_BEGIN_NAMESPACE
84
85 class QDeclarativeContext;
86 class QDeclarativeEngine;
87 class QDeclarativeContextPrivate;
88 class QDeclarativeExpression;
89 class QDeclarativeImportDatabase;
90 class ScarceResourceData;
91 class QNetworkReply;
92 class QNetworkAccessManager;
93 class QDeclarativeNetworkAccessManagerFactory;
94 class QDeclarativeAbstractBinding;
95 class QDeclarativeTypeNameCache;
96 class QDeclarativeComponentAttached;
97 class QDeclarativeCleanup;
98 class QDeclarativeDelayedError;
99 class QDeclarativeWorkerScriptEngine;
100 class QDir;
101 class QSGTexture;
102 class QSGContext;
103
104 class Q_DECLARATIVE_EXPORT QDeclarativeEnginePrivate : public QObjectPrivate
105 {
106     Q_DECLARE_PUBLIC(QDeclarativeEngine)
107 public:
108     QDeclarativeEnginePrivate(QDeclarativeEngine *);
109     ~QDeclarativeEnginePrivate();
110
111     void init();
112
113     struct CapturedProperty {
114         CapturedProperty(QObject *o, int c, int n)
115             : object(o), coreIndex(c), notifier(0), notifyIndex(n) {}
116         CapturedProperty(QDeclarativeNotifier *n)
117             : object(0), coreIndex(-1), notifier(n), notifyIndex(-1) {}
118
119         QObject *object;
120         int coreIndex;
121         QDeclarativeNotifier *notifier;
122         int notifyIndex;
123     };
124     bool captureProperties;
125     QPODVector<CapturedProperty> capturedProperties;
126
127     QDeclarativeContext *rootContext;
128     bool isDebugging;
129
130     bool outputWarningsToStdErr;
131
132     QDeclarativeContextData *sharedContext;
133     QObject *sharedScope;
134
135     // Registered cleanup handlers
136     QDeclarativeCleanup *cleanup;
137
138     // Bindings that have had errors during startup
139     QDeclarativeDelayedError *erroredBindings;
140     int inProgressCreations;
141
142     // V8 Engine
143     QV8Engine v8engine;
144
145     QDeclarativeWorkerScriptEngine *getWorkerScriptEngine();
146     QDeclarativeWorkerScriptEngine *workerScriptEngine;
147
148     QUrl baseUrl;
149
150     template<class T>
151     struct SimpleList {
152         SimpleList()
153             : count(0), values(0) {}
154         SimpleList(int r)
155             : count(0), values(new T*[r]) {}
156
157         int count;
158         T **values;
159
160         void append(T *v) {
161             values[count++] = v;
162         }
163
164         T *at(int idx) const {
165             return values[idx];
166         }
167
168         void clear() {
169             delete [] values;
170         }
171     };
172
173     static void clear(SimpleList<QDeclarativeAbstractBinding> &);
174     static void clear(SimpleList<QDeclarativeParserStatus> &);
175
176     QList<SimpleList<QDeclarativeAbstractBinding> > bindValues;
177     QList<SimpleList<QDeclarativeParserStatus> > parserStatus;
178     QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus;
179     QDeclarativeComponentAttached *componentAttached;
180
181     void registerFinalizedParserStatusObject(QObject *obj, int index) {
182         finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index));
183     }
184
185     bool inBeginCreate;
186
187     QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const;
188     QNetworkAccessManager *getNetworkAccessManager() const;
189     mutable QNetworkAccessManager *networkAccessManager;
190     mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory;
191
192     QHash<QString,QSharedPointer<QDeclarativeImageProvider> > imageProviders;
193     QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url);
194     QSGTexture *getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
195     QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
196     QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
197
198     // Scarce resources are "exceptionally high cost" QVariant types where allowing the
199     // normal JavaScript GC to clean them up is likely to lead to out-of-memory or other
200     // out-of-resource situations.  When such a resource is passed into JavaScript we
201     // add it to the scarceResources list and it is destroyed when we return from the
202     // JavaScript execution that created it.  The user can prevent this behavior by
203     // calling preserve() on the object which removes it from this scarceResource list.
204     class ScarceResourceData {
205     public:
206         ScarceResourceData(const QVariant &data) : data(data) {}
207         QVariant data;
208         QIntrusiveListNode node;
209     };
210     QIntrusiveList<ScarceResourceData, &ScarceResourceData::node> scarceResources;
211     int scarceResourcesRefCount;
212     void referenceScarceResources();
213     void dereferenceScarceResources();
214
215     mutable QMutex mutex;
216
217     QDeclarativeTypeLoader typeLoader;
218     QDeclarativeImportDatabase importDatabase;
219
220     QString offlineStoragePath;
221
222     mutable quint32 uniqueId;
223     quint32 getUniqueId() const {
224         return uniqueId++;
225     }
226
227     QDeclarativeValueTypeFactory valueTypes;
228
229     QHash<QDeclarativeMetaType::ModuleApi, QDeclarativeMetaType::ModuleApiInstance *> moduleApiInstances;
230
231     QHash<const QMetaObject *, QDeclarativePropertyCache *> propertyCache;
232     QHash<QPair<QDeclarativeType *, int>, QDeclarativePropertyCache *> typePropertyCache;
233     inline QDeclarativePropertyCache *cache(QObject *obj);
234     inline QDeclarativePropertyCache *cache(const QMetaObject *);
235     inline QDeclarativePropertyCache *cache(QDeclarativeType *, int, QDeclarativeError &error);
236     QDeclarativePropertyCache *createCache(const QMetaObject *);
237     QDeclarativePropertyCache *createCache(QDeclarativeType *, int, QDeclarativeError &error);
238
239     void registerCompositeType(QDeclarativeCompiledData *);
240
241     bool isQObject(int);
242     QObject *toQObject(const QVariant &, bool *ok = 0) const;
243     QDeclarativeMetaType::TypeCategory typeCategory(int) const;
244     bool isList(int) const;
245     int listType(int) const;
246     const QMetaObject *rawMetaObjectForType(int) const;
247     const QMetaObject *metaObjectForType(int) const;
248     QHash<int, int> m_qmlLists;
249     QHash<int, QDeclarativeCompiledData *> m_compositeTypes;
250
251     void sendQuit();
252     void warning(const QDeclarativeError &);
253     void warning(const QList<QDeclarativeError> &);
254     static void warning(QDeclarativeEngine *, const QDeclarativeError &);
255     static void warning(QDeclarativeEngine *, const QList<QDeclarativeError> &);
256     static void warning(QDeclarativeEnginePrivate *, const QDeclarativeError &);
257     static void warning(QDeclarativeEnginePrivate *, const QList<QDeclarativeError> &);
258
259     static QV8Engine *getV8Engine(QDeclarativeEngine *e) { return &e->d_func()->v8engine; }
260     static QDeclarativeEnginePrivate *get(QDeclarativeEngine *e) { return e->d_func(); }
261     static QDeclarativeEnginePrivate *get(QDeclarativeContext *c) { return (c && c->engine()) ? QDeclarativeEnginePrivate::get(c->engine()) : 0; }
262     static QDeclarativeEnginePrivate *get(QDeclarativeContextData *c) { return (c && c->engine) ? QDeclarativeEnginePrivate::get(c->engine) : 0; }
263     static QDeclarativeEngine *get(QDeclarativeEnginePrivate *p) { return p->q_func(); }
264
265     static QString urlToLocalFileOrQrc(const QUrl& url);
266
267     static void registerBaseTypes(const char *uri, int versionMajor, int versionMinor);
268     static void defineModule();
269
270     static bool qml_debugging_enabled;
271
272     QSGContext *sgContext;
273 };
274
275 /*!
276 Returns a QDeclarativePropertyCache for \a obj if one is available.
277
278 If \a obj is null, being deleted or contains a dynamic meta object 0
279 is returned.
280
281 The returned cache is not referenced, so if it is to be stored, call addref().
282 */
283 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QObject *obj)
284 {
285     if (!obj || QObjectPrivate::get(obj)->metaObject || QObjectPrivate::get(obj)->wasDeleted)
286         return 0;
287
288     const QMetaObject *mo = obj->metaObject();
289     QDeclarativePropertyCache *rv = propertyCache.value(mo);
290     if (!rv) rv = createCache(mo);
291     return rv;
292 }
293
294 /*!
295 Returns a QDeclarativePropertyCache for \a metaObject.
296
297 As the cache is persisted for the life of the engine, \a metaObject must be
298 a static "compile time" meta-object, or a meta-object that is otherwise known to
299 exist for the lifetime of the QDeclarativeEngine.
300
301 The returned cache is not referenced, so if it is to be stored, call addref().
302 */
303 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(const QMetaObject *metaObject)
304 {
305     Q_ASSERT(metaObject);
306
307     QDeclarativePropertyCache *rv = propertyCache.value(metaObject);
308     if (!rv) rv = createCache(metaObject);
309     return rv;
310 }
311
312 /*!
313 Returns a QDeclarativePropertyCache for \a type with \a minorVersion.
314
315 The returned cache is not referenced, so if it is to be stored, call addref().
316 */
317 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QDeclarativeType *type, int minorVersion, QDeclarativeError &error)
318 {
319     Q_ASSERT(type);
320
321     if (minorVersion == -1 || !type->containsRevisionedAttributes())
322         return cache(type->metaObject());
323
324     QDeclarativePropertyCache *rv = typePropertyCache.value(qMakePair(type, minorVersion));
325     if (!rv) rv = createCache(type, minorVersion, error);
326     return rv;
327 }
328
329 QT_END_NAMESPACE
330
331 #endif // QDECLARATIVEENGINE_P_H