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