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
This commit is contained in:
Christian Kieschnick 2018-01-15 17:20:16 +01:00 committed by Janek Bevendorff
parent 83f1a53d32
commit 045f157a63
No known key found for this signature in database
GPG Key ID: 2FDEB0D40BCA5E11
7 changed files with 34 additions and 12 deletions

View File

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

View File

@ -43,6 +43,7 @@ public:
AutoTypeAssociations::Association get(int index) const;
QList<AutoTypeAssociations::Association> getAll() const;
int size() const;
int associationsSize() const;
void clear();
private:

View File

@ -582,7 +582,7 @@ void Entry::truncateHistory()
int histMaxSize = db->metadata()->historyMaxSize();
if (histMaxSize > -1) {
int size = 0;
QSet<QByteArray> foundAttachments = attachments()->values().toSet();
QSet<QByteArray> foundAttachments = attachments()->values();
QMutableListIterator<Entry*> 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<QByteArray> 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) {

View File

@ -18,6 +18,7 @@
#include "EntryAttachments.h"
#include <QStringList>
#include <QSet>
EntryAttachments::EntryAttachments(QObject* parent)
: QObject(parent)
@ -34,9 +35,9 @@ bool EntryAttachments::hasKey(const QString& key) const
return m_attachments.contains(key);
}
QList<QByteArray> EntryAttachments::values() const
QSet<QByteArray> 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<QByteArray> &ignoredAttachments) const
{
int size = 0;
const QSet<QByteArray> consideredAttachments = m_attachments.values().toSet() - ignoredAttachments;
for (const QByteArray& attachment : consideredAttachments) {
size += attachment.size();
}
return size;
}

View File

@ -31,7 +31,7 @@ public:
explicit EntryAttachments(QObject* parent = nullptr);
QList<QString> keys() const;
bool hasKey(const QString& key) const;
QList<QByteArray> values() const;
QSet<QByteArray> 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<QByteArray> &ignoredAttachments) const;
signals:
void modified();

View File

@ -283,14 +283,14 @@ void EntryAttributes::clear()
emit modified();
}
int EntryAttributes::attributesSize()
int EntryAttributes::attributesSize() const
{
int size = 0;
QMapIterator<QString, QString> i(m_attributes);
while (i.hasNext()) {
i.next();
size += i.value().toUtf8().size();
size += i.key().toUtf8().size() + i.value().toUtf8().size();
}
return size;
}

View File

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