From 54ad927044439d5852f524edd7837157f43b43ad Mon Sep 17 00:00:00 2001 From: louib Date: Sun, 21 May 2017 13:05:44 -0400 Subject: [PATCH] Moving print group in Group class. (#586) --- src/cli/List.cpp | 32 +++++++------------------------- src/core/Group.cpp | 36 ++++++++++++++++++++++++++++++++++-- src/core/Group.h | 1 + tests/TestGroup.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestGroup.h | 1 + 5 files changed, 88 insertions(+), 27 deletions(-) diff --git a/src/cli/List.cpp b/src/cli/List.cpp index af3bf0c6d..8dd250b34 100644 --- a/src/cli/List.cpp +++ b/src/cli/List.cpp @@ -30,30 +30,6 @@ #include "core/Group.h" #include "keys/CompositeKey.h" -void printGroup(Group* group, QString baseName, int depth) -{ - - QTextStream out(stdout); - - QString groupName = baseName + group->name() + "/"; - QString indentation = QString(" ").repeated(depth); - - out << indentation << groupName << " " << group->uuid().toHex() << "\n"; - out.flush(); - - if (group->entries().isEmpty() && group->children().isEmpty()) { - out << indentation << " [empty]\n"; - return; - } - - for (Entry* entry : group->entries()) { - out << indentation << " " << entry->title() << " " << entry->uuid().toHex() << "\n"; - } - - for (Group* innerGroup : group->children()) { - printGroup(innerGroup, groupName, depth + 1); - } -} int List::execute(int argc, char** argv) { @@ -63,6 +39,11 @@ int List::execute(int argc, char** argv) QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::translate("main", "List database entries.")); parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database.")); + QCommandLineOption printUuidsOption( + QStringList() << "u" + << "print-uuids", + QCoreApplication::translate("main", "Print the UUIDs of the entries and groups.")); + parser.addOption(printUuidsOption); parser.process(app); const QStringList args = parser.positionalArguments(); @@ -83,6 +64,7 @@ int List::execute(int argc, char** argv) return EXIT_FAILURE; } - printGroup(db->rootGroup(), QString(""), 0); + out << db->rootGroup()->print(parser.isSet("print-uuids")); + out.flush(); return EXIT_SUCCESS; } diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 886e55cae..38acc2d60 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -18,8 +18,8 @@ #include "Group.h" #include "core/Config.h" -#include "core/Global.h" #include "core/DatabaseIcons.h" +#include "core/Global.h" #include "core/Metadata.h" const int Group::DefaultIconNumber = 48; @@ -202,7 +202,7 @@ QString Group::effectiveAutoTypeSequence() const } while (group && sequence.isEmpty()); if (sequence.isEmpty()) { - sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}"; + sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}"; } return sequence; @@ -535,6 +535,38 @@ Entry* Group::findEntryByPath(QString entryPath, QString basePath) return nullptr; } +QString Group::print(bool printUuids, QString baseName, int depth) +{ + + QString response; + QString indentation = QString(" ").repeated(depth); + + if (entries().isEmpty() && children().isEmpty()) { + response += indentation + "[empty]\n"; + return response; + } + + for (Entry* entry : entries()) { + response += indentation + entry->title(); + if (printUuids) { + response += " " + entry->uuid().toHex(); + } + response += "\n"; + } + + for (Group* innerGroup : children()) { + QString newBaseName = baseName + innerGroup->name() + "/"; + response += indentation + newBaseName; + if (printUuids) { + response += " " + innerGroup->uuid().toHex(); + } + response += "\n"; + response += innerGroup->print(printUuids, newBaseName, depth + 1); + } + + return response; +} + QList Group::groupsRecursive(bool includeSelf) const { QList groupList; diff --git a/src/core/Group.h b/src/core/Group.h index a1b2bcb46..9b1f0209f 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -123,6 +123,7 @@ public: Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const; void copyDataFrom(const Group* other); void merge(const Group* other); + QString print(bool printUuids = false, QString baseName = QString(""), int depth = 0); signals: void dataChanged(Group* group); diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index 425bc75c7..4fb5e4b3b 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -619,3 +619,48 @@ void TestGroup::testFindEntry() delete db; } + +void TestGroup::testPrint() +{ + Database* db = new Database(); + + QString output = db->rootGroup()->print(); + QCOMPARE(output, QString("[empty]\n")); + + output = db->rootGroup()->print(true); + QCOMPARE(output, QString("[empty]\n")); + + Entry* entry1 = new Entry(); + entry1->setTitle(QString("entry1")); + entry1->setGroup(db->rootGroup()); + entry1->setUuid(Uuid::random()); + + output = db->rootGroup()->print(); + QCOMPARE(output, QString("entry1\n")); + + output = db->rootGroup()->print(true); + QCOMPARE(output, QString("entry1 " + entry1->uuid().toHex() + "\n")); + + + Group* group1 = new Group(); + group1->setName("group1"); + + Entry* entry2 = new Entry(); + + entry2->setTitle(QString("entry2")); + entry2->setGroup(group1); + entry2->setUuid(Uuid::random()); + + group1->setParent(db->rootGroup()); + + output = db->rootGroup()->print(); + QVERIFY(output.contains(QString("entry1\n"))); + QVERIFY(output.contains(QString("group1/\n"))); + QVERIFY(output.contains(QString(" entry2\n"))); + + output = db->rootGroup()->print(true); + QVERIFY(output.contains(QString("entry1 " + entry1->uuid().toHex() + "\n"))); + QVERIFY(output.contains(QString("group1/ " + group1->uuid().toHex() + "\n"))); + QVERIFY(output.contains(QString(" entry2 " + entry2->uuid().toHex() + "\n"))); + delete db; +} diff --git a/tests/TestGroup.h b/tests/TestGroup.h index 87795dea2..a0ed9282b 100644 --- a/tests/TestGroup.h +++ b/tests/TestGroup.h @@ -39,6 +39,7 @@ private slots: void testMergeDatabase(); void testMergeConflictKeepBoth(); void testFindEntry(); + void testPrint(); private: Database* createMergeTestDatabase();