]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinquery.cpp
0581a02ece041fcc242ca13388d72131ca5e44d4
[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 #ifdef HAVE_BALOO
31 /** Checks if a given term in the Baloo::Query::searchString() is a special search term
32 * @return: the specific search token of the term, or an empty QString() if none is found
33 */
34 QString searchTermToken(const QString& term)
35 {
36 static const QLatin1String searchTokens[] {
37 QLatin1String("filename:"),
38 QLatin1String("modified>="),
39 QLatin1String("rating>="),
40 QLatin1String("tag:"), QLatin1String("tag=")
41 };
42
43 for (const auto &searchToken : searchTokens) {
44 if (term.startsWith(searchToken)) {
45 return searchToken;
46 }
47 }
48 return QString();
49 }
50
51 QString stripQuotes(const QString& text)
52 {
53 if (text.length() >= 2 && text.at(0) == QLatin1Char('"')
54 && text.back() == QLatin1Char('"')) {
55 return text.mid(1, text.size() - 2);
56 }
57 return text;
58 }
59
60 QStringList splitOutsideQuotes(const QString& text)
61 {
62 const QRegularExpression subTermsRegExp("(\\S*?\"[^\"]*?\"|(?<=\\s|^)\\S+(?=\\s|$))");
63 auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);
64
65 QStringList textParts;
66 while (subTermsMatchIterator.hasNext()) {
67 textParts << subTermsMatchIterator.next().captured(0);
68 }
69 return textParts;
70 }
71 #endif
72 }
73
74
75 DolphinQuery DolphinQuery::fromSearchUrl(const QUrl& searchUrl)
76 {
77 DolphinQuery model;
78 model.m_searchUrl = searchUrl;
79
80 if (searchUrl.scheme() == QLatin1String("baloosearch")) {
81 model.parseBalooQuery();
82 }
83
84 return model;
85 }
86
87 bool DolphinQuery::supportsScheme(const QString& urlScheme)
88 {
89 static const QStringList supportedSchemes = {
90 QStringLiteral("baloosearch"),
91 };
92
93 return supportedSchemes.contains(urlScheme);
94 }
95
96 void DolphinQuery::parseBalooQuery()
97 {
98 #ifdef HAVE_BALOO
99 const Baloo::Query query = Baloo::Query::fromSearchUrl(m_searchUrl);
100
101 m_includeFolder = query.includeFolder();
102
103 const QStringList types = query.types();
104 m_fileType = types.isEmpty() ? QString() : types.first();
105
106 QStringList textParts;
107 QString fileName;
108
109 const QStringList subTerms = splitOutsideQuotes(query.searchString());
110 foreach (const QString& subTerm, subTerms) {
111 const QString token = searchTermToken(subTerm);
112 const QString value = stripQuotes(subTerm.mid(token.length()));
113
114 if (token == QLatin1String("filename:")) {
115 if (!value.isEmpty()) {
116 fileName = value;
117 m_hasFileName = true;
118 }
119 continue;
120 } else if (!token.isEmpty()) {
121 m_searchTerms << token + value;
122 continue;
123 } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
124 continue;
125 } else if (!value.isEmpty()) {
126 textParts << value;
127 m_hasContentSearch = true;
128 }
129 }
130
131 if (m_hasFileName) {
132 if (m_hasContentSearch) {
133 textParts << QStringLiteral("filename:\"%1\"").arg(fileName);
134 } else {
135 textParts << fileName;
136 }
137 }
138
139 m_searchText = textParts.join(QLatin1Char(' '));
140 #endif
141 }
142
143
144 QUrl DolphinQuery::searchUrl() const
145 {
146 return m_searchUrl;
147 }
148
149 QString DolphinQuery::text() const
150 {
151 return m_searchText;
152 }
153
154 QString DolphinQuery::type() const
155 {
156 return m_fileType;
157 }
158
159 QStringList DolphinQuery::searchTerms() const
160 {
161 return m_searchTerms;
162 }
163
164 QString DolphinQuery::includeFolder() const
165 {
166 return m_includeFolder;
167 }
168
169 bool DolphinQuery::hasContentSearch() const
170 {
171 return m_hasContentSearch;
172 }
173
174 bool DolphinQuery::hasFileName() const
175 {
176 return m_hasFileName;
177 }