lowlevellibsandfws/apputils/src/BASSND.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 <bassnd.h>
       
    17 #include <baflpan.h>
       
    18 #include <basched.h>
       
    19 #include <bautils.h>
       
    20 #include "BaSsndStore.h"
       
    21 
       
    22 const TInt KSystemSoundArrayGranularity	=4;
       
    23 const TInt KNullFixedSequence=-1;
       
    24 
       
    25 // Full path and name of system sound file.
       
    26 _LIT(KSystemSoundFilePath,"_:\\system\\data\\syssnd.dat");
       
    27 // Buffer descriptor to hold full path and name of system sound file. 
       
    28 typedef TBuf<28> TSystemSoundFileName; // a multiple of 4
       
    29 
       
    30 _LIT(KSystemSoundDefaultSound,"z:\\system\\SystemSounds\\ring.wav");
       
    31 _LIT(KSystemSoundDefaultRing,"z:\\system\\SystemSounds\\ring.wav");
       
    32 _LIT(KSystemSoundDefaultAlarm,"z:\\system\\SystemSounds\\alarm.wav");
       
    33 _LIT(KSystemSoundDefaultMessage,"z:\\system\\SystemSounds\\message.wav");
       
    34 
       
    35 //
       
    36 // class TBaSystemSoundType
       
    37 //
       
    38 
       
    39 EXPORT_C TBaSystemSoundType::TBaSystemSoundType()
       
    40 	: iMajor(KNullUid), iMinor(KNullUid)
       
    41 /** Constructs a TBaSystemSoundType object with a null category and instance. */
       
    42 	{}
       
    43 
       
    44 
       
    45 EXPORT_C TBaSystemSoundType::TBaSystemSoundType(TBaSystemSoundUid aMajor,TUid aMinor)
       
    46 	: iMajor(aMajor), iMinor(aMinor)
       
    47 /** Constructs a TBaSystemSoundType object with the specified category and, optionally, 
       
    48 instance.
       
    49 
       
    50 @param aMajor The category, this can be a file, tone or sequence. 
       
    51 @param aMinor The instance of the category. This is optional. */
       
    52 	{}
       
    53 
       
    54 
       
    55 EXPORT_C TBool TBaSystemSoundType::operator==(const TBaSystemSoundType& aType) const
       
    56 /** Tests whether the sound's category and instance are the same as that specified.
       
    57 
       
    58 @param aType The category and instance to test.
       
    59 @return ETrue if the sound's category and instance is the same as aType, EFalse 
       
    60 otherwise. */
       
    61 	{
       
    62 	return (iMajor==aType.iMajor && iMinor==aType.iMinor);
       
    63 	}
       
    64 
       
    65 //
       
    66 // class TBaSystemSoundInfo
       
    67 //
       
    68 TBool TBaSystemSoundInfo::TTone::IsNull() const
       
    69 /** Tests whether the sound's category and instance are null.
       
    70 @internalAll
       
    71 @return    ETrue if the sound's category and  instance are null, EFalse 
       
    72 otherwise. */	
       
    73     {
       
    74 	return iFrequency==0 && iDuration.Int()==0;
       
    75 	}
       
    76 
       
    77 
       
    78 void TBaSystemSoundInfo::TTone::InternalizeL(RReadStream& aStream)
       
    79 /**
       
    80 @internalAll 
       
    81 Gets a tone's frequency and duration.
       
    82 
       
    83 @param aStream The stream from which the tone's frequency and duration is read. 
       
    84 */
       
    85 	{
       
    86 	iFrequency=aStream.ReadInt32L();
       
    87 	iDuration=aStream.ReadInt32L();
       
    88 	}
       
    89 
       
    90 
       
    91 void TBaSystemSoundInfo::TTone::ExternalizeL(RWriteStream& aStream) const
       
    92 /**
       
    93 @internalAll 
       
    94 Saves a tone's frequency and duration.
       
    95 
       
    96 @param aStream The stream to which the tone's frequency and duration is 
       
    97 written.
       
    98 */
       
    99 
       
   100 	{
       
   101 	aStream.WriteInt32L(iFrequency);
       
   102 	aStream.WriteInt32L(iDuration.Int());
       
   103 	}
       
   104 
       
   105 EXPORT_C TBaSystemSoundInfo::TBaSystemSoundInfo()
       
   106 	: iType(KNullUid,KNullUid), iVolume(KSystemSoundDefaultVolume), iPriority(KSystemSoundDefaultPriority),
       
   107 				iName(KNullDesC), iFixedSequence(KNullFixedSequence), iTone(TTone())
       
   108 /** Constructs a default TBaSystemSoundInfo object. This object has a null type, 
       
   109 default volume, default priority, null name, null fixed sequence, and tone 
       
   110 of TTone. */
       
   111 	{}
       
   112 
       
   113 EXPORT_C TBaSystemSoundInfo::TBaSystemSoundInfo(const TBaSystemSoundType& aType,const TBaSystemSoundName& aName,
       
   114 														TInt aVolume,TInt aPriority)
       
   115 	: iType(aType), iVolume(aVolume), iPriority(aPriority), iName(aName),
       
   116 				iFixedSequence(KNullFixedSequence), iTone(TTone())
       
   117 /** Constructs a TBaSystemSoundInfo object with the specified type and file name. 
       
   118 The sound has default volume and priority.
       
   119 
       
   120 @param aType Sound type
       
   121 @param aName Sound name
       
   122 @param aVolume Sound volume
       
   123 @param aPriority Sound priority */
       
   124 	{}
       
   125 
       
   126 EXPORT_C TBaSystemSoundInfo::TBaSystemSoundInfo(const TBaSystemSoundType& aType,TInt aFixedSequence,
       
   127 														TInt aVolume,TInt aPriority)
       
   128 	: iType(aType), iVolume(aVolume), iPriority(aPriority), iName(KNullDesC),
       
   129 				iFixedSequence(aFixedSequence), iTone(TTone())
       
   130 /** Constructs a TBaSystemSoundInfo object with the specified type and sequence. 
       
   131 The sound has default volume and priority.
       
   132 
       
   133 @param aType Sound type
       
   134 @param aFixedSequence Sound sequence
       
   135 @param aVolume Sound volume
       
   136 @param aPriority Sound priority */
       
   137 	{}
       
   138 
       
   139 EXPORT_C TBaSystemSoundInfo::TBaSystemSoundInfo(const TBaSystemSoundType& aType,TTone aTone,
       
   140 														TInt aVolume,TInt aPriority)
       
   141 	: iType(aType), iVolume(aVolume), iPriority(aPriority), iName(KNullDesC),
       
   142 				iFixedSequence(KNullFixedSequence), iTone(aTone)
       
   143 /** Constructs a TBaSystemSoundInfo object with the specified type and tone. The 
       
   144 sound has default volume and priority.
       
   145 
       
   146 @param aType Sound type
       
   147 @param aTone Sound tone
       
   148 @param aVolume Sound volume
       
   149 @param aPriority Sound priority */
       
   150 	{}
       
   151 
       
   152 
       
   153 EXPORT_C TBaSystemSoundInfo::TSoundCategory TBaSystemSoundInfo::SoundCategory() const
       
   154 /** Gets the sound's category.
       
   155 
       
   156 @return The system sound's category, or NULL if the sound is not a file, sequence, 
       
   157 or tone. */
       
   158 	{
       
   159 	TBaSystemSoundInfo::TSoundCategory cat=TBaSystemSoundInfo::ENull;
       
   160 	 if (!iTone.IsNull())
       
   161 		cat=TBaSystemSoundInfo::ETone;
       
   162 	else if (iName.Length())
       
   163 		cat=TBaSystemSoundInfo::EFile;
       
   164 	else if (iFixedSequence!=KNullFixedSequence)
       
   165 		cat=TBaSystemSoundInfo::ESequence;
       
   166 	return cat;
       
   167 	}
       
   168 
       
   169 
       
   170 EXPORT_C void TBaSystemSoundInfo::SetFixedSequenceNumber(TInt aNumber)
       
   171 /** Replaces the current sound with the specified sequence number.
       
   172 
       
   173 @param aNumber The sequence number with which to replace the current sound. */
       
   174 	{
       
   175 	iFixedSequence=aNumber;
       
   176 	iName=KNullDesC();
       
   177 	iTone=TTone();
       
   178 	}
       
   179 
       
   180 
       
   181 EXPORT_C void TBaSystemSoundInfo::SetFileName(const TBaSystemSoundName& aFileName)
       
   182 /** Replaces the current sound with the specified file.
       
   183 
       
   184 @param aFileName The file with which to replace the current sound. */
       
   185 	{
       
   186 	iFixedSequence=KNullFixedSequence;
       
   187 	iName=aFileName;
       
   188 	iTone=TTone();
       
   189 	}
       
   190 
       
   191 
       
   192 EXPORT_C void TBaSystemSoundInfo::SetTone(const TTone& aTone)
       
   193 /** Replaces the current sound with the specified tone.
       
   194 
       
   195 @param aTone The tone with which to replace the current sound. */
       
   196 	{
       
   197 	iFixedSequence=KNullFixedSequence;
       
   198 	iName=KNullDesC();
       
   199 	iTone=aTone;
       
   200 	}
       
   201 /** Internalises an object of this class from a read stream.
       
   202 Presence of this function means that the standard templated operator>>()
       
   203 can be used to internalise objects of this class.
       
   204 
       
   205 @param aStream The stream from which the object is to be internalised.
       
   206 @internalAll 
       
   207 */
       
   208 void TBaSystemSoundInfo::InternalizeL(RReadStream& aStream)
       
   209 	{
       
   210 	aStream >> iType.iMajor;
       
   211 	aStream >> iType.iMinor;
       
   212 	iVolume = aStream.ReadInt32L();
       
   213 	iPriority = aStream.ReadInt32L();
       
   214 
       
   215 	TSoundCategory cat = (TSoundCategory)aStream.ReadInt32L();
       
   216 	switch (cat)
       
   217 		{
       
   218 		case EFile:
       
   219 			{
       
   220 			TUint16 pathLength = aStream.ReadUint16L();
       
   221 			if (pathLength > iName.MaxLength())
       
   222 				{
       
   223 				User::Leave(KErrCorrupt);
       
   224 				}
       
   225 			TUint16* buf = const_cast<TUint16*>(iName.Ptr());
       
   226 			aStream.ReadL(buf, pathLength);
       
   227 			iName.SetLength(pathLength);
       
   228 
       
   229 			iFixedSequence=KNullFixedSequence;
       
   230 			iTone=TTone();
       
   231 			}
       
   232 			break;
       
   233 
       
   234 		case ESequence:
       
   235 			iFixedSequence = aStream.ReadInt32L();
       
   236 			iName=KNullDesC;
       
   237 			iTone=TTone();
       
   238 			break;
       
   239 
       
   240 		case ETone:
       
   241 			aStream >> iTone;
       
   242 			iName=KNullDesC;
       
   243 			iFixedSequence=KNullFixedSequence;
       
   244 			break;
       
   245 
       
   246 		default:
       
   247 			User::Leave(KErrCorrupt);
       
   248 		}
       
   249 	}
       
   250 
       
   251 /** Externalises an object of this class to a write stream.
       
   252 The presence of this function means that the standard templated operator<<() 
       
   253 can be used to externalise objects of this class. 
       
   254 
       
   255 @param aStream Stream to which the object should be externalised.
       
   256 */
       
   257 void TBaSystemSoundInfo::ExternalizeL(RWriteStream& aStream) const
       
   258 	{
       
   259 	aStream << iType.iMajor;
       
   260 	aStream << iType.iMinor;
       
   261 	aStream.WriteInt32L(iVolume);
       
   262 	aStream.WriteInt32L(iPriority);
       
   263 
       
   264 	TSoundCategory cat = SoundCategory();
       
   265 	aStream.WriteInt32L((TInt32)cat);
       
   266 
       
   267 	switch (cat)
       
   268 		{
       
   269 		case TBaSystemSoundInfo::EFile:
       
   270 			{
       
   271 			// Do it this way so that in InternalizeL we can check
       
   272 			// for data corruption.
       
   273 			TUint16 pathLength = iName.Length();
       
   274 			aStream.WriteUint16L(pathLength);
       
   275 			aStream.WriteL(iName.Ptr(), pathLength);
       
   276 			}
       
   277 			break;
       
   278 		case TBaSystemSoundInfo::ESequence:
       
   279 			aStream.WriteInt32L( iFixedSequence );
       
   280 			break;
       
   281 		case TBaSystemSoundInfo::ETone:
       
   282 			aStream << iTone;
       
   283 			break;
       
   284 		default:
       
   285 			// object not initialized
       
   286 			User::Leave(KErrCorrupt);
       
   287 		}
       
   288 	}
       
   289 
       
   290 //
       
   291 // class BaSystemSound
       
   292 //
       
   293 
       
   294 EXPORT_C TInt BaSystemSound::GetSound(RFs& /* aFsSession */,const TBaSystemSoundType& aType,TBaSystemSoundInfo& aInfo)
       
   295 /** Gets sound information from storage.
       
   296 
       
   297 @param aFsSession unused parameter kept for compatibility reason.
       
   298 @param aType unique identifier of the sound. aType encapsulates a major
       
   299 and optionally a minor UID. The major UID specifes the sound category
       
   300 while the minor UID specifies an instance of the category. 
       
   301 @param aInfo On return, the sound.
       
   302 @return KErrNone if successful, or one of the system wide errors if 
       
   303 unsuccessful. */
       
   304 	{ // static
       
   305 	CBaSsndStore* soundStore(NULL);
       
   306 	TRAPD(err, soundStore = CBaSsndStore::NewL() );
       
   307 
       
   308 	if (err == KErrNone)
       
   309 		{
       
   310 		err = soundStore->GetSound(aType, aInfo);
       
   311 
       
   312 		if (err == KErrNotFound && aType.iMinor != KNullUid)
       
   313 			{
       
   314 			TBaSystemSoundType tmpType(aType.iMajor);
       
   315 			err = soundStore->GetSound(tmpType, aInfo);
       
   316 			}
       
   317 
       
   318 		delete soundStore; // finish with the pointer
       
   319 		} // CBaSsndStore::NewL is successful
       
   320 
       
   321 	// err can be from CBaSsndStore::NewL or CBaSsndStore::GetSound
       
   322 	if (err != KErrNone)
       
   323 		{
       
   324 		aInfo.SetFileName( DefaultSound(aType.iMajor) );
       
   325 		aInfo.iVolume=KSystemSoundDefaultVolume;
       
   326 		aInfo.iPriority=KSystemSoundDefaultPriority;
       
   327 		}
       
   328 
       
   329 	return err;
       
   330 	}
       
   331 
       
   332 
       
   333 EXPORT_C void BaSystemSound::SetSoundL(RFs& /* aFsSession */,const TBaSystemSoundInfo& aInfo)
       
   334 /** Adds the specified sound to the system sound table if it does not 
       
   335 already exist in the table. If the sound already exists then overwrite
       
   336 the entry in the sound table.
       
   337 
       
   338 @param aFsSession unused parameter kept for backward compatibility.
       
   339 @param aInfo The sound to add to the system sound table. 
       
   340 @capability WriteDeviceData Note only clients with WriteDeviceData capability can sucessfully 
       
   341 call this API. 
       
   342 @leave KErrPermissionDenied caller does not have WriteDeviceData capability.
       
   343 @leave KErrArgument if the sound has not been initialized.
       
   344 Other system-wide error codes may also be returned.
       
   345 */
       
   346 	{ // static
       
   347 	TBaSystemSoundInfo::TSoundCategory cat = aInfo.SoundCategory();
       
   348 	if (cat == TBaSystemSoundInfo::ENull)
       
   349 		{
       
   350 		User::Leave(KErrArgument);
       
   351 		}
       
   352 
       
   353 	//check that the name includes a path
       
   354 #ifdef _DEBUG
       
   355 	if (cat == TBaSystemSoundInfo::EFile)
       
   356 		{
       
   357 		TFileName fullpath = BaflUtils::DriveAndPathFromFullName(
       
   358 			aInfo.FileName() );
       
   359 		if (fullpath.Length()==0)
       
   360 			{
       
   361 			Panic(EBafPanicSystemSoundNoPath);
       
   362 			}
       
   363 		}
       
   364 #endif
       
   365 	CBaSsndStore* ssndStore = CBaSsndStore::NewL();
       
   366 	CleanupStack::PushL(ssndStore);
       
   367 	ssndStore->SetSoundL(aInfo);
       
   368 	CleanupStack::PopAndDestroy(ssndStore);
       
   369 	}
       
   370 
       
   371 /**
       
   372 Returns the system sound filename.
       
   373 
       
   374 This function is now deprecated. System sounds are now stored in Central Repository.
       
   375 The function still returns the file name, but the file is no longer read from nor written to.
       
   376 There is no guarantee that the file exists.
       
   377 
       
   378 Client code which uses the filename for backup and restore
       
   379 purposes should be removed. Central Repository handles backup and
       
   380 restore of system sound settings.
       
   381 
       
   382 Client code that uses the filename for change notification
       
   383 must use KSystemSoundRepositoryUID to connect to Central Repository and
       
   384 must register for group notification on system sound repository
       
   385 changes. For example, if you used code such as:
       
   386 @code
       
   387 iFs.Connect() ;
       
   388 TFileName soundFileName = BaSystemSound ::SystemSoundFile();
       
   389 iFs.NotifyChange(ENotifyAll, iRequestStatus, soundFileName);
       
   390 @endcode
       
   391 you would now use:
       
   392 @code
       
   393 const Tuint32 KDoNotCareValue = 0 ;
       
   394 const Tuint32 KAllMask = 0 ;
       
   395 iRepository = CRepository ::NewL(KBaSsndRepositoryUid);
       
   396 iRepository.NotifyRequest(KDoNotCareValue, KAllMask, iRequestStatus);
       
   397 @endcode
       
   398 
       
   399 @see CRepository::NotifyRequest()
       
   400 @deprecated
       
   401 */
       
   402 EXPORT_C TFileName BaSystemSound::SystemSoundFile()
       
   403 	{ // static
       
   404 	TSystemSoundFileName filename(KSystemSoundFilePath);
       
   405 	filename[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
       
   406 	return filename;
       
   407 	}
       
   408 
       
   409 TBaSystemSoundName BaSystemSound::DefaultSound(TBaSystemSoundUid aSSUid)
       
   410 	{ // static
       
   411 	TBaSystemSoundName name;
       
   412 	if (aSSUid==KSystemSoundRingUID )
       
   413 		name=KSystemSoundDefaultRing;
       
   414 	else if (aSSUid==KSystemSoundAlarmUID )
       
   415 		name=KSystemSoundDefaultAlarm;
       
   416 	else if (aSSUid==KSystemSoundMessageUID )
       
   417 		name=KSystemSoundDefaultMessage;
       
   418 	else
       
   419 		name=KSystemSoundDefaultSound;
       
   420 	return name;
       
   421 	}
       
   422 
       
   423 //
       
   424 // class CBaSystemSoundArray
       
   425 //
       
   426 
       
   427 /**
       
   428 	@internalAll*/ 
       
   429 CBaSystemSoundArray::CBaSystemSoundArray()
       
   430 	: iSystemSounds(KSystemSoundArrayGranularity)
       
   431 	
       
   432 	{}
       
   433 
       
   434 /**
       
   435 	@internalAll*/
       
   436 CBaSystemSoundArray::~CBaSystemSoundArray()
       
   437 	{	
       
   438 	iSystemSounds.Reset();
       
   439 	}
       
   440 
       
   441 EXPORT_C CBaSystemSoundArray* CBaSystemSoundArray::NewL()
       
   442 	{
       
   443 	CBaSystemSoundArray* self=new(ELeave) CBaSystemSoundArray;
       
   444 	return self;
       
   445 	}
       
   446 EXPORT_C CBaSystemSoundArray* CBaSystemSoundArray::NewLC()
       
   447 	{
       
   448 	CBaSystemSoundArray* self=CBaSystemSoundArray::NewL();
       
   449 	CleanupStack::PushL(self);
       
   450 	return self;
       
   451 	}
       
   452 
       
   453 /** Retrieves all instances of a sound category.
       
   454 @param aFsSession unused parameter kept for backward compatibility
       
   455 @param aSSuid specifies the sound category to retrieve.
       
   456 */
       
   457 EXPORT_C void CBaSystemSoundArray::RestoreL(RFs& /* aFsSession */,TBaSystemSoundUid aSSUid)
       
   458 	{
       
   459 	iUid=aSSUid;
       
   460 	iSystemSounds.Reset();
       
   461 
       
   462 	CBaSsndStore* ssndStore = CBaSsndStore::NewL();
       
   463 	CleanupStack::PushL(ssndStore);
       
   464 	User::LeaveIfError( ssndStore->GetSoundCategory(aSSUid, iSystemSounds) );
       
   465 	CleanupStack::PopAndDestroy(ssndStore);
       
   466 	}
       
   467 
       
   468 EXPORT_C TBaSystemSoundInfo CBaSystemSoundArray::At(TInt aIndex)
       
   469 	{
       
   470 	return iSystemSounds[aIndex];
       
   471 	}
       
   472 
       
   473 EXPORT_C TInt CBaSystemSoundArray::Count()
       
   474 	{
       
   475 	return iSystemSounds.Count();
       
   476 	}
       
   477 
       
   478 //
       
   479 // class TBaSoundPriorityBase
       
   480 //
       
   481 
       
   482 const TInt KPrefEpocMask		=0x0000FF00;
       
   483 const TInt KPrefDeviceMask		=0xFFFF0000;
       
   484 
       
   485 EXPORT_C TMdaPriorityPreference TBaSoundPriorityBase::PriorityPreference() const
       
   486 	{
       
   487 	TInt pref=0;
       
   488 	pref=(iPriority&KPrefEpocMask)>>8;
       
   489 	pref|=(iPriority&KPrefDeviceMask);
       
   490 	return static_cast<TMdaPriorityPreference>(pref);
       
   491 	}
       
   492 
       
   493 /**
       
   494 	@internalAll*/ 
       
   495 void TBaSoundPriorityBase::Set(TInt aPriority,TMdaPriorityPreference aPriorityPreference)
       
   496 	
       
   497 {
       
   498 	__ASSERT_ALWAYS(aPriority>=EMdaPriorityMin && aPriority<=EMdaPriorityMax,Panic(EBafPanicInvalidSoundPriority));
       
   499 	iPriority=0;
       
   500 	*(TInt8*)&iPriority=(TInt8)aPriority;
       
   501 	iPriority|=((aPriorityPreference<<8)&KPrefEpocMask);
       
   502 	iPriority|=(aPriorityPreference&KPrefDeviceMask);
       
   503 	}
       
   504 
       
   505 //
       
   506 // class TBaSoundPriorityEncoder
       
   507 //
       
   508 
       
   509 EXPORT_C TBaSoundPriorityEncoder::TBaSoundPriorityEncoder(TInt aPriority,TMdaPriorityPreference aPriorityPreference)
       
   510 	{
       
   511 	Set(aPriority,aPriorityPreference);
       
   512 	}