Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / programupdater.cpp
blob317509ed96089961206b6c3d1f4f14746fdeeef1
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/net/downloadmanager.h"
47 #include "base/utils/version.h"
48 #include "base/version.h"
50 namespace
52 bool isVersionMoreRecent(const QString &remoteVersion)
54 using Version = Utils::Version<int, 4, 3>;
56 try
58 const Version newVersion {remoteVersion};
59 const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
60 if (newVersion == currentVersion)
62 const bool isDevVersion = QString::fromLatin1(QBT_VERSION_STATUS).contains(
63 QRegularExpression(QLatin1String("(alpha|beta|rc)")));
64 if (isDevVersion)
65 return true;
67 return (newVersion > currentVersion);
69 catch (const RuntimeError &)
71 return false;
76 void ProgramUpdater::checkForUpdates() const
78 const auto RSS_URL = QString::fromLatin1("https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml");
79 // Don't change this User-Agent. In case our updater goes haywire,
80 // the filehost can identify it and contact us.
81 Net::DownloadManager::instance()->download(
82 Net::DownloadRequest(RSS_URL).userAgent("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)")
83 , this, &ProgramUpdater::rssDownloadFinished);
86 QString ProgramUpdater::getNewVersion() const
88 return m_newVersion;
91 void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
93 if (result.status != Net::DownloadStatus::Success)
95 qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
96 emit updateCheckFinished();
97 return;
100 qDebug("Finished downloading the new qBittorrent updates RSS");
102 const auto getStringValue = [](QXmlStreamReader &xml) -> QString
104 xml.readNext();
105 return (xml.isCharacters() && !xml.isWhitespace())
106 ? xml.text().toString()
107 : QString {};
110 #ifdef Q_OS_MACOS
111 const QString OS_TYPE {"Mac OS X"};
112 #elif defined(Q_OS_WIN)
113 const QString OS_TYPE {(::IsWindows7OrGreater()
114 && QSysInfo::currentCpuArchitecture().endsWith("64"))
115 ? "Windows x64" : "Windows"};
116 #endif
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() == QLatin1String("item"))
131 inItem = true;
132 else if (inItem && (xml.name() == QLatin1String("link")))
133 updateLink = getStringValue(xml);
134 else if (inItem && (xml.name() == QLatin1String("type")))
135 type = getStringValue(xml);
136 else if (inItem && (xml.name() == QLatin1String("version")))
137 version = getStringValue(xml);
139 else if (xml.isEndElement())
141 if (inItem && (xml.name() == QLatin1String("item")))
143 if (type.compare(OS_TYPE, 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);