]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/filter/filterpanel.cpp
Added implementation of the FileInfoExtension to allow KPart plugins to obtain such...
[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(facetsChanged()), this, SLOT(slotFacetsChanged()));
96
97 m_initialized = true;
98 }
99
100 Panel::showEvent(event);
101 }
102
103 void FilterPanel::slotSetUrlStatFinished(KJob* job)
104 {
105 m_lastSetUrlStatJob = 0;
106
107 const KIO::UDSEntry uds = static_cast<KIO::StatJob*>(job)->statResult();
108 const QString nepomukQueryStr = uds.stringValue(KIO::UDSEntry::UDS_NEPOMUK_QUERY);
109 Nepomuk::Query::FileQuery nepomukQuery;
110 if (!nepomukQueryStr.isEmpty()) {
111 nepomukQuery = Nepomuk::Query::Query::fromString(nepomukQueryStr);
112 } else if (url().isLocalFile()) {
113 // Fallback query for local file URLs
114 nepomukQuery.addIncludeFolder(url(), false);
115 }
116 setQuery(nepomukQuery);
117 }
118
119 void FilterPanel::slotFacetsChanged()
120 {
121 Nepomuk::Query::Query query(m_unfacetedRestQuery && m_facetWidget->queryTerm());
122 emit urlActivated(query.toSearchUrl());
123 }
124
125 void FilterPanel::slotRemoveFolderRestrictionClicked()
126 {
127 Nepomuk::Query::FileQuery query(m_unfacetedRestQuery && m_facetWidget->queryTerm());
128 query.setIncludeFolders(KUrl::List());
129 query.setExcludeFolders(KUrl::List());
130 m_facetWidget->setClientQuery(query);
131 emit urlActivated(query.toSearchUrl());
132 }
133
134 void FilterPanel::setQuery(const Nepomuk::Query::Query& query)
135 {
136 if (query.isValid()) {
137 m_removeFolderRestrictionButton->setVisible(query.isFileQuery() && !query.toFileQuery().includeFolders().isEmpty());
138 m_unfacetedRestQuery = query;
139 m_unfacetedRestQuery.setTerm(m_facetWidget->extractFacetsFromTerm(query.term()));
140 m_facetWidget->setClientQuery(query);
141 setEnabled(true);
142 } else {
143 m_unfacetedRestQuery = Nepomuk::Query::Query();
144 setEnabled(false);
145 }
146 }