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