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