Change EntryAttributes::copyFrom() to copyCustomKeysFrom().

That way we don't overwrite the default entries in EditEntryWidget::saveEntry().
This commit is contained in:
Felix Geyer 2012-04-14 19:38:45 +02:00
parent 51854a7a45
commit 3d7479c67b
3 changed files with 55 additions and 35 deletions

View File

@ -17,7 +17,7 @@
#include "EntryAttributes.h" #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) EntryAttributes::EntryAttributes(QObject* parent)
: QObject(parent) : QObject(parent)
@ -99,38 +99,63 @@ void EntryAttributes::remove(const QString& key)
Q_EMIT modified(); Q_EMIT modified();
} }
void EntryAttributes::copyFrom(const EntryAttributes* other) void EntryAttributes::copyCustomKeysFrom(const EntryAttributes* other)
{ {
if (*this != *other) { if (!areCustomKeysDifferent(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()) {
return; return;
} }
Q_EMIT aboutToBeReset(); 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_attributes.clear();
m_protectedAttributes.clear(); m_protectedAttributes.clear();
Q_FOREACH (const QString& key, m_defaultAttibutes) { Q_FOREACH (const QString& key, DEFAULT_ATTRIBUTES) {
m_attributes.insert(key, ""); m_attributes.insert(key, "");
} }
@ -138,12 +163,7 @@ void EntryAttributes::clear()
Q_EMIT modified(); 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) bool EntryAttributes::isDefaultAttribute(const QString& key)
{ {
return m_defaultAttibutes.contains(key); return DEFAULT_ATTRIBUTES.contains(key);
} }

View File

@ -34,10 +34,11 @@ public:
bool isProtected(const QString& key) const; bool isProtected(const QString& key) const;
void set(const QString& key, const QString& value, bool protect = false); void set(const QString& key, const QString& value, bool protect = false);
void remove(const QString& key); void remove(const QString& key);
void copyFrom(const EntryAttributes* other); void copyCustomKeysFrom(const EntryAttributes* other);
void clear(); void clear();
bool operator!=(const EntryAttributes& other) const; bool areCustomKeysDifferent(const EntryAttributes* other);
const static QStringList DEFAULT_ATTRIBUTES;
static bool isDefaultAttribute(const QString& key); static bool isDefaultAttribute(const QString& key);
Q_SIGNALS: Q_SIGNALS:
@ -54,7 +55,6 @@ Q_SIGNALS:
private: private:
QMap<QString, QString> m_attributes; QMap<QString, QString> m_attributes;
QSet<QString> m_protectedAttributes; QSet<QString> m_protectedAttributes;
const static QStringList m_defaultAttibutes;
}; };
#endif // KEEPASSX_ENTRYATTRIBUTES_H #endif // KEEPASSX_ENTRYATTRIBUTES_H

View File

@ -105,7 +105,7 @@ void EditEntryWidget::loadEntry(Entry* entry, bool create, const QString& groupN
m_notesUi->notesEdit->setPlainText(entry->notes()); m_notesUi->notesEdit->setPlainText(entry->notes());
m_entryAttributes->copyFrom(entry->attributes()); m_entryAttributes->copyCustomKeysFrom(entry->attributes());
m_attributesModel->setEntryAttributes(m_entryAttributes); m_attributesModel->setEntryAttributes(m_entryAttributes);
m_entryAttachments->copyFrom(entry->attachments()); m_entryAttachments->copyFrom(entry->attachments());
m_attachmentsModel->setEntryAttachments(m_entryAttachments); m_attachmentsModel->setEntryAttachments(m_entryAttachments);
@ -125,7 +125,7 @@ void EditEntryWidget::saveEntry()
m_entry->setNotes(m_notesUi->notesEdit->toPlainText()); m_entry->setNotes(m_notesUi->notesEdit->toPlainText());
m_entry->attributes()->copyFrom(m_entryAttributes); m_entry->attributes()->copyCustomKeysFrom(m_entryAttributes);
m_entryAttributes->clear(); m_entryAttributes->clear();
m_entry->attachments()->copyFrom(m_entryAttachments); m_entry->attachments()->copyFrom(m_entryAttachments);
m_entryAttachments->clear(); m_entryAttachments->clear();