commonappservices/alarmserver/Server/Source/ASSrvSoundSettings.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1999-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 "ASSrvSoundSettings.h"
       
    17 
       
    18 
       
    19 // User includes
       
    20 #include "ASSrvStaticUtils.h"
       
    21 #include "ASSrvServerWideData.h"
       
    22 #include "ASSrvAnyEventManager.h"
       
    23 
       
    24 #include <alarmserver.rsg>
       
    25 #include "AlarmServer.hrh"
       
    26 
       
    27 // System includes
       
    28 #include <barsc.h> 
       
    29 #include <barsread.h>
       
    30 
       
    31 // Type definitions
       
    32 
       
    33 // Constants
       
    34 const TInt KAlarmSoundOffsetsGranularity = 14;
       
    35 #define KAlarmCycleStartOffsetMinutes {0, 1, 2, 3, 5, 10, 20, 30, 45, 60, 90, 120, 180, 240, KErrNotFound };
       
    36 
       
    37 // Enumerations
       
    38 
       
    39 // Classes referenced
       
    40 
       
    41 
       
    42 //
       
    43 // ----> CASSrvSoundSettings (source)
       
    44 //
       
    45 
       
    46 //*************************************************************************************
       
    47 CASSrvSoundSettings::CASSrvSoundSettings(CASSrvServerWideData& aServerWideData)
       
    48 :	iServerWideData(aServerWideData)
       
    49 	,iRepeatSetting(EAlarmSoundRepeatSettingLoop)
       
    50 	{
       
    51 	}
       
    52 
       
    53 
       
    54 //*************************************************************************************
       
    55 CASSrvSoundSettings::~CASSrvSoundSettings()
       
    56 	{
       
    57 	if	(iSoundPlayIntervals)
       
    58 		iSoundPlayIntervals->Close();
       
    59 	delete iSoundPlayIntervals;
       
    60 	if	(iNewSoundPlayIntervals)
       
    61 		{
       
    62 		iNewSoundPlayIntervals->Close();
       
    63 		delete iNewSoundPlayIntervals;
       
    64 		}
       
    65 	if	(iSettingsObservers)
       
    66 		iSettingsObservers->Close();
       
    67 	delete iSettingsObservers;
       
    68 	}
       
    69 
       
    70 
       
    71 //*************************************************************************************
       
    72 void CASSrvSoundSettings::ConstructL()
       
    73 	{
       
    74 	iSettingsObservers = new(ELeave) RPointerArray<MASSrvSoundSettingsObserver>(2);
       
    75 	iSoundPlayIntervals = new(ELeave) RArray<TASSrvAlarmSoundDetails>(KAlarmSoundOffsetsGranularity);
       
    76 	//
       
    77 	SetSoundIntervalsToDefaultValuesL();
       
    78 	}
       
    79 
       
    80 
       
    81 //*************************************************************************************
       
    82 CASSrvSoundSettings* CASSrvSoundSettings::NewL(CASSrvServerWideData& aServerWideData)
       
    83 	{
       
    84 	CASSrvSoundSettings* self = new(ELeave) CASSrvSoundSettings(aServerWideData);
       
    85 	CleanupStack::PushL(self);
       
    86 	self->ConstructL();
       
    87 	CleanupStack::Pop(self);
       
    88 	return self;
       
    89 	}
       
    90 
       
    91 
       
    92 //
       
    93 //
       
    94 //
       
    95 
       
    96 /**
       
    97 This function changes the sound interval sequence. The default sequence is specified by
       
    98 KAlarmCycleStartOffsetMinutes. alarmserver.rsc takes precedence over the default. 
       
    99 alarmserver.ini takes precedence over alarmserver.rsc. If this function is invoked, the
       
   100 new values are persisted in alarmserver.ini.
       
   101 
       
   102 A zero-length interval list means that CASSrvSoundSettings should be disabled. This function
       
   103 may not be used to set a zero-length interval array; only alarmserver.rsc may do that.
       
   104 If CASSrvSoundSettings is disabled due to that alarmserver.rsc configuration, or if a NULL 
       
   105 pointer or zero-length array is passed to this function, the function will
       
   106 make no changes and return immediately.
       
   107 
       
   108 @param aIntervalsToTakeOwnershipOf An RArray of TASSrvAlarmSoundDetails
       
   109 @return KErrNotSupport If CASSrvSoundSettings is disabled.
       
   110 */
       
   111 TInt CASSrvSoundSettings::SetSoundIntervals(RArray<TASSrvAlarmSoundDetails>* aIntervalsToTakeOwnershipOf)
       
   112 	{
       
   113 	// Check to prevent change between enabled & disabled
       
   114 	if (iSoundPlayIntervals->Count() == 0)
       
   115 		{
       
   116 		// Don't cleanup aIntervalsToTakeOwnershipOf,
       
   117 		// cause it's still on the Session's cleanup stack.
       
   118 		return KErrNotSupported;
       
   119 		}
       
   120 	else if ((EAlarmSoundRepeatSettingRepeatLast == iRepeatSetting) &&
       
   121 			 (iSoundPlayIntervals->Count() < 2 ))
       
   122 		{
       
   123 		return KErrArgument;
       
   124 		}
       
   125 	
       
   126 	iSoundPlayIntervals->Close();
       
   127 	delete iSoundPlayIntervals;
       
   128 	iSoundPlayIntervals = aIntervalsToTakeOwnershipOf;
       
   129 
       
   130 	// Notify observers
       
   131 	NotifyEvent(MASSrvSoundSettingsObserver::ESoundSettingsEventSoundIntervals);
       
   132 
       
   133 	// Notify change
       
   134 	ServerData().AnyEventManager().MASAnyEventManagerObserverNotifyChanges(EAlarmChangeEventPlayIntervalsChanged, KNullAlarmId);
       
   135 	return KErrNone;
       
   136 	}
       
   137 
       
   138 //*************************************************************************************
       
   139 TInt CASSrvSoundSettings::SoundIntervalCount() const
       
   140 	{
       
   141 	return iSoundPlayIntervals->Count();
       
   142 	}
       
   143 
       
   144 
       
   145 //*************************************************************************************
       
   146 const TASSrvAlarmSoundDetails& CASSrvSoundSettings::SoundInterval(TInt aIndex) const
       
   147 	{
       
   148 	__ASSERT_ALWAYS(aIndex >=0 && aIndex < SoundIntervalCount(), ASSrvStaticUtils::Fault(ASSrvStaticUtils::EASSrvFaultBadSoundIntervalIndex));
       
   149 	return (*iSoundPlayIntervals)[aIndex];
       
   150 	}
       
   151 
       
   152 
       
   153 //*************************************************************************************
       
   154 TTime CASSrvSoundSettings::SoundIntervalTime(const TTime& aStartTime, TInt aIndex, TSoundCyclePosition aPosition) const
       
   155 	{
       
   156 	TTime finishTime(aStartTime);
       
   157 	//
       
   158 	const TASSrvAlarmSoundDetails& interval = SoundInterval(aIndex);
       
   159 
       
   160 	// Add on the offset from the start of the cycle. This gets us the starting time
       
   161 	// for a sound playback interval.
       
   162 	finishTime += TTimeIntervalMinutes(interval.OffsetInMinutes());
       
   163 	if	(aPosition == ESoundCyclePositionAtEnd)
       
   164 		{
       
   165 		// If the end time is required, we need to add on the duration
       
   166 		finishTime += TTimeIntervalSeconds(interval.DurationInSeconds());
       
   167 		}
       
   168 
       
   169 	return finishTime;
       
   170 	}
       
   171 
       
   172 
       
   173 //*************************************************************************************
       
   174 void CASSrvSoundSettings::SetGlobalSoundState(TAlarmGlobalSoundState aState)
       
   175 	{
       
   176 	if	(aState == iGlobalSoundState)
       
   177 		return;
       
   178 
       
   179 	iGlobalSoundState = aState;
       
   180 
       
   181 	// Notify observers
       
   182 	NotifyEvent(MASSrvSoundSettingsObserver::ESoundSettingsEventGlobalSoundState);
       
   183 
       
   184 	// Notify change
       
   185 	ServerData().AnyEventManager().MASAnyEventManagerObserverNotifyChanges(EAlarmChangeEventGlobalSoundStateChanged, KNullAlarmId);
       
   186 	}
       
   187 
       
   188 
       
   189 //*************************************************************************************
       
   190 void CASSrvSoundSettings::NotifySoundSettingsChangeL(MASSrvSoundSettingsObserver& aObserver)
       
   191 	{
       
   192 	User::LeaveIfError(iSettingsObservers->InsertInAddressOrder(&aObserver));
       
   193 	}
       
   194 
       
   195 
       
   196 //*************************************************************************************
       
   197 void CASSrvSoundSettings::NotifySoundSettingsChangeCancel(MASSrvSoundSettingsObserver& aObserver)
       
   198 	{
       
   199 	TInt index = KErrNotFound;
       
   200 	const TInt error = iSettingsObservers->FindInAddressOrder(&aObserver, index);
       
   201 	if	(error != KErrNotFound)
       
   202 		iSettingsObservers->Remove(index);
       
   203 	}
       
   204 
       
   205 
       
   206 //
       
   207 //
       
   208 //
       
   209 
       
   210 
       
   211 //*************************************************************************************
       
   212 void CASSrvSoundSettings::InternalizeL(RReadStream& aStream)
       
   213 	{
       
   214 	InternalizeSoundIntervalsL(aStream);
       
   215 	}
       
   216 
       
   217 
       
   218 //*************************************************************************************
       
   219 void CASSrvSoundSettings::InternalizeSoundIntervalsL(RReadStream& aStream)
       
   220 	{
       
   221 	RArray<TASSrvAlarmSoundDetails>* newIntervals = ASSrvStaticUtils::InternalizeSoundPlayIntervalsLC(aStream);
       
   222 	iNewSoundPlayIntervals = newIntervals;
       
   223 	CleanupStack::Pop(); // newIntervals
       
   224 	}
       
   225 
       
   226 
       
   227 //*************************************************************************************
       
   228 void CASSrvSoundSettings::ApplyInternalizedData(TBool aUseNewData)
       
   229 	{
       
   230 	if (aUseNewData)
       
   231 		{
       
   232 		SetSoundIntervals(iNewSoundPlayIntervals);
       
   233 		}
       
   234 	else if (iNewSoundPlayIntervals)
       
   235 		{
       
   236 		// throw away any buffered data
       
   237 		iNewSoundPlayIntervals->Close();
       
   238 		delete iNewSoundPlayIntervals;
       
   239 		}
       
   240 	// the buffer is no longer valid
       
   241 	iNewSoundPlayIntervals = NULL;
       
   242 	}
       
   243 
       
   244 
       
   245 //*************************************************************************************
       
   246 void CASSrvSoundSettings::ExternalizeL(RWriteStream& aStream) const
       
   247 	{
       
   248 	ExternalizeSoundIntervalsL(aStream);
       
   249 	}
       
   250 
       
   251 
       
   252 //*************************************************************************************
       
   253 void CASSrvSoundSettings::ExternalizeSoundIntervalsL(RWriteStream& aStream) const
       
   254 	{
       
   255 	const TInt count = iSoundPlayIntervals->Count();
       
   256 	aStream.WriteInt32L(count);
       
   257 	//
       
   258 	for(TInt i=0; i<count; i++)
       
   259 		{
       
   260 		const TASSrvAlarmSoundDetails interval = (*iSoundPlayIntervals)[i];
       
   261 		aStream << interval;
       
   262 		}
       
   263 	}
       
   264 
       
   265 
       
   266 //
       
   267 //
       
   268 //
       
   269 
       
   270 
       
   271 //*************************************************************************************
       
   272 void CASSrvSoundSettings::SetSoundIntervalsToDefaultValuesL()
       
   273 	{
       
   274 	// We first try to get the Sound Intervals from the resource file.
       
   275 	// If, for some reason, it fails, we fall back to use the default,
       
   276 	// hard-coded values as before.
       
   277 	TRAPD(err, GetSoundIntervalsFromResourceFileL() );
       
   278 	if( err != KErrNone )
       
   279 		{
       
   280 		// use hardcoded defaults
       
   281 		const TInt cycles[] = KAlarmCycleStartOffsetMinutes;
       
   282 		iSoundPlayIntervals->Reset();
       
   283 		
       
   284 		TInt i = 0;
       
   285 		FOREVER
       
   286 			{
       
   287 			const TInt offset = cycles[i++];
       
   288 			if	(offset == KErrNotFound)
       
   289 				break;
       
   290 
       
   291 			TASSrvAlarmSoundDetails soundInterval(offset, KDefaultSoundPlayDurationInSeconds);
       
   292 			User::LeaveIfError(iSoundPlayIntervals->Append(soundInterval));
       
   293 			}
       
   294 		}
       
   295 	}
       
   296 //*************************************************************************************
       
   297 void CASSrvSoundSettings::GetSoundIntervalsFromResourceFileL()
       
   298 	{
       
   299 	iSoundPlayIntervals->Reset();
       
   300 	// Reading the intervals and durations from the resource file is 
       
   301 	// usually a waste of time, since the .ini backup file normally
       
   302 	// exists after the first run. However, if other information is 
       
   303 	// stored in the resource file as well we have to open it anyway.
       
   304 	// To check for the .ini file, use ASSrvStaticUtils::GetServerPathL.
       
   305 	// See also CASSrvAlarmStore::OpenStoreL.
       
   306 
       
   307 	RFs fs;
       
   308 	User::LeaveIfError(fs.Connect());
       
   309 	CleanupClosePushL(fs);
       
   310 
       
   311 	// Read resource file if it exists
       
   312 	RResourceFile resource;
       
   313 
       
   314 	#ifdef __WINS__ 
       
   315 		_LIT(KAlarmSvrResourceFile, "c:\\Private\\101F5027\\AlarmServer.rsc");
       
   316 	#else
       
   317 		_LIT(KAlarmSvrResourceFile, "z:\\Private\\101F5027\\AlarmServer.rsc");
       
   318 	#endif // __WINS__
       
   319 
       
   320 	resource.OpenL(fs, KAlarmSvrResourceFile);
       
   321 	CleanupClosePushL(resource);
       
   322 	resource.ConfirmSignatureL(0);
       
   323 
       
   324 
       
   325 	HBufC8* intervalBuf = resource.AllocReadL(SOUND_CONTROLLER);
       
   326 	CleanupStack::PushL(intervalBuf);
       
   327 		
       
   328 	TResourceReader reader;
       
   329 	reader.SetBuffer(intervalBuf);
       
   330 
       
   331 	// Read the Sound Settings
       
   332 	TUint16 repeatSetting = reader.ReadInt16();
       
   333 	if ((repeatSetting != EAlarmSoundRepeatSettingLoop) &&
       
   334 		(repeatSetting != EAlarmSoundRepeatSettingRepeatLast) &&
       
   335 		(repeatSetting != EAlarmSoundRepeatSettingStop) )
       
   336 		{
       
   337 		User::Leave(KErrArgument);
       
   338 		}
       
   339 	iRepeatSetting = static_cast<TAlarmSoundRepeatSetting>(repeatSetting);
       
   340 
       
   341 	// Read the Sound Intervals
       
   342 	TInt16 numEntries = reader.ReadInt16();
       
   343 	for(TInt i=0; i<numEntries; i++)
       
   344 		{
       
   345 		TInt16 offset = reader.ReadInt16();
       
   346 		TInt16 duration = reader.ReadInt16();
       
   347 
       
   348 		TASSrvAlarmSoundDetails soundInterval(offset, duration);
       
   349 		User::LeaveIfError(iSoundPlayIntervals->Append(soundInterval));
       
   350 		}
       
   351 
       
   352 	CleanupStack::PopAndDestroy(intervalBuf);
       
   353 	CleanupStack::PopAndDestroy(&resource);
       
   354 	CleanupStack::PopAndDestroy(&fs);
       
   355 
       
   356 	// Check the sound play intervals are valid
       
   357 	if (iSoundPlayIntervals->Count() > 0)
       
   358 		{
       
   359 		const TInt error = ASSrvStaticUtils::ValidateSoundPlayIntervals(*iSoundPlayIntervals);
       
   360 		User::LeaveIfError(error);
       
   361 		}
       
   362 	else if ((EAlarmSoundRepeatSettingRepeatLast == iRepeatSetting) &&
       
   363 			 (iSoundPlayIntervals->Count() < 2 ))
       
   364 		{
       
   365 		// We need at least 2 intervals to determine the length of the last
       
   366 		// interval, especially since the first interval's Offset = 0.
       
   367 		User::Leave(KErrArgument);
       
   368 		}
       
   369 	}
       
   370 
       
   371 
       
   372 //*************************************************************************************
       
   373 void CASSrvSoundSettings::NotifyEvent(MASSrvSoundSettingsObserver::TSoundSettingsEvent aEvent)
       
   374 	{
       
   375 	// Cascade changes to observers
       
   376 	const TInt count = iSettingsObservers->Count();
       
   377 	for(TInt i=0; i<count; i++)
       
   378 		{
       
   379 		(*iSettingsObservers)[i]->MASSoundSettingsHandleChangeEvent(aEvent);
       
   380 		}
       
   381 	}
       
   382 
       
   383 //*************************************************************************************
       
   384 void CASSrvSoundSettings::ClearSoundPauseFlag()
       
   385 	{
       
   386 	NotifyEvent(MASSrvSoundSettingsObserver::ESoundSettingsClearPauseFlag);
       
   387 	}
       
   388