Skip to content

Commit

Permalink
Corrections based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
droidmonkey committed Oct 30, 2018
1 parent 5a375cc commit 0162f2a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 deletions.
41 changes: 25 additions & 16 deletions src/core/Group.cpp
Expand Up @@ -550,11 +550,13 @@ QList<Entry*> Group::entriesRecursive(bool includeHistoryItems) const

Entry* Group::findEntryByUuid(const QUuid& uuid) const
{
if (!uuid.isNull()) {
for (Entry* entry : entriesRecursive(false)) {
if (entry->uuid() == uuid) {
return entry;
}
if (uuid.isNull()) {
return nullptr;
}

for (Entry* entry : entriesRecursive(false)) {
if (entry->uuid() == uuid) {
return entry;
}
}

Expand All @@ -573,10 +575,10 @@ Entry* Group::findEntryByPath(QString entryPath)
if (!normalizedEntryPath.startsWith("/") && normalizedEntryPath.contains("/")) {
normalizedEntryPath = "/" + normalizedEntryPath;
}
return findEntryByPathRecursion(normalizedEntryPath, "/");
return findEntryByPathRecursive(normalizedEntryPath, "/");
}

Entry* Group::findEntryByPathRecursion(QString entryPath, QString basePath)
Entry* Group::findEntryByPathRecursive(QString entryPath, QString basePath)
{
// Return the first entry that matches the full path OR if there is no leading
// slash, return the first entry title that matches
Expand All @@ -588,7 +590,7 @@ Entry* Group::findEntryByPathRecursion(QString entryPath, QString basePath)
}

for (Group* group : children()) {
Entry* entry = group->findEntryByPathRecursion(entryPath, basePath + group->name() + "/");
Entry* entry = group->findEntryByPathRecursive(entryPath, basePath + group->name() + "/");
if (entry != nullptr) {
return entry;
}
Expand All @@ -609,10 +611,10 @@ Group* Group::findGroupByPath(QString groupPath)
+ groupPath
+ (groupPath.endsWith("/") ? "" : "/");
}
return findGroupByPathRecursion(normalizedGroupPath, "/");
return findGroupByPathRecursive(normalizedGroupPath, "/");
}

Group* Group::findGroupByPathRecursion(QString groupPath, QString basePath)
Group* Group::findGroupByPathRecursive(QString groupPath, QString basePath)
{
// paths must be normalized
Q_ASSERT(groupPath.startsWith("/") && groupPath.endsWith("/"));
Expand All @@ -624,7 +626,7 @@ Group* Group::findGroupByPathRecursion(QString groupPath, QString basePath)

for (Group* innerGroup : children()) {
QString innerBasePath = basePath + innerGroup->name() + "/";
Group* group = innerGroup->findGroupByPathRecursion(groupPath, innerBasePath);
Group* group = innerGroup->findGroupByPathRecursive(groupPath, innerBasePath);
if (group != nullptr) {
return group;
}
Expand Down Expand Up @@ -710,11 +712,13 @@ QSet<QUuid> Group::customIconsRecursive() const

Group* Group::findGroupByUuid(const QUuid& uuid)
{
if (!uuid.isNull()) {
for (Group* group : groupsRecursive(true)) {
if (group->uuid() == uuid) {
return group;
}
if (uuid.isNull()) {
return nullptr;
}

for (Group* group : groupsRecursive(true)) {
if (group->uuid() == uuid) {
return group;
}
}

Expand All @@ -732,6 +736,11 @@ Group* Group::findChildByName(const QString& name)
return nullptr;
}

/**
* Creates a duplicate of this group.
* Note that you need to copy the custom icons manually when inserting the
* new group into another database.
*/
Group* Group::clone(Entry::CloneFlags entryFlags, Group::CloneFlags groupFlags) const
{
Group* clonedGroup = new Group();
Expand Down
20 changes: 5 additions & 15 deletions src/core/Group.h
Expand Up @@ -153,11 +153,7 @@ class Group : public QObject
QList<const Group*> groupsRecursive(bool includeSelf) const;
QList<Group*> groupsRecursive(bool includeSelf);
QSet<QUuid> customIconsRecursive() const;
/**
* Creates a duplicate of this group.
* Note that you need to copy the custom icons manually when inserting the
* new group into another database.
*/

Group* clone(Entry::CloneFlags entryFlags = DefaultEntryCloneFlags,
CloneFlags groupFlags = DefaultCloneFlags) const;

Expand All @@ -166,28 +162,22 @@ class Group : public QObject

void addEntry(Entry* entry);
void removeEntry(Entry* entry);

signals:
void dataChanged(Group* group);

void aboutToAdd(Group* group, int index);
void added();
void aboutToRemove(Group* group);
void removed();
/**
* Group moved within the database.
*/
void aboutToMove(Group* group, Group* toGroup, int index);
void moved();

void modified();
void entryAboutToAdd(Entry* entry);
void entryAdded(Entry* entry);
void entryAboutToRemove(Entry* entry);
void entryRemoved(Entry* entry);

void entryDataChanged(Entry* entry);

void modified();

private slots:
void updateTimeinfo();

Expand All @@ -200,8 +190,8 @@ private slots:
void cleanupParent();
void recCreateDelObjects();

Entry* findEntryByPathRecursion(QString entryPath, QString basePath);
Group* findGroupByPathRecursion(QString groupPath, QString basePath);
Entry* findEntryByPathRecursive(QString entryPath, QString basePath);
Group* findGroupByPathRecursive(QString groupPath, QString basePath);

QPointer<Database> m_db;
QUuid m_uuid;
Expand Down
70 changes: 35 additions & 35 deletions tests/TestGroup.cpp
Expand Up @@ -494,63 +494,63 @@ void TestGroup::testFindEntry()
Entry* entry;

entry = db->rootGroup()->findEntryByUuid(entry1->uuid());
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry1"));

entry = db->rootGroup()->findEntryByPath(QString("entry1"));
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry1"));

// We also can find the entry with the leading slash.
entry = db->rootGroup()->findEntryByPath(QString("/entry1"));
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry1"));

// But two slashes should not be accepted.
entry = db->rootGroup()->findEntryByPath(QString("//entry1"));
QVERIFY(entry == nullptr);
QVERIFY(!entry);

entry = db->rootGroup()->findEntryByUuid(entry2->uuid());
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry2"));

entry = db->rootGroup()->findEntryByPath(QString("group1/entry2"));
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry2"));

entry = db->rootGroup()->findEntryByPath(QString("/entry2"));
QVERIFY(entry == nullptr);
QVERIFY(!entry);

// We also can find the entry with the leading slash.
entry = db->rootGroup()->findEntryByPath(QString("/group1/entry2"));
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry2"));

// Should also find the entry only by title.
entry = db->rootGroup()->findEntryByPath(QString("entry2"));
QVERIFY(entry != nullptr);
QVERIFY(entry);
QCOMPARE(entry->title(), QString("entry2"));

entry = db->rootGroup()->findEntryByPath(QString("invalid/path/to/entry2"));
QVERIFY(entry == nullptr);
QVERIFY(!entry);

entry = db->rootGroup()->findEntryByPath(QString("entry27"));
QVERIFY(entry == nullptr);
QVERIFY(!entry);

// A valid UUID that does not exist in this database.
entry = db->rootGroup()->findEntryByUuid(QUuid("febfb01ebcdf9dbd90a3f1579dc75281"));
QVERIFY(entry == nullptr);
QVERIFY(!entry);

// An invalid UUID.
entry = db->rootGroup()->findEntryByUuid(QUuid("febfb01ebcdf9dbd90a3f1579dc"));
QVERIFY(entry == nullptr);
QVERIFY(!entry);

// Empty strings
entry = db->rootGroup()->findEntryByUuid({});
QVERIFY(entry == nullptr);
QVERIFY(!entry);

entry = db->rootGroup()->findEntryByPath({});
QVERIFY(entry == nullptr);
QVERIFY(!entry);
}

void TestGroup::testFindGroupByPath()
Expand All @@ -568,51 +568,51 @@ void TestGroup::testFindGroupByPath()
Group* group;

group = db->rootGroup()->findGroupByPath("/");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), db->rootGroup()->uuid());

// We also accept it if the leading slash is missing.
group = db->rootGroup()->findGroupByPath("");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), db->rootGroup()->uuid());

group = db->rootGroup()->findGroupByPath("/group1/");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), group1->uuid());

