aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2025-01-21 22:44:36 +0000
committersaturneric <[email protected]>2025-01-21 22:44:36 +0000
commit3931b64fddffbb12b3276acbdd14a834432b5104 (patch)
tree6069614bbd592fbf1f319d5e702d6e34a7ab0d4a
parentMerge branch 'develop' into dev/2.1.6/main (diff)
downloadGpgFrontend-3931b64fddffbb12b3276acbdd14a834432b5104.tar.gz
GpgFrontend-3931b64fddffbb12b3276acbdd14a834432b5104.zip
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
-rw-r--r--src/cmd.cpp3
-rw-r--r--src/core/GpgCoreInit.cpp91
-rw-r--r--src/core/GpgCoreInit.h8
-rw-r--r--src/core/function/GlobalSettingStation.cpp8
-rw-r--r--src/core/function/GlobalSettingStation.h8
-rw-r--r--src/core/model/KeyDatabaseInfo.h1
-rw-r--r--src/core/struct/settings_object/KeyDatabaseItemSO.h8
-rw-r--r--src/core/utils/GpgUtils.cpp101
-rw-r--r--src/core/utils/GpgUtils.h27
-rw-r--r--src/ui/dialog/KeyDatabaseEditDialog.cpp7
-rw-r--r--src/ui/dialog/controller/GnuPGControllerDialog.cpp99
-rw-r--r--src/ui/dialog/controller/GnuPGControllerDialog.h7
-rw-r--r--ui/KeyDatabaseEditDialog.ui7
13 files changed, 246 insertions, 129 deletions
diff --git a/src/cmd.cpp b/src/cmd.cpp
index b8de7d4c..dca88cf3 100644
--- a/src/cmd.cpp
+++ b/src/cmd.cpp
@@ -39,6 +39,7 @@
#include "core/module/ModuleManager.h"
#include "core/thread/TaskRunnerGetter.h"
#include "core/utils/BuildInfoUtils.h"
+#include "core/utils/GpgUtils.h"
// GpgFrontend
@@ -160,7 +161,7 @@ auto PrintEnvInfo() -> int {
stream << '\n';
int index = 0;
- auto key_dbs = GetKeyDatabaseInfoBySettings(default_database_path);
+ auto key_dbs = GetKeyDatabaseInfoBySettings();
for (const auto& key_database : key_dbs) {
stream << Tr("Key Database [") << index++ << "] " << Tr("Name: ")
<< key_database.name << " " << Tr("-> Path: ") << key_database.path
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<QString>& candidate_paths) -> QString {
for (const auto& path : candidate_paths) {
if (VerifyGpgconfPath(QFileInfo(path))) {
@@ -69,17 +64,12 @@ auto SearchGpgconfPath(const QList<QString>& candidate_paths) -> QString {
return {};
}
-auto SearchKeyDatabasePath(const QList<QString>& 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<KeyDatabaseItemSO> {
- 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<KeyDatabaseInfo> {
- auto key_dbs = GetKeyDatabasesBySettings(default_home_path);
- QList<KeyDatabaseInfo> 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<KeyDatabaseItemSO> 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 {
diff --git a/src/core/GpgCoreInit.h b/src/core/GpgCoreInit.h
index b23f5a9a..0a35d6d1 100644
--- a/src/core/GpgCoreInit.h
+++ b/src/core/GpgCoreInit.h
@@ -72,12 +72,4 @@ auto GPGFRONTEND_CORE_EXPORT InitGpgME() -> bool;
*/
auto GPGFRONTEND_CORE_EXPORT InitBasicPath() -> bool;
-/**
- * @brief Get the Key Databases By Settings object
- *
- * @return auto
- */
-auto GPGFRONTEND_CORE_EXPORT GetKeyDatabaseInfoBySettings(
- QString& default_home_path) -> QList<KeyDatabaseInfo>;
-
} // namespace GpgFrontend
diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp
index 4c88fde4..b0a2182c 100644
--- a/src/core/function/GlobalSettingStation.cpp
+++ b/src/core/function/GlobalSettingStation.cpp
@@ -48,7 +48,7 @@ class GlobalSettingStation::Impl {
Module::UpsertRTValue("core", "env.state.portable", 1);
LOG_I() << "GpgFrontend runs in the portable mode now";
- app_data_path_ = app_path_ + "/../";
+ app_data_path_ = QDir(app_path_ + "/../").canonicalPath();
app_config_path_ = app_data_path_ + "/config";
portable_mode_ = true;
@@ -171,6 +171,8 @@ class GlobalSettingStation::Impl {
return exec_binary_path + "/modules";
}
+ [[nodiscard]] auto IsProtableMode() const -> bool { return portable_mode_; }
+
private:
[[nodiscard]] auto app_config_file_path() const -> QString {
return app_config_path_ + "/config.ini";
@@ -244,4 +246,8 @@ auto GlobalSettingStation::GetConfigPath() const -> QString {
auto GlobalSettingStation::GetIntegratedModulePath() const -> QString {
return p_->GetIntegratedModulePath();
}
+
+auto GlobalSettingStation::IsProtableMode() const -> bool {
+ return p_->IsProtableMode();
+}
} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/function/GlobalSettingStation.h b/src/core/function/GlobalSettingStation.h
index f45c0b49..b7670b95 100644
--- a/src/core/function/GlobalSettingStation.h
+++ b/src/core/function/GlobalSettingStation.h
@@ -131,6 +131,14 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation
*/
[[nodiscard]] auto GetIntegratedModulePath() const -> QString;
+ /**
+ * @brief
+ *
+ * @return true
+ * @return false
+ */
+ [[nodiscard]] auto IsProtableMode() const -> bool;
+
private:
class Impl;
SecureUniquePtr<Impl> p_;
diff --git a/src/core/model/KeyDatabaseInfo.h b/src/core/model/KeyDatabaseInfo.h
index b5bfc1ea..693a4a65 100644
--- a/src/core/model/KeyDatabaseInfo.h
+++ b/src/core/model/KeyDatabaseInfo.h
@@ -34,6 +34,7 @@ struct KeyDatabaseInfo {
int channel;
QString name;
QString path;
+ QString origin_path;
KeyDatabaseInfo() = default;
};
diff --git a/src/core/struct/settings_object/KeyDatabaseItemSO.h b/src/core/struct/settings_object/KeyDatabaseItemSO.h
index 873246b2..f1cdfaf9 100644
--- a/src/core/struct/settings_object/KeyDatabaseItemSO.h
+++ b/src/core/struct/settings_object/KeyDatabaseItemSO.h
@@ -35,12 +35,14 @@ namespace GpgFrontend {
struct KeyDatabaseItemSO {
QString name;
QString path;
+ int channel;
KeyDatabaseItemSO() = default;
explicit KeyDatabaseItemSO(KeyDatabaseInfo i) {
name = i.name;
- path = i.path;
+ path = i.origin_path.isEmpty() ? i.path : i.origin_path;
+ channel = i.channel;
}
explicit KeyDatabaseItemSO(const QJsonObject& j) {
@@ -50,12 +52,16 @@ struct KeyDatabaseItemSO {
if (const auto v = j["path"]; v.isString()) {
path = v.toString();
}
+ if (const auto v = j["channel"]; v.isDouble()) {
+ channel = v.toInt();
+ }
}
[[nodiscard]] auto ToJson() const -> QJsonObject {
QJsonObject j;
j["name"] = name;
j["path"] = path;
+ j["channel"] = channel;
return j;
}
};
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<KeyDatabaseItemSO> {
+ 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<QString>& 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<KeyDatabaseInfo> {
+ auto key_dbs = GetKeyDatabasesBySettings();
+
+ QContainer<KeyDatabaseInfo> 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
diff --git a/src/core/utils/GpgUtils.h b/src/core/utils/GpgUtils.h
index 3cdef574..ab0c328f 100644
--- a/src/core/utils/GpgUtils.h
+++ b/src/core/utils/GpgUtils.h
@@ -30,6 +30,7 @@
#include "core/function/result_analyse/GpgResultAnalyse.h"
#include "core/model/KeyDatabaseInfo.h"
+#include "core/struct/settings_object/KeyDatabaseItemSO.h"
#include "core/typedef/CoreTypedef.h"
#include "core/typedef/GpgTypedef.h"
@@ -109,6 +110,32 @@ auto GPGFRONTEND_CORE_EXPORT SetExtensionOfOutputFileForArchive(
/**
* @brief
*
+ * @param app_path
+ * @param path
+ * @return QString
+ */
+auto GPGFRONTEND_CORE_EXPORT GetCanonicalKeyDatabasePath(
+ const QDir& app_path, const QString& path) -> QString;
+
+/**
+ * @brief Get the Key Databases By Settings object
+ *
+ * @return QContainer<KeyDatabaseItemSO>
+ */
+auto GPGFRONTEND_CORE_EXPORT GetKeyDatabasesBySettings()
+ -> QContainer<KeyDatabaseItemSO>;
+
+/**
+ * @brief
+ *
+ * @return QList<KeyDatabaseInfo>
+ */
+auto GPGFRONTEND_CORE_EXPORT GetKeyDatabaseInfoBySettings()
+ -> QList<KeyDatabaseInfo>;
+
+/**
+ * @brief
+ *
* @return QList<KeyDatabaseItemSO>
*/
auto GPGFRONTEND_CORE_EXPORT GetGpgKeyDatabaseInfos() -> QList<KeyDatabaseInfo>;
diff --git a/src/ui/dialog/KeyDatabaseEditDialog.cpp b/src/ui/dialog/KeyDatabaseEditDialog.cpp
index cc6e2cf9..25884902 100644
--- a/src/ui/dialog/KeyDatabaseEditDialog.cpp
+++ b/src/ui/dialog/KeyDatabaseEditDialog.cpp
@@ -30,6 +30,7 @@
#include <utility>
+#include "core/function/GlobalSettingStation.h"
#include "core/struct/settings_object/KeyDatabaseItemSO.h"
#include "core/utils/MemoryUtils.h"
#include "ui_KeyDatabaseEditDialog.h"
@@ -47,6 +48,7 @@ KeyDatabaseEditDialog::KeyDatabaseEditDialog(
ui_->keyDBNameLabel->setText(tr("Key Database Name"));
ui_->keyDBPathLabel->setText(tr("Key Database Path"));
ui_->selectKeyDBButton->setText(tr("Select A Key Database Path"));
+ ui_->convert2RelativePathCheckBox->setText(tr("Convert to Relative Path"));
this->setWindowTitle(tr("Key Database Info"));
@@ -96,6 +98,11 @@ void KeyDatabaseEditDialog::slot_button_box_accepted() {
}
}
+ if (ui_->convert2RelativePathCheckBox->isChecked()) {
+ path_ = QDir(GlobalSettingStation::GetInstance().GetAppDir())
+ .relativeFilePath(path_);
+ }
+
slot_clear_err_msg();
emit SignalKeyDatabaseInfoAccepted(name_, path_);
this->accept();
diff --git a/src/ui/dialog/controller/GnuPGControllerDialog.cpp b/src/ui/dialog/controller/GnuPGControllerDialog.cpp
index 5b8e96dd..bedfc5de 100644
--- a/src/ui/dialog/controller/GnuPGControllerDialog.cpp
+++ b/src/ui/dialog/controller/GnuPGControllerDialog.cpp
@@ -45,7 +45,8 @@ namespace GpgFrontend::UI {
GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)
: GeneralDialog("GnuPGControllerDialog", parent),
- ui_(GpgFrontend::SecureCreateSharedObject<Ui_GnuPGControllerDialog>()) {
+ ui_(GpgFrontend::SecureCreateSharedObject<Ui_GnuPGControllerDialog>()),
+ app_path_(GlobalSettingStation::GetInstance().GetAppDir()) {
ui_->setupUi(this);
ui_->asciiModeCheckBox->setText(tr("Use Binary Mode for File Operations"));
@@ -73,7 +74,7 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)
ui_->keyDatabaseTable->clear();
QStringList column_titles;
- column_titles << tr("Name") << tr("Path");
+ column_titles << tr("Name") << tr("Status") << tr("Path") << tr("Real Path");
ui_->keyDatabaseTable->setColumnCount(static_cast<int>(column_titles.size()));
ui_->keyDatabaseTable->setHorizontalHeaderLabels(column_titles);
@@ -277,11 +278,8 @@ void GnuPGControllerDialog::set_settings() {
this->slot_set_restart_needed(kNonRestartCode);
- buffered_key_db_so_ = GetGpgKeyDatabaseInfos();
- editable_key_db_so_ = buffered_key_db_so_;
- if (!editable_key_db_so_.isEmpty()) {
- editable_key_db_so_.pop_front();
- }
+ key_db_infos_ = GetKeyDatabaseInfoBySettings();
+ active_key_db_infos_ = GetGpgKeyDatabaseInfos();
this->slot_refresh_key_database_table();
}
@@ -308,7 +306,10 @@ void GnuPGControllerDialog::apply_settings() {
auto so = SettingsObject("key_database_list");
auto key_database_list = KeyDatabaseListSO(so);
key_database_list.key_databases.clear();
- for (const auto& key_db_info : editable_key_db_so_) {
+
+ int index = 0;
+ for (auto& key_db_info : key_db_infos_) {
+ key_db_info.channel = index++;
key_database_list.key_databases.append(KeyDatabaseItemSO(key_db_info));
}
so.Store(key_database_list.ToJson());
@@ -356,9 +357,9 @@ auto GnuPGControllerDialog::check_custom_gnupg_path(QString path) -> bool {
}
void GnuPGControllerDialog::slot_add_new_key_database() {
- auto* dialog = new KeyDatabaseEditDialog(editable_key_db_so_, this);
+ auto* dialog = new KeyDatabaseEditDialog(key_db_infos_, this);
- if (editable_key_db_so_.size() >= 8) {
+ if (key_db_infos_.size() >= 8) {
QMessageBox::critical(
this, tr("Maximum Key Database Limit Reached"),
tr("Currently, GpgFrontend supports a maximum of 8 key databases. "
@@ -368,7 +369,7 @@ void GnuPGControllerDialog::slot_add_new_key_database() {
connect(dialog, &KeyDatabaseEditDialog::SignalKeyDatabaseInfoAccepted, this,
[this](const QString& name, const QString& path) {
- auto& key_databases = buffered_key_db_so_;
+ auto& key_databases = key_db_infos_;
for (const auto& key_database : key_databases) {
if (QFileInfo(key_database.path) == QFileInfo(path)) {
QMessageBox::warning(
@@ -379,20 +380,25 @@ void GnuPGControllerDialog::slot_add_new_key_database() {
}
}
+ auto key_db_fs_path =
+ GpgFrontend::GetCanonicalKeyDatabasePath(app_path_, path);
+ if (key_db_fs_path.isEmpty()) {
+ QMessageBox::warning(this, tr("Invalid Key Database Paths"),
+ tr("The edited key database path is not a "
+ "valid path that GpgFrontend can use"));
+ return;
+ }
+
LOG_D() << "new key database path, name: " << name
- << "path: " << path;
+ << "path: " << path << "canonical path: " << key_db_fs_path;
KeyDatabaseInfo key_database;
key_database.name = name;
- key_database.path = path;
- key_database.channel = buffered_key_db_so_.size();
+ key_database.path = key_db_fs_path;
+ key_database.origin_path = path;
+ key_database.channel = static_cast<int>(key_databases.size());
key_databases.append(key_database);
- editable_key_db_so_ = buffered_key_db_so_;
- if (!editable_key_db_so_.isEmpty()) {
- editable_key_db_so_.pop_front();
- }
-
// refresh ui
slot_refresh_key_database_table();
@@ -403,23 +409,34 @@ void GnuPGControllerDialog::slot_add_new_key_database() {
}
void GnuPGControllerDialog::slot_refresh_key_database_table() {
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
ui_->keyDatabaseTable->setRowCount(static_cast<int>(key_databases.size()));
int index = 0;
- for (const auto& key_database : key_databases) {
+ for (const auto& key_db : key_databases) {
LOG_D() << "key database table item index: " << index
- << "name: " << key_database.name << "path: " << key_database.path;
+ << "name: " << key_db.name << "path: " << key_db.path;
- auto* i_name = new QTableWidgetItem(key_database.name);
+ auto* i_name = new QTableWidgetItem(key_db.name);
i_name->setTextAlignment(Qt::AlignCenter);
ui_->keyDatabaseTable->setVerticalHeaderItem(
index, new QTableWidgetItem(QString::number(index + 1)));
ui_->keyDatabaseTable->setItem(index, 0, i_name);
- ui_->keyDatabaseTable->setItem(index, 1,
- new QTableWidgetItem(key_database.path));
+ auto is_active =
+ std::find_if(active_key_db_infos_.begin(), active_key_db_infos_.end(),
+ [key_db](const KeyDatabaseInfo& i) {
+ return i.name == key_db.name;
+ }) != active_key_db_infos_.end();
+ ui_->keyDatabaseTable->setItem(
+ index, 1,
+ new QTableWidgetItem(is_active ? tr("Active") : tr("Inactive")));
+
+ ui_->keyDatabaseTable->setItem(index, 2,
+ new QTableWidgetItem(key_db.origin_path));
+
+ ui_->keyDatabaseTable->setItem(index, 3, new QTableWidgetItem(key_db.path));
index++;
}
@@ -436,7 +453,7 @@ void GnuPGControllerDialog::contextMenuEvent(QContextMenuEvent* event) {
void GnuPGControllerDialog::slot_remove_existing_key_database() {
const auto row_size = ui_->keyDatabaseTable->rowCount();
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
for (int i = 0; i < row_size; i++) {
auto* const item = ui_->keyDatabaseTable->item(i, 1);
if (!item->isSelected()) continue;
@@ -463,7 +480,7 @@ void GnuPGControllerDialog::slot_remove_existing_key_database() {
void GnuPGControllerDialog::slot_open_key_database() {
const auto row_size = ui_->keyDatabaseTable->rowCount();
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
for (int i = 0; i < row_size; i++) {
auto* const item = ui_->keyDatabaseTable->item(i, 1);
if (!item->isSelected()) continue;
@@ -478,7 +495,7 @@ void GnuPGControllerDialog::slot_move_up_key_database() {
if (row_size <= 0) return;
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
for (int i = 0; i < row_size; i++) {
auto* const item = ui_->keyDatabaseTable->item(i, 1);
@@ -508,7 +525,7 @@ void GnuPGControllerDialog::slot_move_to_top_key_database() {
if (row_size <= 0) return;
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
for (int i = 0; i < row_size; i++) {
auto* const item = ui_->keyDatabaseTable->item(i, 1);
@@ -541,7 +558,7 @@ void GnuPGControllerDialog::slot_move_down_key_database() {
if (row_size <= 0) return;
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
for (int i = row_size - 1; i >= 0; i--) {
auto* const item = ui_->keyDatabaseTable->item(i, 1);
@@ -586,16 +603,16 @@ void GnuPGControllerDialog::slot_edit_key_database() {
return;
}
- auto& key_databases = editable_key_db_so_;
+ auto& key_databases = key_db_infos_;
KeyDatabaseInfo& selected_key_database = key_databases[selected_row];
- auto* dialog = new KeyDatabaseEditDialog(editable_key_db_so_, this);
+ auto* dialog = new KeyDatabaseEditDialog(key_databases, this);
dialog->SetDefaultName(selected_key_database.name);
dialog->SetDefaultPath(selected_key_database.path);
connect(dialog, &KeyDatabaseEditDialog::SignalKeyDatabaseInfoAccepted, this,
[this, selected_row, selected_key_database](const QString& name,
const QString& path) {
- auto& all_key_databases = buffered_key_db_so_;
+ auto& all_key_databases = key_db_infos_;
if (selected_key_database.path != path) {
for (int i = 0; i < all_key_databases.size(); i++) {
@@ -610,12 +627,22 @@ void GnuPGControllerDialog::slot_edit_key_database() {
}
}
+ auto key_db_fs_path =
+ GpgFrontend::GetCanonicalKeyDatabasePath(app_path_, path);
+ if (key_db_fs_path.isEmpty()) {
+ QMessageBox::warning(this, tr("Invalid Key Database Paths"),
+ tr("The edited key database path is not a "
+ "valid path that GpgFrontend can use"));
+ return;
+ }
+
LOG_D() << "edit key database path, name: " << name
- << "path: " << path;
+ << "path: " << path << "canonical path: " << key_db_fs_path;
- KeyDatabaseInfo& key_database = editable_key_db_so_[selected_row];
+ KeyDatabaseInfo& key_database = key_db_infos_[selected_row];
key_database.name = name;
- key_database.path = path;
+ key_database.path = key_db_fs_path;
+ key_database.origin_path = path;
slot_refresh_key_database_table();
diff --git a/src/ui/dialog/controller/GnuPGControllerDialog.h b/src/ui/dialog/controller/GnuPGControllerDialog.h
index 9c42180c..f0994c27 100644
--- a/src/ui/dialog/controller/GnuPGControllerDialog.h
+++ b/src/ui/dialog/controller/GnuPGControllerDialog.h
@@ -126,12 +126,13 @@ class GnuPGControllerDialog : public GeneralDialog {
private:
std::shared_ptr<Ui_GnuPGControllerDialog> ui_; ///<
- int restart_mode_{0}; ///<
+ const QString app_path_;
+ int restart_mode_{0}; ///<
QString custom_key_database_path_;
QString custom_gnupg_path_;
QMenu* popup_menu_{};
- QList<KeyDatabaseInfo> buffered_key_db_so_;
- QList<KeyDatabaseInfo> editable_key_db_so_;
+ QList<KeyDatabaseInfo> active_key_db_infos_;
+ QList<KeyDatabaseInfo> key_db_infos_;
/**
* @brief Get the Restart Needed object
diff --git a/ui/KeyDatabaseEditDialog.ui b/ui/KeyDatabaseEditDialog.ui
index 83a8862d..704fc602 100644
--- a/ui/KeyDatabaseEditDialog.ui
+++ b/ui/KeyDatabaseEditDialog.ui
@@ -58,6 +58,13 @@
</widget>
</item>
<item>
+ <widget class="QCheckBox" name="convert2RelativePathCheckBox">
+ <property name="text">
+ <string>Convert to Relative Path</string>
+ </property>
+ </widget>
+ </item>
+ <item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Vertical</enum>