From 90935c033d8785bba2583c19ce9694eca0dbcafd Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 18 Jul 2021 18:36:04 +0200 Subject: [PATCH] Fix template format --- .../element/template/TemplateEngine.kt | 37 +++++++------------ .../template/TemplateEngineCompatible.kt | 15 ++++++-- .../keepass/view/TemplateAbstractView.kt | 7 ++-- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngine.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngine.kt index bfbeb2630..951a44eca 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngine.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngine.kt @@ -3,15 +3,14 @@ package com.kunzisoft.keepass.database.element.template import android.content.res.Resources import android.util.Log import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.database.element.Field import com.kunzisoft.keepass.database.element.database.DatabaseKDBX 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.database.element.security.ProtectedString -import com.kunzisoft.keepass.database.element.Field import com.kunzisoft.keepass.database.element.icon.IconImage +import com.kunzisoft.keepass.database.element.icon.IconImageStandard import com.kunzisoft.keepass.database.element.node.NodeIdUUID -import com.kunzisoft.keepass.database.element.template.TemplateEngine.Companion.addDecoratorToTemplateEntryField +import com.kunzisoft.keepass.database.element.security.ProtectedString import java.util.* import kotlin.collections.HashMap @@ -83,6 +82,8 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) { abstract fun addMetaTemplateRecognitionToEntry(template: Template, entry: EntryKDBX): EntryKDBX + abstract fun buildTemplateEntryField(attribute: TemplateAttribute): Field + abstract fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX abstract fun encodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX @@ -100,10 +101,9 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) { "$SECTION_DECODED_TEMPLATE_PREFIX${index-1}" else section.name - putField(Field(sectionName, - ProtectedString(false, TemplateAttributeType.DIVIDER.typeString)).apply { - addDecoratorToTemplateEntryField() - }) + putField(Field(addTemplateDecorator(sectionName), + ProtectedString(false, TemplateAttributeType.DIVIDER.typeString)) + ) } putField(buildTemplateEntryField(attribute)) @@ -113,22 +113,11 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) { return encodeTemplateEntry(newEntry) } - protected fun buildTemplateEntryField(attribute: TemplateAttribute): Field { - val typeAndOptions = attribute.type.typeString + - TemplateAttributeOption.getStringFromOptions(attribute.options) - // PREFIX_DECODED_TEMPLATE to fix same label as standard fields - return Field(TemplateEngineCompatible.decodeTemplateAttribute(attribute.label), - ProtectedString(attribute.protected, typeAndOptions)).apply { - addDecoratorToTemplateEntryField() - } - } - private fun buildTemplateSectionFromFields(fields: List): TemplateSection { val sectionAttributes = mutableListOf() fields.forEach { field -> - field.removeDecoratorFromTemplateEntryField() sectionAttributes.add(TemplateAttribute( - field.name, + removeTemplateDecorator(field.name), TemplateAttributeType.getFromString(field.protectedValue.stringValue), field.protectedValue.isProtected, TemplateAttributeOption.getOptionsFromString(field.protectedValue.stringValue)) @@ -197,12 +186,12 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) { && name.endsWith(SUFFIX_DECODED_TEMPLATE) } - fun Field.addDecoratorToTemplateEntryField() { - this.name = "$PREFIX_DECODED_TEMPLATE${this.name}$SUFFIX_DECODED_TEMPLATE" + fun addTemplateDecorator(name: String): String { + return "$PREFIX_DECODED_TEMPLATE${name}$SUFFIX_DECODED_TEMPLATE" } - fun Field.removeDecoratorFromTemplateEntryField() { - this.name = this.name + fun removeTemplateDecorator(name: String): String { + return name .removePrefix(PREFIX_DECODED_TEMPLATE) .removeSuffix(SUFFIX_DECODED_TEMPLATE) } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt index 0e32d14fc..06e04fae5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt @@ -66,6 +66,14 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) } } + override fun buildTemplateEntryField(attribute: TemplateAttribute): Field { + val typeAndOptions = attribute.type.typeString + + TemplateAttributeOption.getStringFromOptions(attribute.options) + // PREFIX_DECODED_TEMPLATE to fix same label as standard fields + return Field(addTemplateDecorator(decodeTemplateAttribute(attribute.label)), + ProtectedString(attribute.protected, typeAndOptions)) + } + override fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX { val attributes = HashMap() val entryCopy = EntryKDBX().apply { @@ -262,8 +270,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) // Dynamic attributes var index = 0 templateEntry.doForEachDecodedCustomField { field -> - field.removeDecoratorFromTemplateEntryField() - val label = encodeTemplateAttribute(field.name) + val label = removeTemplateDecorator(encodeTemplateAttribute(field.name)) val value = field.protectedValue when { label.equals(TEMPLATE_LABEL_VERSION, true) -> { @@ -389,7 +396,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) private const val TEMPLATE_ATTRIBUTE_TYPE_POPOUT = "Popout" private const val TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX = "Rich Textbox" - fun decodeTemplateAttribute(name: String): String { + private fun decodeTemplateAttribute(name: String): String { return when { TEMPLATE_LABEL_VERSION.equals(name, true) -> TemplateField.LABEL_VERSION TEMPLATE_ATTRIBUTE_TITLE.equals(name, true) -> TemplateField.LABEL_TITLE @@ -403,7 +410,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) } } - fun encodeTemplateAttribute(name: String): String { + private fun encodeTemplateAttribute(name: String): String { return when { TemplateField.LABEL_VERSION.equals(name, true) -> TEMPLATE_LABEL_VERSION TemplateField.LABEL_TITLE.equals(name, true) -> TEMPLATE_ATTRIBUTE_TITLE diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt index 5c0da43b0..9b4a0cebe 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt @@ -21,7 +21,7 @@ import com.kunzisoft.keepass.database.element.Field import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.database.element.template.* -import com.kunzisoft.keepass.database.element.template.TemplateEngine.Companion.addDecoratorToTemplateEntryField +import com.kunzisoft.keepass.database.element.template.TemplateEngine.Companion.addTemplateDecorator import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.otp.OtpElement import com.kunzisoft.keepass.otp.OtpEntryFields @@ -473,8 +473,9 @@ abstract class TemplateAbstractView< */ private fun putCustomField(customField: Field, focus: Boolean): Boolean { if (mTemplate == TemplateEngine.CREATION - && !TemplateEngine.containsTemplateDecorator(customField.name)) - customField.addDecoratorToTemplateEntryField() + && !TemplateEngine.containsTemplateDecorator(customField.name)) { + customField.name = addTemplateDecorator(customField.name) + } return if (!isStandardFieldName(customField.name)) { customFieldsContainerView.visibility = View.VISIBLE