diff options
author | saturneric <[email protected]> | 2025-01-27 22:56:22 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2025-01-27 22:56:22 +0000 |
commit | 7505b1131e62af46b785e4482b41dc7a9d002477 (patch) | |
tree | 3af3c8413b8ce1413fc7c37ddba422bf8408bd3a /src/ui | |
parent | fix: correct de_DE translations (diff) | |
download | GpgFrontend-7505b1131e62af46b785e4482b41dc7a9d002477.tar.gz GpgFrontend-7505b1131e62af46b785e4482b41dc7a9d002477.zip |
refactor: clean up code and reduce duplication
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/main_window/KeyMgmt.cpp | 29 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.cpp | 2 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 99 | ||||
-rw-r--r-- | src/ui/widgets/KeyList.cpp | 15 | ||||
-rw-r--r-- | src/ui/widgets/KeyList.h | 7 |
5 files changed, 45 insertions, 107 deletions
diff --git a/src/ui/main_window/KeyMgmt.cpp b/src/ui/main_window/KeyMgmt.cpp index e2f09f40..53b89c91 100644 --- a/src/ui/main_window/KeyMgmt.cpp +++ b/src/ui/main_window/KeyMgmt.cpp @@ -353,16 +353,8 @@ void KeyMgmt::delete_keys_with_warning(KeyIdArgsList uid_list) { } void KeyMgmt::SlotShowKeyDetails() { - auto keys_selected = key_list_->GetSelected(); - if (keys_selected.empty()) return; - - auto key = GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel()) - .GetKey(keys_selected.front()); - - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } + auto [succ, key] = key_list_->GetSelectedGpgKey(); + if (!succ) return; new KeyDetailsDialog(key_list_->GetCurrentGpgContextChannel(), key, this); } @@ -453,20 +445,9 @@ void KeyMgmt::SlotGenerateKeyDialog() { } void KeyMgmt::SlotGenerateSubKey() { - auto keys_selected = key_list_->GetSelected(); - if (keys_selected.empty()) { - QMessageBox::information( - this, tr("Invalid Operation"), - tr("Please select one KeyPair before doing this operation.")); - return; - } - const auto key = - GpgKeyGetter::GetInstance(key_list_->GetCurrentGpgContextChannel()) - .GetKey(keys_selected.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } + auto [succ, key] = key_list_->GetSelectedGpgKey(); + if (!succ) return; + if (!key.IsPrivateKey()) { QMessageBox::critical(this, tr("Invalid Operation"), tr("If a key pair does not have a private key then " diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp index e0f7a49a..f362daa5 100644 --- a/src/ui/main_window/MainWindow.cpp +++ b/src/ui/main_window/MainWindow.cpp @@ -30,7 +30,7 @@ #include "core/function/CacheManager.h" #include "core/function/GlobalSettingStation.h" -#include "core/function/gpg/GpgAdvancedOperator.h" +#include "core/function/gpg/GpgKeyGetter.h" #include "core/model/SettingsObject.h" #include "core/module/ModuleManager.h" #include "ui/UISignalStation.h" diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 825f74f5..36d4435d 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -100,20 +100,8 @@ void MainWindow::slot_append_selected_keys() { } void MainWindow::slot_append_keys_create_datetime() { - auto key_ids = m_key_list_->GetSelected(); - - if (key_ids.empty()) { - FLOG_W("no key is selected"); - return; - } - - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; auto create_datetime_format_str_local = QLocale().toString(key.GetCreateTime()) + " (" + tr("Localize") + ") " + @@ -126,20 +114,8 @@ void MainWindow::slot_append_keys_create_datetime() { } void MainWindow::slot_append_keys_expire_datetime() { - auto key_ids = m_key_list_->GetSelected(); - - if (key_ids.empty()) { - FLOG_W("no key is selected"); - return; - } - - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; auto expire_datetime_format_str_local = QLocale().toString(key.GetExpireTime()) + " (" + tr("Local Time") + ") " + @@ -152,16 +128,8 @@ void MainWindow::slot_append_keys_expire_datetime() { } void MainWindow::slot_append_keys_fingerprint() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; - - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; auto fingerprint_format_str = BeautifyFingerprint(key.GetFingerprint()) + "\n"; @@ -170,72 +138,39 @@ void MainWindow::slot_append_keys_fingerprint() { } void MainWindow::slot_copy_mail_address_to_clipboard() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } QClipboard* cb = QApplication::clipboard(); cb->setText(key.GetEmail()); } void MainWindow::slot_copy_default_uid_to_clipboard() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } QClipboard* cb = QApplication::clipboard(); cb->setText(key.GetUIDs()->front().GetUID()); } void MainWindow::slot_copy_key_id_to_clipboard() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } QClipboard* cb = QApplication::clipboard(); cb->setText(key.GetId()); } void MainWindow::slot_show_key_details() { - auto key_ids = m_key_list_->GetSelected(); - if (key_ids.empty()) return; + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; - auto key = - GpgKeyGetter::GetInstance(m_key_list_->GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); - if (key.IsGood()) { - new KeyDetailsDialog(m_key_list_->GetCurrentGpgContextChannel(), key, this); - } else { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - } + new KeyDetailsDialog(m_key_list_->GetCurrentGpgContextChannel(), key, this); } void MainWindow::slot_add_key_2_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()); - if (!key.IsGood()) return; + auto [succ, key] = m_key_list_->GetSelectedGpgKey(); + if (!succ) return; auto key_db_name = GetGpgKeyDatabaseName(m_key_list_->GetCurrentGpgContextChannel()); diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index 1d954f86..4e581d81 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -633,4 +633,19 @@ void KeyList::UpdateKeyTableColumnType(GpgKeyTableColumn column_type) { auto KeyList::GetCurrentGpgContextChannel() const -> int { return current_gpg_context_channel_; } + +auto KeyList::GetSelectedGpgKey() -> std::tuple<bool, GpgKey> { + auto key_ids = GetSelected(); + if (key_ids.empty()) return {false, GpgKey()}; + + auto key = GpgKeyGetter::GetInstance(GetCurrentGpgContextChannel()) + .GetKey(key_ids.front()); + + if (!key.IsGood()) { + QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); + return {false, GpgKey()}; + } + + return {true, key}; +} } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/KeyList.h b/src/ui/widgets/KeyList.h index abcca2b9..868c5b07 100644 --- a/src/ui/widgets/KeyList.h +++ b/src/ui/widgets/KeyList.h @@ -196,6 +196,13 @@ class KeyList : public QWidget { auto GetSelectedKey() -> QString; /** + * @brief Get the Selected Gpg Key object + * + * @return GpgKey + */ + auto GetSelectedGpgKey() -> std::tuple<bool, GpgKey>; + + /** * @brief * * @return true |