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

网格计算

开发平台:

Visual C++

  1. // TreeForFtp.cpp : implementation file
  2. #include "stdafx.h"
  3. #include "trfAgent.h"
  4. #include "TreeForFtp.h"
  5. #include "MainFrm.h"
  6. // CTreeForFtp Window
  7. CTreeForFtp::CTreeForFtp()
  8. {
  9. m_bIsConnecting = FALSE;
  10. }
  11. CTreeForFtp::~CTreeForFtp(){}
  12. BEGIN_MESSAGE_MAP(CTreeForFtp, CTreeCtrl)
  13. //{{AFX_MSG_MAP(CTreeForFtp)
  14. ON_WM_CREATE()
  15. ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
  16. //}}AFX_MSG_MAP
  17. ON_COMMAND(ID_FTPOPER_REFDIRS, OnRefreshView)
  18. ON_COMMAND(ID_FTPOPER_OPEN, OnConnectFtpSvr)
  19. ON_COMMAND(ID_FTPOPER_CLOSE, OnDisConnectFtpSvr)
  20. END_MESSAGE_MAP()
  21. //////////////////////////////////////////////////////////////////////////
  22. int CTreeForFtp::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  23. {
  24. lpCreateStruct->style |= TVS_SHOWSELALWAYS;
  25. lpCreateStruct->style |= !TVS_DISABLEDRAGDROP;
  26. if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
  27. return -1;
  28. if(!this->m_imgTree.Create(IDB_FTPDIRTREE, 16, 4, RGB(255, 0, 255)))
  29. {
  30. TRACE0("Failed to create the m_imgTree for historyn");
  31. return -1;
  32. }
  33. this->SetImageList(&m_imgTree, TVSIL_NORMAL);
  34. RefreshContents();
  35. return 0;
  36. }
  37. BOOL CTreeForFtp::ExistsConnection(void)
  38. {
  39. return m_oFtpWorker.IsConnected();
  40. }
  41. BOOL CTreeForFtp::IsConnectingState(void)
  42. {
  43. return m_bIsConnecting;
  44. }
  45. //refresh the ftp-sites nodes.
  46. BOOL CTreeForFtp::RefreshContents(void)
  47. {
  48. //initializing the ftp sites listbox control.
  49. CStringList oProfileNamesList;
  50. if(!GetAllFtpProfileNames(oProfileNamesList))
  51. {
  52. TRACE0("Failed to get ftp sites name listn");
  53. return FALSE;
  54. }
  55. //clear the old contents.
  56. CString strItemText("");
  57. CString strCurSiteProfile = m_oFtpWorker.GetFtpSiteInfo().m_sProfile;
  58. HTREEITEM hcurnode = NULL;
  59. for(HTREEITEM hnode = this->GetRootItem(); hnode != NULL; )
  60. {
  61. strItemText = this->GetItemText(hnode);
  62. hcurnode = hnode;
  63. hnode = this->GetNextSiblingItem(hnode);
  64. if(strItemText.CollateNoCase(strCurSiteProfile))
  65. {
  66. //delete self.
  67. this->DeleteItem(hcurnode);
  68. //delete its children.
  69. if(this->ItemHasChildren(hcurnode))
  70. TreeView_Expand(this->GetSafeHwnd(), hcurnode, TVE_COLLAPSE | TVE_COLLAPSERESET);
  71. continue;
  72. }
  73. }
  74. POSITION pos = oProfileNamesList.Find(strCurSiteProfile);
  75. if(NULL != pos) oProfileNamesList.RemoveAt(pos);
  76. //add all ftp sites name into listbox.
  77. pos = oProfileNamesList.GetHeadPosition();
  78. for(; NULL != pos; pos = oProfileNamesList.GetHeadPosition())
  79. {
  80. HTREEITEM hnode = this->InsertItem(oProfileNamesList.GetAt(pos), 3, 3);
  81. oProfileNamesList.RemoveHead();
  82. }
  83. return TRUE;
  84. }
  85. void CTreeForFtp::GetCurOpendFtpSite(CFtpSite &oFtpSite)
  86. {
  87. oFtpSite = m_oFtpWorker.GetFtpSiteInfo();
  88. }
  89. //get current clicked tree node's ftp-site info.
  90. //if nothing selected then return false.
  91. BOOL CTreeForFtp::GetCurClickedFtpNodeInfo(CFtpSite &oFtpSite, HTREEITEM &hnode)
  92. {
  93. hnode = this->GetSelectedItem();
  94. if(NULL == hnode) return FALSE;
  95. for(HTREEITEM hnextnode = hnode; hnextnode != NULL; )
  96. {
  97. hnextnode = this->GetParentItem(hnode);
  98. if(hnextnode != NULL) hnode = hnextnode;
  99. }
  100. return GetFtpSiteInfo(this->GetItemText(hnode), oFtpSite);
  101. }
  102. //disconnect with remote ftp-server event.
  103. void CTreeForFtp::OnDisconnectFtp(void)
  104. {
  105. //reset ftp-site subdirectoies.
  106. CFtpSite oFtpSite;
  107. HTREEITEM hnode = NULL;
  108. oFtpSite = m_oFtpWorker.GetFtpSiteInfo();
  109. for(hnode = this->GetRootItem(); hnode != NULL; )
  110. {
  111. CString stritemtext = this->GetItemText(hnode);
  112. if(oFtpSite.m_sProfile.CompareNoCase(stritemtext) == 0) break;
  113. hnode = this->GetNextSiblingItem(hnode);
  114. }
  115. if(hnode != NULL)
  116. {
  117. //delete its children.
  118. if(this->ItemHasChildren(hnode))
  119. TreeView_Expand(this->GetSafeHwnd(), hnode, TVE_COLLAPSE | TVE_COLLAPSERESET);
  120. this->SetItemImage(hnode, 3, 3);
  121. this->SelectItem(hnode);
  122. }
  123. }
  124. //command message mapping.
  125. void CTreeForFtp::OnRefreshView()
  126. {
  127. CFtpSite  oFtpSite;
  128. HTREEITEM hnode = NULL;
  129. RefreshContents();
  130. if(!GetCurClickedFtpNodeInfo(oFtpSite, hnode)) return ;
  131. if(oFtpSite.m_sProfile.CompareNoCase(m_oFtpWorker.GetFtpSiteInfo().m_sProfile)) return ;
  132. if(!m_oFtpWorker.IsConnected()) return ;
  133. //if current node's connection already exists then refresh its directories.
  134. //get its all subdirs.
  135. if(!m_oFtpWorker.ChangeCurrentDir("/"))
  136. {
  137. MSGBOX_WARNING(_LoadString(IDS_FTPCHANGEDIRFAILED).GetBuffer(0));
  138. OnDisConnectFtpSvr();
  139. return ;
  140. }
  141. CStringList oSubDirNames;
  142. if(!m_oFtpWorker.GetCurSubDirectorys(oSubDirNames))
  143. {
  144. MSGBOX_WARNING(_LoadString(IDS_FTPENUMSUBDIRFAILED).GetBuffer(0));
  145. OnDisConnectFtpSvr();
  146. return ;
  147. }
  148. HTREEITEM hparent = hnode;
  149. if(this->ItemHasChildren(hparent))
  150. {
  151. //delete children of "hparent" node.
  152. TreeView_Expand(this->GetSafeHwnd(), hparent, TVE_COLLAPSE | TVE_COLLAPSERESET);
  153. }
  154. for(; oSubDirNames.GetCount() > 0; )
  155. {
  156. this->InsertItem(oSubDirNames.GetHead(), 5, 6, hparent);
  157. oSubDirNames.RemoveHead();
  158. }
  159. this->Expand(hparent, TVE_EXPAND);
  160. }
  161. DWORD CTreeForFtp::Routine_ConnectFtpSvr(LPVOID lpParameters)
  162. {
  163. CFtpSite oFtpSite;
  164. HTREEITEM hnode = NULL;
  165. CTreeForFtp *pthis = (CTreeForFtp *)lpParameters;
  166. if(pthis == NULL) return 1;
  167. if(!pthis->GetCurClickedFtpNodeInfo(oFtpSite, hnode)) return 1;
  168. //if ftp-connection already exists then close it ?
  169. pthis->m_bIsConnecting = TRUE;
  170. if( pthis->m_oFtpWorker.IsConnected() && 
  171. MSGBOX_CONFIRM(_LoadString(IDS_FTPSUREDISCURCONN).GetBuffer(0)) == IDYES)
  172. {
  173. pthis->m_oFtpWorker.Disconnect();
  174. }
  175. //open current ftp-site.
  176. pthis->m_oFtpWorker.SetFtpSiteInfo(oFtpSite);
  177. if(!pthis->m_oFtpWorker.Connect())
  178. {
  179. pthis->m_bIsConnecting = FALSE;
  180. MSGBOX_WARNING(_LoadString(IDS_FTPCONNSVRFAILED).GetBuffer(0));
  181. return 1;
  182. }
  183. //get its all subdirs.
  184. if(!pthis->m_oFtpWorker.ChangeCurrentDir("/"))
  185. {
  186. MSGBOX_WARNING(_LoadString(IDS_FTPCHANGEDIRFAILED).GetBuffer(0));
  187. pthis->OnDisConnectFtpSvr();
  188. return 1;
  189. }
  190. CStringList oSubDirNames;
  191. if(!pthis->m_oFtpWorker.GetCurSubDirectorys(oSubDirNames))
  192. {
  193. MSGBOX_WARNING(_LoadString(IDS_FTPENUMSUBDIRFAILED).GetBuffer(0));
  194. pthis->OnDisConnectFtpSvr();
  195. return 1;
  196. }
  197. HTREEITEM hparent = hnode;
  198. if(pthis->ItemHasChildren(hparent))
  199. {
  200. //delete children of "hparent" node.
  201. TreeView_Expand(pthis->GetSafeHwnd(), hparent, TVE_COLLAPSE | TVE_COLLAPSERESET);
  202. }
  203. for(; oSubDirNames.GetCount() > 0; )
  204. {
  205. pthis->InsertItem(oSubDirNames.GetHead(), 5, 6, hparent);
  206. oSubDirNames.RemoveHead();
  207. }
  208. pthis->Expand(hparent, TVE_EXPAND);
  209. pthis->SetItemImage(hnode, 4, 4);
  210. pthis->m_bIsConnecting = FALSE;
  211. return 0;
  212. }
  213. void CTreeForFtp::OnConnectFtpSvr()
  214. {
  215. if(!this->m_bIsConnecting)
  216. {
  217. DWORD dwthreadid = 0x0000;
  218. CreateThread(NULL, 0, Routine_ConnectFtpSvr, this, 0, &dwthreadid);
  219. }
  220. }
  221. void CTreeForFtp::OnDisConnectFtpSvr()
  222. {
  223. OnDisconnectFtp();
  224. m_oFtpWorker.Disconnect();
  225. }
  226. CString CTreeForFtp::GetNodeFTPDir(HTREEITEM hNode)
  227. {
  228. CString strCurDir("");
  229. for(; hNode != NULL; )
  230. {
  231. strCurDir = this->GetItemText(hNode) + "/" + strCurDir;
  232. hNode = this->GetParentItem(hNode);
  233. if(!this->GetParentItem(hNode)) break;
  234. }
  235. strCurDir = "/" + strCurDir;
  236. return strCurDir;
  237. }
  238. void CTreeForFtp::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
  239. {
  240. *pResult = 0;
  241. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  242. if(!this->m_oFtpWorker.IsConnected()) return ;
  243. if(!this->GetParentItem(pNMTreeView->itemNew.hItem)) return ;
  244. CWaitCursor waitcursor;
  245. HTREEITEM hnode = pNMTreeView->itemNew.hItem;
  246. //get all files info that in the current directory.
  247. CString strCurdir = this->GetNodeFTPDir(hnode);
  248. if(!this->m_oFtpWorker.ChangeCurrentDir(strCurdir))
  249. {
  250. MSGBOX_WARNING(_LoadString(IDS_FTPCHANGEDIRFAILED).GetBuffer(0));
  251. this->OnDisConnectFtpSvr();
  252. return ;
  253. }
  254. //enum all subdirs in this subdir.
  255. CStringList oSubDirNames;
  256. if(this->ItemHasChildren(hnode))
  257. {
  258. TreeView_Expand(this->GetSafeHwnd(), hnode, TVE_COLLAPSE | TVE_COLLAPSERESET);
  259. }
  260. if(!this->m_oFtpWorker.GetCurSubDirectorys(oSubDirNames))
  261. {
  262. MSGBOX_WARNING(_LoadString(IDS_FTPENUMSUBDIRFAILED).GetBuffer(0));
  263. this->OnDisConnectFtpSvr();
  264. return ;
  265. }
  266. for(; oSubDirNames.GetCount() > 0; )
  267. {
  268. this->InsertItem(oSubDirNames.GetHead(), 5, 6, hnode);
  269. oSubDirNames.RemoveHead();
  270. }
  271. this->Expand(hnode, TVE_EXPAND);
  272. //
  273. CMainFrame *pMainFrame = (CMainFrame*)::AfxGetMainWnd();
  274. if(NULL == pMainFrame) return ;
  275. CList<LPREMOTEFILEINFO,LPREMOTEFILEINFO&> oFileInfos;
  276. if(!this->m_oFtpWorker.GetFileNamesOfCurDir(oFileInfos))
  277. {
  278. MSGBOX_WARNING(_LoadString(IDS_FTPGETFTPFILESFAILED).GetBuffer(0));
  279. this->OnDisConnectFtpSvr();
  280. return ;
  281. }
  282. pMainFrame->m_pwndWorkFrame->m_pwndFileTransView->m_pwndFileSendList->RemoveAllFtpFiles();
  283. for(; oFileInfos.GetCount() > 0; )
  284. {
  285. REMOTEFILEINFO *pitem = (REMOTEFILEINFO*)oFileInfos.GetHead();
  286. if(NULL != pitem)
  287. {
  288. pMainFrame->m_pwndWorkFrame->m_pwndFileTransView->
  289. m_pwndFileSendList->AddFtpFileIntoSendList(
  290. strCurdir, pitem->szRemoteFile, pitem->dwFilesize);
  291. delete pitem;
  292. }
  293. oFileInfos.RemoveHead();
  294. }
  295. }
  296. //make local file name from ftp-server dirname and ftp-filename.
  297. CString CTreeForFtp::MakeLocalFileName(LPCSTR lpszFtpDir, LPCSTR lpszFtpFileName)
  298. {
  299. CString strRet(""), strPath("");
  300. strPath.Format("%s%s\%s%s", ENV_SYSROOTDIR, ENV_SYSFTPFLEDIR,
  301. m_oFtpWorker.GetFtpSiteInfo().m_sProfile, lpszFtpDir);
  302. strPath.Replace(_T("/"), _T("\"));
  303. strPath.Delete(strPath.GetLength()-1);
  304. if(!DirectoryExists(strPath.GetBuffer(0)))
  305. {
  306. if(!CreateDirectorys(strPath.GetBuffer(0))) return strRet;
  307. }
  308. strRet.Format("%s\%s", strPath.GetBuffer(0), lpszFtpFileName);
  309. return strRet;
  310. }
  311. //download remote ftp-server file to local directory.
  312. BOOL CTreeForFtp::DownloadFile(LPREMOTEFILEINFO lpRemoteFile,
  313.    LPCSTR lpszLocalFileName)
  314. {
  315. return m_oFtpWorker.DownloadFile(lpRemoteFile, lpszLocalFileName);
  316. }