~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to khotkeys/shared/conditions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 
3
 
 KHotKeys
4
 
 
5
 
 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
6
 
 
7
 
 Distributed under the terms of the GNU General Public License version 2.
8
 
 
9
 
****************************************************************************/
10
 
 
11
 
#define _CONDITIONS_CPP_
12
 
 
13
 
#ifdef HAVE_CONFIG_H
14
 
#include <config.h>
15
 
#endif
16
 
 
17
 
#include "conditions.h"
18
 
 
19
 
#ifdef KHOTKEYS_DEBUG
20
 
#include <typeinfo>
21
 
#endif
22
 
 
23
 
#include <kconfig.h>
24
 
#include <kdebug.h>
25
 
#include <klocale.h>
26
 
#include <assert.h>
27
 
 
28
 
#include <X11/Xlib.h>
29
 
#include <fixx11h.h>
30
 
 
31
 
#include "action_data.h"
32
 
 
33
 
namespace KHotKeys
34
 
{
35
 
 
36
 
// Condition
37
 
 
38
 
Condition::Condition( Condition_list_base* parent_P )
39
 
    : _parent( parent_P )
40
 
    {
41
 
    if( _parent )
42
 
        _parent->append( this );
43
 
    }
44
 
 
45
 
Condition::Condition( KConfig&, Condition_list_base* parent_P )
46
 
    : _parent( parent_P )
47
 
    {
48
 
    if( _parent )
49
 
        _parent->append( this );
50
 
    }
51
 
 
52
 
Condition* Condition::create_cfg_read( KConfig& cfg_P, Condition_list_base* parent_P )
53
 
    {
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 );
59
 
    if( type == "NOT" )
60
 
        return new Not_condition( cfg_P, parent_P );
61
 
    if( type == "AND" )
62
 
        return new And_condition( cfg_P, parent_P );
63
 
    if( type == "OR" )
64
 
        return new Or_condition( cfg_P, parent_P );
65
 
    kdWarning( 1217 ) << "Unknown Condition type read from cfg file\n";
66
 
    return NULL;
67
 
    }
68
 
 
69
 
Condition::~Condition()
70
 
    {
71
 
    if( _parent )
72
 
        _parent->remove( this );
73
 
    }
74
 
 
75
 
 
76
 
void Condition::cfg_write( KConfig& cfg_P ) const
77
 
    {
78
 
    cfg_P.writeEntry( "Type", "ERROR" );
79
 
    }
80
 
 
81
 
void Condition::updated() const
82
 
    {
83
 
    if( !khotkeys_active())
84
 
        return;
85
 
    assert( _parent != NULL );
86
 
    _parent->updated();
87
 
    }
88
 
 
89
 
#ifdef KHOTKEYS_DEBUG
90
 
void Condition::debug( int depth_P )
91
 
    {
92
 
    char tmp[ 1024 ];
93
 
    int i;
94
 
    for( i = 0;
95
 
         i < depth_P;
96
 
         ++i )
97
 
        tmp[ i ] = ' ';
98
 
    tmp[ i ] = '\0';
99
 
    kdDebug( 1217 ) << tmp << description() << ":(" << this << ")" << endl;
100
 
    }
101
 
 
102
 
void Condition::debug_list( const QPtrList< Condition >& list_P, int depth_P )
103
 
    {
104
 
    char tmp[ 1024 ];
105
 
    int i;
106
 
    for( i = 0;
107
 
         i < depth_P;
108
 
         ++i )
109
 
        tmp[ i ] = ' ';
110
 
    tmp[ i ] = '\0';
111
 
    for( QPtrListIterator< Condition > it( list_P );
112
 
         it;
113
 
         ++it )
114
 
        (*it)->debug( depth_P + 1 );
115
 
    }
116
 
#endif
117
 
 
118
 
 
119
 
// Condition_list_base
120
 
 
121
 
Condition_list_base::Condition_list_base( KConfig& cfg_P, Condition_list_base* parent_P )
122
 
    : Condition( parent_P )
