Fix password generator

This commit is contained in:
J-Jamet
2021-07-18 19:26:08 +02:00
parent 90935c033d
commit 6ce31305c6
5 changed files with 40 additions and 9 deletions

View File

@@ -121,8 +121,8 @@ class Template : Parcelable {
true, true,
TemplateAttributeOption().apply { TemplateAttributeOption().apply {
setNumberLines(3) setNumberLines(3)
}, associatePasswordGenerator()
TemplateAttributeAction.PASSWORD_GENERATION }
) )
val URL_ATTRIBUTE = TemplateAttribute( val URL_ATTRIBUTE = TemplateAttribute(
TemplateField.LABEL_URL, TemplateField.LABEL_URL,

View File

@@ -1,5 +1,5 @@
package com.kunzisoft.keepass.database.element.template package com.kunzisoft.keepass.database.element.template
enum class TemplateAttributeAction { enum class TemplateAttributeAction {
NONE, CUSTOM_EDITION, PASSWORD_GENERATION NONE, CUSTOM_EDITION
} }

View File

@@ -96,6 +96,18 @@ class TemplateAttributeOption() : Parcelable {
mOptions[TEXT_LINK_ATTR] = isLink.toString() mOptions[TEXT_LINK_ATTR] = isLink.toString()
} }
fun isAssociatedWithPasswordGenerator(): Boolean {
return try {
mOptions[PASSWORD_GENERATOR_ATTR]?.toBoolean() ?: PASSWORD_GENERATOR_VALUE_DEFAULT
} catch (e: Exception) {
PASSWORD_GENERATOR_VALUE_DEFAULT
}
}
fun associatePasswordGenerator() {
mOptions[PASSWORD_GENERATOR_ATTR] = true.toString()
}
fun getListItems(): List<String> { fun getListItems(): List<String> {
return mOptions[LIST_ITEMS]?.split(LIST_ITEMS_SEPARATOR) ?: listOf() return mOptions[LIST_ITEMS]?.split(LIST_ITEMS_SEPARATOR) ?: listOf()
} }
@@ -158,6 +170,7 @@ class TemplateAttributeOption() : Parcelable {
/** /**
* Applicable to type TEXT * Applicable to type TEXT
* Define a number of chars
* Integer, can be "many" or "-1" to infinite value * Integer, can be "many" or "-1" to infinite value
* "1" if not defined * "1" if not defined
*/ */
@@ -168,6 +181,7 @@ class TemplateAttributeOption() : Parcelable {
/** /**
* Applicable to type TEXT * Applicable to type TEXT
* Define a number of lines
* Integer, can be "-1" to infinite value * Integer, can be "-1" to infinite value
* "1" if not defined * "1" if not defined
*/ */
@@ -178,14 +192,25 @@ class TemplateAttributeOption() : Parcelable {
/** /**
* Applicable to type TEXT * Applicable to type TEXT
* Define if a text is a link
* Boolean ("true" or "false") * Boolean ("true" or "false")
* "true" if not defined * "true" if not defined
*/ */
private const val TEXT_LINK_ATTR = "link" private const val TEXT_LINK_ATTR = "link"
private const val TEXT_LINK_VALUE_DEFAULT = false private const val TEXT_LINK_VALUE_DEFAULT = false
/**
* Applicable to type TEXT
* Define if a password generator is associated with the text
* Boolean ("true" or "false")
* "false" if not defined
*/
private const val PASSWORD_GENERATOR_ATTR = "generator"
private const val PASSWORD_GENERATOR_VALUE_DEFAULT = false
/** /**
* Applicable to type LIST * Applicable to type LIST
* Define items of a list
* List of items, separator is '|' * List of items, separator is '|'
*/ */
private const val LIST_ITEMS = "items" private const val LIST_ITEMS = "items"
@@ -193,6 +218,7 @@ class TemplateAttributeOption() : Parcelable {
/** /**
* Applicable to type DATETIME * Applicable to type DATETIME
* Define the type of date
* String ("date" or "time" or "datetime" or based on https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) * String ("date" or "time" or "datetime" or based on https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
* "datetime" if not defined * "datetime" if not defined
*/ */
@@ -200,7 +226,6 @@ class TemplateAttributeOption() : Parcelable {
private const val DATETIME_FORMAT_VALUE_DATE = "date" private const val DATETIME_FORMAT_VALUE_DATE = "date"
private const val DATETIME_FORMAT_VALUE_TIME = "time" private const val DATETIME_FORMAT_VALUE_TIME = "time"
fun getOptionsFromString(label: String): TemplateAttributeOption { fun getOptionsFromString(label: String): TemplateAttributeOption {
val options = TemplateAttributeOption() val options = TemplateAttributeOption()
val optionsMap = if (label.contains("{") || label.contains("}")) { val optionsMap = if (label.contains("{") || label.contains("}")) {

View File

@@ -195,6 +195,11 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database)
val attribute = it.attribute val attribute = it.attribute
// Add password generator
if (attribute.label.equals(TEMPLATE_ATTRIBUTE_PASSWORD, true)) {
attribute.options.associatePasswordGenerator()
}
// Recognize each temp option // Recognize each temp option
attribute.options.get(TEMPLATE_ATTRIBUTE_OPTIONS_TEMP)?.let { defaultOption -> attribute.options.get(TEMPLATE_ATTRIBUTE_OPTIONS_TEMP)?.let { defaultOption ->
when (attribute.type) { when (attribute.type) {

View File

@@ -88,6 +88,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context,
?: TemplateField.getLocalizedName(context, field.name) ?: TemplateField.getLocalizedName(context, field.name)
val fieldValue = field.protectedValue.stringValue val fieldValue = field.protectedValue.stringValue
value = if (fieldValue.isEmpty()) templateAttribute.default else fieldValue value = if (fieldValue.isEmpty()) templateAttribute.default else fieldValue
// TODO edition and password generator at same time
when (templateAttribute.action) { when (templateAttribute.action) {
TemplateAttributeAction.NONE -> { TemplateAttributeAction.NONE -> {
setOnActionClickListener(null) setOnActionClickListener(null)
@@ -97,11 +98,11 @@ class TemplateEditView @JvmOverloads constructor(context: Context,
mOnCustomEditionActionClickListener?.invoke(field) mOnCustomEditionActionClickListener?.invoke(field)
}, R.drawable.ic_more_white_24dp) }, R.drawable.ic_more_white_24dp)
} }
TemplateAttributeAction.PASSWORD_GENERATION -> { }
setOnActionClickListener({ if (templateAttribute.options.isAssociatedWithPasswordGenerator()) {
mOnPasswordGenerationActionClickListener?.invoke(field) setOnActionClickListener({
}, R.drawable.ic_generate_password_white_24dp) mOnPasswordGenerationActionClickListener?.invoke(field)
} }, R.drawable.ic_generate_password_white_24dp)
} }
} }
} }