]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
1c1accd26961f00b7876a4e2d25933024635ae1d
[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 #include "panels/places/placesitemmodel.h"
25
26 #include <KLocalizedString>
27 #include <KNS3/KMoreToolsMenuFactory>
28 #include <KSeparator>
29 #include <config-baloo.h>
30 #ifdef HAVE_BALOO
31 #include <Baloo/Query>
32 #include <Baloo/IndexerConfig>
33 #endif
34
35 #include <QButtonGroup>
36 #include <QDir>
37 #include <QFontDatabase>
38 #include <QHBoxLayout>
39 #include <QIcon>
40 #include <QLabel>
41 #include <QLineEdit>
42 #include <QScrollArea>
43 #include <QTimer>
44 #include <QToolButton>
45 #include <QUrlQuery>
46
47 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
48 QWidget(parent),
49 m_startedSearching(false),
50 m_active(true),
51 m_topLayout(nullptr),
52 m_searchInput(nullptr),
53 m_saveSearchAction(nullptr),
54 m_optionsScrollArea(nullptr),
55 m_fileNameButton(nullptr),
56 m_contentButton(nullptr),
57 m_separator(nullptr),
58 m_fromHereButton(nullptr),
59 m_everywhereButton(nullptr),
60 m_facetsToggleButton(nullptr),
61 m_facetsWidget(nullptr),
62 m_searchPath(),
63 m_startSearchTimer(nullptr)
64 {
65 }
66
67 DolphinSearchBox::~DolphinSearchBox()
68 {
69 saveSettings();
70 }
71
72 void DolphinSearchBox::setText(const QString& text)
73 {
74 m_searchInput->setText(text);
75 }
76
77 QString DolphinSearchBox::text() const
78 {
79 return m_searchInput->text();
80 }
81
82 void DolphinSearchBox::setSearchPath(const QUrl& url)
83 {
84 if (url == m_searchPath) {
85 return;
86 }
87
88 m_searchPath = url;
89
90 QFontMetrics metrics(m_fromHereButton->font());
91 const int maxWidth = metrics.height() * 8;
92
93 const QUrl cleanedUrl = url.adjusted(QUrl::RemoveUserInfo | QUrl::StripTrailingSlash);
94 QString location = cleanedUrl.fileName();
95 if (location.isEmpty()) {
96 location = cleanedUrl.toString(QUrl::PreferLocalFile);
97 }
98 if (m_fromHereButton->isChecked() && cleanedUrl.path() == QDir::homePath()) {
99 m_fromHereButton->setChecked(false);
100 m_everywhereButton->setChecked(true);
101 } else {
102 m_fromHereButton->setChecked(true);
103 m_everywhereButton->setChecked(false);
104 }
105
106 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
107 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
108 m_fromHereButton->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl.toString(QUrl::PreferLocalFile)));
109
110 m_facetsWidget->setEnabled(isIndexingEnabled());
111 }
112
113 QUrl DolphinSearchBox::searchPath() const
114 {
115 return m_searchPath;
116 }
117
118 QUrl DolphinSearchBox::urlForSearching() const
119 {
120 QUrl url;
121
122 if (isIndexingEnabled()) {
123 url = balooUrlForSearching();
124 } else {
125 url.setScheme(QStringLiteral("filenamesearch"));
126
127 QUrlQuery query;
128 query.addQueryItem(QStringLiteral("search"), m_searchInput->text());
129 if (m_contentButton->isChecked()) {
130 query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
131 }
132
133 QString encodedUrl;
134 if (m_everywhereButton->isChecked()) {
135 encodedUrl = QDir::homePath();
136 } else {
137 encodedUrl = m_searchPath.url();
138 }
139 query.addQueryItem(QStringLiteral("url"), encodedUrl);
140
141 url.setQuery(query);
142 }
143
144 return url;
145 }
146
147 void DolphinSearchBox::fromSearchUrl(const QUrl& url)
148 {
149 if (url.scheme() == QLatin1String("baloosearch")) {
150 fromBalooSearchUrl(url);
151 } else if (url.scheme() == QLatin1String("filenamesearch")) {
152 const QUrlQuery query(url);
153 setText(query.queryItemValue(QStringLiteral("search")));
154 setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile));
155 m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
156 } else {
157 setText(QString());
158 setSearchPath(url);
159 }
160 }
161
162 void DolphinSearchBox::selectAll()
163 {
164 m_searchInput->selectAll();
165 }
166
167 void DolphinSearchBox::setActive(bool active)
168 {
169 if (active != m_active) {
170 m_active = active;
171
172 if (active) {
173 emit activated();
174 }
175 }
176 }
177
178 bool DolphinSearchBox::isActive() const
179 {
180 return m_active;
181 }
182
183 bool DolphinSearchBox::event(QEvent* event)
184 {
185 if (event->type() == QEvent::Polish) {
186 init();
187 }
188 return QWidget::event(event);
189 }
190
191 void DolphinSearchBox::showEvent(QShowEvent* event)
192 {
193 if (!event->spontaneous()) {
194 m_searchInput->setFocus();
195 m_startedSearching = false;
196 }
197 }
198
199 void DolphinSearchBox::hideEvent(QHideEvent* event)
200 {
201 Q_UNUSED(event);
202 m_startedSearching = false;
203 m_startSearchTimer->stop();
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 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
223 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
224 // To avoid this issue, we delay the activation of the search box.
225 // We also don't want to schedule the activation process if we are already active,
226 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
227 if (!isActive()) {
228 QTimer::singleShot(0, this, [this] {
229 setActive(true);
230 setFocus();
231 });
232 }
233 break;
234
235 default:
236 break;
237 }
238
239 return QObject::eventFilter(obj, event);
240 }
241
242 void DolphinSearchBox::emitSearchRequest()
243 {
244 m_startSearchTimer->stop();
245 m_startedSearching = true;
246 m_saveSearchAction->setEnabled(true);
247 emit searchRequest();
248 }
249
250 void DolphinSearchBox::emitCloseRequest()
251 {
252 m_startSearchTimer->stop();
253 m_startedSearching = false;
254 m_saveSearchAction->setEnabled(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::slotSearchSaved()
298 {
299 const QUrl searchURL = urlForSearching();
300 if (searchURL.isValid()) {
301 PlacesItemModel model;
302 const QString label = i18n("Search for %1 in %2", text(), searchPath().fileName());
303 model.createPlacesItem(label,
304 searchURL,
305 QStringLiteral("folder-saved-search-symbolic"));
306 }
307 }
308
309 void DolphinSearchBox::initButton(QToolButton* button)
310 {
311 button->installEventFilter(this);
312 button->setAutoExclusive(true);
313 button->setAutoRaise(true);
314 button->setCheckable(true);
315 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
316 }
317
318 void DolphinSearchBox::loadSettings()
319 {
320 if (SearchSettings::location() == QLatin1String("Everywhere")) {
321 m_everywhereButton->setChecked(true);
322 } else {
323 m_fromHereButton->setChecked(true);
324 }
325
326 if (SearchSettings::what() == QLatin1String("Content")) {
327 m_contentButton->setChecked(true);
328 } else {
329 m_fileNameButton->setChecked(true);
330 }
331
332 m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
333 }
334
335 void DolphinSearchBox::saveSettings()
336 {
337 SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
338 SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
339 SearchSettings::setShowFacetsWidget(m_facetsToggleButton->isChecked());
340 SearchSettings::self()->save();
341 }
342
343 void DolphinSearchBox::init()
344 {
345 // Create close button
346 QToolButton* closeButton = new QToolButton(this);
347 closeButton->setAutoRaise(true);
348 closeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
349 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
350 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
351
352 // Create search box
353 m_searchInput = new QLineEdit(this);
354 m_searchInput->setPlaceholderText(i18n("Search..."));
355 m_searchInput->installEventFilter(this);
356 m_searchInput->setClearButtonEnabled(true);
357 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
358 connect(m_searchInput, &QLineEdit::returnPressed,
359 this, &DolphinSearchBox::slotReturnPressed);
360 connect(m_searchInput, &QLineEdit::textChanged,
361 this, &DolphinSearchBox::slotSearchTextChanged);
362 setFocusProxy(m_searchInput);
363
364 // Add "Save search" button inside search box
365 m_saveSearchAction = new QAction(this);
366 m_saveSearchAction->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
367 m_saveSearchAction->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
368 m_saveSearchAction->setEnabled(false);
369 m_searchInput->addAction(m_saveSearchAction, QLineEdit::TrailingPosition);
370 connect(m_saveSearchAction, &QAction::triggered, this, &DolphinSearchBox::slotSearchSaved);
371
372 // Apply layout for the search input
373 QHBoxLayout* searchInputLayout = new QHBoxLayout();
374 searchInputLayout->setContentsMargins(0, 0, 0, 0);
375 searchInputLayout->addWidget(closeButton);
376 searchInputLayout->addWidget(m_searchInput);
377
378 // Create "Filename" and "Content" button
379 m_fileNameButton = new QToolButton(this);
380 m_fileNameButton->setText(i18nc("action:button", "Filename"));
381 initButton(m_fileNameButton);
382
383 m_contentButton = new QToolButton();
384 m_contentButton->setText(i18nc("action:button", "Content"));
385 initButton(m_contentButton);
386
387 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
388 searchWhatGroup->addButton(m_fileNameButton);
389 searchWhatGroup->addButton(m_contentButton);
390
391 m_separator = new KSeparator(Qt::Vertical, this);
392
393 // Create "From Here" and "Your files" buttons
394 m_fromHereButton = new QToolButton(this);
395 m_fromHereButton->setText(i18nc("action:button", "From Here"));
396 initButton(m_fromHereButton);
397
398 m_everywhereButton = new QToolButton(this);
399 m_everywhereButton->setText(i18nc("action:button", "Your files"));
400 m_everywhereButton->setToolTip(i18nc("action:button", "Search in your home directory"));
401 m_everywhereButton->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
402 m_everywhereButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
403 initButton(m_everywhereButton);
404
405 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
406 searchLocationGroup->addButton(m_fromHereButton);
407 searchLocationGroup->addButton(m_everywhereButton);
408
409 auto moreSearchToolsButton = new QToolButton(this);
410 moreSearchToolsButton->setAutoRaise(true);
411 moreSearchToolsButton->setPopupMode(QToolButton::InstantPopup);
412 moreSearchToolsButton->setIcon(QIcon::fromTheme("arrow-down-double"));
413 moreSearchToolsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
414 moreSearchToolsButton->setText(i18n("More Search Tools"));
415 moreSearchToolsButton->setMenu(new QMenu(this));
416 connect(moreSearchToolsButton->menu(), &QMenu::aboutToShow, moreSearchToolsButton->menu(), [this, moreSearchToolsButton]()
417 {
418 m_menuFactory.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
419 moreSearchToolsButton->menu()->clear();
420 m_menuFactory->fillMenuFromGroupingNames(moreSearchToolsButton->menu(), { "files-find" }, this->m_searchPath);
421 } );
422
423 // Create "Facets" widgets
424 m_facetsToggleButton = new QToolButton(this);
425 m_facetsToggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
426 initButton(m_facetsToggleButton);
427 connect(m_facetsToggleButton, &QToolButton::clicked, this, &DolphinSearchBox::slotFacetsButtonToggled);
428
429 m_facetsWidget = new DolphinFacetsWidget(this);
430 m_facetsWidget->installEventFilter(this);
431 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
432 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
433
434 // Apply layout for the options
435 QHBoxLayout* optionsLayout = new QHBoxLayout();
436 optionsLayout->setContentsMargins(0, 0, 0, 0);
437 optionsLayout->addWidget(m_fileNameButton);
438 optionsLayout->addWidget(m_contentButton);
439 optionsLayout->addWidget(m_separator);
440 optionsLayout->addWidget(m_fromHereButton);
441 optionsLayout->addWidget(m_everywhereButton);
442 optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
443 optionsLayout->addWidget(m_facetsToggleButton);
444 optionsLayout->addWidget(moreSearchToolsButton);
445 optionsLayout->addStretch(1);
446
447 // Put the options into a QScrollArea. This prevents increasing the view width
448 // in case that not enough width for the options is available.
449 QWidget* optionsContainer = new QWidget(this);
450 optionsContainer->setLayout(optionsLayout);
451
452 m_optionsScrollArea = new QScrollArea(this);
453 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
454 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
455 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
456 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
457 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
458 m_optionsScrollArea->setWidget(optionsContainer);
459 m_optionsScrollArea->setWidgetResizable(true);
460
461 m_topLayout = new QVBoxLayout(this);
462 m_topLayout->setContentsMargins(0, 0, 0, 0);
463 m_topLayout->addLayout(searchInputLayout);
464 m_topLayout->addWidget(m_optionsScrollArea);
465 m_topLayout->addWidget(m_facetsWidget);
466
467 loadSettings();
468
469 // The searching should be started automatically after the user did not change
470 // the text within one second
471 m_startSearchTimer = new QTimer(this);
472 m_startSearchTimer->setSingleShot(true);
473 m_startSearchTimer->setInterval(1000);
474 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
475
476 updateFacetsToggleButton();
477 }
478
479 QUrl DolphinSearchBox::balooUrlForSearching() const
480 {
481 #ifdef HAVE_BALOO
482 const QString text = m_searchInput->text();
483
484 Baloo::Query query;
485 query.addType(m_facetsWidget->facetType());
486
487 QStringList queryStrings;
488 QString ratingQuery = m_facetsWidget->ratingTerm();
489 if (!ratingQuery.isEmpty()) {
490 queryStrings << ratingQuery;
491 }
492
493 if (m_contentButton->isChecked()) {
494 queryStrings << text;
495 } else if (!text.isEmpty()) {
496 queryStrings << QStringLiteral("filename:\"%1\"").arg(text);
497 }
498
499 if (m_fromHereButton->isChecked()) {
500 query.setIncludeFolder(m_searchPath.toLocalFile());
501 }
502
503 query.setSearchString(queryStrings.join(QLatin1Char(' ')));
504
505 return query.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
506 "Query Results from '%1'", text));
507 #else
508 return QUrl();
509 #endif
510 }
511
512 void DolphinSearchBox::fromBalooSearchUrl(const QUrl& url)
513 {
514 #ifdef HAVE_BALOO
515 const Baloo::Query query = Baloo::Query::fromSearchUrl(url);
516
517 // Block all signals to avoid unnecessary "searchRequest" signals
518 // while we adjust the search text and the facet widget.
519 blockSignals(true);
520
521 const QString customDir = query.includeFolder();
522 if (!customDir.isEmpty()) {
523 setSearchPath(QUrl::fromLocalFile(customDir));
524 } else {
525 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
526 }
527
528 setText(query.searchString());
529
530 QStringList types = query.types();
531 if (!types.isEmpty()) {
532 m_facetsWidget->setFacetType(types.first());
533 }
534
535 const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
536 foreach (const QString& subTerm, subTerms) {
537 if (subTerm.startsWith(QLatin1String("filename:"))) {
538 const QString value = subTerm.mid(9);
539 setText(value);
540 } else if (m_facetsWidget->isRatingTerm(subTerm)) {
541 m_facetsWidget->setRatingTerm(subTerm);
542 }
543 }
544
545 m_startSearchTimer->stop();
546 blockSignals(false);
547 #else
548 Q_UNUSED(url);
549 #endif
550 }
551
552 void DolphinSearchBox::updateFacetsToggleButton()
553 {
554 const bool facetsIsVisible = SearchSettings::showFacetsWidget();
555 m_facetsToggleButton->setChecked(facetsIsVisible ? true : false);
556 m_facetsToggleButton->setIcon(QIcon::fromTheme(facetsIsVisible ? QStringLiteral("arrow-up-double") : QStringLiteral("arrow-down-double")));
557 m_facetsToggleButton->setText(facetsIsVisible ? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
558 }
559
560 bool DolphinSearchBox::isIndexingEnabled() const
561 {
562 #ifdef HAVE_BALOO
563 const Baloo::IndexerConfig searchInfo;
564 return searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
565 #else
566 return false;
567 #endif
568 }