mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Remove unused search code
This commit is contained in:
@@ -557,7 +557,6 @@ class Database {
|
||||
searchInOther = true
|
||||
searchInUUIDs = false
|
||||
searchInTags = false
|
||||
ignoreCase = true
|
||||
}, omitBackup, max)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,10 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.element.entry
|
||||
|
||||
import com.kunzisoft.keepass.database.action.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.element.Entry
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
|
||||
import com.kunzisoft.keepass.database.search.SearchHelper
|
||||
import com.kunzisoft.keepass.database.search.SearchParameters
|
||||
import com.kunzisoft.keepass.utils.UuidUtil
|
||||
import java.util.*
|
||||
|
||||
class FieldReferencesEngine {
|
||||
@@ -122,9 +121,17 @@ class FieldReferencesEngine {
|
||||
val scan = Character.toUpperCase(ref[2])
|
||||
val wanted = Character.toUpperCase(ref[0])
|
||||
|
||||
val searchParameters = SearchParameters()
|
||||
searchParameters.setupNone()
|
||||
|
||||
val searchParameters = SearchParameters().apply {
|
||||
searchInTitles = false
|
||||
searchInUserNames = false
|
||||
searchInPasswords = false
|
||||
searchInUrls = false
|
||||
searchInNotes = false
|
||||
searchInOTP = false
|
||||
searchInOther = false
|
||||
searchInUUIDs = false
|
||||
searchInTags = false
|
||||
}
|
||||
searchParameters.searchQuery = ref.substring(4)
|
||||
when (scan) {
|
||||
'T' -> searchParameters.searchInTitles = true
|
||||
@@ -137,13 +144,13 @@ class FieldReferencesEngine {
|
||||
else -> return null
|
||||
}
|
||||
|
||||
val list = ArrayList<EntryKDBX>()
|
||||
searchEntries(contextKDBX, searchParameters, list)
|
||||
|
||||
return if (list.size > 0) {
|
||||
TargetResult(list[0], wanted)
|
||||
var entrySearch: EntryKDBX? = null
|
||||
contextKDBX.databaseKDBX?.rootGroup?.let { root ->
|
||||
entrySearch = root.searchChildEntry { entry -> SearchHelper.searchInEntry(Entry(entry), searchParameters) }
|
||||
}
|
||||
return if (entrySearch != null) {
|
||||
TargetResult(entrySearch, wanted)
|
||||
} else null
|
||||
|
||||
}
|
||||
|
||||
private fun addRefsToCache(ref: String?, value: String?, ctx: SprContextKDBX?) {
|
||||
@@ -170,100 +177,6 @@ class FieldReferencesEngine {
|
||||
return newText
|
||||
}
|
||||
|
||||
private fun searchEntries(contextKDBX: SprContextKDBX,
|
||||
searchParameters: SearchParameters?,
|
||||
listStorage: MutableList<EntryKDBX>?) {
|
||||
|
||||
val root = contextKDBX.databaseKDBX?.rootGroup
|
||||
if (searchParameters == null) {
|
||||
return
|
||||
}
|
||||
if (listStorage == null) {
|
||||
return
|
||||
}
|
||||
|
||||
val terms = splitStringTerms(searchParameters.searchQuery)
|
||||
if (terms.size <= 1 || searchParameters.regularExpression) {
|
||||
root!!.doForEachChild(EntryKDBXSearchHandler(searchParameters, listStorage), null)
|
||||
return
|
||||
}
|
||||
|
||||
// Search longest term first
|
||||
val stringLengthComparator = Comparator<String> { lhs, rhs -> lhs.length - rhs.length }
|
||||
Collections.sort(terms, stringLengthComparator)
|
||||
|
||||
val fullSearch = searchParameters.searchQuery
|
||||
var childEntries: List<EntryKDBX>? = root!!.getChildEntries()
|
||||
for (i in terms.indices) {
|
||||
val pgNew = ArrayList<EntryKDBX>()
|
||||
|
||||
searchParameters.searchQuery = terms[i]
|
||||
|
||||
var negate = false
|
||||
if (searchParameters.searchQuery.startsWith("-")) {
|
||||
searchParameters.searchQuery = searchParameters.searchQuery.substring(1)
|
||||
negate = searchParameters.searchQuery.isNotEmpty()
|
||||
}
|
||||
|
||||
if (!root.doForEachChild(EntryKDBXSearchHandler(searchParameters, pgNew), null)) {
|
||||
childEntries = null
|
||||
break
|
||||
}
|
||||
|
||||
childEntries = if (negate) {
|
||||
val complement = ArrayList<EntryKDBX>()
|
||||
for (entry in childEntries!!) {
|
||||
if (!pgNew.contains(entry)) {
|
||||
complement.add(entry)
|
||||
}
|
||||
}
|
||||
complement
|
||||
} else {
|
||||
pgNew
|
||||
}
|
||||
}
|
||||
|
||||
if (childEntries != null) {
|
||||
listStorage.addAll(childEntries)
|
||||
}
|
||||
searchParameters.searchQuery = fullSearch
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a list of String by split text when ' ', '\t', '\r' or '\n' is found
|
||||
*/
|
||||
private fun splitStringTerms(text: String?): List<String> {
|
||||
val list = ArrayList<String>()
|
||||
if (text == null) {
|
||||
return list
|
||||
}
|
||||
|
||||
val stringBuilder = StringBuilder()
|
||||
var quoted = false
|
||||
|
||||
for (element in text) {
|
||||
|
||||
if ((element == ' ' || element == '\t' || element == '\r' || element == '\n') && !quoted) {
|
||||
|
||||
val len = stringBuilder.length
|
||||
when {
|
||||
len > 0 -> {
|
||||
list.add(stringBuilder.toString())
|
||||
stringBuilder.delete(0, len)
|
||||
}
|
||||
element == '\"' -> quoted = !quoted
|
||||
else -> stringBuilder.append(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stringBuilder.isNotEmpty()) {
|
||||
list.add(stringBuilder.toString())
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
inner class TargetResult(var entry: EntryKDBX?, var wanted: Char)
|
||||
|
||||
private inner class SprContextKDBX {
|
||||
@@ -284,77 +197,6 @@ class FieldReferencesEngine {
|
||||
}
|
||||
}
|
||||
|
||||
private class EntryKDBXSearchHandler(private val mSearchParametersKDBX: SearchParameters,
|
||||
private val mListStorage: MutableList<EntryKDBX>)
|
||||
: NodeHandler<EntryKDBX>() {
|
||||
|
||||
override fun operate(node: EntryKDBX): Boolean {
|
||||
if (mSearchParametersKDBX.excludeExpired
|
||||
&& node.isCurrentlyExpires) {
|
||||
return true
|
||||
}
|
||||
if (searchStrings(node)) {
|
||||
mListStorage.add(node)
|
||||
return true
|
||||
}
|
||||
if (searchInGroupNames(node)) {
|
||||
mListStorage.add(node)
|
||||
return true
|
||||
}
|
||||
if (searchInUUID(node)) {
|
||||
mListStorage.add(node)
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun searchStrings(entry: EntryKDBX): Boolean {
|
||||
var searchFound = false
|
||||
// Search all strings in the KDBX entry
|
||||
EntryFieldsLoop@ for((key, value) in entry.fields) {
|
||||
if (entryKDBXKeyIsAllowedToSearch(key, mSearchParametersKDBX)) {
|
||||
val currentString = value.toString()
|
||||
if (SearchHelper.checkSearchQuery(currentString, mSearchParametersKDBX)) {
|
||||
searchFound = true
|
||||
break@EntryFieldsLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
return searchFound
|
||||
}
|
||||
|
||||
private fun entryKDBXKeyIsAllowedToSearch(key: String, searchParameters: SearchParameters): Boolean {
|
||||
return when (key) {
|
||||
EntryKDBX.STR_TITLE -> searchParameters.searchInTitles
|
||||
EntryKDBX.STR_USERNAME -> searchParameters.searchInUserNames
|
||||
EntryKDBX.STR_PASSWORD -> searchParameters.searchInPasswords
|
||||
EntryKDBX.STR_URL -> searchParameters.searchInUrls
|
||||
EntryKDBX.STR_NOTES -> searchParameters.searchInNotes
|
||||
else -> searchParameters.searchInOther
|
||||
}
|
||||
}
|
||||
|
||||
private fun searchInGroupNames(entry: EntryKDBX): Boolean {
|
||||
if (mSearchParametersKDBX.searchInGroupNames) {
|
||||
val parent = entry.parent
|
||||
if (parent != null) {
|
||||
return parent.title
|
||||
.contains(mSearchParametersKDBX.searchQuery,
|
||||
mSearchParametersKDBX.ignoreCase)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun searchInUUID(entry: EntryKDBX): Boolean {
|
||||
if (mSearchParametersKDBX.searchInUUIDs) {
|
||||
return UuidUtil.toHexString(entry.id)
|
||||
.contains(mSearchParametersKDBX.searchQuery, true)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val MAX_RECURSION_DEPTH = 12
|
||||
private const val STR_REF_START = "{REF:"
|
||||
|
||||
@@ -67,6 +67,26 @@ interface GroupVersionedInterface<Group: GroupVersionedInterface<Group, Entry>,
|
||||
return true
|
||||
}
|
||||
|
||||
fun searchChildEntry(criteria: (entry: Entry) -> Boolean): Entry? {
|
||||
return searchChildEntry(this, criteria)
|
||||
}
|
||||
|
||||
private fun searchChildEntry(rootGroup: GroupVersionedInterface<Group, Entry>,
|
||||
criteria: (entry: Entry) -> Boolean): Entry? {
|
||||
for (childEntry in rootGroup.getChildEntries()) {
|
||||
if (criteria.invoke(childEntry)) {
|
||||
return childEntry
|
||||
}
|
||||
}
|
||||
for (group in rootGroup.getChildGroups()) {
|
||||
val searchChildEntry = searchChildEntry(group, criteria)
|
||||
if (searchChildEntry != null) {
|
||||
return searchChildEntry
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun searchChildGroup(criteria: (group: Group) -> Boolean): Group? {
|
||||
return searchChildGroup(this, criteria)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ import com.kunzisoft.keepass.model.SearchInfo
|
||||
import com.kunzisoft.keepass.otp.OtpEntryFields.OTP_FIELD
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
||||
import com.kunzisoft.keepass.utils.StringUtil.flattenToAscii
|
||||
import com.kunzisoft.keepass.utils.UuidUtil
|
||||
|
||||
class SearchHelper {
|
||||
|
||||
@@ -76,11 +76,7 @@ class SearchHelper {
|
||||
private fun entryContainsString(database: Database,
|
||||
entry: Entry,
|
||||
searchParameters: SearchParameters): Boolean {
|
||||
val searchQuery = searchParameters.searchQuery
|
||||
// Entry don't contains string if the search string is empty
|
||||
if (searchQuery.isEmpty())
|
||||
return false
|
||||
|
||||
// To search in field references
|
||||
database.startManageEntry(entry)
|
||||
// Search all strings in the entry
|
||||
val searchFound = searchInEntry(entry, searchParameters)
|
||||
@@ -89,41 +85,6 @@ class SearchHelper {
|
||||
return searchFound
|
||||
}
|
||||
|
||||
private fun searchInEntry(entry: Entry,
|
||||
searchParameters: SearchParameters): Boolean {
|
||||
// Search all strings in the KDBX entry
|
||||
if (searchParameters.searchInTitles) {
|
||||
if (checkSearchQuery(entry.title, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInUserNames) {
|
||||
if (checkSearchQuery(entry.username, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInPasswords) {
|
||||
if (checkSearchQuery(entry.password, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInUrls) {
|
||||
if (checkSearchQuery(entry.url, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInNotes) {
|
||||
if (checkSearchQuery(entry.notes, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInOther) {
|
||||
entry.getExtraFields().forEach { field ->
|
||||
if (field.name != OTP_FIELD
|
||||
|| (field.name == OTP_FIELD && searchParameters.searchInOTP)) {
|
||||
if (checkSearchQuery(field.protectedValue.toString(), searchParameters))
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val MAX_SEARCH_ENTRY = 10
|
||||
|
||||
@@ -162,15 +123,63 @@ class SearchHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun checkSearchQuery(stringToCheck: String, searchParameters: SearchParameters): Boolean {
|
||||
if (stringToCheck.isNotEmpty()
|
||||
&& stringToCheck
|
||||
.flattenToAscii()
|
||||
.contains(searchParameters.searchQuery.flattenToAscii(),
|
||||
searchParameters.ignoreCase)) {
|
||||
return true
|
||||
/**
|
||||
* Return true if the search query in search parameters is found in available parameters
|
||||
*/
|
||||
fun searchInEntry(entry: Entry,
|
||||
searchParameters: SearchParameters): Boolean {
|
||||
val searchQuery = searchParameters.searchQuery
|
||||
// Entry don't contains string if the search string is empty
|
||||
if (searchQuery.isEmpty())
|
||||
return false
|
||||
|
||||
// Search all strings in the KDBX entry
|
||||
if (searchParameters.searchInTitles) {
|
||||
if (checkSearchQuery(entry.title, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInUserNames) {
|
||||
if (checkSearchQuery(entry.username, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInPasswords) {
|
||||
if (checkSearchQuery(entry.password, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInUrls) {
|
||||
if (checkSearchQuery(entry.url, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInNotes) {
|
||||
if (checkSearchQuery(entry.notes, searchParameters))
|
||||
return true
|
||||
}
|
||||
if (searchParameters.searchInUUIDs) {
|
||||
return UuidUtil.toHexString(entry.nodeId.id)
|
||||
.contains(searchQuery, true)
|
||||
}
|
||||
if (searchParameters.searchInOther) {
|
||||
entry.getExtraFields().forEach { field ->
|
||||
if (field.name != OTP_FIELD
|
||||
|| (field.name == OTP_FIELD && searchParameters.searchInOTP)) {
|
||||
if (checkSearchQuery(field.protectedValue.toString(), searchParameters))
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun checkSearchQuery(stringToCheck: String, searchParameters: SearchParameters): Boolean {
|
||||
/*
|
||||
// TODO Search settings
|
||||
var regularExpression = false
|
||||
var ignoreCase = true
|
||||
var flattenToASCII = true
|
||||
var excludeExpired = false
|
||||
*/
|
||||
return stringToCheck.isNotEmpty()
|
||||
&& stringToCheck.contains(searchParameters.searchQuery, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,57 +23,15 @@ package com.kunzisoft.keepass.database.search
|
||||
* Parameters for searching strings in the database.
|
||||
*/
|
||||
class SearchParameters {
|
||||
|
||||
var searchQuery: String = ""
|
||||
|
||||
var regularExpression = false
|
||||
var searchInTitles = true
|
||||
var searchInUserNames = true
|
||||
var searchInPasswords = false
|
||||
var searchInUrls = true
|
||||
var searchInGroupNames = false
|
||||
var searchInNotes = true
|
||||
var searchInOTP = false
|
||||
var searchInOther = true
|
||||
var searchInUUIDs = false
|
||||
var searchInTags = true
|
||||
var ignoreCase = true
|
||||
var ignoreExpired = false
|
||||
var excludeExpired = false
|
||||
|
||||
constructor()
|
||||
|
||||
/**
|
||||
* Copy search parameters
|
||||
* @param source
|
||||
*/
|
||||
constructor(source: SearchParameters) {
|
||||
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.searchInOTP = source.searchInOTP
|
||||
this.searchInOther = source.searchInOther
|
||||
this.searchInUUIDs = source.searchInUUIDs
|
||||
this.searchInTags = source.searchInTags
|
||||
this.ignoreCase = source.ignoreCase
|
||||
this.ignoreExpired = source.ignoreExpired
|
||||
this.excludeExpired = source.excludeExpired
|
||||
}
|
||||
|
||||
fun setupNone() {
|
||||
searchInTitles = false
|
||||
searchInUserNames = false
|
||||
searchInPasswords = false
|
||||
searchInUrls = false
|
||||
searchInGroupNames = false
|
||||
searchInNotes = false
|
||||
searchInOTP = false
|
||||
searchInOther = false
|
||||
searchInUUIDs = false
|
||||
searchInTags = false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user