~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/app/kded.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 
 
3
 KHotKeys
 
4
 
 
5
 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
6
 
 
7
 Distributed under the terms of the GNU General Public License version 2.
 
8
 
 
9
****************************************************************************/
 
10
 
 
11
#include "kded.h"
 
12
 
 
13
#include "action_data/action_data_group.h"
 
14
#include "action_data/menuentry_shortcut_action_data.h"
 
15
#include "actions/actions.h"
 
16
 
 
17
#include "shortcuts_handler.h"
 
18
 
 
19
#include "triggers/gestures.h"
 
20
 
 
21
 
 
22
#include <kaboutdata.h>
 
23
#include <kdebug.h>
 
24
#include <kglobalaccel.h>
 
25
#include <kpluginfactory.h>
 
26
#include <kpluginloader.h>
 
27
 
 
28
#include <unistd.h>
 
29
 
 
30
#define COMPONENT_NAME "khotkeys"
 
31
 
 
32
K_PLUGIN_FACTORY(KHotKeysModuleFactory,
 
33
                 registerPlugin<KHotKeysModule>();
 
34
    )
 
35
K_EXPORT_PLUGIN(KHotKeysModuleFactory(COMPONENT_NAME))
 
36
 
 
37
using namespace KHotKeys;
 
38
 
 
39
// KhotKeysModule
 
40
 
 
41
KHotKeysModule::KHotKeysModule(QObject* parent, const QList<QVariant>&)
 
42
    : KDEDModule(parent)
 
43
    , actions_root(NULL)
 
44
    , _settings()
 
45
    ,_initialized(false)
 
46
    {
 
47
    // initialize
 
48
    kDebug() << "Installing the delayed initialization callback.";
 
49
    QMetaObject::invokeMethod( this, "initialize", Qt::QueuedConnection);
 
50
    }
 
51
 
 
52
 
 
53
void KHotKeysModule::initialize()
 
54
    {
 
55
    if (_initialized)
 
56
        {
 
57
        return;
 
58
        }
 
59
 
 
60
    kDebug() << "Delayed initialization.";
 
61
    _initialized = true;
 
62
 
 
63
    // Initialize the global data, grab keys
 
64
    KHotKeys::init_global_data( true, this );
 
65
 
 
66
    // If a shortcut was changed (global shortcuts kcm), save
 
67
    connect(
 
68
            keyboard_handler.data(), SIGNAL(shortcutChanged()),
 
69
            this, SLOT(save()));
 
70
 
 
71
    // Read the configuration from file khotkeysrc
 
72
    reread_configuration();
 
73
 
 
74
    KGlobalAccel::cleanComponent(COMPONENT_NAME);
 
75
 
 
76
    if (_settings.update())
 
77
        {
 
78
        save();
 
79
        }
 
80
 
 
81
    }
 
82
 
 
83
 
 
84
KHotKeysModule::~KHotKeysModule()
 
85
    {
 
86
    // actions_root belongs to _settings.
 
87
    actions_root = NULL;
 
88
    }
 
89
 
 
90
 
 
91
void KHotKeysModule::reread_configuration()
 
92
    {
 
93
    kDebug() << "Reloading the khotkeys configuration";
 
94
 
 
95
    // Stop listening
 
96
    actions_root = NULL; // Disables the dbus interface effectively
 
97
    KHotKeys::khotkeys_set_active( false );
 
98
 
 
99
    // Load the settings
 
100
    _settings.reread_settings(true);
 
101
 
 
102
    KHotKeys::gesture_handler->set_mouse_button( _settings.gestureMouseButton() );
 
103
    KHotKeys::gesture_handler->set_timeout( _settings.gestureTimeOut() );
 
104
    kDebug() << _settings.areGesturesDisabled();
 
105
    KHotKeys::gesture_handler->enable( !_settings.areGesturesDisabled() );
 
106
    KHotKeys::gesture_handler->set_exclude( _settings.gesturesExclude() );
 
107
    // FIXME: SOUND
 
108
    // KHotKeys::voice_handler->set_shortcut( _settings.voice_shortcut );
 
109
    actions_root = _settings.actions();
 
110
    KHotKeys::khotkeys_set_active( true );
 
111
    }
 
112
 
 
113
 
 
114
 
 
115
 
 
116
SimpleActionData* KHotKeysModule::menuentry_action(const QString &storageId)
 
