aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2023-03-31 09:21:54 +0000
committerSaturneric <[email protected]>2023-03-31 09:21:54 +0000
commit4010a5662d5561b9a5b24fba183fd1906015afc4 (patch)
tree409a7af8ab8a8e27065e7e785c7bd9266741d8f1
parentfix: solve README issues (diff)
downloadGpgFrontend-4010a5662d5561b9a5b24fba183fd1906015afc4.tar.gz
GpgFrontend-4010a5662d5561b9a5b24fba183fd1906015afc4.zip
feat: improve core init speed and safty.
-rw-r--r--src/core/GpgCoreInit.cpp55
1 files changed, 38 insertions, 17 deletions
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp
index 34780d98..9395bac7 100644
--- a/src/core/GpgCoreInit.cpp
+++ b/src/core/GpgCoreInit.cpp
@@ -36,6 +36,10 @@
#include "core/GpgContext.h"
#include "core/function/GlobalSettingStation.h"
#include "function/gpg/GpgAdvancedOperator.h"
+#include "spdlog/spdlog.h"
+#include "thread/Task.h"
+#include "thread/TaskRunner.h"
+#include "thread/TaskRunnerGetter.h"
namespace GpgFrontend {
@@ -147,7 +151,7 @@ void init_gpgfrontend_core() {
custom_key_database_path);
// init default channel
- GpgFrontend::GpgContext::CreateInstance(
+ auto& default_ctx = GpgFrontend::GpgContext::CreateInstance(
GPGFRONTEND_DEFAULT_CHANNEL, [=]() -> std::unique_ptr<ChannelObject> {
GpgFrontend::GpgContextInitArgs args;
@@ -162,22 +166,39 @@ void init_gpgfrontend_core() {
return std::unique_ptr<ChannelObject>(new GpgContext(args));
});
- // init non-ascii channel
- GpgFrontend::GpgContext::CreateInstance(
- GPGFRONTEND_NON_ASCII_CHANNEL, [=]() -> std::unique_ptr<ChannelObject> {
- GpgFrontend::GpgContextInitArgs args;
- args.ascii = false;
-
- // set key database path
- if (use_custom_key_database_path && !custom_key_database_path.empty()) {
- args.db_path = custom_key_database_path;
- }
-
- args.offline_mode = forbid_all_gnupg_connection;
- args.auto_import_missing_key = auto_import_missing_key;
-
- return std::unique_ptr<ChannelObject>(new GpgContext(args));
- });
+ // exit if failed
+ if (!default_ctx.good()) {
+ SPDLOG_ERROR("default gpgme context init error, exit.");
+ QCoreApplication::exit();
+ };
+
+ // async init no-ascii channel
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_GPG)
+ ->PostTask(
+ new Thread::Task([=](Thread::Task::DataObjectPtr data_obj) -> int {
+ // init non-ascii channel
+ auto& ctx = GpgFrontend::GpgContext::CreateInstance(
+ GPGFRONTEND_NON_ASCII_CHANNEL,
+ [=]() -> std::unique_ptr<ChannelObject> {
+ GpgFrontend::GpgContextInitArgs args;
+ args.ascii = false;
+
+ // set key database path
+ if (use_custom_key_database_path &&
+ !custom_key_database_path.empty()) {
+ args.db_path = custom_key_database_path;
+ }
+
+ args.offline_mode = forbid_all_gnupg_connection;
+ args.auto_import_missing_key = auto_import_missing_key;
+
+ return std::unique_ptr<ChannelObject>(new GpgContext(args));
+ });
+ if (!ctx.good()) SPDLOG_ERROR("no-ascii channel init error");
+
+ return ctx.good() ? 0 : -1;
+ }));
// try to restart all components
GpgFrontend::GpgAdvancedOperator::GetInstance().RestartGpgComponents();