FtpConnectDlg.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:8k
源码类别:

网格计算

开发平台:

Visual C++

  1. // FtpConnectDlg.cpp : implementation file
  2. #include "stdafx.h"
  3. #include "trfAgent.h"
  4. #include "FtpConnectDlg.h"
  5. #include "xShell.h"
  6. // CFtpConnectDlg dialog
  7. CFtpConnectDlg::CFtpConnectDlg(CWnd* pParent /*=NULL*/)
  8. : CDialog(CFtpConnectDlg::IDD, pParent)
  9. {
  10. //{{AFX_DATA_INIT(CFtpConnectDlg)
  11. m_bPASVMode = FALSE;
  12. m_bAnonymous = FALSE;
  13. m_sProfile = _T("");
  14. m_sHost = _T("");
  15. m_sUser = _T("");
  16. m_sPassword = _T("");
  17. m_sDowntoDir = _T("");
  18. m_sDescription = _T("");
  19. m_bUseDefPort = FALSE;
  20. m_nPort = 0;
  21. //}}AFX_DATA_INIT
  22. }
  23. void CFtpConnectDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CFtpConnectDlg)
  27. DDX_Check(pDX, IDC_CHKPASVMODE, m_bPASVMode);
  28. DDX_Check(pDX, IDC_CHKANONYMOUS, m_bAnonymous);
  29. DDX_Text(pDX, IDC_EDTPROFILE, m_sProfile);
  30. DDV_MaxChars(pDX, m_sProfile, 32);
  31. DDX_Text(pDX, IDC_EDTHOST, m_sHost);
  32. DDV_MaxChars(pDX, m_sHost, 256);
  33. DDX_Text(pDX, IDC_EDTUSER, m_sUser);
  34. DDV_MaxChars(pDX, m_sUser, 64);
  35. DDX_Text(pDX, IDC_EDTPASS, m_sPassword);
  36. DDV_MaxChars(pDX, m_sPassword, 128);
  37. DDX_Text(pDX, IDC_EDTDOWNTO, m_sDowntoDir);
  38. DDV_MaxChars(pDX, m_sDowntoDir, 256);
  39. DDX_Text(pDX, IDC_EDTDESCR, m_sDescription);
  40. DDV_MaxChars(pDX, m_sDescription, 256);
  41. DDX_Control(pDX, IDC_STCTIPINFO, m_oTipInfoCtrl);
  42. DDX_Control(pDX, IDC_LISTSITES, m_oFtpSites);
  43. DDX_Check(pDX, IDC_CHKUSEDEFPORT, m_bUseDefPort);
  44. DDX_Text(pDX, IDC_EDTPORT, m_nPort);
  45. DDV_MinMaxUInt(pDX, m_nPort, 0, 655535);
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CFtpConnectDlg, CDialog)
  49. //{{AFX_MSG_MAP(CFtpConnectDlg)
  50. ON_BN_CLICKED(IDC_SAVEIT, OnAddFtpSIte)
  51. ON_BN_CLICKED(IDC_REMOVEIT, OnRemoveFtpSite)
  52. ON_BN_CLICKED(IDC_CONNECT, OnConnect)
  53. ON_LBN_SELCHANGE(IDC_LISTSITES, OnSelchangeListsites)
  54. ON_BN_CLICKED(IDC_CHKANONYMOUS, OnChkanonymous)
  55. ON_BN_CLICKED(IDC_BTNSELDIR, OnSelectDowntoDir)
  56. ON_BN_CLICKED(IDC_CHKUSEDEFPORT, OnChkusedefport)
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. // CFtpConnectDlg message handlers
  60. BOOL CFtpConnectDlg::OnInitDialog() 
  61. {
  62. CDialog::OnInitDialog();
  63. //initializing the ftp sites listbox control.
  64. CStringList oProfileNamesList;
  65. if(!GetAllFtpProfileNames(oProfileNamesList))
  66. {
  67. TRACE0("Failed to get ftp sites name listn");
  68. return FALSE;
  69. }
  70. //clear the listbox's contents.
  71. for(; this->m_oFtpSites.GetCount() > 0;)
  72. {
  73. this->m_oFtpSites.DeleteString(0);
  74. }
  75. //add all ftp sites name into listbox.
  76. POSITION pos = oProfileNamesList.GetHeadPosition();
  77. for(; NULL != pos; pos = oProfileNamesList.GetHeadPosition())
  78. {
  79. this->m_oFtpSites.AddString(oProfileNamesList.GetAt(pos));
  80. oProfileNamesList.RemoveHead();
  81. }
  82. this->CenterWindow();
  83. return TRUE;
  84. }
  85. //update the UI contents.
  86. void CFtpConnectDlg::OnSelchangeListsites() 
  87. {
  88. CString strProfileName;
  89. CFtpSite oFtpSite;
  90. if(!this->m_oFtpSites.GetSelCount())
  91. {
  92. this->ClearUIContents();
  93. return ;
  94. }
  95. this->m_oFtpSites.GetText(this->m_oFtpSites.GetCurSel(), strProfileName);
  96. if(Trim(strProfileName).IsEmpty())
  97. {
  98. this->m_oFtpSites.DeleteString(this->m_oFtpSites.GetSelCount());
  99. this->ClearUIContents();
  100. return ;
  101. }
  102. if(GetFtpSiteInfo(strProfileName.GetBuffer(0), oFtpSite))
  103. {
  104. CString strTipInfo;
  105. strTipInfo.Format("%s --- %s", oFtpSite.m_sProfile, oFtpSite.m_sHost);
  106. this->m_oTipInfoCtrl.SetText(strTipInfo.GetBuffer(0));
  107. this->FillUIContents(oFtpSite);
  108. }
  109. else
  110. {
  111. MSGBOX_ERROR(_LoadString(IDS_FTPGETFTPSITEFAILED).GetBuffer(0));
  112. if(RemoveFtpSiteInfo(strProfileName.GetBuffer(0)))
  113. {
  114. this->m_oFtpSites.DeleteString(this->m_oFtpSites.GetSelCount());
  115. return ;
  116. }
  117. }
  118. }
  119. //trim left or right space char.
  120. CString& CFtpConnectDlg::Trim(CString &strText)
  121. {
  122. strText.TrimLeft();
  123. strText.TrimRight();
  124. return strText;
  125. }
  126. //clear UI contents.
  127. void CFtpConnectDlg::ClearUIContents(void)
  128. {
  129. this->m_sProfile.Empty();
  130. this->m_sHost.Empty();
  131. this->m_sUser.Empty();
  132. this->m_sPassword.Empty();
  133. this->m_sDowntoDir.Empty();
  134. this->m_sDescription.Empty();
  135. this->m_bAnonymous = FALSE;
  136. this->m_bPASVMode  = FALSE;
  137. this->UpdateData(FALSE);
  138. this->OnChkanonymous();
  139. }
  140. //fill UI contents.
  141. void CFtpConnectDlg::FillUIContents(CFtpSite &oFtpSite)
  142. {
  143. this->m_sProfile = oFtpSite.m_sProfile;
  144. this->m_sHost = oFtpSite.m_sHost;
  145. this->m_nPort = oFtpSite.m_nPort;
  146. this->m_bUseDefPort = (oFtpSite.m_nPort == 21);
  147. this->m_sUser = oFtpSite.m_sUser;
  148. this->m_sPassword = oFtpSite.m_sPassword;
  149. this->m_sDowntoDir = oFtpSite.m_sDowntoDir;
  150. this->m_sDescription = oFtpSite.m_sDescription;
  151. this->m_bAnonymous = oFtpSite.m_bAnonymous;
  152. this->m_bPASVMode  = oFtpSite.m_bPASVMode;
  153. this->UpdateData(FALSE);
  154. this->OnChkanonymous();
  155. this->OnChkusedefport();
  156. }
  157. //make ftp setting information from GUI.
  158. void CFtpConnectDlg::MakeCurFtpSiteInfo(CFtpSite &oFtpSite)
  159. {
  160. this->UpdateData(TRUE);
  161. oFtpSite.m_bAnonymous = this->m_bAnonymous;
  162. oFtpSite.m_bPASVMode  = this->m_bPASVMode;
  163. if(!this->m_bUseDefPort)
  164. {
  165. oFtpSite.m_nPort = this->m_nPort;
  166. }
  167. oFtpSite.m_sProfile = Trim(this->m_sProfile);
  168. oFtpSite.m_sHost = Trim(this->m_sHost);
  169. oFtpSite.m_sUser = Trim(this->m_sUser);
  170. oFtpSite.m_sPassword = Trim(this->m_sPassword);
  171. if(Trim(this->m_sDowntoDir).IsEmpty())
  172. {
  173. this->m_sDowntoDir = ENV_DEFAULT_FTPDOWNTODIR;
  174. }
  175. oFtpSite.m_sDowntoDir = Trim(this->m_sDowntoDir);
  176. oFtpSite.m_sDescription = Trim(this->m_sDescription);
  177. }
  178. //add ftp site setting.
  179. void CFtpConnectDlg::OnAddFtpSIte() 
  180. {
  181. CFtpSite oFtpSite;
  182. MakeCurFtpSiteInfo(oFtpSite);
  183. //the ftp-site profile name must be not empty.
  184. oFtpSite.m_sProfile.TrimLeft();
  185. oFtpSite.m_sProfile.TrimRight();
  186. if(oFtpSite.m_sProfile.IsEmpty())
  187. {
  188. MSGBOX_INFO(_LoadString(IDS_FTPPROFILEEMPTY).GetBuffer(0));
  189. return ;
  190. }
  191. if(!AddFtpSiteInfo(oFtpSite))
  192. {
  193. MSGBOX_ERROR(_LoadString(IDS_FTPADDITEMFAILED).GetBuffer(0));
  194. return ;
  195. }
  196. CString strText;
  197. if(this->m_oFtpSites.FindString(0, this->m_sProfile.GetBuffer(0)) == LB_ERR)
  198. {
  199. this->m_oFtpSites.SetCurSel(this->m_oFtpSites.AddString(this->m_sProfile));
  200. }
  201. }
  202. //remove the ftp-site setting from registry.
  203. void CFtpConnectDlg::OnRemoveFtpSite() 
  204. {
  205. if(!this->m_oFtpSites.GetSelCount())
  206. {
  207. MSGBOX_INFO(_LoadString(IDS_FTPNOSELITEM).GetBuffer(0));
  208. return ;
  209. }
  210. if(MSGBOX_CONFIRM(_LoadString(IDS_FTPDELCONFIRM).GetBuffer(0)) == IDYES)
  211. {
  212. this->UpdateData(TRUE);
  213. if(RemoveFtpSiteInfo(this->m_sProfile))
  214. {
  215. //refresh the listbox contents.
  216. int nIndex = this->m_oFtpSites.FindString(0, this->m_sProfile.GetBuffer(0));
  217. if(nIndex != LB_ERR) this->m_oFtpSites.DeleteString(nIndex);
  218. }
  219. else
  220. {
  221. MSGBOX_ERROR(_LoadString(IDS_FTPDELITEMFAILED).GetBuffer(0));
  222. return ;
  223. }
  224. }
  225. }
  226. //connect to current ftp server.
  227. void CFtpConnectDlg::OnConnect()
  228. {
  229. }
  230. //control UI display.
  231. void CFtpConnectDlg::OnChkanonymous() 
  232. {
  233. this->UpdateData(TRUE);
  234. CWnd *phWnd = NULL;
  235. phWnd = this->GetDlgItem(IDC_EDTUSER);
  236. phWnd->EnableWindow(!this->m_bAnonymous);
  237. phWnd = this->GetDlgItem(IDC_EDTPASS);
  238. phWnd->EnableWindow(!this->m_bAnonymous);
  239. if(this->m_bAnonymous)
  240. {
  241. this->m_sUser = "anonymous";
  242. this->m_sPassword = "mail@mail";
  243. this->UpdateData(FALSE);
  244. }
  245. }
  246. //
  247. void CFtpConnectDlg::OnChkusedefport() 
  248. {
  249. this->UpdateData(TRUE);
  250. CWnd *phWnd = NULL;
  251. phWnd = this->GetDlgItem(IDC_EDTPORT);
  252. phWnd->EnableWindow(!this->m_bUseDefPort);
  253. if(this->m_bUseDefPort)
  254. {
  255. this->m_nPort = 21;
  256. this->UpdateData(FALSE);
  257. }
  258. }
  259. void CFtpConnectDlg::OnSelectDowntoDir() 
  260. {
  261. CXShell shell;
  262. CString sPath;
  263. if(shell.GetFolder(&sPath, "Select Destination Directory", 
  264. this->GetSafeHwnd(), NULL, NULL))
  265. {
  266. this->m_sDowntoDir = sPath;
  267. this->UpdateData(FALSE);
  268. }
  269. else
  270. {
  271. //MSGBOX_ERROR(_LoadString(IDS_OPENSHELLFLODERFAILED).GetBuffer(0));
  272. return ;
  273. }
  274. }