Extracting openDatabaseFile.

This commit is contained in:
Louis-Bertrand Varin 2017-03-12 13:47:05 -04:00 committed by Louis-Bertrand Varin
parent db1bf88934
commit 993f90cb2c
3 changed files with 28 additions and 18 deletions

View File

@ -22,14 +22,12 @@
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QTextStream>
#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;
}

View File

@ -25,6 +25,7 @@
#include "core/Metadata.h"
#include "crypto/Random.h"
#include "format/KeePass2.h"
#include "format/KeePass2Reader.h"
QHash<Uuid, Database*> 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;
}

View File

@ -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);