// DlgFtp.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgFtp dialog


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


void CDlgFtp::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgFtp)
	DDX_Control(pDX, IDC_EDIT_FTP_FILEPATH, m_EditFtpPath);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CDlgFtp message handlers

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

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

BOOL CDlgFtp::OnBtnGetFtp() 
{
	char sValue[32] = {0};
	Ms_Ipc_FTP ftpInfo = {0};
	LONG nResult = Ms_Ipc_GetFTP(m_nUserId, &ftpInfo);
	if(nResult)
	{
		CMsSDKTestDlg::OutputTips("Init ftp info failed. Please try again...");
		return FALSE;
	}
	UpdateData(TRUE);
	GetDlgItem(IDC_EDIT_FTP_USERNAME)->SetWindowText(ftpInfo.sFtpUserName);
	GetDlgItem(IDC_EDIT_FTP_PASSWORD)->SetWindowText(ftpInfo.sFtpPassword);
	GetDlgItem(IDC_EDIT_FTP_SERVER)->SetWindowText(ftpInfo.sFtpServer);
	_snprintf(sValue, sizeof(sValue)-1, "%d", ftpInfo.nFtpPort);
	GetDlgItem(IDC_EDIT_FTP_PORT)->SetWindowText(sValue);
	GetDlgItem(IDC_EDIT_FTP_FILEPATH)->SetWindowText(ftpInfo.sFilePath);
	UpdateData(FALSE);	
	return TRUE;
}

BOOL CDlgFtp::OnBtnSetFtp() 
{
	char sValue[32] = {0};
	Ms_Ipc_FTP ftpInfo = {0};
	UpdateData(TRUE);
	GetDlgItem(IDC_EDIT_FTP_USERNAME)->GetWindowText(ftpInfo.sFtpUserName, sizeof(ftpInfo.sFtpUserName));
	GetDlgItem(IDC_EDIT_FTP_PASSWORD)->GetWindowText(ftpInfo.sFtpPassword, sizeof(ftpInfo.sFtpPassword));
	GetDlgItem(IDC_EDIT_FTP_SERVER)->GetWindowText(ftpInfo.sFtpServer, sizeof(ftpInfo.sFtpServer));
	GetDlgItem(IDC_EDIT_FTP_PORT)->GetWindowText(sValue, sizeof(sValue));
	ftpInfo.nFtpPort = atoi(sValue);
	GetDlgItem(IDC_EDIT_FTP_FILEPATH)->GetWindowText(ftpInfo.sFilePath, sizeof(ftpInfo.sFilePath));

	if(ftpInfo.sFtpServer[0] == '\0' || ftpInfo.sFtpUserName[0] == '\0' || ftpInfo.sFtpPassword[0] == '\0' 
		|| ftpInfo.nFtpPort < 1 || ftpInfo.nFtpPort > 65535 || ftpInfo.sFilePath[0] == '\0')
	{
		AfxMessageBox("Server, Port, UserName, Password or Folder Name is invalid.");
		return FALSE;
	}

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