From 0266ff1bbce2feccec34ab789f9e378948d1b24d Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 11 Jan 2024 11:05:45 +0800 Subject: feat: improve api and ui of keys import and export --- .../import_export/ExportKeyPackageDialog.cpp | 71 ++++++++++++++-------- 1 file changed, 44 insertions(+), 27 deletions(-) (limited to 'src/ui/dialog/import_export/ExportKeyPackageDialog.cpp') diff --git a/src/ui/dialog/import_export/ExportKeyPackageDialog.cpp b/src/ui/dialog/import_export/ExportKeyPackageDialog.cpp index 4b616741..66ecb3c9 100644 --- a/src/ui/dialog/import_export/ExportKeyPackageDialog.cpp +++ b/src/ui/dialog/import_export/ExportKeyPackageDialog.cpp @@ -33,6 +33,7 @@ #include "core/GpgModel.h" #include "core/function/KeyPackageOperator.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "ui/UserInterfaceUtils.h" #include "ui_ExportKeyPackageDialog.h" GpgFrontend::UI::ExportKeyPackageDialog::ExportKeyPackageDialog( @@ -98,36 +99,52 @@ GpgFrontend::UI::ExportKeyPackageDialog::ExportKeyPackageDialog( } // get suitable key ids - auto key_id_exported = std::make_unique(); auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids_); - for (const auto& key : *keys) { - if (ui_->noPublicKeyCheckBox->isChecked() && !key.IsPrivateKey()) - continue; - key_id_exported->push_back(key.GetId()); + auto keys_new_end = + std::remove_if(keys->begin(), keys->end(), [this](const auto& key) { + return ui_->noPublicKeyCheckBox->isChecked() && !key.IsPrivateKey(); + }); + keys->erase(keys_new_end, keys->end()); + + if (keys->empty()) { + QMessageBox::critical(this, _("Error"), + _("No key is suitable to export.")); + return; } - if (KeyPackageOperator::GenerateKeyPackage( - ui_->outputPathLabel->text().toStdString(), - ui_->nameValueLabel->text().toStdString(), key_id_exported, - passphrase_, ui_->includeSecretKeyCheckBox->isChecked())) { - QMessageBox::information( - this, _("Success"), - QString( - _("The Key Package has been successfully generated and has been " - "protected by encryption algorithms(AES-256-ECB). You can " - "safely transfer your Key Package.")) + - "

" + "" + - _("But the key file cannot be leaked under any " - "circumstances. Please delete the Key Package and key file as " - "soon " - "as possible after completing the transfer operation.") + - ""); - accept(); - } else { - QMessageBox::critical( - this, _("Error"), - _("An error occurred while exporting the key package.")); - } + CommonUtils::WaitForOpera( + this, _("Generating"), [this, keys](const OperaWaitingHd& op_hd) { + KeyPackageOperator::GenerateKeyPackage( + ui_->outputPathLabel->text().toStdString(), + ui_->nameValueLabel->text().toStdString(), *keys, passphrase_, + ui_->includeSecretKeyCheckBox->isChecked(), + [=](GFError err, const DataObjectPtr&) { + // stop waiting + op_hd(); + + if (err >= 0) { + QMessageBox::information( + this, _("Success"), + QString( + _("The Key Package has been successfully generated " + "and has been protected by encryption " + "algorithms(AES-256-ECB). You can safely transfer " + "your Key Package.")) + + "

" + "" + + _("But the key file cannot be leaked under any " + "circumstances. Please delete the Key Package and " + "key file as soon as possible after completing the " + "transfer " + "operation.") + + ""); + accept(); + } else { + QMessageBox::critical( + this, _("Error"), + _("An error occurred while exporting the key package.")); + } + }); + }); }); connect(ui_->button_box_, &QDialogButtonBox::rejected, this, -- cgit v1.2.3