mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Add templates settings
This commit is contained in:
@@ -371,6 +371,28 @@ class Database {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a configurable templates group is available or not for this version of database
|
||||
* @return true if a configurable templates group available
|
||||
*/
|
||||
val allowConfigurableTemplatesGroup: Boolean
|
||||
get() = mDatabaseKDBX != null
|
||||
|
||||
// Maybe another templates method with KDBX5
|
||||
var isTemplatesEnabled: Boolean
|
||||
get() = mDatabaseKDBX?.isTemplatesGroupEnabled() ?: false
|
||||
set(value) {
|
||||
mDatabaseKDBX?.enableTemplatesGroup(value)
|
||||
}
|
||||
|
||||
val templatesGroup: Group?
|
||||
get() {
|
||||
mDatabaseKDBX?.getTemplatesGroup()?.let {
|
||||
return Group(it)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val groupNamesNotAllowed: List<String>
|
||||
get() {
|
||||
return mDatabaseKDB?.groupNamesNotAllowed ?: ArrayList()
|
||||
|
||||
@@ -335,6 +335,32 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
return this.iconsManager.getIcon(iconUuid)
|
||||
}
|
||||
|
||||
fun isTemplatesGroupEnabled(): Boolean {
|
||||
return entryTemplatesGroup != UUID_ZERO
|
||||
}
|
||||
|
||||
fun enableTemplatesGroup(enable: Boolean) {
|
||||
if (enable) {
|
||||
// TODO Build default templates group
|
||||
setTemplatesGroup(UUID_ZERO)
|
||||
} else {
|
||||
entryTemplatesGroup = UUID_ZERO
|
||||
mTemplateEngine.clearCache()
|
||||
}
|
||||
}
|
||||
|
||||
fun getTemplatesGroup(): GroupKDBX? {
|
||||
if (isTemplatesGroupEnabled()) {
|
||||
return getGroupById(entryTemplatesGroup)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun setTemplatesGroup(uuid: UUID) {
|
||||
entryTemplatesGroup = uuid
|
||||
entryTemplatesGroupChanged = DateInstant()
|
||||
}
|
||||
|
||||
fun getTemplates(): List<Template> {
|
||||
return mTemplateEngine.getTemplates()
|
||||
}
|
||||
|
||||
@@ -12,24 +12,29 @@ class TemplateEngine(private val databaseKDBX: DatabaseKDBX) {
|
||||
private val mCacheTemplates = HashMap<UUID, Template>()
|
||||
|
||||
fun getTemplates(): List<Template> {
|
||||
// TODO retrieve template group
|
||||
val templates = mutableListOf<Template>()
|
||||
try {
|
||||
val templateGroup = databaseKDBX.getGroupById(UUID.fromString("48071a4e-9577-af41-adbc-723e17a1f2ac"))
|
||||
val templateGroup = databaseKDBX.getTemplatesGroup()
|
||||
mCacheTemplates.clear()
|
||||
templates.add(Template.STANDARD)
|
||||
templateGroup?.getChildEntries()?.forEach { templateEntry ->
|
||||
getTemplateFromTemplateEntry(templateEntry)?.let {
|
||||
mCacheTemplates[templateEntry.id] = it
|
||||
if (templateGroup != null) {
|
||||
templates.add(Template.STANDARD)
|
||||
templateGroup.getChildEntries().forEach { templateEntry ->
|
||||
getTemplateFromTemplateEntry(templateEntry)?.let {
|
||||
mCacheTemplates[templateEntry.id] = it
|
||||
}
|
||||
}
|
||||
templates.addAll(mCacheTemplates.values)
|
||||
}
|
||||
templates.addAll(mCacheTemplates.values)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to get templates from group", e)
|
||||
}
|
||||
return templates
|
||||
}
|
||||
|
||||
fun clearCache() {
|
||||
mCacheTemplates.clear()
|
||||
}
|
||||
|
||||
private fun getTemplateByCache(uuid: UUID): Template? {
|
||||
try {
|
||||
if (mCacheTemplates.containsKey(uuid))
|
||||
|
||||
@@ -34,12 +34,14 @@ import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
|
||||
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.Group
|
||||
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
|
||||
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService
|
||||
import com.kunzisoft.keepass.settings.preference.*
|
||||
import com.kunzisoft.keepass.settings.preferencedialogfragment.*
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.utils.MenuUtil
|
||||
import com.kunzisoft.keepass.utils.UuidUtil
|
||||
|
||||
class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
|
||||
|
||||
@@ -53,6 +55,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
|
||||
private var dbCustomColorPref: DialogColorPreference? = null
|
||||
private var dbDataCompressionPref: Preference? = null
|
||||
private var recycleBinGroupPref: Preference? = null
|
||||
private var templatesGroupPref: Preference? = null
|
||||
private var dbMaxHistoryItemsPref: InputNumberPreference? = null
|
||||
private var dbMaxHistorySizePref: InputNumberPreference? = null
|
||||
private var mEncryptionAlgorithmPref: DialogListExplanationPreference? = null
|
||||
@@ -146,10 +149,9 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
|
||||
dbCompressionPrefCategory?.isVisible = false
|
||||
}
|
||||
|
||||
// Recycle bin
|
||||
val dbRecycleBinPrefCategory: PreferenceCategory? = findPreference(getString(R.string.database_category_recycle_bin_key))
|
||||
recycleBinGroupPref = findPreference(getString(R.string.recycle_bin_group_key))
|
||||
|
||||
// Recycle bin
|
||||
if (mDatabase.allowConfigurableRecycleBin) {
|
||||
val recycleBinEnablePref: SwitchPreference? = findPreference(getString(R.string.recycle_bin_enable_key))
|
||||
recycleBinEnablePref?.apply {
|
||||
@@ -158,6 +160,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
val recycleBinEnabled = newValue as Boolean
|
||||
mDatabase.isRecycleBinEnabled = recycleBinEnabled
|
||||
// TODO Change method
|
||||
if (recycleBinEnabled) {
|
||||
mDatabase.ensureRecycleBinExists(resources)
|
||||
} else {
|
||||
@@ -180,6 +183,34 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
|
||||
dbRecycleBinPrefCategory?.isVisible = false
|
||||
}
|
||||
|
||||
// Templates
|
||||
val templatesGroupPrefCategory: PreferenceCategory? = findPreference(getString(R.string.database_category_templates_key))
|
||||
templatesGroupPref = findPreference(getString(R.string.templates_group_uuid_key))
|
||||
if (mDatabase.allowConfigurableTemplatesGroup) {
|
||||
val templatesEnablePref: SwitchPreference? = findPreference(getString(R.string.templates_group_enable_key))
|
||||
templatesEnablePref?.apply {
|
||||
isChecked = mDatabase.isTemplatesEnabled
|
||||
isEnabled = if (!mDatabaseReadOnly) {
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
val templatesEnabled = newValue as Boolean
|
||||
mDatabase.isTemplatesEnabled = templatesEnabled
|
||||
refreshTemplatesGroup()
|
||||
// Save the database if not in readonly mode
|
||||
(context as SettingsActivity?)?.
|
||||
mProgressDatabaseTaskProvider?.startDatabaseSave(mDatabaseAutoSaveEnabled)
|
||||
true
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
// Recycle Bin group
|
||||
refreshTemplatesGroup()
|
||||
} else {
|
||||
templatesGroupPrefCategory?.isVisible = false
|
||||
}
|
||||
|
||||
// History
|
||||
findPreference<PreferenceCategory>(getString(R.string.database_category_history_key))
|
||||
?.isVisible = mDatabase.manageHistory == true
|
||||
@@ -199,10 +230,33 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun groupSummary(group: Group?): String? {
|
||||
val groupTitle = group?.title
|
||||
val groupUUID = group?.nodeIdKDBX?.let {
|
||||
UuidUtil.toHexString(it.id)
|
||||
}
|
||||
if (groupTitle == null
|
||||
|| groupUUID == null)
|
||||
return null
|
||||
return "$groupTitle ($groupUUID)"
|
||||
}
|
||||
|
||||
private fun refreshRecycleBinGroup() {
|
||||
recycleBinGroupPref?.apply {
|
||||
if (mDatabase.isRecycleBinEnabled) {
|
||||
summary = mDatabase.recycleBin?.title
|
||||
summary = groupSummary(mDatabase.recycleBin)
|
||||
isEnabled = true
|
||||
} else {
|
||||
summary = null
|
||||
isEnabled = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshTemplatesGroup() {
|
||||
templatesGroupPref?.apply {
|
||||
if (mDatabase.isTemplatesEnabled) {
|
||||
summary = groupSummary(mDatabase.templatesGroup)
|
||||
isEnabled = true
|
||||
} else {
|
||||
summary = null
|
||||
|
||||
@@ -213,6 +213,10 @@
|
||||
<string name="recycle_bin_enable_key" translatable="false">recycle_bin_enable_key</string>
|
||||
<string name="recycle_bin_group_key" translatable="false">recycle_bin_group_key</string>
|
||||
|
||||
<string name="database_category_templates_key" translatable="false">database_category_templates_key</string>
|
||||
<string name="templates_group_enable_key" translatable="false">templates_group_enable_key</string>
|
||||
<string name="templates_group_uuid_key" translatable="false">templates_group_uuid_key</string>
|
||||
|
||||
<string name="database_category_history_key" translatable="false">database_category_history_key</string>
|
||||
<string name="max_history_items_key" translatable="false">max_history_items_key</string>
|
||||
<string name="max_history_size_key" translatable="false">max_history_size_key</string>
|
||||
|
||||
@@ -376,6 +376,9 @@
|
||||
<string name="recycle_bin_title">Recycle bin usage</string>
|
||||
<string name="recycle_bin_summary">Moves groups and entries to \"Recycle bin\" group before deleting</string>
|
||||
<string name="recycle_bin_group_title">Recycle bin group</string>
|
||||
<string name="templates_group_enable_title">Templates usage</string>
|
||||
<string name="templates_group_enable_summary">Use dynamic templates to fill in the fields of an entry</string>
|
||||
<string name="templates_group_uuid_title">Templates group</string>
|
||||
<string name="max_history_items_title">Maximum number</string>
|
||||
<string name="max_history_items_summary">Limit the number of history items per entry</string>
|
||||
<string name="max_history_size_title">Maximum size</string>
|
||||
@@ -408,6 +411,7 @@
|
||||
<string name="compression_none">None</string>
|
||||
<string name="compression_gzip">Gzip</string>
|
||||
<string name="recycle_bin">Recycle bin</string>
|
||||
<string name="templates">Recycle bin</string>
|
||||
<string name="keyboard">Keyboard</string>
|
||||
<string name="magic_keyboard_title">Magikeyboard</string>
|
||||
<string name="magic_keyboard_explanation_summary">Activate a custom keyboard populating your passwords and all identity fields</string>
|
||||
|
||||
@@ -91,6 +91,23 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="@string/database_category_templates_key"
|
||||
android:title="@string/templates">
|
||||
|
||||
<SwitchPreference
|
||||
android:key="@string/templates_group_enable_key"
|
||||
android:persistent="false"
|
||||
android:title="@string/templates_group_enable_title"
|
||||
android:summary="@string/templates_group_enable_summary"
|
||||
android:checked="false"/>
|
||||
<Preference
|
||||
android:key="@string/templates_group_uuid_key"
|
||||
android:persistent="false"
|
||||
android:title="@string/templates_group_uuid_title"/>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="@string/database_category_history_key"
|
||||
android:title="@string/entry_history">
|
||||
|
||||
Reference in New Issue
Block a user