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

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