123
 
    {
124
 
    QString save_cfg_group = cfg_P.group();
125
 
    int cnt = cfg_P.readNumEntry( "ConditionsCount", 0 );
126
 
    for( int i = 0;
127
 
         i < cnt;
128
 
         ++i )
129
 
        {
130
 
        cfg_P.setGroup( save_cfg_group + QString::number( i ));
131
 
        (void) Condition::create_cfg_read( cfg_P, this );
132
 
        }
133
 
    cfg_P.setGroup( save_cfg_group );
134
 
    }
135
 
 
136
 
Condition_list_base::~Condition_list_base()
137
 
    {
138
 
    while( !isEmpty())
139
 
        {
140
 
        Condition* c = getFirst();
141
 
        remove( c );
142
 
        delete c;
143
 
        }
144
 
    }
145
 
    
146
 
void Condition_list_base::cfg_write( KConfig& cfg_P ) const
147
 
    {
148
 
    QString save_cfg_group = cfg_P.group();
149
 
    int i = 0;
150
 
    for( Iterator it( *this );
151
 
         it;
152
 
         ++it, ++i )
153
 
        {
154
 
        cfg_P.setGroup( save_cfg_group + QString::number( i ));
155
 
        it.current()->cfg_write( cfg_P );
156
 
        }
157
 
    cfg_P.setGroup( save_cfg_group );
158
 
    cfg_P.writeEntry( "ConditionsCount", i );
159
 
    }
160
 
 
161
 
bool Condition_list_base::accepts_children() const
162
 
    {
163
 
    return true;
164
 
    }
165
 
 
166
 
#ifdef KHOTKEYS_DEBUG
167
 
void Condition_list_base::debug( int depth_P )
168
 
    {
169
 
    char tmp[ 1024 ];
170
 
    int i;
171
 
    for( i = 0;
172
 
         i < depth_P;
173
 
         ++i )
174
 
        tmp[ i ] = ' ';
175
 
    tmp[ i ] = '\0';
176
 
    kdDebug( 1217 ) << tmp << typeid( *this ).name() << ":(" << this << ")" << endl;
177
 
    debug_list( *this, depth_P + 1 );
178
 
    }
179
 
#endif
180
 
 
181
 
// Condition_list
182
 
 
183
 
Condition_list::Condition_list( KConfig& cfg_P, Action_data_base* data_P )
184
 
    : Condition_list_base( cfg_P, NULL ), data( data_P )
185
 
    {
186
 
    _comment = cfg_P.readEntry( "Comment" );
187
 
    }
188
 
 
189
 
void Condition_list::cfg_write( KConfig& cfg_P ) const
190
 
    {
191
 
    base::cfg_write( cfg_P );
192
 
    cfg_P.writeEntry( "Comment", comment());
193
 
    }
194
 
 
195
 
Condition_list* Condition_list::copy( Action_data_base* data_P ) const
196
 
    {
197
 
    Condition_list* ret = new Condition_list( comment(), data_P );
198
 
    for( Iterator it( *this );
199
 
         it;
200
 
         ++it )
201
 
        ret->append( it.current()->copy( ret ));
202
 
    return ret;
203
 
    }
204
 
 
205
 
 
206
 
bool Condition_list::match() const
207
 
    {
208
 
    if( count() == 0 ) // no conditions to match => ok
209
 
        return true;
210
 
    for( Iterator it( *this );
211
 
         it;
212
 
         ++it )
213
 
        if( it.current()->match()) // OR
214
 
            return true;
215
 
    return false;
216
 
    }
217
 
 
218
 
void Condition_list::updated() const
219
 
    {
220
 
    if( !khotkeys_active())
221
 
        return;
222
 
    data->update_triggers();
223
 
//    base::updated(); no need to, doesn't have parent
224
 
    }
225
 
 
226
 
// CHECKME tohle je drobet hack, jeste to zvazit
227
 
void Condition_list::set_data( Action_data_base* data_P )
228
 
    {
229
 
    assert( data == NULL || data == data_P );
230
 
    data = data_P;
231
 
    }
232
 
 
233
 
const QString Condition_list::description() const
234
 
    {
235
 
    assert( false );
236
 
    return QString::null;
237
 
    }
238
 
 
239
 
Condition_list* Condition_list::copy( Condition_list_base* ) const
240
 
    {
241
 
    assert( false );
242
 
    return NULL;
243
 
    }
