File indexing completed on 2024-04-21 15:32:07

0001 /** ===========================================================
0002  * @file
0003  *
0004  * This file is a part of KDE project
0005  * <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
0006  *
0007  * @date   2011-03-22
0008  * @brief  a MediaWiki C++ interface for KDE
0009  *
0010  * @author Copyright (C) 2011-2012 by Gilles Caulier
0011  *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
0012  * @author Copyright (C) 2010 by Ludovic Delfau
0013  *         <a href="mailto:ludovicdelfau at gmail dot com">ludovicdelfau at gmail dot com</a>
0014  *
0015  * This program is free software; you can redistribute it
0016  * and/or modify it under the terms of the GNU General
0017  * Public License as published by the Free Software Foundation;
0018  * either version 2, or (at your option)
0019  * any later version.
0020  *
0021  * This program is distributed in the hope that it will be useful,
0022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0024  * GNU General Public License for more details.
0025  *
0026  * ============================================================ */
0027 
0028 #include "queryimages.h"
0029 
0030 
0031 // Qt includes
0032 
0033 #include <QTimer>
0034 #include <QUrl>
0035 #include <QUrlQuery>
0036 #include <QXmlStreamReader>
0037 
0038 #include <QNetworkAccessManager>
0039 #include <QNetworkReply>
0040 #include <QNetworkRequest>
0041 
0042 // Local includes
0043 
0044 #include "mediawiki.h"
0045 #include "job_p.h"
0046 
0047 namespace mediawiki
0048 {
0049 
0050 class QueryImagesPrivate : public JobPrivate
0051 {
0052 public:
0053 
0054     QueryImagesPrivate(MediaWiki& mediawiki, const QString& limit)
0055         : JobPrivate(mediawiki),
0056           limit(limit)
0057     {
0058     }
0059 
0060     QString title;
0061     QString limit;
0062     QString imcontinue;
0063 };
0064 
0065 QueryImages::QueryImages(MediaWiki& mediawiki, QObject* const parent)
0066     : Job(*new QueryImagesPrivate(mediawiki, QStringLiteral("10")), parent)
0067 {
0068 }
0069 
0070 QueryImages::~QueryImages()
0071 {
0072 }
0073 
0074 void QueryImages::setTitle(const QString& title)
0075 {
0076     Q_D(QueryImages);
0077     d->title = title;
0078 }
0079 
0080 void QueryImages::setLimit(unsigned int limit)
0081 {
0082     Q_D(QueryImages);
0083     d->limit = QString::number(limit);
0084 }
0085 
0086 void QueryImages::start()
0087 {
0088     QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0089 }
0090 
0091 void QueryImages::doWorkSendRequest()
0092 {
0093     Q_D(QueryImages);
0094 
0095     // Set the url
0096     QUrl url = d->mediawiki.url();
0097     QUrlQuery query;
0098     query.addQueryItem(QStringLiteral("format"),  QStringLiteral("xml"));
0099     query.addQueryItem(QStringLiteral("action"),  QStringLiteral("query"));
0100     query.addQueryItem(QStringLiteral("titles"),  d->title);
0101     query.addQueryItem(QStringLiteral("prop"),    QStringLiteral("images"));
0102     query.addQueryItem(QStringLiteral("imlimit"), d->limit);
0103     if (!d->imcontinue.isNull())
0104     {
0105         query.addQueryItem(QStringLiteral("imcontinue"), d->imcontinue);
0106     }
0107     url.setQuery(query);
0108 
0109     // Set the request
0110     QNetworkRequest request(url);
0111     request.setRawHeader("User-Agent", d->mediawiki.userAgent().toUtf8());
0112 
0113     // Send the request
0114     d->reply = d->manager->get(request);
0115     connectReply();
0116     connect(d->reply, SIGNAL(finished()),
0117             this, SLOT(doWorkProcessReply()));
0118 }
0119 
0120 void QueryImages::doWorkProcessReply()
0121 {
0122     Q_D(QueryImages);
0123 
0124     disconnect(d->reply, SIGNAL(finished()),
0125                this, SLOT(doWorkProcessReply()));
0126 
0127     if (d->reply->error() == QNetworkReply::NoError)
0128     {
0129         QList<Image> imagesReceived;
0130         d->imcontinue.clear();
0131         QXmlStreamReader reader(d->reply);
0132 
0133         while (!reader.atEnd() && !reader.hasError())
0134         {
0135             QXmlStreamReader::TokenType token = reader.readNext();
0136             if (token == QXmlStreamReader::StartElement)
0137             {
0138                 if (reader.name() == QLatin1String("images"))
0139                 {
0140                     if (reader.attributes().value(QStringLiteral("imcontinue")).isNull())
0141                     {
0142                         imagesReceived.clear();
0143                     }
0144                     else
0145                     {
0146                         d->imcontinue = reader.attributes().value(QStringLiteral("imcontinue")).toString();
0147                     }
0148                 }
0149                 else if (reader.name() == QLatin1String("im"))
0150                 {
0151                     Image image;
0152                     image.setNamespaceId( reader.attributes().value(QStringLiteral("ns")).toString().toUInt());
0153                     image.setTitle(reader.attributes().value(QStringLiteral("title")).toString());
0154                     imagesReceived.push_back(image);
0155                 }
0156             }
0157         }
0158 
0159         if (!reader.hasError())
0160         {
0161             emit images(imagesReceived);
0162 
0163             if (!d->imcontinue.isNull())
0164             {
0165                 QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0166                 return;
0167             }
0168             else
0169             {
0170                 setError(KJob::NoError);
0171             }
0172         }
0173         else
0174         {
0175             setError(QueryImages::XmlError);
0176         }
0177     }
0178     else
0179     {
0180         setError(QueryImages::NetworkError);
0181     }
0182 
0183     emitResult();
0184 }
0185 
0186 } // namespace mediawiki;