Browse Source

FEATURE: qBittorrent can update itself from Sourceforge (Windows/Mac OS X only)

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
166dad51d5
  1. 1
      Changelog
  2. 37
      src/GUI.cpp
  3. 4
      src/GUI.h
  4. 4
      src/downloadthread.cpp
  5. 37
      src/options_imp.cpp
  6. 2
      src/options_imp.h
  7. 3
      src/preferences.h
  8. 233
      src/programupdater.cpp
  9. 68
      src/programupdater.h
  10. 19
      src/qtlibtorrent/qbtsession.cpp
  11. 18
      src/src.pro

1
Changelog

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
- FEATURE: qBittorrent can now act as a tracker
- FEATURE: Added feature to shutdown qbittorrent on torrents completion
- FEATURE: Added a torrent import assistant to seed or keep downloading outside torrents
- FEATURE: qBittorrent can update itself from Sourceforge (Windows/Mac OS X only)
- FEATURE: Added a transfer list column to display the current tracker
- COSMETIC: Replaced message box by on-screen notification for download errors

37
src/GUI.cpp

@ -74,6 +74,9 @@ void qt_mac_set_dock_menu(QMenu *menu); @@ -74,6 +74,9 @@ void qt_mac_set_dock_menu(QMenu *menu);
#endif
#include "lineedit.h"
#include "sessionapplication.h"
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
#include "programupdater.h"
#endif
using namespace libtorrent;
@ -246,6 +249,12 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), for @@ -246,6 +249,12 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), for
#ifdef Q_WS_MAC
qt_mac_set_dock_menu(getTrayIconMenu());
#endif
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
// Check for update
ProgramUpdater *updater = new ProgramUpdater(this);
connect(updater, SIGNAL(updateCheckFinished(bool, QString)), SLOT(handleUpdateCheckFinished(bool, QString)));
updater->checkForUpdates();
#endif
}
void GUI::deleteBTSession() {
@ -533,7 +542,7 @@ void GUI::askRecursiveTorrentDownloadConfirmation(QTorrentHandle &h) { @@ -533,7 +542,7 @@ void GUI::askRecursiveTorrentDownloadConfirmation(QTorrentHandle &h) {
if(confirmBox.clickedButton() == never) {
Preferences::disableRecursiveDownload();
}
}
}
void GUI::handleDownloadFromUrlFailure(QString url, QString reason) const{
// Display a message box
@ -1210,3 +1219,29 @@ void GUI::on_actionDownload_from_URL_triggered() { @@ -1210,3 +1219,29 @@ void GUI::on_actionDownload_from_URL_triggered() {
}
}
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
void GUI::handleUpdateCheckFinished(bool update_available, QString new_version)
{
if(update_available) {
if(QMessageBox::question(this, tr("A newer version is available"),
tr("A newer version of qBittorrent is available on Sourceforge.\nWould you like to update qBittorrent to version %1?").arg(new_version),
QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) {
// The user want to update, let's download the update
ProgramUpdater* updater = dynamic_cast<ProgramUpdater*>(sender());
connect(updater, SIGNAL(updateInstallFinished(QString)), SLOT(handleUpdateInstalled(QString)));
updater->updateProgram();
return;
}
}
sender()->deleteLater();
}
void GUI::handleUpdateInstalled(QString error_msg)
{
if(!error_msg.isEmpty()) {
QMessageBox::critical(this, tr("Impossible to update qBittorrent"), tr("qBittorrent failed to update, reason: %1").arg(error_msg));
}
}
#endif

4
src/GUI.h

@ -129,6 +129,10 @@ protected slots: @@ -129,6 +129,10 @@ protected slots:
void optionsSaved();
// HTTP slots
void on_actionDownload_from_URL_triggered();
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
void handleUpdateCheckFinished(bool update_available, QString new_version);
void handleUpdateInstalled(QString error_msg);
#endif
protected:
void closeEvent(QCloseEvent *);

4
src/downloadthread.cpp

@ -39,8 +39,6 @@ @@ -39,8 +39,6 @@
#include "preferences.h"
#include "qinisettings.h"
enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4, SOCKS4=5};
/** Download Thread **/
downloadThread::downloadThread(QObject* parent) : QObject(parent) {
@ -180,7 +178,7 @@ void downloadThread::applyProxySettings() { @@ -180,7 +178,7 @@ void downloadThread::applyProxySettings() {
qDebug("Using proxy: %s", qPrintable(IP));
proxy.setPort(port.toUShort());
// Default proxy type is HTTP, we must change if it is SOCKS5
if(intValue == SOCKS5 || intValue == SOCKS5_PW) {
if(intValue == Proxy::SOCKS5 || intValue == Proxy::SOCKS5_PW) {
qDebug("Proxy is SOCKS5, not HTTP");
proxy.setType(QNetworkProxy::Socks5Proxy);
} else {

37
src/options_imp.cpp

@ -56,7 +56,6 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){ @@ -56,7 +56,6 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){
setAttribute(Qt::WA_DeleteOnClose);
setModal(true);
QString savePath;
setupUi(this);
hsplitter->setCollapsible(0, false);
hsplitter->setCollapsible(1, false);
@ -516,18 +515,18 @@ bool options_imp::isFilteringEnabled() const{ @@ -516,18 +515,18 @@ bool options_imp::isFilteringEnabled() const{
int options_imp::getPeerProxyType() const{
switch(comboProxyType->currentIndex()) {
case 1:
return SOCKS4;
return Proxy::SOCKS4;
break;
case 2:
if(isPeerProxyAuthEnabled()){
return SOCKS5_PW;
return Proxy::SOCKS5_PW;
}
return SOCKS5;
return Proxy::SOCKS5;
case 3:
if(isPeerProxyAuthEnabled()){
return HTTP_PW;
return Proxy::HTTP_PW;
}
return HTTP;
return Proxy::HTTP;
default:
return -1;
}
@ -537,15 +536,15 @@ int options_imp::getHTTPProxyType() const { @@ -537,15 +536,15 @@ int options_imp::getHTTPProxyType() const {
switch(comboProxyType_http->currentIndex()) {
case 1: {
if(isHTTPProxyAuthEnabled()){
return HTTP_PW;
return Proxy::HTTP_PW;
}
return HTTP;
return Proxy::HTTP;
}
case 2: {
if(isHTTPProxyAuthEnabled()) {
return SOCKS5_PW;
return Proxy::SOCKS5_PW;
}
return SOCKS5;
return Proxy::SOCKS5;
}
default:
return -1; // Disabled
@ -672,15 +671,15 @@ void options_imp::loadOptions(){ @@ -672,15 +671,15 @@ void options_imp::loadOptions(){
intValue = Preferences::getPeerProxyType();
switch(intValue) {
case SOCKS4:
case Proxy::SOCKS4:
comboProxyType->setCurrentIndex(1);
break;
case SOCKS5:
case SOCKS5_PW:
case Proxy::SOCKS5:
case Proxy::SOCKS5_PW:
comboProxyType->setCurrentIndex(2);
break;
case HTTP:
case HTTP_PW:
case Proxy::HTTP:
case Proxy::HTTP_PW:
comboProxyType->setCurrentIndex(3);
break;
default:
@ -698,12 +697,12 @@ void options_imp::loadOptions(){ @@ -698,12 +697,12 @@ void options_imp::loadOptions(){
//}
intValue = Preferences::getHTTPProxyType();
switch(intValue) {
case HTTP:
case HTTP_PW:
case Proxy::HTTP:
case Proxy::HTTP_PW:
comboProxyType_http->setCurrentIndex(1);
break;
case SOCKS5:
case SOCKS5_PW:
case Proxy::SOCKS5:
case Proxy::SOCKS5_PW:
comboProxyType_http->setCurrentIndex(2);
break;
default:

2
src/options_imp.h

@ -34,8 +34,6 @@ @@ -34,8 +34,6 @@
#include "ui_options.h"
#include <libtorrent/ip_filter.hpp>
enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4, SOCKS4=5};
// actions on double-click on torrents
enum DoubleClickAction {TOGGLE_PAUSE, OPEN_DEST, NO_ACTION};

3
src/preferences.h

@ -54,6 +54,9 @@ @@ -54,6 +54,9 @@
#define QBT_REALM "Web UI Access"
enum scheduler_days { EVERY_DAY, WEEK_DAYS, WEEK_ENDS, MON, TUE, WED, THU, FRI, SAT, SUN };
enum maxRatioAction {PAUSE_ACTION, REMOVE_ACTION};
namespace Proxy {
enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4, SOCKS4=5};
}
class Preferences {
public:

233
src/programupdater.cpp

@ -0,0 +1,233 @@ @@ -0,0 +1,233 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2010 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QXmlStreamReader>
#include <QNetworkProxy>
#include <QDesktopServices>
#include "programupdater.h"
#include "misc.h"
#include "preferences.h"
#ifdef Q_WS_MAC
const QString RSS_URL = "http://sourceforge.net/api/file/index/project-id/163414/mtime/desc/rss?path=/qbittorrent-mac";
const QString FILE_EXT = "DMG";
#else
const QString RSS_URL = "http://sourceforge.net/api/file/index/project-id/163414/mtime/desc/rss?path=/qbittorrent-win32";
const QString FILE_EXT = "EXE";
#endif
ProgramUpdater::ProgramUpdater(QObject *parent) :
QObject(parent)
{
mp_manager = new QNetworkAccessManager(this);
// Proxy support
if(Preferences::isHTTPProxyEnabled()) {
QNetworkProxy proxy;
switch(Preferences::getHTTPProxyType()) {
case Proxy::SOCKS4:
case Proxy::SOCKS5:
case Proxy::SOCKS5_PW:
proxy.setType(QNetworkProxy::Socks5Proxy);
default:
proxy.setType(QNetworkProxy::HttpProxy);
break;
}
proxy.setHostName(Preferences::getHTTPProxyIp());
proxy.setPort(Preferences::getHTTPProxyPort());
// Proxy authentication
if(Preferences::isHTTPProxyAuthEnabled()) {
proxy.setUser(Preferences::getHTTPProxyUsername());
proxy.setPassword(Preferences::getHTTPProxyPassword());
}
mp_manager->setProxy(proxy);
}
}
ProgramUpdater::~ProgramUpdater() {
delete mp_manager;
}
void ProgramUpdater::checkForUpdates()
{
// SIGNAL/SLOT
connect(mp_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(rssDownloadFinished(QNetworkReply*)));
// Send the request
mp_manager->get(QNetworkRequest(QUrl(RSS_URL)));
}
void ProgramUpdater::setUpdateUrl(QString title) {
m_updateUrl = "http://downloads.sourceforge.net/project/qbittorrent"+title;
qDebug("The Update URL is %s", qPrintable(m_updateUrl));
}
void ProgramUpdater::rssDownloadFinished(QNetworkReply *reply)
{
// Disconnect SIGNAL/SLOT
disconnect(mp_manager, 0, this, 0);
qDebug("Finished downloading the new qBittorrent updates RSS");
QString new_version;
if(!reply->error()) {
qDebug("No download error, good.");
QXmlStreamReader xml(reply);
QString item_title;
bool in_title = false;
bool in_item = false;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (in_item && xml.name() == "title") {
in_title = true;
item_title = "";
} else if (xml.name() == "item") {
in_item = true;
}
} else if (xml.isEndElement()) {
if(in_item && xml.name() == "title") {
in_title = false;
const QString ext = misc::file_extension(item_title).toUpper();
qDebug("Found an update with file extension: %s", qPrintable(ext));
if(ext == FILE_EXT) {
qDebug("The last update available is %s", qPrintable(item_title));
new_version = extractVersionNumber(item_title);
if(!new_version.isEmpty()) {
qDebug("Detected version is %s", qPrintable(new_version));
if(isVersionMoreRecent(new_version))
setUpdateUrl(item_title);
}
break;
}
} else if (xml.name() == "item") {
in_item = false;
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
if(in_item && in_title)
item_title += xml.text().toString();
}
}
}
emit updateCheckFinished(!m_updateUrl.isEmpty(), new_version);
// Clean up
reply->deleteLater();
}
void ProgramUpdater::updateProgram()
{
Q_ASSERT(!m_updateUrl.isEmpty());
connect(mp_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(saveUpdate(QNetworkReply*)));
// Send the request
mp_manager->get(QNetworkRequest(QUrl(m_updateUrl)));
}
void ProgramUpdater::saveUpdate(QNetworkReply *reply)
{
// Disconnect SIGNAL/SLOT
disconnect(mp_manager, 0, this, 0);
// Process the download
if(!reply->error()) {
// Save the file
const QString installer_path = QDir::temp().absoluteFilePath("qbittorrent_update."+FILE_EXT.toLower());
QFile update_installer(installer_path);
if(update_installer.exists()) {
update_installer.remove();
}
if(update_installer.open(QIODevice::WriteOnly)) {
update_installer.write(reply->readAll());
reply->close();
update_installer.close();
// Install the update
installUpdate(installer_path);
} else {
emit updateInstallFinished(tr("Could not create the file %1").arg(installer_path));
}
} else {
emit updateInstallFinished(tr("Failed to download the update at %1", "%1 is an URL").arg(m_updateUrl));
}
reply->deleteLater();
deleteLater();
}
void ProgramUpdater::installUpdate(QString update_path)
{
qDebug("Installing the update...");
#ifdef Q_WS_WIN
QDesktopServices::openUrl(QUrl(QString("file:///")+update_path, QUrl::TolerantMode));
#else
QDesktopServices::openUrl(QUrl(QString("file://")+update_path, QUrl::TolerantMode));
#endif
}
// title on Windows: /qbittorrent-win32/qbittorrent-2.4.7/qbittorrent_2.4.7_setup.exe
// title on Mac: /qbittorrent-mac/qbittorrent-2.4.4/qbittorrent-2.4.4.dmg
QString ProgramUpdater::extractVersionNumber(QString title) const
{
QString version;
QStringList parts = title.split("/");
if(parts.size() != 4) {
qDebug("ProgramUpdater: Unrecognized title: %s", qPrintable(title));
return version;
}
QString folder = parts.at(2);
if(!folder.contains("-")) {
qDebug("ProgramUpdater: Unrecognized folder name: %s", qPrintable(folder));
return version;
}
version = folder.mid(folder.lastIndexOf("-")+1);
if(version.split(".").size() != 3) {
qDebug("ProgramUpdater: Unrecognized version format: %s", qPrintable(version));
return QString::null;
}
return version;
}
bool ProgramUpdater::isVersionMoreRecent(QString new_version) const
{
/*DEFINES += VERSION_MAJOR=2
DEFINES += VERSION_MINOR=5
DEFINES += VERSION_BUGFIX=0*/
const QStringList parts = new_version.split(".");
Q_ASSERT(parts.size() == 3);
const int major = parts.at(0).toInt();
const int minor = parts.at(1).toInt();
const int bugfix = parts.at(2).toInt();
if(major < VERSION_MAJOR)
return false;
if(minor < VERSION_MINOR)
return false;
if(bugfix <= VERSION_BUGFIX)
return false;
return true;
}

68
src/programupdater.h

@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2010 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#ifndef PROGRAMUPDATER_H
#define PROGRAMUPDATER_H
#include <QObject>
#include <QUrl>
class QNetworkReply;
class QNetworkAccessManager;
class ProgramUpdater : public QObject
{
Q_OBJECT
public:
explicit ProgramUpdater(QObject *parent = 0);
~ProgramUpdater();
void checkForUpdates();
void updateProgram();
protected:
QString extractVersionNumber(QString title) const;
bool isVersionMoreRecent(QString new_version) const;
protected slots:
void rssDownloadFinished(QNetworkReply* reply);
void installUpdate(QString update_path);
void saveUpdate(QNetworkReply* reply);
void setUpdateUrl(QString title);
signals:
void updateCheckFinished(bool update_available, QString version);
void updateInstallFinished(QString error);
private:
QString m_updateUrl;
QNetworkAccessManager *mp_manager;
};
#endif // PROGRAMUPDATER_H

19
src/qtlibtorrent/qbtsession.cpp

@ -70,7 +70,6 @@ @@ -70,7 +70,6 @@
const int MAX_TRACKER_ERRORS = 2;
const float MAX_RATIO = 100.;
enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4, SOCKS4=5};
enum VersionType { NORMAL,ALPHA,BETA,RELEASE_CANDIDATE,DEVEL };
// Main constructor
@ -543,21 +542,21 @@ void QBtSession::configureSession() { @@ -543,21 +542,21 @@ void QBtSession::configureSession() {
}
}
switch(Preferences::getPeerProxyType()) {
case HTTP:
case Proxy::HTTP:
qDebug("type: http");
proxySettings.type = proxy_settings::http;
break;
case HTTP_PW:
case Proxy::HTTP_PW:
qDebug("type: http_pw");
proxySettings.type = proxy_settings::http_pw;
break;
case SOCKS4:
case Proxy::SOCKS4:
proxySettings.type = proxy_settings::socks4;
case SOCKS5:
case Proxy::SOCKS5:
qDebug("type: socks5");
proxySettings.type = proxy_settings::socks5;
break;
case SOCKS5_PW:
case Proxy::SOCKS5_PW:
qDebug("type: socks5_pw");
proxySettings.type = proxy_settings::socks5_pw;
break;
@ -569,24 +568,24 @@ void QBtSession::configureSession() { @@ -569,24 +568,24 @@ void QBtSession::configureSession() {
proxy_settings http_proxySettings;
qDebug("HTTP Communications proxy type: %d", Preferences::getHTTPProxyType());
switch(Preferences::getHTTPProxyType()) {
case HTTP_PW:
case Proxy::HTTP_PW:
http_proxySettings.type = proxy_settings::http_pw;
http_proxySettings.username = Preferences::getHTTPProxyUsername().toStdString();
http_proxySettings.password = Preferences::getHTTPProxyPassword().toStdString();
http_proxySettings.hostname = Preferences::getHTTPProxyIp().toStdString();
http_proxySettings.port = Preferences::getHTTPProxyPort();
break;
case HTTP:
case Proxy::HTTP:
http_proxySettings.type = proxy_settings::http;
http_proxySettings.hostname = Preferences::getHTTPProxyIp().toStdString();
http_proxySettings.port = Preferences::getHTTPProxyPort();
break;
case SOCKS5:
case Proxy::SOCKS5:
http_proxySettings.type = proxy_settings::socks5;
http_proxySettings.hostname = Preferences::getHTTPProxyIp().toStdString();
http_proxySettings.port = Preferences::getHTTPProxyPort();
break;
case SOCKS5_PW:
case Proxy::SOCKS5_PW:
http_proxySettings.type = proxy_settings::socks5_pw;
http_proxySettings.username = Preferences::getHTTPProxyUsername().toStdString();
http_proxySettings.password = Preferences::getHTTPProxyPassword().toStdString();

18
src/src.pro

@ -12,9 +12,9 @@ CONFIG += qt \ @@ -12,9 +12,9 @@ CONFIG += qt \
# Update this VERSION for each release
os2 {
DEFINES += VERSION=\'\"v2.5.0beta1\"\'
DEFINES += VERSION=\'\"v2.5.0beta2\"\'
} else {
DEFINES += VERSION=\\\"v2.5.0beta1\\\"
DEFINES += VERSION=\\\"v2.5.0beta2\\\"
}
DEFINES += VERSION_MAJOR=2
DEFINES += VERSION_MINOR=5
@ -312,8 +312,13 @@ contains(DEFINES, DISABLE_GUI) { @@ -312,8 +312,13 @@ contains(DEFINES, DISABLE_GUI) {
torrentcreatordlg.h \
torrentimportdlg.h
win32 {
HEADERS += programupdater.h
}
macx {
HEADERS += qmacapplication.h
HEADERS += qmacapplication.h \
programupdater.h
}
}
@ -382,8 +387,13 @@ SOURCES += main.cpp \ @@ -382,8 +387,13 @@ SOURCES += main.cpp \
torrentcreatordlg.cpp \
torrentimportdlg.cpp
win32 {
SOURCES += programupdater.cpp
}
macx {
SOURCES += qmacapplication.cpp
SOURCES += qmacapplication.cpp \
programupdater.cpp
}
}

Loading…
Cancel
Save