// -----------------------------------------------------------------------------
// CBSBrandHandler::VerifyVersionL()
// -----------------------------------------------------------------------------
//
void CBSBrandHandler::VerifyVersionL( RFileReadStream& aStream )
	{
	TInt version = aStream.ReadInt16L();
	if( version != iReserved )
		{
		User::Leave( KErrArgument );
		}
	}
void CVideoEntry::ImportL( RFileReadStream &aStream )
	{
	delete iBitmap;
	iBitmap = NULL;
	delete iScaledBitmap;
	iScaledBitmap = NULL;
	delete iMediaTitle;
	iMediaTitle = NULL;
	delete iUrl;
	iUrl = NULL;
	delete iThumbnailUrl;
	iThumbnailUrl = NULL;
	delete iThumbnailFile;
	iThumbnailFile = NULL;
	delete iVideoId;
	iVideoId = NULL;
	delete iAuthorName;
	iAuthorName = NULL;
	delete iAuthorUrl;
	iAuthorUrl = NULL;
	delete iRelatedUrl;
	iRelatedUrl = NULL;
	delete iAuthorVideosUrl;
	iAuthorVideosUrl = NULL;

	TInt len;

	len = aStream.ReadInt16L();
	if( len )
		{
		iMediaTitle = HBufC::NewL( len );
		TPtr tmp(iMediaTitle->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iMediaTitle = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iUrl = HBufC::NewL( len );
		TPtr tmp(iUrl->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iUrl = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iThumbnailUrl = HBufC::NewL( len );
		TPtr tmp(iThumbnailUrl->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iThumbnailUrl = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iThumbnailFile = HBufC::NewL( len );
		TPtr tmp(iThumbnailFile->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iThumbnailFile = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iVideoId = HBufC::NewL( len );
		TPtr tmp(iVideoId->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iVideoId = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iAuthorName = HBufC::NewL( len );
		TPtr tmp(iAuthorName->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iAuthorName = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iAuthorUrl = HBufC::NewL( len );
		TPtr tmp(iAuthorUrl->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iAuthorUrl = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iRelatedUrl = HBufC::NewL( len );
		TPtr tmp(iRelatedUrl->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iRelatedUrl = KNullDesC().AllocL();
		}

	len = aStream.ReadInt16L();
	if( len )
		{
		iAuthorVideosUrl = HBufC::NewL( len );
		TPtr tmp(iAuthorVideosUrl->Des());
		aStream.ReadL(tmp, len);
		}
	else
		{
		iAuthorVideosUrl = KNullDesC().AllocL();
		}

	iThumbnailHeight = aStream.ReadInt32L();
	iThumbnailWidth = aStream.ReadInt32L();
	iDuration = aStream.ReadInt32L();
	iAverageRating = aStream.ReadReal32L();
	iViewCount = aStream.ReadInt32L();
	}
// ---------------------------------------------------------------------------
// CLbtCleanupHandler::ReadCleanupDataFromFileL
// ---------------------------------------------------------------------------
//
void CLbtCleanupHandler::ReadCleanupDataFromFileL()
	{
	FUNC_ENTER("CLbtCleanupHandler::ReadCleanupDataFromFileL");
	
	// First reset the cleanup data 
	iCleanupItems.ResetAndDestroy();
	
	// Open handle to file system
	RFs fs;
	User::LeaveIfError(fs.Connect());
	
	CleanupClosePushL(fs);
	
	// Obtain the file path
    TFileName file;
    
    // Gets the path in which the file can be created
    fs.SessionPath(file);

    // Create the file Directory ie the private directory of the process
    fs.MkDirAll(file);
    
    // Append the name of the file
    file.Append(KLbtAppCleanupFileName);
    
    // Open read stream
    RFileReadStream readStream;
    
    TInt error = readStream.Open( fs, file, EFileRead );
    if( error != KErrNone )
    	{
    	// File may not exists
    	ERROR("Opening of cleanup file failed with : %d", error);
    	readStream.Close();
    	CleanupStack::PopAndDestroy(); //fs
    	return;
    	}
    CleanupClosePushL( readStream );
    
    // Get the count in the cleanup array
    TInt count = readStream.ReadInt16L();    
    for(TInt i=0; i<count; ++i)
    	{
    	RArray<TLbtTriggerId> triggers;
    	CleanupClosePushL(triggers);
    	
    	// Read the trigger ids
    	TInt triggerCount = readStream.ReadInt16L();
    	for(TInt j=0;j<triggerCount;++j)
    		{
    		triggers.Append( readStream.ReadUint32L() );
    		}
    	
    	// Read the universal time
    	TInt year = readStream.ReadInt32L();
    	TMonth month = static_cast<TMonth>( readStream.ReadInt32L() );
    	TInt day = readStream.ReadInt32L();
    	
    	TDateTime dateTime( year,
    						month,
    						day,
    						0,
    						0,
    						0,
    						0 );
    	TTime time( dateTime );
    	
    	CLbtCleanupItem* cleanupItem = new (ELeave) CLbtCleanupItem();
    	cleanupItem->AddCleanupItem( triggers, time );
    	
    	CleanupStack::Pop(); // triggers
    	triggers.Close();
    	
    	// Append cleanup item to the cleaup items array
    	iCleanupItems.Append( cleanupItem );
    	}

    CleanupStack::PopAndDestroy(2); // fs and readStream
	}
// -----------------------------------------------------------------------------
// CBSBrandHandler::ReadElementLC()
// -----------------------------------------------------------------------------
//
MBSElement* CBSBrandHandler::ReadElementLC( const TDesC8& aId, TBool aForceDefault /*= EFalse*/ )
	{
	TRACE( T_LIT( "CBSBrandHandler::ReadElementLC begin aId"));

	if( aForceDefault )
		{
		TRACE( T_LIT( "CBSBrandHandler::ReadElementLC default brand"));
		iStorageManager->BrandHandleL( *iApplicationId,
									   *iDefaultBrandId, iLanguage,
									   *iHandle,
									   iReserved );		
		}
	else
		{
		TInt err = -1;
		TRAP (err, iStorageManager->BrandHandleL( *iApplicationId,
									   *iBrandId, iLanguage,
									   *iHandle,
									   iReserved ));
		if (KErrNone != err)
			{
			iStorageManager->BrandHandleL( *iApplicationId,
										   *iDefaultBrandId, iLanguage,
										   *iHandle,
										   iReserved ); 	
			}
		}
	
	RFileReadStream stream;
	stream.Attach( *iHandle );
	CleanupClosePushL( stream );
	
	VerifyVersionL( stream );
	
	TInt count = stream.ReadInt16L();
	
	MBSElement* returnValue = NULL;

	for( TInt i = 0; i < count; i++ )
		{
		TRAPD( err, returnValue = ReadStreamL( aId, stream ) );
		
		if( err == KErrEof )
			{
			TRACE( T_LIT( "CBSBrandHandler::ReadElementLC EOF!") );
			// the id is not found in this file
			User::Leave( KErrNotFound );
			}
		if( returnValue )
			{
			TRACE( T_LIT( "CBSBrandHandler::ReadElementLC ELEMENT FOUND.. at position %d"), i);
			// we found what we are looking for
			break;
			}
		}
		
	CleanupStack::PopAndDestroy( &stream ); // stream	
	
	TBool popElementFromCleanupStack( EFalse );
	
	/* If retur value is not found and if its read the actual brand, then try in default brand as well. aForceDefault will decide that. */
	if( !returnValue && !aForceDefault)
		{
		TRACE( T_LIT( "CBSBrandHandler::ReadElementLC force default is true") );

		// the element was not found
		// try the default brand if it's not the same as wanted brand
		if( 0 != iBrandId->Compare( *iDefaultBrandId ) )
			{
			TRACE( T_LIT( "CBSBrandHandler::ReadElementLC calling READELEMENTLC again") );

			/* Call ReadElementLC wiht aForceDefault set to TRUE */
			returnValue = ReadElementLC( aId, ETrue );

			if ( returnValue )
			    {
				TRACE( T_LIT( "CBSBrandHandler::ReadElementLC VALUE IS FOUND!!!") );
			    popElementFromCleanupStack = ETrue;
			    }
			else
				{
				TRACE( T_LIT( "CBSBrandHandler::ReadElementLC VALUE IS NOT FOUND!!!") );
				CleanupStack :: Pop (returnValue);
				}
			}
		if( !returnValue )
			{
			TRACE( T_LIT( "CBSBrandHandler::ReadElementLC VALUE not FOUND LEAVING WITH -1 !!!") );
			User::Leave( KErrNotFound );			
			}
		}
	
	CleanupClosePushL( *returnValue );
    // since we make one function call to ReadElementLC in case the default 
	// brand id is used to retrieved the element, we have to pop one returnValue
	// pointer from CleanupStack (otherwise we have two identical pointers on 
	// the stack!!!)
	if ( popElementFromCleanupStack )
   		{
   	 	CleanupStack::Pop( returnValue );
    	}
	
	TRACE( T_LIT( "CBSBrandHandler::ReadElementLC end ") );
	return returnValue;
	}