2010-10-24 01:46:05 +04:00
|
|
|
/*
|
|
|
|
* 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"
|
2012-05-16 22:19:05 +04:00
|
|
|
#include "fs_utils.h"
|
2010-10-24 01:46:05 +04:00
|
|
|
#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
|
|
|
|
|
2010-11-23 00:55:32 +03:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2010-10-24 01:46:05 +04:00
|
|
|
ProgramUpdater::ProgramUpdater(QObject *parent) :
|
2010-12-04 22:14:14 +03:00
|
|
|
QObject(parent)
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
|
|
|
mp_manager = new QNetworkAccessManager(this);
|
2010-11-16 23:34:31 +03:00
|
|
|
Preferences pref;
|
2010-10-24 01:46:05 +04:00
|
|
|
// Proxy support
|
2012-02-20 21:30:53 +04:00
|
|
|
if (pref.isProxyEnabled()) {
|
2010-10-24 01:46:05 +04:00
|
|
|
QNetworkProxy proxy;
|
2010-11-23 00:55:32 +03:00
|
|
|
switch(pref.getProxyType()) {
|
2010-10-24 01:46:05 +04:00
|
|
|
case Proxy::SOCKS4:
|
|
|
|
case Proxy::SOCKS5:
|
|
|
|
case Proxy::SOCKS5_PW:
|
|
|
|
proxy.setType(QNetworkProxy::Socks5Proxy);
|
|
|
|
default:
|
|
|
|
proxy.setType(QNetworkProxy::HttpProxy);
|
|
|
|
break;
|
|
|
|
}
|
2010-11-23 00:55:32 +03:00
|
|
|
proxy.setHostName(pref.getProxyIp());
|
|
|
|
proxy.setPort(pref.getProxyPort());
|
2010-10-24 01:46:05 +04:00
|
|
|
// Proxy authentication
|
2012-02-20 21:30:53 +04:00
|
|
|
if (pref.isProxyAuthEnabled()) {
|
2010-11-22 23:27:57 +03:00
|
|
|
proxy.setUser(pref.getProxyUsername());
|
|
|
|
proxy.setPassword(pref.getProxyPassword());
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
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;
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!reply->error()) {
|
2010-10-24 01:46:05 +04:00
|
|
|
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()) {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (in_item && xml.name() == "title") {
|
2010-10-24 01:46:05 +04:00
|
|
|
in_title = false;
|
2012-05-16 22:19:05 +04:00
|
|
|
const QString ext = fsutils::fileExtension(item_title).toUpper();
|
2010-10-24 01:46:05 +04:00
|
|
|
qDebug("Found an update with file extension: %s", qPrintable(ext));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (ext == FILE_EXT) {
|
2010-10-24 01:46:05 +04:00
|
|
|
qDebug("The last update available is %s", qPrintable(item_title));
|
|
|
|
new_version = extractVersionNumber(item_title);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!new_version.isEmpty()) {
|
2010-10-24 01:46:05 +04:00
|
|
|
qDebug("Detected version is %s", qPrintable(new_version));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (isVersionMoreRecent(new_version))
|
2010-10-24 01:46:05 +04:00
|
|
|
setUpdateUrl(item_title);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (xml.name() == "item") {
|
|
|
|
in_item = false;
|
|
|
|
}
|
|
|
|
} else if (xml.isCharacters() && !xml.isWhitespace()) {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (in_item && in_title)
|
2010-10-24 01:46:05 +04:00
|
|
|
item_title += xml.text().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit updateCheckFinished(!m_updateUrl.isEmpty(), new_version);
|
|
|
|
// Clean up
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramUpdater::updateProgram()
|
|
|
|
{
|
|
|
|
Q_ASSERT(!m_updateUrl.isEmpty());
|
2010-12-04 22:14:14 +03:00
|
|
|
QDesktopServices::openUrl(m_updateUrl);
|
|
|
|
return;
|
|
|
|
/*connect(mp_manager, SIGNAL(finished(QNetworkReply*)),
|
2010-10-24 01:46:05 +04:00
|
|
|
this, SLOT(saveUpdate(QNetworkReply*)));
|
|
|
|
// Send the request
|
2010-12-04 22:14:14 +03:00
|
|
|
mp_manager->get(QNetworkRequest(QUrl(m_updateUrl)));*/
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
|
2010-12-04 22:14:14 +03:00
|
|
|
/*void ProgramUpdater::saveUpdate(QNetworkReply *reply)
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
|
|
|
// Disconnect SIGNAL/SLOT
|
|
|
|
disconnect(mp_manager, 0, this, 0);
|
|
|
|
// Process the download
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!reply->error()) {
|
2010-10-24 01:46:05 +04:00
|
|
|
// Save the file
|
|
|
|
const QString installer_path = QDir::temp().absoluteFilePath("qbittorrent_update."+FILE_EXT.toLower());
|
|
|
|
QFile update_installer(installer_path);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (update_installer.exists()) {
|
2010-10-24 01:46:05 +04:00
|
|
|
update_installer.remove();
|
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (update_installer.open(QIODevice::WriteOnly)) {
|
2010-10-24 01:46:05 +04:00
|
|
|
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();
|
2010-12-04 22:14:14 +03:00
|
|
|
}*/
|
2010-10-24 01:46:05 +04:00
|
|
|
|
2010-12-04 22:14:14 +03:00
|
|
|
/*void ProgramUpdater::installUpdate(QString update_path)
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
2010-12-04 22:14:14 +03:00
|
|
|
qDebug("Installing the update at %s...", qPrintable(update_path));
|
2010-10-24 01:46:05 +04:00
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
QDesktopServices::openUrl(QUrl(QString("file:///")+update_path, QUrl::TolerantMode));
|
|
|
|
#else
|
|
|
|
QDesktopServices::openUrl(QUrl(QString("file://")+update_path, QUrl::TolerantMode));
|
|
|
|
#endif
|
2010-12-04 22:14:14 +03:00
|
|
|
}*/
|
2010-10-24 01:46:05 +04:00
|
|
|
|
|
|
|
// 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
|
2011-12-29 03:26:00 +04:00
|
|
|
QString ProgramUpdater::extractVersionNumber(const QString& title) const
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
2011-12-29 03:26:00 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << title;
|
|
|
|
QRegExp regVer("qbittorrent[_-]([0-9.]+)(_setup)?(\\.exe|\\.dmg)");
|
|
|
|
if (regVer.indexIn(title) < 0) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Failed to extract version from file name:" << title;
|
2010-10-24 01:46:05 +04:00
|
|
|
return QString::null;
|
2011-12-29 03:26:00 +04:00
|
|
|
} else {
|
|
|
|
QString version = regVer.cap(1);
|
|
|
|
qDebug() << Q_FUNC_INFO << "Extracted version:" << version;
|
|
|
|
return version;
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-29 03:26:00 +04:00
|
|
|
bool ProgramUpdater::isVersionMoreRecent(const QString& remote_version) const
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
2011-12-29 03:26:00 +04:00
|
|
|
QRegExp regVer("([0-9.]+)");
|
|
|
|
if (regVer.indexIn(QString(VERSION)) >= 0) {
|
|
|
|
QString local_version = regVer.cap(1);
|
|
|
|
qDebug() << Q_FUNC_INFO << "local version:" << local_version << "/" << VERSION;
|
|
|
|
QStringList remote_parts = remote_version.split('.');
|
|
|
|
QStringList local_parts = local_version.split('.');
|
|
|
|
for (int i=0; i<qMin(remote_parts.size(), local_parts.size()); ++i) {
|
|
|
|
if (remote_parts[i].toInt() > local_parts[i].toInt())
|
|
|
|
return true;
|
|
|
|
if (remote_parts[i].toInt() < local_parts[i].toInt())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Compared parts were equal, if remote version is longer, then it's more recent (2.9.2.1 > 2.9.2)
|
|
|
|
if (remote_parts.size() > local_parts.size())
|
|
|
|
return true;
|
|
|
|
// versions are equal, check if the local version is a development release, in which case it is older (2.9.2beta < 2.9.2)
|
|
|
|
QRegExp regDevel("(alpha|beta|rc)");
|
|
|
|
if (regDevel.indexIn(VERSION) >= 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
|