internetradio2.0/favoritesdbsrc/irfavoritesdb.cpp
changeset 14 896e9dbc5f19
parent 12 608f67c22514
child 15 065198191975
equal deleted inserted replaced
12:608f67c22514 14:896e9dbc5f19
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "pspresetnotifier.h"
       
    20 #include "pstransaction.h"
       
    21 #include "irdebug.h"
       
    22 #include "irfavoritesdb.h"
       
    23 #include "irisdspreset.h"
       
    24 #include "irpreset.h"
       
    25 #include "irsettings.h"
       
    26 
       
    27 const TInt KNullId = 0;
       
    28 const TInt KNineteen = 19;
       
    29 
       
    30 namespace PresetHandler
       
    31 {
       
    32 //-------------------------------------------------------------------------------
       
    33 //TInt OrderByIndex( const CIRPresetImpl& aPreset1, const CIRPresetImpl& aPreset2 )
       
    34 //-------------------------------------------------------------------------------
       
    35 //
       
    36     //Sort algorithm
       
    37     // Orders presets by their index.
       
    38     //
       
    39     TInt OrderByIndex( const CIRPreset& aPreset1, const CIRPreset& aPreset2 )
       
    40         {
       
    41         if ( aPreset1.Index() < aPreset2.Index() )
       
    42             {
       
    43             return -1;
       
    44             }
       
    45         else if ( aPreset1.Index() > aPreset2.Index() )
       
    46             {
       
    47             return 1;
       
    48             }
       
    49         else
       
    50             {
       
    51             return 0;
       
    52             }
       
    53         }
       
    54     
       
    55     TInt OrderByPlayedTimes(const CIRPreset& aPreset1, const CIRPreset& aPreset2)
       
    56     {
       
    57         int difference = aPreset1.GetPlayedTimes() - aPreset2.GetPlayedTimes();
       
    58         if (difference > 0)
       
    59         {
       
    60             return -1;
       
    61         }
       
    62         else if (difference == 0)
       
    63         {
       
    64             return aPreset1.Name().Compare(aPreset2.Name());
       
    65         }
       
    66         else 
       
    67         {
       
    68             return 1;
       
    69         }
       
    70     }
       
    71 }
       
    72 
       
    73 //---------------------------------------------------------------------------
       
    74 //CIRFavoritesDb::NewL()
       
    75 //standard symbian 1st phase constructor
       
    76 //---------------------------------------------------------------------------
       
    77 //
       
    78 
       
    79 EXPORT_C CIRFavoritesDb* CIRFavoritesDb::NewL()
       
    80     {
       
    81 	IRLOG_DEBUG( "CIRFavoritesDb::NewL" );
       
    82     CIRFavoritesDb* self = new ( ELeave ) CIRFavoritesDb;
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop( self );
       
    86 	IRLOG_DEBUG( "CIRFavoritesDb::NewL - Exiting." );
       
    87     return self;
       
    88     }
       
    89 
       
    90 
       
    91 //---------------------------------------------------------------------------
       
    92 //CIRFavoritesDb::~CIRFavoritesDb()
       
    93 //standard c++ destructor
       
    94 //---------------------------------------------------------------------------
       
    95 //
       
    96 
       
    97 CIRFavoritesDb::~CIRFavoritesDb()
       
    98     {
       
    99 	IRLOG_DEBUG( "CIRFavoritesDb::~CIRFavoritesDb" );
       
   100     delete iNotifier;
       
   101     iObservers.Close();
       
   102     //resets the preset array
       
   103     iFavPresetList.ResetAndDestroy();
       
   104     iFavPresetList.Close();
       
   105     iServ.Close();
       
   106 
       
   107 	IRLOG_DEBUG( "CIRFavoritesDb::~CIRFavoritesDb - Exiting." );
       
   108     }
       
   109 
       
   110 
       
   111 //---------------------------------------------------------------------------
       
   112 //CIRFavoritesDb::AddPresetL()
       
   113 //exported function to add a preset
       
   114 //---------------------------------------------------------------------------
       
   115 //
       
   116 
       
   117 EXPORT_C void CIRFavoritesDb::AddPresetL(CIRIsdsPreset& aPreset,
       
   118 			TInt& aRetVal, TBool /*aIsLogoAvialable*/)
       
   119 	{
       
   120 	IRLOG_DEBUG( "CIRFavoritesDb::AddPresetL" );
       
   121     iMoveStatus=EFalse;
       
   122     SortByIndex() ;//sorted by index
       
   123     TBool presetSaved = EFalse;
       
   124 
       
   125     // Check the existence of preset before checking if the list is full 
       
   126     // to avoid KErrNoMemory for a preset that exists in the list.
       
   127     TInt searchResult = 0;
       
   128     if (aPreset.GetChannelType() == 0)
       
   129     {
       
   130         searchResult = SearchUserDefinedPreset(aPreset.GetName(), aPreset.GetChannelUrlAtL(0));
       
   131         //we don't allow user to add presets with the same name
       
   132         if (searchResult >= 0)
       
   133         {
       
   134             ReplaceUserDefinedPresetL(aPreset);
       
   135             aRetVal = KErrAlreadyExists;
       
   136             return;
       
   137         }
       
   138     }
       
   139     else
       
   140     {
       
   141         searchResult = SearchPreset( aPreset.GetId(), KNullId );
       
   142         if( KErrNotFound !=  searchResult)
       
   143         {
       
   144             //Eventhough the preset exists in the favorites db, replace the
       
   145             //same with the new preset i.e. aPreset.  Because the db should 
       
   146             //contain the updated values of the preset.
       
   147             ReplacePresetL(aPreset);
       
   148             
       
   149             aRetVal = KErrAlreadyExists;
       
   150 
       
   151             return;
       
   152         }
       
   153     }
       
   154 
       
   155     if ( EmptyPresetCount()<=0 )
       
   156 	{
       
   157 	 	//max limit reached no more processing
       
   158 		aRetVal = KErrNoMemory;
       
   159 		return;
       
   160 	}
       
   161     
       
   162     // Found the next empty place or replace
       
   163     for ( TInt j=0; j < MaxPresetCount() && !presetSaved; j++ )
       
   164         {
       
   165         CIRPreset* preset = PresetByIndex( j );
       
   166         TBool presetExisted = preset ? ETrue : EFalse;//presetExisted=1 if preset exists
       
   167 
       
   168         if ( !preset )
       
   169             {
       
   170             preset = CreatePresetL( j );//create if preset==NULL
       
   171             }
       
   172 
       
   173         if ( !presetExisted )//crete if preset did not exists
       
   174             {
       
   175             //open a transaction with the preset server
       
   176             CPSTransaction* transaction = preset->CreateTransactionLC();
       
   177             //Convert to Plugnized Preset
       
   178             //That is accepted by the preset server
       
   179             preset->CIRIsdsPresetToCIRPresetImpl(aPreset);
       
   180             //setname of CPSPresetInterface is called
       
   181             preset->SetNameL( aPreset.GetName() );
       
   182             transaction->CommitL();
       
   183             CleanupStack::PopAndDestroy( transaction );
       
   184             //added to check multiple insertion
       
   185             presetSaved = ETrue;
       
   186             }
       
   187         }
       
   188 
       
   189     aRetVal = KErrNone;
       
   190     IRLOG_DEBUG( "CIRFavoritesDb::AddPresetL - Exiting." );
       
   191 	}
       
   192 
       
   193 //---------------------------------------------------------------------------
       
   194 //CIRFavoritesDb::AddPresetL()
       
   195 //exported,overloaded function to add a preset manually
       
   196 //converts the minimum data(name ,url) into an preset
       
   197 //---------------------------------------------------------------------------
       
   198 //
       
   199 
       
   200 EXPORT_C void CIRFavoritesDb::AddPresetL(const TDesC& aName,
       
   201 	const TDesC& aURl,TInt& aReturn, TBool /*aIsLogoAvialable*/)
       
   202 	{
       
   203 	IRLOG_DEBUG( "CIRFavoritesDb::AddPresetL(des des int int)" );
       
   204 	iMoveStatus=EFalse;
       
   205     CIRIsdsPreset* saveItem;
       
   206     saveItem = CIRIsdsPreset::NewLC();
       
   207     saveItem->SetName(aName);
       
   208 	saveItem->SetUrlCount(1);//only one url is saved for manually added channel
       
   209 	saveItem->SetChannelType(EUserDefined);
       
   210 	_LIT(KNotAvailable,"NA");
       
   211 	//bitrate set to zero so that it is accessible to all kinds of bitrate settings
       
   212 	saveItem->SetUrlL(KNotAvailable,aURl,0);
       
   213 	AddPresetL(*saveItem,aReturn);
       
   214 	CleanupStack::PopAndDestroy(saveItem);
       
   215 
       
   216 	IRLOG_DEBUG( "CIRFavoritesDb::AddPresetL(des des int int) - Exiting." );
       
   217 	}
       
   218 
       
   219 
       
   220 //---------------------------------------------------------------------------
       
   221 //CIRFavoritesDb::DeletePresetL()
       
   222 //exported, function to delete a preset
       
   223 //---------------------------------------------------------------------------
       
   224 //
       
   225 
       
   226 EXPORT_C void CIRFavoritesDb::DeletePresetL( TInt aId )
       
   227 	{
       
   228 	IRLOG_DEBUG( "CIRFavoritesDb::DeletePresetL" );
       
   229     iMoveStatus=EFalse;
       
   230 	iServ.DeletePresetL( aId);
       
   231 	
       
   232 	TInt count = iFavPresetList.Count();
       
   233 	//when we delete one preset in the server's db, we also
       
   234 	//need to delete it in the favorite list. 
       
   235 	for( TInt i=0; i<count; i++ )
       
   236 	{	     
       
   237 	    if( aId == iFavPresetList[i]->Id())
       
   238 	    {
       
   239 	    	  //before Remove, the memory should be freed ahead. 
       
   240 	        delete iFavPresetList[i];
       
   241 	        iFavPresetList.Remove(i);	         
       
   242 	        break;
       
   243 	    }
       
   244 	}
       
   245 	IRLOG_DEBUG( "CIRFavoritesDb::DeletePresetL - Exiting." );
       
   246 	}
       
   247 
       
   248 //---------------------------------------------------------------------------
       
   249 //CIRFavoritesDb::GetAllPresetL()
       
   250 //exported,function to get the list of saved presets in the array iFavPresetList
       
   251 //---------------------------------------------------------------------------
       
   252 //
       
   253 
       
   254 EXPORT_C void CIRFavoritesDb::GetAllPreset()
       
   255 	{
       
   256 	IRLOG_DEBUG( "CIRFavoritesDb::GetAllPresetL" );
       
   257 	//sort the array according to the index values
       
   258 	SortByIndex();
       
   259 	IRLOG_DEBUG( "CIRFavoritesDb::GetAllPresetL - Exiting." );
       
   260 	}
       
   261 
       
   262 /*
       
   263  * get all presets sorted by played times
       
   264  */
       
   265 EXPORT_C const RIRPresetArray& CIRFavoritesDb::GetAllSortedPresets()
       
   266 {
       
   267     iFavPresetList.Sort( TLinearOrder<CIRPreset>(PresetHandler::OrderByPlayedTimes));
       
   268     return iFavPresetList;
       
   269 }
       
   270     
       
   271 //---------------------------------------------------------------------------
       
   272 //CIRFavoritesDb::SwapPresetsInDbL()
       
   273 //returns the previous preset for a given presetId
       
   274 //---------------------------------------------------------------------------
       
   275 //
       
   276 
       
   277 EXPORT_C void CIRFavoritesDb::SwapPresetsInDbL( TInt aChannelIdFrom,
       
   278 	TInt /*aChannelIdTo*/,TInt /*aChannelIndexFro*/,TInt aChannelIndexTo )
       
   279 	{
       
   280 	IRLOG_DEBUG( "CIRFavoritesDb::SwapPresetsInDbL" );
       
   281 	MovePresetL(  aChannelIdFrom,aChannelIndexTo);
       
   282 	IRLOG_DEBUG( "CIRFavoritesDb::SwapPresetsInDbL - Exiting." );
       
   283 	}
       
   284 
       
   285 //---------------------------------------------------------------------------
       
   286 //CIRFavoritesDb::MovePresetL()
       
   287 //moves a preset to destination index
       
   288 //---------------------------------------------------------------------------
       
   289 //
       
   290 
       
   291  void CIRFavoritesDb::MovePresetL( const TInt aId, const TInt aDestinationIndex )
       
   292     {
       
   293 	IRLOG_DEBUG( "CIRFavoritesDb::MovePresetL" );
       
   294 	ASSERT( aDestinationIndex >= 0 && aDestinationIndex < iFavPresetList.Count() );
       
   295 	if( aDestinationIndex < 0 || aDestinationIndex >= iFavPresetList.Count())
       
   296 	{
       
   297 	    User::Leave(KErrArgument);
       
   298 	}
       
   299 	
       
   300 	TInt fakeIndex=iFavPresetList[aDestinationIndex]->Index();
       
   301 	SetMoveStatus(ETrue);
       
   302     iServ.MovePresetL( aId, fakeIndex );
       
   303 	IRLOG_DEBUG( "CIRFavoritesDb::MovePresetL - Exiting." );
       
   304     }
       
   305     
       
   306 //---------------------------------------------------------------------------
       
   307 //CIRFavoritesDb::SetMoveStatus()
       
   308 //sets the status of Move functionality
       
   309 //---------------------------------------------------------------------------
       
   310 //
       
   311 EXPORT_C void CIRFavoritesDb::SetMoveStatus(TBool aStatus)
       
   312 	{
       
   313 	iMoveStatus = aStatus;
       
   314 	}
       
   315 
       
   316 //---------------------------------------------------------------------------
       
   317 //CIRFavoritesDb::GetMoveStatus()
       
   318 //returns the status of the Move functionality
       
   319 //---------------------------------------------------------------------------
       
   320 //
       
   321 EXPORT_C TBool CIRFavoritesDb::GetMoveStatus()
       
   322 	{
       
   323 	return iMoveStatus;
       
   324 	}
       
   325     
       
   326     
       
   327 //---------------------------------------------------------------------------
       
   328 //CIRFavoritesDb::SearchPreset()
       
   329 //exported, function to find out if a preset exists in the favorites list
       
   330 //---------------------------------------------------------------------------
       
   331 //
       
   332 
       
   333 EXPORT_C TInt CIRFavoritesDb::SearchPreset( const TInt aIsdsPresetId,
       
   334 							  const TInt aUniqPresetId)
       
   335 	{
       
   336 	IRLOG_DEBUG( "CIRFavoritesDb::SearchPreset" );
       
   337 	TInt count = iFavPresetList.Count();
       
   338 	TInt iter;
       
   339 	//for isds presets
       
   340 	if(aIsdsPresetId!=KNullId)
       
   341 		{
       
   342 		for(iter=0;iter<count;iter++)
       
   343 			{
       
   344 			if(iFavPresetList[iter]->GetId() == aIsdsPresetId)
       
   345 				{
       
   346 			    IRLOG_DEBUG( "CIRFavoritesDb::SearchPreset - Exiting (1)." );
       
   347 				return iter;
       
   348 				}
       
   349 			}
       
   350 		}
       
   351 	if(aUniqPresetId != KNullId )
       
   352 	//for manually added presets
       
   353 		{
       
   354 		for(iter=0;iter<count;iter++)
       
   355 			{
       
   356 			if(aUniqPresetId==iFavPresetList[iter]->Id())
       
   357 				{
       
   358 			    IRLOG_DEBUG( "CIRFavoritesDb::SearchPreset - Exiting (2)." );
       
   359 				return iter;
       
   360 				}
       
   361 			}
       
   362 		}
       
   363 	// both the parameters are null implies that the preset is a non saved station
       
   364 	IRLOG_DEBUG( "CIRFavoritesDb::SearchPreset - Exiting (3)." );
       
   365 	return KErrNotFound;
       
   366 	}
       
   367 
       
   368 //---------------------------------------------------------------------------
       
   369 //CIRFavoritesDb::GetPreviousPreset()
       
   370 //exported, function to find out if a preset exists in the favorites list
       
   371 //---------------------------------------------------------------------------
       
   372 //
       
   373 EXPORT_C TInt CIRFavoritesDb::GetNextPreset(TInt aIndex)
       
   374 	{
       
   375 	if ( (aIndex==(iFavPresetList.Count()-1))||aIndex==KNineteen )
       
   376 		{
       
   377 		return 0;
       
   378 		}
       
   379 	else
       
   380 		{
       
   381 		return aIndex+1;
       
   382 		}
       
   383 	}
       
   384 
       
   385 //---------------------------------------------------------------------------
       
   386 //CIRFavoritesDb::GetNextPreset()
       
   387 //exported, function to find out if a preset exists in the favorites list
       
   388 //---------------------------------------------------------------------------
       
   389 //
       
   390 EXPORT_C TInt CIRFavoritesDb::GetPreviousPreset( TInt aIndex )
       
   391 	{
       
   392 	if ( aIndex==0 )
       
   393 		{
       
   394 		TInt val=(iFavPresetList.Count()-1);
       
   395 		return val;
       
   396 		}
       
   397 	else
       
   398 		{
       
   399 		return aIndex-1;
       
   400 		}
       
   401 	}
       
   402 //---------------------------------------------------------------------------
       
   403 //CIRFavoritesDb::ReplacePresetL()
       
   404 //replaces a preset with a new preset
       
   405 //for presetSync
       
   406 //---------------------------------------------------------------------------
       
   407 //
       
   408 
       
   409 EXPORT_C void CIRFavoritesDb::ReplacePresetL( CIRIsdsPreset& aNewPreset )
       
   410 {
       
   411 	IRLOG_DEBUG( "CIRFavoritesDb::ReplacePresetL" );
       
   412 	iMoveStatus=EFalse;
       
   413 	CIRPreset* preset;
       
   414 	TInt index = SearchPreset(aNewPreset.GetId(),KNullId);
       
   415 	//actual index
       
   416     if (index >= 0)
       
   417     {
       
   418         ASSERT( index >= 0 || index < iFavPresetList.Count() );
       
   419     	index = iFavPresetList[index]->Index();
       
   420     	preset = PresetByIndex(index);
       
   421     	if (preset)
       
   422     	{
       
   423     	    CPSTransaction* transaction = preset->CreateTransactionLC();
       
   424         	//change the preset data and commit
       
   425     	    //update 'name' only if this station has not been renamed before
       
   426     	    if (preset->GetRenamed())
       
   427             {
       
   428     	        aNewPreset.SetName(preset->Name());
       
   429             }
       
   430         	preset->CIRIsdsPresetToCIRPresetImpl(aNewPreset);
       
   431         	preset->SetChannelType(EIsdsPreset);
       
   432         	transaction->CommitL();
       
   433         	CleanupStack::PopAndDestroy( transaction );
       
   434     	}
       
   435     }
       
   436 	IRLOG_DEBUG( "CIRFavoritesDb::ReplacePresetL - Exiting." );
       
   437 }
       
   438 
       
   439 //---------------------------------------------------------------------------
       
   440 //CIRFavoritesDb::ReplaceUserDefinedPresetL()
       
   441 //replaces a userdefined preset with a new userdefiend preset
       
   442 //for presetSync
       
   443 //---------------------------------------------------------------------------
       
   444 //
       
   445 EXPORT_C void CIRFavoritesDb::ReplaceUserDefinedPresetL(CIRIsdsPreset& aNewPreset)
       
   446 {
       
   447 	IRLOG_DEBUG( "CIRFavoritesDb::ReplaceUserDefinedPresetL" );
       
   448 	iMoveStatus=EFalse;
       
   449 	TInt index = SearchUserDefinedPreset(aNewPreset.GetName(), aNewPreset.GetChannelUrlAtL(0));
       
   450 	
       
   451     if (index >= 0 && index < iFavPresetList.Count())
       
   452     {
       
   453         CIRPreset* preset = iFavPresetList[index];
       
   454         if (preset)
       
   455         {
       
   456             CPSTransaction* transaction = preset->CreateTransactionLC();
       
   457             /**
       
   458              * change the preset data and commit
       
   459              */
       
   460             preset->CIRIsdsPresetToCIRPresetImpl(aNewPreset);
       
   461             preset->SetChannelType(EUserDefined);
       
   462             preset->SetNameL(aNewPreset.GetName());
       
   463             preset->SetDescription(aNewPreset.GetShortDescription());
       
   464             transaction->CommitL();
       
   465             CleanupStack::PopAndDestroy(transaction);
       
   466         }
       
   467     }
       
   468 	IRLOG_DEBUG( "CIRFavoritesDb::ReplaceUserDefinedPresetL - Exiting." );	
       
   469 }
       
   470 
       
   471 
       
   472 
       
   473 
       
   474 
       
   475 
       
   476 //---------------------------------------------------------------------------
       
   477 //IRFavoritesDb::MakePresetUserDefinedL()
       
   478 //for a favorite preset that has been removed from the isds.
       
   479 //it is made a user defined preset by changing the type to 0.
       
   480 //index value is preseved so that the relative positions in the saved
       
   481 //stations view remains the same.
       
   482 //---------------------------------------------------------------------------
       
   483 //
       
   484 
       
   485 EXPORT_C void CIRFavoritesDb::MakePresetUserDefinedL( TInt aChannelId,
       
   486 	TInt /*aUserDefinedChannelId*/ )
       
   487 	{
       
   488 	IRLOG_DEBUG( "CIRFavoritesDb::MakePresetUserDefinedL" );
       
   489 	iMoveStatus=EFalse;
       
   490 	//to match the function signature
       
   491 	TInt index = SearchPreset(aChannelId,KNullId);
       
   492 	CIRPreset* preset;
       
   493 	//actual index
       
   494     if (index >= 0)
       
   495         {
       
   496         ASSERT( index >= 0 || index < iFavPresetList.Count() );
       
   497     	index = iFavPresetList[index]->Index();
       
   498     	preset = PresetByIndex(index);
       
   499     	if(preset)
       
   500     		{
       
   501         	CPSTransaction* transaction = preset->CreateTransactionLC();
       
   502         	//convert the flag to user defined and commit
       
   503         	preset->SetChannelType(EUserDefined);
       
   504         	//assign null id
       
   505         	preset->SetId(KNullId);
       
   506 
       
   507         	//make logo as null;;;; added for 2.0
       
   508         	_LIT8(KEmpty, "");
       
   509         	RBuf8 tempLogo;
       
   510         	tempLogo.Create(KEmpty);
       
   511         	preset->SetLogoData(tempLogo);
       
   512         	tempLogo.Close();
       
   513         	_LIT(KEmptyString, "");
       
   514         	RBuf tempString;
       
   515         	tempString.Create(KEmptyString);
       
   516 			//set imgurl to null
       
   517 			preset->SetImgUrl(tempString);
       
   518 			//set genre to null
       
   519 			preset->SetGenreInfo(tempString,tempString);
       
   520 			//set language to null
       
   521 			preset->SetLang(tempString);
       
   522 			//set country to null
       
   523 			preset->SetCountryName(tempString);
       
   524 			tempString.Close();
       
   525 			//set musicStoreEnabled filed to "no";;;;added for 2.0
       
   526         	_LIT(KNo, "no");
       
   527         	RBuf tempMusicStoreEnabled;
       
   528         	tempMusicStoreEnabled.Create(KNo);
       
   529         	preset->SetMusicStoreStatus(tempMusicStoreEnabled);
       
   530         	tempMusicStoreEnabled.Close();
       
   531         	
       
   532         	transaction->CommitL();
       
   533         	CleanupStack::PopAndDestroy( transaction );
       
   534     		}
       
   535         }
       
   536 	IRLOG_DEBUG( "CIRFavoritesDb::MakePresetUserDefinedL - Exiting." );
       
   537 	}
       
   538 
       
   539 //---------------------------------------------------------------------------
       
   540 //const RVRPresetArray& CVRPresetHandler::Presets() const
       
   541 // RIRPresetArray& an array containing all Internet Radio presets.
       
   542 //---------------------------------------------------------------------------
       
   543 //
       
   544 
       
   545 EXPORT_C const RIRPresetArray& CIRFavoritesDb::Presets() const
       
   546     {
       
   547 	IRLOG_DEBUG( "CIRFavoritesDb::Presets" );
       
   548 	//sort by 
       
   549    	return iFavPresetList;
       
   550     }
       
   551 
       
   552 /*
       
   553  * Increase the played times of a channel if it has been in the favorites
       
   554  * return : KErrNone if success
       
   555  *          KErrNotFound if the preset is not in the favorites
       
   556  */
       
   557 EXPORT_C TInt CIRFavoritesDb::IncreasePlayedTimesL(const CIRIsdsPreset &aIsdsPreset)
       
   558 {
       
   559     //step 1 : search the preset in favorites
       
   560     SortByIndex();
       
   561     TInt index = 0;
       
   562     if (aIsdsPreset.GetChannelType() == 0)
       
   563     {
       
   564         index = SearchUserDefinedPreset(aIsdsPreset.GetName(), aIsdsPreset.GetChannelUrlAtL(0));
       
   565     }
       
   566     else
       
   567     {
       
   568         index = SearchPreset(aIsdsPreset.GetId(), KNullId);
       
   569     }
       
   570     
       
   571     if (KErrNotFound == index)
       
   572     {
       
   573         return KErrNotFound;
       
   574     }
       
   575     
       
   576     //step 2 : update the played times of the preset
       
   577     CIRPreset *irPreset = iFavPresetList[index];
       
   578     //open a transaction with the preset server
       
   579     CPSTransaction* transaction = irPreset->CreateTransactionLC();
       
   580     irPreset->SetPlayedTimes(irPreset->GetPlayedTimes() + 1);
       
   581     transaction->CommitL();
       
   582     CleanupStack::PopAndDestroy(transaction);
       
   583     
       
   584     return KErrNone;
       
   585 }
       
   586 
       
   587 EXPORT_C TInt CIRFavoritesDb::RenamePresetL(const CIRIsdsPreset &aIsdsPreset, const TDesC &aNewName)
       
   588 {
       
   589     TInt index = 0;
       
   590     if (aIsdsPreset.GetChannelType() == 0)
       
   591     {
       
   592         index = SearchUserDefinedPreset(aNewName, aIsdsPreset.GetChannelUrlAtL(0));
       
   593         if (KErrNotFound != index)
       
   594         {
       
   595             //there is already a user-defined station with same name and url
       
   596             return KErrAlreadyExists;
       
   597         }
       
   598         else
       
   599         {
       
   600             index = SearchUserDefinedPreset(aIsdsPreset.GetName(), aIsdsPreset.GetChannelUrlAtL(0));
       
   601         }
       
   602     }
       
   603     else
       
   604     {
       
   605         index = SearchPreset(aIsdsPreset.GetId(), KNullId);
       
   606     }
       
   607     
       
   608     if (KErrNotFound == index)
       
   609     {
       
   610         return KErrNotFound;
       
   611     }
       
   612     
       
   613     CIRPreset *irPreset = iFavPresetList[index];
       
   614     //open a transaction with the preset server
       
   615     CPSTransaction* transaction = irPreset->CreateTransactionLC();
       
   616     irPreset->SetNameL(aNewName);
       
   617     irPreset->SetRenamed();
       
   618     transaction->CommitL();
       
   619     CleanupStack::PopAndDestroy(transaction);
       
   620     
       
   621     return KErrNone;
       
   622 }
       
   623 
       
   624 //---------------------------------------------------------------------------
       
   625 //CIRFavoritesDb::ConstructL()
       
   626 //Standard 2nd phase construction
       
   627 //---------------------------------------------------------------------------
       
   628 //
       
   629 
       
   630 void CIRFavoritesDb::ConstructL()
       
   631 	{
       
   632 	IRLOG_DEBUG( "CIRFavoritesDb::ConstructL" );
       
   633 	//cenrep handle
       
   634     CIRSettings *settings = CIRSettings::OpenL();
       
   635     iMaxPresetCount=settings->MaxPresetCount();
       
   636     settings->Close();
       
   637     
       
   638 	User::LeaveIfError( iServ.Connect() );
       
   639 	//a session to the client of the preset server
       
   640     iServ.GetPresetsL( iFavPresetList, KIRPreset );
       
   641     //notifier
       
   642     iNotifier = CPSPresetNotifier::NewL( iServ, *this );
       
   643     
       
   644     //initialization of the list for UI use
       
   645 	//iFavPresetList=new(ELeave)CArrayPtrFlat<CIRPreset>(KGranularity)
       
   646 	iMoveStatus=EFalse;
       
   647 	IRLOG_DEBUG( "CIRFavoritesDb::ConstructL - Exiting." );
       
   648 	}
       
   649 
       
   650 
       
   651 //---------------------------------------------------------------------------
       
   652 //CIRFavoritesDb::SortByIndex()
       
   653 //sorts the preset list by index
       
   654 //---------------------------------------------------------------------------
       
   655 //
       
   656 
       
   657 void CIRFavoritesDb::SortByIndex()
       
   658     {
       
   659 	IRLOG_DEBUG( "CIRFavoritesDb::SortByIndex" );
       
   660     //The sort order is determined by an algorithm supplied by the caller
       
   661     //and packaged as a TLinerOrder<T>
       
   662     iFavPresetList.Sort( TLinearOrder<CIRPreset>( PresetHandler::OrderByIndex ) );
       
   663 	IRLOG_DEBUG( "CIRFavoritesDb::SortByIndex - Exiting." );
       
   664     }
       
   665 
       
   666 //---------------------------------------------------------------------------
       
   667 //EXPORT_C TInt CIRFavoritesDb::MaxPresetCount()
       
   668 //returns the maximum number of presets that can be stored in the favorites list
       
   669 //---------------------------------------------------------------------------
       
   670 //
       
   671 
       
   672 EXPORT_C TInt CIRFavoritesDb::MaxPresetCount()
       
   673 	{
       
   674 	IRLOG_DEBUG( "CIRFavoritesDb::MaxPresetCount" );
       
   675 	return iMaxPresetCount;
       
   676 	}
       
   677 
       
   678 //---------------------------------------------------------------------------
       
   679 //TInt CIRFavoritesDb::EmptyPresetCount() const
       
   680 //Returns the number of empty presets
       
   681 //---------------------------------------------------------------------------
       
   682 //
       
   683 
       
   684 EXPORT_C TInt CIRFavoritesDb::EmptyPresetCount() const
       
   685     {
       
   686 	IRLOG_DEBUG( "CIRFavoritesDb::EmptyPresetCount" );
       
   687     return iMaxPresetCount - iFavPresetList.Count();
       
   688     }
       
   689 
       
   690 
       
   691 
       
   692 //---------------------------------------------------------------------------
       
   693 //TInt CIRFavoritesDb::AddObserver( MPSPresetObserver& aObserver )
       
   694 //Adds an observer that is notified upon changes in presets.
       
   695 //---------------------------------------------------------------------------
       
   696 //
       
   697 
       
   698 EXPORT_C TInt CIRFavoritesDb::AddObserver(const MPSPresetObserver& aObserver )
       
   699     {
       
   700 	IRLOG_DEBUG( "CIRFavoritesDb::AddObserver" );
       
   701     return iObservers.InsertInAddressOrder( &aObserver );
       
   702     }
       
   703 
       
   704 
       
   705 //---------------------------------------------------------------------------
       
   706 //TInt CIRPresetHandler::RemoveObserver( MPSPresetObserver& aObserver )
       
   707 //Removes an observer for a preset.
       
   708 //---------------------------------------------------------------------------
       
   709 //
       
   710 EXPORT_C void CIRFavoritesDb::RemoveObserver(const MPSPresetObserver& aObserver )
       
   711     {
       
   712     TInt idx = iObservers.FindInAddressOrder( &aObserver );
       
   713 
       
   714     if ( idx >= 0 )
       
   715         {
       
   716         iObservers.Remove( idx );
       
   717         iObservers.Compress();
       
   718         }
       
   719     }
       
   720 
       
   721 //---------------------------------------------------------------------------
       
   722 //TInt CIRFavoritesDb::MatchingPresetId( TInt aId )
       
   723 //---------------------------------------------------------------------------
       
   724 //
       
   725 
       
   726 TInt CIRFavoritesDb::MatchingPresetId( TInt aId )
       
   727     {
       
   728     TInt indx = KErrNotFound;
       
   729 
       
   730     SortByIndex() ;
       
   731 
       
   732     for ( TInt i = 0; i < iFavPresetList.Count(); i++ )
       
   733         {
       
   734      	 if (iFavPresetList[i]->Id() == aId )
       
   735             {
       
   736             indx = iFavPresetList[i]->Index();
       
   737           /*lint -save -e960 Note -- Violates MISRA Required Rule 58,
       
   738           non-switch break used*/
       
   739           break;
       
   740           /*lint -restore */
       
   741            }
       
   742         }
       
   743 
       
   744     return indx;
       
   745     }
       
   746 
       
   747 //---------------------------------------------------------------------------
       
   748 //TInt CIRFavoritesDb::CreatePresetL( TInt aIndex )
       
   749 //creates a preset by the index aIndex
       
   750 //---------------------------------------------------------------------------
       
   751 //
       
   752 
       
   753 CIRPreset* CIRFavoritesDb::CreatePresetL( TInt aIndex )
       
   754     {
       
   755 	IRLOG_DEBUG( "CIRFavoritesDb::CreatePresetL" );
       
   756     CIRPreset* preset = static_cast<CIRPreset*>( iServ.CreatePresetL(
       
   757     	aIndex, KIRPreset) );
       
   758     CleanupStack::PushL( preset );
       
   759     iFavPresetList.AppendL( preset );
       
   760     CleanupStack::Pop( preset );
       
   761 	IRLOG_DEBUG( "CIRFavoritesDb::CreatePresetL - Exiting." );
       
   762     return preset;
       
   763     }
       
   764 
       
   765 /*
       
   766  * Search a user defined preset by name and url
       
   767  */
       
   768 TInt CIRFavoritesDb::SearchUserDefinedPreset(const TDesC &aName, const TDesC &aUrl)
       
   769     {
       
   770     TInt count = iFavPresetList.Count();
       
   771     for (TInt index = 0; index < count; ++index)
       
   772         {
       
   773             CIRPreset *preset = iFavPresetList[index];
       
   774             const TDesC &name = preset->Name();
       
   775             TInt urlCount = preset->GetUrlCount();
       
   776             if(preset->GetUrlCount()>0)
       
   777                 {
       
   778                 const TDesC &url = preset->GetChannelUrlAt(0);
       
   779                 if (preset->GetChannelType() == 0 &&
       
   780                     name == aName &&
       
   781                     url  == aUrl)
       
   782                     {
       
   783                         return index;
       
   784                     }
       
   785                 }
       
   786             else
       
   787                 {
       
   788                 if (preset->GetChannelType() == 0 &&
       
   789                     name == aName &&
       
   790                     aUrl == KNullDesC)
       
   791                     {
       
   792                         return index;
       
   793                     }                
       
   794                 }
       
   795         }
       
   796     
       
   797     return KErrNotFound;
       
   798     }
       
   799 
       
   800 //---------------------------------------------------------------------------
       
   801 //CIRFavoritesDb::PresetByIndex(TInt aIndex)
       
   802 // Returns a preset by its id.
       
   803 //---------------------------------------------------------------------------
       
   804 //
       
   805 EXPORT_C CIRPreset* CIRFavoritesDb::PresetByIndex( TInt aIndex )
       
   806 	{
       
   807 	IRLOG_DEBUG( "CIRFavoritesDb::PresetByIndex" );
       
   808 
       
   809     CIRPreset* preset = NULL;
       
   810 
       
   811     for ( TInt i = 0; i < iFavPresetList.Count(); i++ )
       
   812         {
       
   813         if (iFavPresetList[i]->Index()==aIndex)
       
   814             {
       
   815             preset = iFavPresetList[i];
       
   816             /*lint -save -e960 (Note -- Violates MISRA Required Rule 58, non-switch break used)*/
       
   817             break;
       
   818             /*lint -restore*/
       
   819             }
       
   820         }
       
   821 	IRLOG_DEBUG( "CIRFavoritesDb::PresetByIndex - Exiting." );
       
   822     return preset;
       
   823 	}
       
   824 
       
   825 //---------------------------------------------------------------------------
       
   826 // CIRFavoritesDb::PresetById(TInt aIndex)
       
   827 //Returns a preset by its id.
       
   828 //---------------------------------------------------------------------------
       
   829 //
       
   830 EXPORT_C CIRPreset* CIRFavoritesDb::PresetById( TInt aId )
       
   831     {
       
   832 	IRLOG_DEBUG( "CIRFavoritesDb::PresetById" );
       
   833 
       
   834     CIRPreset* preset = NULL;
       
   835 
       
   836     for ( TInt i = 0; i < iFavPresetList.Count(); i++ )
       
   837         {
       
   838         if ( iFavPresetList[i]->Id() == aId )
       
   839             {
       
   840             preset = iFavPresetList[i];
       
   841             /*lint -save -e960 Note -- Violates MISRA Required Rule 58,
       
   842             non-switch break used*/
       
   843             break;
       
   844             /*lint -restore */
       
   845             }
       
   846         }
       
   847 	IRLOG_DEBUG( "CIRFavoritesDb::PresetById - Exiting." );
       
   848 
       
   849     return preset;
       
   850     }
       
   851 
       
   852 
       
   853 //---------------------------------------------------------------------------
       
   854 //CIRFavoritesDb::HandlePresetChangedL()
       
   855 //function to notify a change in saved presets
       
   856 //preset handler i.e CIRPreset,the reason of change.
       
   857 //---------------------------------------------------------------------------
       
   858 //
       
   859 EXPORT_C void CIRFavoritesDb::HandlePresetChangedL( TInt aId,
       
   860 	TUid aDataHandler, MPSPresetObserver::TPSReason aReason )
       
   861 	{
       
   862 	IRLOG_DEBUG( "CIRFavoritesDb::HandlePresetChangedL" );
       
   863     if ( aDataHandler == KIRPreset )
       
   864         {
       
   865         switch ( aReason )
       
   866             {
       
   867             case MPSPresetObserver::EPSCreated:
       
   868                 {
       
   869                 //call back from the preset client
       
   870                 //to append a preset when the preset is added
       
   871                 CPSPresetInterface* preset = NULL;
       
   872                 preset = PresetById( aId );
       
   873                 if ( !preset )
       
   874                     {
       
   875                     preset = iServ.OpenPresetL( aId );
       
   876                     CleanupStack::PushL( preset );
       
   877                     iFavPresetList.AppendL( static_cast<CIRPreset*>( preset ) );
       
   878                     CleanupStack::Pop( preset );
       
   879                     }
       
   880 
       
   881                 }
       
   882             break;
       
   883             case MPSPresetObserver::EPSDeleted:
       
   884                 {
       
   885                 //call back from the preset client
       
   886                 //to remove a preset when the preset is deleted
       
   887                 for ( TInt i = 0; i < iFavPresetList.Count(); i++ )
       
   888                     {
       
   889                     if ( iFavPresetList[i]->Id() == aId )
       
   890                         {
       
   891                         delete iFavPresetList[i];
       
   892                         iFavPresetList.Remove( i );
       
   893                         /*lint -save -e960 (Note -- Violates MISRA Required Rule 58, non-switch break used)*/
       
   894                         break;
       
   895                         /*lint -restore*/
       
   896                         }
       
   897                     }
       
   898                 }
       
   899             break;
       
   900             case MPSPresetObserver::EPSModified:
       
   901             break;
       
   902             default:
       
   903             break;
       
   904             }
       
   905 
       
   906         for ( TInt i = 0; i < iObservers.Count(); i++ )
       
   907             {
       
   908             iObservers[i]->HandlePresetChangedL( aId, aDataHandler, aReason );
       
   909             }
       
   910             
       
   911         }
       
   912 	IRLOG_DEBUG( "CIRFavoritesDb::HandlePresetChangedL - Exiting." );
       
   913 
       
   914     }
       
   915 
       
   916