1 /***************************************************************************
2 * Copyright (C) 2019 by Ismael Asensio <isma.af@mgmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphinquery.h"
22 #include <QRegularExpression>
24 #include <config-baloo.h>
26 #include <Baloo/Query>
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
34 QString
searchTermToken(const QString
& term
)
36 static const QLatin1String searchTokens
[] {
37 QLatin1String("filename:"),
38 QLatin1String("modified>="),
39 QLatin1String("rating>="),
40 QLatin1String("tag:"), QLatin1String("tag=")
43 for (const auto &searchToken
: searchTokens
) {
44 if (term
.startsWith(searchToken
)) {
51 QString
stripQuotes(const QString
& text
)
53 if (text
.length() >= 2 && text
.at(0) == QLatin1Char('"')
54 && text
.back() == QLatin1Char('"')) {
55 return text
.mid(1, text
.size() - 2);
60 QStringList
splitOutsideQuotes(const QString
& text
)
62 // Match groups on 3 possible conditions:
63 // - Groups with two leading quotes must close both on them (filename:""abc xyz" tuv")
64 // - Groups enclosed in quotes
65 // - Words separated by spaces
66 const QRegularExpression
subTermsRegExp("(\\S*?\"\"[^\"]+\"[^\"]+\"+|\\S*?\"[^\"]+\"+|(?<=\\s|^)\\S+(?=\\s|$))");
67 auto subTermsMatchIterator
= subTermsRegExp
.globalMatch(text
);
69 QStringList textParts
;
70 while (subTermsMatchIterator
.hasNext()) {
71 textParts
<< subTermsMatchIterator
.next().captured(0);
79 DolphinQuery
DolphinQuery::fromSearchUrl(const QUrl
& searchUrl
)
82 model
.m_searchUrl
= searchUrl
;
84 if (searchUrl
.scheme() == QLatin1String("baloosearch")) {
85 model
.parseBalooQuery();
91 bool DolphinQuery::supportsScheme(const QString
& urlScheme
)
93 static const QStringList supportedSchemes
= {
94 QStringLiteral("baloosearch"),
97 return supportedSchemes
.contains(urlScheme
);
100 void DolphinQuery::parseBalooQuery()
103 const Baloo::Query query
= Baloo::Query::fromSearchUrl(m_searchUrl
);
105 m_includeFolder
= query
.includeFolder();
107 const QStringList types
= query
.types();
108 m_fileType
= types
.isEmpty() ? QString() : types
.first();
110 QStringList textParts
;
113 const QStringList subTerms
= splitOutsideQuotes(query
.searchString());
114 foreach (const QString
& subTerm
, subTerms
) {
115 const QString token
= searchTermToken(subTerm
);
116 const QString value
= stripQuotes(subTerm
.mid(token
.length()));
118 if (token
== QLatin1String("filename:")) {
119 if (!value
.isEmpty()) {
121 m_hasFileName
= true;
124 } else if (!token
.isEmpty()) {
125 m_searchTerms
<< token
+ value
;
127 } else if (subTerm
== QLatin1String("AND") && subTerm
!= subTerms
.at(0) && subTerm
!= subTerms
.back()) {
129 } else if (!value
.isEmpty()) {
131 m_hasContentSearch
= true;
136 if (m_hasContentSearch
) {
137 textParts
<< QStringLiteral("filename:\"%1\"").arg(fileName
);
139 textParts
<< fileName
;
143 m_searchText
= textParts
.join(QLatin1Char(' '));
148 QUrl
DolphinQuery::searchUrl() const
153 QString
DolphinQuery::text() const
158 QString
DolphinQuery::type() const
163 QStringList
DolphinQuery::searchTerms() const
165 return m_searchTerms
;
168 QString
DolphinQuery::includeFolder() const
170 return m_includeFolder
;
173 bool DolphinQuery::hasContentSearch() const
175 return m_hasContentSearch
;
178 bool DolphinQuery::hasFileName() const
180 return m_hasFileName
;