// MgcpSettingsDlg.cpp : implementation file
//

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

#include "..\\inc\\p_util.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMgcpSettingsDlg dialog


CMgcpSettingsDlg::CMgcpSettingsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMgcpSettingsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMgcpSettingsDlg)
	m_iRegisterTTL = 0;
	m_strNatDns = _T("");
	m_strServiceDns = _T("");
	m_iNatTTL = 0;
	m_strPhoneNumber = _T("");
	m_strAccount = _T("");
	m_strPin = _T("");
	m_iRasPort = 0;
	m_iRtpPort = 0;
	m_iDtmfPayloadType = 0;
	m_iRtpTos = 0;
	m_strSuperPassword = _T("");
	//}}AFX_DATA_INIT
}


void CMgcpSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMgcpSettingsDlg)
	DDX_Control(pDX, IDC_DEBUG, m_ctlDebugLevel);
	DDX_Control(pDX, IDC_DTMF, m_ctlDTMFType);
	DDX_Control(pDX, IDC_LOCALALIAS, m_ctlLocalAlias);
	DDX_Control(pDX, IDC_NAT_TRAVERSAL, m_ctlNatTraversal);
	DDX_Control(pDX, IDC_SERVICE, m_ctlServiceType);
	DDX_Text(pDX, IDC_REGISTER_TTL, m_iRegisterTTL);
	DDV_MinMaxInt(pDX, m_iRegisterTTL, 0, 65535);
	DDX_Text(pDX, IDC_NAT_DNS, m_strNatDns);
	DDV_MaxChars(pDX, m_strNatDns, 32);
	DDX_Text(pDX, IDC_SERVICE_DNS, m_strServiceDns);
	DDV_MaxChars(pDX, m_strServiceDns, 32);
	DDX_Text(pDX, IDC_NAT_TTL, m_iNatTTL);
	DDV_MinMaxInt(pDX, m_iNatTTL, 0, 65535);
	DDX_Text(pDX, IDC_PHONE_NUMBER, m_strPhoneNumber);
	DDV_MaxChars(pDX, m_strPhoneNumber, 16);
	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_DTMF_PAYLOAD, m_iDtmfPayloadType);
	DDV_MinMaxInt(pDX, m_iDtmfPayloadType, 0, 255);
	DDX_Text(pDX, IDC_RTP_TOS, m_iRtpTos);
	DDV_MinMaxInt(pDX, m_iRtpTos, 0, 254);
	DDX_Text(pDX, IDC_SUPER_PASSWORD, m_strSuperPassword);
	DDV_MaxChars(pDX, m_strSuperPassword, 16);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CMgcpSettingsDlg message handlers

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

	for(i = 0; i < MGCP_DTMF_TYPE_MAX; i++)
		m_ctlDTMFType.AddString((char *)(g_cMgcpDtmfDisplay[0] + i * MGCP_DTMF_DISPLAY_LEN));

	for(i = 0; i < MGCP_SERVICE_TYPE_MAX; i++)
		m_ctlServiceType.AddString((char *)(g_cMgcpServiceTypeDisplay[0] + i * MGCP_SERVICE_TYPE_DISPLAY_LEN));

	GetDlgItemText(IDOK, strText);

	if( !strcmp(strText, "È·  ¶¨"))
	{
		for(i = 0; i < MGCP_LOCAL_TYPE_MAX; i++)
			m_ctlLocalAlias.AddString((char *)(g_cMgcpLocalTypeDisplay_GB[0] + i * MGCP_LOCALALIAS_GB_DISPLAY_LEN));

		for(i = 0; i < MGCP_NAT_TRAVERSAL_MAX; i++)
			m_ctlNatTraversal.AddString((char *)(g_cMgcpNatTraversalDisplay_GB[0] + i * MGCP_NAT_TRAVERSAL_GB_DISPLAY_LEN));

		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 < MGCP_LOCAL_TYPE_MAX; i++)
			m_ctlLocalAlias.AddString((char *)(g_cMgcpLocalTypeDisplay[0] + i * MGCP_LOCALALIAS_DISPLAY_LEN));

		for(i = 0; i < MGCP_NAT_TRAVERSAL_MAX; i++)
			m_ctlNatTraversal.AddString((char *)(g_cMgcpNatTraversalDisplay[0] + i * MGCP_NAT_TRAVERSAL_DISPLAY_LEN));

		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"), "");

	/* 0x0001e0-0x0001ff: NAT Server Address */
	m_strNatDns = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("NatAddr"), "");

	/* 0x0000f8--0x0000f9:           NAT hint TTL */
    m_iNatTTL = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("NatTTL"), 240);

	/* 0x000010--0x00001f: My phone number to use with a gatekeeper */
	m_strPhoneNumber = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("PhoneNumber"), _T("100"));

	/* 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);

	/* 0x00015e:           Type of service */
    m_iRtpTos = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Tos"), 0);

	/* 0x00003a:           Dtmf Payload Type */
    m_iDtmfPayloadType = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DtmfPayloadType"), 0);

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

	/* 0x000032:           1 to use e.164 log on with gatekeeper, 0 to use h323-id */
    m_ctlLocalAlias.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("LocalAlias"), 1));

	/* 0x00003f:           Use built in H.323 proxy support */
    m_ctlNatTraversal.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("NatTraversal"), 0));

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

	/* 0x00006d:		   1 to use calling card, 0 to direct dial */	 
	m_ctlServiceType.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("ServiceType"), 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 CMgcpSettingsDlg::OnOK() 
{
	UpdateData();

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

	/* 0x00006d:		   1 to use calling card, 0 to direct dial */	 
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("ServiceType"), m_ctlServiceType.GetCurSel());

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

	/* 0x000010--0x00001f: My phone number to use with a gatekeeper */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("PhoneNumber"), m_strPhoneNumber);

	/* 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);

	/* 0x00003f:           Use built in H.323 proxy support */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NatTraversal"), m_ctlNatTraversal.GetCurSel());

	/* 0x0001e0-0x0001ff: NAT Server Address */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("NatAddr"), m_strNatDns);

	/* 0x0000f7:           NAT hint TTL */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NatTTL"), m_iNatTTL);

	/* 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);

	/* 0x00015f:           Type of service */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Tos"), m_iRtpTos);

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

	/* 0x000032:           1 to use e.164 log on with gatekeeper, 0 to use h323-id */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("LocalAlias"), (int)m_ctlLocalAlias.GetCurSel());

	/* 0x00003a:           Dtmp Payload Type */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DtmfPayloadType"), m_iDtmfPayloadType);

	/* 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();
}
