Move search into separate class.

This commit is contained in:
Florian Geyer 2014-05-15 23:48:54 +02:00
parent e361b0dd81
commit 8bf4826003
11 changed files with 264 additions and 129 deletions

View File

@ -35,6 +35,7 @@ set(keepassx_SOURCES
core/Entry.cpp
core/EntryAttachments.cpp
core/EntryAttributes.cpp
core/EntrySearcher.cpp
core/FilePath.cpp
core/Global.h
core/Group.cpp

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "EntrySearcher.h"
#include "core/Group.h"
QList<Entry*> EntrySearcher::search(const QString &searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity)
{
if (!group->resolveSearchingEnabled()) {
return QList<Entry*>();
}
return searchEntries(searchTerm, group, caseSensitivity);
}
QList<Entry*> EntrySearcher::searchEntries(const QString &searchTerm, const Group* group, Qt::CaseSensitivity caseSensitivity)
{
QList<Entry*> searchResult;
Q_FOREACH (Entry* entry, group->entries()) {
if (entry->match(searchTerm, caseSensitivity)) {
searchResult.append(entry);
}
}
Q_FOREACH (Group* childGroup, group->children()) {
if (childGroup->searchingEnabled() != Group::Disable) {
searchResult.append(searchEntries(searchTerm, childGroup, caseSensitivity));
}
}
return searchResult;
}

35
src/core/EntrySearcher.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2014 Florian Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_ENTRYSEARCHER_H
#define KEEPASSX_ENTRYSEARCHER_H
#include <QString>
class Group;
class Entry;
class EntrySearcher
{
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);
};
#endif // KEEPASSX_ENTRYSEARCHER_H

View File

