Improve QJSValueIterator implementation.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativetypeloader_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 QDECLARATIVETYPELOADER_P_H
43 #define QDECLARATIVETYPELOADER_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 <QtCore/qobject.h>
57 #include <QtNetwork/qnetworkreply.h>
58 #include <QtDeclarative/qjsvalue.h>
59 #include <QtDeclarative/qdeclarativeerror.h>
60 #include <QtDeclarative/qdeclarativeengine.h>
61 #include <private/qdeclarativecleanup_p.h>
62 #include <private/qdeclarativescriptparser_p.h>
63 #include <private/qdeclarativedirparser_p.h>
64 #include <private/qdeclarativeimport_p.h>
65
66 #include <private/qv8_p.h>
67
68 QT_BEGIN_NAMESPACE
69
70 class QDeclarativeScriptData;
71 class QDeclarativeScriptBlob;
72 class QDeclarativeQmldirData;
73 class QDeclarativeTypeLoader;
74 class QDeclarativeCompiledData;
75 class QDeclarativeComponentPrivate;
76 class QDeclarativeTypeData;
77 class QDeclarativeDataLoader;
78
79 class Q_AUTOTEST_EXPORT QDeclarativeDataBlob : public QDeclarativeRefCount
80 {
81 public:
82     enum Status {
83         Null,                    // Prior to QDeclarativeDataLoader::load()
84         Loading,                 // Prior to data being received and dataReceived() being called
85         WaitingForDependencies,  // While there are outstanding addDependency()s
86         Complete,                // Finished
87         Error                    // Error
88     };
89
90     enum Type {
91         QmlFile,
92         JavaScriptFile,
93         QmldirFile
94     };
95
96     QDeclarativeDataBlob(const QUrl &, Type);
97     virtual ~QDeclarativeDataBlob();
98
99     Type type() const;
100
101     Status status() const;
102     bool isNull() const;
103     bool isLoading() const;
104     bool isWaiting() const;
105     bool isComplete() const;
106     bool isError() const;
107     bool isCompleteOrError() const;
108
109     qreal progress() const;
110
111     QUrl url() const;
112     QUrl finalUrl() const;
113
114     QList<QDeclarativeError> errors() const;
115
116     void setError(const QDeclarativeError &);
117     void setError(const QList<QDeclarativeError> &errors);
118
119     void addDependency(QDeclarativeDataBlob *);
120
121 protected:
122     virtual void dataReceived(const QByteArray &) = 0;
123
124     virtual void done();
125     virtual void networkError(QNetworkReply::NetworkError);
126
127     virtual void dependencyError(QDeclarativeDataBlob *);
128     virtual void dependencyComplete(QDeclarativeDataBlob *);
129     virtual void allDependenciesDone();
130     
131     virtual void downloadProgressChanged(qreal);
132
133 private:
134     friend class QDeclarativeDataLoader;
135     void tryDone();
136     void cancelAllWaitingFor();
137     void notifyAllWaitingOnMe();
138     void notifyComplete(QDeclarativeDataBlob *);
139
140     Type m_type;
141     Status m_status;
142     qreal m_progress;
143
144     QUrl m_url;
145     QUrl m_finalUrl;
146
147     // List of QDeclarativeDataBlob's that are waiting for me to complete.
148     QList<QDeclarativeDataBlob *> m_waitingOnMe;
149
150     // List of QDeclarativeDataBlob's that I am waiting for to complete.
151     QList<QDeclarativeDataBlob *> m_waitingFor;
152
153     // Manager that is currently fetching data for me
154     QDeclarativeDataLoader *m_manager;
155     int m_redirectCount:30;
156     bool m_inCallback:1;
157     bool m_isDone:1;
158
159     QList<QDeclarativeError> m_errors;
160 };
161
162 class Q_AUTOTEST_EXPORT QDeclarativeDataLoader : public QObject
163 {
164     Q_OBJECT
165 public:
166     QDeclarativeDataLoader(QDeclarativeEngine *);
167     ~QDeclarativeDataLoader();
168
169     void load(QDeclarativeDataBlob *);
170     void loadWithStaticData(QDeclarativeDataBlob *, const QByteArray &);
171
172     QDeclarativeEngine *engine() const;
173
174 private slots:
175     void networkReplyFinished();
176     void networkReplyProgress(qint64,qint64);
177
178 private:
179     void setData(QDeclarativeDataBlob *, const QByteArray &);
180
181     QDeclarativeEngine *m_engine;
182     typedef QHash<QNetworkReply *, QDeclarativeDataBlob *> NetworkReplies;
183     NetworkReplies m_networkReplies;
184 };
185
186 class Q_AUTOTEST_EXPORT QDeclarativeTypeLoader : public QDeclarativeDataLoader
187 {
188     Q_OBJECT
189 public:
190     QDeclarativeTypeLoader(QDeclarativeEngine *);
191     ~QDeclarativeTypeLoader();
192
193     enum Option {
194         None,
195         PreserveParser
196     };
197     Q_DECLARE_FLAGS(Options, Option)
198
199     QDeclarativeTypeData *get(const QUrl &url);
200     QDeclarativeTypeData *get(const QByteArray &, const QUrl &url, Options = None);
201     void clearCache();
202
203     QDeclarativeScriptBlob *getScript(const QUrl &);
204     QDeclarativeQmldirData *getQmldir(const QUrl &);
205 private:
206     typedef QHash<QUrl, QDeclarativeTypeData *> TypeCache;
207     typedef QHash<QUrl, QDeclarativeScriptBlob *> ScriptCache;
208     typedef QHash<QUrl, QDeclarativeQmldirData *> QmldirCache;
209
210     TypeCache m_typeCache;
211     ScriptCache m_scriptCache;
212     QmldirCache m_qmldirCache;
213 };
214
215 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeTypeLoader::Options)
216
217 class Q_AUTOTEST_EXPORT QDeclarativeTypeData : public QDeclarativeDataBlob
218 {
219 public:
220     struct TypeReference
221     {
222         TypeReference() : type(0), majorVersion(0), minorVersion(0), typeData(0) {}
223
224         QDeclarativeParser::Location location;
225         QDeclarativeType *type;
226         int majorVersion;
227         int minorVersion;
228         QDeclarativeTypeData *typeData;
229     };
230
231     struct ScriptReference
232     {
233         ScriptReference() : script(0) {}
234
235         QDeclarativeParser::Location location;
236         QString qualifier;
237         QDeclarativeScriptBlob *script;
238     };
239
240     QDeclarativeTypeData(const QUrl &, QDeclarativeTypeLoader::Options, QDeclarativeTypeLoader *);
241     ~QDeclarativeTypeData();
242
243     QDeclarativeTypeLoader *typeLoader() const;
244
245     const QDeclarativeImports &imports() const;
246     const QDeclarativeScriptParser &parser() const;
247
248     const QList<TypeReference> &resolvedTypes() const;
249     const QList<ScriptReference> &resolvedScripts() const;
250
251     QDeclarativeCompiledData *compiledData() const;
252
253     // Used by QDeclarativeComponent to get notifications
254     struct TypeDataCallback {
255         ~TypeDataCallback() {}
256         virtual void typeDataProgress(QDeclarativeTypeData *, qreal) {}
257         virtual void typeDataReady(QDeclarativeTypeData *) {}
258     };
259     void registerCallback(TypeDataCallback *);
260     void unregisterCallback(TypeDataCallback *);
261
262 protected:
263     virtual void done();
264     virtual void dataReceived(const QByteArray &);
265     virtual void allDependenciesDone();
266     virtual void downloadProgressChanged(qreal);
267
268 private:
269     void resolveTypes();
270     void compile();
271
272     QDeclarativeTypeLoader::Options m_options;
273
274     QDeclarativeQmldirData *qmldirForUrl(const QUrl &);
275
276     QDeclarativeScriptParser scriptParser;
277     QDeclarativeImports m_imports;
278
279     QList<ScriptReference> m_scripts;
280     QList<QDeclarativeQmldirData *> m_qmldirs;
281
282     QList<TypeReference> m_types;
283     bool m_typesResolved:1;
284
285     QDeclarativeCompiledData *m_compiledData;
286
287     QList<TypeDataCallback *> m_callbacks;
288    
289     QDeclarativeTypeLoader *m_typeLoader;
290 };
291
292 class Q_AUTOTEST_EXPORT QDeclarativeScriptData : public QDeclarativeRefCount, public QDeclarativeCleanup
293 {
294 public:
295     QDeclarativeScriptData(QDeclarativeEngine *);
296     ~QDeclarativeScriptData();
297
298     QUrl url;
299     QDeclarativeTypeNameCache *importCache;
300     QList<QDeclarativeScriptBlob *> scripts;
301     QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas;
302
303 protected:
304     virtual void clear(); // From QDeclarativeCleanup
305
306 private:
307     friend class QDeclarativeVME;
308     friend class QDeclarativeScriptBlob;
309
310     bool m_loaded;
311     v8::Persistent<v8::Script> m_program;
312     v8::Persistent<v8::Object> m_value;
313 //    QScriptProgram m_program;
314 //    QScriptValue m_value;
315 };
316
317 class Q_AUTOTEST_EXPORT QDeclarativeScriptBlob : public QDeclarativeDataBlob
318 {
319 public:
320     QDeclarativeScriptBlob(const QUrl &, QDeclarativeTypeLoader *);
321     ~QDeclarativeScriptBlob();
322
323     struct ScriptReference
324     {
325         ScriptReference() : script(0) {}
326
327         QDeclarativeParser::Location location;
328         QString qualifier;
329         QDeclarativeScriptBlob *script;
330     };
331
332     QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas() const;
333     QString scriptSource() const;
334
335     QDeclarativeTypeLoader *typeLoader() const;
336     const QDeclarativeImports &imports() const;
337
338     QDeclarativeScriptData *scriptData() const;
339
340 protected:
341     virtual void dataReceived(const QByteArray &);
342     virtual void done();
343
344 private:
345     QDeclarativeParser::Object::ScriptBlock::Pragmas m_pragmas;
346     QString m_source;
347
348     QDeclarativeImports m_imports;
349     QList<ScriptReference> m_scripts;
350     QDeclarativeScriptData *m_scriptData;
351
352     QDeclarativeTypeLoader *m_typeLoader;
353 };
354
355 class Q_AUTOTEST_EXPORT QDeclarativeQmldirData : public QDeclarativeDataBlob
356 {
357 public:
358     QDeclarativeQmldirData(const QUrl &);
359
360     const QDeclarativeDirComponents &dirComponents() const;
361
362 protected:
363     virtual void dataReceived(const QByteArray &);
364
365 private:
366     QDeclarativeDirComponents m_components;
367
368 };
369
370 QT_END_NAMESPACE
371
372 #endif // QDECLARATIVETYPELOADER_P_H