internetradio2.0/irsonghistory/src/irsonghistorydb.cpp
changeset 17 38bbf2dcd608
parent 16 5723da102db1
equal deleted inserted replaced
16:5723da102db1 17:38bbf2dcd608
     1 /*
       
     2 * Copyright (c) 2006-2006 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:  Implementation of the songhistory database
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /* ---------------------------------------------------------------------------
       
    20 *  Version history:
       
    21 *  Template version:
       
    22 *  <ccm_history>
       
    23 *
       
    24 * 
       
    25 *  Version: 3, Thu Apr 14 12:00:00 2008 by Rohit
       
    26 *  Ref:
       
    27 *  Pc lint fixes
       
    28 *
       
    29 *  Version: 2, Thu Apr 10 20:00:00 2008 by Rohit
       
    30 *  Ref:
       
    31 *  Implemented SongHistory changes for channelwise songs
       
    32 *
       
    33 *  </ccm_history>
       
    34 * ============================================================================
       
    35 */
       
    36 
       
    37 #include <bautils.h>
       
    38 #include <badesca.h>    // CDesCArrayFlat
       
    39 #include <s32file.h>    // CFileStore & CPermanentFileStore
       
    40 #include <bautils.h>    // file helpers
       
    41 #include <eikenv.h>
       
    42 
       
    43 #include "irsonghistorydb.h"
       
    44 #include "irsonghistoryinfo.h"
       
    45 #include "irdebug.h"
       
    46 #include "irsettings.h"
       
    47 
       
    48 
       
    49  
       
    50 _LIT( KSongHistoryDBTable,              "ChannelHistoryTable"  );
       
    51 _LIT( KSongHistoryDBTable2,             "SongHistoryTable"  );
       
    52 _LIT( KSongHistoryDBSongNameColumn,     "SongName"	   );
       
    53 _LIT( KSongHistoryDBArtistNameColumn,   "ArtistName"   );
       
    54 _LIT( KSongHistoryDBChannelNameColumn,  "ChannelName"  );
       
    55 _LIT( KSongHistoryDBChannelUrlColumn,   "ChannelUrl"  );
       
    56 _LIT( KSongHistoryDBChannelTypeColumn,   "ChannelType"  );
       
    57 _LIT( KSongHistoryDBChannelIdColumn,   "ChannelId"  );
       
    58 _LIT( KSongHistoryDBBitrateColumn,   "Bitrate"  );
       
    59 _LIT( KSongHistoryDBChannelDescColumn,   "ChannelDescription"  );
       
    60 _LIT( KSongHistoryDBImageUrlColumn,   "ImageUrl"  );
       
    61 _LIT( KSongHistoryDBGenreNameColumn,   "GenreName"  );
       
    62 _LIT( KSongHistoryDBCountryNameColumn,   "CountryName"  );
       
    63 _LIT( KSongHistoryDBLanguageNameColumn,   "LanguageName"  );
       
    64 _LIT( KSongHistoryDBMusicStatusColumn,   "MusicStoreStatus"  );
       
    65 
       
    66 _LIT(KSongHistoryDbFile,"songhistoryDb.db");
       
    67 _LIT(KNo , "No") ;
       
    68 
       
    69 const TInt KMaxSize = 255;
       
    70 
       
    71 TBool CIRSongHistoryDb::CompareChannelInfos(const CIRChannelInfo& aFirst,
       
    72 						 const CIRChannelInfo& aSecond)
       
    73 {
       
    74    return ( aFirst.iChannelName == aSecond.iChannelName && 
       
    75    					aFirst.iChannelUrl == aSecond.iChannelUrl &&
       
    76    					aFirst.iChannelType == aSecond.iChannelType);
       
    77 }
       
    78 
       
    79  
       
    80 
       
    81 // ======== MEMBER FUNCTIONS ========
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Function : NewL()
       
    85 // two phased construction
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CIRSongHistoryDb* CIRSongHistoryDb::NewL()
       
    89 {
       
    90 	IRLOG_DEBUG( "CIRSongHistoryDb::NewL" );
       
    91 	CIRSongHistoryDb* self=CIRSongHistoryDb::NewLC();
       
    92 	CleanupStack::Pop(self);
       
    93 	IRLOG_DEBUG( "CIRSongHistoryDb::NewL - Exiting." );
       
    94 	return self;
       
    95 }
       
    96 
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // Function : NewLC()
       
   100 // Two phased construction
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 CIRSongHistoryDb* CIRSongHistoryDb::NewLC()
       
   104 {
       
   105 	IRLOG_DEBUG( "CIRSongHistoryDb::NewLC" );
       
   106 	CIRSongHistoryDb *self=new(ELeave)CIRSongHistoryDb;
       
   107 	CleanupStack::PushL(self);
       
   108 	self->ConstructL();
       
   109 	IRLOG_DEBUG( "CIRSongHistoryDb::NewLC - Exiting." );
       
   110 	return self;
       
   111 }
       
   112 
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CIRSongHistoryDb::~CIRSongHistoryDb()
       
   116 // default destructor
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 CIRSongHistoryDb::~CIRSongHistoryDb()
       
   120 {
       
   121 	IRLOG_DEBUG( "CIRSongHistoryDb::~CIRSongHistoryDb" );
       
   122 
       
   123 	CloseSongHistoryDb();
       
   124 	iFsSession.Close();
       
   125 	IRLOG_DEBUG( "CIRSongHistoryDb::~CIRSongHistoryDb - Exiting." );
       
   126 }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CIRSongHistoryDb::CreateDbCondition()
       
   130 // Creates both the dbms files conditionally(only if not yet created)
       
   131 // calls CreateSongHistoryDb()
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 void CIRSongHistoryDb::CreateDbCondition(const TFileName& aSongHistoryDbFile)
       
   135 {
       
   136 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateDbCondition" );
       
   137 
       
   138 
       
   139 	if( !BaflUtils::FileExists(iFsSession, aSongHistoryDbFile) )
       
   140 	{
       
   141 		TInt error = CreateSongHistoryDb(aSongHistoryDbFile);
       
   142 		if( error )
       
   143 		{
       
   144 			IRLOG_DEBUG( "CIRSongHistoryDb::CreateDbCondition - Exiting (1)." );
       
   145 			
       
   146 		}
       
   147 	}
       
   148 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateDbCondition - Exiting (2)." );
       
   149 	
       
   150 }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CIRSongHistoryDb:AddToSongHistoryDbL()
       
   154 // adds the session log entry into data base
       
   155 // Returns ETure if songHistory count exceeds 30, else EFalse
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 TBool CIRSongHistoryDb::AddToSongHistoryDbL(const TDesC& aSongName,
       
   159                                             const TDesC& aArtistName, 
       
   160                                             const TDesC& aChannelName, 
       
   161                                             const TDesC& aChannelUrl,
       
   162                                             RBuf& aDelSongName, 
       
   163                                             RBuf& aDelArtistName, 
       
   164                                             RBuf& aDelChannelName, 
       
   165                                             RBuf& aDelChannelUrl,
       
   166                                             TInt  aChannelType,
       
   167                                             TInt aChannelId,
       
   168                                             TInt aBitrate,
       
   169                                             const TDesC& aChannelDesc,
       
   170                                             const TDesC& aImageUrl,
       
   171                                             const TDesC& aGenreName,
       
   172                                             const TDesC& aCountryName,
       
   173                                             const TDesC& aLanguageName,                                                
       
   174                                             const TDesC& aMusicFlag
       
   175                                             )
       
   176 {
       
   177 	IRLOG_DEBUG( "CIRSongHistoryDb::AddSongHistoryStartL" );
       
   178 
       
   179 	TBool bRetval = EFalse;
       
   180 	TInt error= 0;
       
   181 
       
   182 	iSongHistoryTable.Reset();
       
   183 
       
   184 	//if song history entries are  greater than or equal to 30
       
   185 	if( iSongHistoryTable.CountL() >= KMaxNoChannelHistoryDbEntries )
       
   186 	{
       
   187 		//first row is selected
       
   188 		iSongHistoryTable.FirstL();
       
   189 
       
   190 		//the current row is selected
       
   191 		iSongHistoryTable.GetL();
       
   192 
       
   193 		//delete that entry
       
   194 		CDbColSet* columns = iSongHistoryTable.ColSetL();
       
   195 		TInt songColumn = columns->ColNo( KSongHistoryDBSongNameColumn );
       
   196 		TInt artistColumn = columns->ColNo( KSongHistoryDBArtistNameColumn );
       
   197 		TInt channelColumn = columns->ColNo( KSongHistoryDBChannelNameColumn );
       
   198 		TInt channelUrlColumn = columns->ColNo( KSongHistoryDBChannelUrlColumn );
       
   199 
       
   200 		delete columns;
       
   201 		columns = NULL;
       
   202 
       
   203         aDelSongName.CreateL(iSongHistoryTable.ColDes( songColumn ));
       
   204         aDelArtistName.CreateL(iSongHistoryTable.ColDes( artistColumn ));
       
   205         aDelChannelName.CreateL(iSongHistoryTable.ColDes( channelColumn ));
       
   206         aDelChannelUrl.CreateL(iSongHistoryTable.ColDes( channelUrlColumn ));
       
   207         
       
   208         iSongHistoryTable.DeleteL();
       
   209         Compact();
       
   210 
       
   211         bRetval = ETrue;
       
   212 	}
       
   213 
       
   214     CDbColSet* columns = iSongHistoryTable.ColSetL();
       
   215     CleanupStack::PushL(columns);
       
   216 
       
   217 	const TPtrC &songName = aSongName.Left(KIRDbMaxStrLen);
       
   218 	const TPtrC &artistName = aArtistName.Left(KIRDbMaxStrLen);
       
   219 	const TPtrC &channelName = aChannelName.Left(KIRDbMaxStrLen);
       
   220 	const TPtrC &channelUrl = aChannelUrl.Left(KIRDbMaxStrLen);
       
   221     const TPtrC &channelDesc = aChannelDesc.Left(KIRDbMaxStrLen);
       
   222     const TPtrC &imageUrl = aImageUrl.Left(KIRDbMaxStrLen);
       
   223     const TPtrC &genreName = aGenreName.Left(KIRDbMaxStrLen);
       
   224     const TPtrC &countryName = aCountryName.Left(KIRDbMaxStrLen);
       
   225     const TPtrC &languageName = aLanguageName.Left(KIRDbMaxStrLen);
       
   226 	
       
   227 	TInt songLen = aSongName.Length();
       
   228 	TInt artistLen = aArtistName.Length();
       
   229 	if(  songLen + artistLen > KMaxSize )
       
   230 	{
       
   231 		IRLOG_DEBUG3( "CIRSongHistoryDb::AddSongHistoryStartL - LONG METADATA %d, %d", songLen, artistLen );
       
   232 	}
       
   233 	
       
   234 	TRAP(error,//trap start
       
   235 
       
   236   		iSongHistoryTable.InsertL();
       
   237 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBSongNameColumn ), songName);
       
   238 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBArtistNameColumn ), artistName);
       
   239 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBChannelNameColumn ), channelName);
       
   240 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBChannelUrlColumn ), channelUrl);
       
   241 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBChannelTypeColumn ), aChannelType);
       
   242 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBChannelIdColumn ), aChannelId);
       
   243 		iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBBitrateColumn ), aBitrate);
       
   244         iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBChannelDescColumn ), channelDesc);
       
   245         iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBImageUrlColumn ), imageUrl);
       
   246         iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBGenreNameColumn ), genreName);
       
   247         iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBCountryNameColumn ), countryName);
       
   248         iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBLanguageNameColumn ), languageName);
       
   249         iSongHistoryTable.SetColL(columns->ColNo( KSongHistoryDBMusicStatusColumn ), aMusicFlag);
       
   250 		iSongHistoryTable.PutL();
       
   251 		);
       
   252 
       
   253  	CleanupStack::PopAndDestroy(columns); 
       
   254  	if( error!=KErrNone )
       
   255  	{
       
   256 	 	User::LeaveIfError(error);
       
   257  	}
       
   258     
       
   259  	iSongHistoryTable.Reset();
       
   260  	IRLOG_DEBUG( "CIRSongHistoryDb::AddSongHistoryStartL - Exiting." );
       
   261 
       
   262 	return bRetval;
       
   263 }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // CIRSongHistoryDb:AddToSongHistoryDb2L()
       
   267 // adds the history song into the db
       
   268 // Returns ETure if songHistory count exceeds 100, else EFalse
       
   269 // ---------------------------------------------------------------------------
       
   270 
       
   271 TBool CIRSongHistoryDb::AddToSongHistoryDb2L( const TDesC& aSongName,
       
   272                                     const TDesC& aArtistName,                                   
       
   273                                     const TDesC& aChannelName,
       
   274                                     const TDesC& aMusicFlag)
       
   275 {
       
   276     IRLOG_DEBUG( "CIRSongHistoryDb::AddToSongHistoryDb2L" );
       
   277 
       
   278     TBool bRetval = EFalse;
       
   279     TInt error= 0;
       
   280     TInt songCompareFlag = 0;
       
   281     TInt artistCompareFlag = 0;    
       
   282      
       
   283 
       
   284     iSongHistoryTable2.Reset();    
       
   285     
       
   286     CDbColSet* columns = iSongHistoryTable2.ColSetL();
       
   287     CleanupStack::PushL(columns);
       
   288     
       
   289     /* the following is to judge wether the song is already in the db */    
       
   290     if ( iSongHistoryTable2.CountL() >= 1 )
       
   291     {
       
   292         TInt songNameColumn = columns->ColNo( KSongHistoryDBSongNameColumn );
       
   293         TInt artistNameColumn = columns->ColNo( KSongHistoryDBArtistNameColumn );     
       
   294 
       
   295         for ( iSongHistoryTable2.LastL(); iSongHistoryTable2.AtRow(); iSongHistoryTable2.PreviousL() )
       
   296         {
       
   297             iSongHistoryTable2.GetL();
       
   298             RBuf songName;
       
   299             RBuf artistName;
       
   300 
       
   301 			CleanupClosePushL(songName);
       
   302 			CleanupClosePushL(artistName);
       
   303 			
       
   304             songName.CreateL(iSongHistoryTable2.ColDes( songNameColumn ));
       
   305             artistName.CreateL(iSongHistoryTable2.ColDes( artistNameColumn ));
       
   306             
       
   307             songCompareFlag = (songName).Compare(aSongName);
       
   308             artistCompareFlag = (artistName).Compare(aArtistName);
       
   309             
       
   310             CleanupStack::PopAndDestroy(2);       
       
   311             
       
   312             if( 0 == songCompareFlag && 0 == artistCompareFlag )
       
   313             {
       
   314                 CleanupStack::PopAndDestroy(columns); 
       
   315                 iSongHistoryTable2.Reset();
       
   316                 return bRetval;
       
   317             }
       
   318         }        
       
   319     }
       
   320 	/* end  */
       
   321     
       
   322     //if song history entries are  greater than or equal to 100
       
   323     if( iSongHistoryTable2.CountL() >= KMaxNoSongHistoryDbEntries )
       
   324     {
       
   325         //first row is selected
       
   326         iSongHistoryTable2.FirstL();
       
   327         //the current row is selected
       
   328         iSongHistoryTable2.GetL();       
       
   329         //delete the current row
       
   330         iSongHistoryTable2.DeleteL();
       
   331         Compact();     
       
   332         bRetval = ETrue;   
       
   333     }
       
   334     
       
   335     const TPtrC &songName = aSongName.Left(KIRDbMaxStrLen);
       
   336     const TPtrC &artistName = aArtistName.Left(KIRDbMaxStrLen);
       
   337     const TPtrC &channelName = aChannelName.Left(KIRDbMaxStrLen);
       
   338     const TPtrC &musicFlag = aMusicFlag.Left(KIRDbMaxStrLen);
       
   339      
       
   340     
       
   341     TInt songLen = aSongName.Length();
       
   342     TInt artistLen = aArtistName.Length();
       
   343     if(  songLen + artistLen > KMaxSize || aChannelName.Length() > KMaxSize)
       
   344     {
       
   345         IRLOG_DEBUG3( "CIRSongHistoryDb::AddToSongHistoryDb2L - too long song info: %d or channel info: %d", songLen+artistLen, aChannelName.Length() );
       
   346     }
       
   347     
       
   348     TRAP(error,//trap start
       
   349 
       
   350         iSongHistoryTable2.InsertL();
       
   351         iSongHistoryTable2.SetColL(columns->ColNo( KSongHistoryDBSongNameColumn ), songName);
       
   352         iSongHistoryTable2.SetColL(columns->ColNo( KSongHistoryDBArtistNameColumn ), artistName);        
       
   353         iSongHistoryTable2.SetColL(columns->ColNo( KSongHistoryDBChannelNameColumn ), channelName);        
       
   354         iSongHistoryTable2.SetColL(columns->ColNo( KSongHistoryDBMusicStatusColumn ), musicFlag);
       
   355         iSongHistoryTable2.PutL();
       
   356         );
       
   357 
       
   358     CleanupStack::PopAndDestroy(columns); 
       
   359     if( error!=KErrNone )
       
   360     {
       
   361         User::LeaveIfError(error);
       
   362     }
       
   363     
       
   364     iSongHistoryTable2.Reset();
       
   365     IRLOG_DEBUG( "CIRSongHistoryDb::AddSongHistoryStartL - Exiting." );
       
   366 
       
   367     return bRetval;  
       
   368 }
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 // CIRSongHistoryDb::GetAllSongHistoryListL()
       
   372 //  gets all the song history entries  into an array
       
   373 // ---------------------------------------------------------------------------
       
   374 //
       
   375 void CIRSongHistoryDb::GetAllSongHistoryListL(RPointerArray<CIRSongHistoryInfo>& aHistoryDataArr)
       
   376 {
       
   377  	IRLOG_DEBUG( "CIRSongHistoryDb::GetAllSongHistoryListL" ); 	 
       
   378 
       
   379     if(iSongHistoryTable.CountL() < 1)
       
   380     {
       
   381     	return;
       
   382     }
       
   383 
       
   384     CDbColSet* columns = iSongHistoryTable.ColSetL();
       
   385     TInt songColumn = columns->ColNo( KSongHistoryDBSongNameColumn );
       
   386     TInt artistColumn = columns->ColNo( KSongHistoryDBArtistNameColumn );
       
   387     TInt channelColumn = columns->ColNo( KSongHistoryDBChannelNameColumn );
       
   388     TInt channelUrlColumn = columns->ColNo( KSongHistoryDBChannelUrlColumn );
       
   389     TInt channelTypeColumn = columns->ColNo( KSongHistoryDBChannelTypeColumn );
       
   390     TInt channelIdColumn = columns->ColNo( KSongHistoryDBChannelIdColumn );
       
   391     TInt bitrateColumn = columns->ColNo( KSongHistoryDBBitrateColumn );
       
   392     TInt channelDescColumn = columns->ColNo( KSongHistoryDBChannelDescColumn );
       
   393     TInt imageUrlColumn = columns->ColNo( KSongHistoryDBImageUrlColumn );
       
   394     TInt genreNameColumn = columns->ColNo( KSongHistoryDBGenreNameColumn );
       
   395     TInt countryNameColumn = columns->ColNo( KSongHistoryDBCountryNameColumn );
       
   396     TInt languageNameColumn = columns->ColNo( KSongHistoryDBLanguageNameColumn );
       
   397     TInt musicStatusColumn = columns->ColNo( KSongHistoryDBMusicStatusColumn );
       
   398 
       
   399     delete columns;
       
   400     columns = NULL; 	 
       
   401     
       
   402  	TInt song = 0;
       
   403  	
       
   404  	
       
   405     for ( iSongHistoryTable.LastL(); iSongHistoryTable.AtRow(); iSongHistoryTable.PreviousL() ) 	 
       
   406     {
       
   407         iSongHistoryTable.GetL();
       
   408         
       
   409         RBuf songName;
       
   410         RBuf artistName;
       
   411         RBuf channelName;
       
   412         RBuf channelUrl;
       
   413         TInt channelType;
       
   414         TInt channelId;
       
   415         TInt bitrate;
       
   416         RBuf channelDesc;
       
   417         RBuf imageUrl;
       
   418         RBuf genreName;
       
   419         RBuf countryName;
       
   420         RBuf languageName;
       
   421         RBuf channelMusicStatus;
       
   422         
       
   423         songName.CreateL(iSongHistoryTable.ColDes( songColumn ));
       
   424         songName.CleanupClosePushL();
       
   425         
       
   426         artistName.CreateL(iSongHistoryTable.ColDes( artistColumn ));
       
   427         artistName.CleanupClosePushL();
       
   428         
       
   429         channelName.CreateL(iSongHistoryTable.ColDes( channelColumn ));
       
   430         channelName.CleanupClosePushL();
       
   431         
       
   432         channelUrl.CreateL(iSongHistoryTable.ColDes( channelUrlColumn ));
       
   433         channelUrl.CleanupClosePushL();
       
   434         
       
   435         channelType=iSongHistoryTable.ColUint8( channelTypeColumn );
       
   436         
       
   437         channelId=iSongHistoryTable.ColUint16( channelIdColumn );
       
   438         
       
   439         bitrate=iSongHistoryTable.ColUint16( bitrateColumn );
       
   440         
       
   441         channelDesc.CreateL(iSongHistoryTable.ColDes( channelDescColumn ));
       
   442         channelDesc.CleanupClosePushL();
       
   443 
       
   444         imageUrl.CreateL(iSongHistoryTable.ColDes( imageUrlColumn ));
       
   445         imageUrl.CleanupClosePushL();
       
   446 
       
   447         genreName.CreateL(iSongHistoryTable.ColDes( genreNameColumn ));
       
   448         genreName.CleanupClosePushL();
       
   449 
       
   450         countryName.CreateL(iSongHistoryTable.ColDes( countryNameColumn ));
       
   451         countryName.CleanupClosePushL();
       
   452 
       
   453         languageName.CreateL(iSongHistoryTable.ColDes( languageNameColumn ));
       
   454         languageName.CleanupClosePushL();
       
   455 
       
   456         channelMusicStatus.CreateL(iSongHistoryTable.ColDes( musicStatusColumn ));
       
   457         channelMusicStatus.CleanupClosePushL();
       
   458  
       
   459         aHistoryDataArr[song]->SetHistoryInfoL(songName, artistName, channelUrl,
       
   460                          channelName,channelType,channelId,bitrate,channelDesc ,
       
   461                          imageUrl, genreName, countryName, languageName, channelMusicStatus);
       
   462         ++song;        
       
   463         CleanupStack::PopAndDestroy(&channelMusicStatus);
       
   464         CleanupStack::PopAndDestroy(&languageName);
       
   465         CleanupStack::PopAndDestroy(&countryName);
       
   466         CleanupStack::PopAndDestroy(&genreName);
       
   467         CleanupStack::PopAndDestroy(&imageUrl);
       
   468         CleanupStack::PopAndDestroy(&channelDesc);
       
   469         CleanupStack::PopAndDestroy(&channelUrl);
       
   470         CleanupStack::PopAndDestroy(&channelName);
       
   471         CleanupStack::PopAndDestroy(&artistName);
       
   472         CleanupStack::PopAndDestroy(&songName);
       
   473     }        
       
   474  
       
   475 
       
   476 	IRLOG_DEBUG( "CIRSongHistoryDb::GetAllSongHistoryListL - Exiting." );
       
   477 }
       
   478 
       
   479 
       
   480 // ---------------------------------------------------------------------------
       
   481 // CIRSongHistoryDb::GetAllSongHistoryList2L()
       
   482 //  gets all the song history entries  into an array
       
   483 // ---------------------------------------------------------------------------
       
   484 //
       
   485 void CIRSongHistoryDb::GetAllSongHistoryList2L(RPointerArray<CIRSongHistoryInfo>& aSongEntries)
       
   486 {
       
   487     IRLOG_DEBUG( "CIRSongHistoryDb::GetAllSongHistoryList2L" );    
       
   488 
       
   489     if(iSongHistoryTable2.CountL() < 1)
       
   490     {
       
   491         return;
       
   492     }
       
   493 
       
   494     CDbColSet* columns = iSongHistoryTable2.ColSetL();
       
   495     TInt songColumn = columns->ColNo( KSongHistoryDBSongNameColumn );
       
   496     TInt artistColumn = columns->ColNo( KSongHistoryDBArtistNameColumn );     
       
   497     TInt channelNameColumn = columns->ColNo( KSongHistoryDBChannelNameColumn );     
       
   498     TInt musicStatusColumn = columns->ColNo( KSongHistoryDBMusicStatusColumn );
       
   499 
       
   500     delete columns;
       
   501     columns = NULL;
       
   502     
       
   503     TInt song = 0;
       
   504     
       
   505     for ( iSongHistoryTable2.LastL(); iSongHistoryTable2.AtRow(); iSongHistoryTable2.PreviousL() )
       
   506     {
       
   507         iSongHistoryTable2.GetL();
       
   508          
       
   509         RBuf songName;
       
   510         RBuf artistInfo, channelName;
       
   511         RBuf musicStatus;
       
   512 
       
   513         CleanupClosePushL(songName);
       
   514         CleanupClosePushL(artistInfo);
       
   515         CleanupClosePushL(channelName);
       
   516         CleanupClosePushL(musicStatus);
       
   517 
       
   518         songName.CreateL(iSongHistoryTable2.ColDes( songColumn ));
       
   519         artistInfo.CreateL(iSongHistoryTable2.ColDes( artistColumn ));
       
   520         channelName.CreateL(iSongHistoryTable2.ColDes( channelNameColumn ));
       
   521         musicStatus.CreateL(iSongHistoryTable2.ColDes(musicStatusColumn));
       
   522         
       
   523         aSongEntries[song]->SetSongNameL(songName);
       
   524         aSongEntries[song]->SetArtistL(artistInfo);
       
   525         aSongEntries[song]->SetChannelNameL(channelName);
       
   526         aSongEntries[song]->SetChannelMusicStatusL(musicStatus);
       
   527         ++song;         
       
   528         
       
   529         CleanupStack::PopAndDestroy(4);
       
   530     }
       
   531      
       
   532     IRLOG_DEBUG( "CIRSongHistoryDb::GetAllSongHistoryListL - Exiting." );
       
   533 }
       
   534 
       
   535 // ---------------------------------------------------------------------------
       
   536 // Function : ClearAllSongHistoryFromDb
       
   537 // delete all the entries from the database.
       
   538 // ---------------------------------------------------------------------------
       
   539 //
       
   540 TInt CIRSongHistoryDb::ClearAllSongHistoryFromDb(/*RPointerArray<CIRSongHistoryInfo>& aSongHistoryInfoArr*/)
       
   541 {
       
   542 
       
   543 	TInt error = KErrNone;
       
   544 
       
   545 	IRLOG_DEBUG( "CIRSongHistoryDb::ClearAllSongHistoryFromDb - Entering" );
       
   546 	TRAP(error, iSongHistoryTable.FirstL());
       
   547 	if(error != KErrNone)
       
   548 	{
       
   549 		return error;
       
   550 	}
       
   551 
       
   552     while (iSongHistoryTable.AtRow())
       
   553 	{
       
   554 		TRAPD(error,
       
   555 		iSongHistoryTable.GetL();
       
   556 		iSongHistoryTable.DeleteL();
       
   557 		iSongHistoryTable.NextL(););
       
   558 		if(error)
       
   559 		{
       
   560 		    Compact();
       
   561 		    return error;
       
   562 		}
       
   563 	}
       
   564 	
       
   565     Compact();
       
   566     IRLOG_DEBUG( "CIRSongHistoryDb::ClearAllSongHistoryFromDb - Exiting." );
       
   567 	return KErrNone;
       
   568 
       
   569 }
       
   570 
       
   571 TInt CIRSongHistoryDb::DeleteOneHistory(TInt aIndex)
       
   572 {
       
   573     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneHistory - Entering" );
       
   574     
       
   575     TInt retValue = 0;
       
   576     TRAPD(error,(retValue = DeleteOneHistoryL(aIndex)));
       
   577     
       
   578     if( KErrNone != error )
       
   579     {
       
   580         return error;
       
   581     }
       
   582     
       
   583     if( KErrNone != retValue)
       
   584     {
       
   585         return retValue;
       
   586     }    
       
   587     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneHistory - exiting" );    
       
   588     return KErrNone;
       
   589 }
       
   590 
       
   591 TInt CIRSongHistoryDb::DeleteOneHistoryL(TInt aIndex)
       
   592 {
       
   593     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneHistoryL - Entering" );
       
   594     TInt totalCount = iSongHistoryTable.CountL();     
       
   595     if (aIndex < 0 || aIndex >= totalCount)
       
   596     {
       
   597         return KErrArgument;
       
   598     }
       
   599 	
       
   600     iSongHistoryTable.LastL();
       
   601     TInt tempIndex = 0;     
       
   602     while (tempIndex < aIndex)
       
   603     {
       
   604         TRAPD(error,iSongHistoryTable.PreviousL());
       
   605         if (KErrNone != error)
       
   606         {
       
   607             Compact();
       
   608             return error;
       
   609         }
       
   610         tempIndex++;
       
   611     }
       
   612     
       
   613     iSongHistoryTable.GetL();
       
   614     iSongHistoryTable.DeleteL();
       
   615     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneHistoryL - exiting" );   
       
   616     return KErrNone;    
       
   617 }
       
   618 
       
   619 TInt CIRSongHistoryDb::DeleteOneSongHistory(TInt aIndex)
       
   620 {
       
   621     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneSongHistory - Entering" );
       
   622     
       
   623     TInt retValue = 0;
       
   624     TRAPD(error,(retValue = DeleteOneSongHistoryL(aIndex)));
       
   625     
       
   626     if( KErrNone != error )
       
   627     {
       
   628         return error;
       
   629     }
       
   630     
       
   631     if( KErrNone != retValue)
       
   632     {
       
   633         return retValue;
       
   634     }    
       
   635     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneSongHistory - exiting" );    
       
   636     return KErrNone;
       
   637 }
       
   638 
       
   639 TInt CIRSongHistoryDb::DeleteOneSongHistoryL(TInt aIndex)
       
   640 {
       
   641     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneSongHistoryL - Entering" );
       
   642     TInt totalCount = iSongHistoryTable2.CountL();     
       
   643     if (aIndex < 0 || aIndex >= totalCount)
       
   644     {
       
   645         return KErrArgument;
       
   646     }
       
   647     
       
   648     iSongHistoryTable2.LastL();
       
   649     TInt tempIndex = 0;     
       
   650     while (tempIndex < aIndex)
       
   651     {
       
   652         TRAPD(error,iSongHistoryTable2.PreviousL());
       
   653         if (KErrNone != error)
       
   654         {
       
   655             Compact();
       
   656             return error;
       
   657         }
       
   658         tempIndex++;
       
   659     }
       
   660     
       
   661     iSongHistoryTable2.GetL();
       
   662     iSongHistoryTable2.DeleteL();
       
   663     IRLOG_DEBUG( "CIRSongHistoryDb::DeleteOneSongHistoryL - exiting" );   
       
   664     return KErrNone;    
       
   665 }
       
   666 
       
   667 // ---------------------------------------------------------------------------
       
   668 // Function : ClearAllSongHistoryFromDb2
       
   669 // delete all the entries from the database.
       
   670 // ---------------------------------------------------------------------------
       
   671 //
       
   672 TInt CIRSongHistoryDb::ClearAllSongHistoryFromDb2()
       
   673 {
       
   674 
       
   675     TInt error = KErrNone;
       
   676 
       
   677     IRLOG_DEBUG( "CIRSongHistoryDb::ClearAllSongHistoryFromDb2 - Entering" );
       
   678     TRAP(error, iSongHistoryTable2.FirstL());
       
   679     if(error != KErrNone)
       
   680     {
       
   681         return error;
       
   682     }
       
   683 
       
   684     while (iSongHistoryTable2.AtRow())
       
   685     {
       
   686         TRAPD(error,
       
   687         iSongHistoryTable2.GetL();
       
   688         iSongHistoryTable2.DeleteL();
       
   689         iSongHistoryTable2.NextL(););
       
   690         if(error)
       
   691         {
       
   692             Compact();
       
   693             return error;
       
   694         }
       
   695 
       
   696     }
       
   697     Compact();
       
   698     IRLOG_DEBUG( "CIRSongHistoryDb::ClearAllSongHistoryFromDb - Exiting." );
       
   699     return KErrNone;
       
   700 
       
   701 }
       
   702 
       
   703 // ---------------------------------------------------------------------------
       
   704 // CIRSongHistoryDb::ConstructL()
       
   705 // Standard 2nd phase construction
       
   706 // ---------------------------------------------------------------------------
       
   707 //
       
   708 void CIRSongHistoryDb::ConstructL()
       
   709 {
       
   710 	IRLOG_DEBUG( "CIRSongHistoryDb::ConstructL" );
       
   711 	User::LeaveIfError(iFsSession.Connect());
       
   712 
       
   713 	CIRSettings* settings = CIRSettings::OpenL();
       
   714 
       
   715 	TFileName songHistoryDbFile = settings->PrivatePath();
       
   716 	songHistoryDbFile.Append(KSongHistoryDbFile);
       
   717 
       
   718 	CreateDbCondition(songHistoryDbFile);
       
   719 	OpenSongHistoryDbL(songHistoryDbFile);
       
   720 
       
   721 	settings->Close();
       
   722 
       
   723 	IRLOG_DEBUG( "CIRSongHistoryDb::ConstructL- Exiting." );
       
   724 }
       
   725 
       
   726 // ---------------------------------------------------------------------------
       
   727 // CIRSongHistoryDb::CloseSongHistoryDb()
       
   728 // Closes the database
       
   729 // ---------------------------------------------------------------------------
       
   730 //
       
   731 void CIRSongHistoryDb::CloseSongHistoryDb()
       
   732 {
       
   733 	IRLOG_DEBUG( "CIRSongHistoryDb::CloseSongHistoryDb" );
       
   734 	iSongHistoryTable.Close();
       
   735 	iSongHistoryTable2.Close();
       
   736 	Close();
       
   737 	IRLOG_DEBUG( "CIRSongHistoryDb::CloseSongHistoryDb - Exiting." );
       
   738 }
       
   739 
       
   740 
       
   741 /**
       
   742 * Function : CreateSongHistoryTablesL()
       
   743 * creates songhistory table with three columns
       
   744 * log data
       
   745 * ---------------------------------------------------------------------------
       
   746 * SongHistoryTable
       
   747 *---------------------------
       
   748 *| SongName | ArtistName | ChannelName | ChannelUrl | ChannelType | ChannelId    | Bitrate      | ChannelDesc | ImageUrl   | GenreName  | CountryName | LanguageName | MusicStatus
       
   749 *---------------------------
       
   750 *|EDbColText| EDbColText | EDbColText  | EDbColText | EDbColUint8 | EDbColUint16 | EDbColUint16 | EDbColText  | EDbColText | EDbColText | EDbColText  | EDbColText   | EDbColText 
       
   751 *----------------------------------------------------------------------------
       
   752 */
       
   753 void CIRSongHistoryDb::CreateSongHistoryTablesL()
       
   754 {
       
   755 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateSongHistoryTablesL" );
       
   756     CDbColSet* columns = CDbColSet::NewLC();
       
   757 
       
   758     TRAPD( error,
       
   759     columns->AddL( TDbCol( KSongHistoryDBSongNameColumn, EDbColText, KMaxColumnLength ) );
       
   760     columns->AddL( TDbCol( KSongHistoryDBArtistNameColumn, EDbColText, KMaxColumnLength) );
       
   761     columns->AddL( TDbCol( KSongHistoryDBChannelNameColumn, EDbColText, KMaxColumnLength ) );
       
   762     columns->AddL( TDbCol( KSongHistoryDBChannelUrlColumn, EDbColText, KMaxColumnLength ) );
       
   763     columns->AddL( TDbCol( KSongHistoryDBChannelTypeColumn, EDbColUint8 ) );
       
   764     columns->AddL( TDbCol( KSongHistoryDBChannelIdColumn, EDbColUint16 ) );
       
   765     columns->AddL( TDbCol( KSongHistoryDBBitrateColumn, EDbColUint16 ) );
       
   766     columns->AddL( TDbCol( KSongHistoryDBChannelDescColumn, EDbColText, KMaxColumnLength ) );
       
   767     columns->AddL( TDbCol( KSongHistoryDBImageUrlColumn, EDbColText, KMaxColumnLength ) );
       
   768     columns->AddL( TDbCol( KSongHistoryDBGenreNameColumn, EDbColText, KMaxColumnLength ) );
       
   769     columns->AddL( TDbCol( KSongHistoryDBCountryNameColumn, EDbColText, KMaxColumnLength ) );
       
   770     columns->AddL( TDbCol( KSongHistoryDBLanguageNameColumn, EDbColText, KMaxColumnLength ) );
       
   771     columns->AddL( TDbCol( KSongHistoryDBMusicStatusColumn, EDbColText, KMaxColumnLength ) );
       
   772     );
       
   773 	User::LeaveIfError( error );
       
   774 
       
   775     User::LeaveIfError( CreateTable( KSongHistoryDBTable, *columns ) );
       
   776 
       
   777     CleanupStack::PopAndDestroy( columns );
       
   778 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateSongHistoryTablesL - Exiting." );
       
   779 }
       
   780 
       
   781 /**
       
   782 * Function : CreateSongHistoryTables2L()
       
   783 * creates songhistory table with three columns
       
   784 * log data
       
   785 * ---------------------------------------------------------------------------
       
   786 * SongHistoryTable
       
   787 *---------------------------
       
   788 *| SongName | ArtistName | ChannelName | MusicStatusFlag 
       
   789 *---------------------------
       
   790 *|EDbColText| EDbColText | EDbColText  | EDbColText 
       
   791 *----------------------------------------------------------------------------
       
   792 */
       
   793 void CIRSongHistoryDb::CreateSongHistoryTables2L()
       
   794 {
       
   795 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateSongHistoryTablesL" );
       
   796     CDbColSet* columns = CDbColSet::NewLC();
       
   797 
       
   798     TRAPD( error,
       
   799     columns->AddL( TDbCol( KSongHistoryDBSongNameColumn, EDbColText, KMaxColumnLength ) );
       
   800     columns->AddL( TDbCol( KSongHistoryDBArtistNameColumn, EDbColText, KMaxColumnLength) );
       
   801     columns->AddL( TDbCol( KSongHistoryDBChannelNameColumn, EDbColText, KMaxColumnLength) );
       
   802     columns->AddL( TDbCol( KSongHistoryDBMusicStatusColumn, EDbColText, KMaxColumnLength ) );
       
   803     );
       
   804 	User::LeaveIfError( error );
       
   805 
       
   806     User::LeaveIfError( CreateTable( KSongHistoryDBTable2, *columns ) );
       
   807 
       
   808     CleanupStack::PopAndDestroy( columns );
       
   809 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateSongHistoryTablesL - Exiting." );
       
   810 }
       
   811 
       
   812 
       
   813 
       
   814 // ---------------------------------------------------------------------------
       
   815 // CIRSongHistoryDb::OpenSongHistoryDbL()
       
   816 // opening the data base
       
   817 // ---------------------------------------------------------------------------
       
   818 //
       
   819 void CIRSongHistoryDb::OpenSongHistoryDbL(const TFileName& aSongHistoryDbFile)
       
   820 {
       
   821 	IRLOG_DEBUG( "CIRSongHistoryDb::OpenSongHistoryDbL" );
       
   822 	CloseSongHistoryDb(); //Ensure that the database is closed before trying to open it.
       
   823 
       
   824 	 
       
   825     TInt error  = KErrNone;
       
   826  
       
   827 	error = Open(iFsSession,aSongHistoryDbFile);
       
   828 	if( error!=KErrNone )
       
   829 	{
       
   830 		//if database is failed to open then
       
   831 		//function leaves
       
   832 		IRLOG_ERROR2( "CIRSongHistoryDb::OpenSongHistoryDbL - Opening session database failed (%d)", error );
       
   833 		User::LeaveIfError(error);
       
   834 	}
       
   835 	if( IsDamaged() || !InTransaction() )
       
   836 	{
       
   837 		//if data base is damaged then
       
   838 		//it tried to recover
       
   839 		//if recovery is not possible function leaves
       
   840 		error = Recover();
       
   841 		if ( KErrNone == error)
       
   842 			{
       
   843 			//if recovered data base is compacted
       
   844 			error = Compact();
       
   845 			}
       
   846 	    User::LeaveIfError(error);
       
   847 	}
       
   848 
       
   849 	//open the table
       
   850 	error = iSongHistoryTable.Open(*this, KSongHistoryDBTable, iSongHistoryTable.EUpdatable);
       
   851 	if( error )
       
   852 	{
       
   853 		User::LeaveIfError(error);
       
   854 	}
       
   855 
       
   856 	error = iSongHistoryTable2.Open(*this, KSongHistoryDBTable2, iSongHistoryTable2.EUpdatable);
       
   857 	if( error )
       
   858 	{
       
   859 	    iSongHistoryTable.Close();
       
   860 		User::LeaveIfError(error);
       
   861 	} 
       
   862 
       
   863 	IRLOG_DEBUG( "CIRSongHistoryDb::OpenSongHistoryDbL - Exiting." );
       
   864 }
       
   865 
       
   866 
       
   867 // ---------------------------------------------------------------------------
       
   868 // CIRSongHistoryDb::CreateSongHistoryDb()
       
   869 // @database filename
       
   870 // ---------------------------------------------------------------------------
       
   871 //
       
   872 TInt CIRSongHistoryDb::CreateSongHistoryDb(const TFileName& aSongHistoryDbFile )
       
   873 {
       
   874 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateSongHistoryDb" );
       
   875 
       
   876 
       
   877 	TInt error = Replace(iFsSession,aSongHistoryDbFile);
       
   878 	if ( error != KErrNone )
       
   879     {
       
   880 	    IRLOG_ERROR2( "CIRSongHistoryDb::CreateSongHistoryDb - Creating history database failed (%d)", error );
       
   881 	    return error;
       
   882     }
       
   883 
       
   884 	TRAPD(err1,CreateSongHistoryTablesL());
       
   885 	if(err1)
       
   886 	{
       
   887 	    return err1;
       
   888 	}
       
   889 
       
   890 	//if error, no handling 
       
   891 	TRAPD(err2, CreateSongHistoryTables2L());
       
   892 	if( err2 )
       
   893 	{
       
   894 	    return err2;	  
       
   895 	}	
       
   896 	IRLOG_DEBUG( "CIRSongHistoryDb::CreateSongHistoryDb - Exiting." );
       
   897 	return KErrNone;
       
   898 }
       
   899 
       
   900 
       
   901 // ---------------------------------------------------------------------------
       
   902 // CIRSongHistoryDb::CountSongHistoryDb()
       
   903 // count the database entries
       
   904 // ---------------------------------------------------------------------------
       
   905 //
       
   906 
       
   907 TInt CIRSongHistoryDb::CountSongHistoryDb()
       
   908 {
       
   909 	IRLOG_DEBUG( "CIRSongHistoryDb::CountSongHistoryDb" );
       
   910 	TInt historyCount = 0;
       
   911 	iSongHistoryTable.Reset();
       
   912 	TRAPD(error,historyCount= iSongHistoryTable.CountL());
       
   913 	if(error)
       
   914 	{
       
   915 		
       
   916 	}
       
   917 	IRLOG_DEBUG( "CIRSongHistoryDb::CountSongHistoryDb - Exiting." );
       
   918  	return historyCount;
       
   919 
       
   920 }
       
   921 
       
   922 // ---------------------------------------------------------------------------
       
   923 // CIRSongHistoryDb::CountSongHistoryDb2()
       
   924 // count the database entries
       
   925 // ---------------------------------------------------------------------------
       
   926 //
       
   927 TInt CIRSongHistoryDb::CountSongHistoryDb2()
       
   928 {
       
   929     IRLOG_DEBUG( "CIRSongHistoryDb::CountSongHistoryDb" );
       
   930     TInt historyCount = 0;
       
   931     iSongHistoryTable2.Reset();
       
   932     TRAPD(error,historyCount= iSongHistoryTable2.CountL());
       
   933     if(error)
       
   934     {
       
   935         historyCount = -1;
       
   936     }
       
   937     IRLOG_DEBUG( "CIRSongHistoryDb::CountSongHistoryDb - Exiting." );
       
   938     return historyCount;
       
   939 }
       
   940 // ---------------------------------------------------------------------------
       
   941 // CIRSongHistoryDb::GetLastHistoryItemL()
       
   942 // Gets the last song history item
       
   943 // ---------------------------------------------------------------------------
       
   944 //
       
   945 void CIRSongHistoryDb::GetLastHistoryItemL(RBuf& aSongName, RBuf& aArtistName,
       
   946 				 RBuf& aChannelName, RBuf& aChannelUrl)
       
   947 {
       
   948 	if( iSongHistoryTable.IsEmptyL() || iSongHistoryTable.LastL() == EFalse)
       
   949 	{
       
   950 		return;
       
   951 	}
       
   952 
       
   953 	iSongHistoryTable.GetL();
       
   954 
       
   955 	CDbColSet* columns = iSongHistoryTable.ColSetL();
       
   956 	TInt songColumn = columns->ColNo( KSongHistoryDBSongNameColumn );
       
   957 	TInt artistColumn = columns->ColNo( KSongHistoryDBArtistNameColumn );
       
   958 	TInt channelColumn = columns->ColNo( KSongHistoryDBChannelNameColumn );
       
   959 	TInt channelUrlColumn = columns->ColNo( KSongHistoryDBChannelUrlColumn );
       
   960 
       
   961 	delete columns;
       
   962     columns = NULL;
       
   963 
       
   964     aSongName.CreateL(iSongHistoryTable.ColDes( songColumn ));
       
   965     aArtistName.CreateL(iSongHistoryTable.ColDes( artistColumn ));
       
   966     aChannelName.CreateL(iSongHistoryTable.ColDes( channelColumn ));
       
   967     aChannelUrl.CreateL(iSongHistoryTable.ColDes( channelUrlColumn ));
       
   968 
       
   969 
       
   970     iSongHistoryTable.Reset();
       
   971 }
       
   972 // ---------------------------------------------------------------------------
       
   973 // CIRSongHistoryDb::GetChannelSongsCountL()
       
   974 // Gets the channel's song count.
       
   975 // ---------------------------------------------------------------------------
       
   976 //
       
   977 
       
   978 TUint CIRSongHistoryDb::GetChannelSongsCountL( const RBuf& aChannelName, const RBuf& aChannelUrl )
       
   979 {
       
   980 	_LIT( strQuery, "Select SongName from SongHistoryTable where ChannelName='%S' AND ChannelUrl='%S'");
       
   981 
       
   982 	TBuf<KMaxSize>  bufQuery;
       
   983 	bufQuery.Format( strQuery, &aChannelName, &aChannelUrl );
       
   984 
       
   985 	TDbQuery	querySearchChannel( bufQuery );
       
   986 	RDbView		dbView;
       
   987 
       
   988 	TInt error = dbView.Prepare( *this, querySearchChannel );
       
   989 	User::LeaveIfError(error);
       
   990 
       
   991 	error = dbView.Evaluate();	// 0, evaluation is complete 
       
   992 	User::LeaveIfError(error);
       
   993 
       
   994 	TInt cRows = dbView.CountL();
       
   995 
       
   996 	dbView.Close();
       
   997 
       
   998 	return cRows;
       
   999 }
       
  1000 
       
  1001 // ---------------------------------------------------------------------------
       
  1002 // CIRSongHistoryDb::SyncSongHistoryDb()
       
  1003 // Synchronises the History DB with removed channel entry in the ISDS
       
  1004 // ---------------------------------------------------------------------------
       
  1005 //
       
  1006 
       
  1007 void CIRSongHistoryDb::SyncSongHistoryDbL(TInt aChannelId)
       
  1008 {
       
  1009  	IRLOG_DEBUG( "CIRSongHistoryDb::SyncSongHistoryDbL" );
       
  1010 
       
  1011 
       
  1012     if(iSongHistoryTable.CountL() < 1)
       
  1013     {
       
  1014     	return;
       
  1015     }
       
  1016 
       
  1017     CDbColSet* columns = iSongHistoryTable.ColSetL();
       
  1018     TInt songColumn = columns->ColNo( KSongHistoryDBSongNameColumn );
       
  1019     TInt artistColumn = columns->ColNo( KSongHistoryDBArtistNameColumn );
       
  1020     TInt channelColumn = columns->ColNo( KSongHistoryDBChannelNameColumn );
       
  1021     TInt channelUrlColumn = columns->ColNo( KSongHistoryDBChannelUrlColumn );
       
  1022     TInt channelTypeColumn = columns->ColNo( KSongHistoryDBChannelTypeColumn );
       
  1023     TInt channelIdColumn = columns->ColNo( KSongHistoryDBChannelIdColumn );
       
  1024     TInt bitrateColumn = columns->ColNo( KSongHistoryDBBitrateColumn );
       
  1025     TInt channelDescColumn = columns->ColNo( KSongHistoryDBChannelDescColumn );
       
  1026     TInt imageUrlColumn = columns->ColNo( KSongHistoryDBImageUrlColumn );
       
  1027     TInt genreNameColumn = columns->ColNo( KSongHistoryDBGenreNameColumn );
       
  1028     TInt countryNameColumn = columns->ColNo( KSongHistoryDBCountryNameColumn );
       
  1029     TInt languageNameColumn = columns->ColNo( KSongHistoryDBLanguageNameColumn );
       
  1030     TInt musicStatusColumn = columns->ColNo( KSongHistoryDBMusicStatusColumn );
       
  1031 
       
  1032     delete columns;
       
  1033     columns = NULL;
       
  1034 
       
  1035 	// Find out the channel to be changed 
       
  1036 		for ( iSongHistoryTable.LastL(); iSongHistoryTable.AtRow(); iSongHistoryTable.PreviousL() )
       
  1037 		{
       
  1038 			iSongHistoryTable.GetL();
       
  1039 			// Extracting the values from the database.
       
  1040 				TInt channelId;
       
  1041 				RBuf imageUrl, genreName, countryName, languageName;
       
  1042                 RBuf channelMusicStatus;
       
  1043 
       
  1044                 CleanupClosePushL(imageUrl);
       
  1045                 CleanupClosePushL(genreName);
       
  1046                 CleanupClosePushL(countryName);
       
  1047                 CleanupClosePushL(languageName);
       
  1048                 CleanupClosePushL(channelMusicStatus);
       
  1049 
       
  1050 				channelId=iSongHistoryTable.ColUint16( channelIdColumn );
       
  1051                 imageUrl.CreateL(iSongHistoryTable.ColDes( imageUrlColumn ));
       
  1052                 genreName.CreateL(iSongHistoryTable.ColDes( genreNameColumn ));
       
  1053                 countryName.CreateL(iSongHistoryTable.ColDes( countryNameColumn ));
       
  1054                 languageName.CreateL(iSongHistoryTable.ColDes( languageNameColumn ));
       
  1055 				channelMusicStatus.CreateL(iSongHistoryTable.ColDes( musicStatusColumn ));
       
  1056 
       
  1057                 
       
  1058                 if(aChannelId == channelId)
       
  1059 	                {
       
  1060 	                iSongHistoryTable.UpdateL();
       
  1061 		            iSongHistoryTable.SetColL(channelTypeColumn, 0);
       
  1062 		            iSongHistoryTable.SetColL(channelIdColumn, 0);
       
  1063                     iSongHistoryTable.SetColL(imageUrlColumn, KNo);
       
  1064                     iSongHistoryTable.SetColL(genreNameColumn, KNo);
       
  1065                     iSongHistoryTable.SetColL(countryNameColumn, KNo);
       
  1066                     iSongHistoryTable.SetColL(languageNameColumn, KNo);
       
  1067 		            iSongHistoryTable.SetColL(musicStatusColumn, KNo);
       
  1068 		            iSongHistoryTable.PutL();
       
  1069 	                }
       
  1070                 CleanupStack::PopAndDestroy(5);
       
  1071 		}
       
  1072 	iSongHistoryTable.Reset();
       
  1073 		
       
  1074 	IRLOG_DEBUG( "CIRSongHistoryDb::SyncSongHistoryDbL - Exiting." );
       
  1075 }
       
  1076 
       
  1077 // ---------------------------------------------------------------------------
       
  1078 // CIRSongHistoryDb::GetIdPresentInDb()
       
  1079 // Gets whether that particular channel id is present in Db or not.
       
  1080 // ---------------------------------------------------------------------------
       
  1081 //
       
  1082 
       
  1083 TBool CIRSongHistoryDb::GetIdPresentInDbL(TInt aChannelId)
       
  1084 {
       
  1085  	IRLOG_DEBUG( "CIRSongHistoryDb::GetIdPresentInDbL" );
       
  1086 
       
  1087  	TBool value = EFalse;
       
  1088 
       
  1089     if(iSongHistoryTable.CountL() < 1)
       
  1090     {
       
  1091     	return value;
       
  1092     }
       
  1093 
       
  1094     CDbColSet* columns = iSongHistoryTable.ColSetL();
       
  1095     TInt channelIdColumn = columns->ColNo( KSongHistoryDBChannelIdColumn );
       
  1096 
       
  1097     delete columns;
       
  1098     columns = NULL;
       
  1099 
       
  1100 	// Find out the channel to be changed 
       
  1101 		for ( iSongHistoryTable.LastL(); iSongHistoryTable.AtRow(); iSongHistoryTable.PreviousL() )
       
  1102 		{
       
  1103 			iSongHistoryTable.GetL();
       
  1104 				TInt channelId;
       
  1105 				
       
  1106 				channelId=iSongHistoryTable.ColUint16( channelIdColumn );
       
  1107                 if(aChannelId == channelId)
       
  1108 	                {
       
  1109 	                value = ETrue;
       
  1110 	                }
       
  1111 
       
  1112 		}
       
  1113 		
       
  1114 	iSongHistoryTable.Reset();
       
  1115 	
       
  1116 	IRLOG_DEBUG( "CIRSongHistoryDb::GetIdPresentInDbL - Exiting." );
       
  1117 	return value ;
       
  1118 }
       
  1119 
       
  1120 
       
  1121 
       
  1122 // ---------------------------------------------------------------------------
       
  1123 // CIRSongHistoryDb::UpdateSongHistoryDb()
       
  1124 // Updates the song history DB when there is a channel change in the isds.
       
  1125 // ---------------------------------------------------------------------------
       
  1126 //
       
  1127 
       
  1128 TInt CIRSongHistoryDb::UpdateSongHistoryDbL(	TInt aChannelId,
       
  1129 											const TDesC& aChannelUrl,
       
  1130                                             const TDesC& aImageUrl,
       
  1131                                             const TDesC& aGenreName,
       
  1132                                             const TDesC& aCountryName,
       
  1133                                             const TDesC& aLanguageName,
       
  1134 											const TDesC& aMusicFlag)
       
  1135 {
       
  1136  	IRLOG_DEBUG( "CIRSongHistoryDb::UpdateSongHistoryDbL" );
       
  1137 
       
  1138  	TBool channelChanged = EFalse ;
       
  1139  	TInt compFlagUrl ;
       
  1140  	TInt compFlagImageUrl, compFlagGenreName, compFlagCountryName, compFlagLanguageName;
       
  1141  	TInt compFlagMusic ;
       
  1142  	TInt compUrlChange ;
       
  1143 
       
  1144     if(iSongHistoryTable.CountL() < 1)
       
  1145     {
       
  1146     	return EFalse;
       
  1147     }
       
  1148 
       
  1149     CDbColSet* columns = iSongHistoryTable.ColSetL();
       
  1150     TInt channelUrlColumn = columns->ColNo( KSongHistoryDBChannelUrlColumn );
       
  1151     TInt channelTypeColumn = columns->ColNo( KSongHistoryDBChannelTypeColumn );
       
  1152     TInt channelIdColumn = columns->ColNo( KSongHistoryDBChannelIdColumn );
       
  1153     TInt channelDescColumn = columns->ColNo( KSongHistoryDBChannelDescColumn );
       
  1154     TInt imageUrlColumn = columns->ColNo( KSongHistoryDBImageUrlColumn );
       
  1155     TInt genreNameColumn = columns->ColNo( KSongHistoryDBGenreNameColumn );
       
  1156     TInt countryNameColumn = columns->ColNo( KSongHistoryDBCountryNameColumn );
       
  1157     TInt languageNameColumn = columns->ColNo( KSongHistoryDBLanguageNameColumn );
       
  1158     TInt musicStatusColumn = columns->ColNo( KSongHistoryDBMusicStatusColumn );
       
  1159 
       
  1160     delete columns;
       
  1161     columns = NULL;
       
  1162 
       
  1163 	// Find out the channel to be changed 
       
  1164 	
       
  1165 		for ( iSongHistoryTable.LastL(); iSongHistoryTable.AtRow(); iSongHistoryTable.PreviousL() )
       
  1166 		{
       
  1167 			iSongHistoryTable.GetL();
       
  1168 			// Extracting the values from the database.
       
  1169 				RBuf channelUrl;
       
  1170 				RBuf imageUrl, genreName, countryName, languageName;
       
  1171 				TInt channelId;
       
  1172                 RBuf channelMusicStatus;
       
  1173 
       
  1174 				CleanupClosePushL(channelUrl);
       
  1175 				CleanupClosePushL(imageUrl);
       
  1176 				CleanupClosePushL(genreName);
       
  1177 				CleanupClosePushL(countryName);
       
  1178 				CleanupClosePushL(languageName);
       
  1179 				CleanupClosePushL(channelMusicStatus);
       
  1180 
       
  1181 				channelUrl.CreateL(iSongHistoryTable.ColDes( channelUrlColumn ));
       
  1182 				imageUrl.CreateL(iSongHistoryTable.ColDes( imageUrlColumn ));
       
  1183                 genreName.CreateL(iSongHistoryTable.ColDes( genreNameColumn ));
       
  1184                 countryName.CreateL(iSongHistoryTable.ColDes( countryNameColumn ));
       
  1185                 languageName.CreateL(iSongHistoryTable.ColDes( languageNameColumn ));
       
  1186 
       
  1187 				channelId=iSongHistoryTable.ColUint16( channelIdColumn );
       
  1188                 
       
  1189                 channelMusicStatus.CreateL(iSongHistoryTable.ColDes( musicStatusColumn ));
       
  1190                 
       
  1191                 if(aChannelId == channelId)
       
  1192 	                {
       
  1193 	                compFlagUrl =   (channelUrl).Compare(aChannelUrl);
       
  1194                     compFlagImageUrl = (imageUrl).Compare(aImageUrl);
       
  1195                     compFlagGenreName = (genreName).Compare(aGenreName);
       
  1196                     compFlagCountryName = (countryName).Compare(aCountryName);
       
  1197                     compFlagLanguageName = (languageName).Compare(aLanguageName);
       
  1198 	                compFlagMusic = (channelMusicStatus).Compare(aMusicFlag);
       
  1199 		            compUrlChange = (aChannelUrl).Compare(KNo);
       
  1200 		                
       
  1201 	                if(compFlagUrl && compUrlChange)
       
  1202 		                {
       
  1203 		                iSongHistoryTable.UpdateL();
       
  1204 		                iSongHistoryTable.SetColL(channelUrlColumn, aChannelUrl);
       
  1205 		                iSongHistoryTable.PutL();
       
  1206 		                channelChanged = ETrue ;
       
  1207 		                }
       
  1208                     if(compFlagImageUrl)
       
  1209                         {
       
  1210                         iSongHistoryTable.UpdateL();
       
  1211                         iSongHistoryTable.SetColL(imageUrlColumn, aImageUrl);
       
  1212                         iSongHistoryTable.PutL();
       
  1213                         channelChanged = ETrue ;
       
  1214                         }
       
  1215                     if(compFlagGenreName)
       
  1216                         {
       
  1217                         iSongHistoryTable.UpdateL();
       
  1218                         iSongHistoryTable.SetColL(genreNameColumn, aGenreName);
       
  1219                         iSongHistoryTable.PutL();
       
  1220                         channelChanged = ETrue ;
       
  1221                         }
       
  1222                     if(compFlagCountryName)
       
  1223                         {
       
  1224                         iSongHistoryTable.UpdateL();
       
  1225                         iSongHistoryTable.SetColL(countryNameColumn, aCountryName);
       
  1226                         iSongHistoryTable.PutL();
       
  1227                         channelChanged = ETrue ;
       
  1228                         }
       
  1229                     if(compFlagLanguageName)
       
  1230                         {
       
  1231                         iSongHistoryTable.UpdateL();
       
  1232                         iSongHistoryTable.SetColL(languageNameColumn, aLanguageName);
       
  1233                         iSongHistoryTable.PutL();
       
  1234                         channelChanged = ETrue ;
       
  1235                         }
       
  1236 	                if(compFlagMusic)
       
  1237 		                {
       
  1238 		                iSongHistoryTable.UpdateL();
       
  1239 		                iSongHistoryTable.SetColL(musicStatusColumn, aMusicFlag);
       
  1240 		                iSongHistoryTable.PutL();
       
  1241 		                channelChanged = ETrue ;
       
  1242 		                }
       
  1243 	                
       
  1244 	                }
       
  1245                 CleanupStack::PopAndDestroy(6);
       
  1246 		}
       
  1247 	iSongHistoryTable.Reset();
       
  1248 	IRLOG_DEBUG( "CIRSongHistoryDb::UpdateSongHistoryDbL - Exiting." );
       
  1249 	return channelChanged;
       
  1250 }
       
  1251 
       
  1252 void CIRSongHistoryDb::SearchAndDeleteRecordL(const TDesC& aChannelName, const TDesC& aChannelUrl,
       
  1253                                               TInt aChannelType, TInt aPresetId)
       
  1254 {
       
  1255     CDbColSet* columns = iSongHistoryTable.ColSetL();
       
  1256     TInt channelNameColumn = columns->ColNo( KSongHistoryDBChannelNameColumn );
       
  1257     TInt channelUrlColumn = columns->ColNo( KSongHistoryDBChannelUrlColumn );
       
  1258     TInt channelTypeColumn = columns->ColNo( KSongHistoryDBChannelTypeColumn );
       
  1259     TInt channelIdColumn = columns->ColNo(KSongHistoryDBChannelIdColumn);
       
  1260     
       
  1261     delete columns;
       
  1262     columns = NULL;
       
  1263     
       
  1264     for (iSongHistoryTable.LastL(); iSongHistoryTable.AtRow(); iSongHistoryTable.PreviousL())
       
  1265     {
       
  1266         iSongHistoryTable.GetL();
       
  1267         TPtrC channelName = iSongHistoryTable.ColDes(channelNameColumn);
       
  1268         TPtrC channelUrl  = iSongHistoryTable.ColDes(channelUrlColumn);
       
  1269         TInt channelType = iSongHistoryTable.ColInt(channelTypeColumn);
       
  1270         TInt channelId = iSongHistoryTable.ColInt(channelIdColumn);
       
  1271         
       
  1272         if (0 == aChannelType)
       
  1273         {
       
  1274             //user-defined channel
       
  1275             if (aChannelName == channelName && aChannelUrl == channelUrl)
       
  1276             {
       
  1277                 iSongHistoryTable.DeleteL();  
       
  1278                 break;
       
  1279             }
       
  1280         }
       
  1281         else
       
  1282         {
       
  1283             //channel from ISDS server
       
  1284             if (aChannelName == channelName && aPresetId == channelId)
       
  1285             {
       
  1286                 iSongHistoryTable.DeleteL();  
       
  1287                 break;
       
  1288             }
       
  1289         }
       
  1290     }
       
  1291     
       
  1292     iSongHistoryTable.Reset();
       
  1293 }