Create new templates group in settings

This commit is contained in:
J-Jamet
2021-05-17 13:12:23 +02:00
parent 66f5ff35d3
commit ce9931d8a3
5 changed files with 35 additions and 23 deletions

View File

@@ -353,12 +353,18 @@ class Database {
val allowConfigurableRecycleBin: Boolean val allowConfigurableRecycleBin: Boolean
get() = mDatabaseKDBX != null get() = mDatabaseKDBX != null
var isRecycleBinEnabled: Boolean val isRecycleBinEnabled: Boolean
// Backup is always enabled in KDB database // Backup is always enabled in KDB database
get() = mDatabaseKDB != null || mDatabaseKDBX?.isRecycleBinEnabled ?: false get() = mDatabaseKDB != null || mDatabaseKDBX?.isRecycleBinEnabled ?: false
set(value) {
mDatabaseKDBX?.isRecycleBinEnabled = value fun enableRecycleBin(enable: Boolean, resources: Resources) {
mDatabaseKDBX?.isRecycleBinEnabled = enable
if (enable) {
ensureRecycleBinExists(resources)
} else {
removeRecycleBin()
} }
}
val recycleBin: Group? val recycleBin: Group?
get() { get() {
@@ -379,11 +385,12 @@ class Database {
get() = mDatabaseKDBX != null get() = mDatabaseKDBX != null
// Maybe another templates method with KDBX5 // Maybe another templates method with KDBX5
var isTemplatesEnabled: Boolean val isTemplatesEnabled: Boolean
get() = mDatabaseKDBX?.isTemplatesGroupEnabled() ?: false get() = mDatabaseKDBX?.isTemplatesGroupEnabled() ?: false
set(value) {
mDatabaseKDBX?.enableTemplatesGroup(value) fun enableTemplates(enable: Boolean) {
} mDatabaseKDBX?.enableTemplatesGroup(enable)
}
val templatesGroup: Group? val templatesGroup: Group?
get() { get() {

View File

@@ -341,8 +341,11 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
fun enableTemplatesGroup(enable: Boolean) { fun enableTemplatesGroup(enable: Boolean) {
if (enable) { if (enable) {
// TODO Build default templates group val uuidTemplatesGroup = mTemplateEngine.createNewTemplatesGroup()
setTemplatesGroup(UUID_ZERO)
addGroupTo(uuidTemplatesGroup, rootGroup)
entryTemplatesGroup = uuidTemplatesGroup.id
entryTemplatesGroupChanged = uuidTemplatesGroup.lastModificationTime
} else { } else {
entryTemplatesGroup = UUID_ZERO entryTemplatesGroup = UUID_ZERO
mTemplateEngine.clearCache() mTemplateEngine.clearCache()
@@ -356,11 +359,6 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
return null return null
} }
fun setTemplatesGroup(uuid: UUID) {
entryTemplatesGroup = uuid
entryTemplatesGroupChanged = DateInstant()
}
fun getTemplates(): List<Template> { fun getTemplates(): List<Template> {
return mTemplateEngine.getTemplates() return mTemplateEngine.getTemplates()
} }

View File

@@ -3,6 +3,8 @@ package com.kunzisoft.keepass.database.element.template
import android.util.Log import android.util.Log
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.entry.EntryKDBX
import com.kunzisoft.keepass.database.element.group.GroupKDBX
import com.kunzisoft.keepass.database.element.icon.IconImageStandard
import com.kunzisoft.keepass.utils.UuidUtil import com.kunzisoft.keepass.utils.UuidUtil
import java.util.* import java.util.*
import kotlin.collections.HashMap import kotlin.collections.HashMap
@@ -31,6 +33,16 @@ class TemplateEngine(private val databaseKDBX: DatabaseKDBX) {
return templates return templates
} }
fun createNewTemplatesGroup(): GroupKDBX {
return databaseKDBX.createGroup().apply {
title = TEMPLATE_GROUP_NAME
icon.standard = databaseKDBX.getStandardIcon(IconImageStandard.FOLDER_ID)
enableAutoType = false
enableSearching = false
isExpanded = false
}
}
fun clearCache() { fun clearCache() {
mCacheTemplates.clear() mCacheTemplates.clear()
} }
@@ -159,6 +171,7 @@ class TemplateEngine(private val databaseKDBX: DatabaseKDBX) {
private data class TemplateAttributePosition(var position: Int, var attribute: TemplateAttribute) private data class TemplateAttributePosition(var position: Int, var attribute: TemplateAttribute)
private val TAG = TemplateEngine::class.java.name private val TAG = TemplateEngine::class.java.name
private const val TEMPLATE_GROUP_NAME = "Templates"
private const val TEMPLATE_LABEL_VERSION = "_etm_template" private const val TEMPLATE_LABEL_VERSION = "_etm_template"
private const val TEMPLATE_ENTRY_UUID = "_etm_template_uuid" private const val TEMPLATE_ENTRY_UUID = "_etm_template_uuid"
private const val TEMPLATE_ATTRIBUTE_POSITION_PREFIX = "_etm_position" private const val TEMPLATE_ATTRIBUTE_POSITION_PREFIX = "_etm_position"

View File

@@ -159,13 +159,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
isEnabled = if (!mDatabaseReadOnly) { isEnabled = if (!mDatabaseReadOnly) {
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val recycleBinEnabled = newValue as Boolean val recycleBinEnabled = newValue as Boolean
mDatabase.isRecycleBinEnabled = recycleBinEnabled mDatabase.enableRecycleBin(recycleBinEnabled, resources)
// TODO Change method
if (recycleBinEnabled) {
mDatabase.ensureRecycleBinExists(resources)
} else {
mDatabase.removeRecycleBin()
}
refreshRecycleBinGroup() refreshRecycleBinGroup()
// Save the database if not in readonly mode // Save the database if not in readonly mode
(context as SettingsActivity?)?. (context as SettingsActivity?)?.
@@ -193,7 +187,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
isEnabled = if (!mDatabaseReadOnly) { isEnabled = if (!mDatabaseReadOnly) {
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val templatesEnabled = newValue as Boolean val templatesEnabled = newValue as Boolean
mDatabase.isTemplatesEnabled = templatesEnabled mDatabase.enableTemplates(templatesEnabled)
refreshTemplatesGroup() refreshTemplatesGroup()
// Save the database if not in readonly mode // Save the database if not in readonly mode
(context as SettingsActivity?)?. (context as SettingsActivity?)?.

View File

@@ -411,7 +411,7 @@
<string name="compression_none">None</string> <string name="compression_none">None</string>
<string name="compression_gzip">Gzip</string> <string name="compression_gzip">Gzip</string>
<string name="recycle_bin">Recycle bin</string> <string name="recycle_bin">Recycle bin</string>
<string name="templates">Recycle bin</string> <string name="templates">Templates</string>
<string name="keyboard">Keyboard</string> <string name="keyboard">Keyboard</string>
<string name="magic_keyboard_title">Magikeyboard</string> <string name="magic_keyboard_title">Magikeyboard</string>
<string name="magic_keyboard_explanation_summary">Activate a custom keyboard populating your passwords and all identity fields</string> <string name="magic_keyboard_explanation_summary">Activate a custom keyboard populating your passwords and all identity fields</string>