diff options
Diffstat (limited to '')
27 files changed, 653 insertions, 573 deletions
diff --git a/src/core/function/KeyPackageOperator.cpp b/src/core/function/KeyPackageOperator.cpp index cd8482f2..5b591ff3 100644 --- a/src/core/function/KeyPackageOperator.cpp +++ b/src/core/function/KeyPackageOperator.cpp @@ -56,7 +56,8 @@ void KeyPackageOperator::GenerateKeyPackage( GF_CORE_LOG_DEBUG("generating key package: {}", key_package_name); GpgKeyImportExporter::GetInstance().ExportKeys( - keys, secret, true, [=](GpgError err, const DataObjectPtr& data_obj) { + keys, secret, true, false, false, + [=](GpgError err, const DataObjectPtr& data_obj) { if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { GF_LOG_ERROR("export keys error, reason: {}", DescribeGpgErrCode(err).second); @@ -86,8 +87,8 @@ void KeyPackageOperator::GenerateKeyPackage( auto KeyPackageOperator::ImportKeyPackage( const std::filesystem::path& key_package_path, - const std::filesystem::path& phrase_path, GpgImportInformation& import_info) - -> bool { + const std::filesystem::path& phrase_path) + -> std::tuple<bool, std::shared_ptr<GpgImportInformation>> { GF_CORE_LOG_DEBUG("importing key package: {}", key_package_path.u8string()); std::string encrypted_data; @@ -96,7 +97,7 @@ auto KeyPackageOperator::ImportKeyPackage( if (encrypted_data.empty()) { GF_CORE_LOG_ERROR("failed to read key package: {}", key_package_path.u8string()); - return false; + return {false, nullptr}; }; std::string passphrase; @@ -104,7 +105,7 @@ auto KeyPackageOperator::ImportKeyPackage( GF_CORE_LOG_DEBUG("passphrase: {} bytes", passphrase.size()); if (passphrase.size() != 256) { GF_CORE_LOG_ERROR("failed to read passphrase: {}", phrase_path.u8string()); - return false; + return {false, nullptr}; } auto hash_key = QCryptographicHash::hash( @@ -117,16 +118,16 @@ auto KeyPackageOperator::ImportKeyPackage( auto decoded = encryption.removePadding(encryption.decode(encoded, hash_key)); auto key_data = QByteArray::fromBase64(decoded); - GF_CORE_LOG_DEBUG("key data size: {}", key_data.size()); + GF_CORE_LOG_DEBUG("import key package, read key data size: {}", + key_data.size()); if (!key_data.startsWith(PGP_PUBLIC_KEY_BEGIN) && !key_data.startsWith(PGP_PRIVATE_KEY_BEGIN)) { - return false; + return {false, nullptr}; } - auto key_data_ptr = std::make_unique<ByteArray>(key_data.toStdString()); - import_info = - GpgKeyImportExporter::GetInstance().ImportKey(std::move(key_data_ptr)); - return true; + auto import_info = + GpgKeyImportExporter::GetInstance().ImportKey(GFBuffer(key_data)); + return {true, import_info}; } auto KeyPackageOperator::GenerateKeyPackageName() -> std::string { diff --git a/src/core/function/KeyPackageOperator.h b/src/core/function/KeyPackageOperator.h index 84b30adc..0bc51eff 100644 --- a/src/core/function/KeyPackageOperator.h +++ b/src/core/function/KeyPackageOperator.h @@ -82,8 +82,8 @@ class GPGFRONTEND_CORE_EXPORT KeyPackageOperator { * @return false if key package was not imported */ static auto ImportKeyPackage(const std::filesystem::path &key_package_path, - const std::filesystem::path &phrase_path, - GpgImportInformation &import_info) -> bool; + const std::filesystem::path &phrase_path) + -> std::tuple<bool, std::shared_ptr<GpgImportInformation>>; private: /** diff --git a/src/core/function/gpg/GpgKeyImportExporter.cpp b/src/core/function/gpg/GpgKeyImportExporter.cpp index 9d474109..09b51359 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.cpp +++ b/src/core/function/gpg/GpgKeyImportExporter.cpp @@ -29,6 +29,7 @@ #include "GpgKeyImportExporter.h" #include "core/GpgModel.h" +#include "core/model/GpgImportInformation.h" #include "core/utils/AsyncUtils.h" #include "core/utils/GpgUtils.h" @@ -43,27 +44,27 @@ GpgKeyImportExporter::GpgKeyImportExporter(int channel) * @param inBuffer input byte array * @return Import information */ -auto GpgKeyImportExporter::ImportKey(StdBypeArrayPtr in_buffer) - -> GpgImportInformation { - if (in_buffer->empty()) return {}; +auto GpgKeyImportExporter::ImportKey(const GFBuffer& in_buffer) + -> std::shared_ptr<GpgImportInformation> { + if (in_buffer.Empty()) return {}; - GpgData data_in(in_buffer->data(), in_buffer->size()); + GpgData data_in(in_buffer); auto err = CheckGpgError(gpgme_op_import(ctx_.DefaultContext(), data_in)); if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) return {}; gpgme_import_result_t result; result = gpgme_op_import_result(ctx_.DefaultContext()); gpgme_import_status_t status = result->imports; - auto import_info = std::make_unique<GpgImportInformation>(result); + auto import_info = SecureCreateSharedObject<GpgImportInformation>(result); while (status != nullptr) { - GpgImportedKey key; + GpgImportInformation::GpgImportedKey key; key.import_status = static_cast<int>(status->status); key.fpr = status->fpr; import_info->imported_keys.emplace_back(key); status = status->next; } - return *import_info; + return import_info; } /** @@ -73,13 +74,14 @@ auto GpgKeyImportExporter::ImportKey(StdBypeArrayPtr in_buffer) * @return if success */ auto GpgKeyImportExporter::ExportKey(const GpgKey& key, bool secret, bool ascii, - bool shortest) const + bool shortest, bool ssh_mode) const -> std::tuple<GpgError, GFBuffer> { if (!key.IsGood()) return {GPG_ERR_CANCELED, {}}; int mode = 0; if (secret) mode |= GPGME_EXPORT_MODE_SECRET; if (shortest) mode |= GPGME_EXPORT_MODE_MINIMAL; + if (ssh_mode) mode |= GPGME_EXPORT_MODE_SSH; std::vector<gpgme_key_t> keys_array; @@ -105,7 +107,7 @@ auto GpgKeyImportExporter::ExportKey(const GpgKey& key, bool secret, bool ascii, * @return if success */ void GpgKeyImportExporter::ExportKeys(const KeyArgsList& keys, bool secret, - bool ascii, + bool ascii, bool shortest, bool ssh_mode, const GpgOperationCallback& cb) const { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -113,6 +115,8 @@ void GpgKeyImportExporter::ExportKeys(const KeyArgsList& keys, bool secret, int mode = 0; if (secret) mode |= GPGME_EXPORT_MODE_SECRET; + if (shortest) mode |= GPGME_EXPORT_MODE_MINIMAL; + if (ssh_mode) mode |= GPGME_EXPORT_MODE_SSH; std::vector<gpgme_key_t> keys_array(keys.begin(), keys.end()); @@ -134,39 +138,4 @@ void GpgKeyImportExporter::ExportKeys(const KeyArgsList& keys, bool secret, cb, "gpgme_op_export_keys", "2.1.0"); } -auto GpgKeyImportExporter::ExportKeyOpenSSH(const GpgKey& key, - ByteArrayPtr& out_buffer) const - -> bool { - GpgData data_out; - auto err = gpgme_op_export(ctx_.DefaultContext(), key.GetId().c_str(), - GPGME_EXPORT_MODE_SSH, data_out); - - GF_CORE_LOG_DEBUG("read_bytes: {}", gpgme_data_seek(data_out, 0, SEEK_END)); - - auto temp_out_buffer = data_out.Read2Buffer(); - std::swap(out_buffer, temp_out_buffer); - return CheckGpgError(err) == GPG_ERR_NO_ERROR; -} - -GpgImportInformation::GpgImportInformation() = default; - -GpgImportInformation::GpgImportInformation(gpgme_import_result_t result) { - if (result->unchanged != 0) unchanged = result->unchanged; - if (result->considered != 0) considered = result->considered; - if (result->no_user_id != 0) no_user_id = result->no_user_id; - if (result->imported != 0) imported = result->imported; - if (result->imported_rsa != 0) imported_rsa = result->imported_rsa; - if (result->unchanged != 0) unchanged = result->unchanged; - if (result->new_user_ids != 0) new_user_ids = result->new_user_ids; - if (result->new_sub_keys != 0) new_sub_keys = result->new_sub_keys; - if (result->new_signatures != 0) new_signatures = result->new_signatures; - if (result->new_revocations != 0) new_revocations = result->new_revocations; - if (result->secret_read != 0) secret_read = result->secret_read; - if (result->secret_imported != 0) secret_imported = result->secret_imported; - if (result->secret_unchanged != 0) { - secret_unchanged = result->secret_unchanged; - } - if (result->not_imported != 0) not_imported = result->not_imported; -} - } // namespace GpgFrontend diff --git a/src/core/function/gpg/GpgKeyImportExporter.h b/src/core/function/gpg/GpgKeyImportExporter.h index 707bd895..80764df8 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.h +++ b/src/core/function/gpg/GpgKeyImportExporter.h @@ -36,49 +36,7 @@ namespace GpgFrontend { -/** - * @brief - * - */ -class GpgImportedKey { - public: - std::string fpr; ///< - int import_status; ///< -}; - -using GpgImportedKeyList = std::list<GpgImportedKey>; ///< - -/** - * @brief - * - */ -class GPGFRONTEND_CORE_EXPORT GpgImportInformation { - public: - GpgImportInformation(); - - /** - * @brief Construct a new Gpg Import Information object - * - * @param result - */ - explicit GpgImportInformation(gpgme_import_result_t result); - - int considered = 0; ///< - int no_user_id = 0; ///< - int imported = 0; ///< - int imported_rsa = 0; ///< - int unchanged = 0; ///< - int new_user_ids = 0; ///< - int new_sub_keys = 0; ///< - int new_signatures = 0; ///< - int new_revocations = 0; ///< - int secret_read = 0; ///< - int secret_imported = 0; ///< - int secret_unchanged = 0; ///< - int not_imported = 0; ///< - - GpgImportedKeyList imported_keys; ///< -}; +class GpgImportInformation; /** * @brief @@ -101,7 +59,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyImportExporter * @param inBuffer * @return GpgImportInformation */ - auto ImportKey(StdBypeArrayPtr) -> GpgImportInformation; + auto ImportKey(const GFBuffer&) -> std::shared_ptr<GpgImportInformation>; /** * @brief @@ -112,7 +70,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyImportExporter * @return std::tuple<GpgError, GFBuffer> */ [[nodiscard]] auto ExportKey(const GpgKey& key, bool secret, bool ascii, - bool shortest) const + bool shortest, bool ssh_mode = false) const -> std::tuple<GpgError, GFBuffer>; /** @@ -125,19 +83,9 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyImportExporter * @return false */ void ExportKeys(const KeyArgsList& keys, bool secret, bool ascii, + bool shortest, bool ssh_mode, const GpgOperationCallback& cb) const; - /** - * @brief - * - * @param key - * @param out_buffer - * @return true - * @return false - */ - auto ExportKeyOpenSSH(const GpgKey& key, ByteArrayPtr& out_buffer) const - -> bool; - private: GpgContext& ctx_; }; diff --git a/src/core/model/GpgImportInformation.cpp b/src/core/model/GpgImportInformation.cpp new file mode 100644 index 00000000..cda146de --- /dev/null +++ b/src/core/model/GpgImportInformation.cpp @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GpgImportInformation.h" + +namespace GpgFrontend { + +GpgImportInformation::GpgImportInformation() = default; + +GpgImportInformation::GpgImportInformation(gpgme_import_result_t result) { + if (result->unchanged != 0) unchanged = result->unchanged; + if (result->considered != 0) considered = result->considered; + if (result->no_user_id != 0) no_user_id = result->no_user_id; + if (result->imported != 0) imported = result->imported; + if (result->imported_rsa != 0) imported_rsa = result->imported_rsa; + if (result->unchanged != 0) unchanged = result->unchanged; + if (result->new_user_ids != 0) new_user_ids = result->new_user_ids; + if (result->new_sub_keys != 0) new_sub_keys = result->new_sub_keys; + if (result->new_signatures != 0) new_signatures = result->new_signatures; + if (result->new_revocations != 0) new_revocations = result->new_revocations; + if (result->secret_read != 0) secret_read = result->secret_read; + if (result->secret_imported != 0) secret_imported = result->secret_imported; + if (result->secret_unchanged != 0) { + secret_unchanged = result->secret_unchanged; + } + if (result->not_imported != 0) not_imported = result->not_imported; +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgImportInformation.h b/src/core/model/GpgImportInformation.h new file mode 100644 index 00000000..87d23bf9 --- /dev/null +++ b/src/core/model/GpgImportInformation.h @@ -0,0 +1,80 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +/** + * @brief + * + */ +class GPGFRONTEND_CORE_EXPORT GpgImportInformation { + public: + /** + * @brief + * + */ + class GpgImportedKey { + public: + std::string fpr; ///< + int import_status; ///< + }; + + using GpgImportedKeyList = std::list<GpgImportedKey>; ///< + + /** + * @brief Construct a new Gpg Import Information object + * + */ + GpgImportInformation(); + + /** + * @brief Construct a new Gpg Import Information object + * + * @param result + */ + explicit GpgImportInformation(gpgme_import_result_t result); + + int considered = 0; ///< + int no_user_id = 0; ///< + int imported = 0; ///< + int imported_rsa = 0; ///< + int unchanged = 0; ///< + int new_user_ids = 0; ///< + int new_sub_keys = 0; ///< + int new_signatures = 0; ///< + int new_revocations = 0; ///< + int secret_read = 0; ///< + int secret_imported = 0; ///< + int secret_unchanged = 0; ///< + int not_imported = 0; ///< + + GpgImportedKeyList imported_keys; ///< +}; +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index bed7fcf2..c7c198b3 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -41,6 +41,7 @@ #include "core/function/CacheManager.h" #include "core/function/CoreSignalStation.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/model/GpgImportInformation.h" #include "core/module/ModuleManager.h" #include "core/thread/Task.h" #include "core/thread/TaskRunner.h" @@ -79,7 +80,7 @@ void import_unknown_key_from_keyserver( "import the public key from Keyserver now?"), QMessageBox::Yes | QMessageBox::No); if (reply == QMessageBox::Yes) { - auto dialog = KeyServerImportDialog(true, parent); + auto dialog = KeyServerImportDialog(parent); auto key_ids = std::make_unique<KeyIdArgsList>(); auto *signature = verify_res.GetSignatures(); while (signature != nullptr) { @@ -228,7 +229,7 @@ void CommonUtils::WaitForOpera(QWidget *parent, const std::string &waiting_dialog_title, const OperaWaitingCb &opera) { QEventLoop looper; - auto *dialog = + QPointer<WaitingDialog> const dialog = new WaitingDialog(QString::fromStdString(waiting_dialog_title), parent); connect(dialog, &QDialog::finished, &looper, &QEventLoop::quit); connect(dialog, &QDialog::finished, dialog, &QDialog::deleteLater); @@ -236,13 +237,26 @@ void CommonUtils::WaitForOpera(QWidget *parent, QTimer::singleShot(64, parent, [=]() { opera([dialog]() { - GF_UI_LOG_DEBUG("called operating waiting cb, dialog: {}", - static_cast<void *>(dialog)); - dialog->close(); - dialog->accept(); + if (dialog != nullptr) { + GF_UI_LOG_DEBUG("called operating waiting cb, dialog: {}", + static_cast<void *>(dialog)); + dialog->close(); + dialog->accept(); + } }); }); + // handling timeout, default 30s + QTimer::singleShot(30000, parent, [parent, dialog]() { + if (dialog != nullptr) { + dialog->close(); + dialog->reject(); + + QMessageBox::critical(parent, _("Timeout"), + _("Operation has timeout, aborted...")); + } + }); + looper.exec(); } @@ -274,14 +288,15 @@ void CommonUtils::RaiseFailureMessageBox(QWidget *parent, GpgError err) { void CommonUtils::SlotImportKeys(QWidget *parent, const std::string &in_buffer) { - GpgImportInformation result = GpgKeyImportExporter::GetInstance().ImportKey( - std::make_unique<ByteArray>(in_buffer)); + auto info = + GpgKeyImportExporter::GetInstance().ImportKey(GFBuffer(in_buffer)); emit SignalKeyStatusUpdated(); - new KeyImportDetailDialog(result, false, parent); + + (new KeyImportDetailDialog(info, parent))->exec(); } void CommonUtils::SlotImportKeyFromFile(QWidget *parent) { - QString file_name = QFileDialog::getOpenFileName( + auto file_name = QFileDialog::getOpenFileName( this, _("Open Key"), QString(), QString(_("Key Files")) + " (*.asc *.txt);;" + _("Keyring files") + " (*.gpg);;All Files (*)"); @@ -297,7 +312,7 @@ void CommonUtils::SlotImportKeyFromFile(QWidget *parent) { } void CommonUtils::SlotImportKeyFromKeyServer(QWidget *parent) { - auto *dialog = new KeyServerImportDialog(false, parent); + auto *dialog = new KeyServerImportDialog(parent); dialog->show(); } @@ -444,11 +459,6 @@ void CommonUtils::SlotImportKeyFromKeyServer( connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(); - // Get Data - auto key_data = reply->readAll(); - auto key_data_ptr = - std::make_unique<ByteArray>(key_data.data(), key_data.size()); - // Detect status std::string status; auto error = reply->error(); @@ -471,11 +481,10 @@ void CommonUtils::SlotImportKeyFromKeyServer( reply->deleteLater(); // Try importing - GpgImportInformation result = - GpgKeyImportExporter::GetInstance().ImportKey( - std::move(key_data_ptr)); + auto result = GpgKeyImportExporter::GetInstance().ImportKey( + GFBuffer(reply->readAll())); - if (result.imported == 1) { + if (result->imported == 1) { status = _("The key has been updated"); } else { status = _("No need to update the key"); 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<KeyIdArgsList>(); 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.")) + - "<br /><br />" + "<b>" + - _("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.") + - "</b>"); - 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.")) + + "<br /><br />" + "<b>" + + _("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.") + + "</b>"); + accept(); + } else { + QMessageBox::critical( + this, _("Error"), + _("An error occurred while exporting the key package.")); + } + }); + }); }); connect(ui_->button_box_, &QDialogButtonBox::rejected, this, diff --git a/src/ui/dialog/import_export/KeyImportDetailDialog.cpp b/src/ui/dialog/import_export/KeyImportDetailDialog.cpp index 6a66d8ef..bf9b325f 100644 --- a/src/ui/dialog/import_export/KeyImportDetailDialog.cpp +++ b/src/ui/dialog/import_export/KeyImportDetailDialog.cpp @@ -30,20 +30,17 @@ #include "core/GpgModel.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/model/GpgImportInformation.h" namespace GpgFrontend::UI { -KeyImportDetailDialog::KeyImportDetailDialog(GpgImportInformation result, - bool automatic, QWidget* parent) +KeyImportDetailDialog::KeyImportDetailDialog( + std::shared_ptr<GpgImportInformation> result, QWidget* parent) : GeneralDialog(typeid(KeyImportDetailDialog).name(), parent), m_result_(std::move(result)) { // If no key for import found, just show a message - if (m_result_.considered == 0) { - if (automatic) - QMessageBox::information(parent, _("Key Update Details"), - _("No keys found")); - else - QMessageBox::information(parent, _("Key Import Details"), - _("No keys found to import")); + if (m_result_->considered == 0) { + QMessageBox::information(parent, _("Key Import Details"), + _("No keys found to import")); emit finished(0); this->close(); this->deleteLater(); @@ -58,18 +55,13 @@ KeyImportDetailDialog::KeyImportDetailDialog(GpgImportInformation result, mv_box->addWidget(button_box_); this->setLayout(mv_box); - if (automatic) - this->setWindowTitle(_("Key Update Details")); - else - this->setWindowTitle(_("Key Import Details")); - - auto pos = QPoint(100, 100); - if (parent) pos += parent->pos(); - this->move(pos); + this->setWindowTitle(_("Key Import Details")); this->setMinimumSize(QSize(600, 300)); this->adjustSize(); + movePosition2CenterOfParent(); + this->setModal(true); this->show(); } @@ -78,53 +70,53 @@ KeyImportDetailDialog::KeyImportDetailDialog(GpgImportInformation result, void KeyImportDetailDialog::create_general_info_box() { // GridBox for general import information general_info_box_ = new QGroupBox(_("General key info")); - auto* generalInfoBoxLayout = new QGridLayout(general_info_box_); + auto* general_info_box_layout = new QGridLayout(general_info_box_); - generalInfoBoxLayout->addWidget(new QLabel(QString(_("Considered")) + ": "), - 1, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.considered)), 1, 1); + general_info_box_layout->addWidget( + new QLabel(QString(_("Considered")) + ": "), 1, 0); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->considered)), 1, 1); int row = 2; - if (m_result_.unchanged != 0) { - generalInfoBoxLayout->addWidget( + if (m_result_->unchanged != 0) { + general_info_box_layout->addWidget( new QLabel(QString(_("Public unchanged")) + ": "), row, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.unchanged)), row, 1); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->unchanged)), row, 1); row++; } - if (m_result_.imported != 0) { - generalInfoBoxLayout->addWidget(new QLabel(QString(_("Imported")) + ": "), - row, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.imported)), row, 1); + if (m_result_->imported != 0) { + general_info_box_layout->addWidget( + new QLabel(QString(_("Imported")) + ": "), row, 0); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->imported)), row, 1); row++; } - if (m_result_.not_imported != 0) { - generalInfoBoxLayout->addWidget( + if (m_result_->not_imported != 0) { + general_info_box_layout->addWidget( new QLabel(QString(_("Not Imported")) + ": "), row, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.not_imported)), row, 1); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->not_imported)), row, 1); row++; } - if (m_result_.secret_read != 0) { - generalInfoBoxLayout->addWidget( + if (m_result_->secret_read != 0) { + general_info_box_layout->addWidget( new QLabel(QString(_("Private Read")) + ": "), row, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.secret_read)), row, 1); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->secret_read)), row, 1); row++; } - if (m_result_.secret_imported != 0) { - generalInfoBoxLayout->addWidget( + if (m_result_->secret_imported != 0) { + general_info_box_layout->addWidget( new QLabel(QString(_("Private Imported")) + ": "), row, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.secret_imported)), row, 1); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->secret_imported)), row, 1); row++; } - if (m_result_.secret_unchanged != 0) { - generalInfoBoxLayout->addWidget( + if (m_result_->secret_unchanged != 0) { + general_info_box_layout->addWidget( new QLabel(QString(_("Private Unchanged")) + ": "), row, 0); - generalInfoBoxLayout->addWidget( - new QLabel(QString::number(m_result_.secret_unchanged)), row, 1); + general_info_box_layout->addWidget( + new QLabel(QString::number(m_result_->secret_unchanged)), row, 1); } } @@ -136,15 +128,15 @@ void KeyImportDetailDialog::create_keys_table() { // Nothing is selectable keys_table_->setSelectionMode(QAbstractItemView::NoSelection); - QStringList headerLabels; - headerLabels << _("Name") << _("Email") << _("Status") << _("Fingerprint"); + QStringList header_labels; + header_labels << _("Name") << _("Email") << _("Status") << _("Fingerprint"); keys_table_->verticalHeader()->hide(); - keys_table_->setHorizontalHeaderLabels(headerLabels); + keys_table_->setHorizontalHeaderLabels(header_labels); int row = 0; - for (const auto& imp_key : m_result_.imported_keys) { + for (const auto& imp_key : m_result_->imported_keys) { keys_table_->setRowCount(row + 1); - GpgKey key = GpgKeyGetter::GetInstance().GetKey(imp_key.fpr); + auto key = GpgKeyGetter::GetInstance().GetKey(imp_key.fpr); if (!key.IsGood()) continue; keys_table_->setItem( row, 0, new QTableWidgetItem(QString::fromStdString(key.GetName()))); diff --git a/src/ui/dialog/import_export/KeyImportDetailDialog.h b/src/ui/dialog/import_export/KeyImportDetailDialog.h index fe4988c0..db355570 100644 --- a/src/ui/dialog/import_export/KeyImportDetailDialog.h +++ b/src/ui/dialog/import_export/KeyImportDetailDialog.h @@ -28,11 +28,13 @@ #pragma once -#include "core/function/gpg/GpgContext.h" #include "core/function/gpg/GpgKeyImportExporter.h" -#include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" +namespace GpgFrontend { +class GpgImportInformation; +} + namespace GpgFrontend::UI { /** @@ -50,8 +52,8 @@ class KeyImportDetailDialog : public GeneralDialog { * @param automatic * @param parent */ - KeyImportDetailDialog(GpgImportInformation result, bool automatic, - QWidget* parent = nullptr); + explicit KeyImportDetailDialog(std::shared_ptr<GpgImportInformation> result, + QWidget* parent = nullptr); private: /** @@ -84,6 +86,7 @@ class KeyImportDetailDialog : public GeneralDialog { QGroupBox* general_info_box_{}; ///< QGroupBox* key_info_box_{}; ///< QDialogButtonBox* button_box_{}; ///< - GpgImportInformation m_result_; ///< + + std::shared_ptr<GpgImportInformation> m_result_; ///< }; } // namespace GpgFrontend::UI diff --git a/src/ui/dialog/import_export/KeyServerImportDialog.cpp b/src/ui/dialog/import_export/KeyServerImportDialog.cpp index 3a23b0df..53c9e5b6 100644 --- a/src/ui/dialog/import_export/KeyServerImportDialog.cpp +++ b/src/ui/dialog/import_export/KeyServerImportDialog.cpp @@ -42,131 +42,86 @@ namespace GpgFrontend::UI { -KeyServerImportDialog::KeyServerImportDialog(bool automatic, QWidget* parent) - : GeneralDialog("key_server_import_dialog", parent), - m_automatic_(automatic) { - // Layout for messagebox - auto* message_layout = new QHBoxLayout(); - - bool forbid_all_gnupg_connection = +KeyServerImportDialog::KeyServerImportDialog(QWidget* parent) + : GeneralDialog("key_server_import_dialog", parent) { + auto forbid_all_gnupg_connection = GlobalSettingStation::GetInstance().LookupSettings( "network.forbid_all_gnupg_connection", false); - if (forbid_all_gnupg_connection) { QMessageBox::critical(this, "Forbidden", "GnuPG is in offline mode now."); this->close(); this->deleteLater(); } - if (automatic) { - setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); - } else { - // Buttons - - close_button_ = new QPushButton(_("Close")); - connect(close_button_, &QPushButton::clicked, this, - &KeyServerImportDialog::close); - import_button_ = new QPushButton(_("Import ALL")); - connect(import_button_, &QPushButton::clicked, this, - &KeyServerImportDialog::slot_import); - import_button_->setDisabled(true); - search_button_ = new QPushButton(_("Search")); - connect(search_button_, &QPushButton::clicked, this, - &KeyServerImportDialog::slot_search); - - // Line edits for search string - search_label_ = new QLabel(QString(_("Search String")) + _(": ")); - search_line_edit_ = new QLineEdit(); - - // combobox for keyserver list - key_server_label_ = new QLabel(QString(_("Key Server")) + _(": ")); - key_server_combo_box_ = create_comboBox(); - - // table containing the keys found - create_keys_table(); - message_ = new QLabel(); - message_->setFixedHeight(24); - icon_ = new QLabel(); - icon_->setFixedHeight(24); - - message_layout->addWidget(icon_); - message_layout->addWidget(message_); - message_layout->addStretch(); - } + // Buttons + close_button_ = new QPushButton(_("Close")); + connect(close_button_, &QPushButton::clicked, this, + &KeyServerImportDialog::close); + import_button_ = new QPushButton(_("Import ALL")); + connect(import_button_, &QPushButton::clicked, this, + &KeyServerImportDialog::slot_import); + import_button_->setDisabled(true); + search_button_ = new QPushButton(_("Search")); + connect(search_button_, &QPushButton::clicked, this, + &KeyServerImportDialog::slot_search); + + // Line edits for search string + search_label_ = new QLabel(QString(_("Search String")) + _(": ")); + search_line_edit_ = new QLineEdit(); + + // combobox for keyserver list + key_server_label_ = new QLabel(QString(_("Key Server")) + _(": ")); + key_server_combo_box_ = create_combo_box(); + + // table containing the keys found + create_keys_table(); + message_ = new QLabel(); + message_->setFixedHeight(24); + icon_ = new QLabel(); + icon_->setFixedHeight(24); + + // Layout for messagebox + message_layout_ = new QHBoxLayout(); + message_layout_->addWidget(icon_); + message_layout_->addWidget(message_); + message_layout_->addStretch(); // Network Waiting waiting_bar_ = new QProgressBar(); waiting_bar_->setVisible(false); waiting_bar_->setRange(0, 0); waiting_bar_->setFixedWidth(200); - message_layout->addWidget(waiting_bar_); - auto* main_layout = new QGridLayout; + auto* main_layout = new QGridLayout(); - // 自动化调用界面布局 - if (automatic) { - main_layout->addLayout(message_layout, 0, 0, 1, 3); - } else { - main_layout->addWidget(search_label_, 1, 0); - main_layout->addWidget(search_line_edit_, 1, 1); - main_layout->addWidget(search_button_, 1, 2); - main_layout->addWidget(key_server_label_, 2, 0); - main_layout->addWidget(key_server_combo_box_, 2, 1); - main_layout->addWidget(keys_table_, 3, 0, 1, 3); - main_layout->addLayout(message_layout, 4, 0, 1, 3); - - // Layout for import and close button - auto* buttons_layout = new QHBoxLayout; - buttons_layout->addStretch(); - buttons_layout->addWidget(import_button_); - buttons_layout->addWidget(close_button_); - main_layout->addLayout(buttons_layout, 6, 0, 1, 3); - } + main_layout->addWidget(search_label_, 1, 0); + main_layout->addWidget(search_line_edit_, 1, 1); + main_layout->addWidget(search_button_, 1, 2); + main_layout->addWidget(key_server_label_, 2, 0); + main_layout->addWidget(key_server_combo_box_, 2, 1); + main_layout->addWidget(keys_table_, 3, 0, 1, 3); + main_layout->addWidget(waiting_bar_, 4, 0, 1, 3); + main_layout->addLayout(message_layout_, 5, 0, 1, 3); - this->setLayout(main_layout); - if (automatic) { - this->setWindowTitle(_("Update Keys from Keyserver")); - } else { - this->setWindowTitle(_("Import Keys from Keyserver")); - } - - if (automatic) { - this->setFixedSize(240, 42); - } + // Layout for import and close button + auto* buttons_layout = new QHBoxLayout(); + buttons_layout->addStretch(); + buttons_layout->addWidget(import_button_); + buttons_layout->addWidget(close_button_); + main_layout->addLayout(buttons_layout, 6, 0, 1, 3); + this->setLayout(main_layout); + this->setWindowTitle(_("Import Keys from Keyserver")); this->setModal(true); + movePosition2CenterOfParent(); + connect(this, &KeyServerImportDialog::SignalKeyImported, UISignalStation::GetInstance(), &UISignalStation::SignalKeyDatabaseRefresh); } -KeyServerImportDialog::KeyServerImportDialog(QWidget* parent) - : GeneralDialog("key_server_import_dialog", parent), m_automatic_(true) { - setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); - - // Network Waiting - waiting_bar_ = new QProgressBar(); - waiting_bar_->setVisible(false); - waiting_bar_->setRange(0, 0); - waiting_bar_->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); - waiting_bar_->setTextVisible(false); - - // Layout for messagebox - auto* layout = new QHBoxLayout(); - layout->setContentsMargins(0, 0, 0, 0); - layout->setSpacing(0); - layout->addWidget(waiting_bar_); - - key_server_combo_box_ = create_comboBox(); - - this->setLayout(layout); - this->setWindowTitle(_("Update Keys from Keyserver")); - this->setFixedSize(240, 42); - this->setModal(true); -} - -auto KeyServerImportDialog::create_comboBox() -> QComboBox* { +auto KeyServerImportDialog::create_combo_box() -> QComboBox* { auto* combo_box = new QComboBox; combo_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); @@ -220,8 +175,6 @@ void KeyServerImportDialog::create_keys_table() { } void KeyServerImportDialog::set_message(const QString& text, bool error) { - if (m_automatic_) return; - message_->setText(text); if (error) { icon_->setPixmap( @@ -436,8 +389,9 @@ void KeyServerImportDialog::slot_import() { key_ids.push_back(keyid.toStdString()); } } - if (!key_ids.empty()) + if (!key_ids.empty()) { SlotImport(key_ids, key_server_combo_box_->currentText().toStdString()); + } } void KeyServerImportDialog::SlotImport(const KeyIdArgsListPtr& keys) { @@ -487,86 +441,35 @@ void KeyServerImportDialog::SlotImport(std::vector<std::string> key_ids, connect(task, &KeyServerImportTask::SignalKeyServerImportResult, this, &KeyServerImportDialog::slot_import_finished); + set_loading(true); Thread::TaskRunnerGetter::GetInstance() .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Network) ->PostTask(task); } void KeyServerImportDialog::slot_import_finished( - QNetworkReply::NetworkError error, QByteArray buffer) { - if (error != QNetworkReply::NoError) { + bool success, QString err_msg, QByteArray buffer, + std::shared_ptr<GpgImportInformation> info) { + set_loading(false); + + if (!success) { GF_UI_LOG_ERROR("Error From Reply", buffer.toStdString()); - if (!m_automatic_) { - switch (error) { - case QNetworkReply::ContentNotFoundError: - set_message(_("Key Not Found"), true); - break; - case QNetworkReply::TimeoutError: - set_message(_("Timeout"), true); - break; - case QNetworkReply::HostNotFoundError: - set_message(_("Key Server Not Found"), true); - break; - default: - set_message(_("Connection Error"), true); - } - } else { - switch (error) { - case QNetworkReply::ContentNotFoundError: - QMessageBox::critical(nullptr, _("Key Not Found"), - QString(_("key not found in the Keyserver"))); - break; - case QNetworkReply::TimeoutError: - QMessageBox::critical(nullptr, _("Timeout"), "Connection timeout"); - break; - case QNetworkReply::HostNotFoundError: - QMessageBox::critical(nullptr, _("Host Not Found"), - "cannot resolve the default Keyserver"); - break; - default: - QMessageBox::critical(nullptr, _("Connection Error"), - _("General Connection Error")); - } - } - if (m_automatic_) { - setWindowFlags(Qt::Window | Qt::WindowTitleHint | - Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); - } + set_message(err_msg, true); return; } - this->import_keys( - std::make_unique<ByteArray>(buffer.constData(), buffer.length())); - - if (!m_automatic_) { - set_message(QString("<h4>") + _("Key Imported") + "</h4>", false); - } -} - -void KeyServerImportDialog::import_keys(ByteArrayPtr in_data) { - GpgImportInformation result = - GpgKeyImportExporter::GetInstance().ImportKey(std::move(in_data)); + set_message(_("Key Imported"), false); // refresh the key database emit SignalKeyImported(); - QWidget* p_parent = qobject_cast<QWidget*>(parent()); - if (m_automatic_) { - auto* dialog = new KeyImportDetailDialog(result, true, p_parent); - dialog->show(); - this->accept(); - } else { - auto* dialog = new KeyImportDetailDialog(result, false, this); - dialog->exec(); - } + // show details + (new KeyImportDetailDialog(std::move(info), this))->exec(); } void KeyServerImportDialog::set_loading(bool status) { waiting_bar_->setVisible(status); - if (!m_automatic_) { - icon_->setVisible(!status); - message_->setVisible(!status); - } + if (status) set_message(_("Processing ..."), false); } } // namespace GpgFrontend::UI diff --git a/src/ui/dialog/import_export/KeyServerImportDialog.h b/src/ui/dialog/import_export/KeyServerImportDialog.h index 13ae00b5..bbd4634a 100644 --- a/src/ui/dialog/import_export/KeyServerImportDialog.h +++ b/src/ui/dialog/import_export/KeyServerImportDialog.h @@ -53,14 +53,7 @@ class KeyServerImportDialog : public GeneralDialog { * @param automatic * @param parent */ - KeyServerImportDialog(bool automatic, QWidget* parent); - - /** - * @brief Construct a new Key Server Import Dialog object - * - * @param parent - */ - explicit KeyServerImportDialog(QWidget* parent); + KeyServerImportDialog(QWidget* parent); public slots: @@ -108,8 +101,8 @@ class KeyServerImportDialog : public GeneralDialog { * * @param keyid */ - void slot_import_finished(QNetworkReply::NetworkError error, - QByteArray buffer); + void slot_import_finished(bool success, QString err_msg, QByteArray buffer, + std::shared_ptr<GpgImportInformation> info); /** * @brief @@ -160,9 +153,10 @@ class KeyServerImportDialog : public GeneralDialog { * * @return QComboBox* */ - QComboBox* create_comboBox(); + QComboBox* create_combo_box(); + - bool m_automatic_ = false; ///< + QHBoxLayout* message_layout_; ///< QLineEdit* search_line_edit_{}; ///< QComboBox* key_server_combo_box_{}; ///< diff --git a/src/ui/dialog/import_export/KeyUploadDialog.cpp b/src/ui/dialog/import_export/KeyUploadDialog.cpp index f12149d8..ed46fe9d 100644 --- a/src/ui/dialog/import_export/KeyUploadDialog.cpp +++ b/src/ui/dialog/import_export/KeyUploadDialog.cpp @@ -32,9 +32,10 @@ #include <algorithm> #include "core/GpgModel.h" -#include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" +#include "core/utils/GpgUtils.h" +#include "ui/UserInterfaceUtils.h" #include "ui/struct/SettingsObject.h" namespace GpgFrontend::UI { @@ -61,17 +62,29 @@ KeyUploadDialog::KeyUploadDialog(const KeyIdArgsListPtr& keys_ids, } void KeyUploadDialog::SlotUpload() { - auto out_data = GpgFrontend::SecureCreateSharedObject<ByteArray>(); - GpgKeyImportExporter::GetInstance().ExportKeys(*m_keys_, out_data); - slot_upload_key_to_server(*out_data); - - // Done - this->hide(); - this->close(); + GpgKeyImportExporter::GetInstance().ExportKeys( + *m_keys_, false, true, false, false, + [=](GpgError err, const DataObjectPtr& data_obj) { + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); + return; + } + + if (data_obj == nullptr || !data_obj->Check<GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto gf_buffer = ExtractParams<GFBuffer>(data_obj, 0); + slot_upload_key_to_server(gf_buffer); + + // Done + this->hide(); + this->close(); + }); } void KeyUploadDialog::slot_upload_key_to_server( - const GpgFrontend::ByteArray& keys_data) { + const GpgFrontend::GFBuffer& keys_data) { std::string target_keyserver; try { @@ -106,7 +119,7 @@ void KeyUploadDialog::slot_upload_key_to_server( // Building Post Data QByteArray post_data; - auto data = std::string(keys_data); + auto data = keys_data.ConvertToStdString(); boost::algorithm::replace_all(data, "\n", "%0A"); boost::algorithm::replace_all(data, "\r", "%0D"); diff --git a/src/ui/dialog/import_export/KeyUploadDialog.h b/src/ui/dialog/import_export/KeyUploadDialog.h index 099382b9..042989e5 100644 --- a/src/ui/dialog/import_export/KeyUploadDialog.h +++ b/src/ui/dialog/import_export/KeyUploadDialog.h @@ -28,8 +28,7 @@ #pragma once -#include "core/function/gpg/GpgContext.h" -#include "core/typedef/CoreTypedef.h" +#include "core/model/GFBuffer.h" #include "core/typedef/GpgTypedef.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" @@ -66,7 +65,7 @@ class KeyUploadDialog : public GeneralDialog { * * @param keys_data */ - void slot_upload_key_to_server(const ByteArray& keys_data); + void slot_upload_key_to_server(const GFBuffer&); /** * @brief diff --git a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp index 9513c3c4..4a1d0f8c 100644 --- a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp @@ -155,26 +155,25 @@ void KeyPairOperaTab::CreateOperaMenu() { secret_key_export_opera_menu_ = new QMenu(this); - auto* exportFullSecretKey = new QAction(_("Export Full Secret Key"), this); - connect(exportFullSecretKey, &QAction::triggered, this, + auto* export_full_secret_key = new QAction(_("Export Full Secret Key"), this); + connect(export_full_secret_key, &QAction::triggered, this, &KeyPairOperaTab::slot_export_private_key); - if (!m_key_.IsPrivateKey()) exportFullSecretKey->setDisabled(true); + if (!m_key_.IsPrivateKey()) export_full_secret_key->setDisabled(true); - auto* exportShortestSecretKey = + auto* export_shortest_secret_key = new QAction(_("Export Shortest Secret Key"), this); - connect(exportShortestSecretKey, &QAction::triggered, this, + connect(export_shortest_secret_key, &QAction::triggered, this, &KeyPairOperaTab::slot_export_short_private_key); - secret_key_export_opera_menu_->addAction(exportFullSecretKey); - secret_key_export_opera_menu_->addAction(exportShortestSecretKey); + secret_key_export_opera_menu_->addAction(export_full_secret_key); + secret_key_export_opera_menu_->addAction(export_shortest_secret_key); } void KeyPairOperaTab::slot_export_public_key() { - ByteArrayPtr keyArray = nullptr; - - if (!GpgKeyImportExporter::GetInstance().ExportKey(m_key_, keyArray)) { - QMessageBox::critical(this, _("Error"), - _("An error occurred during the export operation.")); + auto [err, gf_buffer] = + GpgKeyImportExporter::GetInstance().ExportKey(m_key_, false, true, false); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); return; } @@ -196,7 +195,7 @@ void KeyPairOperaTab::slot_export_public_key() { if (file_name.empty()) return; - if (!WriteBufferToFile(file_name, *keyArray)) { + if (!WriteFileGFBuffer(file_name, gf_buffer)) { QMessageBox::critical( this, _("Export Error"), QString(_("Couldn't open %1 for writing")).arg(file_name.c_str())); @@ -220,13 +219,10 @@ void KeyPairOperaTab::slot_export_short_private_key() { // export key, if ok was clicked if (ret == QMessageBox::Ok) { - ByteArrayPtr keyArray = nullptr; - - if (!GpgKeyImportExporter::GetInstance().ExportSecretKeyShortest( - m_key_, keyArray)) { - QMessageBox::critical( - this, _("Error"), - _("An error occurred during the export operation.")); + auto [err, gf_buffer] = + GpgKeyImportExporter::GetInstance().ExportKey(m_key_, true, true, true); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); return; } @@ -248,7 +244,7 @@ void KeyPairOperaTab::slot_export_short_private_key() { if (file_name.empty()) return; - if (!WriteBufferToFile(file_name, *keyArray)) { + if (!WriteFileGFBuffer(file_name, gf_buffer)) { QMessageBox::critical( this, _("Export Error"), QString(_("Couldn't open %1 for writing")).arg(file_name.c_str())); @@ -269,13 +265,10 @@ void KeyPairOperaTab::slot_export_private_key() { // export key, if ok was clicked if (ret == QMessageBox::Ok) { - ByteArrayPtr keyArray = nullptr; - - if (!GpgKeyImportExporter::GetInstance().ExportSecretKey(m_key_, - keyArray)) { - QMessageBox::critical( - this, _("Error"), - _("An error occurred during the export operation.")); + auto [err, gf_buffer] = GpgKeyImportExporter::GetInstance().ExportKey( + m_key_, true, true, false); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); return; } @@ -297,7 +290,7 @@ void KeyPairOperaTab::slot_export_private_key() { if (file_name.empty()) return; - if (!WriteBufferToFile(file_name, *keyArray)) { + if (!WriteFileGFBuffer(file_name, gf_buffer)) { QMessageBox::critical( this, _("Export Error"), QString(_("Couldn't open %1 for writing")).arg(file_name.c_str())); diff --git a/src/ui/main_window/KeyMgmt.cpp b/src/ui/main_window/KeyMgmt.cpp index 4462011a..d6e209de 100644 --- a/src/ui/main_window/KeyMgmt.cpp +++ b/src/ui/main_window/KeyMgmt.cpp @@ -35,6 +35,8 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/function/gpg/GpgKeyOpera.h" +#include "core/model/GpgImportInformation.h" +#include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" #include "function/SetOwnerTrustLevel.h" #include "ui/UISignalStation.h" @@ -96,9 +98,10 @@ KeyMgmt::KeyMgmt(QWidget* parent) }); setCentralWidget(key_list_); - key_list_->SetDoubleClickedAction([this](const GpgKey& key, QWidget* parent) { - new KeyDetailsDialog(key, this); - }); + key_list_->SetDoubleClickedAction( + [this](const GpgKey& key, QWidget* /*parent*/) { + new KeyDetailsDialog(key, this); + }); key_list_->SlotRefresh(); @@ -173,7 +176,7 @@ void KeyMgmt::create_actions() { CommonUtils::GetInstance()->SlotImportKeyFromClipboard(this); }); - bool forbid_all_gnupg_connection = + bool const forbid_all_gnupg_connection = GlobalSettingStation::GetInstance().LookupSettings( "network.forbid_all_gnupg_connection", false); @@ -197,11 +200,11 @@ void KeyMgmt::create_actions() { export_key_to_clipboard_act_ = new QAction(_("Export To Clipboard"), this); export_key_to_clipboard_act_->setIcon(QIcon(":export_key_to_clipboard.png")); export_key_to_clipboard_act_->setToolTip( - _("Export Selected Key(s) To Clipboard")); + _("Export Checked Key(s) To Clipboard")); connect(export_key_to_clipboard_act_, &QAction::triggered, this, &KeyMgmt::SlotExportKeyToClipboard); - export_key_to_file_act_ = new QAction(_("Export To Key Package"), this); + export_key_to_file_act_ = new QAction(_("Export As Key Package"), this); export_key_to_file_act_->setIcon(QIcon(":key_package.png")); export_key_to_file_act_->setToolTip( _("Export Checked Key(s) To a Key Package")); @@ -211,7 +214,7 @@ void KeyMgmt::create_actions() { export_key_as_open_ssh_format_ = new QAction(_("Export As OpenSSH"), this); export_key_as_open_ssh_format_->setIcon(QIcon(":ssh-key.png")); export_key_as_open_ssh_format_->setToolTip( - _("Export Selected Key(s) As OpenSSH Format to File")); + _("Export Checked Key As OpenSSH Format to File")); connect(export_key_as_open_ssh_format_, &QAction::triggered, this, &KeyMgmt::SlotExportAsOpenSSHFormat); @@ -260,9 +263,10 @@ void KeyMgmt::create_menus() { import_key_menu_->addAction(import_key_from_key_server_act_); import_key_menu_->addAction(import_keys_from_key_package_act_); - key_menu_->addAction(export_key_to_file_act_); - key_menu_->addAction(export_key_to_clipboard_act_); - key_menu_->addAction(export_key_as_open_ssh_format_); + export_key_menu_ = key_menu_->addMenu(_("Export Key")); + export_key_menu_->addAction(export_key_to_file_act_); + export_key_menu_->addAction(export_key_to_clipboard_act_); + export_key_menu_->addAction(export_key_as_open_ssh_format_); key_menu_->addSeparator(); key_menu_->addAction(delete_checked_keys_act_); } @@ -273,23 +277,28 @@ void KeyMgmt::create_tool_bars() { // genrate key pair key_tool_bar->addAction(generate_key_pair_act_); + key_tool_bar->addSeparator(); // add button with popup menu for import - auto* tool_button = new QToolButton(this); - tool_button->setMenu(import_key_menu_); - tool_button->setPopupMode(QToolButton::InstantPopup); - tool_button->setIcon(QIcon(":key_import.png")); - tool_button->setToolTip(_("Import key")); - tool_button->setText(_("Import Key")); - tool_button->setToolButtonStyle(icon_style_); - key_tool_bar->addWidget(tool_button); + auto* import_tool_button = new QToolButton(this); + import_tool_button->setMenu(import_key_menu_); + import_tool_button->setPopupMode(QToolButton::InstantPopup); + import_tool_button->setIcon(QIcon(":key_import.png")); + import_tool_button->setToolTip(_("Import key")); + import_tool_button->setText(_("Import Key")); + import_tool_button->setToolButtonStyle(icon_style_); + key_tool_bar->addWidget(import_tool_button); + + auto* export_tool_button = new QToolButton(this); + export_tool_button->setMenu(export_key_menu_); + export_tool_button->setPopupMode(QToolButton::InstantPopup); + export_tool_button->setIcon(QIcon(":key_export.png")); + export_tool_button->setToolTip(_("Export key")); + export_tool_button->setText(_("Export Key")); + export_tool_button->setToolButtonStyle(icon_style_); + key_tool_bar->addWidget(export_tool_button); - key_tool_bar->addSeparator(); key_tool_bar->addAction(delete_checked_keys_act_); - key_tool_bar->addSeparator(); - key_tool_bar->addAction(export_key_to_file_act_); - key_tool_bar->addAction(export_key_to_clipboard_act_); - key_tool_bar->addAction(export_key_as_open_ssh_format_); } void KeyMgmt::SlotDeleteSelectedKeys() { @@ -317,7 +326,7 @@ void KeyMgmt::delete_keys_with_warning(KeyIdArgsListPtr uidList) { keynames.append("> </i><br/>"); } - int ret = QMessageBox::warning( + int const ret = QMessageBox::warning( this, _("Deleting Keys"), "<b>" + QString( @@ -354,7 +363,7 @@ void KeyMgmt::SlotExportKeyToKeyPackage() { _("Please check some keys before doing this operation.")); return; } - auto dialog = new ExportKeyPackageDialog(std::move(keys_checked), this); + auto* dialog = new ExportKeyPackageDialog(std::move(keys_checked), this); dialog->exec(); emit SignalStatusBarChanged(QString(_("key(s) exported"))); } @@ -368,17 +377,45 @@ void KeyMgmt::SlotExportKeyToClipboard() { return; } - ByteArrayPtr key_export_data = nullptr; - if (!GpgKeyImportExporter::GetInstance().ExportKeys(keys_checked, - key_export_data)) { - return; + if (keys_checked->size() == 1) { + auto key = GpgKeyGetter::GetInstance().GetKey(keys_checked->front()); + auto [err, gf_buffer] = + GpgKeyImportExporter::GetInstance().ExportKey(key, false, true, false); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); + return; + } + QApplication::clipboard()->setText(gf_buffer.ConvertToQByteArray()); + } else { + auto keys = GpgKeyGetter::GetInstance().GetKeys(keys_checked); + CommonUtils::WaitForOpera( + this, _("Exporting"), [=](const OperaWaitingHd& op_hd) { + GpgKeyImportExporter::GetInstance().ExportKeys( + *keys, false, true, false, false, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); + return; + } + + if (data_obj == nullptr || !data_obj->Check<GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto gf_buffer = ExtractParams<GFBuffer>(data_obj, 0); + QApplication::clipboard()->setText( + gf_buffer.ConvertToQByteArray()); + }); + }); } - QApplication::clipboard()->setText(QString::fromStdString(*key_export_data)); } void KeyMgmt::SlotGenerateKeyDialog() { - auto* keyGenDialog = new KeyGenDialog(this); - keyGenDialog->show(); + (new KeyGenDialog(this))->exec(); + this->raise(); } void KeyMgmt::SlotGenerateSubKey() { @@ -401,53 +438,68 @@ void KeyMgmt::SlotGenerateSubKey() { return; } - auto dialog = new SubkeyGenerateDialog(key.GetId(), this); - dialog->show(); + (new SubkeyGenerateDialog(key.GetId(), this))->exec(); + this->raise(); } void KeyMgmt::SlotExportAsOpenSSHFormat() { - ByteArrayPtr key_export_data = nullptr; auto keys_checked = key_list_->GetChecked(); - if (keys_checked->empty()) { QMessageBox::critical( this, _("Forbidden"), - _("Please select a key before performing this operation. If you select " - "multiple keys, only the first key will be exported.")); - return; - } - - auto key = GpgKeyGetter::GetInstance().GetKey(keys_checked->front()); - if (!GpgKeyImportExporter::GetInstance().ExportKeyOpenSSH(key, - key_export_data)) { - QMessageBox::critical(this, _("Error"), _("An error occur in exporting.")); + _("Please check a key before performing this operation.")); return; } - if (key_export_data->empty()) { - QMessageBox::critical( - this, _("Error"), - _("This key may not be able to export as OpenSSH format. Please check " - "the key-size of the subkey(s) used to sign.")); - return; - } - - key = GpgKeyGetter::GetInstance().GetKey(keys_checked->front()); - if (!key.IsGood()) { - QMessageBox::critical(this, _("Error"), _("Key Not Found.")); + if (keys_checked->size() > 1) { + QMessageBox::critical(this, _("Forbidden"), + _("This operation accepts just a single key.")); return; } - QString fileString = QString::fromStdString( - key.GetName() + " " + key.GetEmail() + "(" + key.GetId() + ").pub"); - QString file_name = QFileDialog::getSaveFileName( - this, _("Export OpenSSH Key To File"), fileString, - QString(_("OpenSSH Public Key Files")) + " (*.pub);;All Files (*)"); - - if (!file_name.isEmpty()) { - WriteBufferToFile(file_name.toStdString(), *key_export_data); - emit SignalStatusBarChanged(QString(_("key(s) exported"))); - } + auto keys = GpgKeyGetter::GetInstance().GetKeys(keys_checked); + CommonUtils::WaitForOpera( + this, _("Exporting"), [this, keys](const OperaWaitingHd& op_hd) { + GpgKeyImportExporter::GetInstance().ExportKeys( + *keys, false, true, false, true, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); + return; + } + + if (data_obj == nullptr || !data_obj->Check<GFBuffer>()) { + throw std::runtime_error("data object doesn't pass checking"); + } + + auto gf_buffer = ExtractParams<GFBuffer>(data_obj, 0); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); + return; + } + + if (gf_buffer.Empty()) { + QMessageBox::critical( + this, _("Error"), + _("This key may not be able to export as OpenSSH format. " + "Please check the key-size of the subkey(s) used to " + "sign.")); + return; + } + + QString const file_name = QFileDialog::getSaveFileName( + this, _("Export OpenSSH Key To File"), "authorized_keys", + QString(_("OpenSSH Public Key Files")) + "All Files (*)"); + + if (!file_name.isEmpty()) { + WriteFileGFBuffer(file_name.toStdString(), gf_buffer); + emit SignalStatusBarChanged(QString(_("key(s) exported"))); + } + }); + }); } void KeyMgmt::SlotImportKeyPackage() { @@ -463,17 +515,17 @@ void KeyMgmt::SlotImportKeyPackage() { if (key_package_file_name.isEmpty() || key_file_name.isEmpty()) return; - GpgImportInformation info; - GF_UI_LOG_INFO("importing key package: {}", key_package_file_name.toStdString()); - if (KeyPackageOperator::ImportKeyPackage(key_package_file_name.toStdString(), - key_file_name.toStdString(), info)) { + const auto [success, info] = KeyPackageOperator::ImportKeyPackage( + key_package_file_name.toStdString(), key_file_name.toStdString()); + + if (success) { emit SignalStatusBarChanged(QString(_("key(s) imported"))); emit SignalKeyStatusUpdated(); - auto dialog = new KeyImportDetailDialog(info, false, this); + auto* dialog = new KeyImportDetailDialog(info, this); dialog->exec(); } else { QMessageBox::critical(this, _("Error"), diff --git a/src/ui/main_window/KeyMgmt.h b/src/ui/main_window/KeyMgmt.h index a75f1c48..696ce733 100644 --- a/src/ui/main_window/KeyMgmt.h +++ b/src/ui/main_window/KeyMgmt.h @@ -149,11 +149,13 @@ class KeyMgmt : public GeneralMainWindow { */ void delete_keys_with_warning(GpgFrontend::KeyIdArgsListPtr uidList); - KeyList* key_list_; ///< - QMenu* file_menu_{}; ///< - QMenu* key_menu_{}; ///< - QMenu* generate_key_menu_{}; ///< - QMenu* import_key_menu_{}; ///< + KeyList* key_list_; ///< + QMenu* file_menu_{}; ///< + QMenu* key_menu_{}; ///< + QMenu* generate_key_menu_{}; ///< + QMenu* import_key_menu_{}; ///< + QMenu* export_key_menu_{}; /// < + QAction* open_key_file_act_{}; ///< QAction* export_key_to_file_act_{}; ///< QAction* export_key_as_open_ssh_format_{}; ///< diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 3f29d281..6e87d6b6 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -65,20 +65,28 @@ void MainWindow::slot_find() { * Append the selected (not checked!) Key(s) To Textedit */ void MainWindow::slot_append_selected_keys() { - auto exported = GpgFrontend::SecureCreateSharedObject<ByteArray>(); auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) { - GF_UI_LOG_ERROR("no key is selected"); + GF_UI_LOG_ERROR("no key is selected to export"); + return; + } + + auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); + if (!key.IsGood()) { + GF_UI_LOG_ERROR("selected key for exporting is invalid, key id: {}", + key_ids->front()); return; } - if (!GpgKeyImportExporter::GetInstance().ExportKeys(key_ids, exported)) { - QMessageBox::critical(this, _("Error"), _("Key Export Operation Failed.")); + auto [err, gf_buffer] = + GpgKeyImportExporter::GetInstance().ExportKey(key, false, true, false); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); return; } - edit_->SlotAppendText2CurTextPage(QString::fromStdString(*exported)); + edit_->SlotAppendText2CurTextPage(gf_buffer.ConvertToQByteArray()); } void MainWindow::slot_append_keys_create_datetime() { @@ -300,7 +308,7 @@ void MainWindow::slot_version_upgrade_nofity() { QString(_("GpgFrontend Upgradeable (New Version: %1).")) .arg(latest_version.c_str()), 30000); - auto update_button = new QPushButton("Update GpgFrontend", this); + auto* update_button = new QPushButton("Update GpgFrontend", this); connect(update_button, &QPushButton::clicked, [=]() { auto* about_dialog = new AboutDialog(2, this); about_dialog->show(); diff --git a/src/ui/thread/KeyServerImportTask.cpp b/src/ui/thread/KeyServerImportTask.cpp index 1f9b1b92..e07473a8 100644 --- a/src/ui/thread/KeyServerImportTask.cpp +++ b/src/ui/thread/KeyServerImportTask.cpp @@ -28,7 +28,8 @@ #include "ui/thread/KeyServerImportTask.h" -#include <vector> +#include "core/function/gpg/GpgKeyImportExporter.h" +#include "ui/struct/SettingsObject.h" GpgFrontend::UI::KeyServerImportTask::KeyServerImportTask( std::string keyserver_url, std::vector<std::string> keyids) @@ -37,29 +38,74 @@ GpgFrontend::UI::KeyServerImportTask::KeyServerImportTask( keyids_(std::move(keyids)), manager_(new QNetworkAccessManager(this)) { HoldOnLifeCycle(true); + + if (keyserver_url_.empty()) { + try { + SettingsObject key_server_json("key_server"); + const auto key_server_list = + key_server_json.Check("server_list", nlohmann::json::array()); + + size_t const default_key_server_index = + key_server_json.Check("default_server", 0); + if (default_key_server_index >= key_server_list.size()) { + throw std::runtime_error("default_server index out of range"); + } + auto default_key_server = + key_server_list[default_key_server_index].get<std::string>(); + + keyserver_url_ = default_key_server; + } catch (...) { + GF_UI_LOG_ERROR("setting operation error", "server_list", + "default_server"); + keyserver_url_ = "https://keys.openpgp.org"; + return; + } + + GF_UI_LOG_DEBUG("key server import task sets key server url: {}", + keyserver_url_); + } } -void GpgFrontend::UI::KeyServerImportTask::run() { - QUrl keyserver_url = QUrl(keyserver_url_.c_str()); +void GpgFrontend::UI::KeyServerImportTask::Run() { + QUrl const keyserver_url = QUrl(keyserver_url_.c_str()); for (const auto& key_id : keyids_) { - QUrl req_url(keyserver_url.scheme() + "://" + keyserver_url.host() + - "/pks/lookup?op=get&search=0x" + key_id.c_str() + - "&options=mr"); + QUrl const req_url(keyserver_url.scheme() + "://" + keyserver_url.host() + + "/pks/lookup?op=get&search=0x" + key_id.c_str() + + "&options=mr"); reply_ = manager_->get(QNetworkRequest(req_url)); - connect(reply_, &QNetworkReply::finished, this, &KeyServerImportTask::dealing_reply_from_server); } } void GpgFrontend::UI::KeyServerImportTask::dealing_reply_from_server() { - QByteArray buffer; - QNetworkReply::NetworkError network_reply = reply_->error(); - if (network_reply == QNetworkReply::NoError) { - buffer = reply_->readAll(); + auto const network_reply = reply_->error(); + auto buffer = reply_->readAll(); + + if (network_reply != QNetworkReply::NoError) { + GF_UI_LOG_ERROR("key import error, message from key server reply: ", + buffer); + QString err_msg; + switch (network_reply) { + case QNetworkReply::ContentNotFoundError: + err_msg = QString(_("Key not found in the Keyserver.")); + break; + case QNetworkReply::TimeoutError: + err_msg = QString(_("Network connection timeout.")); + break; + case QNetworkReply::HostNotFoundError: + err_msg = + QString(_("Cannot resolve the address of target key server.")); + break; + default: + err_msg = QString(_("General connection error occurred.")); + } + emit SignalKeyServerImportResult(false, err_msg, buffer, nullptr); } - emit SignalKeyServerImportResult(network_reply, buffer); + + auto info = GpgKeyImportExporter::GetInstance().ImportKey(GFBuffer(buffer)); + emit SignalKeyServerImportResult(true, _("Success"), buffer, info); if (static_cast<size_t>(result_count_++) == keyids_.size() - 1) { emit SignalTaskShouldEnd(0); diff --git a/src/ui/thread/KeyServerImportTask.h b/src/ui/thread/KeyServerImportTask.h index 11ecc42b..5416ca1a 100644 --- a/src/ui/thread/KeyServerImportTask.h +++ b/src/ui/thread/KeyServerImportTask.h @@ -31,10 +31,14 @@ #include <qnetworkaccessmanager.h> #include <qnetworkreply.h> -#include "GpgFrontendUI.h" -#include "core/thread/ThreadingModel.h" +#include "core/thread/Task.h" + +namespace GpgFrontend { +class GpgImportInformation; +} namespace GpgFrontend::UI { + class KeyServerImportTask : public Thread::Task { Q_OBJECT public: @@ -47,22 +51,21 @@ class KeyServerImportTask : public Thread::Task { KeyServerImportTask(std::string keyserver_url, std::vector<std::string> keyid); - signals: - /** * @brief * - * @param result */ - void SignalKeyServerImportResult(QNetworkReply::NetworkError reply, - QByteArray buffer); + void Run() override; + + signals: - protected: /** * @brief * + * @param result */ - void run() override; + void SignalKeyServerImportResult(bool, QString, QByteArray, + std::shared_ptr<GpgImportInformation>); private slots: diff --git a/src/ui/thread/KeyServerSearchTask.cpp b/src/ui/thread/KeyServerSearchTask.cpp index 2aaa95f0..dfaa6474 100644 --- a/src/ui/thread/KeyServerSearchTask.cpp +++ b/src/ui/thread/KeyServerSearchTask.cpp @@ -39,7 +39,7 @@ GpgFrontend::UI::KeyServerSearchTask::KeyServerSearchTask( HoldOnLifeCycle(true); } -void GpgFrontend::UI::KeyServerSearchTask::run() { +void GpgFrontend::UI::KeyServerSearchTask::Run() { QUrl url_from_remote = QString::fromStdString(keyserver_url_) + "/pks/lookup?search=" + QString::fromStdString(search_string_) + diff --git a/src/ui/thread/KeyServerSearchTask.h b/src/ui/thread/KeyServerSearchTask.h index 746ce7a8..ea92903f 100644 --- a/src/ui/thread/KeyServerSearchTask.h +++ b/src/ui/thread/KeyServerSearchTask.h @@ -46,22 +46,21 @@ class KeyServerSearchTask : public Thread::Task { */ KeyServerSearchTask(std::string keyserver_url, std::string search_string); - signals: - /** * @brief * - * @param result */ - void SignalKeyServerSearchResult(QNetworkReply::NetworkError reply, - QByteArray buffer); + void Run() override; + + signals: - protected: /** * @brief * + * @param result */ - void run() override; + void SignalKeyServerSearchResult(QNetworkReply::NetworkError reply, + QByteArray buffer); private slots: diff --git a/src/ui/thread/ListedKeyServerTestTask.cpp b/src/ui/thread/ListedKeyServerTestTask.cpp index 18299725..11552562 100644 --- a/src/ui/thread/ListedKeyServerTestTask.cpp +++ b/src/ui/thread/ListedKeyServerTestTask.cpp @@ -43,7 +43,7 @@ GpgFrontend::UI::ListedKeyServerTestTask::ListedKeyServerTestTask( "std::vector<KeyServerTestResultType>"); } -void GpgFrontend::UI::ListedKeyServerTestTask::run() { +void GpgFrontend::UI::ListedKeyServerTestTask::Run() { size_t index = 0; for (const auto& url : urls_) { auto key_url = QUrl{url}; diff --git a/src/ui/thread/ListedKeyServerTestTask.h b/src/ui/thread/ListedKeyServerTestTask.h index aa17a12a..d6634f2a 100644 --- a/src/ui/thread/ListedKeyServerTestTask.h +++ b/src/ui/thread/ListedKeyServerTestTask.h @@ -52,21 +52,20 @@ class ListedKeyServerTestTask : public Thread::Task { explicit ListedKeyServerTestTask(const QStringList& urls, int timeout, QWidget* parent = nullptr); - signals: /** * @brief * - * @param result */ - void SignalKeyServerListTestResult( - std::vector<KeyServerTestResultType> result); + void Run() override; - protected: + signals: /** * @brief * + * @param result */ - void run() override; + void SignalKeyServerListTestResult( + std::vector<KeyServerTestResultType> result); private: QStringList urls_; ///< diff --git a/src/ui/thread/ProxyConnectionTestTask.cpp b/src/ui/thread/ProxyConnectionTestTask.cpp index b952d0b9..5929c9d1 100644 --- a/src/ui/thread/ProxyConnectionTestTask.cpp +++ b/src/ui/thread/ProxyConnectionTestTask.cpp @@ -39,7 +39,7 @@ GpgFrontend::UI::ProxyConnectionTestTask::ProxyConnectionTestTask(QString url, HoldOnLifeCycle(true); } -void GpgFrontend::UI::ProxyConnectionTestTask::run() { +void GpgFrontend::UI::ProxyConnectionTestTask::Run() { auto* network_reply = network_manager_->get(QNetworkRequest{url_}); auto* timer = new QTimer(this); diff --git a/src/ui/thread/ProxyConnectionTestTask.h b/src/ui/thread/ProxyConnectionTestTask.h index ac8982b5..ac9833a7 100644 --- a/src/ui/thread/ProxyConnectionTestTask.h +++ b/src/ui/thread/ProxyConnectionTestTask.h @@ -52,6 +52,12 @@ class ProxyConnectionTestTask : public Thread::Task { */ explicit ProxyConnectionTestTask(QString url, int timeout); + /** + * @brief + * + */ + void Run() override; + signals: /** * @brief @@ -60,14 +66,13 @@ class ProxyConnectionTestTask : public Thread::Task { */ void SignalProxyConnectionTestResult(const QString& result); - protected: + private slots: + /** * @brief * + * @param reply */ - void run() override; - - private slots: void slot_process_network_reply(QNetworkReply* reply); private: diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index 04c184b0..135a3c6d 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -36,6 +36,7 @@ #include "core/GpgCoreInit.h" #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/model/GpgImportInformation.h" #include "ui/UISignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui_KeyList.h" @@ -417,20 +418,10 @@ void KeyList::dragEnterEvent(QDragEnterEvent* event) { event->acceptProposedAction(); } -/** set background color for Keys and put them to top - * - */ -[[maybe_unused]] void KeyList::MarkKeys(QStringList* keyIds) { - foreach (QString id, *keyIds) { - spdlog::debug("marked: ", id.toStdString()); - } -} - -void KeyList::import_keys(const QByteArray& inBuffer) { - auto std_buffer = std::make_unique<ByteArray>(inBuffer.toStdString()); - GpgImportInformation result = - GpgKeyImportExporter::GetInstance().ImportKey(std::move(std_buffer)); - new KeyImportDetailDialog(result, false, this); +void KeyList::import_keys(const QByteArray& in_buffer) { + auto result = + GpgKeyImportExporter::GetInstance().ImportKey(GFBuffer(in_buffer)); + (new KeyImportDetailDialog(result, this))->exec(); } void KeyList::slot_double_clicked(const QModelIndex& index) { |