1
/***************************************************************************
2
* Copyright (C) 2005 - 2007 by *
3
* Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm> *
4
* Erik Jaelevik, Last.fm Ltd <erik@last.fm> *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
20
***************************************************************************/
22
#include "StopWatch.h"
25
/******************************************************************************
27
******************************************************************************/
28
StopWatch::StopWatch() :
37
/******************************************************************************
39
******************************************************************************/
41
const StopWatch& that) :
47
/******************************************************************************
49
******************************************************************************/
52
const StopWatch& that)
54
// Check for self-assignment
63
/******************************************************************************
65
******************************************************************************/
68
const StopWatch& that)
70
QMutexLocker grab(&mMutex);
71
mnTimer = that.mnTimer;
72
mnTimeout = that.mnTimeout;
76
/******************************************************************************
78
******************************************************************************/
83
EStopWatchState state = mState;
98
/******************************************************************************
100
******************************************************************************/
108
// We need to make sure the timer thread has finished since it might call
109
// Notify after we've called Stop. Since it's going back to
110
// PlayerConnection::onScrobbleTimeout to submit a track, we need to ensure
111
// that the track it submits is the same one that it was timing.
115
/******************************************************************************
117
******************************************************************************/
126
emit valueChanged(mnTimer);
130
/******************************************************************************
132
******************************************************************************/
134
StopWatch::setTimeout(
138
int curTime = mnTimer;
141
Q_ASSERT( nTimeout > curTime );
143
mnTimeout = nTimeout;
145
emit timeoutChanged(mnTimeout);
149
/******************************************************************************
151
******************************************************************************/
155
bool bStopped = false;
156
mLastTime = QDateTime::currentDateTime();
162
int nSleepInterval = 250;
163
msleep(nSleepInterval);
167
// Poll for stop every nSleepInterval
168
bStopped = mState == STOPPED;
170
QDateTime currentTime = QDateTime::currentDateTime();
171
int msSpentSleeping = mLastTime.time().msecsTo( currentTime.time() );
172
if ( msSpentSleeping >= 1000 )
174
mLastTime = currentTime;
176
totalMs += msSpentSleeping;
177
mnTimer = totalMs / (int)1000;
180
LOGL( 3, "msSpent: " << msSpentSleeping );
181
LOGL( 3, "ms: " << totalMs );
182
LOGL( 3, "s: " << mnTimer );
185
// Poll for timeout every second
186
if (!mbTimedOut && mnTimer >= mnTimeout)
188
emit timeoutReached();
192
emit valueChanged(mnTimer);
197
} while( !bStopped );