Merge pull request #4413 from glassez/dlmgr

Improve Download Manager. Closes #4305
This commit is contained in:
sledgehammer999
2016-01-05 10:10:08 -06:00
13 changed files with 319 additions and 248 deletions

View File

@@ -28,23 +28,20 @@
* Contact : chris@qbittorrent.org
*/
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QXmlStreamReader>
#include <QNetworkProxy>
#include <QDesktopServices>
#include <QDebug>
#include <QRegExp>
#include <QStringList>
#include "programupdater.h"
#include "base/utils/fs.h"
#include "base/preferences.h"
#include "base/net/downloadmanager.h"
#include "base/net/downloadhandler.h"
#include "programupdater.h"
namespace
{
const QUrl RSS_URL("http://www.fosshub.com/software/feedqBittorent");
const QString RSS_URL("http://www.fosshub.com/software/feedqBittorent");
#ifdef Q_OS_MAC
const QString OS_TYPE("Mac OS X");
@@ -59,99 +56,73 @@ ProgramUpdater::ProgramUpdater(QObject *parent, bool invokedByUser)
: QObject(parent)
, m_invokedByUser(invokedByUser)
{
m_networkManager = new QNetworkAccessManager(this);
Preferences* const pref = Preferences::instance();
// Proxy support
if (pref->isProxyEnabled()) {
QNetworkProxy proxy;
switch(pref->getProxyType()) {
case Proxy::SOCKS4:
case Proxy::SOCKS5:
case Proxy::SOCKS5_PW:
proxy.setType(QNetworkProxy::Socks5Proxy);
default:
proxy.setType(QNetworkProxy::HttpProxy);
break;
}
proxy.setHostName(pref->getProxyIp());
proxy.setPort(pref->getProxyPort());
// Proxy authentication
if (pref->isProxyAuthEnabled()) {
proxy.setUser(pref->getProxyUsername());
proxy.setPassword(pref->getProxyPassword());
}
m_networkManager->setProxy(proxy);
}
}
ProgramUpdater::~ProgramUpdater()
{
delete m_networkManager;
}
void ProgramUpdater::checkForUpdates()
{
// SIGNAL/SLOT
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(rssDownloadFinished(QNetworkReply*)));
// Send the request
QNetworkRequest request(RSS_URL);
// Don't change this User-Agent. In case our updater goes haywire, the filehost can indetify it and contact us.
request.setRawHeader("User-Agent", QString("qBittorrent/%1 ProgramUpdater (www.qbittorrent.org)").arg(VERSION).toLocal8Bit());
m_networkManager->get(request);
Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(
RSS_URL, false, 0, false,
// Don't change this User-Agent. In case our updater goes haywire,
// the filehost can identify it and contact us.
QString("qBittorrent/%1 ProgramUpdater (www.qbittorrent.org)").arg(VERSION));
connect(handler, SIGNAL(downloadFinished(QString,QByteArray)), SLOT(rssDownloadFinished(QString,QByteArray)));
connect(handler, SIGNAL(downloadFailed(QString,QString)), SLOT(rssDownloadFailed(QString,QString)));
}
void ProgramUpdater::rssDownloadFinished(QNetworkReply *reply)
void ProgramUpdater::rssDownloadFinished(const QString &url, const QByteArray &data)
{
// Disconnect SIGNAL/SLOT
disconnect(m_networkManager, 0, this, 0);
Q_UNUSED(url);
qDebug("Finished downloading the new qBittorrent updates RSS");
QString version;
if (!reply->error()) {
qDebug("No download error, good.");
QXmlStreamReader xml(reply);
bool inItem = false;
QString updateLink;
QString type;
QXmlStreamReader xml(data);
bool inItem = false;
QString updateLink;
QString type;
while (!xml.atEnd()) {
xml.readNext();
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == "item")
inItem = true;
else if (inItem && xml.name() == "link")
updateLink = getStringValue(xml);
else if (inItem && xml.name() == "type")
type = getStringValue(xml);
else if (inItem && xml.name() == "version")
version = getStringValue(xml);
}
else if (xml.isEndElement()) {
if (inItem && xml.name() == "item") {
if (type.compare(OS_TYPE, Qt::CaseInsensitive) == 0) {
qDebug("The last update available is %s", qPrintable(version));
if (!version.isEmpty()) {
qDebug("Detected version is %s", qPrintable(version));
if (isVersionMoreRecent(version))
m_updateUrl = updateLink;
}
break;
if (xml.isStartElement()) {
if (xml.name() == "item")
inItem = true;
else if (inItem && xml.name() == "link")
updateLink = getStringValue(xml);
else if (inItem && xml.name() == "type")
type = getStringValue(xml);
else if (inItem && xml.name() == "version")
version = getStringValue(xml);
}
else if (xml.isEndElement()) {
if (inItem && xml.name() == "item") {
if (type.compare(OS_TYPE, Qt::CaseInsensitive) == 0) {
qDebug("The last update available is %s", qPrintable(version));
if (!version.isEmpty()) {
qDebug("Detected version is %s", qPrintable(version));
if (isVersionMoreRecent(version))
m_updateUrl = updateLink;
}
inItem = false;
updateLink.clear();
type.clear();
version.clear();
break;
}
inItem = false;
updateLink.clear();
type.clear();
version.clear();
}
}
}
emit updateCheckFinished(!m_updateUrl.isEmpty(), version, m_invokedByUser);
// Clean up
reply->deleteLater();
}
void ProgramUpdater::rssDownloadFailed(const QString &url, const QString &error)
{
Q_UNUSED(url);
qDebug() << "Downloading the new qBittorrent updates RSS failed:" << error;
emit updateCheckFinished(false, QString(), m_invokedByUser);
}
void ProgramUpdater::updateProgram()

View File

@@ -34,30 +34,27 @@
#include <QObject>
#include <QUrl>
class QNetworkReply;
class QNetworkAccessManager;
class ProgramUpdater: public QObject
{
Q_OBJECT
public:
explicit ProgramUpdater(QObject *parent = 0, bool invokedByUser = false);
~ProgramUpdater();
void checkForUpdates();
void updateProgram();
protected:
bool isVersionMoreRecent(const QString &remoteVersion) const;
protected slots:
void rssDownloadFinished(QNetworkReply* reply);
signals:
void updateCheckFinished(bool updateAvailable, QString version, bool invokedByUser);
private slots:
void rssDownloadFinished(const QString &url, const QByteArray &data);
void rssDownloadFailed(const QString &url, const QString &error);
private:
bool isVersionMoreRecent(const QString &remoteVersion) const;
QString m_updateUrl;
QNetworkAccessManager *m_networkManager;
bool m_invokedByUser;
};

View File

@@ -31,12 +31,14 @@
#include "cookiesdlg.h"
#include "ui_cookiesdlg.h"
#include "guiiconprovider.h"
#include "base/net/downloadmanager.h"
#include <QNetworkCookie>
#include <QDateTime>
enum CookiesCols { COOKIE_KEY, COOKIE_VALUE};
CookiesDlg::CookiesDlg(QWidget *parent, const QList<QByteArray> &raw_cookies) :
CookiesDlg::CookiesDlg(const QUrl &url, QWidget *parent) :
QDialog(parent),
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->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('=');
if (cookie_parts.size() != 2) continue;
QList<QNetworkCookie> cookies = Net::DownloadManager::instance()->cookiesForUrl(url);
foreach (const QNetworkCookie &cookie, cookies) {
const int i = ui->cookiesTable->rowCount();
ui->cookiesTable->setRowCount(i+1);
ui->cookiesTable->setItem(i, COOKIE_KEY, new QTableWidgetItem(cookie_parts.first().data()));
ui->cookiesTable->setItem(i, COOKIE_VALUE, new QTableWidgetItem(cookie_parts.last().data()));
ui->cookiesTable->setItem(i, COOKIE_KEY, new QTableWidgetItem(QString(cookie.name())));
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<QByteArray> ret;
QList<QNetworkCookie> CookiesDlg::getCookies() const {
QList<QNetworkCookie> ret;
auto now = QDateTime::currentDateTime();
for (int i=0; i<ui->cookiesTable->rowCount(); ++i) {
QString key;
if (ui->cookiesTable->item(i, COOKIE_KEY))
@@ -85,20 +88,23 @@ QList<QByteArray> CookiesDlg::getCookies() const {
if (ui->cookiesTable->item(i, COOKIE_VALUE))
value = ui->cookiesTable->item(i, COOKIE_VALUE)->text().trimmed();
if (!key.isEmpty() && !value.isEmpty()) {
const QString raw_cookie = key+"="+value;
qDebug("Cookie: %s", qPrintable(raw_cookie));
ret << raw_cookie.toLocal8Bit();
QNetworkCookie cookie(key.toUtf8(), value.toUtf8());
// TODO: Delete this hack when advanced Cookie dialog will be implemented.
cookie.setExpirationDate(now.addYears(10));
qDebug("Cookie: %s", cookie.toRawForm().data());
ret << cookie;
}
}
return ret;
}
QList<QByteArray> CookiesDlg::askForCookies(QWidget *parent, const QList<QByteArray> &raw_cookies, bool *ok) {
CookiesDlg dlg(parent, raw_cookies);
bool CookiesDlg::askForCookies(QWidget *parent, const QUrl &url, QList<QNetworkCookie> &out)
{
CookiesDlg dlg(url, parent);
if (dlg.exec()) {
*ok = true;
return dlg.getCookies();
out = dlg.getCookies();
return true;
}
*ok = false;
return QList<QByteArray>();
return false;
}

View File

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

View File

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