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

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_querysiteinfousergroups.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 QuerySiteinfoUsergroupsPrivate : public JobPrivate
0037 {
0038 public:
0039 
0040     QuerySiteinfoUsergroupsPrivate(Iface& MediaWiki, bool includeNumber)
0041             : JobPrivate(MediaWiki),
0042               includeNumber(includeNumber)
0043     {
0044     }
0045 
0046     bool includeNumber;
0047 };
0048 
0049 QuerySiteinfoUsergroups::QuerySiteinfoUsergroups(Iface& MediaWiki, QObject* const parent)
0050     : Job(*new QuerySiteinfoUsergroupsPrivate(MediaWiki, false), parent)
0051 {
0052 }
0053 
0054 QuerySiteinfoUsergroups::~QuerySiteinfoUsergroups()
0055 {
0056 }
0057 
0058 void QuerySiteinfoUsergroups::setIncludeNumber(bool includeNumber)
0059 {
0060     Q_D(QuerySiteinfoUsergroups);
0061     d->includeNumber = includeNumber;
0062 }
0063 
0064 void QuerySiteinfoUsergroups::start()
0065 {
0066     QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0067 }
0068 
0069 void QuerySiteinfoUsergroups::doWorkSendRequest()
0070 {
0071     Q_D(QuerySiteinfoUsergroups);
0072 
0073     // Set the url
0074     QUrl url = d->MediaWiki.url();
0075     QUrlQuery query;
0076     query.addQueryItem(QStringLiteral("format"), QStringLiteral("xml"));
0077     query.addQueryItem(QStringLiteral("action"), QStringLiteral("query"));
0078     query.addQueryItem(QStringLiteral("meta"),   QStringLiteral("siteinfo"));
0079     query.addQueryItem(QStringLiteral("siprop"), QStringLiteral("usergroups"));
0080 
0081     if (d->includeNumber)
0082     {
0083         query.addQueryItem(QStringLiteral("sinumberingroup"), QString());
0084     }
0085 
0086     url.setQuery(query);
0087 
0088     // Set the request
0089     QNetworkRequest request(url);
0090     request.setRawHeader("User-Agent", d->MediaWiki.userAgent().toUtf8());
0091 
0092     // Send the request
0093     d->reply = d->manager->get(request);
0094     connectReply();
0095 
0096     connect(d->reply, SIGNAL(finished()),
0097             this, SLOT(doWorkProcessReply()));
0098 }
0099 
0100 void QuerySiteinfoUsergroups::doWorkProcessReply()
0101 {
0102     Q_D(QuerySiteinfoUsergroups);
0103 
0104     disconnect(d->reply, SIGNAL(finished()),
0105                this, SLOT(doWorkProcessReply()));
0106 
0107     if (d->reply->error() == QNetworkReply::NoError)
0108     {
0109         QList<UserGroup> results;
0110         QString          name;
0111         QList<QString>   rights;
0112         unsigned int     number = 0;
0113         QXmlStreamReader reader(d->reply);
0114 
0115         while (!reader.atEnd() && !reader.hasError())
0116         {
0117             QXmlStreamReader::TokenType token = reader.readNext();
0118 
0119             if (token == QXmlStreamReader::StartElement)
0120             {
0121                 if (reader.name() == QLatin1String("group"))
0122                 {
0123                     name = reader.attributes().value(QStringLiteral("name")).toString();
0124 
0125                     if (d->includeNumber)
0126                     {
0127                         number = reader.attributes().value(QStringLiteral("number")).toString().toUInt();
0128                     }
0129                 }
0130                 else if (reader.name() == QLatin1String("rights"))
0131                 {
0132                     rights.clear();
0133                 }
0134                 else if (reader.name() == QLatin1String("permission"))
0135                 {
0136                     reader.readNext();
0137                     rights.push_back(reader.text().toString());
0138                 }
0139             }
0140             else if (token == QXmlStreamReader::EndElement)
0141             {
0142                 if (reader.name() == QLatin1String("group"))
0143                 {
0144                     UserGroup usergroup;
0145                     usergroup.setName(name);
0146                     usergroup.setRights(rights);
0147 
0148                     if (d->includeNumber)
0149                     {
0150                         usergroup.setNumber(number);
0151                     }
0152 
0153                     results.push_back(usergroup);
0154                 }
0155             }
0156         }
0157 
0158         if (!reader.hasError())
0159         {
0160             setError(Job::NoError);
0161             Q_EMIT usergroups(results);
0162         }
0163         else
0164         {
0165             setError(QuerySiteinfoUsergroups::XmlError);
0166         }
0167     }
0168     else
0169     {
0170         setError(QuerySiteinfoUsergroups::NetworkError);
0171     }
0172 
0173     emitResult();
0174 }
0175 
0176 } // namespace MediaWiki
0177 
0178 #include "moc_mediawiki_querysiteinfousergroups.cpp"