Don't overwrite stored layout of main window with incorrect one
[qBittorrent.git] / src / gui / programupdater.cpp
blobead2e9e49afaaeec6b338d7260483a70392b80cf
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 <QtSystemDetection>
33 #include <QDebug>
34 #include <QDesktopServices>
35 #include <QRegularExpression>
36 #include <QXmlStreamReader>
38 #include "base/global.h"
39 #include "base/net/downloadmanager.h"
40 #include "base/preferences.h"
41 #include "base/utils/version.h"
42 #include "base/version.h"
44 namespace
46 bool isVersionMoreRecent(const QString &remoteVersion)
48 using Version = Utils::Version<4, 3>;
50 const auto newVersion = Version::fromString(remoteVersion);
51 if (!newVersion.isValid())
52 return false;
54 const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
55 if (newVersion == currentVersion)
57 const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
58 QRegularExpression(u"(alpha|beta|rc)"_s));
59 if (isDevVersion)
60 return true;
62 return (newVersion > currentVersion);
66 void ProgramUpdater::checkForUpdates() const
68 const auto RSS_URL = u"https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml"_s;
69 // Don't change this User-Agent. In case our updater goes haywire,
70 // the filehost can identify it and contact us.
71 Net::DownloadManager::instance()->download(
72 Net::DownloadRequest(RSS_URL).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"))
73 , Preferences::instance()->useProxyForGeneralPurposes(), this, &ProgramUpdater::rssDownloadFinished);
76 QString ProgramUpdater::getNewVersion() const
78 return m_newVersion;
81 void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
83 if (result.status != Net::DownloadStatus::Success)
85 qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
86 emit updateCheckFinished();
87 return;
90 qDebug("Finished downloading the new qBittorrent updates RSS");
92 const auto getStringValue = [](QXmlStreamReader &xml) -> QString
94 xml.readNext();
95 return (xml.isCharacters() && !xml.isWhitespace())
96 ? xml.text().toString()
97 : QString {};
100 #ifdef Q_OS_MACOS
101 const QString OS_TYPE = u"Mac OS X"_s;
102 #elif defined(Q_OS_WIN)
103 const QString OS_TYPE = u"Windows x64"_s;
104 #endif
106 bool inItem = false;
107 QString version;
108 QString updateLink;
109 QString type;
110 QXmlStreamReader xml(result.data);
112 while (!xml.atEnd())
114 xml.readNext();
116 if (xml.isStartElement())
118 if (xml.name() == u"item")
119 inItem = true;
120 else if (inItem && (xml.name() == u"link"))
121 updateLink = getStringValue(xml);
122 else if (inItem && (xml.name() == u"type"))
123 type = getStringValue(xml);
124 else if (inItem && (xml.name() == u"version"))
125 version = getStringValue(xml);
127 else if (xml.isEndElement())
129 if (inItem && (xml.name() == u"item"))
131 if (type.compare(OS_TYPE, Qt::CaseInsensitive) == 0)
133 qDebug("The last update available is %s", qUtf8Printable(version));
134 if (!version.isEmpty())
136 qDebug("Detected version is %s", qUtf8Printable(version));
137 if (isVersionMoreRecent(version))
139 m_newVersion = version;
140 m_updateURL = updateLink;
143 break;
146 inItem = false;
147 updateLink.clear();
148 type.clear();
149 version.clear();
154 emit updateCheckFinished();
157 bool ProgramUpdater::updateProgram() const
159 return QDesktopServices::openUrl(m_updateURL);