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(); 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() void AutoTypeAssociations::clear()
{ {
m_associations.clear(); m_associations.clear();

View file

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

View file

@ -582,7 +582,7 @@ void Entry::truncateHistory()
int histMaxSize = db->metadata()->historyMaxSize(); int histMaxSize = db->metadata()->historyMaxSize();
if (histMaxSize > -1) { if (histMaxSize > -1) {
int size = 0; int size = 0;
QSet<QByteArray> foundAttachments = attachments()->values().toSet(); QSet<QByteArray> foundAttachments = attachments()->values();
QMutableListIterator<Entry*> i(m_history); QMutableListIterator<Entry*> i(m_history);
i.toBack(); i.toBack();
@ -592,12 +592,12 @@ void Entry::truncateHistory()
// don't calculate size if it's already above the maximum // don't calculate size if it's already above the maximum
if (size <= histMaxSize) { if (size <= histMaxSize) {
size += historyItem->attributes()->attributesSize(); size += historyItem->attributes()->attributesSize();
size += historyItem->autoTypeAssociations()->associationsSize();
const QSet<QByteArray> newAttachments = historyItem->attachments()->values().toSet() - foundAttachments; size += historyItem->attachments()->attachmentsSize(foundAttachments);
for (const QByteArray& attachment : newAttachments) { foreach( const QString &tag, historyItem->tags().split(QRegExp(",|;|:"), QString::SkipEmptyParts)){
size += attachment.size(); size += tag.toUtf8().size();
} }
foundAttachments += newAttachments; foundAttachments += historyItem->attachments()->values();
} }
if (size > histMaxSize) { if (size > histMaxSize) {

View file

@ -18,6 +18,7 @@
#include "EntryAttachments.h" #include "EntryAttachments.h"
#include <QStringList> #include <QStringList>
#include <QSet>
EntryAttachments::EntryAttachments(QObject* parent) EntryAttachments::EntryAttachments(QObject* parent)
: QObject(parent) : QObject(parent)
@ -34,9 +35,9 @@ bool EntryAttachments::hasKey(const QString& key) const
return m_attachments.contains(key); 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 QByteArray EntryAttachments::value(const QString& key) const
@ -151,3 +152,13 @@ bool EntryAttachments::operator!=(const EntryAttachments& other) const
{ {
return m_attachments != other.m_attachments; 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); explicit EntryAttachments(QObject* parent = nullptr);
QList<QString> keys() const; QList<QString> keys() const;
bool hasKey(const QString& key) const; bool hasKey(const QString& key) const;
QList<QByteArray> values() const; QSet<QByteArray> values() const;
QByteArray value(const QString& key) const; QByteArray value(const QString& key) const;
void set(const QString& key, const QByteArray& value); void set(const QString& key, const QByteArray& value);
void remove(const QString& key); void remove(const QString& key);
@ -41,6 +41,7 @@ public:
void copyDataFrom(const EntryAttachments* other); void copyDataFrom(const EntryAttachments* other);
bool operator==(const EntryAttachments& other) const; bool operator==(const EntryAttachments& other) const;
bool operator!=(const EntryAttachments& other) const; bool operator!=(const EntryAttachments& other) const;
int attachmentsSize(const QSet<QByteArray> &ignoredAttachments) const;
signals: signals:
void modified(); void modified();

View file

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

View file

@ -45,7 +45,7 @@ public:
void copyCustomKeysFrom(const EntryAttributes* other); void copyCustomKeysFrom(const EntryAttributes* other);
bool areCustomKeysDifferent(const EntryAttributes* other); bool areCustomKeysDifferent(const EntryAttributes* other);
void clear(); void clear();
int attributesSize(); int attributesSize() const;
void copyDataFrom(const EntryAttributes* other); void copyDataFrom(const EntryAttributes* other);
bool operator==(const EntryAttributes& other) const; bool operator==(const EntryAttributes& other) const;
bool operator!=(const EntryAttributes& other) const; bool operator!=(const EntryAttributes& other) const;