1 /****************************************************************************
5 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
7 Distributed under the terms of the GNU General Public License version 2.
9 ****************************************************************************/
13 #include "action_data/action_data_group.h"
14 #include "action_data/menuentry_shortcut_action_data.h"
15 #include "actions/actions.h"
17 #include "triggers/gestures.h"
20 #include <kaboutdata.h>
22 #include <kpluginfactory.h>
23 #include <kpluginloader.h>
28 K_PLUGIN_FACTORY(KHotKeysModuleFactory
,
29 registerPlugin
<KHotKeysModule
>();
31 K_EXPORT_PLUGIN(KHotKeysModuleFactory("khotkeys"))
33 using namespace KHotKeys
;
37 KHotKeysModule::KHotKeysModule(QObject
* parent
, const QList
<QVariant
>&)
42 setModuleName("khotkeys");
44 // Initialize the global data, grab keys
45 KHotKeys::init_global_data( true, this );
47 // Read the configuration from file khotkeysrc
48 reread_configuration();
52 KHotKeysModule::~KHotKeysModule()
54 // actions_root belongs to _settings.
59 void KHotKeysModule::reread_configuration()
61 kDebug() << "Reloading the khotkeys configuration";
64 actions_root
= NULL
; // Disables the dbus interface effectively
65 KHotKeys::khotkeys_set_active( false );
68 _settings
.read_settings( false );
69 KHotKeys::gesture_handler
->set_mouse_button( _settings
.gestureMouseButton() );
70 KHotKeys::gesture_handler
->set_timeout( _settings
.gestureTimeOut() );
71 KHotKeys::gesture_handler
->enable( !_settings
.areGesturesDisabled() );
72 KHotKeys::gesture_handler
->set_exclude( _settings
.gesturesExclude() );
74 // KHotKeys::voice_handler->set_shortcut( _settings.voice_shortcut );
75 actions_root
= _settings
.actions();
76 KHotKeys::khotkeys_set_active( true );
82 SimpleActionData
* KHotKeysModule::menuentry_action(const QString
&storageId
)
84 ActionDataGroup
*menuentries
= _settings
.get_system_group(
85 ActionDataGroup::SYSTEM_MENUENTRIES
);
87 // Now try to find the action
88 Q_FOREACH(ActionDataBase
* element
, menuentries
->children())
90 SimpleActionData
*actionData
= dynamic_cast<SimpleActionData
*>(element
);
92 if (actionData
&& actionData
->action())
94 MenuEntryAction
*action
= dynamic_cast<MenuEntryAction
*>(actionData
->action());
95 if (action
&& action
->service() && (action
->service()->storageId() == storageId
))
105 QString
KHotKeysModule::get_menuentry_shortcut(const QString
&storageId
)
107 SimpleActionData
* actionData
= menuentry_action(storageId
);
110 if (actionData
== NULL
) return "";
112 // The action must have a shortcut trigger. but don't assume to much
113 ShortcutTrigger
* shortcutTrigger
= dynamic_cast<ShortcutTrigger
*>(actionData
->trigger());
115 Q_ASSERT(shortcutTrigger
);
116 if (shortcutTrigger
== NULL
) return "";
118 return shortcutTrigger
->shortcut().primary();
122 QString
KHotKeysModule::register_menuentry_shortcut(
123 const QString
&storageId
,
124 const QString
&sequence
)
126 kDebug() << storageId
<< "(" << sequence
<< ")";
128 // Check the service we got. If it is invalid there is no need to
130 KService::Ptr wantedService
= KService::serviceByStorageId(storageId
);
131 if (wantedService
.isNull())
133 kError() << "Storage Id " << storageId
<< "not valid";
137 // Look for the action
138 SimpleActionData
* actionData
= menuentry_action(storageId
);
140 // No action found. Create on if sequence is != ""
141 if (actionData
== NULL
)
143 kDebug() << "No action found";
145 // If the sequence is empty there is no need to create a action.
146 if (sequence
.isEmpty()) return "";
148 kDebug() << "Creating a new action";
151 ActionDataGroup
*menuentries
= _settings
.get_system_group(
152 ActionDataGroup::SYSTEM_MENUENTRIES
);
154 MenuEntryShortcutActionData
*newAction
= new MenuEntryShortcutActionData(
156 wantedService
->name(),
161 _settings
.write_settings();
163 // Return the real shortcut
164 return newAction
->trigger()->shortcut().primary();
169 if (sequence
.isEmpty())
171 kDebug() << "Deleting the action";
172 actionData
->aboutToBeErased();
174 _settings
.write_settings();
179 kDebug() << "Changing the action";
180 // The action must have a shortcut trigger. but don't assume to much
181 ShortcutTrigger
* shortcutTrigger
=
182 dynamic_cast<ShortcutTrigger
*>(actionData
->trigger());
183 Q_ASSERT(shortcutTrigger
);
184 if (shortcutTrigger
== NULL
) return "";
186 // Change the actionData
187 shortcutTrigger
->set_key_sequence(sequence
);
188 _settings
.write_settings();
190 // Remove the resulting real shortcut
191 return shortcutTrigger
->shortcut().primary();
200 void KHotKeysModule::quit()