photosgallery/slideshow/engine/coresrc/shwtimer.cpp
changeset 0 4e91876724a2
child 18 bcb43dc84c44
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Slideshow specific timer class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "shwtimer.h"
       
    23 
       
    24 #include <glxlog.h>
       
    25 #include <glxtracer.h>
       
    26 
       
    27 // LOCAL CONSTANTS
       
    28 namespace
       
    29 	{
       
    30 	// how many milliseconds go to microsecond
       
    31 	const TInt KMultiplyMilli2Micro = 1000;
       
    32 	}
       
    33 
       
    34 // DEPENDENCIES
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // Constructor.
       
    38 // -----------------------------------------------------------------------------
       
    39 inline CShwTimer::CShwTimer()
       
    40 	: iPaused( EFalse ) // not paused by default
       
    41 	{
       
    42 	}
       
    43 	
       
    44 // -----------------------------------------------------------------------------
       
    45 // NewL.
       
    46 // -----------------------------------------------------------------------------
       
    47 CShwTimer* CShwTimer::NewL( TInt aPriority )
       
    48 	{
       
    49 	TRACER("CShwTimer::NewL");
       
    50 	GLX_LOG_INFO( "CShwTimer::NewL" );
       
    51 	CShwTimer* self = new (ELeave) CShwTimer;
       
    52 	CleanupStack::PushL( self );
       
    53 	self->ConstructL( aPriority );
       
    54 	CleanupStack::Pop( self );
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // ConstructL.
       
    60 // -----------------------------------------------------------------------------
       
    61 void CShwTimer::ConstructL( TInt aPriority )
       
    62 	{
       
    63 	TRACER("CShwTimer::ConstructL");
       
    64 	GLX_LOG_INFO( "CShwTimer::ConstructL" );
       
    65 	iTimer = CPeriodic::NewL( aPriority );
       
    66 	}
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // Destructor.
       
    70 // -----------------------------------------------------------------------------
       
    71 CShwTimer::~CShwTimer()
       
    72 	{
       
    73 	TRACER("CShwTimer::~CShwTimer()");
       
    74     GLX_LOG_INFO( "CShwTimer::~CShwTimer" );
       
    75 	Cancel();
       
    76 	delete iTimer;
       
    77 	}
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // Start.
       
    81 // -----------------------------------------------------------------------------
       
    82 void CShwTimer::Start( TInt aDelay, TInt aInterval, TCallBack aCallback )
       
    83 	{
       
    84 	TRACER("CShwTimer::Start");
       
    85     GLX_LOG_INFO( "CShwTimer::Start" );
       
    86 
       
    87     // convert millisecond to microseconds
       
    88     TTimeIntervalMicroSeconds32 delay = aDelay * KMultiplyMilli2Micro;
       
    89     TTimeIntervalMicroSeconds32 interval = aInterval * KMultiplyMilli2Micro;
       
    90     
       
    91 	// remember the callback
       
    92 	iCallback = aCallback;
       
    93 	// remember the interval
       
    94 	iInterval = interval;
       
    95 	// reset the time left in case start is called during pause
       
    96 	iTimeLeft = delay;
       
    97 
       
    98 	// No harm in cancelling the timer before start
       
    99 	iTimer->Cancel();
       
   100 
       
   101 	// if not paused, start the timer
       
   102 	if( !iPaused )
       
   103 		{
       
   104 		// Get the current time
       
   105 		iTimeTimerExpires.HomeTime();
       
   106 		// Add the delay to determine the expiry time
       
   107 		iTimeTimerExpires += iTimeLeft;
       
   108 		// Start the timer
       
   109 		iTimer->Start( iTimeLeft, iInterval, iCallback );
       
   110 		}
       
   111 	}
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // Cancel.
       
   115 // -----------------------------------------------------------------------------
       
   116 void CShwTimer::Cancel()
       
   117 	{
       
   118 	TRACER("CShwTimer::Cancel");
       
   119     GLX_LOG_INFO( "CShwTimer::Cancel" );
       
   120 	if ( iTimer )
       
   121 		{
       
   122 		iTimer->Cancel();
       
   123 		}
       
   124 	}
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // Pause
       
   128 // -----------------------------------------------------------------------------
       
   129 void CShwTimer::Pause()
       
   130 	{
       
   131 	TRACER("CShwTimer::Pause");
       
   132     GLX_LOG_INFO( "CShwTimer::Pause" );
       
   133     // pause only if not yet paused
       
   134 	if( !iPaused )
       
   135 		{
       
   136 		iPaused = ETrue;
       
   137 		// take time left
       
   138 		iTimeLeft = TimeLeft();
       
   139 		//if the iTimeTimerExpires has already elapsed then we get -ve time left.In this case make it zero
       
   140 		if(iTimeLeft.Int() <= 0)
       
   141 		    {
       
   142 		    iTimeLeft = 0;		    
       
   143 		    }
       
   144         GLX_LOG_INFO1( "CShwTimer::Pause timeleft %d", iTimeLeft.Int() );
       
   145 		// and cancel the timer
       
   146 		iTimer->Cancel();
       
   147 		}
       
   148 	}
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // Resume.
       
   152 // -----------------------------------------------------------------------------
       
   153 void CShwTimer::Resume()
       
   154 	{
       
   155 	TRACER("CShwTimer::Resume");
       
   156     GLX_LOG_INFO( "CShwTimer::Resume" );
       
   157     // resume only if paused
       
   158     if( iPaused )
       
   159     	{
       
   160 		iPaused = EFalse;
       
   161 		// if we had time left
       
   162 		if( 0 <= iTimeLeft.Int() )		
       
   163 			{
       
   164             GLX_LOG_INFO1( "CShwTimer::Resume timeleft %d", iTimeLeft.Int() );
       
   165 			// start again, first for time left and after that with interval
       
   166 			// NOTE! cant use Start as it takes time as milliseconds in a TInt
       
   167 			// Get the current time
       
   168 			iTimeTimerExpires.HomeTime();
       
   169 			// Add the delay to determine the expiry time
       
   170 			iTimeTimerExpires += iTimeLeft;
       
   171 			// Start the timer
       
   172 			iTimer->Start( iTimeLeft, iInterval, iCallback );
       
   173 			}
       
   174     	}
       
   175 	}
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // TimeLeft.
       
   179 // -----------------------------------------------------------------------------
       
   180 TTimeIntervalMicroSeconds32 CShwTimer::TimeLeft()
       
   181 	{
       
   182 	TRACER("CShwTimer::TimeLeft");
       
   183     GLX_LOG_INFO( "CShwTimer::TimeLeft" );
       
   184 
       
   185 	TTime timeNow;
       
   186 	timeNow.HomeTime();
       
   187 
       
   188 	// Difference between time now and the time at which timer should expire
       
   189 	TTimeIntervalMicroSeconds remainingTime;
       
   190 	remainingTime = iTimeTimerExpires.MicroSecondsFrom( timeNow );
       
   191 
       
   192 	TTimeIntervalMicroSeconds32 timeLeft = I64INT( remainingTime.Int64() );
       
   193 	
       
   194 	// In case that 64 bit value is larger than 32 bits, just have the 
       
   195 	// maximum 32 bit value
       
   196 	if ( remainingTime > KMaxTInt32 )
       
   197 		{
       
   198 		timeLeft = TTimeIntervalMicroSeconds32( KMaxTInt32 );
       
   199 		}
       
   200 	
       
   201 	return timeLeft;
       
   202 	}
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // IsActive.
       
   206 // -----------------------------------------------------------------------------
       
   207 TBool CShwTimer::IsActive()
       
   208 	{
       
   209 	TRACER("CShwTimer::IsActive");
       
   210 	return iTimer->IsActive();
       
   211 	}