// We also accept it if the leading slash is missing.
group = db->rootGroup()->findGroupByPath("group1/");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), group1->uuid());

// Too many slashes at the end
group = db->rootGroup()->findGroupByPath("group1//");
QVERIFY(group == nullptr);
QVERIFY(!group);

// Missing a slash at the end.
group = db->rootGroup()->findGroupByPath("/group1");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), group1->uuid());

// Too many slashes at the start
group = db->rootGroup()->findGroupByPath("//group1");
QVERIFY(group == nullptr);
QVERIFY(!group);

group = db->rootGroup()->findGroupByPath("/group1/group2/");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), group2->uuid());

// We also accept it if the leading slash is missing.
group = db->rootGroup()->findGroupByPath("group1/group2/");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), group2->uuid());

group = db->rootGroup()->findGroupByPath("group1/group2");
QVERIFY(group != nullptr);
QVERIFY(group);
QCOMPARE(group->uuid(), group2->uuid());

group = db->rootGroup()->findGroupByPath("invalid");
QVERIFY(group == nullptr);
QVERIFY(!group);
}

void TestGroup::testPrint()
Expand Down Expand Up @@ -704,7 +704,7 @@ void TestGroup::testLocate()
QVERIFY(results.contains("/entry1"));

results = db->rootGroup()->locate("invalid");
QVERIFY(results.size() == 0);
QVERIFY(results.isEmpty());

