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

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.

********************************************************************/
// ServerDevicePane2.cpp : implementation file
//

#include "stdafx.h"
#include "scts3devman.h"
#include "ServerDevicePane2.h"
#include "SctsDevManDMAccount.h"

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

/////////////////////////////////////////////////////////////////////////////
// CServerDevicePane2 dialog


CServerDevicePane2::CServerDevicePane2(SctsDMServerDevice *_device,CWnd* pParent /*=NULL*/)
	: CDialog(CServerDevicePane2::IDD, pParent),device(_device),update(false)
{
	//{{AFX_DATA_INIT(CServerDevicePane2)
	m_id = _T("");
	m_company = _T("");
	m_contact = _T("");
	m_name = _T("");
	m_cauth = 0;
	m_sauth = 0;
	m_dmAccPath = _T("");
	//}}AFX_DATA_INIT
}


void CServerDevicePane2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDevicePane2)
	DDX_Text(pDX, IDC_SDEVID, m_id);
	DDX_Text(pDX, IDC_SDEVPC, m_company);
	DDX_Text(pDX, IDC_SDEVPCP, m_contact);
	DDX_Text(pDX, IDC_SDEVPNV, m_name);
	DDX_Radio(pDX, IDC_SDEVCAUTHNONE, m_cauth);
	DDX_Radio(pDX, IDC_SDEVSAUTHNONE, m_sauth);
	DDX_Text(pDX, IDC_SDMACCPATH, m_dmAccPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CServerDevicePane2, CDialog)
	//{{AFX_MSG_MAP(CServerDevicePane2)
	ON_BN_CLICKED(IDC_SDEVAPPLY, OnSdevapply)
	ON_BN_CLICKED(IDC_SDEVDISCARD, OnSdevdiscard)
	ON_EN_CHANGE(IDC_SDEVPC, OnUpdate)
	ON_EN_CHANGE(IDC_SDEVPCP, OnUpdate)
	ON_EN_CHANGE(IDC_SDEVPNV, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVCAUTHBASIC, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVCAUTHHMAC, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVCAUTHNONE, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVSAUTHBASIC, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVSAUTHDIGEST, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVSAUTHHMAC, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVSAUTHNONE, OnUpdate)
	ON_BN_CLICKED(IDC_SDEVCAUTHDIGEST, OnUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDevicePane2 message handlers

BOOL CServerDevicePane2::OnInitDialog() 
{
	LOGFONT logFont;
	CDialog::OnInitDialog();
	
	CFont *font=GetDlgItem(IDC_SDEVINFO)->GetFont();
	CFont tempFont;
	font->GetLogFont(&logFont);
	logFont.lfWeight=FW_BOLD;
	tempFont.CreateFontIndirect(&logFont);
	GetDlgItem(IDC_SDEVINFO)->SetFont(&tempFont);
	GetDlgItem(IDC_SDEVREP)->SetFont(&tempFont);
	if (device) 
		Refresh();
	else
	{
		GetDlgItem(IDC_SDEVID)->EnableWindow(TRUE);
		GetDlgItem(IDC_SDMACCPATH)->EnableWindow(TRUE);
		GetDlgItem(IDC_SDEVDEVID)->SetWindowText("User name");
		GetDlgItem(IDC_SDEVCHANGE)->ShowWindow(TRUE);
		GetDlgItem(IDC_SDEVAPPLY)->ShowWindow(TRUE);
		GetDlgItem(IDC_SDEVDISCARD)->ShowWindow(TRUE);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CServerDevicePane2::OnSdevdiscard() 
{
	if (!device)
	{
		navigator->SelectItem(0);
		return;
	}
	update=false;
	GetDlgItem(IDC_SDEVCHANGE)->ShowWindow(FALSE);
	GetDlgItem(IDC_SDEVAPPLY)->ShowWindow(FALSE);
	GetDlgItem(IDC_SDEVDISCARD)->ShowWindow(FALSE);
	Refresh();
}

void CServerDevicePane2::Refresh()
{
	SctsDMNode *node=device->FindDMNode("./DevInfo/DevId");
	m_id=node?(char *)node->data:"";
    m_dmAccPath=device->dmAccPath;
	m_company=device->productCompany;
	m_contact=device->productContact;
	m_name=device->productName;
	m_cauth=device->GetAuthType(true);
	m_sauth=device->GetAuthType(false);
	UpdateData(FALSE);
}

void CServerDevicePane2::OnSdevapply()
{
	char tempBuf[100];
	char *pContact=0;
	char *pCompany=0;
	char *pName=0;
	UpdateData(TRUE);
	if (m_name!="") pName=(char *)(LPCTSTR)m_name;
	if (m_company!="") pCompany=(char *)(LPCTSTR)m_company;
	if (m_contact!="") pContact=(char *)(LPCTSTR)m_contact;
	if (!device)
	{
		char *id=(char *)(LPCTSTR)m_id;
		if (m_id.IsEmpty())
		{
			MessageBox("User Name cannot be empty.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_SDEVID)->SetFocus();
			return;
		}
		if (SctsDMDevice::GetServerDevice(id))
		{
			MessageBox("Server device with same client user name exists.","Add Server Device",MB_ICONERROR);
			GetDlgItem(IDC_SDEVID)->SetFocus();
			return;
		}
		char *dmAccPath=(char *)(LPCTSTR)m_dmAccPath;
		if (m_dmAccPath.IsEmpty())
		{
			MessageBox("Account URI cannot be empty.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_SDMACCPATH)->SetFocus();
			return;
		}
		if (m_dmAccPath.GetAt(m_dmAccPath.GetLength()-1) == '/')
		{
			MessageBox("Account URI can not be ended by /.","Device", MB_ICONEXCLAMATION);
			GetDlgItem(IDC_SDMACCPATH)->SetFocus();
			return;
		}
		device=SctsDMDevice::AddServerDevice(SctsDMDevice::GetSctsServerID(),dmAccPath,id,pName,pContact,pCompany);
		SCTSASSERT(device);
		m_id="";
		UpdateData(FALSE);
		GetDlgItem(IDC_SDEVDEVID)->SetWindowText("Device ID");
		GetDlgItem(IDC_SDEVID)->EnableWindow(FALSE);
		device->Lock();
		
		/* Create the tree with permanent nodes */
		/* Build ./SyncML */
		sprintf(tempBuf,"%s", device->dmAccPath);
		device->AddFullDMNode(tempBuf,SIDX,"node",true,0,0,0,0,0);

        sprintf(tempBuf,"%s/Con", device->dmAccPath);
		device->AddDMNode(tempBuf,SIDX,"node",true,0,0,0,0,0);

		/* Build ./DevDetail */
		sprintf(tempBuf,"./DevDetail");
		device->AddDMNode(tempBuf,SIDX,"node",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/Bearer");
		device->AddDMNode(tempBuf,SIDX,"node",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/URI");
		device->AddDMNode(tempBuf,SIDX,"node",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/URI/MaxDepth");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/URI/MaxTotLen");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/URI/MaxSegLen");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/DevTyp");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/OEM");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/FwV");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);
		
		sprintf(tempBuf,"./DevDetail/SwV");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/HwV");
		device->AddDMNode(tempBuf,SIDX,"null",true,0,0,0,0,0);

		sprintf(tempBuf,"./DevDetail/LrgObj");
		device->AddDMNode(tempBuf,SIDX,"bool",true,"false",0,5,0,0);
	}
	else
	{
		/* 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;
		}
		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->SetAuthType(true,(SctsAuthType)m_cauth);
	device->SetAuthType(false,(SctsAuthType)m_sauth);
	device->Unlock();
	GetDlgItem(IDC_SDEVCHANGE)->ShowWindow(FALSE);
	GetDlgItem(IDC_SDEVAPPLY)->ShowWindow(FALSE);
	GetDlgItem(IDC_SDEVDISCARD)->ShowWindow(FALSE);
	update=false;
}

void CServerDevicePane2::OnUpdate() 
{
	if (update) return;
	update=true;
	GetDlgItem(IDC_SDEVCHANGE)->ShowWindow(TRUE);
	GetDlgItem(IDC_SDEVAPPLY)->ShowWindow(TRUE);
	GetDlgItem(IDC_SDEVDISCARD)->ShowWindow(TRUE);
}

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