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