blob: ea13fc5f8ffa5d74790ae3a6549ec14c58f8da9a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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());
}
}
|