]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/filter/filterpanel.cpp
Adjust code to the changed signal of the Nepomuk facet-widget.
[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 <nepomuk/filequery.h>
23 #include <nepomuk/facetwidget.h>
24 #include <nepomuk/facet.h>
25 #include <Nepomuk/Query/FileQuery>
26 #include <Nepomuk/Query/Term>
27
28 #include <kfileitem.h>
29 #include <kio/jobclasses.h>
30 #include <kio/job.h>
31
32 #include <QPushButton>
33 #include <QShowEvent>
34 #include <QTreeView>
35 #include <QVBoxLayout>
36
37 FilterPanel::FilterPanel(QWidget* parent) :
38 Panel(parent),
39 m_initialized(false),
40 m_lastSetUrlStatJob(0),
41 m_removeFolderRestrictionButton(0),
42 m_facetWidget(0),
43 m_unfacetedRestQuery()
44 {
45 }
46
47 FilterPanel::~FilterPanel()
48 {
49 }
50
51 bool FilterPanel::urlChanged()
52 {
53 if (isVisible()) {
54 setQuery(Nepomuk::Query::Query());
55
56 delete m_lastSetUrlStatJob;
57
58 m_lastSetUrlStatJob = KIO::stat(url(), KIO::HideProgressInfo);
59 connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
60 this, SLOT(slotSetUrlStatFinished(KJob*)));
61 }
62
63 return true;
64 }
65
66 void FilterPanel::showEvent(QShowEvent* event)
67 {
68 if (event->spontaneous()) {
69 Panel::showEvent(event);
70 return;
71 }
72
73 if (!m_initialized) {
74 QVBoxLayout* layout = new QVBoxLayout(this);
75 Q_ASSERT(m_removeFolderRestrictionButton == 0);
76 m_removeFolderRestrictionButton = new QPushButton(i18n("Remove folder restriction"), this);
77 connect(m_removeFolderRestrictionButton, SIGNAL(clicked()), SLOT(slotRemoveFolderRestrictionClicked()));
78
79 layout->addWidget(m_removeFolderRestrictionButton);
80
81 Q_ASSERT(m_facetWidget == 0);
82 m_facetWidget = new Nepomuk::Utils::FacetWidget(this);
83 layout->addWidget(m_facetWidget, 1);
84
85 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createFileTypeFacet());
86 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createDateFacet());
87 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createRatingFacet());
88 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createTagFacet());
89
90 Q_ASSERT(m_lastSetUrlStatJob == 0);
91 m_lastSetUrlStatJob = KIO::stat(url(), KIO::HideProgressInfo);
92 connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
93 this, SLOT(slotSetUrlStatFinished(KJob*)));
94
95 connect(m_facetWidget, SIGNAL(queryTermChanged(Nepomuk::Query::Term)),
96 this, SLOT(slotQueryTermChanged(Nepomuk::Query::Term)));
97
98 m_initialized = true;
99 }
100
101 Panel::showEvent(event);
102 }
103
104 void FilterPanel::slotSetUrlStatFinished(KJob* job)
105 {
106 m_lastSetUrlStatJob = 0;
107
108 const KIO::UDSEntry uds = static_cast<KIO::StatJob*>(job)->statResult();
109 const QString nepomukQueryStr = uds.stringValue(KIO::UDSEntry::UDS_NEPOMUK_QUERY);
110 Nepomuk::Query::FileQuery nepomukQuery;
111 if (!nepomukQueryStr.isEmpty()) {
112 nepomukQuery = Nepomuk::Query::Query::fromString(nepomukQueryStr);
113 } else if (url().isLocalFile()) {
114 // Fallback query for local file URLs
115 nepomukQuery.addIncludeFolder(url(), false);
116 }
117 setQuery(nepomukQuery);
118 }
119
120 void FilterPanel::slotQueryTermChanged(const Nepomuk::Query::Term& term)
121 {
122 Nepomuk::Query::Query query(m_unfacetedRestQuery && term);
123 emit urlActivated(query.toSearchUrl());
124 }
125
126 void FilterPanel::slotRemoveFolderRestrictionClicked()
127 {
128 Nepomuk::Query::FileQuery query(m_unfacetedRestQuery && m_facetWidget->queryTerm());
129 query.setIncludeFolders(KUrl::List());
130 query.setExcludeFolders(KUrl::List());
131 m_facetWidget->setClientQuery(query);
132 emit urlActivated(query.toSearchUrl());
133 }
134
135 void FilterPanel::setQuery(const Nepomuk::Query::Query& query)
136 {
137 if (query.isValid()) {
138 m_removeFolderRestrictionButton->setVisible(query.isFileQuery() && !query.toFileQuery().includeFolders().isEmpty());
139 m_unfacetedRestQuery = query;
140 m_unfacetedRestQuery.setTerm(m_facetWidget->extractFacetsFromTerm(query.term()));
141 m_facetWidget->setClientQuery(query);
142 setEnabled(true);
143 } else {
144 m_unfacetedRestQuery = Nepomuk::Query::Query();
145 setEnabled(false);
146 }
147 }