www.pudn.com > fanccMSNr.src.rar > DumbMessenger.cpp, change:2005-03-15,size:7495b


// DumbMessenger.cpp  
// 
 
#include "stdafx.h" 
#include "DumbMessenger.h" 
#include "MainFrm.h" 
#include "ChatFrame.h" 
#include "LoginDialog.h" 
#include "ConnectingView.h" 
#include "NotConnectedView.h" 
#include "ConnectedView.h" 
#include "SomeoneAddsYouDialog.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 
 
 
// CDumbMessengerApp 
 
BEGIN_MESSAGE_MAP(CDumbMessengerApp, CWinApp) 
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout) 
	ON_COMMAND(ID_FILE_LOGIN, OnFileLogin) 
	ON_UPDATE_COMMAND_UI(ID_FILE_LOGIN, OnUpdateFileLogin) 
END_MESSAGE_MAP() 
 
 
// CDumbMessengerApp  
 
CDumbMessengerApp::CDumbMessengerApp(): 
model(messageDispatcher) 
{ 
	model.statusName.insert(Property("NLN", "¿Â¶óÀÎ")); 
	model.statusName.insert(Property("FLN", "¿ÀÇÁ¶óÀÎ")); 
	model.statusName.insert(Property("HDN", "¿ÀÇÁ¶óÀÎÀ¸·Î Ç¥½Ã")); 
	model.statusName.insert(Property("IDL", "ÀÚ¸® ºñ¿ò")); 
	model.statusName.insert(Property("AWY", "ÀÚ¸® ºñ¿òÀ¸·Î Ç¥½Ã")); 
	model.statusName.insert(Property("BSY", "´Ù¸¥ ¿ë¹«Áß")); 
	model.statusName.insert(Property("BRB", "°ð µ¹¾Æ¿À°ÚÀ½")); 
	model.statusName.insert(Property("PHN", "ÅëÈ­ Áß")); 
	model.statusName.insert(Property("LUN", "½Ä»ç Áß")); 
 
	// InitInstance 
} 
 
 
//  CDumbMessengerApp  
 
CDumbMessengerApp theApp; 
 
// CDumbMessengerApp  
 
BOOL CDumbMessengerApp::InitInstance() 
{ 
	InitCommonControls(); 
 
	CWinApp::InitInstance(); 
 
	if (!AfxSocketInit()) 
	{ 
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED); 
		return FALSE; 
	} 
 
 
	SetRegistryKey(_T("·ÎÄà ÀÀ¿ë ÇÁ·Î±×·¥ ¸¶¹ý»ç¿¡¼­ »ý¼ºµÈ ÀÀ¿ë ÇÁ·Î±×·¥")); 
	mainFrame = new CMainFrame; 
	m_pMainWnd = mainFrame; 
	if (!mainFrame->LoadFrame(IDR_MAINFRAME)) 
		return FALSE; 
	HINSTANCE hInst = AfxGetResourceHandle(); 
	m_hMDIMenu  = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_DumbMessengerTYPE)); 
	m_hMDIAccel = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_DumbMessengerTYPE)); 
	mainFrame->ShowWindow(SW_SHOW); 
	mainFrame->UpdateWindow(); 
	 
	model.addMessengerListener(*this); 
	return TRUE; 
} 
 
void CDumbMessengerApp::updateMyStatus() { 
	if(model.getStatus()==MessengerModel::CONNECTED) { 
		string text="Dumb Messenger   "; 
		text.append(model.getMy().screenName); 
		text.append("   ("); 
 
		Properties::const_iterator i= model.statusName.find(model.getMy().status); 
		if(i!=model.statusName.end()) { 
			text.append((*i).second); 
		} 
		text.append(")"); 
 
 
		mainFrame->SetWindowText(text.data()); 
	} else { 
		mainFrame->SetWindowText("Dumb Messenger"); 
	} 
} 
 
