// ChattersListView.cpp : implementation file
//
#include "stdafx.h"
#include "ChatServer.h"
#include "ChattersListView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
void CALLBACK EXPORT TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
);
BOOL bLoggedOn = FALSE;
HWND gListCtrl;
UINT iTimerEvent = 0;
#define NICK_WIDTH 3
#define JOIN_WIDTH 8
#define DURN_WIDTH 6
#define IPNO_WIDTH 3
/////////////////////////////////////////////////////////////////////////////
// CChattersListView
IMPLEMENT_DYNCREATE(CChattersListView, CListView)
CChattersListView::CChattersListView()
{
iRowNo = 0;
bLoggedOn = FALSE;
}
CChattersListView::~CChattersListView()
{
}
BEGIN_MESSAGE_MAP(CChattersListView, CListView)
//{{AFX_MSG_MAP(CChattersListView)
ON_WM_SIZE()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChattersListView drawing
void CChattersListView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
/////////////////////////////////////////////////////////////////////////////
// CChattersListView diagnostics
#ifdef _DEBUG
void CChattersListView::AssertValid() const
{
CListView::AssertValid();
}
void CChattersListView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChattersListView message handlers
BOOL CChattersListView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT;
return CListView::PreCreateWindow(cs);
}
void CChattersListView::OnSize(UINT nType, int cx, int cy)
{
CListView::OnSize(nType, cx, cy);
if(GetSafeHwnd())
{
GetListCtrl().SetColumnWidth(0, cx / NICK_WIDTH);
GetListCtrl().SetColumnWidth(1, cx / JOIN_WIDTH);
GetListCtrl().SetColumnWidth(2, cx / DURN_WIDTH);
GetListCtrl().SetColumnWidth(3, cx / IPNO_WIDTH);
}
}
int CChattersListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListView::OnCreate(lpCreateStruct) == -1)
return -1;
LOGFONT lf; // Used to create the CFont.
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 16; // Request a 20-pixel-high font
strcpy(lf.lfFaceName, "Verdana"); // with face name "Arial".
m_font.CreateFontIndirect(&lf); // Create the font.
SetFont(&m_font);
CWinApp* pApp = AfxGetApp();
m_pImageList.Create(16, 16, ILC_COLOR8 | ILC_MASK, 9, 9);
m_pImageList.Add(pApp->LoadIcon(IDI_CHATTER));
GetListCtrl().SetImageList(&m_pImageList , LVSIL_SMALL);
CRect rect;
GetClientRect(&rect);
GetListCtrl().InsertColumn(0, "Nickname" , rect.right / NICK_WIDTH, -1);
GetListCtrl().InsertColumn(1, "Joined" , rect.right / JOIN_WIDTH, -1);
GetListCtrl().InsertColumn(2, "Duration" , rect.right / DURN_WIDTH, -1);
GetListCtrl().InsertColumn(3, "IP Address" , rect.right / IPNO_WIDTH, -1);
gListCtrl = GetListCtrl().m_hWnd;
iTimerEvent = SetTimer(0 , 1000*60, (TIMERPROC)TimerProc );
return 0;
}
void CALLBACK EXPORT TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
{
if(!bLoggedOn)
return;
for(int iItem = 0 ; iItem < ListView_GetItemCount(gListCtrl); iItem++)
{
LVITEM lvi;
lvi.mask = LVIF_PARAM;
lvi.iSubItem = 2;
lvi.iItem = iItem;
ListView_GetItem(gListCtrl , &lvi);
long lStartSecs = (long)lvi.lParam;
time_t t;
struct tm *ptm;
time(&t);
ptm = localtime(&t);
long lCurrentSecs , lActualSecs;
lCurrentSecs = (ptm->tm_hour * 3600) + (ptm->tm_min * 60) + ptm->tm_sec;
lActualSecs = lCurrentSecs - lStartSecs;
int lHours , lMins , lSecs;
lHours = lActualSecs / 3600;
lMins = (lActualSecs - lHours * 3600) / 60;
lSecs = lActualSecs - (lHours * 3600) - (lMins * 60);
char sDuration[11];
sprintf(sDuration, "%d Hr %d Min", lHours , lMins);
ListView_SetItemText(gListCtrl, iItem , 2 , sDuration);
}
}
BOOL CChattersListView::DestroyWindow()
{
KillTimer(iTimerEvent);
return CListView::DestroyWindow();
}
void CChattersListView::UpdateChatters(CString sNickName , CString sIPAddress, UINT iPort)
{
bLoggedOn = TRUE;
LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.iItem = iRowNo++;
lvi.iSubItem = 0;
lvi.iImage = 0;
lvi.pszText = sNickName.GetBuffer(sNickName.GetLength());
int iActualItem = GetListCtrl().InsertItem(&lvi);
time_t t;
struct tm *ptm;
time(&t);
ptm = localtime(&t);
CString sDate;
sDate.Format("%.19s", asctime(ptm));
int lHours , lMins , lSecs;
lHours = atoi(sDate.Mid(11,2));
lMins =atoi(sDate.Mid(14,2));
lSecs = atoi(sDate.Mid(17,2));
CString sDisplayTime;
sDisplayTime.Format("%d:%d" , lHours , lMins);
lvi.mask = LVIF_TEXT;
lvi.iItem = iActualItem;
lvi.iSubItem = 1;
lvi.pszText = sDisplayTime.GetBuffer(sDisplayTime.GetLength());
GetListCtrl().SetItem(&lvi);
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = 2;
long lStartSec;
lStartSec = (lHours * 3600) + (lMins * 60) + lSecs;
char sDuration[20];
lstrcpy(sDuration, "");
lvi.pszText = sDuration;
lvi.lParam = (long)lStartSec;
GetListCtrl().SetItem(&lvi);
GetListCtrl().SetItemData(iActualItem , lStartSec);
CString sIPPort;
sIPPort.Format("%s:%d", sIPAddress , iPort);
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 3;
lvi.pszText = sIPPort.GetBuffer(sIPPort.GetLength());
GetListCtrl().SetItem(&lvi);
}
void CChattersListView::DeleteChatter(CString sNickName)
{
LVFINDINFO lvfi;
lvfi.flags = LVFI_STRING;
lvfi.psz = sNickName.GetBuffer(sNickName.GetLength());
int nFoundAt = GetListCtrl().FindItem(&lvfi , -1);
if(nFoundAt != -1)
{
GetListCtrl().DeleteItem(nFoundAt);
GetListCtrl().RedrawItems(0, GetListCtrl().GetItemCount());
}
}