Correctly handle "torrent finished" events
[qBittorrent.git] / src / gui / programupdater.cpp
blob063bc5473a436456a912064de60cb6ad8e56a423
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021 Mike Tzou (Chocobo1)
4 * Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "programupdater.h"
32 #include <libtorrent/version.hpp>
34 #include <QtCore/qconfig.h>
35 #include <QtSystemDetection>
36 #include <QDebug>
37 #include <QDesktopServices>
38 #include <QRegularExpression>
39 #include <QXmlStreamReader>
41 #include "base/global.h"
42 #include "base/net/downloadmanager.h"
43 #include "base/preferences.h"
44 #include "base/utils/version.h"
45 #include "base/version.h"
47 namespace
49 bool isVersionMoreRecent(const QString &remoteVersion)
51 using Version = Utils::Version<4, 3>;
53 const auto newVersion = Version::fromString(remoteVersion);
54 if (!newVersion.isValid())
55 return false;
57 const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
58 if (newVersion == currentVersion)
60 const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
61 QRegularExpression(u"(alpha|beta|rc)"_s));
62 if (isDevVersion)
63 return true;
65 return (newVersion > currentVersion);
68 QString buildVariant()
70 #if defined(Q_OS_MACOS)
71 const auto BASE_OS = u"Mac OS X"_s;
72 #elif defined(Q_OS_WIN)
73 const auto BASE_OS = u"Windows x64"_s;
74 #endif
76 if constexpr ((QT_VERSION_MAJOR == 6) && (LIBTORRENT_VERSION_MAJOR == 1))
77 return BASE_OS;
79 return u"%1 (qt%2 lt%3%4)"_s.arg(BASE_OS, QString::number(QT_VERSION_MAJOR), QString::number(LIBTORRENT_VERSION_MAJOR), QString::number(LIBTORRENT_VERSION_MINOR));
83 void ProgramUpdater::checkForUpdates() const
85 const auto RSS_URL = u"https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml"_s;
86 // Don't change this User-Agent. In case our updater goes haywire,
87 // the filehost can identify it and contact us.
88 Net::DownloadManager::instance()->download(
89 Net::DownloadRequest(RSS_URL).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"))
90 , Preferences::instance()->useProxyForGeneralPurposes(), this, &ProgramUpdater::rssDownloadFinished);
93 QString ProgramUpdater::getNewVersion() const
95 return m_newVersion;
98 void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
100 if (result.status != Net::DownloadStatus::Success)
102 qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
103 emit updateCheckFinished();
104 return;
107 qDebug("Finished downloading the new qBittorrent updates RSS");
109 const auto getStringValue = [](QXmlStreamReader &xml) -> QString
111 xml.readNext();
112 return (xml.isCharacters() && !xml.isWhitespace())
113 ? xml.text().toString()
114 : QString {};
117 const QString variant = buildVariant();
118 bool inItem = false;
119 QString version;
120 QString updateLink;
121 QString type;
122 QXmlStreamReader xml(result.data);
124 while (!xml.atEnd())
126 xml.readNext();
128 if (xml.isStartElement())
130 if (xml.name() == u"item")
131 inItem = true;
132 else if (inItem && (xml.name() == u"link"))
133 updateLink = getStringValue(xml);
134 else if (inItem && (xml.name() == u"type"))
135 type = getStringValue(xml);
136 else if (inItem && (xml.name() == u"version"))
137 version = getStringValue(xml);
139 else if (xml.isEndElement())
141 if (inItem && (xml.name() == u"item"))
143 if (type.compare(variant, Qt::CaseInsensitive) == 0)
145 qDebug("The last update available is %s", qUtf8Printable(version));
146 if (!version.isEmpty())
148 qDebug("Detected version is %s", qUtf8Printable(version));
149 if (isVersionMoreRecent(version))
151 m_newVersion = version;
152 m_updateURL = updateLink;
155 break;
158 inItem = false;
159 updateLink.clear();
160 type.clear();
161 version.clear();
166 emit updateCheckFinished();
169 bool ProgramUpdater::updateProgram() const
171 return QDesktopServices::openUrl(m_updateURL);