File indexing completed on 2024-12-08 10:16:06

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef WIKIDATAQUERY_H
0008 #define WIKIDATAQUERY_H
0009 
0010 #include "entities.h"
0011 
0012 #include <QObject>
0013 
0014 #include <vector>
0015 
0016 class QNetworkReply;
0017 class QNetworkRequest;
0018 class QUrlQuery;
0019 
0020 /** Base class for Wikidata queries. */
0021 class WikidataQuery : public QObject
0022 {
0023     Q_OBJECT
0024 public:
0025     explicit WikidataQuery(QObject *parent = nullptr);
0026     ~WikidataQuery() override;
0027 
0028     enum Error {
0029         NoError,
0030         NetworkError
0031     };
0032     Error error();
0033 
0034 Q_SIGNALS:
0035     void finished();
0036 
0037 protected:
0038     QUrlQuery commonUrlQuery() const;
0039 
0040     friend class WikidataQueryManager;
0041     virtual QNetworkRequest nextRequest() = 0;
0042     /** Returns @true if this query is complete, @false if another request is needed. */
0043     virtual bool processReply(QNetworkReply *reply) = 0;
0044 
0045     Error m_error = NoError;
0046 };
0047 
0048 /** Wikidata multi-entity retrieval query. */
0049 class WikidataEntitiesQuery : public WikidataQuery
0050 {
0051     Q_OBJECT
0052 public:
0053     explicit WikidataEntitiesQuery(QObject *parent = nullptr);
0054     ~WikidataEntitiesQuery() override;
0055 
0056     void setItems(std::vector<Wikidata::Q> &&items);
0057     std::vector<Wikidata::Item>&& takeResult();
0058 
0059 Q_SIGNALS:
0060     void partialResult(WikidataEntitiesQuery *query);
0061 
0062 private:
0063     QNetworkRequest nextRequest() override;
0064     bool processReply(QNetworkReply *reply) override;
0065 
0066     std::vector<Wikidata::Q> m_items;
0067     std::size_t m_nextBatch = 0;
0068     std::vector<Wikidata::Item> m_result;
0069 };
0070 
0071 /** Wikidata image metadata query. */
0072 class WikidataImageMetadataQuery : public WikidataQuery
0073 {
0074     Q_OBJECT
0075 public:
0076     explicit WikidataImageMetadataQuery(QObject *parent = nullptr);
0077     ~WikidataImageMetadataQuery() override;
0078 
0079     void setImages(std::vector<QString> &&images);
0080     std::vector<Wikidata::Image>&& takeResult();
0081 
0082 Q_SIGNALS:
0083     void partialResult(WikidataImageMetadataQuery *query);
0084 
0085 private:
0086     QNetworkRequest nextRequest() override;
0087     bool processReply(QNetworkReply *reply) override;
0088 
0089     std::vector<QString> m_images;
0090     std::size_t m_nextBatch = 0;
0091     std::vector<Wikidata::Image> m_result;
0092 };
0093 
0094 #endif // WIKIDATAQUERY_H