]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
Apply the facets-patch written by Sebastian TrĂ¼g, which allows to filter search resul...
[dolphin.git] / src / search / dolphinsearchbox.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphinsearchbox.h"
21
22 #include "dolphin_searchsettings.h"
23
24 #include <kicon.h>
25 #include <klineedit.h>
26 #include <klocale.h>
27 #include <kseparator.h>
28
29 #include <QButtonGroup>
30 #include <QDir>
31 #include <QEvent>
32 #include <QFormLayout>
33 #include <QHBoxLayout>
34 #include <QKeyEvent>
35 #include <QLabel>
36 #include <QPushButton>
37 #include <QTimer>
38 #include <QToolButton>
39 #include <QVBoxLayout>
40
41 #include <config-nepomuk.h>
42 #ifdef HAVE_NEPOMUK
43 #include <nepomuk/andterm.h>
44 #include <nepomuk/filequery.h>
45 #include <nepomuk/literalterm.h>
46 #include <nepomuk/query.h>
47 #include <nepomuk/queryparser.h>
48 #include <nepomuk/resourcemanager.h>
49 #include <nepomuk/resourcetypeterm.h>
50 #include <nepomuk/comparisonterm.h>
51 #include "nfo.h"
52
53 #include "filters/datesearchfilterwidget.h"
54 #include "filters/ratingsearchfilterwidget.h"
55 #include "filters/tagsearchfilterwidget.h"
56 #endif
57
58 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
59 QWidget(parent),
60 m_startedSearching(false),
61 m_nepomukActivated(false),
62 m_topLayout(0),
63 m_searchInput(0),
64 m_fromHereButton(0),
65 m_everywhereButton(0),
66 m_fileNameButton(0),
67 m_contentButton(0),
68 m_filterButton(0),
69 m_filterWidgetsLayout(0),
70 m_filterWidgets(),
71 m_searchPath(),
72 m_startSearchTimer(0)
73 {
74 }
75
76 DolphinSearchBox::~DolphinSearchBox()
77 {
78 saveSettings();
79 }
80
81 QString DolphinSearchBox::text() const
82 {
83 return m_searchInput->text();
84 }
85
86 void DolphinSearchBox::setSearchPath(const KUrl& url)
87 {
88 m_searchPath = url;
89 m_filterButton->setVisible(isSearchPathIndexed());
90 }
91
92 KUrl DolphinSearchBox::searchPath() const
93 {
94 return m_searchPath;
95 }
96
97 KUrl DolphinSearchBox::urlForSearching() const
98 {
99 KUrl url;
100 if (isSearchPathIndexed()) {
101 url = nepomukUrlForSearching();
102 } else {
103 url = m_searchPath;
104 url.setProtocol("filenamesearch");
105 url.addQueryItem("search", m_searchInput->text());
106 if (m_contentButton->isChecked()) {
107 url.addQueryItem("checkContent", "yes");
108 }
109 if (m_everywhereButton->isChecked()) {
110 // It is very unlikely, that the majority of Dolphins target users
111 // mean "the whole harddisk" instead of "my home folder" when
112 // selecting the "Everywhere" button.
113 url.setPath(QDir::homePath());
114 }
115 }
116
117 return url;
118 }
119
120 bool DolphinSearchBox::event(QEvent* event)
121 {
122 if (event->type() == QEvent::Polish) {
123 init();
124 } else if (event->type() == QEvent::KeyPress) {
125 if (static_cast<QKeyEvent* >(event)->key() == Qt::Key_Escape) {
126 m_searchInput->clear();
127 }
128 }
129 return QWidget::event(event);
130 }
131
132 void DolphinSearchBox::showEvent(QShowEvent* event)
133 {
134 if (!event->spontaneous()) {
135 #ifdef HAVE_NEPOMUK
136 m_nepomukActivated = (Nepomuk::ResourceManager::instance()->init() == 0);
137 #endif
138
139 m_searchInput->clear();
140 m_searchInput->setFocus();
141 m_startedSearching = false;
142 }
143 }
144
145 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
146 {
147 QWidget::keyReleaseEvent(event);
148 if ((event->key() == Qt::Key_Escape)) {
149 if (m_searchInput->text().isEmpty()) {
150 emit closeRequest();
151 } else {
152 m_searchInput->clear();
153 }
154 }
155 }
156
157 void DolphinSearchBox::emitSearchSignal()
158 {
159 m_startSearchTimer->stop();
160 m_startedSearching = true;
161 emit search(m_searchInput->text());
162 }
163
164 void DolphinSearchBox::slotConfigurationChanged()
165 {
166 if (m_startedSearching) {
167 emitSearchSignal();
168 }
169 }
170
171 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
172 {
173 if (text.isEmpty()) {
174 m_startSearchTimer->stop();
175 } else {
176 m_startSearchTimer->start();
177 }
178 emit searchTextChanged(text);
179 }
180
181 void DolphinSearchBox::slotReturnPressed(const QString& text)
182 {
183 emitSearchSignal();
184 emit returnPressed(text);
185 }
186
187 void DolphinSearchBox::setFilterWidgetsVisible(bool visible)
188 {
189 #ifdef HAVE_NEPOMUK
190 if (visible) {
191 if (m_filterWidgetsLayout == 0) {
192 m_filterWidgetsLayout = new QFormLayout(this);
193 m_filterWidgetsLayout->setSpacing(0);
194
195 m_filterWidgets.append(new DateSearchFilterWidget(this));
196 m_filterWidgets.append(new RatingSearchFilterWidget(this));
197 m_filterWidgets.append(new TagSearchFilterWidget(this));
198
199 foreach (AbstractSearchFilterWidget* filterWidget, m_filterWidgets) {
200 const QString labelText = filterWidget->filterLabel() + QLatin1Char(':');
201 QLabel* label = new QLabel(labelText, this);
202 m_filterWidgetsLayout->addRow(label, filterWidget);
203 connect(filterWidget, SIGNAL(filterChanged()), this, SLOT(emitSearchSignal()));
204 }
205 }
206 m_topLayout->addLayout(m_filterWidgetsLayout);
207 } else {
208 m_topLayout->removeItem(m_filterWidgetsLayout);
209 }
210 #else
211 Q_UNUSED(visible);
212 #endif
213 }
214
215 void DolphinSearchBox::initButton(QPushButton* button)
216 {
217 button->setAutoExclusive(true);
218 button->setFlat(true);
219 button->setCheckable(true);
220 connect(button, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
221 }
222
223 void DolphinSearchBox::loadSettings()
224 {
225 if (SearchSettings::location() == QLatin1String("Everywhere")) {
226 m_everywhereButton->setChecked(true);
227 } else {
228 m_fromHereButton->setChecked(true);
229 }
230
231 if (SearchSettings::what() == QLatin1String("Content")) {
232 m_contentButton->setChecked(true);
233 } else {
234 m_fileNameButton->setChecked(true);
235 }
236 }
237
238 void DolphinSearchBox::saveSettings()
239 {
240 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
241 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
242 SearchSettings::self()->writeConfig();
243 }
244
245 void DolphinSearchBox::init()
246 {
247 // Create close button
248 QToolButton* closeButton = new QToolButton(this);
249 closeButton->setAutoRaise(true);
250 closeButton->setIcon(KIcon("dialog-close"));
251 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
252 connect(closeButton, SIGNAL(clicked()), SIGNAL(closeRequest()));
253
254 // Create search label
255 QLabel* searchLabel = new QLabel(i18nc("@label:textbox", "Find:"), this);
256
257 // Create search box
258 m_searchInput = new KLineEdit(this);
259 m_searchInput->setClearButtonShown(true);
260 m_searchInput->setFont(KGlobalSettings::generalFont());
261 connect(m_searchInput, SIGNAL(returnPressed(QString)),
262 this, SLOT(slotReturnPressed(QString)));
263 connect(m_searchInput, SIGNAL(textChanged(QString)),
264 this, SLOT(slotSearchTextChanged(QString)));
265
266 // Apply layout for the search input
267 QHBoxLayout* searchInputLayout = new QHBoxLayout();
268 searchInputLayout->setMargin(0);
269 searchInputLayout->addWidget(closeButton);
270 searchInputLayout->addWidget(searchLabel);
271 searchInputLayout->addWidget(m_searchInput);
272
273 // Create "From Here" and "Everywhere"button
274 m_fromHereButton = new QPushButton();
275 m_fromHereButton->setText(i18nc("action:button", "From Here"));
276 initButton(m_fromHereButton);
277
278 m_everywhereButton = new QPushButton(this);
279 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
280 initButton(m_everywhereButton);
281
282 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
283 searchLocationGroup->addButton(m_fromHereButton);
284 searchLocationGroup->addButton(m_everywhereButton);
285
286 // Create "Filename" and "Content" button
287 m_fileNameButton = new QPushButton(this);
288 m_fileNameButton->setText(i18nc("action:button", "Filename"));
289 initButton(m_fileNameButton);
290
291 m_contentButton = new QPushButton();
292 m_contentButton->setText(i18nc("action:button", "Content"));
293 initButton(m_contentButton);;
294
295 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
296 searchWhatGroup->addButton(m_fileNameButton);
297 searchWhatGroup->addButton(m_contentButton);
298
299 // Create "Filter" button
300 m_filterButton = new QToolButton(this);
301 m_filterButton->setIcon(KIcon("view-filter"));
302 m_filterButton->setAutoRaise(true);
303 m_filterButton->setCheckable(true);
304 m_filterButton->hide();
305 connect(m_filterButton, SIGNAL(toggled(bool)), this, SLOT(setFilterWidgetsVisible(bool)));
306
307 // Apply layout for the options
308 QHBoxLayout* optionsLayout = new QHBoxLayout();
309 optionsLayout->setMargin(0);
310 optionsLayout->addWidget(m_fromHereButton);
311 optionsLayout->addWidget(m_everywhereButton);
312 optionsLayout->addWidget(new KSeparator(Qt::Vertical));
313 optionsLayout->addWidget(m_fileNameButton);
314 optionsLayout->addWidget(m_contentButton);
315 optionsLayout->addStretch(1);
316 optionsLayout->addWidget(m_filterButton);
317
318 m_topLayout = new QVBoxLayout(this);
319 m_topLayout->addLayout(searchInputLayout);
320 m_topLayout->addLayout(optionsLayout);
321
322 searchLabel->setBuddy(m_searchInput);
323 loadSettings();
324
325 // The searching should be started automatically after the user did not change
326 // the text within one second
327 m_startSearchTimer = new QTimer(this);
328 m_startSearchTimer->setSingleShot(true);
329 m_startSearchTimer->setInterval(1000);
330 connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
331 }
332
333 bool DolphinSearchBox::isSearchPathIndexed() const
334 {
335 return true;
336 #ifdef HAVE_NEPOMUK
337 const QString path = m_searchPath.path();
338
339 const KConfig strigiConfig("nepomukstrigirc");
340 const QStringList indexedFolders = strigiConfig.group("General").readPathEntry("folders", QStringList());
341
342 // Check whether the current search path is part of an indexed folder
343 bool isIndexed = false;
344 foreach (const QString& indexedFolder, indexedFolders) {
345 if (path.startsWith(indexedFolder)) {
346 isIndexed = true;
347 break;
348 }
349 }
350
351 if (isIndexed) {
352 // The current search path is part of an indexed folder. Check whether no
353 // excluded folder is part of the search path.
354 const QStringList excludedFolders = strigiConfig.group("General").readPathEntry("exclude folders", QStringList());
355 foreach (const QString& excludedFolder, excludedFolders) {
356 // trueg: this is still not correct since there might be an include folder in the exclude folder
357 if (path.startsWith(excludedFolder)) {
358 isIndexed = false;
359 break;
360 }
361 }
362 }
363
364 return isIndexed;
365 #else
366 return false;
367 #endif
368 }
369
370 KUrl DolphinSearchBox::nepomukUrlForSearching() const
371 {
372 #ifdef HAVE_NEPOMUK
373 Nepomuk::Query::AndTerm andTerm;
374
375 // Add filter terms
376 foreach (const AbstractSearchFilterWidget* filterWidget, m_filterWidgets) {
377 const Nepomuk::Query::Term term = filterWidget->queryTerm();
378 if (term.isValid()) {
379 andTerm.addSubTerm(term);
380 }
381 }
382
383 // Add input from search filter
384 const QString text = m_searchInput->text();
385 if (!text.isEmpty()) {
386 if ( m_fileNameButton->isChecked() ) {
387 QString regex = QRegExp::escape(text);
388 regex.replace("\\*", QLatin1String( ".*" ));
389 regex.replace("\\?", QLatin1String( "." ));
390 regex.replace("\\", "\\\\");
391 regex.prepend('^');
392 regex.append('$');
393 andTerm.addSubTerm( Nepomuk::Query::ComparisonTerm(
394 Nepomuk::Vocabulary::NFO::fileName(),
395 Nepomuk::Query::LiteralTerm( regex ),
396 Nepomuk::Query::ComparisonTerm::Regexp ) );
397 }
398 else {
399 const Nepomuk::Query::Query customQuery = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern);
400 if (customQuery.isValid()) {
401 andTerm.addSubTerm(customQuery.term());
402 }
403 }
404 }
405
406 Nepomuk::Query::FileQuery fileQuery;
407 fileQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFiles);
408 fileQuery.setTerm(andTerm);
409
410 return fileQuery.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
411 "Query Results from '%1'",
412 text));
413 #else
414 return KUrl();
415 #endif
416 }
417
418 #include "dolphinsearchbox.moc"