mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-12-29 01:06:27 -05:00
Refactor Entry::truncateHistory().
This allows us to drop Entry::getSize() and EntryAttachments::attachmentsSize() which have weird semantics.
This commit is contained in:
parent
0c1fecfb2b
commit
ef579cbe3c
@ -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()
|
QList<Entry*> Entry::historyItems()
|
||||||
{
|
{
|
||||||
return m_history;
|
return m_history;
|
||||||
@ -467,21 +462,26 @@ void Entry::truncateHistory()
|
|||||||
int histMaxSize = db->metadata()->historyMaxSize();
|
int histMaxSize = db->metadata()->historyMaxSize();
|
||||||
if (histMaxSize > -1) {
|
if (histMaxSize > -1) {
|
||||||
int size = 0;
|
int size = 0;
|
||||||
QList<QByteArray> foundAttachements;
|
QSet<QByteArray> foundAttachements = attachments()->values().toSet();
|
||||||
attachments()->attachmentsSize(&foundAttachements);
|
|
||||||
|
|
||||||
QMutableListIterator<Entry*> i(m_history);
|
QMutableListIterator<Entry*> i(m_history);
|
||||||
i.toBack();
|
i.toBack();
|
||||||
while (i.hasPrevious()) {
|
while (i.hasPrevious()) {
|
||||||
Entry* entry = i.previous();
|
Entry* historyItem = i.previous();
|
||||||
|
|
||||||
// don't calculate size if it's already above the maximum
|
// don't calculate size if it's already above the maximum
|
||||||
if (size <= histMaxSize) {
|
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) {
|
if (size > histMaxSize) {
|
||||||
delete entry;
|
delete historyItem;
|
||||||
i.remove();
|
i.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,6 @@ public:
|
|||||||
void setNotes(const QString& notes);
|
void setNotes(const QString& notes);
|
||||||
void setExpires(const bool& value);
|
void setExpires(const bool& value);
|
||||||
void setExpiryTime(const QDateTime& dateTime);
|
void setExpiryTime(const QDateTime& dateTime);
|
||||||
int getSize(QList<QByteArray>* foundAttachements);
|
|
||||||
|
|
||||||
QList<Entry*> historyItems();
|
QList<Entry*> historyItems();
|
||||||
const QList<Entry*>& historyItems() const;
|
const QList<Entry*>& historyItems() const;
|
||||||
|
@ -27,6 +27,11 @@ QList<QString> EntryAttachments::keys() const
|
|||||||
return m_attachments.keys();
|
return m_attachments.keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<QByteArray> EntryAttachments::values() const
|
||||||
|
{
|
||||||
|
return m_attachments.values();
|
||||||
|
}
|
||||||
|
|
||||||
QByteArray EntryAttachments::value(const QString& key) const
|
QByteArray EntryAttachments::value(const QString& key) const
|
||||||
{
|
{
|
||||||
return m_attachments.value(key);
|
return m_attachments.value(key);
|
||||||
@ -87,21 +92,6 @@ void EntryAttachments::clear()
|
|||||||
Q_EMIT modified();
|
Q_EMIT modified();
|
||||||
}
|
}
|
||||||
|
|
||||||
int EntryAttachments::attachmentsSize(QList<QByteArray>* foundAttachements)
|
|
||||||
{
|
|
||||||
int size = 0;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool EntryAttachments::operator==(const EntryAttachments& other) const
|
bool EntryAttachments::operator==(const EntryAttachments& other) const
|
||||||
{
|
{
|
||||||
return m_attachments == other.m_attachments;
|
return m_attachments == other.m_attachments;
|
||||||
|
@ -30,11 +30,11 @@ class EntryAttachments : public QObject
|
|||||||
public:
|
public:
|
||||||
explicit EntryAttachments(QObject* parent = Q_NULLPTR);
|
explicit EntryAttachments(QObject* parent = Q_NULLPTR);
|
||||||
QList<QString> keys() const;
|
QList<QString> keys() const;
|
||||||
|
QList<QByteArray> values() const;
|
||||||
QByteArray value(const QString& key) const;
|
QByteArray value(const QString& key) const;
|
||||||
void set(const QString& key, const QByteArray& value);
|
void set(const QString& key, const QByteArray& value);
|
||||||
void remove(const QString& key);
|
void remove(const QString& key);
|
||||||
void clear();
|
void clear();
|
||||||
int attachmentsSize(QList<QByteArray>* foundAttachements);
|
|
||||||
bool operator==(const EntryAttachments& other) const;
|
bool operator==(const EntryAttachments& other) const;
|
||||||
bool operator!=(const EntryAttachments& other) const;
|
bool operator!=(const EntryAttachments& other) const;
|
||||||
EntryAttachments& operator=(const EntryAttachments& other);
|
EntryAttachments& operator=(const EntryAttachments& other);
|
||||||
|
Loading…
Reference in New Issue
Block a user