File indexing completed on 2024-05-12 17:09:53

0001 /*
0002     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
0003     SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include <QCoreApplication>
0009 #include <QDebug>
0010 #include <QStandardPaths>
0011 
0012 #include <KActionCollection>
0013 #include <KConfig>
0014 #include <KConfigGroup>
0015 #include <KDesktopFile>
0016 #include <KGlobalAccel>
0017 #include <KSharedConfig>
0018 
0019 int main(int argc, char **argv)
0020 {
0021     QCoreApplication app(argc, argv);
0022 
0023     const QString oldComponentName = QStringLiteral("krunner");
0024     const QString oldDesktopFile = QStringLiteral("krunner.desktop");
0025     const QString newDesktopFile = QStringLiteral("org.kde.krunner.desktop");
0026 
0027     // Since we need to fake those actions, read the translated names from the desktop file
0028     KDesktopFile df(QStandardPaths::GenericDataLocation, QStringLiteral("kglobalaccel/") + newDesktopFile);
0029     QString displayName = QStringLiteral("KRunner");
0030     if (!df.readName().isEmpty()) {
0031         displayName = df.readName();
0032     }
0033     const QString clipboardActionName = df.actionGroup(QStringLiteral("RunClipboard"))
0034                                             .readEntry(QStringLiteral("Name"), //
0035                                                        QStringLiteral("Run command on clipboard contents"));
0036 
0037     KActionCollection shortCutActions(nullptr, newDesktopFile);
0038     shortCutActions.setComponentDisplayName(displayName);
0039     // The actions are intentionally allocated and never cleaned up, because otherwise KGlobalAccel
0040     // will mark them as inactive
0041     auto runCommandAction = new QAction(displayName);
0042     shortCutActions.addAction(QStringLiteral("_launch"), runCommandAction);
0043     auto runClipboardAction = new QAction(clipboardActionName);
0044     shortCutActions.addAction(QStringLiteral("RunClipboard"), runClipboardAction);
0045 
0046     QList<QKeySequence> oldRunCommand;
0047     QList<QKeySequence> oldRunClipboard;
0048 
0049     // It can happen that the old component is not active so we do it unconditionally
0050     KActionCollection oldActions(nullptr, oldComponentName);
0051     QAction oldRunCommandAction, oldRunClipboardAction;
0052     oldActions.addAction(QStringLiteral("run command"), &oldRunCommandAction);
0053     oldActions.addAction(QStringLiteral("run command on clipboard contents"), &oldRunClipboardAction);
0054     oldRunCommand = KGlobalAccel::self()->globalShortcut(oldComponentName, oldRunCommandAction.objectName());
0055     oldRunClipboard = KGlobalAccel::self()->globalShortcut(oldComponentName, oldRunClipboardAction.objectName());
0056     KGlobalAccel::self()->setShortcut(&oldRunCommandAction, {});
0057     KGlobalAccel::self()->setShortcut(&oldRunClipboardAction, {});
0058     KGlobalAccel::self()->removeAllShortcuts(&oldRunCommandAction);
0059     KGlobalAccel::self()->removeAllShortcuts(&oldRunClipboardAction);
0060     KGlobalAccel::self()->cleanComponent(oldComponentName);
0061 
0062     if (KGlobalAccel::isComponentActive(oldDesktopFile)) {
0063         oldRunCommand = KGlobalAccel::self()->globalShortcut(oldDesktopFile, runCommandAction->objectName());
0064         oldRunClipboard = KGlobalAccel::self()->globalShortcut(oldDesktopFile, runClipboardAction->objectName());
0065         KGlobalAccel::self()->cleanComponent(oldDesktopFile);
0066     }
0067 
0068     if (!oldRunCommand.isEmpty()) {
0069         KGlobalAccel::self()->setShortcut(runCommandAction, oldRunCommand, KGlobalAccel::NoAutoloading);
0070     }
0071     if (!oldRunClipboard.isEmpty()) {
0072         KGlobalAccel::self()->setShortcut(runClipboardAction, oldRunClipboard, KGlobalAccel::NoAutoloading);
0073     }
0074 
0075     return 0;
0076 }