]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinquery.cpp
Merge branch 'release/19.12'
[dolphin.git] / src / search / dolphinquery.cpp
1 /***************************************************************************
2 * Copyright (C) 2019 by Ismael Asensio <isma.af@mgmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphinquery.h"
21
22 #include <QRegularExpression>
23
24 #include <config-baloo.h>
25 #ifdef HAVE_BALOO
26 #include <Baloo/Query>
27 #endif
28
29 namespace {
30 /** Checks if a given term in the Baloo::Query::searchString() is a special search term.
31 * This is a copy of `DolphinFacetsWidget::isRatingTerm()` method.
32 */
33 bool isSearchTerm(const QString& term)
34 {
35 static const QLatin1String searchTokens[] {
36 QLatin1String("modified>="),
37 QLatin1String("rating>="),
38 QLatin1String("tag:"), QLatin1String("tag=")
39 };
40
41 for (const auto &searchToken : searchTokens) {
42 if (term.startsWith(searchToken)) {
43 return true;
44 }
45 }
46 return false;
47 }
48
49 QString stripQuotes(const QString& text)
50 {
51 QString cleanedText = text;
52 if (!cleanedText.isEmpty() && cleanedText.at(0) == QLatin1Char('"')) {
53 cleanedText = cleanedText.mid(1);
54 }
55 if (!cleanedText.isEmpty() && cleanedText.back() == QLatin1Char('"')) {
56 cleanedText = cleanedText.mid(0, cleanedText.size() - 1);
57 }
58 return cleanedText;
59 }
60
61 QStringList splitOutsideQuotes(const QString& text)
62 {
63 const QRegularExpression subTermsRegExp("([^ ]*\"[^\"]*\"|(?<= |^)[^ ]+(?= |$))");
64 auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);
65
66 QStringList textParts;
67 while (subTermsMatchIterator.hasNext()) {
68 textParts << subTermsMatchIterator.next().captured(0);
69 }
70 return textParts;
71 }
72 }
73
74 DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl)
75 {
76 DolphinQuery model;
77 model.m_searchUrl = searchUrl;
78
79 #ifdef HAVE_BALOO
80 const Baloo::Query query = Baloo::Query::fromSearchUrl(searchUrl);
81
82 model.m_includeFolder = query.includeFolder();
83
84 const QStringList types = query.types();
85 model.m_fileType = types.isEmpty() ? QString() : types.first();
86
87 QStringList textParts;
88 QString fileName;
89
90 const QStringList subTerms = splitOutsideQuotes(query.searchString());
91 foreach (const QString& subTerm, subTerms) {
92 if (subTerm.startsWith(QLatin1String("filename:"))) {
93 fileName = stripQuotes(subTerm.mid(9));
94 if (!fileName.isEmpty()) {
95 model.m_hasFileName = true;
96 }
97 continue;
98 } else if (isSearchTerm(subTerm)) {
99 model.m_searchTerms << subTerm;
100 continue;
101 } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
102 continue;
103 } else {
104 const QString cleanedTerm = stripQuotes(subTerm);
105 if (!cleanedTerm.isEmpty()) {
106 textParts << cleanedTerm;
107 model.m_hasContentSearch = true;
108 }
109 }
110 }
111
112 if (model.m_hasFileName) {
113 if (model.m_hasContentSearch) {
114 textParts << QStringLiteral("filename:\"%1\"").arg(fileName);
115 } else {
116 textParts << fileName;
117 }
118 }
119
120 model.m_searchText = textParts.join(QLatin1Char(' '));
121
122 #endif
123 return model;
124 }
125
126 QUrl DolphinQuery::searchUrl() const
127 {
128 return m_searchUrl;
129 }
130
131 QString DolphinQuery::text() const
132 {
133 return m_searchText;
134 }
135
136 QString DolphinQuery::type() const
137 {
138 return m_fileType;
139 }
140
141 QStringList DolphinQuery::searchTerms() const
142 {
143 return m_searchTerms;
144 }
145
146 QString DolphinQuery::includeFolder() const
147 {
148 return m_includeFolder;
149 }
150
151 bool DolphinQuery::hasContentSearch() const
152 {
153 return m_hasContentSearch;
154 }
155
156 bool DolphinQuery::hasFileName() const
157 {
158 return m_hasFileName;
159 }