summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/taskmanager.cpp
blob: 79ef9a93731c41808a666b4bd2570c94de31baf2 (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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*****************************************************************

Copyright (c) 2000 Matthias Elter <elter@kde.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

#include <kglobal.h>
#include <klocale.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kiconloader.h>
#include <twinmodule.h>
#include <netwm.h>
#include <tqtimer.h>
#include <tqimage.h>

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "taskmanager.h"
#include "taskmanager.moc"

template class TQPtrList<Task>;

// Hack: create a global KWinModule without a parent. We
// can't make it a child of TaskManager because more than one
// TaskManager might be created. We can't make it a class
// variable without changing Task, which also uses it.
// So, we'll leak a little memory, but it's better than crashing.
// The real problem is that KWinModule should be a singleton.
KWinModule* twin_module = 0;

TaskManager::TaskManager(TQObject *parent, const char *name)
    : TQObject(parent, name), _active(0), _startup_info( NULL )
{

    twin_module = new KWinModule();

//    KGlobal::locale()->insertCatalogue("libtaskmanager");
    connect(twin_module, TQT_SIGNAL(windowAdded(WId)), TQT_SLOT(windowAdded(WId)));
    connect(twin_module, TQT_SIGNAL(windowRemoved(WId)), TQT_SLOT(windowRemoved(WId)));
    connect(twin_module, TQT_SIGNAL(activeWindowChanged(WId)), TQT_SLOT(activeWindowChanged(WId)));
    connect(twin_module, TQT_SIGNAL(currentDesktopChanged(int)), TQT_SLOT(currentDesktopChanged(int)));
    connect(twin_module, TQT_SIGNAL(windowChanged(WId,unsigned int)), TQT_SLOT(windowChanged(WId,unsigned int)));

    // register existing windows
    const TQValueList<WId> windows = twin_module->windows();
    TQValueList<WId>::ConstIterator end( windows.end() );
    for (TQValueList<WId>::ConstIterator it = windows.begin(); it != end; ++it )
  windowAdded(*it);

    // set active window
    WId win = twin_module->activeWindow();
    activeWindowChanged(win);

    configure_startup();
}

TaskManager::~TaskManager()
{
}

void TaskManager::configure_startup()
{
    KConfig c("klaunchrc", true);
    c.setGroup("FeedbackStyle");
    if (!c.readBoolEntry("TaskbarButton", true))
        return;
    _startup_info = new KStartupInfo( true, this );
    connect( _startup_info,
        TQT_SIGNAL( gotNewStartup( const KStartupInfoId&, const KStartupInfoData& )),
        TQT_SLOT( gotNewStartup( const KStartupInfoId&, const KStartupInfoData& )));
    connect( _startup_info,
        TQT_SIGNAL( gotStartupChange( const KStartupInfoId&, const KStartupInfoData& )),
        TQT_SLOT( gotStartupChange( const KStartupInfoId&, const KStartupInfoData& )));
    connect( _startup_info,
        TQT_SIGNAL( gotRemoveStartup( const KStartupInfoId&, const KStartupInfoData& )),
        TQT_SLOT( gotRemoveStartup( const KStartupInfoId& )));
    c.setGroup( "TaskbarButtonSettings" );
    _startup_info->setTimeout( c.readUnsignedNumEntry( "Timeout", 30 ));
}

Task* TaskManager::findTask(WId w)
{
    for (Task* t = _tasks.first(); t != 0; t = _tasks.next())
        if (t->window() == w  || t->hasTransient(w))
            return t;
    return 0;
}

#ifdef KDE_3_3
#define NET_ALL_TYPES_MASK (NET::AllTypesMask)
#else
#define NET_ALL_TYPES_MASK (-1LU)
#endif

void TaskManager::windowAdded(WId w )
{
  NETWinInfo info(qt_xdisplay(),  w, qt_xrootwin(),
                  NET::WMWindowType | NET::WMPid | NET::WMState );
  #ifdef KDE_3_2
  NET::WindowType windowType = info.windowType(NET_ALL_TYPES_MASK);
  #else
  NET::WindowType windowType = info.windowType();
  #endif
  // ignore NET::Tool and other special window types
  if (windowType != NET::Normal && windowType != NET::Override
      && windowType != NET::Unknown && windowType != NET::Dialog)
    return;
  // ignore windows that want to be ignored by the taskbar
  if ((info.state() & NET::SkipTaskbar) != 0)
  {
      _skiptaskbar_windows.push_front( w ); // remember them though
    return;
  }

  Window transient_for_tmp;
  if (XGetTransientForHint(qt_xdisplay(), (Window) w, &transient_for_tmp))
  {
    WId transient_for = (WId) transient_for_tmp;

    // check if it's transient for a skiptaskbar window
    if (_skiptaskbar_windows.contains(transient_for))
      return;

    // lets see if this is a transient for an existing task
    if (transient_for != qt_xrootwin() && transient_for != 0 )
    {
      Task* t = findTask(transient_for);
      if (t)
      {
        if (t->window() != w)
        {
          t->addTransient(w);
          // kdDebug() << "TM: Transient " << w << " added for Task: " << t->window() << endl;
        }
        return;
      }
    }
  }
  Task* t = new Task(w, this);
  _tasks.append(t);

  // kdDebug() << "TM: Task added for WId: " << w << endl;
  emit taskAdded(t);
}

void TaskManager::windowRemoved(WId w )
{
    _skiptaskbar_windows.remove( w );
    // find task
    Task* t = findTask(w);
    if (!t) return;

    if (t->window() == w) {
        _tasks.removeRef(t);

        emit taskRemoved(t);

        if(t == _active) _active = 0;
        delete t;
        //kdDebug() << "TM: Task for WId " << w << " removed." << endl;
    }
    else {
        t->removeTransient( w );
        //kdDebug() << "TM: Transient " << w << " for Task " << t->window() << " removed." << endl;
    }
}

void TaskManager::windowChanged(WId w, unsigned int dirty)
{
    if( dirty & NET::WMState ) {
        NETWinInfo info ( qt_xdisplay(),  w, qt_xrootwin(), NET::WMState );
        if ( (info.state() & NET::SkipTaskbar) != 0 ) {
            windowRemoved( w );
            _skiptaskbar_windows.push_front( w );
            return;
        }
        else {
            _skiptaskbar_windows.remove( w );
            if( !findTask( w ))
                windowAdded( w ); // skipTaskBar state was removed, so add this window
        }
    }

    // check if any state we are interested in is marked dirty
    if(!(dirty & (NET::WMVisibleName|NET::WMName|NET::WMState|NET::WMIcon|NET::XAWMState|NET::WMDesktop)) )
        return;

    // find task
    Task* t = findTask( w );
    if (!t) return;

    //kdDebug() << "TaskManager::windowChanged " << w << " " << dirty << endl;


    // refresh icon pixmap if necessary
    if (dirty & NET::WMIcon)
        t->refresh(true);
    else
        t->refresh();

    if(dirty & (NET::WMDesktop|NET::WMState|NET::XAWMState))
        emit windowChanged(w); // moved to different desktop or is on all or change in iconification/withdrawnnes
}

void TaskManager::activeWindowChanged(WId w )
{
    //kdDebug() << "TaskManager::activeWindowChanged" << endl;

    Task* t = findTask( w );
    if (!t) {
        if (_active) {
            _active->setActive(false);
            _active = 0;

            // there is no active window at the moment
            emit activeTaskChanged(0);
        }
    }
    else {
        if (_active)
            _active->setActive(false);

        _active = t;
        _active->setActive(true);

        emit activeTaskChanged(_active);
    }
}

void TaskManager::currentDesktopChanged(int desktop)
{
    emit desktopChanged(desktop);
}

void TaskManager::gotNewStartup( const KStartupInfoId& id, const KStartupInfoData& data )
{
    Startup* s = new Startup( id, data, this );
    _startups.append(s);

    emit startupAdded(s);
}

void TaskManager::gotStartupChange( const KStartupInfoId& id, const KStartupInfoData& data )
{
    for( Startup* s = _startups.first(); s != 0; s = _startups.next()) {
        if ( s->id() == id ) {
            s->update( data );
            return;
        }
    }
}

void TaskManager::gotRemoveStartup( const KStartupInfoId& id )
{
    killStartup( id );
}

void TaskManager::killStartup( const KStartupInfoId& id )
{
    Startup* s = 0;
    for(s = _startups.first(); s != 0; s = _startups.next()) {
        if (s->id() == id)
            break;
    }
    if (s == 0) return;

    _startups.removeRef(s);
    emit startupRemoved(s);
    delete s;
}

void TaskManager::killStartup(Startup* s)
{
    if (s == 0) return;

    _startups.removeRef(s);
    emit startupRemoved(s);
    delete s;
}

TQString TaskManager::desktopName(int desk) const
{
    return twin_module->desktopName(desk);
}

int TaskManager::numberOfDesktops() const
{
    return twin_module->numberOfDesktops();
}

bool TaskManager::isOnTop(const Task* task)
{
    if(!task) return false;

    for (TQValueList<WId>::ConstIterator it = twin_module->stackingOrder().fromLast();
         it != twin_module->stackingOrder().end(); --it ) {
  for (Task* t = _tasks.first(); t != 0; t = _tasks.next() ) {
      if ( (*it) == t->window() ) {
    if ( t == task )
        return true;
    if ( !t->isIconified() && (t->isAlwaysOnTop() == task->isAlwaysOnTop()) )
        return false;
    break;
      }
  }
    }
    return false;
}


Task::Task(WId win, TaskManager * parent, const char *name) :
  TQObject(parent, name),
  _active(false), _win(win),
  _lastWidth(0), _lastHeight(0), _lastResize(false), _lastIcon(),
  _thumbSize(0.2), _thumb(), _grab()
{
#ifdef KDE_3_2
  _info = KWin::windowInfo(_win, 0, 0);
#else
  _info = KWin::info(_win);
#endif
  // try to load icon via net_wm
  _pixmap = KWin::icon(_win, 16, 16, true);

  // try to guess the icon from the classhint
  if(_pixmap.isNull())
    KGlobal::instance()->iconLoader()->loadIcon(className().lower(),
            KIcon::Small,KIcon::Small,
            KIcon::DefaultState, 0, true);

  // load xapp icon
  if (_pixmap.isNull())
    _pixmap = SmallIcon("kcmx");
}

Task::~Task()
{
}

void Task::refresh(bool icon)
{
#ifdef KDE_3_2
  _info = KWin::windowInfo(_win, 0, 0);
#else
  _info = KWin::info(_win);
#endif
  if (icon)
  {
    // try to load icon via net_wm
    _pixmap = KWin::icon(_win, 16, 16, true);

    // try to guess the icon from the classhint
    if(_pixmap.isNull())
    {
        KGlobal::instance()->iconLoader()->loadIcon(className().lower(),
            KIcon::Small, KIcon::Small, KIcon::DefaultState, 0, true);
    }

    // load xapp icon
    if (_pixmap.isNull())
      _pixmap = SmallIcon("kcmx");

    _lastIcon.resize(0,0);
    emit iconChanged();
  }
  emit changed();
}

void Task::setActive(bool a)
{
    _active = a;
    emit changed();
    if ( a )
      emit activated();
    else
      emit deactivated();
}

bool Task::isMaximized() const
{
#ifdef KDE_3_2
  return(_info.state() & NET::Max);
#else
  return(_info.state & NET::Max);
#endif
}

bool Task::isIconified() const
{
#ifdef KDE_3_2
  return (_info.mappingState() == NET::Iconic);
#else
  return (_info.mappingState == NET::Iconic);
#endif
}

bool Task::isAlwaysOnTop() const
{
#ifdef KDE_3_2
  return (_info.state() & NET::StaysOnTop);
#else
  return (_info.state & NET::StaysOnTop);
#endif
}

bool Task::isShaded() const
{
#ifdef KDE_3_2
  return (_info.state() & NET::Shaded);
#else
  return (_info.state & NET::Shaded);
#endif
}

bool Task::isOnCurrentDesktop() const
{
#ifdef KDE_3_2
  return (_info.onAllDesktops() || _info.desktop() == twin_module->currentDesktop());
#else
  return (_info.onAllDesktops || _info.desktop == twin_module->currentDesktop());
#endif
}

bool Task::isOnAllDesktops() const
{
#ifdef KDE_3_2
  return _info.onAllDesktops();
#else
  return _info.onAllDesktops;
#endif
}

bool Task::isActive() const
{
  return _active;
}

bool Task::isOnTop() const
{
  return taskManager()->isOnTop( this );
}

bool Task::isModified() const
{
  static TQString modStr = TQString::fromUtf8("[") + i18n("modified") + TQString::fromUtf8("]");
#ifdef KDE_3_2
  int modStrPos = _info.visibleName().find(modStr);
#else
  int modStrPos = _info.visibleName.find(modStr);
#endif

  return ( modStrPos != -1 );
}

TQString Task::iconName() const
{
    NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMIconName);
    return TQString::fromUtf8(ni.iconName());
}
TQString Task::visibleIconName() const
{
    NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMVisibleIconName);
    return TQString::fromUtf8(ni.visibleIconName());
}

