]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinquery.cpp
GIT_SILENT: Port deprecated QSet::toList method
[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 QLatin1String("tag:"), QLatin1String("tag=")
37 };
38
39 for (const auto &searchToken : searchTokens) {
40 if (term.startsWith(searchToken)) {
41 return true;
42 }
43 }
44 return false;
45 }
46 }
47
48 DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl)
49 {
50 DolphinQuery model;
51 model.m_searchUrl = searchUrl;
52
53 #ifdef HAVE_BALOO
54 const Baloo::Query query = Baloo::Query::fromSearchUrl(searchUrl);
55
56 model.m_includeFolder = query.includeFolder();
57
58 const QStringList types = query.types();
59 model.m_fileType = types.isEmpty() ? QString() : types.first();
60
61 QStringList textParts;
62
63 const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
64 foreach (const QString& subTerm, subTerms) {
65 QString value;
66 if (subTerm.startsWith(QLatin1String("filename:"))) {
67 value = subTerm.mid(9);
68 } else if (isSearchTerm(subTerm)) {
69 model.m_searchTerms << subTerm;
70 continue;
71 } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
72 continue;
73 } else {
74 value = subTerm;
75 }
76
77 if (!value.isEmpty() && value.at(0) == QLatin1Char('"')) {
78 value = value.mid(1);
79 }
80 if (!value.isEmpty() && value.back() == QLatin1Char('"')) {
81 value = value.mid(0, value.size() - 1);
82 }
83 if (!value.isEmpty()) {
84 textParts << value;
85 }
86 }
87
88 model.m_searchText = textParts.join(QLatin1Char(' '));
89
90 #endif
91 return model;
92 }
93
94 QUrl DolphinQuery::searchUrl() const
95 {
96 return m_searchUrl;
97 }
98
99 QString DolphinQuery::text() const
100 {
101 return m_searchText;
102 }
103
104 QString DolphinQuery::type() const
105 {
106 return m_fileType;
107 }
108
109 QStringList DolphinQuery::searchTerms() const
110 {
111 return m_searchTerms;
112 }
113
114 QString DolphinQuery::includeFolder() const
115 {
116 return m_includeFolder;
117 }