Added history limits enforcement

This commit is contained in:
jacek81 2012-05-04 22:45:34 +01:00 committed by Florian Geyer
parent 860a2131b3
commit 8c87a87da6
6 changed files with 63 additions and 1 deletions

View File

@ -337,6 +337,19 @@ void Entry::setExpiryTime(const QDateTime& dateTime)
}
}
int Entry::getHistSize() {
int size = 0;
for(int i=0 ; i<m_history.size() ; i++) {
size += m_history[i]->getSize();
}
return size;
}
int Entry::getSize() {
return attributes()->attributesSize() + attachments()->attachmentsSize();
}
QList<Entry*> 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);

View File

@ -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<Entry*> historyItems();
const QList<Entry*>& 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.

View File

@ -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;

View File

@ -29,9 +29,11 @@ public:
explicit EntryAttachments(QObject* parent = 0);
QList<QString> 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);

View File

@ -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);

View File

@ -22,6 +22,7 @@
#include <QtCore/QSet>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QChar>
class EntryAttributes : public QObject
{
@ -31,6 +32,7 @@ public:
explicit EntryAttributes(QObject* parent = 0);
QList<QString> 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);