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