diff --git a/src/cli/Show.cpp b/src/cli/Show.cpp index b596e521b..9222a093d 100644 --- a/src/cli/Show.cpp +++ b/src/cli/Show.cpp @@ -22,14 +22,12 @@ #include #include -#include #include #include #include "core/Database.h" #include "core/Entry.h" #include "core/Group.h" -#include "format/KeePass2Reader.h" #include "keys/CompositeKey.h" int Show::execute(int argc, char **argv) @@ -57,22 +55,8 @@ int Show::execute(int argc, char **argv) QString line = inputTextStream.readLine(); CompositeKey key = CompositeKey::readFromLine(line); - QString databaseFilename = args.at(0); - QFile dbFile(databaseFilename); - if (!dbFile.exists()) { - qCritical("File %s does not exist.", qPrintable(databaseFilename)); - return EXIT_FAILURE; - } - if (!dbFile.open(QIODevice::ReadOnly)) { - qCritical("Unable to open file %s.", qPrintable(databaseFilename)); - return EXIT_FAILURE; - } - - KeePass2Reader reader; - Database* db = reader.readDatabase(&dbFile, key); - - if (reader.hasError()) { - qCritical("Error while parsing the database:\n%s\n", qPrintable(reader.errorString())); + Database* db = Database::openDatabaseFile(args.at(0), key); + if (db == nullptr) { return EXIT_FAILURE; } diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 23b564143..4aa9e3f5b 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -25,6 +25,7 @@ #include "core/Metadata.h" #include "crypto/Random.h" #include "format/KeePass2.h" +#include "format/KeePass2Reader.h" QHash Database::m_uuidMap; @@ -355,3 +356,27 @@ const CompositeKey & Database::key() const return m_data.key; } +Database* Database::openDatabaseFile(QString fileName, CompositeKey key) +{ + + QFile dbFile(fileName); + if (!dbFile.exists()) { + qCritical("File %s does not exist.", qPrintable(fileName)); + return nullptr; + } + if (!dbFile.open(QIODevice::ReadOnly)) { + qCritical("Unable to open file %s.", qPrintable(fileName)); + return nullptr; + } + + KeePass2Reader reader; + Database* db = reader.readDatabase(&dbFile, key); + + if (reader.hasError()) { + qCritical("Error while parsing the database: %s", qPrintable(reader.errorString())); + return nullptr; + } + + return db; + +} diff --git a/src/core/Database.h b/src/core/Database.h index 3767bb30d..16c149608 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -118,6 +118,7 @@ public: Uuid uuid(); static Database* databaseByUuid(const Uuid& uuid); + static Database* openDatabaseFile(QString fileName, CompositeKey key); signals: void groupDataChanged(Group* group);