]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
Merge branch 'Applications/16.12'
[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 <QLineEdit>
27 #include <KLocalizedString>
28 #include <KSeparator>
29 #include <KNS3/KMoreToolsMenuFactory>
30
31 #include <QButtonGroup>
32 #include <QDir>
33 #include <QEvent>
34 #include <QHBoxLayout>
35 #include <QKeyEvent>
36 #include <QLabel>
37 #include <QScrollArea>
38 #include <QTimer>
39 #include <QToolButton>
40 #include <QVBoxLayout>
41 #include <QUrlQuery>
42
43 #include <config-baloo.h>
44 #ifdef HAVE_BALOO
45 #include <Baloo/Query>
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 QUrl& 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 = QStringLiteral("/");
96 } else {
97 location = url.scheme() + 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 QUrl DolphinSearchBox::searchPath() const
118 {
119 return m_searchPath;
120 }
121
122 QUrl DolphinSearchBox::urlForSearching() const
123 {
124 QUrl 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.setScheme(QStringLiteral("filenamesearch"));
134
135 QUrlQuery query;
136 query.addQueryItem(QStringLiteral("search"), m_searchInput->text());
137 if (m_contentButton->isChecked()) {
138 query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
139 }
140
141 QString encodedUrl;
142 if (m_everywhereButton->isChecked()) {
143 // It is very unlikely, that the majority of Dolphins target users
144 // mean "the whole harddisk" instead of "my home folder" when
145 // selecting the "Everywhere" button.
146 encodedUrl = QDir::homePath();
147 } else {
148 encodedUrl = m_searchPath.url();
149 }
150 query.addQueryItem(QStringLiteral("url"), encodedUrl);
151
152 url.setQuery(query);
153 }
154
155 return url;
156 }
157
158 void DolphinSearchBox::fromSearchUrl(const QUrl& url)
159 {
160 if (url.scheme() == QLatin1String("baloosearch")) {
161 fromBalooSearchUrl(url);
162 } else if (url.scheme() == QLatin1String("filenamesearch")) {
163 const QUrlQuery query(url);
164 setText(query.queryItemValue(QStringLiteral("search")));
165 setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile));
166 m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
167 } else {
168 setText(QString());
169 setSearchPath(url);
170 }
171 }
172
173 void DolphinSearchBox::selectAll()
174 {
175 m_searchInput->selectAll();
176 }
177
178 void DolphinSearchBox::setActive(bool active)
179 {
180 if (active != m_active) {
181 m_active = active;
182
183 if (active) {
184 emit activated();
185 }
186 }
187 }
188
189 bool DolphinSearchBox::isActive() const
190 {
191 return m_active;
192 }
193
194 bool DolphinSearchBox::event(QEvent* event)
195 {
196 if (event->type() == QEvent::Polish) {
197 init();
198 }
199 return QWidget::event(event);
200 }
201
202 void DolphinSearchBox::showEvent(QShowEvent* event)
203 {
204 if (!event->spontaneous()) {
205 m_searchInput->setFocus();
206 m_startedSearching = false;
207 }
208 }
209
210 void DolphinSearchBox::hideEvent(QHideEvent* event)
211 {
212 Q_UNUSED(event);
213 m_startedSearching = false;
214 m_startSearchTimer->stop();
215 }
216
217 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
218 {
219 QWidget::keyReleaseEvent(event);
220 if (event->key() == Qt::Key_Escape) {
221 if (m_searchInput->text().isEmpty()) {
222 emit closeRequest();
223 } else {
224 m_searchInput->clear();
225 }
226 }
227 }
228
229 bool DolphinSearchBox::eventFilter(QObject* obj, QEvent* event)
230 {
231 switch (event->type()) {
232 case QEvent::FocusIn:
233 setActive(true);
234 setFocus();
235 break;
236
237 default:
238 break;
239 }
240
241 return QObject::eventFilter(obj, event);
242 }
243
244 void DolphinSearchBox::emitSearchRequest()
245 {
246 m_startSearchTimer->stop();
247 m_startedSearching = true;
248 emit searchRequest();
249 }
250
251 void DolphinSearchBox::emitCloseRequest()
252 {
253 m_startSearchTimer->stop();
254 m_startedSearching = false;
255 emit closeRequest();
256 }
257
258 void DolphinSearchBox::slotConfigurationChanged()
259 {
260 saveSettings();
261 if (m_startedSearching) {
262 emitSearchRequest();
263 }
264 }
265
266 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
267 {
268
269 if (text.isEmpty()) {
270 m_startSearchTimer->stop();
271 } else {
272 m_startSearchTimer->start();
273 }
274 emit searchTextChanged(text);
275 }
276
277 void DolphinSearchBox::slotReturnPressed()
278 {
279 emitSearchRequest();
280 emit returnPressed();
281 }
282
283 void DolphinSearchBox::slotFacetsButtonToggled()
284 {
285 const bool facetsIsVisible = !m_facetsWidget->isVisible();
286 m_facetsWidget->setVisible(facetsIsVisible);
287 updateFacetsToggleButton();
288 }
289
290 void DolphinSearchBox::slotFacetChanged()
291 {
292 m_startedSearching = true;
293 m_startSearchTimer->stop();
294 emit searchRequest();
295 }
296
297 void DolphinSearchBox::initButton(QToolButton* button)
298 {
299 button->installEventFilter(this);
300 button->setAutoExclusive(true);
301 button->setAutoRaise(true);
302 button->setCheckable(true);
303 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
304 }
305
306 void DolphinSearchBox::loadSettings()
307 {
308 if (SearchSettings::location() == QLatin1String("Everywhere")) {
309 m_everywhereButton->setChecked(true);
310 } else {
311 m_fromHereButton->setChecked(true);
312 }
313
314 if (SearchSettings::what() == QLatin1String("Content")) {
315 m_contentButton->setChecked(true);
316 } else {
317 m_fileNameButton->setChecked(true);
318 }
319
320 m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
321 }
322
323 void DolphinSearchBox::saveSettings()
324 {
325 SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
326 SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
327 SearchSettings::setShowFacetsWidget(m_facetsToggleButton->isChecked());
328 SearchSettings::self()->save();
329 }
330
331 void DolphinSearchBox::init()
332 {
333 // Create close button
334 QToolButton* closeButton = new QToolButton(this);
335 closeButton->setAutoRaise(true);
336 closeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
337 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
338 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
339
340 // Create search label
341 m_searchLabel = new QLabel(this);
342
343 // Create search box
344 m_searchInput = new QLineEdit(this);
345 m_searchInput->installEventFilter(this);
346 m_searchInput->setClearButtonEnabled(true);
347 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
348 connect(m_searchInput, &QLineEdit::returnPressed,
349 this, &DolphinSearchBox::slotReturnPressed);
350 connect(m_searchInput, &QLineEdit::textChanged,
351 this, &DolphinSearchBox::slotSearchTextChanged);
352 setFocusProxy(m_searchInput);
353
354 // Apply layout for the search input
355 QHBoxLayout* searchInputLayout = new QHBoxLayout();
356 searchInputLayout->setMargin(0);
357 searchInputLayout->addWidget(closeButton);
358 searchInputLayout->addWidget(m_searchLabel);
359 searchInputLayout->addWidget(m_searchInput);
360
361 // Create "Filename" and "Content" button
362 m_fileNameButton = new QToolButton(this);
363 m_fileNameButton->setText(i18nc("action:button", "Filename"));
364 initButton(m_fileNameButton);
365
366 m_contentButton = new QToolButton();
367 m_contentButton->setText(i18nc("action:button", "Content"));
368 initButton(m_contentButton);
369
370 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
371 searchWhatGroup->addButton(m_fileNameButton);
372 searchWhatGroup->addButton(m_contentButton);
373
374 m_separator = new KSeparator(Qt::Vertical, this);
375
376 // Create "From Here" and "Everywhere"button
377 m_fromHereButton = new QToolButton(this);
378 m_fromHereButton->setText(i18nc("action:button", "From Here"));
379 initButton(m_fromHereButton);
380
381 m_everywhereButton = new QToolButton(this);
382 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
383 initButton(m_everywhereButton);
384
385 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
386 searchLocationGroup->addButton(m_fromHereButton);
387 searchLocationGroup->addButton(m_everywhereButton);
388
389 auto moreSearchToolsButton = new QToolButton(this);
390 moreSearchToolsButton->setAutoRaise(true);
391 moreSearchToolsButton->setPopupMode(QToolButton::InstantPopup);
392 moreSearchToolsButton->setIcon(QIcon::fromTheme("arrow-down-double"));
393 moreSearchToolsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
394 moreSearchToolsButton->setText(i18n("More Search Tools"));
395 moreSearchToolsButton->setMenu(new QMenu(this));
396 connect(moreSearchToolsButton->menu(), &QMenu::aboutToShow, moreSearchToolsButton->menu(), [this, moreSearchToolsButton]()
397 {
398 m_menuFactory.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
399 moreSearchToolsButton->menu()->clear();
400 m_menuFactory->fillMenuFromGroupingNames(moreSearchToolsButton->menu(), { "files-find" }, this->m_searchPath);
401 } );
402
403 // Create "Facets" widgets
404 m_facetsToggleButton = new QToolButton(this);
405 m_facetsToggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
406 initButton(m_facetsToggleButton);
407 connect(m_facetsToggleButton, &QToolButton::clicked, this, &DolphinSearchBox::slotFacetsButtonToggled);
408
409 m_facetsWidget = new DolphinFacetsWidget(this);
410 m_facetsWidget->installEventFilter(this);
411 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
412 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
413
414 // Apply layout for the options
415 QHBoxLayout* optionsLayout = new QHBoxLayout();
416 optionsLayout->setMargin(0);
417 optionsLayout->addWidget(m_fileNameButton);
418 optionsLayout->addWidget(m_contentButton);
419 optionsLayout->addWidget(m_separator);
420 optionsLayout->addWidget(m_fromHereButton);
421 optionsLayout->addWidget(m_everywhereButton);
422 optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
423 optionsLayout->addWidget(m_facetsToggleButton);
424 optionsLayout->addWidget(moreSearchToolsButton);
425 optionsLayout->addStretch(1);
426
427 // Put the options into a QScrollArea. This prevents increasing the view width
428 // in case that not enough width for the options is available.
429 QWidget* optionsContainer = new QWidget(this);
430 optionsContainer->setLayout(optionsLayout);
431
432 m_optionsScrollArea = new QScrollArea(this);
433 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
434 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
435 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
436 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
437 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
438 m_optionsScrollArea->setWidget(optionsContainer);
439 m_optionsScrollArea->setWidgetResizable(true);
440
441 m_topLayout = new QVBoxLayout(this);
442 m_topLayout->setMargin(0);
443 m_topLayout->addLayout(searchInputLayout);
444 m_topLayout->addWidget(m_optionsScrollArea);
445 m_topLayout->addWidget(m_facetsWidget);
446
447 loadSettings();
448
449 // The searching should be started automatically after the user did not change
450 // the text within one second
451 m_startSearchTimer = new QTimer(this);
452 m_startSearchTimer->setSingleShot(true);
453 m_startSearchTimer->setInterval(1000);
454 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
455
456 updateFacetsToggleButton();
457 }
458
459 QUrl DolphinSearchBox::balooUrlForSearching() const
460 {
461 #ifdef HAVE_BALOO
462 const QString text = m_searchInput->text();
463
464 Baloo::Query query;
465 query.addType(m_facetsWidget->facetType());
466
467 QStringList queryStrings;
468 QString ratingQuery = m_facetsWidget->ratingTerm();
469 if (!ratingQuery.isEmpty()) {
470 queryStrings << ratingQuery;
471 }
472
473 if (m_contentButton->isChecked()) {
474 queryStrings << text;
475 } else if (!text.isEmpty()) {
476 queryStrings << QStringLiteral("filename:\"%1\"").arg(text);
477 }
478
479 if (m_fromHereButton->isChecked()) {
480 query.setIncludeFolder(m_searchPath.toLocalFile());
481 }
482
483 query.setSearchString(queryStrings.join(QStringLiteral(" ")));
484
485 return query.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
486 "Query Results from '%1'", text));
487 #else
488 return QUrl();
489 #endif
490 }
491
492 void DolphinSearchBox::fromBalooSearchUrl(const QUrl& url)
493 {
494 #ifdef HAVE_BALOO
495 const Baloo::Query query = Baloo::Query::fromSearchUrl(url);
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 QString customDir = query.includeFolder();
502 if (!customDir.isEmpty()) {
503 setSearchPath(QUrl::fromLocalFile(customDir));
504 } else {
505 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
506 }
507
508 setText(query.searchString());
509
510 QStringList types = query.types();
511 if (!types.isEmpty()) {
512 m_facetsWidget->setFacetType(types.first());
513 }
514
515 const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
516 foreach (const QString& subTerm, subTerms) {
517 if (subTerm.startsWith(QLatin1String("filename:"))) {
518 const QString value = subTerm.mid(9);
519 setText(value);
520 } else if (m_facetsWidget->isRatingTerm(subTerm)) {
521 m_facetsWidget->setRatingTerm(subTerm);
522 }
523 }
524
525 m_startSearchTimer->stop();
526 blockSignals(false);
527 #else
528 Q_UNUSED(url);
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 ? QStringLiteral("arrow-up-double") : QStringLiteral("arrow-down-double")));
537 m_facetsToggleButton->setText(facetsIsVisible ? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
538 }
539