Add interface for database reader/writer.

This commit is contained in:
Felix Geyer 2010-08-31 16:18:45 +02:00
parent ee4c2c3dd4
commit 1cfc34361a
8 changed files with 124 additions and 40 deletions

View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_DATABASEREADER_H
#define KEEPASSX_DATABASEREADER_H
#include <QtCore/QString>
class Database;
class QIODevice;
class DatabaseReader
{
public:
virtual Database* readDatabase(QIODevice* device) = 0;
virtual bool error() = 0;
virtual QString errorString() = 0;
};
#endif // KEEPASSX_DATABASEREADER_H

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_DATABASEWRITER_H
#define KEEPASSX_DATABASEWRITER_H
#include <QtCore/QString>
class Database;
class QIODevice;
class DatabaseWriter
{
public:
virtual void writeDatabase(QIODevice* device, Database* db) = 0;
};
#endif // KEEPASSX_DATABASEWRITER_H

View File

@ -23,17 +23,18 @@
#include "core/Database.h" #include "core/Database.h"
#include "core/Metadata.h" #include "core/Metadata.h"
KeePass2XmlReader::KeePass2XmlReader(Database* db) KeePass2XmlReader::KeePass2XmlReader()
: m_db(0)
, m_meta(0)
{ {
m_db = db;
m_meta = db->metadata();
} }
bool KeePass2XmlReader::parse(const QString& filename) Database* KeePass2XmlReader::readDatabase(QIODevice* device)
{ {
QFile file(filename); m_xml.setDevice(device);
file.open(QIODevice::ReadOnly | QIODevice::Text);
m_xml.setDevice(&file); m_db = new Database();
m_meta = m_db->metadata();
m_tmpParent = new Group(); m_tmpParent = new Group();
m_tmpParent->setParent(m_db); m_tmpParent->setParent(m_db);
@ -50,10 +51,22 @@ bool KeePass2XmlReader::parse(const QString& filename)
delete m_tmpParent; delete m_tmpParent;
return !m_xml.error(); return m_db;
} }
QString KeePass2XmlReader::errorMsg() Database* KeePass2XmlReader::readDatabase(const QString& filename)
{
QFile file(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
return readDatabase(&file);
}
bool KeePass2XmlReader::error()
{
return m_xml.hasError();
}
QString KeePass2XmlReader::errorString()
{ {
return QString("%1\nLine %2, column %3") return QString("%1\nLine %2, column %3")
.arg(m_xml.errorString()) .arg(m_xml.errorString())

View File

@ -15,13 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef KEEPASSX_PARSER_H #ifndef KEEPASSX_KEEPASS2XMLREADER_H
#define KEEPASSX_PARSER_H #define KEEPASSX_KEEPASS2XMLREADER_H
#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime> #include <QtCore/QDateTime>
#include <QtCore/QXmlStreamReader> #include <QtCore/QXmlStreamReader>
#include <QtGui/QColor> #include <QtGui/QColor>
#include "DatabaseReader.h"
#include "core/TimeInfo.h" #include "core/TimeInfo.h"
#include "core/Uuid.h" #include "core/Uuid.h"
@ -30,14 +32,16 @@ class Entry;
class Group; class Group;
class Metadata; class Metadata;
class KeePass2XmlReader : public QObject class KeePass2XmlReader : public DatabaseReader
{ {
Q_OBJECT Q_DECLARE_TR_FUNCTIONS(KeePass2XmlReader);
public: public:
explicit KeePass2XmlReader(Database* db); KeePass2XmlReader();
bool parse(const QString& filename); Database* readDatabase(QIODevice* device);
QString errorMsg(); Database* readDatabase(const QString& filename);
bool error();
QString errorString();
private: private:
void parseKeePassFile(); void parseKeePassFile();
@ -80,4 +84,4 @@ private:
QList<Entry*> m_entries; QList<Entry*> m_entries;
}; };
#endif // KEEPASSX_PARSER_H #endif // KEEPASSX_KEEPASS2XMLREADER_H

View File

@ -22,19 +22,20 @@
#include "core/Metadata.h" #include "core/Metadata.h"
KeePass2XmlWriter::KeePass2XmlWriter(Database* db) KeePass2XmlWriter::KeePass2XmlWriter()
: QObject(db) : m_db(0)
, m_db(db) , m_meta(0)
, m_meta(db->metadata())
{ {
m_xml.setAutoFormatting(true); m_xml.setAutoFormatting(true);
m_xml.setAutoFormattingIndent(-1); // 1 tab m_xml.setAutoFormattingIndent(-1); // 1 tab
m_xml.setCodec("UTF-8"); m_xml.setCodec("UTF-8");
} }
void KeePass2XmlWriter::write(QIODevice* device) void KeePass2XmlWriter::writeDatabase(QIODevice* device, Database* db)
{ {
m_db = db;
m_meta = db->metadata();
m_xml.setDevice(device); m_xml.setDevice(device);
m_xml.writeStartDocument("1.0", true); m_xml.writeStartDocument("1.0", true);
@ -49,11 +50,11 @@ void KeePass2XmlWriter::write(QIODevice* device)
m_xml.writeEndDocument(); m_xml.writeEndDocument();
} }
void KeePass2XmlWriter::write(const QString& filename) void KeePass2XmlWriter::writeDatabase(const QString& filename, Database* db)
{ {
QFile file(filename); QFile file(filename);
file.open(QIODevice::WriteOnly); file.open(QIODevice::WriteOnly);
write(&file); writeDatabase(&file, db);
} }
void KeePass2XmlWriter::writeMetadata() void KeePass2XmlWriter::writeMetadata()

View File

@ -15,14 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef KEEPASSX_WRITER_H #ifndef KEEPASSX_KEEPASS2XMLWRITER_H
#define KEEPASSX_WRITER_H #define KEEPASSX_KEEPASS2XMLWRITER_H
#include <QtCore/QDateTime> #include <QtCore/QDateTime>
#include <QtCore/QXmlStreamWriter> #include <QtCore/QXmlStreamWriter>
#include <QtGui/QColor> #include <QtGui/QColor>
#include <QtGui/QImage> #include <QtGui/QImage>
#include "DatabaseWriter.h"
#include "core/Database.h" #include "core/Database.h"
#include "core/Entry.h" #include "core/Entry.h"
#include "core/TimeInfo.h" #include "core/TimeInfo.h"
@ -31,14 +32,14 @@
class Group; class Group;
class Metadata; class Metadata;
class KeePass2XmlWriter : public QObject class KeePass2XmlWriter : public DatabaseWriter
{ {
Q_OBJECT
public: public:
KeePass2XmlWriter(Database* db); KeePass2XmlWriter();
void write(QIODevice* device); void writeDatabase(QIODevice* device, Database* db);
void write(const QString& filename); void writeDatabase(const QString& filename, Database* db);
bool error();
QString errorString();
private: private:
void writeMetadata(); void writeMetadata();
@ -73,4 +74,4 @@ private:
Metadata* m_meta; Metadata* m_meta;
}; };
#endif // KEEPASSX_WRITER_H #endif // KEEPASSX_KEEPASS2XMLWRITER_H

View File

@ -28,9 +28,8 @@ int main(int argc, char **argv)
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
Database* db = new Database(); KeePass2XmlReader* reader = new KeePass2XmlReader();
KeePass2XmlReader* reader = new KeePass2XmlReader(db); Database* db = reader->readDatabase(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml"));
reader->parse(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml"));
DatabaseWidget dbWidget(db); DatabaseWidget dbWidget(db);
dbWidget.show(); dbWidget.show();

View File

@ -65,10 +65,10 @@ QDateTime TestParser::genDT(int year, int month, int day, int hour, int min, int
void TestParser::initTestCase() void TestParser::initTestCase()
{ {
m_db = new Database(); KeePass2XmlReader* reader = new KeePass2XmlReader();
KeePass2XmlReader* reader = new KeePass2XmlReader(m_db);
QString xmlFile = QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml"); QString xmlFile = QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml");
QVERIFY(reader->parse(xmlFile)); m_db = reader->readDatabase(xmlFile);
QVERIFY(!reader->error());
} }
void TestParser::testMetadata() void TestParser::testMetadata()