Changeset View
Changeset View
Standalone View
Standalone View
src/search/dolphinquery.cpp
Show All 21 Lines | |||||
22 | #include <QRegularExpression> | 22 | #include <QRegularExpression> | ||
23 | 23 | | |||
24 | #include <config-baloo.h> | 24 | #include <config-baloo.h> | ||
25 | #ifdef HAVE_BALOO | 25 | #ifdef HAVE_BALOO | ||
26 | #include <Baloo/Query> | 26 | #include <Baloo/Query> | ||
27 | #endif | 27 | #endif | ||
28 | 28 | | |||
29 | namespace { | 29 | namespace { | ||
30 | /** 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 | ||
31 | * This is a copy of `DolphinFacetsWidget::isSearchTerm()` method. | 31 | * @return: the specific search token of the term, or an empty QString() if none is found | ||
32 | */ | 32 | */ | ||
33 | bool isSearchTerm(const QString& term) | 33 | QString searchTermToken(const QString& term) | ||
34 | { | 34 | { | ||
35 | static const QLatin1String searchTokens[] { | 35 | static const QLatin1String searchTokens[] { | ||
36 | QLatin1String("filename:"), | ||||
36 | QLatin1String("modified>="), | 37 | QLatin1String("modified>="), | ||
37 | QLatin1String("rating>="), | 38 | QLatin1String("rating>="), | ||
38 | QLatin1String("tag:"), QLatin1String("tag=") | 39 | QLatin1String("tag:"), QLatin1String("tag=") | ||
39 | }; | 40 | }; | ||
40 | 41 | | |||
41 | for (const auto &searchToken : searchTokens) { | 42 | for (const auto &searchToken : searchTokens) { | ||
42 | if (term.startsWith(searchToken)) { | 43 | if (term.startsWith(searchToken)) { | ||
43 | return true; | 44 | return searchToken; | ||
44 | } | 45 | } | ||
45 | } | 46 | } | ||
46 | return false; | 47 | return QString(); | ||
47 | } | 48 | } | ||
48 | 49 | | |||
49 | QString stripQuotes(const QString& text) | 50 | QString stripQuotes(const QString& text) | ||
50 | { | 51 | { | ||
51 | QString cleanedText = text; | 52 | if (text.length() >= 2 && text.at(0) == QLatin1Char('"') | ||
52 | if (!cleanedText.isEmpty() && cleanedText.at(0) == QLatin1Char('"')) { | 53 | && text.back() == QLatin1Char('"')) { | ||
53 | cleanedText = cleanedText.mid(1); | 54 | return text.mid(1, text.size() - 2); | ||
54 | } | 55 | } | ||
55 | if (!cleanedText.isEmpty() && cleanedText.back() == QLatin1Char('"')) { | 56 | return text; | ||
56 | cleanedText = cleanedText.mid(0, cleanedText.size() - 1); | | |||
57 | } | | |||
58 | return cleanedText; | | |||
59 | } | 57 | } | ||
60 | 58 | | |||
61 | QStringList splitOutsideQuotes(const QString& text) | 59 | QStringList splitOutsideQuotes(const QString& text) | ||
62 | { | 60 | { | ||
63 | const QRegularExpression subTermsRegExp("([^ ]*\"[^\"]*\"|(?<= |^)[^ ]+(?= |$))"); | 61 | const QRegularExpression subTermsRegExp("(\\S*?\"[^\"]*?\"|(?<=\\s|^)\\S+(?=\\s|$))"); | ||
64 | auto subTermsMatchIterator = subTermsRegExp.globalMatch(text); | 62 | auto subTermsMatchIterator = subTermsRegExp.globalMatch(text); | ||
65 | 63 | | |||
66 | QStringList textParts; | 64 | QStringList textParts; | ||
67 | while (subTermsMatchIterator.hasNext()) { | 65 | while (subTermsMatchIterator.hasNext()) { | ||
68 | textParts << subTermsMatchIterator.next().captured(0); | 66 | textParts << subTermsMatchIterator.next().captured(0); | ||
69 | } | 67 | } | ||
70 | return textParts; | 68 | return textParts; | ||
71 | } | 69 | } | ||
Show All 12 Lines | 77 | #ifdef HAVE_BALOO | |||
84 | const QStringList types = query.types(); | 82 | const QStringList types = query.types(); | ||
85 | model.m_fileType = types.isEmpty() ? QString() : types.first(); | 83 | model.m_fileType = types.isEmpty() ? QString() : types.first(); | ||
86 | 84 | | |||
87 | QStringList textParts; | 85 | QStringList textParts; | ||
88 | QString fileName; | 86 | QString fileName; | ||
89 | 87 | | |||
90 | const QStringList subTerms = splitOutsideQuotes(query.searchString()); | 88 | const QStringList subTerms = splitOutsideQuotes(query.searchString()); | ||
91 | foreach (const QString& subTerm, subTerms) { | 89 | foreach (const QString& subTerm, subTerms) { | ||
92 | if (subTerm.startsWith(QLatin1String("filename:"))) { | 90 | const QString token = searchTermToken(subTerm); | ||
93 | fileName = stripQuotes(subTerm.mid(9)); | 91 | const QString value = stripQuotes(subTerm.mid(token.length())); | ||
94 | if (!fileName.isEmpty()) { | 92 | | ||
93 | if (token == QLatin1String("filename:")) { | ||||
94 | if (!value.isEmpty()) { | ||||
95 | fileName = value; | ||||
95 | model.m_hasFileName = true; | 96 | model.m_hasFileName = true; | ||
96 | } | 97 | } | ||
97 | continue; | 98 | continue; | ||
98 | } else if (isSearchTerm(subTerm)) { | 99 | } else if (!token.isEmpty()) { | ||
99 | model.m_searchTerms << subTerm; | 100 | model.m_searchTerms << token + value; | ||
100 | continue; | 101 | continue; | ||
101 | } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) { | 102 | } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) { | ||
102 | continue; | 103 | continue; | ||
103 | } else { | 104 | } else if (!value.isEmpty()) { | ||
104 | const QString cleanedTerm = stripQuotes(subTerm); | 105 | textParts << value; | ||
105 | if (!cleanedTerm.isEmpty()) { | | |||
106 | textParts << cleanedTerm; | | |||
107 | model.m_hasContentSearch = true; | 106 | model.m_hasContentSearch = true; | ||
108 | } | 107 | } | ||
109 | } | 108 | } | ||
110 | } | | |||
111 | 109 | | |||
112 | if (model.m_hasFileName) { | 110 | if (model.m_hasFileName) { | ||
113 | if (model.m_hasContentSearch) { | 111 | if (model.m_hasContentSearch) { | ||
114 | textParts << QStringLiteral("filename:\"%1\"").arg(fileName); | 112 | textParts << QStringLiteral("filename:\"%1\"").arg(fileName); | ||
115 | } else { | 113 | } else { | ||
116 | textParts << fileName; | 114 | textParts << fileName; | ||
117 | } | 115 | } | ||
118 | } | 116 | } | ||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |