diff options
Diffstat (limited to 'src/ui/widgets')
-rw-r--r-- | src/ui/widgets/KeyList.cpp | 249 | ||||
-rw-r--r-- | src/ui/widgets/KeyList.h | 140 | ||||
-rw-r--r-- | src/ui/widgets/KeyTable.cpp | 64 | ||||
-rw-r--r-- | src/ui/widgets/KeyTable.h | 44 | ||||
-rw-r--r-- | src/ui/widgets/KeyTreeView.cpp | 26 | ||||
-rw-r--r-- | src/ui/widgets/KeyTreeView.h | 8 | ||||
-rw-r--r-- | src/ui/widgets/VerifyKeyDetailBox.cpp | 23 | ||||
-rw-r--r-- | src/ui/widgets/VerifyKeyDetailBox.h | 2 |
8 files changed, 319 insertions, 237 deletions
diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index fb26295f..429a5bd1 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -31,34 +31,50 @@ #include <cstddef> #include "core/function/GlobalSettingStation.h" +#include "core/function/gpg/GpgAbstractKeyGetter.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/utils/GpgUtils.h" #include "ui/UISignalStation.h" #include "ui/UserInterfaceUtils.h" +#include "ui/dialog/KeyGroupCreationDialog.h" #include "ui/dialog/import_export/KeyImportDetailDialog.h" + +// #include "ui_KeyList.h" namespace GpgFrontend::UI { +KeyList::KeyList(QWidget* parent) + : QWidget(parent), + ui_(GpgFrontend::SecureCreateSharedObject<Ui_KeyList>()), + model_(GpgAbstractKeyGetter::GetInstance(kGpgFrontendDefaultChannel) + .GetGpgKeyTableModel()), + global_column_filter_(static_cast<GpgKeyTableColumn>( + GetSettings() + .value("keys/global_columns_filter", + static_cast<unsigned int>(GpgKeyTableColumn::kALL)) + .toUInt())) { + ui_->setupUi(this); +} + KeyList::KeyList(int channel, KeyMenuAbility menu_ability, GpgKeyTableColumn fixed_columns_filter, QWidget* parent) : QWidget(parent), ui_(GpgFrontend::SecureCreateSharedObject<Ui_KeyList>()), current_gpg_context_channel_(channel), menu_ability_(menu_ability), - model_(GpgKeyGetter::GetInstance(channel).GetGpgKeyTableModel()), + model_(GpgAbstractKeyGetter::GetInstance(channel).GetGpgKeyTableModel()), fixed_columns_filter_(fixed_columns_filter), global_column_filter_(static_cast<GpgKeyTableColumn>( GetSettings() .value("keys/global_columns_filter", static_cast<unsigned int>(GpgKeyTableColumn::kALL)) .toUInt())) { + ui_->setupUi(this); init(); } void KeyList::init() { - ui_->setupUi(this); - ui_->menuWidget->setHidden(menu_ability_ == KeyMenuAbility::kNONE); ui_->refreshKeyListButton->setHidden(~menu_ability_ & KeyMenuAbility::kREFRESH); @@ -70,6 +86,7 @@ void KeyList::init() { ui_->searchBarEdit->setHidden(~menu_ability_ & KeyMenuAbility::kSEARCH_BAR); ui_->switchContextButton->setHidden(~menu_ability_ & KeyMenuAbility::kKEY_DATABASE); + ui_->keyGroupButton->setHidden(~menu_ability_ & KeyMenuAbility::kKEY_GROUP); auto* gpg_context_menu = new QMenu(this); auto* gpg_context_groups = new QActionGroup(this); @@ -243,6 +260,16 @@ void KeyList::init() { GetSettings().setValue("keys/global_columns_filter", static_cast<unsigned int>(global_column_filter_)); }); + connect(ui_->keyGroupButton, &QPushButton::clicked, this, + &KeyList::slot_new_key_group); + connect(this, &KeyList::SignalKeyChecked, this, [=]() { + auto keys = GetCheckedKeys(); + + ui_->keyGroupButton->setDisabled(keys.empty()); + for (const auto& key : keys) { + if (!key->IsHasEncrCap()) ui_->keyGroupButton->setDisabled(true); + } + }); setAcceptDrops(true); @@ -261,10 +288,10 @@ void KeyList::init() { ui_->searchBarEdit->setPlaceholderText(tr("Search for keys...")); } -void KeyList::AddListGroupTab(const QString& name, const QString& id, - GpgKeyTableDisplayMode display_mode, - GpgKeyTableProxyModel::KeyFilter search_filter, - GpgKeyTableColumn custom_columns_filter) { +auto KeyList::AddListGroupTab( + const QString& name, const QString& id, GpgKeyTableDisplayMode display_mode, + GpgKeyTableProxyModel::KeyFilter search_filter, + GpgKeyTableColumn custom_columns_filter) -> KeyTable* { auto* key_table = new KeyTable(this, model_, display_mode, custom_columns_filter, std::move(search_filter)); @@ -274,8 +301,13 @@ void KeyList::AddListGroupTab(const QString& name, const QString& id, connect(this, &KeyList::SignalColumnTypeChange, key_table, &KeyTable::SignalColumnTypeChange); + connect(key_table, &KeyTable::SignalKeyChecked, this, [=]() { + if (sender() != ui_->keyGroupTab->currentWidget()) return; + emit SignalKeyChecked(); + }); UpdateKeyTableColumnType(global_column_filter_); + return key_table; } void KeyList::SlotRefresh() { @@ -284,7 +316,7 @@ void KeyList::SlotRefresh() { LOG_D() << "request new key table module, current gpg context channel: " << current_gpg_context_channel_; - model_ = GpgKeyGetter::GetInstance(current_gpg_context_channel_) + model_ = GpgAbstractKeyGetter::GetInstance(current_gpg_context_channel_) .GetGpgKeyTableModel(); for (int i = 0; i < ui_->keyGroupTab->count(); i++) { @@ -300,112 +332,54 @@ void KeyList::SlotRefreshUI() { emit SignalRefreshStatusBar(tr("Key List Refreshed."), 1000); ui_->refreshKeyListButton->setDisabled(false); ui_->syncButton->setDisabled(false); + emit SignalKeyChecked(); } -auto KeyList::GetChecked(const KeyTable& key_table) -> KeyIdArgsList { - auto ret = KeyIdArgsList{}; - for (int i = 0; i < key_table.GetRowCount(); i++) { - if (key_table.IsRowChecked(i)) { - ret.push_back(key_table.GetKeyIdByRow(i)); - } - } - return ret; -} - -auto KeyList::GetChecked() -> KeyIdArgsList { +auto KeyList::GetCheckedKeys() -> GpgAbstractKeyPtrList { auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); - auto ret = KeyIdArgsList{}; - for (int i = 0; i < key_table->GetRowCount(); i++) { - if (key_table->IsRowChecked(i)) { - ret.push_back(key_table->GetKeyIdByRow(i)); - } - } - return ret; -} -auto KeyList::GetCheckedKeys() -> QStringList { - auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); - QStringList key_id_list; - for (int i = 0; i < key_table->GetRowCount(); i++) { - if (key_table->IsRowChecked(i)) { - key_id_list.append(key_table->GetKeyIdByRow(i)); - } - } - return key_id_list; -} + assert(key_table != nullptr); + if (key_table == nullptr) return {}; -auto KeyList::GetAllPrivateKeys() -> KeyIdArgsList { - auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); - auto ret = KeyIdArgsList{}; - for (int i = 0; i < key_table->GetRowCount(); i++) { - if (key_table->IsPrivateKeyByRow(i)) { - ret.push_back(key_table->GetKeyIdByRow(i)); - } - } - return ret; + return key_table->GetCheckedKeys(); } -auto KeyList::GetCheckedPrivateKey() -> KeyIdArgsList { - auto ret = KeyIdArgsList{}; - if (ui_->keyGroupTab->size().isEmpty()) return ret; - - auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); +auto KeyList::GetCheckedPrivateKey() -> GpgAbstractKeyPtrList { + auto ret = GpgAbstractKeyPtrList{}; - for (int i = 0; i < key_table->GetRowCount(); i++) { - if (key_table->IsRowChecked(i) && key_table->IsPrivateKeyByRow(i)) { - ret.push_back(key_table->GetKeyIdByRow(i)); - } + auto keys = GetCheckedKeys(); + for (const auto& key : keys) { + if (key->IsPrivateKey()) ret.push_back(key); } + return ret; } -auto KeyList::GetCheckedPublicKey() -> KeyIdArgsList { - auto ret = KeyIdArgsList{}; - if (ui_->keyGroupTab->size().isEmpty()) return ret; +auto KeyList::GetCheckedPublicKey() -> GpgAbstractKeyPtrList { + auto ret = GpgAbstractKeyPtrList{}; - auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); - - for (int i = 0; i < key_table->GetRowCount(); i++) { - if (key_table->IsRowChecked(i) && key_table->IsPublicKeyByRow(i)) { - ret.push_back(key_table->GetKeyIdByRow(i)); - } + auto keys = GetCheckedKeys(); + for (const auto& key : keys) { + if (!key->IsPrivateKey()) ret.push_back(key); } + return ret; } -void KeyList::SetChecked(const KeyIdArgsList& keyIds, +void KeyList::SetChecked(const KeyIdArgsList& key_ids, const KeyTable& key_table) { - if (!keyIds.empty()) { + if (!key_ids.empty()) { for (int i = 0; i < key_table.GetRowCount(); i++) { - if (std::find(keyIds.begin(), keyIds.end(), key_table.GetKeyIdByRow(i)) != - keyIds.end()) { + if (std::find( + key_ids.begin(), key_ids.end(), + key_table.GetKeyByIndex(key_table.model()->index(i, 0))->ID()) != + key_ids.end()) { key_table.SetRowChecked(i); } } } } -auto KeyList::GetSelected() -> KeyIdArgsList { - auto ret = KeyIdArgsList{}; - if (ui_->keyGroupTab->size().isEmpty()) return ret; - - auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); - if (key_table == nullptr) { - FLOG_W("fail to get current key table, nullptr"); - return ret; - } - - QItemSelectionModel* select = key_table->selectionModel(); - for (auto index : select->selectedRows()) { - ret.push_back(key_table->GetKeyIdByRow(index.row())); - } - - if (ret.empty()) { - FLOG_W("nothing is selected at key list"); - } - return ret; -} - [[maybe_unused]] auto KeyList::ContainsPrivateKeys() -> bool { if (ui_->keyGroupTab->size().isEmpty()) return false; auto* key_table = @@ -532,24 +506,29 @@ void KeyList::import_keys(const QByteArray& in_buffer) { (new KeyImportDetailDialog(current_gpg_context_channel_, result, this)); } -auto KeyList::GetSelectedKey() -> QString { - if (ui_->keyGroupTab->size().isEmpty()) return {}; - - auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); - - QItemSelectionModel* select = key_table->selectionModel(); +auto KeyList::GetSelectedKey() -> GpgAbstractKeyPtr { + return GetSelectedKeys().front(); +} - auto selected_rows = select->selectedRows(); - if (selected_rows.empty()) return {}; +auto KeyList::GetSelectedGpgKey() -> GpgKeyPtr { + return GetSelectedGpgKeys().front(); +} - return key_table->GetKeyIdByRow(selected_rows.first().row()); +auto KeyList::GetSelectedGpgKeys() -> GpgKeyPtrList { + auto keys = GetSelectedKeys(); + auto g_keys = GpgKeyPtrList{}; + for (const auto& key : keys) { + if (key->KeyType() != GpgAbstractKeyType::kGPG_KEY) continue; + g_keys.push_back(qSharedPointerDynamicCast<GpgKey>(key)); + } + return g_keys; } void KeyList::slot_sync_with_key_server() { - auto checked_public_keys = GetCheckedPublicKey(); + auto target_keys = GetCheckedPublicKey(); KeyIdArgsList key_ids; - if (checked_public_keys.empty()) { + if (target_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 " @@ -559,13 +538,12 @@ void KeyList::slot_sync_with_key_server() { if (reply == QMessageBox::No) return; - auto all_key_ids = model_->GetAllKeyIds(); - for (auto& key_id : all_key_ids) { - key_ids.push_back(key_id); - } + target_keys = model_->GetAllKeys(); + } - } else { - key_ids = checked_public_keys; + for (auto& key : target_keys) { + if (key->KeyType() != GpgAbstractKeyType::kGPG_KEY) continue; + key_ids.push_back(key->ID()); } if (key_ids.empty()) return; @@ -633,18 +611,59 @@ 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 KeyList::GetSelectedKeys() -> GpgAbstractKeyPtrList { + auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->currentWidget()); + + assert(key_table != nullptr); + if (key_table == nullptr) return {}; + + return key_table->GetSelectedKeys(); +} + +void KeyList::slot_new_key_group() { + auto keys = GetCheckedKeys(); + + QStringList proper_key_ids; + for (const auto& key : keys) { + if (!key->IsHasEncrCap()) continue; + proper_key_ids.append(key->ID()); + } + + if (proper_key_ids.isEmpty()) return; + + auto* dialog = + new KeyGroupCreationDialog(current_gpg_context_channel_, proper_key_ids); + dialog->exec(); +} + +void KeyList::UpdateKeyTableFilter( + int index, const GpgKeyTableProxyModel::KeyFilter& filter) { + if (ui_->keyGroupTab->size().isEmpty() || + ui_->keyGroupTab->widget(index) == nullptr) { + return; + } - auto key = GpgKeyGetter::GetInstance(GetCurrentGpgContextChannel()) - .GetKey(key_ids.front()); + auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->widget(index)); + key_table->SetFilter(filter); +} - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return {false, GpgKey()}; +void KeyList::RefreshKeyTable(int index) { + if (ui_->keyGroupTab->size().isEmpty() || + ui_->keyGroupTab->widget(index) == nullptr) { + return; } - return {true, key}; + auto* key_table = qobject_cast<KeyTable*>(ui_->keyGroupTab->widget(index)); + key_table->RefreshProxyModel(); +} + +void KeyList::Init(int channel, KeyMenuAbility menu_ability, + GpgKeyTableColumn fixed_column_filter) { + current_gpg_context_channel_ = channel; + menu_ability_ = menu_ability; + fixed_columns_filter_ = fixed_column_filter; + model_ = GpgAbstractKeyGetter::GetInstance(channel).GetGpgKeyTableModel(); + + init(); } } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/KeyList.h b/src/ui/widgets/KeyList.h index 868c5b07..0b0b9be6 100644 --- a/src/ui/widgets/KeyList.h +++ b/src/ui/widgets/KeyList.h @@ -47,6 +47,7 @@ enum class KeyMenuAbility : unsigned int { kCOLUMN_FILTER = 1 << 4, kSEARCH_BAR = 1 << 5, kKEY_DATABASE = 1 << 6, + kKEY_GROUP = 1 << 7, kALL = ~0U }; @@ -84,6 +85,12 @@ class KeyList : public QWidget { /** * @brief Construct a new Key List object * + */ + explicit KeyList(QWidget* parent = nullptr); + + /** + * @brief Construct a new Key List object + * * @param menu_ability * @param parent */ @@ -95,18 +102,29 @@ class KeyList : public QWidget { /** * @brief * + * @param channel + * @param menu_ability + * @param fixed_column_filter + */ + void Init(int channel, KeyMenuAbility menu_ability, + GpgKeyTableColumn fixed_column_filter = GpgKeyTableColumn::kALL); + + /** + * @brief + * * @param name * @param selectType * @param infoType * @param filter */ - void AddListGroupTab( + auto AddListGroupTab( const QString& name, const QString& id, GpgKeyTableDisplayMode display_mode = GpgKeyTableDisplayMode::kPRIVATE_KEY, GpgKeyTableProxyModel::KeyFilter search_filter = - [](const GpgKey&) -> bool { return true; }, - GpgKeyTableColumn custom_columns_filter = GpgKeyTableColumn::kALL); + [](const GpgAbstractKey*) -> bool { return true; }, + GpgKeyTableColumn custom_columns_filter = GpgKeyTableColumn::kALL) + -> KeyTable*; /** * @brief Set the Column Width object @@ -130,18 +148,11 @@ class KeyList : public QWidget { void AddSeparator(); /** - * @brief Get the Checked object - * - * @return KeyIdArgsListPtr - */ - auto GetChecked() -> KeyIdArgsList; - - /** * @brief Get the Checked Keys object * * @return QStringList */ - auto GetCheckedKeys() -> QStringList; + auto GetCheckedKeys() -> GpgAbstractKeyPtrList; /** * @brief Get the Checked object @@ -149,28 +160,21 @@ class KeyList : public QWidget { * @param key_table * @return KeyIdArgsListPtr */ - static auto GetChecked(const KeyTable& key_table) -> KeyIdArgsList; + static auto GetChecked(const KeyTable& key_table) -> GpgAbstractKeyPtrList; /** * @brief Get the Private Checked object * * @return KeyIdArgsListPtr */ - auto GetCheckedPrivateKey() -> KeyIdArgsList; + auto GetCheckedPrivateKey() -> GpgAbstractKeyPtrList; /** * @brief * * @return KeyIdArgsListPtr */ - auto GetCheckedPublicKey() -> KeyIdArgsList; - - /** - * @brief Get the All Private Keys object - * - * @return KeyIdArgsListPtr - */ - auto GetAllPrivateKeys() -> KeyIdArgsList; + auto GetCheckedPublicKey() -> GpgAbstractKeyPtrList; /** * @brief Set the Checked object @@ -182,25 +186,32 @@ class KeyList : public QWidget { const KeyTable& key_table); /** - * @brief Get the Selected object + * @brief Get the Selected Key object * - * @return KeyIdArgsListPtr + * @return QString */ - auto GetSelected() -> KeyIdArgsList; + auto GetSelectedKey() -> GpgAbstractKeyPtr; + + /** + * @brief Get the Selected Keys object + * + * @return GpgAbstractKeyPtrList + */ + auto GetSelectedKeys() -> GpgAbstractKeyPtrList; /** * @brief Get the Selected Key object * * @return QString */ - auto GetSelectedKey() -> QString; + auto GetSelectedGpgKey() -> GpgKeyPtr; /** - * @brief Get the Selected Gpg Key object + * @brief Get the Selected Keys object * - * @return GpgKey + * @return GpgAbstractKeyPtrList */ - auto GetSelectedGpgKey() -> std::tuple<bool, GpgKey>; + auto GetSelectedGpgKeys() -> GpgKeyPtrList; /** * @brief @@ -223,6 +234,33 @@ class KeyList : public QWidget { */ [[nodiscard]] auto GetCurrentGpgContextChannel() const -> int; + /** + * @brief + * + */ + void UpdateKeyTableFilter(int index, const GpgKeyTableProxyModel::KeyFilter&); + + /** + * @brief + * + * @param index + */ + void RefreshKeyTable(int index); + + public slots: + + /** + * @brief + * + */ + void SlotRefresh(); + + /** + * @brief + * + */ + void SlotRefreshUI(); + signals: /** * @brief @@ -244,52 +282,49 @@ class KeyList : public QWidget { */ void SignalColumnTypeChange(GpgKeyTableColumn); - public slots: - /** * @brief * */ - void SlotRefresh(); + void SignalKeyChecked(); + protected: /** * @brief * + * @param event */ - void SlotRefreshUI(); + void contextMenuEvent(QContextMenuEvent* event) override; - private: /** * @brief * + * @param event */ - void init(); + void dragEnterEvent(QDragEnterEvent* event) override; /** * @brief * - * @param inBuffer + * @param event */ - void import_keys(const QByteArray& in_buffer); + void dropEvent(QDropEvent* event) override; - /** - * @brief - * - */ - void uncheck_all(); + private slots: /** * @brief * */ - void check_all(); + void slot_sync_with_key_server(); /** * @brief * */ - void filter_by_keyword(); + void slot_new_key_group(); + private: std::shared_ptr<Ui_KeyList> ui_; ///< QMenu* popup_menu_{}; ///< std::function<void(const GpgKey&, QWidget*)> m_action_ = nullptr; ///< @@ -306,35 +341,36 @@ class KeyList : public QWidget { QAction* subkeys_number_column_action_; QAction* comment_column_action_; - private slots: + /** + * @brief + * + */ + void init(); /** * @brief * + * @param inBuffer */ - void slot_sync_with_key_server(); + void import_keys(const QByteArray& in_buffer); - protected: /** * @brief * - * @param event */ - void contextMenuEvent(QContextMenuEvent* event) override; + void uncheck_all(); /** * @brief * - * @param event */ - void dragEnterEvent(QDragEnterEvent* event) override; + void check_all(); /** * @brief * - * @param event */ - void dropEvent(QDropEvent* event) override; + void filter_by_keyword(); }; } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/KeyTable.cpp b/src/ui/widgets/KeyTable.cpp index 10ebe6eb..d1135279 100644 --- a/src/ui/widgets/KeyTable.cpp +++ b/src/ui/widgets/KeyTable.cpp @@ -28,16 +28,16 @@ #include "ui/widgets/KeyTable.h" -#include "core/function/gpg/GpgKeyGetter.h" #include "ui/UserInterfaceUtils.h" -#include "ui/dialog/keypair_details/KeyDetailsDialog.h" namespace GpgFrontend::UI { -auto KeyTable::GetChecked() const -> KeyIdArgsList { - auto ret = KeyIdArgsList{}; +auto KeyTable::GetCheckedKeys() const -> GpgAbstractKeyPtrList { + auto ret = GpgAbstractKeyPtrList{}; for (decltype(GetRowCount()) i = 0; i < GetRowCount(); i++) { - if (IsRowChecked(i)) ret.push_back(GetKeyIdByRow(i)); + if (IsRowChecked(i)) { + ret.push_back(GetKeyByIndex(model()->index(i, 0))); + } } return ret; } @@ -76,15 +76,20 @@ KeyTable::KeyTable(QWidget* parent, QSharedPointer<GpgKeyTableModel> model, }); connect(this, &QTableView::doubleClicked, this, [this](const QModelIndex& index) { - if (!index.isValid() || index.column() == 0) return; - - auto key = GpgKeyGetter::GetInstance(model_->GetGpgContextChannel()) - .GetKey(GetKeyIdByRow(index.row())); - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } - new KeyDetailsDialog(model_->GetGpgContextChannel(), key, this); + if (!index.isValid()) return; + + auto key = GetKeyByIndex(index); + if (key == nullptr) return; + + CommonUtils::OpenDetailsDialogByKey( + this, model_->GetGpgContextChannel(), key); + }); + + connect(&proxy_model_, &GpgKeyTableProxyModel::dataChanged, this, + [=](const QModelIndex&, const QModelIndex&, + const QContainer<int>& roles) { + if (!roles.contains(Qt::CheckStateRole)) return; + emit SignalKeyChecked(); }); } @@ -104,22 +109,22 @@ auto KeyTable::IsRowChecked(int row) const -> bool { auto KeyTable::GetRowCount() const -> int { return model()->rowCount(); } -auto KeyTable::GetKeyIdByRow(int row) const -> QString { - if (row < 0 || row >= model()->rowCount()) return {}; - auto origin_row = model()->index(row, 0).data().toInt(); - return model_->GetKeyIDByRow(origin_row); -} +auto KeyTable::GetKeyByIndex(QModelIndex index) const -> GpgAbstractKeyPtr { + auto idx = proxy_model_.mapToSource(index); + auto* i = idx.isValid() ? static_cast<GpgKeyTableItem*>(idx.internalPointer()) + : nullptr; + assert(i != nullptr); -auto KeyTable::IsPrivateKeyByRow(int row) const -> bool { - if (row < 0 || row >= model()->rowCount()) return false; - auto origin_row = model()->index(row, 0).data().toInt(); - return model_->IsPrivateKeyByRow(origin_row); + return i->SharedKey(); } -auto KeyTable::IsPublicKeyByRow(int row) const -> bool { - if (row < 0 || row >= model()->rowCount()) return false; - auto origin_row = model()->index(row, 0).data().toInt(); - return !model_->IsPrivateKeyByRow(origin_row); +auto KeyTable::GetSelectedKeys() const -> GpgAbstractKeyPtrList { + GpgAbstractKeyPtrList ret; + QItemSelectionModel* select = selectionModel(); + for (auto index : select->selectedRows()) { + ret.push_back(GetKeyByIndex(index)); + } + return ret; } void KeyTable::SetRowChecked(int row) const { @@ -148,4 +153,9 @@ void KeyTable::UncheckAll() { return selected_indexes.first().row(); } +void KeyTable::SetFilter(const GpgKeyTableProxyModel::KeyFilter& filter) { + proxy_model_.SetFilter(filter); +} + +void KeyTable::RefreshProxyModel() { proxy_model_.invalidate(); } } // namespace GpgFrontend::UI
\ No newline at end of file diff --git a/src/ui/widgets/KeyTable.h b/src/ui/widgets/KeyTable.h index fe73bc98..9f6d1ba5 100644 --- a/src/ui/widgets/KeyTable.h +++ b/src/ui/widgets/KeyTable.h @@ -54,9 +54,8 @@ struct KeyTable : public QTableView { KeyTable( QWidget* parent, QSharedPointer<GpgKeyTableModel> model, GpgKeyTableDisplayMode _select_type, GpgKeyTableColumn _info_type, - GpgKeyTableProxyModel::KeyFilter _filter = [](const GpgKey&) -> bool { - return true; - }); + GpgKeyTableProxyModel::KeyFilter _filter = + [](const GpgAbstractKey*) -> bool { return true; }); /** * @brief @@ -70,7 +69,7 @@ struct KeyTable : public QTableView { * * @return KeyIdArgsListPtr& */ - [[nodiscard]] auto GetChecked() const -> KeyIdArgsList; + [[nodiscard]] auto GetCheckedKeys() const -> GpgAbstractKeyPtrList; /** * @brief @@ -121,42 +120,45 @@ struct KeyTable : public QTableView { [[nodiscard]] auto GetRowCount() const -> int; /** - * @brief Get the Key Id By Row object + * @brief * - * @param row - * @return QString + * @param index + * @return GpgAbstractKeyPtr + */ + [[nodiscard]] auto GetKeyByIndex(QModelIndex index) const + -> GpgAbstractKeyPtr; + + /** + * @brief Get the Selected Keys object + * + * @param index + * @return GpgAbstractKeyPtrList */ - [[nodiscard]] auto GetKeyIdByRow(int row) const -> QString; + [[nodiscard]] auto GetSelectedKeys() const -> GpgAbstractKeyPtrList; /** * @brief * - * @param row - * @return true - * @return false */ - [[nodiscard]] auto IsPublicKeyByRow(int row) const -> bool; + void CheckAll(); /** * @brief * - * @param row - * @return true - * @return false */ - [[nodiscard]] auto IsPrivateKeyByRow(int row) const -> bool; + void UncheckAll(); /** * @brief * */ - void CheckAll(); + void SetFilter(const GpgKeyTableProxyModel::KeyFilter&); /** * @brief * */ - void UncheckAll(); + void RefreshProxyModel(); signals: @@ -172,6 +174,12 @@ struct KeyTable : public QTableView { */ void SignalGpgContextChannelChange(int); + /** + * @brief + * + */ + void SignalKeyChecked(); + private: QSharedPointer<GpgKeyTableModel> model_; GpgKeyTableProxyModel proxy_model_; diff --git a/src/ui/widgets/KeyTreeView.cpp b/src/ui/widgets/KeyTreeView.cpp index c0a00c4e..46cfa2db 100644 --- a/src/ui/widgets/KeyTreeView.cpp +++ b/src/ui/widgets/KeyTreeView.cpp @@ -31,9 +31,8 @@ #include <utility> #include "core/function/gpg/GpgKeyGetter.h" -#include "core/utils/GpgUtils.h" #include "ui/UISignalStation.h" -#include "ui/dialog/keypair_details/KeyDetailsDialog.h" +#include "ui/UserInterfaceUtils.h" #include "ui/model/GpgKeyTreeProxyModel.h" namespace GpgFrontend::UI { @@ -109,18 +108,13 @@ void KeyTreeView::init() { connect(this, &QTableView::doubleClicked, this, [this](const QModelIndex& index) { - if (!index.isValid() || index.column() == 0) return; + if (!index.isValid()) return; - QModelIndex source_index = proxy_model_.mapToSource(index); - auto key = - GetGpgKeyByGpgAbstractKey(model_->GetKeyByIndex(source_index)); + auto key = GetKeyByIndex(index); + if (key == nullptr) return; - if (!key.IsGood()) { - QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); - return; - } - - new KeyDetailsDialog(model_->GetGpgContextChannel(), key, this); + CommonUtils::OpenDetailsDialogByKey( + this, model_->GetGpgContextChannel(), key); }); connect(UISignalStation::GetInstance(), @@ -150,4 +144,12 @@ void KeyTreeView::SetChannel(int channel) { proxy_model_.invalidate(); } +auto KeyTreeView::GetKeyByIndex(QModelIndex index) -> GpgAbstractKeyPtr { + auto* i = index.isValid() + ? static_cast<GpgKeyTreeItem*>(index.internalPointer()) + : nullptr; + assert(i != nullptr); + + return i->SharedKey(); +} } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/KeyTreeView.h b/src/ui/widgets/KeyTreeView.h index 0e12ad0b..af903273 100644 --- a/src/ui/widgets/KeyTreeView.h +++ b/src/ui/widgets/KeyTreeView.h @@ -64,6 +64,14 @@ class KeyTreeView : public QTreeView { QWidget* parent = nullptr); /** + * @brief Get the Key By Index object + * + * @param index + * @return GpgAbstractKeyPtr + */ + static auto GetKeyByIndex(QModelIndex index) -> GpgAbstractKeyPtr; + + /** * @brief Get the All Checked Key Ids object * * @return KeyIdArgsList diff --git a/src/ui/widgets/VerifyKeyDetailBox.cpp b/src/ui/widgets/VerifyKeyDetailBox.cpp index eca7f03c..0cad1b6e 100644 --- a/src/ui/widgets/VerifyKeyDetailBox.cpp +++ b/src/ui/widgets/VerifyKeyDetailBox.cpp @@ -28,7 +28,6 @@ #include "ui/widgets/VerifyKeyDetailBox.h" -#include "core/GpgModel.h" #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/utils/CommonUtils.h" @@ -41,7 +40,8 @@ VerifyKeyDetailBox::VerifyKeyDetailBox(int channel, QWidget* parent) : QGroupBox(parent), current_gpg_context_channel_(channel), - fpr_(signature.GetFingerprint()) { + key_(GpgKeyGetter::GetInstance(channel).GetKeyPtr( + signature.GetFingerprint())) { auto* vbox = new QVBoxLayout(); switch (gpg_err_code(signature.GetStatus())) { @@ -58,7 +58,7 @@ VerifyKeyDetailBox::VerifyKeyDetailBox(int channel, connect(import_button, &QPushButton::clicked, this, &VerifyKeyDetailBox::slot_import_form_key_server); - this->setTitle(tr("Key not present with id 0x") + fpr_); + this->setTitle(tr("Key not present with id 0x") + key_->ID()); auto* grid = new QGridLayout(); @@ -166,27 +166,26 @@ VerifyKeyDetailBox::VerifyKeyDetailBox(int channel, } void VerifyKeyDetailBox::slot_import_form_key_server() { - CommonUtils::GetInstance()->ImportKeyFromKeyServer( - current_gpg_context_channel_, {fpr_}); + CommonUtils::GetInstance()->ImportGpgKeyFromKeyServer( + current_gpg_context_channel_, {key_}); } auto VerifyKeyDetailBox::create_key_info_grid(const GpgSignature& signature) -> QGridLayout* { auto* grid = new QGridLayout(); - auto key = - GpgKeyGetter::GetInstance(current_gpg_context_channel_).GetKey(fpr_); - assert(key.IsGood()); - if (!key.IsGood()) return nullptr; + assert(key_->IsGood()); + if (!key_->IsGood()) return nullptr; + grid->addWidget(new QLabel(tr("Signer Name") + ":"), 0, 0); grid->addWidget(new QLabel(tr("Signer Email") + ":"), 1, 0); grid->addWidget(new QLabel(tr("Key's Fingerprint") + ":"), 2, 0); grid->addWidget(new QLabel(tr("Valid") + ":"), 3, 0); grid->addWidget(new QLabel(tr("Flags") + ":"), 4, 0); - grid->addWidget(new QLabel(key.Name()), 0, 1); - grid->addWidget(new QLabel(key.Email()), 1, 1); - grid->addWidget(new QLabel(BeautifyFingerprint(fpr_)), 2, 1); + grid->addWidget(new QLabel(key_->Name()), 0, 1); + grid->addWidget(new QLabel(key_->Email()), 1, 1); + grid->addWidget(new QLabel(BeautifyFingerprint(key_->Fingerprint())), 2, 1); if ((signature.GetSummary() & GPGME_SIGSUM_VALID) != 0U) { grid->addWidget(new QLabel(tr("Fully Valid")), 3, 1); diff --git a/src/ui/widgets/VerifyKeyDetailBox.h b/src/ui/widgets/VerifyKeyDetailBox.h index 55bcb7ec..39f2aaca 100644 --- a/src/ui/widgets/VerifyKeyDetailBox.h +++ b/src/ui/widgets/VerifyKeyDetailBox.h @@ -65,7 +65,7 @@ class VerifyKeyDetailBox : public QGroupBox { QGridLayout* create_key_info_grid(const GpgSignature& signature); int current_gpg_context_channel_; - QString fpr_; ///< fingerprint of the key + GpgKeyPtr key_; ///< fingerprint of the key }; } // namespace GpgFrontend::UI |