TQString Task::className()
{
    XClassHint hint;
    if(XGetClassHint(qt_xdisplay(), _win, &hint)) {
  TQString nh( hint.res_name );
  XFree( hint.res_name );
  XFree( hint.res_class );
  return nh;
    }
    return TQString();
}

TQString Task::classClass()
{
    XClassHint hint;
    if(XGetClassHint(qt_xdisplay(), _win, &hint)) {
  TQString ch( hint.res_class );
  XFree( hint.res_name );
  XFree( hint.res_class );
  return ch;
    }
    return TQString();
}

TQPixmap Task::icon( int width, int height, bool allowResize )
{
  if ( (width == _lastWidth)
       && (height == _lastHeight)
       && (allowResize == _lastResize )
       && (!_lastIcon.isNull()) )
    return _lastIcon;

  TQPixmap newIcon = KWin::icon( _win, width, height, allowResize );
  if ( !newIcon.isNull() ) {
    _lastIcon = newIcon;
    _lastWidth = width;
    _lastHeight = height;
    _lastResize = allowResize;
  }

  return newIcon;
}

TQPixmap Task::bestIcon( int size, bool &isStaticIcon )
{
  TQPixmap pixmap;
  isStaticIcon = false;

  switch( size ) {
  case KIcon::SizeSmall:
    {
      pixmap = icon( 16, 16, true  );

      // Icon of last resort
      if( pixmap.isNull() ) {
  pixmap = KGlobal::iconLoader()->loadIcon( "go",
              KIcon::NoGroup,
              KIcon::SizeSmall );
  isStaticIcon = true;
      }
    }
    break;
  case KIcon::SizeMedium:
    {
      //
      // Try 34x34 first for KDE 2.1 icons with shadows, if we don't
      // get one then try 32x32.
      //
      pixmap = icon( 34, 34, false  );

      if ( ( pixmap.width() != 34 ) || ( pixmap.height() != 34 ) ) {
  if ( ( pixmap.width() != 32 ) || ( pixmap.height() != 32 ) ) {
    pixmap = icon( 32, 32, true  );
  }
      }

      // Icon of last resort
      if( pixmap.isNull() ) {
  pixmap = KGlobal::iconLoader()->loadIcon( "go",
              KIcon::NoGroup,
              KIcon::SizeMedium );
  isStaticIcon = true;
      }
    }
    break;
  case KIcon::SizeLarge:
    {
      // If there's a 48x48 icon in the hints then use it
      pixmap = icon( size, size, false  );

      // If not, try to get one from the classname
      if ( pixmap.isNull() || pixmap.width() != size || pixmap.height() != size ) {
  pixmap = KGlobal::iconLoader()->loadIcon( className(),
              KIcon::NoGroup,
              size,
              KIcon::DefaultState,
              0L,
              true );
  isStaticIcon = true;
      }

      // If we still don't have an icon then scale the one in the hints
      if ( pixmap.isNull() || ( pixmap.width() != size ) || ( pixmap.height() != size ) ) {
  pixmap = icon( size, size, true  );
  isStaticIcon = false;
      }

      // Icon of last resort
      if( pixmap.isNull() ) {
  pixmap = KGlobal::iconLoader()->loadIcon( "go",
              KIcon::NoGroup,
              size );
  isStaticIcon = true;
      }
    }
  }

  return pixmap;
}

