Skip to content

Commit

Permalink
refactor: clean up code and reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed Jan 27, 2025
1 parent 992e861 commit 7505b11
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 276 deletions.
194 changes: 25 additions & 169 deletions src/core/function/gpg/GpgAdvancedOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{});

Expand All @@ -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;

Expand All @@ -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{});

Expand All @@ -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
29 changes: 5 additions & 24 deletions src/ui/main_window/KeyMgmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 "
Expand Down
2 changes: 1 addition & 1 deletion src/ui/main_window/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 7505b11

Please sign in to comment.