Commit 537dc786 authored by Piotr Henryk Dabrowski's avatar Piotr Henryk Dabrowski Committed by Elvis Angelaccio
Browse files

[Dolphin] Open Preferred Search Tool action

Summary:
Added "Open Preferred Search Tool" action to Tools menu.

It runs preferred (topmost) external search tool as configured in the "More Search Tools" menu.

By default Ctrl+Shift+F shortcut is assigned to this action.

FEATURE: 384798
FIXED-IN: 20.03.80

{F7134238}
{F7134240}
{F7134242}

Reviewers: #dolphin, ngraham, elvisangelaccio

Reviewed By: #dolphin, ngraham

Subscribers: pkloc, kfm-devel, kde-doc-english

Tags: #dolphin, #documentation

Differential Revision: https://phabricator.kde.org/D22594
parent 808339bf
......@@ -2018,6 +2018,17 @@ for this action.</para></listitem>
<listitem><para><action>Opens &konsole; within the current folder.</action></para></listitem>
</varlistentry>
<varlistentry>
<term><menuchoice>
<shortcut>
<keycombo action="simul">&Ctrl;&Shift;<keycap>F</keycap></keycombo>
</shortcut>
<guimenu>Tools</guimenu>
<guimenuitem>Open Preferred Search Tool</guimenuitem>
</menuchoice></term>
<listitem><para><action>Opens preferred search tool in the current folder.</action></para></listitem>
</varlistentry>
<varlistentry>
<term><menuchoice>
<guimenu>Tools</guimenu>
......
......@@ -57,6 +57,7 @@
#include <KJobWidgets>
#include <KLocalizedString>
#include <KMessageBox>
#include <KNS3/KMoreToolsMenuFactory>
#include <KProtocolInfo>
#include <KProtocolManager>
#include <KRun>
......@@ -197,6 +198,8 @@ DolphinMainWindow::DolphinMainWindow() :
toolBar()->installEventFilter(middleClickEventFilter);
setupWhatsThis();
QTimer::singleShot(0, this, &DolphinMainWindow::setupUpdateOpenPreferredSearchToolAction);
}
DolphinMainWindow::~DolphinMainWindow()
......@@ -933,23 +936,88 @@ void DolphinMainWindow::toggleShowMenuBar()
}
}
void DolphinMainWindow::openTerminal()
QString DolphinMainWindow::activeContainerLocalPath()
{
QString dir(QDir::homePath());
// If the given directory is not local, it can still be the URL of an
// ioslave using UDS_LOCAL_PATH which to be converted first.
KIO::StatJob* statJob = KIO::mostLocalUrl(m_activeViewContainer->url());
KJobWidgets::setWindow(statJob, this);
statJob->exec();
QUrl url = statJob->mostLocalUrl();
//If the URL is local after the above conversion, set the directory.
if (url.isLocalFile()) {
dir = url.toLocalFile();
return url.toLocalFile();
}
return QDir::homePath();
}
QPointer<QAction> DolphinMainWindow::preferredSearchTool()
{
m_searchTools.clear();
KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
&m_searchTools, { "files-find" }, QUrl::fromLocalFile(activeContainerLocalPath())
);
QList<QAction*> actions = m_searchTools.actions();
if (actions.isEmpty()) {
return nullptr;
}
QAction* action = actions.first();
if (action->isSeparator()) {
return nullptr;
}
return action;
}
void DolphinMainWindow::setupUpdateOpenPreferredSearchToolAction()
{
QAction* openPreferredSearchTool = actionCollection()->action(QStringLiteral("open_preferred_search_tool"));
const QList<QWidget*> widgets = openPreferredSearchTool->associatedWidgets();
for (QWidget* widget : widgets) {
QMenu* menu = qobject_cast<QMenu*>(widget);
if (menu) {
connect(menu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
}
}
KToolInvocation::invokeTerminal(QString(), dir);
// Update the open_preferred_search_tool action *before* the Configure Shortcuts window is shown,
// since this action is then listed in that window and it should be up-to-date when it is displayed.
// This update is instantaneous if user made no changes to the search tools in the meantime.
// Maybe all KStandardActions should defer calls to their slots, so that we could simply connect() to trigger()?
connect(
actionCollection()->action(KStandardAction::name(KStandardAction::KeyBindings)), &QAction::hovered,
this, &DolphinMainWindow::updateOpenPreferredSearchToolAction
);
updateOpenPreferredSearchToolAction();
}
void DolphinMainWindow::updateOpenPreferredSearchToolAction()
{
QAction* openPreferredSearchTool = actionCollection()->action(QStringLiteral("open_preferred_search_tool"));
if (!openPreferredSearchTool) {
return;
}
QPointer<QAction> tool = preferredSearchTool();
if (tool) {
openPreferredSearchTool->setVisible(true);
openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open %1", tool->text()));
openPreferredSearchTool->setIcon(tool->icon());
} else {
openPreferredSearchTool->setVisible(false);
// still visible in Shortcuts configuration window
openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open Preferred Search Tool"));
openPreferredSearchTool->setIcon(QIcon::fromTheme(QStringLiteral("search")));
}
}
void DolphinMainWindow::openPreferredSearchTool()
{
QPointer<QAction> tool = preferredSearchTool();
if (tool) {
tool->trigger();
}
}
void DolphinMainWindow::openTerminal()
{
KToolInvocation::invokeTerminal(QString(), activeContainerLocalPath());
}
void DolphinMainWindow::editSettings()
......@@ -1091,7 +1159,9 @@ void DolphinMainWindow::updateControlMenu()
// Add a curated assortment of items from the "Tools" menu
addActionToMenu(ac->action(QStringLiteral("show_filter_bar")), menu);
addActionToMenu(ac->action(QStringLiteral("open_preferred_search_tool")), menu);
addActionToMenu(ac->action(QStringLiteral("open_terminal")), menu);
connect(menu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
menu->addSeparator();
......@@ -1465,6 +1535,15 @@ void DolphinMainWindow::setupActions()
compareFiles->setEnabled(false);
connect(compareFiles, &QAction::triggered, this, &DolphinMainWindow::compareFiles);
QAction* openPreferredSearchTool = actionCollection()->addAction(QStringLiteral("open_preferred_search_tool"));
openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open Preferred Search Tool"));
openPreferredSearchTool->setWhatsThis(xi18nc("@info:whatsthis",
"<para>This opens a preferred search tool for the viewed location.</para>"
"<para>Use <emphasis>More Search Tools</emphasis> menu to configure it.</para>"));
openPreferredSearchTool->setIcon(QIcon::fromTheme(QStringLiteral("search")));
actionCollection()->setDefaultShortcut(openPreferredSearchTool, Qt::CTRL + Qt::SHIFT + Qt::Key_F);
connect(openPreferredSearchTool, &QAction::triggered, this, &DolphinMainWindow::openPreferredSearchTool);
#ifdef HAVE_TERMINAL
if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
QAction* openTerminal = actionCollection()->addAction(QStringLiteral("open_terminal"));
......@@ -2207,6 +2286,8 @@ bool DolphinMainWindow::event(QEvent *event)
QWhatsThisClickedEvent* whatsThisEvent = dynamic_cast<QWhatsThisClickedEvent*>(event);
QDesktopServices::openUrl(QUrl(whatsThisEvent->href()));
return true;
} else if (event->type() == QEvent::WindowActivate) {
updateOpenPreferredSearchToolAction();
}
return KXmlGuiWindow::event(event);
}
......
......@@ -30,6 +30,7 @@
#include <QIcon>
#include <QList>
#include <QMenu>
#include <QPointer>
#include <QUrl>
#include <QVector>
......@@ -352,6 +353,15 @@ private slots:
*/
void toggleShowMenuBar();
/** Sets up updates for "Open Preferred Search Tool" action. */
void setupUpdateOpenPreferredSearchToolAction();
/** Updates "Open Preferred Search Tool" action. */
void updateOpenPreferredSearchToolAction();
/** Opens preferred search tool for the current location. */
void openPreferredSearchTool();
/** Opens a terminal window for the current location. */
void openTerminal();
......@@ -597,6 +607,15 @@ private:
/** Adds "What's This?" texts to many widgets and StandardActions. */
void setupWhatsThis();
/**
* Returns the KIO::StatJob::mostLocalUrl() for the active container URL
* if it's a local file. Otherwise returns the user's home path.
*/
QString activeContainerLocalPath();
/** Returns preferred search tool as configured in "More Search Tools" menu. */
QPointer<QAction> preferredSearchTool();
private:
/**
* Implements a custom error handling for the undo manager. This
......@@ -633,6 +652,9 @@ private:
KToolBarPopupAction* m_backAction;
KToolBarPopupAction* m_forwardAction;
QMenu m_searchTools;
};
inline DolphinViewContainer* DolphinMainWindow::activeViewContainer() const
......
......@@ -40,6 +40,7 @@
#include <KLocalizedString>
#include <KMessageBox>
#include <KMimeTypeEditor>
#include <KNS3/KMoreToolsMenuFactory>
#include <KPluginFactory>
#include <KRun>
#include <KSharedConfig>
......@@ -553,7 +554,16 @@ void DolphinPart::slotOpenTerminal()
void DolphinPart::slotFindFile()
{
KRun::run(QStringLiteral("kfind"), {url()}, widget());
QMenu searchTools;
KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
&searchTools, { "files-find" }, QUrl::fromLocalFile(KParts::ReadOnlyPart::localFilePath())
);
QList<QAction*> actions = searchTools.actions();
if (!(actions.isEmpty())) {
actions.first()->trigger();
} else {
KRun::run(QStringLiteral("kfind"), {url()}, widget());
}
}
void DolphinPart::updateNewMenu()
......
......@@ -194,7 +194,7 @@ private Q_SLOTS:
void slotOpenTerminal();
/**
* Open KFind with the current path.
* Open preferred search tool in the current directory to find files.
*/
void slotFindFile();
......
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="dolphin" version="27">
<kpartgui name="dolphin" version="28">
<MenuBar>
<Menu name="file">
<Action name="new_menu" />
......@@ -54,6 +54,7 @@
</Menu>
<Menu name="tools">
<Action name="show_filter_bar" />
<Action name="open_preferred_search_tool" />
<Action name="open_terminal" />
<Action name="compare_files" />
<Action name="change_remote_encoding" />
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment