aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-05-20 18:07:02 +0000
committerSaturneric <[email protected]>2022-05-20 18:07:02 +0000
commita62eed1038f618ec294fbfefe8ef5c8427147a33 (patch)
tree34e07f6029b25987aa46378ede668b9a7b4f755c
parentfeat: handle application's exceptions and crash (diff)
downloadGpgFrontend-a62eed1038f618ec294fbfefe8ef5c8427147a33.tar.gz
GpgFrontend-a62eed1038f618ec294fbfefe8ef5c8427147a33.zip
refactor: change version checking to task
-rw-r--r--src/ui/main_window/MainWindow.cpp18
-rw-r--r--src/ui/main_window/MainWindow.h46
-rw-r--r--src/ui/struct/SoftwareVersion.h8
-rw-r--r--src/ui/thread/VersionCheckTask.cpp (renamed from src/ui/thread/VersionCheckThread.cpp)136
-rw-r--r--src/ui/thread/VersionCheckTask.h (renamed from src/ui/thread/VersionCheckThread.h)33
5 files changed, 152 insertions, 89 deletions
diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp
index f4d2c7da..d04e3dbd 100644
--- a/src/ui/main_window/MainWindow.cpp
+++ b/src/ui/main_window/MainWindow.cpp
@@ -28,13 +28,11 @@
#include "MainWindow.h"
-#include "ui/UserInterfaceUtils.h"
-#ifdef RELEASE
-#include "ui/thread/VersionCheckThread.h"
-#endif
#include "core/function/GlobalSettingStation.h"
#include "ui/SignalStation.h"
+#include "ui/UserInterfaceUtils.h"
#include "ui/struct/SettingsObject.h"
+#include "ui/thread/VersionCheckTask.h"
namespace GpgFrontend::UI {
@@ -114,16 +112,14 @@ void MainWindow::Init() noexcept {
// if not prohibit update checking
if (!prohibit_update_checking_) {
-#ifdef RELEASE
- auto version_thread = new VersionCheckThread();
+ auto *version_task = new VersionCheckTask();
- connect(version_thread, &VersionCheckThread::finished, version_thread,
- &VersionCheckThread::deleteLater);
- connect(version_thread, &VersionCheckThread::SignalUpgradeVersion, this,
+ connect(version_task, &VersionCheckTask::SignalUpgradeVersion, this,
&MainWindow::slot_version_upgrade);
- version_thread->start();
-#endif
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Network)
+ ->PostTask(version_task);
}
} catch (...) {
diff --git a/src/ui/main_window/MainWindow.h b/src/ui/main_window/MainWindow.h
index c5ba02a2..a0f1a5d4 100644
--- a/src/ui/main_window/MainWindow.h
+++ b/src/ui/main_window/MainWindow.h
@@ -53,15 +53,14 @@ class MainWindow : public QMainWindow {
Q_OBJECT
public:
-
- struct CryptoMenu{
+ struct CryptoMenu {
using OperationType = unsigned int;
static constexpr OperationType None = 0;
static constexpr OperationType Encrypt = 1 << 0;
static constexpr OperationType Sign = 1 << 1;
static constexpr OperationType Decrypt = 1 << 2;
- static constexpr OperationType Verify = 1 << 3;
+ static constexpr OperationType Verify = 1 << 3;
static constexpr OperationType EncryptAndSign = 1 << 4;
static constexpr OperationType DecryptAndVerify = 1 << 5;
};
@@ -89,7 +88,6 @@ class MainWindow : public QMainWindow {
*/
void SignalLoaded();
-
public slots:
/**
@@ -371,26 +369,26 @@ class MainWindow : public QMainWindow {
append_selected_keys_act_{}; ///< Action to append selected keys to edit
QAction* copy_mail_address_to_clipboard_act_{}; ///< Action to copy mail to
///< clipboard
- QAction* open_key_management_act_{}; ///< Action to open key management
- QAction* copy_act_{}; ///< Action to copy text
- QAction* quote_act_{}; ///< Action to quote text
- QAction* cut_act_{}; ///< Action to cut text
- QAction* paste_act_{}; ///< Action to paste text
- QAction* select_all_act_{}; ///< Action to select whole text
- QAction* find_act_{}; ///< Action to find text
- QAction* undo_act_{}; ///< Action to undo last action
- QAction* redo_act_{}; ///< Action to redo last action
- QAction* zoom_in_act_{}; ///< Action to zoom in
- QAction* zoom_out_act_{}; ///< Action to zoom out
- QAction* about_act_{}; ///< Action to open about dialog
- QAction* check_update_act_{}; ///< Action to open about dialog
- QAction* translate_act_{}; ///< Action to open about dialog
- QAction* open_settings_act_{}; ///< Action to open settings dialog
- QAction* show_key_details_act_{}; ///< Action to open key-details dialog
- QAction* start_wizard_act_{}; ///< Action to open the wizard
- QAction* cut_pgp_header_act_{}; ///< Action for cutting the PGP header
- QAction* add_pgp_header_act_{}; ///< Action for adding the PGP header
- QAction* import_key_from_file_act_{}; ///<
+ QAction* open_key_management_act_{}; ///< Action to open key management
+ QAction* copy_act_{}; ///< Action to copy text
+ QAction* quote_act_{}; ///< Action to quote text
+ QAction* cut_act_{}; ///< Action to cut text
+ QAction* paste_act_{}; ///< Action to paste text
+ QAction* select_all_act_{}; ///< Action to select whole text
+ QAction* find_act_{}; ///< Action to find text
+ QAction* undo_act_{}; ///< Action to undo last action
+ QAction* redo_act_{}; ///< Action to redo last action
+ QAction* zoom_in_act_{}; ///< Action to zoom in
+ QAction* zoom_out_act_{}; ///< Action to zoom out
+ QAction* about_act_{}; ///< Action to open about dialog
+ QAction* check_update_act_{}; ///< Action to open about dialog
+ QAction* translate_act_{}; ///< Action to open about dialog
+ QAction* open_settings_act_{}; ///< Action to open settings dialog
+ QAction* show_key_details_act_{}; ///< Action to open key-details dialog
+ QAction* start_wizard_act_{}; ///< Action to open the wizard
+ QAction* cut_pgp_header_act_{}; ///< Action for cutting the PGP header
+ QAction* add_pgp_header_act_{}; ///< Action for adding the PGP header
+ QAction* import_key_from_file_act_{}; ///<
QAction* import_key_from_clipboard_act_{}; ///<
QAction* import_key_from_key_server_act_{}; ///<
diff --git a/src/ui/struct/SoftwareVersion.h b/src/ui/struct/SoftwareVersion.h
index 04300053..da93f8c6 100644
--- a/src/ui/struct/SoftwareVersion.h
+++ b/src/ui/struct/SoftwareVersion.h
@@ -54,6 +54,14 @@ struct SoftwareVersion {
* @return true
* @return false
*/
+ [[nodiscard]] bool InfoVaild() const { return load_info_done; }
+
+ /**
+ * @brief
+ *
+ * @return true
+ * @return false
+ */
[[nodiscard]] bool NeedUpgrade() const {
return load_info_done && !latest_prerelease && !latest_draft &&
current_version < latest_version;
diff --git a/src/ui/thread/VersionCheckThread.cpp b/src/ui/thread/VersionCheckTask.cpp
index 6d73417d..7de3b511 100644
--- a/src/ui/thread/VersionCheckThread.cpp
+++ b/src/ui/thread/VersionCheckTask.cpp
@@ -26,45 +26,58 @@
*
*/
-#include "VersionCheckThread.h"
+#include "VersionCheckTask.h"
#include <QMetaType>
-#include <nlohmann/json.hpp>
+#include <memory>
#include "GpgFrontendBuildInfo.h"
namespace GpgFrontend::UI {
-void VersionCheckThread::run() {
- auto current_version = std::string("v") + std::to_string(VERSION_MAJOR) +
- "." + std::to_string(VERSION_MINOR) + "." +
- std::to_string(VERSION_PATCH);
-
- SoftwareVersion version;
- version.current_version = current_version;
+VersionCheckTask::VersionCheckTask()
+ : network_manager_(new QNetworkAccessManager(this)),
+ current_version_(std::string("v") + std::to_string(VERSION_MAJOR) + "." +
+ std::to_string(VERSION_MINOR) + "." +
+ std::to_string(VERSION_PATCH)) {
+ qRegisterMetaType<SoftwareVersion>("SoftwareVersion");
+ version_.current_version = current_version_;
+}
- auto manager = std::make_unique<QNetworkAccessManager>(nullptr);
+void VersionCheckTask::Run() {
+ SetFinishAfterRun(false);
try {
using namespace nlohmann;
- LOG(INFO) << "current version" << current_version;
-
+ LOG(INFO) << "current version" << current_version_;
std::string latest_version_url =
"https://api.github.com/repos/saturneric/gpgfrontend/releases/latest";
- std::string current_version_url =
- "https://api.github.com/repos/saturneric/gpgfrontend/releases/tags/" +
- current_version;
- QNetworkRequest latest_request, current_request;
+ QNetworkRequest latest_request;
latest_request.setUrl(QUrl(latest_version_url.c_str()));
- current_request.setUrl(QUrl(current_version_url.c_str()));
- auto _reply = manager->get(latest_request);
- while (_reply->isRunning()) QApplication::processEvents();
- if (_reply->error() != QNetworkReply::NoError) {
- LOG(ERROR) << "current version request error";
- version.latest_version = current_version;
+ latest_reply_ = network_manager_->get(latest_request);
+ connect(latest_reply_, &QNetworkReply::finished, this,
+ &VersionCheckTask::slot_parse_latest_version_info);
+
+ // loading done
+ version_.load_info_done = true;
+
+ } catch (...) {
+ emit SignalTaskFinished();
+ }
+}
+
+void VersionCheckTask::slot_parse_latest_version_info() {
+ version_.current_version = current_version_;
+
+ try {
+ if (latest_reply_ == nullptr ||
+ latest_reply_->error() != QNetworkReply::NoError) {
+ LOG(ERROR) << "latest version request error";
+ version_.latest_version = current_version_;
} else {
- latest_reply_bytes_ = _reply->readAll();
+ latest_reply_bytes_ = latest_reply_->readAll();
+
auto latest_reply_json =
nlohmann::json::parse(latest_reply_bytes_.toStdString());
@@ -78,7 +91,7 @@ void VersionCheckThread::run() {
latest_version = version_match.captured(0).toStdString();
LOG(INFO) << "latest version matched" << latest_version;
} else {
- latest_version = current_version;
+ latest_version = current_version_;
LOG(WARNING) << "latest version unknown";
}
@@ -86,44 +99,69 @@ void VersionCheckThread::run() {
draft = latest_reply_json["draft"];
std::string publish_date = latest_reply_json["published_at"];
std::string release_note = latest_reply_json["body"];
- version.latest_version = latest_version;
- version.latest_prerelease = prerelease;
- version.latest_draft = draft;
- version.publish_date = publish_date;
- version.release_note = release_note;
+ version_.latest_version = latest_version;
+ version_.latest_prerelease = prerelease;
+ version_.latest_draft = draft;
+ version_.publish_date = publish_date;
+ version_.release_note = release_note;
}
+ } catch (...) {
+ LOG(INFO) << "error occurred";
+ version_.load_info_done = false;
+ }
- _reply->deleteLater();
+ if (latest_reply_ != nullptr) {
+ latest_reply_->deleteLater();
+ }
- _reply = manager->get(current_request);
- while (_reply->isRunning()) QApplication::processEvents();
- current_reply_bytes_ = _reply->readAll();
- if (_reply->error() != QNetworkReply::NoError) {
+ try {
+ std::string current_version_url =
+ "https://api.github.com/repos/saturneric/gpgfrontend/releases/tags/" +
+ current_version_;
+
+ QNetworkRequest current_request;
+ current_request.setUrl(QUrl(current_version_url.c_str()));
+ current_reply_ = network_manager_->get(current_request);
+
+ connect(current_reply_, &QNetworkReply::finished, this,
+ &VersionCheckTask::slot_parse_current_version_info);
+ } catch (...) {
+ LOG(ERROR) << "current version request create error";
+ emit SignalTaskFinished();
+ }
+}
+
+void VersionCheckTask::slot_parse_current_version_info() {
+ try {
+ if (current_reply_ == nullptr ||
+ current_reply_->error() != QNetworkReply::NoError) {
LOG(ERROR) << "current version request network error";
- version.current_version_found = false;
+ version_.current_version_found = false;
} else {
- version.current_version_found = true;
+ version_.current_version_found = true;
+ current_reply_bytes_ = current_reply_->readAll();
+ LOG(INFO) << "current version" << current_reply_bytes_.size();
auto current_reply_json =
nlohmann::json::parse(current_reply_bytes_.toStdString());
bool current_prerelease = current_reply_json["prerelease"],
current_draft = current_reply_json["draft"];
- version.latest_prerelease = current_prerelease;
- version.latest_draft = current_draft;
+ version_.latest_prerelease = current_prerelease;
+ version_.latest_draft = current_draft;
+ version_.load_info_done = true;
}
- _reply->deleteLater();
-
- // loading done
- version.load_info_done = true;
-
} catch (...) {
LOG(INFO) << "error occurred";
- version.load_info_done = false;
+ version_.load_info_done = false;
}
- emit SignalUpgradeVersion(version);
-}
-VersionCheckThread::VersionCheckThread() : QThread(nullptr) {
- qRegisterMetaType<SoftwareVersion>("SoftwareVersion");
-};
+ LOG(INFO) << "current version parse done" << version_.current_version_found;
+
+ if (current_reply_ != nullptr) {
+ current_reply_->deleteLater();
+ }
+
+ emit SignalUpgradeVersion(version_);
+ emit SignalTaskFinished();
+}
} // namespace GpgFrontend::UI
diff --git a/src/ui/thread/VersionCheckThread.h b/src/ui/thread/VersionCheckTask.h
index 6ad35afe..0dbce17f 100644
--- a/src/ui/thread/VersionCheckThread.h
+++ b/src/ui/thread/VersionCheckTask.h
@@ -29,6 +29,10 @@
#ifndef GPGFRONTEND_VERSIONCHECKTHREAD_H
#define GPGFRONTEND_VERSIONCHECKTHREAD_H
+#include <memory>
+#include <string>
+
+#include "core/thread/Task.h"
#include "ui/GpgFrontendUI.h"
#include "ui/struct/SoftwareVersion.h"
@@ -38,7 +42,7 @@ namespace GpgFrontend::UI {
* @brief
*
*/
-class VersionCheckThread : public QThread {
+class VersionCheckTask : public Thread::Task {
Q_OBJECT
public:
@@ -46,7 +50,7 @@ class VersionCheckThread : public QThread {
* @brief Construct a new Version Check Thread object
*
*/
- explicit VersionCheckThread();
+ explicit VersionCheckTask();
signals:
@@ -63,11 +67,30 @@ class VersionCheckThread : public QThread {
*
*/
- void run() override;
+ void Run() override;
+
+ private slots:
+
+ /**
+ * @brief
+ *
+ */
+ void slot_parse_latest_version_info();
+
+ /**
+ * @brief
+ *
+ */
+ void slot_parse_current_version_info();
private:
- QByteArray latest_reply_bytes_; ///<
- QByteArray current_reply_bytes_; ///<
+ QByteArray latest_reply_bytes_; ///<
+ QByteArray current_reply_bytes_; ///<
+ QNetworkReply* latest_reply_ = nullptr; ///< latest version info reply
+ QNetworkReply* current_reply_ = nullptr; ///< current version info reply
+ QNetworkAccessManager* network_manager_; ///<
+ std::string current_version_;
+ SoftwareVersion version_;
};
} // namespace GpgFrontend::UI