Skip to content

Commit

Permalink
Fix issues with group functions
Browse files Browse the repository at this point in the history
  • Loading branch information
droidmonkey committed Oct 30, 2018
1 parent 7263dcd commit 5a375cc
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/cli/Clip.cpp
Expand Up @@ -84,7 +84,7 @@ int Clip::clipEntry(Database* database, QString entryPath, QString timeout)
}

TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
err << QObject::tr("Entry %1 not found.").arg(entryPath) << endl;
return EXIT_FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/Show.cpp
Expand Up @@ -82,7 +82,7 @@ int Show::showEntry(Database* database, QStringList attributes, const QString& e
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);

Entry* entry = database->rootGroup()->findEntry(entryPath);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
return EXIT_FAILURE;
Expand Down
101 changes: 41 additions & 60 deletions src/core/Group.cpp
Expand Up @@ -321,9 +321,7 @@ void Group::setNotes(const QString& notes)

void Group::setIcon(int iconNumber)
{
Q_ASSERT(iconNumber >= 0);

if (m_data.iconNumber != iconNumber || !m_data.customIcon.isNull()) {
if (iconNumber >= 0 && (m_data.iconNumber != iconNumber || !m_data.customIcon.isNull())) {
m_data.iconNumber = iconNumber;
m_data.customIcon = QUuid();
emit modified();
Expand All @@ -333,9 +331,7 @@ void Group::setIcon(int iconNumber)

void Group::setIcon(const QUuid& uuid)
{
Q_ASSERT(!uuid.isNull());

if (m_data.customIcon != uuid) {
if (!uuid.isNull() && m_data.customIcon != uuid) {
m_data.customIcon = uuid;
m_data.iconNumber = 0;
emit modified();
Expand Down Expand Up @@ -552,59 +548,47 @@ QList<Entry*> Group::entriesRecursive(bool includeHistoryItems) const
return entryList;
}

Entry* Group::findEntry(QString entryId)
Entry* Group::findEntryByUuid(const QUuid& uuid) const
{
Q_ASSERT(!entryId.isNull());

Entry* entry;
QUuid entryUuid = QUuid::fromRfc4122(QByteArray::fromHex(entryId.toLatin1()));
if (!entryUuid.isNull()) {
entry = findEntryByUuid(entryUuid);
if (entry) {
return entry;
}
}

entry = findEntryByPath(entryId);
if (entry) {
return entry;
}

for (Entry* entry : entriesRecursive(false)) {
if (entry->title() == entryId) {
return entry;
if (!uuid.isNull()) {
for (Entry* entry : entriesRecursive(false)) {
if (entry->uuid() == uuid) {
return entry;
}
}
}

return nullptr;
}

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

return nullptr;
// Add a beginning slash if the search string contains a slash
// We don't add a slash by default to allow searching by entry title
QString normalizedEntryPath = entryPath;
if (!normalizedEntryPath.startsWith("/") && normalizedEntryPath.contains("/")) {
normalizedEntryPath = "/" + normalizedEntryPath;
}
return findEntryByPathRecursion(normalizedEntryPath, "/");
}

Entry* Group::findEntryByPath(QString entryPath, QString basePath)
Entry* Group::findEntryByPathRecursion(QString entryPath, QString basePath)
{

Q_ASSERT(!entryPath.isNull());

for (Entry* entry : asConst(m_entries)) {
QString currentEntryPath = basePath + entry->title();
if (entryPath == currentEntryPath || entryPath == QString("/" + currentEntryPath)) {
// Return the first entry that matches the full path OR if there is no leading
// slash, return the first entry title that matches
for (Entry* entry : entries()) {
if (entryPath == (basePath + entry->title())
|| (!entryPath.startsWith("/") && entry->title() == entryPath)) {
return entry;
}
}

for (Group* group : asConst(m_children)) {
Entry* entry = group->findEntryByPath(entryPath, basePath + group->name() + QString("/"));
for (Group* group : children()) {
Entry* entry = group->findEntryByPathRecursion(entryPath, basePath + group->name() + "/");
if (entry != nullptr) {
return entry;
}
Expand All @@ -615,17 +599,15 @@ Entry* Group::findEntryByPath(QString entryPath, QString basePath)

Group* Group::findGroupByPath(QString groupPath)
{
Q_ASSERT(!groupPath.isNull());

// normalize the groupPath by adding missing front and rear slashes. once.
QString normalizedGroupPath;

if (groupPath == "") {
if (groupPath.isEmpty()) {
normalizedGroupPath = QString("/"); // root group
} else {
normalizedGroupPath = ((groupPath.startsWith("/"))? "" : "/")
normalizedGroupPath = (groupPath.startsWith("/") ? "" : "/")
+ groupPath
+ ((groupPath.endsWith("/") )? "" : "/");
+ (groupPath.endsWith("/") ? "" : "/");
}
return findGroupByPathRecursion(normalizedGroupPath, "/");
}
Expand Down Expand Up @@ -683,7 +665,7 @@ QList<const Group*> Group::groupsRecursive(bool includeSelf) const
groupList.append(this);
}

for (const Group* group : m_children) {
for (const Group* group : asConst(m_children)) {
groupList.append(group->groupsRecursive(true));
}

Expand Down Expand Up @@ -728,10 +710,11 @@ QSet<QUuid> Group::customIconsRecursive() const

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

Expand Down Expand Up @@ -936,8 +919,11 @@ bool Group::resolveAutoTypeEnabled() const

QStringList Group::locate(QString locateTerm, QString currentPath)
{
Q_ASSERT(!locateTerm.isNull());
// TODO: Replace with EntrySearcher
QStringList response;
if (locateTerm.isEmpty()) {
return response;
}

for (Entry* entry : asConst(m_entries)) {
QString entryPath = currentPath + entry->title();
Expand All @@ -957,20 +943,15 @@ QStringList Group::locate(QString locateTerm, QString currentPath)

Entry* Group::addEntryWithPath(QString entryPath)
{
Q_ASSERT(!entryPath.isNull());
if (this->findEntryByPath(entryPath)) {
if (entryPath.isEmpty() || findEntryByPath(entryPath)) {
return nullptr;
}

QStringList groups = entryPath.split("/");
QString entryTitle = groups.takeLast();
QString groupPath = groups.join("/");
if (groupPath.isNull()) {
groupPath = QString("");
}

Q_ASSERT(!groupPath.isNull());
Group* group = this->findGroupByPath(groupPath);
Group* group = findGroupByPath(groupPath);
if (!group) {
return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/Group.h
Expand Up @@ -114,12 +114,11 @@ class Group : public QObject
static const QString RootAutoTypeSequence;

Group* findChildByName(const QString& name);
Entry* findEntry(QString entryId);
Entry* findEntryByUuid(const QUuid& uuid) const;
Entry* findEntryByPath(QString entryPath, QString basePath = QString(""));
Entry* findEntryByPath(QString entryPath);
Group* findGroupByUuid(const QUuid& uuid);
Group* findGroupByPath(QString groupPath);
QStringList locate(QString locateTerm, QString currentPath = QString("/"));
QStringList locate(QString locateTerm, QString currentPath = {"/"});
Entry* addEntryWithPath(QString entryPath);
void setUuid(const QUuid& uuid);
void setName(const QString& name);
Expand Down Expand Up @@ -201,6 +200,7 @@ private slots:
void cleanupParent();
void recCreateDelObjects();

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

QPointer<Database> m_db;
Expand Down
33 changes: 20 additions & 13 deletions tests/TestGroup.cpp
Expand Up @@ -493,56 +493,63 @@ void TestGroup::testFindEntry()

Entry* entry;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down

0 comments on commit 5a375cc

Please sign in to comment.