aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/KeyList.cpp
diff options
context:
space:
mode:
authorSaturn&Eric <[email protected]>2023-07-13 16:51:19 +0000
committerGitHub <[email protected]>2023-07-13 16:51:19 +0000
commit40bcaec6c8c0c363bf793745131a2e6d0274fd6d (patch)
tree8dc11045bee7caf8a1b936ee2203dd39bec59e07 /src/ui/widgets/KeyList.cpp
parentMerge pull request #102 from CDmking/main (diff)
parentMerge branch 'main' into dev/2.1.0/main (diff)
downloadGpgFrontend-40bcaec6c8c0c363bf793745131a2e6d0274fd6d.tar.gz
GpgFrontend-40bcaec6c8c0c363bf793745131a2e6d0274fd6d.zip
Merge pull request #106 from saturneric/dev/2.1.0/main
Develop 2.1.1.4
Diffstat (limited to 'src/ui/widgets/KeyList.cpp')
-rw-r--r--src/ui/widgets/KeyList.cpp96
1 files changed, 90 insertions, 6 deletions
diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp
index 2d4c925a..e411e036 100644
--- a/src/ui/widgets/KeyList.cpp
+++ b/src/ui/widgets/KeyList.cpp
@@ -35,9 +35,11 @@
#include "core/GpgCoreInit.h"
#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgKeyGetter.h"
+#include "spdlog/spdlog.h"
#include "ui/SignalStation.h"
#include "ui/UserInterfaceUtils.h"
#include "ui_KeyList.h"
+#include "widgets/TextEdit.h"
namespace GpgFrontend::UI {
@@ -56,6 +58,7 @@ void KeyList::init() {
KeyMenuAbility::REFRESH);
ui_->syncButton->setHidden(~menu_ability_ & KeyMenuAbility::SYNC_PUBLIC_KEY);
ui_->uncheckButton->setHidden(~menu_ability_ & KeyMenuAbility::UNCHECK_ALL);
+ ui_->searchBarEdit->setHidden(~menu_ability_ & KeyMenuAbility::SEARCH_BAR);
ui_->keyGroupTab->clear();
popup_menu_ = new QMenu(this);
@@ -73,6 +76,8 @@ void KeyList::init() {
connect(SignalStation::GetInstance(),
&SignalStation::SignalKeyDatabaseRefreshDone, this,
&KeyList::SlotRefresh);
+ connect(SignalStation::GetInstance(), &SignalStation::SignalUIRefresh, this,
+ &KeyList::SlotRefreshUI);
// register key database sync signal for refresh button
connect(ui_->refreshKeyListButton, &QPushButton::clicked, this,
@@ -84,6 +89,8 @@ void KeyList::init() {
&KeyList::check_all);
connect(ui_->syncButton, &QPushButton::clicked, this,
&KeyList::slot_sync_with_key_server);
+ connect(ui_->searchBarEdit, &QLineEdit::textChanged, this,
+ &KeyList::filter_by_keyword);
connect(this, &KeyList::SignalRefreshStatusBar, SignalStation::GetInstance(),
&SignalStation::SignalRefreshStatusBar);
@@ -101,20 +108,23 @@ void KeyList::init() {
ui_->checkALLButton->setText(_("Check ALL"));
ui_->checkALLButton->setToolTip(
_("Check all items in the current tab at once"));
+ ui_->searchBarEdit->setPlaceholderText(_("Search for keys..."));
}
-void KeyList::AddListGroupTab(
- const QString& name, KeyListRow::KeyType selectType,
- KeyListColumn::InfoType infoType,
- const std::function<bool(const GpgKey&)>& filter) {
+void KeyList::AddListGroupTab(const QString& name, const QString& id,
+ KeyListRow::KeyType selectType,
+ KeyListColumn::InfoType infoType,
+ const KeyTable::KeyTableFilter filter) {
SPDLOG_DEBUG("add tab: {}", name.toStdString());
auto key_list = new QTableWidget(this);
if (m_key_list_ == nullptr) {
m_key_list_ = key_list;
}
+ key_list->setObjectName(id);
ui_->keyGroupTab->addTab(key_list, name);
m_key_tables_.emplace_back(key_list, selectType, infoType, filter);
+ m_key_tables_.back().SetMenuAbility(menu_ability_);
key_list->setColumnCount(7);
key_list->horizontalHeader()->setSectionResizeMode(
@@ -155,7 +165,7 @@ void KeyList::AddListGroupTab(
QStringList labels;
labels << _("Select") << _("Type") << _("Name") << _("Email Address")
- << _("Usage") << _("Validity") << _("Finger Print");
+ << _("Usage") << _("Trust") << _("Finger Print");
key_list->setHorizontalHeaderLabels(labels);
key_list->horizontalHeader()->setStretchLastSection(false);
@@ -175,6 +185,11 @@ void KeyList::SlotRefresh() {
this->slot_refresh_ui();
}
+void KeyList::SlotRefreshUI() {
+ SPDLOG_DEBUG("refresh, address: {}", static_cast<void*>(this));
+ this->slot_refresh_ui();
+}
+
KeyIdArgsListPtr KeyList::GetChecked(const KeyTable& key_table) {
auto ret = std::make_unique<KeyIdArgsList>();
for (int i = 0; i < key_table.key_list_->rowCount(); i++) {
@@ -297,6 +312,30 @@ void KeyList::contextMenuEvent(QContextMenuEvent* event) {
if (ui_->keyGroupTab->size().isEmpty()) return;
m_key_list_ = qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
+ QString current_tab_widget_obj_name =
+ ui_->keyGroupTab->widget(ui_->keyGroupTab->currentIndex())->objectName();
+ SPDLOG_DEBUG("current tab widget object name: {}",
+ current_tab_widget_obj_name.toStdString());
+ if (current_tab_widget_obj_name == "favourite") {
+ QList<QAction*> actions = popup_menu_->actions();
+ for (QAction* action : actions) {
+ if (action->data().toString() == "remove_key_from_favourtie_action") {
+ action->setVisible(true);
+ } else if (action->data().toString() == "add_key_2_favourite_action") {
+ action->setVisible(false);
+ }
+ }
+ } else {
+ QList<QAction*> actions = popup_menu_->actions();
+ for (QAction* action : actions) {
+ if (action->data().toString() == "remove_key_from_favourtie_action") {
+ action->setVisible(false);
+ } else if (action->data().toString() == "add_key_2_favourite_action") {
+ action->setVisible(true);
+ }
+ }
+ }
+
if (m_key_list_->selectedItems().length() > 0) {
popup_menu_->exec(event->globalPos());
}
@@ -426,6 +465,7 @@ void KeyList::slot_refresh_ui() {
SPDLOG_DEBUG("refresh: {}", static_cast<void*>(buffered_keys_list_.get()));
if (buffered_keys_list_ != nullptr) {
std::lock_guard<std::mutex> guard(buffered_key_list_mutex_);
+
for (auto& key_table : m_key_tables_) {
key_table.Refresh(
GpgKeyGetter::GetInstance().GetKeysCopy(buffered_keys_list_));
@@ -473,6 +513,20 @@ void KeyList::slot_sync_with_key_server() {
});
}
+void KeyList::filter_by_keyword() {
+ auto keyword = ui_->searchBarEdit->text();
+ keyword = keyword.trimmed();
+
+ SPDLOG_DEBUG("get new keyword of search bar: {}", keyword.toStdString());
+ for (auto& table : m_key_tables_) {
+ // refresh arguments
+ table.SetFilterKeyword(keyword.toLower().toStdString());
+ table.SetMenuAbility(menu_ability_);
+ }
+ // refresh ui
+ SlotRefreshUI();
+}
+
void KeyList::uncheck_all() {
auto key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
@@ -539,8 +593,30 @@ void KeyTable::Refresh(KeyLinkListPtr m_keys) {
while (it != keys->end()) {
SPDLOG_DEBUG("filtering key id: {}", it->GetId());
+ // filter by search bar's keyword
+ if (ability_ & KeyMenuAbility::SEARCH_BAR && !keyword_.empty()) {
+ auto name = it->GetName();
+ std::transform(name.begin(), name.end(), name.begin(),
+ [](unsigned char c) { return std::tolower(c); });
+
+ auto email = it->GetEmail();
+ std::transform(email.begin(), email.end(), email.begin(),
+ [](unsigned char c) { return std::tolower(c); });
+
+ auto comment = it->GetComment();
+ std::transform(comment.begin(), comment.end(), comment.begin(),
+ [](unsigned char c) { return std::tolower(c); });
+
+ if (name.find(keyword_) == std::string::npos &&
+ email.find(keyword_) == std::string::npos &&
+ comment.find(keyword_) == std::string::npos) {
+ it = keys->erase(it);
+ continue;
+ }
+ }
+
if (filter_ != nullptr) {
- if (!filter_(*it)) {
+ if (!filter_(*it, *this)) {
it = keys->erase(it);
continue;
}
@@ -659,4 +735,12 @@ void KeyTable::CheckALL() const {
key_list_->item(i, 0)->setCheckState(Qt::Checked);
}
}
+
+void KeyTable::SetMenuAbility(KeyMenuAbility::AbilityType ability) {
+ this->ability_ = ability;
+}
+
+void KeyTable::SetFilterKeyword(std::string keyword) {
+ this->keyword_ = keyword;
+}
} // namespace GpgFrontend::UI