From 045f157a6384dc68342cc709c9154c52f6ed95dd Mon Sep 17 00:00:00 2001 From: Christian Kieschnick Date: Mon, 15 Jan 2018 17:20:16 +0100 Subject: [PATCH] Add extend entry-size calculation (resolved #1387) Extended the calculation for the size of history items to match KeePass2 Small refactoring regarding readability in EntryAttachements --- src/core/AutoTypeAssociations.cpp | 9 +++++++++ src/core/AutoTypeAssociations.h | 1 + src/core/Entry.cpp | 12 ++++++------ src/core/EntryAttachments.cpp | 15 +++++++++++++-- src/core/EntryAttachments.h | 3 ++- src/core/EntryAttributes.cpp | 4 ++-- src/core/EntryAttributes.h | 2 +- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/core/AutoTypeAssociations.cpp b/src/core/AutoTypeAssociations.cpp index 5ec4eb3b3..3d7a2c36f 100644 --- a/src/core/AutoTypeAssociations.cpp +++ b/src/core/AutoTypeAssociations.cpp @@ -103,6 +103,15 @@ int AutoTypeAssociations::size() const return m_associations.size(); } +int AutoTypeAssociations::associationsSize() const +{ + int size = 0; + foreach (const Association &association, m_associations) { + size += association.sequence.toUtf8().size() + association.window.toUtf8().size(); + } + return size; +} + void AutoTypeAssociations::clear() { m_associations.clear(); diff --git a/src/core/AutoTypeAssociations.h b/src/core/AutoTypeAssociations.h index 61ef3fd4a..31e58cda0 100644 --- a/src/core/AutoTypeAssociations.h +++ b/src/core/AutoTypeAssociations.h @@ -43,6 +43,7 @@ public: AutoTypeAssociations::Association get(int index) const; QList getAll() const; int size() const; + int associationsSize() const; void clear(); private: diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index ed8860835..b8e589291 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -582,7 +582,7 @@ void Entry::truncateHistory() int histMaxSize = db->metadata()->historyMaxSize(); if (histMaxSize > -1) { int size = 0; - QSet foundAttachments = attachments()->values().toSet(); + QSet foundAttachments = attachments()->values(); QMutableListIterator i(m_history); i.toBack(); @@ -592,12 +592,12 @@ void Entry::truncateHistory() // don't calculate size if it's already above the maximum if (size <= histMaxSize) { size += historyItem->attributes()->attributesSize(); - - const QSet newAttachments = historyItem->attachments()->values().toSet() - foundAttachments; - for (const QByteArray& attachment : newAttachments) { - size += attachment.size(); + size += historyItem->autoTypeAssociations()->associationsSize(); + size += historyItem->attachments()->attachmentsSize(foundAttachments); + foreach( const QString &tag, historyItem->tags().split(QRegExp(",|;|:"), QString::SkipEmptyParts)){ + size += tag.toUtf8().size(); } - foundAttachments += newAttachments; + foundAttachments += historyItem->attachments()->values(); } if (size > histMaxSize) { diff --git a/src/core/EntryAttachments.cpp b/src/core/EntryAttachments.cpp index 957558609..6d3e38bbb 100644 --- a/src/core/EntryAttachments.cpp +++ b/src/core/EntryAttachments.cpp @@ -18,6 +18,7 @@ #include "EntryAttachments.h" #include +#include EntryAttachments::EntryAttachments(QObject* parent) : QObject(parent) @@ -34,9 +35,9 @@ bool EntryAttachments::hasKey(const QString& key) const return m_attachments.contains(key); } -QList EntryAttachments::values() const +QSet EntryAttachments::values() const { - return m_attachments.values(); + return m_attachments.values().toSet(); } QByteArray EntryAttachments::value(const QString& key) const @@ -151,3 +152,13 @@ bool EntryAttachments::operator!=(const EntryAttachments& other) const { return m_attachments != other.m_attachments; } + +int EntryAttachments::attachmentsSize(const QSet &ignoredAttachments) const +{ + int size = 0; + const QSet consideredAttachments = m_attachments.values().toSet() - ignoredAttachments; + for (const QByteArray& attachment : consideredAttachments) { + size += attachment.size(); + } + return size; +} diff --git a/src/core/EntryAttachments.h b/src/core/EntryAttachments.h index 0dba9543f..835a13dc8 100644 --- a/src/core/EntryAttachments.h +++ b/src/core/EntryAttachments.h @@ -31,7 +31,7 @@ public: explicit EntryAttachments(QObject* parent = nullptr); QList keys() const; bool hasKey(const QString& key) const; - QList values() const; + QSet values() const; QByteArray value(const QString& key) const; void set(const QString& key, const QByteArray& value); void remove(const QString& key); @@ -41,6 +41,7 @@ public: void copyDataFrom(const EntryAttachments* other); bool operator==(const EntryAttachments& other) const; bool operator!=(const EntryAttachments& other) const; + int attachmentsSize(const QSet &ignoredAttachments) const; signals: void modified(); diff --git a/src/core/EntryAttributes.cpp b/src/core/EntryAttributes.cpp index 8cc7f2f0a..4297f10ac 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -283,14 +283,14 @@ void EntryAttributes::clear() emit modified(); } -int EntryAttributes::attributesSize() +int EntryAttributes::attributesSize() const { int size = 0; QMapIterator i(m_attributes); while (i.hasNext()) { i.next(); - size += i.value().toUtf8().size(); + size += i.key().toUtf8().size() + i.value().toUtf8().size(); } return size; } diff --git a/src/core/EntryAttributes.h b/src/core/EntryAttributes.h index f483b8a9b..3eca9171c 100644 --- a/src/core/EntryAttributes.h +++ b/src/core/EntryAttributes.h @@ -45,7 +45,7 @@ public: void copyCustomKeysFrom(const EntryAttributes* other); bool areCustomKeysDifferent(const EntryAttributes* other); void clear(); - int attributesSize(); + int attributesSize() const; void copyDataFrom(const EntryAttributes* other); bool operator==(const EntryAttributes& other) const; bool operator!=(const EntryAttributes& other) const;