c8417162062d5ed90feb23eaaaa4771d8fa3c335
[profile/ivi/qtdeclarative.git] / src / qtquick1 / util / qdeclarativexmllistmodel_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 QDECLARATIVEXMLLISTMODEL_H
43 #define QDECLARATIVEXMLLISTMODEL_H
44
45 #include <QtDeclarative/qdeclarative.h>
46 #include <QtDeclarative/qdeclarativeinfo.h>
47
48 #include <QtCore/qurl.h>
49 #include <QtCore/qstringlist.h>
50
51 #include <QtDeclarative/private/qlistmodelinterface_p.h>
52 #include <private/qv8engine_p.h>
53
54 QT_BEGIN_HEADER
55
56 QT_BEGIN_NAMESPACE
57
58
59 class QDeclarativeContext;
60 class QDeclarative1XmlListModelRole;
61 class QDeclarative1XmlListModelPrivate;
62
63 struct QDeclarative1XmlQueryResult {
64     int queryId;
65     int size;
66     QList<QList<QVariant> > data;
67     QList<QPair<int, int> > inserted;
68     QList<QPair<int, int> > removed;
69     QStringList keyRoleResultsCache;
70 };
71
72 class Q_AUTOTEST_EXPORT QDeclarative1XmlListModel : public QListModelInterface, public QDeclarativeParserStatus
73 {
74     Q_OBJECT
75     Q_INTERFACES(QDeclarativeParserStatus)
76     Q_ENUMS(Status)
77
78     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
79     Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
80     Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
81     Q_PROPERTY(QString xml READ xml WRITE setXml NOTIFY xmlChanged)
82     Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
83     Q_PROPERTY(QString namespaceDeclarations READ namespaceDeclarations WRITE setNamespaceDeclarations NOTIFY namespaceDeclarationsChanged)
84     Q_PROPERTY(QDeclarativeListProperty<QDeclarative1XmlListModelRole> roles READ roleObjects)
85     Q_PROPERTY(int count READ count NOTIFY countChanged)
86     Q_CLASSINFO("DefaultProperty", "roles")
87
88 public:
89     QDeclarative1XmlListModel(QObject *parent = 0);
90     ~QDeclarative1XmlListModel();
91
92     virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const;
93     virtual QVariant data(int index, int role) const;
94     virtual int count() const;
95     virtual QList<int> roles() const;
96     virtual QString toString(int role) const;
97
98     QDeclarativeListProperty<QDeclarative1XmlListModelRole> roleObjects();
99
100     QUrl source() const;
101     void setSource(const QUrl&);
102
103     QString xml() const;
104     void setXml(const QString&);
105
106     QString query() const;
107     void setQuery(const QString&);
108
109     QString namespaceDeclarations() const;
110     void setNamespaceDeclarations(const QString&);
111
112     Q_INVOKABLE QDeclarativeV8Handle get(int index) const;
113
114     enum Status { Null, Ready, Loading, Error };
115     Status status() const;
116     qreal progress() const;
117
118     Q_INVOKABLE QString errorString() const;
119
120     virtual void classBegin();
121     virtual void componentComplete();
122
123 Q_SIGNALS:
124     void statusChanged(QDeclarative1XmlListModel::Status);
125     void progressChanged(qreal progress);
126     void countChanged();
127     void sourceChanged();
128     void xmlChanged();
129     void queryChanged();
130     void namespaceDeclarationsChanged();
131
132 public Q_SLOTS:
133     // ### need to use/expose Expiry to guess when to call this?
134     // ### property to auto-call this on reasonable Expiry?
135     // ### LastModified/Age also useful to guess.
136     // ### Probably also applies to other network-requesting types.
137     void reload();
138
139 private Q_SLOTS:
140     void requestFinished();
141     void requestProgress(qint64,qint64);
142     void dataCleared();
143     void queryCompleted(const QDeclarative1XmlQueryResult &);
144     void queryError(void* object, const QString& error);
145
146 private:
147     Q_DECLARE_PRIVATE(QDeclarative1XmlListModel)
148     Q_DISABLE_COPY(QDeclarative1XmlListModel)
149 };
150
151 class Q_AUTOTEST_EXPORT QDeclarative1XmlListModelRole : public QObject
152 {
153     Q_OBJECT
154     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
155     Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
156     Q_PROPERTY(bool isKey READ isKey WRITE setIsKey NOTIFY isKeyChanged)
157 public:
158     QDeclarative1XmlListModelRole() : m_isKey(false) {}
159     ~QDeclarative1XmlListModelRole() {}
160
161     QString name() const { return m_name; }
162     void setName(const QString &name) {
163         if (name == m_name)
164             return;
165         m_name = name;
166         emit nameChanged();
167     }
168
169     QString query() const { return m_query; }
170     void setQuery(const QString &query)
171     {
172         if (query.startsWith(QLatin1Char('/'))) {
173             qmlInfo(this) << tr("An XmlRole query must not start with '/'");
174             return;
175         }
176         if (m_query == query)
177             return;
178         m_query = query;
179         emit queryChanged();
180     }
181
182     bool isKey() const { return m_isKey; }
183     void setIsKey(bool b) {
184         if (m_isKey == b)
185             return;
186         m_isKey = b;
187         emit isKeyChanged();
188     }
189
190     bool isValid() {
191         return !m_name.isEmpty() && !m_query.isEmpty();
192     }
193
194 Q_SIGNALS:
195     void nameChanged();
196     void queryChanged();
197     void isKeyChanged();
198
199 private:
200     QString m_name;
201     QString m_query;
202     bool m_isKey;
203 };
204
205 QT_END_NAMESPACE
206
207 QML_DECLARE_TYPE(QDeclarative1XmlListModel)
208 QML_DECLARE_TYPE(QDeclarative1XmlListModelRole)
209
210 QT_END_HEADER
211
212 #endif // QDECLARATIVEXMLLISTMODEL_H