File indexing completed on 2025-01-05 03:53:36

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2011-03-22
0007  * Description : a Iface C++ interface
0008  *
0009  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2011      by Ludovic Delfau <ludovicdelfau at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "mediawiki_queryimages.h"
0017 
0018 // Qt includes
0019 
0020 #include <QTimer>
0021 #include <QUrl>
0022 #include <QUrlQuery>
0023 #include <QXmlStreamReader>
0024 #include <QNetworkAccessManager>
0025 #include <QNetworkReply>
0026 #include <QNetworkRequest>
0027 
0028 // Local includes
0029 
0030 #include "mediawiki_iface.h"
0031 #include "mediawiki_job_p.h"
0032 
0033 namespace MediaWiki
0034 {
0035 
0036 class Q_DECL_HIDDEN QueryImagesPrivate : public JobPrivate
0037 {
0038 public:
0039 
0040     QueryImagesPrivate(Iface& MediaWiki, const QString& limit)
0041         : JobPrivate(MediaWiki),
0042           limit(limit)
0043     {
0044     }
0045 
0046     QString title;
0047     QString limit;
0048     QString imcontinue;
0049 };
0050 
0051 QueryImages::QueryImages(Iface& MediaWiki, QObject* const parent)
0052     : Job(*new QueryImagesPrivate(MediaWiki, QStringLiteral("10")), parent)
0053 {
0054 }
0055 
0056 QueryImages::~QueryImages()
0057 {
0058 }
0059 
0060 void QueryImages::setTitle(const QString& title)
0061 {
0062     Q_D(QueryImages);
0063     d->title = title;
0064 }
0065 
0066 void QueryImages::setLimit(unsigned int limit)
0067 {
0068     Q_D(QueryImages);
0069     d->limit = QString::number(limit);
0070 }
0071 
0072 void QueryImages::start()
0073 {
0074     QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0075 }
0076 
0077 void QueryImages::doWorkSendRequest()
0078 {
0079     Q_D(QueryImages);
0080 
0081     // Set the url
0082     QUrl url = d->MediaWiki.url();
0083     QUrlQuery query;
0084     query.addQueryItem(QStringLiteral("format"),  QStringLiteral("xml"));
0085     query.addQueryItem(QStringLiteral("action"),  QStringLiteral("query"));
0086     query.addQueryItem(QStringLiteral("titles"),  d->title);
0087     query.addQueryItem(QStringLiteral("prop"),    QStringLiteral("images"));
0088     query.addQueryItem(QStringLiteral("imlimit"), d->limit);
0089     if (!d->imcontinue.isNull())
0090     {
0091         query.addQueryItem(QStringLiteral("imcontinue"), d->imcontinue);
0092     }
0093     url.setQuery(query);
0094 
0095     // Set the request
0096     QNetworkRequest request(url);
0097     request.setRawHeader("User-Agent", d->MediaWiki.userAgent().toUtf8());
0098 
0099     // Send the request
0100     d->reply = d->manager->get(request);
0101     connectReply();
0102     connect(d->reply, SIGNAL(finished()),
0103             this, SLOT(doWorkProcessReply()));
0104 }
0105 
0106 void QueryImages::doWorkProcessReply()
0107 {
0108     Q_D(QueryImages);
0109 
0110     disconnect(d->reply, SIGNAL(finished()),
0111                this, SLOT(doWorkProcessReply()));
0112 
0113     if (d->reply->error() == QNetworkReply::NoError)
0114     {
0115         QList<Image> imagesReceived;
0116         d->imcontinue.clear();
0117         QXmlStreamReader reader(d->reply);
0118 
0119         while (!reader.atEnd() && !reader.hasError())
0120         {
0121             QXmlStreamReader::TokenType token = reader.readNext();
0122             if (token == QXmlStreamReader::StartElement)
0123             {
0124                 if (reader.name() == QLatin1String("images"))
0125                 {
0126                     if (reader.attributes().value(QStringLiteral("imcontinue")).isNull())
0127                     {
0128                         imagesReceived.clear();
0129                     }
0130                     else
0131                     {
0132                         d->imcontinue = reader.attributes().value(QStringLiteral("imcontinue")).toString();
0133                     }
0134                 }
0135                 else if (reader.name() == QLatin1String("im"))
0136                 {
0137                     Image image;
0138                     image.setNamespaceId( reader.attributes().value(QStringLiteral("ns")).toString().toUInt());
0139                     image.setTitle(reader.attributes().value(QStringLiteral("title")).toString());
0140                     imagesReceived.push_back(image);
0141                 }
0142             }
0143         }
0144 
0145         if (!reader.hasError())
0146         {
0147             Q_EMIT images(imagesReceived);
0148 
0149             if (!d->imcontinue.isNull())
0150             {
0151                 QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0152                 return;
0153             }
0154             else
0155             {
0156                 setError(KJob::NoError);
0157             }
0158         }
0159         else
0160         {
0161             setError(QueryImages::XmlError);
0162         }
0163     }
0164     else
0165     {
0166         setError(QueryImages::NetworkError);
0167     }
0168 
0169     emitResult();
0170 }
0171 
0172 } // namespace MediaWiki;
0173 
0174 #include "moc_mediawiki_queryimages.cpp"