aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2025-01-27 22:56:22 +0000
committersaturneric <[email protected]>2025-01-27 22:56:22 +0000
commit7505b1131e62af46b785e4482b41dc7a9d002477 (patch)
tree3af3c8413b8ce1413fc7c37ddba422bf8408bd3a
parentfix: correct de_DE translations (diff)
downloadGpgFrontend-7505b1131e62af46b785e4482b41dc7a9d002477.tar.gz
GpgFrontend-7505b1131e62af46b785e4482b41dc7a9d002477.zip
refactor: clean up code and reduce duplication
-rw-r--r--src/core/function/gpg/GpgAdvancedOperator.cpp194
-rw-r--r--src/ui/main_window/KeyMgmt.cpp29
-rw-r--r--src/ui/main_window/MainWindow.cpp2
-rw-r--r--src/ui/main_window/MainWindowSlotFunction.cpp99
-rw-r--r--src/ui/widgets/KeyList.cpp15
-rw-r--r--src/ui/widgets/KeyList.h7
6 files changed, 70 insertions, 276 deletions
diff --git a/src/core/function/gpg/GpgAdvancedOperator.cpp b/src/core/function/gpg/GpgAdvancedOperator.cpp
index b310df85..0fb2861c 100644
--- a/src/core/function/gpg/GpgAdvancedOperator.cpp
+++ b/src/core/function/gpg/GpgAdvancedOperator.cpp
@@ -36,8 +36,10 @@
#include "core/module/ModuleManager.h"
#include "core/utils/GpgUtils.h"
-void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
- OperationCallback cb) {
+namespace GpgFrontend {
+
+void ExecuteGpgCommand(const QString &operation, const QStringList &extra_args,
+ OperationCallback cb) {
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gpgconf_path", QString{});
@@ -58,12 +60,14 @@ void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
const auto target_home_dir =
QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
- GpgFrontend::GpgCommandExecutor::ExecuteSync(
- {gpgconf_path,
- QStringList{"--homedir", target_home_dir, "--reload", "gpg-agent"},
+ QStringList arguments = QStringList{"--homedir", target_home_dir};
+ arguments.append(extra_args);
+
+ GpgCommandExecutor::ExecuteSync(
+ {gpgconf_path, arguments,
[=, &completed_tasks, &results](int exit_code, const QString &,
const QString &) {
- FLOG_D("gpgconf reload exit code: %d", exit_code);
+ FLOG_D("%s exit code: %d", qPrintable(operation), exit_code);
results[current_index] = exit_code;
@@ -79,94 +83,29 @@ void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
}
}
-void GpgFrontend::GpgAdvancedOperator::ReloadGpgComponents(
- OperationCallback cb) {
- const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
- "core", "gpgme.ctx.gpgconf_path", QString{});
-
- if (gpgconf_path.isEmpty()) {
- FLOG_W("cannot get valid gpgconf path from rt, abort.");
- if (cb) cb(-1, TransferParams());
- return;
- }
-
- auto key_dbs = GetGpgKeyDatabaseInfos();
- auto total_tasks = static_cast<int>(key_dbs.size());
- std::atomic<int> completed_tasks{0};
- std::vector<int> results(total_tasks, 0);
-
- int task_index = 0;
- for (const auto &key_db : key_dbs) {
- const int current_index = task_index++;
- const auto target_home_dir =
- QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
-
- GpgFrontend::GpgCommandExecutor::ExecuteSync(
- {gpgconf_path,
- QStringList{"--homedir", target_home_dir, "--reload", "all"},
- [=, &completed_tasks, &results](int exit_code, const QString &,
- const QString &) {
- FLOG_D("gpgconf reload exit code: %d", exit_code);
- results[current_index] = exit_code;
-
- if (++completed_tasks == total_tasks && cb) {
- int final_result =
- std::all_of(results.begin(), results.end(),
- [](int result) { return result >= 0; })
- ? 0
- : -1;
- cb(final_result, TransferParams());
- }
- }});
- }
+void GpgAdvancedOperator::ClearGpgPasswordCache(OperationCallback cb) {
+ ExecuteGpgCommand("Clear GPG Password Cache", {"--reload", "gpg-agent"}, cb);
}
-void GpgFrontend::GpgAdvancedOperator::KillAllGpgComponents(
- OperationCallback cb) {
+void GpgAdvancedOperator::ReloadGpgComponents(OperationCallback cb) {
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gpgconf_path", QString{});
+ ExecuteGpgCommand("Reload GPG Components", {"--reload", "all"}, cb);
+}
- if (gpgconf_path.isEmpty()) {
- FLOG_W("cannot get valid gpgconf path from rt, abort.");
- if (cb) cb(-1, TransferParams());
- return;
- }
-
- auto key_dbs = GetGpgKeyDatabaseInfos();
- auto total_tasks = static_cast<int>(key_dbs.size());
- std::atomic<int> completed_tasks{0};
- std::vector<int> results(total_tasks, 0);
-
- int task_index = 0;
- for (const auto &key_db : key_dbs) {
- const int current_index = task_index++;
- const auto target_home_dir =
- QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
-
- LOG_D() << "closing all gpg component at home path: " << target_home_dir;
- GpgFrontend::GpgCommandExecutor::ExecuteSync(
- {gpgconf_path,
- QStringList{"--homedir", target_home_dir, "--kill", "all"},
- [=, &completed_tasks, &results](int exit_code, const QString &p_out,
- const QString &p_err) {
- FLOG_D("gpgconf --kill --all exit code: %d", exit_code);
+void GpgAdvancedOperator::KillAllGpgComponents(OperationCallback cb) {
+ ExecuteGpgCommand("Kill All GPG Components", {"--kill", "all"}, cb);
+}
- results[current_index] = exit_code;
+void GpgAdvancedOperator::ResetConfigures(OperationCallback cb) {
+ ExecuteGpgCommand("Kill All GPG Components", {"--apply-defaults"}, cb);
+}
- if (++completed_tasks == total_tasks && cb) {
- int final_result =
- std::all_of(results.begin(), results.end(),
- [](int result) { return result >= 0; })
- ? 0
- : -1;
- cb(final_result, TransferParams());
- }
- }});
- }
+void GpgAdvancedOperator::LaunchGpgComponents(OperationCallback cb) {
+ ExecuteGpgCommand("Kill All GPG Components", {"--launch", "all"}, cb);
}
-void GpgFrontend::GpgAdvancedOperator::RestartGpgComponents(
- OperationCallback cb) {
+void GpgAdvancedOperator::RestartGpgComponents(OperationCallback cb) {
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gpgconf_path", QString{});
@@ -181,87 +120,4 @@ void GpgFrontend::GpgAdvancedOperator::RestartGpgComponents(
LaunchGpgComponents(cb);
}
-void GpgFrontend::GpgAdvancedOperator::ResetConfigures(OperationCallback cb) {
- const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
- "core", "gpgme.ctx.gpgconf_path", QString{});
-
- if (gpgconf_path.isEmpty()) {
- FLOG_W("cannot get valid gpgconf path from rt, abort.");
- if (cb) cb(-1, TransferParams());
- return;
- }
-
- auto key_dbs = GetGpgKeyDatabaseInfos();
- auto total_tasks = static_cast<int>(key_dbs.size());
- std::atomic<int> completed_tasks{0};
- std::vector<int> results(total_tasks, 0);
-
- int task_index = 0;
- for (const auto &key_db : key_dbs) {
- const int current_index = task_index++;
- const auto target_home_dir =
- QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
-
- GpgFrontend::GpgCommandExecutor::ExecuteSync(
- {gpgconf_path,
- QStringList{"--homedir", target_home_dir, "--apply-defaults"},
- [=, &completed_tasks, &results](int exit_code, const QString &,
- const QString &) {
- FLOG_D("gpgconf --apply-defaults exit code: %d", exit_code);
-
- results[current_index] = exit_code;
-
- if (++completed_tasks == total_tasks && cb) {
- int final_result =
- std::all_of(results.begin(), results.end(),
- [](int result) { return result >= 0; })
- ? 0
- : -1;
- cb(final_result, TransferParams());
- }
- }});
- }
-}
-
-void GpgFrontend::GpgAdvancedOperator::LaunchGpgComponents(
- OperationCallback cb) {
- const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
- "core", "gpgme.ctx.gpgconf_path", QString{});
-
- if (gpgconf_path.isEmpty()) {
- FLOG_W("cannot get valid gpgconf path from rt, abort.");
- if (cb) cb(-1, TransferParams());
- return;
- }
-
- auto key_dbs = GetGpgKeyDatabaseInfos();
- auto total_tasks = static_cast<int>(key_dbs.size());
- std::atomic<int> completed_tasks{0};
- std::vector<int> results(total_tasks, 0);
-
- int task_index = 0;
- for (const auto &key_db : key_dbs) {
- const int current_index = task_index++;
- const auto target_home_dir =
- QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
-
- GpgFrontend::GpgCommandExecutor::ExecuteSync(
- {gpgconf_path,
- QStringList{"--homedir", target_home_dir, "--launch", "all"},
- [=, &completed_tasks, &results](int exit_code, const QString &,
- const QString &) {
- FLOG_D("gpgconf --launch all exit code: %d", exit_code);
-
- results[current_index] = exit_code;
-
- if (++completed_tasks == total_tasks && cb) {
- int final_result =
- std::all_of(results.begin(), results.end(),
- [](int result) { return result >= 0; })
- ? 0
- : -1;
- cb(final_result, TransferParams());
- }
- }});
- }
-}
+} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/ui/main_window/KeyMgmt.cpp b/src/ui/main_window/KeyMgmt.cpp
index e2f09f40..53b89c91 100644
--- a/src/ui/main_window/KeyMgmt.cpp
+++ b/src/ui/main_window/KeyMgmt.cpp
@@ -353,16 +353,8 @@ void KeyMgmt::delete_keys_with_warning(KeyIdArgsList uid_list) {
}
void KeyMgmt::SlotShowKeyDetails() {
- auto keys_selected = key_list_->GetSelected();
- if (keys_selected.empty()) return;
-
- auto key = GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel())
- .GetKey(keys_selected.front());
-
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
+ auto [succ, key] = key_list_->GetSelectedGpgKey();
+ if (!succ) return;
new KeyDetailsDialog(key_list_->GetCurrentGpgContextChannel(), key, this);
}
@@ -453,20 +445,9 @@ void KeyMgmt::SlotGenerateKeyDialog() {
}
void KeyMgmt::SlotGenerateSubKey() {
- auto keys_selected = key_list_->GetSelected();
- if (keys_selected.empty()) {
- QMessageBox::information(
- this, tr("Invalid Operation"),
- tr("Please select one KeyPair before doing this operation."));
- return;
- }
- const auto key =
- GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel())
- .GetKey(keys_selected.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
+ auto [succ, key] = key_list_->GetSelectedGpgKey();
+ if (!succ) return;
+
if (!key.IsPrivateKey()) {
QMessageBox::critical(this, tr("Invalid Operation"),
tr("If a key pair does not have a private key then "
diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp
index e0f7a49a..f362daa5 100644
--- a/src/ui/main_window/MainWindow.cpp
+++ b/src/ui/main_window/MainWindow.cpp
@@ -30,7 +30,7 @@
#include "core/function/CacheManager.h"
#include "core/function/GlobalSettingStation.h"
-#include "core/function/gpg/GpgAdvancedOperator.h"
+#include "core/function/gpg/GpgKeyGetter.h"
#include "core/model/SettingsObject.h"
#include "core/module/ModuleManager.h"
#include "ui/UISignalStation.h"
diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp
index 825f74f5..36d4435d 100644
--- a/src/ui/main_window/MainWindowSlotFunction.cpp
+++ b/src/ui/main_window/MainWindowSlotFunction.cpp
@@ -100,20 +100,8 @@ void MainWindow::slot_append_selected_keys() {
}
void MainWindow::slot_append_keys_create_datetime() {
- auto key_ids = m_key_list_->GetSelected();
-
- if (key_ids.empty()) {
- FLOG_W("no key is selected");
- return;
- }
-
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
auto create_datetime_format_str_local =
QLocale().toString(key.GetCreateTime()) + " (" + tr("Localize") + ") " +
@@ -126,20 +114,8 @@ void MainWindow::slot_append_keys_create_datetime() {
}
void MainWindow::slot_append_keys_expire_datetime() {
- auto key_ids = m_key_list_->GetSelected();
-
- if (key_ids.empty()) {
- FLOG_W("no key is selected");
- return;
- }
-
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
auto expire_datetime_format_str_local =
QLocale().toString(key.GetExpireTime()) + " (" + tr("Local Time") + ") " +
@@ -152,16 +128,8 @@ void MainWindow::slot_append_keys_expire_datetime() {
}
void MainWindow::slot_append_keys_fingerprint() {
- auto key_ids = m_key_list_->GetSelected();
- if (key_ids.empty()) return;
-
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
auto fingerprint_format_str =
BeautifyFingerprint(key.GetFingerprint()) + "\n";
@@ -170,72 +138,39 @@ void MainWindow::slot_append_keys_fingerprint() {
}
void MainWindow::slot_copy_mail_address_to_clipboard() {
- auto key_ids = m_key_list_->GetSelected();
- if (key_ids.empty()) return;
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
QClipboard* cb = QApplication::clipboard();
cb->setText(key.GetEmail());
}
void MainWindow::slot_copy_default_uid_to_clipboard() {
- auto key_ids = m_key_list_->GetSelected();
- if (key_ids.empty()) return;
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
QClipboard* cb = QApplication::clipboard();
cb->setText(key.GetUIDs()->front().GetUID());
}
void MainWindow::slot_copy_key_id_to_clipboard() {
- auto key_ids = m_key_list_->GetSelected();
- if (key_ids.empty()) return;
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- return;
- }
QClipboard* cb = QApplication::clipboard();
cb->setText(key.GetId());
}
void MainWindow::slot_show_key_details() {
- auto key_ids = m_key_list_->GetSelected();
- if (key_ids.empty()) return;
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (key.IsGood()) {
- new KeyDetailsDialog(m_key_list_->GetCurrentGpgContextChannel(), key, this);
- } else {
- QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
- }
+ new KeyDetailsDialog(m_key_list_->GetCurrentGpgContextChannel(), key, this);
}
void MainWindow::slot_add_key_2_favorite() {
- auto key_ids = m_key_list_->GetSelected();
- if (key_ids.empty()) return;
-
- auto key =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKey(key_ids.front());
- if (!key.IsGood()) return;
+ auto [succ, key] = m_key_list_->GetSelectedGpgKey();
+ if (!succ) return;
auto key_db_name =
GetGpgKeyDatabaseName(m_key_list_->GetCurrentGpgContextChannel());
diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp
index 1d954f86..4e581d81 100644
--- a/src/ui/widgets/KeyList.cpp
+++ b/src/ui/widgets/KeyList.cpp
@@ -633,4 +633,19 @@ void KeyList::UpdateKeyTableColumnType(GpgKeyTableColumn column_type) {
auto KeyList::GetCurrentGpgContextChannel() const -> int {
return current_gpg_context_channel_;
}
+
+auto KeyList::GetSelectedGpgKey() -> std::tuple<bool, GpgKey> {
+ auto key_ids = GetSelected();
+ if (key_ids.empty()) return {false, GpgKey()};
+
+ auto key = GpgKeyGetter::GetInstance(GetCurrentGpgContextChannel())
+ .GetKey(key_ids.front());
+
+ if (!key.IsGood()) {
+ QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
+ return {false, GpgKey()};
+ }
+
+ return {true, key};
+}
} // namespace GpgFrontend::UI
diff --git a/src/ui/widgets/KeyList.h b/src/ui/widgets/KeyList.h
index abcca2b9..868c5b07 100644
--- a/src/ui/widgets/KeyList.h
+++ b/src/ui/widgets/KeyList.h
@@ -196,6 +196,13 @@ class KeyList : public QWidget {
auto GetSelectedKey() -> QString;
/**
+ * @brief Get the Selected Gpg Key object
+ *
+ * @return GpgKey
+ */
+ auto GetSelectedGpgKey() -> std::tuple<bool, GpgKey>;
+
+ /**
* @brief
*
* @return true