/****************************************************************/
/*																*/
/*  OnlineUsersPage.cpp											*/
/*																*/
/*  Implementation of the COnlineUsersPage class.				*/
/*	This class is a part of the FTP Server Application			*/
/*																*/
/*  Programmed by LYFZ van der Meer							*/
/*  Copyright LYFZ Software Solutions 2002						*/
/*	http://www.LYFZvandermeer.nl								*/
/*																*/
/*  Last updated: 10 july 2002									*/
/*																*/
/****************************************************************/


#include "stdafx.h"
#include "DBServer.h"
#include "theDBServer.h"
#include "OnlineUsersPage.h"
#include "UserAccountsDlg.h"
#include "DBServerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CStringArray g_connuserarray;
extern CStringArray g_logintimearray;
extern CtheDBServer theServer;

IMPLEMENT_DYNCREATE(COnlineUsersPage, CDialog)

COnlineUsersPage::COnlineUsersPage() : CDialog(COnlineUsersPage::IDD)
{
	//{{AFX_DATA_INIT(COnlineUsersPage)
	//}}AFX_DATA_INIT
}

COnlineUsersPage::~COnlineUsersPage()
{
}

void COnlineUsersPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COnlineUsersPage)
	DDX_Control(pDX, IDC_ONLINE_USERS, m_OnlineUsers);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COnlineUsersPage, CDialog)
	//{{AFX_MSG_MAP(COnlineUsersPage)
	ON_WM_SIZE()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(IDC_KICK_USER, OnKickUser)
	ON_COMMAND(IDC_EDIT_USER, OnEditUserAccount)
	ON_COMMAND(IDC_BLOCK_IP, OnBlockIp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



/********************************************************************/
/*																	*/
/* Function name : OnInitDialog										*/		
/* Description   : Called by the framework in response to the		*/
/*				   WM_INITDIALOG message.							*/
/*																	*/
/********************************************************************/
BOOL COnlineUsersPage::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_OnlineUsers.InsertColumn(0, "ThreadID");			
	m_OnlineUsers.InsertColumn(1, "IP��ַ");	
	m_OnlineUsers.InsertColumn(2, "��½ʱ��");
	
	DWORD dwStyle = m_OnlineUsers.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
    m_OnlineUsers.SetExtendedStyle(dwStyle);
	return TRUE;
}


/********************************************************************/
/*																	*/
/* Function name : OnSize											*/		
/* Description   : Handle WM_SIZE message							*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if (IsWindow(::GetDlgItem(m_hWnd, IDC_ONLINE_USERS)))
	{
		CRect rect;
		GetClientRect(rect);
		m_OnlineUsers.MoveWindow(rect);
		m_OnlineUsers.SetColumnWidth(0, 0);
		m_OnlineUsers.SetColumnWidth(1, rect.Width()/2-2);
		m_OnlineUsers.SetColumnWidth(2, rect.Width()/2-2);
 
	}	
}


/********************************************************************/
/*																	*/
/* Function name : AddUser											*/
/* Description   : Add new connected user to list					*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::AddUser(DWORD nThreadID, LPCTSTR lpszName, LPCTSTR lpszAddress)
{
/*	CString strThreadID;
	strThreadID.Format("%d", nThreadID);

	LVFINDINFO info;
	
	info.flags = LVFI_PARTIAL|LVFI_STRING;
	info.psz = (LPCTSTR)strThreadID;

	int nIndex = m_OnlineUsers.FindItem(&info);
	if (nIndex == -1)
	{
		nIndex = m_OnlineUsers.InsertItem(0, strThreadID);
	}

	m_OnlineUsers.SetItemText(nIndex, 1, lpszName);
	m_OnlineUsers.SetItemText(nIndex, 2, lpszAddress);
	m_OnlineUsers.SetItemText(nIndex, 3, CTime::GetCurrentTime().Format("%H:%M:%S"));*/
	
}


