// N2PSettingsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "afxres.h"
#include "palmtool.h"
#include "UdpSocket.h"
#include "tcpsocket.h"
#include "N2PSettingsDlg.h"

#include "p_util.h"

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

/////////////////////////////////////////////////////////////////////////////
// CN2PSettingsDlg dialog


CN2PSettingsDlg::CN2PSettingsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CN2PSettingsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CN2PSettingsDlg)
	m_iRegisterTTL = 0;
	m_strServiceDns = _T("");
	m_strServiceID = _T("");
	m_strAccount = _T("");
	m_strPin = _T("");
	m_iRasPort = 0;
	m_iRtpPort = 0;
	m_strSuperPassword = _T("");
	//}}AFX_DATA_INIT
}


void CN2PSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CN2PSettingsDlg)
	DDX_Control(pDX, IDC_DEBUG, m_ctlDebugLevel);
	DDX_Control(pDX, IDC_DTMF, m_ctlDTMFType);
	DDX_Text(pDX, IDC_REGISTER_TTL, m_iRegisterTTL);
	DDV_MinMaxInt(pDX, m_iRegisterTTL, 0, 65535);
	DDX_Text(pDX, IDC_SERVICE_DNS, m_strServiceDns);
	DDV_MaxChars(pDX, m_strServiceDns, 32);
	DDX_Text(pDX, IDC_SERVICE_ID, m_strServiceID);
	DDV_MaxChars(pDX, m_strServiceID, 32);
	DDX_Text(pDX, IDC_ACCOUNT, m_strAccount);
	DDV_MaxChars(pDX, m_strAccount, 32);
	DDX_Text(pDX, IDC_PIN, m_strPin);
	DDV_MaxChars(pDX, m_strPin, 32);
	DDX_Text(pDX, IDC_RAS_PORT, m_iRasPort);
	DDV_MinMaxInt(pDX, m_iRasPort, 0, 65535);
	DDX_Text(pDX, IDC_RTP_PORT, m_iRtpPort);
	DDV_MinMaxInt(pDX, m_iRtpPort, 0, 65535);
	DDX_Text(pDX, IDC_SUPER_PASSWORD, m_strSuperPassword);
	DDV_MaxChars(pDX, m_strSuperPassword, 16);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CN2PSettingsDlg, CDialog)
	//{{AFX_MSG_MAP(CN2PSettingsDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CN2PSettingsDlg message handlers

BOOL CN2PSettingsDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	int i;
	CString strText;

	for(i = 0; i < N2P_DTMF_TYPE_MAX; i++)
		m_ctlDTMFType.AddString((char *)(g_cN2pDtmfDisplay[0] + i * N2P_DTMF_DISPLAY_LEN));

	GetDlgItemText(IDOK, strText);

	if( !strcmp(strText, "È·  ¶¨"))
	{
		for(i = 0; i < DEBUG_LEVEL_MAX; i++)
			m_ctlDebugLevel.AddString((char *)(g_cDebugDisplay_GB[0] + i * DEBUG_GB_DISPLAY_LEN));
	}
	else if (!strcmp(strText, "OK"))
	{
		for(i = 0; i < DEBUG_LEVEL_MAX; i++)
			m_ctlDebugLevel.AddString((char *)(g_cDebugDisplay[0] + i * DEBUG_DISPLAY_LEN));
	}

	/* 0x0000f6--0x0000f7:           Register TTL */
    m_iRegisterTTL = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RegisterTTL"), 60);

	/* 0x0001a0-0x0001bf: Service address */
	m_strServiceDns = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("ServiceDns"), "");

	/* 0x000180--0x00019f: Service ID */
	m_strServiceID = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("ServiceID"), _T(""));

	/* 0x00008e--0x0000ad: calling card account */
	m_strAccount = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Account"), _T(""));

	/* 0x00006e--0x00008d: calling card PIN */
	m_strPin = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Pin"), _T(""));

	/* 0x0000f0:           Ras source port high
	   0x0000f1:           Ras source port low */
	m_iRasPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RasPort"), 1720);

	/* 0x0000f4:           Rtp source port high 
	   0x0000f5:           Rtp source port low */
	m_iRtpPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RtpPort"), 1722);

	/* 0x000130--0x00013f: Supervisor Password */
	m_strSuperPassword = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("SuperPassword"), "12345678");

	/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */	
    m_ctlDTMFType.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DTMFType"), 0));

	/* 0x0000fe:           1 to output debug information by udp, 0 to output nothing */
	m_ctlDebugLevel.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Output"), 0));

	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CN2PSettingsDlg::OnOK() 
{
	UpdateData();

	/* 0x0000f6--0x0000f7:           Register TTL */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RegisterTTL"), m_iRegisterTTL);

	/* 0x0001a0-0x0001bf: Service address */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("ServiceDns"), m_strServiceDns);

	/* 0x000180--0x00019f: Service ID */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("ServiceID"), m_strServiceID);

	/* 0x00006e--0x00008d: calling card account */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Account"), m_strAccount);

	/* 0x00008e--0x0000ad: calling card PIN */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Pin"), m_strPin);

	/* 0x0000f0:           Ras source port high
	   0x0000f1:           Ras source port low */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RasPort"), m_iRasPort);

	/* 0x0000f4:           Rtp source port high 
	   0x0000f5:           Rtp source port low */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RtpPort"), m_iRtpPort);

	/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */	
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DTMFType"), m_ctlDTMFType.GetCurSel());

	/* 0x000130--0x00013f: Supervisor Password */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("SuperPassword"), m_strSuperPassword);

	/* 0x0000fe:           1 to output debug information by udp, 0 to output nothing */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Output"), m_ctlDebugLevel.GetCurSel());
	
	CDialog::OnOK();
}
