Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / khotkeys / shared / conditions.cpp
blob43e51c018a80a6654002a341d3e9958ee3f20d2e
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 _CONDITIONS_CPP_
13 #include "conditions.h"
15 #ifdef KHOTKEYS_DEBUG
16 #include <typeinfo>
17 #endif
19 #include <kconfig.h>
20 #include <kconfiggroup.h>
21 #include <kdebug.h>
22 #include <klocale.h>
23 #include <assert.h>
25 #include <X11/Xlib.h>
26 #include <fixx11h.h>
28 #include "action_data.h"
29 //Added by qt3to4:
30 #include <Q3PtrList>
32 namespace KHotKeys
35 // Condition
37 Condition::Condition( Condition_list_base* parent_P )
38 : _parent( parent_P )
40 if( _parent )
41 _parent->append( this );
44 Condition::Condition( KConfigGroup&, Condition_list_base* parent_P )
45 : _parent( parent_P )
47 if( _parent )
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 );
58 if( type == "NOT" )
59 return new Not_condition( cfg_P, parent_P );
60 if( type == "AND" )
61 return new And_condition( cfg_P, parent_P );
62 if( type == "OR" )
63 return new Or_condition( cfg_P, parent_P );
64 kWarning( 1217 ) << "Unknown Condition type read from cfg file\n";
65 return NULL;
68 Condition::~Condition()
70 if( _parent )
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())
83 return;
84 assert( _parent != NULL );
85 _parent->updated();
88 #ifdef KHOTKEYS_DEBUG
89 void Condition::debug( int depth_P )
91 char tmp[ 1024 ];
92 int i;
93 for( i = 0;
94 i < depth_P;
95 ++i )
96 tmp[ i ] = ' ';
97 tmp[ i ] = '\0';
98 kDebug( 1217 ) << tmp << description() << ":(" << this << ")";
101 void Condition::debug_list( const Q3PtrList< Condition >& list_P, int depth_P )
103 char tmp[ 1024 ];
104 int i;
105 for( i = 0;
106 i < depth_P;
107 ++i )
108 tmp[ i ] = ' ';
109 tmp[ i ] = '\0';
110 for( Q3PtrListIterator< Condition > it( list_P );
112 ++it )
113 (*it)->debug( depth_P + 1 );
115 #endif
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 );
124 for( int i = 0;
125 i < cnt;
126 ++i )
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()
135 while( !isEmpty())
137 Condition* c = getFirst();
138 remove( c );
139 delete c;
143 void Condition_list_base::cfg_write( KConfigGroup& cfg_P ) const
145 int i = 0;
146 for( Iterator it( *this );
148 ++it, ++i )
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
158 return true;
161 #ifdef KHOTKEYS_DEBUG
162 void Condition_list_base::debug( int depth_P )
164 char tmp[ 1024 ];
165 int i;
166 for( i = 0;
167 i < depth_P;
168 ++i )
169 tmp[ i ] = ' ';
170 tmp[ i ] = '\0';
171 kDebug( 1217 ) << tmp << typeid( *this ).name() << ":(" << this << ")";
172 debug_list( *this, depth_P + 1 );
174 #endif
176 // Condition_list
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 );
195 ++it )
196 ret->append( it.current()->copy( ret ));
197 return ret;
201 bool Condition_list::match() const
203 if( count() == 0 ) // no conditions to match => ok
204 return true;
205 for( Iterator it( *this );
207 ++it )
208 if( it.current()->match()) // OR
209 return true;
210 return false;
213 void Condition_list::updated() const
215 if( !khotkeys_active())
216 return;
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 );
225 data = data_P;
228 const QString Condition_list::description() const
230 assert( false );
231 return QString();
234 Condition_list* Condition_list::copy( Condition_list_base* ) const
236 assert( false );
237 return NULL;
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 );
247 init();
248 set_match();
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
259 return is_match;
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;
266 updated();
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
279 #else
280 Condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
281 #endif
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 )
293 set_match();
296 Active_window_condition::~Active_window_condition()
298 disconnect( windows_handler, NULL, this, NULL );
299 delete _window;
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 );
309 init();
310 set_match();
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
321 return is_match;
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 ));
328 else
329 is_match = windows_handler->find_window( window()) != None;
330 kDebug( 1217 ) << "Existing_window_condition::set_match :" << is_match;
331 updated();
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
344 #else
345 Condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
346 #endif
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 )
358 set_match( w_P );
361 void Existing_window_condition::window_removed( WId )
363 set_match();
366 Existing_window_condition::~Existing_window_condition()
368 disconnect( windows_handler, NULL, this, NULL );
369 delete _window;
372 // Not_condition
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 );
394 if( condition())
395 ret->append( condition()->copy( ret ));
396 return ret;
399 const QString Not_condition::description() const
401 return i18nc( "Not_condition", "Not" );
404 bool Not_condition::accepts_children() const
406 return count() == 0;
409 // And_condition
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 );
421 ++it )
422 if( !it.current()->match()) // AND
423 return false;
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 );
438 ++it )
439 ret->append( (*it)->copy( ret ));
440 return ret;
443 const QString And_condition::description() const
445 return i18nc( "And_condition", "And" );
448 // Or_condition
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
459 return true;
460 for( Iterator it( *this );
462 ++it )
463 if( it.current()->match()) // OR
464 return true;
465 return false;
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 );
479 ++it )
480 ret->append( (*it)->copy( ret ));
481 return ret;
484 const QString Or_condition::description() const
486 return i18nc( "Or_condition", "Or" );
489 } // namespace KHotKeys
491 #include "conditions.moc"