FtpConnectDlg.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:8k
- // FtpConnectDlg.cpp : implementation file
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "FtpConnectDlg.h"
- #include "xShell.h"
- // CFtpConnectDlg dialog
- CFtpConnectDlg::CFtpConnectDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFtpConnectDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFtpConnectDlg)
- m_bPASVMode = FALSE;
- m_bAnonymous = FALSE;
- m_sProfile = _T("");
- m_sHost = _T("");
- m_sUser = _T("");
- m_sPassword = _T("");
- m_sDowntoDir = _T("");
- m_sDescription = _T("");
- m_bUseDefPort = FALSE;
- m_nPort = 0;
- //}}AFX_DATA_INIT
- }
- void CFtpConnectDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFtpConnectDlg)
- DDX_Check(pDX, IDC_CHKPASVMODE, m_bPASVMode);
- DDX_Check(pDX, IDC_CHKANONYMOUS, m_bAnonymous);
- DDX_Text(pDX, IDC_EDTPROFILE, m_sProfile);
- DDV_MaxChars(pDX, m_sProfile, 32);
- DDX_Text(pDX, IDC_EDTHOST, m_sHost);
- DDV_MaxChars(pDX, m_sHost, 256);
- DDX_Text(pDX, IDC_EDTUSER, m_sUser);
- DDV_MaxChars(pDX, m_sUser, 64);
- DDX_Text(pDX, IDC_EDTPASS, m_sPassword);
- DDV_MaxChars(pDX, m_sPassword, 128);
- DDX_Text(pDX, IDC_EDTDOWNTO, m_sDowntoDir);
- DDV_MaxChars(pDX, m_sDowntoDir, 256);
- DDX_Text(pDX, IDC_EDTDESCR, m_sDescription);
- DDV_MaxChars(pDX, m_sDescription, 256);
- DDX_Control(pDX, IDC_STCTIPINFO, m_oTipInfoCtrl);
- DDX_Control(pDX, IDC_LISTSITES, m_oFtpSites);
- DDX_Check(pDX, IDC_CHKUSEDEFPORT, m_bUseDefPort);
- DDX_Text(pDX, IDC_EDTPORT, m_nPort);
- DDV_MinMaxUInt(pDX, m_nPort, 0, 655535);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFtpConnectDlg, CDialog)
- //{{AFX_MSG_MAP(CFtpConnectDlg)
- ON_BN_CLICKED(IDC_SAVEIT, OnAddFtpSIte)
- ON_BN_CLICKED(IDC_REMOVEIT, OnRemoveFtpSite)
- ON_BN_CLICKED(IDC_CONNECT, OnConnect)
- ON_LBN_SELCHANGE(IDC_LISTSITES, OnSelchangeListsites)
- ON_BN_CLICKED(IDC_CHKANONYMOUS, OnChkanonymous)
- ON_BN_CLICKED(IDC_BTNSELDIR, OnSelectDowntoDir)
- ON_BN_CLICKED(IDC_CHKUSEDEFPORT, OnChkusedefport)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CFtpConnectDlg message handlers
- BOOL CFtpConnectDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- //initializing the ftp sites listbox control.
- CStringList oProfileNamesList;
- if(!GetAllFtpProfileNames(oProfileNamesList))
- {
- TRACE0("Failed to get ftp sites name listn");
- return FALSE;
- }
- //clear the listbox's contents.
- for(; this->m_oFtpSites.GetCount() > 0;)
- {
- this->m_oFtpSites.DeleteString(0);
- }
- //add all ftp sites name into listbox.
- POSITION pos = oProfileNamesList.GetHeadPosition();
- for(; NULL != pos; pos = oProfileNamesList.GetHeadPosition())
- {
- this->m_oFtpSites.AddString(oProfileNamesList.GetAt(pos));
- oProfileNamesList.RemoveHead();
- }
- this->CenterWindow();
- return TRUE;
- }
- //update the UI contents.
- void CFtpConnectDlg::OnSelchangeListsites()
- {
- CString strProfileName;
- CFtpSite oFtpSite;
-
- if(!this->m_oFtpSites.GetSelCount())
- {
- this->ClearUIContents();
- return ;
- }
- this->m_oFtpSites.GetText(this->m_oFtpSites.GetCurSel(), strProfileName);
- if(Trim(strProfileName).IsEmpty())
- {
- this->m_oFtpSites.DeleteString(this->m_oFtpSites.GetSelCount());
- this->ClearUIContents();
- return ;
- }
- if(GetFtpSiteInfo(strProfileName.GetBuffer(0), oFtpSite))
- {
- CString strTipInfo;
- strTipInfo.Format("%s --- %s", oFtpSite.m_sProfile, oFtpSite.m_sHost);
- this->m_oTipInfoCtrl.SetText(strTipInfo.GetBuffer(0));
- this->FillUIContents(oFtpSite);
- }
- else
- {
- MSGBOX_ERROR(_LoadString(IDS_FTPGETFTPSITEFAILED).GetBuffer(0));
- if(RemoveFtpSiteInfo(strProfileName.GetBuffer(0)))
- {
- this->m_oFtpSites.DeleteString(this->m_oFtpSites.GetSelCount());
- return ;
- }
- }
- }
- //trim left or right space char.
- CString& CFtpConnectDlg::Trim(CString &strText)
- {
- strText.TrimLeft();
- strText.TrimRight();
- return strText;
- }
- //clear UI contents.
- void CFtpConnectDlg::ClearUIContents(void)
- {
- this->m_sProfile.Empty();
- this->m_sHost.Empty();
- this->m_sUser.Empty();
- this->m_sPassword.Empty();
- this->m_sDowntoDir.Empty();
- this->m_sDescription.Empty();
-
- this->m_bAnonymous = FALSE;
- this->m_bPASVMode = FALSE;
- this->UpdateData(FALSE);
- this->OnChkanonymous();
- }
- //fill UI contents.
- void CFtpConnectDlg::FillUIContents(CFtpSite &oFtpSite)
- {
- this->m_sProfile = oFtpSite.m_sProfile;
- this->m_sHost = oFtpSite.m_sHost;
- this->m_nPort = oFtpSite.m_nPort;
- this->m_bUseDefPort = (oFtpSite.m_nPort == 21);
- this->m_sUser = oFtpSite.m_sUser;
- this->m_sPassword = oFtpSite.m_sPassword;
- this->m_sDowntoDir = oFtpSite.m_sDowntoDir;
- this->m_sDescription = oFtpSite.m_sDescription;
-
- this->m_bAnonymous = oFtpSite.m_bAnonymous;
- this->m_bPASVMode = oFtpSite.m_bPASVMode;
- this->UpdateData(FALSE);
- this->OnChkanonymous();
- this->OnChkusedefport();
- }
- //make ftp setting information from GUI.
- void CFtpConnectDlg::MakeCurFtpSiteInfo(CFtpSite &oFtpSite)
- {
- this->UpdateData(TRUE);
- oFtpSite.m_bAnonymous = this->m_bAnonymous;
- oFtpSite.m_bPASVMode = this->m_bPASVMode;
- if(!this->m_bUseDefPort)
- {
- oFtpSite.m_nPort = this->m_nPort;
- }
- oFtpSite.m_sProfile = Trim(this->m_sProfile);
- oFtpSite.m_sHost = Trim(this->m_sHost);
- oFtpSite.m_sUser = Trim(this->m_sUser);
- oFtpSite.m_sPassword = Trim(this->m_sPassword);
- if(Trim(this->m_sDowntoDir).IsEmpty())
- {
- this->m_sDowntoDir = ENV_DEFAULT_FTPDOWNTODIR;
- }
- oFtpSite.m_sDowntoDir = Trim(this->m_sDowntoDir);
- oFtpSite.m_sDescription = Trim(this->m_sDescription);
- }
- //add ftp site setting.
- void CFtpConnectDlg::OnAddFtpSIte()
- {
- CFtpSite oFtpSite;
- MakeCurFtpSiteInfo(oFtpSite);
- //the ftp-site profile name must be not empty.
- oFtpSite.m_sProfile.TrimLeft();
- oFtpSite.m_sProfile.TrimRight();
- if(oFtpSite.m_sProfile.IsEmpty())
- {
- MSGBOX_INFO(_LoadString(IDS_FTPPROFILEEMPTY).GetBuffer(0));
- return ;
- }
- if(!AddFtpSiteInfo(oFtpSite))
- {
- MSGBOX_ERROR(_LoadString(IDS_FTPADDITEMFAILED).GetBuffer(0));
- return ;
- }
- CString strText;
- if(this->m_oFtpSites.FindString(0, this->m_sProfile.GetBuffer(0)) == LB_ERR)
- {
- this->m_oFtpSites.SetCurSel(this->m_oFtpSites.AddString(this->m_sProfile));
- }
- }
- //remove the ftp-site setting from registry.
- void CFtpConnectDlg::OnRemoveFtpSite()
- {
- if(!this->m_oFtpSites.GetSelCount())
- {
- MSGBOX_INFO(_LoadString(IDS_FTPNOSELITEM).GetBuffer(0));
- return ;
- }
- if(MSGBOX_CONFIRM(_LoadString(IDS_FTPDELCONFIRM).GetBuffer(0)) == IDYES)
- {
- this->UpdateData(TRUE);
-
- if(RemoveFtpSiteInfo(this->m_sProfile))
- {
- //refresh the listbox contents.
- int nIndex = this->m_oFtpSites.FindString(0, this->m_sProfile.GetBuffer(0));
- if(nIndex != LB_ERR) this->m_oFtpSites.DeleteString(nIndex);
- }
- else
- {
- MSGBOX_ERROR(_LoadString(IDS_FTPDELITEMFAILED).GetBuffer(0));
- return ;
- }
- }
- }
- //connect to current ftp server.
- void CFtpConnectDlg::OnConnect()
- {
- }
- //control UI display.
- void CFtpConnectDlg::OnChkanonymous()
- {
- this->UpdateData(TRUE);
- CWnd *phWnd = NULL;
- phWnd = this->GetDlgItem(IDC_EDTUSER);
- phWnd->EnableWindow(!this->m_bAnonymous);
- phWnd = this->GetDlgItem(IDC_EDTPASS);
- phWnd->EnableWindow(!this->m_bAnonymous);
- if(this->m_bAnonymous)
- {
- this->m_sUser = "anonymous";
- this->m_sPassword = "mail@mail";
- this->UpdateData(FALSE);
- }
- }
- //
- void CFtpConnectDlg::OnChkusedefport()
- {
- this->UpdateData(TRUE);
-
- CWnd *phWnd = NULL;
-
- phWnd = this->GetDlgItem(IDC_EDTPORT);
- phWnd->EnableWindow(!this->m_bUseDefPort);
-
- if(this->m_bUseDefPort)
- {
- this->m_nPort = 21;
- this->UpdateData(FALSE);
- }
- }
- void CFtpConnectDlg::OnSelectDowntoDir()
- {
- CXShell shell;
- CString sPath;
- if(shell.GetFolder(&sPath, "Select Destination Directory",
- this->GetSafeHwnd(), NULL, NULL))
- {
- this->m_sDowntoDir = sPath;
- this->UpdateData(FALSE);
- }
- else
- {
- //MSGBOX_ERROR(_LoadString(IDS_OPENSHELLFLODERFAILED).GetBuffer(0));
- return ;
- }
- }