From a236f22b15273a0877f0e2ea20ab677334f03fc6 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 11 May 2024 15:42:53 +0200 Subject: [PATCH] feat: let user select keys to sync from key server --- src/ui/dialog/SignersPicker.cpp | 2 +- .../MainWindowGpgOperaFunction.cpp | 2 +- src/ui/widgets/KeyList.cpp | 48 ++++++++++++++++--- src/ui/widgets/KeyList.h | 23 +++++---- 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/ui/dialog/SignersPicker.cpp b/src/ui/dialog/SignersPicker.cpp index 378a58c7..9c342cc7 100644 --- a/src/ui/dialog/SignersPicker.cpp +++ b/src/ui/dialog/SignersPicker.cpp @@ -78,7 +78,7 @@ SignersPicker::SignersPicker(QWidget* parent) } auto SignersPicker::GetCheckedSigners() -> GpgFrontend::KeyIdArgsListPtr { - return key_list_->GetPrivateChecked(); + return key_list_->GetCheckedPrivateKey(); } auto SignersPicker::GetStatus() const -> bool { return this->accepted_; } diff --git a/src/ui/main_window/MainWindowGpgOperaFunction.cpp b/src/ui/main_window/MainWindowGpgOperaFunction.cpp index b1b22a6e..3cef497d 100644 --- a/src/ui/main_window/MainWindowGpgOperaFunction.cpp +++ b/src/ui/main_window/MainWindowGpgOperaFunction.cpp @@ -141,7 +141,7 @@ void MainWindow::SlotEncrypt() { void MainWindow::SlotSign() { if (edit_->SlotCurPageTextEdit() == nullptr) return; - auto key_ids = m_key_list_->GetPrivateChecked(); + auto key_ids = m_key_list_->GetCheckedPrivateKey(); if (key_ids->empty()) { QMessageBox::critical( this, tr("No Key Checked"), diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index 48c99269..8b3e9907 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -231,7 +231,7 @@ auto KeyList::GetAllPrivateKeys() -> KeyIdArgsListPtr { return ret; } -auto KeyList::GetPrivateChecked() -> KeyIdArgsListPtr { +auto KeyList::GetCheckedPrivateKey() -> KeyIdArgsListPtr { auto ret = std::make_unique(); if (ui_->keyGroupTab->size().isEmpty()) return ret; @@ -242,7 +242,27 @@ auto KeyList::GetPrivateChecked() -> KeyIdArgsListPtr { for (int i = 0; i < key_list->rowCount(); i++) { if ((key_list->item(i, 0)->checkState() == Qt::Checked) && - ((key_list->item(i, 1)) != nullptr)) { + ((key_list->item(i, 1)) != nullptr) && + buffered_keys[i].IsPrivateKey()) { + ret->push_back(buffered_keys[i].GetId()); + } + } + return ret; +} + +auto KeyList::GetCheckedPublicKey() -> KeyIdArgsListPtr { + auto ret = std::make_unique(); + if (ui_->keyGroupTab->size().isEmpty()) return ret; + + auto* key_list = + qobject_cast(ui_->keyGroupTab->currentWidget()); + const auto& buffered_keys = + m_key_tables_[ui_->keyGroupTab->currentIndex()].buffered_keys_; + + for (int i = 0; i < key_list->rowCount(); i++) { + if ((key_list->item(i, 0)->checkState() == Qt::Checked) && + ((key_list->item(i, 1)) != nullptr) && + !buffered_keys[i].IsPrivateKey()) { ret->push_back(buffered_keys[i].GetId()); } } @@ -458,14 +478,28 @@ void KeyList::slot_refresh_ui() { } void KeyList::slot_sync_with_key_server() { + auto checked_public_keys = GetCheckedPublicKey(); + KeyIdArgsList key_ids; - { - std::lock_guard guard(buffered_key_list_mutex_); - for (const auto& key : *buffered_keys_list_) { - if (!(key.IsPrivateKey() && key.IsHasMasterKey())) { - key_ids.push_back(key.GetId()); + if (checked_public_keys->empty()) { + QMessageBox::StandardButton const reply = QMessageBox::question( + this, QCoreApplication::tr("Sync All Public Key"), + QCoreApplication::tr("You have not checked any public keys that you " + "want to synchronize, do you want to synchronize " + "all public keys from the key server?"), + QMessageBox::Yes | QMessageBox::No); + + if (reply == QMessageBox::No) return; + { + std::lock_guard guard(buffered_key_list_mutex_); + for (const auto& key : *buffered_keys_list_) { + if (!(key.IsPrivateKey() && key.IsHasMasterKey())) { + key_ids.push_back(key.GetId()); + } } } + } else { + key_ids = *checked_public_keys; } if (key_ids.empty()) return; diff --git a/src/ui/widgets/KeyList.h b/src/ui/widgets/KeyList.h index 25180496..1761fb23 100644 --- a/src/ui/widgets/KeyList.h +++ b/src/ui/widgets/KeyList.h @@ -188,7 +188,7 @@ class KeyList : public QWidget { const QString& name, const QString& id, KeyListRow::KeyType selectType = KeyListRow::SECRET_OR_PUBLIC_KEY, KeyListColumn::InfoType infoType = KeyListColumn::ALL, - const KeyTable::KeyTableFilter filter = + KeyTable::KeyTableFilter filter = [](const GpgKey&, const KeyTable&) -> bool { return true; }); /** @@ -225,7 +225,7 @@ class KeyList : public QWidget { * * @return KeyIdArgsListPtr */ - KeyIdArgsListPtr GetChecked(); + auto GetChecked() -> KeyIdArgsListPtr; /** * @brief Get the Checked object @@ -233,21 +233,28 @@ class KeyList : public QWidget { * @param key_table * @return KeyIdArgsListPtr */ - static KeyIdArgsListPtr GetChecked(const KeyTable& key_table); + static auto GetChecked(const KeyTable& key_table) -> KeyIdArgsListPtr; /** * @brief Get the Private Checked object * * @return KeyIdArgsListPtr */ - KeyIdArgsListPtr GetPrivateChecked(); + auto GetCheckedPrivateKey() -> KeyIdArgsListPtr; + + /** + * @brief + * + * @return KeyIdArgsListPtr + */ + auto GetCheckedPublicKey() -> KeyIdArgsListPtr; /** * @brief Get the All Private Keys object * * @return KeyIdArgsListPtr */ - KeyIdArgsListPtr GetAllPrivateKeys(); + auto GetAllPrivateKeys() -> KeyIdArgsListPtr; /** * @brief Set the Checked object @@ -270,14 +277,14 @@ class KeyList : public QWidget { * * @return KeyIdArgsListPtr */ - KeyIdArgsListPtr GetSelected(); + auto GetSelected() -> KeyIdArgsListPtr; /** * @brief Get the Selected Key object * * @return QString */ - QString GetSelectedKey(); + auto GetSelectedKey() -> QString; /** * @brief @@ -285,7 +292,7 @@ class KeyList : public QWidget { * @return true * @return false */ - [[maybe_unused]] bool ContainsPrivateKeys(); + [[maybe_unused]] auto ContainsPrivateKeys() -> bool; signals: /**