┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/search
diff options
context:
space:
mode:
authorIsmael Asensio <[email protected]>2019-12-21 19:14:17 +0100
committerIsmael Asensio <[email protected]>2019-12-21 19:17:18 +0100
commit616d4e6bdd707ac819707263255809df576a8c2b (patch)
tree832f183210281c002423d7cac2ea1d649824ddac /src/search
parent8c386bdb4733fc12c5e8f411d59c79f97e412f9e (diff)
fix(search): Correctly parse filename and/or content search
Summary: Currently, the search url parsing does not detect if the search is based on Content or Filename, and it just keeps the last selection which can be inconsistent with the actual search. This patch add such detection, and since an advanced user can combine filename and content search (using the keyword `filename:`), now the parsing detects both items and handles the four possible cases: | Content | Filename | Search text | Search type | |---|---|------------------------|------------------| | T | T | abc filename:"xyz" | Content | | T | F | abc | Content | | F | T | xyz | Filename | | F | F | | do not set | Depends on: D25260 Test Plan: `bin/dolphinquerytest`: Added new test cases for searches with content text and/or filename Reviewers: elvisangelaccio, bruns, #dolphin Reviewed By: elvisangelaccio, #dolphin Subscribers: kfm-devel Tags: #dolphin Differential Revision: https://phabricator.kde.org/D25416
Diffstat (limited to 'src/search')
-rw-r--r--src/search/dolphinquery.cpp66
-rw-r--r--src/search/dolphinquery.h6
-rw-r--r--src/search/dolphinsearchbox.cpp6
3 files changed, 66 insertions, 12 deletions
diff --git a/src/search/dolphinquery.cpp b/src/search/dolphinquery.cpp
index 92694c093..4d5f8e132 100644
--- a/src/search/dolphinquery.cpp
+++ b/src/search/dolphinquery.cpp
@@ -19,6 +19,8 @@
#include "dolphinquery.h"
+#include <QRegularExpression>
+
#include <config-baloo.h>
#ifdef HAVE_BALOO
#include <Baloo/Query>
@@ -43,6 +45,30 @@ namespace {
}
return false;
}
+
+ QString stripQuotes(const QString& text)
+ {
+ QString cleanedText = text;
+ if (!cleanedText.isEmpty() && cleanedText.at(0) == QLatin1Char('"')) {
+ cleanedText = cleanedText.mid(1);
+ }
+ if (!cleanedText.isEmpty() && cleanedText.back() == QLatin1Char('"')) {
+ cleanedText = cleanedText.mid(0, cleanedText.size() - 1);
+ }
+ return cleanedText;
+ }
+
+ QStringList splitOutsideQuotes(const QString& text)
+ {
+ const QRegularExpression subTermsRegExp("([^ ]*\"[^\"]*\"|(?<= |^)[^ ]+(?= |$))");
+ auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);
+
+ QStringList textParts;
+ while (subTermsMatchIterator.hasNext()) {
+ textParts << subTermsMatchIterator.next().captured(0);
+ }
+ return textParts;
+ }
}
DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl)
@@ -59,29 +85,35 @@ DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl)
model.m_fileType = types.isEmpty() ? QString() : types.first();
QStringList textParts;
+ QString fileName;
- const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
+ const QStringList subTerms = splitOutsideQuotes(query.searchString());
foreach (const QString& subTerm, subTerms) {
- QString value;
if (subTerm.startsWith(QLatin1String("filename:"))) {
- value = subTerm.mid(9);
+ fileName = stripQuotes(subTerm.mid(9));
+ if (!fileName.isEmpty()) {
+ model.m_hasFileName = true;
+ }
+ continue;
} else if (isSearchTerm(subTerm)) {
model.m_searchTerms << subTerm;
continue;
} else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
continue;
} else {
- value = subTerm;
+ const QString cleanedTerm = stripQuotes(subTerm);
+ if (!cleanedTerm.isEmpty()) {
+ textParts << cleanedTerm;
+ model.m_hasContentSearch = true;
+ }
}
+ }
- if (!value.isEmpty() && value.at(0) == QLatin1Char('"')) {
- value = value.mid(1);
- }
- if (!value.isEmpty() && value.back() == QLatin1Char('"')) {
- value = value.mid(0, value.size() - 1);
- }
- if (!value.isEmpty()) {
- textParts << value;
+ if (model.m_hasFileName) {
+ if (model.m_hasContentSearch) {
+ textParts << QStringLiteral("filename:\"%1\"").arg(fileName);
+ } else {
+ textParts << fileName;
}
}
@@ -115,3 +147,13 @@ QString DolphinQuery::includeFolder() const
{
return m_includeFolder;
}
+
+bool DolphinQuery::hasContentSearch() const
+{
+ return m_hasContentSearch;
+}
+
+bool DolphinQuery::hasFileName() const
+{
+ return m_hasFileName;
+}
diff --git a/src/search/dolphinquery.h b/src/search/dolphinquery.h
index e60008e3b..544f246bc 100644
--- a/src/search/dolphinquery.h
+++ b/src/search/dolphinquery.h
@@ -48,6 +48,10 @@ public:
/** @return Baloo::Query::includeFolder(), that is, the initial directory
* for the query or an empty string if its a global search" */
QString includeFolder() const;
+ /** @return whether the query includes search in file content */
+ bool hasContentSearch() const;
+ /** @return whether the query includes a filter by fileName */
+ bool hasFileName() const;
private:
QUrl m_searchUrl;
@@ -55,6 +59,8 @@ private:
QString m_fileType;
QStringList m_searchTerms;
QString m_includeFolder;
+ bool m_hasContentSearch = false;
+ bool m_hasFileName = false;
};
#endif //DOLPHINQUERY_H
diff --git a/src/search/dolphinsearchbox.cpp b/src/search/dolphinsearchbox.cpp
index 16f17bbcd..302081d7d 100644
--- a/src/search/dolphinsearchbox.cpp
+++ b/src/search/dolphinsearchbox.cpp
@@ -517,6 +517,12 @@ void DolphinSearchBox::updateFromQuery(const DolphinQuery& query)
setText(query.text());
+ if (query.hasContentSearch()) {
+ m_contentButton->setChecked(true);
+ } else if (query.hasFileName()) {
+ m_fileNameButton->setChecked(true);
+ }
+
m_facetsWidget->resetOptions();
m_facetsWidget->setFacetType(query.type());
const QStringList searchTerms = query.searchTerms();