results = db->rootGroup()->locate("google");
QVERIFY(results.size() == 1);
Expand Down Expand Up @@ -732,37 +732,37 @@ void TestGroup::testAddEntryWithPath()
group2->setParent(group1);

Entry* entry = db->rootGroup()->addEntryWithPath("entry1");
QVERIFY(entry != nullptr);
QVERIFY(entry);
QVERIFY(!entry->uuid().isNull());

entry = db->rootGroup()->addEntryWithPath("entry1");
QVERIFY(entry == nullptr);
QVERIFY(!entry);

entry = db->rootGroup()->addEntryWithPath("/entry1");
QVERIFY(entry == nullptr);
QVERIFY(!entry);

entry = db->rootGroup()->addEntryWithPath("entry2");
QVERIFY(entry != nullptr);
QVERIFY(entry);
QVERIFY(entry->title() == "entry2");
QVERIFY(!entry->uuid().isNull());

entry = db->rootGroup()->addEntryWithPath("/entry3");
QVERIFY(entry != nullptr);
QVERIFY(entry);
QVERIFY(entry->title() == "entry3");
QVERIFY(!entry->uuid().isNull());

entry = db->rootGroup()->addEntryWithPath("/group1/entry4");
QVERIFY(entry != nullptr);
QVERIFY(entry);
QVERIFY(entry->title() == "entry4");
QVERIFY(!entry->uuid().isNull());

entry = db->rootGroup()->addEntryWithPath("/group1/group2/entry5");
QVERIFY(entry != nullptr);
QVERIFY(entry);
QVERIFY(entry->title() == "entry5");
QVERIFY(!entry->uuid().isNull());

entry = db->rootGroup()->addEntryWithPath("/group1/invalid_group/entry6");
QVERIFY(entry == nullptr);
QVERIFY(!entry);

delete db;
}

0 comments on commit 0162f2a

Please sign in to comment.