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

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.

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

// ClientDevicePane.cpp : implementation file
//

#include "stdafx.h"
#include "scts3devman.h"
#include "ClientDevicePane.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClientDevicePane dialog


CClientDevicePane::CClientDevicePane(SctsDMClientDevice *_device,CWnd* pParent /*=NULL*/)
	: CDialog(CClientDevicePane::IDD, pParent),device(_device)
{
	//{{AFX_DATA_INIT(CClientDevicePane)
	m_id = _T("");
	m_maxmsgsize = 20000;
	m_maxobjsize = 0;
	m_wbxml = 0;
	m_testpc = _T("");
	m_testpcp = _T("");
	m_testpnv = _T("");
	m_largeObjects = FALSE;
	m_dmAccPath = _T("");
	//}}AFX_DATA_INIT
}


void CClientDevicePane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDevicePane)
	DDX_Text(pDX, IDC_CDEVID, m_id);
	DDX_Text(pDX, IDC_CDEVMAXMSG, m_maxmsgsize);
	DDX_Text(pDX, IDC_CDEVMAXOBJ, m_maxobjsize);
	DDX_Radio(pDX, IDC_CDEVWBXML, m_wbxml);
	DDX_Text(pDX, IDC_CDEVPC, m_testpc);
	DDX_Text(pDX, IDC_CDEVPCP, m_testpcp);
	DDX_Text(pDX, IDC_CDEVPNV, m_testpnv);
	DDX_Check(pDX, IDC_CDEVLARGEOBJS, m_largeObjects);
	DDX_Text(pDX, IDC_CDMACCPATH, m_dmAccPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClientDevicePane, CDialog)
	//{{AFX_MSG_MAP(CClientDevicePane)
	ON_EN_CHANGE(IDC_CDEVMAXMSG, OnUpdate)
	ON_BN_CLICKED(IDC_CDEVAPPLY, OnApply)
	ON_BN_CLICKED(IDC_CDEVDISCARD, OnCdevdiscard)
	ON_EN_CHANGE(IDC_CDEVMAXOBJ, OnUpdate)
	ON_BN_CLICKED(IDC_CDEVWBXML, OnUpdate)
	ON_BN_CLICKED(IDC_CDEVXML, OnUpdate)
	ON_EN_CHANGE(IDC_CDEVPNV, OnUpdate)
	ON_EN_CHANGE(IDC_CDEVPCP, OnUpdate)
	ON_EN_CHANGE(IDC_CDEVPC, OnUpdate)
	ON_BN_CLICKED(IDC_CDEVLARGEOBJS, OnCdevlargeobjs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDevicePane message handlers

BOOL CClientDevicePane::OnInitDialog() 
{
	LOGFONT logFont;
	CDialog::OnInitDialog();
	
	CFont *font=GetDlgItem(IDC_CDEVINFO)->GetFont();
	CFont tempFont;
	font->GetLogFont(&logFont);
	logFont.lfWeight=FW_BOLD;
	tempFont.CreateFontIndirect(&logFont);
	GetDlgItem(IDC_CDEVINFO)->SetFont(&tempFont);
	GetDlgItem(IDC_CDEVREP)->SetFont(&tempFont);

	/* Disable device name if its not a new device */
	update=!device;
	GetDlgItem(IDC_CDEVID)->EnableWindow(update);
	GetDlgItem(IDC_CDEVCHANGE)->ShowWindow(update);
	GetDlgItem(IDC_CDEVAPPLY)->ShowWindow(update);
	GetDlgItem(IDC_CDEVDISCARD)->ShowWindow(update);
    /* DM 1.2 DMAcc node */
	GetDlgItem(IDC_CDMACCPATH)->EnableWindow(update);
	if (device) Refresh();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CClientDevicePane::OnUpdate() 
{
	if (update) return;
	update=true;
	GetDlgItem(IDC_CDEVCHANGE)->ShowWindow(TRUE);
	GetDlgItem(IDC_CDEVAPPLY)->ShowWindow(TRUE);
	GetDlgItem(IDC_CDEVDISCARD)->ShowWindow(TRUE);
}

BOOL CClientDevicePane::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);
}

void CClientDevicePane::Refresh()
{
	SctsDMNode *node=device->FindDMNode("./DevDetail/LrgObj");
	SCTSASSERT(node && node->data);
	m_largeObjects=sctsStrcmp((char *)node->data,"false");
	GetDlgItem(IDC_CDEVMAXOBJ)->EnableWindow(m_largeObjects);
	m_id=device->devID;
    m_dmAccPath=device->dmAccPath;
	m_maxmsgsize=device->maxMsgSize;
	m_maxobjsize=device->maxObjSize;
	m_testpc=device->productCompany;
	m_testpcp=device->productContact;
	m_testpnv=device->productName;
	if (device->encoding==WBXML)
		m_wbxml=0;
	else
		m_wbxml=1;
	UpdateData(FALSE);
}

