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

网格计算

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "xshell.h"
  3. #include "mainfrm.h"
  4. #include "trfAgent.h"
  5. #include "variantex.h"
  6. #include "FileListRecv.h"
  7. #include "FileListSend.h"
  8. #include "FileRenameDlg.h"
  9. #include "FileNameInputDlg.h"
  10. //文件接收和发送列表的列头数
  11. static const int G_FILELISTVIEW_COL_COUNTS = 7;
  12. IMPLEMENT_DYNCREATE(CFileListRecv, CListView)
  13. CFileListRecv::CFileListRecv()
  14. {
  15. this->m_sortedBy = FILE_NAME;
  16. this->m_bCanSort = TRUE;
  17. this->m_nRecvRefCnt = 0;
  18. }
  19. CFileListRecv::~CFileListRecv(){}
  20. BEGIN_MESSAGE_MAP(CFileListRecv, CListView)
  21. //{{AFX_MSG_MAP(CFileListRecv)
  22. ON_WM_CONTEXTMENU()
  23. ON_WM_CREATE()
  24. ON_WM_DESTROY()
  25. ON_WM_KEYDOWN()
  26. ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  27. ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBeginDrag)
  28. ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
  29. //}}AFX_MSG_MAP
  30. ON_COMMAND(ID_FLEOPER_OPEN, OnFileOperForOpen)
  31. ON_COMMAND(ID_FLEOPER_COPY, OnFileOperForCopy)
  32. ON_COMMAND(ID_FLEOPER_MOVEFILE, OnFileOperForMove)
  33. ON_COMMAND(ID_FLEOPER_RENAME, OnFileOperForRename)
  34. ON_COMMAND(ID_FLEOPER_DELFILE, OnFileOperForDelete)
  35. ON_COMMAND(ID_FLEOPER_DELLINK, OnFileOperForDelLink)
  36. ON_COMMAND(ID_FLEOPER_VIEWFLEPROP, OnFileOperForViewProp)
  37. ON_COMMAND(ID_FLEOPER_TODESTDIR, OnFileOperForViewDestDir)
  38. ON_COMMAND(ID_FLEOPER_RESENDFLE, OnFileOperForResendFiles)
  39. END_MESSAGE_MAP()
  40. void CFileListRecv::OnDraw(CDC* pDC)
  41. {
  42. CDocument* pDoc = GetDocument();
  43. }
  44. #ifdef _DEBUG
  45. void CFileListRecv::AssertValid() const
  46. {
  47. CListView::AssertValid();
  48. }
  49. void CFileListRecv::Dump(CDumpContext& dc) const
  50. {
  51. CListView::Dump(dc);
  52. }
  53. #endif //_DEBUG
  54. int CFileListRecv::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  55. {
  56. lpCreateStruct->style |= (LVS_REPORT | LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS);
  57. if (CListView::OnCreate(lpCreateStruct) == -1)
  58. return -1;
  59. //初始化ListView的显示风格
  60. this->GetListCtrl().SetExtendedStyle(LVS_EX_FULLROWSELECT);
  61. //设置EDIT的字体
  62. this->SetFont(CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT)));
  63. //设置ImageList
  64. if(!m_imgView.Create(IDB_OUTPUTSPACE, 16, 3, RGB (255, 0, 0)))
  65. {
  66. TRACE0("Failed to create the m_imgViewn");
  67. return -1;
  68. }
  69. this->GetListCtrl().SetImageList(&m_imgView, LVSIL_SMALL);
  70. //初始化ListView的列头
  71. CString strColCap = _T("");
  72. //列头的宽度和风格
  73. static const struct
  74. {
  75. UINT nColHdrId;
  76. int  nFormat;
  77. int  nWidth;
  78. }colRecvData[] = {
  79. {IDS_LIST_FILENAME, LVCFMT_LEFT, 120},
  80. {IDS_LIST_FILESIZE, LVCFMT_RIGHT, 100},
  81. {IDS_LIST_LEFTFILESIZE, LVCFMT_RIGHT, 100},
  82. {IDS_LIST_SRCADDR, LVCFMT_LEFT, 120},
  83. {IDS_LIST_SRCDIR, LVCFMT_LEFT, 150},
  84. {IDS_LIST_DESTDIR, LVCFMT_LEFT, 100},
  85. {IDS_LIST_RECVTIME, LVCFMT_LEFT, 100}
  86. };
  87. for(int i = 0; i < G_FILELISTVIEW_COL_COUNTS; ++i){
  88. VERIFY(strColCap.LoadString(colRecvData[i].nColHdrId));
  89. this->GetListCtrl().InsertColumn(i, LPCTSTR(strColCap), colRecvData[i].nFormat, colRecvData[i].nWidth);
  90. }
  91. //初始化时以文件名来对列表数据进行排序
  92.     this->GetListCtrl().SortItems(LVCompareProc, static_cast<DWORD>(this->m_sortedBy));
  93. if(!m_popmenu.LoadMenu(IDR_FILEOPERS))
  94. {
  95. MSGBOX_ERROR(_LoadString(IDS_LOADRESFAILED).GetBuffer(0));
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. void CFileListRecv::OnInitialUpdate() 
  101. {
  102. CListView::OnInitialUpdate();
  103. VERIFY(m_DropTarget.Register(this));
  104. }
  105. void CFileListRecv::OnDestroy() 
  106. {
  107. //dispose all items' data.
  108. this->SelectAllItems();
  109. this->CleanupSelectedItems();
  110. CListView::OnDestroy();
  111. }
  112. void CFileListRecv::OnContextMenu(CWnd* pWnd, CPoint point) 
  113. {
  114. if(m_nRecvRefCnt > 0 || NULL == m_popmenu.GetSafeHmenu()) return ;
  115. CMenu *psubmenu = m_popmenu.GetSubMenu(0);
  116. if(NULL == psubmenu) return ;
  117. psubmenu->TrackPopupMenu(TPM_RIGHTBUTTON, point.x, point.y, pWnd);
  118. }
  119. void CFileListRecv::SelectAllItems()
  120. {
  121. int index = 0;
  122. CListCtrl &pListCtrl = this->GetListCtrl();
  123. for(index = 0; index < pListCtrl.GetItemCount(); ++index)
  124. {
  125. ListView_SetItemState (this->GetSafeHwnd(),
  126. index,
  127. LVIS_FOCUSED | LVIS_SELECTED,
  128. 0x000F); 
  129. }
  130. }
  131. void CFileListRecv::EnableSort(const BOOL bCanSort)
  132. {
  133. this->m_bCanSort = bCanSort;
  134. }
  135. //return the item index.
  136. int CFileListRecv::AddFile(LPNETFILEHDR lpfilehdr)
  137. {
  138. assert(NULL != lpfilehdr);
  139. CListCtrl &pListCtrl = this->GetListCtrl();
  140. int nitem = pListCtrl.GetItemCount();
  141. CString *plpsz = new CString[G_FILELISTVIEW_COL_COUNTS];
  142. //file name
  143. char lpfilename[_MAX_PATH];
  144. memset(lpfilename, 0x0, sizeof(lpfilename));
  145. ExtractFileName(lpfilehdr->szFileName, lpfilename, sizeof(lpfilename));
  146. nitem = pListCtrl.InsertItem(nitem, lpfilename, 6);
  147. plpsz[0].Format("%s", lpfilename);
  148. //file size
  149. char lpfilesize[32+1];
  150. memset(lpfilesize, 0x0, sizeof(lpfilesize));
  151. DWORD dwsize = lpfilehdr->fiFileInfo.nFileSizeLow;
  152. _snprintf(lpfilesize, 32, "%d", dwsize);
  153. pListCtrl.SetItemText(nitem, 1, lpfilesize);
  154. plpsz[1].Format("%s", lpfilesize);
  155. //file left size
  156. _snprintf(lpfilesize, 32, "%d", 0);
  157. pListCtrl.SetItemText(nitem, 2, lpfilesize);
  158. plpsz[2].Format("%s", lpfilesize);
  159. //file source address
  160. _snprintf(lpfilename, sizeof(lpfilename), "%s / %s", lpfilehdr->szSenderHost, lpfilehdr->szSenderIp);
  161. pListCtrl.SetItemText(nitem, 3, lpfilename);
  162. plpsz[3].Format("%s", lpfilename);
  163. //file source directory
  164. memset(lpfilename, 0x0, sizeof(lpfilename));
  165. ExtractFilePath(lpfilehdr->szFileName, lpfilename, sizeof(lpfilename));
  166. pListCtrl.SetItemText(nitem, 4, lpfilename);
  167. plpsz[4].Format("%s", lpfilename);
  168. //file dest directory
  169. pListCtrl.SetItemText(nitem, 5, lpfilehdr->szInLocalFileName);
  170. plpsz[5].Format("%s", lpfilehdr->szInLocalFileName);
  171. //file recved time.
  172. CTime now = CTime::GetCurrentTime();
  173. _snprintf(lpfilename, sizeof(lpfilename), SYS_TIMEFORMAT, 
  174. now.GetYear(), now.GetMonth(), now.GetDay(), 
  175. now.GetHour(), now.GetMinute(), now.GetSecond());
  176. pListCtrl.SetItemText(nitem, 6, lpfilename);
  177. plpsz[6].Format("%s", lpfilename);
  178. pListCtrl.SetItemData(nitem, (DWORD)plpsz);
  179. return nitem;
  180. }
  181. void CFileListRecv::CleanupSelectedItems()
  182. {
  183. int index = 0;
  184. CListCtrl &pListCtrl = this->GetListCtrl();
  185. if(0 == pListCtrl.GetSelectedCount()) return ;
  186. POSITION pos = pListCtrl.GetFirstSelectedItemPosition();
  187. while(pos)
  188. {
  189. index = pListCtrl.GetNextSelectedItem(pos);
  190. CString *p = reinterpret_cast<CString*>(pListCtrl.GetItemData(index));
  191. if(NULL != p) delete []p;
  192. pListCtrl.DeleteItem(index);
  193. pos = pListCtrl.GetFirstSelectedItemPosition();
  194. }
  195. }
  196. void CFileListRecv::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
  197. {
  198. LPNMLISTVIEW pNMListView = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  199. *pResult = 0;
  200. //@1---if current state is in sending state then disable sort function.
  201. if(!this->m_bCanSort) return;
  202. //@2---sorting action
  203. SORT_BY sortBy = FILE_NAME;
  204. switch (pNMListView->iSubItem)
  205. {
  206. case 0:
  207. sortBy = FILE_NAME;
  208. break;
  209. case 1:
  210. sortBy = FILE_SIZE;
  211. break;
  212. case 2:
  213. sortBy = FILE_LEFTSIZE;
  214. break;
  215. case 3:
  216. sortBy = FILE_SRCADDR;
  217. break;
  218. case 4:
  219. sortBy = FILE_SRCDIR;
  220. break;
  221. case 5:
  222. sortBy = FILE_DESTDIR;
  223. break;
  224. case 6:
  225. sortBy = FILE_RECVTIME;
  226. break;
  227. default: ASSERT(false);
  228. /**/
  229. }
  230. if (m_sortedBy == sortBy)
  231. m_sortedBy= static_cast<SORT_BY>(-sortBy);
  232. else
  233. m_sortedBy = sortBy;
  234. //开始对列表进行排序
  235. this->GetListCtrl().SortItems(LVCompareProc, static_cast<DWORD>(this->m_sortedBy));
  236. }
  237. int CALLBACK CFileListRecv::LVCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  238. {
  239. bool    bComplementResult = false;
  240. int     result = 0;
  241. //如果是要求反向排序
  242. if (lParamSort < 0)
  243. {
  244. //那么下次应该正向排序(前提:用户对同一列点击两次)
  245. lParamSort = -lParamSort;
  246. bComplementResult = true;
  247. }
  248. else{
  249. bComplementResult = false;
  250. }
  251. SORT_BY sortedBy = static_cast<SORT_BY>(lParamSort);
  252. //获取当前需要排序的两个SubItem的Text串
  253. CString *plpsz1 = reinterpret_cast<CString *>(lParam1);
  254. CString *plpsz2 = reinterpret_cast<CString *>(lParam2);
  255. ASSERT(NULL != plpsz1);
  256. ASSERT(NULL != plpsz2);
  257. CString psz1 = plpsz1[sortedBy - 1],
  258. psz2 = plpsz2[sortedBy - 1];
  259. //FILE_NAME = 1, FILE_SIZE, FILE_SRCADDR, FILE_DESTDIR, FILE_RECVTIME
  260. switch (sortedBy)
  261. {
  262. case FILE_NAME:
  263. case FILE_SRCADDR:
  264. case FILE_SRCDIR:
  265. case FILE_DESTDIR:
  266. case FILE_RECVTIME:
  267. {
  268. result = psz1.CompareNoCase(psz2);
  269. break;
  270. }
  271. case FILE_SIZE:
  272. case FILE_LEFTSIZE:
  273. {
  274. int nSize1, nSize2;
  275. nSize1 = atoi(psz1.GetBuffer(0));
  276. nSize2 = atoi(psz2.GetBuffer(0));
  277. result = nSize1 - nSize2;
  278. break;
  279. }
  280. default:
  281. ASSERT(false);
  282. break;
  283. }
  284. if (bComplementResult) result = -result;
  285. return result;
  286. }
  287. void CFileListRecv::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  288. {
  289. #define CTRLED 0x8000 
  290. switch(nChar) {
  291. case 'A':
  292. {
  293. //select all items.
  294. SHORT ks = GetKeyState(VK_CONTROL);
  295. if(ks & CTRLED)
  296. {
  297. this->SelectAllItems();
  298. }
  299. }
  300. break;
  301. case VK_DELETE:
  302. {
  303. //delete file links.
  304. this->SendMessage(WM_COMMAND, MAKEWORD(ID_FLEOPER_DELLINK, 0), NULL);
  305. }
  306. break;
  307. }
  308. CListView::OnKeyDown(nChar, nRepCnt, nFlags);
  309. }
  310. void CFileListRecv::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
  311. {
  312. this->OnFileOperForOpen();
  313. *pResult = 0;
  314. }
  315. //////////////////////////////////////////////////////////////////////////
  316. void CFileListRecv::OnFileOperForOpen()
  317. {
  318. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  319. CString sfilename;
  320. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  321. for(; NULL != pos; )
  322. {
  323. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  324. sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  325. if(FileExists(sfilename.GetBuffer(0)))
  326. {
  327. ShellExecute(this->GetSafeHwnd(), "open", sfilename.GetBuffer(0), NULL, NULL, SW_SHOW);
  328. }
  329. }
  330. }
  331. void CFileListRecv::OnFileOperForCopy()
  332. {
  333. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  334. CXShell shell;
  335. CString sdestdir, sfilename;
  336. //////////////////////////////////////////////////////////////////////////
  337. //select dest directory.
  338. if(!shell.GetFolder(&sdestdir, "Select Destination Directory", this->GetSafeHwnd(), NULL, NULL))
  339. {
  340. //MSGBOX_ERROR(_LoadString(IDS_OPENSHELLFLODERFAILED).GetBuffer(0));
  341. return ;
  342. }
  343. //////////////////////////////////////////////////////////////////////////
  344. BOOL bdontaskme = FALSE;
  345. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  346. for(; NULL != pos; )
  347. {
  348. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  349. sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  350. if(FileExists(sfilename.GetBuffer(0)))
  351. {
  352. char extractname[_MAX_PATH];
  353. memset(extractname, 0x0, sizeof(extractname));
  354. ExtractFileName(sfilename.GetBuffer(0), extractname, sizeof(extractname));
  355. CString destfilename;
  356. destfilename.Format("%s\%s", sdestdir.GetBuffer(0), extractname);
  357. if(!bdontaskme && FileExists(destfilename.GetBuffer(0)))
  358. {
  359. CFileNameInputDlg dlg;
  360. dlg.m_sfilename = extractname;
  361. int nmodle = dlg.DoModal();
  362. if(nmodle == IDOK)
  363. {
  364. bdontaskme = dlg.m_bdontask;
  365. if(!bdontaskme)
  366. {
  367. destfilename.Format("%s\%s", sdestdir.GetBuffer(0), dlg.m_sfilename.GetBuffer(0));
  368. }
  369. }
  370. else return ;
  371. }
  372. CopyFile(sfilename.GetBuffer(0), destfilename.GetBuffer(0), FALSE);
  373. }
  374. }
  375. }
  376. void CFileListRecv::OnFileOperForMove()
  377. {
  378. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  379. //////////////////////////////////////////////////////////////////////////
  380. CXShell shell;
  381. CString sdestdir;
  382. //select dest directory.
  383. if(!shell.GetFolder(&sdestdir, "Select Destination Directory", this->GetSafeHwnd(), NULL, NULL))
  384. {
  385. //MSGBOX_ERROR(_LoadString(IDS_OPENSHELLFLODERFAILED).GetBuffer(0));
  386. return ;
  387. }
  388. //////////////////////////////////////////////////////////////////////////
  389. CString sfilename;
  390. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  391. for(; NULL != pos; )
  392. {
  393. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  394. sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  395. if(FileExists(sfilename.GetBuffer(0)))
  396. {
  397. char extractname[_MAX_PATH];
  398. CString sdestfile, strlog;
  399. memset(extractname, 0x0, sizeof(extractname));
  400. ExtractFileName(sfilename.GetBuffer(0), extractname, sizeof(extractname));
  401. sdestfile.Format("%s\%s", sdestdir.GetBuffer(0), extractname);
  402. strlog.Format(_LoadString(IDS_FOMOVEFILEFAIL).GetBuffer(0), sfilename.GetBuffer(0), sdestfile.GetBuffer(0));
  403. if(MoveFile(sfilename.GetBuffer(0), sdestfile.GetBuffer(0)))
  404. {
  405. //modify the db contents.
  406. if(ModifyFileNameFromIndex(nitem, sdestfile.GetBuffer(0)))
  407. {
  408. //update UI display.
  409. this->GetListCtrl().SetItemText(nitem, 5, sdestfile.GetBuffer(0));
  410. //record the system log.
  411. strlog.Format(_LoadString(IDS_FOMOVEFILEOK).GetBuffer(0), sfilename.GetBuffer(0), sdestfile.GetBuffer(0));
  412. }
  413. }
  414. //add log info.
  415. ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  416. }
  417. }
  418. }
  419. void CFileListRecv::OnFileOperForRename()
  420. {
  421. //get the selected file full name, if user selected multiple items 
  422. //but we only show the first selected file's propertys.
  423. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  424. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  425. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  426. CString sfilename;
  427. sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  428. char extractpath[_MAX_PATH], extractname[_MAX_PATH];
  429. memset(extractpath, 0x0, sizeof(extractpath));
  430. ExtractFilePath(sfilename.GetBuffer(0), extractpath, sizeof(extractpath));
  431. memset(extractname, 0x0, sizeof(extractname));
  432. ExtractFileName(sfilename.GetBuffer(0), extractname, sizeof(extractname));
  433. //////////////////////////////////////////////////////////////////////////
  434. CFileRenameDlg dlg;
  435. dlg.m_sfilename = extractname;
  436. if(dlg.DoModal() == IDCANCEL) return ;
  437. //////////////////////////////////////////////////////////////////////////
  438. CString newfilename;
  439. newfilename.Format("%s\%s", extractpath, dlg.m_sfilename.GetBuffer(0));
  440. //////////////////////////////////////////////////////////////////////////
  441. CString strlog;
  442. strlog.Format(_LoadString(IDS_FORENAMEFAIL).GetBuffer(0), sfilename.GetBuffer(0), newfilename.GetBuffer(0));
  443. if(MoveFile(sfilename.GetBuffer(0), newfilename.GetBuffer(0)))
  444. {
  445. strlog.Format(_LoadString(IDS_FOMOVEFILEOK).GetBuffer(0), sfilename.GetBuffer(0), newfilename.GetBuffer(0));
  446. if(this->ModifyFileNameFromIndex(nitem, newfilename.GetBuffer(0)))
  447. {
  448. //udpate UI display.
  449. this->GetListCtrl().SetItemText(nitem, 5, newfilename.GetBuffer(0));
  450. strlog.Format(_LoadString(IDS_FORENAMEOK).GetBuffer(0), sfilename.GetBuffer(0), newfilename.GetBuffer(0));
  451. }
  452. }
  453. ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  454. }
  455. void CFileListRecv::OnFileOperForDelete()
  456. {
  457. int index = 0;
  458. CString strlog;
  459. CListCtrl &pListCtrl = this->GetListCtrl();
  460. if(pListCtrl.GetSelectedCount() == 0) return ;
  461. //////////////////////////////////////////////////////////////////////////
  462. POSITION pos = pListCtrl.GetFirstSelectedItemPosition();
  463. while(pos)
  464. {
  465. index = pListCtrl.GetNextSelectedItem(pos);
  466. CString sfilename = this->GetListCtrl().GetItemText(index, 5);
  467. //first, delete file link from database.
  468. strlog.Format(_LoadString(IDS_FODELFILEFAIL).GetBuffer(0), sfilename.GetBuffer(0));
  469. if(this->DeleteFileLinkFromIndex(index))
  470. {
  471. //delete file from disk.
  472. if(DeleteFile(sfilename.GetBuffer(0)))
  473. {
  474. //record system log information.
  475. strlog.Format(_LoadString(IDS_FODELFILEOK).GetBuffer(0), sfilename.GetBuffer(0));
  476. }
  477. //delete item from list.
  478. CString *p = reinterpret_cast<CString*>(pListCtrl.GetItemData(index));
  479. if(NULL != p) delete []p;
  480. pListCtrl.DeleteItem(index);
  481. }
  482. else break;
  483. ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  484. //next item looping.
  485. pos = pListCtrl.GetFirstSelectedItemPosition();
  486. }
  487. }
  488. void CFileListRecv::OnFileOperForDelLink()
  489. {
  490. int index = 0;
  491. CString strlog;
  492. CListCtrl &pListCtrl = this->GetListCtrl();
  493. if(pListCtrl.GetSelectedCount() == 0) return ;
  494. //////////////////////////////////////////////////////////////////////////
  495. POSITION pos = pListCtrl.GetFirstSelectedItemPosition();
  496. while(pos)
  497. {
  498. index = pListCtrl.GetNextSelectedItem(pos);
  499. CString sfilename = pListCtrl.GetItemText(index, 5);
  500. //first, delete file link from database.
  501. strlog.Format(_LoadString(IDS_FODELFILELNKFAIL).GetBuffer(0), sfilename.GetBuffer(0));
  502. if(this->DeleteFileLinkFromIndex(index))
  503. {
  504. strlog.Format(_LoadString(IDS_FODELFILELNKOK).GetBuffer(0), sfilename.GetBuffer(0));
  505. }
  506. //update UI display.
  507. ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  508. //delete item from list.
  509. CString *p = reinterpret_cast<CString*>(pListCtrl.GetItemData(index));
  510. if(NULL != p) delete []p;
  511. pListCtrl.DeleteItem(index);
  512. pos = pListCtrl.GetFirstSelectedItemPosition();
  513. }
  514. }
  515. void CFileListRecv::OnFileOperForViewProp()
  516. {
  517. //get the selected file full name, if user selected multiple items 
  518. //but we only show the first selected file's propertys.
  519. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  520. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  521. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  522. CString sfilename;
  523. sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  524. //show property window.
  525. CXShell shell;
  526. shell.ShowFileProperties(sfilename.GetBuffer(0));
  527. }
  528. void CFileListRecv::OnFileOperForViewDestDir()
  529. {
  530. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  531. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  532. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  533. char lpath[_MAX_PATH];
  534. memset(lpath, 0x0, sizeof(lpath));
  535. ExtractFilePath(this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0), lpath, sizeof(lpath));
  536. ShellExecute(this->GetSafeHwnd(), "open", lpath, NULL, NULL, SW_SHOW);
  537. }
  538. void CFileListRecv::OnFileOperForResendFiles()
  539. {
  540. //get file full name.
  541. if(this->GetListCtrl().GetSelectedCount() == 0) return ;
  542. CString sfilename;
  543. CMainFrame *pmanfrm = (CMainFrame*)::AfxGetMainWnd();
  544. POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  545. for(; NULL != pos; )
  546. {
  547. int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
  548. sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  549. //add this file into send file list.
  550. CFileListSend *pSndFileList = pmanfrm->m_pwndWorkFrame->m_pwndFileTransView->m_pwndFileSendList;
  551. pSndFileList->AddFile(sfilename.GetBuffer(0));
  552. //delete the item from recved file list.
  553. CString *p = reinterpret_cast<CString*>(this->GetListCtrl().GetItemData(nitem));
  554. if(NULL != p) delete []p;
  555. this->GetListCtrl().DeleteItem(nitem);
  556. pos = this->GetListCtrl().GetFirstSelectedItemPosition();
  557. }
  558. }
  559. //insert one record into db contents.
  560. BOOL CFileListRecv::AddRecvRecord(const int nitem, const BOOL bsuccessful)
  561. {
  562. //////////////////////////////////////////////////////////////////////////
  563. CMainFrame *pmanfrm = (CMainFrame*)::AfxGetMainWnd();
  564. CFileListSend *pSndFileList = pmanfrm->m_pwndWorkFrame->m_pwndFileTransView->m_pwndFileSendList;
  565. //////////////////////////////////////////////////////////////////////////
  566. CGuiRecordSet rs;
  567. char sql[MAX_SQL_SIZE];
  568. try
  569. {
  570. int maxid = pSndFileList->GetMaxTransID();
  571. if(0 == maxid) return FALSE;
  572. int transtype = SQL::TRANSTYP_RECOR | (bsuccessful ? SQL::TRANSTYP_RECVOK : 0x0);
  573. CString srcfile, destfile, peeraddr, stranstime;
  574. srcfile.Format("%s\%s", 
  575. this->GetListCtrl().GetItemText(nitem, 4).GetBuffer(0),
  576. this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
  577. destfile = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  578. peeraddr = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
  579. stranstime = this->GetListCtrl().GetItemText(nitem, 6).GetBuffer(0);
  580. _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_ADDLOG, 
  581. maxid, destfile.GetBuffer(0), srcfile.GetBuffer(0), 
  582. peeraddr.GetBuffer(0), stranstime.GetBuffer(0), transtype);
  583. return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
  584. }
  585. catch (...) 
  586. {
  587. return FALSE;
  588. }
  589. }
  590. //display history recved or send file records.
  591. void CFileListRecv::AddHistoryFromRS(CGuiRecordSet &rs)
  592. {
  593. if(!rs.IsOpen()) return ;
  594. //////////////////////////////////////////////////////////////////////////
  595. this->SelectAllItems();
  596. this->CleanupSelectedItems();
  597. if(rs.IsEof()) return ;
  598. //////////////////////////////////////////////////////////////////////////
  599. NETFILEHDR lpfilehdr;
  600. CVariantEx varex;
  601. try
  602. {
  603. for(rs.MoveFirst(); !rs.IsEof(); rs.MoveNext())
  604. {
  605. memset(&lpfilehdr, 0x0, sizeof(lpfilehdr));
  606. rs.GetCollect("srcfilename", varex.get_variant_t());
  607. if(strchr(varex.AsString().c_str(), ':'))
  608. strncpy(lpfilehdr.szFileName, varex.AsString().c_str(), sizeof(lpfilehdr.szFileName));
  609. else
  610. {
  611. rs.GetCollect("filename", varex.get_variant_t());
  612. strncpy(lpfilehdr.szFileName, varex.AsString().c_str(), sizeof(lpfilehdr.szFileName));
  613. }
  614. rs.GetCollect("filename", varex.get_variant_t());
  615. strncpy(lpfilehdr.szInLocalFileName, varex.AsString().c_str(), sizeof(lpfilehdr.szInLocalFileName));
  616. rs.GetCollect("peername", varex.get_variant_t());
  617. sscanf(varex.AsString().c_str(), "%s / %s", lpfilehdr.szSenderHost, lpfilehdr.szSenderIp);
  618. HANDLE hfile = ::CreateFile(lpfilehdr.szInLocalFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  619. if(INVALID_HANDLE_VALUE != hfile)
  620. {
  621. DWORD dwhigh = 0;
  622. lpfilehdr.fiFileInfo.nFileSizeLow = GetFileSize(hfile, &dwhigh);
  623. ::CloseHandle(hfile);
  624. }
  625. this->AddFile(&lpfilehdr);
  626. //////////////////////////////////////////////////////////////////////////
  627. rs.GetCollect("transtime", varex.get_variant_t());
  628. int nitem = this->GetListCtrl().GetItemCount() - 1;
  629. this->GetListCtrl().SetItemText(nitem, 6, varex.AsString().c_str());
  630. }
  631. }
  632. catch(...) 
  633. {
  634. return ;
  635. }
  636. }
  637. //modify file name or move file into another directory.
  638. BOOL CFileListRecv::ModifyFileNameFromIndex(const int nitem, const char *psnewname)
  639. {
  640. CGuiRecordSet rs;
  641. char sql[MAX_SQL_SIZE];
  642. try
  643. {
  644. CString srcfile, destfile, peeraddr, stranstime;
  645. srcfile.Format("%s\%s", 
  646. this->GetListCtrl().GetItemText(nitem, 4).GetBuffer(0),
  647. this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
  648. destfile = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  649. peeraddr = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
  650. stranstime = this->GetListCtrl().GetItemText(nitem, 6).GetBuffer(0);
  651. _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_MODIFYLOG,
  652. psnewname, destfile.GetBuffer(0), srcfile.GetBuffer(0),
  653. peeraddr.GetBuffer(0), stranstime.GetBuffer(0));
  654. return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
  655. }
  656. catch (...) 
  657. {
  658. return FALSE;
  659. }
  660. }
  661. //delete file link from database.
  662. BOOL CFileListRecv::DeleteFileLinkFromIndex(const int nitem)
  663. {
  664. CGuiRecordSet rs;
  665. char sql[MAX_SQL_SIZE];
  666. try
  667. {
  668. CString srcfile, destfile, peeraddr, stranstime;
  669. srcfile.Format("%s\%s", 
  670. this->GetListCtrl().GetItemText(nitem, 4).GetBuffer(0),
  671. this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
  672. destfile = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
  673. peeraddr = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
  674. stranstime = this->GetListCtrl().GetItemText(nitem, 6).GetBuffer(0);
  675. _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_DELLOG, 
  676. destfile.GetBuffer(0), srcfile.GetBuffer(0), 
  677. peeraddr.GetBuffer(0), stranstime.GetBuffer(0));
  678. return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
  679. }
  680. catch (...) 
  681. {
  682. return FALSE;
  683. }
  684. }
  685. //////////////////////////////////////////////////////////////////////////
  686. /*
  687.  * support drag-drop operation from shell system.
  688.  */
  689. BOOL CFileListRecv::PrepareFileBuff(CDropFiles& DropFiles) const
  690. {
  691. CListCtrl& mylist = GetListCtrl();
  692. POSITION pos = mylist.GetFirstSelectedItemPosition();
  693. if( pos == NULL)
  694. {
  695. return FALSE;
  696. }
  697. while (pos)
  698. {
  699. int index = mylist.GetNextSelectedItem(pos);
  700. CString sfilename = mylist.GetItemText(index, 5);
  701. if(FileExists(sfilename.GetBuffer(0)))
  702. {
  703. DropFiles.AddFile(sfilename);
  704. }
  705. }
  706. DropFiles.CreateBuffer();
  707. return TRUE;
  708. }
  709. void CFileListRecv::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
  710. {
  711. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  712. CDropFiles DropFiles;
  713. if(!PrepareFileBuff(DropFiles)) return ;
  714. COleDropSource DropSource;
  715. COleDataSource DropData;
  716. HGLOBAL hMem = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE, DropFiles.GetBuffSize());
  717. memcpy( (char*)::GlobalLock(hMem), DropFiles.GetBuffer(), DropFiles.GetBuffSize() );
  718. ::GlobalUnlock(hMem);
  719. DropData.CacheGlobalData( CF_HDROP, hMem );
  720. DROPEFFECT de = DropData.DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE,NULL);
  721. if(de == DROPEFFECT_COPY){
  722. // Copy files if CTRL btn is hold;
  723. }
  724. else{
  725. // Move files, as default;
  726. }
  727. *pResult = 0;
  728. }
  729. DROPEFFECT CFileListRecv::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
  730. {
  731. if( ! pDataObject->IsDataAvailable( CF_HDROP ) ){
  732. return DROPEFFECT_NONE;
  733. }
  734. DROPEFFECT de = DROPEFFECT_COPY;
  735. /*
  736. if(dwKeyState & MK_CONTROL)
  737. {
  738. de = DROPEFFECT_MOVE;
  739. }
  740. else
  741. {
  742. de = DROPEFFECT_COPY;
  743. }
  744. */
  745. return de;
  746. }