diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index a035f713..6cbf8211 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -337,17 +337,8 @@ void Entry::setExpiryTime(const QDateTime& dateTime) } } -int Entry::getHistSize() { - int size = 0; - - for(int i=0 ; igetSize(); - } - return size; -} - -int Entry::getSize() { - return attributes()->attributesSize() + attachments()->attachmentsSize(); +int Entry::getSize(QList* foundAttachements) { + return attributes()->attributesSize() + attachments()->attachmentsSize(foundAttachements); } QList Entry::historyItems() @@ -366,18 +357,49 @@ void Entry::addHistoryItem(Entry* entry) Q_ASSERT(entry->uuid() == uuid()); m_history.append(entry); - truncateHistory(); Q_EMIT modified(); } void Entry::truncateHistory() { const Database *db = database(); if(db) { - while(m_history.size() > db->metadata()->historyMaxItems()) { - m_history.removeFirst(); + int histMaxItems = db->metadata()->historyMaxItems(); + if (histMaxItems > -1) { + int historyCount = 0; + QMutableListIterator i(m_history); + i.toBack(); + while (i.hasPrevious()) { + historyCount++; + Entry* entry = i.previous(); + if (historyCount > histMaxItems) { + delete entry; + i.remove(); + } + } } - while(getHistSize() > db->metadata()->historyMaxSize()) { - m_history.removeFirst(); + + int histMaxSize = db->metadata()->historyMaxSize(); + if (histMaxSize > -1) { + int size = 0; + QList* foundAttachements = new QList(); + attachments()->attachmentsSize(foundAttachements); + + QMutableListIterator i(m_history); + i.toBack(); + while (i.hasPrevious()) { + Entry* entry = i.previous(); + if (size > histMaxSize) { + delete entry; + i.remove(); + } + else { + size += entry->getSize(foundAttachements); + if (size > histMaxSize) { + delete entry; + i.remove(); + } + } + } } } } @@ -399,10 +421,10 @@ void Entry::beginUpdate() void Entry::endUpdate() { Q_ASSERT(m_tmpHistoryItem); - if (m_modifiedSinceBegin) { m_tmpHistoryItem->setUpdateTimeinfo(true); addHistoryItem(m_tmpHistoryItem); + truncateHistory(); } else { delete m_tmpHistoryItem; diff --git a/src/core/Entry.h b/src/core/Entry.h index 7bd32466..e51b7e95 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -108,8 +108,7 @@ public: void setNotes(const QString& notes); void setExpires(const bool& value); void setExpiryTime(const QDateTime& dateTime); - int getHistSize(); - int getSize(); + int getSize(QList* foundAttachements); QList historyItems(); const QList& historyItems() const; diff --git a/src/core/EntryAttachments.cpp b/src/core/EntryAttachments.cpp index 0e82c67e..0ce2d138 100644 --- a/src/core/EntryAttachments.cpp +++ b/src/core/EntryAttachments.cpp @@ -32,11 +32,6 @@ QByteArray EntryAttachments::value(const QString& key) const return m_attachments.value(key); } -int EntryAttachments::dataSize(const QString& key) -{ - return m_attachments.value(key).size(); -} - void EntryAttachments::set(const QString& key, const QByteArray& value) { bool emitModified = false; @@ -92,11 +87,16 @@ void EntryAttachments::clear() Q_EMIT modified(); } -int EntryAttachments::attachmentsSize() { +int EntryAttachments::attachmentsSize(QList* foundAttachements) { int size = 0; - Q_FOREACH (const QString& key, keys()) { - size += dataSize(key); + QMapIterator i(m_attachments); + while (i.hasNext()) { + i.next(); + if (!foundAttachements->contains(i.value())) { + foundAttachements->append(i.value()); + size += i.value().size(); + } } return size; } diff --git a/src/core/EntryAttachments.h b/src/core/EntryAttachments.h index 4ae32156..a4cfe304 100644 --- a/src/core/EntryAttachments.h +++ b/src/core/EntryAttachments.h @@ -29,11 +29,10 @@ public: explicit EntryAttachments(QObject* parent = 0); QList keys() const; QByteArray value(const QString& key) const; - int dataSize(const QString& key); void set(const QString& key, const QByteArray& value); void remove(const QString& key); void clear(); - int attachmentsSize(); + int attachmentsSize(QList *foundAttachements); bool operator==(const EntryAttachments& other) const; bool operator!=(const EntryAttachments& other) const; EntryAttachments& operator=(EntryAttachments& other); diff --git a/src/core/EntryAttributes.cpp b/src/core/EntryAttributes.cpp index cccff9e8..66709b8e 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -36,11 +36,6 @@ QString EntryAttributes::value(const QString& key) const return m_attributes.value(key); } -int EntryAttributes::valueSize(const QString& key) -{ - return m_attributes.value(key).size() * sizeof(QChar); -} - bool EntryAttributes::isProtected(const QString& key) const { return m_protectedAttributes.contains(key); @@ -231,8 +226,10 @@ void EntryAttributes::clear() int EntryAttributes::attributesSize() { int size = 0; - Q_FOREACH (const QString& key, keys()) { - size += valueSize(key); + QMapIterator i(m_attributes); + while (i.hasNext()) { + i.next(); + size += i.value().toUtf8().size(); } return size; } diff --git a/src/core/EntryAttributes.h b/src/core/EntryAttributes.h index c5f55d10..6f4dac8e 100644 --- a/src/core/EntryAttributes.h +++ b/src/core/EntryAttributes.h @@ -22,7 +22,6 @@ #include #include #include -#include class EntryAttributes : public QObject { @@ -32,7 +31,6 @@ public: explicit EntryAttributes(QObject* parent = 0); QList keys() const; QString value(const QString& key) const; - int valueSize(const QString& key); bool isProtected(const QString& key) const; void set(const QString& key, const QString& value, bool protect = false); void remove(const QString& key); @@ -44,6 +42,7 @@ public: EntryAttributes& operator=(const EntryAttributes& other); bool operator==(const EntryAttributes& other) const; bool operator!=(const EntryAttributes& other) const; + static const QStringList DEFAULT_ATTRIBUTES; static bool isDefaultAttribute(const QString& key);