not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / app / kded.cpp
blobce0f4a5601ce7a08c3f97cf4ad4391882f3207b6
1 /****************************************************************************
3 KHotKeys
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 ****************************************************************************/
11 #include "kded.h"
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>
21 #include <kdebug.h>
22 #include <kpluginfactory.h>
23 #include <kpluginloader.h>
25 #include <unistd.h>
28 K_PLUGIN_FACTORY(KHotKeysModuleFactory,
29 registerPlugin<KHotKeysModule>();
31 K_EXPORT_PLUGIN(KHotKeysModuleFactory("khotkeys"))
33 using namespace KHotKeys;
35 // KhotKeysModule
37 KHotKeysModule::KHotKeysModule(QObject* parent, const QList<QVariant>&)
38 : KDEDModule(parent)
39 , actions_root(NULL)
40 , _settings()
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.
55 actions_root = NULL;
59 void KHotKeysModule::reread_configuration()
61 kDebug() << "Reloading the khotkeys configuration";
63 // Stop listening
64 actions_root = NULL; // Disables the dbus interface effectively
65 KHotKeys::khotkeys_set_active( false );
67 // Load the settings
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() );
73 // FIXME: SOUND
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))
97 return actionData;
102 return NULL;
105 QString KHotKeysModule::get_menuentry_shortcut(const QString &storageId)
107 SimpleActionData* actionData = menuentry_action(storageId);
109 // No action found
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
129 // continue.
130 KService::Ptr wantedService = KService::serviceByStorageId(storageId);
131 if (wantedService.isNull())
133 kError() << "Storage Id " << storageId << "not valid";
134 return "";
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";
150 // Create the action
151 ActionDataGroup *menuentries = _settings.get_system_group(
152 ActionDataGroup::SYSTEM_MENUENTRIES);
154 MenuEntryShortcutActionData *newAction = new MenuEntryShortcutActionData(
155 menuentries,
156 wantedService->name(),
157 storageId,
158 KShortcut(sequence),
159 storageId);
161 _settings.write_settings();
163 // Return the real shortcut
164 return newAction->trigger()->shortcut().primary();
166 // We found a action
167 else
169 if (sequence.isEmpty())
171 kDebug() << "Deleting the action";
172 actionData->aboutToBeErased();
173 delete actionData;
174 _settings.write_settings();
175 return "";
177 else
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();
195 Q_ASSERT(false);
196 return "";
200 void KHotKeysModule::quit()
202 deleteLater();
206 #include "kded.moc"