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