2
* Copyright (C) 2012 Ivan Cukic <ivan.cukic@kde.org>
3
* Copyright (C) 2012 Makis Marimpis <makhsm@gmail.com>
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of
8
* the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "GlobalShortcutsPlugin.h"
21
#include <QStringList>
26
#include <utils/nullptr.h>
27
#include <utils/val.h>
29
val objectNamePattern = QString::fromLatin1("switch-to-activity-%1");
30
val objectNamePatternLength = objectNamePattern.length() - 2;
32
GlobalShortcutsPlugin::GlobalShortcutsPlugin(QObject * parent, const QVariantList & args)
34
m_activitiesService(nullptr),
35
m_signalMapper(new QSignalMapper(this)),
36
m_actionCollection(new KActionCollection(this))
41
GlobalShortcutsPlugin::~GlobalShortcutsPlugin()
43
m_actionCollection->clear();
46
bool GlobalShortcutsPlugin::init(const QHash < QString, QObject * > & modules)
48
m_activitiesService = modules["activities"];
50
val activitiesList = Plugin::callOn <QStringList, Qt::DirectConnection> (m_activitiesService, "ListActivities", "QStringList");
52
foreach (val & activityId, activitiesList) {
53
activityAdded(activityId);
57
m_signalMapper, SIGNAL(mapped(QString)),
58
m_activitiesService, SLOT(SetCurrentActivity(QString)),
62
m_actionCollection->readSettings();
64
foreach (val action, m_actionCollection->actions()) {
65
if (!activitiesList.contains(action->objectName().mid(objectNamePatternLength))) {
66
m_actionCollection->removeAction(action);
70
m_actionCollection->writeSettings();
75
void GlobalShortcutsPlugin::activityAdded(const QString & activityId)
77
val action = m_actionCollection->addAction(
78
objectNamePattern.arg(activityId)
81
action->setText(i18nc("@action", "Switch to activity \"%1\"", activityName(activityId)));
82
action->setGlobalShortcut(KShortcut());
84
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
85
m_signalMapper->setMapping(action, activityId);
87
m_actionCollection->writeSettings();
90
void GlobalShortcutsPlugin::activityRemoved(const QString & activityId)
92
foreach (val action, m_actionCollection->actions()) {
93
if (activityId == action->objectName().mid(objectNamePatternLength)) {
94
m_actionCollection->removeAction(action);
98
m_actionCollection->writeSettings();
101
void GlobalShortcutsPlugin::activityChanged(const QString & activityId)
103
foreach (val action, m_actionCollection->actions()) {
104
if (activityId == action->objectName().mid(objectNamePatternLength)) {
105
action->setText(i18nc("@action", "Switch to activity \"%1\"", activityName(activityId)));
110
QString GlobalShortcutsPlugin::activityName(const QString & activityId) const
112
return Plugin::callOnWithArgs <QString, Qt::DirectConnection> (
113
m_activitiesService, "ActivityName", "QString",
114
Q_ARG(QString, activityId)
118
KAMD_EXPORT_PLUGIN(GlobalShortcutsPlugin, "activitymanager_plugin_globalshortcuts")