From 3d7479c67b92d1a65ecd5c97efc749f914f7804a Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 14 Apr 2012 19:38:45 +0200 Subject: [PATCH] Change EntryAttributes::copyFrom() to copyCustomKeysFrom(). That way we don't overwrite the default entries in EditEntryWidget::saveEntry(). --- src/core/EntryAttributes.cpp | 80 ++++++++++++++++++++++-------------- src/core/EntryAttributes.h | 6 +-- src/gui/EditEntryWidget.cpp | 4 +- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/core/EntryAttributes.cpp b/src/core/EntryAttributes.cpp index dd27b82ee..c81bdc402 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -17,7 +17,7 @@ #include "EntryAttributes.h" -const QStringList EntryAttributes::m_defaultAttibutes(QStringList() << "Title" << "URL" << "UserName" << "Password" << "Notes"); +const QStringList EntryAttributes::DEFAULT_ATTRIBUTES(QStringList() << "Title" << "URL" << "UserName" << "Password" << "Notes"); EntryAttributes::EntryAttributes(QObject* parent) : QObject(parent) @@ -99,38 +99,63 @@ void EntryAttributes::remove(const QString& key) Q_EMIT modified(); } -void EntryAttributes::copyFrom(const EntryAttributes* other) +void EntryAttributes::copyCustomKeysFrom(const EntryAttributes* other) { - if (*this != *other) { - Q_EMIT aboutToBeReset(); - - m_attributes.clear(); - m_protectedAttributes.clear(); - - Q_FOREACH (const QString& key, other->keys()) { - m_attributes.insert(key, other->value(key)); - if (other->isProtected(key)) { - m_protectedAttributes.insert(key); - } - } - - Q_EMIT reset(); - Q_EMIT modified(); - } -} - -void EntryAttributes::clear() -{ - if (m_attributes.keys().size() == m_defaultAttibutes.size() && m_protectedAttributes.isEmpty()) { + if (!areCustomKeysDifferent(other)) { return; } Q_EMIT aboutToBeReset(); + // remove all non-default keys + Q_FOREACH (const QString& key, keys()) { + if (!isDefaultAttribute(key)) { + m_attributes.remove(key); + m_protectedAttributes.remove(key); + } + } + + Q_FOREACH (const QString& key, other->keys()) { + if (!isDefaultAttribute(key)) { + m_attributes.insert(key, other->value(key)); + if (other->isProtected(key)) { + m_protectedAttributes.insert(key); + } + } + } + + Q_EMIT reset(); + Q_EMIT modified(); +} + +bool EntryAttributes::areCustomKeysDifferent(const EntryAttributes* other) +{ + // check if they are equal ignoring the order of the keys + if (keys().toSet() != other->keys().toSet()) { + return true; + } + + Q_FOREACH (const QString& key, keys()) { + if (isDefaultAttribute(key)) { + continue; + } + + if (isProtected(key) != other->isProtected(key) || value(key) != other->value(key)) { + return true; + } + } + + return false; +} + +void EntryAttributes::clear() +{ + Q_EMIT aboutToBeReset(); + m_attributes.clear(); m_protectedAttributes.clear(); - Q_FOREACH (const QString& key, m_defaultAttibutes) { + Q_FOREACH (const QString& key, DEFAULT_ATTRIBUTES) { m_attributes.insert(key, ""); } @@ -138,12 +163,7 @@ void EntryAttributes::clear() Q_EMIT modified(); } -bool EntryAttributes::operator!=(const EntryAttributes& other) const -{ - return m_attributes != other.m_attributes || m_protectedAttributes != other.m_protectedAttributes; -} - bool EntryAttributes::isDefaultAttribute(const QString& key) { - return m_defaultAttibutes.contains(key); + return DEFAULT_ATTRIBUTES.contains(key); } diff --git a/src/core/EntryAttributes.h b/src/core/EntryAttributes.h index 68628c0f7..03a696db8 100644 --- a/src/core/EntryAttributes.h +++ b/src/core/EntryAttributes.h @@ -34,10 +34,11 @@ public: bool isProtected(const QString& key) const; void set(const QString& key, const QString& value, bool protect = false); void remove(const QString& key); - void copyFrom(const EntryAttributes* other); + void copyCustomKeysFrom(const EntryAttributes* other); void clear(); - bool operator!=(const EntryAttributes& other) const; + bool areCustomKeysDifferent(const EntryAttributes* other); + const static QStringList DEFAULT_ATTRIBUTES; static bool isDefaultAttribute(const QString& key); Q_SIGNALS: @@ -54,7 +55,6 @@ Q_SIGNALS: private: QMap m_attributes; QSet m_protectedAttributes; - const static QStringList m_defaultAttibutes; }; #endif // KEEPASSX_ENTRYATTRIBUTES_H diff --git a/src/gui/EditEntryWidget.cpp b/src/gui/EditEntryWidget.cpp index 2c981d0e6..cba9d8326 100644 --- a/src/gui/EditEntryWidget.cpp +++ b/src/gui/EditEntryWidget.cpp @@ -105,7 +105,7 @@ void EditEntryWidget::loadEntry(Entry* entry, bool create, const QString& groupN m_notesUi->notesEdit->setPlainText(entry->notes()); - m_entryAttributes->copyFrom(entry->attributes()); + m_entryAttributes->copyCustomKeysFrom(entry->attributes()); m_attributesModel->setEntryAttributes(m_entryAttributes); m_entryAttachments->copyFrom(entry->attachments()); m_attachmentsModel->setEntryAttachments(m_entryAttachments); @@ -125,7 +125,7 @@ void EditEntryWidget::saveEntry() m_entry->setNotes(m_notesUi->notesEdit->toPlainText()); - m_entry->attributes()->copyFrom(m_entryAttributes); + m_entry->attributes()->copyCustomKeysFrom(m_entryAttributes); m_entryAttributes->clear(); m_entry->attachments()->copyFrom(m_entryAttachments); m_entryAttachments->clear();