www.pudn.com > smssend-vc.rar > csmsappui.cpp


/* Copyright (c) 2003, Nokia. All rights reserved */ 
 
// INCLUDE FILES 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include "CSmsAppUi.h" 
#include "CSmsHandler.h" 
#include "CSmsDialog.h" 
 
// ============================ MEMBER FUNCTIONS =============================== 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::CSmsAppUi() 
// C++ default constructor can NOT contain any code, that might leave. 
// ----------------------------------------------------------------------------- 
// 
CSmsAppUi::CSmsAppUi() 
    { 
    // Used for determining which menu elements are shown. 
    iUiState = EWrite; 
    // We do not have a received message. 
    iReceivedMessage = EFalse; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::ConstructL() 
// Symbian 2nd phase constructor can leave. 
// ----------------------------------------------------------------------------- 
// 
void CSmsAppUi::ConstructL() 
    { 
    // Initialise app UI with standard values. 
    BaseConstructL(); 
 
    iSendDialog = new ( ELeave ) CSmsDialog; 
 
	// This is the enclosing parent control for the dialog. 
    iSendDialog->SetMopParent( this ); 
 
    // Execute the dialog following the data in the resource file. 
    iSendDialog->ExecuteLD( R_SMS_SEND_DIALOG ); 
    iSendDialog->SetEditableL( ETrue );     // we want to edit the content 
 
     // Add to control stack in order to get key presses. 
    AddToStackL( iSendDialog ); 
 
    iSmsHandler = CSmsHandler::NewL( this ); // CSmsAppUi creates CSmsHandler 
    } 
 
// ---------------------------------------------------------- 
// CSmsAppUi::~CSmsAppUi() 
// Destructor. 
// ---------------------------------------------------------- 
// 
CSmsAppUi::~CSmsAppUi() 
    { 
    if ( iSendDialog ) 
        { 
        RemoveFromStack( iSendDialog ); 
        delete iSendDialog; 
        } 
 
	delete iSmsHandler; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::DynInitMenuPaneL() 
// This function is called by the Symbian framework just before it displays 
// a menu pane. 
// ----------------------------------------------------------------------------- 
// 
void CSmsAppUi::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane ) 
    { 
    if ( aResourceId == R_SMS_MENU ) 
		{ 
		// Determine the state that the program is currently in. 
        switch ( iUiState ) 
            { 
            case ( EWrite ):  // we are writing a new message 
                if ( iReceivedMessage )   // there is a received message... 
                    { 
                    // ...so the Read option is shown 
                    aMenuPane->SetItemDimmed( ERead, EFalse ); 
                    aMenuPane->SetItemDimmed( ENewMsg, ETrue ); 
                    } 
                else 
                    { 
                    aMenuPane->SetItemDimmed( ERead, ETrue ); 
                    aMenuPane->SetItemDimmed( ENewMsg, ETrue ); 
                    } 
                break; 
 
            case ( EMsgSent ):    // we have sent a message 
                if ( iReceivedMessage ) 
                    { 
                    aMenuPane->SetItemDimmed( ESend, ETrue ); 
                    aMenuPane->SetItemDimmed( ERead, EFalse ); 
                    aMenuPane->SetItemDimmed( ENewMsg, EFalse ); 
                    } 
                else 
                    { 
                    aMenuPane->SetItemDimmed( ESend, ETrue ); 
                    aMenuPane->SetItemDimmed( ERead, ETrue ); 
                    aMenuPane->SetItemDimmed( ENewMsg, EFalse ); 
                    } 
                break; 
 
            default: 
                break; 
            } 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::HandleKeyEventL() 
// Takes care of key event handling. 
// ----------------------------------------------------------------------------- 
// 
TKeyResponse CSmsAppUi::HandleKeyEventL( const TKeyEvent& /*aKeyEvent*/, 
                                           TEventCode /*aType*/ ) 
    { 
    return EKeyWasNotConsumed; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::HandleCommandL() 
// Takes care of command handling. 
// ----------------------------------------------------------------------------- 
// 
void CSmsAppUi::HandleCommandL( TInt aCommand ) 
    { 
    switch ( aCommand ) 
        { 
        case EEikCmdExit: 
            Exit(); 
            break; 
 
        case ESend:      // user has chosen Send from the menu 
            { 
            TBuf recipientNumber; 
            TBuf messageText; 
 
            // Get user input. 
            STATIC_CAST( CEikEdwin*, ( iSendDialog->Control( EToId ) ) ) 
                ->GetText( recipientNumber ); 
		    if ( recipientNumber.Length() == 0 )    // there is no input 
                { 
                HBufC* text = StringLoader::LoadLC( R_SMS_NBR_MISSING ); 
                CAknWarningNote* warningNote = new ( ELeave ) CAknWarningNote; 
                warningNote->ExecuteLD( *text ); 
                CleanupStack::PopAndDestroy( text ); 
                break; 
                } 
 
            STATIC_CAST( CEikEdwin*, ( iSendDialog->Control( EMessageId ) ) ) 
                ->GetText( messageText ); 
 
            // Handler takes care of creating and sending the message. 
            TBool returnValue( iSmsHandler->SendL( recipientNumber, messageText ) ); 
            if ( returnValue ) 
                { 
                // The message can no longer be edited. 
                iSendDialog->SetEditableL( EFalse ); 
                iUiState = EMsgSent; 
                } 
 
            break; 
            } 
 
		case ERead:     // user wants to read a received message 
			iSmsHandler->ViewL();  // display next unread message 
            break; 
 
		case ENewMsg:   // user wants to write another message 
			{ 
            // Empty string declared in vtoken.h. The lenght of 1 is 
            // sufficient because we got an empty string. 
            TBufC<1> emptyTxt( KVersitTokenEmpty ); 
            TPtrC ptrEmpty( emptyTxt ); 
 
            STATIC_CAST( CEikEdwin*, ( iSendDialog->Control( EToId ) ) ) 
                ->SetTextL( &ptrEmpty ); 
            STATIC_CAST( CEikEdwin*, ( iSendDialog->Control( EMessageId ) ) ) 
                ->SetTextL( &ptrEmpty ); 
 
            iSendDialog->SetEditableL( ETrue ); 
  			iUiState = EWrite; 
 
            break; 
			} 
 
        default: 
            break; 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::MessageReceived() 
// Called by the handler class to indicate that there is an unread message. 
// ----------------------------------------------------------------------------- 
// 
void CSmsAppUi::MessageReceived() 
    { 
    iReceivedMessage = ETrue;       // we got an unread message 
    } 
 
// ----------------------------------------------------------------------------- 
// CSmsAppUi::NoMoreUnread() 
// Called by the handler class to indicate that no more unread messages exist. 
// ----------------------------------------------------------------------------- 
// 
void CSmsAppUi::NoMoreUnread() 
    { 
    iReceivedMessage = EFalse;       // there are no more unread messages 
    } 
 
// ---------------------------------------------------- 
// CSmsAppUi::ServerDown() 
// Prints error code and exits the program. 
// ---------------------------------------------------- 
// 
void CSmsAppUi::ServerDown( TInt aReason ) 
    { 
    HBufC16* aText1 = StringLoader::LoadLC( R_SMS_MSV_ERROR, aReason ); 
    HBufC16* aText2 = StringLoader::LoadLC( R_SMS_EXIT ); 
 
    CEikonEnv::Static()->AlertWin( aText1->Des(), aText2->Des() ); 
 
    CleanupStack::PopAndDestroy( 2 ); 
 
    User::Exit( 0 ); 
    } 
 
// End of File