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 ****************************************************************************/
11 // BEWARE ! unbelievably messy code
13 #define _MENUEDIT_CPP_
21 #include <kapplication.h>
25 //#include <kkeydialog.h>
26 #include <QtDBus/QtDBus>
27 #include "khotkeysiface.h"
30 #include <action_data.h>
31 #include <ktoolinvocation.h>
36 static QObject
* owner
= NULL
;
41 KGlobal::locale()->insertCatalog("khotkeys");
43 assert( owner
== NULL
);
45 init_global_data( false, owner
);
48 void khotkeys_cleanup()
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 ))
59 for( Action_data_group::ConstIterator it
= data_P
->first_child();
60 it
!= data_P
->after_last_child();
63 if( !(*it
)->enabled( true ))
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
) )
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
);
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();
88 if( Action_data_group
* group
= dynamic_cast< Action_data_group
* >( *it
))
90 if( group
->system_group() == Action_data_group::SYSTEM_MENUENTRIES
)
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();
111 void khotkeys_get_all_shortcuts_internal(const Action_data_group
* data_P
, QStringList
&result
)
113 if( !data_P
->enabled( false ))
115 for( Action_data_group::ConstIterator it
= data_P
->first_child();
116 it
!= data_P
->after_last_child();
119 if( !(*it
)->enabled( true ))
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( )
139 settings
.read_settings( true );
141 khotkeys_get_all_shortcuts_internal(settings
.actions
, 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();
155 if( !(*it
)->enabled( true ))
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
)
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
);
175 return KService::Ptr();
179 KService::Ptr
khotkeys_find_menu_entry( const QString
& shortcut_P
)
182 settings
.read_settings( true );
184 return khotkeys_find_menu_entry_internal(settings
.actions
, shortcut_P
);
188 void khotkeys_send_reread_config()
191 QDBusConnection bus
= QDBusConnection::sessionBus();
192 if( !bus
.interface()->isServiceRegistered( "org.kde.khotkeys" ))
194 kDebug( 1217 ) << "launching new khotkeys daemon";
195 KToolInvocation::kdeinitExec( "khotkeys" );
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
)
208 settings
.read_settings( true );
209 Menuentry_shortcut_action_data
* entry
210 = khotkeys_get_menu_entry_internal( settings
.actions
, entry_P
);
213 delete settings
.actions
;
216 QString shortcut
= khotkeys_get_menu_shortcut( entry
);
217 delete settings
.actions
;
221 bool khotkeys_menu_entry_moved( const QString
& new_P
, const QString
& old_P
)
224 settings
.read_settings( true );
225 Menuentry_shortcut_action_data
* entry
226 = khotkeys_get_menu_entry_internal( settings
.actions
, old_P
);
229 delete settings
.actions
;
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
));
241 settings
.write_settings();
242 delete settings
.actions
;
243 khotkeys_send_reread_config();
247 void khotkeys_menu_entry_deleted( const QString
& entry_P
)
250 settings
.read_settings( true );
251 Menuentry_shortcut_action_data
* entry
252 = khotkeys_get_menu_entry_internal( settings
.actions
, entry_P
);
255 delete settings
.actions
;
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
)
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
);
274 entry
= new Menuentry_shortcut_action_data( NULL
, i18n( "K Menu - " ) + entry_P
,
276 entry
->set_action( new Menuentry_action( entry
, entry_P
));
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
));
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())
295 if( !new_entry
) // remove from config file
297 settings
.write_settings();
298 khotkeys_send_reread_config();
300 delete settings
.actions
;
303 entry
->reparent( khotkeys_get_menu_root( settings
.actions
));
304 settings
.daemon_disabled
= false; // #91782
305 settings
.write_settings();
306 khotkeys_send_reread_config();
310 // CHECKME nejaka forma kontroly, ze tahle kl. kombinace neni pouzita pro jiny menuentry ?
312 } // namespace KHotKeys
315 // exported functions
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
);