www.pudn.com > sockets.rar > socketsappui.cpp


/* Copyright (c) 2004, Nokia. All rights reserved */ 
 
 
// INCLUDE FILES 
#include  
#include  
#include  
#include  
#include  
 
#include "Sockets.pan" 
#include "SocketsAppUi.h" 
#include "SocketsAppView.h" 
#include "Sockets.hrh" 
#include "SocketsEngine.h" 
 
// ========================= MEMBER FUNCTIONS ================================== 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppUi::CSocketsAppUi() 
// C++ default constructor can NOT contain any code, that might leave. 
// ----------------------------------------------------------------------------- 
// 
CSocketsAppUi::CSocketsAppUi() 
    { 
    // No implementation required 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppUi::ConstructL() 
// Symbian 2nd phase constructor can leave. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppUi::ConstructL() 
    { 
    BaseConstructL(); 
 
    // Create view 
    iAppView = CSocketsAppView::NewL( ClientRect() ); 
    AddToStackL( iAppView ); 
 
    // Create engine 
    iSocketsEngine = CSocketsEngine::NewL( *iAppView ); 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppUi::~CSocketsAppUi() 
// Destructor. 
// ----------------------------------------------------------------------------- 
// 
CSocketsAppUi::~CSocketsAppUi() 
    { 
    delete iSocketsEngine; 
    iSocketsEngine = NULL; 
 
    if ( iAppView ) 
        { 
        RemoveFromStack( iAppView ); 
        delete iAppView; 
        iAppView = NULL; 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppUi::HandleCommandL() 
// Handles user menu selections. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppUi::HandleCommandL( TInt aCommand ) 
    { 
    switch( aCommand ) 
        { 
        case EEikCmdExit: 
        case EAknSoftkeyExit: 
            Exit(); 
            break; 
 
        case ESocketsCmdConnect: 
            { 
            // Create dialog to allow user to view/edit connection details 
            TBuf serverName( 
                                            iSocketsEngine->ServerName() ); 
            TInt port( iSocketsEngine->Port() ); 
 
            CAknMultiLineDataQueryDialog* dialog = 
                CAknMultiLineDataQueryDialog::NewL( serverName, port ); 
 
            // Display and execute dialog, and act according to return value 
            if ( dialog->ExecuteLD( R_SOCKETS_DIALOG_CONNECT ) ) 
                { 
                iSocketsEngine->SetServerName( serverName ); 
                iSocketsEngine->SetPort( port ); 
                iSocketsEngine->ConnectL(); // Initiate connection 
                } 
            break; 
            } 
        case ESocketsCmdDisconnect: 
            iSocketsEngine->Disconnect(); 
            break; 
        case ESocketsCmdClear: 
            iAppView->ClearTextL(); 
            break; 
        default: 
            User::Panic ( KPanicSockets, ESocketsBasicUi ); 
            break; 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppUi::DynInitMenuPaneL() 
// Initialises a menu pane before it is displayed. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppUi::DynInitMenuPaneL( TInt aMenuId, CEikMenuPane* aMenuPane ) 
    { 
    if ( aMenuId == R_SOCKETS_MENU ) 
        { 
        // Disable 'Connect' menu item if already connected 
        aMenuPane->SetItemDimmed( ESocketsCmdConnect, 
                                  iSocketsEngine->Connected() ); 
 
        // Disable 'Disconnect' menu item if not connected 
        aMenuPane->SetItemDimmed( ESocketsCmdDisconnect, 
                                  !iSocketsEngine->Connected() ); 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppUi::HandleKeyEventL() 
// Handles user key input. 
// ----------------------------------------------------------------------------- 
// 
TKeyResponse CSocketsAppUi::HandleKeyEventL( const TKeyEvent& aKeyEvent, 
                                             TEventCode aType ) 
    { 
    TChar theCharacter( aKeyEvent.iCode ); 
    if ( ( aType == EEventKey ) && 
         ( iSocketsEngine->Connected() ) && 
         ( theCharacter.IsPrint() || theCharacter == EKeyEnter ) ) 
        { 
        // This key event will end up as 'data' when written 
        // to the socket, so use TBuf8 
        TBuf8<2> buf; 
 
        // This will 'slice off' the higher order byte, OK for this example 
        buf.Append( aKeyEvent.iCode ); 
 
        if ( theCharacter == EKeyEnter ) 
            { 
            buf.Append( EKeyLineFeed ); 
            } 
 
        iSocketsEngine->WriteL( buf ); 
        iAppView->PrintNotify( buf, CEikGlobalTextEditor::EBold ); 
 
        return( EKeyWasConsumed ); 
        } 
 
    return( EKeyWasNotConsumed ); 
    } 
 
// End of File