diff options
Diffstat (limited to 'src/ui/KeyMgmt.cpp')
-rwxr-xr-x | src/ui/KeyMgmt.cpp | 401 |
1 files changed, 196 insertions, 205 deletions
diff --git a/src/ui/KeyMgmt.cpp b/src/ui/KeyMgmt.cpp index 2c23d1bf..57d8ba1f 100755 --- a/src/ui/KeyMgmt.cpp +++ b/src/ui/KeyMgmt.cpp @@ -1,4 +1,6 @@ /** + * Copyright (C) 2021 Saturneric + * * This file is part of GpgFrontend. * * GpgFrontend is free software: you can redistribute it and/or modify @@ -6,19 +8,21 @@ * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * Foobar is distributed in the hope that it will be useful, + * 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * 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. * - * The initial version of the source code is inherited from gpg4usb-team. - * Their source code version also complies with GNU General Public License. + * All the source code of GpgFrontend was modified and released by + * Saturneric<[email protected]> starting on May 12, 2021. * - * The source code version of this software was modified and released - * by Saturneric<[email protected]> starting on May 12, 2021. + * SPDX-License-Identifier: GPL-3.0-or-later * */ @@ -26,23 +30,25 @@ #include <utility> -#include "gpg/function/GpgKeyGetter.h" -#include "gpg/function/GpgKeyImportExporter.h" -#include "gpg/function/GpgKeyOpera.h" +#include "core/function/gpg/GpgKeyGetter.h" +#include "core/function/gpg/GpgKeyImportExporter.h" +#include "core/function/gpg/GpgKeyOpera.h" +#include "core/function/KeyPackageOperator.h" #include "ui/SignalStation.h" #include "ui/UserInterfaceUtils.h" -#include "ui/keygen/SubkeyGenerateDialog.h" -#include "ui/settings/GlobalSettingStation.h" -#include "ui/widgets/ExportKeyPackageDialog.h" +#include "ui/import_export/ExportKeyPackageDialog.h" +#include "ui/key_generate/SubkeyGenerateDialog.h" +#include "ui/main_window/MainWindow.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { KeyMgmt::KeyMgmt(QWidget* parent) : QMainWindow(parent) { /* the list of Keys available*/ key_list_ = new KeyList(KeyMenuAbility::ALL, this); - key_list_->addListGroupTab(_("All"), KeyListRow::SECRET_OR_PUBLIC_KEY); + key_list_->AddListGroupTab(_("All"), KeyListRow::SECRET_OR_PUBLIC_KEY); - key_list_->addListGroupTab( + key_list_->AddListGroupTab( _("Only Public Key"), KeyListRow::SECRET_OR_PUBLIC_KEY, KeyListColumn::TYPE | KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage | KeyListColumn::Validity, @@ -51,7 +57,7 @@ KeyMgmt::KeyMgmt(QWidget* parent) : QMainWindow(parent) { !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); }); - key_list_->addListGroupTab( + key_list_->AddListGroupTab( _("Has Private Key"), KeyListRow::SECRET_OR_PUBLIC_KEY, KeyListColumn::TYPE | KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage | KeyListColumn::Validity, @@ -60,7 +66,7 @@ KeyMgmt::KeyMgmt(QWidget* parent) : QMainWindow(parent) { !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); }); - key_list_->addListGroupTab( + key_list_->AddListGroupTab( _("No Primary Key"), KeyListRow::SECRET_OR_PUBLIC_KEY, KeyListColumn::TYPE | KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage | KeyListColumn::Validity, @@ -69,30 +75,32 @@ KeyMgmt::KeyMgmt(QWidget* parent) : QMainWindow(parent) { !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); }); - key_list_->addListGroupTab( + key_list_->AddListGroupTab( _("Revoked"), KeyListRow::SECRET_OR_PUBLIC_KEY, KeyListColumn::TYPE | KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage | KeyListColumn::Validity, [](const GpgKey& key) -> bool { return key.IsRevoked(); }); - key_list_->addListGroupTab( + key_list_->AddListGroupTab( _("Expired"), KeyListRow::SECRET_OR_PUBLIC_KEY, KeyListColumn::TYPE | KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage | KeyListColumn::Validity, [](const GpgKey& key) -> bool { return key.IsExpired(); }); setCentralWidget(key_list_); - key_list_->setDoubleClickedAction([this](const GpgKey& key, QWidget* parent) { + key_list_->SetDoubleClickedAction([this](const GpgKey& key, QWidget* parent) { new KeyDetailsDialog(key, parent); }); - key_list_->slotRefresh(); + key_list_->SlotRefresh(); + + create_actions(); + create_menus(); + create_tool_bars(); - createActions(); - createMenus(); - createToolBars(); - connect(this, SIGNAL(signalStatusBarChanged(QString)), this->parent(), - SLOT(slotSetStatusBarText(QString))); + connect(this, &KeyMgmt::SignalStatusBarChanged, + qobject_cast<MainWindow*>(this->parent()), + &MainWindow::SlotSetStatusBarText); auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); @@ -146,137 +154,143 @@ KeyMgmt::KeyMgmt(QWidget* parent) : QMainWindow(parent) { this->statusBar()->show(); setWindowTitle(_("KeyPair Management")); - key_list_->addMenuAction(deleteSelectedKeysAct); - key_list_->addMenuAction(showKeyDetailsAct); + key_list_->AddMenuAction(delete_selected_keys_act_); + key_list_->AddMenuAction(show_key_details_act_); - connect(this, SIGNAL(signalKeyStatusUpdated()), SignalStation::GetInstance(), - SIGNAL(KeyDatabaseRefresh())); - connect(SignalStation::GetInstance(), &SignalStation::signalRefreshStatusBar, + connect(this, &KeyMgmt::SignalKeyStatusUpdated, SignalStation::GetInstance(), + &SignalStation::SignalKeyDatabaseRefresh); + connect(SignalStation::GetInstance(), &SignalStation::SignalRefreshStatusBar, this, [=](const QString& message, int timeout) { statusBar()->showMessage(message, timeout); }); } -void KeyMgmt::createActions() { - openKeyFileAct = new QAction(_("Open"), this); - openKeyFileAct->setShortcut(QKeySequence(_("Ctrl+O"))); - openKeyFileAct->setToolTip(_("Open Key File")); - connect(importKeyFromFileAct, &QAction::triggered, this, - [&]() { CommonUtils::GetInstance()->slotImportKeyFromFile(this); }); - - closeAct = new QAction(_("Close"), this); - closeAct->setShortcut(QKeySequence(_("Ctrl+Q"))); - closeAct->setIcon(QIcon(":exit.png")); - closeAct->setToolTip(_("Close")); - connect(closeAct, SIGNAL(triggered()), this, SLOT(close())); - - generateKeyPairAct = new QAction(_("New Keypair"), this); - generateKeyPairAct->setShortcut(QKeySequence(_("Ctrl+N"))); - generateKeyPairAct->setIcon(QIcon(":key_generate.png")); - generateKeyPairAct->setToolTip(_("Generate KeyPair")); - connect(generateKeyPairAct, SIGNAL(triggered()), this, - SLOT(slotGenerateKeyDialog())); - - generateSubKeyAct = new QAction(_("New Subkey"), this); - generateSubKeyAct->setShortcut(QKeySequence(_("Ctrl+Shift+N"))); - generateSubKeyAct->setIcon(QIcon(":key_generate.png")); - generateSubKeyAct->setToolTip(_("Generate Subkey For Selected KeyPair")); - connect(generateSubKeyAct, SIGNAL(triggered()), this, - SLOT(slotGenerateSubKey())); - - importKeyFromFileAct = new QAction(_("File"), this); - importKeyFromFileAct->setIcon(QIcon(":import_key_from_file.png")); - importKeyFromFileAct->setToolTip(_("Import New Key From File")); - connect(importKeyFromFileAct, &QAction::triggered, this, - [&]() { CommonUtils::GetInstance()->slotImportKeyFromFile(this); }); - - importKeyFromClipboardAct = new QAction(_("Clipboard"), this); - importKeyFromClipboardAct->setIcon(QIcon(":import_key_from_clipboard.png")); - importKeyFromClipboardAct->setToolTip(_("Import New Key From Clipboard")); - connect(importKeyFromClipboardAct, &QAction::triggered, this, [&]() { - CommonUtils::GetInstance()->slotImportKeyFromClipboard(this); +void KeyMgmt::create_actions() { + open_key_file_act_ = new QAction(_("Open"), this); + open_key_file_act_->setShortcut(QKeySequence(_("Ctrl+O"))); + open_key_file_act_->setToolTip(_("Open Key File")); + connect(open_key_file_act_, &QAction::triggered, this, + [&]() { CommonUtils::GetInstance()->SlotImportKeyFromFile(this); }); + + close_act_ = new QAction(_("Close"), this); + close_act_->setShortcut(QKeySequence(_("Ctrl+Q"))); + close_act_->setIcon(QIcon(":exit.png")); + close_act_->setToolTip(_("Close")); + connect(close_act_, &QAction::triggered, this, &KeyMgmt::close); + + generate_key_pair_act_ = new QAction(_("New Keypair"), this); + generate_key_pair_act_->setShortcut(QKeySequence(_("Ctrl+N"))); + generate_key_pair_act_->setIcon(QIcon(":key_generate.png")); + generate_key_pair_act_->setToolTip(_("Generate KeyPair")); + connect(generate_key_pair_act_, &QAction::triggered, this, + &KeyMgmt::SlotGenerateKeyDialog); + + generate_subkey_act_ = new QAction(_("New Subkey"), this); + generate_subkey_act_->setShortcut(QKeySequence(_("Ctrl+Shift+N"))); + generate_subkey_act_->setIcon(QIcon(":key_generate.png")); + generate_subkey_act_->setToolTip(_("Generate Subkey For Selected KeyPair")); + connect(generate_subkey_act_, &QAction::triggered, this, + &KeyMgmt::SlotGenerateSubKey); + + import_key_from_file_act_ = new QAction(_("File"), this); + import_key_from_file_act_->setIcon(QIcon(":import_key_from_file.png")); + import_key_from_file_act_->setToolTip(_("Import New Key From File")); + connect(import_key_from_file_act_, &QAction::triggered, this, + [&]() { CommonUtils::GetInstance()->SlotImportKeyFromFile(this); }); + + import_key_from_clipboard_act_ = new QAction(_("Clipboard"), this); + import_key_from_clipboard_act_->setIcon( + QIcon(":import_key_from_clipboard.png")); + import_key_from_clipboard_act_->setToolTip( + _("Import New Key From Clipboard")); + connect(import_key_from_clipboard_act_, &QAction::triggered, this, [&]() { + CommonUtils::GetInstance()->SlotImportKeyFromClipboard(this); }); - importKeyFromKeyServerAct = new QAction(_("Keyserver"), this); - importKeyFromKeyServerAct->setIcon(QIcon(":import_key_from_server.png")); - importKeyFromKeyServerAct->setToolTip(_("Import New Key From Keyserver")); - connect(importKeyFromKeyServerAct, &QAction::triggered, this, [&]() { - CommonUtils::GetInstance()->slotImportKeyFromKeyServer(this); + import_key_from_key_server_act_ = new QAction(_("Keyserver"), this); + import_key_from_key_server_act_->setIcon( + QIcon(":import_key_from_server.png")); + import_key_from_key_server_act_->setToolTip( + _("Import New Key From Keyserver")); + connect(import_key_from_key_server_act_, &QAction::triggered, this, [&]() { + CommonUtils::GetInstance()->SlotImportKeyFromKeyServer(this); }); - importKeysFromKeyPackageAct = new QAction(_("Key Package"), this); - importKeysFromKeyPackageAct->setIcon(QIcon(":key_package.png")); - importKeysFromKeyPackageAct->setToolTip( + import_keys_from_key_package_act_ = new QAction(_("Key Package"), this); + import_keys_from_key_package_act_->setIcon(QIcon(":key_package.png")); + import_keys_from_key_package_act_->setToolTip( _("Import Key(s) From a Key Package")); - connect(importKeysFromKeyPackageAct, &QAction::triggered, this, - &KeyMgmt::slotImportKeyPackage); - - exportKeyToClipboardAct = new QAction(_("Export To Clipboard"), this); - exportKeyToClipboardAct->setIcon(QIcon(":export_key_to_clipboard.png")); - exportKeyToClipboardAct->setToolTip(_("Export Selected Key(s) To Clipboard")); - connect(exportKeyToClipboardAct, SIGNAL(triggered()), this, - SLOT(slotExportKeyToClipboard())); - - exportKeyToFileAct = new QAction(_("Export To Key Package"), this); - exportKeyToFileAct->setIcon(QIcon(":key_package.png")); - exportKeyToFileAct->setToolTip(_("Export Checked Key(s) To a Key Package")); - connect(exportKeyToFileAct, SIGNAL(triggered()), this, - SLOT(slotExportKeyToKeyPackage())); - - exportKeyAsOpenSSHFormat = new QAction(_("Export As OpenSSH"), this); - exportKeyAsOpenSSHFormat->setIcon(QIcon(":ssh-key.png")); - exportKeyAsOpenSSHFormat->setToolTip( + connect(import_keys_from_key_package_act_, &QAction::triggered, this, + &KeyMgmt::SlotImportKeyPackage); + + 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")); + 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_->setIcon(QIcon(":key_package.png")); + export_key_to_file_act_->setToolTip( + _("Export Checked Key(s) To a Key Package")); + connect(export_key_to_file_act_, &QAction::triggered, this, + &KeyMgmt::SlotExportKeyToKeyPackage); + + 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")); - connect(exportKeyAsOpenSSHFormat, SIGNAL(triggered()), this, - SLOT(slotExportAsOpenSSHFormat())); - - deleteSelectedKeysAct = new QAction(_("Delete Selected Key(s)"), this); - deleteSelectedKeysAct->setToolTip(_("Delete the Selected keys")); - connect(deleteSelectedKeysAct, SIGNAL(triggered()), this, - SLOT(slotDeleteSelectedKeys())); - - deleteCheckedKeysAct = new QAction(_("Delete Checked Key(s)"), this); - deleteCheckedKeysAct->setToolTip(_("Delete the Checked keys")); - deleteCheckedKeysAct->setIcon(QIcon(":button_delete.png")); - connect(deleteCheckedKeysAct, SIGNAL(triggered()), this, - SLOT(slotDeleteCheckedKeys())); - - showKeyDetailsAct = new QAction(_("Show Key Details"), this); - showKeyDetailsAct->setToolTip(_("Show Details for this Key")); - connect(showKeyDetailsAct, SIGNAL(triggered()), this, - SLOT(slotShowKeyDetails())); + connect(export_key_as_open_ssh_format_, &QAction::triggered, this, + &KeyMgmt::SlotExportAsOpenSSHFormat); + + delete_selected_keys_act_ = new QAction(_("Delete Selected Key(s)"), this); + delete_selected_keys_act_->setToolTip(_("Delete the Selected keys")); + connect(delete_selected_keys_act_, &QAction::triggered, this, + &KeyMgmt::SlotDeleteSelectedKeys); + + delete_checked_keys_act_ = new QAction(_("Delete Checked Key(s)"), this); + delete_checked_keys_act_->setToolTip(_("Delete the Checked keys")); + delete_checked_keys_act_->setIcon(QIcon(":button_delete.png")); + connect(delete_checked_keys_act_, &QAction::triggered, this, + &KeyMgmt::SlotDeleteCheckedKeys); + + show_key_details_act_ = new QAction(_("Show Key Details"), this); + show_key_details_act_->setToolTip(_("Show Details for this Key")); + connect(show_key_details_act_, &QAction::triggered, this, + &KeyMgmt::SlotShowKeyDetails); } -void KeyMgmt::createMenus() { - fileMenu = menuBar()->addMenu(_("File")); - fileMenu->addAction(openKeyFileAct); - fileMenu->addAction(closeAct); - - keyMenu = menuBar()->addMenu(_("Key")); - generateKeyMenu = keyMenu->addMenu(_("Generate Key")); - generateKeyMenu->addAction(generateKeyPairAct); - generateKeyMenu->addAction(generateSubKeyAct); - - importKeyMenu = keyMenu->addMenu(_("Import Key")); - importKeyMenu->addAction(importKeyFromFileAct); - importKeyMenu->addAction(importKeyFromClipboardAct); - importKeyMenu->addAction(importKeyFromKeyServerAct); - importKeyMenu->addAction(importKeysFromKeyPackageAct); - - keyMenu->addAction(exportKeyToFileAct); - keyMenu->addAction(exportKeyToClipboardAct); - keyMenu->addAction(exportKeyAsOpenSSHFormat); - keyMenu->addSeparator(); - keyMenu->addAction(deleteCheckedKeysAct); +void KeyMgmt::create_menus() { + file_menu_ = menuBar()->addMenu(_("File")); + file_menu_->addAction(open_key_file_act_); + file_menu_->addAction(close_act_); + + key_menu_ = menuBar()->addMenu(_("Key")); + generate_key_menu_ = key_menu_->addMenu(_("Generate Key")); + generate_key_menu_->addAction(generate_key_pair_act_); + generate_key_menu_->addAction(generate_subkey_act_); + + import_key_menu_ = key_menu_->addMenu(_("Import Key")); + import_key_menu_->addAction(import_key_from_file_act_); + import_key_menu_->addAction(import_key_from_clipboard_act_); + 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_); + key_menu_->addSeparator(); + key_menu_->addAction(delete_checked_keys_act_); } -void KeyMgmt::createToolBars() { +void KeyMgmt::create_tool_bars() { QToolBar* keyToolBar = addToolBar(_("Key")); keyToolBar->setObjectName("keytoolbar"); // add button with popup menu for import auto* generateToolButton = new QToolButton(this); - generateToolButton->setMenu(generateKeyMenu); + generateToolButton->setMenu(generate_key_menu_); generateToolButton->setPopupMode(QToolButton::InstantPopup); generateToolButton->setIcon(QIcon(":key_generate.png")); generateToolButton->setText(_("Generate")); @@ -286,7 +300,7 @@ void KeyMgmt::createToolBars() { // add button with popup menu for import auto* toolButton = new QToolButton(this); - toolButton->setMenu(importKeyMenu); + toolButton->setMenu(import_key_menu_); toolButton->setPopupMode(QToolButton::InstantPopup); toolButton->setIcon(QIcon(":key_import.png")); toolButton->setToolTip(_("Import key")); @@ -295,30 +309,30 @@ void KeyMgmt::createToolBars() { keyToolBar->addWidget(toolButton); keyToolBar->addSeparator(); - keyToolBar->addAction(deleteCheckedKeysAct); + keyToolBar->addAction(delete_checked_keys_act_); keyToolBar->addSeparator(); - keyToolBar->addAction(exportKeyToFileAct); - keyToolBar->addAction(exportKeyToClipboardAct); - keyToolBar->addAction(exportKeyAsOpenSSHFormat); + keyToolBar->addAction(export_key_to_file_act_); + keyToolBar->addAction(export_key_to_clipboard_act_); + keyToolBar->addAction(export_key_as_open_ssh_format_); } -void KeyMgmt::slotDeleteSelectedKeys() { - deleteKeysWithWarning(key_list_->getSelected()); +void KeyMgmt::SlotDeleteSelectedKeys() { + delete_keys_with_warning(key_list_->GetSelected()); } -void KeyMgmt::slotDeleteCheckedKeys() { - deleteKeysWithWarning(key_list_->getChecked()); +void KeyMgmt::SlotDeleteCheckedKeys() { + delete_keys_with_warning(key_list_->GetChecked()); } -void KeyMgmt::deleteKeysWithWarning(KeyIdArgsListPtr key_ids) { +void KeyMgmt::delete_keys_with_warning(KeyIdArgsListPtr uidList) { /** * TODO: Different Messages for private/public key, check if * more than one selected... compare to seahorse "delete-dialog" */ - if (key_ids->empty()) return; + if (uidList->empty()) return; QString keynames; - for (const auto& key_id : *key_ids) { + for (const auto& key_id : *uidList) { auto key = GpgKeyGetter::GetInstance().GetKey(key_id); if (!key.IsGood()) continue; keynames.append(QString::fromStdString(key.GetName())); @@ -337,13 +351,13 @@ void KeyMgmt::deleteKeysWithWarning(KeyIdArgsListPtr key_ids) { QMessageBox::No | QMessageBox::Yes); if (ret == QMessageBox::Yes) { - GpgKeyOpera::GetInstance().DeleteKeys(std::move(key_ids)); - emit signalKeyStatusUpdated(); + GpgKeyOpera::GetInstance().DeleteKeys(std::move(uidList)); + emit SignalKeyStatusUpdated(); } } -void KeyMgmt::slotShowKeyDetails() { - auto keys_selected = key_list_->getSelected(); +void KeyMgmt::SlotShowKeyDetails() { + auto keys_selected = key_list_->GetSelected(); if (keys_selected->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(keys_selected->front()); @@ -356,8 +370,8 @@ void KeyMgmt::slotShowKeyDetails() { new KeyDetailsDialog(key); } -void KeyMgmt::slotExportKeyToKeyPackage() { - auto keys_checked = key_list_->getChecked(); +void KeyMgmt::SlotExportKeyToKeyPackage() { + auto keys_checked = key_list_->GetChecked(); if (keys_checked->empty()) { QMessageBox::critical( this, _("Forbidden"), @@ -366,11 +380,11 @@ void KeyMgmt::slotExportKeyToKeyPackage() { } auto dialog = new ExportKeyPackageDialog(std::move(keys_checked), this); dialog->exec(); - emit signalStatusBarChanged(QString(_("key(s) exported"))); + emit SignalStatusBarChanged(QString(_("key(s) exported"))); } -void KeyMgmt::slotExportKeyToClipboard() { - auto keys_checked = key_list_->getChecked(); +void KeyMgmt::SlotExportKeyToClipboard() { + auto keys_checked = key_list_->GetChecked(); if (keys_checked->empty()) { QMessageBox::critical( this, _("Forbidden"), @@ -386,18 +400,18 @@ void KeyMgmt::slotExportKeyToClipboard() { QApplication::clipboard()->setText(QString::fromStdString(*key_export_data)); } -void KeyMgmt::slotGenerateKeyDialog() { +void KeyMgmt::SlotGenerateKeyDialog() { auto* keyGenDialog = new KeyGenDialog(this); keyGenDialog->show(); } void KeyMgmt::closeEvent(QCloseEvent* event) { - slotSaveWindowState(); + SlotSaveWindowState(); QMainWindow::closeEvent(event); } -void KeyMgmt::slotGenerateSubKey() { - auto keys_selected = key_list_->getSelected(); +void KeyMgmt::SlotGenerateSubKey() { + auto keys_selected = key_list_->GetSelected(); if (keys_selected->empty()) { QMessageBox::information( this, _("Invalid Operation"), @@ -419,9 +433,9 @@ void KeyMgmt::slotGenerateSubKey() { auto dialog = new SubkeyGenerateDialog(key.GetId(), this); dialog->show(); } -void KeyMgmt::slotSaveWindowState() { +void KeyMgmt::SlotSaveWindowState() { auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("window") || settings.lookup("window").getType() != libconfig::Setting::TypeGroup) @@ -466,9 +480,9 @@ void KeyMgmt::slotSaveWindowState() { GlobalSettingStation::GetInstance().SyncSettings(); } -void KeyMgmt::slotExportAsOpenSSHFormat() { +void KeyMgmt::SlotExportAsOpenSSHFormat() { ByteArrayPtr key_export_data = nullptr; - auto keys_checked = key_list_->getChecked(); + auto keys_checked = key_list_->GetChecked(); if (keys_checked->empty()) { QMessageBox::critical( @@ -507,63 +521,40 @@ void KeyMgmt::slotExportAsOpenSSHFormat() { if (!file_name.isEmpty()) { write_buffer_to_file(file_name.toStdString(), *key_export_data); - emit signalStatusBarChanged(QString(_("key(s) exported"))); + emit SignalStatusBarChanged(QString(_("key(s) exported"))); } } -void KeyMgmt::slotImportKeyPackage() { +void KeyMgmt::SlotImportKeyPackage() { + + LOG(INFO) << "Importing key package..."; + auto key_package_file_name = QFileDialog::getOpenFileName( this, _("Import Key Package"), {}, QString(_("Key Package")) + " (*.gfepack);;All Files (*)"); - if (key_package_file_name.isEmpty()) return; - - auto encrypted_data = - read_all_data_in_file(key_package_file_name.toStdString()); - - if (encrypted_data.empty()) { - QMessageBox::critical(this, _("Error"), - _("No data was read from the key package.")); - return; - }; - auto key_file_name = QFileDialog::getOpenFileName( this, _("Import Key Package Passphrase File"), {}, QString(_("Key Package Passphrase File")) + " (*.key);;All Files (*)"); - auto passphrase = read_all_data_in_file(key_file_name.toStdString()); - - LOG(INFO) << "passphrase size" << passphrase.size(); - if (passphrase.size() != 256) { - QMessageBox::critical( - this, _("Wrong Passphrase"), - _("Please double check the passphrase you entered is correct.")); + if(key_package_file_name.isEmpty() || key_file_name.isEmpty()) return; - } - auto hash_key = QCryptographicHash::hash( - QByteArray::fromStdString(passphrase), QCryptographicHash::Sha256); - auto encoded = QByteArray::fromStdString(encrypted_data); - - QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, - QAESEncryption::Padding::ISO); - auto decoded = encryption.removePadding(encryption.decode(encoded, hash_key)); - auto key_data = QByteArray::fromBase64(decoded); + GpgImportInformation info; - if (!key_data.startsWith(GpgConstants::PGP_PUBLIC_KEY_BEGIN) && - !key_data.startsWith(GpgConstants::PGP_PRIVATE_KEY_BEGIN)) { - QMessageBox::critical( - this, _("Wrong Passphrase"), - _("Please double check the passphrase you entered is correct.")); - return; - } + LOG(INFO) << "Importing key package: " << key_package_file_name.toStdString(); - auto key_data_ptr = std::make_unique<ByteArray>(key_data.toStdString()); - auto info = - GpgKeyImportExporter::GetInstance().ImportKey(std::move(key_data_ptr)); + if (KeyPackageOperator::ImportKeyPackage(key_package_file_name.toStdString(), + key_file_name.toStdString(), info)) { + emit SignalStatusBarChanged(QString(_("key(s) imported"))); + emit SignalKeyStatusUpdated(); - auto dialog = new KeyImportDetailDialog(info, false, this); - dialog->exec(); + auto dialog = new KeyImportDetailDialog(info, false, this); + dialog->exec(); + } else { + QMessageBox::critical(this, _("Error"), + _("An error occur in importing key package.")); + } } } // namespace GpgFrontend::UI |