Optimizations to imports.
[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 #include "private/qhashedstring_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
207     QString absoluteFilePath(const QString &path);
208     bool directoryExists(const QString &path);
209     const QDeclarativeDirParser *qmlDirParser(const QString &absoluteFilePath);
210
211 private:
212     typedef QHash<QUrl, QDeclarativeTypeData *> TypeCache;
213     typedef QHash<QUrl, QDeclarativeScriptBlob *> ScriptCache;
214     typedef QHash<QUrl, QDeclarativeQmldirData *> QmldirCache;
215     typedef QStringHash<bool> StringSet;
216     typedef QStringHash<StringSet*> ImportDirCache;
217     typedef QStringHash<QDeclarativeDirParser*> ImportQmlDirCache;
218
219     TypeCache m_typeCache;
220     ScriptCache m_scriptCache;
221     QmldirCache m_qmldirCache;
222     ImportDirCache m_importDirCache;
223     ImportQmlDirCache m_importQmlDirCache;
224 };
225
226 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeTypeLoader::Options)
227
228 class Q_AUTOTEST_EXPORT QDeclarativeTypeData : public QDeclarativeDataBlob
229 {
230 public:
231     struct TypeReference
232     {
233         TypeReference() : type(0), majorVersion(0), minorVersion(0), typeData(0) {}
234
235         QDeclarativeParser::Location location;
236         QDeclarativeType *type;
237         int majorVersion;
238         int minorVersion;
239         QDeclarativeTypeData *typeData;
240     };
241
242     struct ScriptReference
243     {
244         ScriptReference() : script(0) {}
245
246         QDeclarativeParser::Location location;
247         QString qualifier;
248         QDeclarativeScriptBlob *script;
249     };
250
251     QDeclarativeTypeData(const QUrl &, QDeclarativeTypeLoader::Options, QDeclarativeTypeLoader *);
252     ~QDeclarativeTypeData();
253
254     QDeclarativeTypeLoader *typeLoader() const;
255
256     const QDeclarativeImports &imports() const;
257     const QDeclarativeScriptParser &parser() const;
258
259     const QList<TypeReference> &resolvedTypes() const;
260     const QList<ScriptReference> &resolvedScripts() const;
261
262     QDeclarativeCompiledData *compiledData() const;
263
264     // Used by QDeclarativeComponent to get notifications
265     struct TypeDataCallback {
266         ~TypeDataCallback() {}
267         virtual void typeDataProgress(QDeclarativeTypeData *, qreal) {}
268         virtual void typeDataReady(QDeclarativeTypeData *) {}
269     };
270     void registerCallback(TypeDataCallback *);
271     void unregisterCallback(TypeDataCallback *);
272
273 protected:
274     virtual void done();
275     virtual void dataReceived(const QByteArray &);
276     virtual void allDependenciesDone();
277     virtual void downloadProgressChanged(qreal);
278
279 private:
280     void resolveTypes();
281     void compile();
282
283     QDeclarativeTypeLoader::Options m_options;
284
285     QDeclarativeQmldirData *qmldirForUrl(const QUrl &);
286
287     QDeclarativeScriptParser scriptParser;
288     QDeclarativeImports m_imports;
289
290     QList<ScriptReference> m_scripts;
291     QList<QDeclarativeQmldirData *> m_qmldirs;
292
293     QList<TypeReference> m_types;
294     bool m_typesResolved:1;
295
296     QDeclarativeCompiledData *m_compiledData;
297
298     QList<TypeDataCallback *> m_callbacks;
299    
300     QDeclarativeTypeLoader *m_typeLoader;
301 };
302
303 class Q_AUTOTEST_EXPORT QDeclarativeScriptData : public QDeclarativeRefCount, public QDeclarativeCleanup
304 {
305 public:
306     QDeclarativeScriptData(QDeclarativeEngine *);
307     ~QDeclarativeScriptData();
308
309     QUrl url;
310     QDeclarativeTypeNameCache *importCache;
311     QList<QDeclarativeScriptBlob *> scripts;
312     QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas;
313
314 protected:
315     virtual void clear(); // From QDeclarativeCleanup
316
317 private:
318     friend class QDeclarativeVME;
319     friend class QDeclarativeScriptBlob;
320
321     bool m_loaded;
322     v8::Persistent<v8::Script> m_program;
323     v8::Persistent<v8::Object> m_value;
324 //    QScriptProgram m_program;
325 //    QScriptValue m_value;
326 };
327
328 class Q_AUTOTEST_EXPORT QDeclarativeScriptBlob : public QDeclarativeDataBlob
329 {
330 public:
331     QDeclarativeScriptBlob(const QUrl &, QDeclarativeTypeLoader *);
332     ~QDeclarativeScriptBlob();
333
334     struct ScriptReference
335     {
336         ScriptReference() : script(0) {}
337
338         QDeclarativeParser::Location location;
339         QString qualifier;
340         QDeclarativeScriptBlob *script;
341     };
342
343     QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas() const;
344     QString scriptSource() const;
345
346     QDeclarativeTypeLoader *typeLoader() const;
347     const QDeclarativeImports &imports() const;
348
349     QDeclarativeScriptData *scriptData() const;
350
351 protected:
352     virtual void dataReceived(const QByteArray &);
353     virtual void done();
354
355 private:
356     QDeclarativeParser::Object::ScriptBlock::Pragmas m_pragmas;
357     QString m_source;
358
359     QDeclarativeImports m_imports;
360     QList<ScriptReference> m_scripts;
361     QDeclarativeScriptData *m_scriptData;
362
363     QDeclarativeTypeLoader *m_typeLoader;
364 };
365
366 class Q_AUTOTEST_EXPORT QDeclarativeQmldirData : public QDeclarativeDataBlob
367 {
368 public:
369     QDeclarativeQmldirData(const QUrl &);
370
371     const QDeclarativeDirComponents &dirComponents() const;
372
373 protected:
374     virtual void dataReceived(const QByteArray &);
375
376 private:
377     QDeclarativeDirComponents m_components;
378
379 };
380
381 QT_END_NAMESPACE
382
383 #endif // QDECLARATIVETYPELOADER_P_H