diff options
author | saturneric <[email protected]> | 2023-12-26 06:48:04 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-26 06:48:04 +0000 |
commit | 9adf241f87c707ffdb644cfaff8441ff0a3c67b7 (patch) | |
tree | 83389a435105c17b3994db60ca5dbe3859f1aafb | |
parent | feat: upgrade basical operations fully to async style and update test cases (diff) | |
download | GpgFrontend-9adf241f87c707ffdb644cfaff8441ff0a3c67b7.tar.gz GpgFrontend-9adf241f87c707ffdb644cfaff8441ff0a3c67b7.zip |
fix: test basical operations of gpg at both test cases and gui
-rw-r--r-- | src/core/function/gpg/GpgKeyManager.cpp | 2 | ||||
-rw-r--r-- | src/core/model/GpgVerifyResult.cpp | 1 | ||||
-rw-r--r-- | src/test/core/GpgCoreTestBasicOpera.cpp | 8 | ||||
-rw-r--r-- | src/ui/dialog/details/VerifyDetailsDialog.cpp | 32 | ||||
-rw-r--r-- | src/ui/dialog/details/VerifyDetailsDialog.h | 1 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowFileSlotFunction.cpp | 596 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 450 |
7 files changed, 479 insertions, 611 deletions
diff --git a/src/core/function/gpg/GpgKeyManager.cpp b/src/core/function/gpg/GpgKeyManager.cpp index 7afc356a..b2de0ed7 100644 --- a/src/core/function/gpg/GpgKeyManager.cpp +++ b/src/core/function/gpg/GpgKeyManager.cpp @@ -43,7 +43,7 @@ auto GpgFrontend::GpgKeyManager::SignKey( const GpgFrontend::GpgKey& target, GpgFrontend::KeyArgsList& keys, const std::string& uid, const std::unique_ptr<boost::posix_time::ptime>& expires) -> bool { - GpgBasicOperator::GetInstance().SetSigners(keys); + GpgBasicOperator::GetInstance().SetSigners(keys, true); unsigned int flags = 0; unsigned int expires_time_t = 0; diff --git a/src/core/model/GpgVerifyResult.cpp b/src/core/model/GpgVerifyResult.cpp index 98b0e527..75421c69 100644 --- a/src/core/model/GpgVerifyResult.cpp +++ b/src/core/model/GpgVerifyResult.cpp @@ -57,5 +57,6 @@ auto GpgVerifyResult::GetSignature() const -> std::vector<GpgSignature> { sigatures.emplace_back(signature); signature = signature->next; } + return sigatures; } } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/test/core/GpgCoreTestBasicOpera.cpp b/src/test/core/GpgCoreTestBasicOpera.cpp index de6d6805..8c93f3f8 100644 --- a/src/test/core/GpgCoreTestBasicOpera.cpp +++ b/src/test/core/GpgCoreTestBasicOpera.cpp @@ -161,7 +161,7 @@ TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { auto d_result = ExtractParams<GpgVerifyResult>(data_obj, 0); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_FALSE(d_result.GetSignature().empty()); - ASSERT_EQ(d_result.GetSignature().at(0), + ASSERT_EQ(d_result.GetSignature().at(0).GetFingerprint(), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); }); }); @@ -187,7 +187,7 @@ TEST_F(GpgCoreTest, CoreSignVerifyDetachTest) { auto d_result = ExtractParams<GpgVerifyResult>(data_obj, 0); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_FALSE(d_result.GetSignature().empty()); - ASSERT_EQ(d_result.GetSignature().at(0), + ASSERT_EQ(d_result.GetSignature().at(0).GetFingerprint(), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); }); }); @@ -213,7 +213,7 @@ TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { auto verify_reult = ExtractParams<GpgVerifyResult>(data_obj, 0); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_FALSE(verify_reult.GetSignature().empty()); - ASSERT_EQ(verify_reult.GetSignature().at(0), + ASSERT_EQ(verify_reult.GetSignature().at(0).GetFingerprint(), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); }); }); @@ -259,7 +259,7 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { ASSERT_EQ(decrypt_result.Recipients()[0].keyid, "F89C95A05088CC93"); ASSERT_FALSE(verify_reult.GetSignature().empty()); - ASSERT_EQ(verify_reult.GetSignature().at(0), + ASSERT_EQ(verify_reult.GetSignature().at(0).GetFingerprint(), "8933EB283A18995F45D61DAC021D89771B680FFB"); }); }); diff --git a/src/ui/dialog/details/VerifyDetailsDialog.cpp b/src/ui/dialog/details/VerifyDetailsDialog.cpp index 9cb61963..f77660a5 100644 --- a/src/ui/dialog/details/VerifyDetailsDialog.cpp +++ b/src/ui/dialog/details/VerifyDetailsDialog.cpp @@ -36,7 +36,7 @@ namespace GpgFrontend::UI { VerifyDetailsDialog::VerifyDetailsDialog(QWidget* parent, GpgError error, GpgVerifyResult result) - : QDialog(parent), m_result_(std::move(result)), error_(error) { + : QDialog(parent), m_result_(result), error_(error) { this->setWindowTitle(_("Signatures Details")); main_layout_ = new QHBoxLayout(); @@ -49,7 +49,7 @@ VerifyDetailsDialog::VerifyDetailsDialog(QWidget* parent, GpgError error, void VerifyDetailsDialog::slot_refresh() { m_vbox_ = new QWidget(); - auto* mVboxLayout = new QVBoxLayout(m_vbox_); + auto* m_vbox_layout = new QVBoxLayout(m_vbox_); main_layout_->addWidget(m_vbox_); // Button Box for close button @@ -57,47 +57,45 @@ void VerifyDetailsDialog::slot_refresh() { connect(button_box_, &QDialogButtonBox::rejected, this, &VerifyDetailsDialog::close); - auto sign = m_result_->signatures; + auto signatures = m_result_.GetSignature(); - if (sign == nullptr) { - mVboxLayout->addWidget(new QLabel(_("No valid input found"))); - mVboxLayout->addWidget(button_box_); + if (signatures.empty()) { + m_vbox_layout->addWidget(new QLabel(_("No valid input found"))); + m_vbox_layout->addWidget(button_box_); return; } // Get timestamp of signature of current text QDateTime timestamp; #ifdef GPGFRONTEND_GUI_QT6 - timestamp.setSecsSinceEpoch(sign->timestamp); + timestamp.setSecsSinceEpoch(to_time_t(signatures[0].GetCreateTime())); #else timestamp.setTime_t(sign->timestamp); #endif // Set the title widget depending on sign status - if (gpg_err_code(sign->status) == GPG_ERR_BAD_SIGNATURE) { - mVboxLayout->addWidget(new QLabel(_("Error Validating signature"))); + if (gpg_err_code(signatures[0].GetStatus()) == GPG_ERR_BAD_SIGNATURE) { + m_vbox_layout->addWidget(new QLabel(_("Error Validating signature"))); } else if (input_signature_ != nullptr) { const auto info = (boost::format(_("File was signed on %1%")) % QLocale::system().toString(timestamp).toStdString()) .str() + "<br/>" + _("It Contains") + ": " + "<br/><br/>"; - mVboxLayout->addWidget(new QLabel(info.c_str())); + m_vbox_layout->addWidget(new QLabel(info.c_str())); } else { const auto info = (boost::format(_("Signed on %1%")) % QLocale::system().toString(timestamp).toStdString()) .str() + "<br/>" + _("It Contains") + ": " + "<br/><br/>"; - mVboxLayout->addWidget(new QLabel(info.c_str())); + m_vbox_layout->addWidget(new QLabel(info.c_str())); } // Add information box for every single key - while (sign) { - GpgSignature signature(sign); - auto* sign_box = new VerifyKeyDetailBox(signature, this); - sign = sign->next; - mVboxLayout->addWidget(sign_box); + for (const auto& signature : signatures) { + auto* detail_box = new VerifyKeyDetailBox(signature, this); + m_vbox_layout->addWidget(detail_box); } - mVboxLayout->addWidget(button_box_); + m_vbox_layout->addWidget(button_box_); } } // namespace GpgFrontend::UI
\ No newline at end of file diff --git a/src/ui/dialog/details/VerifyDetailsDialog.h b/src/ui/dialog/details/VerifyDetailsDialog.h index 44c2e387..7b684e07 100644 --- a/src/ui/dialog/details/VerifyDetailsDialog.h +++ b/src/ui/dialog/details/VerifyDetailsDialog.h @@ -29,6 +29,7 @@ #pragma once #include "core/function/result_analyse/GpgResultAnalyse.h" +#include "core/model/GpgVerifyResult.h" #include "ui/GpgFrontendUI.h" #include "ui/widgets/PlainTextEditorPage.h" #include "ui/widgets/VerifyKeyDetailBox.h" diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index f26a9587..7447a1e7 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -71,14 +71,15 @@ bool path_pre_check(QWidget* parent, const QString& path) { * @param path the directory to be converted * @return */ -bool process_tarball_into_directory(QWidget* parent, - std::filesystem::path& path) { +auto ProcessTarballIntoDirectory(QWidget* parent, + const std::filesystem::path& path) + -> std::tuple<bool, std::filesystem::path> { SPDLOG_DEBUG("converting directory into tarball: {}", path.u8string()); auto selected_dir_path = std::filesystem::path(path); if (selected_dir_path.extension() != ".tar") { QMessageBox::critical(parent, _("Error"), _("The file is not a tarball.")); - return false; + return {false, path}; } try { @@ -105,12 +106,11 @@ bool process_tarball_into_directory(QWidget* parent, if (if_error || !exists(target_path)) { throw std::runtime_error("Decompress Failed"); } - path = target_path.u8string().c_str(); + return {true, target_path.u8string()}; } catch (...) { SPDLOG_ERROR("decompress error"); - return false; + return {false, path}; } - return true; } /** @@ -158,8 +158,8 @@ bool process_directory_into_tarball(QWidget* parent, QString& path) { } void MainWindow::SlotFileEncrypt() { - auto fileTreeView = edit_->SlotCurPageFileTreeView(); - auto path = fileTreeView->GetSelected(); + auto* file_tree_view = edit_->SlotCurPageFileTreeView(); + auto path = file_tree_view->GetSelected(); if (!path_pre_check(this, path)) { SPDLOG_ERROR("path pre check failed"); @@ -168,30 +168,22 @@ void MainWindow::SlotFileEncrypt() { // check selected keys auto key_ids = m_key_list_->GetChecked(); - GpgEncrResult result = nullptr; - GpgError error; - bool if_error = false; - - bool non_ascii_when_export = + bool const non_ascii_when_export = GlobalSettingStation::GetInstance().LookupSettings( "general.non_ascii_when_export", true); // get file info - QFileInfo file_info(path); - + QFileInfo const file_info(path); if (file_info.isDir()) { path = path + (file_info.isDir() ? ".tar" : ""); } - auto _channel = kGpgFrontendDefaultChannel; - auto _extension = ".asc"; + const auto* extension = ".asc"; if (non_ascii_when_export || file_info.isDir()) { - _channel = kGpgFrontendNonAsciiChannel; - _extension = ".gpg"; + extension = ".gpg"; } - auto out_path = path + _extension; - + auto out_path = path + extension; if (QFile::exists(out_path)) { #ifdef WINDOWS std::filesystem::path _out_path = out_path.toStdU16String(); @@ -224,76 +216,96 @@ void MainWindow::SlotFileEncrypt() { _("No Key Selected. Do you want to encrypt with a " "symmetric cipher using a passphrase?"), QMessageBox::Ok | QMessageBox::Cancel); - if (ret == QMessageBox::Cancel) return; - process_operation(this, _("Symmetrically Encrypting"), - [&](DataObjectPtr) -> int { - try { - // error = - // GpgFrontend::GpgFileOpera::EncryptFileSymmetric( - // path.toStdString(), out_path.toStdString(), - // result, _channel); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; - }); - } else { - auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); - - // check key abilities - for (const auto& key : *p_keys) { - bool key_can_encrypt = key.IsHasActualEncryptionCapability(); - - if (!key_can_encrypt) { - QMessageBox::critical( - nullptr, _("Invalid KeyPair"), - QString(_("The selected keypair cannot be used for encryption.")) + - "<br/><br/>" + _("For example the Following Key:") + " <br/>" + - QString::fromStdString(key.GetUIDs()->front().GetUID())); - return; - } - } + CommonUtils::WaitForOpera( + this, _("Symmetrically Encrypting"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::EncryptFileSymmetric( + path.toStdString(), out_path.toStdString(), + !non_ascii_when_export, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (data_obj == nullptr || + !data_obj->Check<GpgEncryptResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); + auto result_analyse = GpgEncryptResultAnalyse(err, result); + result_analyse.Analyse(); + + process_result_analyse(edit_, info_board_, result_analyse); + file_tree_view->update(); + + // remove xxx.tar and only left xxx.tar.gpg + if (file_info.isDir()) { + auto selected_dir_path = + std::filesystem::path(path.toStdString()); + auto target_path = + selected_dir_path.replace_extension(".tar"); + if (exists(target_path)) { + std::filesystem::remove(target_path); + } + } + }); + }); - process_operation(this, _("Encrypting"), [&](DataObjectPtr) -> int { - try { - // TODO - // error = - // GpgFileOpera::EncryptFile(std::move(p_keys), path.toStdString(), - // out_path.toStdString(), result, - // _channel); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; - }); + return; } + auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); - // remove xxx.tar and only left xxx.tar.gpg - if (file_info.isDir()) { - auto selected_dir_path = std::filesystem::path(path.toStdString()); - auto target_path = selected_dir_path.replace_extension(".tar"); - if (exists(target_path)) { - std::filesystem::remove(target_path); + // check key abilities + for (const auto& key : *p_keys) { + bool const key_can_encrypt = key.IsHasActualEncryptionCapability(); + + if (!key_can_encrypt) { + QMessageBox::critical( + nullptr, _("Invalid KeyPair"), + QString(_("The selected keypair cannot be used for encryption.")) + + "<br/><br/>" + _("For example the Following Key:") + " <br/>" + + QString::fromStdString(key.GetUIDs()->front().GetUID())); + return; } } - if (!if_error) { - // auto resultAnalyse = GpgEncryptResultAnalyse(error, std::move(result)); - // resultAnalyse.Analyse(); - // process_result_analyse(edit_, info_board_, resultAnalyse); - // fileTreeView->update(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } + CommonUtils::WaitForOpera( + this, _("Encrypting"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::EncryptFile( + {p_keys->begin(), p_keys->end()}, path.toStdString(), + out_path.toStdString(), !non_ascii_when_export, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (data_obj == nullptr || !data_obj->Check<GpgEncryptResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); + auto result_analyse = GpgEncryptResultAnalyse(err, result); + result_analyse.Analyse(); + + process_result_analyse(edit_, info_board_, result_analyse); + file_tree_view->update(); + + // remove xxx.tar and only left xxx.tar.gpg + if (file_info.isDir()) { + auto selected_dir_path = + std::filesystem::path(path.toStdString()); + auto target_path = selected_dir_path.replace_extension(".tar"); + if (exists(target_path)) { + std::filesystem::remove(target_path); + } + } + }); + }); } void MainWindow::SlotFileDecrypt() { - auto fileTreeView = edit_->SlotCurPageFileTreeView(); - auto path = fileTreeView->GetSelected(); + auto* file_tree_view = edit_->SlotCurPageFileTreeView(); + auto path = file_tree_view->GetSelected(); if (!path_pre_check(this, path)) return; @@ -318,56 +330,56 @@ void MainWindow::SlotFileDecrypt() { if (ret == QMessageBox::Cancel) return; } - GpgDecrResult result = nullptr; - gpgme_error_t error; - bool if_error = false; - process_operation(this, _("Decrypting"), [&](DataObjectPtr) -> int { - try { - // TODO - // error = GpgFileOpera::DecryptFile(path.toStdString(), - // out_path.u8string(), - // result); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; - }); - - if (!if_error) { - auto resultAnalyse = GpgDecryptResultAnalyse(error, std::move(result)); - resultAnalyse.Analyse(); - process_result_analyse(edit_, info_board_, resultAnalyse); - - fileTreeView->update(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - - // extract the tarball - if (out_path.extension() == ".tar" && exists(out_path)) { - bool ret = QMessageBox::information( - this, _("Decrypting"), - _("Do you want to extract and delete the decrypted tarball?"), - QMessageBox::Ok | QMessageBox::Cancel); - if (ret) { - if (process_tarball_into_directory(this, out_path)) { - QMessageBox::information(this, _("Decrypting"), - _("Extracting tarball succeeded.")); - // remove tarball - std::filesystem::remove(out_path); - } else { - QMessageBox::critical(this, _("Decrypting"), - _("Extracting tarball failed.")); - } - } - } + CommonUtils::WaitForOpera( + this, _("Decrypting"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::DecryptFile( + path.toStdString(), out_path.u8string(), + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + auto result = ExtractParams<GpgDecryptResult>(data_obj, 0); + + if (data_obj == nullptr || !data_obj->Check<GpgDecryptResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto result_analyse = GpgDecryptResultAnalyse(err, result); + result_analyse.Analyse(); + + process_result_analyse(edit_, info_board_, result_analyse); + file_tree_view->update(); + + // extract the tarball + if (out_path.extension() == ".tar" && exists(out_path)) { + bool const ret = + QMessageBox::information( + this, _("Decrypting"), + _("Do you want to extract and delete the decrypted " + "tarball?"), + QMessageBox::Ok | QMessageBox::Cancel) != 0; + if (ret) { + auto archieve_result = + ProcessTarballIntoDirectory(this, out_path); + if (std::get<0>(archieve_result)) { + QMessageBox::information( + this, _("Decrypting"), + _("Extracting tarball succeeded.")); + // remove tarball + std::filesystem::remove(out_path); + } else { + QMessageBox::critical(this, _("Decrypting"), + _("Extracting tarball failed.")); + } + } + } + }); + }); } void MainWindow::SlotFileSign() { - auto fileTreeView = edit_->SlotCurPageFileTreeView(); - auto path = fileTreeView->GetSelected(); + auto* file_tree_view = edit_->SlotCurPageFileTreeView(); + auto path = file_tree_view->GetSelected(); if (!path_pre_check(this, path)) return; @@ -397,11 +409,9 @@ void MainWindow::SlotFileSign() { GlobalSettingStation::GetInstance().LookupSettings( "general.non_ascii_when_export", true); - auto _channel = kGpgFrontendDefaultChannel; - auto _extension = ".asc"; + const auto* extension = ".asc"; if (non_ascii_when_export) { - _channel = kGpgFrontendNonAsciiChannel; - _extension = ".sig"; + extension = ".sig"; } #ifdef WINDOWS @@ -411,7 +421,7 @@ void MainWindow::SlotFileSign() { #endif auto sig_file_path = in_path; - sig_file_path += _extension; + sig_file_path += extension; if (exists(sig_file_path)) { auto ret = QMessageBox::warning( this, _("Warning"), @@ -423,40 +433,32 @@ void MainWindow::SlotFileSign() { if (ret == QMessageBox::Cancel) return; } - GpgSignResult result = nullptr; - gpgme_error_t error; - bool if_error = false; - - process_operation(this, _("Signing"), [&](DataObjectPtr) -> int { - try { - error = - GpgFileOpera::SignFile(std::move(keys), in_path.u8string(), - sig_file_path.u8string(), result, _channel); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; - }); + CommonUtils::WaitForOpera( + this, _("Signing"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::EncryptFile( + {keys->begin(), keys->end()}, in_path.u8string(), + sig_file_path.u8string(), !non_ascii_when_export, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); - if (!if_error) { - auto resultAnalyse = GpgSignResultAnalyse(error, std::move(result)); - resultAnalyse.Analyse(); - process_result_analyse(edit_, info_board_, resultAnalyse); + if (data_obj == nullptr || !data_obj->Check<GpgSignResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } - fileTreeView->update(); + auto result = ExtractParams<GpgSignResult>(data_obj, 0); + auto result_analyse = GpgSignResultAnalyse(err, result); + result_analyse.Analyse(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - - fileTreeView->update(); + process_result_analyse(edit_, info_board_, result_analyse); + file_tree_view->update(); + }); + }); } void MainWindow::SlotFileVerify() { - auto fileTreeView = edit_->SlotCurPageFileTreeView(); - auto path = fileTreeView->GetSelected(); + auto* file_tree_view = edit_->SlotCurPageFileTreeView(); + auto path = file_tree_view->GetSelected(); #ifdef WINDOWS std::filesystem::path in_path = path.toStdU16String(); @@ -464,16 +466,8 @@ void MainWindow::SlotFileVerify() { std::filesystem::path in_path = path.toStdString(); #endif - std::filesystem::path sign_file_path = in_path, data_file_path; - - bool non_ascii_when_export = - GlobalSettingStation::GetInstance().LookupSettings( - "general.non_ascii_when_export", true); - - auto _channel = kGpgFrontendDefaultChannel; - if (non_ascii_when_export) { - _channel = kGpgFrontendNonAsciiChannel; - } + std::filesystem::path sign_file_path = in_path; + std::filesystem::path data_file_path; if (in_path.extension() == ".gpg") { swap(data_file_path, sign_file_path); @@ -486,7 +480,7 @@ void MainWindow::SlotFileVerify() { if (in_path.extension() != ".gpg") { bool ok; - QString text = QInputDialog::getText( + QString const text = QInputDialog::getText( this, _("Origin file to verify"), _("Filepath"), QLineEdit::Normal, data_file_path.u8string().c_str(), &ok); if (ok && !text.isEmpty()) { @@ -508,39 +502,33 @@ void MainWindow::SlotFileVerify() { SPDLOG_DEBUG("data path: {}", data_file_path.u8string()); SPDLOG_DEBUG("sign path: {}", sign_file_path.u8string()); - GpgVerifyResult result = nullptr; - gpgme_error_t error; - bool if_error = false; - process_operation(this, _("Verifying"), [&](DataObjectPtr) -> int { - try { - error = - GpgFileOpera::VerifyFile(data_file_path.u8string(), - sign_file_path.u8string(), result, _channel); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; - }); - - if (!if_error) { - auto result_analyse = GpgVerifyResultAnalyse(error, result); - result_analyse.Analyse(); - process_result_analyse(edit_, info_board_, result_analyse); - - if (result_analyse.GetStatus() == -2) { - import_unknown_key_from_keyserver(this, result_analyse); - } - - if (result_analyse.GetStatus() >= 0) { - show_verify_details(this, info_board_, error, result); - } - - fileTreeView->update(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } + CommonUtils::WaitForOpera( + this, _("Verifying"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::DecryptFile( + data_file_path.u8string(), sign_file_path.u8string(), + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (data_obj == nullptr || !data_obj->Check<GpgVerifyResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto result = ExtractParams<GpgVerifyResult>(data_obj, 0); + auto result_analyse = GpgVerifyResultAnalyse(err, result); + result_analyse.Analyse(); + + process_result_analyse(edit_, info_board_, result_analyse); + if (result_analyse.GetStatus() == -2) { + import_unknown_key_from_keyserver(this, result_analyse); + } + if (result_analyse.GetStatus() >= 0) { + show_verify_details(this, info_board_, err, result); + } + + file_tree_view->update(); + }); + }); } void MainWindow::SlotFileEncryptSign() { @@ -574,21 +562,19 @@ void MainWindow::SlotFileEncryptSign() { } } - bool non_ascii_when_export = + bool const non_ascii_when_export = GlobalSettingStation::GetInstance().LookupSettings( "general.non_ascii_when_export", true); // get file info - QFileInfo file_info(path); + QFileInfo const file_info(path); if (file_info.isDir()) { path = path + (file_info.isDir() ? ".tar" : ""); } - auto channel = kGpgFrontendDefaultChannel; const auto* extension = ".asc"; if (non_ascii_when_export || file_info.isDir()) { - channel = kGpgFrontendNonAsciiChannel; extension = ".gpg"; } @@ -624,48 +610,47 @@ void MainWindow::SlotFileEncryptSign() { } } - GpgEncrResult encr_result = nullptr; - GpgSignResult sign_result = nullptr; - - gpgme_error_t error; - bool if_error = false; - - process_operation( - this, _("Encrypting and Signing"), [&](DataObjectPtr) -> int { - try { - error = GpgFileOpera::EncryptSignFile( - std::move(p_keys), std::move(p_signer_keys), path.toStdString(), - out_path.toStdString(), encr_result, sign_result, channel); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; + CommonUtils::WaitForOpera( + this, _("Encrypting and Signing"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::EncryptSignFile( + {p_keys->begin(), p_keys->end()}, + {p_signer_keys->begin(), p_signer_keys->end()}, path.toStdString(), + out_path.toStdString(), !non_ascii_when_export, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (data_obj == nullptr || + !data_obj->Check<GpgEncryptResult, GpgSignResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + auto encrypt_result = + ExtractParams<GpgEncryptResult>(data_obj, 0); + auto sign_result = ExtractParams<GpgSignResult>(data_obj, 1); + + auto encrypt_result_analyse = + GpgEncryptResultAnalyse(err, encrypt_result); + encrypt_result_analyse.Analyse(); + + auto sign_result_analyse = GpgSignResultAnalyse(err, sign_result); + sign_result_analyse.Analyse(); + + process_result_analyse(edit_, info_board_, encrypt_result_analyse, + sign_result_analyse); + + file_tree_view->update(); + + // remove xxx.tar and only left xxx.tar.gpg + if (file_info.isDir()) { + auto selected_dir_path = + std::filesystem::path(path.toStdString()); + auto target_path = selected_dir_path.replace_extension(".tar"); + if (exists(target_path)) { + std::filesystem::remove(target_path); + } + } + }); }); - - if (!if_error) { - // auto encrypt_result = - // GpgEncryptResultAnalyse(error, std::move(encr_result)); - // auto sign_res = GpgSignResultAnalyse(error, std::move(sign_result)); - // encrypt_result.Analyse(); - // sign_res.Analyse(); - // process_result_analyse(edit_, info_board_, encrypt_result, sign_res); - - // file_tree_view->update(); - - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - - // remove xxx.tar and only left xxx.tar.gpg - if (file_info.isDir()) { - auto selected_dir_path = std::filesystem::path(path.toStdString()); - auto target_path = selected_dir_path.replace_extension(".tar"); - if (exists(target_path)) { - std::filesystem::remove(target_path); - } - } } void MainWindow::SlotFileDecryptVerify() { @@ -699,62 +684,65 @@ void MainWindow::SlotFileDecryptVerify() { if (ret == QMessageBox::Cancel) return; } - GpgDecrResult d_result = nullptr; - GpgVerifyResult v_result = nullptr; - gpgme_error_t error; - bool if_error = false; - process_operation( - this, _("Decrypting and Verifying"), [&](DataObjectPtr) -> int { - try { - error = GpgFileOpera::DecryptVerifyFile( - path.toStdString(), out_path.u8string(), d_result, v_result); - } catch (const std::runtime_error& e) { - if_error = true; - } - return 0; + CommonUtils::WaitForOpera( + this, _("Decrypting and Verifying"), [=](const OperaWaitingHd& op_hd) { + GpgFileOpera::DecryptVerifyFile( + path.toStdString(), out_path.u8string(), + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (data_obj == nullptr || + !data_obj->Check<GpgDecryptResult, GpgVerifyResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + auto decrypt_result = + ExtractParams<GpgDecryptResult>(data_obj, 0); + auto verify_result = ExtractParams<GpgVerifyResult>(data_obj, 1); + + auto decrypt_result_analyse = + GpgDecryptResultAnalyse(err, decrypt_result); + decrypt_result_analyse.Analyse(); + + auto verify_result_analyse = + GpgVerifyResultAnalyse(err, verify_result); + verify_result_analyse.Analyse(); + + process_result_analyse(edit_, info_board_, decrypt_result_analyse, + verify_result_analyse); + if (verify_result_analyse.GetStatus() == -2) { + import_unknown_key_from_keyserver(this, verify_result_analyse); + } + if (verify_result_analyse.GetStatus() >= 0) { + show_verify_details(this, info_board_, err, verify_result); + } + file_tree_view->update(); + + // extract the tarball + if (out_path.extension() == ".tar" && exists(out_path)) { + bool const ret = + QMessageBox::information( + this, _("Decrypting"), + _("Do you want to extract and delete the decrypted " + "tarball?"), + QMessageBox::Ok | QMessageBox::Cancel) != 0; + if (ret) { + auto archieve_result = + ProcessTarballIntoDirectory(this, out_path); + if (std::get<0>(archieve_result)) { + QMessageBox::information( + this, _("Decrypting"), + _("Extracting tarball succeeded.")); + // remove tarball + std::filesystem::remove(out_path); + } else { + QMessageBox::critical(this, _("Decrypting"), + _("Extracting tarball failed.")); + } + } + } + }); }); - - if (!if_error) { - auto decrypt_res = GpgDecryptResultAnalyse(error, std::move(d_result)); - auto verify_res = GpgVerifyResultAnalyse(error, v_result); - decrypt_res.Analyse(); - verify_res.Analyse(); - process_result_analyse(edit_, info_board_, decrypt_res, verify_res); - - if (verify_res.GetStatus() == -2) { - import_unknown_key_from_keyserver(this, verify_res); - } - - if (verify_res.GetStatus() >= 0) { - show_verify_details(this, info_board_, error, v_result); - } - - file_tree_view->update(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - - // extract the tarball - if (out_path.extension() == ".tar" && exists(out_path)) { - bool ret = - QMessageBox::information( - this, _("Decrypting"), - _("Do you want to extract and delete the decrypted tarball?"), - QMessageBox::Ok | QMessageBox::Cancel) != 0; - if (ret) { - if (process_tarball_into_directory(this, out_path)) { - QMessageBox::information(this, _("Decrypting"), - _("Extracting tarball succeeded.")); - // remove tarball - std::filesystem::remove(out_path); - } else { - QMessageBox::critical(this, _("Decrypting"), - _("Extracting tarball failed.")); - } - } - } } } // namespace GpgFrontend::UI diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 35992395..191e80cb 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -80,12 +80,17 @@ void MainWindow::slot_encrypt() { auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Symmetrically Encrypting"), - [this, buffer](const OperaWaitingHd& hd) { + [this, buffer](const OperaWaitingHd& op_hd) { GpgFrontend::GpgBasicOperator::GetInstance().EncryptSymmetric( buffer, true, - [this, hd](GpgError err, const DataObjectPtr& data_obj) { + [this, op_hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting - hd(); + op_hd(); + + if (data_obj == nullptr || + !data_obj->Check<GpgEncryptResult, GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); auto buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -120,12 +125,17 @@ void MainWindow::slot_encrypt() { auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( - this, _("Encrypting"), [this, keys, buffer](const OperaWaitingHd& hd) { + this, _("Encrypting"), [this, keys, buffer](const OperaWaitingHd& op_hd) { GpgFrontend::GpgBasicOperator::GetInstance().Encrypt( {keys->begin(), keys->end()}, buffer, true, - [this, hd](GpgError err, const DataObjectPtr& data_obj) { + [this, op_hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting - hd(); + op_hd(); + + if (data_obj == nullptr || + !data_obj->Check<GpgEncryptResult, GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); auto buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -171,60 +181,32 @@ void MainWindow::slot_sign() { } } - // data to transfer into task - auto data_object = TransferParams(); - // set input buffer - auto buffer = - edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString(); - data_object->AppendObject(std::move(buffer)); - - // push the keys into data object - data_object->AppendObject(std::move(keys)); - - auto sign_ruunner = [](DataObjectPtr data_object) -> int { - // check the size of the data object - if (data_object == nullptr || data_object->GetObjectSize() != 2) - throw std::runtime_error("data object doesn't pass checking"); - - auto buffer = ExtractParams<std::string>(data_object, 0); - auto keys = ExtractParams<KeyListPtr>(data_object, 1); - - try { - GpgSignResult result = nullptr; - auto tmp = GpgFrontend::SecureCreateSharedObject<ByteArray>(); - GpgError error = GpgFrontend::GpgBasicOperator::GetInstance().Sign( - std::move(keys), buffer, tmp, GPGME_SIG_MODE_CLEAR, result); - - data_object->Swap({error, result, tmp}); - } catch (const std::runtime_error& e) { - return -1; - } - return 0; - }; - - auto result_callback = [this](int rtn, DataObjectPtr data_object) { - if (!rtn) { - if (data_object == nullptr || data_object->GetObjectSize() != 3) - throw std::runtime_error("data object doesn't pass checking"); - auto error = ExtractParams<GpgError>(data_object, 0); - auto result = ExtractParams<GpgSignResult>(data_object, 1); - auto tmp = ExtractParams<ByteArrayPtr>(data_object, 2); - auto resultAnalyse = GpgSignResultAnalyse(error, std::move(result)); - resultAnalyse.Analyse(); - process_result_analyse(edit_, info_board_, resultAnalyse); - - if (CheckGpgError(error) == GPG_ERR_NO_ERROR) - edit_->SlotFillTextEditWithText(QString::fromStdString(*tmp)); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - }; + auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); + CommonUtils::WaitForOpera( + this, _("Signing"), [this, keys, buffer](const OperaWaitingHd& hd) { + GpgFrontend::GpgBasicOperator::GetInstance().Sign( + {keys->begin(), keys->end()}, buffer, GPGME_SIG_MODE_CLEAR, true, + [this, hd](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + hd(); + + if (data_obj == nullptr || + !data_obj->Check<GpgSignResult, GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + auto sign_result = ExtractParams<GpgSignResult>(data_obj, 0); + auto sign_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); + auto result_analyse = GpgSignResultAnalyse(err, sign_result); + result_analyse.Analyse(); + process_result_analyse(edit_, info_board_, result_analyse); - process_operation(this, _("Signing"), std::move(sign_ruunner), - std::move(result_callback), data_object); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + edit_->SlotFillTextEditWithText( + sign_out_buffer.ConvertToQByteArray()); + } + }); + }); } void MainWindow::slot_decrypt() { @@ -233,55 +215,34 @@ void MainWindow::slot_decrypt() { return; } - QByteArray text = edit_->CurTextPage()->GetTextPage()->toPlainText().toUtf8(); - // data to transfer into task - auto data_object = TransferParams( - edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString()); - - auto decrypt_runner = [](DataObjectPtr data_object) -> int { - // check the size of the data object - if (data_object == nullptr || data_object->GetObjectSize() != 1) - throw std::runtime_error("data object doesn't pass checking"); - - auto buffer = ExtractParams<std::string>(data_object, 0); - try { - GpgDecrResult result = nullptr; - auto decrypted = GpgFrontend::SecureCreateSharedObject<ByteArray>(); - // GpgError error = GpgFrontend::GpgBasicOperator::GetInstance().Decrypt( - // GFBuffer(buffer), decrypted, result); - GpgError error; - - data_object->Swap({error, result, decrypted}); - } catch (const std::runtime_error& e) { - return -1; - } - return 0; - }; - - auto result_callback = [this](int rtn, DataObjectPtr data_object) { - if (!rtn) { - if (data_object == nullptr || - !data_object->Check<GpgError, GpgDecrResult, ByteArrayPtr>()) - throw std::runtime_error("data object doesn't pass checking"); - auto error = ExtractParams<GpgError>(data_object, 0); - auto result = ExtractParams<GpgDecrResult>(data_object, 1); - auto decrypted = ExtractParams<ByteArrayPtr>(data_object, 2); - auto resultAnalyse = GpgDecryptResultAnalyse(error, std::move(result)); - resultAnalyse.Analyse(); - process_result_analyse(edit_, info_board_, resultAnalyse); - - if (CheckGpgError(error) == GPG_ERR_NO_ERROR) - edit_->SlotFillTextEditWithText(QString::fromStdString(*decrypted)); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - }; + auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); + + CommonUtils::WaitForOpera( + this, _("Decrypting"), [this, buffer](const OperaWaitingHd& hd) { + GpgFrontend::GpgBasicOperator::GetInstance().Decrypt( + buffer, [this, hd](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + hd(); - process_operation(this, _("Decrypting"), std::move(decrypt_runner), - std::move(result_callback), data_object); + if (data_obj == nullptr || + !data_obj->Check<GpgDecryptResult, GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + auto decrypt_result = + ExtractParams<GpgDecryptResult>(data_obj, 0); + auto out_buffer = ExtractParams<GFBuffer>(data_obj, 1); + auto result_analyse = + GpgDecryptResultAnalyse(err, decrypt_result); + result_analyse.Analyse(); + process_result_analyse(edit_, info_board_, result_analyse); + + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + edit_->SlotFillTextEditWithText( + out_buffer.ConvertToQByteArray()); + } + }); + }); } void MainWindow::slot_verify() { @@ -290,64 +251,28 @@ void MainWindow::slot_verify() { return; } - // data to transfer into task - auto data_object = TransferParams(); - // set input buffer - auto buffer = - edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString(); - data_object->AppendObject(std::move(buffer)); - - auto verify_runner = [](DataObjectPtr data_object) -> int { - // check the size of the data object - if (data_object == nullptr || !data_object->Check<std::string>()) - throw std::runtime_error("data object doesn't pass checking"); - - auto buffer = ExtractParams<std::string>(data_object, 0); - - SPDLOG_DEBUG("verify buffer size: {}", buffer.size()); + auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); - try { - GpgVerifyResult verify_result = nullptr; - auto sig_buffer = ByteArrayPtr(nullptr); - GpgError error = GpgFrontend::GpgBasicOperator::GetInstance().Verify( - buffer, sig_buffer, verify_result); + CommonUtils::WaitForOpera( + this, _("Verifying"), [this, buffer](const OperaWaitingHd& hd) { + GpgFrontend::GpgBasicOperator::GetInstance().Verify( + buffer, GFBuffer(), + [this, hd](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + hd(); - data_object->Swap({error, verify_result}); - } catch (const std::runtime_error& e) { - return -1; - } - return 0; - }; - - auto result_callback = [this](int rtn, DataObjectPtr data_object) { - if (!rtn) { - if (data_object == nullptr || - !data_object->Check<GpgError, GpgVerifyResult>()) - throw std::runtime_error("data object doesn't pass checking"); - auto error = ExtractParams<GpgError>(data_object, 0); - auto verify_result = ExtractParams<GpgVerifyResult>(data_object, 1); - - auto result_analyse = GpgVerifyResultAnalyse(error, verify_result); - result_analyse.Analyse(); - process_result_analyse(edit_, info_board_, result_analyse); - - if (CheckGpgError(error) == GPG_ERR_NO_ERROR) { - if (result_analyse.GetStatus() == -2) - import_unknown_key_from_keyserver(this, result_analyse); - - if (result_analyse.GetStatus() >= 0) - show_verify_details(this, info_board_, error, verify_result); - } - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - }; + if (data_obj == nullptr || !data_obj->Check<GpgVerifyResult>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + auto verify_result = ExtractParams<GpgVerifyResult>(data_obj, 0); - process_operation(this, _("Verifying"), verify_runner, result_callback, - data_object); + // analyse result + auto result_analyse = GpgVerifyResultAnalyse(err, verify_result); + result_analyse.Analyse(); + process_result_analyse(edit_, info_board_, result_analyse); + }); + }); } void MainWindow::slot_encrypt_sign() { @@ -381,15 +306,15 @@ void MainWindow::slot_encrypt_sign() { } } - auto signersPicker = new SignersPicker(this); + auto* signers_picker = new SignersPicker(this); QEventLoop loop; - connect(signersPicker, &SignersPicker::finished, &loop, &QEventLoop::quit); + connect(signers_picker, &SignersPicker::finished, &loop, &QEventLoop::quit); loop.exec(); // return when canceled - if (!signersPicker->GetStatus()) return; + if (!signers_picker->GetStatus()) return; - auto signer_key_ids = signersPicker->GetCheckedSigners(); + auto signer_key_ids = signers_picker->GetCheckedSigners(); auto signer_keys = GpgKeyGetter::GetInstance().GetKeys(signer_key_ids); for (const auto& key : *keys) { @@ -401,67 +326,46 @@ void MainWindow::slot_encrypt_sign() { } // data to transfer into task - auto data_object = TransferParams( - std::move(signer_keys), std::move(keys), - edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString()); - - auto encrypt_sign_runner = [](DataObjectPtr data_object) -> int { - // check the size of the data object - if (data_object == nullptr || - !data_object->Check<KeyListPtr, KeyListPtr, std::string>()) - throw std::runtime_error("data object doesn't pass checking"); - - auto signer_keys = ExtractParams<KeyListPtr>(data_object, 0); - auto keys = ExtractParams<KeyListPtr>(data_object, 1); - auto buffer = ExtractParams<std::string>(data_object, 2); - try { - GpgEncrResult encr_result = nullptr; - GpgSignResult sign_result = nullptr; - auto tmp = GpgFrontend::SecureCreateSharedObject<ByteArray>(); - GpgError error = GpgFrontend::GpgBasicOperator::GetInstance().EncryptSign( - std::move(keys), std::move(signer_keys), buffer, tmp, encr_result, - sign_result); - - data_object->Swap({error, encr_result, sign_result, tmp}); - - } catch (const std::runtime_error& e) { - return -1; - } - return 0; - }; - - auto result_callback = [this](int rtn, DataObjectPtr data_object) { - if (!rtn) { - if (data_object == nullptr || - !data_object - ->Check<GpgError, GpgEncrResult, GpgSignResult, ByteArrayPtr>()) - throw std::runtime_error("data object doesn't pass checking"); - auto error = ExtractParams<GpgError>(data_object, 0); - auto encrypt_result = ExtractParams<GpgEncrResult>(data_object, 1); - auto sign_result = ExtractParams<GpgSignResult>(data_object, 2); - auto tmp = ExtractParams<ByteArrayPtr>(data_object, 3); - - auto encrypt_result_analyse = GpgEncryptResultAnalyse( - error, GpgEncryptResult(encrypt_result.get())); - auto sign_result_analyse = - GpgSignResultAnalyse(error, std::move(sign_result)); - encrypt_result_analyse.Analyse(); - sign_result_analyse.Analyse(); - process_result_analyse(edit_, info_board_, encrypt_result_analyse, - sign_result_analyse); - if (CheckGpgError(error) == GPG_ERR_NO_ERROR) - edit_->SlotFillTextEditWithText(QString::fromStdString(*tmp)); - - info_board_->ResetOptionActionsMenu(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - }; + auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); + + CommonUtils::WaitForOpera( + this, _("Encrypting and Signing"), + [this, keys, signer_keys, buffer](const OperaWaitingHd& hd) { + GpgFrontend::GpgBasicOperator::GetInstance().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 (data_obj == nullptr || + !data_obj + ->Check<GpgEncryptResult, GpgSignResult, GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + 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(err, encrypt_result); + encrypt_result_analyse.Analyse(); - process_operation(this, _("Encrypting and Signing"), encrypt_sign_runner, - result_callback, data_object); + auto sign_result_analyse = GpgSignResultAnalyse(err, sign_result); + sign_result_analyse.Analyse(); + + // show analyse result + process_result_analyse(edit_, info_board_, encrypt_result_analyse, + sign_result_analyse); + + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + edit_->SlotFillTextEditWithText( + out_buffer.ConvertToQByteArray()); + } + }); + }); } void MainWindow::slot_decrypt_verify() { @@ -472,69 +376,45 @@ void MainWindow::slot_decrypt_verify() { } // data to transfer into task - auto data_object = TransferParams( - edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString()); - - auto decrypt_verify_runner = [](DataObjectPtr data_object) -> int { - // check the size of the data object - if (data_object == nullptr || !data_object->Check<std::string>()) - throw std::runtime_error("data object doesn't pass checking"); - - auto buffer = ExtractParams<std::string>(data_object, 0); - try { - GpgDecrResult decrypt_result = nullptr; - GpgVerifyResult verify_result = nullptr; - auto decrypted_buffer = - GpgFrontend::SecureCreateSharedObject<ByteArray>(); - GpgError error = GpgBasicOperator::GetInstance().DecryptVerify( - buffer, decrypted_buffer, decrypt_result, verify_result); - - data_object->Swap( - {error, decrypt_result, verify_result, decrypted_buffer}); - } catch (const std::runtime_error& e) { - SPDLOG_ERROR(e.what()); - return -1; - } - return 0; - }; - - auto result_callback = [this](int rtn, DataObjectPtr data_object) { - if (!rtn) { - if (data_object == nullptr || - !data_object->Check<GpgError, GpgDecrResult, GpgVerifyResult, - ByteArrayPtr>()) - throw std::runtime_error("data object doesn't pass checking"); - - auto error = ExtractParams<GpgError>(data_object, 0); - auto decrypt_result = ExtractParams<GpgDecrResult>(data_object, 1); - auto verify_result = ExtractParams<GpgVerifyResult>(data_object, 2); - auto decrypted = ExtractParams<ByteArrayPtr>(data_object, 3); - - auto decrypt_result_analyse = - GpgDecryptResultAnalyse(error, std::move(decrypt_result)); - auto verify_result_analyse = GpgVerifyResultAnalyse(error, verify_result); - decrypt_result_analyse.Analyse(); - verify_result_analyse.Analyse(); - process_result_analyse(edit_, info_board_, decrypt_result_analyse, - verify_result_analyse); - if (CheckGpgError(error) == GPG_ERR_NO_ERROR) - edit_->SlotFillTextEditWithText(QString::fromStdString(*decrypted)); - - if (verify_result_analyse.GetStatus() == -2) - import_unknown_key_from_keyserver(this, verify_result_analyse); - - if (verify_result_analyse.GetStatus() >= 0) - show_verify_details(this, info_board_, error, verify_result); - - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - }; + auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); - process_operation(this, _("Decrypting and Verifying"), decrypt_verify_runner, - result_callback, data_object); + CommonUtils::WaitForOpera( + this, _("Decrypting and Verifying"), + [this, buffer](const OperaWaitingHd& hd) { + GpgFrontend::GpgBasicOperator::GetInstance().DecryptVerify( + buffer, [this, hd](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + hd(); + + if (data_obj == nullptr || + !data_obj + ->Check<GpgDecryptResult, GpgVerifyResult, GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + 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(err, decrypt_result); + decrypt_result_analyse.Analyse(); + + auto verify_result_analyse = + GpgVerifyResultAnalyse(err, verify_result); + verify_result_analyse.Analyse(); + + // show analyse result + process_result_analyse(edit_, info_board_, decrypt_result_analyse, + verify_result_analyse); + + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + edit_->SlotFillTextEditWithText( + out_buffer.ConvertToQByteArray()); + } + }); + }); } void MainWindow::slot_find() { |