bool Task::idMatch( const TQString& id1, const TQString& id2 )
{
  if ( id1.isEmpty() || id2.isEmpty() )
    return false;

  if ( id1.contains( id2 ) > 0 )
    return true;

  if ( id2.contains( id1 ) > 0 )
    return true;

  return false;
}


void Task::maximize()
{
  NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMState);
  ni.setState( NET::Max, NET::Max );

#ifdef KDE_3_2
  if (_info.mappingState() == NET::Iconic)
#else
  if (_info.mappingState == NET::Iconic)
#endif
    activate();
}

void Task::restore()
{
  NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMState);
  ni.setState( 0, NET::Max );
#ifdef KDE_3_2
  if (_info.mappingState() == NET::Iconic)
#else
  if (_info.mappingState == NET::Iconic)
#endif
    activate();
}

void Task::iconify()
{
  XIconifyWindow( qt_xdisplay(), _win, qt_xscreen() );
}

void Task::close()
{
    NETRootInfo ri( qt_xdisplay(),  NET::CloseWindow );
    ri.closeWindowRequest( _win );
}

void Task::raise()
{
//    kdDebug(1210) << "Task::raise(): " << name() << endl;
    XRaiseWindow( qt_xdisplay(), _win );
}

void Task::lower()
{
//    kdDebug(1210) << "Task::lower(): " << name() << endl;
    XLowerWindow( qt_xdisplay(), _win );
}

