diff --git a/src/Updater.cpp b/src/Updater.cpp index 18c0db6..9b64974 100644 --- a/src/Updater.cpp +++ b/src/Updater.cpp @@ -68,7 +68,7 @@ public: virtual void SetUpdatePolicy(UpdatePolicyT updatePolicy) override; virtual void ForceUpdateCheck() override; virtual void DownloadUpdate(const std::string &url, std::filesystem::path *file, std::filesystem::path *signatureFile) override; - virtual UpdateStatus GetCurrentStatus() const override { return this->currentResult; } + virtual UpdateStatus GetCurrentStatus() const override; virtual UpdateStatus GetReleaseGeneratorStatus() override; private: @@ -156,7 +156,7 @@ UpdateStatus UpdaterImpl::GetCachedUpdateStatus() reader.read(&status); // cached curruent version might come from a different version. - status.ResetCurrentVersion(); + status.UpdateForCurrentVersion(); return status; } } @@ -416,6 +416,36 @@ static int compareVersions(const std::string &l, const std::string &r) } return 0; } + +void UpdateRelease::UpdateForCurrentVersion(const std::string¤tVersion) +{ + if (compareVersions(currentVersion,this->UpgradeVersion()) >= 0) + { + this->updateAvailable_ = false; + } +} + +bool UpdateStatus::UpdateAvailable() const +{ + if (!isValid_) return false; + + switch (this->UpdatePolicy()) + { + case UpdatePolicyT::Development: + return this->devRelease_.UpdateAvailable(); + break; + case UpdatePolicyT::ReleaseOnly: + return this->releaseOnlyRelease_.UpdateAvailable(); + case UpdatePolicyT::ReleaseOrBeta: + this->releaseOrBetaRelease_.UpdateAvailable(); + default: + case UpdatePolicyT::Disabled: + return false; + } + +} + + static std::string normalizeReleaseName(const std::string &releaseName) { // e.g. "PiPedal 1.2.34 Release" -> "PiPedal v1.2.34-Release" @@ -897,16 +927,20 @@ UpdateStatus::UpdateStatus() #endif } -void UpdateStatus::ResetCurrentVersion() +void UpdateStatus::UpdateForCurrentVersion() { currentVersion_ = PROJECT_VER; currentVersionDisplayName_ = PROJECT_DISPLAY_VERSION; -#ifdef TEST_UPDATE +#if defined(TEST_UPDATE) && defined(DEBUG) // uncomment this line to test upgrading. currentVersion_ = "1.2.39"; - currentVersionDisplayName_ = "PiPedal 1.2.39-Debug"; + currentVersionDisplayName_ = "PiPedal 1.2.39-Fictional"; #endif + + this->devRelease_.UpdateForCurrentVersion(currentVersion_);; + this->releaseOnlyRelease_.UpdateForCurrentVersion(currentVersion_); + this->releaseOrBetaRelease_.UpdateForCurrentVersion(currentVersion_); } std::chrono::system_clock::time_point UpdateStatus::LastUpdateTime() const @@ -953,9 +987,6 @@ const GithubAsset *GithubRelease::GetDownloadForCurrentArchitecture() const return nullptr; } -UpdateRelease::UpdateRelease() -{ -} std::string UpdaterImpl::GetSignatureUrl(const std::string &url) { @@ -1327,6 +1358,10 @@ void UpdaterImpl::DownloadUpdate(const std::string &url, std::filesystem::path * } } +UpdateStatus UpdaterImpl::GetCurrentStatus() const { + return this->currentResult; +} + void UpdateStatus::AddRelease(const std::string&packageName) { diff --git a/src/UpdaterStatus.hpp b/src/UpdaterStatus.hpp index 4d50326..f4acf1c 100644 --- a/src/UpdaterStatus.hpp +++ b/src/UpdaterStatus.hpp @@ -22,7 +22,9 @@ #include "json.hpp" namespace pipedal { - enum class UpdatePolicyT + class UpdateStatus; + + enum class UpdatePolicyT { // ordinal values must not change, as files depend on them. // must match declaration in Updater.tsx @@ -37,14 +39,15 @@ namespace pipedal { { private: friend class UpdaterImpl; + friend class UpdateStatus; bool updateAvailable_ = false; std::string upgradeVersion_; // just the version. std::string upgradeVersionDisplayName_; // display name for the version. std::string assetName_; // filename only std::string updateUrl_; // url from which to download the .deb file. std::string gpgSignatureUrl_; // url from which to download the .deb.asc file. + void UpdateForCurrentVersion(const std::string¤tVersion); public: - UpdateRelease(); bool UpdateAvailable() const { return updateAvailable_; } const std::string &UpgradeVersion() const { return upgradeVersion_; } @@ -78,8 +81,12 @@ namespace pipedal { std::chrono::system_clock::time_point LastUpdateTime() const; void LastUpdateTime(const std::chrono::system_clock::time_point &timePoint); - void ResetCurrentVersion(); + + void UpdateForCurrentVersion(); bool IsValid() const { return isValid_; } + + bool UpdateAvailable() const; + const std::string &ErrorMessage() const { return errorMessage_; } bool IsOnline() const { return isOnline_; } const std::string &CurrentVersion() const { return currentVersion_; }