Browse Source

Clean up coding style

adaptive-webui-19844
Chocobo1 4 years ago
parent
commit
40bd2039d4
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 87
      src/gui/programupdater.cpp
  2. 12
      src/gui/programupdater.h

87
src/gui/programupdater.cpp

@ -48,29 +48,52 @@
namespace namespace
{ {
const QString RSS_URL {QStringLiteral("https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml")}; bool isVersionMoreRecent(const QString &remoteVersion)
{
const QRegularExpressionMatch regVerMatch = QRegularExpression("([0-9.]+)").match(QBT_VERSION);
if (regVerMatch.hasMatch())
{
const QString localVersion = regVerMatch.captured(1);
const QVector<QStringRef> remoteParts = remoteVersion.splitRef('.');
const QVector<QStringRef> localParts = localVersion.splitRef('.');
QString getStringValue(QXmlStreamReader &xml); for (int i = 0; i < qMin(remoteParts.size(), localParts.size()); ++i)
{
if (remoteParts[i].toInt() > localParts[i].toInt())
return true;
if (remoteParts[i].toInt() < localParts[i].toInt())
return false;
}
// Compared parts were equal, if remote version is longer, then it's more recent (2.9.2.1 > 2.9.2)
if (remoteParts.size() > localParts.size())
return true;
// versions are equal, check if the local version is a development release, in which case it is older (2.9.2beta < 2.9.2)
const QRegularExpressionMatch regDevelMatch = QRegularExpression("(alpha|beta|rc)").match(QBT_VERSION);
if (regDevelMatch.hasMatch())
return true;
}
return false;
}
} }
ProgramUpdater::ProgramUpdater(QObject *parent, bool invokedByUser) ProgramUpdater::ProgramUpdater(QObject *parent, const bool invokedByUser)
: QObject(parent) : QObject(parent)
, m_invokedByUser(invokedByUser) , m_invokedByUser(invokedByUser)
{ {
} }
void ProgramUpdater::checkForUpdates() void ProgramUpdater::checkForUpdates() const
{ {
const auto RSS_URL = QString::fromLatin1("https://www.fosshub.com/feed/5b8793a7f9ee5a5c3e97a3b2.xml");
// Don't change this User-Agent. In case our updater goes haywire, // Don't change this User-Agent. In case our updater goes haywire,
// the filehost can identify it and contact us. // the filehost can identify it and contact us.
Net::DownloadManager::instance()->download( Net::DownloadManager::instance()->download(
Net::DownloadRequest(RSS_URL).userAgent("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)") Net::DownloadRequest(RSS_URL).userAgent("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)")
, this, &ProgramUpdater::rssDownloadFinished); , this, &ProgramUpdater::rssDownloadFinished);
} }
void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result) void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
{ {
if (result.status != Net::DownloadStatus::Success) if (result.status != Net::DownloadStatus::Success)
{ {
qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString; qDebug() << "Downloading the new qBittorrent updates RSS failed:" << result.errorString;
@ -80,6 +103,14 @@ void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
qDebug("Finished downloading the new qBittorrent updates RSS"); qDebug("Finished downloading the new qBittorrent updates RSS");
const auto getStringValue = [](QXmlStreamReader &xml) -> QString
{
xml.readNext();
return (xml.isCharacters() && !xml.isWhitespace())
? xml.text().toString()
: QString {};
};
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
const QString OS_TYPE {"Mac OS X"}; const QString OS_TYPE {"Mac OS X"};
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
@ -137,48 +168,8 @@ void ProgramUpdater::rssDownloadFinished(const Net::DownloadResult &result)
emit updateCheckFinished(!m_updateUrl.isEmpty(), version, m_invokedByUser); emit updateCheckFinished(!m_updateUrl.isEmpty(), version, m_invokedByUser);
} }
void ProgramUpdater::updateProgram() void ProgramUpdater::updateProgram() const
{ {
Q_ASSERT(!m_updateUrl.isEmpty()); Q_ASSERT(!m_updateUrl.isEmpty());
QDesktopServices::openUrl(m_updateUrl); QDesktopServices::openUrl(m_updateUrl);
return;
}
bool ProgramUpdater::isVersionMoreRecent(const QString &remoteVersion) const
{
const QRegularExpressionMatch regVerMatch = QRegularExpression("([0-9.]+)").match(QBT_VERSION);
if (regVerMatch.hasMatch())
{
const QString localVersion = regVerMatch.captured(1);
const QVector<QStringRef> remoteParts = remoteVersion.splitRef('.');
const QVector<QStringRef> localParts = localVersion.splitRef('.');
for (int i = 0; i < qMin(remoteParts.size(), localParts.size()); ++i)
{
if (remoteParts[i].toInt() > localParts[i].toInt())
return true;
if (remoteParts[i].toInt() < localParts[i].toInt())
return false;
}
// Compared parts were equal, if remote version is longer, then it's more recent (2.9.2.1 > 2.9.2)
if (remoteParts.size() > localParts.size())
return true;
// versions are equal, check if the local version is a development release, in which case it is older (2.9.2beta < 2.9.2)
const QRegularExpressionMatch regDevelMatch = QRegularExpression("(alpha|beta|rc)").match(QBT_VERSION);
if (regDevelMatch.hasMatch())
return true;
}
return false;
}
namespace
{
QString getStringValue(QXmlStreamReader &xml)
{
xml.readNext();
if (xml.isCharacters() && !xml.isWhitespace())
return xml.text().toString();
return {};
}
} }

12
src/gui/programupdater.h

@ -35,7 +35,7 @@ namespace Net
struct DownloadResult; struct DownloadResult;
} }
class ProgramUpdater : public QObject class ProgramUpdater final : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(ProgramUpdater) Q_DISABLE_COPY(ProgramUpdater)
@ -43,18 +43,16 @@ class ProgramUpdater : public QObject
public: public:
explicit ProgramUpdater(QObject *parent = nullptr, bool invokedByUser = false); explicit ProgramUpdater(QObject *parent = nullptr, bool invokedByUser = false);
void checkForUpdates(); void checkForUpdates() const;
void updateProgram(); void updateProgram() const;
signals: signals:
void updateCheckFinished(bool updateAvailable, QString version, bool invokedByUser); void updateCheckFinished(bool updateAvailable, const QString &version, bool invokedByUser);
private slots: private slots:
void rssDownloadFinished(const Net::DownloadResult &result); void rssDownloadFinished(const Net::DownloadResult &result);
private: private:
bool isVersionMoreRecent(const QString &remoteVersion) const;
QString m_updateUrl; QString m_updateUrl;
bool m_invokedByUser; const bool m_invokedByUser;
}; };

Loading…
Cancel
Save