// Mobldlg.cpp : implementation file
//

#include "stdafx.h"
#include "UDPServ.h"
#include "Mobldlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMobldlg dialog


CMobldlg::CMobldlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMobldlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMobldlg)
	m_sMobilePort = _T("");
	m_sMobileIP = _T("");
	m_sMobileName = _T("");
	//}}AFX_DATA_INIT

	m_strIniFile = "Udp_VList.ini";
}


void CMobldlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMobldlg)
	DDX_Text(pDX, IDC_EDIT_M_PORT, m_sMobilePort);
	DDX_Text(pDX, IDC_EDIT_M_IP, m_sMobileIP);
	DDX_CBString(pDX, IDC_COMBO_MOBILE, m_sMobileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMobldlg, CDialog)
	//{{AFX_MSG_MAP(CMobldlg)
	ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDOK, OnOk)
	ON_CBN_SELENDOK(IDC_COMBO_MOBILE, OnSelendokComboMobile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobldlg message handlers

void CMobldlg::OnButtonNew() 
{
	m_sMobileName = "";
	m_sMobileIP = "";
	m_sMobilePort = "";
	UpdateData(FALSE);
	((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->Clear();
}

void CMobldlg::OnButtonSave() 
{
	UpdateData(TRUE);
	WritePrivateProfileString
		("PROFILE", m_sMobileName, "", m_strIniFile);
	WritePrivateProfileString
		(m_sMobileName, "MOBILE_IP", m_sMobileIP, m_strIniFile);
	WritePrivateProfileString
		(m_sMobileName, "MOBILE_PORT", m_sMobilePort, m_strIniFile);
	((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->AddString(m_sMobileName);
}

void CMobldlg::OnButtonDelete() 
{
	UpdateData(TRUE);
	((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->DeleteString
		(((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->GetCurSel());
	// delete profile name
	WritePrivateProfileString
		("PROFILE", m_sMobileName, NULL, m_strIniFile);
	// delete section
	WritePrivateProfileString
		(m_sMobileName, NULL, NULL, m_strIniFile);
	m_sMobileName = "";	
	m_sMobileIP = "";
	m_sMobilePort = "";
	UpdateData(FALSE);	
}

void CMobldlg::OnOk() 
{
	UpdateData(TRUE);
	// Save mobile terminal configuration
	WritePrivateProfileString
		("_CONFIG_", "SESSION", m_sMobileName, m_strIniFile);	
	CDialog::OnOK();
}

void CMobldlg::SetComboIP(LPCSTR lpszName)
{
	char pszIP[100], pszPort[100];
	GetPrivateProfileString
		(lpszName, "MOBILE_IP", "", pszIP, 100, m_strIniFile);
	m_sMobileIP = pszIP; 
	GetPrivateProfileString
		(lpszName, "MOBILE_PORT", "", pszPort, 100, m_strIniFile);
	m_sMobilePort = pszPort; 
	UpdateData(FALSE);
}

void CMobldlg::OnSelendokComboMobile()
{
	// get current selection in ComboBox
	((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->GetLBText
		(((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->GetCurSel(), m_sMobileName);
	SetComboIP(m_sMobileName);	
}

BOOL CMobldlg::OnInitDialog() 
{
	PSTR pszBuf, pszProile;
	char pszName[100]; 

	CDialog::OnInitDialog();
	// Allocate a buffer for the entries - mobile terminal name. 
	pszBuf = (PSTR) LocalAlloc(LMEM_FIXED, 1024);

    // Retrieve all the entries in the [windows] section. 
	GetPrivateProfileString
		("PROFILE", NULL, "", pszBuf, 1024,m_strIniFile); 
		
	// Retrieve the string for each entry, show in ComboBox, 
	// until reaching the double null character.
	for (pszProile = pszBuf; *pszProile != '\0'; pszProile += strlen(pszProile) + 1)
    	((CComboBox *)GetDlgItem(IDC_COMBO_MOBILE))->AddString(pszProile);

	LocalFree((HANDLE) pszBuf);
	
	// Set mobile terminal configuration in ComboBox
	GetPrivateProfileString
		("_CONFIG_", "SESSION", "", pszName, 100, m_strIniFile);
	m_sMobileName = pszName;
	SetComboIP(m_sMobileName);
		 
	return TRUE;  // return TRUE  unless you set the focus to a control
}
