1 /***************************************************************************
2 * Copyright (C) 2010 by Sebastian Trueg <trueg@kde.org> *
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. *
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. *
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 ***************************************************************************/
20 #include "filterpanel.h"
22 #include <nepomuk/filequery.h>
23 #include <nepomuk/facetwidget.h>
24 #include <Nepomuk/Query/FileQuery>
25 #include <Nepomuk/Query/Term>
27 #include <kfileitem.h>
28 #include <kio/jobclasses.h>
31 #include <QtGui/QVBoxLayout>
32 #include <QtGui/QTreeView>
33 #include <QtGui/QPushButton>
37 FilterPanel::FilterPanel(QWidget
* parent
)
40 QVBoxLayout
* layout
= new QVBoxLayout(this);
41 m_buttonRemoveFolderRestriction
= new QPushButton( i18n( "Remove folder restriction" ), this );
42 connect( m_buttonRemoveFolderRestriction
, SIGNAL( clicked() ), SLOT( slotRemoveFolderRestrictionClicked() ) );
44 layout
->addWidget(m_buttonRemoveFolderRestriction
);
46 m_facetWidget
= new Nepomuk::Utils::FacetWidget( this );
47 layout
->addWidget( m_facetWidget
, 1 );
48 connect(m_facetWidget
, SIGNAL(facetsChanged()), this, SLOT(slotFacetsChanged()) );
50 // init to empty panel
51 setQuery(Nepomuk::Query::Query());
55 FilterPanel::~FilterPanel()
59 void FilterPanel::setUrl(const KUrl
& url
)
65 setQuery(Nepomuk::Query::Query());
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
*)));
74 void FilterPanel::setQuery(const Nepomuk::Query::Query
& query
)
76 kDebug() << query
<< query
.isValid() << query
.toSparqlQuery();
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
;
87 m_unfacetedRestQuery
= Nepomuk::Query::Query();
93 void FilterPanel::slotSetUrlStatFinished(KJob
* job
)
95 m_lastSetUrlStatJob
= 0;
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
);
104 else if ( url().isLocalFile() ) {
105 // fallback query for local file URLs
106 nepomukQuery
.addIncludeFolder(url(), false);
108 kDebug() << nepomukQuery
;
109 setQuery(nepomukQuery
);
113 void FilterPanel::slotFacetsChanged()
115 Nepomuk::Query::Query
query( m_unfacetedRestQuery
&& m_facetWidget
->queryTerm() );
117 emit
urlActivated( query
.toSearchUrl() );
121 void FilterPanel::slotRemoveFolderRestrictionClicked()
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() );