┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/search/dolphinquery.cpp
blob: 4b762784647f8cdde269461ffcfb996afd0c27e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
    SPDX-FileCopyrightText: 2019 Ismael Asensio <[email protected]>
    SPDX-FileCopyrightText: 2025 Felix Ernst <[email protected]>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "dolphinquery.h"

#if HAVE_BALOO
#include <Baloo/IndexerConfig>
#include <Baloo/Query>
#endif
#include "dolphinplacesmodelsingleton.h"

#include <KFileMetaData/TypeInfo>
#include <KLocalizedString>
#include <KShell>

#include <QRegularExpression>
#include <QUrlQuery>

using namespace Search;

bool Search::isSupportedSearchScheme(const QString &urlScheme)
{
    static const QStringList supportedSchemes = {
        QStringLiteral("filenamesearch"),
        QStringLiteral("baloosearch"),
        QStringLiteral("tags"),
    };

    return supportedSchemes.contains(urlScheme);
}

bool g_testMode = false;

bool Search::isIndexingEnabledIn(const QUrl &directory)
{
    if (g_testMode) {
        return true; // For unit-testing, let's pretend everything is indexed correctly.
    }

#if HAVE_BALOO
    const Baloo::IndexerConfig searchInfo;
    return searchInfo.fileIndexingEnabled() && !directory.isEmpty() && searchInfo.shouldBeIndexed(directory.toLocalFile());
#else
    Q_UNUSED(directory)
    return false;
#endif
}

bool Search::isContentIndexingEnabled()
{
    if (g_testMode) {
        return true; // For unit-testing, let's pretend everything is indexed correctly.
    }

#if HAVE_BALOO
    return !Baloo::IndexerConfig{}.onlyBasicIndexing();
#else
    return false;
#endif
}

namespace
{
/** The path to be passed so Baloo searches everywhere. */
constexpr auto balooSearchEverywherePath = QLatin1String("");
/** The path to be passed so Filenamesearch searches everywhere. */
constexpr auto filenamesearchEverywherePath = QLatin1String("file:///");

#if HAVE_BALOO
/** Checks if a given term in the Baloo::Query::searchString() is a special search term
 * @return: the specific search token of the term, or an empty QString() if none is found
 */
QString searchTermToken(const QString &term)
{
    static const QLatin1String searchTokens[]{QLatin1String("filename:"),
                                              QLatin1String("modified>="),
                                              QLatin1String("modified>"),
                                              QLatin1String("rating>="),
                                              QLatin1String("tag:"),
                                              QLatin1String("tag=")};

    for (const auto &searchToken : searchTokens) {
        if (term.startsWith(searchToken)) {
            return searchToken;
        }
    }
    return QString();
}

QString stripQuotes(const QString &text)
{
    if (text.length() >= 2 && text.at(0) == QLatin1Char('"') && text.back() == QLatin1Char('"')) {
        return text.mid(1, text.size() - 2);
    }
    return text;
}

QStringList splitOutsideQuotes(const QString &text)
{
    // Match groups on 3 possible conditions:
    //   - Groups with two leading quotes must close both on them (filename:""abc xyz" tuv")
    //   - Groups enclosed in quotes
    //   - Words separated by spaces
    static const QRegularExpression subTermsRegExp("(\\S*?\"\"[^\"]+\"[^\"]+\"+|\\S*?\"[^\"]+\"+|(?<=\\s|^)\\S+(?=\\s|$))");
    auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);

    QStringList textParts;
    while (subTermsMatchIterator.hasNext()) {
        textParts << subTermsMatchIterator.next().captured(0);
    }
    return textParts;
}
#endif

QString trimChar(const QString &text, const QLatin1Char aChar)
{
    const int start = text.startsWith(aChar) ? 1 : 0;
    const int end = (text.length() > 1 && text.endsWith(aChar)) ? 1 : 0;

    return text.mid(start, text.length() - start - end);
}
}

