www.pudn.com > NetTest2nd.rar > NetTestAppUi.cpp


/* 
============================================================================ 
 Name        : CNetTestAppUi from NetTestAppui.cpp 
 Author      :  
 Version     : 
 Copyright   : Your copyright notice 
 Description : CNetTestAppUi implementation 
============================================================================ 
*/ 
 
// INCLUDE FILES 
#include "NetTestAppui.h" 
#include "NetTestContainer.h"  
#include  
#include "NetTest.hrh" 
#include  // for CloseSTDLib() 
#include  
#include  
#include  
#include    // for file exist check  
 
_LIT8(KTargetUri, "http://image.fotoever.com/PhotoServer01/Blog/Music/1132037/319200852305AM42589.mp3") ; //http://www.5mbox.com/bbs/test1.mp3 
                                                                                                          //http://www.chatyy.cn/bbsxp/mid/fangmin/qhc.mp3 
_LIT(KTargetFilename, "c:\\Deepinlove.mp3") ;  
 
void CNetTestAppUi::ConstructL() 
    { 
    BaseConstructL(); 
 
    iAppContainer = new (ELeave) CNetTestContainer; 
    iAppContainer->SetMopParent( this ); 
    iAppContainer->ConstructL( ClientRect() ); 
    AddToStackL( iAppContainer ); 
	 
	iPrintBuf.SetLength(0) ;  
	iHttpDown = NULL ;  
	iRecvSize = 0 ;  
	iFirstIn = ETrue ;  
    } 
 
// ---------------------------------------------------- 
// CNetTestAppUi::~CNetTestAppUi() 
// Destructor 
// Frees reserved resources 
// ---------------------------------------------------- 
// 
CNetTestAppUi::~CNetTestAppUi() 
{ 
    if (iAppContainer) 
        { 
        RemoveFromStack( iAppContainer ); 
        delete iAppContainer; 
        } 
} 
 
// ------------------------------------------------------------------------------ 
// CNetTestAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane) 
//  This function is called by the EIKON framework just before it displays 
//  a menu pane. Its default implementation is empty, and by overriding it, 
//  the application can set the state of menu items dynamically according 
//  to the state of application data. 
// ------------------------------------------------------------------------------ 
// 
void CNetTestAppUi::DynInitMenuPaneL( TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/) 
{} 
 
// ---------------------------------------------------- 
// CNetTestAppUi::HandleKeyEventL( 
//     const TKeyEvent& aKeyEvent,TEventCode /*aType*/) 
// takes care of key event handling 
// ---------------------------------------------------- 
// 
TKeyResponse CNetTestAppUi::HandleKeyEventL( 
    const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/) 
{ 
    return EKeyWasNotConsumed; 
} 
 
void CNetTestAppUi::M5PrintNotify(const TDesC & aMessage) 
{ 
	if(iPrintBuf.Length() + aMessage.Length() >= PRINT_BUF_LEN) { 
		iPrintBuf.SetLength(0) ;  
		return ;  
	} 
	iPrintBuf.Append(aMessage) ;  
	iPrintBuf.Append(_L("\n")) ;  
	iAppContainer->SetMsgBody(iPrintBuf) ; 
} 
 
void CNetTestAppUi::M5RecvNotify(const TDesC8 & recv_buf)  
{	 
	if(iFirstIn) { 
		TBuf<30> s ;  
		s.Format(_L("total size : %d"), iHttpDown->HttpTotalSize()) ;  
		M5PrintNotify(s) ;  
		s.Format(_L("recv size : %d"), iHttpDown->HttpRecvSize()) ;  
		M5PrintNotify(s) ;  
		iFirstIn = EFalse ;  
	} else { 
		iPrintBuf.Format(_L("total %d bytes\nrecv %d bytes"), 
						 iHttpDown->HttpTotalSize(),  
						 iHttpDown->HttpRecvSize()) ; 
		iAppContainer->SetMsgBody(iPrintBuf) ; 
	} 
	 
	// record the music data into file 
	iFile.Write(recv_buf) ;  
 
	if(iHttpDown->HttpTotalSize() > 0 && 
	   iHttpDown->HttpTotalSize() <= iHttpDown->HttpRecvSize()) { 
		TBuf<20> s ;  
		s.Format(_L("\nend recv!\n")) ;  
		M5PrintNotify(s) ;  
	} 
} 
 