void CDumbMessengerApp::messengerUpdate(MessengerModel &model, MessengerListener::Event event) { 
	switch(event) { 
	case MessengerListener::CONNECTING: 
		mainFrame->setView(RUNTIME_CLASS(CConnectingView)); 
		break; 
	case MessengerListener::CONNECTION_FAILED: 
		updateMyStatus(); 
		mainFrame->setView(RUNTIME_CLASS(CNotConnectedView)); 
		AfxMessageBox(_T("Á¬½Óʧ°Ü.")); 
		break; 
	case MessengerListener::CONNECTION_CLOSED_BY_OTHERS: 
		updateMyStatus(); 
		mainFrame->setView(RUNTIME_CLASS(CNotConnectedView)); 
		AfxMessageBox(_T("¶Ô·½½áÊøÁÄÌì.")); 
		break; 
	case MessengerListener::CONNECTION_CLOSED_BY_SERVER: 
		updateMyStatus(); 
		mainFrame->setView(RUNTIME_CLASS(CNotConnectedView)); 
		AfxMessageBox(_T("·þÎñÆ÷¶Ï¿ªÁ¬½Ó")); 
		break; 
	case MessengerListener::AUTH_FAILED: 
		updateMyStatus(); 
		mainFrame->setView(RUNTIME_CLASS(CNotConnectedView)); 
		AfxMessageBox(_T("µÇ¼·þÎñÆ÷ʧ°Ü£¬ÑéÖ¤´íÎó.")); 
		break; 
	case MessengerListener::CONNECTION_CLOSED: 
		updateMyStatus(); 
		mainFrame->setView(RUNTIME_CLASS(CNotConnectedView)); 
		break; 
	case MessengerListener::CONNECTED: 
		mainFrame->setView(RUNTIME_CLASS(CConnectedView)); 
		updateMyStatus(); 
		try { 
			CConnectedView &view=dynamic_cast(mainFrame->getView()); 
			view.setModel(model); 
		} catch(bad_cast) { 
			//do noting 
		} 
		break; 
	case MessengerListener::MY_STATUS_CHANGED: 
	case MessengerListener::MY_SCREEN_NAME_CHANGED: 
		updateMyStatus(); 
		break; 
	} 
} 
 
void CDumbMessengerApp::userUpdate(const User &user, MessengerListener::UserEvent event) { 
	if(event==MessengerListener::UserEvent::ADDS_YOU) { 
		CSomeoneAddsYouDialog dialog; 
		dialog.notice.Format("%s (%s)\r\nÇëÇó¼ÓΪºÃÓÑ.", user.ID.data(), user.screenName.data()); 
		dialog.DoModal(); 
		switch(dialog.accept) { 
		case CSomeoneAddsYouDialog::ACCEPTED: 
			model.addUser(user.ID); 
			break; 
		case CSomeoneAddsYouDialog::DECLINED: 
			model.blockUser(true, user.ID); 
			break; 
		} 
		return; 
	} 
 
	try { 
		CConnectedView &view= dynamic_cast(mainFrame->getView()); 
		CTreeCtrl &tree=view.GetTreeCtrl(); 
		tree.DeleteAllItems(); 
		HTREEITEM onlineNode  =tree.InsertItem(TVIF_TEXT, _T("Á¢¼ÓÁßÀÎ »ç¿ëÀÚ"), 0, 0, 0, 0, 0, TVI_ROOT, TVI_LAST); 
		HTREEITEM offlineNode =tree.InsertItem(TVIF_TEXT, _T("¿ÀÇÁ¶óÀÎ »ç¿ëÀÚ"), 0, 0, 0, 0, 0, TVI_ROOT, TVI_LAST); 
		HTREEITEM group; 
 
 
		int nUsers= (int)model.users.size(); 
 
		for(int i=0; iLoadFrame(IDR_CHATFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,  
					  NULL, NULL); 
	pFrame->ShowWindow(SW_SHOW); 
	pFrame->UpdateWindow(); 
	pFrame->setChatterModel(model); 
	m_aryFrames.Add(pFrame->GetSafeHwnd()); 
 
} 
 
 
// CDumbMessengerApp  
 
 
 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
	enum { IDD = IDD_ABOUTBOX }; 
 
protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV Áö¿ø 
 
protected: 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
END_MESSAGE_MAP() 
 
void CDumbMessengerApp::OnAppAbout() 
{ 
	CAboutDlg aboutDlg; 
	aboutDlg.DoModal(); 
} 
 
 
// CDumbMessengerApp  
 
 
void CDumbMessengerApp::OnFileLogin() 
{ 
	CLoginDialog dlg; 
 
	 
	switch(model.getStatus()) { 
	case MessengerModel::NOT_CONNECTED: 
		try { 
			dlg.DoModal(); 
			if(dlg.ok) { 
				model.login((char*)((LPCTSTR)dlg.ID), (char*)((LPCTSTR)dlg.password)); 
			} 
		} catch(SocketException e) { 
			model.cancelLogin(); 
			AfxMessageBox(_T("ÍøÂçÒì³£.")); 
		} 
		break; 
	case MessengerModel::CONNECTING: 
		model.cancelLogin(); 
		break; 
	case MessengerModel::CONNECTED: 
		model.logout(); 
		break; 
	} 
} 
 
void CDumbMessengerApp::OnUpdateFileLogin(CCmdUI *pCmdUI) 
{ 
	switch(model.getStatus()) { 
	case MessengerModel::NOT_CONNECTED: 
		pCmdUI->SetText(_T("怬(&L)")); 
		break; 
	case MessengerModel::CONNECTING: 
		pCmdUI->SetText(_T("Í˳ö(&C)")); 
		break; 
	case MessengerModel::CONNECTED: 
		pCmdUI->SetText(_T("×¢Ïú(&N)")); 
		break; 
	} 
	 
}