internetradio2.0/uisrc/irhistory.cpp
changeset 3 ee64f059b8e1
parent 2 2e1adbfc62af
child 4 3f2d53f144fe
child 5 0930554dc389
equal deleted inserted replaced
2:2e1adbfc62af 3:ee64f059b8e1
     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 <bautils.h>
       
    20 #include <s32file.h>
       
    21 
       
    22 #include "irdebug.h"
       
    23 #include "irhistory.h"
       
    24 #include "irisdspreset.h"
       
    25 #include "irui.h"
       
    26 
       
    27 //constant declaration
       
    28 _LIT(KLastPlayedChannel,"lastPlayed.dat");
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CIRLastPlayedStationInfo::NewL
       
    32 // 
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CIRLastPlayedStationInfo* CIRLastPlayedStationInfo::NewL()
       
    36 	{
       
    37 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::NewL - Entering" );
       
    38 	CIRLastPlayedStationInfo *self=CIRLastPlayedStationInfo::NewLC();
       
    39 	CleanupStack::Pop(self);
       
    40 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::NewL - Exiting." );
       
    41 	return self;
       
    42 	}
       
    43 	
       
    44 // ---------------------------------------------------------------------------
       
    45 // CIRLastPlayedStationInfo::NewLC
       
    46 // 
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CIRLastPlayedStationInfo* CIRLastPlayedStationInfo::NewLC()
       
    50 	{
       
    51 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::NewLC - Entering" );
       
    52 	CIRLastPlayedStationInfo *self=new(ELeave)CIRLastPlayedStationInfo;
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55     IRLOG_DEBUG( "CIRLastPlayedSongInfo::NewLC - Exiting." );	
       
    56 	return self;
       
    57 	}
       
    58 	
       
    59 // ---------------------------------------------------------------------------
       
    60 // CIRLastPlayedStationInfo::ConstructL
       
    61 // 2nd phase construction
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CIRLastPlayedStationInfo::ConstructL()
       
    65 	{
       
    66 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::ConstructL - Entering" );
       
    67 	iLastPlayedStation=CIRIsdsPreset::NewL();
       
    68 	ui = REINTERPRET_CAST(CIRUi*,CEikonEnv::Static()->EikAppUi());
       
    69 	TFileName pathLastPlayed=ui->GetPrivatePath();
       
    70 	pathLastPlayed.Append(KLastPlayedChannel);
       
    71 	
       
    72     RFs& fs = CCoeEnv::Static()->FsSession();	
       
    73 	TBool res;
       
    74 	res= BaflUtils::FileExists( fs, pathLastPlayed );
       
    75 	if(res)
       
    76 		{
       
    77         TRAPD( err, RetriveLastPlayedChannelL() );
       
    78         if ( err != KErrNone )
       
    79             {
       
    80             IRLOG_ERROR2( "CIRLastPlayedSongInfo::ConstructL - Retrieving last played channel failed, err=%d", err );            
       
    81             delete iLastPlayedStation;
       
    82             iLastPlayedStation = NULL;
       
    83             iLastPlayedStation = CIRIsdsPreset::NewL();
       
    84             User::LeaveIfError( BaflUtils::DeleteFile( fs, pathLastPlayed ) );            
       
    85             }
       
    86 		}	
       
    87 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::ConstructL - Exiting." );		
       
    88 	}
       
    89 	
       
    90 // ---------------------------------------------------------------------------
       
    91 // CIRLastPlayedStationInfo::~CIRLastPlayedStationInfo
       
    92 // standard c++ destructor
       
    93 // ---------------------------------------------------------------------------
       
    94 //	
       
    95 CIRLastPlayedStationInfo::~CIRLastPlayedStationInfo()
       
    96 	{
       
    97 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::~CIRLastPlayedStationInfo - Entering" );
       
    98 	if(iLastPlayedStation)
       
    99 		{
       
   100 		delete iLastPlayedStation;
       
   101 		iLastPlayedStation = NULL;
       
   102 		}
       
   103 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::~CIRLastPlayedStationInfo - Exiting." );	
       
   104 	}
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // CIRLastPlayedStationInfo::RetriveLastPlayedChannelL
       
   108 // Internalizes the preset data from the last played dat file
       
   109 // ---------------------------------------------------------------------------
       
   110 //	
       
   111 void CIRLastPlayedStationInfo::RetriveLastPlayedChannelL()
       
   112 	{
       
   113 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::RetriveLastPlayedChannelL - Entering" );
       
   114 	TFileName filename;
       
   115 	filename=ui->GetPrivatePath();
       
   116 	filename.Append(KLastPlayedChannel);
       
   117 	TParse	filestorename;
       
   118 	ui->iFsSession.Parse(filename,filestorename);
       
   119 	CFileStore* store = CDirectFileStore::OpenLC(ui->iFsSession,filestorename.
       
   120 		FullName(),EFileRead);
       
   121 		
       
   122 	RStoreReadStream instream;
       
   123 	instream.OpenLC(*store,store->Root());
       
   124    	iLastPlayedStation->InternalizeL(instream);
       
   125 	CleanupStack::PopAndDestroy(&instream);
       
   126 	CleanupStack::PopAndDestroy(store);
       
   127     IRLOG_DEBUG( "CIRLastPlayedSongInfo::RetriveLastPlayedChannelL - Exiting." );	
       
   128 	}
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // CIRLastPlayedStationInfo::CommitLastPlayedChannelL
       
   132 // Externalizes the preset data into last played dat
       
   133 // ---------------------------------------------------------------------------
       
   134 //	
       
   135 void CIRLastPlayedStationInfo::CommitLastPlayedChannelL()
       
   136 	{
       
   137 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::CommitLastPlayedChannelL - Entering" );
       
   138 	TFileName filename;
       
   139 	filename=ui->GetPrivatePath();
       
   140 	filename.Append(KLastPlayedChannel);
       
   141 	TParse	filestorename;
       
   142 	ui->iFsSession.Parse(filename,filestorename);
       
   143 	CFileStore* store = CDirectFileStore::ReplaceLC(ui->iFsSession,
       
   144 		filestorename.FullName(),EFileWrite);
       
   145 		
       
   146 	// Must say what kind of file store.
       
   147 	store->SetTypeL(KDirectFileStoreLayoutUid);
       
   148     // Construct the output stream.
       
   149 	RStoreWriteStream outstream;
       
   150 	TStreamId id = outstream.CreateLC(*store);
       
   151 	iLastPlayedStation->ExternalizeL(outstream);
       
   152 	outstream.CommitL();
       
   153 	// Cleanup the stream object
       
   154 	CleanupStack::PopAndDestroy(&outstream);
       
   155 	// Set this stream id as the root
       
   156 	store->SetRootL(id);
       
   157 	// Commit changes to the store
       
   158 	store->CommitL();
       
   159 	CleanupStack::PopAndDestroy(store);	
       
   160 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::CommitLastPlayedChannelL - Exiting." );	
       
   161 	}
       
   162 	
       
   163 // ---------------------------------------------------------------------------
       
   164 // CIRLastPlayedStationInfo::FileExists
       
   165 // checks if the last played dat file exists
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 TBool CIRLastPlayedStationInfo::FileExists()
       
   169 	{
       
   170 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::FileExists - Entering" );
       
   171 	TFileName filename; 
       
   172 	filename=ui->GetPrivatePath();
       
   173 	filename.Append(KLastPlayedChannel);
       
   174 	TBool result=BaflUtils::FileExists(ui->iFsSession,filename);
       
   175 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::FileExists - Exiting." );
       
   176 	return result;
       
   177 	}
       
   178 	
       
   179 // ---------------------------------------------------------------------------
       
   180 // CIRLastPlayedStationInfo::RemoveLastPlayedFile
       
   181 // removes the last played dat file if it gets corrupted
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 TInt CIRLastPlayedStationInfo::RemoveLastPlayedFile()
       
   185 	{
       
   186 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::RemoveLastPlayedFile - Entering" );
       
   187 	TInt ret = KErrNone;
       
   188 	if(FileExists())
       
   189 		{
       
   190 		CFileMan *fileHandle = NULL;
       
   191 		TRAPD(err , fileHandle=CFileMan::NewL(ui->iFsSession));	
       
   192 		if(err)
       
   193 			{
       
   194 			ret = err ;
       
   195 			}
       
   196 		TFileName filename; 
       
   197 		filename=ui->GetPrivatePath();
       
   198     		filename.Append(KLastPlayedChannel);
       
   199     		ret = fileHandle->Delete(filename);
       
   200 		delete fileHandle;
       
   201 		}
       
   202 	IRLOG_DEBUG( "CIRLastPlayedSongInfo::RemoveLastPlayedFile - Exiting." );	
       
   203 	return ret;
       
   204 	
       
   205 	}
       
   206