]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinquery.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / search / dolphinquery.cpp
1 /*
2 SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@mgmail.com>
3 SPDX-FileCopyrightText: 2025 Felix Ernst <felixernst@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "dolphinquery.h"
9
10 #include "config-dolphin.h"
11 #if HAVE_BALOO
12 #include <Baloo/IndexerConfig>
13 #include <Baloo/Query>
14 #endif
15 #include "dolphinplacesmodelsingleton.h"
16
17 #include <KFileMetaData/TypeInfo>
18 #include <KLocalizedString>
19 #include <KShell>
20
21 #include <QRegularExpression>
22 #include <QUrlQuery>
23
24 using namespace Search;
25
26 bool Search::isSupportedSearchScheme(const QString &urlScheme)
27 {
28 static const QStringList supportedSchemes = {
29 QStringLiteral("filenamesearch"),
30 QStringLiteral("baloosearch"),
31 QStringLiteral("tags"),
32 };
33
34 return supportedSchemes.contains(urlScheme);
35 }
36
37 bool g_testMode = false;
38
39 bool Search::isIndexingEnabledIn(QUrl directory)
40 {
41 if (g_testMode) {
42 return true; // For unit-testing, let's pretend everything is indexed correctly.
43 }
44
45 #if HAVE_BALOO
46 const Baloo::IndexerConfig searchInfo;
47 return searchInfo.fileIndexingEnabled() && !directory.isEmpty() && searchInfo.shouldBeIndexed(directory.toLocalFile());
48 #else
49 Q_UNUSED(directory)
50 return false;
51 #endif
52 }
53
54 bool Search::isContentIndexingEnabled()
55 {
56 if (g_testMode) {
57 return true; // For unit-testing, let's pretend everything is indexed correctly.
58 }
59
60 #if HAVE_BALOO
61 return !Baloo::IndexerConfig{}.onlyBasicIndexing();
62 #else
63 return false;
64 #endif
65 }
66
67 namespace
68 {
69 /** The path to be passed so Baloo searches everywhere. */
70 constexpr auto balooSearchEverywherePath = QLatin1String("");
71 /** The path to be passed so Filenamesearch searches everywhere. */
72 constexpr auto filenamesearchEverywherePath = QLatin1String("file:///");
73
74 #if HAVE_BALOO
75 /** Checks if a given term in the Baloo::Query::searchString() is a special search term
76 * @return: the specific search token of the term, or an empty QString() if none is found
77 */
78 QString searchTermToken(const QString &term)
79 {
80 static const QLatin1String searchTokens[]{QLatin1String("filename:"),
81 QLatin1String("modified>="),
82 QLatin1String("modified>"),
83 QLatin1String("rating>="),
84 QLatin1String("tag:"),
85 QLatin1String("tag=")};
86
87 for (const auto &searchToken : searchTokens) {
88 if (term.startsWith(searchToken)) {
89 return searchToken;
90 }
91 }
92 return QString();
93 }
94
95 QString stripQuotes(const QString &text)
96 {
97 if (text.length() >= 2 && text.at(0) == QLatin1Char('"') && text.back() == QLatin1Char('"')) {
98 return text.mid(1, text.size() - 2);
99 }
100 return text;
101 }
102
103 QStringList splitOutsideQuotes(const QString &text)
104 {
105 // Match groups on 3 possible conditions:
106 // - Groups with two leading quotes must close both on them (filename:""abc xyz" tuv")
107 // - Groups enclosed in quotes
108 // - Words separated by spaces
109 static const QRegularExpression subTermsRegExp("(\\S*?\"\"[^\"]+\"[^\"]+\"+|\\S*?\"[^\"]+\"+|(?<=\\s|^)\\S+(?=\\s|$))");
110 auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);
111
112 QStringList textParts;
113 while (subTermsMatchIterator.hasNext()) {
114 textParts << subTermsMatchIterator.next().captured(0);
115 }
116 return textParts;
117 }
118 #endif
119
120 QString trimChar(const QString &text, const QLatin1Char aChar)
121 {
122 const int start = text.startsWith(aChar) ? 1 : 0;
123 const int end = (text.length() > 1 && text.endsWith(aChar)) ? 1 : 0;
124
125 return text.mid(start, text.length() - start - end);
126 }
127 }
128
129 Search::DolphinQuery::DolphinQuery(const QUrl &url, const QUrl &backupSearchPath)
130 {
131 if (url.scheme() == QLatin1String("filenamesearch")) {
132 m_searchTool = SearchTool::Filenamesearch;
133 const QUrlQuery query(url);
134 const QString filenamesearchSearchPathString = query.queryItemValue(QStringLiteral("url"), QUrl::FullyDecoded);
135 const QUrl filenamesearchSearchPathUrl = QUrl::fromUserInput(filenamesearchSearchPathString, QString(), QUrl::AssumeLocalFile);
136 if (!filenamesearchSearchPathUrl.isValid() || filenamesearchSearchPathString == filenamesearchEverywherePath) {
137 // The parsed search location is either invalid or matches a string that represents searching "everywhere".
138 m_searchLocations = SearchLocations::Everywhere;
139 m_searchPath = backupSearchPath;
140 } else {
141 m_searchLocations = SearchLocations::FromHere;
142 m_searchPath = filenamesearchSearchPathUrl;
143 }
144 m_searchTerm = query.queryItemValue(QStringLiteral("search"), QUrl::FullyDecoded);
145 m_searchThrough = query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes") ? SearchThrough::FileContents : SearchThrough::FileNames;
146 return;
147 }
148
149 #if HAVE_BALOO
150 if (url.scheme() == QLatin1String("baloosearch")) {
151 m_searchTool = SearchTool::Baloo;
152 initializeFromBalooQuery(Baloo::Query::fromSearchUrl(url), backupSearchPath);
153 return;
154 }
155 #endif
156
157 if (url.scheme() == QLatin1String("tags")) {
158 #if HAVE_BALOO
159 m_searchTool = SearchTool::Baloo;
160 #endif
161 m_searchLocations = SearchLocations::Everywhere;
162 m_searchPath = backupSearchPath;
163 // tags can contain # symbols or slashes within the Url
164 const auto tag = trimChar(url.toString(QUrl::RemoveScheme), QLatin1Char('/'));
165 if (!tag.isEmpty()) {
166 m_requiredTags.append(trimChar(url.toString(QUrl::RemoveScheme), QLatin1Char('/')));
167 }
168 return;
169 }
170
171 m_searchPath = url;
172 switchToPreferredSearchTool();
173 }
174
175 QUrl DolphinQuery::toUrl() const
176 {
177 // The following pre-conditions are sanity checks on this DolphinQuery object. If they fail, the issue is that we ever allowed the DolphinQuery to be in an
178 // inconsistent state to begin with. This should be fixed by bringing this DolphinQuery object into a reasonable state at the end of the constructors or
179 // setter methods which caused this impossible-to-fulfill combination of conditions.
180 Q_ASSERT_X(m_searchLocations == SearchLocations::Everywhere || m_searchPath.isValid(),
181 "DolphinQuery::toUrl()",
182 "We are supposed to search in a specific location but we do not know where!");
183 #if HAVE_BALOO
184 Q_ASSERT_X(m_searchLocations == SearchLocations::Everywhere || m_searchTool != SearchTool::Baloo || isIndexingEnabledIn(m_searchPath),
185 "DolphinQuery::toUrl()",
186 "We are asking Baloo to search in a location which Baloo is not supposed to have indexed!");
187 #endif
188
189 QUrl url;
190
191 #if HAVE_BALOO
192 /// Create Baloo search URL
193 if (m_searchTool == SearchTool::Baloo) {
194 Baloo::Query query;
195 if (m_fileType != KFileMetaData::Type::Empty) {
196 query.addType(KFileMetaData::TypeInfo{m_fileType}.name());
197 }
198
199 QStringList balooQueryStrings = m_unrecognizedBalooQueryStrings;
200
201 if (m_searchThrough == SearchThrough::FileContents) {
202 balooQueryStrings << m_searchTerm;
203 } else if (!m_searchTerm.isEmpty()) {
204 balooQueryStrings << QStringLiteral("filename:\"%1\"").arg(m_searchTerm);
205 }
206
207 if (m_searchLocations == SearchLocations::FromHere) {
208 query.setIncludeFolder(m_searchPath.toLocalFile());
209 }
210
211 if (m_modifiedSinceDate.isValid()) {
212 balooQueryStrings << QStringLiteral("modified>=%1").arg(m_modifiedSinceDate.toString(Qt::ISODate));
213 }
214
215 if (m_minimumRating >= 1) {
216 balooQueryStrings << QStringLiteral("rating>=%1").arg(m_minimumRating);
217 }
218
219 for (const auto &tag : m_requiredTags) {
220 if (tag.contains(QLatin1Char(' '))) {
221 balooQueryStrings << QStringLiteral("tag:\"%1\"").arg(tag);
222 } else {
223 balooQueryStrings << QStringLiteral("tag:%1").arg(tag);
224 }
225 }
226
227 query.setSearchString(balooQueryStrings.join(QLatin1Char(' ')));
228
229 return query.toSearchUrl(QUrl::toPercentEncoding(title()));
230 }
231 #endif
232
233 /// Create Filenamsearch search URL
234 url.setScheme(QStringLiteral("filenamesearch"));
235
236 QUrlQuery qUrlQuery;
237 qUrlQuery.addQueryItem(QStringLiteral("search"), QUrl::toPercentEncoding(m_searchTerm));
238 if (m_searchThrough == SearchThrough::FileContents) {
239 qUrlQuery.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
240 }
241
242 if (m_searchLocations == SearchLocations::FromHere && m_searchPath.isValid()) {
243 qUrlQuery.addQueryItem(QStringLiteral("url"), QUrl::toPercentEncoding(m_searchPath.url()));
244 } else {
245 // Search in root which is considered searching "everywhere".
246 qUrlQuery.addQueryItem(QStringLiteral("url"), QUrl::toPercentEncoding(filenamesearchEverywherePath));
247 }
248 qUrlQuery.addQueryItem(QStringLiteral("title"), QUrl::toPercentEncoding(title()));
249
250 url.setQuery(qUrlQuery);
251 return url;
252 }
253
254 void DolphinQuery::setSearchLocations(SearchLocations searchLocations)
255 {
256 m_searchLocations = searchLocations;
257 switchToPreferredSearchTool();
258 }
259
260 void DolphinQuery::setSearchPath(const QUrl &searchPath)
261 {
262 m_searchPath = searchPath;
263 switchToPreferredSearchTool();
264 }
265
266 void DolphinQuery::setSearchThrough(SearchThrough searchThrough)
267 {
268 m_searchThrough = searchThrough;
269 switchToPreferredSearchTool();
270 }
271
272 void DolphinQuery::switchToPreferredSearchTool()
273 {
274 const bool isIndexingEnabledInCurrentSearchLocation = m_searchLocations == SearchLocations::Everywhere || isIndexingEnabledIn(m_searchPath);
275 const bool searchThroughFileContentsWithoutIndexing = m_searchThrough == SearchThrough::FileContents && !isContentIndexingEnabled();
276 if (!isIndexingEnabledInCurrentSearchLocation || searchThroughFileContentsWithoutIndexing) {
277 m_searchTool = SearchTool::Filenamesearch;
278 return;
279 }
280 #if HAVE_BALOO
281 // The current search location allows searching with Baloo. We switch to Baloo if this is the saved user preference.
282 if (SearchSettings::searchTool() == QStringLiteral("Baloo")) {
283 m_searchTool = SearchTool::Baloo;
284 }
285 #endif
286 }
287
288 #if HAVE_BALOO
289 void DolphinQuery::initializeFromBalooQuery(const Baloo::Query &balooQuery, const QUrl &backupSearchPath)
290 {
291 const QString balooSearchPathString = balooQuery.includeFolder();
292 const QUrl balooSearchPathUrl = QUrl::fromUserInput(balooSearchPathString, QString(), QUrl::AssumeLocalFile);
293 if (!balooSearchPathUrl.isValid() || balooSearchPathString == balooSearchEverywherePath) {
294 // The parsed search location is either invalid or matches a string that represents searching "everywhere" i.e. in all indexed locations.
295 m_searchLocations = SearchLocations::Everywhere;
296 m_searchPath = backupSearchPath;
297 } else {
298 m_searchLocations = SearchLocations::FromHere;
299 m_searchPath = balooSearchPathUrl;
300 }
301
302 const QStringList types = balooQuery.types();
303 // We currently only allow searching for one file type at once. (Searching for more seems out of scope for Dolphin anyway IMO.)
304 m_fileType = types.isEmpty() ? KFileMetaData::Type::Empty : KFileMetaData::TypeInfo::fromName(types.first()).type();
305
306 /// If nothing is requested, we use the default.
307 std::optional<SearchThrough> requestedToSearchThrough;
308 const QStringList subTerms = splitOutsideQuotes(balooQuery.searchString());
309 for (const QString &subTerm : subTerms) {
310 const QString token = searchTermToken(subTerm);
311 const QString value = stripQuotes(subTerm.mid(token.length()));
312
313 if (token == QLatin1String("filename:")) {
314 // This query is meant to not search in file contents.
315 if (!value.isEmpty()) {
316 if (m_searchTerm.isEmpty()) { // Seems like we already received a search term for the content search. We don't overwrite it because the Dolphin
317 // UI does not support searching for differing strings in content and file name.
318 m_searchTerm = value;
319 }
320 if (!requestedToSearchThrough.has_value()) { // If requested to search thorugh contents, searching file names is already implied.
321 requestedToSearchThrough = SearchThrough::FileNames;
322 }
323 }
324 continue;
325 } else if (token.startsWith(QLatin1String("modified>="))) {
326 m_modifiedSinceDate = QDate::fromString(value, Qt::ISODate);
327 continue;
328 } else if (token.startsWith(QLatin1String("modified>"))) {
329 m_modifiedSinceDate = QDate::fromString(value, Qt::ISODate).addDays(1);
330 continue;
331 } else if (token.startsWith(QLatin1String("rating>="))) {
332 m_minimumRating = value.toInt();
333 continue;
334 } else if (token.startsWith(QLatin1String("tag"))) {
335 m_requiredTags.append(value);
336 continue;
337 } else if (!token.isEmpty()) {
338 m_unrecognizedBalooQueryStrings << token + value;
339 continue;
340 } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
341 continue;
342 } else if (!value.isEmpty()) {
343 // An empty token means this is just blank text, which is where the generic search term is located.
344 if (!m_searchTerm.isEmpty()) {
345 // Multiple search terms are separated by spaces.
346 m_searchTerm.append(QLatin1Char{' '});
347 }
348 m_searchTerm.append(value);
349 requestedToSearchThrough = SearchThrough::FileContents;
350 }
351 }
352 if (requestedToSearchThrough.has_value()) {
353 m_searchThrough = requestedToSearchThrough.value();
354 }
355 }
356 #endif // HAVE_BALOO
357
358 QString DolphinQuery::title() const
359 {
360 if (m_searchLocations == SearchLocations::FromHere) {
361 QString prettySearchLocation;
362 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
363 QModelIndex url_index = placesModel->closestItem(m_searchPath);
364 if (url_index.isValid() && placesModel->url(url_index).matches(m_searchPath, QUrl::StripTrailingSlash)) {
365 prettySearchLocation = placesModel->text(url_index);
366 } else {
367 prettySearchLocation = m_searchPath.fileName();
368 }
369 if (prettySearchLocation.isEmpty()) {
370 if (m_searchPath.isLocalFile()) {
371 prettySearchLocation = KShell::tildeCollapse(m_searchPath.adjusted(QUrl::StripTrailingSlash).toLocalFile());
372 } else {
373 prettySearchLocation = m_searchPath.toString(QUrl::RemoveAuthority | QUrl::StripTrailingSlash);
374 }
375 }
376
377 // A great title clearly identifies a search results page among many tabs, windows, or links in the Places panel.
378 // Using the search term is phenomenal for this because the user has typed this very specific string with a clear intention, so we definitely want to
379 // reuse the search term in the title if possible.
380 if (!m_searchTerm.isEmpty()) {
381 if (m_searchThrough == SearchThrough::FileNames) {
382 return i18nc("@title of a search results page. %1 is the search term a user entered, %2 is a folder name",
383 "Search results for “%1” in %2",
384 m_searchTerm,
385 prettySearchLocation);
386 }
387 Q_ASSERT(m_searchThrough == SearchThrough::FileContents);
388 return i18nc("@title of a search results page. %1 is the search term a user entered, %2 is a folder name",
389 "Files containing “%1” in %2",
390 m_searchTerm,
391 prettySearchLocation);
392 }
393 if (!m_requiredTags.isEmpty()) {
394 if (m_requiredTags.count() == 1) {
395 return i18nc("@title of a search results page. %1 is a tag e.g. 'important'. %2 is a folder name",
396 "Search items tagged “%1” in %2",
397 m_requiredTags.constFirst(),
398 prettySearchLocation);
399 }
400 return i18nc("@title of a search results page. %1 and %2 are tags e.g. 'important'. %3 is a folder name",
401 "Search items tagged “%1” and “%2” in %3",
402 m_requiredTags.constFirst(),
403 m_requiredTags.constLast(),
404 prettySearchLocation);
405 }
406 if (m_fileType != KFileMetaData::Type::Empty) {
407 return i18nc("@title of a search results page for items of a specified type. %1 is a file type e.g. 'Document', 'Folder'. %2 is a folder name",
408 "%1 search results in %2",
409 KFileMetaData::TypeInfo{m_fileType}.displayName(),
410 prettySearchLocation);
411 }
412 // Everything else failed so we use a very generic title.
413 return i18nc("@title of a search results page with items matching pre-defined conditions. %1 is a folder name",
414 "Search results in %1",
415 prettySearchLocation);
416 }
417
418 Q_ASSERT(m_searchLocations == SearchLocations::Everywhere);
419 // A great title clearly identifies a search results page among many tabs, windows, or links in the Places panel.
420 // Using the search term is phenomenal for this because the user has typed this very specific string with a clear intention, so we definitely want to reuse
421 // the search term in the title if possible.
422 if (!m_searchTerm.isEmpty()) {
423 if (m_searchThrough == SearchThrough::FileNames) {
424 return i18nc("@title of a search results page. %1 is the search term a user entered", "Search results for “%1”", m_searchTerm);
425 }
426 Q_ASSERT(m_searchThrough == SearchThrough::FileContents);
427 return i18nc("@title of a search results page. %1 is the search term a user entered", "Files containing “%1”", m_searchTerm);
428 }
429 if (!m_requiredTags.isEmpty()) {
430 if (m_requiredTags.count() == 1) {
431 return i18nc("@title of a search results page. %1 is a tag e.g. 'important'", "Search items tagged “%1”", m_requiredTags.constFirst());
432 }
433 return i18nc("@title of a search results page. %1 and %2 are tags e.g. 'important'",
434 "Search items tagged “%1” and “%2”",
435 m_requiredTags.constFirst(),
436 m_requiredTags.constLast());
437 }
438 if (m_fileType != KFileMetaData::Type::Empty) {
439 // i18n: Results page for items of a specified type. %1 is a file type e.g. 'Audio', 'Document', 'Folder', 'Archive'. 'Presentation'.
440 // If putting such a file type at the start does not work in your language in this context, you might want to translate this liberally with
441 // something along the lines of 'Search items of type “%1”'.
442 return i18nc("@title of a search. %1 is file type", "%1 search results", KFileMetaData::TypeInfo{m_fileType}.displayName());
443 }
444 // Everything else failed so we use a very generic title.
445 return i18nc("@title of a search results page with items matching pre-defined conditions", "Search results");
446 }
447
448 /** For testing Baloo in DolphinQueryTest even if it is not indexing any locations yet. */
449 void Search::setTestMode()
450 {
451 g_testMode = true;
452 };