]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/filter/filterpanel.cpp
Rename FacetPanel to FilterPanel so that the name reflects the headline.
[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/Query/FileQuery>
25 #include <Nepomuk/Query/Term>
26
27 #include <kfileitem.h>
28 #include <kio/jobclasses.h>
29 #include <kio/job.h>
30
31 #include <QtGui/QVBoxLayout>
32 #include <QtGui/QTreeView>
33 #include <QtGui/QPushButton>
34 #include <kdebug.h>
35
36
37 FilterPanel::FilterPanel(QWidget* parent)
38 : Panel(parent)
39 {
40 QVBoxLayout* layout = new QVBoxLayout(this);
41 m_buttonRemoveFolderRestriction = new QPushButton( i18n( "Remove folder restriction" ), this );
42 connect( m_buttonRemoveFolderRestriction, SIGNAL( clicked() ), SLOT( slotRemoveFolderRestrictionClicked() ) );
43
44 layout->addWidget(m_buttonRemoveFolderRestriction);
45
46 m_facetWidget = new Nepomuk::Utils::FacetWidget( this );
47 layout->addWidget( m_facetWidget, 1 );
48 connect(m_facetWidget, SIGNAL(facetsChanged()), this, SLOT(slotFacetsChanged()) );
49
50 // init to empty panel
51 setQuery(Nepomuk::Query::Query());
52 }
53
54
55 FilterPanel::~FilterPanel()
56 {
57 }
58
59 void FilterPanel::setUrl(const KUrl& url)
60 {
61 kDebug() << url;
62 Panel::setUrl(url);
63
64 // disable us
65 setQuery(Nepomuk::Query::Query());
66
67 // get the query from the item
68 m_lastSetUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
69 connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
70 this, SLOT(slotSetUrlStatFinished(KJob*)));
71 }
72
73
74 void FilterPanel::setQuery(const Nepomuk::Query::Query& query)
75 {
76 kDebug() << query << query.isValid() << query.toSparqlQuery();
77
78 if (query.isValid()) {
79 m_buttonRemoveFolderRestriction->setVisible( query.isFileQuery() && !query.toFileQuery().includeFolders().isEmpty() );
80 m_unfacetedRestQuery = query;
81 m_unfacetedRestQuery.setTerm( m_facetWidget->extractFacetsFromTerm( query.term() ) );
82 m_facetWidget->setClientQuery( query );
83 kDebug() << "Rest query after facets:" << m_unfacetedRestQuery;
84 setEnabled(true);
85 }
86 else {
87 m_unfacetedRestQuery = Nepomuk::Query::Query();
88 setEnabled(false);
89 }
90 }
91
92
93 void FilterPanel::slotSetUrlStatFinished(KJob* job)
94 {
95 m_lastSetUrlStatJob = 0;
96 kDebug() << url();
97 const KIO::UDSEntry uds = static_cast<KIO::StatJob*>(job)->statResult();
98 const QString nepomukQueryStr = uds.stringValue( KIO::UDSEntry::UDS_NEPOMUK_QUERY );
99 kDebug() << nepomukQueryStr;
100 Nepomuk::Query::FileQuery nepomukQuery;
101 if ( !nepomukQueryStr.isEmpty() ) {
102 nepomukQuery = Nepomuk::Query::Query::fromString( nepomukQueryStr );
103 }
104 else if ( url().isLocalFile() ) {
105 // fallback query for local file URLs
106 nepomukQuery.addIncludeFolder(url(), false);
107 }
108 kDebug() << nepomukQuery;
109 setQuery(nepomukQuery);
110 }
111
112
113 void FilterPanel::slotFacetsChanged()
114 {
115 Nepomuk::Query::Query query( m_unfacetedRestQuery && m_facetWidget->queryTerm() );
116 kDebug() << query;
117 emit urlActivated( query.toSearchUrl() );
118 }
119
120
121 void FilterPanel::slotRemoveFolderRestrictionClicked()
122 {
123 Nepomuk::Query::FileQuery query( m_unfacetedRestQuery && m_facetWidget->queryTerm() );
124 query.setIncludeFolders( KUrl::List() );
125 query.setExcludeFolders( KUrl::List() );
126 m_facetWidget->setClientQuery( query );
127 emit urlActivated( query.toSearchUrl() );
128 }