From 01694c3271c2bfb7e4898e993f04cd04cd3e4c4f Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 14 Aug 2010 12:24:35 +0200 Subject: [PATCH] Implement Entry::image(). --- src/core/Database.cpp | 2 +- src/core/Database.h | 2 +- src/core/Entry.cpp | 16 ++++++++++++++-- src/core/Entry.h | 2 ++ src/core/Group.cpp | 29 +++++++++++++++++++++++++++-- src/core/Group.h | 3 +++ src/core/Parser.cpp | 12 ++++++++++-- 7 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index cd2c50c68..368c96683 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -50,7 +50,7 @@ QImage Database::icon(int number) return QImage(); } -QImage Database::customIcon(const Uuid& uuid) +QImage Database::customIcon(const Uuid& uuid) const { return m_customIcons[uuid]; } diff --git a/src/core/Database.h b/src/core/Database.h index f89e083d0..a7f275b9e 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -37,7 +37,7 @@ public: void setRootGroup(Group* group); Metadata* metadata(); static QImage icon(int number); - QImage customIcon(const Uuid& uuid); + QImage customIcon(const Uuid& uuid) const; Entry* resolveEntry(const Uuid& uuid); Group* resolveGroup(const Uuid& uuid); diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index b5e87a051..4aa4b2cef 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -17,11 +17,13 @@ #include "Entry.h" +#include "Database.h" #include "Group.h" Entry::Entry() { m_group = 0; + m_db = 0; } Uuid Entry::uuid() const @@ -31,8 +33,12 @@ Uuid Entry::uuid() const QImage Entry::icon() const { - // TODO implement - return QImage(); + Q_ASSERT(m_iconNumber != 0 || !m_customIcon.isNull()); + + if (m_iconNumber == 0) + return m_db->customIcon(m_customIcon); + else + return Database::icon(m_iconNumber); } QColor Entry::foregroundColor() const @@ -88,17 +94,22 @@ const QHash& Entry::attachments() const void Entry::setUuid(const Uuid& uuid) { Q_ASSERT(!uuid.isNull()); + m_uuid = uuid; } void Entry::setIcon(int iconNumber) { + Q_ASSERT(iconNumber >= 0); + m_iconNumber = iconNumber; m_customIcon = Uuid(); } void Entry::setIcon(const Uuid& uuid) { + Q_ASSERT(!uuid.isNull()); + m_iconNumber = 0; m_customIcon = uuid; } @@ -160,5 +171,6 @@ void Entry::setGroup(Group* group) } group->addEntry(this); m_group = group; + m_db = group->database(); QObject::setParent(group); } diff --git a/src/core/Entry.h b/src/core/Entry.h index 7bb85cba1..f44178cfa 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -26,6 +26,7 @@ #include "TimeInfo.h" #include "Uuid.h" +class Database; class Group; struct AutoTypeAssociation @@ -85,6 +86,7 @@ private: QHash m_binaries; Group* m_group; + const Database* m_db; }; #endif // KEEPASSX_ENTRY_H diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 478aa0884..dc4359484 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -88,12 +88,16 @@ void Group::setNotes(const QString& notes) void Group::setIcon(int iconNumber) { + Q_ASSERT(iconNumber >= 0); + m_iconNumber = iconNumber; m_customIcon = Uuid(); } void Group::setIcon(const Uuid& uuid) { + Q_ASSERT(!uuid.isNull()); + m_iconNumber = 0; m_customIcon = uuid; } @@ -130,7 +134,11 @@ void Group::setParent(Group* parent) } m_parent = parent; - m_db = parent->m_db; + + if (m_db != parent->m_db) { + recSetDatabase(parent->m_db); + } + QObject::setParent(parent); parent->m_children << this; @@ -148,12 +156,18 @@ void Group::setParent(Database* db) } m_parent = 0; - m_db = db; + recSetDatabase(db); + QObject::setParent(db); db->setRootGroup(this); } +const Database* Group::database() const +{ + return m_db; +} + QList Group::children() const { return m_children; @@ -166,6 +180,8 @@ QList Group::entries() const void Group::addEntry(Entry *entry) { + Q_ASSERT(entry != 0); + m_entries << entry; } @@ -173,3 +189,12 @@ void Group::removeEntry(Entry* entry) { m_entries.removeAll(entry); } + +void Group::recSetDatabase(Database* db) +{ + m_db = db; + + Q_FOREACH (Group* group, m_children) { + group->recSetDatabase(db); + } +} diff --git a/src/core/Group.h b/src/core/Group.h index 7802f7da2..7f851443d 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -54,12 +54,15 @@ public: void setParent(Group* parent); void setParent(Database* db); + const Database* database() const; QList children() const; QList entries() const; void addEntry(Entry* entry); void removeEntry(Entry* entry); private: + void recSetDatabase(Database* db); + Database* m_db; Uuid m_uuid; QString m_name; diff --git a/src/core/Parser.cpp b/src/core/Parser.cpp index 2b9cbc03d..443453f19 100644 --- a/src/core/Parser.cpp +++ b/src/core/Parser.cpp @@ -260,8 +260,12 @@ Group* Parser::parseGroup() } else if (m_xml.name() == "IconID") { int iconId = readNumber(); - if (iconId != 0) + if (iconId < 0) { + raiseError(); + } + else if (iconId != 0) { group->setIcon(iconId); + } } else if (m_xml.name() == "CustomIconUUID") { Uuid uuid = readUuid(); @@ -326,8 +330,12 @@ Entry* Parser::parseEntry() } else if (m_xml.name() == "IconID") { int iconId = readNumber(); - if (iconId != 0) + if (iconId < 0) { + raiseError(); + } + else if (iconId != 0) { entry->setIcon(iconId); + } } else if (m_xml.name() == "CustomIconUUID") { Uuid uuid = readUuid();