/********************************************************************/
/*																	*/
/* Function name : RemoveUser										*/
/* Description   : Remove disconnected user from list				*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::RemoveUser(DWORD nThreadID)
{
	LVFINDINFO info;
	
	CString strThreadID;
	strThreadID.Format("%d", nThreadID);

	info.flags = LVFI_PARTIAL|LVFI_STRING;
	info.psz = (LPCTSTR)strThreadID;

	int nIndex = m_OnlineUsers.FindItem(&info);
	if (nIndex != -1)
	{
		m_OnlineUsers.DeleteItem(nIndex);
	}
}


/********************************************************************/
/*																	*/
/* Function name : OnContextMenu									*/
/* Description   : Show context menu								*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// get selected user
 		

}


/********************************************************************/
/*																	*/
/* Function name : OnKickUser										*/
/* Description   : Close connection for this user(s)				*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::OnKickUser() 
{
 
}


/********************************************************************/
/*																	*/
/* Function name : OnEditUserAccount								*/
/* Description   : Change user rights of selected user				*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::OnEditUserAccount() 
{
 
}


/********************************************************************/
/*																	*/
/* Function name : OnBlockIp										*/
/* Description   : Change user rights of selected user				*/
/*																	*/
/********************************************************************/
void COnlineUsersPage::OnBlockIp() 
{
 
}
extern CtheDBServer *g_pWndServer;

void COnlineUsersPage::RefreshData()
{
#ifdef CONNCOUNT_VERSION
	m_OnlineUsers.DeleteAllItems ();
 
	int i=0;    CString strThreadID;
	for(i=0; i<g_logintimearray.GetSize (); i++)
	{
	strThreadID.Format("%d", i+1);
	m_OnlineUsers.InsertItem(i, strThreadID);
	m_OnlineUsers.SetItemText(i, 1, g_connuserarray.ElementAt (i));
	m_OnlineUsers.SetItemText(i, 2, g_logintimearray.ElementAt (i)); 
	} 
	i=g_logintimearray.GetSize ();
	m_OnlineUsers.InsertItem(i, "");i++;

	strThreadID.Format("%d", g_logintimearray.GetSize());
	m_OnlineUsers.InsertItem(i, "");
	m_OnlineUsers.SetItemText(i, 1, "������ϯ��:"+strThreadID);
	m_OnlineUsers.SetItemText(i, 2, ""); 

	g_pWndServer->m_CriticalSection.Unlock();
#else
	m_OnlineUsers.DeleteAllItems ();
	g_pWndServer->m_CriticalSection.Lock();
	CConnectThread *pThread;
 
	CStringArray iparray;
	POSITION   pos;
	pos=g_pWndServer->m_ThreadList.GetHeadPosition();
	int i=0;    CString strThreadID;
	while(pos)
	{
		pThread=g_pWndServer->m_ThreadList.GetNext(pos);
 	int nThreadID = pThread->m_nThreadID;

	strThreadID.Format("%d", nThreadID);
 
	m_OnlineUsers.InsertItem(i, strThreadID);
	m_OnlineUsers.SetItemText(i, 1, pThread->m_strRemoteHost);
	m_OnlineUsers.SetItemText(i, 2, pThread->m_LastDataTransferTime.Format("%H:%M:%S")); 
	i++;

	if(g_localip==pThread->m_strRemoteHost)continue;
	if(::FindArray(&iparray, pThread->m_strRemoteHost)==-1)
		iparray.Add(pThread->m_strRemoteHost);

	} 
	m_OnlineUsers.InsertItem(i, "");i++;

	strThreadID.Format("%d", iparray.GetSize());
	m_OnlineUsers.InsertItem(i, "");
	m_OnlineUsers.SetItemText(i, 1, "������ϯ��:"+strThreadID);
	m_OnlineUsers.SetItemText(i, 2, ""); 

	g_pWndServer->m_CriticalSection.Unlock();
#endif
}