diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 8da259e23..c98cc2a1c 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -402,11 +402,6 @@ void Entry::setExpiryTime(const QDateTime& dateTime) } } -int Entry::getSize(QList* foundAttachements) -{ - return attributes()->attributesSize() + attachments()->attachmentsSize(foundAttachements); -} - QList Entry::historyItems() { return m_history; @@ -467,21 +462,26 @@ void Entry::truncateHistory() int histMaxSize = db->metadata()->historyMaxSize(); if (histMaxSize > -1) { int size = 0; - QList foundAttachements; - attachments()->attachmentsSize(&foundAttachements); + QSet foundAttachements = attachments()->values().toSet(); QMutableListIterator i(m_history); i.toBack(); while (i.hasPrevious()) { - Entry* entry = i.previous(); + Entry* historyItem = i.previous(); // don't calculate size if it's already above the maximum if (size <= histMaxSize) { - size += entry->getSize(&foundAttachements); + size += historyItem->attributes()->attributesSize(); + + QSet newAttachments = historyItem->attachments()->values().toSet() - foundAttachements; + Q_FOREACH (const QByteArray& attachment, newAttachments) { + size += attachment.size(); + } + foundAttachements += newAttachments; } if (size > histMaxSize) { - delete entry; + delete historyItem; i.remove(); } } diff --git a/src/core/Entry.h b/src/core/Entry.h index f3536fa9e..9a292f359 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -105,7 +105,6 @@ public: void setNotes(const QString& notes); void setExpires(const bool& value); void setExpiryTime(const QDateTime& dateTime); - int getSize(QList* foundAttachements); QList historyItems(); const QList& historyItems() const; diff --git a/src/core/EntryAttachments.cpp b/src/core/EntryAttachments.cpp index a8dc89531..dc89576e3 100644 --- a/src/core/EntryAttachments.cpp +++ b/src/core/EntryAttachments.cpp @@ -27,6 +27,11 @@ QList EntryAttachments::keys() const return m_attachments.keys(); } +QList EntryAttachments::values() const +{ + return m_attachments.values(); +} + QByteArray EntryAttachments::value(const QString& key) const { return m_attachments.value(key); @@ -87,21 +92,6 @@ void EntryAttachments::clear() Q_EMIT modified(); } -int EntryAttachments::attachmentsSize(QList* foundAttachements) -{ - int size = 0; - - QMapIterator i(m_attachments); - while (i.hasNext()) { - i.next(); - if (!foundAttachements->contains(i.value())) { - foundAttachements->append(i.value()); - size += i.value().size(); - } - } - return size; -} - bool EntryAttachments::operator==(const EntryAttachments& other) const { return m_attachments == other.m_attachments; diff --git a/src/core/EntryAttachments.h b/src/core/EntryAttachments.h index db7ce2a4e..a62f8b08a 100644 --- a/src/core/EntryAttachments.h +++ b/src/core/EntryAttachments.h @@ -30,11 +30,11 @@ class EntryAttachments : public QObject public: explicit EntryAttachments(QObject* parent = Q_NULLPTR); QList keys() const; + QList values() const; QByteArray value(const QString& key) const; void set(const QString& key, const QByteArray& value); void remove(const QString& key); void clear(); - int attachmentsSize(QList* foundAttachements); bool operator==(const EntryAttachments& other) const; bool operator!=(const EntryAttachments& other) const; EntryAttachments& operator=(const EntryAttachments& other);