]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinquery.cpp
fix(search): Fix baloo searchString parsing
[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 <config-baloo.h>
23 #ifdef HAVE_BALOO
24 #include <Baloo/Query>
25 #endif
26
27 namespace {
28 /** Checks if a given term in the Baloo::Query::searchString() is a special search term.
29 * This is a copy of `DolphinFacetsWidget::isRatingTerm()` method.
30 */
31 bool isSearchTerm(const QString& term)
32 {
33 static const QLatin1String searchTokens[] {
34 QLatin1String("modified>="),
35 QLatin1String("rating>=")
36 };
37
38 for (const auto &searchToken : searchTokens) {
39 if (term.startsWith(searchToken)) {
40 return true;
41 }
42 }
43 return false;
44 }
45 }
46
47 DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl)
48 {
49 DolphinQuery model;
50 model.m_searchUrl = searchUrl;
51
52 #ifdef HAVE_BALOO
53 const Baloo::Query query = Baloo::Query::fromSearchUrl(searchUrl);
54
55 model.m_includeFolder = query.includeFolder();
56
57 const QStringList types = query.types();
58 model.m_fileType = types.isEmpty() ? QString() : types.first();
59
60 QStringList textParts;
61
62 const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
63 foreach (const QString& subTerm, subTerms) {
64 QString value;
65 if (subTerm.startsWith(QLatin1String("filename:"))) {
66 value = subTerm.mid(9);
67 } else if (isSearchTerm(subTerm)) {
68 model.m_searchTerms << subTerm;
69 continue;
70 } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
71 continue;
72 } else {
73 value = subTerm;
74 }
75
76 if (!value.isEmpty() && value.at(0) == QLatin1Char('"')) {
77 value = value.mid(1);
78 }
79 if (!value.isEmpty() && value.back() == QLatin1Char('"')) {
80 value = value.mid(0, value.size() - 1);
81 }
82 if (!value.isEmpty()) {
83 textParts << value;
84 }
85 }
86
87 model.m_searchText = textParts.join(QLatin1Char(' '));
88
89 #endif
90 return model;
91 }
92
93 QUrl DolphinQuery::searchUrl() const
94 {
95 return m_searchUrl;
96 }
97
98 QString DolphinQuery::text() const
99 {
100 return m_searchText;
101 }
102
103 QString DolphinQuery::type() const
104 {
105 return m_fileType;
106 }
107
108 QStringList DolphinQuery::searchTerms() const
109 {
110 return m_searchTerms;
111 }
112
113 QString DolphinQuery::includeFolder() const
114 {
115 return m_includeFolder;
116 }