117
    {
 
118
    ActionDataGroup *menuentries = _settings.get_system_group(
 
119
            ActionDataGroup::SYSTEM_MENUENTRIES);
 
120
 
 
121
    // Now try to find the action
 
122
    Q_FOREACH(ActionDataBase* element, menuentries->children())
 
123
        {
 
124
        SimpleActionData *actionData = dynamic_cast<SimpleActionData*>(element);
 
125
 
 
126
        if (actionData && actionData->action())
 
127
            {
 
128
            MenuEntryAction *action = dynamic_cast<MenuEntryAction*>(actionData->action());
 
129
            if (action && action->service() && (action->service()->storageId() == storageId))
 
130
                {
 
131
                return actionData;
 
132
                }
 
133
            }
 
134
        }
 
135
 
 
136
    return NULL;
 
137
    }
 
138
 
 
139
 
 
140
QString KHotKeysModule::get_menuentry_shortcut(const QString &storageId)
 
141
    {
 
142
    SimpleActionData* actionData = menuentry_action(storageId);
 
143
 
 
144
    // No action found
 
145
    if (actionData == NULL) return "";
 
146
 
 
147
    // The action must have a shortcut trigger. but don't assume to much
 
148
    ShortcutTrigger* shortcutTrigger = dynamic_cast<ShortcutTrigger*>(actionData->trigger());
 
149
 
 
150
    Q_ASSERT(shortcutTrigger);
 
151
    if (shortcutTrigger == NULL) return "";
 
152
 
 
153
    return shortcutTrigger->shortcut().primary();
 
154
    }
 
155
 
 
156
 
 
157
QString KHotKeysModule::register_menuentry_shortcut(
 
158
        const QString &storageId,
 
159
        const QString &sequence)
 
160
    {
 
161
    kDebug() << storageId << "(" << sequence << ")";
 
162
 
 
163
    // Check the service we got. If it is invalid there is no need to
 
164
    // continue.
 
165
    KService::Ptr wantedService = KService::serviceByStorageId(storageId);
 
166
    if (wantedService.isNull())
 
167
        {
 
168
        kError() << "Storage Id " << storageId << "not valid";
 
169
        return "";
 
170
        }
 
171
 
 
172
    // Look for the action
 
173
    SimpleActionData* actionData = menuentry_action(storageId);
 
174
 
 
175
    // No action found. Create on if sequence is != ""
 
176
    if (actionData == NULL)
 
177
        {
 
178
        kDebug() << "No action found";
 
179
 
 
180
        // If the sequence is empty there is no need to create a action.
 
181
        if (sequence.isEmpty()) return "";
 
182
 
 
183
        kDebug() << "Creating a new action";
 
184
 
 
185
        // Create the action
 
186
        ActionDataGroup *menuentries = _settings.get_system_group(
 
187
                ActionDataGroup::SYSTEM_MENUENTRIES);
 
188
 
 
189
        MenuEntryShortcutActionData *newAction = new MenuEntryShortcutActionData(
 
190
                menuentries,
 
191
                wantedService->name(),
 
192
                storageId,
 
193
                KShortcut(sequence),
 
194
                storageId);
 
195
 
 
196
        newAction->enable();
 
197
 
 
198
        _settings.write();
 
199
 
 
200
        // Return the real shortcut
 
201
        return newAction->trigger()->shortcut().primary();
 
202
        }
 
203
    // We found a action
 
204
    else
 
205
        {
 
206
        if (sequence.isEmpty())
 
207
            {
 
208
            kDebug() << "Deleting the action";
 
209
            actionData->aboutToBeErased();
 
210
            delete actionData;
 
211
            _settings.write();
 
212
            return "";
 
213
            }
 
214
        else
 
215
            {
 
216
            kDebug() << "Changing the action";
 
217
            // The action must have a shortcut trigger. but don't assume to much
 
218
            ShortcutTrigger* shortcutTrigger =
 
219
                    dynamic_cast<ShortcutTrigger*>(actionData->trigger());
 
220
            Q_ASSERT(shortcutTrigger);
 
221
            if (shortcutTrigger == NULL) return "";
 
222
 
 
223
            // Change the actionData
 
224
            shortcutTrigger->set_key_sequence(sequence);
 
225
            _settings.write();
 
226
 
 
227
            // Remove the resulting real shortcut
 
228
            return shortcutTrigger->shortcut().primary();
 
229
            }
 
230
        }
 
231
 
 
232
    Q_ASSERT(false);
 
233
    return "";
 
234
    }
 
235
 
 
236
 
 
237
void KHotKeysModule::quit()
 
238
    {
 
239
    deleteLater();
 
240
    }
 
241
 
 
242
 
 
243
void KHotKeysModule::save()
 
244
    {
 
245
    KHotKeys::khotkeys_set_active( false );
 
246
    _settings.write();
 
247
    KHotKeys::khotkeys_set_active( true );
 
248
    }
 
249
 
 
250
#include "kded.moc"