From d8d758f0e17d616840d2321476733b497a5c1b3a Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Mon, 19 Mar 2018 23:16:22 -0400 Subject: [PATCH] Streamlined searcher code * Remove searching of group title and notes * End search when selecting a new group * Correct entry searcher tests to align with new code --- src/core/EntrySearcher.cpp | 68 +++++++++++++------------------------ src/core/EntrySearcher.h | 6 ++-- src/gui/DatabaseWidget.cpp | 7 ++-- src/gui/DatabaseWidget.h | 2 +- src/gui/SearchWidget.cpp | 1 + src/gui/SearchWidget.h | 8 ++--- tests/TestEntrySearcher.cpp | 7 ++-- 7 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/core/EntrySearcher.cpp b/src/core/EntrySearcher.cpp index 3413f1cd0..b181ad389 100644 --- a/src/core/EntrySearcher.cpp +++ b/src/core/EntrySearcher.cpp @@ -22,47 +22,44 @@ QList EntrySearcher::search(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity) { - if (!group->resolveSearchingEnabled()) { - return QList(); + QList results; + + if (group->resolveSearchingEnabled()) { + results.append(searchEntries(searchTerm, group->entries(), caseSensitivity)); } - return searchEntries(searchTerm, group, caseSensitivity); -} - -QList -EntrySearcher::searchEntries(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity) -{ - QList searchResult; - - const QList& entryList = group->entries(); - for (Entry* entry : entryList) { - searchResult.append(matchEntry(searchTerm, entry, caseSensitivity)); - } - - const QList& children = group->children(); - for (Group* childGroup : children) { - if (childGroup->searchingEnabled() != Group::Disable) { - if (matchGroup(searchTerm, childGroup, caseSensitivity)) { - searchResult.append(childGroup->entriesRecursive()); - } else { - searchResult.append(searchEntries(searchTerm, childGroup, caseSensitivity)); - } + for (Group* childGroup : group->children()) { + if (childGroup->resolveSearchingEnabled()) { + results.append(searchEntries(searchTerm, childGroup->entries(), caseSensitivity)); } } - return searchResult; + return results; } -QList EntrySearcher::matchEntry(const QString& searchTerm, Entry* entry, Qt::CaseSensitivity caseSensitivity) +QList EntrySearcher::searchEntries(const QString& searchTerm, const QList& entries, + Qt::CaseSensitivity caseSensitivity) +{ + QList results; + for (Entry* entry : entries) { + if (matchEntry(searchTerm, entry, caseSensitivity)) { + results.append(entry); + } + } + return results; +} + +bool EntrySearcher::matchEntry(const QString& searchTerm, Entry* entry, + Qt::CaseSensitivity caseSensitivity) { const QStringList wordList = searchTerm.split(QRegExp("\\s"), QString::SkipEmptyParts); for (const QString& word : wordList) { if (!wordMatch(word, entry, caseSensitivity)) { - return QList(); + return false; } } - return QList() << entry; + return true; } bool EntrySearcher::wordMatch(const QString& word, Entry* entry, Qt::CaseSensitivity caseSensitivity) @@ -72,20 +69,3 @@ bool EntrySearcher::wordMatch(const QString& word, Entry* entry, Qt::CaseSensiti || entry->resolvePlaceholder(entry->url()).contains(word, caseSensitivity) || entry->resolvePlaceholder(entry->notes()).contains(word, caseSensitivity); } - -bool EntrySearcher::matchGroup(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity) -{ - const QStringList wordList = searchTerm.split(QRegExp("\\s"), QString::SkipEmptyParts); - for (const QString& word : wordList) { - if (!wordMatch(word, group, caseSensitivity)) { - return false; - } - } - - return true; -} - -bool EntrySearcher::wordMatch(const QString& word, const Group* group, Qt::CaseSensitivity caseSensitivity) -{ - return group->name().contains(word, caseSensitivity) || group->notes().contains(word, caseSensitivity); -} diff --git a/src/core/EntrySearcher.h b/src/core/EntrySearcher.h index 343734737..def5eb8f6 100644 --- a/src/core/EntrySearcher.h +++ b/src/core/EntrySearcher.h @@ -30,11 +30,9 @@ public: QList search(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity); private: - QList searchEntries(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity); - QList matchEntry(const QString& searchTerm, Entry* entry, Qt::CaseSensitivity caseSensitivity); + QList searchEntries(const QString& searchTerm, const QList& entries, Qt::CaseSensitivity caseSensitivity); + bool matchEntry(const QString& searchTerm, Entry* entry, Qt::CaseSensitivity caseSensitivity); bool wordMatch(const QString& word, Entry* entry, Qt::CaseSensitivity caseSensitivity); - bool matchGroup(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity); - bool wordMatch(const QString& word, const Group* group, Qt::CaseSensitivity caseSensitivity); }; #endif // KEEPASSX_ENTRYSEARCHER_H diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 3faf43a65..a4f014bf6 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -1047,9 +1047,12 @@ void DatabaseWidget::setSearchLimitGroup(bool state) void DatabaseWidget::onGroupChanged(Group* group) { - // Intercept group changes if in search mode - if (isInSearchMode()) { + if (isInSearchMode() && m_searchLimitGroup) { + // Perform new search if we are limiting search to the current group search(m_lastSearchText); + } else if (isInSearchMode()) { + // Otherwise cancel search + emit clearSearch(); } else { m_entryView->setGroup(group); } diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index a5cf538d7..8f268c94a 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -138,7 +138,7 @@ signals: void mainSplitterSizesChanged(); void previewSplitterSizesChanged(); void entryViewStateChanged(); - void updateSearch(QString text); + void clearSearch(); public slots: void createEntry(); diff --git a/src/gui/SearchWidget.cpp b/src/gui/SearchWidget.cpp index 40c63036c..96bd05a5b 100644 --- a/src/gui/SearchWidget.cpp +++ b/src/gui/SearchWidget.cpp @@ -113,6 +113,7 @@ void SearchWidget::connectSignals(SignalMultiplexer& mx) mx.connect(this, SIGNAL(limitGroupChanged(bool)), SLOT(setSearchLimitGroup(bool))); mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword())); mx.connect(this, SIGNAL(downPressed()), SLOT(setFocus())); + mx.connect(SIGNAL(clearSearch()), m_ui->searchEdit, SLOT(clear())); mx.connect(m_ui->searchEdit, SIGNAL(returnPressed()), SLOT(switchToEntryEdit())); } diff --git a/src/gui/SearchWidget.h b/src/gui/SearchWidget.h index 0ec3287c1..39e17bcf4 100644 --- a/src/gui/SearchWidget.h +++ b/src/gui/SearchWidget.h @@ -36,14 +36,16 @@ class SearchWidget : public QWidget public: explicit SearchWidget(QWidget* parent = nullptr); - ~SearchWidget(); + ~SearchWidget() override; + + Q_DISABLE_COPY(SearchWidget) void connectSignals(SignalMultiplexer& mx); void setCaseSensitive(bool state); void setLimitGroup(bool state); protected: - bool eventFilter(QObject* obj, QEvent* event); + bool eventFilter(QObject* obj, QEvent* event) override; signals: void search(const QString& text); @@ -69,8 +71,6 @@ private: QTimer* m_searchTimer; QAction* m_actionCaseSensitive; QAction* m_actionLimitGroup; - - Q_DISABLE_COPY(SearchWidget) }; #endif // SEARCHWIDGET_H diff --git a/tests/TestEntrySearcher.cpp b/tests/TestEntrySearcher.cpp index 659f7a489..0c0a2c3e4 100644 --- a/tests/TestEntrySearcher.cpp +++ b/tests/TestEntrySearcher.cpp @@ -53,7 +53,6 @@ void TestEntrySearcher::testSearch() group2111->setParent(group211); group1->setSearchingEnabled(Group::Disable); - group11->setSearchingEnabled(Group::Enable); Entry* eRoot = new Entry(); eRoot->setNotes("test search term test"); @@ -88,15 +87,13 @@ void TestEntrySearcher::testSearch() e3b->setGroup(group3); m_searchResult = m_entrySearcher.search("search term", m_groupRoot, Qt::CaseInsensitive); - QCOMPARE(m_searchResult.count(), 3); + QCOMPARE(m_searchResult.count(), 2); m_searchResult = m_entrySearcher.search("search term", group211, Qt::CaseInsensitive); QCOMPARE(m_searchResult.count(), 1); + // Parent group disabled search m_searchResult = m_entrySearcher.search("search term", group11, Qt::CaseInsensitive); - QCOMPARE(m_searchResult.count(), 1); - - m_searchResult = m_entrySearcher.search("search term", group1, Qt::CaseInsensitive); QCOMPARE(m_searchResult.count(), 0); }