Use tray icon from system theme only if option is set
[qBittorrent.git] / src / gui / programupdater.cpp
blob7d6de7807e7060570dae203c524570fbc4dbf127
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 #if defined(Q_OS_WIN)
33 #include <Windows.h>
34 #include <versionhelpers.h> // must follow after Windows.h
35 #endif
37 #include <QDebug>
38 #include <QDesktopServices>
39 #include <QRegularExpression>
40 #include <QXmlStreamReader>
42 #if defined(Q_OS_WIN)
43 #include <QSysInfo>
44 #endif
46 #include "base/global.h"
47 #include "base/net/downloadmanager.h"
48 #include "base/preferences.h"
49 #include "base/utils/version.h"
50 #include "base/version.h"
52 namespace
54 bool isVersionMoreRecent(const QString &remoteVersion)
56 using Version = Utils::Version<4, 3>;
58 const auto newVersion = Version::fromString(remoteVersion);
59 if (!newVersion.isValid())
60 return false;
62 const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
63 if (newVersion == currentVersion)
65 const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
66 QRegularExpression(u"(alpha|beta|rc)"_qs));
67 if (isDevVersion)
68 return true;
70 return (newVersion > currentVersion);
74 void ProgramUpdater::checkForUpdates() const
76 const auto RSS_URL = u"https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml"_qs;
77 // Don't change this User-Agent. In case our updater goes haywire,
78 // the filehost can identify it and contact us.
79 Net::DownloadManager::instance()->download(
80 Net::DownloadRequest(RSS_URL).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"))
81 , Preferences::instance()->useProxyForGeneralPurposes(), this, &ProgramUpdater::rssDownloadFinished);
84 QString ProgramUpdater::getNewVersion() const
86 return m_newVersion;
89 void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
91 if (result.status != Net::DownloadStatus::Success)
93 qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
94 emit updateCheckFinished();
95 return;
98 qDebug("Finished downloading the new qBittorrent updates RSS");
100 const auto getStringValue = [](QXmlStreamReader &xml) -> QString
102 xml.readNext();
103 return (xml.isCharacters() && !xml.isWhitespace())
104 ? xml.text().toString()
105 : QString {};
108 #ifdef Q_OS_MACOS
109 const QString OS_TYPE = u"Mac OS X"_qs;
110 #elif defined(Q_OS_WIN)
111 const QString OS_TYPE = (::IsWindows7OrGreater() && QSysInfo::currentCpuArchitecture().endsWith(u"64"))
112 ? u"Windows x64"_qs
113 : u"Windows"_qs;
114 #endif
116 bool inItem = false;
117 QString version;
118 QString updateLink;
119 QString type;
120 QXmlStreamReader xml(result.data);
122 while (!xml.atEnd())
124 xml.readNext();
126 if (xml.isStartElement())
128 if (xml.name() == u"item")
129 inItem = true;
130 else if (inItem && (xml.name() == u"link"))
131 updateLink = getStringValue(xml);
132 else if (inItem && (xml.name() == u"type"))
133 type = getStringValue(xml);
134 else if (inItem && (xml.name() == u"version"))
135 version = getStringValue(xml);
137 else if (xml.isEndElement())
139 if (inItem && (xml.name() == u"item"))
141 if (type.compare(OS_TYPE, Qt::CaseInsensitive) == 0)
143 qDebug("The last update available is %s", qUtf8Printable(version));
144 if (!version.isEmpty())
146 qDebug("Detected version is %s", qUtf8Printable(version));
147 if (isVersionMoreRecent(version))
149 m_newVersion = version;
150 m_updateURL = updateLink;
153 break;
156 inItem = false;
157 updateLink.clear();
158 type.clear();
159 version.clear();
164 emit updateCheckFinished();
167 bool ProgramUpdater::updateProgram() const
169 return QDesktopServices::openUrl(m_updateURL);