diff options
author | Saturn&Eric <[email protected]> | 2024-01-23 07:21:28 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2024-01-23 07:21:28 +0000 |
commit | 56acf161d439ce73eceaa145c40fe703bb2c3f02 (patch) | |
tree | a5d4790a6b2efc8786a3c0f74a07f5a8032d4e94 /src/ui/dialog/keypair_details/KeyPairOperaTab.cpp | |
parent | fix: use more secure cdn links (diff) | |
parent | fix: solve issues on detected gnupg path function and fix linking errors on w... (diff) | |
download | GpgFrontend-56acf161d439ce73eceaa145c40fe703bb2c3f02.tar.gz GpgFrontend-56acf161d439ce73eceaa145c40fe703bb2c3f02.zip |
Merge pull request #126 from saturneric/dev/2.1.1/main
Develop 2.1.2.1
Diffstat (limited to 'src/ui/dialog/keypair_details/KeyPairOperaTab.cpp')
-rw-r--r-- | src/ui/dialog/keypair_details/KeyPairOperaTab.cpp | 316 |
1 files changed, 135 insertions, 181 deletions
diff --git a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp index c5d0670a..7756ac37 100644 --- a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric <[email protected]> * * This file is part of GpgFrontend. * @@ -19,8 +19,10 @@ * The initial version of the source code is inherited from * the gpg4usb project, which is under GPL-3.0-or-later. * - * The source code version of this software was modified and released - * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * 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 * */ @@ -28,44 +30,49 @@ #include "KeySetExpireDateDialog.h" #include "core/function/GlobalSettingStation.h" +#include "core/function/gpg//GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" -#include "core/function/gpg/GpgKeyManager.h" #include "core/function/gpg/GpgKeyOpera.h" -#include "ui/SignalStation.h" +#include "core/model/GpgKey.h" +#include "core/typedef/GpgTypedef.h" +#include "core/utils/GpgUtils.h" +#include "core/utils/IOUtils.h" +#include "ui/UISignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/import_export/KeyUploadDialog.h" +#include "ui/function/SetOwnerTrustLevel.h" namespace GpgFrontend::UI { -KeyPairOperaTab::KeyPairOperaTab(const std::string& key_id, QWidget* parent) +KeyPairOperaTab::KeyPairOperaTab(const QString& key_id, QWidget* parent) : QWidget(parent), m_key_(GpgKeyGetter::GetInstance().GetKey(key_id)) { // Set Menu CreateOperaMenu(); - auto m_vbox = new QVBoxLayout(this); + auto* m_vbox = new QVBoxLayout(this); - auto* opera_key_box = new QGroupBox(_("General Operations")); + auto* opera_key_box = new QGroupBox(tr("General Operations")); auto* vbox_p_k = new QVBoxLayout(); - auto export_h_box_layout = new QHBoxLayout(); + auto* export_h_box_layout = new QHBoxLayout(); vbox_p_k->addLayout(export_h_box_layout); - auto* export_public_button = new QPushButton(_("Export Public Key")); + auto* export_public_button = new QPushButton(tr("Export Public Key")); export_h_box_layout->addWidget(export_public_button); connect(export_public_button, &QPushButton::clicked, this, &KeyPairOperaTab::slot_export_public_key); if (m_key_.IsPrivateKey()) { - auto* export_private_button = new QPushButton(_("Export Private Key")); + auto* export_private_button = new QPushButton(tr("Export Private Key")); export_private_button->setStyleSheet("text-align:center;"); export_private_button->setMenu(secret_key_export_opera_menu_); export_h_box_layout->addWidget(export_private_button); if (m_key_.IsHasMasterKey()) { auto* edit_expires_button = - new QPushButton(_("Modify Expiration Datetime (Primary Key)")); + new QPushButton(tr("Modify Expiration Datetime (Primary Key)")); connect(edit_expires_button, &QPushButton::clicked, this, &KeyPairOperaTab::slot_modify_edit_datetime); - auto* edit_password_button = new QPushButton(_("Modify Password")); + auto* edit_password_button = new QPushButton(tr("Modify Password")); connect(edit_password_button, &QPushButton::clicked, this, &KeyPairOperaTab::slot_modify_password); @@ -74,21 +81,17 @@ KeyPairOperaTab::KeyPairOperaTab(const std::string& key_id, QWidget* parent) } } - auto advance_h_box_layout = new QHBoxLayout(); + auto* advance_h_box_layout = new QHBoxLayout(); + + auto settings = + GpgFrontend::GlobalSettingStation::GetInstance().GetSettings(); - // get settings - auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); // read settings - bool forbid_all_gnupg_connection = false; - try { - forbid_all_gnupg_connection = - settings.lookup("network.forbid_all_gnupg_connection"); - } catch (...) { - SPDLOG_ERROR("setting operation error: forbid_all_gnupg_connection"); - } + bool forbid_all_gnupg_connection = + settings.value("network/forbid_all_gnupg_connection").toBool(); auto* key_server_opera_button = - new QPushButton(_("Key Server Operation (Pubkey)")); + new QPushButton(tr("Key Server Operation (Pubkey)")); key_server_opera_button->setStyleSheet("text-align:center;"); key_server_opera_button->setMenu(key_server_opera_menu_); key_server_opera_button->setDisabled(forbid_all_gnupg_connection); @@ -96,18 +99,20 @@ KeyPairOperaTab::KeyPairOperaTab(const std::string& key_id, QWidget* parent) if (m_key_.IsPrivateKey() && m_key_.IsHasMasterKey()) { auto* revoke_cert_gen_button = - new QPushButton(_("Generate Revoke Certificate")); + new QPushButton(tr("Generate Revoke Certificate")); connect(revoke_cert_gen_button, &QPushButton::clicked, this, &KeyPairOperaTab::slot_gen_revoke_cert); advance_h_box_layout->addWidget(revoke_cert_gen_button); } - auto* modify_tofu_button = new QPushButton(_("Modify TOFU Policy")); + auto* modify_tofu_button = new QPushButton(tr("Modify TOFU Policy")); + // do not show, useless + modify_tofu_button->setHidden(true); connect(modify_tofu_button, &QPushButton::clicked, this, &KeyPairOperaTab::slot_modify_tofu_policy); auto* set_owner_trust_level_button = - new QPushButton(_("Set Owner Trust Level")); + new QPushButton(tr("Set Owner Trust Level")); connect(set_owner_trust_level_button, &QPushButton::clicked, this, &KeyPairOperaTab::slot_set_owner_trust_level); @@ -123,53 +128,56 @@ KeyPairOperaTab::KeyPairOperaTab(const std::string& key_id, QWidget* parent) // set up signal connect(this, &KeyPairOperaTab::SignalKeyDatabaseRefresh, - SignalStation::GetInstance(), - &SignalStation::SignalKeyDatabaseRefresh); + UISignalStation::GetInstance(), + &UISignalStation::SignalKeyDatabaseRefresh); } void KeyPairOperaTab::CreateOperaMenu() { key_server_opera_menu_ = new QMenu(this); - auto* uploadKeyPair = new QAction(_("Upload Key Pair to Key Server"), this); - connect(uploadKeyPair, &QAction::triggered, this, + auto* upload_key_pair = + new QAction(tr("Upload Key Pair to Key Server"), this); + connect(upload_key_pair, &QAction::triggered, this, &KeyPairOperaTab::slot_upload_key_to_server); - if (!(m_key_.IsPrivateKey() && m_key_.IsHasMasterKey())) - uploadKeyPair->setDisabled(true); + if (!(m_key_.IsPrivateKey() && m_key_.IsHasMasterKey())) { + upload_key_pair->setDisabled(true); + } - auto* updateKeyPair = new QAction(_("Sync Key Pair From Key Server"), this); - connect(updateKeyPair, &QAction::triggered, this, + auto* update_key_pair = + new QAction(tr("Sync Key Pair From Key Server"), this); + connect(update_key_pair, &QAction::triggered, this, &KeyPairOperaTab::slot_update_key_from_server); // when a key has primary key, it should always upload to keyserver. if (m_key_.IsHasMasterKey()) { - updateKeyPair->setDisabled(true); + update_key_pair->setDisabled(true); } - key_server_opera_menu_->addAction(uploadKeyPair); - key_server_opera_menu_->addAction(updateKeyPair); + key_server_opera_menu_->addAction(upload_key_pair); + key_server_opera_menu_->addAction(update_key_pair); 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(tr("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 = - new QAction(_("Export Shortest Secret Key"), this); - connect(exportShortestSecretKey, &QAction::triggered, this, + auto* export_shortest_secret_key = + new QAction(tr("Export Shortest Secret Key"), 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; } @@ -183,18 +191,15 @@ void KeyPairOperaTab::slot_export_public_key() { #endif std::replace(file_string.begin(), file_string.end(), ' ', '_'); - auto file_name = - QFileDialog::getSaveFileName( - this, _("Export Key To File"), QString::fromStdString(file_string), - QString(_("Key Files")) + " (*.asc *.txt);;All Files (*)") - .toStdString(); + auto file_name = QFileDialog::getSaveFileName( + this, tr("Export Key To File"), file_string, + tr("Key Files") + " (*.asc *.txt);;All Files (*)"); - if (file_name.empty()) return; + if (file_name.isEmpty()) return; - if (!write_buffer_to_file(file_name, *keyArray)) { - QMessageBox::critical( - this, _("Export Error"), - QString(_("Couldn't open %1 for writing")).arg(file_name.c_str())); + if (!WriteFileGFBuffer(file_name, gf_buffer)) { + QMessageBox::critical(this, tr("Export Error"), + tr("Couldn't open %1 for writing").arg(file_name)); return; } } @@ -202,26 +207,23 @@ void KeyPairOperaTab::slot_export_public_key() { void KeyPairOperaTab::slot_export_short_private_key() { // Show a information box with explanation about private key int ret = QMessageBox::information( - this, _("Exporting short private Key"), - "<h3>" + QString(_("You are about to export your")) + - "<font color=\"red\">" + _(" PRIVATE KEY ") + "</font>!</h3>\n" + - _("This is NOT your Public Key, so DON'T give it away.") + "<br />" + - _("Do you REALLY want to export your PRIVATE KEY in a Minimum " - "Size?") + + this, tr("Exporting short private Key"), + "<h3>" + tr("You are about to export your") + "<font color=\"red\">" + + tr(" PRIVATE KEY ") + "</font>!</h3>\n" + + tr("This is NOT your Public Key, so DON'T give it away.") + "<br />" + + tr("Do you REALLY want to export your PRIVATE KEY in a Minimum " + "Size?") + "<br />" + - _("For OpenPGP keys it removes all signatures except for the latest " - "self-signatures."), + tr("For OpenPGP keys it removes all signatures except for the latest " + "self-signatures."), QMessageBox::Cancel | QMessageBox::Ok); // 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; } @@ -235,18 +237,15 @@ void KeyPairOperaTab::slot_export_short_private_key() { #endif std::replace(file_string.begin(), file_string.end(), ' ', '_'); - auto file_name = - QFileDialog::getSaveFileName( - this, _("Export Key To File"), QString::fromStdString(file_string), - QString(_("Key Files")) + " (*.asc *.txt);;All Files (*)") - .toStdString(); + auto file_name = QFileDialog::getSaveFileName( + this, tr("Export Key To File"), file_string, + tr("Key Files") + " (*.asc *.txt);;All Files (*)"); - if (file_name.empty()) return; + if (file_name.isEmpty()) return; - if (!write_buffer_to_file(file_name, *keyArray)) { - QMessageBox::critical( - this, _("Export Error"), - QString(_("Couldn't open %1 for writing")).arg(file_name.c_str())); + if (!WriteFileGFBuffer(file_name, gf_buffer)) { + QMessageBox::critical(this, tr("Export Error"), + tr("Couldn't open %1 for writing").arg(file_name)); return; } } @@ -255,22 +254,19 @@ void KeyPairOperaTab::slot_export_short_private_key() { void KeyPairOperaTab::slot_export_private_key() { // Show a information box with explanation about private key int ret = QMessageBox::information( - this, _("Exporting private Key"), - "<h3>" + QString(_("You are about to export your")) + - "<font color=\"red\">" + _(" PRIVATE KEY ") + "</font>!</h3>\n" + - _("This is NOT your Public Key, so DON'T give it away.") + "<br />" + - _("Do you REALLY want to export your PRIVATE KEY?"), + this, tr("Exporting private Key"), + "<h3>" + tr("You are about to export your") + "<font color=\"red\">" + + tr(" PRIVATE KEY ") + "</font>!</h3>\n" + + tr("This is NOT your Public Key, so DON'T give it away.") + "<br />" + + tr("Do you REALLY want to export your PRIVATE KEY?"), QMessageBox::Cancel | QMessageBox::Ok); // 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; } @@ -284,25 +280,22 @@ void KeyPairOperaTab::slot_export_private_key() { #endif std::replace(file_string.begin(), file_string.end(), ' ', '_'); - auto file_name = - QFileDialog::getSaveFileName( - this, _("Export Key To File"), QString::fromStdString(file_string), - QString(_("Key Files")) + " (*.asc *.txt);;All Files (*)") - .toStdString(); + auto file_name = QFileDialog::getSaveFileName( + this, tr("Export Key To File"), file_string, + tr("Key Files") + " (*.asc *.txt);;All Files (*)"); - if (file_name.empty()) return; + if (file_name.isEmpty()) return; - if (!write_buffer_to_file(file_name, *keyArray)) { - QMessageBox::critical( - this, _("Export Error"), - QString(_("Couldn't open %1 for writing")).arg(file_name.c_str())); + if (!WriteFileGFBuffer(file_name, gf_buffer)) { + QMessageBox::critical(this, tr("Export Error"), + tr("Couldn't open %1 for writing").arg(file_name)); return; } } } void KeyPairOperaTab::slot_modify_edit_datetime() { - auto dialog = new KeySetExpireDateDialog(m_key_.GetId(), this); + auto* dialog = new KeySetExpireDateDialog(m_key_.GetId(), this); dialog->show(); } @@ -315,114 +308,75 @@ void KeyPairOperaTab::slot_upload_key_to_server() { } void KeyPairOperaTab::slot_update_key_from_server() { - auto keys = std::make_unique<KeyIdArgsList>(); - keys->push_back(m_key_.GetId()); - auto* dialog = new KeyServerImportDialog(this); - dialog->show(); - dialog->SlotImport(keys); + CommonUtils::GetInstance()->ImportKeyFromKeyServer({m_key_.GetId()}); } void KeyPairOperaTab::slot_gen_revoke_cert() { - auto literal = QString("%1 (*.rev)").arg(_("Revocation Certificates")); + auto literal = QString("%1 (*.rev)").arg(tr("Revocation Certificates")); QString m_output_file_name; - QFileDialog dialog(this, "Generate revocation certificate", QString(), +#ifndef WINDOWS + auto file_string = m_key_.GetName() + "<" + m_key_.GetEmail() + ">(" + + m_key_.GetId() + ").rev"; +#else + auto file_string = m_key_.GetName() + "[" + m_key_.GetEmail() + "](" + + m_key_.GetId() + ").rev"; +#endif + + QFileDialog dialog(this, tr("Generate revocation certificate"), file_string, literal); dialog.setDefaultSuffix(".rev"); dialog.setAcceptMode(QFileDialog::AcceptSave); - if (dialog.exec()) m_output_file_name = dialog.selectedFiles().front(); + if (dialog.exec() != 0) m_output_file_name = dialog.selectedFiles().front(); if (!m_output_file_name.isEmpty()) { - GpgKeyOpera::GetInstance().GenerateRevokeCert( - m_key_, m_output_file_name.toStdString()); + GpgKeyOpera::GetInstance().GenerateRevokeCert(m_key_, m_output_file_name); } } void KeyPairOperaTab::slot_modify_password() { - auto err = GpgKeyOpera::GetInstance().ModifyPassword(m_key_); - if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) { - QMessageBox::critical(this, _("Not Successful"), - QString(_("Modify password not successfully."))); - } + GpgKeyOpera::GetInstance().ModifyPassword( + m_key_, [this](GpgError err, const DataObjectPtr&) { + CommonUtils::RaiseMessageBox(this, err); + }); } void KeyPairOperaTab::slot_modify_tofu_policy() { QStringList items; - items << _("Policy Auto") << _("Policy Good") << _("Policy Bad") - << _("Policy Ask") << _("Policy Unknown"); + items << tr("Policy Auto") << tr("Policy Good") << tr("Policy Bad") + << tr("Policy Ask") << tr("Policy Unknown"); bool ok; QString item = QInputDialog::getItem( - this, _("Modify TOFU Policy(Default is Auto)"), - _("Policy for the Key Pair:"), items, 0, false, &ok); + this, tr("Modify TOFU Policy(Default is Auto)"), + tr("Policy for the Key Pair:"), items, 0, false, &ok); if (ok && !item.isEmpty()) { - SPDLOG_DEBUG("selected policy: {}", item.toStdString()); + GF_UI_LOG_DEBUG("selected policy: {}", item.toStdString()); gpgme_tofu_policy_t tofu_policy = GPGME_TOFU_POLICY_AUTO; - if (item == _("Policy Auto")) { + if (item == tr("Policy Auto")) { tofu_policy = GPGME_TOFU_POLICY_AUTO; - } else if (item == _("Policy Good")) { + } else if (item == tr("Policy Good")) { tofu_policy = GPGME_TOFU_POLICY_GOOD; - } else if (item == _("Policy Bad")) { + } else if (item == tr("Policy Bad")) { tofu_policy = GPGME_TOFU_POLICY_BAD; - } else if (item == _("Policy Ask")) { + } else if (item == tr("Policy Ask")) { tofu_policy = GPGME_TOFU_POLICY_ASK; - } else if (item == _("Policy Unknown")) { + } else if (item == tr("Policy Unknown")) { tofu_policy = GPGME_TOFU_POLICY_UNKNOWN; } auto err = GpgKeyOpera::GetInstance().ModifyTOFUPolicy(m_key_, tofu_policy); - if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) { - QMessageBox::critical(this, _("Not Successful"), - QString(_("Modify TOFU policy not successfully."))); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + QMessageBox::critical(this, tr("Not Successful"), + tr("Modify TOFU policy not successfully.")); } } } void KeyPairOperaTab::slot_set_owner_trust_level() { - QStringList items; - - items << _("Unknown") << _("Undefined") << _("Never") << _("Marginal") - << _("Full") << _("Ultimate"); - bool ok; - QString item = QInputDialog::getItem(this, _("Modify Owner Trust Level"), - _("Trust for the Key Pair:"), items, - m_key_.GetOwnerTrustLevel(), false, &ok); - - if (ok && !item.isEmpty()) { - SPDLOG_DEBUG("selected policy: {}", item.toStdString()); - int trust_level = 0; // Unknown Level - if (item == _("Ultimate")) { - trust_level = 5; - } else if (item == _("Full")) { - trust_level = 4; - } else if (item == _("Marginal")) { - trust_level = 3; - } else if (item == _("Never")) { - trust_level = 2; - } else if (item == _("Undefined")) { - trust_level = 1; - } - - if (trust_level == 0) { - QMessageBox::warning( - this, _("Warning"), - QString(_("Owner Trust Level cannot set to Unknown level, automately " - "changing it into Undefined level."))); - trust_level = 1; - } - - bool status = - GpgKeyManager::GetInstance().SetOwnerTrustLevel(m_key_, trust_level); - if (!status) { - QMessageBox::critical(this, _("Failed"), - QString(_("Modify Owner Trust Level failed."))); - } else { - QMessageBox::information(this, _("Success"), - QString(_("Set Owner Trust Level successful."))); - // update key database and refresh ui - emit SignalKeyDatabaseRefresh(); - } - } + auto* function = new SetOwnerTrustLevel(this); + function->Exec(m_key_.GetId()); + function->deleteLater(); } } // namespace GpgFrontend::UI |