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 #define _CONDITIONS_CPP_
13 #include "conditions.h"
20 #include <kconfiggroup.h>
28 #include "action_data.h"
37 Condition::Condition( Condition_list_base
* parent_P
)
41 _parent
->append( this );
44 Condition::Condition( KConfigGroup
&, Condition_list_base
* parent_P
)
48 _parent
->append( this );
51 Condition
* Condition::create_cfg_read( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
53 QString type
= cfg_P
.readEntry( "Type" );
54 if( type
== "ACTIVE_WINDOW" )
55 return new Active_window_condition( cfg_P
, parent_P
);
56 if( type
== "EXISTING_WINDOW" )
57 return new Existing_window_condition( cfg_P
, parent_P
);
59 return new Not_condition( cfg_P
, parent_P
);
61 return new And_condition( cfg_P
, parent_P
);
63 return new Or_condition( cfg_P
, parent_P
);
64 kWarning( 1217 ) << "Unknown Condition type read from cfg file\n";
68 Condition::~Condition()
71 _parent
->remove( this );
75 void Condition::cfg_write( KConfigGroup
& cfg_P
) const
77 cfg_P
.writeEntry( "Type", "ERROR" );
80 void Condition::updated() const
82 if( !khotkeys_active())
84 assert( _parent
!= NULL
);
89 void Condition::debug( int depth_P
)
98 kDebug( 1217 ) << tmp
<< description() << ":(" << this << ")";
101 void Condition::debug_list( const Q3PtrList
< Condition
>& list_P
, int depth_P
)
110 for( Q3PtrListIterator
< Condition
> it( list_P
);
113 (*it
)->debug( depth_P
+ 1 );
118 // Condition_list_base
120 Condition_list_base::Condition_list_base( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
121 : Condition( parent_P
)
123 int cnt
= cfg_P
.readEntry( "ConditionsCount", 0 );
128 KConfigGroup
conditionConfig( cfg_P
.config(), cfg_P
.name() + QString::number( i
) );
129 (void) Condition::create_cfg_read( conditionConfig
, this );
133 Condition_list_base::~Condition_list_base()
137 Condition
* c
= getFirst();
143 void Condition_list_base::cfg_write( KConfigGroup
& cfg_P
) const
146 for( Iterator
it( *this );
150 KConfigGroup
conditionConfig( cfg_P
.config(), cfg_P
.name() + QString::number( i
) );
151 it
.current()->cfg_write( conditionConfig
);
153 cfg_P
.writeEntry( "ConditionsCount", i
);
156 bool Condition_list_base::accepts_children() const
161 #ifdef KHOTKEYS_DEBUG
162 void Condition_list_base::debug( int depth_P
)
171 kDebug( 1217 ) << tmp
<< typeid( *this ).name() << ":(" << this << ")";
172 debug_list( *this, depth_P
+ 1 );
178 Condition_list::Condition_list( KConfigGroup
& cfg_P
, Action_data_base
* data_P
)
179 : Condition_list_base( cfg_P
, NULL
), data( data_P
)
181 _comment
= cfg_P
.readEntry( "Comment" );
184 void Condition_list::cfg_write( KConfigGroup
& cfg_P
) const
186 base::cfg_write( cfg_P
);
187 cfg_P
.writeEntry( "Comment", comment());
190 Condition_list
* Condition_list::copy( Action_data_base
* data_P
) const
192 Condition_list
* ret
= new Condition_list( comment(), data_P
);
193 for( Iterator
it( *this );
196 ret
->append( it
.current()->copy( ret
));
201 bool Condition_list::match() const
203 if( count() == 0 ) // no conditions to match => ok
205 for( Iterator
it( *this );
208 if( it
.current()->match()) // OR
213 void Condition_list::updated() const
215 if( !khotkeys_active())
217 data
->update_triggers();
218 // base::updated(); no need to, doesn't have parent
221 // CHECKME tohle je drobet hack, jeste to zvazit
222 void Condition_list::set_data( Action_data_base
* data_P
)
224 assert( data
== NULL
|| data
== data_P
);
228 const QString
Condition_list::description() const
234 Condition_list
* Condition_list::copy( Condition_list_base
* ) const
240 // Active_window_condition
242 Active_window_condition::Active_window_condition( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
243 : Condition( cfg_P
, parent_P
)
245 KConfigGroup
windowConfig( cfg_P
.config(), cfg_P
.name() + "Window" );
246 _window
= new Windowdef_list( windowConfig
);
251 void Active_window_condition::init()
253 connect( windows_handler
, SIGNAL( active_window_changed( WId
)),
254 this, SLOT( active_window_changed( WId
)));
257 bool Active_window_condition::match() const
262 void Active_window_condition::set_match()
264 is_match
= window()->match( Window_data( windows_handler
->active_window()));
265 kDebug( 1217 ) << "Active_window_condition::set_match :" << is_match
;
269 void Active_window_condition::cfg_write( KConfigGroup
& cfg_P
) const
271 base::cfg_write( cfg_P
);
272 KConfigGroup
windowConfig( cfg_P
.config(), cfg_P
.name() + "Window" );
273 window()->cfg_write( windowConfig
);
274 cfg_P
.writeEntry( "Type", "ACTIVE_WINDOW" ); // overwrites value set in base::cfg_write()
277 #ifdef HAVE_COVARIANT_RETURN
278 Active_window_condition
* Active_window_condition::copy( Condition_list_base
* parent_P
) const
280 Condition
* Active_window_condition::copy( Condition_list_base
* parent_P
) const
283 return new Active_window_condition( window()->copy(), parent_P
);
286 const QString
Active_window_condition::description() const
288 return i18n( "Active window: " ) + window()->comment();
291 void Active_window_condition::active_window_changed( WId
)
296 Active_window_condition::~Active_window_condition()
298 disconnect( windows_handler
, NULL
, this, NULL
);
302 // Existing_window_condition
304 Existing_window_condition::Existing_window_condition( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
305 : Condition( cfg_P
, parent_P
)
307 KConfigGroup
windowConfig( cfg_P
.config(), cfg_P
.name() + "Window" );
308 _window
= new Windowdef_list( windowConfig
);
313 void Existing_window_condition::init()
315 connect( windows_handler
, SIGNAL( window_added( WId
)), this, SLOT( window_added( WId
)));
316 connect( windows_handler
, SIGNAL( window_removed( WId
)), this, SLOT( window_removed( WId
)));
319 bool Existing_window_condition::match() const
324 void Existing_window_condition::set_match( WId w_P
)
326 if( w_P
!= None
&& !is_match
)
327 is_match
= window()->match( Window_data( w_P
));
329 is_match
= windows_handler
->find_window( window()) != None
;
330 kDebug( 1217 ) << "Existing_window_condition::set_match :" << is_match
;
334 void Existing_window_condition::cfg_write( KConfigGroup
& cfg_P
) const
336 base::cfg_write( cfg_P
);
337 KConfigGroup
windowConfig( cfg_P
.config(), cfg_P
.name() + "Window" );
338 window()->cfg_write( windowConfig
);
339 cfg_P
.writeEntry( "Type", "EXISTING_WINDOW" ); // overwrites value set in base::cfg_write()
342 #ifdef HAVE_COVARIANT_RETURN
343 Existing_window_condition
* Existing_window_condition::copy( Condition_list_base
* parent_P
) const
345 Condition
* Existing_window_condition::copy( Condition_list_base
* parent_P
) const
348 return new Existing_window_condition( window()->copy(), parent_P
);
351 const QString
Existing_window_condition::description() const
353 return i18n( "Existing window: " ) + window()->comment();
356 void Existing_window_condition::window_added( WId w_P
)
361 void Existing_window_condition::window_removed( WId
)
366 Existing_window_condition::~Existing_window_condition()
368 disconnect( windows_handler
, NULL
, this, NULL
);
374 Not_condition::Not_condition( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
375 : Condition_list_base( cfg_P
, parent_P
)
377 // CHECKME kontrola poctu ?
380 bool Not_condition::match() const
382 return condition() ? !condition()->match() : false;
385 void Not_condition::cfg_write( KConfigGroup
& cfg_P
) const
387 base::cfg_write( cfg_P
);
388 cfg_P
.writeEntry( "Type", "NOT" ); // overwrites value set in base::cfg_write()
391 Not_condition
* Not_condition::copy( Condition_list_base
* parent_P
) const
393 Not_condition
* ret
= new Not_condition( parent_P
);
395 ret
->append( condition()->copy( ret
));
399 const QString
Not_condition::description() const
401 return i18nc( "Not_condition", "Not" );
404 bool Not_condition::accepts_children() const
411 And_condition::And_condition( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
412 : Condition_list_base( cfg_P
, parent_P
)
414 // CHECKME kontrola poctu ?
417 bool And_condition::match() const
419 for( Iterator
it( *this );
422 if( !it
.current()->match()) // AND
424 return true; // all true (or empty)
427 void And_condition::cfg_write( KConfigGroup
& cfg_P
) const
429 base::cfg_write( cfg_P
);
430 cfg_P
.writeEntry( "Type", "AND" ); // overwrites value set in base::cfg_write()
433 And_condition
* And_condition::copy( Condition_list_base
* parent_P
) const
435 And_condition
* ret
= new And_condition( parent_P
);
436 for( Iterator
it( *this );
439 ret
->append( (*it
)->copy( ret
));
443 const QString
And_condition::description() const
445 return i18nc( "And_condition", "And" );
450 Or_condition::Or_condition( KConfigGroup
& cfg_P
, Condition_list_base
* parent_P
)
451 : Condition_list_base( cfg_P
, parent_P
)
453 // CHECKME kontrola poctu ?
456 bool Or_condition::match() const
458 if( count() == 0 ) // empty => ok
460 for( Iterator
it( *this );
463 if( it
.current()->match()) // OR
468 void Or_condition::cfg_write( KConfigGroup
& cfg_P
) const
470 base::cfg_write( cfg_P
);
471 cfg_P
.writeEntry( "Type", "OR" ); // overwrites value set in base::cfg_write()
474 Or_condition
* Or_condition::copy( Condition_list_base
* parent_P
) const
476 Or_condition
* ret
= new Or_condition( parent_P
);
477 for( Iterator
it( *this );
480 ret
->append( (*it
)->copy( ret
));
484 const QString
Or_condition::description() const
486 return i18nc( "Or_condition", "Or" );
489 } // namespace KHotKeys
491 #include "conditions.moc"