/**
 @fn void WriteIniFileL()
 Open the ini file and store the values
 */
LOCAL_C void WriteIniFileL()
	{
	RFs fileSession;
	User::LeaveIfError(fileSession.Connect());
	CleanupClosePushL(fileSession);
	
	// create folder path
	fileSession.MkDirAll(IniFileName);
	
	RFileWriteStream fileWriteStream;
	User::LeaveIfError(fileWriteStream.Create(fileSession, IniFileName ,EFileWrite));
	CleanupClosePushL(fileWriteStream);
	
	//write the KEComUid to the stream
	fileWriteStream.WriteInt32L(KEComUid);
	
	//write the KSsaUid to the stream
	fileWriteStream.WriteInt32L(KSsaUid);
	
	//write the SSA value to the stream
	fileWriteStream.WriteInt8L(KSsaDisabled);
	
	//commit changes to the stream
	fileWriteStream.CommitL();

	// close: fileSession, fileWriteStream
	CleanupStack::PopAndDestroy(2);
	}
void CCmdCustomCommand::WriteHandleToFileL(TInt aHandle)
	{
	const TChar sysDrive = RFs::GetSystemDriveChar();
	RBuf filename;
	filename.CreateL(KNeverUnloadLibHandleFile().Length() + 1);
	filename.Append(sysDrive);
	filename.Append(KNeverUnloadLibHandleFile());
	filename.CleanupClosePushL();
	RFs fs;
	User::LeaveIfError(fs.Connect());
	CleanupClosePushL(fs);

	fs.MkDirAll(filename); // ignore any error
	RFile file;
	CleanupClosePushL(file);
	TInt err=KErrNone;
	TInt pos = 0;
	if(KErrNotFound == (err = file.Open(fs, filename, EFileShareExclusive|EFileStream|EFileWrite)))
		{
		User::LeaveIfError(file.Replace(fs, filename, EFileShareExclusive|EFileStream|EFileWrite));
		}
	else
		{
		User::LeaveIfError(err);
		file.Seek(ESeekEnd, pos);
		}
	RFileWriteStream targetStream;
	targetStream.Attach(file, pos); // file gets closed by this call, but that's okay, we don't need it any more (targetStream has its own copy of this RFile object that it owns)
	CleanupClosePushL(targetStream);
	targetStream.WriteInt32L(aHandle);
	targetStream.CommitL();
	CleanupStack::PopAndDestroy(4); 
	}
// -----------------------------------------------------------------------------
// CSisxUIStartupFile::WritePrivateStartupItemsL
// Write startup items into private file.
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CSisxUIStartupFile::WritePrivateStartupItemsL( RArray<TStartupTaskParam>& aStartupItems )
    {
    if ( PrivateFileExists() )
        {
        iFileSession.Delete( *iPrivateFile );
        }

    if ( aStartupItems.Count() > 0 )
        {
        RFileWriteStream writer;
        writer.PushL();    
        User::LeaveIfError( writer.Create( iFileSession, *iPrivateFile, EFileWrite ) );
    
        // Write the number of entries to the file
        writer.WriteInt32L( aStartupItems.Count() );
        
        // Write file names to the file
        for ( TInt index = 0; index < aStartupItems.Count(); index++ )
            {
            if ( iFileSession.IsValidName( aStartupItems[index].iFileName ) )
                {            
                writer << aStartupItems[index].iFileName;
                }
            else
                {
                User::Leave( KErrArgument );            
                }               
            }

        writer.CommitL();    
        CleanupStack::PopAndDestroy(); // writer
        }    
    }
void CEmTubePlaylistEntry::ExportL( RFileWriteStream& aStream )
	{
	TInt l = iLocation->Length();
	aStream.WriteInt32L( l );
	if( l )
		{
		aStream.WriteL( *iLocation );
		}

	l = iName->Length();
	aStream.WriteInt32L( l );
	if( l )
		{
		aStream.WriteL( *iName );
		}

	aStream.WriteInt32L( iPlayCount );
	aStream.WriteInt32L( (TInt)iType );
	aStream.WriteReal64L( TReal(iTime.Int64()) );
	}
void CEmTubePlaylist::ExportL( RFileWriteStream& aStream )
	{
	if( Name().Length() )
		{
		aStream.WriteInt32L( Name().Length() );
		aStream.WriteL( Name() );
		}
	else
		{
		aStream.WriteInt32L( 0 );
		}

	aStream.WriteInt32L( (TInt)iType );
	aStream.WriteInt32L( iFlags );

	aStream.WriteInt32L( iEntries.Count() );
	for(TInt i=0;i<iEntries.Count();i++)
		{
		iEntries[i]->ExportL( aStream );
		}
	}
void CVideoEntry::ExportL( RFileWriteStream &aStream )
	{
	aStream.WriteInt16L( iMediaTitle->Length() );
	aStream.WriteL( *iMediaTitle, iMediaTitle->Length() );

	aStream.WriteInt16L( iUrl->Length() );
	aStream.WriteL( *iUrl, iUrl->Length() );

	aStream.WriteInt16L( iThumbnailUrl->Length() );
	aStream.WriteL( *iThumbnailUrl, iThumbnailUrl->Length() );

	aStream.WriteInt16L( iThumbnailFile->Length() );
	aStream.WriteL( *iThumbnailFile, iThumbnailFile->Length() );

	aStream.WriteInt16L( iVideoId->Length() );
	aStream.WriteL( *iVideoId, iVideoId->Length() );

	aStream.WriteInt16L( iAuthorName->Length() );
	aStream.WriteL( *iAuthorName, iAuthorName->Length() );

	aStream.WriteInt16L( iAuthorUrl->Length() );
	aStream.WriteL( *iAuthorUrl, iAuthorUrl->Length() );

	aStream.WriteInt16L( iRelatedUrl->Length() );
	aStream.WriteL( *iRelatedUrl, iRelatedUrl->Length() );

	aStream.WriteInt16L( iAuthorVideosUrl->Length() );
	aStream.WriteL( *iAuthorVideosUrl, iAuthorVideosUrl->Length() );

	aStream.WriteInt32L( iThumbnailHeight );
	aStream.WriteInt32L( iThumbnailWidth );
	aStream.WriteInt32L( iDuration );
	aStream.WriteReal32L( iAverageRating );

	aStream.WriteInt32L( iViewCount );

	aStream.CommitL();
	}