void CClientDevicePane::OnApply() 
{
	char *pContact=0;
	char *pCompany=0;
	char *pName=0;
	char *pDevID=0;
	SctsAuth *iauth=0;
	SctsDMNode *node;
	UpdateData(TRUE);
	/* Check maxmsgsize */
	if (m_maxmsgsize>MAX_WORKSPACE_SIZE/2)
	{
		char message[200];
		sprintf(message,"Maximum message size less that or equal to %d.",MAX_WORKSPACE_SIZE/2);
		MessageBox(message,"Device", MB_ICONEXCLAMATION);
		GetDlgItem(IDC_CDEVMAXMSG)->SetFocus();
		return;
	}
	if (m_maxobjsize && m_maxobjsize<=m_maxmsgsize)
	{
		MessageBox("Maximum object size must be greater than the maximum message size.","Device",MB_ICONEXCLAMATION);
		GetDlgItem(IDC_CDEVMAXOBJ)->SetFocus();
		return;
	}
	if (m_testpnv!="") pName=(char *)(LPCTSTR)m_testpnv;
	if (m_testpc!="") pCompany=(char *)(LPCTSTR)m_testpc;
	if (m_testpcp!="") pContact=(char *)(LPCTSTR)m_testpcp;
	if (!device)
	{
		char *id=(char *)(LPCTSTR)m_id;
		if (m_id.IsEmpty())
		{
			MessageBox("Device ID cannot be empty.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_CDEVID)->SetFocus();
			return;
		}
		char *dmAccPath=(char *)(LPCTSTR)m_dmAccPath;
		if (m_dmAccPath.IsEmpty())
		{
			MessageBox("Account URI cannot be empty.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_CDMACCPATH)->SetFocus();
			return;
		}
		if (m_dmAccPath.GetAt(m_dmAccPath.GetLength()-1) == '/')
		{
			MessageBox("Account URI can not be ended by /.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_CDMACCPATH)->SetFocus();
			return;
		}
		/* Check if there is already a device with the same device id  */
		std::map <std::string,SctsDMClientDevice *>::iterator devicesIt;
		sctsStatusLog("Adding client device (");
		sctsStatusLog(id);
		sctsStatusLog(")...\r\n");
		if ((devicesIt=SctsDMDevice::clientDevices.find(id))!=SctsDMDevice::clientDevices.end())
		{
			MessageBox("Device with same name already exists.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_CDEVID)->SetFocus();
			return;
		}
		device=SctsDMDevice::AddClientDevice(id,dmAccPath,m_maxobjsize,m_wbxml?WBXML:XML,pName,pContact,pCompany);
		device->Lock();
		GetDlgItem(IDC_CDEVID)->EnableWindow(FALSE);
		device->maxMsgSize=m_maxmsgsize;
		device->maxObjSize=m_maxobjsize;
		if (m_largeObjects)
		{
			node=device->FindDMNode("./DevDetail/LrgObj");
			SCTSASSERT(node && node->data);
			sctsFree(node->data);
			node->data=sctsStrdup("true");
			node->size=4;
		}

		if (m_wbxml)
			device->encoding=XML;
		else
			device->encoding=WBXML;
		device->Unlock();
		navigator->SelectItem((HTREEITEM)device->uiData);
		return;
	}
	/* Ref#0004 : Apply changes only if device can be locked */
	if (!device->Lock(500))
	{
		MessageBox("Cannot apply changes to device while session is active.","Device",MB_ICONEXCLAMATION);
		return;
	}
	device->maxMsgSize=m_maxmsgsize;
	/* Large Object Delivery */
	device->maxObjSize=m_maxobjsize;
	node=device->FindDMNode("./DevDetail/LrgObj");
	SCTSASSERT(node && node->data);
	sctsFree(node->data);
	node->data=sctsStrdup(m_largeObjects?"true":"false");
	node->size=m_largeObjects?4:5;
	if (m_wbxml)
		device->encoding=XML;
	else
		device->encoding=WBXML;
	if (device->productName)
	{
		sctsFree(device->productName);
		device->productName=0;
	}
	if (device->productCompany)
	{
		sctsFree(device->productCompany);
		device->productCompany=0;
	}
	if (device->productContact)
	{
		sctsFree(device->productContact);
		device->productContact=0;
	}
	if (pName) device->productName=sctsStrdup(pName);
	if (pContact) device->productContact=sctsStrdup(pContact);
	if (pCompany) device->productCompany=sctsStrdup(pCompany);
	device->Unlock();
	GetDlgItem(IDC_CDEVCHANGE)->ShowWindow(FALSE);
	GetDlgItem(IDC_CDEVAPPLY)->ShowWindow(FALSE);
	GetDlgItem(IDC_CDEVDISCARD)->ShowWindow(FALSE);
	update=false;
	
}

void CClientDevicePane::OnCdevdiscard() 
{
	if (device)
	{
		update=false;
		GetDlgItem(IDC_CDEVCHANGE)->ShowWindow(FALSE);
		GetDlgItem(IDC_CDEVAPPLY)->ShowWindow(FALSE);
		GetDlgItem(IDC_CDEVDISCARD)->ShowWindow(FALSE);
		Refresh();
	}
	else
		navigator->SelectItem(0);
}

void CClientDevicePane::OnCdevlargeobjs() 
{
	UpdateData(TRUE);
	GetDlgItem(IDC_CDEVMAXOBJ)->EnableWindow(m_largeObjects);
	OnUpdate();
}
