File indexing completed on 2025-01-26 05:00:54
0001 /* 0002 * SPDX-FileCopyrightText: 2012, 2013, 2014 Ivan Cukic <ivan.cukic(at)kde.org> 0003 * SPDX-FileCopyrightText: 2012 Makis Marimpis <makhsm@gmail.com> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "GlobalShortcutsPlugin.h" 0009 0010 #include <QAction> 0011 #include <QString> 0012 #include <QStringList> 0013 0014 #include <KActionCollection> 0015 #include <KGlobalAccel> 0016 #include <KLocalizedString> 0017 0018 K_PLUGIN_CLASS(GlobalShortcutsPlugin) 0019 0020 const auto objectNamePattern = QStringLiteral("switch-to-activity-%1"); 0021 const auto objectNamePatternLength = objectNamePattern.length() - 2; 0022 0023 GlobalShortcutsPlugin::GlobalShortcutsPlugin(QObject *parent) 0024 : Plugin(parent) 0025 , m_activitiesService(nullptr) 0026 , m_actionCollection(new KActionCollection(this)) 0027 { 0028 m_actionCollection->setComponentName("ActivityManager"); 0029 m_actionCollection->setComponentDisplayName(i18n("Activity Manager")); 0030 } 0031 0032 GlobalShortcutsPlugin::~GlobalShortcutsPlugin() 0033 { 0034 m_actionCollection->clear(); 0035 } 0036 0037 bool GlobalShortcutsPlugin::init(QHash<QString, QObject *> &modules) 0038 { 0039 Plugin::init(modules); 0040 0041 m_activitiesService = modules["activities"]; 0042 0043 m_activitiesList = Plugin::retrieve<QStringList>(m_activitiesService, "ListActivities"); 0044 0045 for (const auto &activity : m_activitiesList) { 0046 activityAdded(activity); 0047 } 0048 connect(this, SIGNAL(currentActivityChanged(QString)), m_activitiesService, SLOT(SetCurrentActivity(QString)), Qt::QueuedConnection); 0049 0050 connect(m_activitiesService, SIGNAL(ActivityAdded(QString)), this, SLOT(activityAdded(QString))); 0051 connect(m_activitiesService, SIGNAL(ActivityRemoved(QString)), this, SLOT(activityRemoved(QString))); 0052 0053 m_actionCollection->readSettings(); 0054 0055 return true; 0056 } 0057 0058 void GlobalShortcutsPlugin::activityAdded(const QString &activity) 0059 { 0060 if (activity == "00000000-0000-0000-0000-000000000000") { 0061 return; 0062 } 0063 0064 if (!m_activitiesList.contains(activity)) { 0065 m_activitiesList << activity; 0066 } 0067 0068 const auto action = m_actionCollection->addAction(objectNamePattern.arg(activity)); 0069 0070 action->setText(i18nc("@action", "Switch to activity \"%1\"", activityName(activity))); 0071 KGlobalAccel::setGlobalShortcut(action, QList<QKeySequence>{}); 0072 0073 connect(action, &QAction::triggered, this, [this, activity]() { 0074 Q_EMIT currentActivityChanged(activity); 0075 }); 0076 0077 // m_actionCollection->writeSettings(); 0078 } 0079 0080 QString GlobalShortcutsPlugin::activityForAction(QAction *action) const 0081 { 0082 return action->objectName().mid(objectNamePatternLength); 0083 } 0084 0085 void GlobalShortcutsPlugin::activityRemoved(const QString &deletedActivity) 0086 { 0087 m_activitiesList.removeAll(deletedActivity); 0088 0089 // Removing all shortcuts that refer to an unknown activity 0090 for (const auto &action : m_actionCollection->actions()) { 0091 const auto actionActivity = activityForAction(action); 0092 if ((deletedActivity.isEmpty() && !m_activitiesList.contains(actionActivity)) // 0093 || deletedActivity == actionActivity) { 0094 KGlobalAccel::self()->removeAllShortcuts(action); 0095 m_actionCollection->removeAction(action); 0096 } 0097 } 0098 0099 m_actionCollection->writeSettings(); 0100 } 0101 0102 void GlobalShortcutsPlugin::activityChanged(const QString &activity) 0103 { 0104 for (const auto &action : m_actionCollection->actions()) { 0105 if (activity == activityForAction(action)) { 0106 action->setText(i18nc("@action", "Switch to activity \"%1\"", activityName(activity))); 0107 } 0108 } 0109 } 0110 0111 QString GlobalShortcutsPlugin::activityName(const QString &activity) const 0112 { 0113 return Plugin::retrieve<QString>(m_activitiesService, "ActivityName", Q_ARG(QString, activity)); 0114 } 0115 0116 #include "GlobalShortcutsPlugin.moc" 0117 0118 #include "moc_GlobalShortcutsPlugin.cpp"