2010-10-24 01:46:05 +04:00
|
|
|
/*
|
2018-06-06 16:48:17 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2018-05-24 18:39:02 +03:00
|
|
|
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
|
2010-10-24 01:46:05 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-05-24 18:39:02 +03:00
|
|
|
#include "programupdater.h"
|
|
|
|
|
2019-07-25 20:21:53 +03:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <versionhelpers.h> // must follow after Windows.h
|
|
|
|
#endif
|
|
|
|
|
2014-07-05 16:44:13 +04:00
|
|
|
#include <QDebug>
|
2018-05-24 18:39:02 +03:00
|
|
|
#include <QDesktopServices>
|
2018-05-24 18:41:03 +03:00
|
|
|
#include <QRegularExpression>
|
2014-07-05 16:44:13 +04:00
|
|
|
#include <QStringList>
|
2018-05-24 18:39:02 +03:00
|
|
|
#include <QXmlStreamReader>
|
2010-10-24 01:46:05 +04:00
|
|
|
|
2019-07-25 20:21:53 +03:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
#include <QSysInfo>
|
|
|
|
#endif
|
|
|
|
|
2018-05-24 18:39:02 +03:00
|
|
|
#include "base/net/downloadmanager.h"
|
2010-10-24 01:46:05 +04:00
|
|
|
|
2015-12-06 16:19:00 +03:00
|
|
|
namespace
|
|
|
|
{
|
2018-11-11 19:30:00 +03:00
|
|
|
const QString RSS_URL {QStringLiteral("https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml")};
|
2015-12-06 16:19:00 +03:00
|
|
|
|
|
|
|
QString getStringValue(QXmlStreamReader &xml);
|
|
|
|
}
|
|
|
|
|
2015-12-06 14:14:07 +03:00
|
|
|
ProgramUpdater::ProgramUpdater(QObject *parent, bool invokedByUser)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_invokedByUser(invokedByUser)
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramUpdater::checkForUpdates()
|
|
|
|
{
|
2018-06-25 20:31:32 +03:00
|
|
|
// Don't change this User-Agent. In case our updater goes haywire,
|
|
|
|
// the filehost can identify it and contact us.
|
2019-03-03 12:43:42 +03:00
|
|
|
Net::DownloadManager::instance()->download(
|
|
|
|
Net::DownloadRequest(RSS_URL).userAgent("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)")
|
|
|
|
, this, &ProgramUpdater::rssDownloadFinished);
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
|
2019-03-01 10:38:16 +03:00
|
|
|
void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
2019-03-01 10:38:16 +03:00
|
|
|
|
|
|
|
if (result.status != Net::DownloadStatus::Success) {
|
|
|
|
qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
|
|
|
|
emit updateCheckFinished(false, QString(), m_invokedByUser);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-06 14:14:07 +03:00
|
|
|
qDebug("Finished downloading the new qBittorrent updates RSS");
|
2015-12-06 16:19:00 +03:00
|
|
|
|
2018-08-04 17:22:35 +03:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
const QString OS_TYPE {"Mac OS X"};
|
|
|
|
#elif defined(Q_OS_WIN)
|
2019-07-25 20:21:53 +03:00
|
|
|
const QString OS_TYPE {(::IsWindows7OrGreater()
|
2018-08-04 17:22:35 +03:00
|
|
|
&& QSysInfo::currentCpuArchitecture().endsWith("64"))
|
|
|
|
? "Windows x64" : "Windows"};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QString version;
|
2019-03-01 10:38:16 +03:00
|
|
|
QXmlStreamReader xml(result.data);
|
2015-12-19 09:48:46 +03:00
|
|
|
bool inItem = false;
|
|
|
|
QString updateLink;
|
|
|
|
QString type;
|
|
|
|
|
|
|
|
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) {
|
2017-08-13 13:56:03 +03:00
|
|
|
qDebug("The last update available is %s", qUtf8Printable(version));
|
2015-12-19 09:48:46 +03:00
|
|
|
if (!version.isEmpty()) {
|
2017-08-13 13:56:03 +03:00
|
|
|
qDebug("Detected version is %s", qUtf8Printable(version));
|
2015-12-19 09:48:46 +03:00
|
|
|
if (isVersionMoreRecent(version))
|
|
|
|
m_updateUrl = updateLink;
|
2015-12-06 14:14:07 +03:00
|
|
|
}
|
2015-12-19 09:48:46 +03:00
|
|
|
break;
|
2015-12-06 14:14:07 +03:00
|
|
|
}
|
2015-12-19 09:48:46 +03:00
|
|
|
|
|
|
|
inItem = false;
|
|
|
|
updateLink.clear();
|
|
|
|
type.clear();
|
|
|
|
version.clear();
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 16:19:00 +03:00
|
|
|
|
|
|
|
emit updateCheckFinished(!m_updateUrl.isEmpty(), version, m_invokedByUser);
|
2015-12-19 09:48:46 +03:00
|
|
|
}
|
|
|
|
|
2010-10-24 01:46:05 +04:00
|
|
|
void ProgramUpdater::updateProgram()
|
|
|
|
{
|
2015-12-06 14:14:07 +03:00
|
|
|
Q_ASSERT(!m_updateUrl.isEmpty());
|
|
|
|
QDesktopServices::openUrl(m_updateUrl);
|
|
|
|
return;
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
|
2015-12-06 14:14:07 +03:00
|
|
|
bool ProgramUpdater::isVersionMoreRecent(const QString &remoteVersion) const
|
2010-10-24 01:46:05 +04:00
|
|
|
{
|
2018-05-24 18:41:03 +03:00
|
|
|
const QRegularExpressionMatch regVerMatch = QRegularExpression("([0-9.]+)").match(QBT_VERSION);
|
|
|
|
if (regVerMatch.hasMatch()) {
|
2019-08-06 18:07:57 +03:00
|
|
|
const QString localVersion = regVerMatch.captured(1);
|
|
|
|
const QVector<QStringRef> remoteParts = remoteVersion.splitRef('.');
|
|
|
|
const QVector<QStringRef> localParts = localVersion.splitRef('.');
|
|
|
|
|
2018-11-06 18:49:17 +03:00
|
|
|
for (int i = 0; i < qMin(remoteParts.size(), localParts.size()); ++i) {
|
2015-12-06 14:14:07 +03:00
|
|
|
if (remoteParts[i].toInt() > localParts[i].toInt())
|
|
|
|
return true;
|
|
|
|
if (remoteParts[i].toInt() < localParts[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 (remoteParts.size() > localParts.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)
|
2018-05-24 18:41:03 +03:00
|
|
|
const QRegularExpressionMatch regDevelMatch = QRegularExpression("(alpha|beta|rc)").match(QBT_VERSION);
|
|
|
|
if (regDevelMatch.hasMatch())
|
2015-12-06 14:14:07 +03:00
|
|
|
return true;
|
2011-12-29 03:26:00 +04:00
|
|
|
}
|
2015-12-06 14:14:07 +03:00
|
|
|
return false;
|
2010-10-24 01:46:05 +04:00
|
|
|
}
|
|
|
|
|
2015-12-06 16:19:00 +03:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
QString getStringValue(QXmlStreamReader &xml)
|
|
|
|
{
|
|
|
|
xml.readNext();
|
|
|
|
if (xml.isCharacters() && !xml.isWhitespace())
|
|
|
|
return xml.text().toString();
|
|
|
|
|
2019-02-14 20:16:42 +03:00
|
|
|
return {};
|
2015-12-06 16:19:00 +03:00
|
|
|
}
|
|
|
|
}
|