From 5ad3f62de5aa24064fde80d6419ad8b6cc0b7171 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 19 Sep 2019 14:20:28 +0200 Subject: [PATCH] Add history after each entry update for DatabaseV4 --- .../action/node/UpdateEntryRunnable.kt | 14 +++++--- .../keepass/database/element/Database.kt | 17 ++++------ .../database/element/EntryVersioned.kt | 34 ++++++++++--------- .../keepass/database/element/PwEntryV4.kt | 9 +++-- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt index 4c5be2905..94d67f2f8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt @@ -33,11 +33,17 @@ class UpdateEntryRunnable constructor( : ActionNodeDatabaseRunnable(context, database, finishRunnable, save) { // Keep backup of original values in case save fails - private var mBackupEntry: EntryVersioned? = null + private var mBackupEntryHistory: EntryVersioned? = null override fun nodeAction() { - mBackupEntry = database.addHistoryBackupTo(mOldEntry) - mOldEntry.touch(modified = true, touchParents = true) + mNewEntry.touch(modified = true, touchParents = true) + + mBackupEntryHistory = EntryVersioned(mOldEntry) + + // Create an entry history (an entry history don't have history) + mNewEntry.addEntryToHistory(EntryVersioned(mOldEntry, copyHistory = false)) + + database.removeOldestHistory(mNewEntry) // Update entry with new values mOldEntry.updateWith(mNewEntry) } @@ -45,7 +51,7 @@ class UpdateEntryRunnable constructor( override fun nodeFinish(result: Result): ActionNodeValues { if (!result.isSuccess) { // If we fail to save, back out changes to global structure - mBackupEntry?.let { + mBackupEntryHistory?.let { mOldEntry.updateWith(it) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt index e4061579f..701425b3c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt @@ -702,12 +702,9 @@ class Database { } } - fun addHistoryBackupTo(entry: EntryVersioned): EntryVersioned { - val backupEntry = EntryVersioned(entry) + fun removeOldestHistory(entry: EntryVersioned) { - entry.addBackupToHistory() - - // Remove oldest backup if more than max items or max memory + // Remove oldest history if more than max items or max memory pwDatabaseV4?.let { val history = entry.getHistory() @@ -721,12 +718,12 @@ class Database { val maxSize = it.historyMaxSize if (maxSize >= 0) { while (true) { - var histSize: Long = 0 - for (backup in history) { - histSize += backup.getSize() + var historySize: Long = 0 + for (entryHistory in history) { + historySize += entryHistory.getSize() } - if (histSize > maxSize) { + if (historySize > maxSize) { entry.removeOldestEntryFromHistory() } else { break @@ -734,8 +731,6 @@ class Database { } } } - - return backupEntry } companion object : SingletonHolder(::Database) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt index 38d74cf85..1c5aafaa6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt @@ -15,26 +15,26 @@ class EntryVersioned : NodeVersioned, PwEntryInterface { var pwEntryV4: PwEntryV4? = null private set - fun updateWith(entry: EntryVersioned) { + fun updateWith(entry: EntryVersioned, copyHistory: Boolean = true) { entry.pwEntryV3?.let { this.pwEntryV3?.updateWith(it) } entry.pwEntryV4?.let { - this.pwEntryV4?.updateWith(it) + this.pwEntryV4?.updateWith(it, copyHistory) } } /** * Use this constructor to copy an Entry with exact same values */ - constructor(entry: EntryVersioned) { + constructor(entry: EntryVersioned, copyHistory: Boolean = true) { if (entry.pwEntryV3 != null) { this.pwEntryV3 = PwEntryV3() } if (entry.pwEntryV4 != null) { this.pwEntryV4 = PwEntryV4() } - updateWith(entry) + updateWith(entry, copyHistory) } constructor(entry: PwEntryV3) { @@ -258,18 +258,6 @@ class EntryVersioned : NodeVersioned, PwEntryInterface { pwEntryV4?.stopToManageFieldReferences() } - fun addBackupToHistory() { - pwEntryV4?.let { - val entryHistory = PwEntryV4() - entryHistory.updateWith(it) - it.addEntryToHistory(entryHistory) - } - } - - fun removeOldestEntryFromHistory() { - pwEntryV4?.removeOldestEntryFromHistory() - } - fun getHistory(): ArrayList { val history = ArrayList() val entryV4History = pwEntryV4?.history ?: ArrayList() @@ -279,6 +267,20 @@ class EntryVersioned : NodeVersioned, PwEntryInterface { return history } + fun addEntryToHistory(entry: EntryVersioned) { + entry.pwEntryV4?.let { + pwEntryV4?.addEntryToHistory(it) + } + } + + fun removeAllHistory() { + pwEntryV4?.removeAllHistory() + } + + fun removeOldestEntryFromHistory() { + pwEntryV4?.removeOldestEntryFromHistory() + } + fun getSize(): Long { return pwEntryV4?.size ?: 0L } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.kt index 3e279932e..c300dadd5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.kt @@ -129,7 +129,7 @@ class PwEntryV4 : PwEntry, PwNodeV4Interface { * Update with deep copy of each entry element * @param source */ - fun updateWith(source: PwEntryV4) { + fun updateWith(source: PwEntryV4, copyHistory: Boolean = true) { super.updateWith(source) iconCustom = PwIconCustom(source.iconCustom) usageCount = source.usageCount @@ -146,7 +146,8 @@ class PwEntryV4 : PwEntry, PwNodeV4Interface { overrideURL = source.overrideURL autoType = AutoType(source.autoType) history.clear() - history.addAll(source.history) + if (copyHistory) + history.addAll(source.history) url = source.url additional = source.additional tags = source.tags @@ -287,6 +288,10 @@ class PwEntryV4 : PwEntry, PwNodeV4Interface { history.add(entry) } + fun removeAllHistory() { + history.clear() + } + fun removeOldestEntryFromHistory() { var min: Date? = null var index = -1