From f7a00c58d2824f49ecaafc0152fc0b8213772e46 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 13 Dec 2024 16:22:33 +0100 Subject: refactor: using qt containers instead of std containers --- src/core/GpgCoreInit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 0659377a..7199bd2d 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -616,7 +616,7 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { continue; } - if (!GpgKeyGetter::GetInstance(ctx.GetChannel()).FetchKey()) { + if (!GpgKeyGetter::GetInstance(ctx.GetChannel()).FlushKeyCache()) { FLOG_E() << "gpgme context init key cache failed, index:" << channel_index; continue; -- cgit v1.2.3 From 3931b64fddffbb12b3276acbdd14a834432b5104 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 21 Jan 2025 23:44:36 +0100 Subject: feat: enhance key db functions 1. allow using relative key db path 2. allow editing default key db 3. use a key db in own directory at portable mode --- src/core/GpgCoreInit.cpp | 91 +++++++----------------------------------------- 1 file changed, 12 insertions(+), 79 deletions(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 7199bd2d..1b987bc3 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -54,11 +54,6 @@ auto VerifyGpgconfPath(const QFileInfo& gnupg_install_fs_path) -> bool { gnupg_install_fs_path.isFile(); } -auto VerifyKeyDatabasePath(const QFileInfo& key_database_fs_path) -> bool { - return key_database_fs_path.isAbsolute() && key_database_fs_path.exists() && - key_database_fs_path.isDir(); -} - auto SearchGpgconfPath(const QList& candidate_paths) -> QString { for (const auto& path : candidate_paths) { if (VerifyGpgconfPath(QFileInfo(path))) { @@ -69,17 +64,12 @@ auto SearchGpgconfPath(const QList& candidate_paths) -> QString { return {}; } -auto SearchKeyDatabasePath(const QList& candidate_paths) -> QString { - for (const auto& path : candidate_paths) { - if (VerifyKeyDatabasePath(QFileInfo(path))) { - // return a unify path - return QFileInfo(path).absoluteFilePath(); - } +auto GetDefaultKeyDatabasePath(const QString& gpgconf_path) -> QString { + // portable mode + if (GlobalSettingStation::GetInstance().IsProtableMode()) { + return GlobalSettingStation::GetInstance().GetAppDataPath(); } - return {}; -} -auto GetDefaultKeyDatabasePath(const QString& gpgconf_path) -> QString { if (gpgconf_path.isEmpty()) return {}; QFileInfo info(gpgconf_path); @@ -425,45 +415,6 @@ auto InitBasicPath() -> bool { return true; } -auto GetKeyDatabasesBySettings(QString& default_home_path) - -> QList { - auto key_db_list_so = SettingsObject("key_database_list"); - auto key_db_list = KeyDatabaseListSO(key_db_list_so); - auto key_dbs = key_db_list.key_databases; - -#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) - key_dbs.removeIf( - [default_home_path](const KeyDatabaseItemSO& key_database) -> bool { - return key_database.path == default_home_path; - }); -#else - for (auto iter = key_dbs.begin(); iter != key_dbs.end();) { - if (iter->path == default_home_path) { - iter = key_dbs.erase(iter); - } else { - ++iter; - } - } -#endif - - key_db_list_so.Store(key_db_list.ToJson()); - return key_dbs; -} - -auto GetKeyDatabaseInfoBySettings(QString& default_home_path) - -> QList { - auto key_dbs = GetKeyDatabasesBySettings(default_home_path); - QList infos; - for (const auto& key_db : key_dbs) { - KeyDatabaseInfo info; - info.name = key_db.name; - info.path = key_db.path; - info.channel = -1; - infos.append(info); - } - return infos; -} - auto InitGpgFrontendCore(CoreInitArgs args) -> int { // initialize gpgme if (!InitGpgME()) { @@ -519,16 +470,16 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { return 0; } + auto key_dbs = GetKeyDatabaseInfoBySettings(); + // load default context auto& default_ctx = GpgFrontend::GpgContext::CreateInstance( kGpgFrontendDefaultChannel, [=]() -> ChannelObjectPtr { GpgFrontend::GpgContextInitArgs args; - // set key database path - if (!default_home_path.isEmpty()) { - args.db_name = "DEFAULT"; - args.db_path = default_home_path; - } + const auto& default_key_db_info = key_dbs.front(); + args.db_name = default_key_db_info.name; + args.db_path = default_key_db_info.path; args.offline_mode = forbid_all_gnupg_connection; args.auto_import_missing_key = auto_import_missing_key; @@ -564,30 +515,12 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { CoreSignalStation::GetInstance()->SignalGoodGnupgEnv(); LOG_I() << "Basic ENV Checking Finished"; - auto key_dbs = GetKeyDatabasesBySettings(default_home_path); - auto* task = new Thread::Task( [=](const DataObjectPtr&) -> int { - // key database path - QList buffered_key_dbs; - - // try to use user defined key database - if (!key_dbs.empty()) { - for (const auto& key_database : key_dbs) { - if (VerifyKeyDatabasePath(QFileInfo(key_database.path))) { - auto key_database_fs_path = - QFileInfo(key_database.path).absoluteFilePath(); - LOG_D() << "load gpg key database: " << key_database.path; - buffered_key_dbs.append(key_database); - } else { - LOG_W() << "gpg key database path is not suitable: " - << key_database.path; - } - } - } - int channel_index = kGpgFrontendDefaultChannel + 1; - for (const auto& key_db : buffered_key_dbs) { + for (int i = 1; i < key_dbs.size(); i++) { + const auto& key_db = key_dbs[i]; + // init ctx, also checking the basic env auto& ctx = GpgFrontend::GpgContext::CreateInstance( channel_index, [=]() -> ChannelObjectPtr { -- cgit v1.2.3 From 56140c6bdc567bf85c5916a36d707196a52b9272 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 21 Jan 2025 23:59:35 +0100 Subject: fix: make app fully portable --- src/core/GpgCoreInit.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 1b987bc3..5c535319 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -67,7 +67,7 @@ auto SearchGpgconfPath(const QList& candidate_paths) -> QString { auto GetDefaultKeyDatabasePath(const QString& gpgconf_path) -> QString { // portable mode if (GlobalSettingStation::GetInstance().IsProtableMode()) { - return GlobalSettingStation::GetInstance().GetAppDataPath(); + return GlobalSettingStation::GetInstance().GetAppDataPath() + "/db"; } if (gpgconf_path.isEmpty()) return {}; @@ -402,6 +402,8 @@ auto InitBasicPath() -> bool { return false; } + if (!QDir(default_home_path).exists()) QDir(default_home_path).mkpath("."); + RefreshGpgMEBackendEngine(target_gpgconf_path, target_gnupg_path, default_home_path); -- cgit v1.2.3 From 731ec7339dc6f251a814d4aef59c05b1900ecf3f Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 26 Jan 2025 19:40:43 +0100 Subject: fix: improve code compatibility --- src/core/GpgCoreInit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 5c535319..86d68af4 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -54,7 +54,7 @@ auto VerifyGpgconfPath(const QFileInfo& gnupg_install_fs_path) -> bool { gnupg_install_fs_path.isFile(); } -auto SearchGpgconfPath(const QList& candidate_paths) -> QString { +auto SearchGpgconfPath(const QStringList& candidate_paths) -> QString { for (const auto& path : candidate_paths) { if (VerifyGpgconfPath(QFileInfo(path))) { // return a unify path -- cgit v1.2.3 From af1870fd422fc615a7039b998f505f100e98474a Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 27 Jan 2025 16:39:09 +0100 Subject: fix: kill all gnupg daemons in a proper way --- src/core/GpgCoreInit.cpp | 68 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 21 deletions(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 86d68af4..d6de2240 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -36,9 +36,7 @@ #include "core/function/gpg/GpgAdvancedOperator.h" #include "core/function/gpg/GpgContext.h" #include "core/function/gpg/GpgKeyGetter.h" -#include "core/model/SettingsObject.h" #include "core/module/ModuleManager.h" -#include "core/struct/settings_object/KeyDatabaseListSO.h" #include "core/thread/Task.h" #include "core/thread/TaskRunnerGetter.h" #include "core/utils/CommonUtils.h" @@ -47,7 +45,13 @@ namespace GpgFrontend { -void DestroyGpgFrontendCore() { SingletonStorageCollection::Destroy(); } +void DestroyGpgFrontendCore() { + // stop all task runner + Thread::TaskRunnerGetter::GetInstance().StopAllTeakRunner(); + + // destroy all singleton objects + SingletonStorageCollection::Destroy(); +} auto VerifyGpgconfPath(const QFileInfo& gnupg_install_fs_path) -> bool { return gnupg_install_fs_path.isAbsolute() && gnupg_install_fs_path.exists() && @@ -260,7 +264,8 @@ auto RefreshGpgMEBackendEngine(const QString& gpgconf_path, return true; } -auto GetGnuPGPathByGpgConf(const QString& gpgconf_install_fs_path) -> QString { +auto GetComponentPathsByGpgConf(const QString& gpgconf_install_fs_path) + -> bool { auto* process = new QProcess(QCoreApplication::instance()); process->setProgram(gpgconf_install_fs_path); process->start(); @@ -268,7 +273,7 @@ auto GetGnuPGPathByGpgConf(const QString& gpgconf_install_fs_path) -> QString { auto output_buffer = process->readAllStandardOutput(); process->deleteLater(); - if (output_buffer.isEmpty()) return {}; + if (output_buffer.isEmpty()) return false; auto line_split_list = QString(output_buffer).split("\n"); for (const auto& line : line_split_list) { @@ -276,23 +281,26 @@ auto GetGnuPGPathByGpgConf(const QString& gpgconf_install_fs_path) -> QString { if (info_split_list.size() != 3) continue; - auto component_name = info_split_list[0].trimmed(); + auto component_name = info_split_list[0].trimmed().toLower(); auto component_desc = info_split_list[1].trimmed(); auto component_path = info_split_list[2].trimmed(); - if (component_name.toLower() == "gpg") { #if defined(_WIN32) || defined(WIN32) - // replace some special substrings on windows platform - component_path.replace("%3a", ":"); + // replace some special substrings on windows platform + component_path.replace("%3a", ":"); #endif - QFileInfo file_info(component_path); - if (file_info.exists() && file_info.isFile()) { - return file_info.absoluteFilePath(); - } - return {}; - } + + QFileInfo file_info(component_path); + if (!file_info.exists() || !file_info.isFile()) continue; + + Module::UpsertRTValue("core", "gnupg.component.paths." + component_name, + file_info.absoluteFilePath()); + + LOG_D() << "gpg components: " << component_name + << "path: " << file_info.absoluteFilePath(); } - return ""; + + return true; } auto DecideGpgConfPath(const QString& default_gpgconf_path) -> QString { @@ -349,8 +357,15 @@ auto DecideGpgConfPath(const QString& default_gpgconf_path) -> QString { return ""; } -auto DecideGnuPGPath(const QString& gpgconf_path) -> QString { - return GetGnuPGPathByGpgConf(gpgconf_path); +auto DecideGnuPGPath(const QString& default_gnupg_path) -> QString { + QFileInfo info(default_gnupg_path); + + if (default_gnupg_path.isEmpty() || !info.exists() || !info.isFile()) { + return Module::RetrieveRTValueTypedOrDefault<>( + "core", "gnupg.component.paths.gpg", QString{}); + } + + return default_gnupg_path; } auto InitBasicPath() -> bool { @@ -364,12 +379,23 @@ auto InitBasicPath() -> bool { LOG_I() << "default gnupg path found by gpgme: " << default_gnupg_path; auto target_gpgconf_path = DecideGpgConfPath(default_gpgconf_path); + + if (!GetComponentPathsByGpgConf(default_gpgconf_path)) { + LOG_E() << "Cannot get components paths by gpgconf!" + << "GpgFrontend cannot start under this situation!"; + CoreSignalStation::GetInstance()->SignalBadGnupgEnv( + QCoreApplication::tr("Cannot get Infos from GpgConf")); + return false; + } + auto target_gnupg_path = default_gnupg_path; + if (target_gpgconf_path != default_gpgconf_path) { - target_gnupg_path = DecideGnuPGPath(target_gpgconf_path); + target_gnupg_path = DecideGnuPGPath(target_gnupg_path); } + LOG_I() << "gpgconf path used: " << target_gpgconf_path; - LOG_I() << "gnupg path provided by gpgconf: " << target_gnupg_path; + LOG_I() << "gnupg path used: " << target_gnupg_path; if (target_gpgconf_path.isEmpty()) { LOG_E() << "Cannot find gpgconf!" @@ -577,7 +603,7 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { ->PostTask(task); if (!args.unit_test_mode && restart_all_gnupg_components_on_start) { - GpgAdvancedOperator::RestartGpgComponents(); + GpgAdvancedOperator::RestartGpgComponents(nullptr); } return 0; } -- cgit v1.2.3 From 5d2b02097eeb6ed166611a22cef66a8c56b7c997 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 27 Jan 2025 16:51:27 +0100 Subject: refactor: clean up code --- src/core/GpgCoreInit.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index d6de2240..7088dd99 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -304,7 +304,7 @@ auto GetComponentPathsByGpgConf(const QString& gpgconf_install_fs_path) } auto DecideGpgConfPath(const QString& default_gpgconf_path) -> QString { - auto settings = GlobalSettingStation::GetInstance().GetSettings(); + auto settings = GetSettings(); auto use_custom_gnupg_install_path = settings.value("gnupg/use_custom_gnupg_install_path", false).toBool(); auto custom_gnupg_install_path = @@ -470,7 +470,7 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { auto default_home_path = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.default_database_path", QString{}); - auto settings = GlobalSettingStation::GetInstance().GetSettings(); + auto settings = GetSettings(); // read settings from config file auto forbid_all_gnupg_connection = -- cgit v1.2.3 From a0cafd633df5255cf19a95f4f00124cbb9a5f71c Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 27 Jan 2025 18:21:01 +0100 Subject: feat: check gpg components before start --- src/core/GpgCoreInit.cpp | 60 +++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 21 deletions(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 7088dd99..b564307a 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -69,27 +69,36 @@ auto SearchGpgconfPath(const QStringList& candidate_paths) -> QString { } auto GetDefaultKeyDatabasePath(const QString& gpgconf_path) -> QString { + QString default_db_path; + // portable mode if (GlobalSettingStation::GetInstance().IsProtableMode()) { - return GlobalSettingStation::GetInstance().GetAppDataPath() + "/db"; - } + default_db_path = + GlobalSettingStation::GetInstance().GetAppDataPath() + "/db"; + } else { + if (gpgconf_path.isEmpty()) return {}; - if (gpgconf_path.isEmpty()) return {}; + QFileInfo info(gpgconf_path); + if (!info.exists() || !info.isFile()) return {}; - QFileInfo info(gpgconf_path); - if (!info.exists() || !info.isFile()) return {}; + auto* p = new QProcess(QCoreApplication::instance()); + p->setProgram(info.absoluteFilePath()); + p->setArguments({"--list-dirs", "homedir"}); + p->start(); - auto* p = new QProcess(QCoreApplication::instance()); - p->setProgram(info.absoluteFilePath()); - p->setArguments({"--list-dirs", "homedir"}); - p->start(); + p->waitForFinished(); + default_db_path = p->readAll().trimmed(); + p->deleteLater(); + } - p->waitForFinished(); - auto home_path = p->readAll().trimmed(); - p->deleteLater(); + QFileInfo info(default_db_path); + default_db_path = info.absoluteFilePath(); - QFileInfo home_info(home_path); - return home_info.absoluteFilePath(); + // update GRT + Module::UpsertRTValue("core", "gpgme.ctx.default_database_path", + default_db_path); + + return default_db_path; } auto InitGpgME() -> bool { @@ -268,8 +277,10 @@ auto GetComponentPathsByGpgConf(const QString& gpgconf_install_fs_path) -> bool { auto* process = new QProcess(QCoreApplication::instance()); process->setProgram(gpgconf_install_fs_path); + process->setArguments({"--check-programs"}); process->start(); - process->waitForFinished(1000); + process->waitForFinished(30000); + auto output_buffer = process->readAllStandardOutput(); process->deleteLater(); @@ -279,24 +290,31 @@ auto GetComponentPathsByGpgConf(const QString& gpgconf_install_fs_path) for (const auto& line : line_split_list) { auto info_split_list = line.split(":"); - if (info_split_list.size() != 3) continue; + if (info_split_list.size() != 6) continue; auto component_name = info_split_list[0].trimmed().toLower(); auto component_desc = info_split_list[1].trimmed(); auto component_path = info_split_list[2].trimmed(); + auto exists = info_split_list[3].trimmed(); + auto runnable = info_split_list[4].trimmed(); #if defined(_WIN32) || defined(WIN32) // replace some special substrings on windows platform component_path.replace("%3a", ":"); #endif + if (exists != "1" || runnable != "1") continue; + QFileInfo file_info(component_path); if (!file_info.exists() || !file_info.isFile()) continue; - Module::UpsertRTValue("core", "gnupg.component.paths." + component_name, - file_info.absoluteFilePath()); + Module::UpsertRTValue( + "core", QString("gnupg.components.%1.checked").arg(component_name), 1); + Module::UpsertRTValue( + "core", QString("gnupg.components.%1.path").arg(component_name), + file_info.absoluteFilePath()); - LOG_D() << "gpg components: " << component_name + LOG_D() << "gpg components checked: " << component_name << "path: " << file_info.absoluteFilePath(); } @@ -362,7 +380,7 @@ auto DecideGnuPGPath(const QString& default_gnupg_path) -> QString { if (default_gnupg_path.isEmpty() || !info.exists() || !info.isFile()) { return Module::RetrieveRTValueTypedOrDefault<>( - "core", "gnupg.component.paths.gpg", QString{}); + "core", "gnupg.components.gpg.path", QString{}); } return default_gnupg_path; @@ -424,7 +442,7 @@ auto InitBasicPath() -> bool { LOG_E() << "Cannot find default home path by gpgconf!" << "GpgFrontend cannot start under this situation!"; CoreSignalStation::GetInstance()->SignalBadGnupgEnv( - QCoreApplication::tr("Cannot Find Home Path")); + QCoreApplication::tr("Cannot Find Default Home Path")); return false; } -- cgit v1.2.3 From d06c31abe7e2540518c4d3471acd381edfababa3 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 2 Feb 2025 00:21:59 +0100 Subject: feat: upgrade KeyGenDialog to meet easy and advanced requirements --- src/core/GpgCoreInit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/GpgCoreInit.cpp') diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index b564307a..5018b8e4 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -637,7 +637,7 @@ void StartMonitorCoreInitializationStatus() { "core", "env.state.basic", 0); LOG_D() << "monitor: core env is still initializing, waiting..."; - QThread::msleep(15); + QThread::msleep(100); } if (core_init_state < 0) return -1; -- cgit v1.2.3