TreeForFtp.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:10k
- // TreeForFtp.cpp : implementation file
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "TreeForFtp.h"
- #include "MainFrm.h"
- // CTreeForFtp Window
- CTreeForFtp::CTreeForFtp()
- {
- m_bIsConnecting = FALSE;
- }
- CTreeForFtp::~CTreeForFtp(){}
- BEGIN_MESSAGE_MAP(CTreeForFtp, CTreeCtrl)
- //{{AFX_MSG_MAP(CTreeForFtp)
- ON_WM_CREATE()
- ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
- //}}AFX_MSG_MAP
-
- ON_COMMAND(ID_FTPOPER_REFDIRS, OnRefreshView)
- ON_COMMAND(ID_FTPOPER_OPEN, OnConnectFtpSvr)
- ON_COMMAND(ID_FTPOPER_CLOSE, OnDisConnectFtpSvr)
-
- END_MESSAGE_MAP()
- //////////////////////////////////////////////////////////////////////////
- int CTreeForFtp::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- lpCreateStruct->style |= TVS_SHOWSELALWAYS;
- lpCreateStruct->style |= !TVS_DISABLEDRAGDROP;
-
- if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if(!this->m_imgTree.Create(IDB_FTPDIRTREE, 16, 4, RGB(255, 0, 255)))
- {
- TRACE0("Failed to create the m_imgTree for historyn");
- return -1;
- }
- this->SetImageList(&m_imgTree, TVSIL_NORMAL);
-
- RefreshContents();
- return 0;
- }
- BOOL CTreeForFtp::ExistsConnection(void)
- {
- return m_oFtpWorker.IsConnected();
- }
- BOOL CTreeForFtp::IsConnectingState(void)
- {
- return m_bIsConnecting;
- }
- //refresh the ftp-sites nodes.
- BOOL CTreeForFtp::RefreshContents(void)
- {
- //initializing the ftp sites listbox control.
- CStringList oProfileNamesList;
-
- if(!GetAllFtpProfileNames(oProfileNamesList))
- {
- TRACE0("Failed to get ftp sites name listn");
- return FALSE;
- }
- //clear the old contents.
-
- CString strItemText("");
-
- CString strCurSiteProfile = m_oFtpWorker.GetFtpSiteInfo().m_sProfile;
-
- HTREEITEM hcurnode = NULL;
- for(HTREEITEM hnode = this->GetRootItem(); hnode != NULL; )
- {
- strItemText = this->GetItemText(hnode);
-
- hcurnode = hnode;
- hnode = this->GetNextSiblingItem(hnode);
-
- if(strItemText.CollateNoCase(strCurSiteProfile))
- {
- //delete self.
- this->DeleteItem(hcurnode);
-
- //delete its children.
- if(this->ItemHasChildren(hcurnode))
- TreeView_Expand(this->GetSafeHwnd(), hcurnode, TVE_COLLAPSE | TVE_COLLAPSERESET);
-
- continue;
- }
- }
- POSITION pos = oProfileNamesList.Find(strCurSiteProfile);
- if(NULL != pos) oProfileNamesList.RemoveAt(pos);
- //add all ftp sites name into listbox.
- pos = oProfileNamesList.GetHeadPosition();
-
- for(; NULL != pos; pos = oProfileNamesList.GetHeadPosition())
- {
- HTREEITEM hnode = this->InsertItem(oProfileNamesList.GetAt(pos), 3, 3);
- oProfileNamesList.RemoveHead();
- }
- return TRUE;
- }
- void CTreeForFtp::GetCurOpendFtpSite(CFtpSite &oFtpSite)
- {
- oFtpSite = m_oFtpWorker.GetFtpSiteInfo();
- }
- //get current clicked tree node's ftp-site info.
- //if nothing selected then return false.
- BOOL CTreeForFtp::GetCurClickedFtpNodeInfo(CFtpSite &oFtpSite, HTREEITEM &hnode)
- {
- hnode = this->GetSelectedItem();
- if(NULL == hnode) return FALSE;
- for(HTREEITEM hnextnode = hnode; hnextnode != NULL; )
- {
- hnextnode = this->GetParentItem(hnode);
- if(hnextnode != NULL) hnode = hnextnode;
- }
- return GetFtpSiteInfo(this->GetItemText(hnode), oFtpSite);
- }
- //disconnect with remote ftp-server event.
- void CTreeForFtp::OnDisconnectFtp(void)
- {
- //reset ftp-site subdirectoies.
- CFtpSite oFtpSite;
- HTREEITEM hnode = NULL;
-
- oFtpSite = m_oFtpWorker.GetFtpSiteInfo();
- for(hnode = this->GetRootItem(); hnode != NULL; )
- {
- CString stritemtext = this->GetItemText(hnode);
- if(oFtpSite.m_sProfile.CompareNoCase(stritemtext) == 0) break;
- hnode = this->GetNextSiblingItem(hnode);
- }
- if(hnode != NULL)
- {
- //delete its children.
- if(this->ItemHasChildren(hnode))
- TreeView_Expand(this->GetSafeHwnd(), hnode, TVE_COLLAPSE | TVE_COLLAPSERESET);
-
- this->SetItemImage(hnode, 3, 3);
- this->SelectItem(hnode);
- }
- }
- //command message mapping.
- void CTreeForFtp::OnRefreshView()
- {
- CFtpSite oFtpSite;
- HTREEITEM hnode = NULL;
-
- RefreshContents();
- if(!GetCurClickedFtpNodeInfo(oFtpSite, hnode)) return ;
- if(oFtpSite.m_sProfile.CompareNoCase(m_oFtpWorker.GetFtpSiteInfo().m_sProfile)) return ;
- if(!m_oFtpWorker.IsConnected()) return ;
- //if current node's connection already exists then refresh its directories.
- //get its all subdirs.
- if(!m_oFtpWorker.ChangeCurrentDir("/"))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPCHANGEDIRFAILED).GetBuffer(0));
-
- OnDisConnectFtpSvr();
-
- return ;
- }
- CStringList oSubDirNames;
- if(!m_oFtpWorker.GetCurSubDirectorys(oSubDirNames))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPENUMSUBDIRFAILED).GetBuffer(0));
-
- OnDisConnectFtpSvr();
-
- return ;
- }
- HTREEITEM hparent = hnode;
-
- if(this->ItemHasChildren(hparent))
- {
- //delete children of "hparent" node.
- TreeView_Expand(this->GetSafeHwnd(), hparent, TVE_COLLAPSE | TVE_COLLAPSERESET);
- }
- for(; oSubDirNames.GetCount() > 0; )
- {
- this->InsertItem(oSubDirNames.GetHead(), 5, 6, hparent);
- oSubDirNames.RemoveHead();
- }
- this->Expand(hparent, TVE_EXPAND);
- }
- DWORD CTreeForFtp::Routine_ConnectFtpSvr(LPVOID lpParameters)
- {
- CFtpSite oFtpSite;
- HTREEITEM hnode = NULL;
-
- CTreeForFtp *pthis = (CTreeForFtp *)lpParameters;
- if(pthis == NULL) return 1;
- if(!pthis->GetCurClickedFtpNodeInfo(oFtpSite, hnode)) return 1;
-
- //if ftp-connection already exists then close it ?
-
- pthis->m_bIsConnecting = TRUE;
-
- if( pthis->m_oFtpWorker.IsConnected() &&
- MSGBOX_CONFIRM(_LoadString(IDS_FTPSUREDISCURCONN).GetBuffer(0)) == IDYES)
- {
- pthis->m_oFtpWorker.Disconnect();
- }
- //open current ftp-site.
-
- pthis->m_oFtpWorker.SetFtpSiteInfo(oFtpSite);
- if(!pthis->m_oFtpWorker.Connect())
- {
- pthis->m_bIsConnecting = FALSE;
- MSGBOX_WARNING(_LoadString(IDS_FTPCONNSVRFAILED).GetBuffer(0));
- return 1;
- }
- //get its all subdirs.
-
- if(!pthis->m_oFtpWorker.ChangeCurrentDir("/"))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPCHANGEDIRFAILED).GetBuffer(0));
-
- pthis->OnDisConnectFtpSvr();
-
- return 1;
- }
- CStringList oSubDirNames;
- if(!pthis->m_oFtpWorker.GetCurSubDirectorys(oSubDirNames))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPENUMSUBDIRFAILED).GetBuffer(0));
-
- pthis->OnDisConnectFtpSvr();
-
- return 1;
- }
- HTREEITEM hparent = hnode;
-
- if(pthis->ItemHasChildren(hparent))
- {
- //delete children of "hparent" node.
- TreeView_Expand(pthis->GetSafeHwnd(), hparent, TVE_COLLAPSE | TVE_COLLAPSERESET);
- }
- for(; oSubDirNames.GetCount() > 0; )
- {
- pthis->InsertItem(oSubDirNames.GetHead(), 5, 6, hparent);
- oSubDirNames.RemoveHead();
- }
- pthis->Expand(hparent, TVE_EXPAND);
- pthis->SetItemImage(hnode, 4, 4);
-
- pthis->m_bIsConnecting = FALSE;
- return 0;
- }
- void CTreeForFtp::OnConnectFtpSvr()
- {
- if(!this->m_bIsConnecting)
- {
- DWORD dwthreadid = 0x0000;
- CreateThread(NULL, 0, Routine_ConnectFtpSvr, this, 0, &dwthreadid);
- }
- }
- void CTreeForFtp::OnDisConnectFtpSvr()
- {
- OnDisconnectFtp();
- m_oFtpWorker.Disconnect();
- }
- CString CTreeForFtp::GetNodeFTPDir(HTREEITEM hNode)
- {
- CString strCurDir("");
-
- for(; hNode != NULL; )
- {
- strCurDir = this->GetItemText(hNode) + "/" + strCurDir;
- hNode = this->GetParentItem(hNode);
- if(!this->GetParentItem(hNode)) break;
- }
- strCurDir = "/" + strCurDir;
- return strCurDir;
- }
- void CTreeForFtp::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
-
- if(!this->m_oFtpWorker.IsConnected()) return ;
-
- if(!this->GetParentItem(pNMTreeView->itemNew.hItem)) return ;
-
- CWaitCursor waitcursor;
-
- HTREEITEM hnode = pNMTreeView->itemNew.hItem;
- //get all files info that in the current directory.
-
- CString strCurdir = this->GetNodeFTPDir(hnode);
-
- if(!this->m_oFtpWorker.ChangeCurrentDir(strCurdir))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPCHANGEDIRFAILED).GetBuffer(0));
-
- this->OnDisConnectFtpSvr();
-
- return ;
- }
- //enum all subdirs in this subdir.
-
- CStringList oSubDirNames;
-
- if(this->ItemHasChildren(hnode))
- {
- TreeView_Expand(this->GetSafeHwnd(), hnode, TVE_COLLAPSE | TVE_COLLAPSERESET);
- }
- if(!this->m_oFtpWorker.GetCurSubDirectorys(oSubDirNames))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPENUMSUBDIRFAILED).GetBuffer(0));
-
- this->OnDisConnectFtpSvr();
-
- return ;
- }
- for(; oSubDirNames.GetCount() > 0; )
- {
- this->InsertItem(oSubDirNames.GetHead(), 5, 6, hnode);
- oSubDirNames.RemoveHead();
- }
- this->Expand(hnode, TVE_EXPAND);
- //
- CMainFrame *pMainFrame = (CMainFrame*)::AfxGetMainWnd();
- if(NULL == pMainFrame) return ;
- CList<LPREMOTEFILEINFO,LPREMOTEFILEINFO&> oFileInfos;
- if(!this->m_oFtpWorker.GetFileNamesOfCurDir(oFileInfos))
- {
- MSGBOX_WARNING(_LoadString(IDS_FTPGETFTPFILESFAILED).GetBuffer(0));
-
- this->OnDisConnectFtpSvr();
-
- return ;
- }
- pMainFrame->m_pwndWorkFrame->m_pwndFileTransView->m_pwndFileSendList->RemoveAllFtpFiles();
- for(; oFileInfos.GetCount() > 0; )
- {
- REMOTEFILEINFO *pitem = (REMOTEFILEINFO*)oFileInfos.GetHead();
- if(NULL != pitem)
- {
- pMainFrame->m_pwndWorkFrame->m_pwndFileTransView->
- m_pwndFileSendList->AddFtpFileIntoSendList(
- strCurdir, pitem->szRemoteFile, pitem->dwFilesize);
- delete pitem;
- }
- oFileInfos.RemoveHead();
- }
- }
- //make local file name from ftp-server dirname and ftp-filename.
- CString CTreeForFtp::MakeLocalFileName(LPCSTR lpszFtpDir, LPCSTR lpszFtpFileName)
- {
- CString strRet(""), strPath("");
- strPath.Format("%s%s\%s%s", ENV_SYSROOTDIR, ENV_SYSFTPFLEDIR,
- m_oFtpWorker.GetFtpSiteInfo().m_sProfile, lpszFtpDir);
- strPath.Replace(_T("/"), _T("\"));
- strPath.Delete(strPath.GetLength()-1);
- if(!DirectoryExists(strPath.GetBuffer(0)))
- {
- if(!CreateDirectorys(strPath.GetBuffer(0))) return strRet;
- }
- strRet.Format("%s\%s", strPath.GetBuffer(0), lpszFtpFileName);
- return strRet;
- }
- //download remote ftp-server file to local directory.
- BOOL CTreeForFtp::DownloadFile(LPREMOTEFILEINFO lpRemoteFile,
- LPCSTR lpszLocalFileName)
- {
- return m_oFtpWorker.DownloadFile(lpRemoteFile, lpszLocalFileName);
- }