/*
* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Header of TransferBuffer.
*
*/


#ifndef __MMFFILEPRIV_H_
#define __MMFFILEPRIV_H_

#include <f32file.h>

#include <mmf/server/mmfclip.h>
#include <mmf/server/mmfdatabuffer.h>

/**
*  @publishedAll
*
*  Represents a copy of a KUidMmfTransferBuffer used for reading/writting to the file server
*/

class CTransferBufferCopy : public CBase
    {
    public:
        static CTransferBufferCopy* NewL(TInt aMaxLength);
        
        virtual ~CTransferBufferCopy()
            {delete iBuffer;}
        
        
        TDes8& Des() {return iBufferDes;}
        
        TInt MaxLength() {return iBufferDes.MaxLength();}
        
        void ReUse(TInt aMaxLength) {iBufferDes.Set(iBuffer,0, Min(aMaxLength, iMaxLength));}
        
        TBool InUse() {return iInUse;}
        
        void SetInUse(TBool aInUse) {iInUse=aInUse;}
        
    private:
        CTransferBufferCopy(TInt aMaxLength) : CBase(), iMaxLength(aMaxLength), iBufferDes(0,0,0), iInUse(EFalse){}
        
        void ConstructL();
        
    private:
        TUint8*	iBuffer;
        
        //Holds the original MaxLength when class constructed. 
        //May be larger than MaxLength of iBufferDes
        TInt	iMaxLength;
        
        TPtr8	iBufferDes;
        
        TBool	iInUse;
    };




    /**
    * @internalComponent
    *
    * A request is created when an external object requests or supplies data.  Calls to the File Server are
    * made asynchronously and a CReadWriteRequest created to notify the caller on completion.
    *
    * CReadWriteRequest is an abstract class.  Concrete instances are of CReadRequest & CWriteRequest.
    * Concrete instances need to know whether to call MDataSink::BufferFilledL() or MDataSource::BufferEmptiedL()
*/
class CReadWriteRequest : public CActive
    {
    public:
        CReadWriteRequest(TAny* aSinkOrSource, CMMFBuffer* aBuffer,TBool aSourceType)
            : CActive(EPriorityStandard),
            iSinkOrSource(aSinkOrSource), 
            iBuffer(aBuffer),
            iSourceType(aSourceType)
            {
            CActiveScheduler::Add( this );
            }
        
        CReadWriteRequest(TAny* aSinkOrSource, CMMFBuffer* aBuffer, CTransferBufferCopy* aOptionalDataBuffer,TBool aSourceType)
            : CActive(EPriorityStandard),
            iSinkOrSource(aSinkOrSource), 
            iBuffer(aBuffer),
            iSourceType(aSourceType),
            iTransferBufferCopy(aOptionalDataBuffer)
            {
            CActiveScheduler::Add( this );
            iTransferBufferCopy->SetInUse(ETrue);
            }
        
        CReadWriteRequest(CReadWriteRequest& aRequest)
            : CActive(EPriorityStandard),
            iSinkOrSource(aRequest.iSinkOrSource),
            iBuffer(aRequest.iBuffer),
            iSourceType(aRequest.iSourceType),
            iTransferBufferCopy(aRequest.iTransferBufferCopy)
            {
            CActiveScheduler::Add( this );
            iTransferBufferCopy->SetInUse(ETrue);
            }
        
        
        TBool Completed() ;
        TInt SetStatus(TBool aStatus);
        TDes8& BufferDes() ;
        TBool SourceType();
        const TDesC8& BufferDesC() ;
        CMMFBuffer* Buffer();
        TAny* GetSinkOrSource();
        ~CReadWriteRequest() ;
        
        
        // CActive functions.
        // 
        void SetActive() ;
        void DoCancel() ;
        virtual void RunL() = 0 ;
        virtual TInt RunError( TInt aError ) ;
        
        
    protected :
        TAny* iSinkOrSource;
        CMMFBuffer* iBuffer;
        TBool iSourceType;
        CTransferBufferCopy* iTransferBufferCopy;
        
        TBool iCompleted ;
        TDes8* iBufferDes ;
        TInt iError ;
        TBool iUseTransferBuffer ;
        void SetTransferBuffer (TBool aTBuffer) ;
        TBool CanUseTransferBuffer () ;
    } ;

    /**
    * @internalComponent
*/
class CReadRequest : public CReadWriteRequest
    {
    public :
        CReadRequest(TAny* aParent,TAny* aSinkOrSource, CMMFBuffer* aBuffer, TUint aPosition, TUint aFileSize,TBool aSourceType)
            : CReadWriteRequest(aSinkOrSource, aBuffer,aSourceType),
            iPosition(aPosition), iFileSize(aFileSize),iParent(aParent)
            { } 
        
        CReadRequest(TAny* aParent,TAny* aSinkOrSource, CMMFBuffer* aBuffer, CTransferBufferCopy* aOptionalDataBuffer, TUint aPosition, TUint aFileSize,TBool aSourceType)
            : CReadWriteRequest(aSinkOrSource, aBuffer, aOptionalDataBuffer,aSourceType),
            iPosition(aPosition), iFileSize(aFileSize),iParent(aParent)
            { } 
        
        void RunL();
    private:
        TUint iPosition;
        TUint iFileSize;
        TAny* iParent;
    };

#endif

