Whitespace separated search terms are AND concatenated.

This commit is contained in:
Florian Geyer 2012-10-21 20:45:01 +02:00
parent 4d263a09db
commit 92bf7c94d0
4 changed files with 48 additions and 4 deletions

View File

@ -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

View File

@ -141,6 +141,7 @@ private Q_SLOTS:
void updateModifiedSinceBegin();
private:
bool wordMatch(const QString& word, Qt::CaseSensitivity caseSensitivity);
const Database* database() const;
template <class T> bool set(T& property, const T& value);

View File

@ -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<Entry*> 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)

View File

@ -32,6 +32,7 @@ private Q_SLOTS:
void testDeleteSignals();
void testCopyCustomIcon();
void testSearch();
void testAndConcatenationInSearch();
};
#endif // KEEPASSX_TESTGROUP_H