// Iax2SettingsDlg.cpp : implementation file
//

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CIax2SettingsDlg dialog


CIax2SettingsDlg::CIax2SettingsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIax2SettingsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIax2SettingsDlg)
	m_iRegisterTTL = 0;
	m_strServiceDns = _T("");
	m_strPhoneNumber = _T("");
	m_strAccount = _T("");
	m_strPin = _T("");
	m_iRasPort = 0;
	m_strSuperPassword = _T("");
	m_iRtpTos = 0;
	m_bUseService = FALSE;
	m_bRemoteDialplan = FALSE;
	//}}AFX_DATA_INIT
}


void CIax2SettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIax2SettingsDlg)
	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_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_SUPER_PASSWORD, m_strSuperPassword);
	DDV_MaxChars(pDX, m_strSuperPassword, 16);
	DDX_Text(pDX, IDC_RTP_TOS, m_iRtpTos);
	DDV_MinMaxInt(pDX, m_iRtpTos, 0, 254);
	DDX_Check(pDX, IDC_USE_GK, m_bUseService);
	DDX_Check(pDX, IDC_REMOTE_DIALPLAN, m_bRemoteDialplan);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CIax2SettingsDlg message handlers

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

	for(i = 0; i < DTMF_TYPE_MAX; i++)
		m_ctlDTMFType.AddString((char *)(g_cDtmfDisplay[0] + i * 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));
	}

	/* 0x00000e:           1 to use gatekeeper, 0 to dial IP address directly 1,*/
	m_bUseService = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseService"), 0);

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

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

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

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

	/* 0x000046:		   1 to use fast start, 0 to bring normal h245 */
	m_bRemoteDialplan = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RemoteDialplan"), 0);

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

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

	/* 0x00000e:           1 to use gatekeeper, 0 to dial IP address directly 1,*/
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseService"), (int)m_bUseService);

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

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

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

	/* 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());
	
	/* 0x00015f:           Type of service */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Tos"), m_iRtpTos);

	/* 0x000046:		   1 to use fast start, 0 to bring normal h245 */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RemoteDialplan"), (int)m_bRemoteDialplan);

	CDialog::OnOK();
}
