not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / action_data / action_data_base.cpp
blob69a37a83711aa00d81a94f8d9911d4eb32a9444f
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 "action_data_base.h"
13 #include "generic_action_data.h"
14 #include "command_url_shortcut_action_data.h"
15 #include "menuentry_shortcut_action_data.h"
16 #include "keyboard_input_gesture_action_data.h"
18 #include "triggers/triggers.h"
19 #include "conditions/conditions.h"
22 #include <kconfiggroup.h>
23 #include <kdebug.h>
26 namespace KHotKeys
29 ActionDataBase::ActionDataBase(
30 ActionDataGroup* parent_P
31 ,const QString& name_P
32 ,const QString& comment_P
33 ,Condition_list* conditions_P
34 ,bool enabled_P)
35 : _parent(parent_P)
36 ,_conditions(conditions_P)
37 ,_name(name_P)
38 ,_comment(comment_P)
39 ,_enabled(enabled_P)
41 if( parent())
42 parent()->add_child( this );
43 if( _conditions != 0 )
44 _conditions->set_data( this );
48 ActionDataBase::ActionDataBase(
49 KConfigGroup& cfg_P
50 ,ActionDataGroup* parent_P)
51 : _parent( parent_P)
52 ,_conditions(NULL)
54 _name = cfg_P.readEntry( "Name" );
55 _comment = cfg_P.readEntry( "Comment" );
56 _enabled = cfg_P.readEntry( "Enabled", true);
57 KConfigGroup conditionsConfig( cfg_P.config(), cfg_P.name() + "Conditions" );
59 // Load the conditions if they exist
60 if ( conditionsConfig.exists() )
62 kDebug() << "Reading conditions";
63 _conditions = new Condition_list( conditionsConfig, this );
66 if (parent())
67 parent()->add_child( this );
71 ActionDataBase::~ActionDataBase()
73 if( parent())
74 parent()->remove_child( this );
75 delete _conditions;
79 bool ActionDataBase::cfg_is_enabled( KConfigGroup& cfg_P )
81 return cfg_P.readEntry( "Enabled", true);
85 void ActionDataBase::cfg_write( KConfigGroup& cfg_P ) const
87 kDebug() << cfg_P.keyList();
89 cfg_P.writeEntry( "Type", "ERROR" ); // derived classes should call with their type
90 cfg_P.writeEntry( "Name", name());
91 cfg_P.writeEntry( "Comment", comment());
92 cfg_P.writeEntry( "Enabled", enabled( true ));
94 if ( conditions() != 0 )
96 kDebug() << "writing conditions";
97 KConfigGroup conditionsConfig( cfg_P.config(), cfg_P.name() + "Conditions" );
98 conditions()->cfg_write( conditionsConfig );
103 QString ActionDataBase::comment() const
105 return _comment;
109 const Condition_list* ActionDataBase::conditions() const
111 return _conditions;
115 bool ActionDataBase::conditions_match() const
117 return ( conditions() ? conditions()->match() : true )
118 && ( parent() ? parent()->conditions_match() : true );
122 ActionDataBase* ActionDataBase::create_cfg_read( KConfigGroup& cfg_P, ActionDataGroup* parent_P )
124 QString type = cfg_P.readEntry( "Type" );
125 if( type == "ACTION_DATA_GROUP" )
127 if( cfg_P.readEntry( "AllowMerge", false ))
129 Q_FOREACH(ActionDataBase *child,parent_P->children())
131 if( ActionDataGroup* existing = dynamic_cast< ActionDataGroup* >(child))
133 if( cfg_P.readEntry( "Name" ) == existing->name())
134 return existing;
138 return new ActionDataGroup( cfg_P, parent_P );
140 if( type == "GENERIC_ACTION_DATA" )
141 return new Generic_action_data( cfg_P, parent_P );
142 else if( type == "KEYBOARD_INPUT_GESTURE_ACTION_DATA" )
143 return new Keyboard_input_gesture_action_data( cfg_P, parent_P );
144 else if( type == "SIMPLE_ACTION_DATA"
145 || type == "DCOP_SHORTCUT_ACTION_DATA" || type == "DBUS_SHORTCUT_ACTION_DATA"
146 || type == "MENUENTRY_SHORTCUT_ACTION_DATA"
147 || type == "COMMAND_URL_SHORTCUT_ACTION_DATA"
148 || type == "KEYBOARD_INPUT_SHORTCUT_ACTION_DATA"
149 || type == "ACTIVATE_WINDOW_SHORTCUT_ACTION_DATA" )
150 return new SimpleActionData( cfg_P, parent_P );
151 kWarning() << "Unknown ActionDataBase type read from cfg file\n";
152 return 0;
156 bool ActionDataBase::enabled( bool ignore_group_P ) const
158 if( ignore_group_P )
159 return _enabled;
160 else
161 return _enabled && ( parent() == 0 || parent()->enabled( false ));
165 QString ActionDataBase::name() const
167 return _name;
171 ActionDataGroup* ActionDataBase::parent() const
173 return _parent;
177 void ActionDataBase::set_comment( const QString &comment )
179 _comment = comment;
183 void ActionDataBase::set_enabled( bool enabled )
185 _enabled = enabled;
189 void ActionDataBase::set_name( const QString& name_P )
191 _name = name_P;
195 void ActionDataBase::reparent( ActionDataGroup* new_parent_P )
197 if( parent() == new_parent_P)
198 return;
200 if( parent())
201 parent()->remove_child( this );
202 _parent = new_parent_P;
203 if( parent())
204 parent()->add_child( this );
208 void ActionDataBase::set_conditions( Condition_list* conditions_P )
210 Q_ASSERT( _conditions == 0 );
211 _conditions = conditions_P;
215 } // namespace KHotKeys