[dolphin/search] Fix search behavior when selecting "Your Files"

Summary:
Fix the search box forgetting the location where the user was previously searching into.

To do a search on "All Files" instead of setting the `m_searchPath` to `$HOME`, it checks the button state, so the "From Here" location is not lost.
As an added benefit, selecting "Your Files" when in a non-indexed folder will use the baloo search instead of a fully non-indexed search from $HOME.

This issue is the last remaining one of the series started with D24422, with the purpose of fixing the searchbox parsing and update.

Test Plan:
- Toggle between "From Here/Your Files" and navigate between locations
- The search box remembers the location and keeps a coherent state

{F7575402}

Reviewers: #dolphin, elvisangelaccio

Subscribers: kfm-devel

Tags: #dolphin

Differential Revision: https://phabricator.kde.org/D24577
This commit is contained in:
Ismael Asensio 2019-10-20 11:43:32 +02:00 committed by Elvis Angelaccio
parent 0caab3c0a2
commit 167d5184ed
2 changed files with 24 additions and 22 deletions

View file

@ -85,34 +85,36 @@ void DolphinSearchBox::setSearchPath(const QUrl& url)
return; return;
} }
const QUrl cleanedUrl = url.adjusted(QUrl::RemoveUserInfo | QUrl::StripTrailingSlash);
if (cleanedUrl.path() == QDir::homePath()) {
m_fromHereButton->setChecked(false);
m_everywhereButton->setChecked(true);
if (!m_searchPath.isEmpty()) {
return;
}
} else {
m_everywhereButton->setChecked(false);
m_fromHereButton->setChecked(true);
}
m_searchPath = url; m_searchPath = url;
QFontMetrics metrics(m_fromHereButton->font()); QFontMetrics metrics(m_fromHereButton->font());
const int maxWidth = metrics.height() * 8; const int maxWidth = metrics.height() * 8;
const QUrl cleanedUrl = url.adjusted(QUrl::RemoveUserInfo | QUrl::StripTrailingSlash);
QString location = cleanedUrl.fileName(); QString location = cleanedUrl.fileName();
if (location.isEmpty()) { if (location.isEmpty()) {
location = cleanedUrl.toString(QUrl::PreferLocalFile); location = cleanedUrl.toString(QUrl::PreferLocalFile);
} }
if (m_fromHereButton->isChecked() && cleanedUrl.path() == QDir::homePath()) {
m_fromHereButton->setChecked(false);
m_everywhereButton->setChecked(true);
} else {
m_fromHereButton->setChecked(true);
m_everywhereButton->setChecked(false);
}
const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth); const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation)); m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
m_fromHereButton->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl.toString(QUrl::PreferLocalFile))); m_fromHereButton->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl.toString(QUrl::PreferLocalFile)));
setFacetsVisible(SearchSettings::showFacetsWidget());
} }
QUrl DolphinSearchBox::searchPath() const QUrl DolphinSearchBox::searchPath() const
{ {
return m_searchPath; return m_everywhereButton->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath;
} }
QUrl DolphinSearchBox::urlForSearching() const QUrl DolphinSearchBox::urlForSearching() const
@ -130,13 +132,7 @@ QUrl DolphinSearchBox::urlForSearching() const
query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes")); query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
} }
QString encodedUrl; query.addQueryItem(QStringLiteral("url"), searchPath().url());
if (m_everywhereButton->isChecked()) {
encodedUrl = QDir::homePath();
} else {
encodedUrl = m_searchPath.url();
}
query.addQueryItem(QStringLiteral("url"), encodedUrl);
url.setQuery(query); url.setQuery(query);
} }
@ -151,12 +147,18 @@ void DolphinSearchBox::fromSearchUrl(const QUrl& url)
} else if (url.scheme() == QLatin1String("filenamesearch")) { } else if (url.scheme() == QLatin1String("filenamesearch")) {
const QUrlQuery query(url); const QUrlQuery query(url);
setText(query.queryItemValue(QStringLiteral("search"))); setText(query.queryItemValue(QStringLiteral("search")));
if (m_searchPath.scheme() != url.scheme()) {
m_searchPath = QUrl();
}
setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile)); setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile));
m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes")); m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
} else { } else {
setText(QString()); setText(QString());
m_searchPath = QUrl();
setSearchPath(url); setSearchPath(url);
} }
setFacetsVisible(SearchSettings::showFacetsWidget());
} }
void DolphinSearchBox::selectAll() void DolphinSearchBox::selectAll()
@ -592,7 +594,7 @@ bool DolphinSearchBox::isIndexingEnabled() const
{ {
#ifdef HAVE_BALOO #ifdef HAVE_BALOO
const Baloo::IndexerConfig searchInfo; const Baloo::IndexerConfig searchInfo;
return searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile()); return searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(searchPath().toLocalFile());
#else #else
return false; return false;
#endif #endif

View file

@ -62,8 +62,8 @@ public:
QString text() const; QString text() const;
/** /**
* Sets the current path that is used as root for * Sets the current path that is used as root for searching files.
* searching files, if "From Here" has been selected. * If @url is the Home dir, "From Here" is selected instead.
*/ */
void setSearchPath(const QUrl& url); void setSearchPath(const QUrl& url);
QUrl searchPath() const; QUrl searchPath() const;