Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / khotkeys / shared / action_data.cpp
blob194166383356bcb27366daa193dc7bc25f6cd023
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 #define _ACTION_DATA_CPP_
13 #include "action_data.h"
15 #include <kconfig.h>
16 #include <kconfiggroup.h>
18 #include "actions.h"
20 namespace KHotKeys
23 // Action_data_base
25 Action_data_base::Action_data_base( Action_data_group* parent_P, const QString& name_P,
26 const QString& comment_P, Condition_list* conditions_P, bool enabled_P )
27 : _parent( parent_P ), _conditions( conditions_P ), _name( name_P ), _comment( comment_P ),
28 _enabled( enabled_P )
30 if( parent())
31 parent()->add_child( this );
32 if( _conditions != NULL )
33 _conditions->set_data( this );
36 Action_data_base::Action_data_base( KConfigGroup& cfg_P, Action_data_group* parent_P )
37 : _parent( parent_P )
39 _name = cfg_P.readEntry( "Name" );
40 _comment = cfg_P.readEntry( "Comment" );
41 _enabled = cfg_P.readEntry( "Enabled", true);
42 KConfigGroup conditionsConfig( cfg_P.config(), cfg_P.name() + "Conditions" );
43 _conditions = new Condition_list( conditionsConfig, this );
44 if( parent())
45 parent()->add_child( this );
48 Action_data_base::~Action_data_base()
50 // kDebug( 1217 ) << "~Action_data_base() :" << this;
51 if( parent())
52 parent()->remove_child( this );
53 delete _conditions;
56 void Action_data_base::cfg_write( KConfigGroup& cfg_P ) const
58 cfg_P.writeEntry( "Type", "ERROR" ); // derived classes should call with their type
59 cfg_P.writeEntry( "Name", name());
60 cfg_P.writeEntry( "Comment", comment());
61 cfg_P.writeEntry( "Enabled", enabled( true ));
62 KConfigGroup conditionsConfig( cfg_P.config(), cfg_P.name() + "Conditions" );
63 assert( conditions() != NULL );
64 conditions()->cfg_write( conditionsConfig );
68 Action_data_base* Action_data_base::create_cfg_read( KConfigGroup& cfg_P, Action_data_group* parent_P )
70 QString type = cfg_P.readEntry( "Type" );
71 if( type == "ACTION_DATA_GROUP" )
73 if( cfg_P.readEntry( "AllowMerge", false ))
75 for( Action_data_group::ConstIterator it = parent_P->first_child();
76 it != parent_P->after_last_child();
77 ++it )
79 if( Action_data_group* existing = dynamic_cast< Action_data_group* >( *it ))
81 if( cfg_P.readEntry( "Name" ) == existing->name())
82 return existing;
86 return new Action_data_group( cfg_P, parent_P );
88 if( type == "GENERIC_ACTION_DATA" )
89 return new Generic_action_data( cfg_P, parent_P );
90 if( type == "COMMAND_URL_SHORTCUT_ACTION_DATA" )
91 return new Command_url_shortcut_action_data( cfg_P, parent_P );
92 if( type == "MENUENTRY_SHORTCUT_ACTION_DATA" )
93 return new Menuentry_shortcut_action_data( cfg_P, parent_P );
94 if( type == "DCOP_SHORTCUT_ACTION_DATA" || type == "DBUS_SHORTCUT_ACTION_DATA" )
95 return new Dbus_shortcut_action_data( cfg_P, parent_P );
96 if( type == "KEYBOARD_INPUT_SHORTCUT_ACTION_DATA" )
97 return new Keyboard_input_shortcut_action_data( cfg_P, parent_P );
98 if( type == "KEYBOARD_INPUT_GESTURE_ACTION_DATA" )
99 return new Keyboard_input_gesture_action_data( cfg_P, parent_P );
100 if( type == "ACTIVATE_WINDOW_SHORTCUT_ACTION_DATA" )
101 return new Activate_window_shortcut_action_data( cfg_P, parent_P );
102 kWarning( 1217 ) << "Unknown Action_data_base type read from cfg file\n";
103 return NULL;
106 bool Action_data_base::cfg_is_enabled( KConfigGroup& cfg_P )
108 return cfg_P.readEntry( "Enabled", true);
111 void Action_data_base::reparent( Action_data_group* new_parent_P )
113 if( parent())
114 parent()->remove_child( this );
115 _parent = new_parent_P;
116 if( parent())
117 parent()->add_child( this );
120 bool Action_data_base::enabled( bool ignore_group_P ) const
122 if( ignore_group_P )
123 return _enabled;
124 else
125 return _enabled && ( parent() == NULL || parent()->enabled( false ));
128 void Action_data_base::set_conditions( Condition_list* conditions_P )
130 assert( _conditions == NULL );
131 _conditions = conditions_P;
134 bool Action_data_base::conditions_match() const
136 return ( conditions() ? conditions()->match() : true )
137 && ( parent() ? parent()->conditions_match() : true );
140 // Action_data_group
142 Action_data_group::Action_data_group( KConfigGroup& cfg_P, Action_data_group* parent_P )
143 : Action_data_base( cfg_P, parent_P )
145 unsigned int system_group_tmp = cfg_P.readEntry( "SystemGroup", 0 );
146 if( system_group_tmp >= SYSTEM_MAX )
147 system_group_tmp = 0;
148 _system_group = static_cast< system_group_t >( system_group_tmp );
151 void Action_data_group::cfg_write( KConfigGroup& cfg_P ) const
153 Action_data_base::cfg_write( cfg_P );
154 cfg_P.writeEntry( "SystemGroup", int(system_group()));
155 cfg_P.writeEntry( "Type", "ACTION_DATA_GROUP" );
158 void Action_data_group::update_triggers()
160 for( Action_data_group::ConstIterator it = first_child();
161 it != after_last_child();
162 ++it )
163 ( *it )->update_triggers();
166 // Action_data
168 Action_data::Action_data( KConfigGroup& cfg_P, Action_data_group* parent_P )
169 : Action_data_base( cfg_P, parent_P )
171 KConfigGroup triggersGroup( cfg_P.config(), cfg_P.name() + "Triggers" );
172 _triggers = new Trigger_list( triggersGroup, this );
173 KConfigGroup actionsGroup( cfg_P.config(), cfg_P.name() + "Actions" );
174 _actions = new Action_list( actionsGroup, this );
177 Action_data::~Action_data()
179 // kDebug( 1217 ) << "~Action_data" << this;
180 delete _triggers;
181 delete _actions;
182 // CHECKME jeste remove z parenta ?
185 void Action_data::cfg_write( KConfigGroup& cfg_P ) const
187 Action_data_base::cfg_write( cfg_P );
188 KConfigGroup triggersGroup( cfg_P.config(), cfg_P.name() + "Triggers" );
189 triggers()->cfg_write( triggersGroup );
190 KConfigGroup actionsGroup( cfg_P.config(), cfg_P.name() + "Actions" );
191 actions()->cfg_write( actionsGroup );
194 void Action_data::execute()
196 for( Action_list::Iterator it( *_actions );
198 ++it )
199 it.current()->execute();
200 // CHECKME nebo nejak zpozdeni ?
203 void Action_data::add_trigger( Trigger* trigger_P )
205 _triggers->append( trigger_P );
208 void Action_data::add_triggers( Trigger_list* triggers_P )
210 for( Trigger_list::Iterator it = *triggers_P;
212 ++it )
213 _triggers->append( *it );
214 triggers_P->setAutoDelete( false );
215 delete triggers_P;
218 void Action_data::set_triggers( Trigger_list* triggers_P )
220 assert( _triggers == NULL );
221 _triggers = triggers_P;
224 void Action_data::add_action( Action* action_P, Action* after_P )
226 int index = 0;
227 for( Action_list::Iterator it = *_actions;
229 ++it )
231 ++index;
232 if( *it == after_P )
233 break;
235 _actions->insert( index, action_P );
238 void Action_data::add_actions( Action_list* actions_P, Action* after_P )
240 int index = 0;
241 for( Action_list::Iterator it = *_actions;
243 ++it )
245 ++index;
246 if( *it == after_P )
247 break;
249 for( Action_list::Iterator it = *actions_P;
251 ++it )
252 _actions->insert( index++, *it );
253 actions_P->setAutoDelete( false );
254 delete actions_P;
257 void Action_data::set_actions( Action_list* actions_P )
259 assert( _actions == NULL );
260 _actions = actions_P;
263 void Action_data::update_triggers()
265 bool activate = conditions_match() && enabled( false );
266 kDebug( 1217 ) << "Update triggers: " << name() << ":" << activate;
267 for( Trigger_list::Iterator it = ( *triggers());
269 ++it )
270 ( *it )->activate( activate );
273 // Generic_action_data
275 void Generic_action_data::cfg_write( KConfigGroup& cfg_P ) const
277 base::cfg_write( cfg_P );
278 cfg_P.writeEntry( "Type", "GENERIC_ACTION_DATA" );
281 // Command_url_shortcut_action_data
283 Command_url_shortcut_action_data::Command_url_shortcut_action_data( Action_data_group* parent_P,
284 const QString& name_P, const QString& comment_P,
285 const KShortcut& shortcut_P, const QString& command_url_P, bool enabled_P )
286 : Simple_action_data< Shortcut_trigger, Command_url_action >( parent_P, name_P,
287 comment_P, enabled_P )
289 set_action( new Command_url_action( this, command_url_P ));
290 set_trigger( new Shortcut_trigger( this, shortcut_P ));
293 template<> KDE_EXPORT
294 void Simple_action_data< Shortcut_trigger, Command_url_action >
295 ::cfg_write( KConfigGroup& cfg_P ) const
297 base::cfg_write( cfg_P );
298 cfg_P.writeEntry( "Type", "COMMAND_URL_SHORTCUT_ACTION_DATA" );
301 // Menuentry_shortcut_action_data
303 Menuentry_shortcut_action_data::Menuentry_shortcut_action_data( Action_data_group* parent_P,
304 const QString& name_P, const QString& comment_P,
305 const KShortcut& shortcut_P, const QString& menuentry_P, bool enabled_P )
306 : Simple_action_data< Shortcut_trigger, Menuentry_action >( parent_P, name_P,
307 comment_P, enabled_P )
309 set_action( new Menuentry_action( this, menuentry_P ));
310 set_trigger( new Shortcut_trigger( this, shortcut_P ));
313 template<> KDE_EXPORT
314 void Simple_action_data< Shortcut_trigger, Menuentry_action >
315 ::cfg_write( KConfigGroup& cfg_P ) const
317 base::cfg_write( cfg_P );
318 cfg_P.writeEntry( "Type", "MENUENTRY_SHORTCUT_ACTION_DATA" );
321 // Dbus_shortcut_action_data
323 template<> KDE_EXPORT
324 void Simple_action_data< Shortcut_trigger, Dbus_action >
325 ::cfg_write( KConfigGroup& cfg_P ) const
327 base::cfg_write( cfg_P );
328 cfg_P.writeEntry( "Type", "DBUS_SHORTCUT_ACTION_DATA" );
331 // Keyboard_input_shortcut_action_data
333 template<> KDE_EXPORT
334 void Simple_action_data< Shortcut_trigger, Keyboard_input_action >
335 ::cfg_write( KConfigGroup& cfg_P ) const
337 base::cfg_write( cfg_P );
338 cfg_P.writeEntry( "Type", "KEYBOARD_INPUT_SHORTCUT_ACTION_DATA" );
341 // Activate_window_shortcut_action_data
343 template<> KDE_EXPORT
344 void Simple_action_data< Shortcut_trigger, Activate_window_action >
345 ::cfg_write( KConfigGroup& cfg_P ) const
347 base::cfg_write( cfg_P );
348 cfg_P.writeEntry( "Type", "ACTIVATE_WINDOW_SHORTCUT_ACTION_DATA" );
351 // Keyboard_input_gesture_action_data
353 void Keyboard_input_gesture_action_data::set_action( Keyboard_input_action* action_P )
355 Action_list* tmp = new Action_list( "Keyboard_input_gesture_action_data" );
356 tmp->append( action_P );
357 set_actions( tmp );
360 const Keyboard_input_action* Keyboard_input_gesture_action_data::action() const
362 if( actions() == NULL ) // CHECKME tohle poradne zkontrolovat
363 return NULL;
364 return static_cast< Keyboard_input_action* >( const_cast< Action_list* >( actions())->first());
367 void Keyboard_input_gesture_action_data::cfg_write( KConfigGroup& cfg_P ) const
369 base::cfg_write( cfg_P );
370 cfg_P.writeEntry( "Type", "KEYBOARD_INPUT_GESTURE_ACTION_DATA" );
373 } // namespace KHotKeys