void Task::activate()
{
//    kdDebug(1210) << "Task::activate():" << name() << endl;
    NETRootInfo ri( qt_xdisplay(), 0 );
    ri.setActiveWindow( _win );
}

void Task::activateRaiseOrIconify()
{
    if ( !isActive() || isIconified() ) {
        activate();
    } else if ( !isOnTop() ) {
       raise();
    } else {
       iconify();
    }
}

void Task::toDesktop(int desk)
{
  NETWinInfo ni(qt_xdisplay(), _win, qt_xrootwin(), NET::WMDesktop);
  if (desk == 0)
  {
#ifdef KDE_3_2
    if (_info.onAllDesktops())
    {
        ni.setDesktop(twin_module->currentDesktop());
        KWin::forceActiveWindow(_win);
    }
#else
    if (_info.onAllDesktops)
    {
        ni.setDesktop(twin_module->currentDesktop());
        KWin::setActiveWindow(_win);
    }
#endif
    else
        ni.setDesktop(NETWinInfo::OnAllDesktops);
    return;
  }
  ni.setDesktop(desk);
  if (desk == twin_module->currentDesktop())
#ifdef KDE_3_2
    KWin::forceActiveWindow(_win);
#else
    KWin::setActiveWindow(_win);
#endif
}

void Task::toCurrentDesktop()
{
    toDesktop(twin_module->currentDesktop());
}

