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

📄 tcpcocketappui.cpp

📁 《基于Symbian OS的手机开发与应用实践》这本书的配套源码。
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CTcpCocketAppUi from TcpCocketAppui.cpp
*  Part of  : TcpCocket
*  Created  : 06.03.2006 by ToBeReplacedByAuthor
*  Implementation notes:
*     Initial content was generated by Series 60 Application Wizard.
*  Version  :
*  Copyright: ToBeReplacedByCopyright
* ============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknquerydialog.h> // for CAknMultiLineDataQueryDialog
#include <eikmenup.h>       // for CEikMenuPane
#include <TcpCocket.rsg>

#include "TcpCocket.hrh"
#include "TcpCocketAppui.h"
#include "TcpCocketContainer.h" 


// ================= MEMBER FUNCTIONS =======================
//
// ----------------------------------------------------------
// CTcpCocketAppUi::ConstructL()
// 
// ----------------------------------------------------------
//
void CTcpCocketAppUi::ConstructL()
    {
    BaseConstructL();

    iAppContainer = new (ELeave) CTcpCocketContainer;
    iAppContainer->SetMopParent( this );
    iAppContainer->ConstructL( ClientRect() );
    AddToStackL( iAppContainer );

    iEngine = CTcpCocketEngine::NewL(*this);
    }

// ----------------------------------------------------
// CTcpCocketAppUi::~CTcpCocketAppUi()
// Destructor
// Frees reserved resources
// ----------------------------------------------------
//
CTcpCocketAppUi::~CTcpCocketAppUi()
    {
    if (iAppContainer)
        {
        RemoveFromStack( iAppContainer );
        delete iAppContainer;
        }
    delete iEngine;
    }

// Functions from CTcpCocketEngine::MObserver
void CTcpCocketAppUi::OnConnectL(TInt aError)
    {
    if(aError==KErrNone)
        {
        OutputMsgL(R_QTN_PROMPT_CONNECTED);
        }
    else
        {
        OutputErrL(aError);
        }
    }

void CTcpCocketAppUi::OnSendL(const TDesC& aMsg, TInt aError)
    {
    OutputMsgL(R_QTN_PROMPT_LOCAL);
    if(aError == KErrNone)
        {
        iAppContainer->OutputMsgL(aMsg);
        }
    else
        {
        OutputErrL(aError);
        }
    }

void CTcpCocketAppUi::OnReceiveL(const TDesC& aMsg, TInt aError)
    {
    if(aError == KErrNone)
        {
        OutputMsgL(R_QTN_PROMPT_REMOTE);
        iAppContainer->OutputMsgL(aMsg);
        }
    else
        {
        OutputMsgL(R_QTN_PROMPT_DISCONNECTED);
        }
    }
// ------------------------------------------------------------------------------
// CTcpCocketAppUi::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 CTcpCocketAppUi::DynInitMenuPaneL(
    TInt aResourceId,CEikMenuPane* aMenuPane)
    {
    if(aResourceId!=R_TCPCOCKET_MENU)
        {
        return;
        }

    switch(iEngine->State())
        {
        case CTcpCocketEngine::EStateIdle:
            {
            aMenuPane->SetItemDimmed(ETcpCocketCmdSend, ETrue);
            break;
            }
        case CTcpCocketEngine::EStateConnecting:
        case CTcpCocketEngine::EStateListening:
            {
            aMenuPane->SetItemDimmed(ETcpCocketCmdStartServer, ETrue);
            aMenuPane->SetItemDimmed(ETcpCocketCmdConnectServer, ETrue);
            aMenuPane->SetItemDimmed(ETcpCocketCmdSend, ETrue);
            break;
            }
        case CTcpCocketEngine::EStateConnected:
            {
            aMenuPane->SetItemDimmed(ETcpCocketCmdStartServer, ETrue);
            aMenuPane->SetItemDimmed(ETcpCocketCmdConnectServer, ETrue);
            if(iAppContainer->LocalMsgLength()<=0)
                {
                aMenuPane->SetItemDimmed(ETcpCocketCmdSend, ETrue);
                }
            break;
            }
        default:
            {
            // unreachable
            break;
            }
        }

    }


// ----------------------------------------------------
// CTcpCocketAppUi::HandleCommandL(TInt aCommand)
// takes care of command handling
// ----------------------------------------------------
//
void CTcpCocketAppUi::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        case EAknSoftkeyBack:
        case EEikCmdExit:
            {
            Exit();
            break;
            }
        case ETcpCocketCmdStartServer:
            {
            iEngine->ListenL();
            OutputMsgL(R_QTN_PROMPT_LISTENING);
            break;
            }
        case ETcpCocketCmdConnectServer:
            {
            // Create dialog to allow user to view/edit connection details
            _LIT(KRemoteIP, "127.0.0.1");
            TBuf<15> ip = KRemoteIP();
            TInt port = 8002;

            CAknMultiLineDataQueryDialog* dialog =
                CAknMultiLineDataQueryDialog::NewL(ip, port);

            // Display and execute dialog, and act according to return value
            if ( dialog->ExecuteLD(R_TCPCOCKET_ADDRESS_QUERY) )
                {
                iEngine->ConnectL(ip, port);
                OutputMsgL(R_QTN_PROMPT_CONNECTING);
                }
            break;
            }
        case ETcpCocketCmdSend:
            {
            iAppContainer->SelectLocalMsgL();
            HBufC* buf = HBufC::NewLC(iAppContainer->LocalMsgLength());
            TPtr ptr = buf->Des();
            iAppContainer->GetLocalMsg(ptr);
            iEngine->SendL(*buf);
            CleanupStack::PopAndDestroy();
            break;
            }
        default:
            break;      
        }
    }

void CTcpCocketAppUi::OutputMsgL(TInt aResourceId)
    {
    HBufC* buf = iCoeEnv->AllocReadResourceLC(aResourceId);
    iAppContainer->OutputMsgL(*buf);
    CleanupStack::PopAndDestroy();
    }

void CTcpCocketAppUi::OutputErrL(TInt aError)
    {
    TBuf<32> msg;
    HBufC* buf = iCoeEnv->AllocReadResourceLC(R_QTN_PROMPT_ERROR);
    msg.Copy(*buf);
    CleanupStack::PopAndDestroy();
    msg.Append(' ');
    msg.AppendNum(aError);
    iAppContainer->OutputMsgL(msg);
    }
// End of File  

⌨️ 快捷键说明

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