2 * Copyright (C) 2003 by Tomas Pospisek <tpo@sourcepole.ch>
3 * 2007 the ktimetracker developers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the
17 * Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor
19 * Boston, MA 02110-1301 USA.
23 #include "desktoptracker.h"
25 #include "ktimetracker.h"
26 #include "ktimetrackerutility.h"
27 #include <KWindowSystem>
30 DesktopTracker::DesktopTracker ()
32 // Setup desktop change handling
34 connect( KWindowSystem::self(), SIGNAL(currentDesktopChanged(int)),
35 this, SLOT(handleDesktopChange(int)) );
36 mDesktopCount
= desktopCount();
37 mPreviousDesktop
= KWindowSystem::self()->currentDesktop()-1;
40 #warning non-X11 support missing
43 // currentDesktop will return 0 if no window manager is started
44 if( mPreviousDesktop
< 0 ) mPreviousDesktop
= 0;
45 mTimer
= new QTimer( this );
46 mTimer
->setSingleShot( true );
47 connect( mTimer
, SIGNAL(timeout()), this, SLOT(changeTimers()) );
50 void DesktopTracker::handleDesktopChange( int desktop
)
54 // If user changes back and forth between desktops rapidly and frequently,
55 // the data file can get huge fast if logging is turned on. Then saving
56 // gets slower, etc. There's no benefit in saving a lot of start/stop
57 // events that are very small. Wait a bit to make sure the user is settled.
58 mTimer
->start( KTimeTrackerSettings::minActiveTime() * 1000 );
61 void DesktopTracker::changeTimers()
63 --mDesktop
; // desktopTracker starts with 0 for desktop 1
64 // notify start all tasks setup for running on desktop
66 // stop trackers for mPreviousDesktop
67 foreach ( Task
*task
, mDesktopTracker
[mPreviousDesktop
] )
69 emit
leftActiveDesktop( task
);
72 // start trackers for desktop
73 foreach ( Task
*task
, mDesktopTracker
[mDesktop
] )
75 emit
reachedActiveDesktop( task
);
77 mPreviousDesktop
= mDesktop
;
80 QString
DesktopTracker::startTracking()
84 int currentDesktop
= KWindowSystem::self()->currentDesktop() -1;
86 int currentDesktop
= 0;
88 if ( currentDesktop
< 0 ) currentDesktop
= 0;
89 if ( currentDesktop
>= maxDesktops
) err
="desktop number too high, desktop tracking will not work";
91 foreach ( Task
*task
, mDesktopTracker
[ currentDesktop
] )
93 emit
reachedActiveDesktop( task
);
98 void DesktopTracker::registerForDesktops( Task
* task
, DesktopList desktopList
)
100 kDebug(5970) << "Entering function";
101 // if no desktop is marked, disable auto tracking for this task
102 if ( desktopList
.size() == 0 )
104 for ( int i
= 0; i
< maxDesktops
; ++i
)
106 TaskVector
*v
= &( mDesktopTracker
[i
] );
107 TaskVector::iterator tit
= qFind( v
->begin(), v
->end(), task
);
108 if ( tit
!= v
->end() )
109 mDesktopTracker
[i
].erase( tit
);
110 // if the task was priviously tracking this desktop then
111 // emit a signal that is not tracking it any more
113 if ( i
== KWindowSystem::self()->currentDesktop() - 1 )
114 emit
leftActiveDesktop( task
);
117 kDebug(5970) << "Leaving function, desktopList.size=0";
121 // If desktop contains entries then configure desktopTracker
122 // If a desktop was disabled, it will not be stopped automatically.
123 // If enabled: Start it now.
124 if ( desktopList
.size() > 0 )
126 for ( int i
= 0; i
< maxDesktops
; ++i
)
128 TaskVector
& v
= mDesktopTracker
[i
];
129 TaskVector::iterator tit
= qFind( v
.begin(), v
.end(), task
);
130 // Is desktop i in the desktop list?
131 if ( qFind( desktopList
.begin(), desktopList
.end(), i
)
132 != desktopList
.end() )
134 if ( tit
== v
.end() ) // not yet in start vector
135 v
.push_back( task
); // track in desk i
139 if ( tit
!= v
.end() ) // not in start vector any more
141 v
.erase( tit
); // so we delete it from desktopTracker
142 // if the task was priviously tracking this desktop then
143 // emit a signal that is not tracking it any more
145 if( i
== KWindowSystem::self()->currentDesktop() -1)
146 emit
leftActiveDesktop( task
);
153 kDebug(5970) << "Leaving function";