void CHttpCacheManager::GenerateEmptyIndexFileL(const TDesC& aIndexFile, RFs& aRfs )
    {
    // Going to remove non-web client cache files in OrphanedFilesL call,
    // Signal to these clients by emptying (or creating) an 'empty' index file
    // do this before we start cleaning up files, to lessen the chances that any
    // of the files are in use when we're trying to delete them.
    
    // 'adopt' code from httpcachelookuptable for dealing with indexes.
    // save entries to index.dat
    RFileWriteStream writeStream;

    TInt ret = KErrNone;
    TInt tryCount = 0;
    for (tryCount = 0; tryCount < 5; tryCount++) 
        {
        ret = writeStream.Replace( aRfs, aIndexFile, EFileWrite );
        if (ret == KErrInUse)
            {
            // When the cache is full, it takes 65 - 85 miliseconds to write the index.
            // So wait 50 miliseconds and try again
            User::After(50000);
            }
        else
            {
            break;
            }
        }
    if( ret == KErrNone )
        {
        CleanupClosePushL( writeStream );
        writeStream.WriteInt32L( KCacheVersionNumber );
        writeStream.WriteInt32L( 0 ); // no entries in the index.
        writeStream.CommitL();
        CleanupStack::PopAndDestroy(); // writeStream
        }
    }
// ---------------------------------------------------------------------------
// CLbtCleanupHandler::WriteCleanupDataToFileL
// ---------------------------------------------------------------------------
//
void CLbtCleanupHandler::WriteCleanupDataToFileL()
	{
	FUNC_ENTER("CLbtCleanupHandler::WriteCleanupDataToFileL");	
	
	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 write stream to write to the file
    RFileWriteStream writeStream;
    
    // Open the file to replace the contents. If the file is not preset
    // this method will create the file
    TInt error = writeStream.Replace( fs, file, EFileWrite );    
    if( error != KErrNone )
    	{
    	ERROR("Opening of cleanup file failed with : %d", error);
    	writeStream.Close();
    	CleanupStack::PopAndDestroy(); //fs
    	User::Leave(error);
    	}
    CleanupClosePushL( writeStream );
    
    // First write the number of cleanup items
    writeStream.WriteInt16L( iCleanupItems.Count() );

	for(TInt i=0;i<iCleanupItems.Count();++i)
		{
		RArray<TLbtTriggerId>& triggers = iCleanupItems[i]->GetTriggers();
		
		// Write the trigger ids into the file
		writeStream.WriteInt16L( triggers.Count() );
		for(TInt j=0;j<triggers.Count();++j)
			{
			writeStream.WriteUint32L( triggers[j] );
			}
		
		// Write the time into the file
		const TDateTime dateTime = iCleanupItems[i]->GetTime().DateTime();
		
		// Write the year
		writeStream.WriteInt32L( dateTime.Year() );
		
		// Write the month
		writeStream.WriteInt32L( dateTime.Month() );
		
		// Write the day
		writeStream.WriteInt32L( dateTime.Day() );
		}
    
    CleanupStack::PopAndDestroy(2); //fs and writeSteam    
	}
TBool CSupLoginServiceProvider::WriteDataToFileL()
{
	__LOGSTR_TOFILE("CSupLoginServiceProvider::WriteDataToFileL() begins");

	// If current operation should be cancelled
	if (iCancelStatus)
		return EFalse;

	// Return value
	TBool retValue = EFalse;

	// If credentials data is not empty
	if (iMemberID && iUsername && iPassword)
	{
		retValue = ETrue;

		RFs fsSession;
		RFileWriteStream writeStream; // Write file stream

		// Install write file session
		User::LeaveIfError(fsSession.Connect());
		CleanupClosePushL(fsSession);

		// Open file stream
		// if already exists - replace with newer version
		TInt err = writeStream.Replace(fsSession, iSettingsFile, EFileStream | EFileWrite | EFileShareExclusive);
		CleanupClosePushL(writeStream);

		// Return EFalse if failed to open stream
		if (err != KErrNone)
		{
			retValue = EFalse;

			__LOGSTR_TOFILE("CSupLoginServiceProvider::WriteDataToFileL() failed to open file");
		}

		if (retValue)
		{
			__LOGSTR_TOFILE("CSupLoginServiceProvider::WriteDataToFileL() succeed to open the file");

			// Write data
			// iMemberID
			writeStream.WriteInt32L(iMemberID->Des().MaxLength());
			writeStream << *iMemberID;
			// iUsername
			writeStream.WriteInt32L(iUsername->Des().MaxLength());
			writeStream << *iUsername;
			// iPassword
			writeStream.WriteInt32L(iPassword->Des().MaxLength());
			writeStream << *iPassword;

			// Just to ensure that any buffered data is written to the stream
			writeStream.CommitL();
		}	

		// Free resource handlers
		CleanupStack::PopAndDestroy(&writeStream);
		CleanupStack::PopAndDestroy(&fsSession);		
	}	

	__LOGSTR_TOFILE("CSupLoginServiceProvider::WriteDataToFileL() ends");

	return retValue;
}