diff options
Diffstat (limited to 'src/ui/main_window')
-rw-r--r-- | src/ui/main_window/KeyMgmt.cpp | 197 | ||||
-rw-r--r-- | src/ui/main_window/KeyMgmt.h | 50 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.cpp | 5 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.h | 13 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowGpgOperaFunction.cpp | 49 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 145 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotUI.cpp | 41 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowUI.cpp | 32 |
8 files changed, 268 insertions, 264 deletions
diff --git a/src/ui/main_window/KeyMgmt.cpp b/src/ui/main_window/KeyMgmt.cpp index 59e7a658..cd5a9f05 100644 --- a/src/ui/main_window/KeyMgmt.cpp +++ b/src/ui/main_window/KeyMgmt.cpp @@ -30,7 +30,6 @@ #include "core/function/GlobalSettingStation.h" #include "core/function/KeyPackageOperator.h" -#include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/function/gpg/GpgKeyOpera.h" #include "core/model/GpgImportInformation.h" @@ -43,7 +42,6 @@ #include "ui/dialog/import_export/KeyImportDetailDialog.h" #include "ui/dialog/key_generate/KeyGenerateDialog.h" #include "ui/dialog/key_generate/SubkeyGenerateDialog.h" -#include "ui/dialog/keypair_details/KeyDetailsDialog.h" #include "ui/function/GpgOperaHelper.h" #include "ui/main_window/MainWindow.h" #include "ui/widgets/KeyList.h" @@ -62,25 +60,28 @@ KeyMgmt::KeyMgmt(QWidget* parent) key_list_->AddListGroupTab( tr("Only Public Key"), "only_public_key", - GpgKeyTableDisplayMode::kPUBLIC_KEY, [](const GpgKey& key) -> bool { - return !key.IsPrivateKey() && - !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); + GpgKeyTableDisplayMode::kPUBLIC_KEY, + [](const GpgAbstractKey* key) -> bool { + return !key->IsPrivateKey() && + !(key->IsRevoked() || key->IsDisabled() || key->IsExpired()); }); key_list_->AddListGroupTab( tr("Has Private Key"), "has_private_key", - GpgKeyTableDisplayMode::kPRIVATE_KEY, [](const GpgKey& key) -> bool { - return key.IsPrivateKey() && - !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); + GpgKeyTableDisplayMode::kPRIVATE_KEY, + [](const GpgAbstractKey* key) -> bool { + return key->IsPrivateKey() && + !(key->IsRevoked() || key->IsDisabled() || key->IsExpired()); }); key_list_->AddListGroupTab( tr("No Primary Key"), "no_primary_key", GpgKeyTableDisplayMode::kPUBLIC_KEY | GpgKeyTableDisplayMode::kPRIVATE_KEY, - [](const GpgKey& key) -> bool { - return !key.IsHasMasterKey() && - !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); + [](const GpgAbstractKey* key) -> bool { + if (key->KeyType() != GpgAbstractKeyType::kGPG_KEY) return false; + return !dynamic_cast<const GpgKey*>(key)->IsHasMasterKey() && + !(key->IsRevoked() || key->IsDisabled() || key->IsExpired()); }); key_list_->AddListGroupTab( @@ -88,14 +89,20 @@ KeyMgmt::KeyMgmt(QWidget* parent) GpgKeyTableDisplayMode::kPUBLIC_KEY | GpgKeyTableDisplayMode::kPRIVATE_KEY, - [](const GpgKey& key) -> bool { return key.IsRevoked(); }); + [](const GpgAbstractKey* key) -> bool { return key->IsRevoked(); }); key_list_->AddListGroupTab( tr("Expired"), "expired", GpgKeyTableDisplayMode::kPUBLIC_KEY | GpgKeyTableDisplayMode::kPRIVATE_KEY, - [](const GpgKey& key) -> bool { return key.IsExpired(); }); + [](const GpgAbstractKey* key) -> bool { return key->IsExpired(); }); + + key_list_->AddListGroupTab( + tr("Disabled"), "disabled", + GpgKeyTableDisplayMode::kPUBLIC_KEY | + GpgKeyTableDisplayMode::kPRIVATE_KEY, + [](const GpgAbstractKey* key) -> bool { return key->IsDisabled(); }); setCentralWidget(key_list_); @@ -248,12 +255,11 @@ void KeyMgmt::create_actions() { set_owner_trust_of_key_act_->setToolTip(tr("Set Owner Trust Level")); set_owner_trust_of_key_act_->setData(QVariant("set_owner_trust_level")); connect(set_owner_trust_of_key_act_, &QAction::triggered, this, [this]() { - auto keys_selected = key_list_->GetSelected(); - if (keys_selected.empty()) return; + auto key = key_list_->GetSelectedGpgKey(); + if (key == nullptr) return; auto* function = new SetOwnerTrustLevel(this); - function->Exec(key_list_->GetCurrentGpgContextChannel(), - keys_selected.front()); + function->Exec(key_list_->GetCurrentGpgContextChannel(), key); function->deleteLater(); }); } @@ -313,29 +319,22 @@ void KeyMgmt::create_tool_bars() { } void KeyMgmt::SlotDeleteSelectedKeys() { - delete_keys_with_warning(key_list_->GetSelected()); + delete_keys_with_warning(key_list_->GetSelectedKeys()); } void KeyMgmt::SlotDeleteCheckedKeys() { - delete_keys_with_warning(key_list_->GetChecked()); + delete_keys_with_warning(key_list_->GetCheckedKeys()); } -void KeyMgmt::delete_keys_with_warning(KeyIdArgsList uid_list) { - /** - * TODO: Different Messages for private/public key, check if - * more than one selected... compare to seahorse "delete-dialog" - */ +void KeyMgmt::delete_keys_with_warning(const GpgAbstractKeyPtrList& keys) { + if (keys.empty()) return; - if (uid_list.empty()) return; QString keynames; - for (const auto& key_id : uid_list) { - auto key = - GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_id); - if (!key.IsGood()) continue; - keynames.append(key.Name()); + for (const auto& key : keys) { + if (!key->IsGood()) continue; + keynames.append(key->Name()); keynames.append("<i> <"); - keynames.append(key.Email()); + keynames.append(key->Email()); keynames.append("> </i><br/>"); } @@ -348,96 +347,79 @@ void KeyMgmt::delete_keys_with_warning(KeyIdArgsList uid_list) { if (ret == QMessageBox::Yes) { GpgKeyOpera::GetInstance(key_list_->GetCurrentGpgContextChannel()) - .DeleteKeys(std::move(uid_list)); + .DeleteKeys(keys); emit SignalKeyStatusUpdated(); } } void KeyMgmt::SlotShowKeyDetails() { - auto [succ, key] = key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto keys = key_list_->GetSelectedKeys(); + if (keys.isEmpty()) return; - new KeyDetailsDialog(key_list_->GetCurrentGpgContextChannel(), key, this); + CommonUtils::OpenDetailsDialogByKey( + this, key_list_->GetCurrentGpgContextChannel(), keys.front()); } void KeyMgmt::SlotExportKeyToKeyPackage() { - auto keys_checked = key_list_->GetChecked(); - if (keys_checked.empty()) { + auto keys = key_list_->GetCheckedKeys(); + if (keys.empty()) { QMessageBox::critical( this, tr("Forbidden"), tr("Please check some keys before doing this operation.")); return; } + auto* dialog = new ExportKeyPackageDialog( - key_list_->GetCurrentGpgContextChannel(), std::move(keys_checked), this); + key_list_->GetCurrentGpgContextChannel(), keys, this); dialog->exec(); emit SignalStatusBarChanged(tr("key(s) exported")); } void KeyMgmt::SlotExportKeyToClipboard() { - auto keys_checked = key_list_->GetChecked(); - if (keys_checked.empty()) { + auto keys = key_list_->GetCheckedKeys(); + if (keys.empty()) { QMessageBox::critical( this, tr("Forbidden"), tr("Please check some keys before doing this operation.")); return; } - if (keys_checked.size() == 1) { - auto key = - GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel()) - .GetKey(keys_checked.front()); - assert(key.IsGood()); - - auto [err, gf_buffer] = GpgKeyImportExporter::GetInstance( - key_list_->GetCurrentGpgContextChannel()) - .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(key_list_->GetCurrentGpgContextChannel()) - .GetKeys(keys_checked); - assert(std::all_of(keys.begin(), keys.end(), - [](const auto& key) { return key.IsGood(); })); - - GpgOperaHelper::WaitForOpera( - this, tr("Exporting"), [=](const OperaWaitingHd& op_hd) { - GpgKeyImportExporter::GetInstance( - key_list_->GetCurrentGpgContextChannel()) - .ExportKeys( - keys, false, true, false, false, - [=](GpgError err, const DataObjectPtr& data_obj) { - // stop waiting - op_hd(); - - if (CheckGpgError(err) == GPG_ERR_USER_1) { - QMessageBox::critical(this, tr("Error"), - tr("Unknown error occurred")); - return; - } - - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - CommonUtils::RaiseMessageBox(this, err); - return; - } - - if (data_obj == nullptr || !data_obj->Check<GFBuffer>()) { - FLOG_W("data object checking failed"); - QMessageBox::critical(this, tr("Error"), - tr("Unknown error occurred")); - return; - } - - auto gf_buffer = ExtractParams<GFBuffer>(data_obj, 0); - QApplication::clipboard()->setText( - gf_buffer.ConvertToQByteArray()); - }); - }); - } + assert(std::all_of(keys.begin(), keys.end(), + [](const auto& key) { return key->IsGood(); })); + + GpgOperaHelper::WaitForOpera( + this, tr("Exporting"), [=](const OperaWaitingHd& op_hd) { + GpgKeyImportExporter::GetInstance( + key_list_->GetCurrentGpgContextChannel()) + .ExportKeys( + keys, false, true, false, false, + [=](GpgError err, const DataObjectPtr& data_obj) { + // stop waiting + op_hd(); + + if (CheckGpgError(err) == GPG_ERR_USER_1) { + QMessageBox::critical(this, tr("Error"), + tr("Unknown error occurred")); + return; + } + + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + CommonUtils::RaiseMessageBox(this, err); + return; + } + + if (data_obj == nullptr || !data_obj->Check<GFBuffer>()) { + FLOG_W("data object checking failed"); + QMessageBox::critical(this, tr("Error"), + tr("Unknown error occurred")); + return; + } + + auto gf_buffer = ExtractParams<GFBuffer>(data_obj, 0); + QApplication::clipboard()->setText( + gf_buffer.ConvertToQByteArray()); + }); + }); } void KeyMgmt::SlotGenerateKeyDialog() { @@ -447,43 +429,40 @@ void KeyMgmt::SlotGenerateKeyDialog() { } void KeyMgmt::SlotGenerateSubKey() { - auto [succ, key] = key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = key_list_->GetSelectedGpgKey(); + if (key == nullptr) return; - if (!key.IsPrivateKey()) { + if (!key->IsPrivateKey()) { QMessageBox::critical(this, tr("Invalid Operation"), tr("If a key pair does not have a private key then " "it will not be able to generate sub-keys.")); return; } - (new SubkeyGenerateDialog(key_list_->GetCurrentGpgContextChannel(), key.ID(), + (new SubkeyGenerateDialog(key_list_->GetCurrentGpgContextChannel(), key, this)) ->exec(); this->raise(); } void KeyMgmt::SlotExportAsOpenSSHFormat() { - auto keys_checked = key_list_->GetChecked(); - if (keys_checked.empty()) { + auto keys = key_list_->GetCheckedKeys(); + if (keys.empty()) { QMessageBox::critical( this, tr("Forbidden"), tr("Please check a key before performing this operation.")); return; } - if (keys_checked.size() > 1) { + assert(std::all_of(keys.begin(), keys.end(), + [](const auto& key) { return key->IsGood(); })); + + if (keys.size() > 1) { QMessageBox::critical(this, tr("Forbidden"), tr("This operation accepts just a single key.")); return; } - auto keys = - GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel()) - .GetKeys(keys_checked); - assert(std::all_of(keys.begin(), keys.end(), - [](const auto& key) { return key.IsGood(); })); - GpgOperaHelper::WaitForOpera( this, tr("Exporting"), [this, keys](const OperaWaitingHd& op_hd) { GpgKeyImportExporter::GetInstance( diff --git a/src/ui/main_window/KeyMgmt.h b/src/ui/main_window/KeyMgmt.h index c0c004af..b25cfaaa 100644 --- a/src/ui/main_window/KeyMgmt.h +++ b/src/ui/main_window/KeyMgmt.h @@ -121,31 +121,6 @@ class KeyMgmt : public GeneralMainWindow { void SignalKeyStatusUpdated(); private: - /** - * @brief Create a menus object - * - */ - void create_menus(); - - /** - * @brief Create a actions object - * - */ - void create_actions(); - - /** - * @brief Create a tool bars object - * - */ - void create_tool_bars(); - - /** - * @brief - * - * @param uidList - */ - void delete_keys_with_warning(KeyIdArgsList uid_list); - KeyList* key_list_; ///< QMenu* file_menu_{}; ///< QMenu* key_menu_{}; ///< @@ -169,6 +144,31 @@ class KeyMgmt : public GeneralMainWindow { QAction* close_act_{}; ///< QAction* show_key_details_act_{}; ///< QAction* set_owner_trust_of_key_act_{}; + + /** + * @brief Create a menus object + * + */ + void create_menus(); + + /** + * @brief Create a actions object + * + */ + void create_actions(); + + /** + * @brief Create a tool bars object + * + */ + void create_tool_bars(); + + /** + * @brief + * + * @param uidList + */ + void delete_keys_with_warning(const GpgAbstractKeyPtrList& keys); }; } // namespace GpgFrontend::UI diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp index 698cd535..5ebfbb00 100644 --- a/src/ui/main_window/MainWindow.cpp +++ b/src/ui/main_window/MainWindow.cpp @@ -60,7 +60,8 @@ void MainWindow::Init() noexcept { kGpgFrontendDefaultChannel, KeyMenuAbility::kREFRESH | KeyMenuAbility::kCHECK_ALL | KeyMenuAbility::kUNCHECK_ALL | KeyMenuAbility::kCOLUMN_FILTER | - KeyMenuAbility::kSEARCH_BAR | KeyMenuAbility::kKEY_DATABASE, + KeyMenuAbility::kSEARCH_BAR | KeyMenuAbility::kKEY_DATABASE | + KeyMenuAbility::kKEY_GROUP, GpgKeyTableColumn::kTYPE | GpgKeyTableColumn::kNAME | GpgKeyTableColumn::kKEY_ID | GpgKeyTableColumn::kEMAIL_ADDRESS | GpgKeyTableColumn::kUSAGE | GpgKeyTableColumn::kOWNER_TRUST | @@ -138,6 +139,8 @@ void MainWindow::Init() noexcept { // recover unsaved page from cache if it exists recover_editor_unsaved_pages_from_cache(); + slot_update_operations_menu_by_checked_keys(); + // check if need to open wizard window if (GetSettings().value("wizard/show_wizard", true).toBool()) { slot_start_wizard(); diff --git a/src/ui/main_window/MainWindow.h b/src/ui/main_window/MainWindow.h index 58aa406d..6eb4da65 100644 --- a/src/ui/main_window/MainWindow.h +++ b/src/ui/main_window/MainWindow.h @@ -522,6 +522,13 @@ class MainWindow : public GeneralMainWindow { void slot_gpg_opera_buffer_show_helper( const QContainer<GpgOperaResult>& results); + /** + * @brief + * + * @param results + */ + void slot_update_operations_menu_by_checked_keys(); + private: /** * @details Create actions for the main-menu and the context-menu of the @@ -625,9 +632,9 @@ class MainWindow : public GeneralMainWindow { * @return GpgKeyList */ auto check_keys_helper( - const KeyIdArgsList& key_ids, - const std::function<bool(const GpgKey&)>& capability_check, - const QString& capability_err_string) -> GpgKeyList; + const GpgAbstractKeyPtrList& keys, + const std::function<bool(const GpgAbstractKeyPtr&)>& capability_check, + const QString& capability_err_string) -> GpgAbstractKeyPtrList; /** * @brief diff --git a/src/ui/main_window/MainWindowGpgOperaFunction.cpp b/src/ui/main_window/MainWindowGpgOperaFunction.cpp index dc65fd1c..925950eb 100644 --- a/src/ui/main_window/MainWindowGpgOperaFunction.cpp +++ b/src/ui/main_window/MainWindowGpgOperaFunction.cpp @@ -27,8 +27,6 @@ */ #include "MainWindow.h" -#include "core/function/GlobalSettingStation.h" -#include "core/function/gpg/GpgKeyGetter.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" #include "ui/UserInterfaceUtils.h" @@ -42,10 +40,10 @@ namespace GpgFrontend::UI { auto MainWindow::encrypt_operation_key_validate( const QSharedPointer<GpgOperaContextBasement>& contexts) -> bool { - auto key_ids = m_key_list_->GetChecked(); + auto keys = m_key_list_->GetCheckedKeys(); // symmetric encryption - if (key_ids.isEmpty()) { + if (keys.isEmpty()) { auto ret = QMessageBox::information( this, tr("Symmetric Encryption"), tr("No Key Selected. Do you want to encrypt with a " @@ -56,7 +54,7 @@ auto MainWindow::encrypt_operation_key_validate( contexts->keys = {}; } else { contexts->keys = check_keys_helper( - key_ids, [](const GpgKey& key) { return key.IsHasActualEncrCap(); }, + keys, [](const GpgAbstractKeyPtr& key) { return key->IsHasEncrCap(); }, tr("The selected keypair cannot be used for encryption.")); if (contexts->keys.empty()) return false; } @@ -75,12 +73,9 @@ auto MainWindow::sign_operation_key_validate( // return when canceled if (!signers_picker->GetStatus()) return false; - auto signer_key_ids = signers_picker->GetCheckedSigners(); - auto signer_keys = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKeys(signer_key_ids); + auto signer_keys = signers_picker->GetCheckedSigners(); assert(std::all_of(signer_keys.begin(), signer_keys.end(), - [](const auto& key) { return key.IsGood(); })); + [](const auto& key) { return key->IsGood(); })); contexts->singer_keys = signer_keys; @@ -150,21 +145,18 @@ auto MainWindow::check_write_file_paths_helper(const QStringList& o_paths) } auto MainWindow::check_keys_helper( - const KeyIdArgsList& key_ids, - const std::function<bool(const GpgKey&)>& capability_check, - const QString& capability_err_string) -> GpgKeyList { - if (key_ids.empty()) { + const GpgAbstractKeyPtrList& keys, + const std::function<bool(const GpgAbstractKeyPtr&)>& capability_check, + const QString& capability_err_string) -> GpgAbstractKeyPtrList { + if (keys.isEmpty()) { QMessageBox::critical( this, tr("No Key Checked"), tr("Please check the key in the key toolbox on the right.")); return {}; } - auto keys = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKeys(key_ids); assert(std::all_of(keys.begin(), keys.end(), - [](const auto& key) { return key.IsGood(); })); + [](const auto& key) { return key->IsGood(); })); // check key abilities for (const auto& key : keys) { @@ -172,7 +164,7 @@ auto MainWindow::check_keys_helper( QMessageBox::critical(nullptr, tr("Invalid KeyPair"), capability_err_string + "<br/><br/>" + tr("For example the Following Key:") + - " <br/>" + key.UIDs().front().GetUID()); + " <br/>" + key->Email()); return {}; } } @@ -202,9 +194,10 @@ void MainWindow::SlotSign() { auto contexts = QSharedPointer<GpgOperaContextBasement>::create(); contexts->ascii = true; - auto key_ids = m_key_list_->GetChecked(); + auto keys = m_key_list_->GetCheckedKeys(); + contexts->keys = check_keys_helper( - key_ids, [](const GpgKey& key) { return key.IsHasActualSignCap(); }, + keys, [](const GpgAbstractKeyPtr& key) { return key->IsHasSignCap(); }, tr("The selected key contains a key that does not actually have a " "sign usage.")); if (contexts->keys.empty()) return; @@ -255,9 +248,10 @@ void MainWindow::SlotEncryptSign() { auto contexts = QSharedPointer<GpgOperaContextBasement>::create(); contexts->ascii = true; - auto key_ids = m_key_list_->GetChecked(); + auto keys = m_key_list_->GetCheckedKeys(); + contexts->keys = check_keys_helper( - key_ids, [](const GpgKey& key) { return key.IsHasActualEncrCap(); }, + keys, [](const GpgAbstractKeyPtr& key) { return key->IsHasEncrCap(); }, tr("The selected keypair cannot be used for encryption.")); if (contexts->keys.empty()) return; @@ -363,9 +357,10 @@ void MainWindow::SlotFileSign(const QStringList& paths, bool ascii) { contexts->ascii = ascii; - auto key_ids = m_key_list_->GetChecked(); + auto keys = m_key_list_->GetCheckedKeys(); + contexts->keys = check_keys_helper( - key_ids, [](const GpgKey& key) { return key.IsHasActualSignCap(); }, + keys, [](const GpgAbstractKeyPtr& key) { return key->IsHasSignCap(); }, tr("The selected key contains a key that does not actually have a " "sign usage.")); if (contexts->keys.empty()) return; @@ -442,9 +437,9 @@ void MainWindow::SlotFileEncryptSign(const QStringList& paths, bool ascii) { auto contexts = QSharedPointer<GpgOperaContextBasement>::create(); contexts->ascii = ascii; - auto key_ids = m_key_list_->GetChecked(); + auto keys = m_key_list_->GetCheckedKeys(); contexts->keys = check_keys_helper( - key_ids, [](const GpgKey& key) { return key.IsHasActualEncrCap(); }, + keys, [](const GpgAbstractKeyPtr& key) { return key->IsHasEncrCap(); }, tr("The selected keypair cannot be used for encryption.")); if (contexts->keys.empty()) return; diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index fb4f9e73..b19c15e8 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -27,7 +27,6 @@ */ #include "MainWindow.h" -#include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/function/result_analyse/GpgDecryptResultAnalyse.h" #include "core/function/result_analyse/GpgEncryptResultAnalyse.h" @@ -45,7 +44,6 @@ #include "ui/dialog/SignersPicker.h" #include "ui/dialog/help/AboutDialog.h" #include "ui/dialog/import_export/KeyUploadDialog.h" -#include "ui/dialog/keypair_details/KeyDetailsDialog.h" #include "ui/function/GpgOperaHelper.h" #include "ui/function/SetOwnerTrustLevel.h" #include "ui/struct/GpgOperaResult.h" @@ -71,25 +69,12 @@ void MainWindow::slot_find() { * Append the selected (not checked!) Key(s) To Textedit */ void MainWindow::slot_append_selected_keys() { - auto key_ids = m_key_list_->GetSelected(); - - if (key_ids.empty()) { - FLOG_W("no key is selected to export"); - return; - } - - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - LOG_W() << "selected key for exporting is invalid, key id: " - << key_ids.front(); - return; - } + auto keys = m_key_list_->GetSelectedKeys(); + if (keys.empty()) return; auto [err, gf_buffer] = GpgKeyImportExporter::GetInstance( m_key_list_->GetCurrentGpgContextChannel()) - .ExportKey(key, false, true, false); + .ExportKey(keys.front(), false, true, false); if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { CommonUtils::RaiseMessageBox(this, err); return; @@ -99,123 +84,121 @@ void MainWindow::slot_append_selected_keys() { } void MainWindow::slot_append_keys_create_datetime() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr) return; auto create_datetime_format_str_local = - QLocale().toString(key.CreationTime()) + " (" + tr("Localize") + ") " + + QLocale().toString(key->CreationTime()) + " (" + tr("Localize") + ") " + "\n"; auto create_datetime_format_str = - QLocale().toString(key.CreationTime().toUTC()) + " (" + tr("UTC") + ") " + - "\n "; + QLocale().toString(key->CreationTime().toUTC()) + " (" + tr("UTC") + + ") " + "\n "; edit_->SlotAppendText2CurTextPage(create_datetime_format_str_local + create_datetime_format_str); } void MainWindow::slot_append_keys_expire_datetime() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr) return; auto expire_datetime_format_str_local = - QLocale().toString(key.ExpirationTime()) + " (" + tr("Local Time") + + QLocale().toString(key->ExpirationTime()) + " (" + tr("Local Time") + ") " + "\n"; auto expire_datetime_format_str = - QLocale().toString(key.ExpirationTime().toUTC()) + " (UTC) " + "\n"; + QLocale().toString(key->ExpirationTime().toUTC()) + " (UTC) " + "\n"; edit_->SlotAppendText2CurTextPage(expire_datetime_format_str_local + expire_datetime_format_str); } void MainWindow::slot_append_keys_fingerprint() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr) return; - auto fingerprint_format_str = BeautifyFingerprint(key.Fingerprint()) + "\n"; + auto fingerprint_format_str = BeautifyFingerprint(key->Fingerprint()) + "\n"; edit_->SlotAppendText2CurTextPage(fingerprint_format_str); } void MainWindow::slot_copy_mail_address_to_clipboard() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr) return; QClipboard* cb = QApplication::clipboard(); - cb->setText(key.Email()); + cb->setText(key->Email()); } void MainWindow::slot_copy_default_uid_to_clipboard() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr || key->KeyType() != GpgAbstractKeyType::kGPG_KEY) return; QClipboard* cb = QApplication::clipboard(); - cb->setText(key.UIDs().front().GetUID()); + cb->setText(qSharedPointerDynamicCast<GpgKey>(key)->UIDs().front().GetUID()); } void MainWindow::slot_copy_key_id_to_clipboard() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr) return; QClipboard* cb = QApplication::clipboard(); - cb->setText(key.ID()); + cb->setText(key->ID()); } void MainWindow::slot_show_key_details() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto keys = m_key_list_->GetSelectedKeys(); + if (keys.isEmpty()) return; - new KeyDetailsDialog(m_key_list_->GetCurrentGpgContextChannel(), key, this); + CommonUtils::OpenDetailsDialogByKey( + this, m_key_list_->GetCurrentGpgContextChannel(), keys.front()); } void MainWindow::slot_add_key_2_favorite() { - auto [succ, key] = m_key_list_->GetSelectedGpgKey(); - if (!succ) return; + auto key = m_key_list_->GetSelectedKey(); + if (key == nullptr) return; auto key_db_name = GetGpgKeyDatabaseName(m_key_list_->GetCurrentGpgContextChannel()); - LOG_D() << "add key" << key.ID() << "to favorite at key db" << key_db_name; + LOG_D() << "add key" << key->ID() << "to favorite at key db" << key_db_name; CommonUtils::GetInstance()->AddKey2Favorite(key_db_name, key); emit SignalUIRefresh(); } void MainWindow::slot_remove_key_from_favorite() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; - - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - assert(key.IsGood()); + auto keys = m_key_list_->GetSelectedKeys(); + if (keys.empty()) return; auto key_db_name = GetGpgKeyDatabaseName(m_key_list_->GetCurrentGpgContextChannel()); - CommonUtils::GetInstance()->RemoveKeyFromFavorite(key_db_name, key); + CommonUtils::GetInstance()->RemoveKeyFromFavorite(key_db_name, keys.front()); emit SignalUIRefresh(); } void MainWindow::refresh_keys_from_key_server() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; - CommonUtils::GetInstance()->ImportKeyFromKeyServer( - m_key_list_->GetCurrentGpgContextChannel(), key_ids); + auto keys = m_key_list_->GetSelectedGpgKeys(); + if (keys.empty()) return; + CommonUtils::GetInstance()->ImportGpgKeyFromKeyServer( + m_key_list_->GetCurrentGpgContextChannel(), keys); } void MainWindow::slot_set_owner_trust_level_of_key() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; + auto keys = m_key_list_->GetSelectedGpgKeys(); + if (keys.empty()) return; auto* function = new SetOwnerTrustLevel(this); - function->Exec(m_key_list_->GetCurrentGpgContextChannel(), key_ids.front()); + function->Exec(m_key_list_->GetCurrentGpgContextChannel(), keys.front()); function->deleteLater(); } void MainWindow::upload_key_to_server() { - auto key_ids = m_key_list_->GetSelected(); + auto keys = m_key_list_->GetSelectedKeys(); + if (keys.empty()) return; + auto* dialog = new KeyUploadDialog(m_key_list_->GetCurrentGpgContextChannel(), - key_ids, this); + keys, this); dialog->show(); dialog->SlotUpload(); } @@ -640,25 +623,28 @@ void MainWindow::decrypt_email_by_eml_data_result_helper( void MainWindow::SlotEncryptEML() { if (edit_->TabCount() == 0 || edit_->CurEMailPage() == nullptr) return; - auto checked_keys = m_key_list_->GetCheckedKeys(); + auto keys = m_key_list_->GetCheckedKeys(); - if (checked_keys.isEmpty()) { + if (keys.isEmpty()) { QMessageBox::warning(this, tr("No Key Selected"), tr("Please select a key for encrypt the EML.")); return; } auto buffer = edit_->CurPlainText().toUtf8(); + auto key_ids = + ConvertKey2GpgKeyIdList(m_key_list_->GetCurrentGpgContextChannel(), keys); + GpgOperaHelper::WaitForOpera( this, tr("Encrypting"), - [this, buffer, checked_keys](const OperaWaitingHd& hd) { + [this, buffer, key_ids](const OperaWaitingHd& hd) { Module::TriggerEvent( "EMAIL_ENCRYPT_EML_DATA", { {"body_data", QString::fromLatin1(buffer.toBase64())}, {"channel", QString::number(m_key_list_->GetCurrentGpgContextChannel())}, - {"encrypt_keys", checked_keys.join(';')}, + {"encrypt_keys", key_ids.join(';')}, }, [=](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei, @@ -700,32 +686,33 @@ void MainWindow::SlotEncryptEML() { void MainWindow::SlotSignEML() { if (edit_->TabCount() == 0 || edit_->CurEMailPage() == nullptr) return; - auto checked_keys = m_key_list_->GetCheckedKeys(); + auto keys = m_key_list_->GetCheckedKeys(); - if (checked_keys.isEmpty()) { + if (keys.isEmpty()) { QMessageBox::warning(this, tr("No Key Selected"), tr("Please select a key for signing the EML.")); return; } - if (checked_keys.size() > 1) { + if (keys.size() > 1) { QMessageBox::warning(this, tr("Multiple Keys Selected"), tr("Please select only one key to sign the EML.")); return; } auto buffer = edit_->CurPlainText().toUtf8(); + auto key_ids = + ConvertKey2GpgKeyIdList(m_key_list_->GetCurrentGpgContextChannel(), keys); GpgOperaHelper::WaitForOpera( - this, tr("Signing"), - [this, buffer, checked_keys](const OperaWaitingHd& hd) { + this, tr("Signing"), [this, buffer, key_ids](const OperaWaitingHd& hd) { Module::TriggerEvent( "EMAIL_SIGN_EML_DATA", { {"body_data", QString::fromLatin1(buffer.toBase64())}, {"channel", QString::number(m_key_list_->GetCurrentGpgContextChannel())}, - {"sign_key", checked_keys.front()}, + {"sign_key", key_ids.front()}, }, [=](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei, Module::Event::Params p) { @@ -766,9 +753,9 @@ void MainWindow::SlotSignEML() { void MainWindow::SlotEncryptSignEML() { if (edit_->TabCount() == 0 || edit_->CurEMailPage() == nullptr) return; - auto checked_keys = m_key_list_->GetCheckedKeys(); + auto keys = m_key_list_->GetCheckedKeys(); - if (checked_keys.isEmpty()) { + if (keys.isEmpty()) { QMessageBox::warning(this, tr("No Key Selected"), tr("Please select a key for encrypt the EML.")); return; @@ -783,7 +770,7 @@ void MainWindow::SlotEncryptSignEML() { // return when canceled if (!signers_picker->GetStatus()) return; - auto signer_keys = signers_picker->GetCheckedSignerKeyIds(); + auto signer_keys = signers_picker->GetCheckedSigners(); if (signer_keys.isEmpty()) { QMessageBox::warning(this, tr("No Key Selected"), @@ -798,18 +785,20 @@ void MainWindow::SlotEncryptSignEML() { } auto buffer = edit_->CurPlainText().toUtf8(); + auto key_ids = + ConvertKey2GpgKeyIdList(m_key_list_->GetCurrentGpgContextChannel(), keys); GpgOperaHelper::WaitForOpera( this, tr("Encrypting and Signing"), - [this, buffer, checked_keys, signer_keys](const OperaWaitingHd& hd) { + [this, buffer, key_ids, signer_keys](const OperaWaitingHd& hd) { Module::TriggerEvent( "EMAIL_ENCRYPT_SIGN_EML_DATA", { {"body_data", QString::fromLatin1(buffer.toBase64())}, {"channel", QString::number(m_key_list_->GetCurrentGpgContextChannel())}, - {"sign_key", signer_keys.front()}, - {"encrypt_keys", checked_keys.front()}, + {"sign_key", signer_keys.front()->ID()}, + {"encrypt_keys", key_ids.front()}, }, [=](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei, Module::Event::Params p) { diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index 248e6488..51de7d23 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -29,7 +29,6 @@ #include "MainWindow.h" #include "core/GpgConstants.h" #include "core/function/CacheManager.h" -#include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgAdvancedOperator.h" #include "core/model/SettingsObject.h" #include "ui/UserInterfaceUtils.h" @@ -37,6 +36,7 @@ #include "ui/dialog/settings/SettingsDialog.h" #include "ui/main_window/KeyMgmt.h" #include "ui/struct/settings_object/AppearanceSO.h" +#include "ui/widgets/KeyList.h" #include "ui/widgets/TextEdit.h" namespace GpgFrontend::UI { @@ -168,7 +168,7 @@ void MainWindow::slot_cut_pgp_header() { void MainWindow::SlotSetRestartNeeded(int mode) { this->restart_mode_ = mode; } void MainWindow::SlotUpdateCryptoMenuStatus(unsigned int type) { - MainWindow::OperationMenu::OperationType opera_type = type; + OperationMenu::OperationType opera_type = type; // refresh status to disable all verify_act_->setDisabled(true); @@ -179,22 +179,22 @@ void MainWindow::SlotUpdateCryptoMenuStatus(unsigned int type) { decrypt_verify_act_->setDisabled(true); // gnupg operations - if ((opera_type & MainWindow::OperationMenu::kVerify) != 0U) { + if ((opera_type & OperationMenu::kVerify) != 0U) { verify_act_->setDisabled(false); } - if ((opera_type & MainWindow::OperationMenu::kSign) != 0U) { + if ((opera_type & OperationMenu::kSign) != 0U) { sign_act_->setDisabled(false); } - if ((opera_type & MainWindow::OperationMenu::kEncrypt) != 0U) { + if ((opera_type & OperationMenu::kEncrypt) != 0U) { encrypt_act_->setDisabled(false); } - if ((opera_type & MainWindow::OperationMenu::kEncryptAndSign) != 0U) { + if ((opera_type & OperationMenu::kEncryptAndSign) != 0U) { encrypt_sign_act_->setDisabled(false); } - if ((opera_type & MainWindow::OperationMenu::kDecrypt) != 0U) { + if ((opera_type & OperationMenu::kDecrypt) != 0U) { decrypt_act_->setDisabled(false); } - if ((opera_type & MainWindow::OperationMenu::kDecryptAndVerify) != 0U) { + if ((opera_type & OperationMenu::kDecryptAndVerify) != 0U) { decrypt_verify_act_->setDisabled(false); } } @@ -346,4 +346,29 @@ void MainWindow::slot_restart_gpg_components(bool) { }); } +void MainWindow::slot_update_operations_menu_by_checked_keys() { + auto keys = m_key_list_->GetCheckedKeys(); + + OperationMenu::OperationType type = ~0; + + if (keys.isEmpty()) { + type &= ~(OperationMenu::kEncrypt | OperationMenu::kEncryptAndSign | + OperationMenu::kSign); + + } else { + for (const auto& key : keys) { + if (key == nullptr || key->IsDisabled()) continue; + + if (!key->IsHasEncrCap()) { + type &= ~(OperationMenu::kEncrypt | OperationMenu::kEncryptAndSign); + } + if (!key->IsHasSignCap()) { + type &= ~(OperationMenu::kSign); + } + } + } + + SlotUpdateCryptoMenuStatus(type); +} + } // namespace GpgFrontend::UI diff --git a/src/ui/main_window/MainWindowUI.cpp b/src/ui/main_window/MainWindowUI.cpp index 6598aede..26c3a8ad 100644 --- a/src/ui/main_window/MainWindowUI.cpp +++ b/src/ui/main_window/MainWindowUI.cpp @@ -570,28 +570,31 @@ void MainWindow::create_dock_windows() { tr("Default"), "default", GpgKeyTableDisplayMode::kPUBLIC_KEY | GpgKeyTableDisplayMode::kPRIVATE_KEY, - [](const GpgKey& key) -> bool { - return !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); + [](const GpgAbstractKey* key) -> bool { + return !(key->IsRevoked() || key->IsDisabled() || key->IsExpired()); }); - m_key_list_->AddListGroupTab(tr("Favourite"), "favourite", - GpgKeyTableDisplayMode::kPUBLIC_KEY | - GpgKeyTableDisplayMode::kPRIVATE_KEY | - GpgKeyTableDisplayMode::kFAVORITES, - [](const GpgKey&) -> bool { return true; }); + m_key_list_->AddListGroupTab( + tr("Favourite"), "favourite", + GpgKeyTableDisplayMode::kPUBLIC_KEY | + GpgKeyTableDisplayMode::kPRIVATE_KEY | + GpgKeyTableDisplayMode::kFAVORITES, + [](const GpgAbstractKey*) -> bool { return true; }); m_key_list_->AddListGroupTab( tr("Only Public Key"), "only_public_key", - GpgKeyTableDisplayMode::kPUBLIC_KEY, [](const GpgKey& key) -> bool { - return !key.IsPrivateKey() && - !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); + GpgKeyTableDisplayMode::kPUBLIC_KEY, + [](const GpgAbstractKey* key) -> bool { + return !key->IsPrivateKey() && + !(key->IsRevoked() || key->IsDisabled() || key->IsExpired()); }); m_key_list_->AddListGroupTab( tr("Has Private Key"), "has_private_key", - GpgKeyTableDisplayMode::kPRIVATE_KEY, [](const GpgKey& key) -> bool { - return key.IsPrivateKey() && - !(key.IsRevoked() || key.IsDisabled() || key.IsExpired()); + GpgKeyTableDisplayMode::kPRIVATE_KEY, + [](const GpgAbstractKey* key) -> bool { + return key->IsPrivateKey() && + !(key->IsRevoked() || key->IsDisabled() || key->IsExpired()); }); m_key_list_->SlotRefresh(); @@ -606,6 +609,9 @@ void MainWindow::create_dock_windows() { info_board_dock_->setWidget(info_board_); info_board_dock_->widget()->layout()->setContentsMargins(0, 0, 0, 0); view_menu_->addAction(info_board_dock_->toggleViewAction()); + + connect(m_key_list_, &KeyList::SignalKeyChecked, this, + &MainWindow::slot_update_operations_menu_by_checked_keys); } } // namespace GpgFrontend::UI |