244
 
 
245
 
// Active_window_condition
246
 
 
247
 
Active_window_condition::Active_window_condition( KConfig& cfg_P, Condition_list_base* parent_P )
248
 
    : Condition( cfg_P, parent_P )
249
 
    {
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 );
254
 
    init();
255
 
    set_match();
256
 
    }
257
 
 
258
 
void Active_window_condition::init()
259
 
    {
260
 
    connect( windows_handler, SIGNAL( active_window_changed( WId )),
261
 
        this, SLOT( active_window_changed( WId )));
262
 
    }
263
 
 
264
 
bool Active_window_condition::match() const
265
 
    {
266
 
    return is_match;
267
 
    }
268
 
 
269
 
void Active_window_condition::set_match()
270
 
    {
271
 
    is_match = window()->match( Window_data( windows_handler->active_window()));
272
 
    kdDebug( 1217 ) << "Active_window_condition::set_match :" << is_match << endl;
273
 
    updated();
274
 
    }
275
 
 
276
 
void Active_window_condition::cfg_write( KConfig& cfg_P ) const
277
 
    {
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()
284
 
    }
285
 
 
286
 
#ifndef COVARIANT_RETURN_BROKEN
287
 
Active_window_condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
288
 
#else
289
 
Condition* Active_window_condition::copy( Condition_list_base* parent_P ) const
290
 
#endif
291
 
    {
292
 
    return new Active_window_condition( window()->copy(), parent_P );
293
 
    }
294
 
 
295
 
const QString Active_window_condition::description() const
296
 
    {
297
 
    return i18n( "Active window: " ) + window()->comment();
298
 
    }
299
 
 
300
 
void Active_window_condition::active_window_changed( WId )
301
 
    {
302
 
    set_match();
303
 
    }
304
 
 
305
 
Active_window_condition::~Active_window_condition()
306
 
    {
307
 
    disconnect( windows_handler, NULL, this, NULL );
308
 
    delete _window;
309
 
    }
310
 
 
311
 
// Existing_window_condition
312
 
 
313
 
Existing_window_condition::Existing_window_condition( KConfig& cfg_P, Condition_list_base* parent_P )
314
 
    : Condition( cfg_P, parent_P )
315
 
    {
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 );
320
 
    init();
321
 
    set_match();
322
 
    }
323
 
 
324
 
void Existing_window_condition::init()
325
 
    {
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 )));
328
 
    }
329
 
 
330
 
bool Existing_window_condition::match() const
331
 
    {
332
 
    return is_match;
333
 
    }
334
 
 
335
 
void Existing_window_condition::set_match( WId w_P )
336
 
    {
337
 
    if( w_P != None && !is_match )
338
 
        is_match = window()->match( Window_data( w_P ));
339
 
    else
340
 
        is_match = windows_handler->find_window( window()) != None;
341
 
    kdDebug( 1217 ) << "Existing_window_condition::set_match :" << is_match << endl;
342
 
    updated();
343
 
    }
344
 
 
345
 
void Existing_window_condition::cfg_write( KConfig& cfg_P ) const
346
 
    {
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()
353
 
    }
354
 
 
355
 
#ifndef COVARIANT_RETURN_BROKEN
356
 
Existing_window_condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
357
 
#else
358
 
Condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const
359
 
#endif
360
 
    {
361
 
    return new Existing_window_condition( window()->copy(), parent_P );
362
 
    }
363
 
 
364
 
const QString Existing_window_condition::description() const
365
 
    {
366
 
    return i18n( "Existing window: " ) + window()->comment();
367
 
    }
368
 
 
369
 
void Existing_window_condition::window_added( WId w_P )
370
 
    {
371
 
    set_match( w_P );
372
 
    }
373
 
 
374
 
void Existing_window_condition::window_removed( WId )
375
 
    {
376
 
    set_match();
377
 
    }
378
 
 
379
 
Existing_window_condition::~Existing_window_condition()
380
 
    {
381
 
    disconnect( windows_handler, NULL, this, NULL );
382
 
    delete _window;
383
 
    }
384
 
 
385
 
// Not_condition
386
 
 
387
 
