socketsappui.cpp

来自「国内著名嵌入式培训机构内部资料,内含一些实例代码,包括技术专题书籍」· C++ 代码 · 共 221 行

CPP
221 行
字号
/*
 ============================================================================
 Author	  : hou maoqing
 Version	 : 1.0
 Copyright   : Copyright (c) Hou maoqing 2008
 Mail	: houmqing@163.com
 ============================================================================
 */

#include <aknquerydialog.h>
#include <avkon.hrh>
#include <eikmenup.h>
#include <eikgted.h>
#include <ImClientExam.rsg>
#include <utf.h> 

#include "ImMsgEngine.h"
#include "ImUserLoginView.h"
#include "ImFriendListView.h"
#include "ImChatRecordView.h"
#include "ImMsgCommandID.h"
#include "ImRegistryResp.h"
#include "ImPublic.h"

#include "Sockets.pan"
#include "SocketsAppUi.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(CAknAppUi::EAknEnableSkin);

    // Create view
    m_viewUserLogin = new (ELeave) CImUserLoginView;
	CleanupStack::PushL( m_viewUserLogin );
	m_viewUserLogin->ConstructL();
	AddViewL( m_viewUserLogin ); // transfer ownership to CAknViewAppUi
	CleanupStack::Pop(); // view1
	
	m_viewFriendList = new (ELeave) CImFriendListView;
	CleanupStack::PushL( m_viewFriendList );
	m_viewFriendList->ConstructL();
	AddViewL( m_viewFriendList ); // transfer ownership to CAknViewAppUi
	CleanupStack::Pop(); // view1
	
	m_viewChatRecord = new (ELeave) CImChatRecordView;
	CleanupStack::PushL( m_viewChatRecord );
	m_viewChatRecord->ConstructL();
	AddViewL( m_viewChatRecord ); // transfer ownership to CAknViewAppUi
	CleanupStack::Pop(); // view1

	//创建消息处理引擎
	iImMsgEngine=CImMsgEngine::NewL();
	iImMsgEngine->SetObserver(this);
	
    //创建通信引擎Create engine
    iSocketsEngine = CSocketsEngine::NewL( *iImMsgEngine );
    iSocketsEngine->SetServerName(_L("127.0.0.1"));
    iSocketsEngine->SetPort(2000);
    iSocketsEngine->ConnectL();

    //SetDefaultViewL(*m_viewUserLogin);
    SetDefaultViewL(*m_viewUserLogin);
    }

// -----------------------------------------------------------------------------
// CSocketsAppUi::~CSocketsAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CSocketsAppUi::~CSocketsAppUi()
    {
    delete iImMsgEngine;
    iImMsgEngine=NULL;
    
    delete iSocketsEngine;
    iSocketsEngine = NULL;
    }

// -----------------------------------------------------------------------------
// CSocketsAppUi::HandleCommandL()
// Handles user menu selections.
// -----------------------------------------------------------------------------
//
void CSocketsAppUi::HandleCommandL( TInt aCommand )
    {
    switch( aCommand )
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            Exit();
            break;
        default:
            User::Panic ( KPanicSockets, ESocketsBasicUi );
            break;
        }
    }

// -----------------------------------------------------------------------------
// CSocketsAppUi::DynInitMenuPaneL()
// Initialises a menu pane before it is displayed.
// -----------------------------------------------------------------------------
//
void CSocketsAppUi::DynInitMenuPaneL( TInt aMenuId, CEikMenuPane* aMenuPane )
    {
    
    }

// -----------------------------------------------------------------------------
// CSocketsAppUi::HandleKeyEventL()
// Handles user key input.
// -----------------------------------------------------------------------------
//
TKeyResponse CSocketsAppUi::HandleKeyEventL( const TKeyEvent& aKeyEvent,
                                             TEventCode aType )
    {
    return( EKeyWasNotConsumed );
    }

void CSocketsAppUi::HandleImMessage(CImMsg* pMsg)
	{
	switch(pMsg->m_nCommandID)
		{
		case ImRegisterUserResp:
			{
			if(pMsg->m_nRespResult==0)
				{
				CImRegistryResp* pRespBody=(CImRegistryResp*)pMsg->m_pBody;
				int nLen=StringLen(pRespBody->m_strUserAcc);
				TPtr8 ptr8(pRespBody->m_strUserAcc,nLen,nLen);
				m_bufMyAcc.Copy(ptr8);
				
				ActivateLocalViewL(KImFriendListViewId);
				}
			else
				iEikonEnv->InfoMsg( _L("Register failed.") );
			break;
			}
		case ImUserLoginResp:
			{
			if(pMsg->m_nRespResult==0)
				{
				ActivateLocalViewL(KImFriendListViewId);
				m_viewFriendList->SendGetFriendListMessage();
				}
			else
				iEikonEnv->InfoMsg( _L("Login failed.") );
			break;
			}
		
		case ImGetFriendListResp:
			{
			if(pMsg->m_nRespResult==0)
				{
				m_viewFriendList->HandleFriendListMessage(pMsg->m_pBody);
				}
			else
				iEikonEnv->InfoMsg( _L("Get Friend List failed.") );
			break;
			}
			
		case ImNotifyReceivedMessage:
			{
			if(pMsg->m_nRespResult==0)
				{
				if(!m_viewChatRecord->IsForeground())
					ActivateLocalViewL(KImChatRecordViewId);
				
				int nLen=StringLen(pMsg->m_strSrcUser);
				TPtr8 ptr8(pMsg->m_strSrcUser,nLen,nLen);
				m_viewChatRecord->m_bufFriendAcc.Copy(ptr8);
				m_viewChatRecord->HandleImMessageNotify(pMsg->m_pBody);
				}
			break;
			}
			
		case ImNotifyFriendStatusChange:
			{
			if(pMsg->m_nRespResult==0)
				{
				m_viewFriendList->HandleImNotifyFriendStatusChange(pMsg->m_pBody);
				}
			break;
			}
			
		default:
			break;
		}
	}

void CSocketsAppUi::SendImMessage(CImMsg* pImMsg)
	{
	TUint8* pMsgBuf=NULL;
	TInt nMsgLen=0;
	pImMsg->Encode(&pMsgBuf,nMsgLen);
	CleanupStack::PushL(pMsgBuf);
	
	TPtrC8 ptr8(pMsgBuf,nMsgLen);
	iSocketsEngine->WriteL(ptr8);
	CleanupStack::PopAndDestroy(1);
	}

// End of File

⌨️ 快捷键说明

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