From 027362be76cde3ff386ad657a7c1ab44087f2c9d Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 9 Jul 2011 21:54:01 +0200 Subject: Notify entry/group parent on deletion. Also make the root group pseudo static, i.e. it shouldn't be changed after the database has been fully constructed. --- src/core/Database.cpp | 7 +++---- src/core/Database.h | 4 ++++ src/core/Entry.cpp | 5 ++++- src/core/Entry.h | 3 ++- src/core/Group.cpp | 37 +++++++++++++++---------------------- src/core/Group.h | 7 +++++-- src/format/KeePass2XmlReader.cpp | 6 ++++-- 7 files changed, 37 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 2fe208909..20fe8cbc7 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -28,7 +28,7 @@ Database::Database() { m_metadata = new Metadata(this); - m_rootGroup = 0; + setRootGroup(new Group()); m_cipher = KeePass2::CIPHER_AES; m_compressionAlgo = CompressionGZip; @@ -47,11 +47,10 @@ const Group* Database::rootGroup() const void Database::setRootGroup(Group* group) { - if (group != 0) { - group->setParent(this); - } + Q_ASSERT(group); m_rootGroup = group; + m_rootGroup->setParent(this); } Metadata* Database::metadata() diff --git a/src/core/Database.h b/src/core/Database.h index 8876b8382..dc7fa3bbb 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -53,6 +53,10 @@ public: /** * Sets group as the root group and takes ownership of it. + * Warning: Be careful when calling this method as it doesn't + * emit any notifications so e.g. models aren't updated. + * The caller is responsible for cleaning up the pervious + root group. */ void setRootGroup(Group* group); diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index ac47a2fc3..0d7c1848d 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -40,7 +40,10 @@ Entry::Entry() Entry::~Entry() { - // TODO notify group + if (m_group) { + m_group->removeEntry(this); + } + qDeleteAll(m_history); } diff --git a/src/core/Entry.h b/src/core/Entry.h index 248bb71a4..319b0da04 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -19,6 +19,7 @@ #define KEEPASSX_ENTRY_H #include +#include #include #include #include @@ -122,7 +123,7 @@ private: QSet m_protectedAttachments; QList m_history; - Group* m_group; + QPointer m_group; const Database* m_db; const static QStringList m_defaultAttibutes; }; diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 543cdb75c..e5732c8f1 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -37,7 +37,7 @@ Group::Group() Group::~Group() { - // TODO notify parent + cleanupParent(); } Uuid Group::uuid() const @@ -186,22 +186,14 @@ void Group::setParent(Group* parent, int index) { Q_ASSERT(parent); Q_ASSERT(index >= -1 && index <= parent->children().size()); + // setting a new parent for root groups is not allowed + Q_ASSERT(!m_db || (m_db->rootGroup() != this)); if (index == -1) { index = parent->children().size(); } - Q_EMIT aboutToRemove(this); - - if (m_parent) { - m_parent->m_children.removeAll(this); - } - else if (m_db) { - // parent was a Database - m_db->setRootGroup(0); - } - - Q_EMIT removed(); + cleanupParent(); m_parent = parent; @@ -222,16 +214,7 @@ void Group::setParent(Database* db) { Q_ASSERT(db); - Q_EMIT aboutToRemove(this); - - if (m_parent) { - m_parent->m_children.removeAll(this); - } - else if (m_db) { - m_db->setRootGroup(0); - } - - Q_EMIT removed(); + cleanupParent(); m_parent = 0; recSetDatabase(db); @@ -295,6 +278,7 @@ void Group::recSetDatabase(Database* db) disconnect(SIGNAL(added()), m_db); connect(this, SIGNAL(dataChanged(Group*)), db, SIGNAL(groupDataChanged(Group*))); + connect(this, SIGNAL(aboutToRemove(Group*)), db, SIGNAL(groupAboutToRemove(Group*))); connect(this, SIGNAL(removed()), db, SIGNAL(groupRemoved())); connect(this, SIGNAL(aboutToAdd(Group*,int)), db, SIGNAL(groupAboutToAdd(Group*,int))); @@ -306,3 +290,12 @@ void Group::recSetDatabase(Database* db) group->recSetDatabase(db); } } + +void Group::cleanupParent() +{ + if (m_parent) { + Q_EMIT aboutToRemove(this); + m_parent->m_children.removeAll(this); + Q_EMIT removed(); + } +} diff --git a/src/core/Group.h b/src/core/Group.h index 54febf9ce..64734a7ad 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -18,6 +18,7 @@ #ifndef KEEPASSX_GROUP_H #define KEEPASSX_GROUP_H +#include #include #include "core/Database.h" @@ -90,8 +91,9 @@ private: void setParent(Database* db); void recSetDatabase(Database* db); + void cleanupParent(); - Database* m_db; + QPointer m_db; Uuid m_uuid; QString m_name; QString m_notes; @@ -106,9 +108,10 @@ private: QList m_children; QList m_entries; - Group* m_parent; + QPointer m_parent; friend void Database::setRootGroup(Group* group); + friend Entry::~Entry(); friend void Entry::setGroup(Group *group); }; diff --git a/src/format/KeePass2XmlReader.cpp b/src/format/KeePass2XmlReader.cpp index aa2549218..af3b7d450 100644 --- a/src/format/KeePass2XmlReader.cpp +++ b/src/format/KeePass2XmlReader.cpp @@ -41,7 +41,6 @@ void KeePass2XmlReader::readDatabase(QIODevice* device, Database* db, KeePass2Ra m_randomStream = randomStream; m_tmpParent = new Group(); - m_db->setRootGroup(m_tmpParent); if (!m_xml.error() && m_xml.readNextStartElement()) { if (m_xml.name() == "KeePassFile") { @@ -49,6 +48,7 @@ void KeePass2XmlReader::readDatabase(QIODevice* device, Database* db, KeePass2Ra } } + // TODO check if m_tmpParent doesn't have entries if (!m_xml.error() && !m_tmpParent->children().isEmpty()) { raiseError(); } @@ -277,7 +277,9 @@ void KeePass2XmlReader::parseRoot() if (m_xml.name() == "Group") { Group* rootGroup = parseGroup(); if (rootGroup) { + Group* oldRoot = m_db->rootGroup(); m_db->setRootGroup(rootGroup); + delete oldRoot; } } else if (m_xml.name() == "DeletedObjects") { @@ -302,7 +304,7 @@ Group* KeePass2XmlReader::parseGroup() } else { group = getGroup(uuid); - } + } } else if (m_xml.name() == "Name") { group->setName(readString()); -- cgit v1.2.3