diff options
Diffstat (limited to 'src/m_ver_check')
-rw-r--r-- | src/m_ver_check/BKTUSVersionCheckTask.cpp | 1 | ||||
-rw-r--r-- | src/m_ver_check/GitHubVersionCheckTask.cpp | 1 | ||||
-rw-r--r-- | src/m_ver_check/SoftwareVersion.cpp | 33 | ||||
-rw-r--r-- | src/m_ver_check/SoftwareVersion.h | 18 | ||||
-rw-r--r-- | src/m_ver_check/UpdateTab.cpp | 113 | ||||
-rw-r--r-- | src/m_ver_check/UpdateTab.h | 12 | ||||
-rw-r--r-- | src/m_ver_check/Utils.cpp | 3 | ||||
-rw-r--r-- | src/m_ver_check/VersionCheckingModule.cpp | 59 |
8 files changed, 191 insertions, 49 deletions
diff --git a/src/m_ver_check/BKTUSVersionCheckTask.cpp b/src/m_ver_check/BKTUSVersionCheckTask.cpp index ce428e1..e9b3b1a 100644 --- a/src/m_ver_check/BKTUSVersionCheckTask.cpp +++ b/src/m_ver_check/BKTUSVersionCheckTask.cpp @@ -104,6 +104,7 @@ void BKTUSVersionCheckTask::slot_parse_reply(QNetworkReply* reply) { reply->deleteLater(); if (replies_.isEmpty()) { + meta_.timestamp = QDateTime::currentDateTime(); FillGrtWithVersionInfo(meta_); emit SignalUpgradeVersion(meta_); } diff --git a/src/m_ver_check/GitHubVersionCheckTask.cpp b/src/m_ver_check/GitHubVersionCheckTask.cpp index 3ea1f25..72e47e7 100644 --- a/src/m_ver_check/GitHubVersionCheckTask.cpp +++ b/src/m_ver_check/GitHubVersionCheckTask.cpp @@ -104,6 +104,7 @@ void GitHubVersionCheckTask::slot_parse_reply(QNetworkReply* reply) { reply->deleteLater(); if (replies_.isEmpty()) { + meta_.timestamp = QDateTime::currentDateTime(); FillGrtWithVersionInfo(meta_); emit SignalUpgradeVersion(meta_); } diff --git a/src/m_ver_check/SoftwareVersion.cpp b/src/m_ver_check/SoftwareVersion.cpp index 9f53791..c6dbf6e 100644 --- a/src/m_ver_check/SoftwareVersion.cpp +++ b/src/m_ver_check/SoftwareVersion.cpp @@ -32,6 +32,8 @@ #include <GFSDKExtra.h> #include <GFSDKLog.h> +#include <QJsonDocument> +#include <QJsonObject> #include <QString> #include "GFModuleCommonUtils.hpp" @@ -64,3 +66,34 @@ auto SoftwareVersion::GitCommitHashMismatch() const -> bool { remote_commit_hash_by_tag, local_commit_hash); return remote_commit_hash_by_tag.trimmed() != local_commit_hash.trimmed(); } + +auto SoftwareVersion::ToJson() const -> QJsonObject { + QJsonObject obj; + obj["api"] = api; + obj["latest_version"] = latest_version; + obj["current_version"] = current_version; + obj["current_version_publish_in_remote"] = current_version_publish_in_remote; + obj["current_commit_hash_publish_in_remote"] = + current_commit_hash_publish_in_remote; + obj["publish_date"] = publish_date; + obj["release_note"] = release_note; + obj["remote_commit_hash_by_tag"] = remote_commit_hash_by_tag; + obj["local_commit_hash"] = local_commit_hash; + obj["timestamp"] = timestamp.toSecsSinceEpoch(); + return obj; +} + +void SoftwareVersion::FromJson(const QJsonObject& obj) { + api = obj.value("api").toString(); + latest_version = obj.value("latest_version").toString(); + current_version = obj.value("current_version").toString(); + current_version_publish_in_remote = + obj.value("current_version_publish_in_remote").toBool(); + current_commit_hash_publish_in_remote = + obj.value("current_commit_hash_publish_in_remote").toBool(); + publish_date = obj.value("publish_date").toString(); + release_note = obj.value("release_note").toString(); + remote_commit_hash_by_tag = obj.value("remote_commit_hash_by_tag").toString(); + local_commit_hash = obj.value("local_commit_hash").toString(); + timestamp = QDateTime::fromSecsSinceEpoch(obj.value("timestamp").toInt()); +} diff --git a/src/m_ver_check/SoftwareVersion.h b/src/m_ver_check/SoftwareVersion.h index c2fc713..4eeab52 100644 --- a/src/m_ver_check/SoftwareVersion.h +++ b/src/m_ver_check/SoftwareVersion.h @@ -28,6 +28,7 @@ #pragma once +#include <QJsonDocument> #include <QString> /** @@ -47,6 +48,8 @@ struct SoftwareVersion { QString remote_commit_hash_by_tag; QString local_commit_hash; + QDateTime timestamp; + /** * @brief * @@ -89,6 +92,21 @@ struct SoftwareVersion { */ [[nodiscard]] auto CurrentVersionReleased() const -> bool; + /** + * @brief + * + * @return QJsonDocument + */ + [[nodiscard]] auto ToJson() const -> QJsonObject; + + /** + * @brief + * + * @param obj + * @return auto + */ + void FromJson(const QJsonObject& obj); + private: static auto version_compare(const QString& a, const QString& b) -> int; }; diff --git a/src/m_ver_check/UpdateTab.cpp b/src/m_ver_check/UpdateTab.cpp index 34951e5..7f4a321 100644 --- a/src/m_ver_check/UpdateTab.cpp +++ b/src/m_ver_check/UpdateTab.cpp @@ -31,9 +31,12 @@ #include "GFModuleCommonUtils.hpp" #include "GFSDKBasic.h" #include "GFSDKModule.h" -#include "GitHubVersionCheckTask.h" #include "VersionCheckingModule.h" +// +#include "BKTUSVersionCheckTask.h" +#include "GitHubVersionCheckTask.h" + UpdateTab::UpdateTab(QWidget* parent) : QWidget(parent), current_version_(GFProjectVersion()) { auto* layout = new QVBoxLayout(); @@ -56,13 +59,18 @@ UpdateTab::UpdateTab(QWidget* parent) upgrade_label_->setWordWrap(true); upgrade_label_->setOpenExternalLinks(true); upgrade_label_->setHidden(true); + pb_ = new QProgressBar(); pb_->setRange(0, 0); pb_->setTextVisible(false); + upgrade_info_layout->addWidget(upgrade_label_); - upgrade_info_layout->addWidget(pb_); upgrade_info_box_->setLayout(upgrade_info_layout); + check_update_btn_ = new QPushButton(tr("Check for Updates")); + check_update_btn_->setIcon(QIcon::fromTheme("view-refresh")); + check_update_btn_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + release_note_box_ = new QGroupBox(tr("Release Notes")); auto* release_note_layout = new QVBoxLayout(); release_note_viewer_ = new QTextEdit(); @@ -80,35 +88,25 @@ UpdateTab::UpdateTab(QWidget* parent) layout->addWidget(upgrade_info_box_); layout->addWidget(release_note_box_); - setLayout(layout); -} - -void UpdateTab::showEvent(QShowEvent* event) { - QWidget::showEvent(event); + auto* hbox = new QHBoxLayout(); + hbox->addWidget(pb_, 1); + hbox->addWidget(check_update_btn_, 0, Qt::AlignRight); + layout->addLayout(hbox); - auto is_loading_done = GFModuleRetrieveRTValueOrDefaultBool( - GFGetModuleID(), GFModuleStrDup("version.loading_done"), 0); + connect(check_update_btn_, &QPushButton::clicked, this, + &UpdateTab::slot_check_version_update); - if (is_loading_done == 0) { - auto* task = new GitHubVersionCheckTask(); - QObject::connect(task, &GitHubVersionCheckTask::SignalUpgradeVersion, - QThread::currentThread(), [this](const SoftwareVersion&) { - slot_show_version_status(); - }); - QObject::connect(task, &GitHubVersionCheckTask::SignalUpgradeVersion, task, - &QObject::deleteLater); - task->Run(); + setLayout(layout); - } else { - slot_show_version_status(); - } + slot_show_version_status(); } void UpdateTab::slot_show_version_status() { + check_update_btn_->setEnabled(true); this->pb_->setHidden(true); auto is_loading_done = GFModuleRetrieveRTValueOrDefaultBool( - GFGetModuleID(), GFModuleStrDup("version.loading_done"), 0); + GFGetModuleID(), DUP("version.loading_done"), 0); if (is_loading_done == 0) { MLogDebug("version info loading haven't been done yet."); @@ -116,8 +114,7 @@ void UpdateTab::slot_show_version_status() { upgrade_label_->setText( "<center>" + tr("Unable to retrieve the latest version information. This may be " - "due " - "to a network issue or the server being unavailable.") + + "due to a network issue or the server being unavailable.") + "</center><center>" + tr("Please check your internet connection or try again later.") + "</center><center>" + tr("Alternatively, you can visit the") + @@ -131,33 +128,33 @@ void UpdateTab::slot_show_version_status() { } auto is_need_upgrade = GFModuleRetrieveRTValueOrDefaultBool( - GFGetModuleID(), GFModuleStrDup("version.need_upgrade"), 0); + GFGetModuleID(), DUP("version.need_upgrade"), 0); auto is_current_version_publish_in_remote = GFModuleRetrieveRTValueOrDefaultBool( - GFGetModuleID(), - GFModuleStrDup("version.current_version_publish_in_remote"), 0); + GFGetModuleID(), DUP("version.current_version_publish_in_remote"), 0); auto is_git_commit_hash_mismatch = GFModuleRetrieveRTValueOrDefaultBool( - GFGetModuleID(), GFModuleStrDup("version.git_commit_hash_mismatch"), 0); + GFGetModuleID(), DUP("version.git_commit_hash_mismatch"), 0); auto is_current_commit_hash_publish_in_remote = GFModuleRetrieveRTValueOrDefaultBool( - GFGetModuleID(), - GFModuleStrDup("version.current_commit_hash_publish_in_remote"), 0); + GFGetModuleID(), DUP("version.current_commit_hash_publish_in_remote"), + 0); QString const latest_version = UDUP(GFModuleRetrieveRTValueOrDefault( - GFGetModuleID(), GFModuleStrDup("version.latest_version"), - GFModuleStrDup(""))); + GFGetModuleID(), DUP("version.latest_version"), DUP(""))); QString const release_note = UDUP(GFModuleRetrieveRTValueOrDefault( - GFGetModuleID(), GFModuleStrDup("version.release_note"), - GFModuleStrDup(""))); + GFGetModuleID(), DUP("version.release_note"), DUP(""))); + + QString const api = UDUP(GFModuleRetrieveRTValueOrDefault( + GFGetModuleID(), DUP("version.api"), DUP("Unknown"))); - FLOG_INFO("latest version from GitHub: %1", latest_version); + FLOG_INFO("latest version from remote: %1", latest_version); latest_version_label_->setText("<center><b>" + - tr("Latest Version From Github") + ": " + + tr("Latest Version From %1").arg(api) + ": " + latest_version + "</b></center>"); current_version_box_->show(); @@ -227,4 +224,46 @@ void UpdateTab::slot_show_version_status() { } } -auto UpdateTabFactory(void*) -> void* { return new UpdateTab(); }
\ No newline at end of file +auto UpdateTabFactory(void*) -> void* { return new UpdateTab(); } + +void UpdateTab::slot_check_version_update() { + check_update_btn_->setEnabled(false); + pb_->show(); + + auto api = UDUP(GFModuleRetrieveRTValueOrDefault( + DUP("ui"), DUP("settings.network.update_checking_api"), DUP("github"))); + + if (api == "bktus") { + auto* task = new BKTUSVersionCheckTask(); + connect(task, &BKTUSVersionCheckTask::SignalUpgradeVersion, + QThread::currentThread(), + [this](const SoftwareVersion&) { slot_show_version_status(); }); + connect(task, &BKTUSVersionCheckTask::SignalUpgradeVersion, task, + &QObject::deleteLater); + task->Run(); + } else { + auto* task = new GitHubVersionCheckTask(); + connect(task, &GitHubVersionCheckTask::SignalUpgradeVersion, + QThread::currentThread(), + [this](const SoftwareVersion&) { slot_show_version_status(); }); + connect(task, &GitHubVersionCheckTask::SignalUpgradeVersion, task, + &QObject::deleteLater); + task->Run(); + } +} + +void UpdateTab::showEvent(QShowEvent* event) { + QWidget::showEvent(event); + + auto is_loading_done = GFModuleRetrieveRTValueOrDefaultBool( + GFGetModuleID(), DUP("version.loading_done"), 0); + + auto prohibit = GFModuleRetrieveRTValueOrDefaultBool( + DUP("ui"), DUP("settings.network.prohibit_update_checking"), 0); + + if ((prohibit == 0) && is_loading_done == 0) { + slot_check_version_update(); + } else { + slot_show_version_status(); + } +} diff --git a/src/m_ver_check/UpdateTab.h b/src/m_ver_check/UpdateTab.h index fef54e6..9c969de 100644 --- a/src/m_ver_check/UpdateTab.h +++ b/src/m_ver_check/UpdateTab.h @@ -46,6 +46,7 @@ class UpdateTab : public QWidget { QGroupBox* release_note_box_; QGroupBox* upgrade_info_box_; QGroupBox* current_version_box_; + QPushButton* check_update_btn_; public: /** @@ -56,6 +57,11 @@ class UpdateTab : public QWidget { explicit UpdateTab(QWidget* parent = nullptr); protected: + /** + * @brief + * + * @param event + */ void showEvent(QShowEvent* event) override; private slots: @@ -66,6 +72,12 @@ class UpdateTab : public QWidget { */ void slot_show_version_status(); + /** + * @brief + * + */ + void slot_check_version_update(); + signals: /** * @brief diff --git a/src/m_ver_check/Utils.cpp b/src/m_ver_check/Utils.cpp index 4b45968..8ca1baa 100644 --- a/src/m_ver_check/Utils.cpp +++ b/src/m_ver_check/Utils.cpp @@ -69,6 +69,9 @@ void FillGrtWithVersionInfo(const SoftwareVersion& version) { GFModuleUpsertRTValue(GFGetModuleID(), GFModuleStrDup("version.release_note"), GFModuleStrDup(version.release_note.toUtf8())); + GFModuleUpsertRTValue(GFGetModuleID(), GFModuleStrDup("version.api"), + GFModuleStrDup(version.api.toUtf8())); + GFModuleUpsertRTValueBool(GFGetModuleID(), GFModuleStrDup("version.loading_done"), version.IsInfoValid() ? 1 : 0); diff --git a/src/m_ver_check/VersionCheckingModule.cpp b/src/m_ver_check/VersionCheckingModule.cpp index 2a37824..f86ad4c 100644 --- a/src/m_ver_check/VersionCheckingModule.cpp +++ b/src/m_ver_check/VersionCheckingModule.cpp @@ -43,9 +43,10 @@ #include "GitHubVersionCheckTask.h" #include "SoftwareVersion.h" #include "UpdateTab.h" +#include "Utils.h" GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.version_checking", - "VersionChecking", "1.3.1", + "VersionChecking", "1.4.1", "Try checking GpgFrontend version.", "Saturneric"); DEFINE_TRANSLATIONS_STRUCTURE(ModuleVersionChecking); @@ -71,30 +72,64 @@ auto GFActiveModule() -> int { return 0; } -EXECUTE_MODULE() { - FLOG_INFO("version checking module executing, event id: %1", - event["event_id"]); +namespace { - if (event["source"] == "bktus") { +auto CheckUpdate(const QMap<QString, QString>& event) -> int { + if (event["api"] == "bktus") { + MLogInfo("checking updating using api of bktus.com"); auto* task = new BKTUSVersionCheckTask(); - QObject::connect(task, &BKTUSVersionCheckTask::SignalUpgradeVersion, - QThread::currentThread(), - [event](const SoftwareVersion&) { CB_SUCC(event); }); + QObject::connect( + task, &BKTUSVersionCheckTask::SignalUpgradeVersion, + QThread::currentThread(), [event](const SoftwareVersion& sv) { + GFDurableCacheSave(DUP("update_checking_cache"), + DUP(QJsonDocument(sv.ToJson()).toJson())); + CB_SUCC(event); + }); QObject::connect(task, &BKTUSVersionCheckTask::SignalUpgradeVersion, task, &QObject::deleteLater); task->Run(); } else { + MLogInfo("checking updating using api of github.com"); auto* task = new GitHubVersionCheckTask(); - QObject::connect(task, &GitHubVersionCheckTask::SignalUpgradeVersion, - QThread::currentThread(), - [event](const SoftwareVersion&) { CB_SUCC(event); }); + QObject::connect( + task, &GitHubVersionCheckTask::SignalUpgradeVersion, + QThread::currentThread(), [event](const SoftwareVersion& sv) { + GFDurableCacheSave(DUP("update_checking_cache"), + DUP(QJsonDocument(sv.ToJson()).toJson())); + CB_SUCC(event); + }); QObject::connect(task, &GitHubVersionCheckTask::SignalUpgradeVersion, task, &QObject::deleteLater); task->Run(); } - return 0; } + +} // namespace + +EXECUTE_MODULE() { + FLOG_INFO("version checking module executing, event id: %1", + event["event_id"]); + + auto cache = UDUP(GFDurableCacheGet(DUP("update_checking_cache"))); + auto json = QJsonDocument::fromJson(cache.toUtf8()); + + if (!json.isEmpty() && json.isObject()) { + SoftwareVersion sv; + sv.FromJson(json.object()); + + FLOG_DEBUG("got software version meta data: %1", json.toJson()); + if (sv.timestamp.addDays(1) < QDateTime::currentDateTime()) { + return CheckUpdate(event); + } + + FillGrtWithVersionInfo(sv); + CB_SUCC(event); + return 0; + } + + return CheckUpdate(event); +} END_EXECUTE_MODULE() auto GFDeactivateModule() -> int { return 0; } |