Bump to 4.6.7
[qBittorrent.git] / src / gui / programupdater.cpp
blobc6ef71d7dfae80d448eaa0b03a5fb70c2f4f26b9
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 <QtGlobal>
37 #if defined(Q_OS_WIN)
38 #include <Windows.h>
39 #include <versionhelpers.h> // must follow after Windows.h
40 #endif
42 #include <QDebug>
43 #include <QDesktopServices>
44 #include <QRegularExpression>
45 #include <QXmlStreamReader>
47 #if defined(Q_OS_WIN)
48 #include <QSysInfo>
49 #endif
51 #include "base/global.h"
52 #include "base/net/downloadmanager.h"
53 #include "base/preferences.h"
54 #include "base/utils/version.h"
55 #include "base/version.h"
57 namespace
59 bool isVersionMoreRecent(const QString &remoteVersion)
61 using Version = Utils::Version<4, 3>;
63 const auto newVersion = Version::fromString(remoteVersion);
64 if (!newVersion.isValid())
65 return false;
67 const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
68 if (newVersion == currentVersion)
70 const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
71 QRegularExpression(u"(alpha|beta|rc)"_s));
72 if (isDevVersion)
73 return true;
75 return (newVersion > currentVersion);
78 QString buildVariant()
80 #if defined(Q_OS_MACOS)
81 const auto BASE_OS = u"Mac OS X"_s;
82 #elif defined(Q_OS_WIN)
83 const auto BASE_OS = (::IsWindows7OrGreater() && QSysInfo::currentCpuArchitecture().endsWith(u"64"))
84 ? u"Windows x64"_s
85 : u"Windows"_s;
86 #endif
88 if constexpr ((QT_VERSION_MAJOR == 6) && (LIBTORRENT_VERSION_MAJOR == 1))
89 return BASE_OS;
91 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));
95 void ProgramUpdater::checkForUpdates() const
97 const auto RSS_URL = u"https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml"_s;
98 // Don't change this User-Agent. In case our updater goes haywire,
99 // the filehost can identify it and contact us.
100 Net::DownloadManager::instance()->download(
101 Net::DownloadRequest(RSS_URL).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"))
102 , Preferences::instance()->useProxyForGeneralPurposes(), this, &ProgramUpdater::rssDownloadFinished);
105 QString ProgramUpdater::getNewVersion() const
107 return m_newVersion;
110 void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
112 if (result.status != Net::DownloadStatus::Success)
114 qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
115 emit updateCheckFinished();
116 return;
119 qDebug("Finished downloading the new qBittorrent updates RSS");
121 const auto getStringValue = [](QXmlStreamReader &xml) -> QString
123 xml.readNext();
124 return (xml.isCharacters() && !xml.isWhitespace())
125 ? xml.text().toString()
126 : QString {};
129 const QString variant = buildVariant();
130 bool inItem = false;
131 QString version;
132 QString updateLink;
133 QString type;
134 QXmlStreamReader xml(result.data);
136 while (!xml.atEnd())
138 xml.readNext();
140 if (xml.isStartElement())
142 if (xml.name() == u"item")
143 inItem = true;
144 else if (inItem && (xml.name() == u"link"))
145 updateLink = getStringValue(xml);
146 else if (inItem && (xml.name() == u"type"))
147 type = getStringValue(xml);
148 else if (inItem && (xml.name() == u"version"))
149 version = getStringValue(xml);
151 else if (xml.isEndElement())
153 if (inItem && (xml.name() == u"item"))
155 if (type.compare(variant, Qt::CaseInsensitive) == 0)
157 qDebug("The last update available is %s", qUtf8Printable(version));
158 if (!version.isEmpty())
160 qDebug("Detected version is %s", qUtf8Printable(version));
161 if (isVersionMoreRecent(version))
163 m_newVersion = version;
164 m_updateURL = updateLink;
167 break;
170 inItem = false;
171 updateLink.clear();
172 type.clear();
173 version.clear();
178 emit updateCheckFinished();
181 bool ProgramUpdater::updateProgram() const
183 return QDesktopServices::openUrl(m_updateURL);