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>
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
33 QString
searchTermToken(const QString
& term
)
35 static const QLatin1String searchTokens
[] {
36 QLatin1String("filename:"),
37 QLatin1String("modified>="),
38 QLatin1String("rating>="),
39 QLatin1String("tag:"), QLatin1String("tag=")
42 for (const auto &searchToken
: searchTokens
) {
43 if (term
.startsWith(searchToken
)) {
50 QString
stripQuotes(const QString
& text
)
52 if (text
.length() >= 2 && text
.at(0) == QLatin1Char('"')
53 && text
.back() == QLatin1Char('"')) {
54 return text
.mid(1, text
.size() - 2);
59 QStringList
splitOutsideQuotes(const QString
& text
)
61 const QRegularExpression
subTermsRegExp("(\\S*?\"[^\"]*?\"|(?<=\\s|^)\\S+(?=\\s|$))");
62 auto subTermsMatchIterator
= subTermsRegExp
.globalMatch(text
);
64 QStringList textParts
;
65 while (subTermsMatchIterator
.hasNext()) {
66 textParts
<< subTermsMatchIterator
.next().captured(0);
72 DolphinQuery
DolphinQuery::fromBalooSearchUrl(const QUrl
& searchUrl
)
75 model
.m_searchUrl
= searchUrl
;
78 const Baloo::Query query
= Baloo::Query::fromSearchUrl(searchUrl
);
80 model
.m_includeFolder
= query
.includeFolder();
82 const QStringList types
= query
.types();
83 model
.m_fileType
= types
.isEmpty() ? QString() : types
.first();
85 QStringList textParts
;
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()));
93 if (token
== QLatin1String("filename:")) {
94 if (!value
.isEmpty()) {
96 model
.m_hasFileName
= true;
99 } else if (!token
.isEmpty()) {
100 model
.m_searchTerms
<< token
+ value
;
102 } else if (subTerm
== QLatin1String("AND") && subTerm
!= subTerms
.at(0) && subTerm
!= subTerms
.back()) {
104 } else if (!value
.isEmpty()) {
106 model
.m_hasContentSearch
= true;
110 if (model
.m_hasFileName
) {
111 if (model
.m_hasContentSearch
) {
112 textParts
<< QStringLiteral("filename:\"%1\"").arg(fileName
);
114 textParts
<< fileName
;
118 model
.m_searchText
= textParts
.join(QLatin1Char(' '));
124 QUrl
DolphinQuery::searchUrl() const
129 QString
DolphinQuery::text() const
134 QString
DolphinQuery::type() const
139 QStringList
DolphinQuery::searchTerms() const
141 return m_searchTerms
;
144 QString
DolphinQuery::includeFolder() const
146 return m_includeFolder
;
149 bool DolphinQuery::hasContentSearch() const
151 return m_hasContentSearch
;
154 bool DolphinQuery::hasFileName() const
156 return m_hasFileName
;