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