Not_condition::Not_condition( KConfig& cfg_P, Condition_list_base* parent_P )
388
 
    : Condition_list_base( cfg_P, parent_P )
389
 
    {
390
 
    // CHECKME kontrola poctu ?
391
 
    }
392
 
 
393
 
bool Not_condition::match() const
394
 
    {
395
 
    return condition() ? !condition()->match() : false;
396
 
    }
397
 
 
398
 
void Not_condition::cfg_write( KConfig& cfg_P ) const
399
 
    {
400
 
    base::cfg_write( cfg_P );
401
 
    cfg_P.writeEntry( "Type", "NOT" ); // overwrites value set in base::cfg_write()
402
 
    }
403
 
 
404
 
Not_condition* Not_condition::copy( Condition_list_base* parent_P ) const
405
 
    {
406
 
    Not_condition* ret = new Not_condition( parent_P );
407
 
    if( condition())
408
 
        ret->append( condition()->copy( ret ));
409
 
    return ret;
410
 
    }
411
 
 
412
 
const QString Not_condition::description() const
413
 
    {
414
 
    return i18n( "Not_condition", "Not" );
415
 
    }
416
 
 
417
 
bool Not_condition::accepts_children() const
418
 
    {
419
 
    return count() == 0;
420
 
    }
421
 
 
422
 
// And_condition
423
 
 
424
 
And_condition::And_condition( KConfig& cfg_P, Condition_list_base* parent_P )
425
 
    : Condition_list_base( cfg_P, parent_P )
426
 
    {
427
 
    // CHECKME kontrola poctu ?
428
 
    }
429
 
 
430
 
bool And_condition::match() const
431
 
    {
432
 
    for( Iterator it( *this );
433
 
         it;
434
 
         ++it )
435
 
        if( !it.current()->match()) // AND
436
 
            return false;
437
 
    return true; // all true (or empty)
438
 
    }
439
 
 
440
 
void And_condition::cfg_write( KConfig& cfg_P ) const
441
 
    {
442
 
    base::cfg_write( cfg_P );
443
 
    cfg_P.writeEntry( "Type", "AND" ); // overwrites value set in base::cfg_write()
444
 
    }
445
 
 
446
 
And_condition* And_condition::copy( Condition_list_base* parent_P ) const
447
 
    {
448
 
    And_condition* ret = new And_condition( parent_P );
449
 
    for( Iterator it( *this );
450
 
         it;
451
 
         ++it )
452
 
        ret->append( (*it)->copy( ret ));
453
 
    return ret;
454
 
    }
455
 
 
456
 
const QString And_condition::description() const
457
 
    {
458
 
    return i18n( "And_condition", "And" );
459
 
    }
460
 
 
461
 
// Or_condition
462
 
 
463
 
Or_condition::Or_condition( KConfig& cfg_P, Condition_list_base* parent_P )
464
 
    : Condition_list_base( cfg_P, parent_P )
465
 
    {
466
 
    // CHECKME kontrola poctu ?
467
 
    }
468
 
 
469
 
bool Or_condition::match() const
470
 
    {
471
 
    if( count() == 0 ) // empty => ok
472
 
        return true;
473
 
    for( Iterator it( *this );
474
 
         it;
475
 
         ++it )
476
 
        if( it.current()->match()) // OR
477
 
            return true;
478
 
    return false;
479
 
    }
480
 
 
481
 
void Or_condition::cfg_write( KConfig& cfg_P ) const
482
 
    {
483
 
    base::cfg_write( cfg_P );
484
 
    cfg_P.writeEntry( "Type", "OR" ); // overwrites value set in base::cfg_write()
485
 
    }
486
 
 
487
 
Or_condition* Or_condition::copy( Condition_list_base* parent_P ) const
488
 
    {
489
 
    Or_condition* ret = new Or_condition( parent_P );
490
 
    for( Iterator it( *this );
491
 
         it;
492
 
         ++it )
493
 
        ret->append( (*it)->copy( ret ));
494
 
    return ret;
495
 
    }
496
 
 
497
 
const QString Or_condition::description() const
498
 
    {
499
 
    return i18n( "Or_condition", "Or" );
500
 
    }
501
 
 
502
 
} // namespace KHotKeys
503
 
 
504
 
#include "conditions.moc"