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