Skip to content

Commit 7505b11

Browse files
committed
refactor: clean up code and reduce duplication
1 parent 992e861 commit 7505b11

File tree

6 files changed

+70
-276
lines changed

6 files changed

+70
-276
lines changed

src/core/function/gpg/GpgAdvancedOperator.cpp

Lines changed: 25 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
#include "core/module/ModuleManager.h"
3737
#include "core/utils/GpgUtils.h"
3838

39-
void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
40-
OperationCallback cb) {
39+
namespace GpgFrontend {
40+
41+
void ExecuteGpgCommand(const QString &operation, const QStringList &extra_args,
42+
OperationCallback cb) {
4143
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
4244
"core", "gpgme.ctx.gpgconf_path", QString{});
4345

@@ -58,12 +60,14 @@ void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
5860
const auto target_home_dir =
5961
QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
6062

61-
GpgFrontend::GpgCommandExecutor::ExecuteSync(
62-
{gpgconf_path,
63-
QStringList{"--homedir", target_home_dir, "--reload", "gpg-agent"},
63+
QStringList arguments = QStringList{"--homedir", target_home_dir};
64+
arguments.append(extra_args);
65+
66+
GpgCommandExecutor::ExecuteSync(
67+
{gpgconf_path, arguments,
6468
[=, &completed_tasks, &results](int exit_code, const QString &,
6569
const QString &) {
66-
FLOG_D("gpgconf reload exit code: %d", exit_code);
70+
FLOG_D("%s exit code: %d", qPrintable(operation), exit_code);
6771

6872
results[current_index] = exit_code;
6973

@@ -79,94 +83,29 @@ void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
7983
}
8084
}
8185

82-
void GpgFrontend::GpgAdvancedOperator::ReloadGpgComponents(
83-
OperationCallback cb) {
84-
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
85-
"core", "gpgme.ctx.gpgconf_path", QString{});
86-
87-
if (gpgconf_path.isEmpty()) {
88-
FLOG_W("cannot get valid gpgconf path from rt, abort.");
89-
if (cb) cb(-1, TransferParams());
90-
return;
91-
}
92-
93-
auto key_dbs = GetGpgKeyDatabaseInfos();
94-
auto total_tasks = static_cast<int>(key_dbs.size());
95-
std::atomic<int> completed_tasks{0};
96-
std::vector<int> results(total_tasks, 0);
97-
98-
int task_index = 0;
99-
for (const auto &key_db : key_dbs) {
100-
const int current_index = task_index++;
101-
const auto target_home_dir =
102-
QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
103-
104-
GpgFrontend::GpgCommandExecutor::ExecuteSync(
105-
{gpgconf_path,
106-
QStringList{"--homedir", target_home_dir, "--reload", "all"},
107-
[=, &completed_tasks, &results](int exit_code, const QString &,
108-
const QString &) {
109-
FLOG_D("gpgconf reload exit code: %d", exit_code);
110-
results[current_index] = exit_code;
111-
112-
if (++completed_tasks == total_tasks && cb) {
113-
int final_result =
114-
std::all_of(results.begin(), results.end(),
115-
[](int result) { return result >= 0; })
116-
? 0
117-
: -1;
118-
cb(final_result, TransferParams());
119-
}
120-
}});
121-
}
86+
void GpgAdvancedOperator::ClearGpgPasswordCache(OperationCallback cb) {
87+
ExecuteGpgCommand("Clear GPG Password Cache", {"--reload", "gpg-agent"}, cb);
12288
}
12389

124-
void GpgFrontend::GpgAdvancedOperator::KillAllGpgComponents(
125-
OperationCallback cb) {
90+
void GpgAdvancedOperator::ReloadGpgComponents(OperationCallback cb) {
12691
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
12792
"core", "gpgme.ctx.gpgconf_path", QString{});
93+
ExecuteGpgCommand("Reload GPG Components", {"--reload", "all"}, cb);
94+
}
12895

129-
if (gpgconf_path.isEmpty()) {
130-
FLOG_W("cannot get valid gpgconf path from rt, abort.");
131-
if (cb) cb(-1, TransferParams());
132-
return;
133-
}
134-
135-
auto key_dbs = GetGpgKeyDatabaseInfos();
136-
auto total_tasks = static_cast<int>(key_dbs.size());
137-
std::atomic<int> completed_tasks{0};
138-
std::vector<int> results(total_tasks, 0);
139-
140-
int task_index = 0;
141-
for (const auto &key_db : key_dbs) {
142-
const int current_index = task_index++;
143-
const auto target_home_dir =
144-
QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
145-
146-
LOG_D() << "closing all gpg component at home path: " << target_home_dir;
147-
GpgFrontend::GpgCommandExecutor::ExecuteSync(
148-
{gpgconf_path,
149-
QStringList{"--homedir", target_home_dir, "--kill", "all"},
150-
[=, &completed_tasks, &results](int exit_code, const QString &p_out,
151-
const QString &p_err) {
152-
FLOG_D("gpgconf --kill --all exit code: %d", exit_code);
96+
void GpgAdvancedOperator::KillAllGpgComponents(OperationCallback cb) {
97+
ExecuteGpgCommand("Kill All GPG Components", {"--kill", "all"}, cb);
98+
}
15399

154-
results[current_index] = exit_code;
100+
void GpgAdvancedOperator::ResetConfigures(OperationCallback cb) {
101+
ExecuteGpgCommand("Kill All GPG Components", {"--apply-defaults"}, cb);
102+
}
155103

156-
if (++completed_tasks == total_tasks && cb) {
157-
int final_result =
158-
std::all_of(results.begin(), results.end(),
159-
[](int result) { return result >= 0; })
160-
? 0
161-
: -1;
162-
cb(final_result, TransferParams());
163-
}
164-
}});
165-
}
104+
void GpgAdvancedOperator::LaunchGpgComponents(OperationCallback cb) {
105+
ExecuteGpgCommand("Kill All GPG Components", {"--launch", "all"}, cb);
166106
}
167107

168-
void GpgFrontend::GpgAdvancedOperator::RestartGpgComponents(
169-
OperationCallback cb) {
108+
void GpgAdvancedOperator::RestartGpgComponents(OperationCallback cb) {
170109
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
171110
"core", "gpgme.ctx.gpgconf_path", QString{});
172111

@@ -181,87 +120,4 @@ void GpgFrontend::GpgAdvancedOperator::RestartGpgComponents(
181120
LaunchGpgComponents(cb);
182121
}
183122

184-
void GpgFrontend::GpgAdvancedOperator::ResetConfigures(OperationCallback cb) {
185-
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
186-
"core", "gpgme.ctx.gpgconf_path", QString{});
187-
188-
if (gpgconf_path.isEmpty()) {
189-
FLOG_W("cannot get valid gpgconf path from rt, abort.");
190-
if (cb) cb(-1, TransferParams());
191-
return;
192-
}
193-
194-
auto key_dbs = GetGpgKeyDatabaseInfos();
195-
auto total_tasks = static_cast<int>(key_dbs.size());
196-
std::atomic<int> completed_tasks{0};
197-
std::vector<int> results(total_tasks, 0);
198-
199-
int task_index = 0;
200-
for (const auto &key_db : key_dbs) {
201-
const int current_index = task_index++;
202-
const auto target_home_dir =
203-
QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
204-
205-
GpgFrontend::GpgCommandExecutor::ExecuteSync(
206-
{gpgconf_path,
207-
QStringList{"--homedir", target_home_dir, "--apply-defaults"},
208-
[=, &completed_tasks, &results](int exit_code, const QString &,
209-
const QString &) {
210-
FLOG_D("gpgconf --apply-defaults exit code: %d", exit_code);
211-
212-
results[current_index] = exit_code;
213-
214-
if (++completed_tasks == total_tasks && cb) {
215-
int final_result =
216-
std::all_of(results.begin(), results.end(),
217-
[](int result) { return result >= 0; })
218-
? 0
219-
: -1;
220-
cb(final_result, TransferParams());
221-
}
222-
}});
223-
}
224-
}
225-
226-
void GpgFrontend::GpgAdvancedOperator::LaunchGpgComponents(
227-
OperationCallback cb) {
228-
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
229-
"core", "gpgme.ctx.gpgconf_path", QString{});
230-
231-
if (gpgconf_path.isEmpty()) {
232-
FLOG_W("cannot get valid gpgconf path from rt, abort.");
233-
if (cb) cb(-1, TransferParams());
234-
return;
235-
}
236-
237-
auto key_dbs = GetGpgKeyDatabaseInfos();
238-
auto total_tasks = static_cast<int>(key_dbs.size());
239-
std::atomic<int> completed_tasks{0};
240-
std::vector<int> results(total_tasks, 0);
241-
242-
int task_index = 0;
243-
for (const auto &key_db : key_dbs) {
244-
const int current_index = task_index++;
245-
const auto target_home_dir =
246-
QDir::toNativeSeparators(QFileInfo(key_db.path).canonicalFilePath());
247-
248-
GpgFrontend::GpgCommandExecutor::ExecuteSync(
249-
{gpgconf_path,
250-
QStringList{"--homedir", target_home_dir, "--launch", "all"},
251-
[=, &completed_tasks, &results](int exit_code, const QString &,
252-
const QString &) {
253-
FLOG_D("gpgconf --launch all exit code: %d", exit_code);
254-
255-
results[current_index] = exit_code;
256-
257-
if (++completed_tasks == total_tasks && cb) {
258-
int final_result =
259-
std::all_of(results.begin(), results.end(),
260-
[](int result) { return result >= 0; })
261-
? 0
262-
: -1;
263-
cb(final_result, TransferParams());
264-
}
265-
}});
266-
}
267-
}
123+
} // namespace GpgFrontend

src/ui/main_window/KeyMgmt.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,8 @@ void KeyMgmt::delete_keys_with_warning(KeyIdArgsList uid_list) {
353353
}
354354

355355
void KeyMgmt::SlotShowKeyDetails() {
356-
auto keys_selected = key_list_->GetSelected();
357-
if (keys_selected.empty()) return;
358-
359-
auto key = GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel())
360-
.GetKey(keys_selected.front());
361-
362-
if (!key.IsGood()) {
363-
QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
364-
return;
365-
}
356+
auto [succ, key] = key_list_->GetSelectedGpgKey();
357+
if (!succ) return;
366358

367359
new KeyDetailsDialog(key_list_->GetCurrentGpgContextChannel(), key, this);
368360
}
@@ -453,20 +445,9 @@ void KeyMgmt::SlotGenerateKeyDialog() {
453445
}
454446

455447
void KeyMgmt::SlotGenerateSubKey() {
456-
auto keys_selected = key_list_->GetSelected();
457-
if (keys_selected.empty()) {
458-
QMessageBox::information(
459-
this, tr("Invalid Operation"),
460-
tr("Please select one KeyPair before doing this operation."));
461-
return;
462-
}
463-
const auto key =
464-
GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel())
465-
.GetKey(keys_selected.front());
466-
if (!key.IsGood()) {
467-
QMessageBox::critical(this, tr("Error"), tr("Key Not Found."));
468-
return;
469-
}
448+
auto [succ, key] = key_list_->GetSelectedGpgKey();
449+
if (!succ) return;
450+
470451
if (!key.IsPrivateKey()) {
471452
QMessageBox::critical(this, tr("Invalid Operation"),
472453
tr("If a key pair does not have a private key then "

src/ui/main_window/MainWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include "core/function/CacheManager.h"
3232
#include "core/function/GlobalSettingStation.h"
33-
#include "core/function/gpg/GpgAdvancedOperator.h"
33+
#include "core/function/gpg/GpgKeyGetter.h"
3434
#include "core/model/SettingsObject.h"
3535
#include "core/module/ModuleManager.h"
3636
#include "ui/UISignalStation.h"

0 commit comments

Comments
 (0)