buildverification/smoketest/Timew/Src/SM_ALM.CPP
changeset 0 9736f095102e
equal deleted inserted replaced
-1:000000000000 0:9736f095102e
       
     1 // Copyright (c) 1997-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 //
       
    15 
       
    16 #include "almmod.h"
       
    17 #include "sm_std.h"
       
    18 #include <ecom/ecom.h>
       
    19 
       
    20 //
       
    21 //	class CAlmModel
       
    22 //
       
    23 
       
    24 CAlmModel::CAlmModel()
       
    25 //
       
    26 //	Constructor
       
    27 //
       
    28 	{
       
    29 	}
       
    30 
       
    31 CAlmModel::~CAlmModel()
       
    32 //
       
    33 //	Destructor
       
    34 //
       
    35 	{
       
    36 	Stop();
       
    37 	iClockAlarms.ResetAndDestroy();
       
    38 	iClockAlarms.Close();
       
    39 	iReviewAlarmIds.Close();
       
    40 	iNextAlarmIds.Close();
       
    41 	iAlmSvr.Close();
       
    42  	REComSession::FinalClose();
       
    43 	}
       
    44 
       
    45 void CAlmModel::ConstructL(MTimewModelObserver* aObserver,TInt aResponderPriority)
       
    46 //
       
    47 //	Construct the alarm engine components
       
    48 //
       
    49 	{
       
    50 	SetTimewModelObserver(aObserver);
       
    51 	//
       
    52 	iReviewAlarmIds = RArray<TAlarmId>(KArrayOfAlarmIdsGranularity);
       
    53 	iNextAlarmIds = RArray<TAlarmId>(KArrayOfAlarmIdsGranularity);
       
    54 	//
       
    55 	const TInt r = iAlmSvr.Connect();
       
    56 	User::LeaveIfError(r);
       
    57 	//
       
    58 	TInt i;
       
    59 	iClockAlarms = RPointerArray<TASShdAlarm>(KMaxClockAlarms);
       
    60 	for(i=0; i<KMaxClockAlarms; i++)
       
    61 		{
       
    62 		TASShdAlarm* blankAlarm = new(ELeave) TASShdAlarm();
       
    63 		CleanupStack::PushL(blankAlarm);
       
    64 		User::LeaveIfError(iClockAlarms.Append(blankAlarm));
       
    65 		CleanupStack::Pop(blankAlarm);
       
    66 		}
       
    67 
       
    68 	// Identify any clock alarms in the alarm server and put them
       
    69 	// into the slots.
       
    70 
       
    71 	// Fetch
       
    72 	RArray<TAlarmId> clockIds(KMaxClockAlarms);
       
    73 	CleanupClosePushL(clockIds);
       
    74 	iAlmSvr.GetAlarmIdListForCategoryL(KASCliCategoryClock, clockIds);
       
    75 
       
    76 	// Put them in the slots - we can only display KMaxClockAlarms alarms
       
    77 	// so any beyond that are ignored.
       
    78 	TInt slotsUsedSoFar = 0;
       
    79 	const TInt count = Min(KMaxClockAlarms, clockIds.Count());
       
    80 	for(i=0; i<count; i++)
       
    81 		{
       
    82 		TASShdAlarm* alarm = iClockAlarms[slotsUsedSoFar];
       
    83 		User::LeaveIfError(iAlmSvr.GetAlarmDetails(clockIds[i], *alarm));
       
    84 
       
    85 		// Only show alarms which haven't yet notified
       
    86 		if	(alarm->State() != EAlarmStateNotified)
       
    87 			{
       
    88 			// If we don't do this, then the alarm slot is treated
       
    89 			// as available.
       
    90 			++slotsUsedSoFar;
       
    91 			alarm->ClientFlags().Set(EClockAlarmFlagsIsSet);
       
    92 			}
       
    93 		}
       
    94 	CleanupStack::PopAndDestroy(&clockIds);
       
    95 	//
       
    96 	CAlmResponder* pR=new(ELeave) CAlmResponder(iAlmSvr,*this,aResponderPriority);
       
    97 	SetResponderActive(pR);
       
    98 	}
       
    99 
       
   100 CAlmModel* CAlmModel::NewL(MTimewModelObserver* aObserver,TInt aResponderPriority)
       
   101 //
       
   102 //	Create a new alarm engine
       
   103 //
       
   104 	{
       
   105 	CAlmModel* model=new(ELeave) CAlmModel;
       
   106 	CleanupStack::PushL(model);
       
   107 	model->ConstructL(aObserver,aResponderPriority);
       
   108 	CleanupStack::Pop(model);
       
   109 	return(model);
       
   110 	}
       
   111 
       
   112 void CAlmModel::ProcessResponder(TInt aStatus)
       
   113 //
       
   114 //	Interpret responder status
       
   115 //
       
   116 	{
       
   117 	// FIX
       
   118 	if (aStatus!=KErrCancel && aStatus!=KErrNone)
       
   119 		Notify(aStatus);
       
   120 	else
       
   121 		Notify(0);
       
   122 	}
       
   123 
       
   124 TInt CAlmModel::AlarmQuietPeriodMinutes() const
       
   125 //
       
   126 //	Return the time that sound is delayed until
       
   127 //
       
   128 	{
       
   129 	TTime timeNow;
       
   130 	timeNow.HomeTime();
       
   131 	TTime timeQuiet=AlarmQuietPeriodEnd();
       
   132 	TInt quietMinutes;
       
   133 	if (timeQuiet<=timeNow || timeQuiet==Time::NullTTime())
       
   134 		quietMinutes=0;
       
   135 	else
       
   136 		{
       
   137 		TTimeIntervalMinutes minutes;
       
   138 		timeQuiet.MinutesFrom(timeNow,minutes);
       
   139 		quietMinutes=minutes.Int()+1;
       
   140 		}
       
   141 	return(quietMinutes);
       
   142 	}
       
   143 
       
   144 TInt CAlmModel::AlarmQuietPeriodSet(TInt aMinutes)
       
   145 //
       
   146 //	Delay alarm sounds for a given number of minutes
       
   147 //
       
   148 	{
       
   149 	TInt ret = KErrNone;
       
   150 	//
       
   151 	if	(!aMinutes)
       
   152 		ret = iAlmSvr.CancelAlarmSilence();
       
   153 	else
       
   154 		ret = iAlmSvr.SetAlarmSoundsSilentFor(TTimeIntervalMinutes(aMinutes));
       
   155 	//
       
   156 	return ret;
       
   157 	}
       
   158 
       
   159 TInt CAlmModel::ClockAlarmEnable(TInt aAlarmIndex, TTimeWClockAlarmState aClockAlarmState)
       
   160 //
       
   161 //	Enable or disable a clock alarm
       
   162 //
       
   163 	{
       
   164 	__ASSERT_DEBUG(aAlarmIndex < KMaxClockAlarms, Panic(EModelOutOfRange));
       
   165 	//
       
   166 	TAlarmStatus newStatus;
       
   167 	switch(aClockAlarmState)
       
   168 		{
       
   169 	case ETimeWClockAlarmStateNotSet:
       
   170 		return ClockAlarmDelete(aAlarmIndex);
       
   171 
       
   172 	case ETimeWClockAlarmStateSet:
       
   173 		newStatus = EAlarmStatusEnabled;
       
   174 		break;
       
   175 
       
   176 	default:
       
   177 	case ETimeWClockAlarmStateDisabled:
       
   178 		newStatus = EAlarmStatusDisabled;
       
   179 		break;
       
   180 		}
       
   181 
       
   182 	TASShdAlarm* alarm = iClockAlarms[aAlarmIndex];
       
   183 	__ASSERT_ALWAYS(alarm->Id() != KNullAlarmId, Fault(EModelfTryingToChangeStateOfNullClockAlarm));
       
   184 	TInt error = iAlmSvr.SetAlarmStatus(alarm->Id(), newStatus);
       
   185 	if	(error != KErrNone)
       
   186 		return error;
       
   187 
       
   188 	// Refetch alarm from server (will update status)
       
   189 	error = iAlmSvr.GetAlarmDetails(alarm->Id(), *alarm);
       
   190 	if	(error == KErrNone)
       
   191 		alarm->ClientFlags().Set(EClockAlarmFlagsIsSet);
       
   192 	return error;
       
   193 	}
       
   194 
       
   195 TInt CAlmModel::ClockAlarmSet(TInt aAlarmIndex, const TASShdAlarm& aInfo)
       
   196 //
       
   197 //	Set a clock alarm
       
   198 //
       
   199 	{
       
   200 	__ASSERT_DEBUG(aAlarmIndex < KMaxClockAlarms, Panic(EModelOutOfRange));
       
   201 	//
       
   202 	TInt error = KErrNone;
       
   203 	TASShdAlarm* alarm = iClockAlarms[aAlarmIndex];
       
   204 	if	(alarm->Id() != KNullAlarmId)
       
   205 		{
       
   206 		// There is already an alarm set in this slot. Delete
       
   207 		// the old, replace it with the new.
       
   208 		error = iAlmSvr.AlarmDelete(alarm->Id());
       
   209 		if	(error != KErrNone)
       
   210 			return error;
       
   211 		alarm->ClientFlags().Clear(EClockAlarmFlagsIsSet);
       
   212 		}
       
   213 
       
   214 	// Add alarm (will also update cached alarm details)
       
   215 	*alarm = aInfo;
       
   216 	alarm->Category() = KASCliCategoryClock;
       
   217 	error = iAlmSvr.AlarmAdd(*alarm);
       
   218 	if	(error == KErrNone)
       
   219 		alarm->ClientFlags().Set(EClockAlarmFlagsIsSet);
       
   220 	return error;
       
   221 	}
       
   222 
       
   223 TInt CAlmModel::ClockAlarmInfo(TInt aAlarmIndex,TASShdAlarm& aInfo)
       
   224 //
       
   225 //	Get information about a clock alarm
       
   226 //
       
   227 	{
       
   228 	__ASSERT_DEBUG(aAlarmIndex < KMaxClockAlarms, Panic(EModelOutOfRange));
       
   229 	//
       
   230 	TInt error = KErrNone;
       
   231 	aInfo = *(iClockAlarms[aAlarmIndex]);
       
   232 	if	(aInfo.Id() != KNullAlarmId && aInfo.ClientFlags().IsSet(EClockAlarmFlagsIsSet))
       
   233 		{
       
   234 		// Preserve flags
       
   235 		const TBitFlags16 flags = aInfo.ClientFlags();
       
   236 
       
   237 		// Fetch alarm details
       
   238 		error = iAlmSvr.GetAlarmDetails(aInfo.Id(), aInfo);
       
   239 
       
   240 		// Restore flags
       
   241 		if	(error == KErrNone)
       
   242 			{
       
   243 			// Has the alarm already notified?
       
   244 			if	(aInfo.State() != EAlarmStateQueued && aInfo.State() != EAlarmStateNotifying)
       
   245 				{
       
   246 				// Treat this slot as free
       
   247 				TASShdAlarm* alarm = iClockAlarms[aAlarmIndex];
       
   248 				alarm->Reset();
       
   249 				aInfo = *alarm;
       
   250 				}
       
   251 			else
       
   252 				aInfo.ClientFlags() = flags;
       
   253 			}
       
   254 		else if (error == KErrNotFound)
       
   255 			{
       
   256 			// Alarm has been deleted
       
   257 			TASShdAlarm* alarm = iClockAlarms[aAlarmIndex];
       
   258 			alarm->Reset();
       
   259 			aInfo = *alarm;
       
   260 			}
       
   261 		}
       
   262 	return error;
       
   263 	}
       
   264 
       
   265 TInt CAlmModel::ClockAlarmDelete(TInt aClockAlarmNum)
       
   266 	{
       
   267 	__ASSERT_DEBUG(aClockAlarmNum < KMaxClockAlarms, Panic(EModelOutOfRange));
       
   268 	TASShdAlarm* alarm = iClockAlarms[aClockAlarmNum];
       
   269 	__ASSERT_ALWAYS(alarm->Id() != KNullAlarmId, Fault(EModelfTryingToChangeStateOfNullClockAlarm));
       
   270 	const TInt error = iAlmSvr.AlarmDelete(alarm->Id());
       
   271 	if	(error == KErrNone || error == KErrNotFound)
       
   272 		{
       
   273 		// Update cached representation of deleted alarm
       
   274 		alarm->Reset();
       
   275 		}
       
   276 	return error;
       
   277 	}
       
   278 
       
   279 TTimeWClockAlarmState CAlmModel::ClockAlarmState(TInt aClockAlarmNum) const
       
   280 	{
       
   281 	__ASSERT_DEBUG(aClockAlarmNum < KMaxClockAlarms, Panic(EModelOutOfRange));
       
   282 	//
       
   283 	const TASShdAlarm* alarm = iClockAlarms[aClockAlarmNum];
       
   284 	if	(alarm->ClientFlags().IsSet(EClockAlarmFlagsIsSet))
       
   285 		{
       
   286 		switch(alarm->Status())
       
   287 			{
       
   288 		case EAlarmStatusEnabled:
       
   289 			return ETimeWClockAlarmStateSet;
       
   290 		case EAlarmStatusDisabled:
       
   291 			return ETimeWClockAlarmStateDisabled;
       
   292 			}
       
   293 		}
       
   294 	return ETimeWClockAlarmStateNotSet;
       
   295 	}
       
   296 
       
   297 void CAlmModel::NextAlarmResetListL()
       
   298 //
       
   299 //	Fetch a list of ids of all the alarms that are pending
       
   300 //	within the alarm server.
       
   301 //
       
   302 	{
       
   303 	iAlmSvr.GetAlarmIdListByStateL(EAlarmStateQueued, iNextAlarmIds);
       
   304 	}
       
   305 
       
   306 void CAlmModel::NextAlarmClearList()
       
   307 	{
       
   308 	iNextAlarmIds.Reset();
       
   309 	}
       
   310 
       
   311 TInt CAlmModel::NextAlarmNumber() const
       
   312 	{
       
   313 	return iAlmSvr.NumberOfAlarmsActiveInQueue();
       
   314 	}
       
   315 
       
   316 TInt CAlmModel::NextAlarmDelete(TInt aIndex)
       
   317 //
       
   318 //	Delete one of the next alarms
       
   319 //
       
   320 	{
       
   321 	TInt ret = KErrNotFound;
       
   322 	if	(aIndex < iNextAlarmIds.Count())
       
   323 		{
       
   324 		const TAlarmId id = iNextAlarmIds[aIndex];
       
   325 		ret = iAlmSvr.AlarmDelete(id);
       
   326 		}
       
   327 	return ret;
       
   328 	}
       
   329 
       
   330 TBool CAlmModel::NextAlarmCanDelete(TInt aIndex) const
       
   331 //
       
   332 //	Determine whether can delete the specified alarm.
       
   333 //
       
   334 	{
       
   335 	__ASSERT_DEBUG(aIndex < iNextAlarmIds.Count(), Panic(EModelOutOfRange));
       
   336 	//
       
   337 	const TAlarmId id = iNextAlarmIds[aIndex];
       
   338 	//
       
   339 	TASShdAlarm alarm;
       
   340 	if	(iAlmSvr.GetAlarmDetails(id, alarm) == KErrNone)
       
   341 		{
       
   342 		return (
       
   343 			alarm.Category() == KASCliCategoryClock ||
       
   344 			alarm.State() == EAlarmStateSnoozed ||
       
   345 			!alarm.HasOwningSession()
       
   346 			);
       
   347 		}
       
   348 	return EFalse;
       
   349 	}
       
   350 
       
   351 TInt CAlmModel::NextAlarm(TFullName& aOwner, TASShdAlarm& aInfo) const
       
   352 //
       
   353 //	Get information about the next alarm
       
   354 //
       
   355 	{
       
   356 	aOwner = KNullDesC;
       
   357 	//
       
   358 	TAlarmId id = KNullAlarmId;
       
   359 	TInt ret = iAlmSvr.GetNextDueAlarmId(id);
       
   360 	if	(ret == KErrNone)
       
   361 		{
       
   362 		if	(id == KNullAlarmId)
       
   363 			{
       
   364 			aInfo.Reset();
       
   365 			ret = KErrNotFound;
       
   366 			}
       
   367 		else
       
   368 			{
       
   369 			ret = iAlmSvr.GetAlarmDetails(id, aInfo);
       
   370 			if	(ret == KErrNone)
       
   371 				ret = iAlmSvr.GetAlarmOwner(id, aOwner);
       
   372 			}
       
   373 		}
       
   374 	return ret;
       
   375 	}
       
   376 
       
   377 TInt CAlmModel::NextAlarmInfo(TInt aIndex, TFullName& aOwner, TASShdAlarm& aInfo) const
       
   378 //
       
   379 //	Get info about the next alarm
       
   380 //
       
   381 	{
       
   382 	TInt ret = KErrNotFound;
       
   383 	if	(aIndex < iNextAlarmIds.Count())
       
   384 		{
       
   385 		const TAlarmId id = iNextAlarmIds[aIndex];
       
   386 		ret = iAlmSvr.GetAlarmDetails(id, aInfo);
       
   387 		if	(ret == KErrNone)
       
   388 			{
       
   389 			ret = iAlmSvr.GetAlarmOwner(id, aOwner);
       
   390 			}
       
   391 		}
       
   392 	return ret;
       
   393 	}
       
   394 
       
   395 void CAlmModel::ReviewAlarmResetListL()
       
   396 	{
       
   397 	iAlmSvr.GetAlarmIdListByStateL(EAlarmStateNotified, iReviewAlarmIds);
       
   398 	}
       
   399 
       
   400 void CAlmModel::ReviewAlarmClearList()
       
   401 	{
       
   402 	iReviewAlarmIds.Reset();
       
   403 	}
       
   404 
       
   405 TInt CAlmModel::ReviewAlarmNumber() const
       
   406 	{
       
   407 	return iAlmSvr.AlarmCountByState(EAlarmStateNotified);
       
   408 	}
       
   409 
       
   410 TInt CAlmModel::ReviewAlarmInfo(TInt aIndex, TASShdAlarm& aInfo) const
       
   411 //
       
   412 //	Get information about a past alarm
       
   413 //
       
   414 	{
       
   415 	TInt ret = KErrNotFound;
       
   416 	if	(aIndex < iReviewAlarmIds.Count())
       
   417 		{
       
   418 		const TAlarmId id = iReviewAlarmIds[aIndex];
       
   419 		ret = iAlmSvr.GetAlarmDetails(id, aInfo);
       
   420 		}
       
   421 	return ret;
       
   422 	}
       
   423 
       
   424 void CAlmModel::AlarmSoundToggle()
       
   425 	{
       
   426 	TTimeWAlarmSoundState soundState = AlarmSoundState();
       
   427 	if	(soundState == ETimeWAlarmSoundOn)
       
   428 		AlarmSoundStateSet(EFalse);
       
   429 	else
       
   430 		AlarmSoundStateSet(ETrue);
       
   431 	}
       
   432 
       
   433 TInt CAlmModel::AlarmSoundStateSet(TBool aSoundOn)
       
   434 	{
       
   435 	TAlarmGlobalSoundState state = EAlarmGlobalSoundStateOn;
       
   436 	if	(!aSoundOn)
       
   437 		state = EAlarmGlobalSoundStateOff;
       
   438 
       
   439 	return iAlmSvr.SetAlarmSoundState(state);
       
   440 	}
       
   441 
       
   442 TTimeWAlarmSoundState CAlmModel::AlarmSoundState() const
       
   443 	{
       
   444 	// First check whether we're in a silent period
       
   445 	if	(iAlmSvr.AlarmSoundsTemporarilySilenced())
       
   446 		return ETimeWAlarmQuietPeriod;
       
   447 
       
   448 	// We're not, so check global sound state and map
       
   449 	// to TimeW settings
       
   450 	TAlarmGlobalSoundState state = EAlarmGlobalSoundStateOff;
       
   451 	if	(iAlmSvr.GetAlarmSoundState(state) == KErrNone)
       
   452 		{
       
   453 		if	(state == EAlarmGlobalSoundStateOn)
       
   454 			return ETimeWAlarmSoundOn;
       
   455 		}
       
   456 	return ETimeWAlarmSoundOff;
       
   457 	}
       
   458 
       
   459 TTime CAlmModel::AlarmQuietPeriodEnd() const
       
   460 	{
       
   461 	TTime endTime;
       
   462 	if	(iAlmSvr.GetAlarmSoundsSilentUntil(endTime) == KErrNone)
       
   463 		return endTime;
       
   464 	//
       
   465 	endTime.HomeTime();
       
   466 	return endTime;
       
   467 	}
       
   468 
       
   469 TInt CAlmModel::OrphanAlarmNumber() const
       
   470 	{
       
   471 	// Get the alarms
       
   472 	RArray<TAlarmId> clockIds(KMaxClockAlarms);
       
   473 	CleanupClosePushL(clockIds);
       
   474 	iAlmSvr.GetAlarmIdListForCategoryL(KASCliCategoryClock, clockIds);
       
   475 
       
   476 	//which are orphaned?
       
   477 	TInt count = clockIds.Count();
       
   478 	TInt orphaned = 0;
       
   479 	for	(TInt ii=0; ii<count; ii++)
       
   480 		{
       
   481 		TASShdAlarm alarm;
       
   482 		iAlmSvr.GetAlarmDetails(clockIds[ii], alarm);
       
   483 		if	(alarm.HasBecomeOrphaned())
       
   484 			orphaned++;
       
   485 		}
       
   486 	CleanupStack::PopAndDestroy(&clockIds);
       
   487 
       
   488 	return orphaned;
       
   489 	}