Improve history limits.

Closes #16
This commit is contained in:
Florian Geyer 2012-05-10 09:56:41 +02:00
parent 8c87a87da6
commit 1a74feb253
6 changed files with 54 additions and 38 deletions

View File

@ -337,17 +337,8 @@ 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();
int Entry::getSize(QList<QByteArray>* foundAttachements) {
return attributes()->attributesSize() + attachments()->attachmentsSize(foundAttachements);
}
QList<Entry*> 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<Entry*> 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<QByteArray>* foundAttachements = new QList<QByteArray>();
attachments()->attachmentsSize(foundAttachements);
QMutableListIterator<Entry*> 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;

View File

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

View File

@ -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<QByteArray>* foundAttachements) {
int size = 0;
Q_FOREACH (const QString& key, keys()) {
size += dataSize(key);
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;
}

View File

@ -29,11 +29,10 @@ 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();
int attachmentsSize(QList<QByteArray> *foundAttachements);
bool operator==(const EntryAttachments& other) const;
bool operator!=(const EntryAttachments& other) const;
EntryAttachments& operator=(EntryAttachments& other);

View File

@ -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<QString, QString> i(m_attributes);
while (i.hasNext()) {
i.next();
size += i.value().toUtf8().size();
}
return size;
}

View File

@ -22,7 +22,6 @@
#include <QtCore/QSet>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QChar>
class EntryAttributes : public QObject
{
@ -32,7 +31,6 @@ 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);
@ -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);