void CNetTestAppUi::M5TimerExpireNotify() 
{ 
} 
 
void CNetTestAppUi::M5ErrorNotify(const TDesC& aErrMessage, TInt aErrCode)  
{ 
} 
 
void CNetTestAppUi::HandleCommandL(TInt aCommand) 
{ 
	TBuf8<100> s8(KTargetUri) ; 
    switch ( aCommand ) 
        { 
        case EAknSoftkeyBack: 
        case EEikCmdExit: 
            { 
				if(iHttpDown) delete iHttpDown ;  
				Exit(); 
            } 
			break; 
        case ENetTestCMWapConn: 
            {  
				iPrintBuf.SetLength(0) ;  
				iFirstIn = ETrue ;  
				if(iHttpDown) delete iHttpDown ;    
				iHttpDown = CM5HttpDown::NewL(*this) ;  
				iHttpDown->HttpConnPorxy(s8, HTTP_DOWN_CMWAP) ;  
				iRecvSize = 0 ;  
				// check whether the file exists  
				User::LeaveIfError(iFs.Connect()) ;  
				iRecvSize = 0 ;  
				if(BaflUtils::FileExists(iFs, KTargetFilename)) { 
					TInt seek_pos = 0 ;  
					iFile.Open(iFs, KTargetFilename, EFileStream | EFileWrite) ;  
					if(iFile.Size(iRecvSize) != KErrNone)  
						iRecvSize = 0 ;  
					iFile.Seek(ESeekEnd, seek_pos) ;  
				} else { 
					iFile.Replace(iFs, KTargetFilename, EFileStream | EFileWrite) ;  
				} 
            } 
			break ;  
		case ENetTestCMNetConn: 
			{ 
				iPrintBuf.SetLength(0) ;  
				iFirstIn = ETrue ;  
				if(iHttpDown) delete iHttpDown ;    
				iHttpDown = CM5HttpDown::NewL(*this) ;  
				iHttpDown->HttpConnPorxy(s8, HTTP_DOWN_CMNET) ;  
				iRecvSize = 0 ;  
				// check whether the file exists  
				User::LeaveIfError(iFs.Connect()) ;  
				iRecvSize = 0 ;  
				if(BaflUtils::FileExists(iFs, KTargetFilename)) { 
					TInt seek_pos = 0 ;  
					iFile.Open(iFs, KTargetFilename, EFileStream | EFileWrite) ;  
					if(iFile.Size(iRecvSize) != KErrNone)  
						iRecvSize = 0 ;  
					iFile.Seek(ESeekEnd, seek_pos) ;  
				} else { 
					iFile.Replace(iFs, KTargetFilename, EFileStream | EFileWrite) ;  
				} 
			} 
			break ;  
		case ENetTestResume: 
			{ 
				TBuf<20>   s16 ;  
				if(iHttpDown->HttpDown(s8, iRecvSize)) { 
					s16.Format(_L("req sent!")) ;  
					M5PrintNotify(s16) ;  
				} else { 
					s16.Format(_L("req send failed!")) ;  
					M5PrintNotify(s16) ;  
				} 
			} 
			break;  
		case ENetTestStop: 
			{ 
				if(iHttpDown->IsRunning()) { 
					iHttpDown->HttpStopDown() ;  
					delete iHttpDown ;  
					iHttpDown = NULL ;  
					iFile.Close() ;  
					iFs.Close() ;  
					iFirstIn = ETrue ; 
				}  
			} 
			break ;  
        default: 
            break;       
        } 
}