diff --git a/src/core/Group.cpp b/src/core/Group.cpp index dd6572cc28..dab48ebd17 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -550,11 +550,13 @@ QList 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; } } @@ -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 @@ -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; } @@ -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("/")); @@ -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; } @@ -710,11 +712,13 @@ QSet 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; } } @@ -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(); diff --git a/src/core/Group.h b/src/core/Group.h index 57d44690fd..e2b55cbd47 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -153,11 +153,7 @@ class Group : public QObject QList groupsRecursive(bool includeSelf) const; QList groupsRecursive(bool includeSelf); QSet 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; @@ -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(); @@ -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 m_db; QUuid m_uuid; diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index 2e07ef101e..7abb808d1a 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -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() @@ -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() @@ -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); @@ -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; }