karm

desktoptracker.cpp
1 #include <algorithm> // std::find
2 
3 #include <tqtimer.h>
4 #include <kdebug.h>
5 
6 #include "desktoptracker.h"
7 
8 // TODO: Put in config dialog
9 const int minimumInterval = 5; // seconds
10 
11 DesktopTracker::DesktopTracker ()
12 {
13  // Setup desktop change handling
14  connect( &kWinModule, TQ_SIGNAL( currentDesktopChanged(int) ),
15  this, TQ_SLOT( handleDesktopChange(int) ));
16 
17  _desktopCount = kWinModule.numberOfDesktops();
18  _previousDesktop = kWinModule.currentDesktop()-1;
19  // TODO: removed? fixed by Lubos?
20  // currentDesktop will return 0 if no window manager is started
21  if( _previousDesktop < 0 ) _previousDesktop = 0;
22 
23  _timer = new TQTimer(this);
24  connect( _timer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( changeTimers() ) );
25 }
26 
27 void DesktopTracker::handleDesktopChange( int desktop )
28 {
29  _desktop = desktop;
30 
31  // If user changes back and forth between desktops rapidly and frequently,
32  // the data file can get huge fast if logging is turned on. Then saving
33  // get's slower, etc. There's no benefit in saving a lot of start/stop
34  // events that are very small. Wait a bit to make sure the user is settled.
35  if ( !_timer->start( minimumInterval * 1000, true ) ) changeTimers();
36 }
37 
38 void DesktopTracker::changeTimers()
39 {
40  _desktop--; // desktopTracker starts with 0 for desktop 1
41  // notify start all tasks setup for running on desktop
42  TaskVector::iterator it;
43 
44  // stop trackers for _previousDesktop
45  TaskVector tv = desktopTracker[_previousDesktop];
46  for (it = tv.begin(); it != tv.end(); ++it) {
47  emit leftActiveDesktop(*it);
48  }
49 
50  // start trackers for desktop
51  tv = desktopTracker[_desktop];
52  for (it = tv.begin(); it != tv.end(); ++it) {
53  emit reachedtActiveDesktop(*it);
54  }
55  _previousDesktop = _desktop;
56 
57  // emit updateButtons();
58 }
59 
60 TQString DesktopTracker::startTracking()
61 {
62  TQString err;
63  int currentDesktop = kWinModule.currentDesktop() -1;
64  // TODO: removed? fixed by Lubos?
65  // currentDesktop will return 0 if no window manager is started
66  if ( currentDesktop < 0 ) currentDesktop = 0;
67  if ( currentDesktop < maxDesktops )
68  {
69  TaskVector &tv = desktopTracker[ currentDesktop ];
70  TaskVector::iterator tit = tv.begin();
71  while(tit!=tv.end())
72  {
73  emit reachedtActiveDesktop(*tit);
74  tit++;
75  }
76  }
77  else err="ETooHighDeskTopNumber";
78  return err;
79 }
80 
81 void DesktopTracker::registerForDesktops( Task* task, DesktopList desktopList)
82 {
83  // if no desktop is marked, disable auto tracking for this task
84  if (desktopList.size()==0) {
85  for (int i=0; i<maxDesktops; i++) {
86  TaskVector *v = &(desktopTracker[i]);
87  TaskVector::iterator tit = std::find(v->begin(), v->end(), task);
88  if (tit != v->end())
89  desktopTracker[i].erase(tit);
90  // if the task was previously tracking this desktop then
91  // emit a signal that is not tracking it any more
92  if( i == kWinModule.currentDesktop() -1)
93  emit leftActiveDesktop(task);
94  }
95 
96  return;
97  }
98 
99  // If desktop contains entries then configure desktopTracker
100  // If a desktop was disabled, it will not be stopped automatically.
101  // If enabled: Start it now.
102  if (desktopList.size()>0) {
103  for (int i=0; i<maxDesktops; i++) {
104  TaskVector& v = desktopTracker[i];
105  TaskVector::iterator tit = std::find(v.begin(), v.end(), task);
106  // Is desktop i in the desktop list?
107  if ( std::find( desktopList.begin(), desktopList.end(), i)
108  != desktopList.end()) {
109  if (tit == v.end()) // not yet in start vector
110  v.push_back(task); // track in desk i
111  }
112  else { // delete it
113  if (tit != v.end()) // not in start vector any more
114  {
115  v.erase(tit); // so we delete it from desktopTracker
116  // if the task was previously tracking this desktop then
117  // emit a signal that is not tracking it any more
118  if( i == kWinModule.currentDesktop() -1)
119  emit leftActiveDesktop(task);
120  }
121  }
122  }
123  startTracking();
124  }
125 }
126 
127 void DesktopTracker::printTrackers() {
128  TaskVector::iterator it;
129  for (int i=0; i<maxDesktops; i++) {
130  TaskVector& start = desktopTracker[i];
131  it = start.begin();
132  while (it != start.end()) {
133  it++;
134  }
135  }
136 }
137 #include "desktoptracker.moc"
A class representing a task.
Definition: task.h:42