diff --git a/src/format/DatabaseReader.h b/src/format/DatabaseReader.h new file mode 100644 index 000000000..d9ea3b997 --- /dev/null +++ b/src/format/DatabaseReader.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2010 Felix Geyer + * + * 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 . + */ + +#ifndef KEEPASSX_DATABASEREADER_H +#define KEEPASSX_DATABASEREADER_H + +#include + +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 diff --git a/src/format/DatabaseWriter.h b/src/format/DatabaseWriter.h new file mode 100644 index 000000000..1624fad30 --- /dev/null +++ b/src/format/DatabaseWriter.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2010 Felix Geyer + * + * 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 . + */ + +#ifndef KEEPASSX_DATABASEWRITER_H +#define KEEPASSX_DATABASEWRITER_H + +#include + +class Database; +class QIODevice; + +class DatabaseWriter +{ +public: + virtual void writeDatabase(QIODevice* device, Database* db) = 0; +}; + +#endif // KEEPASSX_DATABASEWRITER_H diff --git a/src/format/KeePass2XmlReader.cpp b/src/format/KeePass2XmlReader.cpp index bd54c077d..3be5ca094 100644 --- a/src/format/KeePass2XmlReader.cpp +++ b/src/format/KeePass2XmlReader.cpp @@ -23,17 +23,18 @@ #include "core/Database.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); - file.open(QIODevice::ReadOnly | QIODevice::Text); - m_xml.setDevice(&file); + m_xml.setDevice(device); + + m_db = new Database(); + m_meta = m_db->metadata(); m_tmpParent = new Group(); m_tmpParent->setParent(m_db); @@ -50,10 +51,22 @@ bool KeePass2XmlReader::parse(const QString& filename) 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") .arg(m_xml.errorString()) diff --git a/src/format/KeePass2XmlReader.h b/src/format/KeePass2XmlReader.h index 6c6eaec69..c9e715ac9 100644 --- a/src/format/KeePass2XmlReader.h +++ b/src/format/KeePass2XmlReader.h @@ -15,13 +15,15 @@ * along with this program. If not, see . */ -#ifndef KEEPASSX_PARSER_H -#define KEEPASSX_PARSER_H +#ifndef KEEPASSX_KEEPASS2XMLREADER_H +#define KEEPASSX_KEEPASS2XMLREADER_H +#include #include #include #include +#include "DatabaseReader.h" #include "core/TimeInfo.h" #include "core/Uuid.h" @@ -30,14 +32,16 @@ class Entry; class Group; class Metadata; -class KeePass2XmlReader : public QObject +class KeePass2XmlReader : public DatabaseReader { - Q_OBJECT + Q_DECLARE_TR_FUNCTIONS(KeePass2XmlReader); public: - explicit KeePass2XmlReader(Database* db); - bool parse(const QString& filename); - QString errorMsg(); + KeePass2XmlReader(); + Database* readDatabase(QIODevice* device); + Database* readDatabase(const QString& filename); + bool error(); + QString errorString(); private: void parseKeePassFile(); @@ -80,4 +84,4 @@ private: QList m_entries; }; -#endif // KEEPASSX_PARSER_H +#endif // KEEPASSX_KEEPASS2XMLREADER_H diff --git a/src/format/KeePass2XmlWriter.cpp b/src/format/KeePass2XmlWriter.cpp index 2e0b4c8f0..3b378c0bd 100644 --- a/src/format/KeePass2XmlWriter.cpp +++ b/src/format/KeePass2XmlWriter.cpp @@ -22,19 +22,20 @@ #include "core/Metadata.h" -KeePass2XmlWriter::KeePass2XmlWriter(Database* db) - : QObject(db) - , m_db(db) - , m_meta(db->metadata()) +KeePass2XmlWriter::KeePass2XmlWriter() + : m_db(0) + , m_meta(0) { m_xml.setAutoFormatting(true); m_xml.setAutoFormattingIndent(-1); // 1 tab 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.writeStartDocument("1.0", true); @@ -49,11 +50,11 @@ void KeePass2XmlWriter::write(QIODevice* device) m_xml.writeEndDocument(); } -void KeePass2XmlWriter::write(const QString& filename) +void KeePass2XmlWriter::writeDatabase(const QString& filename, Database* db) { QFile file(filename); file.open(QIODevice::WriteOnly); - write(&file); + writeDatabase(&file, db); } void KeePass2XmlWriter::writeMetadata() diff --git a/src/format/KeePass2XmlWriter.h b/src/format/KeePass2XmlWriter.h index 0142f01c3..e2149d6ef 100644 --- a/src/format/KeePass2XmlWriter.h +++ b/src/format/KeePass2XmlWriter.h @@ -15,14 +15,15 @@ * along with this program. If not, see . */ -#ifndef KEEPASSX_WRITER_H -#define KEEPASSX_WRITER_H +#ifndef KEEPASSX_KEEPASS2XMLWRITER_H +#define KEEPASSX_KEEPASS2XMLWRITER_H #include #include #include #include +#include "DatabaseWriter.h" #include "core/Database.h" #include "core/Entry.h" #include "core/TimeInfo.h" @@ -31,14 +32,14 @@ class Group; class Metadata; -class KeePass2XmlWriter : public QObject +class KeePass2XmlWriter : public DatabaseWriter { - Q_OBJECT - public: - KeePass2XmlWriter(Database* db); - void write(QIODevice* device); - void write(const QString& filename); + KeePass2XmlWriter(); + void writeDatabase(QIODevice* device, Database* db); + void writeDatabase(const QString& filename, Database* db); + bool error(); + QString errorString(); private: void writeMetadata(); @@ -73,4 +74,4 @@ private: Metadata* m_meta; }; -#endif // KEEPASSX_WRITER_H +#endif // KEEPASSX_KEEPASS2XMLWRITER_H diff --git a/src/main.cpp b/src/main.cpp index 6e4873414..6bd3a7739 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,9 +28,8 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - Database* db = new Database(); - KeePass2XmlReader* reader = new KeePass2XmlReader(db); - reader->parse(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); + KeePass2XmlReader* reader = new KeePass2XmlReader(); + Database* db = reader->readDatabase(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); DatabaseWidget dbWidget(db); dbWidget.show(); diff --git a/tests/TestKeePass2XmlReader.cpp b/tests/TestKeePass2XmlReader.cpp index b5844f3f3..64d726a57 100644 --- a/tests/TestKeePass2XmlReader.cpp +++ b/tests/TestKeePass2XmlReader.cpp @@ -65,10 +65,10 @@ QDateTime TestParser::genDT(int year, int month, int day, int hour, int min, int void TestParser::initTestCase() { - m_db = new Database(); - KeePass2XmlReader* reader = new KeePass2XmlReader(m_db); + KeePass2XmlReader* reader = new KeePass2XmlReader(); QString xmlFile = QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml"); - QVERIFY(reader->parse(xmlFile)); + m_db = reader->readDatabase(xmlFile); + QVERIFY(!reader->error()); } void TestParser::testMetadata()