desktoptracker.cpp
00001 #include <algorithm> // std::find 00002 00003 #include <tqtimer.h> 00004 #include <kdebug.h> 00005 00006 #include "desktoptracker.h" 00007 00008 // TODO: Put in config dialog 00009 const int minimumInterval = 5; // seconds 00010 00011 DesktopTracker::DesktopTracker () 00012 { 00013 // Setup desktop change handling 00014 connect( &kWinModule, TQT_SIGNAL( currentDesktopChanged(int) ), 00015 this, TQT_SLOT( handleDesktopChange(int) )); 00016 00017 _desktopCount = kWinModule.numberOfDesktops(); 00018 _previousDesktop = kWinModule.currentDesktop()-1; 00019 // TODO: removed? fixed by Lubos? 00020 // currentDesktop will return 0 if no window manager is started 00021 if( _previousDesktop < 0 ) _previousDesktop = 0; 00022 00023 _timer = new TQTimer(this); 00024 connect( _timer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( changeTimers() ) ); 00025 } 00026 00027 void DesktopTracker::handleDesktopChange( int desktop ) 00028 { 00029 _desktop = desktop; 00030 00031 // If user changes back and forth between desktops rapidly and frequently, 00032 // the data file can get huge fast if logging is turned on. Then saving 00033 // get's slower, etc. There's no benefit in saving a lot of start/stop 00034 // events that are very small. Wait a bit to make sure the user is settled. 00035 if ( !_timer->start( minimumInterval * 1000, true ) ) changeTimers(); 00036 } 00037 00038 void DesktopTracker::changeTimers() 00039 { 00040 _desktop--; // desktopTracker starts with 0 for desktop 1 00041 // notify start all tasks setup for running on desktop 00042 TaskVector::iterator it; 00043 00044 // stop trackers for _previousDesktop 00045 TaskVector tv = desktopTracker[_previousDesktop]; 00046 for (it = tv.begin(); it != tv.end(); ++it) { 00047 emit leftActiveDesktop(*it); 00048 } 00049 00050 // start trackers for desktop 00051 tv = desktopTracker[_desktop]; 00052 for (it = tv.begin(); it != tv.end(); ++it) { 00053 emit reachedtActiveDesktop(*it); 00054 } 00055 _previousDesktop = _desktop; 00056 00057 // emit updateButtons(); 00058 } 00059 00060 TQString DesktopTracker::startTracking() 00061 { 00062 TQString err; 00063 int currentDesktop = kWinModule.currentDesktop() -1; 00064 // TODO: removed? fixed by Lubos? 00065 // currentDesktop will return 0 if no window manager is started 00066 if ( currentDesktop < 0 ) currentDesktop = 0; 00067 if ( currentDesktop < maxDesktops ) 00068 { 00069 TaskVector &tv = desktopTracker[ currentDesktop ]; 00070 TaskVector::iterator tit = tv.begin(); 00071 while(tit!=tv.end()) 00072 { 00073 emit reachedtActiveDesktop(*tit); 00074 tit++; 00075 } 00076 } 00077 else err="ETooHighDeskTopNumber"; 00078 return err; 00079 } 00080 00081 void DesktopTracker::registerForDesktops( Task* task, DesktopList desktopList) 00082 { 00083 // if no desktop is marked, disable auto tracking for this task 00084 if (desktopList.size()==0) { 00085 for (int i=0; i<maxDesktops; i++) { 00086 TaskVector *v = &(desktopTracker[i]); 00087 TaskVector::iterator tit = std::find(v->begin(), v->end(), task); 00088 if (tit != v->end()) 00089 desktopTracker[i].erase(tit); 00090 // if the task was previously tracking this desktop then 00091 // emit a signal that is not tracking it any more 00092 if( i == kWinModule.currentDesktop() -1) 00093 emit leftActiveDesktop(task); 00094 } 00095 00096 return; 00097 } 00098 00099 // If desktop contains entries then configure desktopTracker 00100 // If a desktop was disabled, it will not be stopped automatically. 00101 // If enabled: Start it now. 00102 if (desktopList.size()>0) { 00103 for (int i=0; i<maxDesktops; i++) { 00104 TaskVector& v = desktopTracker[i]; 00105 TaskVector::iterator tit = std::find(v.begin(), v.end(), task); 00106 // Is desktop i in the desktop list? 00107 if ( std::find( desktopList.begin(), desktopList.end(), i) 00108 != desktopList.end()) { 00109 if (tit == v.end()) // not yet in start vector 00110 v.push_back(task); // track in desk i 00111 } 00112 else { // delete it 00113 if (tit != v.end()) // not in start vector any more 00114 { 00115 v.erase(tit); // so we delete it from desktopTracker 00116 // if the task was previously tracking this desktop then 00117 // emit a signal that is not tracking it any more 00118 if( i == kWinModule.currentDesktop() -1) 00119 emit leftActiveDesktop(task); 00120 } 00121 } 00122 } 00123 startTracking(); 00124 } 00125 } 00126 00127 void DesktopTracker::printTrackers() { 00128 TaskVector::iterator it; 00129 for (int i=0; i<maxDesktops; i++) { 00130 TaskVector& start = desktopTracker[i]; 00131 it = start.begin(); 00132 while (it != start.end()) { 00133 it++; 00134 } 00135 } 00136 } 00137 #include "desktoptracker.moc"