From 48c3376927e5e9c13377bf3cfc8b0c411783e7f3 Mon Sep 17 00:00:00 2001
From: Nicolas Fella <nicolas.fella@gmx.de>
Date: Sat, 10 Apr 2021 17:08:58 +0200
Subject: [PATCH] Prevent kglobalaccel5 getting activated on non-Plasma systems

While in theory kglobalaccel works on any X11 system we don't want it to run on non-Plasma systems. It isn't very useful and may interfere with the desktop's native shortcut system.

Calling some API of KGlobalAccel may result in a DBus call to the daemon which then may launch by DBus activation. Prevent that from happening on non-Plasma systems.

BUG: 435420
CCBUG: 430691
---
 src/kglobalaccel.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

--- ./src/kglobalaccel.cpp.orig	2022-01-01 06:30:27.000000000 -0600
+++ ./src/kglobalaccel.cpp	2022-01-15 23:01:56.683914925 -0600
@@ -30,6 +30,11 @@
 #endif
 #endif
 
+bool active()
+{
+    return qgetenv("XDG_CURRENT_DESKTOP") == QByteArrayLiteral("KDE");
+}
+
 org::kde::kglobalaccel::Component *KGlobalAccelPrivate::getComponent(const QString &componentUnique, bool remember = false)
 {
     // Check if we already have this component
@@ -150,6 +155,11 @@
 void KGlobalAccel::activateGlobalShortcutContext(const QString &contextUnique, const QString &contextFriendly, const QString &programName)
 {
     Q_UNUSED(contextFriendly);
+
+    if (!active()) {
+        return;
+    }
+
     // TODO: provide contextFriendly
     self()->d->iface()->activateGlobalShortcutContext(programName, contextUnique);
 }
@@ -157,6 +167,10 @@
 // static
 bool KGlobalAccel::cleanComponent(const QString &componentUnique)
 {
+    if (!active()) {
+        return false;
+    }
+
     org::kde::kglobalaccel::Component *component = self()->getComponent(componentUnique);
     if (!component) {
         return false;
@@ -168,6 +182,10 @@
 // static
 bool KGlobalAccel::isComponentActive(const QString &componentUnique)
 {
+    if (!active()) {
+        return false;
+    }
+
     org::kde::kglobalaccel::Component *component = self()->getComponent(componentUnique);
     if (!component) {
         return false;
@@ -185,6 +203,10 @@
 
 org::kde::kglobalaccel::Component *KGlobalAccel::getComponent(const QString &componentUnique)
 {
+    if (!active()) {
+        return nullptr;
+    }
+
     return d->getComponent(componentUnique);
 }
 
@@ -528,6 +550,10 @@
 #if KGLOBALACCEL_BUILD_DEPRECATED_SINCE(4, 2)
 QList<QStringList> KGlobalAccel::allMainComponents()
 {
+    if (!active()) {
+        return {};
+    }
+
     return d->iface()->allMainComponents();
 }
 #endif
@@ -535,6 +561,9 @@
 #if KGLOBALACCEL_BUILD_DEPRECATED_SINCE(4, 2)
 QList<QStringList> KGlobalAccel::allActionsForComponent(const QStringList &actionId)
 {
+    if (!active()) {
+        return {};
+    }
     return d->iface()->allActionsForComponent(actionId);
 }
 #endif
@@ -543,6 +572,10 @@
 #if KGLOBALACCEL_BUILD_DEPRECATED_SINCE(4, 2)
 QStringList KGlobalAccel::findActionNameSystemwide(const QKeySequence &seq)
 {
+    if (!active()) {
+        return {};
+    }
+
     return self()->d->iface()->actionList(seq);
 }
 #endif
@@ -550,6 +583,10 @@
 #if KGLOBALACCEL_BUILD_DEPRECATED_SINCE(5, 90)
 QList<KGlobalShortcutInfo> KGlobalAccel::getGlobalShortcutsByKey(const QKeySequence &seq)
 {
+    if (!active()) {
+        return {};
+    }
+
     return globalShortcutsByKey(seq);
 }
 #endif
@@ -561,6 +598,10 @@
 
 bool KGlobalAccel::isGlobalShortcutAvailable(const QKeySequence &seq, const QString &comp)
 {
+    if (!active()) {
+        return false;
+    }
+
     return self()->d->iface()->globalShortcutAvailable(seq, comp);
 }
 
@@ -568,6 +609,10 @@
 #if KGLOBALACCEL_BUILD_DEPRECATED_SINCE(4, 2)
 bool KGlobalAccel::promptStealShortcutSystemwide(QWidget *parent, const QStringList &actionIdentifier, const QKeySequence &seq)
 {
+    if (!active()) {
+        return false;
+    }
+
     if (actionIdentifier.size() < 4) {
         return false;
     }
@@ -590,6 +635,10 @@
 // static
 bool KGlobalAccel::promptStealShortcutSystemwide(QWidget *parent, const QList<KGlobalShortcutInfo> &shortcuts, const QKeySequence &seq)
 {
+    if (!active()) {
+        return false;
+    }
+
     if (shortcuts.isEmpty()) {
         // Usage error. Just say no
         return false;
@@ -622,6 +671,10 @@
 // static
 void KGlobalAccel::stealShortcutSystemwide(const QKeySequence &seq)
 {
+    if (!active()) {
+        return;
+    }
+
     // get the shortcut, remove seq, and set the new shortcut
     const QStringList actionId = self()->d->iface()->actionList(seq);
     if (actionId.size() < 4) { // not a global shortcut
@@ -659,6 +712,10 @@
 
 bool KGlobalAccel::setDefaultShortcut(QAction *action, const QList<QKeySequence> &shortcut, GlobalShortcutLoading loadFlag)
 {
+    if (!active()) {
+        return false;
+    }
+
     if (checkGarbageKeycode(shortcut)) {
         return false;
     }
@@ -674,6 +731,10 @@
 
 bool KGlobalAccel::setShortcut(QAction *action, const QList<QKeySequence> &shortcut, GlobalShortcutLoading loadFlag)
 {
+    if (!active()) {
+        return false;
+    }
+
     if (checkGarbageKeycode(shortcut)) {
         return false;
     }
@@ -699,6 +760,9 @@
 
 QList<QKeySequence> KGlobalAccel::globalShortcut(const QString &componentName, const QString &actionId) const
 {
+    if (!active()) {
+        return {};
+    }
     // see also d->updateGlobalShortcut(action, KGlobalAccelPrivate::ActiveShortcut, KGlobalAccel::Autoloading);
 
     // how componentName and actionId map to QAction, e.g:
@@ -711,11 +775,19 @@
 
 void KGlobalAccel::removeAllShortcuts(QAction *action)
 {
+    if (!active()) {
+        return;
+    }
+
     d->remove(action, KGlobalAccelPrivate::UnRegister);
 }
 
 bool KGlobalAccel::hasShortcut(const QAction *action) const
 {
+    if (!active()) {
+        return false;
+    }
+
     return d->actionShortcuts.contains(action) || d->actionDefaultShortcuts.contains(action);
 }
 
@@ -728,6 +800,10 @@
 
 bool KGlobalAccel::setGlobalShortcut(QAction *action, const QList<QKeySequence> &shortcut)
 {
+    if (!active()) {
+        return false;
+    }
+
     KGlobalAccel *g = KGlobalAccel::self();
     return g->d->setShortcutWithDefault(action, shortcut, Autoloading);
 }
@@ -739,6 +815,10 @@
 
 bool KGlobalAccelPrivate::setShortcutWithDefault(QAction *action, const QList<QKeySequence> &shortcut, KGlobalAccel::GlobalShortcutLoading loadFlag)
 {
+    if (!active()) {
+        return false;
+    }
+
     if (checkGarbageKeycode(shortcut)) {
         return false;
     }
