diff --git a/src/singletons/Updates.cpp b/src/singletons/Updates.cpp index b5fcea2f..7d38bffb 100644 --- a/src/singletons/Updates.cpp +++ b/src/singletons/Updates.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace chatterino { namespace { @@ -20,6 +21,38 @@ namespace { { return getSettings()->betaUpdates ? "beta" : "stable"; } + + /// Checks if the online version is newer or older than the current version. + bool isDowngradeOf(const QString &online, const QString ¤t) + { + static auto matchVersion = + QRegularExpression(R"((\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?)"); + + // Versions are just strings, they don't need to follow a specific + // format so we can only assume if one version is newer than another + // one. + + // We match x.x.x.x with each version level being optional. + + auto onlineMatch = matchVersion.match(online); + auto currentMatch = matchVersion.match(current); + + for (int i = 1; i <= 4; i++) + { + if (onlineMatch.captured(i).toInt() < + currentMatch.captured(i).toInt()) + { + return true; + } + if (onlineMatch.captured(i).toInt() > + currentMatch.captured(i).toInt()) + { + break; + } + } + + return false; + } } // namespace Updates::Updates() @@ -263,6 +296,8 @@ void Updates::checkForUpdates() if (this->currentVersion_ != this->onlineVersion_) { this->setStatus_(UpdateAvailable); + this->isDowngrade_ = + isDowngradeOf(this->onlineVersion_, this->currentVersion_); } else { @@ -309,6 +344,11 @@ bool Updates::isError() const } } +bool Updates::isDowngrade() const +{ + return this->isDowngrade_; +} + void Updates::setStatus_(Status status) { if (this->status_ != status) diff --git a/src/singletons/Updates.hpp b/src/singletons/Updates.hpp index 377210ed..ac1bba9d 100644 --- a/src/singletons/Updates.hpp +++ b/src/singletons/Updates.hpp @@ -32,6 +32,7 @@ public: bool shouldShowUpdateButton() const; bool isError() const; + bool isDowngrade() const; pajlada::Signals::Signal statusUpdated; @@ -39,6 +40,7 @@ private: QString currentVersion_; QString onlineVersion_; Status status_ = None; + bool isDowngrade_{}; QString updateExe_; QString updatePortable_; diff --git a/src/widgets/dialogs/UpdateDialog.cpp b/src/widgets/dialogs/UpdateDialog.cpp index 1099912f..f28d6223 100644 --- a/src/widgets/dialogs/UpdateDialog.cpp +++ b/src/widgets/dialogs/UpdateDialog.cpp @@ -50,9 +50,17 @@ void UpdateDialog::updateStatusChanged(Updates::Status status) { case Updates::UpdateAvailable: { this->ui_.label->setText( - QString("An update (%1) is available.\n\nDo you want to " - "download and install it?") - .arg(Updates::getInstance().getOnlineVersion())); + (Updates::getInstance().isDowngrade() + ? QString( + "The version online (%1) seems to be lower than the " + "current (%2).\nEither a version was reverted or " + "you are running a newer build.\n\nDo you want to " + "download and install it?") + .arg(Updates::getInstance().getOnlineVersion(), + Updates::getInstance().getCurrentVersion()) + : QString("An update (%1) is available.\n\nDo you want to " + "download and install it?") + .arg(Updates::getInstance().getOnlineVersion()))); this->updateGeometry(); } break;