diff options
| author | Albert Mkhitaryan <[email protected]> | 2026-02-20 08:48:54 +0000 |
|---|---|---|
| committer | Méven Car <[email protected]> | 2026-02-20 08:48:54 +0000 |
| commit | db49ac4e1916f87143574b064b36ec86f8407145 (patch) | |
| tree | 9620cb68a243caf018473f93833da8e52c57973e /src/servicemenushortcutmanager.cpp | |
| parent | 2438a457bf7499c91e3b035ed10ade41ac632660 (diff) | |
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
Diffstat (limited to 'src/servicemenushortcutmanager.cpp')
| -rw-r--r-- | src/servicemenushortcutmanager.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
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 <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include <servicemenushortcutmanager.h> + +#include <KActionCategory> +#include <KActionCollection> +#include <KDesktopFileAction> +#include <KFileItemActions> +#include <KLocalizedString> +#include <KXMLGUIClient> +#include <KXMLGUIFactory> + +#include <QAction> +#include <QDomDocument> +#include <QFileInfo> + +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<QAction *> actions = fileItemActions->createServiceMenuActions(); + + for (QAction *action : actions) { + const auto desktopAction = action->data().value<KDesktopFileAction>(); + 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 |
