summaryrefslogtreecommitdiffstats
path: root/khotkeys/shared/conditions.cpp
blob: 6120ca875077ec3f27d01c1d015c6b37e94bd5eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/****************************************************************************

 KHotKeys

 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>

 Distributed under the terms of the GNU General Public License version 2.

****************************************************************************/

#define _CONDITIONS_CPP_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "conditions.h"

#ifdef KHOTKEYS_DEBUG
#include <typeinfo>
#endif

#include <tdeconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <assert.h>

#include <X11/Xlib.h>
#include <fixx11h.h>

#include "action_data.h"

namespace KHotKeys
{

// Condition

Condition::Condition( Condition_list_base* parent_P )
    : _parent( parent_P )
    {
    if( _parent )
        _parent->append( this );
    }

Condition::Condition( TDEConfig&, Condition_list_base* parent_P )
    : _parent( parent_P )
    {
    if( _parent )
        _parent->append( this );
    }

Condition* Condition::create_cfg_read( TDEConfig& cfg_P, Condition_list_base* parent_P )
    {
    TQString type = cfg_P.readEntry( "Type" );
    if( type == "ACTIVE_WINDOW" )
        return new Active_window_condition( cfg_P, parent_P );
    if( type == "EXISTING_WINDOW" )
        return new Existing_window_condition( cfg_P, parent_P );
    if( type == "NOT" )
        return new Not_condition( cfg_P, parent_P );
    if( type == "AND" )
        return new And_condition( cfg_P, parent_P );
    if( type == "OR" )
        return new Or_condition( cfg_P, parent_P );
    kdWarning( 1217 ) << "Unknown Condition type read from cfg file\n";
    return NULL;
    }

Condition::~Condition()
    {
    if( _parent )
        _parent->remove( this );
    }


void Condition::cfg_write( TDEConfig& cfg_P ) const
    {
    cfg_P.writeEntry( "Type", "ERROR" );
    }

void Condition::updated() const
    {
    if( !khotkeys_active())
        return;
    assert( _parent != NULL );
    _parent->updated();
    }

#ifdef KHOTKEYS_DEBUG
void Condition::debug( int depth_P )
    {
    char tmp[ 1024 ];
    int i;
    for( i = 0;
         i < depth_P;
         ++i )
        tmp[ i ] = ' ';
    tmp[ i ] = '\0';
    kdDebug( 1217 ) << tmp << description() << ":(" << this << ")" << endl;
    }

void Condition::debug_list( const TQPtrList< Condition >& list_P, int depth_P )
    {
    char tmp[ 1024 ];
    int i;
    for( i = 0;
         i < depth_P;
         ++i )
        tmp[ i ] = ' ';
    tmp[ i ] = '\0';
    for( TQPtrListIterator< Condition > it( list_P );
         it;
         ++it )
        (*it)->debug( depth_P + 1 );
    }
#endif


// Condition_list_base

Condition_list_base::Condition_list_base( TDEConfig& cfg_P, Condition_list_base* parent_P )
    : Condition( parent_P )
    {
    TQString save_cfg_group = cfg_P.group();
    int cnt = cfg_P.readNumEntry( "ConditionsCount", 0 );
    for( int i = 0;
         i < cnt;
         ++i )
        {
        cfg_P.setGroup( save_cfg_group + TQString::number( i ));
        (void) Condition::create_cfg_read( cfg_P, this );
        }
    cfg_P.setGroup( save_cfg_group );
    }

Condition_list_base::~Condition_list_base()
    {
    while( !isEmpty())
        {
        Condition* c = getFirst();
        remove( c );
        delete c;
        }
    }
    
void Condition_list_base::cfg_write( TDEConfig& cfg_P ) const
    {
    TQString save_cfg_group = cfg_P.group();
    int i = 0;
    for( Iterator it( *this );
         it;
         ++it, ++i )
        {
        cfg_P.setGroup( save_cfg_group + TQString::number( i ));
        it.current()->cfg_write( cfg_P );
        }
    cfg_P.setGroup( save_cfg_group );
    cfg_P.writeEntry( "ConditionsCount", i );
    }

bool Condition_list_base::accepts_children() const
    {
    return true;
    }

#ifdef KHOTKEYS_DEBUG
void Condition_list_base::debug( int depth_P )
    {
    char tmp[ 1024 ];
    int i;
    for( i = 0;
         i < depth_P;
         ++i )
        tmp[ i ] = ' ';
    tmp[ i ] = '\0';
    kdDebug( 1217 ) << tmp << typeid( *this ).name() << ":(" << this << ")" << endl;
    debug_list( *this, depth_P + 1 );
    }
#endif

// Condition_list

Condition_list::Condition_list( TDEConfig& cfg_P, Action_data_base* data_P )
    : Condition_list_base( cfg_P, NULL ), data( data_P )
    {
    _comment = cfg_P.readEntry( "Comment" );
    }

void Condition_list::cfg_write( TDEConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Comment", comment());
    }

Condition_list* Condition_list::copy( Action_data_base* data_P ) const
    {
    Condition_list* ret = new Condition_list( comment(), data_P );
    for( Iterator it( *this );
         it;
         ++it )
        ret->append( it.current()->copy( ret ));
    return ret;
    }


bool Condition_list::match() const
    {
    if( count() == 0 ) // no conditions to match => ok
        return true;
    for( Iterator it( *this );
         it;
         ++it )
        if( it.current()->match()) // OR
            return true;
    return false;
    }

void Condition_list::updated() const
    {
    if( !khotkeys_active())
        return;
    data->update_triggers();
//    base::updated(); no need to, doesn't have parent
    }

// CHECKME tohle je drobet hack, jeste to zvazit
void Condition_list::set_data( Action_data_base* data_P )
    {
    assert( data == NULL || data == data_P );
    data = data_P;
    }

const TQString Condition_list::description() const
    {
    assert( false );
    return TQString::null;
    }

Condition_list* Condition_list::copy( Condition_list_base* ) const
    {
    assert( false );
    return NULL;
    }

// Active_window_condition

Active_window_condition::Active_window_condition( TDEConfig& cfg_P, Condition_list_base* parent_P )
    : Condition( cfg_P, parent_P )
    {
    TQString save_cfg_group = cfg_P.group();
    cfg_P.setGroup( save_cfg_group + "Window" );
    _window = new Windowdef_list( cfg_P );
    cfg_P.setGroup( save_cfg_group );
    init();
    set_match();
    }

void Active_window_condition::init()
    {
    connect( windows_handler, TQT_SIGNAL( active_window_changed( WId )),
        this, TQT_SLOT( active_window_changed( WId )));
    }

bool Active_window_condition::match() const
    {
    return is_match;
    }

void Active_window_condition::set_match()
    {
    is_match = window()->match( Window_data( windows_handler->active_window()));
    kdDebug( 1217 ) << "Active_window_condition::set_match :" << is_match << endl;
    updated();
    }

void Active_window_condition::cfg_write( TDEConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    TQString save_cfg_group = cfg_P.group();
    cfg_P.setGroup( save_cfg_group + "Window" );
    window()->cfg_write( cfg_P );
    cfg_P.setGroup( save_cfg_group );
    cfg_P.writeEntry( "Type", "ACTIVE_WINDOW" ); // overwrites value set in base::cfg_write()
    }

#ifndef COVARIANT_RETURN_BROKEN
Active_window_condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
#else
Condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
#endif
    {
    return new Active_window_condition( window()->copy(), parent_P );
    }

const TQString Active_window_condition::description() const
    {
    return i18n( "Active window: " ) + window()->comment();
    }

void Active_window_condition::active_window_changed( WId )
    {
    set_match();
    }

Active_window_condition::~Active_window_condition()
    {
    disconnect( windows_handler, NULL, this, NULL );
    delete _window;
    }

// Existing_window_condition

Existing_window_condition::Existing_window_condition( TDEConfig& cfg_P, Condition_list_base* parent_P )
    : Condition( cfg_P, parent_P )
    {
    TQString save_cfg_group = cfg_P.group();
    cfg_P.setGroup( save_cfg_group + "Window" );
    _window = new Windowdef_list( cfg_P );
    cfg_P.setGroup( save_cfg_group );
    init();
    set_match();
    }

void Existing_window_condition::init()
    {
    connect( windows_handler, TQT_SIGNAL( window_added( WId )), this, TQT_SLOT( window_added( WId )));
    connect( windows_handler, TQT_SIGNAL( window_removed( WId )), this, TQT_SLOT( window_removed( WId )));
    }

bool Existing_window_condition::match() const
    {
    return is_match;
    }

void Existing_window_condition::set_match( WId w_P )
    {
    if( w_P != None && !is_match )
        is_match = window()->match( Window_data( w_P ));
    else
        is_match = windows_handler->find_window( window()) != None;
    kdDebug( 1217 ) << "Existing_window_condition::set_match :" << is_match << endl;
    updated();
    }

void Existing_window_condition::cfg_write( TDEConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    TQString save_cfg_group = cfg_P.group();
    cfg_P.setGroup( save_cfg_group + "Window" );
    window()->cfg_write( cfg_P );
    cfg_P.setGroup( save_cfg_group );
    cfg_P.writeEntry( "Type", "EXISTING_WINDOW" ); // overwrites value set in base::cfg_write()
    }

#ifndef COVARIANT_RETURN_BROKEN
Existing_window_condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
#else
Condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
#endif
    {
    return new Existing_window_condition( window()->copy(), parent_P );
    }

const TQString Existing_window_condition::description() const
    {
    return i18n( "Existing window: " ) + window()->comment();
    }

void Existing_window_condition::window_added( WId w_P )
    {
    set_match( w_P );
    }

void Existing_window_condition::window_removed( WId )
    {
    set_match();
    }

Existing_window_condition::~Existing_window_condition()
    {
    disconnect( windows_handler, NULL, this, NULL );
    delete _window;
    }

// Not_condition

Not_condition::Not_condition( TDEConfig& cfg_P, Condition_list_base* parent_P )
    : Condition_list_base( cfg_P, parent_P )
    {
    // CHECKME kontrola poctu ?
    }

bool Not_condition::match() const
    {
    return condition() ? !condition()->match() : false;
    }

void Not_condition::cfg_write( TDEConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Type", "NOT" ); // overwrites value set in base::cfg_write()
    }

Not_condition* Not_condition::copy( Condition_list_base* parent_P ) const
    {
    Not_condition* ret = new Not_condition( parent_P );
    if( condition())
        ret->append( condition()->copy( ret ));
    return ret;
    }

const TQString Not_condition::description() const
    {
    return i18n( "Not_condition", "Not" );
    }

bool Not_condition::accepts_children() const
    {
    return count() == 0;
    }

// And_condition

And_condition::And_condition( TDEConfig& cfg_P, Condition_list_base* parent_P )
    : Condition_list_base( cfg_P, parent_P )
    {
    // CHECKME kontrola poctu ?
    }

bool And_condition::match() const
    {
    for( Iterator it( *this );
         it;
         ++it )
        if( !it.current()->match()) // AND
            return false;
    return true; // all true (or empty)
    }

void And_condition::cfg_write( TDEConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Type", "AND" ); // overwrites value set in base::cfg_write()
    }

And_condition* And_condition::copy( Condition_list_base* parent_P ) const
    {
    And_condition* ret = new And_condition( parent_P );
    for( Iterator it( *this );
         it;
         ++it )
        ret->append( (*it)->copy( ret ));
    return ret;
    }

const TQString And_condition::description() const
    {
    return i18n( "And_condition", "And" );
    }

// Or_condition

Or_condition::Or_condition( TDEConfig& cfg_P, Condition_list_base* parent_P )
    : Condition_list_base( cfg_P, parent_P )
    {
    // CHECKME kontrola poctu ?
    }

bool Or_condition::match() const
    {
    if( count() == 0 ) // empty => ok
        return true;
    for( Iterator it( *this );
         it;
         ++it )
        if( it.current()->match()) // OR
            return true;
    return false;
    }

void Or_condition::cfg_write( TDEConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Type", "OR" ); // overwrites value set in base::cfg_write()
    }

Or_condition* Or_condition::copy( Condition_list_base* parent_P ) const
    {
    Or_condition* ret = new Or_condition( parent_P );
    for( Iterator it( *this );
         it;
         ++it )
        ret->append( (*it)->copy( ret ));
    return ret;
    }

const TQString Or_condition::description() const
    {
    return i18n( "Or_condition", "Or" );
    }

} // namespace KHotKeys

#include "conditions.moc"