Handle CustomData element.

This should make support for reading and writing
KeePass 2 XML files complete (closes #1).
This commit is contained in:
Felix Geyer 2010-08-26 00:31:07 +02:00
parent 8df8f69e10
commit 3bf0564436
10 changed files with 65 additions and 67 deletions

View File

@ -19,7 +19,6 @@ configure_file( config-keepassx.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-keepa
set(keepassx_SOURCES
core/Database.cpp
core/DbAttribute.cpp
core/Entry.cpp
core/Group.cpp
core/Metadata.cpp

View File

@ -20,8 +20,6 @@
#include "Group.h"
#include "DbAttribute.h"
#include <QtCore/QHash>
#include <QtGui/QImage>
@ -64,7 +62,6 @@ private:
Metadata* m_metadata;
Group* m_rootGroup;
QHash<Uuid, QImage> m_customIcons;
DbAttribute m_unhandledAttirbute;
QList<DeletedObject> m_deletedObjects;
};

View File

@ -1,22 +0,0 @@
/*
* 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/>.
*/
#include "DbAttribute.h"
DbAttribute::DbAttribute()
{
}

View File

@ -1,33 +0,0 @@
/*
* 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_DBATTRIBUTE_H
#define KEEPASSX_DBATTRIBUTE_H
#include <QtCore/QHash>
class DbAttribute
{
public:
DbAttribute();
private:
QHash<QString, QString> properties;
QHash<QString, DbAttribute> children;
};
#endif // KEEPASSX_DBATTRIBUTE_H

View File

@ -210,9 +210,31 @@ void Parser::parseCustomData()
{
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "CustomData");
// TODO implement
while (!m_xml.error() && m_xml.readNextStartElement()) {
skipCurrentElement();
if (m_xml.name() == "Item") {
parseCustomDataItem();
}
else {
skipCurrentElement();
}
}
}
void Parser::parseCustomDataItem()
{
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Item");
QString key;
while (!m_xml.error() && m_xml.readNextStartElement()) {
if (m_xml.name() == "Key") {
key = readString();
}
else if (m_xml.name() == "Value") {
m_meta->addCustomField(key, readString());
}
else {
skipCurrentElement();
}
}
}

View File

@ -46,6 +46,7 @@ private:
void parseCustomIcons();
void parseIcon();
void parseCustomData();
void parseCustomDataItem();
void parseRoot();
Group* parseGroup();
void parseDeletedObjects();

View File

@ -100,8 +100,9 @@ void Writer::writeCustomIcons()
{
m_xml.writeStartElement("CustomIcons");
Q_FOREACH (const Uuid& uuid, m_meta->customIcons().keys()) {
writeIcon(uuid, m_meta->customIcons().value(uuid));
QHash<Uuid, QImage> customIcons = m_meta->customIcons();
Q_FOREACH (const Uuid& uuid, customIcons.keys()) {
writeIcon(uuid, customIcons.value(uuid));
}
m_xml.writeEndElement();
@ -127,7 +128,20 @@ void Writer::writeCustomData()
{
m_xml.writeStartElement("CustomData");
// TODO implement
QHash<QString, QString> customFields = m_meta->customFields();
Q_FOREACH (const QString& key, customFields.keys()) {
writeCustomDataItem(key, customFields.value(key));
}
m_xml.writeEndElement();
}
void Writer::writeCustomDataItem(const QString& key, const QString& value)
{
m_xml.writeStartElement("Item");
writeString("Key", key);
writeString("Value", value);
m_xml.writeEndElement();
}

View File

@ -46,6 +46,7 @@ private:
void writeCustomIcons();
void writeIcon(const Uuid& uuid, const QImage& image);
void writeCustomData();
void writeCustomDataItem(const QString& key, const QString& value);
void writeRoot();
void writeGroup(const Group* group);
void writeTimes(const TimeInfo& ti);

View File

@ -30,7 +30,16 @@
<EntryTemplatesGroupChanged>2010-08-08T17:24:19Z</EntryTemplatesGroupChanged>
<LastSelectedGroup>lmU+9n0aeESKZvcEze+bRg==</LastSelectedGroup>
<LastTopVisibleGroup>lmU+9n0aeESKZvcEze+bRg==</LastTopVisibleGroup>
<CustomData />
<CustomData>
<Item>
<Key>A Sample Test Key</Key>
<Value>valu</Value>
</Item>
<Item>
<Key>custom key</Key>
<Value>blub</Value>
</Item>
</CustomData>
</Meta>
<Root>
<Group>

View File

@ -40,7 +40,8 @@ class TestParser : public QObject
private Q_SLOTS:
void initTestCase();
void testMetadata();
void testCustomIcon();
void testCustomIcons();
void testCustomData();
void testGroupRoot();
void testGroup1();
void testGroup2();
@ -97,7 +98,7 @@ void TestParser::testMetadata()
QVERIFY(m_db->metadata()->lastTopVisibleGroup() == m_db->metadata()->lastSelectedGroup());
}
void TestParser::testCustomIcon()
void TestParser::testCustomIcons()
{
QCOMPARE(m_db->metadata()->customIcons().size(), 1);
Uuid uuid = Uuid::fromBase64("++vyI+daLk6omox4a6kQGA==");
@ -115,6 +116,15 @@ void TestParser::testCustomIcon()
}
}
void TestParser::testCustomData()
{
QHash<QString, QString> customFields = m_db->metadata()->customFields();
QCOMPARE(customFields.size(), 2);
QCOMPARE(customFields.value("A Sample Test Key"), QString("valu"));
QCOMPARE(customFields.value("custom key"), QString("blub"));
}
void TestParser::testGroupRoot()
{
const Group* group = m_db->rootGroup();