Fix template format

This commit is contained in:
J-Jamet
2021-07-18 18:36:04 +02:00
parent b4c3f831a7
commit 90935c033d
3 changed files with 28 additions and 31 deletions

View File

@@ -3,15 +3,14 @@ package com.kunzisoft.keepass.database.element.template
import android.content.res.Resources import android.content.res.Resources
import android.util.Log import android.util.Log
import com.kunzisoft.keepass.R 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.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.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.IconImage
import com.kunzisoft.keepass.database.element.icon.IconImageStandard
import com.kunzisoft.keepass.database.element.node.NodeIdUUID 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 java.util.*
import kotlin.collections.HashMap 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 addMetaTemplateRecognitionToEntry(template: Template, entry: EntryKDBX): EntryKDBX
abstract fun buildTemplateEntryField(attribute: TemplateAttribute): Field
abstract fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX abstract fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX
abstract fun encodeTemplateEntry(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}" "$SECTION_DECODED_TEMPLATE_PREFIX${index-1}"
else else
section.name section.name
putField(Field(sectionName, putField(Field(addTemplateDecorator(sectionName),
ProtectedString(false, TemplateAttributeType.DIVIDER.typeString)).apply { ProtectedString(false, TemplateAttributeType.DIVIDER.typeString))
addDecoratorToTemplateEntryField() )
})
} }
putField(buildTemplateEntryField(attribute)) putField(buildTemplateEntryField(attribute))
@@ -113,22 +113,11 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
return encodeTemplateEntry(newEntry) 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<Field>): TemplateSection { private fun buildTemplateSectionFromFields(fields: List<Field>): TemplateSection {
val sectionAttributes = mutableListOf<TemplateAttribute>() val sectionAttributes = mutableListOf<TemplateAttribute>()
fields.forEach { field -> fields.forEach { field ->
field.removeDecoratorFromTemplateEntryField()
sectionAttributes.add(TemplateAttribute( sectionAttributes.add(TemplateAttribute(
field.name, removeTemplateDecorator(field.name),
TemplateAttributeType.getFromString(field.protectedValue.stringValue), TemplateAttributeType.getFromString(field.protectedValue.stringValue),
field.protectedValue.isProtected, field.protectedValue.isProtected,
TemplateAttributeOption.getOptionsFromString(field.protectedValue.stringValue)) TemplateAttributeOption.getOptionsFromString(field.protectedValue.stringValue))
@@ -197,12 +186,12 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
&& name.endsWith(SUFFIX_DECODED_TEMPLATE) && name.endsWith(SUFFIX_DECODED_TEMPLATE)
} }
fun Field.addDecoratorToTemplateEntryField() { fun addTemplateDecorator(name: String): String {
this.name = "$PREFIX_DECODED_TEMPLATE${this.name}$SUFFIX_DECODED_TEMPLATE" return "$PREFIX_DECODED_TEMPLATE${name}$SUFFIX_DECODED_TEMPLATE"
} }
fun Field.removeDecoratorFromTemplateEntryField() { fun removeTemplateDecorator(name: String): String {
this.name = this.name return name
.removePrefix(PREFIX_DECODED_TEMPLATE) .removePrefix(PREFIX_DECODED_TEMPLATE)
.removeSuffix(SUFFIX_DECODED_TEMPLATE) .removeSuffix(SUFFIX_DECODED_TEMPLATE)
} }

View File

@@ -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 { override fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX {
val attributes = HashMap<String, TemplateAttributePosition>() val attributes = HashMap<String, TemplateAttributePosition>()
val entryCopy = EntryKDBX().apply { val entryCopy = EntryKDBX().apply {
@@ -262,8 +270,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database)
// Dynamic attributes // Dynamic attributes
var index = 0 var index = 0
templateEntry.doForEachDecodedCustomField { field -> templateEntry.doForEachDecodedCustomField { field ->
field.removeDecoratorFromTemplateEntryField() val label = removeTemplateDecorator(encodeTemplateAttribute(field.name))
val label = encodeTemplateAttribute(field.name)
val value = field.protectedValue val value = field.protectedValue
when { when {
label.equals(TEMPLATE_LABEL_VERSION, true) -> { 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_POPOUT = "Popout"
private const val TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX = "Rich Textbox" private const val TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX = "Rich Textbox"
fun decodeTemplateAttribute(name: String): String { private fun decodeTemplateAttribute(name: String): String {
return when { return when {
TEMPLATE_LABEL_VERSION.equals(name, true) -> TemplateField.LABEL_VERSION TEMPLATE_LABEL_VERSION.equals(name, true) -> TemplateField.LABEL_VERSION
TEMPLATE_ATTRIBUTE_TITLE.equals(name, true) -> TemplateField.LABEL_TITLE 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 { return when {
TemplateField.LABEL_VERSION.equals(name, true) -> TEMPLATE_LABEL_VERSION TemplateField.LABEL_VERSION.equals(name, true) -> TEMPLATE_LABEL_VERSION
TemplateField.LABEL_TITLE.equals(name, true) -> TEMPLATE_ATTRIBUTE_TITLE TemplateField.LABEL_TITLE.equals(name, true) -> TEMPLATE_ATTRIBUTE_TITLE

View File

@@ -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.icon.IconImage
import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.database.element.security.ProtectedString
import com.kunzisoft.keepass.database.element.template.* 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.model.EntryInfo
import com.kunzisoft.keepass.otp.OtpElement import com.kunzisoft.keepass.otp.OtpElement
import com.kunzisoft.keepass.otp.OtpEntryFields import com.kunzisoft.keepass.otp.OtpEntryFields
@@ -473,8 +473,9 @@ abstract class TemplateAbstractView<
*/ */
private fun putCustomField(customField: Field, focus: Boolean): Boolean { private fun putCustomField(customField: Field, focus: Boolean): Boolean {
if (mTemplate == TemplateEngine.CREATION if (mTemplate == TemplateEngine.CREATION
&& !TemplateEngine.containsTemplateDecorator(customField.name)) && !TemplateEngine.containsTemplateDecorator(customField.name)) {
customField.addDecoratorToTemplateEntryField() customField.name = addTemplateDecorator(customField.name)
}
return if (!isStandardFieldName(customField.name)) { return if (!isStandardFieldName(customField.name)) {
customFieldsContainerView.visibility = View.VISIBLE customFieldsContainerView.visibility = View.VISIBLE