From b89d2a6da126ca2bd8cccd719dd5bfab79d78aba Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 3 Jul 2021 15:52:30 +0200 Subject: [PATCH] Encapsulate local Template types --- .../database/element/database/DatabaseKDBX.kt | 3 +- .../database/element/template/Template.kt | 6 +- .../element/template/TemplateAttribute.kt | 2 +- .../element/template/TemplateAttributeType.kt | 30 +- .../element/template/TemplateBuilder.kt | 34 +- .../element/template/TemplateEngine.kt | 311 ++---------------- .../template/TemplateEngineCompatible.kt | 266 +++++++++++++++ .../keepass/view/TemplateAbstractView.kt | 5 +- 8 files changed, 344 insertions(+), 313 deletions(-) create mode 100644 app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt index 2e22f9392..377fbf99f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt @@ -47,6 +47,7 @@ import com.kunzisoft.keepass.database.element.node.NodeVersioned import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig import com.kunzisoft.keepass.database.element.template.Template import com.kunzisoft.keepass.database.element.template.TemplateEngine +import com.kunzisoft.keepass.database.element.template.TemplateEngineCompatible import com.kunzisoft.keepass.database.exception.UnknownKDF import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_31 import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_40 @@ -81,7 +82,7 @@ class DatabaseKDBX : DatabaseVersioned { private var numKeyEncRounds: Long = 0 var publicCustomData = VariantDictionary() private val mFieldReferenceEngine = FieldReferencesEngine(this) - private val mTemplateEngine = TemplateEngine(this) + private val mTemplateEngine = TemplateEngineCompatible(this) var kdbxVersion = UnsignedInt(0) var name = "" diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt index d3084c6bd..d7061a880 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt @@ -108,15 +108,15 @@ class Template : Parcelable { return arrayOfNulls(size) } - val TITLE_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_TITLE, TemplateAttributeType.INLINE) - val USERNAME_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_USERNAME, TemplateAttributeType.INLINE) + val TITLE_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_TITLE, TemplateAttributeType.SINGLE_LINE) + val USERNAME_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_USERNAME, TemplateAttributeType.SINGLE_LINE) val PASSWORD_ATTRIBUTE = TemplateAttribute( TemplateField.LABEL_PASSWORD, TemplateAttributeType.SMALL_MULTILINE, true, "", TemplateAttributeAction.PASSWORD_GENERATION) - val URL_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_URL, TemplateAttributeType.INLINE) + val URL_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_URL, TemplateAttributeType.SINGLE_LINE) val EXPIRATION_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_EXPIRATION, TemplateAttributeType.DATETIME) val NOTES_ATTRIBUTE = TemplateAttribute(TemplateField.LABEL_NOTES, TemplateAttributeType.MULTILINE) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt index b9f555679..700a7aa48 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt @@ -32,7 +32,7 @@ data class TemplateAttribute(var label: String, constructor(parcel: Parcel) : this( parcel.readString() ?: "", - parcel.readEnum() ?: TemplateAttributeType.INLINE, + parcel.readEnum() ?: TemplateAttributeType.SINGLE_LINE, parcel.readByte() != 0.toByte(), parcel.readString() ?: "", parcel.readEnum() ?: TemplateAttributeAction.NONE, diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeType.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeType.kt index c1c685ddb..caabc4444 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeType.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeType.kt @@ -18,11 +18,27 @@ */ package com.kunzisoft.keepass.database.element.template -enum class TemplateAttributeType { - INLINE, - SMALL_MULTILINE, - MULTILINE, - DATE, - TIME, - DATETIME +enum class TemplateAttributeType(val label: String) { + SINGLE_LINE("Single Line"), + SMALL_MULTILINE("Small Multiline"), + MULTILINE("Multiline"), + DATE("Date"), + TIME("Time"), + DATETIME("DateTime"), + DIVIDER("Divider"); + + companion object { + fun getFromLabel(label: String): TemplateAttributeType { + return when { + label.equals(SINGLE_LINE.label, true) -> SINGLE_LINE + label.equals(SMALL_MULTILINE.label, true) -> SMALL_MULTILINE + label.equals(MULTILINE.label, true) -> MULTILINE + label.equals(DATE.label, true) -> DATE + label.equals(TIME.label, true) -> TIME + label.equals(DATETIME.label, true) -> DATETIME + label.equals(DIVIDER.label, true) -> DIVIDER + else -> SINGLE_LINE + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt index 9c7af76c6..4147a4be3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt @@ -6,26 +6,26 @@ import java.util.* class TemplateBuilder { - private val urlAttribute = TemplateAttribute(TemplateField.LABEL_URL, TemplateAttributeType.INLINE) - private val usernameAttribute = TemplateAttribute(TemplateField.LABEL_USERNAME, TemplateAttributeType.INLINE) + private val urlAttribute = TemplateAttribute(TemplateField.LABEL_URL, TemplateAttributeType.SINGLE_LINE) + private val usernameAttribute = TemplateAttribute(TemplateField.LABEL_USERNAME, TemplateAttributeType.SINGLE_LINE) private val notesPlainAttribute = TemplateAttribute(TemplateField.LABEL_NOTES, TemplateAttributeType.MULTILINE) - private val holderAttribute = TemplateAttribute(TemplateField.LABEL_HOLDER, TemplateAttributeType.INLINE) - private val numberAttribute = TemplateAttribute(TemplateField.LABEL_NUMBER, TemplateAttributeType.INLINE) - private val cvvAttribute = TemplateAttribute(TemplateField.LABEL_CVV, TemplateAttributeType.INLINE, true) - private val pinAttribute = TemplateAttribute(TemplateField.LABEL_PIN, TemplateAttributeType.INLINE, true) - private val nameAttribute = TemplateAttribute(TemplateField.LABEL_NAME, TemplateAttributeType.INLINE) - private val placeOfIssueAttribute = TemplateAttribute(TemplateField.LABEL_PLACE_OF_ISSUE, TemplateAttributeType.INLINE) + private val holderAttribute = TemplateAttribute(TemplateField.LABEL_HOLDER, TemplateAttributeType.SINGLE_LINE) + private val numberAttribute = TemplateAttribute(TemplateField.LABEL_NUMBER, TemplateAttributeType.SINGLE_LINE) + private val cvvAttribute = TemplateAttribute(TemplateField.LABEL_CVV, TemplateAttributeType.SINGLE_LINE, true) + private val pinAttribute = TemplateAttribute(TemplateField.LABEL_PIN, TemplateAttributeType.SINGLE_LINE, true) + private val nameAttribute = TemplateAttribute(TemplateField.LABEL_NAME, TemplateAttributeType.SINGLE_LINE) + private val placeOfIssueAttribute = TemplateAttribute(TemplateField.LABEL_PLACE_OF_ISSUE, TemplateAttributeType.SINGLE_LINE) private val dateOfIssueAttribute = TemplateAttribute(TemplateField.LABEL_DATE_OF_ISSUE, TemplateAttributeType.DATE) private val expirationDateAttribute = TemplateAttribute(TemplateField.LABEL_EXPIRATION, TemplateAttributeType.DATE) - private val emailAddressAttribute = TemplateAttribute(TemplateField.LABEL_EMAIL_ADDRESS, TemplateAttributeType.INLINE) - private val passwordAttribute = TemplateAttribute(TemplateField.LABEL_PASSWORD, TemplateAttributeType.INLINE, true) - private val ssidAttribute = TemplateAttribute(TemplateField.LABEL_SSID, TemplateAttributeType.INLINE) - private val tokenAttribute = TemplateAttribute(TemplateField.LABEL_TOKEN, TemplateAttributeType.INLINE) - private val publicKeyAttribute = TemplateAttribute(TemplateField.LABEL_PUBLIC_KEY, TemplateAttributeType.INLINE) - private val privateKeyAttribute = TemplateAttribute(TemplateField.LABEL_PRIVATE_KEY, TemplateAttributeType.INLINE, true) - private val seedAttribute = TemplateAttribute(TemplateField.LABEL_SEED, TemplateAttributeType.INLINE, true) - private val bicAttribute = TemplateAttribute(TemplateField.LABEL_BIC, TemplateAttributeType.INLINE) - private val ibanAttribute = TemplateAttribute(TemplateField.LABEL_IBAN, TemplateAttributeType.INLINE) + private val emailAddressAttribute = TemplateAttribute(TemplateField.LABEL_EMAIL_ADDRESS, TemplateAttributeType.SINGLE_LINE) + private val passwordAttribute = TemplateAttribute(TemplateField.LABEL_PASSWORD, TemplateAttributeType.SINGLE_LINE, true) + private val ssidAttribute = TemplateAttribute(TemplateField.LABEL_SSID, TemplateAttributeType.SINGLE_LINE) + private val tokenAttribute = TemplateAttribute(TemplateField.LABEL_TOKEN, TemplateAttributeType.SINGLE_LINE) + private val publicKeyAttribute = TemplateAttribute(TemplateField.LABEL_PUBLIC_KEY, TemplateAttributeType.SINGLE_LINE) + private val privateKeyAttribute = TemplateAttribute(TemplateField.LABEL_PRIVATE_KEY, TemplateAttributeType.SINGLE_LINE, true) + private val seedAttribute = TemplateAttribute(TemplateField.LABEL_SEED, TemplateAttributeType.SINGLE_LINE, true) + private val bicAttribute = TemplateAttribute(TemplateField.LABEL_BIC, TemplateAttributeType.SINGLE_LINE) + private val ibanAttribute = TemplateAttribute(TemplateField.LABEL_IBAN, TemplateAttributeType.SINGLE_LINE) val email: Template get() { 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 5b653f622..fd707c7f9 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 @@ -10,11 +10,10 @@ 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.node.NodeIdUUID -import com.kunzisoft.keepass.utils.UuidUtil import java.util.* import kotlin.collections.HashMap -class TemplateEngine(private val mDatabase: DatabaseKDBX) { +abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) { private val mCacheTemplates = HashMap() @@ -56,7 +55,7 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) { mCacheTemplates.clear() } - private fun getTemplateByCache(uuid: UUID): Template? { + protected fun getTemplateByCache(uuid: UUID): Template? { try { if (mCacheTemplates.containsKey(uuid)) return mCacheTemplates[uuid] @@ -74,185 +73,15 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) { return null } - fun getTemplate(entryKDBX: EntryKDBX): Template? { - UuidUtil.fromHexString(entryKDBX.getCustomFieldValue(TEMPLATE_ENTRY_UUID))?.let { templateUUID -> - return getTemplateByCache(templateUUID) - } - return null - } + abstract fun getTemplate(entryKDBX: EntryKDBX): Template? - private fun getOrRetrieveAttributeFromName(attributes: HashMap, name: String): TemplateAttributePosition { - return if (attributes.containsKey(name)) { - attributes[name]!! - } else { - val newAttribute = TemplateAttributePosition(-1, TemplateAttribute("", TemplateAttributeType.INLINE)) - attributes[name] = newAttribute - newAttribute - } - } + abstract fun removeMetaTemplateRecognitionFromEntry(entry: EntryKDBX): EntryKDBX - fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX { - val attributes = HashMap() - val entryCopy = EntryKDBX().apply { - updateWith(templateEntry) - } - // Remove template version - entryCopy.getFieldValue(TEMPLATE_LABEL_VERSION) - try { - // value.toIntOrNull() - // TODO template decoder version - } catch (e: Exception) { - Log.e(TAG, "Unable to retrieve template version", e) - } - entryCopy.removeField(TEMPLATE_LABEL_VERSION) - // Dynamic attributes - templateEntry.doForEachDecodedCustomField { field -> + abstract fun addMetaTemplateRecognitionToEntry(template: Template, entry: EntryKDBX): EntryKDBX - val label = field.name - val value = field.protectedValue.stringValue - when { - label.startsWith(TEMPLATE_ATTRIBUTE_POSITION_PREFIX) -> { - try { - val attributeName = label.substring(TEMPLATE_ATTRIBUTE_POSITION_PREFIX.length) - val attribute = getOrRetrieveAttributeFromName(attributes, attributeName) - attribute.position = value.toInt() - entryCopy.removeField(field.name) - } catch (e: Exception) { - Log.e(TAG, "Unable to retrieve template position", e) - } - } - label.startsWith(TEMPLATE_ATTRIBUTE_TITLE_PREFIX) -> { - try { - val attributeName = label.substring(TEMPLATE_ATTRIBUTE_TITLE_PREFIX.length) - val attribute = getOrRetrieveAttributeFromName(attributes, attributeName) - attribute.attribute.label = value - entryCopy.removeField(field.name) - } catch (e: Exception) { - Log.e(TAG, "Unable to retrieve template title", e) - } - } - label.startsWith(TEMPLATE_ATTRIBUTE_TYPE_PREFIX) -> { - try { - val attributeName = label.substring(TEMPLATE_ATTRIBUTE_TYPE_PREFIX.length) - val attribute = getOrRetrieveAttributeFromName(attributes, attributeName) - if (value.contains(TEMPLATE_ATTRIBUTE_TYPE_PROTECTED, true)) { - attribute.attribute.protected = true - } - when { - value.contains(TEMPLATE_ATTRIBUTE_TYPE_INLINE, true) || - value.contains(TEMPLATE_ATTRIBUTE_TYPE_POPOUT, true) -> { - attribute.attribute.type = TemplateAttributeType.INLINE - } - value.contains(TEMPLATE_ATTRIBUTE_TYPE_MULTILINE, true) || - value.contains(TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX, true) -> { - attribute.attribute.type = TemplateAttributeType.MULTILINE - } - value.contains(TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME, true) -> { - attribute.attribute.type = TemplateAttributeType.DATETIME - } - value.contains(TEMPLATE_ATTRIBUTE_TYPE_DATE, true) -> { - attribute.attribute.type = TemplateAttributeType.DATE - } - value.contains(TEMPLATE_ATTRIBUTE_TYPE_TIME, true) -> { - attribute.attribute.type = TemplateAttributeType.TIME - } - value.contains(TEMPLATE_ATTRIBUTE_TYPE_LISTBOX, true) -> { - // TODO List box - attribute.attribute.type = TemplateAttributeType.INLINE - } - } - attribute.attribute.defaultValue = field.protectedValue.stringValue - entryCopy.removeField(field.name) - } catch (e: Exception) { - Log.e(TAG, "Unable to retrieve template type", e) - } - } - } - } + abstract fun decodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX - val newFields = arrayOfNulls(attributes.size) - attributes.values.forEach { - // PREFIX_DECODED_TEMPLATE to fix same label as standard fields - newFields[it.position] = Field(PREFIX_DECODED_TEMPLATE + decodeTemplateAttribute(it.attribute.label) + SUFFIX_DECODED_TEMPLATE, - ProtectedString(false, it.attribute.defaultValue)) - } - newFields.forEach { field -> - field?.let { - entryCopy.putField(field) - } - } - - return entryCopy - } - - fun encodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX { - val entryCopy = EntryKDBX().apply { - updateWith(templateEntry) - } - // Add template version - entryCopy.putField(TEMPLATE_LABEL_VERSION, ProtectedString(false, "1")) - // Dynamic attributes - var index = 0 - templateEntry.doForEachDecodedCustomField { field -> - val label = encodeTemplateAttribute(field.name - .removePrefix(PREFIX_DECODED_TEMPLATE).removeSuffix(SUFFIX_DECODED_TEMPLATE)) - val value = field.protectedValue - when { - label.equals(TEMPLATE_LABEL_VERSION, true) -> { - // Keep template version as is - } - else -> { - entryCopy.removeField(field.name) - entryCopy.putField(TEMPLATE_ATTRIBUTE_POSITION_PREFIX+'_'+label, - ProtectedString(false, index.toString())) - entryCopy.putField(TEMPLATE_ATTRIBUTE_TITLE_PREFIX+'_'+label, - ProtectedString(false, label)) - // Add protected string if needed - var typeString = value.stringValue - if (value.isProtected - && !typeString.contains(TEMPLATE_ATTRIBUTE_TYPE_PROTECTED)) { - typeString = "$TEMPLATE_ATTRIBUTE_TYPE_PROTECTED $typeString" - } - entryCopy.putField(TEMPLATE_ATTRIBUTE_TYPE_PREFIX+'_'+label, - ProtectedString(false, typeString)) - index++ - } - } - } - return entryCopy - } - - private fun getTemplateUUIDField(template: Template): Field? { - UuidUtil.toHexString(template.uuid)?.let { uuidString -> - return Field(TEMPLATE_ENTRY_UUID, - ProtectedString(false, uuidString)) - } - return null - } - - fun removeMetaTemplateRecognitionFromEntry(entry: EntryKDBX): EntryKDBX { - val entryCopy = EntryKDBX().apply { - updateWith(entry) - } - entryCopy.removeField(TEMPLATE_ENTRY_UUID) - return entryCopy - } - - fun addMetaTemplateRecognitionToEntry(template: Template, entry: EntryKDBX): EntryKDBX { - val entryCopy = EntryKDBX().apply { - updateWith(entry) - } - // Add template field - if (template != Template.STANDARD - && template != Template.CREATION) { - getTemplateUUIDField(template)?.let { templateField -> - entryCopy.putField(templateField) - } - } else { - entryCopy.removeField(TEMPLATE_ENTRY_UUID) - } - return entryCopy - } + abstract fun encodeTemplateEntry(templateEntry: EntryKDBX): EntryKDBX fun createTemplateEntry(template: Template): EntryKDBX { val newEntry = EntryKDBX().apply { @@ -263,111 +92,56 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) { section.attributes.forEach { attribute -> if (index > 0) { // Label is not important with section => [Section_X]: Divider - putField("$PREFIX_DECODED_TEMPLATE$SECTION_DECODED_TEMPLATE${index-1}$SUFFIX_DECODED_TEMPLATE", - ProtectedString(false, TEMPLATE_ATTRIBUTE_TYPE_DIVIDER)) + putField("$PREFIX_DECODED_TEMPLATE$SECTION_DECODED_TEMPLATE_PREFIX${index-1}$SUFFIX_DECODED_TEMPLATE", + ProtectedString(false, TemplateAttributeType.DIVIDER.label)) } - putField(buildFieldFromTemplateAttribute(attribute)) + putField(Field("$PREFIX_DECODED_TEMPLATE${attribute.label}$SUFFIX_DECODED_TEMPLATE", + ProtectedString(attribute.protected, attribute.type.label))) } } } return encodeTemplateEntry(newEntry) } + private fun buildTemplateSectionFromFields(fields: List): TemplateSection { + val sectionAttributes = mutableListOf() + fields.forEach { field -> + sectionAttributes.add(TemplateAttribute( + field.name.removePrefix(PREFIX_DECODED_TEMPLATE).removeSuffix(SUFFIX_DECODED_TEMPLATE), + TemplateAttributeType.getFromLabel(field.protectedValue.stringValue), + field.protectedValue.isProtected) + ) + } + return TemplateSection(sectionAttributes) + } + private fun getTemplateFromTemplateEntry(templateEntry: EntryKDBX): Template? { val templateEntryDecoded = decodeTemplateEntry(templateEntry) - val templateSections = mutableListOf() val sectionFields = mutableListOf() templateEntryDecoded.doForEachDecodedCustomField { field -> - - if (field.name.contains(SECTION_DECODED_TEMPLATE)) { - - val sectionAttributes = mutableListOf() - sectionFields.forEach { - sectionAttributes.add(buildTemplateAttributeFromField(it)) - } - templateSections.add(TemplateSection(sectionAttributes)) + if (field.protectedValue.stringValue.contains(TemplateAttributeType.DIVIDER.label)) { + templateSections.add(buildTemplateSectionFromFields(sectionFields)) sectionFields.clear() } else { sectionFields.add(field) } } - - val sectionAttributes = mutableListOf() - sectionFields.forEach { - sectionAttributes.add(buildTemplateAttributeFromField(it)) - } - templateSections.add(TemplateSection(sectionAttributes)) + templateSections.add(buildTemplateSectionFromFields(sectionFields)) // TODO Add decoder version return Template(templateEntry.id, templateEntry.title, templateEntry.icon, templateSections, 1) } - private fun buildTemplateAttributeFromField(field: Field): TemplateAttribute { - val type = when { - field.protectedValue.stringValue.equals(TEMPLATE_ATTRIBUTE_TYPE_INLINE, true) -> TemplateAttributeType.INLINE - field.protectedValue.stringValue.equals(TEMPLATE_ATTRIBUTE_TYPE_MULTILINE, true) -> TemplateAttributeType.MULTILINE - field.protectedValue.stringValue.equals(TEMPLATE_ATTRIBUTE_TYPE_DATE, true) -> TemplateAttributeType.DATE - field.protectedValue.stringValue.equals(TEMPLATE_ATTRIBUTE_TYPE_TIME, true) -> TemplateAttributeType.TIME - field.protectedValue.stringValue.equals(TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME, true) -> TemplateAttributeType.DATETIME - else -> TemplateAttributeType.INLINE - } - return TemplateAttribute( - field.name.removePrefix(PREFIX_DECODED_TEMPLATE).removeSuffix(SUFFIX_DECODED_TEMPLATE), - type, - field.protectedValue.stringValue.contains(TEMPLATE_ATTRIBUTE_TYPE_PROTECTED, true) - ) - } - - private fun buildFieldFromTemplateAttribute(attribute: TemplateAttribute): Field { - var fieldValue = if (attribute.protected) "$TEMPLATE_ATTRIBUTE_TYPE_PROTECTED " else "" - fieldValue += when (attribute.type) { - TemplateAttributeType.INLINE -> TEMPLATE_ATTRIBUTE_TYPE_INLINE - TemplateAttributeType.SMALL_MULTILINE -> TEMPLATE_ATTRIBUTE_TYPE_INLINE - TemplateAttributeType.MULTILINE -> TEMPLATE_ATTRIBUTE_TYPE_MULTILINE - TemplateAttributeType.DATE -> TEMPLATE_ATTRIBUTE_TYPE_DATE - TemplateAttributeType.TIME -> TEMPLATE_ATTRIBUTE_TYPE_TIME - TemplateAttributeType.DATETIME -> TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME - } - return Field("$PREFIX_DECODED_TEMPLATE${attribute.label}$SUFFIX_DECODED_TEMPLATE", - ProtectedString(false, fieldValue)) - } - companion object { - private data class TemplateAttributePosition(var position: Int, var attribute: TemplateAttribute) private val TAG = TemplateEngine::class.java.name - private const val PREFIX_DECODED_TEMPLATE = "[" - private const val SUFFIX_DECODED_TEMPLATE = "]" - private const val SECTION_DECODED_TEMPLATE = "Divider_" - - // Custom template ref - private const val TEMPLATE_ATTRIBUTE_TITLE = "@title" - private const val TEMPLATE_ATTRIBUTE_USERNAME = "@username" - private const val TEMPLATE_ATTRIBUTE_PASSWORD = "@password" - private const val TEMPLATE_ATTRIBUTE_URL = "@url" - private const val TEMPLATE_ATTRIBUTE_EXP_DATE = "@exp_date" - private const val TEMPLATE_ATTRIBUTE_EXPIRES = "@expires" - private const val TEMPLATE_ATTRIBUTE_NOTES = "@notes" - - private const val TEMPLATE_LABEL_VERSION = "_etm_template" - private const val TEMPLATE_ENTRY_UUID = "_etm_template_uuid" - private const val TEMPLATE_ATTRIBUTE_POSITION_PREFIX = "_etm_position" - private const val TEMPLATE_ATTRIBUTE_TITLE_PREFIX = "_etm_title" - private const val TEMPLATE_ATTRIBUTE_TYPE_PREFIX = "_etm_type" - private const val TEMPLATE_ATTRIBUTE_TYPE_PROTECTED = "Protected" - private const val TEMPLATE_ATTRIBUTE_TYPE_INLINE = "Inline" - private const val TEMPLATE_ATTRIBUTE_TYPE_MULTILINE = "Multiline" - private const val TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME = "Date Time" - private const val TEMPLATE_ATTRIBUTE_TYPE_DATE = "Date" - private const val TEMPLATE_ATTRIBUTE_TYPE_TIME = "Time" - private const val TEMPLATE_ATTRIBUTE_TYPE_DIVIDER = "Divider" - private const val TEMPLATE_ATTRIBUTE_TYPE_LISTBOX = "Listbox" - private const val TEMPLATE_ATTRIBUTE_TYPE_POPOUT = "Popout" - private const val TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX = "Rich Textbox" + const val PREFIX_DECODED_TEMPLATE = "[" + const val SUFFIX_DECODED_TEMPLATE = "]" + const val SECTION_DECODED_TEMPLATE_PREFIX = "Section " fun getDefaultTemplateGroupName(resources: Resources): String { return resources.getString(R.string.templates) @@ -377,33 +151,6 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) { return name.startsWith(PREFIX_DECODED_TEMPLATE) && name.endsWith(SUFFIX_DECODED_TEMPLATE) } - - 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 - TEMPLATE_ATTRIBUTE_USERNAME.equals(name, true) -> TemplateField.LABEL_USERNAME - TEMPLATE_ATTRIBUTE_PASSWORD.equals(name, true) -> TemplateField.LABEL_PASSWORD - TEMPLATE_ATTRIBUTE_URL.equals(name, true) -> TemplateField.LABEL_URL - TEMPLATE_ATTRIBUTE_EXP_DATE.equals(name, true) -> TemplateField.LABEL_EXPIRATION - TEMPLATE_ATTRIBUTE_EXPIRES.equals(name, true) -> TemplateField.LABEL_EXPIRATION - TEMPLATE_ATTRIBUTE_NOTES.equals(name, true) -> TemplateField.LABEL_NOTES - else -> name - } - } - - 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 - TemplateField.LABEL_USERNAME.equals(name, true) -> TEMPLATE_ATTRIBUTE_USERNAME - TemplateField.LABEL_PASSWORD.equals(name, true) -> TEMPLATE_ATTRIBUTE_PASSWORD - TemplateField.LABEL_URL.equals(name, true) -> TEMPLATE_ATTRIBUTE_URL - TemplateField.LABEL_EXPIRATION.equals(name, true) -> TEMPLATE_ATTRIBUTE_EXP_DATE - TemplateField.LABEL_NOTES.equals(name, true) -> TEMPLATE_ATTRIBUTE_NOTES - else -> name - } - } fun getDefaults(): List