From ee4c2c3dd43e965eab19eb8b98f0081a4ddbbf27 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Tue, 31 Aug 2010 14:39:35 +0200 Subject: [PATCH] Move core/Parser to format/KeePass2XmlReader and core/Writer to format/KeePass2XmlWriter. --- src/CMakeLists.txt | 4 +- src/core/Database.cpp | 1 - .../KeePass2XmlReader.cpp} | 72 +++++++++---------- .../Parser.h => format/KeePass2XmlReader.h} | 8 +-- .../KeePass2XmlWriter.cpp} | 58 +++++++-------- .../Writer.h => format/KeePass2XmlWriter.h} | 4 +- src/main.cpp | 8 +-- tests/CMakeLists.txt | 4 +- ...stParser.cpp => TestKeePass2XmlReader.cpp} | 6 +- 9 files changed, 82 insertions(+), 83 deletions(-) rename src/{core/Parser.cpp => format/KeePass2XmlReader.cpp} (92%) rename src/{core/Parser.h => format/KeePass2XmlReader.h} (93%) rename src/{core/Writer.cpp => format/KeePass2XmlWriter.cpp} (83%) rename src/{core/Writer.h => format/KeePass2XmlWriter.h} (97%) rename tests/{TestParser.cpp => TestKeePass2XmlReader.cpp} (98%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f39d2f90..606799fc7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,10 +22,10 @@ set(keepassx_SOURCES core/Entry.cpp core/Group.cpp core/Metadata.cpp - core/Parser.cpp core/TimeInfo.cpp core/Uuid.cpp - core/Writer.cpp + format/KeePass2XmlReader.cpp + format/KeePass2XmlWriter.cpp gui/DatabaseWidget.cpp gui/EntryModel.cpp gui/EntryView.cpp diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 5ddb273a7..fb48c8cf3 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -21,7 +21,6 @@ #include #include "Metadata.h" -#include "Parser.h" Database::Database() { diff --git a/src/core/Parser.cpp b/src/format/KeePass2XmlReader.cpp similarity index 92% rename from src/core/Parser.cpp rename to src/format/KeePass2XmlReader.cpp index 80a31292a..bd54c077d 100644 --- a/src/core/Parser.cpp +++ b/src/format/KeePass2XmlReader.cpp @@ -15,21 +15,21 @@ * along with this program. If not, see . */ -#include "Parser.h" +#include "KeePass2XmlReader.h" #include #include -#include "Database.h" -#include "Metadata.h" +#include "core/Database.h" +#include "core/Metadata.h" -Parser::Parser(Database* db) +KeePass2XmlReader::KeePass2XmlReader(Database* db) { m_db = db; m_meta = db->metadata(); } -bool Parser::parse(const QString& filename) +bool KeePass2XmlReader::parse(const QString& filename) { QFile file(filename); file.open(QIODevice::ReadOnly | QIODevice::Text); @@ -53,7 +53,7 @@ bool Parser::parse(const QString& filename) return !m_xml.error(); } -QString Parser::errorMsg() +QString KeePass2XmlReader::errorMsg() { return QString("%1\nLine %2, column %3") .arg(m_xml.errorString()) @@ -61,7 +61,7 @@ QString Parser::errorMsg() .arg(m_xml.columnNumber()); } -void Parser::parseKeePassFile() +void KeePass2XmlReader::parseKeePassFile() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "KeePassFile"); @@ -78,7 +78,7 @@ void Parser::parseKeePassFile() } } -void Parser::parseMeta() +void KeePass2XmlReader::parseMeta() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Meta"); @@ -143,7 +143,7 @@ void Parser::parseMeta() } } -void Parser::parseMemoryProtection() +void KeePass2XmlReader::parseMemoryProtection() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "MemoryProtection"); @@ -172,7 +172,7 @@ void Parser::parseMemoryProtection() } } -void Parser::parseCustomIcons() +void KeePass2XmlReader::parseCustomIcons() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "CustomIcons"); @@ -186,7 +186,7 @@ void Parser::parseCustomIcons() } } -void Parser::parseIcon() +void KeePass2XmlReader::parseIcon() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Icon"); @@ -206,7 +206,7 @@ void Parser::parseIcon() } } -void Parser::parseCustomData() +void KeePass2XmlReader::parseCustomData() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "CustomData"); @@ -220,7 +220,7 @@ void Parser::parseCustomData() } } -void Parser::parseCustomDataItem() +void KeePass2XmlReader::parseCustomDataItem() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Item"); @@ -238,7 +238,7 @@ void Parser::parseCustomDataItem() } } -void Parser::parseRoot() +void KeePass2XmlReader::parseRoot() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Root"); @@ -258,7 +258,7 @@ void Parser::parseRoot() } } -Group* Parser::parseGroup() +Group* KeePass2XmlReader::parseGroup() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Group"); @@ -359,7 +359,7 @@ Group* Parser::parseGroup() return group; } -void Parser::parseDeletedObjects() +void KeePass2XmlReader::parseDeletedObjects() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "DeletedObjects"); @@ -373,7 +373,7 @@ void Parser::parseDeletedObjects() } } -void Parser::parseDeletedObject() +void KeePass2XmlReader::parseDeletedObject() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "DeletedObject"); @@ -400,7 +400,7 @@ void Parser::parseDeletedObject() m_db->addDeletedObject(delObj); } -Entry* Parser::parseEntry(bool history) +Entry* KeePass2XmlReader::parseEntry(bool history) { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Entry"); @@ -472,7 +472,7 @@ Entry* Parser::parseEntry(bool history) return entry; } -void Parser::parseEntryString(Entry *entry) +void KeePass2XmlReader::parseEntryString(Entry *entry) { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "String"); @@ -490,7 +490,7 @@ void Parser::parseEntryString(Entry *entry) } } -void Parser::parseEntryBinary(Entry *entry) +void KeePass2XmlReader::parseEntryBinary(Entry *entry) { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Binary"); @@ -508,7 +508,7 @@ void Parser::parseEntryBinary(Entry *entry) } } -void Parser::parseAutoType(Entry* entry) +void KeePass2XmlReader::parseAutoType(Entry* entry) { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "AutoType"); @@ -531,7 +531,7 @@ void Parser::parseAutoType(Entry* entry) } } -void Parser::parseAutoTypeAssoc(Entry *entry) +void KeePass2XmlReader::parseAutoTypeAssoc(Entry *entry) { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Association"); @@ -550,7 +550,7 @@ void Parser::parseAutoTypeAssoc(Entry *entry) } } -void Parser::parseEntryHistory(Entry* entry) +void KeePass2XmlReader::parseEntryHistory(Entry* entry) { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "History"); @@ -565,7 +565,7 @@ void Parser::parseEntryHistory(Entry* entry) } } -TimeInfo Parser::parseTimes() +TimeInfo KeePass2XmlReader::parseTimes() { Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Times"); @@ -600,12 +600,12 @@ TimeInfo Parser::parseTimes() return timeInfo; } -QString Parser::readString() +QString KeePass2XmlReader::readString() { return m_xml.readElementText(); } -bool Parser::readBool() +bool KeePass2XmlReader::readBool() { QString str = readString(); @@ -621,7 +621,7 @@ bool Parser::readBool() } } -QDateTime Parser::readDateTime() +QDateTime KeePass2XmlReader::readDateTime() { QString str = readString(); QDateTime dt = QDateTime::fromString(str, Qt::ISODate); @@ -633,7 +633,7 @@ QDateTime Parser::readDateTime() return dt; } -QColor Parser::readColor() +QColor KeePass2XmlReader::readColor() { QString colorStr = readString(); @@ -670,7 +670,7 @@ QColor Parser::readColor() return color; } -int Parser::readNumber() +int KeePass2XmlReader::readNumber() { bool ok; int result = readString().toInt(&ok); @@ -680,7 +680,7 @@ int Parser::readNumber() return result; } -Uuid Parser::readUuid() +Uuid KeePass2XmlReader::readUuid() { QByteArray uuidBin = readBinary(); if (uuidBin.length() != Uuid::length) { @@ -692,12 +692,12 @@ Uuid Parser::readUuid() } } -QByteArray Parser::readBinary() +QByteArray KeePass2XmlReader::readBinary() { return QByteArray::fromBase64(readString().toAscii()); } -Group* Parser::getGroup(const Uuid& uuid) +Group* KeePass2XmlReader::getGroup(const Uuid& uuid) { if (uuid.isNull()) { return 0; @@ -716,7 +716,7 @@ Group* Parser::getGroup(const Uuid& uuid) return group; } -Entry* Parser::getEntry(const Uuid& uuid) +Entry* KeePass2XmlReader::getEntry(const Uuid& uuid) { if (uuid.isNull()) { return 0; @@ -735,13 +735,13 @@ Entry* Parser::getEntry(const Uuid& uuid) return entry; } -void Parser::raiseError() +void KeePass2XmlReader::raiseError() { m_xml.raiseError(tr("Invalid database file")); } -void Parser::skipCurrentElement() +void KeePass2XmlReader::skipCurrentElement() { - qDebug() << "Parser::skipCurrentElement(): skip: " << m_xml.name(); + qDebug() << "KeePass2XmlReader::skipCurrentElement(): skip: " << m_xml.name(); m_xml.skipCurrentElement(); } diff --git a/src/core/Parser.h b/src/format/KeePass2XmlReader.h similarity index 93% rename from src/core/Parser.h rename to src/format/KeePass2XmlReader.h index b38bef226..6c6eaec69 100644 --- a/src/core/Parser.h +++ b/src/format/KeePass2XmlReader.h @@ -22,20 +22,20 @@ #include #include -#include "TimeInfo.h" -#include "Uuid.h" +#include "core/TimeInfo.h" +#include "core/Uuid.h" class Database; class Entry; class Group; class Metadata; -class Parser : public QObject +class KeePass2XmlReader : public QObject { Q_OBJECT public: - explicit Parser(Database* db); + explicit KeePass2XmlReader(Database* db); bool parse(const QString& filename); QString errorMsg(); diff --git a/src/core/Writer.cpp b/src/format/KeePass2XmlWriter.cpp similarity index 83% rename from src/core/Writer.cpp rename to src/format/KeePass2XmlWriter.cpp index 0de34fb95..2e0b4c8f0 100644 --- a/src/core/Writer.cpp +++ b/src/format/KeePass2XmlWriter.cpp @@ -15,14 +15,14 @@ * along with this program. If not, see . */ -#include "Writer.h" +#include "KeePass2XmlWriter.h" #include #include #include "core/Metadata.h" -Writer::Writer(Database* db) +KeePass2XmlWriter::KeePass2XmlWriter(Database* db) : QObject(db) , m_db(db) , m_meta(db->metadata()) @@ -33,7 +33,7 @@ Writer::Writer(Database* db) } -void Writer::write(QIODevice* device) +void KeePass2XmlWriter::write(QIODevice* device) { m_xml.setDevice(device); @@ -49,14 +49,14 @@ void Writer::write(QIODevice* device) m_xml.writeEndDocument(); } -void Writer::write(const QString& filename) +void KeePass2XmlWriter::write(const QString& filename) { QFile file(filename); file.open(QIODevice::WriteOnly); write(&file); } -void Writer::writeMetadata() +void KeePass2XmlWriter::writeMetadata() { m_xml.writeStartElement("Meta"); @@ -82,7 +82,7 @@ void Writer::writeMetadata() m_xml.writeEndElement(); } -void Writer::writeMemoryProtection() +void KeePass2XmlWriter::writeMemoryProtection() { m_xml.writeStartElement("MemoryProtection"); @@ -96,7 +96,7 @@ void Writer::writeMemoryProtection() m_xml.writeEndElement(); } -void Writer::writeCustomIcons() +void KeePass2XmlWriter::writeCustomIcons() { m_xml.writeStartElement("CustomIcons"); @@ -108,7 +108,7 @@ void Writer::writeCustomIcons() m_xml.writeEndElement(); } -void Writer::writeIcon(const Uuid& uuid, const QImage& image) +void KeePass2XmlWriter::writeIcon(const Uuid& uuid, const QImage& image) { m_xml.writeStartElement("Icon"); @@ -124,7 +124,7 @@ void Writer::writeIcon(const Uuid& uuid, const QImage& image) m_xml.writeEndElement(); } -void Writer::writeCustomData() +void KeePass2XmlWriter::writeCustomData() { m_xml.writeStartElement("CustomData"); @@ -136,7 +136,7 @@ void Writer::writeCustomData() m_xml.writeEndElement(); } -void Writer::writeCustomDataItem(const QString& key, const QString& value) +void KeePass2XmlWriter::writeCustomDataItem(const QString& key, const QString& value) { m_xml.writeStartElement("Item"); @@ -146,7 +146,7 @@ void Writer::writeCustomDataItem(const QString& key, const QString& value) m_xml.writeEndElement(); } -void Writer::writeRoot() +void KeePass2XmlWriter::writeRoot() { Q_ASSERT(m_db->rootGroup()); @@ -158,7 +158,7 @@ void Writer::writeRoot() m_xml.writeEndElement(); } -void Writer::writeGroup(const Group* group) +void KeePass2XmlWriter::writeGroup(const Group* group) { m_xml.writeStartElement("Group"); @@ -212,7 +212,7 @@ void Writer::writeGroup(const Group* group) m_xml.writeEndElement(); } -void Writer::writeTimes(const TimeInfo& ti) +void KeePass2XmlWriter::writeTimes(const TimeInfo& ti) { m_xml.writeStartElement("Times"); @@ -227,7 +227,7 @@ void Writer::writeTimes(const TimeInfo& ti) m_xml.writeEndElement(); } -void Writer::writeDeletedObjects() +void KeePass2XmlWriter::writeDeletedObjects() { m_xml.writeStartElement("DeletedObjects"); @@ -238,7 +238,7 @@ void Writer::writeDeletedObjects() m_xml.writeEndElement(); } -void Writer::writeDeletedObject(const DeletedObject& delObj) +void KeePass2XmlWriter::writeDeletedObject(const DeletedObject& delObj) { m_xml.writeStartElement("DeletedObject"); @@ -248,7 +248,7 @@ void Writer::writeDeletedObject(const DeletedObject& delObj) m_xml.writeEndElement(); } -void Writer::writeEntry(const Entry* entry) +void KeePass2XmlWriter::writeEntry(const Entry* entry) { m_xml.writeStartElement("Entry"); @@ -282,7 +282,7 @@ void Writer::writeEntry(const Entry* entry) m_xml.writeEndElement(); } -void Writer::writeAutoType(const Entry* entry) +void KeePass2XmlWriter::writeAutoType(const Entry* entry) { m_xml.writeStartElement("AutoType"); @@ -297,7 +297,7 @@ void Writer::writeAutoType(const Entry* entry) m_xml.writeEndElement(); } -void Writer::writeAutoTypeAssoc(const AutoTypeAssociation& assoc) +void KeePass2XmlWriter::writeAutoTypeAssoc(const AutoTypeAssociation& assoc) { m_xml.writeStartElement("Association"); @@ -307,7 +307,7 @@ void Writer::writeAutoTypeAssoc(const AutoTypeAssociation& assoc) m_xml.writeEndElement(); } -void Writer::writeEntryHistory(const Entry* entry) +void KeePass2XmlWriter::writeEntryHistory(const Entry* entry) { m_xml.writeStartElement("History"); @@ -319,17 +319,17 @@ void Writer::writeEntryHistory(const Entry* entry) m_xml.writeEndElement(); } -void Writer::writeString(const QString& qualifiedName, const QString& string) +void KeePass2XmlWriter::writeString(const QString& qualifiedName, const QString& string) { m_xml.writeTextElement(qualifiedName, string); } -void Writer::writeNumber(const QString& qualifiedName, int number) +void KeePass2XmlWriter::writeNumber(const QString& qualifiedName, int number) { writeString(qualifiedName, QString::number(number)); } -void Writer::writeBool(const QString& qualifiedName, bool b) +void KeePass2XmlWriter::writeBool(const QString& qualifiedName, bool b) { if (b) { writeString(qualifiedName, "True"); @@ -339,17 +339,17 @@ void Writer::writeBool(const QString& qualifiedName, bool b) } } -void Writer::writeDateTime(const QString& qualifiedName, const QDateTime& dateTime) +void KeePass2XmlWriter::writeDateTime(const QString& qualifiedName, const QDateTime& dateTime) { writeString(qualifiedName, dateTime.toUTC().toString(Qt::ISODate).append('Z')); } -void Writer::writeUuid(const QString& qualifiedName, const Uuid& uuid) +void KeePass2XmlWriter::writeUuid(const QString& qualifiedName, const Uuid& uuid) { writeString(qualifiedName, uuid.toBase64()); } -void Writer::writeUuid(const QString& qualifiedName, const Group* group) +void KeePass2XmlWriter::writeUuid(const QString& qualifiedName, const Group* group) { if (group) { writeUuid(qualifiedName, group->uuid()); @@ -359,7 +359,7 @@ void Writer::writeUuid(const QString& qualifiedName, const Group* group) } } -void Writer::writeUuid(const QString& qualifiedName, const Entry* entry) +void KeePass2XmlWriter::writeUuid(const QString& qualifiedName, const Entry* entry) { if (entry) { writeUuid(qualifiedName, entry->uuid()); @@ -369,12 +369,12 @@ void Writer::writeUuid(const QString& qualifiedName, const Entry* entry) } } -void Writer::writeBinary(const QString& qualifiedName, const QByteArray& ba) +void KeePass2XmlWriter::writeBinary(const QString& qualifiedName, const QByteArray& ba) { writeString(qualifiedName, QString::fromAscii(ba.toBase64())); } -void Writer::writeColor(const QString& qualifiedName, const QColor& color) +void KeePass2XmlWriter::writeColor(const QString& qualifiedName, const QColor& color) { QString colorStr = QString("#%1%2%3").arg(colorPartToString(color.red())) .arg(colorPartToString(color.green())) @@ -383,7 +383,7 @@ void Writer::writeColor(const QString& qualifiedName, const QColor& color) writeString(qualifiedName, colorStr); } -QString Writer::colorPartToString(int value) +QString KeePass2XmlWriter::colorPartToString(int value) { QString str = QString::number(value, 16).toUpper(); if (str.length() == 1) { diff --git a/src/core/Writer.h b/src/format/KeePass2XmlWriter.h similarity index 97% rename from src/core/Writer.h rename to src/format/KeePass2XmlWriter.h index 62d0fb649..0142f01c3 100644 --- a/src/core/Writer.h +++ b/src/format/KeePass2XmlWriter.h @@ -31,12 +31,12 @@ class Group; class Metadata; -class Writer : public QObject +class KeePass2XmlWriter : public QObject { Q_OBJECT public: - Writer(Database* db); + KeePass2XmlWriter(Database* db); void write(QIODevice* device); void write(const QString& filename); diff --git a/src/main.cpp b/src/main.cpp index f00b28d14..6e4873414 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,7 @@ #include #include "core/Database.h" -#include "core/Parser.h" +#include "format/KeePass2XmlReader.h" #include "gui/DatabaseWidget.h" #include "../tests/config-keepassx-tests.h" @@ -28,9 +28,9 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - Database* db= new Database(); - Parser* parser = new Parser(db); - parser->parse(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); + Database* db = new Database(); + KeePass2XmlReader* reader = new KeePass2XmlReader(db); + reader->parse(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); DatabaseWidget dbWidget(db); dbWidget.show(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b4184cd97..55523bdaa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -66,8 +66,8 @@ endmacro (ADD_UNIT_TEST) add_unit_test( testgroup TestGroup.cpp ) target_link_libraries( testgroup keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) -add_unit_test( testparser TestParser.cpp ) -target_link_libraries( testparser keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) +add_unit_test( testkeepass2xmlreader TestKeePass2XmlReader.cpp ) +target_link_libraries( testkeepass2xmlreader keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) add_unit_test( testgroupmodel TestGroupModel.cpp modeltest.cpp ) target_link_libraries( testgroupmodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) diff --git a/tests/TestParser.cpp b/tests/TestKeePass2XmlReader.cpp similarity index 98% rename from tests/TestParser.cpp rename to tests/TestKeePass2XmlReader.cpp index 85b193064..b5844f3f3 100644 --- a/tests/TestParser.cpp +++ b/tests/TestKeePass2XmlReader.cpp @@ -19,7 +19,7 @@ #include "core/Database.h" #include "core/Metadata.h" -#include "core/Parser.h" +#include "format/KeePass2XmlReader.h" #include "config-keepassx-tests.h" namespace QTest { @@ -66,9 +66,9 @@ QDateTime TestParser::genDT(int year, int month, int day, int hour, int min, int void TestParser::initTestCase() { m_db = new Database(); - Parser* parser = new Parser(m_db); + KeePass2XmlReader* reader = new KeePass2XmlReader(m_db); QString xmlFile = QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml"); - QVERIFY(parser->parse(xmlFile)); + QVERIFY(reader->parse(xmlFile)); } void TestParser::testMetadata()