Search::DolphinQuery::DolphinQuery(const QUrl &url, const QUrl &backupSearchPath)
{
    if (url.scheme() == QLatin1String("filenamesearch")) {
        m_searchTool = SearchTool::Filenamesearch;
        const QUrlQuery query(url);
        const QString filenamesearchSearchPathString = query.queryItemValue(QStringLiteral("url"), QUrl::FullyDecoded);
        const QUrl filenamesearchSearchPathUrl = QUrl::fromUserInput(filenamesearchSearchPathString, QString(), QUrl::AssumeLocalFile);
        if (!filenamesearchSearchPathUrl.isValid() || filenamesearchSearchPathString == filenamesearchEverywherePath) {
            // The parsed search location is either invalid or matches a string that represents searching "everywhere".
            m_searchLocations = SearchLocations::Everywhere;
            m_searchPath = backupSearchPath;
        } else {
            m_searchLocations = SearchLocations::FromHere;
            m_searchPath = filenamesearchSearchPathUrl;
        }
        m_searchTerm = query.queryItemValue(QStringLiteral("search"), QUrl::FullyDecoded);
        m_searchThrough = query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes") ? SearchThrough::FileContents : SearchThrough::FileNames;
        return;
    }

#if HAVE_BALOO
    if (url.scheme() == QLatin1String("baloosearch")) {
        m_searchTool = SearchTool::Baloo;
        initializeFromBalooQuery(Baloo::Query::fromSearchUrl(url), backupSearchPath);
        return;
    }
#endif

    if (url.scheme() == QLatin1String("tags")) {
#if HAVE_BALOO
        m_searchTool = SearchTool::Baloo;
#endif
        m_searchLocations = SearchLocations::Everywhere;
        m_searchPath = backupSearchPath;
        // tags can contain # symbols or slashes within the Url
        const auto tag = trimChar(url.toString(QUrl::RemoveScheme), QLatin1Char('/'));
        if (!tag.isEmpty()) {
            m_requiredTags.append(trimChar(url.toString(QUrl::RemoveScheme), QLatin1Char('/')));
        }
        return;
    }

    m_searchPath = url;
    switchToPreferredSearchTool();
}

QUrl DolphinQuery::toUrl() const
{
    // 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
    // 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
    // setter methods which caused this impossible-to-fulfill combination of conditions.
    Q_ASSERT_X(m_searchLocations == SearchLocations::Everywhere || m_searchPath.isValid(),
               "DolphinQuery::toUrl()",
               "We are supposed to search in a specific location but we do not know where!");
#if HAVE_BALOO
    Q_ASSERT_X(m_searchLocations == SearchLocations::Everywhere || m_searchTool != SearchTool::Baloo || isIndexingEnabledIn(m_searchPath),
               "DolphinQuery::toUrl()",
               "We are asking Baloo to search in a location which Baloo is not supposed to have indexed!");
#endif

    QUrl url;

#if HAVE_BALOO
    /// Create Baloo search URL
    if (m_searchTool == SearchTool::Baloo) {
        Baloo::Query query;
        if (m_fileType != KFileMetaData::Type::Empty) {
            query.addType(KFileMetaData::TypeInfo{m_fileType}.name());
        }

        QStringList balooQueryStrings = m_unrecognizedBalooQueryStrings;

        if (m_searchThrough == SearchThrough::FileContents) {
            balooQueryStrings << m_searchTerm;
        } else if (!m_searchTerm.isEmpty()) {
            balooQueryStrings << QStringLiteral("filename:\"%1\"").arg(m_searchTerm);
        }

        if (m_searchLocations == SearchLocations::FromHere) {
            query.setIncludeFolder(m_searchPath.toLocalFile());
        }

        if (m_modifiedSinceDate.isValid()) {
            balooQueryStrings << QStringLiteral("modified>=%1").arg(m_modifiedSinceDate.toString(Qt::ISODate));
        }

        if (m_minimumRating >= 1) {
            balooQueryStrings << QStringLiteral("rating>=%1").arg(m_minimumRating);
        }

        for (const auto &tag : m_requiredTags) {
            if (tag.contains(QLatin1Char(' '))) {
                balooQueryStrings << QStringLiteral("tag:\"%1\"").arg(tag);
            } else {
                balooQueryStrings << QStringLiteral("tag:%1").arg(tag);
            }
        }

        query.setSearchString(balooQueryStrings.join(QLatin1Char(' ')));

        return query.toSearchUrl(QUrl::toPercentEncoding(title()));
    }
#endif

    /// Create Filenamsearch search URL
    url.setScheme(QStringLiteral("filenamesearch"));

    QUrlQuery qUrlQuery;
    qUrlQuery.addQueryItem(QStringLiteral("search"), QUrl::toPercentEncoding(m_searchTerm));
    if (m_searchThrough == SearchThrough::FileContents) {
        qUrlQuery.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
    }

    if (m_searchLocations == SearchLocations::FromHere && m_searchPath.isValid()) {
        qUrlQuery.addQueryItem(QStringLiteral("url"), QUrl::toPercentEncoding(m_searchPath.url()));
    } else {
        // Search in root which is considered searching "everywhere".
        qUrlQuery.addQueryItem(QStringLiteral("url"), QUrl::toPercentEncoding(filenamesearchEverywherePath));
    }
    qUrlQuery.addQueryItem(QStringLiteral("title"), QUrl::toPercentEncoding(title()));

    url.setQuery(qUrlQuery);
    return url;
}

