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 * This is a copy of `DolphinFacetsWidget::isRatingTerm()` method.
33 bool isSearchTerm(const QString
& term
)
35 static const QLatin1String searchTokens
[] {
36 QLatin1String("modified>="),
37 QLatin1String("rating>="),
38 QLatin1String("tag:"), QLatin1String("tag=")
41 for (const auto &searchToken
: searchTokens
) {
42 if (term
.startsWith(searchToken
)) {
49 QString
stripQuotes(const QString
& text
)
51 QString cleanedText
= text
;
52 if (!cleanedText
.isEmpty() && cleanedText
.at(0) == QLatin1Char('"')) {
53 cleanedText
= cleanedText
.mid(1);
55 if (!cleanedText
.isEmpty() && cleanedText
.back() == QLatin1Char('"')) {
56 cleanedText
= cleanedText
.mid(0, cleanedText
.size() - 1);
61 QStringList
splitOutsideQuotes(const QString
& text
)
63 const QRegularExpression
subTermsRegExp("([^ ]*\"[^\"]*\"|(?<= |^)[^ ]+(?= |$))");
64 auto subTermsMatchIterator
= subTermsRegExp
.globalMatch(text
);
66 QStringList textParts
;
67 while (subTermsMatchIterator
.hasNext()) {
68 textParts
<< subTermsMatchIterator
.next().captured(0);
74 DolphinQuery
DolphinQuery::fromBalooSearchUrl(const QUrl
& searchUrl
)
77 model
.m_searchUrl
= searchUrl
;
80 const Baloo::Query query
= Baloo::Query::fromSearchUrl(searchUrl
);
82 model
.m_includeFolder
= query
.includeFolder();
84 const QStringList types
= query
.types();
85 model
.m_fileType
= types
.isEmpty() ? QString() : types
.first();
87 QStringList textParts
;
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;
98 } else if (isSearchTerm(subTerm
)) {
99 model
.m_searchTerms
<< subTerm
;
101 } else if (subTerm
== QLatin1String("AND") && subTerm
!= subTerms
.at(0) && subTerm
!= subTerms
.back()) {
104 const QString cleanedTerm
= stripQuotes(subTerm
);
105 if (!cleanedTerm
.isEmpty()) {
106 textParts
<< cleanedTerm
;
107 model
.m_hasContentSearch
= true;
112 if (model
.m_hasFileName
) {
113 if (model
.m_hasContentSearch
) {
114 textParts
<< QStringLiteral("filename:\"%1\"").arg(fileName
);
116 textParts
<< fileName
;
120 model
.m_searchText
= textParts
.join(QLatin1Char(' '));
126 QUrl
DolphinQuery::searchUrl() const
131 QString
DolphinQuery::text() const
136 QString
DolphinQuery::type() const
141 QStringList
DolphinQuery::searchTerms() const
143 return m_searchTerms
;
146 QString
DolphinQuery::includeFolder() const
148 return m_includeFolder
;
151 bool DolphinQuery::hasContentSearch() const
153 return m_hasContentSearch
;
156 bool DolphinQuery::hasFileName() const
158 return m_hasFileName
;