FTPSOCKETDlg.cpp
上传用户:ap04031022
上传日期:2022-02-14
资源大小:1981k
文件大小:18k
源码类别:

Ftp客户端

开发平台:

Visual C++

  1. // FTPSOCKETDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FTPSOCKET.h"
  5. #include "FTPSOCKETDlg.h"
  6. #include "FTPclass.h"
  7. #include "ListItem.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. char szLocDirectory[MAX_PATH];
  16. char szFtpDirectory[MAX_PATH];
  17. char szAppName[256];
  18. char szFtpName[256];
  19. char szFtpUser[20];
  20. char szFtpPassword[20];
  21. class CAboutDlg : public CDialog
  22. {
  23. public:
  24. CAboutDlg();
  25. // Dialog Data
  26. //{{AFX_DATA(CAboutDlg)
  27. enum { IDD = IDD_ABOUTBOX };
  28. //}}AFX_DATA
  29. // ClassWizard generated virtual function overrides
  30. //{{AFX_VIRTUAL(CAboutDlg)
  31. protected:
  32. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  33. //}}AFX_VIRTUAL
  34. // Implementation
  35. protected:
  36. //{{AFX_MSG(CAboutDlg)
  37. //}}AFX_MSG
  38. DECLARE_MESSAGE_MAP()
  39. };
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42. //{{AFX_DATA_INIT(CAboutDlg)
  43. //}}AFX_DATA_INIT
  44. }
  45. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  46. {
  47. CDialog::DoDataExchange(pDX);
  48. //{{AFX_DATA_MAP(CAboutDlg)
  49. //}}AFX_DATA_MAP
  50. }
  51. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  52. //{{AFX_MSG_MAP(CAboutDlg)
  53. // No message handlers
  54. //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CFTPSOCKETDlg dialog
  58. CFTPSOCKETDlg::CFTPSOCKETDlg(CWnd* pParent /*=NULL*/)
  59. : CDialog(CFTPSOCKETDlg::IDD, pParent)
  60. {
  61. //{{AFX_DATA_INIT(CFTPSOCKETDlg)
  62. m_PWD = _T("");
  63. m_Server = _T("");
  64. m_UID = _T("");
  65. m_Message = _T("");
  66. m_Port = 0;
  67. m_PASV = FALSE;
  68. //}}AFX_DATA_INIT
  69. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  70. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71. }
  72. void CFTPSOCKETDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74. CDialog::DoDataExchange(pDX);
  75. //{{AFX_DATA_MAP(CFTPSOCKETDlg)
  76. DDX_Control(pDX, IDC_LIST_SERVERFILE, m_ctrlServerFile);
  77. DDX_Control(pDX, IDC_LIST_LOCALFILE, m_ctrlLocalFile);
  78. DDX_Control(pDX, IDC_COMBOBOXEX_SERVER, m_ctrlServer);
  79. DDX_Control(pDX, IDC_COMBOBOXEX_LOCAL, m_ctrlLocal);
  80. DDX_Text(pDX, IDC_EDIT_PWD, m_PWD);
  81. DDX_Text(pDX, IDC_EDIT_SERVER, m_Server);
  82. DDX_Text(pDX, IDC_EDIT_UID, m_UID);
  83. DDX_Text(pDX, IDC_EDIT_MESSAGE, m_Message);
  84. DDX_Text(pDX, IDC_EDIT_PORT, m_Port);
  85. DDX_Check(pDX, IDC_CHECK_PASV, m_PASV);
  86. //}}AFX_DATA_MAP
  87. }
  88. BEGIN_MESSAGE_MAP(CFTPSOCKETDlg, CDialog)
  89. //{{AFX_MSG_MAP(CFTPSOCKETDlg)
  90. ON_WM_SYSCOMMAND()
  91. ON_WM_PAINT()
  92. ON_WM_QUERYDRAGICON()
  93. ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
  94. ON_NOTIFY(NM_DBLCLK, IDC_LIST_LOCALFILE, OnDblclkListLocalfile)
  95. ON_CBN_SELCHANGE(IDC_COMBOBOXEX_LOCAL, OnSelchangeComboboxexLocal)
  96. ON_NOTIFY(NM_DBLCLK, IDC_LIST_SERVERFILE, OnDblclkListServerfile)
  97. //}}AFX_MSG_MAP
  98. ON_MESSAGE(WM_SETFILE,CFTPSOCKETDlg::OnInsertLocFile)
  99. ON_MESSAGE(WM_SETREDRAWFLAG,OnSetLocRedrawFlag)
  100. ON_MESSAGE(WM_SETDIRECTORYDISPLAY,OnSetLocDirectoryDisplay)
  101. ON_MESSAGE(WM_REDISPLAYFILE,OnReDisplayLocFile)
  102. ON_MESSAGE(WM_SETDIR,OnSetLocDir)
  103. END_MESSAGE_MAP()
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CFTPSOCKETDlg message handlers
  106. BOOL CFTPSOCKETDlg::OnInitDialog()
  107. {
  108. CDialog::OnInitDialog();
  109. // Add "About..." menu item to system menu.
  110. // IDM_ABOUTBOX must be in the system command range.
  111. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  112. ASSERT(IDM_ABOUTBOX < 0xF000);
  113. CMenu* pSysMenu = GetSystemMenu(FALSE);
  114. if (pSysMenu != NULL)
  115. {
  116. CString strAboutMenu;
  117. strAboutMenu.LoadString(IDS_ABOUTBOX);
  118. if (!strAboutMenu.IsEmpty())
  119. {
  120. pSysMenu->AppendMenu(MF_SEPARATOR);
  121. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  122. }
  123. }
  124. // Set the icon for this dialog.  The framework does this automatically
  125. //  when the application's main window is not a dialog
  126. SetIcon(m_hIcon, TRUE); // Set big icon
  127. SetIcon(m_hIcon, FALSE); // Set small icon
  128. //创建ImageList
  129.     HIMAGELIST hImageList;
  130. //定义文件信息变量
  131. SHFILEINFO shFi;
  132. //获取文件图标信息
  133. hImageList = (HIMAGELIST)SHGetFileInfo("C:\",0,&shFi,sizeof(shFi),
  134. SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
  135. //将文件图象列表存入m_ctImageList
  136. m_ctImageList.m_hImageList = hImageList;
  137. // TODO: Add extra initialization here
  138. //设定客户端文件显示列表框的栏标题
  139. SetFileColumns(&m_ctrlLocalFile);
  140. SetServerFileColumns(&m_ctrlServerFile);
  141. SetImageList(&m_ctrlLocalFile,&m_ctrlLocal);
  142. SetImageList(&m_ctrlServerFile,NULL);
  143. m_ctrlLocalFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  144. //m_ctrlLocalFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  145. //获取本地驱动器信息 
  146. GetLocDir();
  147. int i=m_ctrlLocal.FindStringExact(-1,"C:\");
  148. m_ctrlLocal.SetCurSel(i);
  149. ::SetCurrentDirectory("C:\");
  150. GetLocCurrentDirectory();
  151. BeginFindLocFile();
  152. //初始化,为了调试,设定初试值
  153. m_Server="10.11.111.129";
  154. m_Port=21;
  155. m_UID="ee";
  156. m_PWD="ee";
  157. UpdateData(false);
  158. return TRUE;  // 
  159. }
  160. void CFTPSOCKETDlg::OnSysCommand(UINT nID, LPARAM lParam)
  161. {
  162. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  163. {
  164. CAboutDlg dlgAbout;
  165. dlgAbout.DoModal();
  166. }
  167. else
  168. {
  169. CDialog::OnSysCommand(nID, lParam);
  170. }
  171. }
  172. void CFTPSOCKETDlg::OnPaint() 
  173. {
  174. if (IsIconic())
  175. {
  176. CPaintDC dc(this); // device context for painting
  177. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  178. // Center icon in client rectangle
  179. int cxIcon = GetSystemMetrics(SM_CXICON);
  180. int cyIcon = GetSystemMetrics(SM_CYICON);
  181. CRect rect;
  182. GetClientRect(&rect);
  183. int x = (rect.Width() - cxIcon + 1) / 2;
  184. int y = (rect.Height() - cyIcon + 1) / 2;
  185. // Draw the icon
  186. dc.DrawIcon(x, y, m_hIcon);
  187. }
  188. else
  189. {
  190. CDialog::OnPaint();
  191. }
  192. }
  193. // The system calls this to obtain the cursor to display while the user drags
  194. //  the minimized window.
  195. HCURSOR CFTPSOCKETDlg::OnQueryDragIcon()
  196. {
  197. return (HCURSOR) m_hIcon;
  198. }
  199. void CFTPSOCKETDlg::OnButtonConnect() 
  200. {
  201. int ndx=0;
  202. CString str;
  203. //将控件中的数据更新到对应的变量中
  204. UpdateData(TRUE);
  205. m_Message=m_Message+"连接服务器:"+m_Server+"rn";
  206. m_Message=m_Message+"登录用户名:"+m_UID+"rn";
  207. UpdateData(false);
  208. if(!ftp.LogOnToServer(m_Server,m_Port,m_UID,m_PWD,"","","","",1080,0)) 
  209. {
  210. m_Message=m_Message+m_UID+"登录失败。"+"rn";
  211. UpdateData(false);
  212. return;
  213. }
  214. else
  215. {
  216. ftp.List ();
  217. while(ftp.GetLine(ndx))
  218. {
  219. AddLine(ftp.m_strLine );
  220. str="/";
  221. strcpy(szFtpDirectory,str);
  222. SetServerDir(str);
  223. }
  224. ftp.FTPcommand(""); 
  225. }
  226. }
  227. void CFTPSOCKETDlg::SetFileColumns(CListCtrl *pListCtrl)
  228. {
  229.     static struct
  230. {
  231. LPSTR pszText;
  232. int uiFormat;
  233. }columns[]={
  234. _T("文件名"),LVCFMT_LEFT,
  235. _T("日期"),LVCFMT_CENTER,
  236. _T("字节数"),LVCFMT_CENTER
  237. };
  238. for(int i=0,j=120;i<sizeof(columns)/sizeof(columns[0]);i++,j-=10)
  239. pListCtrl->InsertColumn(i,columns[i].pszText,columns[i].uiFormat,j);
  240. }
  241. void CFTPSOCKETDlg::SetServerFileColumns(CListCtrl *pListCtrl)
  242. {
  243.     static struct
  244. {
  245. LPSTR pszText;
  246. int uiFormat;
  247. }columns[]={
  248. _T("文件名"),LVCFMT_LEFT,
  249. _T("日期"),LVCFMT_CENTER,
  250. _T("权限"),LVCFMT_CENTER
  251. };
  252. for(int i=0,j=120;i<sizeof(columns)/sizeof(columns[0]);i++,j-=10)
  253. pListCtrl->InsertColumn(i,columns[i].pszText,columns[i].uiFormat,j);
  254. }
  255. BOOL CFTPSOCKETDlg::SetImageList(CListCtrl* pListCtrl,CComboBoxEx* pCombo)
  256. {
  257. pListCtrl->SetImageList(&m_ctImageList,LVSIL_SMALL);
  258. if(!pCombo)
  259. return TRUE;
  260. pCombo->SetImageList(&m_ctImageList);
  261. return TRUE;
  262. }
  263. void CFTPSOCKETDlg::GetLocDir()
  264. {
  265. int iIcon,iIconSel;
  266. char lpBuffer[MAX_PATH];
  267. char* ptr;
  268. CString str;
  269. m_iDirCount=0;
  270. ::GetLogicalDriveStrings(MAX_PATH,lpBuffer);
  271. ptr=lpBuffer;
  272. while(*ptr)
  273. {
  274. m_iDirCount++;
  275. str=ptr;
  276. GetFileIcon(str,&iIcon,&iIconSel);
  277. InsertLocDir(str,iIcon,iIconSel);
  278. ptr+=strlen(ptr)+1;
  279. }
  280. }
  281. int CFTPSOCKETDlg::InsertLocDir(CString &str, int iIcon, int iIconSel)
  282. {
  283.    int nItem=m_ctrlLocal.GetCount();
  284. COMBOBOXEXITEM Combo;
  285. Combo.mask=CBEIF_IMAGE|CBEIF_TEXT|CBEIF_SELECTEDIMAGE;
  286. Combo.iImage=iIcon;
  287. Combo.iSelectedImage=iIconSel;
  288. Combo.pszText=(LPTSTR)(LPCTSTR)str;
  289. Combo.iItem=nItem;
  290. m_ctrlLocal.InsertItem(&Combo);
  291. return nItem;
  292. }
  293. void CFTPSOCKETDlg::GetFileIcon(CString& fileName,int* iIcon,int* iIconSel)
  294. {
  295. CString str=fileName;
  296. if(str.Right(1)!="\")
  297. str+="\";
  298. SHFILEINFO shFi;
  299. if(!SHGetFileInfo(str,0,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_DISPLAYNAME))
  300. {
  301. AfxMessageBox("111");
  302. return;
  303. }
  304. *iIcon=shFi.iIcon;
  305. DestroyIcon(shFi.hIcon);
  306. if(iIconSel)
  307. {
  308. if(!SHGetFileInfo(str,0,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_OPENICON|SHGFI_DISPLAYNAME))
  309. {
  310. AfxMessageBox("222");
  311. return;
  312. }
  313. *iIconSel=shFi.iIcon;
  314. DestroyIcon(shFi.hIcon);
  315. }
  316. return ;
  317. }
  318. void CFTPSOCKETDlg::GetLocCurrentDirectory()
  319. {
  320.     CString str;
  321. m_ctrlLocal.GetWindowText(str);
  322. strcpy(szLocDirectory,str);
  323. }
  324. BOOL CFTPSOCKETDlg::BeginFindLocFile()
  325. {
  326. m_ctrlLocalFile.DeleteAllItems();
  327. FindLocFile();
  328. return TRUE;
  329. }
  330. void  CFTPSOCKETDlg::FindLocFile()
  331. {
  332. FILE_FTP_INFO* pInfo=new FILE_FTP_INFO;
  333. CFileFind fileFind;
  334. UINT i=0;
  335. CTime time;
  336. BOOL bContinue;
  337. ::SetCurrentDirectory(szLocDirectory);
  338. ::PostMessage(this->m_hWnd ,WM_SETDIRECTORYDISPLAY,0,0);
  339. bContinue=fileFind.FindFile("*.*");
  340. CString str;
  341. while(bContinue)
  342. {
  343. bContinue=fileFind.FindNextFile();
  344. if(fileFind.IsHidden())continue;
  345. str=fileFind.GetFileName();
  346. strcpy(pInfo->szFileName,str);
  347. fileFind.GetLastWriteTime(time);
  348. str=time.Format("%x");
  349. strcpy(pInfo->szFileDate,str);
  350. if(fileFind.IsDirectory())
  351. {
  352. strcpy(pInfo->szFileSize,"");
  353. pInfo->nType=DIRECTORYICON;
  354. ::SendMessage(this->m_hWnd,WM_SETFILE,(WPARAM)pInfo,
  355. (LPARAM)LOCFILE);
  356. }
  357. else
  358. {
  359. int i=fileFind.GetLength();
  360. if(i>1024)
  361. {
  362. str.Format("%d",i/1024);
  363. str+="KB";
  364. }
  365. else
  366. str.Format("%d",i);
  367. strcpy(pInfo->szFileSize,str);
  368. pInfo->nType=FILEICON;
  369. ::SendMessage(this->m_hWnd,WM_SETFILE,(WPARAM)pInfo,
  370. (LPARAM)LOCFILE);
  371. }
  372. }
  373. ::SendMessage(this->m_hWnd,WM_SETREDRAWFLAG,0,0);
  374. delete pInfo;
  375. fileFind.Close();
  376. //return 0L;
  377. }
  378. LRESULT CFTPSOCKETDlg::OnSetLocRedrawFlag(WPARAM wParam,LPARAM lParam)
  379. {
  380. if(m_bHaveDotFlag)
  381. {
  382. CString str="..";
  383. int iIcon=DIRECTORYICON;
  384. m_ctrlLocalFile.InsertItem(0,str,iIcon);
  385. m_ctrlLocalFile.SetItemData(0,DIRECTORYICON);
  386. m_bHaveDotFlag=FALSE;
  387. }
  388. m_ctrlLocalFile.Invalidate();
  389. m_ctrlLocalFile.SetRedraw();
  390. return 0L;
  391. }
  392. LRESULT CFTPSOCKETDlg::OnSetLocDirectoryDisplay(WPARAM wParam,LPARAM lParam)
  393. {
  394. CString str;
  395. str=szLocDirectory;
  396. m_ctrlLocal.SetWindowText(str);
  397. m_ctrlLocal.Invalidate();
  398. return 0L;
  399. }
  400. LRESULT CFTPSOCKETDlg::OnReDisplayLocFile(WPARAM wParam,LPARAM lParam)
  401. {
  402. CString str=(LPCTSTR)wParam;
  403. m_ctrlLocal.SetWindowText(str);
  404. m_ctrlLocal.Invalidate();
  405. GetLocCurrentDirectory();
  406. if(!BeginFindLocFile())
  407. AfxMessageBox("建立网络线程失败");
  408. return 0L;
  409. }
  410. LRESULT CFTPSOCKETDlg::OnInsertLocFile(WPARAM wParam,LPARAM lParam)
  411. {
  412. FILE_FTP_INFO* pInfo=(FILE_FTP_INFO*)wParam;
  413. CString str=pInfo->szFileName;
  414. if(str=="."||str=="..")
  415. {
  416. m_bHaveDotFlag=TRUE;
  417. return 0L;
  418. }
  419. OnInsertFile(&m_ctrlLocalFile,pInfo,lParam);
  420. return 0L;
  421. }
  422. LRESULT CFTPSOCKETDlg::OnSetLocDir(WPARAM wParam,LPARAM lParam)
  423. {
  424. CString str=(LPCTSTR)lParam;
  425. m_ctrlLocal.SetWindowText(str);
  426. m_ctrlLocalFile.SetRedraw(FALSE);
  427. m_ctrlLocalFile.DeleteAllItems();
  428. m_ctrlLocalFile.SetRedraw();
  429. return 0L;
  430. }
  431.   
  432. void CFTPSOCKETDlg::OnInsertFile(CListCtrl* pListCtrl,LPVOID pIn,LPARAM lParam)
  433. {
  434. FILE_FTP_INFO*pInfo=(FILE_FTP_INFO*)pIn;
  435. LVITEM lvIt;
  436. int iIcon,nItem;
  437. if(pInfo->nType==DIRECTORYICON)
  438. nItem=0;
  439. else
  440. nItem=pListCtrl->GetItemCount();
  441. CString fileName=pInfo->szFileName;;
  442. if((int)lParam==LOCFILE)
  443. GetFileIcon(fileName,&iIcon);
  444. else
  445. iIcon=pInfo->nType;
  446. lvIt.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
  447. lvIt.iImage=iIcon;
  448. lvIt.lParam=pInfo->nType;
  449. lvIt.pszText=pInfo->szFileName;
  450. lvIt.iSubItem=0;
  451. lvIt.iItem=nItem;
  452. int iPos=pListCtrl->InsertItem(&lvIt);
  453. lvIt.mask=LVIF_TEXT;
  454. lvIt.iItem=iPos;
  455. lvIt.pszText=pInfo->szFileDate;
  456. lvIt.iSubItem=1;
  457. pListCtrl->SetItem(&lvIt);
  458. //m_ctrlLocalFile.SetItem(&lvIt);
  459. lvIt.pszText=pInfo->szFileSize;
  460. lvIt.iSubItem=2;
  461. pListCtrl->SetItem(&lvIt);
  462. //IsFree=TRUE;
  463. }
  464. void CFTPSOCKETDlg::OnDblclkListLocalfile(NMHDR* pNMHDR, LRESULT* pResult) 
  465. {
  466.     NMLVDISPINFO* pLocInfo=(NMLVDISPINFO*)pNMHDR;
  467. CString str,str1,filename,buf;
  468. int iIcon,iIconSel;
  469. str.Format("%d",pLocInfo->item);
  470. if(str=="-1")
  471. return;
  472. int nItem=atoi(str);
  473. DWORD dwFlag=m_ctrlLocalFile.GetItemData(nItem);
  474. if(dwFlag==FILEICON)
  475. {
  476. //OnLocFileOpen();
  477. str=szLocDirectory;
  478. m_ctrlLocalFile.GetItemText(nItem,0,szLocDirectory,MAX_PATH);
  479. str1=szLocDirectory;
  480. filename=str1;
  481. if (str.Right(1)!="\")
  482. str1=str+"\"+str1;
  483. else
  484. str1=str+str1;
  485. strcpy(szLocDirectory,str);
  486. MessageBox("你要上载文件:"+str1);
  487. UpdateData(true);
  488. UpLoadFile(str1,filename,m_PASV,false);
  489. return;
  490. }
  491. ::GetCurrentDirectory(MAX_PATH,szLocDirectory);
  492. str=szLocDirectory;
  493. m_ctrlLocalFile.GetItemText(nItem,0,szLocDirectory,MAX_PATH);
  494. buf=szLocDirectory;
  495. if(str.Right(1)!="\")
  496. str+="\";
  497. str+=szLocDirectory;
  498. GetFileIcon(buf,&iIcon,&iIconSel);
  499. int i=InsertLocDir(str,iIcon,iIconSel);
  500. m_ctrlLocal.SetCurSel(i);
  501. ::SetCurrentDirectory(str);
  502. ::GetCurrentDirectory(MAX_PATH,szLocDirectory);
  503. BeginFindLocFile();
  504. *pResult = 0;
  505. }
  506. void CFTPSOCKETDlg::OnSelchangeComboboxexLocal() 
  507. {
  508. CString str;
  509.     char lpBuffer[MAX_PATH];
  510. ::GetCurrentDirectory(MAX_PATH,lpBuffer);
  511. int i=m_ctrlLocal.GetCurSel();
  512. m_ctrlLocal.GetLBText(i,(LPTSTR)(LPCTSTR)str);
  513. i=0;
  514. while(1)
  515. {
  516. if(!SetCurrentDirectory(str))
  517. {
  518. i++;
  519. if(i<2)
  520. {
  521. AfxMessageBox("请插入磁盘再试");
  522. continue;
  523. }
  524. str=lpBuffer;
  525. if((i=m_ctrlLocal.FindStringExact(-1,str))!=-1)
  526. m_ctrlLocal.SetCurSel(i);
  527. else
  528. m_ctrlLocal.SetWindowText(str);
  529. }
  530. else
  531. {
  532. ::SetCurrentDirectory(str);
  533. GetLocCurrentDirectory();
  534. BeginFindLocFile();
  535. }
  536. break;
  537. }
  538. }
  539. void CFTPSOCKETDlg::SetServerDir (CString& str)
  540. {
  541. m_ctrlServer.SetWindowText(str);
  542. m_ctrlServer.Invalidate();
  543. }
  544. void CFTPSOCKETDlg::AddLine(CString strLine)
  545. {
  546. CListItem* pItem = new CListItem();
  547. if(!pItem->ParseLine(strLine)){
  548. delete pItem;
  549. return;
  550. }
  551. AddItem(pItem);
  552. }
  553. BOOL CFTPSOCKETDlg::AddItem(CListItem *pItem)
  554. {
  555. //CListCtrl& list= GetListCtrl();
  556. // look for a match on the file name....
  557. LVITEM lvfi;
  558. LV_ITEM lvi;
  559. lvi.lParam = (LPARAM)pItem;
  560. lvi.iImage = pItem->GetImage();
  561. lvi.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
  562. int ndx = m_ctrlServerFile.GetItemCount ();
  563. lvi.pszText = pItem->m_strName.GetBuffer(0);
  564. pItem->m_strName.ReleaseBuffer();
  565. lvi.iItem = ndx;
  566. lvi.iSubItem = 0;
  567. ndx=m_ctrlServerFile.InsertItem( &lvi );
  568. lvi.mask =LVIF_TEXT;
  569. lvi.pszText = pItem->m_strDate.GetBuffer(0);
  570. pItem->m_strDate.ReleaseBuffer();
  571. lvi.iItem = ndx;
  572. lvi.iSubItem = 1;
  573. m_ctrlServerFile.SetItem( &lvi );
  574. lvi.pszText = pItem->m_strSec.GetBuffer(0);
  575. pItem->m_strSec.ReleaseBuffer();
  576. lvi.iItem = ndx;
  577. lvi.iSubItem = 2;
  578. m_ctrlServerFile.SetItem( &lvi );
  579. if (pItem->GetImage()==7)
  580. m_ctrlServerFile.SetItemData(ndx,DIRECTORYICON);
  581. else
  582. m_ctrlServerFile.SetItemData(ndx,FILEICON);
  583. return (pItem != NULL);
  584. }
  585. void CFTPSOCKETDlg::OnDblclkListServerfile(NMHDR* pNMHDR, LRESULT* pResult) 
  586. {
  587. // TODO: Add your control notification handler code here
  588. NMLVDISPINFO* pLocInfo=(NMLVDISPINFO*)pNMHDR;
  589. CString str,tempstr,tempstr2,buf;
  590. int pos1=0;
  591. str.Format("%d",pLocInfo->item);
  592. if(str=="-1")
  593. return;
  594. int nItem=atoi(str);
  595. DWORD dwFlag=m_ctrlServerFile.GetItemData(nItem);
  596. tempstr2=m_ctrlServerFile.GetItemText(nItem,0);
  597. if(dwFlag==FILEICON)
  598. {
  599. str=szFtpDirectory;
  600. if (str.Right (1)!="/")
  601. str+="/"+tempstr2;
  602. else
  603. str+=tempstr2;
  604. DownLoadFile(str,tempstr2,m_PASV,true);
  605. return;
  606. }
  607. str=szFtpDirectory;
  608. tempstr=szFtpDirectory;
  609. if(str.Right (1)!="/")
  610. str+="/";
  611. str+=m_ctrlServerFile.GetItemText(nItem,0);
  612. tempstr2=m_ctrlServerFile.GetItemText(nItem,0);
  613. if(!tempstr2.CompareNoCase(".."))
  614. {
  615. if (tempstr=="/")
  616. return;
  617. else
  618. {
  619. m_ctrlServerFile.DeleteAllItems ();
  620. Back();
  621. pos1=tempstr.GetLength ()-1;
  622. while (pos1>=0)
  623. { if (tempstr.Mid (pos1,1)!="/")
  624. {
  625. pos1--;
  626. }
  627. else
  628. {
  629. str=tempstr.Mid (0,pos1+1);
  630. break;
  631. }
  632. }
  633. strcpy(szFtpDirectory,str);
  634. SetServerDir(str);
  635. }
  636. }
  637. else
  638. {
  639. m_ctrlServerFile.DeleteAllItems ();
  640. CD(str);
  641. strcpy(szFtpDirectory,str);
  642. SetServerDir(str);
  643. }
  644. *pResult = 0;
  645. }
  646. void CFTPSOCKETDlg::CD(CString dir)
  647. {
  648. int ndx=0;
  649. dir.TrimLeft();
  650. dir.TrimRight();
  651. CString cmd = "CWD " + dir;// + "rn";
  652. ftp.FTPcommand(cmd);
  653. ftp.FTPcommand("PWD");
  654. ftp.List();
  655. while(ftp.GetLine(ndx))
  656. {
  657. UpdateData(false);
  658. AddLine(ftp.m_strLine );
  659. }
  660. ftp.FTPcommand(""); 
  661. }
  662. void CFTPSOCKETDlg::Back()
  663. {
  664. int ndx=0;
  665. ftp.FTPcommand("CDUP");
  666. ftp.List();
  667. while(ftp.GetLine(ndx))
  668. {
  669. UpdateData(false);
  670. AddLine(ftp.m_strLine );
  671. }
  672. ftp.FTPcommand(""); 
  673. }
  674. void CFTPSOCKETDlg::UpLoadFile(CString LocalFilePath,CString LocalFileName,bool pasv,bool get)
  675. {
  676. CString str;
  677. str=szFtpDirectory;
  678. if (str.Right(1)!="/")
  679. str+="/"+LocalFileName;
  680. else
  681. str+=LocalFileName;
  682. ftp.MoveFile (str,LocalFilePath,pasv,get);
  683. }
  684. void CFTPSOCKETDlg::DownLoadFile(CString ServerFilePath,CString ServerFileName,bool pasv,bool get)
  685. {
  686. CString str;
  687. str=szLocDirectory;
  688. if (str.Right(1)!="\")
  689. str+="\"+ServerFileName;
  690. else
  691. str+=ServerFileName;
  692. ftp.MoveFile (ServerFilePath,str,pasv,get);
  693. }