FileSource.cpp
上传用户:wpp2016
上传日期:2010-02-01
资源大小:1250k
文件大小:14k
源码类别:

Telnet服务器

开发平台:

Visual C++

  1. // FileSource.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "anywhere.h"
  5. #include "FileSource.h"
  6. #include "anywheredoc.h"
  7. #include "common.h"//可考虑不要
  8. #include "thread1.h"//线程
  9. #include "searchdlg.h"
  10. //#include "shadowwnd.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CFileSource
  18. IMPLEMENT_DYNCREATE(CFileSource, CFormView)
  19. CFileSource::CFileSource()
  20. : CFormView(CFileSource::IDD)
  21. {
  22. //{{AFX_DATA_INIT(CFileSource)
  23. //}}AFX_DATA_INIT
  24.   bDrv=FALSE;
  25.   remotedir="";
  26. }
  27. CFileSource::~CFileSource()
  28. {
  29. }
  30. void CFileSource::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CFormView::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CFileSource)
  34. DDX_Control(pDX, IDC_REMOTEDIR, m_cRemotedir);
  35. DDX_Control(pDX, IDC_REFRESH, m_refresh);
  36. DDX_Control(pDX, IDC_SEARCH, m_btsearch);
  37. DDX_Control(pDX, IDC_UPLOAD, m_btupload);
  38. DDX_Control(pDX, IDC_RUN, m_btrun);
  39. DDX_Control(pDX, IDC_DOWN, m_btdown);
  40. DDX_Control(pDX, IDC_DELETE, m_btdel);
  41. DDX_Control(pDX, IDC_REMOTE, m_remote);
  42. DDX_Control(pDX, IDC_LOCAL, m_local);
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CFileSource, CFormView)
  46. //{{AFX_MSG_MAP(CFileSource)
  47. ON_WM_SIZE()
  48. ON_WM_CREATE()
  49. ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
  50. ON_NOTIFY(NM_DBLCLK, IDC_REMOTE, OnDblclkRemote)
  51. ON_BN_CLICKED(IDC_SEARCH, OnSearch)
  52. ON_BN_CLICKED(IDC_DOWN, OnDown)
  53. ON_MESSAGE(WM_SETACTIVE,OnSetActive)
  54. ON_NOTIFY(NM_DBLCLK, IDC_LOCAL, OnDblclkLocal)
  55. ON_BN_CLICKED(IDC_UPLOAD, OnUpload)
  56. ON_BN_CLICKED(IDC_RUN, OnRun)
  57. ON_BN_CLICKED(IDC_DELETE, OnDelete)
  58. //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CFileSource diagnostics
  62. #ifdef _DEBUG
  63. void CFileSource::AssertValid() const
  64. {
  65. CFormView::AssertValid();
  66. }
  67. void CFileSource::Dump(CDumpContext& dc) const
  68. {
  69. CFormView::Dump(dc);
  70. }
  71. #endif //_DEBUG
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CFileSource message handlers
  74. void CFileSource::OnInitialUpdate() 
  75. {
  76. CFormView::OnInitialUpdate();
  77. m_ImageList.Create(IDB_LIST_DIR,16,1,RGB(0,255,0));
  78.     m_remote.SetImageList(&m_ImageList,LVSIL_SMALL);
  79.     CRect rect;
  80. GetClientRect(&rect);
  81.     m_remote.InsertColumn(0, "名字", LVCFMT_LEFT,rect.Width()/4);
  82. m_remote.InsertColumn(1, "大小", LVCFMT_LEFT, rect.Width()/6);
  83.     m_local.Initialize();//DisplayTree(NULL,TRUE);
  84. //EnableButton();
  85.    
  86. }
  87. void CFileSource::OnSize(UINT nType, int cx, int cy) 
  88. {
  89. CFormView::OnSize(nType, cx, cy);
  90. //int rate=4/5;
  91. CFormView::ShowScrollBar(SB_VERT,FALSE);
  92. CFormView::ShowScrollBar(SB_HORZ,FALSE);
  93. if (GetSafeHwnd())
  94. {
  95. if (m_local.GetSafeHwnd())
  96. {
  97. CRect rect(0,0,cx/2,cy*7/8);
  98. m_local.MoveWindow(&rect);
  99. }
  100. if (m_remote.GetSafeHwnd())
  101. {
  102. CRect rect2(cx/2,cy/15,cx,cy*7/8);
  103. m_remote.MoveWindow(&rect2);
  104. }
  105. if (m_cRemotedir.GetSafeHwnd())
  106. {
  107. m_cRemotedir.MoveWindow(cx/2,0,cx,cy/15,TRUE);
  108. }
  109. if (m_btupload.GetSafeHwnd())
  110. {
  111. //CRect rc1(cx/20,cy*8/9,cx/5,cy*17/18);
  112.             //m_btupload.MoveWindow(&rc1,TRUE);
  113. m_btupload.MoveWindow(cx/54,cy*9/10,cx/9,cy/13,TRUE);
  114. }
  115. if (m_btdown.GetSafeHwnd())
  116. {
  117.             //CRect rc2(cx/4+cx/40,cy*8/9,cx/3,cy*17/18);
  118. //m_btdown.MoveWindow(&rc2,TRUE);
  119. m_btdown.MoveWindow(cx*8/54,cy*9/10,cx/9,cy/13,TRUE);
  120. }
  121.         if (m_btrun.GetSafeHwnd())
  122. {
  123. m_btrun.MoveWindow(cx*15/54,cy*9/10,cx/9,cy/13,TRUE);
  124. }
  125. if (m_btdel.GetSafeHwnd())
  126. {
  127. m_btdel.MoveWindow(cx*22/54,cy*9/10,cx/9,cy/13,TRUE);
  128. }
  129. if (m_btsearch.GetSafeHwnd())
  130. {
  131. m_btsearch.MoveWindow(cx*29/54,cy*9/10,cx/9,cy/13,TRUE);
  132. }
  133. if (m_refresh.GetSafeHwnd())
  134. {
  135. m_refresh.MoveWindow(cx*36/54,cy*9/10,cx/9,cy/13,TRUE);
  136. }
  137. }
  138. }
  139. void CFileSource::AddFile(CObList &filelist)
  140. {
  141. CAnywhereDoc *pDoc=(CAnywhereDoc *)GetDocument();
  142. POSITION filelist_pos;
  143. if (!filelist.IsEmpty()) {
  144. filelist_pos=filelist.GetHeadPosition();
  145. }
  146. else return;
  147. POSITION pos;
  148. int num=0;
  149. while ((pos=filelist_pos)!=NULL) 
  150. {
  151.        filelist.GetNext(pos);
  152. if (pos) {
  153. //pDoc->filetype *pfile=(pDoc->filetype *)
  154. //CString file=(char *)filelist.GetAt(pos);
  155. filetype * pfile=(filetype*)filelist.GetAt(pos);
  156. CString file=pfile->filename;
  157.             CString length=pfile->filelength;
  158. if (file.Right(1)=="*") {
  159. file=file.Left(file.GetLength()-1);
  160. m_remote.InsertItem(num,file,1);
  161. }
  162. else 
  163. m_remote.InsertItem(num,file,3);
  164. m_remote.SetItemText(num,1,length);
  165. }
  166. num++;
  167. filelist_pos=pos;
  168. //else {
  169. //msglist_pos=Msglist.GetTailPosition();
  170. // return TRUE;//没找到
  171. //}
  172. }
  173. }
  174. void CFileSource::message(CString text)
  175. {
  176. CString order=text.Left(8);
  177. text=text.Right(text.GetLength()-8);
  178. if (order=="LISTFILE")  {
  179. CAnywhereDoc *pDoc=(CAnywhereDoc *)GetDocument();
  180. m_remote.DeleteAllItems();
  181. AddDrv();
  182. BOOL bFinished=FALSE;
  183. int pos,pos1;
  184. CString text1;
  185. int num=0;
  186.     while (!bFinished) {
  187.  if ((pos=text.Find("/"))!=-1)
  188.  {
  189. text1=text.Left(pos);//为名字+长度(目录无长度)
  190. if (text1.Right(1)=="*")
  191. {
  192. m_remote.InsertItem(num,text1.Left(text1.GetLength()-1),1);//名字为目录
  193. m_remote.SetItemData(num,1);//为目录
  194. }
  195.    
  196. else {
  197. pos1=text1.Find("?");
  198. m_remote.InsertItem(num,text1.Left(pos1),3);//插入名字
  199.             text1=text1.Right(text1.GetLength()-pos1-1);//长度
  200. m_remote.SetItemText(num,1,text1);
  201. m_remote.SetItemData(num,0);//为文件
  202. }
  203.     text=text.Right(text.GetLength()-pos-1);
  204.  }
  205.  else bFinished=true;
  206.  num++;
  207.  }
  208. m_cRemotedir.SetWindowText(remotedir);
  209. }//end if 
  210. }
  211. int CFileSource::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  212. {
  213. if (CFormView::OnCreate(lpCreateStruct) == -1)
  214. return -1;
  215.  
  216. return 0;
  217. }
  218. void CFileSource::OnRefresh() 
  219. {
  220.     if (!linkrc.bConnected) return;
  221. DWORD ThreadId;
  222.     if (remotedir.IsEmpty()) return;   
  223. CString filename="LISTFILE"+remotedir;
  224.     strcpy(linkrc.sbuf,filename);
  225. linkrc.m_hWnd=m_hMainWnd;
  226.     hSend=CreateThread(NULL,0,SendThread,&linkrc,0,&ThreadId);
  227.   
  228. }
  229. void CFileSource::OnDblclkRemote(NMHDR* pNMHDR, LRESULT* pResult) 
  230. {
  231. DWORD ThreadId;
  232.     int i =m_remote.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
  233. int dir=(int)m_remote.GetItemData(i);
  234.     if (dir==0) return;
  235.    else if (dir==1) { 
  236. CString tmp=m_remote.GetItemText(i,0);
  237. if (tmp.Right(2)=="..") {
  238.     char * str=remotedir.GetBuffer(remotedir.GetLength());
  239. for (int j=strlen(str)-1;j>=0;j--)
  240. {
  241. if (str[j]=='\') {str[j]=0;break;}
  242. }
  243. remotedir.ReleaseBuffer();
  244. if (remotedir.Right(1)==":") remotedir+="\";
  245. }
  246. else if (remotedir.Right(1)=="\") remotedir+=tmp;
  247.          else remotedir=remotedir+"\"+tmp;
  248.    }
  249.    else if (dir==2) {
  250.    remotedir=m_remote.GetItemText(i,0);}
  251. //if (remotedir.Right(2)=="..")
  252. // remotedir=olddir;
  253.     
  254. CString filename="LISTFILE"+remotedir;
  255.     strcpy(linkrc.sbuf,filename);
  256. linkrc.m_hWnd=m_hMainWnd;
  257.     hSend=CreateThread(NULL,0,SendThread,&linkrc,0,&ThreadId);
  258.     
  259. *pResult = 0;
  260. }
  261. void CFileSource::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  262. {
  263.     if (!bDrv) AddDrv();
  264. //EnableButton();
  265. CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  266. }
  267. void CFileSource::OnSearch() 
  268. {
  269.    if (!linkrc.bConnected) return;
  270. CSearchDlg sehdlg;
  271.    sehdlg.DoModal();
  272.    /*if (IDOK==sehdlg.DoModal())
  273.    {
  274.    DWORD ThreadId;
  275.    CString filename="FINDFILE"+sehdlg.filename;
  276.    strcpy(linkrc.sbuf,filename);
  277.        hSend=CreateThread(NULL,0,SendThread,&linkrc,0,&ThreadId);
  278.    }
  279.    */
  280. }
  281. void CFileSource::AddDrv()
  282. {
  283. CAnywhereDoc *pDoc=(CAnywhereDoc *)GetDocument();
  284. CString tmpdrv=pDoc->m_sys[8];
  285. if (tmpdrv.IsEmpty()) return;
  286.   int num=0;
  287.       while (!tmpdrv.IsEmpty())
  288.   {
  289. CString tmp=tmpdrv.Left(3);
  290. tmpdrv=tmpdrv.Right(tmpdrv.GetLength()-3);
  291. m_remote.InsertItem(num,tmp);
  292. m_remote.SetItemData(num,2);//为驱动器
  293. num++;
  294.   }
  295. bDrv=TRUE;
  296. }
  297. void CFileSource::OnDown() 
  298. {
  299.   if (!linkrc.bConnected) return;
  300. CString filename=m_local.GetFullPath(m_local.GetSelectedItem());
  301.   int i=m_remote.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
  302.   int j;
  303.   if ((i==-1) || (filename.IsEmpty())) {
  304.   CString sErrmsg,caption;
  305.   sErrmsg.LoadString(IDS_WRONG_ACTION);
  306.   caption.LoadString(IDS_CAPTION);
  307.   MessageBox(sErrmsg,caption,MB_OK);
  308.   return ;
  309.   }//错误的操作
  310.   
  311.   if ((j=m_remote.GetItemData(i))!=0) return;//选定下载的那个文件
  312.   CString ask,caption;
  313.   ask.LoadString(IDS_DOWN_ASK);
  314.   caption.LoadString(IDS_CAPTION);
  315.   ask=ask+m_remote.GetItemText(i,0);
  316.   if (IDYES==MessageBox(ask,caption,MB_YESNO)) {
  317.   if (remotedir.Right(1)!="\") remotedir+="\";
  318.   filename=filename+m_remote.GetItemText(i,0);//存到本地机的路径
  319.   CString remote_file="GETFILE*"+remotedir+m_remote.GetItemText(i,0);
  320.   DWORD ThreadId;
  321.   strcpy(linkrc.sbuf,remote_file);
  322.   strcpy(linkrc.rbuf,filename);
  323.   linkrc.m_hWnd=GetSafeHwnd();
  324.   hSend=CreateThread(NULL,0,GetFileThread,&linkrc,0,&ThreadId);
  325.   m_btdown.EnableWindow(FALSE);}
  326. }
  327. LRESULT CFileSource::OnSetActive(WPARAM wParam,LPARAM lParam)
  328. {
  329. if (!(BOOL)wParam) {
  330. /*CString sErrmsg,caption;
  331. sErrmsg.LoadString(IDS_GETFILE_FAILED);
  332. caption.LoadString(IDS_CAPTION);
  333. MessageBox(sErrmsg,caption,MB_OK);
  334. return 0;*/
  335. m_btdown.EnableWindow(TRUE);
  336. m_btupload.EnableWindow(TRUE);
  337. return 0;
  338. }//getfile失败
  339.    CString text=*(CString *)lParam;
  340.    CString order=text.Left(8);
  341.    text=text.Right(text.GetLength()-8);
  342.     m_btdown.EnableWindow(TRUE);
  343. m_btupload.EnableWindow(TRUE);
  344.    if (order=="GETFILE*") 
  345.    {
  346.    
  347.    CString sSucmsg,caption;
  348.    sSucmsg.LoadString(IDS_GETFILE_OK);
  349.    sSucmsg+=text;
  350.    caption.LoadString(IDS_CAPTION);
  351.    MessageBox(sSucmsg,caption,MB_OK);
  352.    return 1;
  353.    }//成功
  354.    
  355.    if (order=="PUTFILE*")
  356.    {
  357.    CString sSucmsg,caption;
  358.    sSucmsg.LoadString(IDS_PUTFILE_OK);
  359.    sSucmsg+=text;
  360.    caption.LoadString(IDS_CAPTION);
  361.    MessageBox(sSucmsg,caption,MB_OK);
  362.    return 1;
  363.    }
  364.    if (order=="RUNFILE*")
  365.    {
  366.    CString sSucmsg,caption;
  367.    sSucmsg.LoadString(IDS_RUN_OK);
  368.    caption.LoadString(IDS_CAPTION);
  369.    sSucmsg+=text;
  370.    MessageBox(sSucmsg,caption,MB_OK);
  371.    return 1;
  372.    }
  373.    if (order=="DELFILE*")
  374.    {
  375.    CString sSucmsg,caption;
  376.    sSucmsg.LoadString(IDS_DEL_OK);
  377.    caption.LoadString(IDS_CAPTION);
  378.    sSucmsg+=text;
  379.    MessageBox(sSucmsg,caption,MB_OK);
  380.    return 1;
  381.    }
  382. return 1;
  383. }
  384. void CFileSource::OnDblclkLocal(NMHDR* pNMHDR, LRESULT* pResult) 
  385. {
  386.     HTREEITEM hti=m_local.GetSelectedItem();
  387. CString filename=m_local.GetFullPath(hti);
  388. if (GetFileAttributes(filename)==FILE_ATTRIBUTE_DIRECTORY)
  389. return ;
  390. else 
  391. ShellExecute(GetSafeHwnd(),NULL,filename,NULL,NULL,SW_SHOWNORMAL);
  392. *pResult = 0;
  393. }
  394. void CFileSource::OnUpload() 
  395. {
  396.   if (!linkrc.bConnected) return;
  397. CString filepath=m_local.GetFullPath(m_local.GetSelectedItem());
  398.   //本地文件
  399.   CString filename=m_local.GetItemText(m_local.GetSelectedItem());
  400.   //欲存为的远端文件名
  401.   //int i=m_remote.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
  402.   
  403.   if ((remotedir.IsEmpty()) || (filename.IsEmpty())) {
  404.   CString sErrmsg,caption;
  405.   sErrmsg.LoadString(IDS_WRONG_ACTION);
  406.   caption.LoadString(IDS_CAPTION);
  407.   MessageBox(sErrmsg,caption,MB_OK);
  408.   return ;
  409.   }//错误的操作
  410.   //if  return;//如果当前目录为空返回
  411.   CString ask,caption;
  412.   ask.LoadString(IDS_UPLOAD_ASK);
  413.   caption.LoadString(IDS_CAPTION);
  414.   ask=ask+filepath;
  415.   if (IDYES==MessageBox(ask,caption,MB_YESNO)) {
  416.       if (remotedir.Right(1)!="\") remotedir+="\";
  417.    //filename=remotedir+filename;
  418.       CString remote_file="PUTFILE*"+remotedir+filename;
  419.       DWORD ThreadId;
  420.   strcpy(linkrc.sbuf,remote_file);
  421.   strcpy(linkrc.rbuf,filepath);
  422.   linkrc.m_hWnd=GetSafeHwnd();
  423.   hSend=CreateThread(NULL,0,PutFileThread,&linkrc,0,&ThreadId);
  424.   m_btupload.EnableWindow(FALSE);}
  425.   
  426.   //int j;
  427.   //if ((j=m_remote.GetItemData(i))!=1) return; 
  428.   
  429.   
  430. }
  431. void CFileSource::OnRun() 
  432. {
  433. if (!linkrc.bConnected) return;
  434. int i=m_remote.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
  435. if (i==-1) return;
  436.     int j=m_remote.GetItemData(i);
  437. if (j!=0) return;
  438. CString sAskmsg;
  439. sAskmsg.LoadString(IDS_RUN_ASK);
  440. if (remotedir.Right(1)!="\") remotedir+="\";
  441. CString filename=remotedir+m_remote.GetItemText(i,0);
  442. sAskmsg=sAskmsg+filename;
  443. CString caption;
  444. caption.LoadString(IDS_CAPTION);
  445. if (IDYES==MessageBox(sAskmsg,caption,MB_YESNO))
  446. {
  447. DWORD ThreadId;
  448. filename="RUNFILE*"+filename;
  449.         strcpy(linkrc.sbuf,filename);
  450.         strcpy(linkrc.rbuf,filename);
  451.         linkrc.m_hWnd=GetSafeHwnd();
  452.         hSend=CreateThread(NULL,0,SendThread,&linkrc,0,&ThreadId);
  453. }
  454. }
  455. void CFileSource::OnDelete() 
  456. {
  457. if (!linkrc.bConnected) return;
  458. int i=m_remote.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
  459. if (i==-1) return;
  460.     int j=m_remote.GetItemData(i);
  461. if (j!=0) return;
  462. CString sAskmsg;
  463. sAskmsg.LoadString(IDS_DEL_ASK);
  464. if (remotedir.Right(1)!="\") remotedir+="\";
  465. CString filename=remotedir+m_remote.GetItemText(i,0);
  466. sAskmsg=sAskmsg+filename;
  467. CString caption;
  468. caption.LoadString(IDS_CAPTION);
  469. if (IDYES==MessageBox(sAskmsg,caption,MB_YESNO))
  470. {
  471. DWORD ThreadId;
  472. filename="DELFILE*"+filename;
  473.         strcpy(linkrc.sbuf,filename);
  474.         strcpy(linkrc.rbuf,filename);
  475.         linkrc.m_hWnd=GetSafeHwnd();
  476.         hSend=CreateThread(NULL,0,SendThread,&linkrc,0,&ThreadId);
  477. }
  478. }
  479. void CFileSource::EnableButton()
  480. {
  481. if (!linkrc.bConnected) {
  482. m_btdel.EnableWindow(FALSE);
  483. m_btrun.EnableWindow(FALSE);
  484. m_btsearch.EnableWindow(FALSE);
  485. m_btupload.EnableWindow(FALSE);
  486. m_btdown.EnableWindow(FALSE);
  487. m_refresh.EnableWindow(FALSE);
  488. }
  489. else {
  490. m_btdel.EnableWindow(TRUE);
  491. m_btrun.EnableWindow(TRUE);
  492. m_btsearch.EnableWindow(TRUE);
  493. m_btupload.EnableWindow(TRUE);
  494. m_btdown.EnableWindow(TRUE);
  495. m_refresh.EnableWindow(TRUE);
  496. }
  497. }