]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinquery.cpp
fix(search): Allow to set empty type
[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 model.m_searchText = query.searchString();
58
59 const QStringList types = query.types();
60 model.m_fileType = types.isEmpty() ? QString() : types.first();
61
62 const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
63 foreach (const QString& subTerm, subTerms) {
64 if (subTerm.startsWith(QLatin1String("filename:"))) {
65 const QString value = subTerm.mid(9);
66 model.m_searchText = value;
67 } else if (isSearchTerm(subTerm)) {
68 model.m_searchTerms << subTerm;
69 }
70 }
71 #endif
72 return model;
73 }
74
75 QUrl DolphinQuery::searchUrl() const
76 {
77 return m_searchUrl;
78 }
79
80 QString DolphinQuery::text() const
81 {
82 return m_searchText;
83 }
84
85 QString DolphinQuery::type() const
86 {
87 return m_fileType;
88 }
89
90 QStringList DolphinQuery::searchTerms() const
91 {
92 return m_searchTerms;
93 }
94
95 QString DolphinQuery::includeFolder() const
96 {
97 return m_includeFolder;
98 }