From db49ac4e1916f87143574b064b36ec86f8407145 Mon Sep 17 00:00:00 2001 From: Albert Mkhitaryan Date: Fri, 20 Feb 2026 08:48:54 +0000 Subject: Add keyboard shortcut support for service menu actions Introduce ServiceMenuShortcutManager, which registers all service menu actions with KActionCollection at startup allowing users to assign keyboard shortcuts in Configure Keyboard Shorcuts. Save/Load of configs happens via KXMLGUI in dolphinui.rc. Notes: - Manager initializes before setupGUI() for shortcut restoration - Execution and validation handled entirely by KFileItemAction in KIO. BUG: 260266 --- src/servicemenushortcutmanager.cpp | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/servicemenushortcutmanager.cpp (limited to 'src/servicemenushortcutmanager.cpp') diff --git a/src/servicemenushortcutmanager.cpp b/src/servicemenushortcutmanager.cpp new file mode 100644 index 000000000..ea13fc5f8 --- /dev/null +++ b/src/servicemenushortcutmanager.cpp @@ -0,0 +1,81 @@ +/* + * SPDX-FileCopyrightText: 2026 Albert Mkhitaryan + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static constexpr QLatin1String serviceMenuNamePrefix("servicemenu_"); + +ServiceMenuShortcutManager::ServiceMenuShortcutManager(KActionCollection *actionCollection, QObject *parent) + : QObject(parent) + , m_actionCollection(actionCollection) + , m_category(new KActionCategory(i18nc("@title:group", "Context Menu Actions"), actionCollection)) +{ +} + +void ServiceMenuShortcutManager::refresh(KFileItemActions *fileItemActions) +{ + // Uses takeAction() to remove without deleting. Actions are owned by KFileItemActions + // and are deleted when called createServiceMenuActions() or destroyed + for (const QString &name : std::as_const(m_actionNames)) { + m_actionCollection->takeAction(m_actionCollection->action(name)); + } + m_actionNames.clear(); + + // Get action->execution mapping from KIO + const QList actions = fileItemActions->createServiceMenuActions(); + + for (QAction *action : actions) { + const auto desktopAction = action->data().value(); + const QString fileName = QFileInfo(desktopAction.desktopFilePath()).fileName(); + const QString name = serviceMenuNamePrefix + fileName + QLatin1String("::") + desktopAction.actionsKey(); + m_category->addAction(name, action); + m_actionNames.append(name); + } +} + +void ServiceMenuShortcutManager::cleanupStaleShortcuts(KXMLGUIClient *client) +{ + const QString file = client->localXMLFile(); + if (file.isEmpty()) { + return; + } + + const QString xml = KXMLGUIFactory::readConfigFile(file, client->componentName()); + if (xml.isEmpty()) { + return; + } + + QDomDocument doc; + doc.setContent(xml); + QDomElement actionPropElement = KXMLGUIFactory::actionPropertiesElement(doc); + + bool modified = false; + for (QDomElement e = actionPropElement.firstChildElement(); !e.isNull();) { + QDomElement next = e.nextSiblingElement(); + const QString name = e.attribute(QStringLiteral("name")); + if (name.startsWith(serviceMenuNamePrefix) && !m_actionNames.contains(name)) { + actionPropElement.removeChild(e); + modified = true; + } + e = next; + } + + if (modified) { + KXMLGUIFactory::saveConfigFile(doc, file, client->componentName()); + } +} \ No newline at end of file -- cgit v1.3