Refactor Entry::truncateHistory().

This allows us to drop Entry::getSize() and EntryAttachments::attachmentsSize()
which have weird semantics.
This commit is contained in:
Felix Geyer 2012-07-20 00:45:34 +02:00
parent 0c1fecfb2b
commit ef579cbe3c
4 changed files with 16 additions and 27 deletions

View File

@ -402,11 +402,6 @@ void Entry::setExpiryTime(const QDateTime& dateTime)
}
}
int Entry::getSize(QList<QByteArray>* foundAttachements)
{
return attributes()->attributesSize() + attachments()->attachmentsSize(foundAttachements);
}
QList<Entry*> Entry::historyItems()
{
return m_history;
@ -467,21 +462,26 @@ void Entry::truncateHistory()
int histMaxSize = db->metadata()->historyMaxSize();
if (histMaxSize > -1) {
int size = 0;
QList<QByteArray> foundAttachements;
attachments()->attachmentsSize(&foundAttachements);
QSet<QByteArray> foundAttachements = attachments()->values().toSet();
QMutableListIterator<Entry*> 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<QByteArray> 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();
}
}

View File

@ -105,7 +105,6 @@ public:
void setNotes(const QString& notes);
void setExpires(const bool& value);
void setExpiryTime(const QDateTime& dateTime);
int getSize(QList<QByteArray>* foundAttachements);
QList<Entry*> historyItems();
const QList<Entry*>& historyItems() const;

View File

@ -27,6 +27,11 @@ QList<QString> EntryAttachments::keys() const
return m_attachments.keys();
}
QList<QByteArray> 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<QByteArray>* foundAttachements)
{
int size = 0;
QMapIterator<QString, QByteArray> 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;

View File

@ -30,11 +30,11 @@ class EntryAttachments : public QObject
public:
explicit EntryAttachments(QObject* parent = Q_NULLPTR);
QList<QString> keys() const;
QList<QByteArray> 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<QByteArray>* foundAttachements);
bool operator==(const EntryAttachments& other) const;
bool operator!=(const EntryAttachments& other) const;
EntryAttachments& operator=(const EntryAttachments& other);