// ChatClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChatClient.h"
#include "ChatClientDlg.h"
#include "ChatProtocol.h"
#include "LoginDialog.h"
#include ".\chatclientdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChatClientDlg dialog
extern UINT WM_CHAT_TRANS;
extern UINT WM_CHAT_CONNECT;
extern UINT WM_CHAT_DISCONNECT;
CChatClientDlg::CChatClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatClientDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LOG, m_edtLog);
DDX_Control(pDX, IDC_LIST_USERS, m_lstUsers);
DDX_Control(pDX, IDC_BUTTON_SEND, m_btnSend);
DDX_Control(pDX, IDC_CHAT, m_edtChat);
}
BEGIN_MESSAGE_MAP(CChatClientDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_REGISTERED_MESSAGE(WM_CHAT_TRANS, OnChatTrans)
ON_REGISTERED_MESSAGE(WM_CHAT_CONNECT, OnChatConnect)
ON_REGISTERED_MESSAGE(WM_CHAT_DISCONNECT, OnChatDisconnect)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_SEND, OnBnClickedButtonSend)
ON_WM_DESTROY()
END_MESSAGE_MAP()
// CChatClientDlg message handlers
BOOL CChatClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
try
{
chat.Init(this->GetSafeHwnd());
chat.Connect("localhost");
}
catch (Datatal::DtSocketException& ex)
{
AfxMessageBox(ex.ToString());
}
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CChatClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CChatClientDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
LRESULT CChatClientDlg::OnChatConnect(WPARAM wp, LPARAM lp)
{
if (m_strUsername.IsEmpty())
{
CLoginDialog dlg;
dlg.DoModal();
m_strUsername = dlg.m_strUsername;
} // if (m_strUsername.IsEmpty())
chat.Login(m_strUsername, "none");
chat.GetUsers();
return TRUE;
}
LRESULT CChatClientDlg::OnChatDisconnect(WPARAM wp, LPARAM lp)
{
AfxMessageBox("Disconnected");
return TRUE;
}
LRESULT CChatClientDlg::OnChatTrans(WPARAM wp, LPARAM lp)
{
ChatProtocol::Packet* pInPacket = (ChatProtocol::Packet*)lp;
switch (pInPacket->nFunctionCode)
{
// got an answer from the login transaction.
case ChatProtocol::TC_LOGIN:
if (pInPacket->Status != ChatProtocol::TS_OK)
AfxMessageBox("Login failed!");
break;
case ChatProtocol::TC_SEND_MESSAGE_OUT:
AddMessage(pInPacket->pData);
break;
case ChatProtocol::TC_SEND_MESSAGE:
//we got a ACK
break;
case ChatProtocol::TC_LIST_USERS:
ListUsers(pInPacket->pData);
break;
default:
AfxMessageBox("Got junc transaction");
}
delete pInPacket;
return TRUE;
}
void CChatClientDlg::AddMessage(char* szBuffer)
{
CString strText, strTmp, strUser, strTo;
SYSTEMTIME stTime;
char *pszToken;
pszToken = strtok( szBuffer, "\04" );
if (!pszToken)
{
AfxMessageBox("Incorrect message");
return;
}
strUser = pszToken;
pszToken = strtok( NULL, "\04" );
if (!pszToken)
{
AfxMessageBox("Incorrect message");
return;
}
strTo = pszToken;
pszToken = strtok( NULL, "\04" );
if (!pszToken)
{
AfxMessageBox("Incorrect message");
return;
}
strTmp = pszToken;
m_edtLog.GetWindowText(strTmp);
GetLocalTime(&stTime);
if (strTo == "all")
strText.Format("%s\r\n[%.02d:%.02d] <%s> %s", strTmp, stTime.wHour, stTime.wMinute, strUser, pszToken);
else
strText.Format("%s\r\n[%.02d:%.02d] *%s* %s", strTmp, stTime.wHour, stTime.wMinute, strUser, pszToken);
m_edtLog.SetWindowText(strText);
m_edtLog.LineScroll(m_edtLog.GetLineCount());
}
void CChatClientDlg::ListUsers(char* szBuffer)
{
m_lstUsers.ResetContent();
printf("Buffer: %s\n", szBuffer);
char *pszToken;
pszToken = strtok( szBuffer, "\04" );
while( pszToken != NULL )
{
m_lstUsers.AddString(pszToken);
pszToken = strtok( NULL, "\04" );
}
}
void CChatClientDlg::OnBnClickedButtonSend()
{
CString strMsg;
m_edtChat.GetWindowText(strMsg);
m_edtChat.SetWindowText(NULL);
if (m_lstUsers.GetCurSel() != -1)
{
CString strSelUser;
m_lstUsers.GetText(m_lstUsers.GetCurSel(), strSelUser);
// we have selected someone for private messages.
if (strSelUser != m_strUsername)
{
chat.SendMessage(m_strUsername, strSelUser, strMsg);
char szPriv[512];
sprintf(szPriv, "%s%c%s%c%s", m_strUsername, 0x04, strSelUser, 0x04, strMsg);
AddMessage(szPriv);
return;
}
}
chat.SendMessage(m_strUsername, "all", strMsg);
}
void CChatClientDlg::OnDestroy()
{
chat.Disconnect(true);
CDialog::OnDestroy();
}