1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-10 23:07:59 +00:00

Save RSS cookies to common cookie storage

Closes #4305
This commit is contained in:
Vladimir Golovnev (qlassez) 2015-12-18 20:45:43 +03:00
parent 0bffa066db
commit 12c151eb69
6 changed files with 68 additions and 65 deletions

View File

@ -45,6 +45,7 @@
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "base/utils/string.h" #include "base/utils/string.h"
#include "base/qinisettings.h" #include "base/qinisettings.h"
#include "base/preferences.h"
bool userAcceptsUpgrade() bool userAcceptsUpgrade()
{ {
@ -114,6 +115,9 @@ bool upgradeResumeFile(const QString &filepath, const QVariantHash &oldTorrent,
bool upgrade(bool ask = true) bool upgrade(bool ask = true)
{ {
// Move RSS cookies to common storage
Preferences::instance()->moveRSSCookies();
QIniSettings *oldResumeSettings = new QIniSettings("qBittorrent", "qBittorrent-resume"); QIniSettings *oldResumeSettings = new QIniSettings("qBittorrent", "qBittorrent-resume");
QString oldResumeFilename = oldResumeSettings->fileName(); QString oldResumeFilename = oldResumeSettings->fileName();
QVariantHash oldResumeData = oldResumeSettings->value("torrents").toHash(); QVariantHash oldResumeData = oldResumeSettings->value("torrents").toHash();

View File

@ -2525,38 +2525,29 @@ void Preferences::setToolbarTextPosition(const int position)
setValue("Toolbar/textPosition", position); setValue("Toolbar/textPosition", position);
} }
QList<QByteArray> Preferences::getHostNameCookies(const QString &host_name) const void Preferences::moveRSSCookies()
{ {
QMap<QString, QVariant> hosts_table = value("Rss/hosts_cookies").toMap(); QList<QNetworkCookie> cookies = getNetworkCookies();
if (!hosts_table.contains(host_name)) return QList<QByteArray>(); QVariantMap hostsTable = value("Rss/hosts_cookies").toMap();
QByteArray raw_cookies = hosts_table.value(host_name).toByteArray(); foreach (const QString &key, hostsTable.keys()) {
return raw_cookies.split(':'); QVariant value = hostsTable[key];
} QList<QByteArray> rawCookies = value.toByteArray().split(':');
foreach (const QByteArray &rawCookie, rawCookies) {
QList<QNetworkCookie> Preferences::getHostNameQNetworkCookies(const QString& host_name) const foreach (QNetworkCookie cookie, QNetworkCookie::parseCookies(rawCookie)) {
{ cookie.setDomain(key);
QList<QNetworkCookie> cookies; cookie.setPath("/");
const QList<QByteArray> raw_cookies = getHostNameCookies(host_name); cookie.setExpirationDate(QDateTime::currentDateTime().addYears(10));
foreach (const QByteArray& raw_cookie, raw_cookies) { cookies << cookie;
QList<QByteArray> cookie_parts = raw_cookie.split('='); }
if (cookie_parts.size() == 2) {
qDebug("Loading cookie: %s = %s", cookie_parts.first().constData(), cookie_parts.last().constData());
cookies << QNetworkCookie(cookie_parts.first(), cookie_parts.last());
} }
} }
return cookies;
}
void Preferences::setHostNameCookies(const QString &host_name, const QList<QByteArray> &cookies) setNetworkCookies(cookies);
{
QMap<QString, QVariant> hosts_table = value("Rss/hosts_cookies").toMap(); QWriteLocker locker(&lock);
QByteArray raw_cookies = ""; dirty = true;
foreach (const QByteArray& cookie, cookies) timer.start();
raw_cookies += cookie + ":"; m_data.remove("Rss/hosts_cookies");
if (raw_cookies.endsWith(":"))
raw_cookies.chop(1);
hosts_table.insert(host_name, raw_cookies);
setValue("Rss/hosts_cookies", hosts_table);
} }
QList<QNetworkCookie> Preferences::getNetworkCookies() const QList<QNetworkCookie> Preferences::getNetworkCookies() const

View File

@ -538,13 +538,12 @@ public:
void setRssFeedsUrls(const QStringList &rssFeeds); void setRssFeedsUrls(const QStringList &rssFeeds);
QStringList getRssFeedsAliases() const; QStringList getRssFeedsAliases() const;
void setRssFeedsAliases(const QStringList &rssAliases); void setRssFeedsAliases(const QStringList &rssAliases);
QList<QByteArray> getHostNameCookies(const QString &host_name) const;
QList<QNetworkCookie> getHostNameQNetworkCookies(const QString& host_name) const;
void setHostNameCookies(const QString &host_name, const QList<QByteArray> &cookies);
// Network // Network
QList<QNetworkCookie> getNetworkCookies() const; QList<QNetworkCookie> getNetworkCookies() const;
void setNetworkCookies(const QList<QNetworkCookie> &cookies); void setNetworkCookies(const QList<QNetworkCookie> &cookies);
// Temporary method for upgrade purposes
void moveRSSCookies();
// SpeedWidget // SpeedWidget
int getSpeedWidgetPeriod() const; int getSpeedWidgetPeriod() const;

View File

@ -31,12 +31,14 @@
#include "cookiesdlg.h" #include "cookiesdlg.h"
#include "ui_cookiesdlg.h" #include "ui_cookiesdlg.h"
#include "guiiconprovider.h" #include "guiiconprovider.h"
#include "base/net/downloadmanager.h"
#include <QNetworkCookie> #include <QNetworkCookie>
#include <QDateTime>
enum CookiesCols { COOKIE_KEY, COOKIE_VALUE}; enum CookiesCols { COOKIE_KEY, COOKIE_VALUE};
CookiesDlg::CookiesDlg(QWidget *parent, const QList<QByteArray> &raw_cookies) : CookiesDlg::CookiesDlg(const QUrl &url, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::CookiesDlg) ui(new Ui::CookiesDlg)
{ {
@ -46,13 +48,13 @@ CookiesDlg::CookiesDlg(QWidget *parent, const QList<QByteArray> &raw_cookies) :
ui->del_btn->setIcon(GuiIconProvider::instance()->getIcon("list-remove")); ui->del_btn->setIcon(GuiIconProvider::instance()->getIcon("list-remove"));
ui->infos_lbl->setText(tr("Common keys for cookies are: '%1', '%2'.\nYou should get this information from your Web browser preferences.").arg("uid").arg("pass")); ui->infos_lbl->setText(tr("Common keys for cookies are: '%1', '%2'.\nYou should get this information from your Web browser preferences.").arg("uid").arg("pass"));
foreach (const QByteArray &raw_cookie, raw_cookies) {
QList<QByteArray> cookie_parts = raw_cookie.split('='); QList<QNetworkCookie> cookies = Net::DownloadManager::instance()->cookiesForUrl(url);
if (cookie_parts.size() != 2) continue; foreach (const QNetworkCookie &cookie, cookies) {
const int i = ui->cookiesTable->rowCount(); const int i = ui->cookiesTable->rowCount();
ui->cookiesTable->setRowCount(i+1); ui->cookiesTable->setRowCount(i+1);
ui->cookiesTable->setItem(i, COOKIE_KEY, new QTableWidgetItem(cookie_parts.first().data())); ui->cookiesTable->setItem(i, COOKIE_KEY, new QTableWidgetItem(QString(cookie.name())));
ui->cookiesTable->setItem(i, COOKIE_VALUE, new QTableWidgetItem(cookie_parts.last().data())); ui->cookiesTable->setItem(i, COOKIE_VALUE, new QTableWidgetItem(QString(cookie.value())));
} }
} }
@ -75,8 +77,9 @@ void CookiesDlg::on_del_btn_clicked() {
} }
} }
QList<QByteArray> CookiesDlg::getCookies() const { QList<QNetworkCookie> CookiesDlg::getCookies() const {
QList<QByteArray> ret; QList<QNetworkCookie> ret;
auto now = QDateTime::currentDateTime();
for (int i=0; i<ui->cookiesTable->rowCount(); ++i) { for (int i=0; i<ui->cookiesTable->rowCount(); ++i) {
QString key; QString key;
if (ui->cookiesTable->item(i, COOKIE_KEY)) if (ui->cookiesTable->item(i, COOKIE_KEY))
@ -85,20 +88,23 @@ QList<QByteArray> CookiesDlg::getCookies() const {
if (ui->cookiesTable->item(i, COOKIE_VALUE)) if (ui->cookiesTable->item(i, COOKIE_VALUE))
value = ui->cookiesTable->item(i, COOKIE_VALUE)->text().trimmed(); value = ui->cookiesTable->item(i, COOKIE_VALUE)->text().trimmed();
if (!key.isEmpty() && !value.isEmpty()) { if (!key.isEmpty() && !value.isEmpty()) {
const QString raw_cookie = key+"="+value; QNetworkCookie cookie(key.toUtf8(), value.toUtf8());
qDebug("Cookie: %s", qPrintable(raw_cookie)); // TODO: Delete this hack when advanced Cookie dialog will be implemented.
ret << raw_cookie.toLocal8Bit(); cookie.setExpirationDate(now.addYears(10));
qDebug("Cookie: %s", cookie.toRawForm().data());
ret << cookie;
} }
} }
return ret; return ret;
} }
QList<QByteArray> CookiesDlg::askForCookies(QWidget *parent, const QList<QByteArray> &raw_cookies, bool *ok) { bool CookiesDlg::askForCookies(QWidget *parent, const QUrl &url, QList<QNetworkCookie> &out)
CookiesDlg dlg(parent, raw_cookies); {
CookiesDlg dlg(url, parent);
if (dlg.exec()) { if (dlg.exec()) {
*ok = true; out = dlg.getCookies();
return dlg.getCookies(); return true;
} }
*ok = false;
return QList<QByteArray>(); return false;
} }

View File

@ -32,22 +32,24 @@
#define COOKIESDLG_H #define COOKIESDLG_H
#include <QDialog> #include <QDialog>
#include <QList>
class QNetworkCookie;
class QUrl;
QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
class CookiesDlg; class CookiesDlg;
} }
QT_END_NAMESPACE
class CookiesDlg : public QDialog class CookiesDlg : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CookiesDlg(QWidget *parent = 0, const QList<QByteArray> &raw_cookies = QList<QByteArray>()); explicit CookiesDlg(const QUrl &url, QWidget *parent = 0);
~CookiesDlg(); ~CookiesDlg();
QList<QByteArray> getCookies() const; QList<QNetworkCookie> getCookies() const;
static QList<QByteArray> askForCookies(QWidget *parent, const QList<QByteArray> &raw_cookies, bool *ok); static bool askForCookies(QWidget *parent, const QUrl &url, QList<QNetworkCookie> &out);
protected slots: protected slots:
void on_add_btn_clicked(); void on_add_btn_clicked();