void DolphinQuery::setSearchLocations(SearchLocations searchLocations)
{
    m_searchLocations = searchLocations;
    switchToPreferredSearchTool();
}

void DolphinQuery::setSearchPath(const QUrl &searchPath)
{
    m_searchPath = searchPath;
    switchToPreferredSearchTool();
}

void DolphinQuery::setSearchThrough(SearchThrough searchThrough)
{
    m_searchThrough = searchThrough;
    switchToPreferredSearchTool();
}

void DolphinQuery::switchToPreferredSearchTool()
{
    const bool isIndexingEnabledInCurrentSearchLocation = m_searchLocations == SearchLocations::Everywhere || isIndexingEnabledIn(m_searchPath);
    const bool searchThroughFileContentsWithoutIndexing = m_searchThrough == SearchThrough::FileContents && !isContentIndexingEnabled();
    if (!isIndexingEnabledInCurrentSearchLocation || searchThroughFileContentsWithoutIndexing) {
        m_searchTool = SearchTool::Filenamesearch;
        return;
    }
#if HAVE_BALOO
    // The current search location allows searching with Baloo. We switch to Baloo if this is the saved user preference.
    if (SearchSettings::searchTool() == QStringLiteral("Baloo")) {
        m_searchTool = SearchTool::Baloo;
    }
#endif
}

#if HAVE_BALOO
void DolphinQuery::initializeFromBalooQuery(const Baloo::Query &balooQuery, const QUrl &backupSearchPath)
{
    const QString balooSearchPathString = balooQuery.includeFolder();
    const QUrl balooSearchPathUrl = QUrl::fromUserInput(balooSearchPathString, QString(), QUrl::AssumeLocalFile);
    if (!balooSearchPathUrl.isValid() || balooSearchPathString == balooSearchEverywherePath) {
        // The parsed search location is either invalid or matches a string that represents searching "everywhere" i.e. in all indexed locations.
        m_searchLocations = SearchLocations::Everywhere;
        m_searchPath = backupSearchPath;
    } else {
        m_searchLocations = SearchLocations::FromHere;
        m_searchPath = balooSearchPathUrl;
    }

    const QStringList types = balooQuery.types();
    // We currently only allow searching for one file type at once. (Searching for more seems out of scope for Dolphin anyway IMO.)
    m_fileType = types.isEmpty() ? KFileMetaData::Type::Empty : KFileMetaData::TypeInfo::fromName(types.first()).type();

    /// If nothing is requested, we use the default.
    std::optional<SearchThrough> requestedToSearchThrough;
    const QStringList subTerms = splitOutsideQuotes(balooQuery.searchString());
    for (const QString &subTerm : subTerms) {
        const QString token = searchTermToken(subTerm);
        const QString value = stripQuotes(subTerm.mid(token.length()));

        if (token == QLatin1String("filename:")) {
            // This query is meant to not search in file contents.
            if (!value.isEmpty()) {
                if (m_searchTerm.isEmpty()) { // Seems like we already received a search term for the content search. We don't overwrite it because the Dolphin
                                              // UI does not support searching for differing strings in content and file name.
                    m_searchTerm = value;
                }
                if (!requestedToSearchThrough.has_value()) { // If requested to search through contents, searching file names is already implied.
                    requestedToSearchThrough = SearchThrough::FileNames;
                }
            }
            continue;
        } else if (token.startsWith(QLatin1String("modified>="))) {
            m_modifiedSinceDate = QDate::fromString(value, Qt::ISODate);
            continue;
        } else if (token.startsWith(QLatin1String("modified>"))) {
            m_modifiedSinceDate = QDate::fromString(value, Qt::ISODate).addDays(1);
            continue;
        } else if (token.startsWith(QLatin1String("rating>="))) {
            m_minimumRating = value.toInt();
            continue;
        } else if (token.startsWith(QLatin1String("tag"))) {
            m_requiredTags.append(value);
            continue;
        } else if (!token.isEmpty()) {
            m_unrecognizedBalooQueryStrings << token + value;
            continue;
        } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
            continue;
        } else if (!value.isEmpty()) {
            // An empty token means this is just blank text, which is where the generic search term is located.
            if (!m_searchTerm.isEmpty()) {
                // Multiple search terms are separated by spaces.
                m_searchTerm.append(QLatin1Char{' '});
            }
            m_searchTerm.append(value);
            requestedToSearchThrough = SearchThrough::FileContents;
        }
    }
    if (requestedToSearchThrough.has_value()) {
        m_searchThrough = requestedToSearchThrough.value();
    }
}
#endif // HAVE_BALOO

