Changeset View
Changeset View
Standalone View
Standalone View
desktop/MigrateShortcuts.cpp
- This file was added.
1 | #include <iostream> | ||||
---|---|---|---|---|---|
2 | | ||||
3 | #include <QCoreApplication> | ||||
4 | #include <QDBusInterface> | ||||
5 | #include <QString> | ||||
6 | #include <QDebug> | ||||
7 | #include <KActionCollection> | ||||
8 | #include <KConfig> | ||||
9 | #include <KConfigGroup> | ||||
10 | #include <KGlobalAccel> | ||||
11 | #include <KLocalizedString> | ||||
12 | | ||||
13 | int main(int argc, char **argv) | ||||
14 | { | ||||
15 | QCoreApplication app(argc, argv); | ||||
16 | QDBusInterface khotkeys(QStringLiteral("org.kde.kded5"), QStringLiteral("/modules/khotkeys"), | ||||
17 | QStringLiteral("org.kde.khotkeys")); | ||||
18 | KConfig khotkeysrc(QStringLiteral("khotkeysrc"), KConfig::SimpleConfig); | ||||
19 | int dataCount = KConfigGroup(&khotkeysrc, "Data").readEntry("DataCount", 0); | ||||
20 | bool found_spectacle = false; | ||||
21 | int spectacleIndex; | ||||
22 | for (int i = 1; i <= dataCount; ++i) { | ||||
23 | if( KConfigGroup(&khotkeysrc, QStringLiteral("Data_%1").arg(i)).readEntry("ImportId", QString()) == QStringLiteral("spectacle")) { | ||||
24 | found_spectacle = true; | ||||
25 | spectacleIndex = i; | ||||
26 | break; | ||||
27 | } | ||||
28 | } | ||||
29 | QList<QKeySequence> launchKey; | ||||
30 | QList<QKeySequence> fullScreenKey; | ||||
31 | QList<QKeySequence> regionKey; | ||||
32 | QList<QKeySequence> activeWindowKey; | ||||
33 | QStringList ids; | ||||
34 | if (found_spectacle) { | ||||
35 | for (int i = 1; i <= 4; ++i) { | ||||
36 | QString groupName = QStringLiteral("Data_%1_%2").arg(spectacleIndex).arg(i); | ||||
37 | QString method = KConfigGroup(&khotkeysrc, groupName + QStringLiteral("Actions0")).readEntry("Call"); | ||||
38 | QString id = KConfigGroup(&khotkeysrc, groupName + QStringLiteral("Triggers0")).readEntry("Uuid"); | ||||
39 | QList<QKeySequence> shortcut = KGlobalAccel::self()->globalShortcut(QStringLiteral("khotkeys"), id); | ||||
40 | ids.append(id); | ||||
41 | /* Name and Comment field are translated but we can find out which action is which by looking at the called | ||||
42 | * D-Bus Method */ | ||||
43 | if (method == QStringLiteral("StartAgent")) { | ||||
44 | launchKey = shortcut; | ||||
45 | } else if (method == QStringLiteral("FullScreen")) { | ||||
46 | fullScreenKey = shortcut; | ||||
47 | } else if (method == QStringLiteral("ActiveWindow")) { | ||||
48 | activeWindowKey = shortcut; | ||||
49 | } else if (method == QStringLiteral("RectangularRegion")) { | ||||
50 | regionKey = shortcut; | ||||
51 | } | ||||
52 | // Delete the groups from khotkeysrc | ||||
53 | khotkeysrc.deleteGroup(groupName); | ||||
54 | khotkeysrc.deleteGroup(groupName + QStringLiteral("Actions")); | ||||
55 | khotkeysrc.deleteGroup(groupName + QStringLiteral("Actions0")); | ||||
56 | khotkeysrc.deleteGroup(groupName + QStringLiteral("Conditions")); | ||||
57 | khotkeysrc.deleteGroup(groupName + QStringLiteral("Triggers")); | ||||
58 | khotkeysrc.deleteGroup(groupName + QStringLiteral("Triggers0")); | ||||
59 | } | ||||
60 | khotkeysrc.deleteGroup(QStringLiteral("Data_%1").arg(spectacleIndex)); | ||||
61 | khotkeysrc.deleteGroup(QStringLiteral("Data_%1Conditions").arg(spectacleIndex)); | ||||
62 | khotkeysrc.sync(); | ||||
63 | } | ||||
64 | QDBusInterface kglobalaccel(QStringLiteral("org.kde.kglobalaccel"), QStringLiteral("/kglobalaccel"), | ||||
65 | QStringLiteral("org.kde.KGlobalAccel")); | ||||
66 | // Unregister the khotkeysActions from globalAccel, removeAll didn't Work, so using D-Bus | ||||
67 | for(const QString &action : ids) { | ||||
68 | kglobalaccel.call(QStringLiteral("unregister"), QStringLiteral("khotkeys"), action); | ||||
69 | } | ||||
70 | //Setup the default Shortcuts | ||||
71 | KActionCollection shortCutActions(static_cast<QObject*>(nullptr)); | ||||
72 | shortCutActions.setComponentName(QStringLiteral("org.kde.spectacle.desktop")); | ||||
73 | shortCutActions.setComponentDisplayName(QStringLiteral("Spectacle")); | ||||
74 | { | ||||
75 | QAction *action = new QAction(i18n("Launch Spectacle")); | ||||
76 | action->setObjectName(QStringLiteral("_launch")); | ||||
77 | shortCutActions.addAction(action->objectName(), action); | ||||
78 | } | ||||
79 | { | ||||
80 | QAction *action = new QAction(i18n("Capture Entire Desktop")); | ||||
81 | action->setObjectName(QStringLiteral("FullScreenScreenShot")); | ||||
82 | shortCutActions.addAction(action->objectName(), action); | ||||
83 | } | ||||
84 | { | ||||
85 | QAction *action = new QAction(i18n("Capture Current Monitor")); | ||||
86 | action->setObjectName(QStringLiteral("CurrentMonitorScreenShot")); | ||||
87 | shortCutActions.addAction(action->objectName(), action); | ||||
88 | } | ||||
89 | { | ||||
90 | QAction *action = new QAction(i18n("Capture Active Window")); | ||||
91 | action->setObjectName(QStringLiteral("ActiveWindowScreenShot")); | ||||
92 | shortCutActions.addAction(action->objectName(), action); | ||||
93 | } | ||||
94 | { | ||||
95 | QAction *action = new QAction(i18n("Capture Rectangular Region")); | ||||
96 | action->setObjectName(QStringLiteral("RectangularRegionScreenShot")); | ||||
97 | shortCutActions.addAction(action->objectName(), action); | ||||
98 | } | ||||
99 | QAction* openAction = shortCutActions.action(QStringLiteral("_launch")); | ||||
100 | KGlobalAccel::self()->setDefaultShortcut(openAction, {Qt::Key_Print}); | ||||
101 | QAction* fullScreenAction = shortCutActions.action(QStringLiteral("FullScreenScreenShot")); | ||||
102 | KGlobalAccel::self()->setDefaultShortcut(fullScreenAction, {Qt::SHIFT + Qt::Key_Print}); | ||||
103 | QAction* currentScreenAction = shortCutActions.action(QStringLiteral("CurrentMonitorScreenShot")); | ||||
104 | //Using global for current Screen because it is new | ||||
105 | KGlobalAccel::self()->setGlobalShortcut(currentScreenAction, Qt::SHIFT + Qt::ALT + Qt::Key_Print); | ||||
106 | QAction* activeWindowAction = shortCutActions.action(QStringLiteral("ActiveWindowScreenShot")); | ||||
107 | KGlobalAccel::self()->setDefaultShortcut(activeWindowAction, {Qt::META + Qt::Key_Print}); | ||||
108 | QAction* regionAction = shortCutActions.action(QStringLiteral("RectangularRegionScreenShot")); | ||||
109 | KGlobalAccel::self()->setDefaultShortcut(regionAction, {Qt::META + Qt::SHIFT + Qt::Key_Print}); | ||||
110 | // Finally reinstate the old shortcuts | ||||
111 | if (found_spectacle) { | ||||
112 | KGlobalAccel::self()->setShortcut(openAction, launchKey, KGlobalAccel::NoAutoloading); | ||||
113 | KGlobalAccel::self()->setShortcut(fullScreenAction, fullScreenKey, KGlobalAccel::NoAutoloading); | ||||
114 | KGlobalAccel::self()->setShortcut(activeWindowAction, activeWindowKey, KGlobalAccel::NoAutoloading); | ||||
115 | KGlobalAccel::self()->setShortcut(regionAction, regionKey, KGlobalAccel::NoAutoloading); | ||||
116 | } | ||||
117 | } |