// DialPlanSettingsDlg.cpp : implementation file
//

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

#include "p_util.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDialPlanSettingsDlg dialog


CDialPlanSettingsDlg::CDialPlanSettingsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDialPlanSettingsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialPlanSettingsDlg)
	m_strDialNumber = _T("");
	m_strDDDCode = _T("");
	m_strDDDPrefix = _T("");
	m_strIDDCode = _T("");
	m_strIDDPrefix = _T("");
	m_strInnerLinePrefix = _T("");
	m_bUseDigitMap = FALSE;
	m_strForwardNumber = _T("");
	m_bCallWaiting = FALSE;
	m_iRingTimeout = 0;
	//}}AFX_DATA_INIT
}


void CDialPlanSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialPlanSettingsDlg)
	DDX_Control(pDX, IDC_DIALPLAN, m_ctlUseDialPlan);
	DDX_Control(pDX, IDC_INNERLINE, m_ctlInnerLine);
	DDX_Text(pDX, IDC_DIAL_NUMBER, m_strDialNumber);
	DDV_MaxChars(pDX, m_strDialNumber, 16);
	DDX_Text(pDX, IDC_DDDCODE, m_strDDDCode);
	DDV_MaxChars(pDX, m_strDDDCode, 4);
	DDX_Text(pDX, IDC_DDDPREFIX, m_strDDDPrefix);
	DDV_MaxChars(pDX, m_strDDDPrefix, 3);
	DDX_Text(pDX, IDC_IDDCODE, m_strIDDCode);
	DDV_MaxChars(pDX, m_strIDDCode, 4);
	DDX_Text(pDX, IDC_IDDPREFIX, m_strIDDPrefix);
	DDV_MaxChars(pDX, m_strIDDPrefix, 4);
	DDX_Text(pDX, IDC_INNERLINEPREFIX, m_strInnerLinePrefix);
	DDV_MaxChars(pDX, m_strInnerLinePrefix, 2);
	DDX_Check(pDX, IDC_USE_DIGITMAP, m_bUseDigitMap);
	DDX_Text(pDX, IDC_FORWARD_NUMBER, m_strForwardNumber);
	DDV_MaxChars(pDX, m_strForwardNumber, 24);
	DDX_Check(pDX, IDC_FWD_ALWAYS, m_bFwdAlways);
	DDX_Check(pDX, IDC_FWD_BUSY, m_bFwdBusy);
	DDX_Check(pDX, IDC_FWD_NOANSWER, m_bFwdNoanswer);
	DDX_Check(pDX, IDC_FWD_POWEROFF, m_bFwdPoweroff);
	DDX_Check(pDX, IDC_CALL_WAITING, m_bCallWaiting);
	DDX_Text(pDX, IDC_RING_TIMEOUT, m_iRingTimeout);
	DDV_MinMaxInt(pDX, m_iRingTimeout, 0, 60);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialPlanSettingsDlg, CDialog)
	//{{AFX_MSG_MAP(CDialPlanSettingsDlg)
	ON_CBN_SELCHANGE(IDC_DIALPLAN, OnSelchangeDialplan)
	ON_CBN_SELCHANGE(IDC_INNERLINE, OnSelchangeInnerline)
	ON_BN_CLICKED(IDC_FWD_ALWAYS, OnFwdNumber)
	ON_BN_CLICKED(IDC_FWD_BUSY, OnFwdNumber)
	ON_BN_CLICKED(IDC_FWD_NOANSWER, OnFwdNumber)
	ON_BN_CLICKED(IDC_FWD_POWEROFF, OnFwdNumber)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialPlanSettingsDlg message handlers

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

	GetDlgItemText(IDOK, strText);

	if( !strcmp(strText, "È·  ¶¨"))
	{
		for(i = 0; i < DIALPLAN_MAX; i++)
			m_ctlUseDialPlan.AddString((char *)(g_cDialplanDisplay_GB[0] + i * DIALPLAN_DISPLAY_LEN));

		for(i = 0; i < INNER_LINE_MAX; i++)
			m_ctlInnerLine.AddString((char *)(g_cInnerLineDisplay_GB[0] + i * INNERLINE_DISPLAY_LEN));
	}
	else if (!strcmp(strText, "OK"))
	{
		for(i = 0; i < DIALPLAN_MAX; i++)
			m_ctlUseDialPlan.AddString((char *)(g_cDialplanDisplay[0] + i * DIALPLAN_DISPLAY_LEN));

		for(i = 0; i < INNER_LINE_MAX; i++)
			m_ctlInnerLine.AddString((char *)(g_cInnerLineDisplay[0] + i * INNERLINE_DISPLAY_LEN));
	}

	
	/* 0x00005f:		   1 to use dial plan, 0 to ignore */
	m_ctlUseDialPlan.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseDialPlan"), 0));

	/* 0x000170--0x00017f: dial number */
	m_strDialNumber = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DialNumber"), _T(""));

	/* 0x000060--0x000063: Area Code: maximum length 4 digits */
	m_strDDDCode = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DDDCode"), _T("0"));

	/* 0x000064--0x000066: Longprefix: maximum length 3 digits */
	m_strDDDPrefix = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DDDPrefix"), _T("10"));

	/* 0x000068:		   1 - inner line, 0 - direct dial, default 0 */ 
    m_ctlInnerLine.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("InnerLine"), 0));

	/* 0x00006b--0x00006c: to access long-distance outer line */
	m_strInnerLinePrefix = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("InnerLinePrefix"), _T("0"));

	/* 0x000058--0x00005b: Country Code; maximum length 4 digits */
	m_strIDDCode = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("IDDCode"), _T("00"));
	
	/* 0x00005c--0x00005e: International Prefix: maximum length 3 digits */
	m_strIDDPrefix = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("IDDPrefix"), _T("86"));

	/* 0x0000ff:		   1 to use digit map */
	m_bUseDigitMap = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseDigitMap"), 1);

	/* 0x000140--0x00014f: forward number */
	m_strForwardNumber = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("ForwardNumber"), _T(""));

	/* 0x000150:		   Forward type, 0 - none, 1 - always, 2 - busy, 3 - no answer */	 
	m_bFwdAlways = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdAlways"), 0);
	m_bFwdBusy = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdBusy"), 0);
	m_bFwdNoanswer = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdNoanswer"), 0);
	m_bFwdPoweroff = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdPoweroff"), 0);
	m_bCallWaiting = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("CallWaiting"), 0);

	/* 0x000031:           Number to ring before auto answer */
    m_iRingTimeout = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RingTimeout"), 5);
	
	UpdateData(FALSE);

	OnSelchangeDialplan();
	OnSelchangeInnerline();
	OnFwdNumber();

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

void CDialPlanSettingsDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	UpdateData();

	/* 0x00005f:		   1 to use dial plan, 0 to ignore */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseDialPlan"), m_ctlUseDialPlan.GetCurSel());

	/* 0x000170--0x00017f: dial number */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DialNumber"), m_strDialNumber);

	/* 0x000060--0x000063: Area Code: maximum length 4 digits */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DDDCode"), m_strDDDCode);

	/* 0x000064--0x000066: Longprefix: maximum length 3 digits */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DDDPrefix"), m_strDDDPrefix);

	/* 0x000068:		   1 - inner line, 0 - direct dial, default 0 */ 
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("InnerLine"), m_ctlInnerLine.GetCurSel());

	/* 0x00006b--0x00006c: to access long-distance outer line */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("InnerLinePrefix"), m_strInnerLinePrefix);

	/* 0x000058--0x00005b: Country Code; maximum length 4 digits */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("IDDCode"), m_strIDDCode);
	
	/* 0x00005c--0x00005e: International Prefix: maximum length 3 digits */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("IDDPrefix"), m_strIDDPrefix);

	/* 0x0000ff:		   1 to use digit map */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseDigitMap"), (int)m_bUseDigitMap);

	/* 0x000140--0x000157: forward number */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("ForwardNumber"), m_strForwardNumber);

	/* 0x000158:		   Forward type, 0 - none, 1 - always, 2 - busy, 3 - no answer */	 
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdAlways"), (int)m_bFwdAlways);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdBusy"), (int)m_bFwdBusy);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdNoanswer"), (int)m_bFwdNoanswer);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdPoweroff"), (int)m_bFwdPoweroff);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("CallWaiting"), (int)m_bCallWaiting);

	/* 0x000031:           Number to ring before auto answer */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RingTimeout"), m_iRingTimeout);

	CDialog::OnOK();
}

void CDialPlanSettingsDlg::OnSelchangeDialplan() 
{
	BOOL m_bDialplan, m_bDialnum;

	UpdateData();

	if((m_ctlUseDialPlan.GetCurSel()) == DIALPLAN_DISABLE)
	{
		m_bDialplan = FALSE;
		m_bDialnum = FALSE;
	}
	else if((m_ctlUseDialPlan.GetCurSel()) == DIALPLAN_ENABLE)
	{
		m_bDialplan = TRUE;
		m_bDialnum = FALSE;
	}
	else if((m_ctlUseDialPlan.GetCurSel()) == DIALPLAN_HOTLINE)
	{
		m_bDialplan = FALSE;
		m_bDialnum = TRUE;
	}
	else
	{
		m_bDialplan = TRUE;
		m_bDialnum = TRUE;
	}

	GetDlgItem(IDC_DDDCODE)->EnableWindow(m_bDialplan);
	GetDlgItem(IDC_IDDCODE)->EnableWindow(m_bDialplan);	
	GetDlgItem(IDC_IDDPREFIX)->EnableWindow(m_bDialplan);	
	GetDlgItem(IDC_DDDPREFIX)->EnableWindow(m_bDialplan);	
	GetDlgItem(IDC_DIAL_NUMBER)->EnableWindow(m_bDialnum);	
}

void CDialPlanSettingsDlg::OnSelchangeInnerline() 
{
	BOOL m_bInnerLine;

	UpdateData();

	if((m_ctlInnerLine.GetCurSel()) != 0)
		m_bInnerLine = TRUE;
	else
		m_bInnerLine = FALSE;

	GetDlgItem(IDC_INNERLINEPREFIX)->EnableWindow(m_bInnerLine);	
}

void CDialPlanSettingsDlg::OnFwdNumber() 
{
	BOOL m_bNumber, m_bAnswer;

	UpdateData();
	if((m_bFwdPoweroff == 0) && (m_bFwdAlways == 0) && (m_bFwdBusy == 0) && (m_bFwdNoanswer == 0))
		m_bNumber = FALSE;
	else
		m_bNumber = TRUE;

	if(m_bFwdNoanswer == 0)
		m_bAnswer = FALSE;
	else
		m_bAnswer = TRUE;

	GetDlgItem(IDC_FORWARD_NUMBER)->EnableWindow(m_bNumber);
	GetDlgItem(IDC_RING_TIMEOUT)->EnableWindow(m_bAnswer);
}
