From 9b5f56980bc4d2f399a6b828c94b23f3626c9d21 Mon Sep 17 00:00:00 2001 From: oioi 555 Date: Mon, 2 May 2022 20:25:24 +0000 Subject: [PATCH] Re-add "Open Terminal Here" feature This is equivalent to the "Open Terminal Here" feature that existed until Version 20.12. If the user has selected folders, replace "Open Terminal" in the context menu with "Open Terminal Here". When more than 5 folders are selected, a modal window will ask the user if they are sure they want to open all 6 or more terminal windows. In Detail View, users can also select a file, which will open a terminal at the location of that file. BUG: 452637 FIXED-IN: 22.08 --- src/dolphincontextmenu.cpp | 4 +- src/dolphinmainwindow.cpp | 49 ++++++++++++++++++- src/dolphinmainwindow.h | 6 +++ src/dolphinpart.rc | 3 +- src/dolphinui.rc | 3 +- .../contextmenu/contextmenusettingspage.cpp | 4 +- src/settings/dolphinsettingsdialog.cpp | 2 +- 7 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 65d841060..2568f503f 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -445,8 +445,8 @@ void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &pro addSeparator(); QList additionalActions; - if (props.isDirectory() && props.isLocal() && ContextMenuSettings::showOpenTerminal()) { - additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal")); + if (props.isLocal() && ContextMenuSettings::showOpenTerminal()) { + additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal_here")); } m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions); diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 589c2c185..3e9d463d2 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -1087,8 +1087,45 @@ void DolphinMainWindow::openPreferredSearchTool() void DolphinMainWindow::openTerminal() { - const QUrl url = m_activeViewContainer->url(); + openTerminalJob(m_activeViewContainer->url()); +} + +void DolphinMainWindow::openTerminalHere() +{ + QList urls = {}; + + for (const KFileItem& item : m_activeViewContainer->view()->selectedItems()) { + QUrl url = item.url(); + if (item.isFile()) { + url.setPath(QFileInfo(url.path()).absolutePath()); + } + if (!urls.contains(url)) { + urls << url; + } + } + + // No items are selected. Open a terminal window for the current location. + if (urls.count() == 0) { + openTerminal(); + return; + } + if (urls.count() > 5) { + QString question = i18np("Are you sure you want to open 1 terminal window?", + "Are you sure you want to open %1 terminal windows?", urls.count()); + const int answer = KMessageBox::warningYesNo(this, question); + if (answer != KMessageBox::Yes) { + return; + } + } + + for (const QUrl& url : urls) { + openTerminalJob(url); + } +} + +void DolphinMainWindow::openTerminalJob(const QUrl& url) +{ if (url.isLocalFile()) { auto job = new KTerminalLauncherJob(QString()); job->setWorkingDirectory(url.toLocalFile()); @@ -1721,6 +1758,16 @@ void DolphinMainWindow::setupActions() actionCollection()->setDefaultShortcut(openTerminal, Qt::SHIFT | Qt::Key_F4); connect(openTerminal, &QAction::triggered, this, &DolphinMainWindow::openTerminal); + QAction* openTerminalHere = actionCollection()->addAction(QStringLiteral("open_terminal_here")); + // i18n: "Here" refers to the location(s) of the currently selected item(s) or the currently viewed location if nothing is selected. + openTerminalHere->setText(i18nc("@action:inmenu Tools", "Open Terminal Here")); + openTerminalHere->setWhatsThis(xi18nc("@info:whatsthis", + "This opens terminal applications for the selected items' locations." + "To learn more about terminals use the help in the terminal application.")); + openTerminalHere->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); + actionCollection()->setDefaultShortcut(openTerminalHere, Qt::SHIFT | Qt::ALT | Qt::Key_F4); + connect(openTerminalHere, &QAction::triggered, this, &DolphinMainWindow::openTerminalHere); + #ifdef HAVE_TERMINAL QAction* focusTerminalPanel = actionCollection()->addAction(QStringLiteral("focus_terminal_panel")); focusTerminalPanel->setText(i18nc("@action:inmenu Tools", "Focus Terminal Panel")); diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 17327f2de..6e775c5cc 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -399,6 +399,12 @@ private Q_SLOTS: /** Opens a terminal window for the current location. */ void openTerminal(); + /** Opens terminal windows for the selected items' locations. */ + void openTerminalHere(); + + /** Opens a terminal window for the URL. */ + void openTerminalJob(const QUrl& url); + /** Focus a Terminal Panel. */ void focusTerminalPanel(); diff --git a/src/dolphinpart.rc b/src/dolphinpart.rc index a65cf685e..d13f4aaed 100644 --- a/src/dolphinpart.rc +++ b/src/dolphinpart.rc @@ -1,6 +1,6 @@ - + &Edit @@ -40,6 +40,7 @@ Tools + diff --git a/src/dolphinui.rc b/src/dolphinui.rc index 1b7f2e8b1..b6afe2abb 100644 --- a/src/dolphinui.rc +++ b/src/dolphinui.rc @@ -1,6 +1,6 @@ - + @@ -69,6 +69,7 @@ + diff --git a/src/settings/contextmenu/contextmenusettingspage.cpp b/src/settings/contextmenu/contextmenusettingspage.cpp index cec1f9649..97bb39ff0 100644 --- a/src/settings/contextmenu/contextmenusettingspage.cpp +++ b/src/settings/contextmenu/contextmenusettingspage.cpp @@ -127,7 +127,7 @@ bool ContextMenuSettingsPage::entryVisible(const QString& id) return ContextMenuSettings::showCopyLocation(); } else if (id == "duplicate") { return ContextMenuSettings::showDuplicateHere(); - } else if (id == "open_terminal") { + } else if (id == "open_terminal_here") { return ContextMenuSettings::showOpenTerminal(); } return false; @@ -149,7 +149,7 @@ void ContextMenuSettingsPage::setEntryVisible(const QString& id, bool visible) ContextMenuSettings::setShowCopyLocation(visible); } else if (id == "duplicate") { ContextMenuSettings::setShowDuplicateHere(visible); - } else if (id == "open_terminal") { + } else if (id == "open_terminal_here") { ContextMenuSettings::setShowOpenTerminal(visible); } } diff --git a/src/settings/dolphinsettingsdialog.cpp b/src/settings/dolphinsettingsdialog.cpp index d699ef894..bd4c5fb02 100644 --- a/src/settings/dolphinsettingsdialog.cpp +++ b/src/settings/dolphinsettingsdialog.cpp @@ -86,7 +86,7 @@ DolphinSettingsDialog::DolphinSettingsDialog(const QUrl& url, QWidget* parent, K QStringLiteral("open_in_new_window"), QStringLiteral("copy_location"), QStringLiteral("duplicate"), - QStringLiteral("open_terminal"), + QStringLiteral("open_terminal_here") }); KPageWidgetItem* contextMenuSettingsFrame = addPage(contextMenuSettingsPage, i18nc("@title:group", "Context Menu")); -- 2.47.3