mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Refactor search parameters and Autofill search
This commit is contained in:
@@ -44,6 +44,7 @@ import com.kunzisoft.keepass.database.file.input.DatabaseInputKDBX
|
||||
import com.kunzisoft.keepass.database.file.output.DatabaseOutputKDB
|
||||
import com.kunzisoft.keepass.database.file.output.DatabaseOutputKDBX
|
||||
import com.kunzisoft.keepass.database.search.SearchHelper
|
||||
import com.kunzisoft.keepass.database.search.SearchParameters
|
||||
import com.kunzisoft.keepass.icons.IconDrawableFactory
|
||||
import com.kunzisoft.keepass.model.SearchInfo
|
||||
import com.kunzisoft.keepass.stream.readBytes4ToInt
|
||||
@@ -397,17 +398,28 @@ class Database {
|
||||
false
|
||||
}
|
||||
|
||||
fun createVirtualGroupFromSearch(searchQuery: String, max: Int = Integer.MAX_VALUE): Group? {
|
||||
return mSearchHelper?.createVirtualGroupWithSearchResult(this, searchQuery, max)
|
||||
fun createVirtualGroupFromSearch(searchQuery: String,
|
||||
max: Int = Integer.MAX_VALUE): Group? {
|
||||
return mSearchHelper?.createVirtualGroupWithSearchResult(this, searchQuery, SearchParameters(), max)
|
||||
}
|
||||
|
||||
fun createVirtualGroupFromSearch(searchInfo: SearchInfo, max: Int = Integer.MAX_VALUE): Group? {
|
||||
fun createVirtualGroupFromSearch(searchInfo: SearchInfo,
|
||||
max: Int = Integer.MAX_VALUE): Group? {
|
||||
val query = (if (searchInfo.webDomain != null)
|
||||
searchInfo.webDomain
|
||||
else
|
||||
searchInfo.applicationId)
|
||||
?: return null
|
||||
return mSearchHelper?.createVirtualGroupWithSearchResult(this, query, max)
|
||||
return mSearchHelper?.createVirtualGroupWithSearchResult(this, query, SearchParameters().apply {
|
||||
searchInTitles = false
|
||||
searchInUserNames = false
|
||||
searchInPasswords = false
|
||||
searchInNotes = true
|
||||
searchInOther = true
|
||||
searchInUUIDs = false
|
||||
searchInTags = false
|
||||
ignoreCase = true
|
||||
}, max)
|
||||
}
|
||||
|
||||
@Throws(DatabaseOutputException::class)
|
||||
|
||||
@@ -22,7 +22,7 @@ package com.kunzisoft.keepass.database.element.entry
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
|
||||
import com.kunzisoft.keepass.database.element.group.GroupKDBX
|
||||
import com.kunzisoft.keepass.database.search.EntryKDBXSearchHandler
|
||||
import com.kunzisoft.keepass.database.search.SearchParametersKDBX
|
||||
import com.kunzisoft.keepass.database.search.SearchParameters
|
||||
import com.kunzisoft.keepass.utils.StringUtil
|
||||
import java.util.*
|
||||
|
||||
@@ -142,23 +142,23 @@ class FieldReferencesEngine {
|
||||
val scan = Character.toUpperCase(ref[2])
|
||||
val wanted = Character.toUpperCase(ref[0])
|
||||
|
||||
val searchParametersV4 = SearchParametersKDBX()
|
||||
searchParametersV4.setupNone()
|
||||
val searchParameters = SearchParameters()
|
||||
searchParameters.setupNone()
|
||||
|
||||
searchParametersV4.searchString = ref.substring(4)
|
||||
searchParameters.searchString = ref.substring(4)
|
||||
when (scan) {
|
||||
'T' -> searchParametersV4.searchInTitles = true
|
||||
'U' -> searchParametersV4.searchInUserNames = true
|
||||
'A' -> searchParametersV4.searchInUrls = true
|
||||
'P' -> searchParametersV4.searchInPasswords = true
|
||||
'N' -> searchParametersV4.searchInNotes = true
|
||||
'I' -> searchParametersV4.searchInUUIDs = true
|
||||
'O' -> searchParametersV4.searchInOther = true
|
||||
'T' -> searchParameters.searchInTitles = true
|
||||
'U' -> searchParameters.searchInUserNames = true
|
||||
'A' -> searchParameters.searchInUrls = true
|
||||
'P' -> searchParameters.searchInPasswords = true
|
||||
'N' -> searchParameters.searchInNotes = true
|
||||
'I' -> searchParameters.searchInUUIDs = true
|
||||
'O' -> searchParameters.searchInOther = true
|
||||
else -> return null
|
||||
}
|
||||
|
||||
val list = ArrayList<EntryKDBX>()
|
||||
searchEntries(contextV4.databaseV4?.rootGroup, searchParametersV4, list)
|
||||
searchEntries(contextV4.databaseV4?.rootGroup, searchParameters, list)
|
||||
|
||||
return if (list.size > 0) {
|
||||
TargetResult(list[0], wanted)
|
||||
@@ -190,17 +190,17 @@ class FieldReferencesEngine {
|
||||
return newText
|
||||
}
|
||||
|
||||
private fun searchEntries(root: GroupKDBX?, searchParametersV4: SearchParametersKDBX?, listStorage: MutableList<EntryKDBX>?) {
|
||||
if (searchParametersV4 == null) {
|
||||
private fun searchEntries(root: GroupKDBX?, searchParameters: SearchParameters?, listStorage: MutableList<EntryKDBX>?) {
|
||||
if (searchParameters == null) {
|
||||
return
|
||||
}
|
||||
if (listStorage == null) {
|
||||
return
|
||||
}
|
||||
|
||||
val terms = StringUtil.splitStringTerms(searchParametersV4.searchString)
|
||||
if (terms.size <= 1 || searchParametersV4.regularExpression) {
|
||||
root!!.doForEachChild(EntryKDBXSearchHandler(searchParametersV4, listStorage), null)
|
||||
val terms = StringUtil.splitStringTerms(searchParameters.searchString)
|
||||
if (terms.size <= 1 || searchParameters.regularExpression) {
|
||||
root!!.doForEachChild(EntryKDBXSearchHandler(searchParameters, listStorage), null)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -208,20 +208,20 @@ class FieldReferencesEngine {
|
||||
val stringLengthComparator = Comparator<String> { lhs, rhs -> lhs.length - rhs.length }
|
||||
Collections.sort(terms, stringLengthComparator)
|
||||
|
||||
val fullSearch = searchParametersV4.searchString
|
||||
val fullSearch = searchParameters.searchString
|
||||
var childEntries: List<EntryKDBX>? = root!!.getChildEntries()
|
||||
for (i in terms.indices) {
|
||||
val pgNew = ArrayList<EntryKDBX>()
|
||||
|
||||
searchParametersV4.searchString = terms[i]
|
||||
searchParameters.searchString = terms[i]
|
||||
|
||||
var negate = false
|
||||
if (searchParametersV4.searchString.startsWith("-")) {
|
||||
searchParametersV4.searchString = searchParametersV4.searchString.substring(1)
|
||||
negate = searchParametersV4.searchString.isNotEmpty()
|
||||
if (searchParameters.searchString.startsWith("-")) {
|
||||
searchParameters.searchString = searchParameters.searchString.substring(1)
|
||||
negate = searchParameters.searchString.isNotEmpty()
|
||||
}
|
||||
|
||||
if (!root.doForEachChild(EntryKDBXSearchHandler(searchParametersV4, pgNew), null)) {
|
||||
if (!root.doForEachChild(EntryKDBXSearchHandler(searchParameters, pgNew), null)) {
|
||||
childEntries = null
|
||||
break
|
||||
}
|
||||
@@ -242,7 +242,7 @@ class FieldReferencesEngine {
|
||||
if (childEntries != null) {
|
||||
listStorage.addAll(childEntries)
|
||||
}
|
||||
searchParametersV4.searchString = fullSearch
|
||||
searchParameters.searchString = fullSearch
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
|
||||
* Copyright 2020 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
@@ -23,10 +23,11 @@ import com.kunzisoft.keepass.database.action.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
|
||||
import com.kunzisoft.keepass.database.search.iterator.EntrySearchStringIteratorKDBX
|
||||
import com.kunzisoft.keepass.utils.StringUtil
|
||||
import java.util.*
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
class EntryKDBXSearchHandler(private val mSearchParametersKDBX: SearchParametersKDBX, private val mListStorage: MutableList<EntryKDBX>) : NodeHandler<EntryKDBX>() {
|
||||
class EntryKDBXSearchHandler(private val mSearchParametersKDBX: SearchParameters,
|
||||
private val mListStorage: MutableList<EntryKDBX>)
|
||||
: NodeHandler<EntryKDBX>() {
|
||||
|
||||
override fun operate(node: EntryKDBX): Boolean {
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.kunzisoft.keepass.database.action.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.Entry
|
||||
import com.kunzisoft.keepass.database.element.Group
|
||||
import com.kunzisoft.keepass.database.search.iterator.EntrySearchStringIterator
|
||||
import com.kunzisoft.keepass.database.search.iterator.EntrySearchStringIteratorKDB
|
||||
import com.kunzisoft.keepass.database.search.iterator.EntrySearchStringIteratorKDBX
|
||||
|
||||
@@ -35,7 +34,10 @@ class SearchHelper(private val isOmitBackup: Boolean) {
|
||||
|
||||
private var incrementEntry = 0
|
||||
|
||||
fun createVirtualGroupWithSearchResult(database: Database, searchQuery: String, max: Int): Group? {
|
||||
fun createVirtualGroupWithSearchResult(database: Database,
|
||||
searchQuery: String,
|
||||
searchParameters: SearchParameters,
|
||||
max: Int): Group? {
|
||||
|
||||
val searchGroup = database.createGroup()
|
||||
searchGroup?.title = "\"" + searchQuery + "\""
|
||||
@@ -47,7 +49,7 @@ class SearchHelper(private val isOmitBackup: Boolean) {
|
||||
override fun operate(node: Entry): Boolean {
|
||||
if (incrementEntry >= max)
|
||||
return false
|
||||
if (entryContainsString(node, searchQuery)) {
|
||||
if (entryContainsString(node, searchQuery, searchParameters)) {
|
||||
searchGroup?.addChildEntry(node)
|
||||
incrementEntry++
|
||||
}
|
||||
@@ -69,26 +71,28 @@ class SearchHelper(private val isOmitBackup: Boolean) {
|
||||
return searchGroup
|
||||
}
|
||||
|
||||
private fun entryContainsString(entry: Entry, searchString: String): Boolean {
|
||||
private fun entryContainsString(entry: Entry,
|
||||
searchQuery: String,
|
||||
searchParameters: SearchParameters): Boolean {
|
||||
|
||||
// Entry don't contains string if the search string is empty
|
||||
if (searchString.isEmpty())
|
||||
if (searchQuery.isEmpty())
|
||||
return false
|
||||
|
||||
// Search all strings in the entry
|
||||
var iterator: EntrySearchStringIterator? = null
|
||||
var iterator: Iterator<String>? = null
|
||||
entry.entryKDB?.let {
|
||||
iterator = EntrySearchStringIteratorKDB(it)
|
||||
iterator = EntrySearchStringIteratorKDB(it, searchParameters)
|
||||
}
|
||||
entry.entryKDBX?.let {
|
||||
iterator = EntrySearchStringIteratorKDBX(it)
|
||||
iterator = EntrySearchStringIteratorKDBX(it, searchParameters)
|
||||
}
|
||||
|
||||
iterator?.let {
|
||||
while (it.hasNext()) {
|
||||
val currentString = it.next()
|
||||
if (currentString.isNotEmpty()
|
||||
&& currentString.contains(searchString, true)) {
|
||||
&& currentString.contains(searchQuery, true)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
* Copyright 2020 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
@@ -22,7 +22,7 @@ package com.kunzisoft.keepass.database.search
|
||||
/**
|
||||
* Parameters for searching strings in the database.
|
||||
*/
|
||||
open class SearchParameters {
|
||||
class SearchParameters {
|
||||
|
||||
var searchString: String = ""
|
||||
|
||||
@@ -33,6 +33,9 @@ open class SearchParameters {
|
||||
var searchInUrls = true
|
||||
var searchInGroupNames = false
|
||||
var searchInNotes = true
|
||||
var searchInOther = true
|
||||
var searchInUUIDs = false
|
||||
var searchInTags = true
|
||||
var ignoreCase = true
|
||||
var ignoreExpired = false
|
||||
var excludeExpired = false
|
||||
@@ -44,24 +47,30 @@ open class SearchParameters {
|
||||
* @param source
|
||||
*/
|
||||
constructor(source: SearchParameters) {
|
||||
regularExpression = source.regularExpression
|
||||
searchInTitles = source.searchInTitles
|
||||
searchInUserNames = source.searchInUserNames
|
||||
searchInPasswords = source.searchInPasswords
|
||||
searchInUrls = source.searchInUrls
|
||||
searchInGroupNames = source.searchInGroupNames
|
||||
searchInNotes = source.searchInNotes
|
||||
ignoreCase = source.ignoreCase
|
||||
ignoreExpired = source.ignoreExpired
|
||||
excludeExpired = source.excludeExpired
|
||||
this.regularExpression = source.regularExpression
|
||||
this.searchInTitles = source.searchInTitles
|
||||
this.searchInUserNames = source.searchInUserNames
|
||||
this.searchInPasswords = source.searchInPasswords
|
||||
this.searchInUrls = source.searchInUrls
|
||||
this.searchInGroupNames = source.searchInGroupNames
|
||||
this.searchInNotes = source.searchInNotes
|
||||
this.searchInOther = source.searchInOther
|
||||
this.searchInUUIDs = source.searchInUUIDs
|
||||
this.searchInTags = source.searchInTags
|
||||
this.ignoreCase = source.ignoreCase
|
||||
this.ignoreExpired = source.ignoreExpired
|
||||
this.excludeExpired = source.excludeExpired
|
||||
}
|
||||
|
||||
open fun setupNone() {
|
||||
fun setupNone() {
|
||||
searchInTitles = false
|
||||
searchInUserNames = false
|
||||
searchInPasswords = false
|
||||
searchInUrls = false
|
||||
searchInGroupNames = false
|
||||
searchInNotes = false
|
||||
searchInOther = false
|
||||
searchInUUIDs = false
|
||||
searchInTags = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.search
|
||||
|
||||
class SearchParametersKDBX : SearchParameters {
|
||||
|
||||
var searchInOther = true
|
||||
var searchInUUIDs = false
|
||||
var searchInTags = true
|
||||
|
||||
constructor() : super()
|
||||
|
||||
constructor(searchParametersV4: SearchParametersKDBX) : super(searchParametersV4) {
|
||||
this.searchInOther = searchParametersV4.searchInOther
|
||||
this.searchInUUIDs = searchParametersV4.searchInUUIDs
|
||||
this.searchInTags = searchParametersV4.searchInTags
|
||||
}
|
||||
|
||||
override fun setupNone() {
|
||||
super.setupNone()
|
||||
searchInOther = false
|
||||
searchInUUIDs = false
|
||||
searchInTags = false
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.search.iterator
|
||||
|
||||
abstract class EntrySearchStringIterator : Iterator<String>
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
* Copyright 2020 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
@@ -24,12 +24,10 @@ import com.kunzisoft.keepass.database.search.SearchParameters
|
||||
|
||||
import java.util.NoSuchElementException
|
||||
|
||||
class EntrySearchStringIteratorKDB
|
||||
|
||||
@JvmOverloads
|
||||
constructor(private val mEntry: EntryKDB,
|
||||
private val mSearchParameters: SearchParameters? = SearchParameters())
|
||||
: EntrySearchStringIterator() {
|
||||
class EntrySearchStringIteratorKDB(
|
||||
private val mEntry: EntryKDB,
|
||||
private val mSearchParameters: SearchParameters)
|
||||
: Iterator<String> {
|
||||
|
||||
private var current = 0
|
||||
|
||||
@@ -62,11 +60,6 @@ constructor(private val mEntry: EntryKDB,
|
||||
}
|
||||
|
||||
private fun useSearchParameters() {
|
||||
|
||||
if (mSearchParameters == null) {
|
||||
return
|
||||
}
|
||||
|
||||
var found = false
|
||||
while (!found) {
|
||||
found = when (current) {
|
||||
@@ -84,7 +77,6 @@ constructor(private val mEntry: EntryKDB,
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val title = 0
|
||||
private const val url = 1
|
||||
private const val username = 2
|
||||
|
||||
@@ -20,25 +20,20 @@
|
||||
package com.kunzisoft.keepass.database.search.iterator
|
||||
|
||||
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
|
||||
import com.kunzisoft.keepass.database.search.SearchParametersKDBX
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.database.search.SearchParameters
|
||||
import java.util.*
|
||||
import kotlin.collections.Map.Entry
|
||||
|
||||
class EntrySearchStringIteratorKDBX : EntrySearchStringIterator {
|
||||
class EntrySearchStringIteratorKDBX(
|
||||
entry: EntryKDBX,
|
||||
private val mSearchParameters: SearchParameters)
|
||||
: Iterator<String> {
|
||||
|
||||
private var mCurrent: String? = null
|
||||
private var mSetIterator: Iterator<Entry<String, ProtectedString>>? = null
|
||||
private var mSearchParametersV4: SearchParametersKDBX
|
||||
|
||||
constructor(entry: EntryKDBX) {
|
||||
this.mSearchParametersV4 = SearchParametersKDBX()
|
||||
mSetIterator = entry.fields.entries.iterator()
|
||||
advance()
|
||||
}
|
||||
|
||||
constructor(entry: EntryKDBX, searchParametersV4: SearchParametersKDBX) {
|
||||
this.mSearchParametersV4 = searchParametersV4
|
||||
init {
|
||||
mSetIterator = entry.fields.entries.iterator()
|
||||
advance()
|
||||
}
|
||||
@@ -75,12 +70,12 @@ class EntrySearchStringIteratorKDBX : EntrySearchStringIterator {
|
||||
|
||||
private fun searchInField(key: String): Boolean {
|
||||
return when (key) {
|
||||
EntryKDBX.STR_TITLE -> mSearchParametersV4.searchInTitles
|
||||
EntryKDBX.STR_USERNAME -> mSearchParametersV4.searchInUserNames
|
||||
EntryKDBX.STR_PASSWORD -> mSearchParametersV4.searchInPasswords
|
||||
EntryKDBX.STR_URL -> mSearchParametersV4.searchInUrls
|
||||
EntryKDBX.STR_NOTES -> mSearchParametersV4.searchInNotes
|
||||
else -> mSearchParametersV4.searchInOther
|
||||
EntryKDBX.STR_TITLE -> mSearchParameters.searchInTitles
|
||||
EntryKDBX.STR_USERNAME -> mSearchParameters.searchInUserNames
|
||||
EntryKDBX.STR_PASSWORD -> mSearchParameters.searchInPasswords
|
||||
EntryKDBX.STR_URL -> mSearchParameters.searchInUrls
|
||||
EntryKDBX.STR_NOTES -> mSearchParameters.searchInNotes
|
||||
else -> mSearchParameters.searchInOther
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user