File indexing completed on 2024-04-14 05:43:45

0001 /*
0002     SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include <QAction>
0007 #include <QCoreApplication>
0008 #include <QKeySequence>
0009 
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KGlobalAccel>
0013 #include <KService>
0014 
0015 #include <optional>
0016 
0017 static void migrateShortcut(const QString &desktopFile, const QList<QKeySequence> &shortcuts)
0018 {
0019     const KService::Ptr service = KService::serviceByStorageId(desktopFile);
0020     QAction action(service->name());
0021     action.setProperty("componentName", desktopFile);
0022     action.setProperty("componentDisplayName", service->name());
0023     action.setObjectName(QStringLiteral("_launch"));
0024     // Tell kglobalaccel that the action is active.
0025     KGlobalAccel::self()->setShortcut(&action, shortcuts);
0026     action.setProperty("isConfigurationAction", true);
0027     KGlobalAccel::self()->setShortcut(&action, shortcuts, KGlobalAccel::NoAutoloading);
0028 }
0029 
0030 int main(int argc, char **argv)
0031 {
0032     QCoreApplication app(argc, argv);
0033 
0034     KConfig khotkeysrc(QStringLiteral("khotkeysrc"), KConfig::SimpleConfig);
0035     const int dataCount = KConfigGroup(&khotkeysrc, QStringLiteral("Data")).readEntry("DataCount", 0);
0036     std::optional<int> kmenueditIndex = std::nullopt;
0037     KConfigGroup kmenueditGroup;
0038     for (int i = 1; i <= dataCount; ++i) {
0039         kmenueditGroup = KConfigGroup(&khotkeysrc, QStringLiteral("Data_%1").arg(i));
0040         if (kmenueditGroup.readEntry("Name") == QLatin1String("KMenuEdit")) {
0041             kmenueditIndex = i;
0042             break;
0043         }
0044     }
0045     if (!kmenueditIndex.has_value()) {
0046         return 0;
0047     }
0048 
0049     const int shortcutCount = kmenueditGroup.readEntry("DataCount", 0);
0050     for (int i = 1; i <= shortcutCount; ++i) {
0051         const QString groupName = QStringLiteral("Data_%1_%2").arg(kmenueditIndex.value()).arg(i);
0052         if (KConfigGroup(&khotkeysrc, groupName).readEntry("Type") == QLatin1String("MENUENTRY_SHORTCUT_ACTION_DATA")) {
0053             const QString desktopFile = KConfigGroup(&khotkeysrc, groupName + QStringLiteral("Actions0")).readEntry("CommandURL");
0054             if (desktopFile == QLatin1String("org.kde.konsole.desktop")) {
0055                 const QString shortcutId = KConfigGroup(&khotkeysrc, groupName + QStringLiteral("Triggers0")).readEntry("Uuid");
0056                 const QList<QKeySequence> shortcuts = KGlobalAccel::self()->globalShortcut(QStringLiteral("khotkeys"), shortcutId);
0057 
0058                 // Unset old shortcut. setShortcut() is needed to make the action active.
0059                 QAction action;
0060                 action.setObjectName(shortcutId);
0061                 action.setProperty("componentName", QStringLiteral("khotkeys"));
0062                 KGlobalAccel::self()->setShortcut(&action, {});
0063                 KGlobalAccel::self()->removeAllShortcuts(&action);
0064 
0065                 // Migrate to kglobalaccel.
0066                 migrateShortcut(desktopFile, shortcuts);
0067 
0068                 // khotkeys will automagically update the DataCount key.
0069                 khotkeysrc.deleteGroup(groupName);
0070                 khotkeysrc.deleteGroup(groupName + QStringLiteral("Actions"));
0071                 khotkeysrc.deleteGroup(groupName + QStringLiteral("Actions0"));
0072                 khotkeysrc.deleteGroup(groupName + QStringLiteral("Conditions"));
0073                 khotkeysrc.deleteGroup(groupName + QStringLiteral("Triggers"));
0074                 khotkeysrc.deleteGroup(groupName + QStringLiteral("Triggers0"));
0075             }
0076         }
0077     }
0078 
0079     khotkeysrc.sync();
0080     return 0;
0081 }