From 92bf7c94d025139bd63a300821c971b37172543d Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Sun, 21 Oct 2012 20:45:01 +0200 Subject: [PATCH] Whitespace separated search terms are AND concatenated. --- src/core/Entry.cpp | 19 +++++++++++++++---- src/core/Entry.h | 1 + tests/TestGroup.cpp | 31 +++++++++++++++++++++++++++++++ tests/TestGroup.h | 1 + 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index bf79a5efe..8aac43d6d 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -536,10 +536,21 @@ const Database* Entry::database() const bool Entry::match(const QString& searchTerm, Qt::CaseSensitivity caseSensitivity) { - return title().contains(searchTerm, caseSensitivity) || - username().contains(searchTerm, caseSensitivity) || - url().contains(searchTerm, caseSensitivity) || - notes().contains(searchTerm, caseSensitivity); + QStringList wordList = searchTerm.split(QRegExp("\\s"), QString::SkipEmptyParts); + Q_FOREACH (const QString& word, wordList) { + if (!wordMatch(word, caseSensitivity)) { + return false; + } + } + return true; +} + +bool Entry::wordMatch(const QString& word, Qt::CaseSensitivity caseSensitivity) +{ + return title().contains(word, caseSensitivity) || + username().contains(word, caseSensitivity) || + url().contains(word, caseSensitivity) || + notes().contains(word, caseSensitivity); } QString Entry::resolvePlaceholders(const QString& str) const diff --git a/src/core/Entry.h b/src/core/Entry.h index 9c32ac4b5..0a1c9a3dd 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -141,6 +141,7 @@ private Q_SLOTS: void updateModifiedSinceBegin(); private: + bool wordMatch(const QString& word, Qt::CaseSensitivity caseSensitivity); const Database* database() const; template bool set(T& property, const T& value); diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index 169178494..eab686e68 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -399,4 +399,35 @@ void TestGroup::testSearch() delete groupRoot; } +void TestGroup::testAndConcatenationInSearch() +{ + Group* group = new Group(); + Entry* entry = new Entry(); + entry->setNotes("abc def ghi"); + entry->setTitle("jkl"); + entry->setGroup(group); + + QList searchResult; + + searchResult = group->search("", Qt::CaseInsensitive); + QCOMPARE(searchResult.count(), 1); + + searchResult = group->search("def", Qt::CaseInsensitive); + QCOMPARE(searchResult.count(), 1); + + searchResult = group->search(" abc ghi ", Qt::CaseInsensitive); + QCOMPARE(searchResult.count(), 1); + + searchResult = group->search("ghi ef", Qt::CaseInsensitive); + QCOMPARE(searchResult.count(), 1); + + searchResult = group->search("abc ef xyz", Qt::CaseInsensitive); + QCOMPARE(searchResult.count(), 0); + + searchResult = group->search("abc kl", Qt::CaseInsensitive); + QCOMPARE(searchResult.count(), 1); + + delete group; +} + QTEST_GUILESS_MAIN(TestGroup) diff --git a/tests/TestGroup.h b/tests/TestGroup.h index 6e5307dcf..49ad6dfe9 100644 --- a/tests/TestGroup.h +++ b/tests/TestGroup.h @@ -32,6 +32,7 @@ private Q_SLOTS: void testDeleteSignals(); void testCopyCustomIcon(); void testSearch(); + void testAndConcatenationInSearch(); }; #endif // KEEPASSX_TESTGROUP_H