Browse Source

Make DownloadManager to save/load cookies

adaptive-webui-19844
Vladimir Golovnev (qlassez) 9 years ago
parent
commit
0bffa066db
  1. 2
      src/app/application.cpp
  2. 81
      src/base/net/downloadmanager.cpp
  3. 8
      src/base/net/downloadmanager.h
  4. 29
      src/base/preferences.cpp
  5. 4
      src/base/preferences.h

2
src/app/application.cpp

@ -462,10 +462,10 @@ void Application::cleanup()
#ifndef DISABLE_COUNTRIES_RESOLUTION #ifndef DISABLE_COUNTRIES_RESOLUTION
Net::GeoIPManager::freeInstance(); Net::GeoIPManager::freeInstance();
#endif #endif
Net::DownloadManager::freeInstance();
Preferences::freeInstance(); Preferences::freeInstance();
Logger::freeInstance(); Logger::freeInstance();
IconProvider::freeInstance(); IconProvider::freeInstance();
Net::DownloadManager::freeInstance();
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
typedef BOOL (WINAPI *PSHUTDOWNBRDESTROY)(HWND); typedef BOOL (WINAPI *PSHUTDOWNBRDESTROY)(HWND);

81
src/base/net/downloadmanager.cpp

@ -27,11 +27,13 @@
* exception statement from your version. * exception statement from your version.
*/ */
#include <QDateTime>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QNetworkCookieJar> #include <QNetworkCookieJar>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkCookie> #include <QNetworkCookie>
#include <QNetworkCookieJar>
#include <QSslError> #include <QSslError>
#include <QUrl> #include <QUrl>
#include <QDebug> #include <QDebug>
@ -40,6 +42,71 @@
#include "downloadhandler.h" #include "downloadhandler.h"
#include "downloadmanager.h" #include "downloadmanager.h"
namespace
{
class NetworkCookieJar: public QNetworkCookieJar
{
public:
explicit NetworkCookieJar(QObject *parent = 0)
: QNetworkCookieJar(parent)
{
QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies();
foreach (const QNetworkCookie &cookie, Preferences::instance()->getNetworkCookies()) {
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
setAllCookies(cookies);
}
~NetworkCookieJar()
{
QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = allCookies();
foreach (const QNetworkCookie &cookie, allCookies()) {
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
Preferences::instance()->setNetworkCookies(cookies);
}
#ifndef QBT_USES_QT5
virtual bool deleteCookie(const QNetworkCookie &cookie)
{
auto myCookies = allCookies();
myCookies.removeAll(cookie);
setAllCookies(myCookies);
}
#endif
QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const override
{
QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
foreach (const QNetworkCookie &cookie, QNetworkCookieJar::cookiesForUrl(url)) {
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
return cookies;
}
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) override
{
QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = cookieList;
foreach (const QNetworkCookie &cookie, cookieList) {
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
}
};
}
using namespace Net; using namespace Net;
DownloadManager *DownloadManager::m_instance = 0; DownloadManager *DownloadManager::m_instance = 0;
@ -50,10 +117,7 @@ DownloadManager::DownloadManager(QObject *parent)
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
connect(&m_networkManager, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply *, QList<QSslError>))); connect(&m_networkManager, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply *, QList<QSslError>)));
#endif #endif
} m_networkManager.setCookieJar(new NetworkCookieJar(this));
DownloadManager::~DownloadManager()
{
} }
void DownloadManager::initInstance() void DownloadManager::initInstance()
@ -92,22 +156,27 @@ DownloadHandler *DownloadManager::downloadUrl(const QString &url, bool saveToFil
request.setRawHeader("Referer", request.url().toEncoded().data()); request.setRawHeader("Referer", request.url().toEncoded().data());
qDebug("Downloading %s...", request.url().toEncoded().data()); qDebug("Downloading %s...", request.url().toEncoded().data());
qDebug() << "Cookies:" << m_networkManager.cookieJar()->cookiesForUrl(request.url());
// accept gzip // accept gzip
request.setRawHeader("Accept-Encoding", "gzip"); request.setRawHeader("Accept-Encoding", "gzip");
return new DownloadHandler(m_networkManager.get(request), this, saveToFile, limit, handleRedirectToMagnet); return new DownloadHandler(m_networkManager.get(request), this, saveToFile, limit, handleRedirectToMagnet);
} }
QList<QNetworkCookie> DownloadManager::cookiesForUrl(const QString &url) const QList<QNetworkCookie> DownloadManager::cookiesForUrl(const QUrl &url) const
{ {
return m_networkManager.cookieJar()->cookiesForUrl(url); return m_networkManager.cookieJar()->cookiesForUrl(url);
} }
bool DownloadManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) bool DownloadManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{ {
qDebug("Setting %d cookies for url: %s", cookieList.size(), qPrintable(url.toString()));
return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url); return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url);
} }
bool DownloadManager::deleteCookie(const QNetworkCookie &cookie)
{
return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->deleteCookie(cookie);
}
void DownloadManager::applyProxySettings() void DownloadManager::applyProxySettings()
{ {
QNetworkProxy proxy; QNetworkProxy proxy;

8
src/base/net/downloadmanager.h

@ -33,12 +33,10 @@
#include <QObject> #include <QObject>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
QT_BEGIN_NAMESPACE
class QNetworkReply; class QNetworkReply;
class QNetworkCookie; class QNetworkCookie;
class QSslError; class QSslError;
class QUrl; class QUrl;
QT_END_NAMESPACE
namespace Net namespace Net
{ {
@ -54,8 +52,9 @@ namespace Net
static DownloadManager *instance(); static DownloadManager *instance();
DownloadHandler *downloadUrl(const QString &url, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false); DownloadHandler *downloadUrl(const QString &url, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false);
QList<QNetworkCookie> cookiesForUrl(const QString &url) const; QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url); bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
bool deleteCookie(const QNetworkCookie &cookie);
private slots: private slots:
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
@ -63,8 +62,7 @@ namespace Net
#endif #endif
private: private:
DownloadManager(QObject *parent = 0); explicit DownloadManager(QObject *parent = 0);
~DownloadManager();
void applyProxySettings(); void applyProxySettings();

29
src/base/preferences.cpp

@ -1027,12 +1027,12 @@ void Preferences::setFilteringEnabled(bool enabled)
bool Preferences::isFilteringTrackerEnabled() const bool Preferences::isFilteringTrackerEnabled() const
{ {
return value("Preferences/IPFilter/FilterTracker", false).toBool(); return value("Preferences/IPFilter/FilterTracker", false).toBool();
} }
void Preferences::setFilteringTrackerEnabled(bool enabled) void Preferences::setFilteringTrackerEnabled(bool enabled)
{ {
setValue("Preferences/IPFilter/FilterTracker", enabled); setValue("Preferences/IPFilter/FilterTracker", enabled);
} }
QString Preferences::getFilter() const QString Preferences::getFilter() const
@ -2559,11 +2559,32 @@ void Preferences::setHostNameCookies(const QString &host_name, const QList<QByte
setValue("Rss/hosts_cookies", hosts_table); setValue("Rss/hosts_cookies", hosts_table);
} }
int Preferences::getSpeedWidgetPeriod() const { QList<QNetworkCookie> Preferences::getNetworkCookies() const
{
QList<QNetworkCookie> cookies;
QStringList rawCookies = value("Network/Cookies").toStringList();
foreach (const QString &rawCookie, rawCookies)
cookies << QNetworkCookie::parseCookies(rawCookie.toUtf8());
return cookies;
}
void Preferences::setNetworkCookies(const QList<QNetworkCookie> &cookies)
{
QStringList rawCookies;
foreach (const QNetworkCookie &cookie, cookies)
rawCookies << cookie.toRawForm();
setValue("Network/Cookies", rawCookies);
}
int Preferences::getSpeedWidgetPeriod() const
{
return value("SpeedWidget/period", 1).toInt(); return value("SpeedWidget/period", 1).toInt();
} }
void Preferences::setSpeedWidgetPeriod(const int period) { void Preferences::setSpeedWidgetPeriod(const int period)
{
setValue("SpeedWidget/period", period); setValue("SpeedWidget/period", period);
} }

4
src/base/preferences.h

@ -542,6 +542,10 @@ public:
QList<QNetworkCookie> getHostNameQNetworkCookies(const QString& host_name) const; QList<QNetworkCookie> getHostNameQNetworkCookies(const QString& host_name) const;
void setHostNameCookies(const QString &host_name, const QList<QByteArray> &cookies); void setHostNameCookies(const QString &host_name, const QList<QByteArray> &cookies);
// Network
QList<QNetworkCookie> getNetworkCookies() const;
void setNetworkCookies(const QList<QNetworkCookie> &cookies);
// SpeedWidget // SpeedWidget
int getSpeedWidgetPeriod() const; int getSpeedWidgetPeriod() const;
void setSpeedWidgetPeriod(const int period); void setSpeedWidgetPeriod(const int period);

Loading…
Cancel
Save