diff --git a/src/librssguard/miscellaneous/settings.cpp b/src/librssguard/miscellaneous/settings.cpp index 151fa32d5..c8f89c531 100755 --- a/src/librssguard/miscellaneous/settings.cpp +++ b/src/librssguard/miscellaneous/settings.cpp @@ -15,6 +15,9 @@ DKEY WebEngineAttributes::ID = "web_engine_attributes"; #endif +// Cookies. +DKEY Cookies::ID = "cookies"; + // AdBlock. DKEY AdBlock::ID = "adblock"; diff --git a/src/librssguard/miscellaneous/settings.h b/src/librssguard/miscellaneous/settings.h index befe1320d..1a253b8a4 100644 --- a/src/librssguard/miscellaneous/settings.h +++ b/src/librssguard/miscellaneous/settings.h @@ -32,6 +32,10 @@ namespace WebEngineAttributes { } #endif +namespace Cookies { + KEY ID; +} + namespace AdBlock { KEY ID; diff --git a/src/librssguard/network-web/cookiejar.cpp b/src/librssguard/network-web/cookiejar.cpp index c7d349b1f..031852d9c 100755 --- a/src/librssguard/network-web/cookiejar.cpp +++ b/src/librssguard/network-web/cookiejar.cpp @@ -2,14 +2,22 @@ #include "network-web/cookiejar.h" +#include "3rd-party/boolinq/boolinq.h" #include "definitions/definitions.h" +#include "miscellaneous/application.h" +#include "miscellaneous/iofactory.h" +#include "miscellaneous/settings.h" #include +#include #include +#include -CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) {} +CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) { + loadCookies(); +} -QList CookieJar::extractCookiesFromUrl(const QString& url) const { +QList CookieJar::extractCookiesFromUrl(const QString& url) { if (!url.contains(QSL(COOKIE_URL_IDENTIFIER))) { return {}; } @@ -37,11 +45,85 @@ QList CookieJar::extractCookiesFromUrl(const QString& url) const return cookies; } -bool CookieJar::insertCookies(const QList& cookies) { - bool result = true; +void CookieJar::loadCookies() { + Settings* sett = qApp->settings(); + + sett->beginGroup(GROUP(Cookies)); + auto keys = sett->allKeys(); + + sett->endGroup(); + + for (const QString& cookie_key : qAsConst(keys)) { + QByteArray encoded = sett->password(GROUP(Cookies), cookie_key, {}).toByteArray(); + + if (!encoded.isEmpty()) { + auto cookie = QNetworkCookie::parseCookies(encoded); + + if (!cookie.isEmpty()) { + if (!QNetworkCookieJar::insertCookie(cookie.at(0))) { + qCriticalNN << LOGSEC_NETWORK + << "Failed to load cookie" + << QUOTE_W_SPACE(cookie_key) + << "from settings."; + } + } + } + } +} + +void CookieJar::saveCookies() { + auto cookies = allCookies(); + Settings* sett = qApp->settings(); + int i = 1; + + sett->beginGroup(GROUP(Cookies)); + qobject_cast(sett)->remove(QString()); + sett->endGroup(); for (const QNetworkCookie& cookie : cookies) { - result &= insertCookie(cookie); + if (cookie.isSessionCookie()) { + continue; + } + + sett->setPassword(GROUP(Cookies), + QSL("%1-%2").arg(QString::number(i++), cookie.name()), + cookie.toRawForm(QNetworkCookie::RawForm::Full)); + } +} + +QList CookieJar::cookiesForUrl(const QUrl& url) const { + return QNetworkCookieJar::cookiesForUrl(url); +} + +bool CookieJar::setCookiesFromUrl(const QList& cookie_list, const QUrl& url) { + return QNetworkCookieJar::setCookiesFromUrl(cookie_list, url); +} + +bool CookieJar::insertCookie(const QNetworkCookie& cookie) { + auto result = QNetworkCookieJar::insertCookie(cookie); + + if (result) { + saveCookies(); + } + + return result; +} + +bool CookieJar::updateCookie(const QNetworkCookie& cookie) { + auto result = QNetworkCookieJar::updateCookie(cookie); + + if (result) { + saveCookies(); + } + + return result; +} + +bool CookieJar::deleteCookie(const QNetworkCookie& cookie) { + auto result = QNetworkCookieJar::deleteCookie(cookie); + + if (result) { + saveCookies(); } return result; diff --git a/src/librssguard/network-web/cookiejar.h b/src/librssguard/network-web/cookiejar.h index 9045c6da6..cfe9f57d8 100755 --- a/src/librssguard/network-web/cookiejar.h +++ b/src/librssguard/network-web/cookiejar.h @@ -9,8 +9,16 @@ class CookieJar : public QNetworkCookieJar { public: explicit CookieJar(QObject* parent = nullptr); - QList extractCookiesFromUrl(const QString& url) const; - bool insertCookies(const QList& cookies); + virtual QList cookiesForUrl(const QUrl& url) const; + virtual bool setCookiesFromUrl(const QList& cookie_list, const QUrl& url); + virtual bool insertCookie(const QNetworkCookie& cookie); + virtual bool updateCookie(const QNetworkCookie& cookie); + virtual bool deleteCookie(const QNetworkCookie& cookie); + static QList extractCookiesFromUrl(const QString& url); + + private: + void loadCookies(); + void saveCookies(); }; #endif // COOKIEJAR_H diff --git a/src/librssguard/network-web/downloader.cpp b/src/librssguard/network-web/downloader.cpp index 1bcf77255..4c6aace5a 100644 --- a/src/librssguard/network-web/downloader.cpp +++ b/src/librssguard/network-web/downloader.cpp @@ -61,7 +61,7 @@ void Downloader::manipulateData(const QString& url, const QString& username, const QString& password) { - auto cookies = qApp->web()->cookieJar()->extractCookiesFromUrl(url); + auto cookies = CookieJar::extractCookiesFromUrl(url); if (!cookies.isEmpty()) { qApp->web()->cookieJar()->setCookiesFromUrl(cookies, url);