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 Alexandre Mendes
0013  *         <a href="mailto:alex dot mendes1988 at gmail dot com">alex dot mendes1988 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 "logout.h"
0029 
0030 
0031 // Qt includes
0032 
0033 #include <QDateTime>
0034 #include <QTimer>
0035 #include <QUrl>
0036 #include <QUrlQuery>
0037 #include <QXmlStreamReader>
0038 
0039 #include <QNetworkCookie>
0040 #include <QNetworkReply>
0041 #include <QNetworkRequest>
0042 
0043 // Local includes
0044 
0045 #include "mediawiki.h"
0046 #include "job_p.h"
0047 
0048 namespace mediawiki
0049 {
0050 
0051 class LogoutPrivate : public JobPrivate
0052 {
0053 public:
0054 
0055     LogoutPrivate(MediaWiki& mediawiki)
0056         : JobPrivate(mediawiki)
0057     {
0058     }
0059 };
0060 
0061 Logout::Logout(MediaWiki& mediawiki, QObject* const parent)
0062     : Job(*new LogoutPrivate(mediawiki), parent)
0063 {
0064 }
0065 
0066 Logout::~Logout()
0067 {
0068 }
0069 
0070 void Logout::start()
0071 {
0072     QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0073 }
0074 
0075 void Logout::doWorkSendRequest()
0076 {
0077     Q_D(Logout);
0078     
0079     QUrl url = d->mediawiki.url();
0080     QUrlQuery query;
0081     query.addQueryItem(QStringLiteral("format"), QStringLiteral("xml"));
0082     query.addQueryItem(QStringLiteral("action"), QStringLiteral("logout"));
0083     url.setQuery(query);
0084     
0085     QByteArray cookie = "";
0086     QList<QNetworkCookie> mediawikiCookies = d->manager->cookieJar()->cookiesForUrl(d->mediawiki.url());
0087 
0088     for(int i = 0 ; i < mediawikiCookies.size(); ++i)
0089     {
0090         cookie += mediawikiCookies.at(i).toRawForm(QNetworkCookie::NameAndValueOnly);
0091         cookie += ';';
0092     }
0093 
0094     // Set the request
0095     QNetworkRequest request(url);
0096     request.setRawHeader("User-Agent", d->mediawiki.userAgent().toUtf8());
0097     request.setRawHeader( "Cookie", cookie );
0098 
0099     // Delete cookies
0100     d->manager->setCookieJar(new QNetworkCookieJar);
0101 
0102     // Send the request
0103     d->reply = d->manager->get(request);
0104     connectReply();
0105     connect(d->reply, SIGNAL(finished()),
0106             this, SLOT(doWorkProcessReply()));
0107 }
0108 
0109 void Logout::doWorkProcessReply()
0110 {
0111     Q_D(Logout);
0112     disconnect(d->reply, SIGNAL(finished()),
0113                this, SLOT(doWorkProcessReply()));
0114 
0115     this->setError(KJob::NoError);
0116     d->reply->close();
0117     d->reply->deleteLater();
0118     emitResult();
0119 }
0120 
0121 } // namespace mediawiki