Moving print group in Group class. (#586)

This commit is contained in:
louib 2017-05-21 13:05:44 -04:00 committed by GitHub
parent a2e82dc883
commit 54ad927044
5 changed files with 88 additions and 27 deletions

View File

@ -30,30 +30,6 @@
#include "core/Group.h" #include "core/Group.h"
#include "keys/CompositeKey.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) int List::execute(int argc, char** argv)
{ {
@ -63,6 +39,11 @@ int List::execute(int argc, char** argv)
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "List database entries.")); parser.setApplicationDescription(QCoreApplication::translate("main", "List database entries."));
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database.")); 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); parser.process(app);
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
@ -83,6 +64,7 @@ int List::execute(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printGroup(db->rootGroup(), QString(""), 0); out << db->rootGroup()->print(parser.isSet("print-uuids"));
out.flush();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -18,8 +18,8 @@
#include "Group.h" #include "Group.h"
#include "core/Config.h" #include "core/Config.h"
#include "core/Global.h"
#include "core/DatabaseIcons.h" #include "core/DatabaseIcons.h"
#include "core/Global.h"
#include "core/Metadata.h" #include "core/Metadata.h"
const int Group::DefaultIconNumber = 48; const int Group::DefaultIconNumber = 48;
@ -202,7 +202,7 @@ QString Group::effectiveAutoTypeSequence() const
} while (group && sequence.isEmpty()); } while (group && sequence.isEmpty());
if (sequence.isEmpty()) { if (sequence.isEmpty()) {
sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}"; sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}";
} }
return sequence; return sequence;
@ -535,6 +535,38 @@ Entry* Group::findEntryByPath(QString entryPath, QString basePath)
return nullptr; 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<const Group*> Group::groupsRecursive(bool includeSelf) const QList<const Group*> Group::groupsRecursive(bool includeSelf) const
{ {
QList<const Group*> groupList; QList<const Group*> groupList;

View File

@ -123,6 +123,7 @@ public:
Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const; Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const;
void copyDataFrom(const Group* other); void copyDataFrom(const Group* other);
void merge(const Group* other); void merge(const Group* other);
QString print(bool printUuids = false, QString baseName = QString(""), int depth = 0);
signals: signals:
void dataChanged(Group* group); void dataChanged(Group* group);

View File

@ -619,3 +619,48 @@ void TestGroup::testFindEntry()
delete db; 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;
}

View File

@ -39,6 +39,7 @@ private slots:
void testMergeDatabase(); void testMergeDatabase();
void testMergeConflictKeepBoth(); void testMergeConflictKeepBoth();
void testFindEntry(); void testFindEntry();
void testPrint();
private: private:
Database* createMergeTestDatabase(); Database* createMergeTestDatabase();