]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
Merge branch 'master' into frameworks
[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 #include "dolphinfacetswidget.h"
24
25 #include <KIcon>
26 #include <KLineEdit>
27 #include <KLocale>
28 #include <KSeparator>
29 #include <KGlobalSettings>
30
31 #include <QButtonGroup>
32 #include <QDir>
33 #include <QEvent>
34 #include <QFormLayout>
35 #include <QHBoxLayout>
36 #include <QKeyEvent>
37 #include <QLabel>
38 #include <QScrollArea>
39 #include <QTimer>
40 #include <QToolButton>
41 #include <QVBoxLayout>
42
43 #ifdef HAVE_BALOO
44 #include <Baloo/NaturalFileQueryParser>
45 #include <Baloo/QueryBuilder>
46 #include <Baloo/Query>
47 #include <Baloo/Term>
48 #include <Baloo/IndexerConfig>
49 #endif
50
51 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
52 QWidget(parent),
53 m_startedSearching(false),
54 m_active(true),
55 m_topLayout(0),
56 m_searchLabel(0),
57 m_searchInput(0),
58 m_optionsScrollArea(0),
59 m_fileNameButton(0),
60 m_contentButton(0),
61 m_separator(0),
62 m_fromHereButton(0),
63 m_everywhereButton(0),
64 m_facetsToggleButton(0),
65 m_facetsWidget(0),
66 m_searchPath(),
67 m_startSearchTimer(0)
68 {
69 }
70
71 DolphinSearchBox::~DolphinSearchBox()
72 {
73 saveSettings();
74 }
75
76 void DolphinSearchBox::setText(const QString& text)
77 {
78 m_searchInput->setText(text);
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
90 QFontMetrics metrics(m_fromHereButton->font());
91 const int maxWidth = metrics.height() * 8;
92
93 QString location = url.fileName();
94 if (location.isEmpty()) {
95 if (url.isLocalFile()) {
96 location = QLatin1String("/");
97 } else {
98 location = url.protocol() + QLatin1String(" - ") + url.host();
99 }
100 }
101
102 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
103 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
104
105 const bool showSearchFromButtons = url.isLocalFile();
106 m_separator->setVisible(showSearchFromButtons);
107 m_fromHereButton->setVisible(showSearchFromButtons);
108 m_everywhereButton->setVisible(showSearchFromButtons);
109
110 bool hasFacetsSupport = false;
111 #ifdef HAVE_BALOO
112 const Baloo::IndexerConfig searchInfo;
113 hasFacetsSupport = searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
114 #endif
115 m_facetsWidget->setEnabled(hasFacetsSupport);
116 }
117
118 KUrl DolphinSearchBox::searchPath() const
119 {
120 return m_searchPath;
121 }
122
123 KUrl DolphinSearchBox::urlForSearching() const
124 {
125 KUrl url;
126 bool useBalooSearch = false;
127 #ifdef HAVE_BALOO
128 const Baloo::IndexerConfig searchInfo;
129 useBalooSearch = searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
130 #endif
131 if (useBalooSearch) {
132 url = balooUrlForSearching();
133 } else {
134 url.setProtocol("filenamesearch");
135 url.addQueryItem("search", m_searchInput->text());
136 if (m_contentButton->isChecked()) {
137 url.addQueryItem("checkContent", "yes");
138 }
139
140 QString encodedUrl;
141 if (m_everywhereButton->isChecked()) {
142 // It is very unlikely, that the majority of Dolphins target users
143 // mean "the whole harddisk" instead of "my home folder" when
144 // selecting the "Everywhere" button.
145 encodedUrl = QDir::homePath();
146 } else {
147 encodedUrl = m_searchPath.url();
148 }
149 url.addQueryItem("url", encodedUrl);
150 }
151
152 return url;
153 }
154
155 void DolphinSearchBox::fromSearchUrl(const KUrl& url)
156 {
157 if (url.protocol() == "baloosearch") {
158 fromBalooSearchUrl(url);
159 } else if (url.protocol() == "filenamesearch") {
160 const QMap<QString, QString>& queryItems = url.queryItems();
161 setText(queryItems.value("search"));
162 setSearchPath(queryItems.value("url"));
163 m_contentButton->setChecked(queryItems.value("checkContent") == "yes");
164 } else {
165 setText(QString());
166 setSearchPath(url);
167 }
168 }
169
170 void DolphinSearchBox::selectAll()
171 {
172 m_searchInput->selectAll();
173 }
174
175 void DolphinSearchBox::setActive(bool active)
176 {
177 if (active != m_active) {
178 m_active = active;
179
180 if (active) {
181 emit activated();
182 }
183 }
184 }
185
186 bool DolphinSearchBox::isActive() const
187 {
188 return m_active;
189 }
190
191 bool DolphinSearchBox::event(QEvent* event)
192 {
193 if (event->type() == QEvent::Polish) {
194 init();
195 }
196 return QWidget::event(event);
197 }
198
199 void DolphinSearchBox::showEvent(QShowEvent* event)
200 {
201 if (!event->spontaneous()) {
202 m_searchInput->setFocus();
203 m_startedSearching = false;
204 }
205 }
206
207 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
208 {
209 QWidget::keyReleaseEvent(event);
210 if (event->key() == Qt::Key_Escape) {
211 if (m_searchInput->text().isEmpty()) {
212 emit closeRequest();
213 } else {
214 m_searchInput->clear();
215 }
216 }
217 }
218
219 bool DolphinSearchBox::eventFilter(QObject* obj, QEvent* event)
220 {
221 switch (event->type()) {
222 case QEvent::FocusIn:
223 setActive(true);
224 setFocus();
225 break;
226
227 default:
228 break;
229 }
230
231 return QObject::eventFilter(obj, event);
232 }
233
234 void DolphinSearchBox::emitSearchRequest()
235 {
236 m_startSearchTimer->stop();
237 m_startedSearching = true;
238 emit searchRequest();
239 }
240
241 void DolphinSearchBox::emitCloseRequest()
242 {
243 m_startSearchTimer->stop();
244 m_startedSearching = false;
245 emit closeRequest();
246 }
247
248 void DolphinSearchBox::slotConfigurationChanged()
249 {
250 saveSettings();
251 if (m_startedSearching) {
252 emitSearchRequest();
253 }
254 }
255
256 void DolphinSearchBox::slotSearchTextChanged()
257 {
258 const QString text = m_searchInput->text();
259
260 if (text.isEmpty()) {
261 m_startSearchTimer->stop();
262 } else {
263 m_startSearchTimer->start();
264 }
265 emit searchTextChanged(text);
266 }
267
268 void DolphinSearchBox::slotReturnPressed()
269 {
270 emitSearchRequest();
271 emit returnPressed(m_searchInput->text());
272 }
273
274 void DolphinSearchBox::updateSearchInputParsing()
275 {
276 #ifdef HAVE_BALOO
277 m_searchInput->setParsingEnabled(m_contentButton->isChecked());
278 #endif
279 }
280
281 void DolphinSearchBox::slotFacetsButtonToggled()
282 {
283 const bool facetsIsVisible = !m_facetsWidget->isVisible();
284 m_facetsWidget->setVisible(facetsIsVisible);
285 updateFacetsToggleButton();
286 }
287
288 void DolphinSearchBox::slotFacetChanged()
289 {
290 m_startedSearching = true;
291 m_startSearchTimer->stop();
292 emit searchRequest();
293 }
294
295 void DolphinSearchBox::initButton(QToolButton* button)
296 {
297 button->installEventFilter(this);
298 button->setAutoExclusive(true);
299 button->setAutoRaise(true);
300 button->setCheckable(true);
301 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
302 }
303
304 void DolphinSearchBox::loadSettings()
305 {
306 if (SearchSettings::location() == QLatin1String("Everywhere")) {
307 m_everywhereButton->setChecked(true);
308 } else {
309 m_fromHereButton->setChecked(true);
310 }
311
312 if (SearchSettings::what() == QLatin1String("Content")) {
313 m_contentButton->setChecked(true);
314 } else {
315 m_fileNameButton->setChecked(true);
316 }
317
318 m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
319 updateSearchInputParsing();
320 }
321
322 void DolphinSearchBox::saveSettings()
323 {
324 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
325 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
326 SearchSettings::setShowFacetsWidget(m_facetsToggleButton->isChecked());
327 SearchSettings::self()->writeConfig();
328 }
329
330 void DolphinSearchBox::init()
331 {
332 // Create close button
333 QToolButton* closeButton = new QToolButton(this);
334 closeButton->setAutoRaise(true);
335 closeButton->setIcon(KIcon("dialog-close"));
336 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
337 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
338
339 // Create search label
340 m_searchLabel = new QLabel(this);
341
342 // Create search box
343 #ifdef HAVE_BALOO
344 m_queryParser.reset(new Baloo::NaturalFileQueryParser);
345 m_searchInput = new Baloo::QueryBuilder(m_queryParser.data(), this);
346 connect(m_searchInput, &Baloo::QueryBuilder::editingFinished,
347 this, &DolphinSearchBox::slotReturnPressed);
348 connect(m_searchInput, &Baloo::QueryBuilder::textChanged,
349 this, &DolphinSearchBox::slotSearchTextChanged);
350 #else
351 m_searchInput = new KLineEdit(this);
352 m_searchInput->installEventFilter(this);
353 m_searchInput->setClearButtonShown(true);
354 m_searchInput->setFont(KGlobalSettings::generalFont());
355 connect(m_searchInput, &KLineEdit::returnPressed,
356 this, &DolphinSearchBox::slotReturnPressed);
357 connect(m_searchInput, &KLineEdit::textChanged,
358 this, &DolphinSearchBox::slotSearchTextChanged);
359 #endif
360 setFocusProxy(m_searchInput);
361
362 // Apply layout for the search input
363 QHBoxLayout* searchInputLayout = new QHBoxLayout();
364 searchInputLayout->setMargin(0);
365 searchInputLayout->addWidget(closeButton);
366 searchInputLayout->addWidget(m_searchLabel);
367 searchInputLayout->addWidget(m_searchInput);
368
369 // Create "Filename" and "Content" button
370 m_fileNameButton = new QToolButton(this);
371 m_fileNameButton->setText(i18nc("action:button", "Filename"));
372 initButton(m_fileNameButton);
373
374 m_contentButton = new QToolButton();
375 m_contentButton->setText(i18nc("action:button", "Content"));
376 initButton(m_contentButton);
377
378 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
379 searchWhatGroup->addButton(m_fileNameButton);
380 searchWhatGroup->addButton(m_contentButton);
381 connect(searchWhatGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
382 this, &DolphinSearchBox::updateSearchInputParsing);
383
384 m_separator = new KSeparator(Qt::Vertical, this);
385
386 // Create "From Here" and "Everywhere"button
387 m_fromHereButton = new QToolButton(this);
388 m_fromHereButton->setText(i18nc("action:button", "From Here"));
389 initButton(m_fromHereButton);
390
391 m_everywhereButton = new QToolButton(this);
392 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
393 initButton(m_everywhereButton);
394
395 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
396 searchLocationGroup->addButton(m_fromHereButton);
397 searchLocationGroup->addButton(m_everywhereButton);
398
399 // Create "Facets" widgets
400 m_facetsToggleButton = new QToolButton(this);
401 m_facetsToggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
402 initButton(m_facetsToggleButton);
403 connect(m_facetsToggleButton, &QToolButton::clicked, this, &DolphinSearchBox::slotFacetsButtonToggled);
404
405 m_facetsWidget = new DolphinFacetsWidget(this);
406 m_facetsWidget->installEventFilter(this);
407 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
408 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
409
410 // Apply layout for the options
411 QHBoxLayout* optionsLayout = new QHBoxLayout();
412 optionsLayout->setMargin(0);
413 optionsLayout->addWidget(m_fileNameButton);
414 optionsLayout->addWidget(m_contentButton);
415 optionsLayout->addWidget(m_separator);
416 optionsLayout->addWidget(m_fromHereButton);
417 optionsLayout->addWidget(m_everywhereButton);
418 optionsLayout->addStretch(1);
419 optionsLayout->addWidget(m_facetsToggleButton);
420
421 // Put the options into a QScrollArea. This prevents increasing the view width
422 // in case that not enough width for the options is available.
423 QWidget* optionsContainer = new QWidget(this);
424 optionsContainer->setLayout(optionsLayout);
425
426 m_optionsScrollArea = new QScrollArea(this);
427 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
428 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
429 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
430 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
431 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
432 m_optionsScrollArea->setWidget(optionsContainer);
433 m_optionsScrollArea->setWidgetResizable(true);
434
435 m_topLayout = new QVBoxLayout(this);
436 m_topLayout->setMargin(0);
437 m_topLayout->addLayout(searchInputLayout);
438 m_topLayout->addWidget(m_optionsScrollArea);
439 m_topLayout->addWidget(m_facetsWidget);
440
441 loadSettings();
442
443 // The searching should be started automatically after the user did not change
444 // the text within one second
445 m_startSearchTimer = new QTimer(this);
446 m_startSearchTimer->setSingleShot(true);
447 m_startSearchTimer->setInterval(1000);
448 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
449
450 updateFacetsToggleButton();
451 }
452
453 KUrl DolphinSearchBox::balooUrlForSearching() const
454 {
455 #ifdef HAVE_BALOO
456 const QString text = m_searchInput->text();
457
458 Baloo::Query query;
459
460 if (m_contentButton->isChecked()) {
461 query = m_queryParser->parse(text, Baloo::NaturalQueryParser::DetectFilenamePattern);
462 } else {
463 query.setTerm(Baloo::Term(QLatin1String("filename"), text));
464 }
465
466 // Configure the query so that it returns files and takes the rating into account
467 query.addType("File");
468 query.addType(m_facetsWidget->facetType());
469
470 Baloo::Term term(Baloo::Term::And);
471 Baloo::Term ratingTerm = m_facetsWidget->ratingTerm();
472
473 if (ratingTerm.isValid()) {
474 term.addSubTerm(query.term());
475 term.addSubTerm(ratingTerm);
476
477 query.setTerm(term);
478 }
479
480 if (m_fromHereButton->isChecked()) {
481 query.addCustomOption("includeFolder", m_searchPath.toLocalFile());
482 }
483
484 return query.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
485 "Query Results from '%1'", text));
486 #else
487 return KUrl();
488 #endif
489 }
490
491 void DolphinSearchBox::fromBalooSearchUrl(const KUrl& url)
492 {
493 #ifdef HAVE_BALOO
494 const Baloo::Query query = Baloo::Query::fromSearchUrl(url);
495 const Baloo::Term term = query.term();
496
497 // Block all signals to avoid unnecessary "searchRequest" signals
498 // while we adjust the search text and the facet widget.
499 blockSignals(true);
500
501 const QVariantMap customOptions = query.customOptions();
502 if (customOptions.contains("includeFolder")) {
503 setSearchPath(customOptions.value("includeFolder").toString());
504 } else {
505 setSearchPath(QDir::homePath());
506 }
507
508 if (!query.searchString().isEmpty()) {
509 setText(query.searchString());
510 }
511
512 QStringList types = query.types();
513 types.removeOne("File"); // We are only interested in facet widget types
514 if (!types.isEmpty()) {
515 m_facetsWidget->setFacetType(types.first());
516 }
517
518 foreach (const Baloo::Term& subTerm, term.subTerms()) {
519 const QString property = subTerm.property();
520
521 if (property == QLatin1String("filename")) {
522 setText(subTerm.value().toString());
523 } else if (m_facetsWidget->isRatingTerm(subTerm)) {
524 m_facetsWidget->setRatingTerm(subTerm);
525 }
526 }
527
528 m_startSearchTimer->stop();
529 blockSignals(false);
530 #endif
531 }
532
533 void DolphinSearchBox::updateFacetsToggleButton()
534 {
535 const bool facetsIsVisible = SearchSettings::showFacetsWidget();
536 m_facetsToggleButton->setChecked(facetsIsVisible ? true : false);
537 m_facetsToggleButton->setIcon(KIcon(facetsIsVisible ? "arrow-up-double" : "arrow-down-double"));
538 m_facetsToggleButton->setText(facetsIsVisible ? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
539 }
540