aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/main_window/MainWindowGpgOperaFunction.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2025-01-27 22:12:00 +0000
committersaturneric <[email protected]>2025-01-27 22:12:00 +0000
commita66d868f500c0bd94fe9783172ac3d17b1dbb6f5 (patch)
tree3c2c07e0ec717fac5beec08d9678bd311a84e134 /src/ui/main_window/MainWindowGpgOperaFunction.cpp
parentrefactor: reduce code duplication of gnupg file operations (diff)
downloadGpgFrontend-a66d868f500c0bd94fe9783172ac3d17b1dbb6f5.tar.gz
GpgFrontend-a66d868f500c0bd94fe9783172ac3d17b1dbb6f5.zip
refactor: reduce code duplication of gnupg operations
Diffstat (limited to 'src/ui/main_window/MainWindowGpgOperaFunction.cpp')
-rw-r--r--src/ui/main_window/MainWindowGpgOperaFunction.cpp486
1 files changed, 98 insertions, 388 deletions
diff --git a/src/ui/main_window/MainWindowGpgOperaFunction.cpp b/src/ui/main_window/MainWindowGpgOperaFunction.cpp
index d9d2000f..a2daa06b 100644
--- a/src/ui/main_window/MainWindowGpgOperaFunction.cpp
+++ b/src/ui/main_window/MainWindowGpgOperaFunction.cpp
@@ -39,6 +39,8 @@
#include "core/utils/GpgUtils.h"
#include "ui/UserInterfaceUtils.h"
#include "ui/dialog/SignersPicker.h"
+#include "ui/function/GpgOperaHelper.h"
+#include "ui/struct/GpgOperaResultContext.h"
#include "ui/widgets/KeyList.h"
#include "ui/widgets/TextEdit.h"
@@ -47,326 +49,115 @@ namespace GpgFrontend::UI {
void MainWindow::SlotEncrypt() {
if (edit_->CurPageTextEdit() == nullptr) return;
+ auto contexts = QSharedPointer<GpgOperaContextBasement>::create();
+ contexts->ascii = true;
+
auto key_ids = m_key_list_->GetChecked();
- if (key_ids.empty()) {
- // Symmetric Encrypt
+ // Symmetric Encrypt
+ if (key_ids.isEmpty()) {
auto ret = QMessageBox::information(
this, tr("Symmetric Encryption"),
- tr("No Key Checked. Do you want to encrypt with a "
+ tr("No Key Selected. Do you want to encrypt with a "
"symmetric cipher using a passphrase?"),
QMessageBox::Ok | QMessageBox::Cancel);
-
if (ret == QMessageBox::Cancel) return;
- auto buffer = GFBuffer(edit_->CurPlainText());
- CommonUtils::WaitForOpera(
- this, tr("Symmetrically Encrypting"),
- [this, buffer](const OperaWaitingHd& op_hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .EncryptSymmetric(
- buffer, true,
- [this, op_hd](GpgError err, const DataObjectPtr& data_obj) {
- // stop waiting
- op_hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 ||
- data_obj == nullptr ||
- !data_obj->Check<GpgEncryptResult, GFBuffer>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
-
- auto result = ExtractParams<GpgEncryptResult>(data_obj, 0);
- auto buffer = ExtractParams<GFBuffer>(data_obj, 1);
-
- auto result_analyse = GpgEncryptResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- result);
- result_analyse.Analyse();
- slot_result_analyse_show_helper(result_analyse);
-
- if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- edit_->SlotFillTextEditWithText(
- buffer.ConvertToQByteArray());
- }
- info_board_->ResetOptionActionsMenu();
- });
- });
-
- return;
+ contexts->keys = {};
+ } else {
+ contexts->keys = check_keys_helper(
+ key_ids,
+ [](const GpgKey& key) { return key.IsHasActualEncryptionCapability(); },
+ tr("The selected keypair cannot be used for encryption."));
+ if (contexts->keys.empty()) return;
}
- auto keys =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKeys(key_ids);
- assert(std::all_of(keys.begin(), keys.end(),
- [](const auto& key) { return key.IsGood(); }));
-
- for (const auto& key : keys) {
- if (!key.IsHasActualEncryptionCapability()) {
- QMessageBox::information(
- this, tr("Invalid Operation"),
- tr("The selected key contains a key that does not actually have a "
- "encrypt usage.") +
- "<br/><br/>" + tr("For example the Following Key:") + " <br/>" +
- key.GetUIDs()->front().GetUID());
- return;
- }
- }
+ contexts->GetContextBuffer(0).append(GFBuffer(edit_->CurPlainText()));
+ GpgOperaHelper::BuildOperas(contexts, 0,
+ m_key_list_->GetCurrentGpgContextChannel(),
+ GpgOperaHelper::BuildOperasEncrypt);
- auto buffer = GFBuffer(edit_->CurPlainText());
- CommonUtils::WaitForOpera(
- this, tr("Encrypting"),
- [this, keys, buffer](const OperaWaitingHd& op_hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .Encrypt(
- {keys.begin(), keys.end()}, buffer, true,
- [this, op_hd](GpgError err, const DataObjectPtr& data_obj) {
- // stop waiting
- op_hd();
-
- if (data_obj == nullptr ||
- !data_obj->Check<GpgEncryptResult, GFBuffer>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
-
- auto result = ExtractParams<GpgEncryptResult>(data_obj, 0);
- auto buffer = ExtractParams<GFBuffer>(data_obj, 1);
-
- auto result_analyse = GpgEncryptResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err, result);
- result_analyse.Analyse();
- slot_result_analyse_show_helper(result_analyse);
-
- if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- edit_->SlotFillTextEditWithText(
- buffer.ConvertToQByteArray());
- }
- info_board_->ResetOptionActionsMenu();
- });
- });
+ exec_operas_helper(tr("Encrypting"), contexts);
}
void MainWindow::SlotSign() {
if (edit_->CurPageTextEdit() == nullptr) return;
- auto key_ids = m_key_list_->GetCheckedPrivateKey();
- if (key_ids.empty()) {
- QMessageBox::critical(
- this, tr("No Key Checked"),
- tr("Please check the key in the key toolbox on the right."));
- return;
- }
-
- auto keys =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKeys(key_ids);
- assert(std::all_of(keys.begin(), keys.end(),
- [](const auto& key) { return key.IsGood(); }));
+ auto contexts = QSharedPointer<GpgOperaContextBasement>::create();
+ contexts->ascii = true;
- for (const auto& key : keys) {
- if (!key.IsHasActualSigningCapability()) {
- QMessageBox::information(
- this, tr("Invalid Operation"),
- tr("The selected key contains a key that does not actually have a "
- "signature usage.") +
- "<br/><br/>" + tr("For example the Following Key:") + "<br/>" +
- key.GetUIDs()->front().GetUID());
- return;
- }
- }
-
- // set input buffer
- auto buffer = GFBuffer(edit_->CurPlainText());
- CommonUtils::WaitForOpera(
- this, tr("Signing"), [this, keys, buffer](const OperaWaitingHd& hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .Sign(
- {keys.begin(), keys.end()}, buffer, GPGME_SIG_MODE_CLEAR, true,
- [this, hd](GpgError err, const DataObjectPtr& data_obj) {
- // stop waiting
- hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 ||
- data_obj == nullptr ||
- !data_obj->Check<GpgSignResult, GFBuffer>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
- auto sign_result = ExtractParams<GpgSignResult>(data_obj, 0);
- auto sign_out_buffer = ExtractParams<GFBuffer>(data_obj, 1);
- auto result_analyse = GpgSignResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- sign_result);
- result_analyse.Analyse();
- slot_result_analyse_show_helper(result_analyse);
-
- if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- edit_->SlotFillTextEditWithText(
- sign_out_buffer.ConvertToQByteArray());
- }
- });
- });
+ auto key_ids = m_key_list_->GetChecked();
+ contexts->keys = check_keys_helper(
+ key_ids,
+ [](const GpgKey& key) { return key.IsHasActualSigningCapability(); },
+ tr("The selected key contains a key that does not actually have a "
+ "sign usage."));
+ if (contexts->keys.empty()) return;
+
+ contexts->GetContextBuffer(0).append(GFBuffer(edit_->CurPlainText()));
+ GpgOperaHelper::BuildOperas(contexts, 0,
+ m_key_list_->GetCurrentGpgContextChannel(),
+ GpgOperaHelper::BuildOperasSign);
+
+ exec_operas_helper(tr("Signing"), contexts);
}
void MainWindow::SlotDecrypt() {
if (edit_->CurPageTextEdit() == nullptr) return;
- // data to transfer into task
- auto buffer = GFBuffer(edit_->CurPlainText());
-
- CommonUtils::WaitForOpera(
- this, tr("Decrypting"), [this, buffer](const OperaWaitingHd& hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .Decrypt(buffer, [this, hd](GpgError err,
- const DataObjectPtr& data_obj) {
- // stop waiting
- hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 || data_obj == nullptr ||
- !data_obj->Check<GpgDecryptResult, GFBuffer>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
- auto decrypt_result =
- ExtractParams<GpgDecryptResult>(data_obj, 0);
- auto out_buffer = ExtractParams<GFBuffer>(data_obj, 1);
- auto result_analyse = GpgDecryptResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- decrypt_result);
- result_analyse.Analyse();
- slot_result_analyse_show_helper(result_analyse);
-
- if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- edit_->SlotFillTextEditWithText(
- out_buffer.ConvertToQByteArray());
- }
- });
- });
+ auto contexts = QSharedPointer<GpgOperaContextBasement>::create();
+ contexts->ascii = true;
+
+ contexts->GetContextBuffer(0).append(GFBuffer(edit_->CurPlainText()));
+ GpgOperaHelper::BuildOperas(contexts, 0,
+ m_key_list_->GetCurrentGpgContextChannel(),
+ GpgOperaHelper::BuildOperasDecrypt);
+
+ exec_operas_helper(tr("Decrypting"), contexts);
}
void MainWindow::SlotVerify() {
if (edit_->CurPageTextEdit() == nullptr) return;
- // set input buffer
- auto buffer = GFBuffer(edit_->CurPlainText());
-
- CommonUtils::WaitForOpera(
- this, tr("Verifying"), [this, buffer](const OperaWaitingHd& hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .Verify(buffer, GFBuffer(),
- [this, hd](GpgError err, const DataObjectPtr& data_obj) {
- // stop waiting
- hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 ||
- data_obj == nullptr ||
- !data_obj->Check<GpgVerifyResult>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
- auto verify_result =
- ExtractParams<GpgVerifyResult>(data_obj, 0);
-
- // analyse result
- auto result_analyse = GpgVerifyResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- verify_result);
- result_analyse.Analyse();
- slot_result_analyse_show_helper(result_analyse);
-
- if (!result_analyse.GetUnknownSignatures().isEmpty() &&
- Module::IsModuleActivate(kKeyServerSyncModuleID)) {
- slot_verifying_unknown_signature_helper(
- result_analyse.GetUnknownSignatures());
- }
- });
- });
-}
+ auto contexts = QSharedPointer<GpgOperaContextBasement>::create();
+ contexts->ascii = true;
+
+ contexts->GetContextBuffer(0).append(GFBuffer(edit_->CurPlainText()));
+ GpgOperaHelper::BuildOperas(contexts, 0,
+ m_key_list_->GetCurrentGpgContextChannel(),
+ GpgOperaHelper::BuildOperasVerify);
-void MainWindow::SlotVerify(const QByteArray& raw_data,
- const QByteArray& signature) {
- // set input buffer
- auto raw_data_buffer = GFBuffer(raw_data);
- auto signature_buffer = GFBuffer(signature);
-
- CommonUtils::WaitForOpera(
- this, tr("Verifying"),
- [this, raw_data_buffer, signature_buffer](const OperaWaitingHd& hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .Verify(raw_data_buffer, signature_buffer,
- [this, hd](GpgError err, const DataObjectPtr& data_obj) {
- // stop waiting
- hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 ||
- data_obj == nullptr ||
- !data_obj->Check<GpgVerifyResult>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
- auto verify_result =
- ExtractParams<GpgVerifyResult>(data_obj, 0);
-
- // analyse result
- auto result_analyse = GpgVerifyResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- verify_result);
- result_analyse.Analyse();
- slot_result_analyse_show_helper(result_analyse);
-
- if (!result_analyse.GetUnknownSignatures().isEmpty() &&
- Module::IsModuleActivate(kKeyServerSyncModuleID)) {
- slot_verifying_unknown_signature_helper(
- result_analyse.GetUnknownSignatures());
- }
- });
- });
+ exec_operas_helper(tr("Verifying"), contexts);
+
+ if (!contexts->unknown_fprs.isEmpty()) {
+ slot_verifying_unknown_signature_helper(contexts->unknown_fprs);
+ }
}
void MainWindow::SlotEncryptSign() {
if (edit_->CurPageTextEdit() == nullptr) return;
- auto key_ids = m_key_list_->GetChecked();
+ auto contexts = QSharedPointer<GpgOperaContextBasement>::create();
+ contexts->ascii = true;
- if (key_ids.empty()) {
- QMessageBox::critical(
- this, tr("No Key Checked"),
- tr("Please check some key in the key toolbox on the right."));
- return;
- }
+ auto key_ids = m_key_list_->GetChecked();
- auto keys =
- GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
- .GetKeys(key_ids);
- assert(std::all_of(keys.begin(), keys.end(),
- [](const auto& key) { return key.IsGood(); }));
+ // Symmetric Encrypt
+ if (key_ids.isEmpty()) {
+ auto ret = QMessageBox::information(
+ this, tr("Symmetric Encryption"),
+ tr("No Key Selected. Do you want to encrypt with a "
+ "symmetric cipher using a passphrase?"),
+ QMessageBox::Ok | QMessageBox::Cancel);
+ if (ret == QMessageBox::Cancel) return;
- for (const auto& key : keys) {
- bool key_can_encrypt = key.IsHasActualEncryptionCapability();
-
- if (!key_can_encrypt) {
- QMessageBox::critical(
- this, tr("Invalid KeyPair"),
- tr("The selected keypair cannot be used for encryption.") +
- "<br/><br/>" + tr("For example the Following Key:") + " <br/>" +
- key.GetUIDs()->front().GetUID());
- return;
- }
+ contexts->keys = {};
+ } else {
+ contexts->keys = check_keys_helper(
+ key_ids,
+ [](const GpgKey& key) { return key.IsHasActualEncryptionCapability(); },
+ tr("The selected keypair cannot be used for encryption."));
+ if (contexts->keys.empty()) return;
}
auto* signers_picker =
@@ -382,116 +173,35 @@ void MainWindow::SlotEncryptSign() {
auto signer_keys =
GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel())
.GetKeys(signer_key_ids);
- for (const auto& key : signer_keys) {
- assert(key.IsGood());
- }
+ assert(std::all_of(signer_keys.begin(), signer_keys.end(),
+ [](const auto& key) { return key.IsGood(); }));
+
+ contexts->singer_keys = signer_keys;
+
+ contexts->GetContextBuffer(0).append(GFBuffer(edit_->CurPlainText()));
+ GpgOperaHelper::BuildOperas(contexts, 0,
+ m_key_list_->GetCurrentGpgContextChannel(),
+ GpgOperaHelper::BuildOperasEncryptSign);
- // data to transfer into task
- auto buffer = GFBuffer(edit_->CurPlainText());
-
- CommonUtils::WaitForOpera(
- this, tr("Encrypting and Signing"),
- [this, keys, signer_keys, buffer](const OperaWaitingHd& hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .EncryptSign(
- {keys.begin(), keys.end()},
- {signer_keys.begin(), signer_keys.end()}, buffer, true,
- [this, hd](GpgError err, const DataObjectPtr& data_obj) {
- // stop waiting
- hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 ||
- data_obj == nullptr ||
- !data_obj->Check<GpgEncryptResult, GpgSignResult,
- GFBuffer>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
- auto encrypt_result =
- ExtractParams<GpgEncryptResult>(data_obj, 0);
- auto sign_result = ExtractParams<GpgSignResult>(data_obj, 1);
- auto out_buffer = ExtractParams<GFBuffer>(data_obj, 2);
-
- // analyse result
- auto encrypt_result_analyse = GpgEncryptResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- encrypt_result);
- encrypt_result_analyse.Analyse();
-
- auto sign_result_analyse = GpgSignResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- sign_result);
- sign_result_analyse.Analyse();
-
- // show analyse result
- slot_result_analyse_show_helper(encrypt_result_analyse,
- sign_result_analyse);
-
- if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- edit_->SlotFillTextEditWithText(
- out_buffer.ConvertToQByteArray());
- }
- });
- });
+ exec_operas_helper(tr("Encrypting and Signing"), contexts);
}
void MainWindow::SlotDecryptVerify() {
if (edit_->CurPageTextEdit() == nullptr) return;
- // data to transfer into task
- auto buffer = GFBuffer(edit_->CurPlainText());
-
- CommonUtils::WaitForOpera(
- this, tr("Decrypting and Verifying"),
- [this, buffer](const OperaWaitingHd& hd) {
- GpgFrontend::GpgBasicOperator::GetInstance(
- m_key_list_->GetCurrentGpgContextChannel())
- .DecryptVerify(buffer, [this, hd](GpgError err,
- const DataObjectPtr& data_obj) {
- // stop waiting
- hd();
-
- if (CheckGpgError(err) == GPG_ERR_USER_1 || data_obj == nullptr ||
- !data_obj
- ->Check<GpgDecryptResult, GpgVerifyResult, GFBuffer>()) {
- QMessageBox::critical(this, tr("Error"),
- tr("Unknown error occurred"));
- return;
- }
- auto decrypt_result =
- ExtractParams<GpgDecryptResult>(data_obj, 0);
- auto verify_result = ExtractParams<GpgVerifyResult>(data_obj, 1);
- auto out_buffer = ExtractParams<GFBuffer>(data_obj, 2);
-
- // analyse result
- auto decrypt_result_analyse = GpgDecryptResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- decrypt_result);
- decrypt_result_analyse.Analyse();
-
- auto verify_result_analyse = GpgVerifyResultAnalyse(
- m_key_list_->GetCurrentGpgContextChannel(), err,
- verify_result);
- verify_result_analyse.Analyse();
-
- // show analyse result
- slot_result_analyse_show_helper(decrypt_result_analyse,
- verify_result_analyse);
-
- if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- edit_->SlotFillTextEditWithText(
- out_buffer.ConvertToQByteArray());
- }
-
- if (!verify_result_analyse.GetUnknownSignatures().isEmpty() &&
- Module::IsModuleActivate(kKeyServerSyncModuleID)) {
- slot_verifying_unknown_signature_helper(
- verify_result_analyse.GetUnknownSignatures());
- }
- });
- });
+ auto contexts = QSharedPointer<GpgOperaContextBasement>::create();
+ contexts->ascii = true;
+
+ contexts->GetContextBuffer(0).append(GFBuffer(edit_->CurPlainText()));
+ GpgOperaHelper::BuildOperas(contexts, 0,
+ m_key_list_->GetCurrentGpgContextChannel(),
+ GpgOperaHelper::BuildOperasDecryptVerify);
+
+ exec_operas_helper(tr("Decrypting and Verifying"), contexts);
+
+ if (!contexts->unknown_fprs.isEmpty()) {
+ slot_verifying_unknown_signature_helper(contexts->unknown_fprs);
+ }
}
} // namespace GpgFrontend::UI \ No newline at end of file