diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 27614ecc9..a035f7138 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -337,6 +337,19 @@ 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(); +} + QList Entry::historyItems() { return m_history; @@ -353,9 +366,22 @@ 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(); + } + while(getHistSize() > db->metadata()->historyMaxSize()) { + m_history.removeFirst(); + } + } +} + void Entry::beginUpdate() { Q_ASSERT(!m_tmpHistoryItem); diff --git a/src/core/Entry.h b/src/core/Entry.h index 0536109da..7bd324664 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -108,10 +108,14 @@ public: void setNotes(const QString& notes); void setExpires(const bool& value); void setExpiryTime(const QDateTime& dateTime); + int getHistSize(); + int getSize(); QList historyItems(); const QList& historyItems() const; void addHistoryItem(Entry* entry); + void truncateHistory(); + /** * Call before and after set*() methods to create a history item * if the entry has been changed. diff --git a/src/core/EntryAttachments.cpp b/src/core/EntryAttachments.cpp index 384dee3e1..0e82c67ea 100644 --- a/src/core/EntryAttachments.cpp +++ b/src/core/EntryAttachments.cpp @@ -32,6 +32,11 @@ 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; @@ -87,6 +92,15 @@ void EntryAttachments::clear() Q_EMIT modified(); } +int EntryAttachments::attachmentsSize() { + int size = 0; + + Q_FOREACH (const QString& key, keys()) { + size += dataSize(key); + } + return size; +} + bool EntryAttachments::operator==(const EntryAttachments& other) const { return m_attachments == other.m_attachments; diff --git a/src/core/EntryAttachments.h b/src/core/EntryAttachments.h index 4d36f3b0e..4ae321566 100644 --- a/src/core/EntryAttachments.h +++ b/src/core/EntryAttachments.h @@ -29,9 +29,11 @@ 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(); 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 c35aa4b22..cccff9e85 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -36,6 +36,11 @@ 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); @@ -223,6 +228,15 @@ void EntryAttributes::clear() Q_EMIT modified(); } +int EntryAttributes::attributesSize() { + int size = 0; + + Q_FOREACH (const QString& key, keys()) { + size += valueSize(key); + } + return size; +} + bool EntryAttributes::isDefaultAttribute(const QString& key) { return DEFAULT_ATTRIBUTES.contains(key); diff --git a/src/core/EntryAttributes.h b/src/core/EntryAttributes.h index 83316811e..c5f55d108 100644 --- a/src/core/EntryAttributes.h +++ b/src/core/EntryAttributes.h @@ -22,6 +22,7 @@ #include #include #include +#include class EntryAttributes : public QObject { @@ -31,6 +32,7 @@ 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); @@ -38,10 +40,10 @@ public: void copyCustomKeysFrom(const EntryAttributes* other); bool areCustomKeysDifferent(const EntryAttributes* other); void clear(); + int attributesSize(); 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);