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

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.

********************************************************************/
// ServerDevicePane.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CServerDevicePane dialog


CServerDevicePane::CServerDevicePane(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDevicePane::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDevicePane)
	m_company = _T("");
	m_contact = _T("");
	m_name = _T("");
	m_cuser = _T("");
	m_sauth = 0;
	m_spassword = _T("");
	m_dmAccPath = _T("");
	//}}AFX_DATA_INIT
}


void CServerDevicePane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDevicePane)
	DDX_Text(pDX, IDC_SDEVPC, m_company);
	DDX_Text(pDX, IDC_SDEVPCP, m_contact);
	DDX_Text(pDX, IDC_SDEVPNV, m_name);
	DDX_Text(pDX, IDC_SDEVCUSER, m_cuser);
	DDX_Radio(pDX, IDC_SDEVSAUTHNONE, m_sauth);
	DDX_Text(pDX, IDC_SDEVSPWD, m_spassword);
	DDX_Text(pDX, IDC_SDMACCPATH, m_dmAccPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CServerDevicePane, CDialog)
	//{{AFX_MSG_MAP(CServerDevicePane)
	ON_BN_CLICKED(IDC_SDEVDISCOVER, OnSdevdiscover)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDevicePane message handlers

void CServerDevicePane::OnSdevdiscover() 
{
	SctsDMTree *tree;
    std::string dmAccName;
	UpdateData(TRUE);
	/* Check user name */
	if (m_cuser.IsEmpty())
	{
		MessageBox("Client User name must have a value.","Add Server Device",MB_ICONERROR);
		GetDlgItem(IDC_SDEVCUSER)->SetFocus();
		return;
	}
	if (m_dmAccPath.IsEmpty())
	{
		MessageBox("Device Account URI must have a value.","Add Server Device",MB_ICONERROR);
		GetDlgItem(IDC_SDMACCPATH)->SetFocus();
		return;
	}
	if (m_dmAccPath.GetAt(m_dmAccPath.GetLength()-1) == '/')
	{
		MessageBox("Device Account URI can not be ended by /.","Add Server Device",MB_ICONERROR);
		GetDlgItem(IDC_SDMACCPATH)->SetFocus();
		return;
	}
    if (m_dmAccPath.GetAt(m_dmAccPath.GetLength()-1) == '/')
        m_dmAccPath = m_dmAccPath.Left(m_dmAccPath.GetLength()-1);
    if (m_dmAccPath.ReverseFind('/') == -1)
	{
		MessageBox("DMAcc need full path, such as \"./XXX...\" .","Add Server Device",MB_ICONERROR);
		GetDlgItem(IDC_SDMACCPATH)->SetFocus();
		return;
	}
    else
    {
        dmAccName = (LPCSTR)m_dmAccPath;
        dmAccName = dmAccName.substr(dmAccName.rfind("/")+1);
        m_dmAccPath = m_dmAccPath.Left(m_dmAccPath.ReverseFind('/'));
    }
	if (SctsDMDevice::GetServerDevice((char *)(LPCTSTR)m_cuser))
	{
		MessageBox("Server device with same client user name exists.","Add Server Device",MB_ICONERROR);
		GetDlgItem(IDC_SDEVCUSER)->SetFocus();
		return;
	}
	if (m_sauth>0 && m_spassword.IsEmpty())
	{
		MessageBox("Server password must have a value.","Add Server Device",MB_ICONERROR);
		GetDlgItem(IDC_SDEVSPWD)->SetFocus();
		return;
	}
	SctsDMServerSession::discoveryAccountLock.Lock();
	tree=new SctsDMTree();

	/* DMAcc for SCTS */
    std::string fullPath = SctsDMServerSession::discoveryTempPath;
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsClient/AAuthName").c_str() ,SIDX,"chr",false,(char *)(LPCTSTR)m_cuser,0,m_cuser.GetLength());
	tree->AddFullDMNode((char *)(fullPath + "/ServerID").c_str() ,SIDX,"chr",false,SctsDMDevice::GetSctsServerID(),0,strlen(SctsDMDevice::GetSctsServerID()));
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsClient/AAuthSecret").c_str() ,SIDX,"chr",false,"",0,0);
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsServer/AAuthSecret").c_str() ,SIDX,"chr",false,(char *)(LPCTSTR)m_spassword,0,m_spassword.GetLength());
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsClient/AAuthData").c_str() ,SIDX,"chr",false,"",0,0);
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsServer/AAuthData").c_str(),SIDX,"chr",false,"",0,0);
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsClient/AAuthLevel").c_str() ,SIDX,"chr",false,"CLCRED",0,6);
	tree->AddFullDMNode((char *)(fullPath + "/AppAuth/SctsServer/AAuthLevel").c_str(),SIDX,"chr",false,"SRVCRED",0,7);

    //tree->AddDMNode("./UserName",SIDX,"chr",false,(char *)(LPCTSTR)m_cuser,0,m_cuser.GetLength());
	//tree->AddDMNode("./ServerID",SIDX,"chr",false,SctsDMDevice::GetSctsServerID(),0,strlen(SctsDMDevice::GetSctsServerID()));
	//tree->AddDMNode("./ClientPW",SIDX,"chr",false,"",0,0);
	//tree->AddDMNode("./ServerPW",SIDX,"chr",false,(char *)(LPCTSTR)m_spassword,0,m_spassword.GetLength());
	//tree->AddDMNode("./ClientNonce",SIDX,"chr",false,"",0,0);
	//tree->AddDMNode("./ServerNonce",SIDX,"chr",false,"",0,0);

    if (SctsDMServerSession::discoveryAccount) delete SctsDMServerSession::discoveryAccount;
	SctsDMServerSession::discoveryAccount=tree;
	if (SctsDMServerSession::discoveryName) sctsFree(SctsDMServerSession::discoveryName);
	if (SctsDMServerSession::discoveryContact) sctsFree(SctsDMServerSession::discoveryContact);
	if (SctsDMServerSession::discoveryCompany) sctsFree(SctsDMServerSession::discoveryCompany);
    if (SctsDMServerSession::discoveryDMAccPath) sctsFree(SctsDMServerSession::discoveryDMAccPath);
	SctsDMServerSession::discoveryName=m_name.IsEmpty()?0:strdup((char *)(LPCSTR)m_name);
	SctsDMServerSession::discoveryContact=m_contact.IsEmpty()?0:strdup((char *)(LPCSTR)m_contact);
	SctsDMServerSession::discoveryCompany=m_company.IsEmpty()?0:strdup((char *)(LPCSTR)m_company);
	SctsDMServerSession::discoveryDMAccPath=m_dmAccPath.IsEmpty()?0:strdup((char *)(LPCSTR)m_dmAccPath);
    SctsDMServerSession::discoveryDMAccName=dmAccName.length()==0?0:strdup(dmAccName.c_str());
	SctsDMServerSession::discoveryAccountLock.Unlock();
	mainPane->currentLeftPane->m_accountTree.SelectItem(0);
}

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