| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QQMLXMLLISTMODEL_H |
| 5 | #define QQMLXMLLISTMODEL_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <private/qflatmap_p.h> |
| 19 | #include <private/qtqmlxmllistmodelglobal_p.h> |
| 20 | |
| 21 | #include <QtQml/qqmllist.h> |
| 22 | #include <QtQml/qqmlparserstatus.h> |
| 23 | |
| 24 | #include <QtQmlIntegration/qqmlintegration.h> |
| 25 | |
| 26 | #include <QtCore/qabstractitemmodel.h> |
| 27 | #include <QtCore/qbytearray.h> |
| 28 | #include <QtCore/qfuture.h> |
| 29 | #include <QtCore/qhash.h> |
| 30 | #include <QtCore/qstringlist.h> |
| 31 | #include <QtCore/qurl.h> |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | #if QT_CONFIG(qml_network) |
| 36 | class QNetworkReply; |
| 37 | #endif |
| 38 | |
| 39 | class QXmlStreamReader; |
| 40 | class QQmlContext; |
| 41 | struct QQmlXmlListModelQueryJob |
| 42 | { |
| 43 | int queryId; |
| 44 | QByteArray data; |
| 45 | QString query; |
| 46 | QStringList roleNames; |
| 47 | QStringList elementNames; |
| 48 | QStringList elementAttributes; |
| 49 | QList<void *> roleQueryErrorId; |
| 50 | }; |
| 51 | struct QQmlXmlListModelQueryResult |
| 52 | { |
| 53 | Q_GADGET |
| 54 | QML_ANONYMOUS |
| 55 | public: |
| 56 | int queryId; |
| 57 | QList<QFlatMap<int, QString>> data; |
| 58 | QList<QPair<void *, QString>> errors; |
| 59 | }; |
| 60 | |
| 61 | class Q_QMLXMLLISTMODEL_EXPORT QQmlXmlListModelRole : public QObject |
| 62 | { |
| 63 | Q_OBJECT |
| 64 | Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) |
| 65 | Q_PROPERTY(QString elementName READ elementName WRITE setElementName NOTIFY elementNameChanged) |
| 66 | Q_PROPERTY(QString attributeName READ attributeName WRITE setAttributeName NOTIFY |
| 67 | attributeNameChanged) |
| 68 | QML_NAMED_ELEMENT(XmlListModelRole) |
| 69 | |
| 70 | public: |
| 71 | QQmlXmlListModelRole() = default; |
| 72 | ~QQmlXmlListModelRole() = default; |
| 73 | |
| 74 | QString name() const; |
| 75 | void setName(const QString &name); |
| 76 | QString elementName() const; |
| 77 | void setElementName(const QString &name); |
| 78 | QString attributeName() const; |
| 79 | void setAttributeName(const QString &attributeName); |
| 80 | bool isValid() const; |
| 81 | |
| 82 | Q_SIGNALS: |
| 83 | void nameChanged(); |
| 84 | void elementNameChanged(); |
| 85 | void attributeNameChanged(); |
| 86 | |
| 87 | private: |
| 88 | QString m_name; |
| 89 | QString m_elementName; |
| 90 | QString m_attributeName; |
| 91 | }; |
| 92 | |
| 93 | class QQmlXmlListModelQueryExecutor; |
| 94 | |
| 95 | class Q_QMLXMLLISTMODEL_EXPORT QQmlXmlListModel : public QAbstractListModel, |
| 96 | public QQmlParserStatus |
| 97 | { |
| 98 | Q_OBJECT |
| 99 | Q_INTERFACES(QQmlParserStatus) |
| 100 | |
| 101 | Q_PROPERTY(Status status READ status NOTIFY statusChanged) |
| 102 | Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) |
| 103 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
| 104 | Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged) |
| 105 | Q_PROPERTY(QQmlListProperty<QQmlXmlListModelRole> roles READ roleObjects) |
| 106 | Q_PROPERTY(int count READ count NOTIFY countChanged) |
| 107 | QML_NAMED_ELEMENT(XmlListModel) |
| 108 | Q_CLASSINFO("DefaultProperty" , "roles" ) |
| 109 | |
| 110 | public: |
| 111 | QQmlXmlListModel(QObject *parent = nullptr); |
| 112 | ~QQmlXmlListModel(); |
| 113 | |
| 114 | QModelIndex index(int row, int column, const QModelIndex &parent) const override; |
| 115 | int rowCount(const QModelIndex &parent) const override; |
| 116 | QVariant data(const QModelIndex &index, int role) const override; |
| 117 | QHash<int, QByteArray> roleNames() const override; |
| 118 | |
| 119 | int count() const; |
| 120 | |
| 121 | QUrl source() const; |
| 122 | void setSource(const QUrl &); |
| 123 | |
| 124 | QString query() const; |
| 125 | void setQuery(const QString &); |
| 126 | |
| 127 | QQmlListProperty<QQmlXmlListModelRole> roleObjects(); |
| 128 | |
| 129 | void appendRole(QQmlXmlListModelRole *); |
| 130 | void clearRole(); |
| 131 | |
| 132 | enum Status { Null, Ready, Loading, Error }; |
| 133 | Q_ENUM(Status) |
| 134 | Status status() const; |
| 135 | qreal progress() const; |
| 136 | |
| 137 | Q_INVOKABLE QString errorString() const; |
| 138 | |
| 139 | void classBegin() override; |
| 140 | void componentComplete() override; |
| 141 | |
| 142 | Q_SIGNALS: |
| 143 | void statusChanged(QQmlXmlListModel::Status); |
| 144 | void progressChanged(qreal progress); |
| 145 | void countChanged(); |
| 146 | void sourceChanged(); |
| 147 | void queryChanged(); |
| 148 | |
| 149 | public Q_SLOTS: |
| 150 | void reload(); |
| 151 | |
| 152 | private Q_SLOTS: |
| 153 | #if QT_CONFIG(qml_network) |
| 154 | void requestFinished(); |
| 155 | #endif |
| 156 | void requestProgress(qint64, qint64); |
| 157 | void dataCleared(); |
| 158 | void queryCompleted(const QQmlXmlListModelQueryResult &); |
| 159 | void queryError(void *object, const QString &error); |
| 160 | |
| 161 | private: |
| 162 | Q_DISABLE_COPY(QQmlXmlListModel) |
| 163 | |
| 164 | void notifyQueryStarted(bool remoteSource); |
| 165 | |
| 166 | static void appendRole(QQmlListProperty<QQmlXmlListModelRole> *, QQmlXmlListModelRole *); |
| 167 | static void clearRole(QQmlListProperty<QQmlXmlListModelRole> *); |
| 168 | |
| 169 | void tryExecuteQuery(const QByteArray &data); |
| 170 | |
| 171 | QQmlXmlListModelQueryJob createJob(const QByteArray &data); |
| 172 | int nextQueryId(); |
| 173 | |
| 174 | #if QT_CONFIG(qml_network) |
| 175 | void deleteReply(); |
| 176 | |
| 177 | QNetworkReply *m_reply = nullptr; |
| 178 | #endif |
| 179 | |
| 180 | int m_size = 0; |
| 181 | QUrl m_source; |
| 182 | QString m_query; |
| 183 | QStringList m_roleNames; |
| 184 | QList<int> m_roles; |
| 185 | QList<QQmlXmlListModelRole *> m_roleObjects; |
| 186 | QList<QFlatMap<int, QString>> m_data; |
| 187 | bool m_isComponentComplete = true; |
| 188 | Status m_status = QQmlXmlListModel::Null; |
| 189 | QString m_errorString; |
| 190 | qreal m_progress = 0; |
| 191 | int m_queryId = -1; |
| 192 | int m_nextQueryIdGenerator = -1; |
| 193 | int m_highestRole = Qt::UserRole; |
| 194 | using ResultFutureWatcher = QFutureWatcher<QQmlXmlListModelQueryResult>; |
| 195 | QFlatMap<int, ResultFutureWatcher *> m_watchers; |
| 196 | }; |
| 197 | |
| 198 | class QQmlXmlListModelQueryRunnable : public QRunnable |
| 199 | { |
| 200 | public: |
| 201 | explicit QQmlXmlListModelQueryRunnable(QQmlXmlListModelQueryJob &&job); |
| 202 | void run() override; |
| 203 | |
| 204 | QFuture<QQmlXmlListModelQueryResult> future() const; |
| 205 | |
| 206 | private: |
| 207 | void doQueryJob(QQmlXmlListModelQueryResult *currentResult); |
| 208 | void processElement(QQmlXmlListModelQueryResult *currentResult, const QString &element, |
| 209 | QXmlStreamReader &reader); |
| 210 | void readSubTree(const QString &prefix, QXmlStreamReader &reader, |
| 211 | QFlatMap<int, QString> &results, QList<QPair<void *, QString>> *errors); |
| 212 | |
| 213 | QQmlXmlListModelQueryJob m_job; |
| 214 | QPromise<QQmlXmlListModelQueryResult> m_promise; |
| 215 | }; |
| 216 | |
| 217 | QT_END_NAMESPACE |
| 218 | |
| 219 | #endif // QQMLXMLLISTMODEL_H |
| 220 | |