void Task::setAlwaysOnTop(bool stay)
{
    NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMState);
    if(stay)
        ni.setState( NET::StaysOnTop, NET::StaysOnTop );
    else
        ni.setState( 0, NET::StaysOnTop );
}

void Task::toggleAlwaysOnTop()
{
    setAlwaysOnTop( !isAlwaysOnTop() );
}

void Task::setShaded(bool shade)
{
    NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMState);
    if(shade)
        ni.setState( NET::Shaded, NET::Shaded );
    else
        ni.setState( 0, NET::Shaded );
}

void Task::toggleShaded()
{
    setShaded( !isShaded() );
}

void Task::publishIconGeometry(TQRect rect)
{
    NETWinInfo ni( qt_xdisplay(),  _win, qt_xrootwin(), NET::WMIconGeometry);
    NETRect r;
    r.pos.x = rect.x();
    r.pos.y = rect.y();
    r.size.width = rect.width();
    r.size.height = rect.height();
    ni.setIconGeometry(r);
}

void Task::updateThumbnail()
{
  if ( !isOnCurrentDesktop() )
    return;
  if ( !isActive() )
    return;
  if ( !_grab.isNull() ) // We're already processing one...
    return;

   //
   // We do this as a two stage process to remove the delay caused
   // by the thumbnail generation. This makes things much smoother
   // on slower machines.
   //
  TQWidget *rootWin = TQT_TQWIDGET(tqApp->desktop());
  TQRect geom = _info.geometry();
   _grab = TQPixmap::grabWindow( rootWin->winId(),
        geom.x(), geom.y(),
        geom.width(), geom.height() );

   if ( !_grab.isNull() )
     TQTimer::singleShot( 200, this, TQT_SLOT( generateThumbnail() ) );
}

void Task::generateThumbnail()
{
   if ( _grab.isNull() )
      return;

   TQImage img = _grab.convertToImage();

   double width = img.width();
   double height = img.height();
   width = width * _thumbSize;
   height = height * _thumbSize;

   img = img.smoothScale( (int) width, (int) height );
   _thumb = img;
   _grab.resize( 0, 0 ); // Makes grab a null image.

   emit thumbnailChanged();
}

Startup::Startup( const KStartupInfoId& id, const KStartupInfoData& data,
    TQObject * parent, const char *name)
    : TQObject(parent, name), _id( id ), _data( data )
{
}

Startup::~Startup()
{

}

void Startup::update( const KStartupInfoData& data )
{
    _data.update( data );
    emit changed();
}

int TaskManager::currentDesktop() const
{
    return twin_module->currentDesktop();
}