]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/filter/filterpanel.cpp
Use capitalized KDE includes
[dolphin.git] / src / panels / filter / filterpanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Sebastian Trueg <trueg@kde.org> *
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 "filterpanel.h"
21
22 #include "dolphin_searchsettings.h"
23
24 #include <Nepomuk/ResourceManager>
25 #include <Nepomuk/Utils/FacetWidget>
26 #include <Nepomuk/Utils/Facet>
27 #include <Nepomuk/Utils/SimpleFacet>
28 #include <Nepomuk/Utils/ProxyFacet>
29 #include <Nepomuk/Utils/DynamicResourceFacet>
30 #include <Nepomuk/Query/FileQuery>
31 #include <Nepomuk/Query/ResourceTypeTerm>
32 #include <Nepomuk/Query/LiteralTerm>
33 #include <Nepomuk/Query/ComparisonTerm>
34 #include <Nepomuk/Vocabulary/NFO>
35 #include <Nepomuk/Vocabulary/NMM>
36 #include <Nepomuk/Vocabulary/NIE>
37
38 #include <search/dolphinsearchinformation.h>
39
40 #include <KFileItem>
41 #include <kio/jobclasses.h>
42 #include <KIO/Job>
43 #include <KMenu>
44
45 #include <QPushButton>
46 #include <QShowEvent>
47 #include <QTreeView>
48 #include <QVBoxLayout>
49
50 FilterPanel::FilterPanel(QWidget* parent) :
51 Panel(parent),
52 m_initialized(false),
53 m_lastSetUrlStatJob(0),
54 m_startedFromDir(),
55 m_facetWidget(0),
56 m_unfacetedRestQuery()
57 {
58 setEnabled(false);
59 }
60
61 FilterPanel::~FilterPanel()
62 {
63 }
64
65 bool FilterPanel::urlChanged()
66 {
67 if (!url().protocol().startsWith(QLatin1String("nepomuk"))) {
68 // Remember the current directory before a searching is started.
69 // This is required to restore the directory in case that all facets
70 // have been reset by the user (see slotQueryTermChanged()).
71 m_startedFromDir = url();
72 }
73
74 if (isVisible() && DolphinSearchInformation::instance().isIndexingEnabled()) {
75 setQuery(Nepomuk::Query::Query());
76
77 delete m_lastSetUrlStatJob;
78
79 m_lastSetUrlStatJob = KIO::stat(url(), KIO::HideProgressInfo);
80 connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
81 this, SLOT(slotSetUrlStatFinished(KJob*)));
82 }
83
84 return true;
85 }
86
87 void FilterPanel::showEvent(QShowEvent* event)
88 {
89 if (event->spontaneous()) {
90 Panel::showEvent(event);
91 return;
92 }
93
94 if (!m_initialized) {
95 QVBoxLayout* layout = new QVBoxLayout(this);
96 layout->setMargin(0);
97
98 Q_ASSERT(m_facetWidget == 0);
99 m_facetWidget = new Nepomuk::Utils::FacetWidget(this);
100 layout->addWidget(m_facetWidget, 1);
101
102 // File Type
103 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createFileTypeFacet());
104
105 // Image Size
106 Nepomuk::Utils::ProxyFacet* imageSizeProxy = new Nepomuk::Utils::ProxyFacet();
107 imageSizeProxy->setFacetCondition(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
108 Nepomuk::Utils::SimpleFacet* imageSizeFacet = new Nepomuk::Utils::SimpleFacet(imageSizeProxy);
109 imageSizeFacet->setSelectionMode(Nepomuk::Utils::Facet::MatchAny);
110 imageSizeFacet->addTerm( i18nc("option:check Refers to a filter on image size", "Small"),
111 Nepomuk::Vocabulary::NFO::width() <= Nepomuk::Query::LiteralTerm(300));
112 imageSizeFacet->addTerm( i18nc("option:check Refers to a filter on image size", "Medium"),
113 (Nepomuk::Vocabulary::NFO::width() > Nepomuk::Query::LiteralTerm(300)) &&
114 (Nepomuk::Vocabulary::NFO::width() <= Nepomuk::Query::LiteralTerm(800)));
115 imageSizeFacet->addTerm( i18nc("option:check Refers to a filter on image size", "Large"),
116 Nepomuk::Vocabulary::NFO::width() > Nepomuk::Query::LiteralTerm(800));
117 imageSizeProxy->setSourceFacet(imageSizeFacet);
118 m_facetWidget->addFacet(imageSizeProxy);
119
120 // Artists
121 Nepomuk::Utils::ProxyFacet* artistProxy = new Nepomuk::Utils::ProxyFacet();
122 artistProxy->setFacetCondition(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Audio()) ||
123 Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
124 Nepomuk::Query::LiteralTerm(QLatin1String("audio"))));
125 Nepomuk::Utils::DynamicResourceFacet* artistFacet = new Nepomuk::Utils::DynamicResourceFacet(artistProxy);
126 artistFacet->setSelectionMode(Nepomuk::Utils::Facet::MatchAny);
127 artistFacet->setRelation(Nepomuk::Vocabulary::NMM::performer());
128 artistProxy->setSourceFacet(artistFacet);
129 m_facetWidget->addFacet(artistProxy);
130
131 // Misc
132 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createDateFacet());
133 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createRatingFacet());
134 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createTagFacet());
135
136 Q_ASSERT(m_lastSetUrlStatJob == 0);
137 m_lastSetUrlStatJob = KIO::stat(url(), KIO::HideProgressInfo);
138 connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
139 this, SLOT(slotSetUrlStatFinished(KJob*)));
140
141 connect(m_facetWidget, SIGNAL(queryTermChanged(Nepomuk::Query::Term)),
142 this, SLOT(slotQueryTermChanged(Nepomuk::Query::Term)));
143
144 m_initialized = true;
145 }
146
147 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
148 setEnabled(searchInfo.isIndexingEnabled() &&
149 searchInfo.isPathIndexed(m_startedFromDir));
150
151 Panel::showEvent(event);
152 }
153
154 void FilterPanel::hideEvent(QHideEvent* event)
155 {
156 if (!event->spontaneous()) {
157 setEnabled(false);
158 }
159
160 Panel::hideEvent(event);
161 }
162
163 void FilterPanel::contextMenuEvent(QContextMenuEvent* event)
164 {
165 Panel::contextMenuEvent(event);
166
167 QWeakPointer<KMenu> popup = new KMenu(this);
168 foreach (QAction* action, customContextMenuActions()) {
169 popup.data()->addAction(action);
170 }
171 popup.data()->exec(QCursor::pos());
172 delete popup.data();
173 }
174
175 void FilterPanel::slotSetUrlStatFinished(KJob* job)
176 {
177 m_lastSetUrlStatJob = 0;
178
179 const KIO::UDSEntry uds = static_cast<KIO::StatJob*>(job)->statResult();
180 const QString nepomukQueryStr = uds.stringValue(KIO::UDSEntry::UDS_NEPOMUK_QUERY);
181 Nepomuk::Query::FileQuery nepomukQuery;
182 if (!nepomukQueryStr.isEmpty()) {
183 nepomukQuery = Nepomuk::Query::Query::fromString(nepomukQueryStr);
184 } else if (url().isLocalFile()) {
185 // Fallback query for local file URLs: List all files
186 Nepomuk::Query::ComparisonTerm compTerm(
187 Nepomuk::Vocabulary::NFO::fileName(),
188 Nepomuk::Query::Term());
189 nepomukQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFiles);
190 if (SearchSettings::location() == QLatin1String("FromHere")) {
191 nepomukQuery.addIncludeFolder(url(), true);
192 }
193 nepomukQuery.setTerm(compTerm);
194 }
195 setQuery(nepomukQuery);
196 }
197
198 void FilterPanel::slotQueryTermChanged(const Nepomuk::Query::Term& term)
199 {
200 if (term.isValid()) {
201 // Default case: A facet has been changed by the user to restrict the query.
202 Nepomuk::Query::FileQuery query(m_unfacetedRestQuery && term);
203 emit urlActivated(query.toSearchUrl());
204 return;
205 }
206
207 // All facets have been reset by the user to be unrestricted.
208 // Verify whether the unfaceted rest query contains any additional restriction
209 // (e.g. a filename in the search field). If no further restriction is given, exit
210 // the search mode by returning to the directory where the searching has been
211 // started from.
212 const Nepomuk::Query::Term rootTerm = m_unfacetedRestQuery.term();
213 if (rootTerm.type() == Nepomuk::Query::Term::Comparison) {
214 const Nepomuk::Query::ComparisonTerm& compTerm = static_cast<const Nepomuk::Query::ComparisonTerm&>(rootTerm);
215 if (compTerm.subTerm().isValid()) {
216 Nepomuk::Query::FileQuery query(m_unfacetedRestQuery);
217 emit urlActivated(query.toSearchUrl());
218 return;
219 }
220 }
221
222 emit urlActivated(m_startedFromDir);
223 }
224
225 void FilterPanel::setQuery(const Nepomuk::Query::Query& query)
226 {
227 if (query.isValid()) {
228 const bool block = m_facetWidget->blockSignals(true);
229
230 m_unfacetedRestQuery = m_facetWidget->extractFacetsFromQuery(query);
231 m_facetWidget->setClientQuery(query);
232
233 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
234 setEnabled(searchInfo.isIndexingEnabled() &&
235 searchInfo.isPathIndexed(m_startedFromDir));
236
237 m_facetWidget->blockSignals(block);
238 } else {
239 m_unfacetedRestQuery = Nepomuk::Query::Query();
240 setEnabled(false);
241 }
242 }