Changeset View
Changeset View
Standalone View
Standalone View
src/search/dolphinquery.cpp
Show All 13 Lines | |||||
14 | * You should have received a copy of the GNU General Public License * | 14 | * You should have received a copy of the GNU General Public License * | ||
15 | * along with this program; if not, write to the * | 15 | * along with this program; if not, write to the * | ||
16 | * Free Software Foundation, Inc., * | 16 | * Free Software Foundation, Inc., * | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #include "dolphinquery.h" | 20 | #include "dolphinquery.h" | ||
21 | 21 | | |||
22 | #include <QRegularExpression> | ||||
23 | | ||||
22 | #include <config-baloo.h> | 24 | #include <config-baloo.h> | ||
23 | #ifdef HAVE_BALOO | 25 | #ifdef HAVE_BALOO | ||
24 | #include <Baloo/Query> | 26 | #include <Baloo/Query> | ||
25 | #endif | 27 | #endif | ||
26 | 28 | | |||
27 | namespace { | 29 | namespace { | ||
28 | /** Checks if a given term in the Baloo::Query::searchString() is a special search term. | 30 | /** Checks if a given term in the Baloo::Query::searchString() is a special search term. | ||
29 | * This is a copy of `DolphinFacetsWidget::isRatingTerm()` method. | 31 | * This is a copy of `DolphinFacetsWidget::isRatingTerm()` method. | ||
30 | */ | 32 | */ | ||
31 | bool isSearchTerm(const QString& term) | 33 | bool isSearchTerm(const QString& term) | ||
32 | { | 34 | { | ||
33 | static const QLatin1String searchTokens[] { | 35 | static const QLatin1String searchTokens[] { | ||
34 | QLatin1String("modified>="), | 36 | QLatin1String("modified>="), | ||
35 | QLatin1String("rating>=") | 37 | QLatin1String("rating>=") | ||
36 | }; | 38 | }; | ||
37 | 39 | | |||
38 | for (const auto &searchToken : searchTokens) { | 40 | for (const auto &searchToken : searchTokens) { | ||
39 | if (term.startsWith(searchToken)) { | 41 | if (term.startsWith(searchToken)) { | ||
40 | return true; | 42 | return true; | ||
41 | } | 43 | } | ||
42 | } | 44 | } | ||
43 | return false; | 45 | return false; | ||
44 | } | 46 | } | ||
47 | | ||||
48 | QString stripQuotes(const QString& text) | ||||
elvisangelaccio: Coding style: opening brace to the next line | |||||
49 | { | ||||
50 | QString cleanedText = text; | ||||
51 | if (!cleanedText.isEmpty() && cleanedText.at(0) == QLatin1Char('"')) { | ||||
52 | cleanedText = cleanedText.mid(1); | ||||
53 | } | ||||
54 | if (!cleanedText.isEmpty() && cleanedText.back() == QLatin1Char('"')) { | ||||
55 | cleanedText = cleanedText.mid(0, cleanedText.size() - 1); | ||||
56 | } | ||||
57 | return cleanedText; | ||||
58 | } | ||||
59 | | ||||
elvisangelaccio: Same here | |||||
60 | QStringList splitOutsideQuotes(const QString& text) | ||||
61 | { | ||||
62 | const QRegularExpression subTermsRegExp("([^ ]*\"[^\"]*\"|(?<= |^)[^ ]+(?= |$))"); | ||||
63 | auto subTermsMatchIterator = subTermsRegExp.globalMatch(text); | ||||
64 | | ||||
elvisangelaccio: Space after `while` | |||||
65 | QStringList textParts; | ||||
66 | while (subTermsMatchIterator.hasNext()) { | ||||
67 | textParts << subTermsMatchIterator.next().captured(0); | ||||
68 | } | ||||
69 | return textParts; | ||||
70 | } | ||||
45 | } | 71 | } | ||
46 | 72 | | |||
47 | DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl) | 73 | DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl) | ||
48 | { | 74 | { | ||
49 | DolphinQuery model; | 75 | DolphinQuery model; | ||
50 | model.m_searchUrl = searchUrl; | 76 | model.m_searchUrl = searchUrl; | ||
51 | 77 | | |||
52 | #ifdef HAVE_BALOO | 78 | #ifdef HAVE_BALOO | ||
53 | const Baloo::Query query = Baloo::Query::fromSearchUrl(searchUrl); | 79 | const Baloo::Query query = Baloo::Query::fromSearchUrl(searchUrl); | ||
54 | 80 | | |||
55 | model.m_includeFolder = query.includeFolder(); | 81 | model.m_includeFolder = query.includeFolder(); | ||
56 | 82 | | |||
57 | const QStringList types = query.types(); | 83 | const QStringList types = query.types(); | ||
58 | model.m_fileType = types.isEmpty() ? QString() : types.first(); | 84 | model.m_fileType = types.isEmpty() ? QString() : types.first(); | ||
59 | 85 | | |||
60 | QStringList textParts; | 86 | QStringList textParts; | ||
87 | QString fileName; | ||||
61 | 88 | | |||
62 | const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts); | 89 | const QStringList subTerms = splitOutsideQuotes(query.searchString()); | ||
63 | foreach (const QString& subTerm, subTerms) { | 90 | foreach (const QString& subTerm, subTerms) { | ||
64 | QString value; | | |||
65 | if (subTerm.startsWith(QLatin1String("filename:"))) { | 91 | if (subTerm.startsWith(QLatin1String("filename:"))) { | ||
66 | value = subTerm.mid(9); | 92 | fileName = stripQuotes(subTerm.mid(9)); | ||
93 | if (!fileName.isEmpty()) { | ||||
94 | model.m_hasFileName = true; | ||||
95 | } | ||||
96 | continue; | ||||
67 | } else if (isSearchTerm(subTerm)) { | 97 | } else if (isSearchTerm(subTerm)) { | ||
68 | model.m_searchTerms << subTerm; | 98 | model.m_searchTerms << subTerm; | ||
69 | continue; | 99 | continue; | ||
70 | } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) { | 100 | } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) { | ||
71 | continue; | 101 | continue; | ||
72 | } else { | 102 | } else { | ||
73 | value = subTerm; | 103 | const QString cleanedTerm = stripQuotes(subTerm); | ||
104 | if (!cleanedTerm.isEmpty()) { | ||||
105 | textParts << cleanedTerm; | ||||
106 | model.m_hasContentSearch = true; | ||||
74 | } | 107 | } | ||
75 | | ||||
76 | if (!value.isEmpty() && value.at(0) == QLatin1Char('"')) { | | |||
77 | value = value.mid(1); | | |||
78 | } | 108 | } | ||
79 | if (!value.isEmpty() && value.back() == QLatin1Char('"')) { | | |||
80 | value = value.mid(0, value.size() - 1); | | |||
81 | } | 109 | } | ||
82 | if (!value.isEmpty()) { | 110 | | ||
83 | textParts << value; | 111 | if (model.m_hasFileName) { | ||
112 | if (model.m_hasContentSearch) { | ||||
113 | textParts << QStringLiteral("filename:\"%1\"").arg(fileName); | ||||
elvisangelaccio: `QStringLiteral` | |||||
114 | } else { | ||||
115 | textParts << fileName; | ||||
84 | } | 116 | } | ||
85 | } | 117 | } | ||
86 | 118 | | |||
87 | model.m_searchText = textParts.join(QLatin1Char(' ')); | 119 | model.m_searchText = textParts.join(QLatin1Char(' ')); | ||
88 | 120 | | |||
89 | #endif | 121 | #endif | ||
90 | return model; | 122 | return model; | ||
91 | } | 123 | } | ||
Show All 17 Lines | |||||
109 | { | 141 | { | ||
110 | return m_searchTerms; | 142 | return m_searchTerms; | ||
111 | } | 143 | } | ||
112 | 144 | | |||
113 | QString DolphinQuery::includeFolder() const | 145 | QString DolphinQuery::includeFolder() const | ||
114 | { | 146 | { | ||
115 | return m_includeFolder; | 147 | return m_includeFolder; | ||
116 | } | 148 | } | ||
149 | | ||||
150 | bool DolphinQuery::hasContentSearch() const | ||||
151 | { | ||||
152 | return m_hasContentSearch; | ||||
153 | } | ||||
154 | | ||||
155 | bool DolphinQuery::hasFileName() const | ||||
156 | { | ||||
157 | return m_hasFileName; | ||||
158 | } |
Coding style: opening brace to the next line