From d55f3b5affffeab8510d4bbc20b02c5fc29c5d1d Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 20 May 2012 16:03:10 +0300 Subject: [PATCH] RSS: Better cookies support --- src/downloadthread.cpp | 37 ++++++++------------------------- src/downloadthread.h | 8 +++---- src/qtlibtorrent/qbtsession.cpp | 5 +++-- src/qtlibtorrent/qbtsession.h | 3 ++- src/rss/rss_imp.cpp | 20 +++++++++++++++--- 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/downloadthread.cpp b/src/downloadthread.cpp index c0e5d17dd..6a765f85a 100644 --- a/src/downloadthread.cpp +++ b/src/downloadthread.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include "downloadthread.h" @@ -109,39 +108,21 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) { reply->deleteLater(); } -#ifndef DISABLE_GUI -void DownloadThread::loadCookies(const QString &host_name, QString url) { - const QList raw_cookies = RssSettings().getHostNameCookies(host_name); - QNetworkCookieJar *cookie_jar = m_networkManager.cookieJar(); - QList cookies; - qDebug("Loading cookies for host name: %s", qPrintable(host_name)); - foreach (const QByteArray& raw_cookie, raw_cookies) { - QList cookie_parts = raw_cookie.split('='); - if (cookie_parts.size() == 2) { - qDebug("Loading cookie: %s", raw_cookie.constData()); - cookies << QNetworkCookie(cookie_parts.first(), cookie_parts.last()); - } - } - cookie_jar->setCookiesFromUrl(cookies, url); - m_networkManager.setCookieJar(cookie_jar); -} -#endif - -void DownloadThread::downloadTorrentUrl(const QString &url) { +void DownloadThread::downloadTorrentUrl(const QString &url, const QList& cookies) +{ // Process request - QNetworkReply *reply = downloadUrl(url); + QNetworkReply *reply = downloadUrl(url, cookies); connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64))); } -QNetworkReply* DownloadThread::downloadUrl(const QString &url) { +QNetworkReply* DownloadThread::downloadUrl(const QString &url, const QList& cookies) { // Update proxy settings applyProxySettings(); -#ifndef DISABLE_GUI - // Load cookies - QString host_name = QUrl::fromEncoded(url.toUtf8()).host(); - if (!host_name.isEmpty()) - loadCookies(host_name, url); -#endif + // Set cookies + if (!cookies.empty()) { + qDebug("Setting %d cookies for url: %s", cookies.size(), qPrintable(url)); + m_networkManager.cookieJar()->setCookiesFromUrl(cookies, url); + } // Process download request qDebug("url is %s", qPrintable(url)); const QUrl qurl = QUrl::fromEncoded(url.toUtf8()); diff --git a/src/downloadthread.h b/src/downloadthread.h index 3115c3588..23d5b1637 100644 --- a/src/downloadthread.h +++ b/src/downloadthread.h @@ -32,6 +32,7 @@ #define DOWNLOADTHREAD_H #include +#include #include #include #include @@ -45,8 +46,8 @@ class DownloadThread : public QObject { public: DownloadThread(QObject* parent = 0); - QNetworkReply* downloadUrl(const QString &url); - void downloadTorrentUrl(const QString &url); + QNetworkReply* downloadUrl(const QString &url, const QList& raw_cookies = QList()); + void downloadTorrentUrl(const QString &url, const QList& raw_cookies = QList()); //void setProxy(QString IP, int port, QString username, QString password); signals: @@ -63,9 +64,6 @@ private slots: private: QString errorCodeToString(QNetworkReply::NetworkError status); void applyProxySettings(); -#ifndef DISABLE_GUI - void loadCookies(const QString &host_name, QString url); -#endif private: QNetworkAccessManager m_networkManager; diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index e96245a66..4a91d146e 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -2613,7 +2613,8 @@ QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString f // Take an url string to a torrent file, // download the torrent file to a tmp location, then // add it to download list -void QBtSession::downloadFromUrl(const QString &url) { +void QBtSession::downloadFromUrl(const QString &url, const QList& cookies) +{ addConsoleMessage(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(url) #ifndef DISABLE_GUI , QPalette::WindowText @@ -2621,7 +2622,7 @@ void QBtSession::downloadFromUrl(const QString &url) { ); //emit aboutToDownloadFromUrl(url); // Launch downloader thread - downloader->downloadTorrentUrl(url); + downloader->downloadTorrentUrl(url, cookies); } void QBtSession::downloadFromURLList(const QStringList& urls) { diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index 78f3701a0..0a4b3765a 100644 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -41,6 +41,7 @@ #endif #include #include +#include #include #include @@ -107,7 +108,7 @@ public slots: QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); void loadSessionState(); void saveSessionState(); - void downloadFromUrl(const QString &url); + void downloadFromUrl(const QString &url, const QList& cookies = QList()); void deleteTorrent(const QString &hash, bool delete_local_files = false); void startUpTorrents(); void recheckTorrent(const QString &hash); diff --git a/src/rss/rss_imp.cpp b/src/rss/rss_imp.cpp index b669c52ce..d31cf6e36 100644 --- a/src/rss/rss_imp.cpp +++ b/src/rss/rss_imp.cpp @@ -124,13 +124,14 @@ void RSSImp::on_actionManage_cookies_triggered() { Q_ASSERT(!m_feedList->selectedItems().empty()); // Get feed hostname QString feed_url = m_feedList->getItemID(m_feedList->selectedItems().first()); - QString feed_hostname = QUrl::fromEncoded(feed_url.toLocal8Bit()).host(); + QString feed_hostname = QUrl::fromEncoded(feed_url.toUtf8()).host(); qDebug("RSS Feed hostname is: %s", qPrintable(feed_hostname)); Q_ASSERT(!feed_hostname.isEmpty()); bool ok = false; RssSettings settings; QList raw_cookies = CookiesDlg::askForCookies(this, settings.getHostNameCookies(feed_hostname), &ok); if (ok) { + qDebug() << "Settings cookies for host name: " << feed_hostname; settings.setHostNameCookies(feed_hostname, raw_cookies); } } @@ -325,10 +326,23 @@ void RSSImp::downloadTorrent() { foreach (const QListWidgetItem* item, selected_items) { RssArticlePtr article = m_feedList->getRSSItemFromUrl(item->data(Article::FeedUrlRole).toString()) ->getItem(item->data(Article::IdRole).toString()); + // Load possible cookies + QList cookies; + QString feed_url = m_feedList->getItemID(m_feedList->selectedItems().first()); + QString feed_hostname = QUrl::fromEncoded(feed_url.toUtf8()).host(); + const QList raw_cookies = RssSettings().getHostNameCookies(feed_hostname); + foreach (const QByteArray& raw_cookie, raw_cookies) { + QList 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()); + } + } + qDebug("Loaded %d cookies for RSS item\n", cookies.size()); if (article->hasAttachment()) { - QBtSession::instance()->downloadFromUrl(article->torrentUrl()); + QBtSession::instance()->downloadFromUrl(article->torrentUrl(), cookies); } else { - QBtSession::instance()->downloadFromUrl(article->link()); + QBtSession::instance()->downloadFromUrl(article->link(), cookies); } } }