Encode and decode labels

This commit is contained in:
J-Jamet
2021-07-03 14:42:35 +02:00
parent 719d45e75e
commit 55a4af9f00

View File

@@ -173,7 +173,7 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) {
val newFields = arrayOfNulls<Field>(attributes.size)
attributes.values.forEach {
// PREFIX_DECODED_TEMPLATE to fix same label as standard fields
newFields[it.position] = Field(PREFIX_DECODED_TEMPLATE + it.attribute.label + SUFFIX_DECODED_TEMPLATE,
newFields[it.position] = Field(PREFIX_DECODED_TEMPLATE + decodeTemplateAttribute(it.attribute.label) + SUFFIX_DECODED_TEMPLATE,
ProtectedString(false, it.attribute.defaultValue))
}
newFields.forEach { field ->
@@ -194,7 +194,8 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) {
// Dynamic attributes
var index = 0
templateEntry.doForEachDecodedCustomField { field ->
val label = field.name.removePrefix(PREFIX_DECODED_TEMPLATE).removeSuffix(SUFFIX_DECODED_TEMPLATE)
val label = encodeTemplateAttribute(field.name
.removePrefix(PREFIX_DECODED_TEMPLATE).removeSuffix(SUFFIX_DECODED_TEMPLATE))
val value = field.protectedValue
when {
label.equals(TEMPLATE_LABEL_VERSION, true) -> {
@@ -377,7 +378,6 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) {
&& name.endsWith(SUFFIX_DECODED_TEMPLATE)
}
// TODO template attribute
fun decodeTemplateAttribute(name: String): String {
return when {
TEMPLATE_LABEL_VERSION.equals(name, true) -> TemplateField.LABEL_VERSION
@@ -391,6 +391,19 @@ class TemplateEngine(private val mDatabase: DatabaseKDBX) {
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<Template> {
val templateBuilder = TemplateBuilder()