diff --git a/src/core/EntrySearcher.cpp b/src/core/EntrySearcher.cpp index cf6a4b5f3..79e43e71a 100644 --- a/src/core/EntrySearcher.cpp +++ b/src/core/EntrySearcher.cpp @@ -173,6 +173,17 @@ bool EntrySearcher::searchEntryImpl(Entry* entry) void EntrySearcher::parseSearchTerms(const QString& searchString) { + static const QList> fieldnames{ + {QStringLiteral("attachment"), Field::Attachment}, + {QStringLiteral("attribute"), Field::AttributeKey}, + {QStringLiteral("notes"), Field::Notes}, + {QStringLiteral("pw"), Field::Password}, + {QStringLiteral("password"), Field::Password}, + {QStringLiteral("title"), Field::Title}, + {QStringLiteral("u"), Field::Username}, // u: stands for username rather than url + {QStringLiteral("url"), Field::Url}, + {QStringLiteral("username"), Field::Username}}; + m_searchTerms.clear(); auto results = m_termParser.globalMatch(searchString); while (results.hasNext()) { @@ -201,32 +212,24 @@ void EntrySearcher::parseSearchTerms(const QString& searchString) term->exclude = mods.contains("-") || mods.contains("!"); // Determine the field to search + term->field = Field::Undefined; + QString field = result.captured(2); if (!field.isEmpty()) { - auto cs = Qt::CaseInsensitive; - if (field.compare("title", cs) == 0) { - term->field = Field::Title; - } else if (field.startsWith("user", cs)) { - term->field = Field::Username; - } else if (field.startsWith("pass", cs)) { - term->field = Field::Password; - } else if (field.compare("url", cs) == 0) { - term->field = Field::Url; - } else if (field.compare("notes", cs) == 0) { - term->field = Field::Notes; - } else if (field.startsWith("attr", cs)) { - term->field = Field::AttributeKey; - } else if (field.startsWith("attach", cs)) { - term->field = Field::Attachment; - } else if (field.startsWith("_", cs)) { + if (field.startsWith("_", Qt::CaseInsensitive)) { term->field = Field::AttributeValue; // searching a custom attribute // in this case term->word is the attribute key (removing the leading "_") // and term->regex is used to match attribute value term->word = field.mid(1); + } else { + for (const auto& pair : fieldnames) { + if (pair.first.startsWith(field, Qt::CaseInsensitive)) { + term->field = pair.second; + break; + } + } } - } else { - term->field = Field::Undefined; } m_searchTerms.append(term); diff --git a/src/gui/SearchHelpWidget.ui b/src/gui/SearchHelpWidget.ui index e4d77b1a0..45e0d0bc6 100644 --- a/src/gui/SearchHelpWidget.ui +++ b/src/gui/SearchHelpWidget.ui @@ -237,21 +237,21 @@ - username + username (u) - password + password (p, pw) - title + title (t) @@ -265,21 +265,21 @@ - notes + notes (n) - attribute + attribute (attr) - attachment + attachment (attach) diff --git a/tests/TestEntrySearcher.cpp b/tests/TestEntrySearcher.cpp index 5a14481ae..7b129df17 100644 --- a/tests/TestEntrySearcher.cpp +++ b/tests/TestEntrySearcher.cpp @@ -96,7 +96,7 @@ void TestEntrySearcher::testSearch() e3->setGroup(group3); Entry* e3b = new Entry(); - e3b->setTitle("test search test"); + e3b->setTitle("test search test 123"); e3b->setUsername("test@email.com"); e3b->setPassword("realpass"); e3b->setGroup(group3); @@ -108,16 +108,31 @@ void TestEntrySearcher::testSearch() m_searchResult = m_entrySearcher.search("search term", m_rootGroup); QCOMPARE(m_searchResult.count(), 3); + m_searchResult = m_entrySearcher.search("123", m_rootGroup); + QCOMPARE(m_searchResult.count(), 2); + m_searchResult = m_entrySearcher.search("search term", group211); QCOMPARE(m_searchResult.count(), 1); // Test advanced search terms + m_searchResult = m_entrySearcher.search("title:123", m_rootGroup); + QCOMPARE(m_searchResult.count(), 1); + + m_searchResult = m_entrySearcher.search("t:123", m_rootGroup); + QCOMPARE(m_searchResult.count(), 1); + m_searchResult = m_entrySearcher.search("password:testpass", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); + m_searchResult = m_entrySearcher.search("pw:testpass", m_rootGroup); + QCOMPARE(m_searchResult.count(), 1); + m_searchResult = m_entrySearcher.search("!user:email.com", m_rootGroup); QCOMPARE(m_searchResult.count(), 5); + m_searchResult = m_entrySearcher.search("!u:email.com", m_rootGroup); + QCOMPARE(m_searchResult.count(), 5); + m_searchResult = m_entrySearcher.search("*user:\".*@.*\\.com\"", m_rootGroup); QCOMPARE(m_searchResult.count(), 1);