QString DolphinQuery::title() const
{
    if (m_searchLocations == SearchLocations::FromHere) {
        QString prettySearchLocation;
        KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
        QModelIndex url_index = placesModel->closestItem(m_searchPath);
        if (url_index.isValid() && placesModel->url(url_index).matches(m_searchPath, QUrl::StripTrailingSlash)) {
            prettySearchLocation = placesModel->text(url_index);
        } else {
            prettySearchLocation = m_searchPath.fileName();
        }
        if (prettySearchLocation.isEmpty()) {
            if (m_searchPath.isLocalFile()) {
                prettySearchLocation = KShell::tildeCollapse(m_searchPath.adjusted(QUrl::StripTrailingSlash).toLocalFile());
            } else {
                prettySearchLocation = m_searchPath.toString(QUrl::RemoveAuthority | QUrl::StripTrailingSlash);
            }
        }

        // A great title clearly identifies a search results page among many tabs, windows, or links in the Places panel.
        // 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 the search term in the title if possible.
        if (!m_searchTerm.isEmpty()) {
            if (m_searchThrough == SearchThrough::FileNames) {
                return i18nc("@title of a search results page. %1 is the search term a user entered, %2 is a folder name",
                             "Search results for “%1” in %2",
                             m_searchTerm,
                             prettySearchLocation);
            }
            Q_ASSERT(m_searchThrough == SearchThrough::FileContents);
            return i18nc("@title of a search results page. %1 is the search term a user entered, %2 is a folder name",
                         "Files containing “%1” in %2",
                         m_searchTerm,
                         prettySearchLocation);
        }
        if (!m_requiredTags.isEmpty()) {
            if (m_requiredTags.count() == 1) {
                return i18nc("@title of a search results page. %1 is a tag e.g. 'important'. %2 is a folder name",
                             "Search items tagged “%1” in %2",
                             m_requiredTags.constFirst(),
                             prettySearchLocation);
            }
            return i18nc("@title of a search results page. %1 and %2 are tags e.g. 'important'. %3 is a folder name",
                         "Search items tagged “%1” and “%2” in %3",
                         m_requiredTags.constFirst(),
                         m_requiredTags.constLast(),
                         prettySearchLocation);
        }
        if (m_fileType != KFileMetaData::Type::Empty) {
            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",
                         "%1 search results in %2",
                         KFileMetaData::TypeInfo{m_fileType}.displayName(),
                         prettySearchLocation);
        }
        // Everything else failed so we use a very generic title.
        return i18nc("@title of a search results page with items matching pre-defined conditions. %1 is a folder name",
                     "Search results in %1",
                     prettySearchLocation);
    }

    Q_ASSERT(m_searchLocations == SearchLocations::Everywhere);
    // A great title clearly identifies a search results page among many tabs, windows, or links in the Places panel.
    // 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
    // the search term in the title if possible.
    if (!m_searchTerm.isEmpty()) {
        if (m_searchThrough == SearchThrough::FileNames) {
            return i18nc("@title of a search results page. %1 is the search term a user entered", "Search results for “%1”", m_searchTerm);
        }
        Q_ASSERT(m_searchThrough == SearchThrough::FileContents);
        return i18nc("@title of a search results page. %1 is the search term a user entered", "Files containing “%1”", m_searchTerm);
    }
    if (!m_requiredTags.isEmpty()) {
        if (m_requiredTags.count() == 1) {
            return i18nc("@title of a search results page. %1 is a tag e.g. 'important'", "Search items tagged “%1”", m_requiredTags.constFirst());
        }
        return i18nc("@title of a search results page. %1 and %2 are tags e.g. 'important'",
                     "Search items tagged “%1” and “%2”",
                     m_requiredTags.constFirst(),
                     m_requiredTags.constLast());
    }
    if (m_fileType != KFileMetaData::Type::Empty) {
        // i18n: Results page for items of a specified type. %1 is a file type e.g. 'Audio', 'Document', 'Folder', 'Archive'. 'Presentation'.
        // 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
        // something along the lines of 'Search items of type “%1”'.
        return i18nc("@title of a search. %1 is file type", "%1 search results", KFileMetaData::TypeInfo{m_fileType}.displayName());
    }
    // Everything else failed so we use a very generic title.
    return i18nc("@title of a search results page with items matching pre-defined conditions", "Search results");
}

/** For testing Baloo in DolphinQueryTest even if it is not indexing any locations yet. */
void Search::setTestMode()
{
    g_testMode = true;
};