telephonyserverplugins/simtsy/src/CSimReduceTimers.cpp
changeset 0 3553901f7fa8
child 19 630d2f34d719
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // A subject for oberving requests to reduce the timers currently
       
    15 // running in the simulator.
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 
       
    24 #include "Simlog.h"
       
    25 #include "CSimReduceTimers.h"
       
    26 
       
    27 
       
    28 /**
       
    29 Factory function for creating a CSimReduceTimers object.
       
    30 
       
    31 @return A pointer to the object created.
       
    32 */
       
    33 CSimReduceTimers* CSimReduceTimers::NewL()
       
    34 	{
       
    35 	CSimReduceTimers* self = new(ELeave) CSimReduceTimers;
       
    36 	CleanupStack::PushL(self);
       
    37 	self->ConstructL();
       
    38 	CleanupStack::Pop(self);
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 /**
       
    43 Second phase constructor.  Ensures that the required Publish &
       
    44 Subscribe property is defined and starts listening to any changes (or
       
    45 Set() calls) made to this property.
       
    46 */
       
    47 void CSimReduceTimers::ConstructL()
       
    48 	{
       
    49 	TInt dummy;
       
    50 	TInt ret = RProperty::Get(KUidPSSimTsyCategory, KPSSimTsyTimersReduceTime, dummy);
       
    51 
       
    52 	if (ret == KErrNone)
       
    53 		{
       
    54 		iPSProperty = CSimPubSub::TPubSubProperty(KUidPSSimTsyCategory,KPSSimTsyTimersReduceTime,
       
    55 												KPSSimTsyTimersReduceTimeSignalKeyType);
       
    56 		iSimPubSub = CSimPubSub::NewL(this,iPSProperty);
       
    57 		iSimPubSub->Start();
       
    58 		}
       
    59 	else
       
    60 		{
       
    61 		User::Leave(ret);
       
    62 		}
       
    63 	}
       
    64 
       
    65 /**
       
    66 Default, first-phase, constructor.
       
    67 */
       
    68 CSimReduceTimers::CSimReduceTimers()
       
    69 	{
       
    70 	// NOP
       
    71 	}
       
    72 
       
    73 /**
       
    74 Default destrucor for the reduce timers subject.
       
    75 */
       
    76 CSimReduceTimers::~CSimReduceTimers()
       
    77 	{
       
    78 	delete iSimPubSub;
       
    79 	iObservers.Close();
       
    80 	}
       
    81 
       
    82 void CSimReduceTimers::SimPSEvent(const CSimPubSub::TPubSubProperty aProperty, TInt aStatus)
       
    83 	{
       
    84 	if (aProperty == iPSProperty && // Condition for correct property
       
    85 		(aStatus == ETimerIdAllTimers)) // and correct value for signal
       
    86 		{
       
    87 		LOGMISC1(">>CSimReduceTimers.cpp: Reduce Timer event fired for all timers");
       
    88 		Notify();
       
    89 		}
       
    90 	else if(aProperty == iPSProperty && aStatus > 0)
       
    91 		{
       
    92 		LOGMISC2(">>CSimReduceTimers.cpp: Reduce Timer event fired for event ID %d", aStatus);
       
    93 		Notify(aStatus);
       
    94 		}
       
    95 	}
       
    96 
       
    97 void CSimReduceTimers::AttachL(MSimTimerUpdateObserver* aObserver)
       
    98 	{
       
    99 	LOGMISC1("CSimReduceTimers::AttachL");
       
   100 	User::LeaveIfError(iObservers.Append(aObserver));
       
   101 	}
       
   102 
       
   103 void CSimReduceTimers::DetachL(MSimTimerUpdateObserver* aObserver)
       
   104 	{
       
   105 	TInt pos = iObservers.Find(aObserver);
       
   106 	LOGMISC2("CSimReduceTimers::DetachL, from position %d", pos);
       
   107 	User::LeaveIfError(pos);
       
   108 	iObservers.Remove(pos);
       
   109 	}
       
   110 
       
   111 void CSimReduceTimers::Notify()
       
   112 	{
       
   113 	TInt totalItems = iObservers.Count();
       
   114 	LOGMISC2("CSimReduceTimers::Notify, %d observers registered", totalItems);
       
   115 	if (totalItems <= 0)
       
   116 		{
       
   117 		LOGMISC1("CSimReduceTimers::Notify, No timers to reduce.");
       
   118 		return;
       
   119 		}
       
   120 
       
   121 	TInt minTimeReduce = KMaxTInt;
       
   122 	TInt num;
       
   123 	for (num = 0; num < totalItems; num++)
       
   124 		{
       
   125 		TInt temp = iObservers[num]->GetRemainingSeconds();
       
   126 		LOGMISC3("CSimReduceTimers::Notify, iObservers[%d] remaining time = %d", num, temp);
       
   127 
       
   128 		// Ensure no error is returned and check if the current itteration gives a lower time remaining.
       
   129 		if (temp > 0 && temp < minTimeReduce)
       
   130 			{
       
   131 			minTimeReduce = temp;
       
   132 			}
       
   133 		}
       
   134 
       
   135 	if (minTimeReduce == KMaxTInt)
       
   136 		{
       
   137 		LOGMISC1("CSimReduceTimers::Notify, No running timers.");
       
   138 		return;
       
   139 		}
       
   140 
       
   141 	LOGMISC2("CSimReduceTimers::Notify, Min time to reduce all timers: %d", minTimeReduce);
       
   142 	// Ensure that reducing timers by minTimeReduce does not reduce any timer to less than KTimerDelayOnReduceTimeSignal.
       
   143 	minTimeReduce -= KTimerDelayOnReduceTimeSignal;
       
   144 	if (minTimeReduce <= 0)
       
   145 		{
       
   146 		LOGMISC1("CSimReduceTimers::Notify, Min time to reduce <= KTimerDelayOnReduceTimeSignal, no change to timers.");
       
   147 		return;
       
   148 		}
       
   149 
       
   150 	for (num = 0; num < totalItems; num++)
       
   151 		{
       
   152 		iObservers[num]->Update(minTimeReduce);
       
   153 		}
       
   154 	}
       
   155 	
       
   156 void CSimReduceTimers::Notify(TInt aTimerEventId)
       
   157 	{
       
   158 	TInt totalItems = iObservers.Count();
       
   159 	LOGMISC2("CSimReduceTimers::Notify, %d observers registered", totalItems);
       
   160 	if (totalItems <= 0)
       
   161 		{
       
   162 		LOGMISC1("CSimReduceTimers::Notify, No timers to reduce.");
       
   163 		return;
       
   164 		}
       
   165 	
       
   166 	//Get the index of the observer with with event id aTimerEventId
       
   167 	//and the lowest remaining timer 
       
   168 	TInt lowestTime = KMaxTInt;
       
   169 	TInt indexOfLowest = KErrNotFound;
       
   170 	
       
   171 	TInt index;
       
   172 	for(index = 0; index < totalItems; index++)
       
   173 		{
       
   174 		if((iObservers[index]->GetTimerEventInfo() == aTimerEventId) &&
       
   175 			(iObservers[index]->GetRemainingSeconds() > 0) &&
       
   176 			(iObservers[index]->GetRemainingSeconds() < lowestTime))
       
   177 			{
       
   178 			indexOfLowest = index;
       
   179 			lowestTime = iObservers[index]->GetRemainingSeconds();
       
   180 			}
       
   181 		}
       
   182 		
       
   183 	if(indexOfLowest == KErrNotFound)
       
   184 		{
       
   185 		LOGMISC2(">>CSimReduceTimers::Notify, No observers with event ID %d found", 
       
   186 					aTimerEventId);
       
   187 		return;
       
   188 		}
       
   189 
       
   190 	//Check that reducing the timer of the selected observer is acceptable
       
   191 	TInt reduceBy = lowestTime - KTimerDelayOnReduceTimeSignal;
       
   192 	if(reduceBy >= lowestTime)
       
   193 		{
       
   194 		LOGMISC3("CSimReduceTimers::Notify, Lowest timer for event ID %d already less than %d sec. No timer reduced.",
       
   195 				KTimerDelayOnReduceTimeSignal, aTimerEventId);
       
   196 		}
       
   197 	else
       
   198 		{
       
   199 		LOGMISC3(">>CSimReduceTimers::Notify, Timer for observer with event ID %d, reduced by %d sec", 
       
   200 					aTimerEventId, reduceBy);
       
   201 		iObservers[indexOfLowest]->Update(reduceBy);
       
   202 		}
       
   203 	}