@ -612,33 +612,6 @@ void Group::recCreateDelObjects()
}
}
QList<Entry*> Group::search(const QString& searchTerm, Qt::CaseSensitivity caseSensitivity,
bool resolveInherit)
{
QList<Entry*> searchResult;
bool search;
if (resolveInherit) {
search = resolveSearchingEnabled();
}
else if (searchingEnabled() == Disable) {
search = false;
}
else {
search = true;
}
if (search) {
Q_FOREACH (Entry* entry, m_entries) {
if (entry->match(searchTerm, caseSensitivity)) {
searchResult.append(entry);
}
}
Q_FOREACH (Group* group, m_children) {
searchResult.append(group->search(searchTerm, caseSensitivity, false));
}
}
return searchResult;
}
bool Group::resolveSearchingEnabled() const
{
switch (m_data.searchingEnabled) {

View File

@ -113,9 +113,6 @@ public:
void copyDataFrom(const Group* other);
Database* exportToDb();
QList<Entry*> search(const QString& searchTerm, Qt::CaseSensitivity caseSensitivity,
bool resolveInherit = true);
Q_SIGNALS:
void dataChanged(Group* group);

View File

@ -28,6 +28,7 @@
#include "autotype/AutoType.h"
#include "core/Config.h"
#include "core/EntrySearcher.h"
#include "core/FilePath.h"
#include "core/Group.h"
#include "core/Metadata.h"
@ -754,8 +755,8 @@ void DatabaseWidget::search()
else {
sensitivity = Qt::CaseInsensitive;
}
QList<Entry*> searchResult = searchGroup->search(m_searchUi->searchEdit->text(), sensitivity);
QList<Entry*> searchResult = EntrySearcher().search(m_searchUi->searchEdit->text(), searchGroup, sensitivity);
m_entryView->setEntryList(searchResult);
}

View File

@ -165,6 +165,9 @@ add_unit_test(NAME testqcommandlineparser SOURCES TestQCommandLineParser.cpp MOC
add_unit_test(NAME testrandom SOURCES TestRandom.cpp MOCS TestRandom.h
LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testentrysearcher SOURCES TestEntrySearcher.cpp MOCS TestEntrySearcher.h
LIBS ${TEST_LIBRARIES})
if(WITH_GUI_TESTS)
add_subdirectory(gui)
endif(WITH_GUI_TESTS)

141
tests/TestEntrySearcher.cpp Normal file
View File

@ -0,0 +1,141 @@
/*
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestEntrySearcher.h"
#include <QTest>
#include "tests.h"
#include "core/EntrySearcher.h"
#include "core/Group.h"
QTEST_GUILESS_MAIN(TestEntrySearcher)
void TestEntrySearcher::initTestCase()
{
}
void TestEntrySearcher::testSearch()
{
Group* groupRoot = new Group();
Group* group1 = new Group();
Group* group2 = new Group();
Group* group3 = new Group();
group1->setParent(groupRoot);
group2->setParent(groupRoot);
group3->setParent(groupRoot);
Group* group11 = new Group();
group11->setParent(group1);
Group* group21 = new Group();
Group* group211 = new Group();
Group* group2111 = new Group();
group21->setParent(group2);
group211->setParent(group21);
group2111->setParent(group211);
group1->setSearchingEnabled(Group::Disable);
group11->setSearchingEnabled(Group::Enable);
Entry* eRoot = new Entry();
eRoot->setNotes("test search term test");
eRoot->setGroup(groupRoot);
Entry* eRoot2 = new Entry();
eRoot2->setNotes("test term test");
eRoot2->setGroup(groupRoot);
Entry* e1 = new Entry();
e1->setNotes("test search term test");
e1->setGroup(group1);
Entry* e11 = new Entry();
e11->setNotes("test search term test");
e11->setGroup(group11);
Entry* e2111 = new Entry();
e2111->setNotes("test search term test");
e2111->setGroup(group2111);
Entry* e2111b = new Entry();
e2111b->setNotes("test search test");
e2111b->setGroup(group2111);
Entry* e3 = new Entry();
e3->setNotes("test search term test");
e3->setGroup(group3);
Entry* e3b = new Entry();
e3b->setNotes("test search test");
e3b->setGroup(group3);
QList<Entry*> searchResult;
EntrySearcher entrySearcher;
searchResult = entrySearcher.search("search term", groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 3);
searchResult = entrySearcher.search("search term", group211, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search("search term", group11, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search("search term", group1, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 0);
delete groupRoot;
}
void TestEntrySearcher::testAndConcatenationInSearch()
{
Group* group = new Group();
Entry* entry = new Entry();
entry->setNotes("abc def ghi");
entry->setTitle("jkl");
entry->setGroup(group);
EntrySearcher entrySearcher;
QList<Entry*> searchResult;
searchResult = entrySearcher.search("", group, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search("def", group, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search(" abc ghi ", group, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search("ghi ef", group, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search("abc ef xyz", group, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 0);
searchResult = entrySearcher.search("abc kl", group, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
delete group;
}

35
tests/TestEntrySearcher.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_TESTENTRYSEARCHER_H
#define KEEPASSX_TESTENTRYSEARCHER_H
#include <QObject>
class TestEntrySearcher : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testAndConcatenationInSearch();
void testSearch();
};
#endif // KEEPASSX_TESTENTRYSEARCHER_H

View File

@ -334,102 +334,6 @@ void TestGroup::testCopyCustomIcon()
delete dbTarget;
}
void TestGroup::testSearch()
{
Group* groupRoot = new Group();
Group* group1 = new Group();
Group* group2 = new Group();
Group* group3 = new Group();
group1->setParent(groupRoot);
group2->setParent(groupRoot);
group3->setParent(groupRoot);
Group* group11 = new Group();
group11->setParent(group1);
Group* group21 = new Group();
Group* group211 = new Group();
Group* group2111 = new Group();
group21->setParent(group2);
group211->setParent(group21);
group2111->setParent(group211);
group1->setSearchingEnabled(Group::Disable);
group11->setSearchingEnabled(Group::Enable);
Entry* eRoot = new Entry();
eRoot->setNotes("test search term test");
eRoot->setGroup(groupRoot);
Entry* eRoot2 = new Entry();
eRoot2->setNotes("test term test");
eRoot2->setGroup(groupRoot);
Entry* e1 = new Entry();
e1->setNotes("test search term test");
e1->setGroup(group1);
Entry* e2111 = new Entry();
e2111->setNotes("test search term test");
e2111->setGroup(group2111);
Entry* e2111b = new Entry();
e2111b->setNotes("test search test");
e2111b->setGroup(group2111);
Entry* e3 = new Entry();
e3->setNotes("test search term test");
e3->setGroup(group3);
Entry* e3b = new Entry();
e3b->setNotes("test search test");
e3b->setGroup(group3);
QList<Entry*> searchResult;
searchResult = groupRoot->search("search term", Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 3);
searchResult = group211->search("search term", Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
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;
}
void TestGroup::testClone()
{
Database* db = new Database();

View File

@ -31,8 +31,6 @@ private Q_SLOTS:
void testEntries();
void testDeleteSignals();
void testCopyCustomIcon();
void testSearch();
void testAndConcatenationInSearch();
void testClone();
void testCopyCustomIcons();
void testExportToDb();