⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testgnhttpdownappui.cpp

📁 SYMIBNA 支持大型文件断点续传的引擎源代码
💻 CPP
字号:
/*
 ============================================================================
 Name        : TestGnHttpDownAppUi.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : CTestGnHttpDownAppUi implementation
 ============================================================================
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>
#include <hlplch.h>
#include <mhttpdatasupplier.h>

#include <TestGnHttpDown_0xECCD2992.rsg>

#include "TestGnHttpDown_0xECCD2992.hlp.hrh"
#include "TestGnHttpDown.hrh"
#include "TestGnHttpDown.pan"
#include "TestGnHttpDownApplication.h"
#include "TestGnHttpDownAppUi.h"
#include "TestGnHttpDownAppView.h"


_LIT( KFileName, "C:\\private\\ECCD2992\\TestGnHttpDown.txt" );
_LIT( KText, "Hello World!");
_LIT( KTargetUri, "http://media.prefixmag.com/site_media/uploads/mp3s/Ne-Yo-All-Because-Of-You.mp3");
#ifdef __WINS__
_LIT( KMySavedFile, "C:\\data\\TestGnHttpDown.file");
_LIT( KMyRecFile, "C:\\data\\TestGnHttpDown.rec");
#else
_LIT( KMySavedFile, "E:\\data\\TestGnHttpDown.file");
_LIT( KMyRecFile, "E:\\data\\TestGnHttpDown.rec");
#endif

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CTestGnHttpDownAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTestGnHttpDownAppUi::ConstructL()
    {
    // Initialise app UI with standard value.
    BaseConstructL(CAknAppUi::EAknEnableSkin);

    // Create view object
    iAppView = CTestGnHttpDownAppView::NewL(ClientRect());
    
    iHttpDown = CGnHttpDown::NewL(*this);

    iRFs = iEikonEnv->FsSession();
    }
// -----------------------------------------------------------------------------
// CTestGnHttpDownAppUi::CTestGnHttpDownAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CTestGnHttpDownAppUi::CTestGnHttpDownAppUi()
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CTestGnHttpDownAppUi::~CTestGnHttpDownAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CTestGnHttpDownAppUi::~CTestGnHttpDownAppUi()
    {
    if (iAppView)
        {
        delete iAppView;
        iAppView = NULL;
        }
    delete iHttpDown; iHttpDown = NULL;

    }


TBool CTestGnHttpDownAppUi::ShouldUpdateTheResumeRecL(const TDesC8 & aData, CGnHttpDown * /*aGnHttpDown*/)
{
    // save rec info when receive data > 6k
    if(iDownSizePreTime + aData.Size() > (6 * 1024))
        {
        iDownSizePreTime = 0;
        return ETrue; // return ETrue means update rec file.
        }
    else
        {
        iDownSizePreTime += aData.Size();
        return EFalse;// return EFalse means update rec file.
        }
}

void CTestGnHttpDownAppUi::MHFRunL(
    RHTTPTransaction aTransaction, 
    const THTTPEvent& aEvent, 
    const TDesC8 & aDataBlock, CGnHttpDown * /*aGnHttpDown*/)
{
    RDebug::Printf("aEvent.iStatus=%d", aEvent.iStatus);
    
    switch (aEvent.iStatus)
    {
        // receive the response from http server, 
        // and you can get all info about the file at here.
        case THTTPEvent::EGotResponseHeaders:
        {
            // HTTP response headers have been received.
            // Pass status information to observer.
            RHTTPResponse resp = aTransaction.Response();

            // Get status code
            TInt statusCode = resp.StatusCode();

            // Get status text
            RStringF statusStr = resp.StatusText();
            HBufC* statusBuf = HBufC::NewLC(statusStr.DesC().Length());
            statusBuf->Des().Copy(statusStr.DesC());

            CleanupStack::PopAndDestroy(statusBuf);

            if(0 == iPosition) // new downloading
                {
                iRFile.Close();
                iRFile.Replace(iRFs, KMySavedFile, EFileWrite);
                }
            else
                {
                iRFile.Close();
                iRFile.Open(iRFs, KMySavedFile, EFileWrite);
                TInt pos = 0;
                iRFile.Seek(ESeekEnd, pos);
                __ASSERT_ALWAYS(pos == iPosition, User::Panic(_L("bad pos"), -1));
                }
        }
            break;

        // receiving the body data. and the body data stored in aDataBlock
        case THTTPEvent::EGotResponseBodyData:
        {
            User::LeaveIfError(iRFile.Write(aDataBlock));
            iPosition += aDataBlock.Size();
        }
            break;
            
        // the downlonding has been complete
        case THTTPEvent::EResponseComplete:
        {
            iPosition = 0;
            iDownSizePreTime= 0;

            //TParse parse;
            //parse.Set(KMySavedFile, NULL, NULL);
            
            //iRFile.Rename(const TDesC & aNewName);
            iRFile.Close();
        }
            break;
    }
}

