Skip to content

Commit

Permalink
Allow abbreviation of search field names
Browse files Browse the repository at this point in the history
This allows `t:word` instead of `title:word` and `p:word` instead of `password:word`, and so on.  The rule is that an abbreviated name expands to the first field name that starts with it, with exceptions
`u:` expanding to `username:` instead of `url:` and `pw:` expanding to `password:`.
  • Loading branch information
knu authored and droidmonkey committed Sep 3, 2019
1 parent 0a3b19e commit a38008a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
39 changes: 21 additions & 18 deletions src/core/EntrySearcher.cpp
Expand Up @@ -173,6 +173,17 @@ bool EntrySearcher::searchEntryImpl(Entry* entry)

void EntrySearcher::parseSearchTerms(const QString& searchString)
{
static const QList<QPair<QString, Field>> 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()) {
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/gui/SearchHelpWidget.ui
Expand Up @@ -237,21 +237,21 @@
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string notr="true">username</string>
<string notr="true">username (u)</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_11">
<property name="text">
<string notr="true">password</string>
<string notr="true">password (p, pw)</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string notr="true">title</string>
<string notr="true">title (t)</string>
</property>
</widget>
</item>
Expand All @@ -265,21 +265,21 @@
<item row="0" column="1">
<widget class="QLabel" name="label_5">
<property name="text">
<string notr="true">notes</string>
<string notr="true">notes (n)</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_12">
<property name="text">
<string notr="true">attribute</string>
<string notr="true">attribute (attr)</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_14">
<property name="text">
<string notr="true">attachment</string>
<string notr="true">attachment (attach)</string>
</property>
</widget>
</item>
Expand Down
17 changes: 16 additions & 1 deletion tests/TestEntrySearcher.cpp
Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit a38008a

Please sign in to comment.