/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
// ServersPane.cpp : implementation file
//

#include "stdafx.h"
#include "SCTS3DevMan.h"
#include "ServersPane.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static char serverStateString[][10]={"Running","Stopped","Stopping","Error"};

void serversUICallback(void *uiCallbackData,SctsTransportServer *server,SctsTransportServerState state)
{
	if (server==httpServer) 
	{
		if (uiCallbackData) 
		{
			((CListCtrl *)uiCallbackData)->SetItemText(0,1,serverStateString[(int)state]);
			((CListCtrl *)uiCallbackData)->SetItemData(0,(DWORD)state);
		}
		httpServerState=state;
		if (state==SERVER_STOPPED || state==SERVER_ERROR) httpServer=0;
	}
	if (server==obexTCPServer) 
	{
		if (uiCallbackData) ((CListCtrl *)uiCallbackData)->SetItemText(1,1,serverStateString[(int)state]);
		obexTCPServerState=state;
		if (state==SERVER_STOPPED || state==SERVER_ERROR) obexTCPServer=0;
	}
	if (server==obexIRServer) 
	{
		if (uiCallbackData) ((CListCtrl *)uiCallbackData)->SetItemText(2,1,serverStateString[(int)state]);
		obexIRServerState=state;
		if (state==SERVER_STOPPED || state==SERVER_ERROR) obexIRServer=0;
	}	
}

/////////////////////////////////////////////////////////////////////////////
// CServersPane dialog


CServersPane::CServersPane(CWnd* pParent /*=NULL*/)
	: CDialog(CServersPane::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServersPane)
	//}}AFX_DATA_INIT
}