TInt CTestGnHttpDownAppUi::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/, CGnHttpDown * /*aGnHttpDown*/)
{
    return aError;
}

// -----------------------------------------------------------------------------
// CTestGnHttpDownAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CTestGnHttpDownAppUi::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            Exit();
            break;

        case EInputUri:
            {
            /*
            CAknTextQueryDialog* queryUri = new (ELeave )CAknTextQueryDialog(iUri);//::NewL(iUri);
            CleanupStack::PushL(queryUri);
            
            HBufC* prompt = StringLoader::LoadLC(R_DATA_QUERY_PROMPT);
            
            queryUri->SetPromptL(*prompt);
            

            CleanupStack::PopAndDestroy(prompt);
            CleanupStack::PopAndDestroy(queryUri);
            //*/
            iHttpDown->SetFileUriL(KTargetUri);
            iHttpDown->SetRecFileL(KMyRecFile);
            }
            
            break;
        case EDownload:
            {
            iHttpDown->DownloadL( CGnHttpDown::EGetReq, 0, -1, ETrue);

            /* for post request
            iHttpDown->DownloadLSetPostDataBodyL(_L("first_name"), _L("cheney23"));
            iHttpDown->DownloadLSetPostDataBodyL(_L("second_name"), _L("ggan"));
            iHttpDown->DownloadL( CGnHttpDown::EPostReq, 0, -1, ETrue);
            //*/
            }
            break;
        case EStop:
            {
            iHttpDown->Stop();
            }
            break;
        case EResumeDownload:
            {
            if(KErrNotFound == iHttpDown->DownloadResumeL(CGnHttpDown::EGetReq))
                {
                iHttpDown->DownloadL( CGnHttpDown::EGetReq, 0, -1, ETrue);
                }
            }
            break;
        default:
            Panic(ETestGnHttpDownUi);
            break;
        }
    }
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CTestGnHttpDownAppUi::HandleStatusPaneSizeChange()
    {
    iAppView->SetRect(ClientRect());
    }

CArrayFix<TCoeHelpContext>* CTestGnHttpDownAppUi::HelpContextL() const
    {
#warning "Please see comment about help and UID3..."
    // Note: Help will not work if the application uid3 is not in the
    // protected range.  The default uid3 range for projects created
    // from this template (0xE0000000 - 0xEFFFFFFF) are not in the protected range so that they
    // can be self signed and installed on the device during testing.
    // Once you get your official uid3 from Symbian Ltd. and find/replace
    // all occurrences of uid3 in your project, the context help will
    // work. Alternatively, a patch now exists for the versions of 
    // HTML help compiler in SDKs and can be found here along with an FAQ:
    // http://www3.symbian.com/faq.nsf/AllByDate/E9DF3257FD565A658025733900805EA2?OpenDocument
    CArrayFixFlat<TCoeHelpContext>* array = new (ELeave) CArrayFixFlat<
            TCoeHelpContext> (1);
    CleanupStack::PushL(array);
    array->AppendL(TCoeHelpContext(KUidTestGnHttpDownApp, KGeneral_Information));
    CleanupStack::Pop(array);
    return array;
    }

// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -