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.
This commit is contained in:
Felix Geyer
2011-07-09 21:54:01 +02:00
parent d4f02a78a7
commit 027362be76
11 changed files with 90 additions and 41 deletions

View File

@@ -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()

View File

@@ -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);

View File

@@ -40,7 +40,10 @@ Entry::Entry()
Entry::~Entry()
{
// TODO notify group
if (m_group) {
m_group->removeEntry(this);
}
qDeleteAll(m_history);
}

View File

@@ -19,6 +19,7 @@
#define KEEPASSX_ENTRY_H
#include <QtCore/QHash>
#include <QtCore/QPointer>
#include <QtCore/QSet>
#include <QtCore/QUrl>
#include <QtGui/QColor>
@@ -122,7 +123,7 @@ private:
QSet<QString> m_protectedAttachments;
QList<Entry*> m_history;
Group* m_group;
QPointer<Group> m_group;
const Database* m_db;
const static QStringList m_defaultAttibutes;
};

View File

@@ -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();
}
}

View File

@@ -18,6 +18,7 @@
#ifndef KEEPASSX_GROUP_H
#define KEEPASSX_GROUP_H
#include <QtCore/QPointer>
#include <QtGui/QIcon>
#include "core/Database.h"
@@ -90,8 +91,9 @@ private:
void setParent(Database* db);
void recSetDatabase(Database* db);
void cleanupParent();
Database* m_db;
QPointer<Database> m_db;
Uuid m_uuid;
QString m_name;
QString m_notes;
@@ -106,9 +108,10 @@ private:
QList<Group*> m_children;
QList<Entry*> m_entries;
Group* m_parent;
QPointer<Group> m_parent;
friend void Database::setRootGroup(Group* group);
friend Entry::~Entry();
friend void Entry::setGroup(Group *group);
};

View File

@@ -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());