View File

@ -142,18 +142,19 @@ void RSSImp::displayItemsListMenu(const QPoint&)
void RSSImp::on_actionManage_cookies_triggered() void RSSImp::on_actionManage_cookies_triggered()
{ {
Q_ASSERT(!m_feedList->selectedItems().empty()); Q_ASSERT(!m_feedList->selectedItems().empty());
// Get feed hostname
QString feed_url = m_feedList->getItemID(m_feedList->selectedItems().first()); // TODO: Create advanced application wide Cookie dialog and use it everywhere.
QString feed_hostname = QUrl::fromEncoded(feed_url.toUtf8()).host(); QUrl feedUrl = QUrl::fromEncoded(m_feedList->getItemID(m_feedList->selectedItems().first()).toUtf8());
qDebug("RSS Feed hostname is: %s", qPrintable(feed_hostname)); QList<QNetworkCookie> cookies;
Q_ASSERT(!feed_hostname.isEmpty()); if (CookiesDlg::askForCookies(this, feedUrl, cookies)) {
bool ok = false; auto downloadManager = Net::DownloadManager::instance();
Preferences* const pref = Preferences::instance(); QList<QNetworkCookie> oldCookies = downloadManager->cookiesForUrl(feedUrl);
QList<QByteArray> raw_cookies = CookiesDlg::askForCookies(this, pref->getHostNameCookies(feed_hostname), &ok); foreach (const QNetworkCookie &oldCookie, oldCookies) {
if (ok) { if (!cookies.contains(oldCookie))
qDebug() << "Settings cookies for host name: " << feed_hostname; downloadManager->deleteCookie(oldCookie);
pref->setHostNameCookies(feed_hostname, raw_cookies); }
Net::DownloadManager::instance()->setCookiesFromUrl(pref->getHostNameQNetworkCookies(feed_hostname), feed_hostname);
downloadManager->setCookiesFromUrl(cookies, feedUrl);
} }
} }