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/utils/GpgUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/utils/GpgUtils.cpp') diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index 29b150bd..dfedd098 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -36,7 +36,7 @@ namespace GpgFrontend { inline auto Trim(QString& s) -> QString { return s.trimmed(); } auto GetGpgmeErrorString(size_t buffer_size, gpgme_error_t err) -> QString { - std::vector buffer(buffer_size); + QContainer buffer(buffer_size); gpgme_error_t const ret = gpgme_strerror_r(err, buffer.data(), buffer.size()); if (ret == ERANGE && buffer_size < 1024) { -- 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/utils/GpgUtils.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'src/core/utils/GpgUtils.cpp') diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index dfedd098..38eef081 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -28,8 +28,11 @@ #include "GpgUtils.h" +#include "core/function/GlobalSettingStation.h" #include "core/model/KeyDatabaseInfo.h" +#include "core/model/SettingsObject.h" #include "core/module/ModuleManager.h" +#include "core/struct/settings_object/KeyDatabaseListSO.h" namespace GpgFrontend { @@ -218,4 +221,102 @@ auto GPGFRONTEND_CORE_EXPORT GetGpgKeyDatabaseName(int channel) -> QString { if (channel >= info.size()) return {}; return info[channel].name; } + +auto GetKeyDatabasesBySettings() -> QContainer { + 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 (key_dbs.empty()) { + KeyDatabaseItemSO key_db; + + auto default_home_path = Module::RetrieveRTValueTypedOrDefault<>( + "core", "gpgme.ctx.default_database_path", QString{}); + + key_db.channel = 0; + key_db.name = "DEFAULT"; + key_db.path = default_home_path; + + key_dbs.append(key_db); + } + + // Sort by channel + std::sort(key_dbs.begin(), key_dbs.end(), + [](const auto& a, const auto& b) { return a.channel < b.channel; }); + + // Resolve duplicate channels by incrementing + for (auto it = key_dbs.begin(); it != key_dbs.end(); ++it) { + auto next_it = std::next(it); + while (next_it != key_dbs.end() && next_it->channel == it->channel) { + next_it->channel = it->channel + 1; + ++next_it; + } + } + + key_db_list_so.Store(key_db_list.ToJson()); + + return key_db_list.key_databases; +} + +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 SearchKeyDatabasePath(const QList& candidate_paths) -> QString { + for (const auto& path : candidate_paths) { + if (VerifyKeyDatabasePath(QFileInfo(path))) { + // return a unify path + return QFileInfo(path).absoluteFilePath(); + } + } + return {}; +} + +auto GetCanonicalKeyDatabasePath(const QDir& app_path, + const QString& path) -> QString { + auto target_path = path; + if (!QDir::isAbsolutePath(target_path)) { + target_path = app_path.absoluteFilePath(target_path); + LOG_D() << "convert relative path: " << path + << "to absolute path: " << target_path; + } + + auto info = QFileInfo(target_path); + if (VerifyKeyDatabasePath(info)) { + auto key_database_fs_path = info.canonicalFilePath(); + LOG_D() << "load gpg key database:" << key_database_fs_path; + + return key_database_fs_path; + } + + LOG_W() << "gpg key database path is invalid: " << path; + return {}; +} + +auto GetKeyDatabaseInfoBySettings() -> QList { + auto key_dbs = GetKeyDatabasesBySettings(); + + QContainer key_db_infos; + + const auto app_path = QDir(GlobalSettingStation::GetInstance().GetAppDir()); + + // try to use user defined key database + for (const auto& key_database : key_dbs) { + auto key_database_fs_path = + GetCanonicalKeyDatabasePath(app_path, key_database.path); + + if (key_database_fs_path.isEmpty()) continue; + + KeyDatabaseInfo key_db_info; + key_db_info.name = key_database.name; + key_db_info.path = key_database_fs_path; + key_db_info.origin_path = key_database.path; + key_db_infos.append(key_db_info); + + LOG_D() << "plan to load gpg key database:" << key_database_fs_path; + } + + return key_db_infos; +} } // namespace GpgFrontend -- 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/utils/GpgUtils.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/core/utils/GpgUtils.cpp') diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index 38eef081..c4478f6f 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -230,12 +230,17 @@ auto GetKeyDatabasesBySettings() -> QContainer { if (key_dbs.empty()) { KeyDatabaseItemSO key_db; - auto default_home_path = Module::RetrieveRTValueTypedOrDefault<>( + auto home_path = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.default_database_path", QString{}); + if (GlobalSettingStation::GetInstance().IsProtableMode()) { + home_path = QDir(GlobalSettingStation::GetInstance().GetAppDir()) + .relativeFilePath(home_path); + } + key_db.channel = 0; key_db.name = "DEFAULT"; - key_db.path = default_home_path; + key_db.path = home_path; key_dbs.append(key_db); } -- 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/utils/GpgUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/utils/GpgUtils.cpp') diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index c4478f6f..bfbee963 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -268,7 +268,7 @@ auto VerifyKeyDatabasePath(const QFileInfo& key_database_fs_path) -> bool { key_database_fs_path.isDir(); } -auto SearchKeyDatabasePath(const QList& candidate_paths) -> QString { +auto SearchKeyDatabasePath(const QStringList& candidate_paths) -> QString { for (const auto& path : candidate_paths) { if (VerifyKeyDatabasePath(QFileInfo(path))) { // return a unify path -- cgit v1.2.3 From fb2e47db7b8cd5ca7aac9c0ee1ca8b58ada0158f Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 27 Jan 2025 19:59:29 +0100 Subject: refactor: reduce code duplication --- src/core/utils/GpgUtils.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/core/utils/GpgUtils.cpp') diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index bfbee963..ec0f35e2 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -29,6 +29,7 @@ #include "GpgUtils.h" #include "core/function/GlobalSettingStation.h" +#include "core/model/GpgKey.h" #include "core/model/KeyDatabaseInfo.h" #include "core/model/SettingsObject.h" #include "core/module/ModuleManager.h" @@ -324,4 +325,12 @@ auto GetKeyDatabaseInfoBySettings() -> QList { return key_db_infos; } + +auto GPGFRONTEND_CORE_EXPORT Convert2RawGpgMEKeyList( + const QContainer& keys) -> QContainer { + QContainer recipients(keys.begin(), keys.end()); + recipients.emplace_back(nullptr); + + return recipients; +} } // namespace GpgFrontend -- cgit v1.2.3