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_
17
#include "conditions.h"
31
#include "action_data.h"
38
Condition::Condition( Condition_list_base* parent_P )
42
_parent->append( this );
45
Condition::Condition( KConfig&, Condition_list_base* parent_P )
49
_parent->append( this );
52
Condition* Condition::create_cfg_read( KConfig& cfg_P, Condition_list_base* parent_P )
54
QString type = cfg_P.readEntry( "Type" );
55
if( type == "ACTIVE_WINDOW" )
56
return new Active_window_condition( cfg_P, parent_P );
57
if( type == "EXISTING_WINDOW" )
58
return new Existing_window_condition( cfg_P, parent_P );
60
return new Not_condition( cfg_P, parent_P );
62
return new And_condition( cfg_P, parent_P );
64
return new Or_condition( cfg_P, parent_P );
65
kdWarning( 1217 ) << "Unknown Condition type read from cfg file\n";
69
Condition::~Condition()
72
_parent->remove( this );
76
void Condition::cfg_write( KConfig& cfg_P ) const
78
cfg_P.writeEntry( "Type", "ERROR" );
81
void Condition::updated() const
83
if( !khotkeys_active())
85
assert( _parent != NULL );
90
void Condition::debug( int depth_P )
99
kdDebug( 1217 ) << tmp << description() << ":(" << this << ")" << endl;
102
void Condition::debug_list( const QPtrList< Condition >& list_P, int depth_P )
111
for( QPtrListIterator< Condition > it( list_P );
114
(*it)->debug( depth_P + 1 );
119
// Condition_list_base
121
Condition_list_base::Condition_list_base( KConfig& cfg_P, Condition_list_base* parent_P )
122
: Condition( parent_P )
124
QString save_cfg_group = cfg_P.group();
125
int cnt = cfg_P.readNumEntry( "ConditionsCount", 0 );
130
cfg_P.setGroup( save_cfg_group + QString::number( i ));
131
(void) Condition::create_cfg_read( cfg_P, this );
133
cfg_P.setGroup( save_cfg_group );
136
Condition_list_base::~Condition_list_base()
140
Condition* c = getFirst();
146
void Condition_list_base::cfg_write( KConfig& cfg_P ) const
148
QString save_cfg_group = cfg_P.group();
150
for( Iterator it( *this );
154
cfg_P.setGroup( save_cfg_group + QString::number( i ));
155
it.current()->cfg_write( cfg_P );
157
cfg_P.setGroup( save_cfg_group );
158
cfg_P.writeEntry( "ConditionsCount", i );
161
bool Condition_list_base::accepts_children() const
166
#ifdef KHOTKEYS_DEBUG
167
void Condition_list_base::debug( int depth_P )
176
kdDebug( 1217 ) << tmp << typeid( *this ).name() << ":(" << this << ")" << endl;
177
debug_list( *this, depth_P + 1 );
183
Condition_list::Condition_list( KConfig& cfg_P, Action_data_base* data_P )
184
: Condition_list_base( cfg_P, NULL ), data( data_P )
186
_comment = cfg_P.readEntry( "Comment" );
189
void Condition_list::cfg_write( KConfig& cfg_P ) const
191
base::cfg_write( cfg_P );
192
cfg_P.writeEntry( "Comment", comment());
195
Condition_list* Condition_list::copy( Action_data_base* data_P ) const
197
Condition_list* ret = new Condition_list( comment(), data_P );
198
for( Iterator it( *this );
201
ret->append( it.current()->copy( ret ));
206
bool Condition_list::match() const
208
if( count() == 0 ) // no conditions to match => ok
210
for( Iterator it( *this );
213
if( it.current()->match()) // OR
218
void Condition_list::updated() const
220
if( !khotkeys_active())
222
data->update_triggers();
223
// base::updated(); no need to, doesn't have parent
226
// CHECKME tohle je drobet hack, jeste to zvazit
227
void Condition_list::set_data( Action_data_base* data_P )
229
assert( data == NULL || data == data_P );
233
const QString Condition_list::description() const
236
return QString::null;
239
Condition_list* Condition_list::copy( Condition_list_base* ) const
245
// Active_window_condition
247
Active_window_condition::Active_window_condition( KConfig& cfg_P, Condition_list_base* parent_P )
248
: Condition( cfg_P, parent_P )
250
QString save_cfg_group = cfg_P.group();
251
cfg_P.setGroup( save_cfg_group + "Window" );
252
_window = new Windowdef_list( cfg_P );
253
cfg_P.setGroup( save_cfg_group );
258
void Active_window_condition::init()
260
connect( windows_handler, SIGNAL( active_window_changed( WId )),
261
this, SLOT( active_window_changed( WId )));
264
bool Active_window_condition::match() const
269
void Active_window_condition::set_match()
271
is_match = window()->match( Window_data( windows_handler->active_window()));
272
kdDebug( 1217 ) << "Active_window_condition::set_match :" << is_match << endl;
276
void Active_window_condition::cfg_write( KConfig& cfg_P ) const
278
base::cfg_write( cfg_P );
279
QString save_cfg_group = cfg_P.group();
280
cfg_P.setGroup( save_cfg_group + "Window" );
281
window()->cfg_write( cfg_P );
282
cfg_P.setGroup( save_cfg_group );
283
cfg_P.writeEntry( "Type", "ACTIVE_WINDOW" ); // overwrites value set in base::cfg_write()
286
#ifndef COVARIANT_RETURN_BROKEN
287
Active_window_condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
289
Condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
292
return new Active_window_condition( window()->copy(), parent_P );
295
const QString Active_window_condition::description() const
297
return i18n( "Active window: " ) + window()->comment();
300
void Active_window_condition::active_window_changed( WId )
305
Active_window_condition::~Active_window_condition()
307
disconnect( windows_handler, NULL, this, NULL );
311
// Existing_window_condition
313
Existing_window_condition::Existing_window_condition( KConfig& cfg_P, Condition_list_base* parent_P )
314
: Condition( cfg_P, parent_P )
316
QString save_cfg_group = cfg_P.group();
317
cfg_P.setGroup( save_cfg_group + "Window" );
318
_window = new Windowdef_list( cfg_P );
319
cfg_P.setGroup( save_cfg_group );
324
void Existing_window_condition::init()
326
connect( windows_handler, SIGNAL( window_added( WId )), this, SLOT( window_added( WId )));
327
connect( windows_handler, SIGNAL( window_removed( WId )), this, SLOT( window_removed( WId )));
330
bool Existing_window_condition::match() const
335
void Existing_window_condition::set_match( WId w_P )
337
if( w_P != None && !is_match )
338
is_match = window()->match( Window_data( w_P ));
340
is_match = windows_handler->find_window( window()) != None;
341
kdDebug( 1217 ) << "Existing_window_condition::set_match :" << is_match << endl;
345
void Existing_window_condition::cfg_write( KConfig& cfg_P ) const
347
base::cfg_write( cfg_P );
348
QString save_cfg_group = cfg_P.group();
349
cfg_P.setGroup( save_cfg_group + "Window" );
350
window()->cfg_write( cfg_P );
351
cfg_P.setGroup( save_cfg_group );
352
cfg_P.writeEntry( "Type", "EXISTING_WINDOW" ); // overwrites value set in base::cfg_write()
355
#ifndef COVARIANT_RETURN_BROKEN
356
Existing_window_condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
358
Condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
361
return new Existing_window_condition( window()->copy(), parent_P );
364
const QString Existing_window_condition::description() const
366
return i18n( "Existing window: " ) + window()->comment();
369
void Existing_window_condition::window_added( WId w_P )
374
void Existing_window_condition::window_removed( WId )
379
Existing_window_condition::~Existing_window_condition()
381
disconnect( windows_handler, NULL, this, NULL );
387
Not_condition::Not_condition( KConfig& cfg_P, Condition_list_base* parent_P )
388
: Condition_list_base( cfg_P, parent_P )
390
// CHECKME kontrola poctu ?
393
bool Not_condition::match() const
395
return condition() ? !condition()->match() : false;
398
void Not_condition::cfg_write( KConfig& cfg_P ) const
400
base::cfg_write( cfg_P );
401
cfg_P.writeEntry( "Type", "NOT" ); // overwrites value set in base::cfg_write()
404
Not_condition* Not_condition::copy( Condition_list_base* parent_P ) const
406
Not_condition* ret = new Not_condition( parent_P );
408
ret->append( condition()->copy( ret ));
412
const QString Not_condition::description() const
414
return i18n( "Not_condition", "Not" );
417
bool Not_condition::accepts_children() const
424
And_condition::And_condition( KConfig& cfg_P, Condition_list_base* parent_P )
425
: Condition_list_base( cfg_P, parent_P )
427
// CHECKME kontrola poctu ?
430
bool And_condition::match() const
432
for( Iterator it( *this );
435
if( !it.current()->match()) // AND
437
return true; // all true (or empty)
440
void And_condition::cfg_write( KConfig& cfg_P ) const
442
base::cfg_write( cfg_P );
443
cfg_P.writeEntry( "Type", "AND" ); // overwrites value set in base::cfg_write()
446
And_condition* And_condition::copy( Condition_list_base* parent_P ) const
448
And_condition* ret = new And_condition( parent_P );
449
for( Iterator it( *this );
452
ret->append( (*it)->copy( ret ));
456
const QString And_condition::description() const
458
return i18n( "And_condition", "And" );
463
Or_condition::Or_condition( KConfig& cfg_P, Condition_list_base* parent_P )
464
: Condition_list_base( cfg_P, parent_P )
466
// CHECKME kontrola poctu ?
469
bool Or_condition::match() const
471
if( count() == 0 ) // empty => ok
473
for( Iterator it( *this );
476
if( it.current()->match()) // OR
481
void Or_condition::cfg_write( KConfig& cfg_P ) const
483
base::cfg_write( cfg_P );
484
cfg_P.writeEntry( "Type", "OR" ); // overwrites value set in base::cfg_write()
487
Or_condition* Or_condition::copy( Condition_list_base* parent_P ) const
489
Or_condition* ret = new Or_condition( parent_P );
490
for( Iterator it( *this );
493
ret->append( (*it)->copy( ret ));
497
const QString Or_condition::description() const
499
return i18n( "Or_condition", "Or" );
502
} // namespace KHotKeys
504
#include "conditions.moc"