mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-10-01 01:26:01 -04:00
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
This commit is contained in:
parent
a5e7da67d8
commit
d8d758f0e1
@ -22,47 +22,44 @@
|
||||
|
||||
QList<Entry*> EntrySearcher::search(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity)
|
||||
{
|
||||
if (!group->resolveSearchingEnabled()) {
|
||||
return QList<Entry*>();
|
||||
QList<Entry*> results;
|
||||
|
||||
if (group->resolveSearchingEnabled()) {
|
||||
results.append(searchEntries(searchTerm, group->entries(), caseSensitivity));
|
||||
}
|
||||
|
||||
return searchEntries(searchTerm, group, caseSensitivity);
|
||||
}
|
||||
|
||||
QList<Entry*>
|
||||
EntrySearcher::searchEntries(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity)
|
||||
{
|
||||
QList<Entry*> searchResult;
|
||||
|
||||
const QList<Entry*>& entryList = group->entries();
|
||||
for (Entry* entry : entryList) {
|
||||
searchResult.append(matchEntry(searchTerm, entry, caseSensitivity));
|
||||
}
|
||||
|
||||
const QList<Group*>& 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<Entry*> EntrySearcher::matchEntry(const QString& searchTerm, Entry* entry, Qt::CaseSensitivity caseSensitivity)
|
||||
QList<Entry*> EntrySearcher::searchEntries(const QString& searchTerm, const QList<Entry*>& entries,
|
||||
Qt::CaseSensitivity caseSensitivity)
|
||||
{
|
||||
QList<Entry*> 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<Entry*>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return QList<Entry*>() << 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);
|
||||
}
|
||||
|
@ -30,11 +30,9 @@ public:
|
||||
QList<Entry*> search(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity);
|
||||
|
||||
private:
|
||||
QList<Entry*> searchEntries(const QString& searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity);
|
||||
QList<Entry*> matchEntry(const QString& searchTerm, Entry* entry, Qt::CaseSensitivity caseSensitivity);
|
||||
QList<Entry*> searchEntries(const QString& searchTerm, const QList<Entry*>& 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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ signals:
|
||||
void mainSplitterSizesChanged();
|
||||
void previewSplitterSizesChanged();
|
||||
void entryViewStateChanged();
|
||||
void updateSearch(QString text);
|
||||
void clearSearch();
|
||||
|
||||
public slots:
|
||||
void createEntry();
|
||||
|
@ -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()));
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user