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