// DlgSmtp.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "DlgSmtp.h"
#include "MsSDKTestDlg.h"
#include "MsIpcSDK.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgSmtp dialog


CDlgSmtp::CDlgSmtp(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSmtp::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSmtp)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgSmtp::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSmtp)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CDlgSmtp message handlers

BOOL CDlgSmtp::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	OnBtnGetSmtp();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSmtp::OnOK() 
{
	// TODO: Add extra validation here
	if(!OnBtnSetSmtp()){
		return;
	}
	
	CDialog::OnOK();
}

BOOL CDlgSmtp::OnBtnGetSmtp() 
{
	char sValue[32] = {0};
	Ms_Ipc_SMTP smtpInfo = {0};
	LONG nResult = Ms_Ipc_GetSMTP(m_nUserId, &smtpInfo);
	if(nResult)
	{
		CMsSDKTestDlg::OutputTips("Init smtp info failed. Please try again...");
		return FALSE;
	}
	UpdateData(TRUE);
	GetDlgItem(IDC_EDIT_SMTP_NAME)->SetWindowText(smtpInfo.sSmtpName);
	GetDlgItem(IDC_EDIT_SMTP_SENDER)->SetWindowText(smtpInfo.sSmtpSender);
	GetDlgItem(IDC_EDIT_SMTP_PASSWORD)->SetWindowText(smtpInfo.sSmtpPassword);
	GetDlgItem(IDC_EDIT_SMTP_SERVER)->SetWindowText(smtpInfo.sSmtpServer);
	_snprintf(sValue, sizeof(sValue)-1, "%d", smtpInfo.nSmtpServerPort);
	GetDlgItem(IDC_EDIT_SMTP_PORT)->SetWindowText(sValue);
	GetDlgItem(IDC_EDIT_SMTP_RECEIVER)->SetWindowText(smtpInfo.sStmpReceiver);
	UpdateData(FALSE);
	return TRUE;
}

BOOL CDlgSmtp::OnBtnSetSmtp() 
{
	char sValue[32] = {0};
	Ms_Ipc_SMTP smtpInfo = {0};
	UpdateData(TRUE);
	GetDlgItem(IDC_EDIT_SMTP_NAME)->GetWindowText(smtpInfo.sSmtpName, sizeof(smtpInfo.sSmtpName));
	GetDlgItem(IDC_EDIT_SMTP_SENDER)->GetWindowText(smtpInfo.sSmtpSender, sizeof(smtpInfo.sSmtpSender));
	GetDlgItem(IDC_EDIT_SMTP_PASSWORD)->GetWindowText(smtpInfo.sSmtpPassword, sizeof(smtpInfo.sSmtpPassword));
	GetDlgItem(IDC_EDIT_SMTP_SERVER)->GetWindowText(smtpInfo.sSmtpServer, sizeof(smtpInfo.sSmtpServer));
	GetDlgItem(IDC_EDIT_SMTP_PORT)->GetWindowText(sValue, sizeof(sValue));
	smtpInfo.nSmtpServerPort = atoi(sValue);
	GetDlgItem(IDC_EDIT_SMTP_RECEIVER)->GetWindowText(smtpInfo.sStmpReceiver, sizeof(smtpInfo.sStmpReceiver));

	if(smtpInfo.sSmtpSender[0] == '\0' || smtpInfo.sSmtpServer[0] == '\0' || smtpInfo.nSmtpServerPort < 1 || smtpInfo.nSmtpServerPort > 65535 || smtpInfo.sStmpReceiver[0] == '\0')
	{
		AfxMessageBox("Sender, Server, Port or Receiver is invalid.");
		return FALSE;
	}

	LONG nResult = Ms_Ipc_SetSMTP(m_nUserId, &smtpInfo);
	if(nResult){
		AfxMessageBox("Failed.");
		return FALSE;
	}else{
		CMsSDKTestDlg::OutputTips("Set SMTP Successfully.");
		return TRUE;
	}
}


