]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/filter/filterpanel.cpp
- Automatically show the filter-panel when a searching is done
[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 <QtGui/QVBoxLayout>
33 #include <QtGui/QTreeView>
34 #include <QtGui/QPushButton>
35
36 FilterPanel::FilterPanel(QWidget* parent) :
37 Panel(parent)
38 {
39 QVBoxLayout* layout = new QVBoxLayout(this);
40 m_removeFolderRestrictionButton = new QPushButton(i18n("Remove folder restriction"), this);
41 connect(m_removeFolderRestrictionButton, SIGNAL(clicked()), SLOT(slotRemoveFolderRestrictionClicked()));
42
43 layout->addWidget(m_removeFolderRestrictionButton);
44
45 m_facetWidget = new Nepomuk::Utils::FacetWidget(this);
46 layout->addWidget(m_facetWidget, 1);
47 connect(m_facetWidget, SIGNAL(facetsChanged()), this, SLOT(slotFacetsChanged()));
48
49 /*m_facetWidget->addFacet(Nepomuk::Utils::Facet::createFileTypeFacet());
50 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createTypeFacet());
51 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createDateFacet());
52 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createPriorityFacet());
53 m_facetWidget->addFacet(Nepomuk::Utils::Facet::createRatingFacet());*/
54
55 // Init to empty panel
56 setQuery(Nepomuk::Query::Query());
57 }
58
59 FilterPanel::~FilterPanel()
60 {
61 }
62
63 bool FilterPanel::urlChanged()
64 {
65 if (!isVisible()) {
66 return true;
67 }
68
69 // Disable us
70 setQuery(Nepomuk::Query::Query());
71
72 // Get the query from the item
73 m_lastSetUrlStatJob = KIO::stat(url(), KIO::HideProgressInfo);
74 connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
75 this, SLOT(slotSetUrlStatFinished(KJob*)));
76
77 return true;
78 }
79
80 void FilterPanel::slotSetUrlStatFinished(KJob* job)
81 {
82 m_lastSetUrlStatJob = 0;
83 const KIO::UDSEntry uds = static_cast<KIO::StatJob*>(job)->statResult();
84 const QString nepomukQueryStr = uds.stringValue(KIO::UDSEntry::UDS_NEPOMUK_QUERY);
85 Nepomuk::Query::FileQuery nepomukQuery;
86 if (!nepomukQueryStr.isEmpty()) {
87 nepomukQuery = Nepomuk::Query::Query::fromString(nepomukQueryStr);
88 } else if (url().isLocalFile()) {
89 // Fallback query for local file URLs
90 nepomukQuery.addIncludeFolder(url(), false);
91 }
92 setQuery(nepomukQuery);
93 }
94
95 void FilterPanel::slotFacetsChanged()
96 {
97 Nepomuk::Query::Query query(m_unfacetedRestQuery && m_facetWidget->queryTerm());
98 emit urlActivated(query.toSearchUrl());
99 }
100
101 void FilterPanel::slotRemoveFolderRestrictionClicked()
102 {
103 Nepomuk::Query::FileQuery query(m_unfacetedRestQuery && m_facetWidget->queryTerm());
104 query.setIncludeFolders(KUrl::List());
105 query.setExcludeFolders(KUrl::List());
106 m_facetWidget->setClientQuery(query);
107 emit urlActivated(query.toSearchUrl());
108 }
109
110 void FilterPanel::setQuery(const Nepomuk::Query::Query& query)
111 {
112 if (query.isValid()) {
113 m_removeFolderRestrictionButton->setVisible(query.isFileQuery() && !query.toFileQuery().includeFolders().isEmpty());
114 m_unfacetedRestQuery = query;
115 m_unfacetedRestQuery.setTerm(m_facetWidget->extractFacetsFromTerm(query.term()));
116 m_facetWidget->setClientQuery(query);
117 setEnabled(true);
118 }
119 else {
120 m_unfacetedRestQuery = Nepomuk::Query::Query();
121 setEnabled(false);
122 }
123 }