Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / khotkeys / kcontrol / menuedit.cpp
bloba063bc20fd4ef5439913900628d250c939278ba7
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 // BEWARE ! unbelievably messy code
13 #define _MENUEDIT_CPP_
17 #include "menuedit.h"
19 #include <kglobal.h>
20 #include <klocale.h>
21 #include <kapplication.h>
22 #include <QLabel>
23 #include <QLineEdit>
24 #include <QLayout>
25 //#include <kkeydialog.h>
26 #include <QtDBus/QtDBus>
27 #include "khotkeysiface.h"
29 #include <settings.h>
30 #include <action_data.h>
31 #include <ktoolinvocation.h>
33 namespace KHotKeys
36 static QObject* owner = NULL;
38 void khotkeys_init()
40 // I hope this works
41 KGlobal::locale()->insertCatalog("khotkeys");
42 // CHECKME hack
43 assert( owner == NULL );
44 owner = new QObject;
45 init_global_data( false, owner );
48 void khotkeys_cleanup()
50 delete owner;
51 owner = NULL;
54 Menuentry_shortcut_action_data* khotkeys_get_menu_entry_internal2(
55 const Action_data_group* data_P, const QString& entry_P )
57 if( !data_P->enabled( false ))
58 return NULL;
59 for( Action_data_group::ConstIterator it = data_P->first_child();
60 it != data_P->after_last_child();
61 ++it )
63 if( !(*it)->enabled( true ))
64 continue;
65 if( Menuentry_shortcut_action_data* entry
66 = dynamic_cast< Menuentry_shortcut_action_data* >( *it ))
68 KService::Ptr service = entry->action() ? entry->action()->service() : KService::Ptr(0);
69 if ( service && (service->storageId() == entry_P) )
70 return entry;
72 if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
74 Menuentry_shortcut_action_data* data
75 = khotkeys_get_menu_entry_internal2( group, entry_P );
76 if( data != NULL )
77 return data;
80 return NULL;
83 Action_data_group* khotkeys_get_menu_root( Action_data_group* data_P )
85 for( Action_data_group::ConstIterator it = data_P->first_child();
86 it != data_P->after_last_child();
87 ++it )
88 if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
90 if( group->system_group() == Action_data_group::SYSTEM_MENUENTRIES )
91 return group;
93 return new Action_data_group( data_P, i18n( MENU_EDITOR_ENTRIES_GROUP_NAME ),
94 i18n( "These entries were created using Menu Editor." ), new Condition_list( "", NULL ), // CHECKME tenhle condition list
95 Action_data_group::SYSTEM_MENUENTRIES, true );
98 Menuentry_shortcut_action_data* khotkeys_get_menu_entry_internal( Action_data_group* data_P,
99 const QString& entry_P )
101 return khotkeys_get_menu_entry_internal2( khotkeys_get_menu_root( data_P ), entry_P );
104 QString khotkeys_get_menu_shortcut( Menuentry_shortcut_action_data* data_P )
106 if( data_P->trigger() != NULL )
107 return data_P->trigger()->shortcut().toString();
108 return "";
111 void khotkeys_get_all_shortcuts_internal(const Action_data_group* data_P, QStringList &result)
113 if( !data_P->enabled( false ))
114 return;
115 for( Action_data_group::ConstIterator it = data_P->first_child();
116 it != data_P->after_last_child();
117 ++it )
119 if( !(*it)->enabled( true ))
120 continue;
121 if( Menuentry_shortcut_action_data* entry
122 = dynamic_cast< Menuentry_shortcut_action_data* >( *it ))
124 if (entry->trigger() && !entry->trigger()->shortcut().isEmpty())
125 result.append(entry->trigger()->shortcut().toString());
127 if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
129 khotkeys_get_all_shortcuts_internal( group, result );
135 QStringList khotkeys_get_all_shortcuts( )
137 QStringList result;
138 Settings settings;
139 settings.read_settings( true );
141 khotkeys_get_all_shortcuts_internal(settings.actions, result);
143 return result;
147 KService::Ptr khotkeys_find_menu_entry_internal(const Action_data_group* data_P, const QString &shortcut_P)
149 if( !data_P->enabled( false ))
150 return KService::Ptr();
151 for( Action_data_group::ConstIterator it = data_P->first_child();
152 it != data_P->after_last_child();
153 ++it )
155 if( !(*it)->enabled( true ))
156 continue;
157 if( Menuentry_shortcut_action_data* entry
158 = dynamic_cast< Menuentry_shortcut_action_data* >( *it ))
160 if (entry->trigger() &&
161 entry->trigger()->shortcut().toString() == shortcut_P)
163 if (entry->action())
164 return entry->action()->service();
165 return KService::Ptr();
168 if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
170 KService::Ptr result = khotkeys_find_menu_entry_internal( group, shortcut_P );
171 if (result)
172 return result;
175 return KService::Ptr();
179 KService::Ptr khotkeys_find_menu_entry( const QString& shortcut_P )
181 Settings settings;
182 settings.read_settings( true );
184 return khotkeys_find_menu_entry_internal(settings.actions, shortcut_P);
188 void khotkeys_send_reread_config()
190 QByteArray data;
191 QDBusConnection bus = QDBusConnection::sessionBus();
192 if( !bus.interface()->isServiceRegistered( "org.kde.khotkeys" ))
194 kDebug( 1217 ) << "launching new khotkeys daemon";
195 KToolInvocation::kdeinitExec( "khotkeys" );
197 else
199 org::kde::khotkeys iface("org.kde.khotkeys", "/KHotKeys", bus);
200 iface.reread_configuration();
201 kDebug( 1217 ) << "telling khotkeys daemon to reread configuration";
205 QString khotkeys_get_menu_entry_shortcut( const QString& entry_P )
207 Settings settings;
208 settings.read_settings( true );
209 Menuentry_shortcut_action_data* entry
210 = khotkeys_get_menu_entry_internal( settings.actions, entry_P );
211 if( entry == NULL )
213 delete settings.actions;
214 return "";
216 QString shortcut = khotkeys_get_menu_shortcut( entry );
217 delete settings.actions;
218 return shortcut;
221 bool khotkeys_menu_entry_moved( const QString& new_P, const QString& old_P )
223 Settings settings;
224 settings.read_settings( true );
225 Menuentry_shortcut_action_data* entry
226 = khotkeys_get_menu_entry_internal( settings.actions, old_P );
227 if( entry == NULL )
229 delete settings.actions;
230 return false;
232 Action_data_group* parent = entry->parent();
233 QString new_name = new_P;
234 if( entry->name().startsWith( i18n( "K Menu - " )))
235 new_name = i18n( "K Menu - " ) + new_P;
236 Menuentry_shortcut_action_data* new_entry = new Menuentry_shortcut_action_data( parent,
237 new_name, entry->comment(), entry->enabled( true ));
238 new_entry->set_trigger( entry->trigger()->copy( new_entry ));
239 new_entry->set_action( new Menuentry_action( new_entry, new_P ));
240 delete entry;
241 settings.write_settings();
242 delete settings.actions;
243 khotkeys_send_reread_config();
244 return true;
247 void khotkeys_menu_entry_deleted( const QString& entry_P )
249 Settings settings;
250 settings.read_settings( true );
251 Menuentry_shortcut_action_data* entry
252 = khotkeys_get_menu_entry_internal( settings.actions, entry_P );
253 if( entry == NULL )
255 delete settings.actions;
256 return;
258 delete entry;
259 settings.write_settings();
260 delete settings.actions;
261 khotkeys_send_reread_config();
264 QString khotkeys_change_menu_entry_shortcut( const QString& entry_P,
265 const QString& shortcut_P )
267 Settings settings;
268 settings.read_settings( true );
269 Menuentry_shortcut_action_data* entry
270 = khotkeys_get_menu_entry_internal( settings.actions, entry_P );
271 bool new_entry = ( entry == NULL );
272 if( new_entry )
274 entry = new Menuentry_shortcut_action_data( NULL, i18n( "K Menu - " ) + entry_P,
275 "" );
276 entry->set_action( new Menuentry_action( entry, entry_P ));
278 else
280 // erase the trigger, i.e. replace with a copy with no trigger and no parent yet
281 Menuentry_shortcut_action_data* entry_tmp = new Menuentry_shortcut_action_data( NULL,
282 entry->name(), entry->comment(), entry->enabled( false ));
283 entry_tmp->set_action( new Menuentry_action( entry_tmp, entry_P ));
284 delete entry;
285 entry = entry_tmp;
287 QString shortcut = "";
288 // make sure the shortcut is valid
289 shortcut = (KShortcut( shortcut_P )).toString();
290 if( !shortcut.isEmpty())
291 entry->set_trigger( new Shortcut_trigger( entry, KShortcut( shortcut )));
292 if( shortcut.isEmpty())
294 delete entry;
295 if( !new_entry ) // remove from config file
297 settings.write_settings();
298 khotkeys_send_reread_config();
300 delete settings.actions;
301 return "";
303 entry->reparent( khotkeys_get_menu_root( settings.actions ));
304 settings.daemon_disabled = false; // #91782
305 settings.write_settings();
306 khotkeys_send_reread_config();
307 return shortcut;
310 // CHECKME nejaka forma kontroly, ze tahle kl. kombinace neni pouzita pro jiny menuentry ?
312 } // namespace KHotKeys
315 // exported functions
318 void khotkeys_init()
320 KHotKeys::khotkeys_init();
323 void khotkeys_cleanup()
325 KHotKeys::khotkeys_cleanup();
328 QString khotkeys_get_menu_entry_shortcut( const QString& entry_P )
330 return KHotKeys::khotkeys_get_menu_entry_shortcut( entry_P );
333 bool khotkeys_menu_entry_moved( const QString& new_P, const QString& old_P )
335 return KHotKeys::khotkeys_menu_entry_moved( new_P, old_P );
338 void khotkeys_menu_entry_deleted( const QString& entry_P )
340 KHotKeys::khotkeys_menu_entry_deleted( entry_P );
343 QString khotkeys_change_menu_entry_shortcut( const QString& entry_P,
344 const QString& shortcut_P )
346 return KHotKeys::khotkeys_change_menu_entry_shortcut( entry_P, shortcut_P );
349 QStringList khotkeys_get_all_shortcuts( )
351 return KHotKeys::khotkeys_get_all_shortcuts();
354 KService::Ptr khotkeys_find_menu_entry( const QString& shortcut_P )
356 return KHotKeys::khotkeys_find_menu_entry( shortcut_P );