From a91ff4e4b61f22ecf49debccf16ce7d190248c91 Mon Sep 17 00:00:00 2001 From: Neal Gompa Date: Sat, 10 Sep 2022 10:54:06 -0400 Subject: [PATCH] containmentactions/contextmenu: Add "Open Terminal" action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds the ability to open the terminal from the main Plasma Shell context menu. It was originally written as a Red Hat/Fedora patch for KDE Workspace 4 to open Konsole from the desktop context menu when Fedora switched to KDE 4, and ported forward to KDE Plasma 5 when Fedora Linux switched to Plasma 5 in Fedora 22. It has been maintained as a downstream patch since then. This version of the patch genericizes the patch by not implying that Konsole is the user's terminal (it actually opens the user's chosen terminal anyway), can be restricted via the desktop kiosk mode settings, and is disabled by default. The major maintainers of this patch are being honored as co-authors for this patch. They have all helped keep this functionality working across two KDE major versions for a decade. Co-authored-by: Daniel Vrátil Co-authored-by: Jan Grulich Co-authored-by: Marc Deop Co-authored-by: Rex Dieter BUG: 451217 --- containmentactions/contextmenu/menu.cpp | 21 +++++++++++++++++++++ containmentactions/contextmenu/menu.h | 2 ++ 2 files changed, 23 insertions(+) diff --git a/containmentactions/contextmenu/menu.cpp b/containmentactions/contextmenu/menu.cpp index 7dbd236f1..6bad29751 100644 --- a/containmentactions/contextmenu/menu.cpp +++ b/containmentactions/contextmenu/menu.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -62,6 +63,7 @@ void ContextMenu::restore(const KConfigGroup &config) << QStringLiteral("configure shortcuts") << QStringLiteral("_sep1") << QStringLiteral("_context") + << QStringLiteral("_open_terminal") << QStringLiteral("_run_command") << QStringLiteral("add widgets") << QStringLiteral("_add panel") @@ -74,6 +76,7 @@ void ContextMenu::restore(const KConfigGroup &config) << QStringLiteral("_sep3") << QStringLiteral("_wallpaper"); disabled.insert(QStringLiteral("configure shortcuts")); + disabled.insert(QStringLiteral("_open_terminal")); disabled.insert(QStringLiteral("_run_command")); disabled.insert(QStringLiteral("run associated application")); disabled.insert(QStringLiteral("_lock_screen")); @@ -98,6 +101,10 @@ void ContextMenu::restore(const KConfigGroup &config) m_runCommandAction->setShortcut(KGlobalAccel::self()->globalShortcut(QStringLiteral("krunner.desktop"), QStringLiteral("_launch")).value(0)); connect(m_runCommandAction, &QAction::triggered, this, &ContextMenu::runCommand); + m_openTerminalAction = new QAction(i18n("Open Terminal"), this); + m_openTerminalAction->setIcon(QIcon::fromTheme("utilities-terminal")); + connect(m_openTerminalAction, &QAction::triggered, this, &ContextMenu::openTerminal); + m_lockScreenAction = new QAction(i18nc("plasma_containmentactions_contextmenu", "Lock Screen"), this); m_lockScreenAction->setIcon(QIcon::fromTheme(QStringLiteral("system-lock-screen"))); m_lockScreenAction->setShortcut(KGlobalAccel::self()->globalShortcut(QStringLiteral("ksmserver"), QStringLiteral("Lock Session")).value(0)); @@ -180,6 +187,10 @@ QAction *ContextMenu::action(const QString &name) if (KAuthorized::authorizeAction(QStringLiteral("run_command")) && KAuthorized::authorize(QStringLiteral("run_command"))) { return m_runCommandAction; } + } else if (name == QLatin1String("_open_terminal")) { + if (KAuthorized::authorizeAction(QStringLiteral("shell_access"))) { + return m_openTerminalAction; + } } else if (name == QLatin1String("_lock_screen")) { if (KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) { return m_lockScreenAction; @@ -213,6 +224,16 @@ QAction *ContextMenu::action(const QString &name) return nullptr; } +void ContextMenu::openTerminal() +{ + if (!KAuthorized::authorizeAction(QStringLiteral("shell_access"))) { + return; + } + auto job = new KTerminalLauncherJob(QString()); + job->setWorkingDirectory(QDir::homePath()); + job->start(); +} + void ContextMenu::runCommand() { if (!KAuthorized::authorizeAction(QStringLiteral("run_command"))) { diff --git a/containmentactions/contextmenu/menu.h b/containmentactions/contextmenu/menu.h index 348180163..c22f3800b 100644 --- a/containmentactions/contextmenu/menu.h +++ b/containmentactions/contextmenu/menu.h @@ -29,11 +29,13 @@ public: void save(KConfigGroup &config) override; public Q_SLOTS: + void openTerminal(); void runCommand(); void startLogout(); void configureDisplays(); private: + QAction *m_openTerminalAction = nullptr; QAction *m_runCommandAction = nullptr; QAction *m_lockScreenAction = nullptr; QAction *m_logoutAction = nullptr;