void CServersPane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServersPane)
	DDX_Control(pDX, IDC_SERVERPORT, m_edit);
	DDX_Control(pDX, IDC_SERVERLIST, m_servers);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CServersPane, CDialog)
	//{{AFX_MSG_MAP(CServersPane)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_SERVERLIST, OnItemchangedServerlist)
	ON_BN_CLICKED(IDC_SERVERSTART, OnServerstart)
	ON_BN_CLICKED(IDC_SERVERSTOP, OnServerstop)
	ON_WM_DESTROY()
	ON_NOTIFY(NM_CLICK, IDC_SERVERLIST, OnClickServerlist)
	ON_EN_KILLFOCUS(IDC_SERVERPORT, OnKillfocusServerport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServersPane message handlers

BOOL CServersPane::OnInitDialog() 
{
	CDialog::OnInitDialog();
	char buffer[10];
	m_servers.InsertColumn(0,"Server",LVCFMT_LEFT,155);
	m_servers.InsertColumn(1,"State",LVCFMT_LEFT,87);
	m_servers.InsertColumn(2,"Port/Service",LVCFMT_LEFT,87);
	m_servers.InsertItem(0,"HTTP");
	m_servers.SetItemText(0,1,serverStateString[(int)httpServerState]);
	m_servers.SetItemText(0,2,itoa(httpServerPort,buffer,10));
	m_servers.InsertItem(1,"OBEX/TCP");
	m_servers.SetItemText(1,1,serverStateString[(int)obexTCPServerState]);
	m_servers.SetItemText(1,2,itoa(obexTCPServerPort,buffer,10));
	m_servers.InsertItem(2,"OBEX/IR");
	m_servers.SetItemText(2,1,serverStateString[(int)obexIRServerState]);
	m_servers.SetItemText(2,2,obexIRServerService);

	m_servers.SetExtendedStyle(LVS_EX_FULLROWSELECT | m_servers.GetExtendedStyle());
	
	((CSpinButtonCtrl *)GetDlgItem(IDC_SERVERSPIN))->SetRange32(1,MAX_SESSION_TIMEOUT);
	((CSpinButtonCtrl *)GetDlgItem(IDC_SERVERSPIN))->SetBuddy(GetDlgItem(IDC_SERVERTIMEOUT));
	((CSpinButtonCtrl *)GetDlgItem(IDC_SERVERSPIN))->SetPos(sctsSessionTimeout/1000);
	SctsTransportServer::SetUICallback(&m_servers,serversUICallback);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CServersPane::OnItemchangedServerlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	bool stop;
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	*pResult = 0;
	if (pNMListView->uNewState&LVIS_SELECTED) selectedItem=pNMListView->iItem;
	if (pNMListView->uChanged&LVIF_PARAM || pNMListView->uNewState&LVIS_SELECTED)
	{
		stop=m_servers.GetItemText(selectedItem,1)=="Running";
		GetDlgItem(IDC_SERVERSTOP)->EnableWindow(stop);
		GetDlgItem(IDC_SERVERSTART)->EnableWindow(!stop);
	}
}

void CServersPane::OnServerstart() 
{
	switch (selectedItem)
	{
	case 0:
		httpServer=new SctsHttpTransportServer(httpServerPort);
		httpServer->Create();
		break;
	case 1:
		if (obexTCPState==2)
		{
			MessageBox("Cannot start OBEX/TCP server while OBEX/TCP client sessions are active.","OBEX/TCP server",MB_ICONEXCLAMATION);
			return;
		}
		obexTCPServer=new SctsObexTCPTransportServer(obexTCPServerPort);
		obexTCPServer->Create();
		break;
	case 2:
		if (obexIRState==2)
		{
			MessageBox("Cannot start OBEX/IR server while OBEX/IR client sessions are active.","OBEX/IR server",MB_ICONEXCLAMATION);
			return;
		}
		if (!strcmp(obexIRServerService,"OBEX") && !sctsStopIrmon()) return;
		obexIRServer=new SctsObexIRTransportServer(obexIRServerService);
		obexIRServer->Create();
		break;
	}
	GetDlgItem(IDC_SERVERSTART)->EnableWindow(FALSE);
	m_servers.SetItemState(selectedItem,0,LVIS_SELECTED);
}

void CServersPane::OnServerstop() 
{
	switch (selectedItem)
	{
	case 0:
		httpServer->SetTerminate(true);
		break;
	case 1:
		obexTCPServer->SetTerminate(true);
		break;
	case 2:
		obexIRServer->SetTerminate(true);
		break;
	}
	GetDlgItem(IDC_SERVERSTOP)->EnableWindow(FALSE);
	m_servers.SetItemState(selectedItem,0,LVIS_SELECTED);
}

void CServersPane::OnDestroy() 
{
	CDialog::OnDestroy();
	SctsTransportServer::SetUICallback(0,serversUICallback);
	sctsSessionTimeout=((CSpinButtonCtrl *)GetDlgItem(IDC_SERVERSPIN))->GetPos()*1000;
}

void CServersPane::OnClickServerlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	if (pNMListView->iSubItem==2 && m_servers.GetItemText(selectedItem,1)=="Stopped")
	{
		CRect rect;
		m_servers.GetSubItemRect(selectedItem,2,LVIR_LABEL,rect);
		m_edit.SetWindowText(m_servers.GetItemText(selectedItem,2));
		m_edit.SetSel(0,-1);
		m_edit.ShowWindow(SW_SHOW);
		m_servers.ClientToScreen(&rect);
		ScreenToClient(&rect);
		m_edit.MoveWindow(&rect);
		m_edit.SetFocus();
		editedItem=selectedItem;
	}
	*pResult = 0;
}

void CServersPane::OnKillfocusServerport() 
{
	char sPort[100]="0";
	int iPort;
	m_edit.GetWindowText(sPort+1,99);
	iPort=atoi(sPort);
	switch (editedItem)
	{
	case 0:
		if (iPort) 
		{
			itoa(iPort,sPort,10);
			httpServerPort=iPort;
			m_servers.SetItemText(0,2,sPort);
		}
		break;
	case 1:
		if (iPort)
		{
			itoa(iPort,sPort,10);
			obexTCPServerPort=iPort;
			m_servers.SetItemText(1,2,sPort);
		}
		break;
	case 2:
		if (sPort[1])
		{
			if (!strcmp(sPort+1,"OBEX") && !sctsStopIrmon()) break;
			free(obexIRServerService);
			obexIRServerService=strdup(sPort+1);
			m_servers.SetItemText(2,2,sPort+1);
		}
		break;
	}
	m_edit.ShowWindow(SW_HIDE);
}

BOOL CServersPane::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN && (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN)) return TRUE;
	
	return CDialog::PreTranslateMessage(pMsg);
}
