Refactor prefix and suffix template

This commit is contained in:
J-Jamet
2021-07-18 11:48:35 +02:00
parent 3929b478a7
commit 257992d314
5 changed files with 51 additions and 38 deletions

View File

@@ -41,6 +41,7 @@ import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.node.NodeIdInt
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
import com.kunzisoft.keepass.database.element.template.Template
import com.kunzisoft.keepass.database.element.template.TemplateEngine
import com.kunzisoft.keepass.database.exception.*
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX
@@ -150,7 +151,7 @@ class Database {
fun getTemplate(entry: Entry): Template? {
if (entryIsTemplate(entry))
return Template.CREATION
return TemplateEngine.CREATION
entry.entryKDBX?.let { entryKDBX ->
return mDatabaseKDBX?.getTemplate(entryKDBX)
}

View File

@@ -160,18 +160,5 @@ class Template : Parcelable {
IconImage(),
sections)
}
val CREATION: Template
get() {
val sections = mutableListOf<TemplateSection>()
val mainSection = TemplateSection(mutableListOf<TemplateAttribute>().apply {
// Dynamic part
})
sections.add(mainSection)
return Template(UUID(0, 1),
TemplateField.LABEL_TEMPLATE,
IconImage(IconImageStandard(IconImageStandard.BUILD_ID)),
sections)
}
}
}

View File

@@ -9,7 +9,9 @@ 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.node.NodeIdUUID
import com.kunzisoft.keepass.database.element.template.TemplateEngine.Companion.addDecoratorToTemplateEntryField
import java.util.*
import kotlin.collections.HashMap
@@ -38,7 +40,7 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
}
fun getTemplateCreation(): Template {
return Template(Template.CREATION)
return Template(CREATION)
}
fun createNewTemplatesGroup(resources: Resources): GroupKDBX {
@@ -96,8 +98,10 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
"$SECTION_DECODED_TEMPLATE_PREFIX${index-1}"
else
section.name
putField("$PREFIX_DECODED_TEMPLATE$sectionName$SUFFIX_DECODED_TEMPLATE",
ProtectedString(false, TemplateAttributeType.DIVIDER.typeString))
putField(Field(sectionName,
ProtectedString(false, TemplateAttributeType.DIVIDER.typeString)).apply {
addDecoratorToTemplateEntryField()
})
}
putField(buildTemplateEntryField(attribute))
@@ -111,17 +115,18 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
val typeAndOptions = attribute.type.typeString +
TemplateAttributeOption.getStringFromOptions(attribute.options)
// PREFIX_DECODED_TEMPLATE to fix same label as standard fields
return Field(PREFIX_DECODED_TEMPLATE
+ TemplateEngineCompatible.decodeTemplateAttribute(attribute.label)
+ SUFFIX_DECODED_TEMPLATE,
ProtectedString(attribute.protected, typeAndOptions))
return Field(TemplateEngineCompatible.decodeTemplateAttribute(attribute.label),
ProtectedString(attribute.protected, typeAndOptions)).apply {
addDecoratorToTemplateEntryField()
}
}
private fun buildTemplateSectionFromFields(fields: List<Field>): TemplateSection {
val sectionAttributes = mutableListOf<TemplateAttribute>()
fields.forEach { field ->
field.removeDecoratorFromTemplateEntryField()
sectionAttributes.add(TemplateAttribute(
field.name.removePrefix(PREFIX_DECODED_TEMPLATE).removeSuffix(SUFFIX_DECODED_TEMPLATE),
field.name,
TemplateAttributeType.getFromString(field.protectedValue.stringValue),
field.protectedValue.isProtected,
TemplateAttributeOption.getOptionsFromString(field.protectedValue.stringValue))
@@ -153,18 +158,26 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
private val TAG = TemplateEngine::class.java.name
const val PREFIX_DECODED_TEMPLATE = "["
const val SUFFIX_DECODED_TEMPLATE = "]"
const val SECTION_DECODED_TEMPLATE_PREFIX = "Section "
private const val PREFIX_DECODED_TEMPLATE = "["
private const val SUFFIX_DECODED_TEMPLATE = "]"
private const val SECTION_DECODED_TEMPLATE_PREFIX = "Section "
val CREATION: Template
get() {
val sections = mutableListOf<TemplateSection>()
val mainSection = TemplateSection(mutableListOf<TemplateAttribute>().apply {
// Dynamic part
})
sections.add(mainSection)
return Template(UUID(0, 1),
TemplateField.LABEL_TEMPLATE,
IconImage(IconImageStandard(IconImageStandard.BUILD_ID)),
sections)
}
fun getDefaultTemplateGroupName(resources: Resources): String {
return resources.getString(R.string.templates)
}
fun isTemplateNameAttribute(name: String): Boolean {
return name.startsWith(PREFIX_DECODED_TEMPLATE)
&& name.endsWith(SUFFIX_DECODED_TEMPLATE)
}
fun getDefaults(): List<Template> {
val templateBuilder = TemplateBuilder()
@@ -177,5 +190,20 @@ abstract class TemplateEngine(private val mDatabase: DatabaseKDBX) {
templateBuilder.bank,
templateBuilder.cryptocurrency)
}
fun isTemplateNameAttribute(name: String): Boolean {
return name.startsWith(PREFIX_DECODED_TEMPLATE)
&& name.endsWith(SUFFIX_DECODED_TEMPLATE)
}
fun Field.addDecoratorToTemplateEntryField() {
this.name = "$PREFIX_DECODED_TEMPLATE${this.name}$SUFFIX_DECODED_TEMPLATE"
}
fun Field.removeDecoratorFromTemplateEntryField() {
this.name = this.name
.removePrefix(PREFIX_DECODED_TEMPLATE)
.removeSuffix(SUFFIX_DECODED_TEMPLATE)
}
}
}

View File

@@ -39,7 +39,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database)
}
// Add template field
if (template != Template.STANDARD
&& template != Template.CREATION) {
&& template != CREATION) {
getTemplateUUIDField(template)?.let { templateField ->
entryCopy.putField(templateField)
}
@@ -258,11 +258,8 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database)
// Dynamic attributes
var index = 0
templateEntry.doForEachDecodedCustomField { field ->
val label = encodeTemplateAttribute(
field.name
.removePrefix(PREFIX_DECODED_TEMPLATE)
.removeSuffix(SUFFIX_DECODED_TEMPLATE)
)
field.removeDecoratorFromTemplateEntryField()
val label = encodeTemplateAttribute(field.name)
val value = field.protectedValue
when {
label.equals(TEMPLATE_LABEL_VERSION, true) -> {

View File

@@ -454,7 +454,7 @@ abstract class TemplateAbstractView<
return mCustomFieldIds.indexOfFirst { it.label.equals(name, true) }
}
protected fun retrieveCustomFieldsFromView(templateFieldNotEmpty: Boolean = false) {
private fun retrieveCustomFieldsFromView(templateFieldNotEmpty: Boolean = false) {
mEntryInfo?.customFields = mCustomFieldIds.mapNotNull {
getCustomField(it.label, templateFieldNotEmpty)
}.toMutableList()
@@ -470,7 +470,7 @@ abstract class TemplateAbstractView<
/**
* Update a custom field or create a new one if doesn't exists, the old value is lost
*/
protected fun putCustomField(customField: Field, focus: Boolean): Boolean {
private fun putCustomField(customField: Field, focus: Boolean): Boolean {
return if (!isStandardFieldName(customField.name)) {
customFieldsContainerView.visibility = View.VISIBLE
if (indexCustomFieldIdByName(customField.name) >= 0) {