Browse Source

RSS: Better cookies support

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
d55f3b5aff
  1. 37
      src/downloadthread.cpp
  2. 8
      src/downloadthread.h
  3. 5
      src/qtlibtorrent/qbtsession.cpp
  4. 3
      src/qtlibtorrent/qbtsession.h
  5. 20
      src/rss/rss_imp.cpp

37
src/downloadthread.cpp

@ -32,7 +32,6 @@ @@ -32,7 +32,6 @@
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkProxy>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include "downloadthread.h"
@ -109,39 +108,21 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) { @@ -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<QByteArray> raw_cookies = RssSettings().getHostNameCookies(host_name);
QNetworkCookieJar *cookie_jar = m_networkManager.cookieJar();
QList<QNetworkCookie> cookies;
qDebug("Loading cookies for host name: %s", qPrintable(host_name));
foreach (const QByteArray& raw_cookie, raw_cookies) {
QList<QByteArray> 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<QNetworkCookie>& 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<QNetworkCookie>& 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());

8
src/downloadthread.h

@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
#define DOWNLOADTHREAD_H
#include <QNetworkReply>
#include <QNetworkCookie>
#include <QObject>
#include <QHash>
#include <QSslError>
@ -45,8 +46,8 @@ class DownloadThread : public QObject { @@ -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<QNetworkCookie>& raw_cookies = QList<QNetworkCookie>());
void downloadTorrentUrl(const QString &url, const QList<QNetworkCookie>& raw_cookies = QList<QNetworkCookie>());
//void setProxy(QString IP, int port, QString username, QString password);
signals:
@ -63,9 +64,6 @@ private slots: @@ -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;

5
src/qtlibtorrent/qbtsession.cpp

@ -2613,7 +2613,8 @@ QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString f @@ -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<QNetworkCookie>& 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) { @@ -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) {

3
src/qtlibtorrent/qbtsession.h

@ -41,6 +41,7 @@ @@ -41,6 +41,7 @@
#endif
#include <QPointer>
#include <QTimer>
#include <QNetworkCookie>
#include <libtorrent/version.hpp>
#include <libtorrent/session.hpp>
@ -107,7 +108,7 @@ public slots: @@ -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<QNetworkCookie>& cookies = QList<QNetworkCookie>());
void deleteTorrent(const QString &hash, bool delete_local_files = false);
void startUpTorrents();
void recheckTorrent(const QString &hash);

20
src/rss/rss_imp.cpp

@ -124,13 +124,14 @@ void RSSImp::on_actionManage_cookies_triggered() { @@ -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<QByteArray> 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() { @@ -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<QNetworkCookie> cookies;
QString feed_url = m_feedList->getItemID(m_feedList->selectedItems().first());
QString feed_hostname = QUrl::fromEncoded(feed_url.toUtf8()).host();
const QList<QByteArray> raw_cookies = RssSettings().getHostNameCookies(feed_hostname);
foreach (const QByteArray& raw_cookie, raw_cookies) {
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());
}
}
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);
}
}
}

Loading…
Cancel
Save