FileListRecv.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:26k
- #include "stdafx.h"
- #include "xshell.h"
- #include "mainfrm.h"
- #include "trfAgent.h"
- #include "variantex.h"
- #include "FileListRecv.h"
- #include "FileListSend.h"
- #include "FileRenameDlg.h"
- #include "FileNameInputDlg.h"
- //文件接收和发送列表的列头数
- static const int G_FILELISTVIEW_COL_COUNTS = 7;
- IMPLEMENT_DYNCREATE(CFileListRecv, CListView)
- CFileListRecv::CFileListRecv()
- {
- this->m_sortedBy = FILE_NAME;
- this->m_bCanSort = TRUE;
- this->m_nRecvRefCnt = 0;
- }
- CFileListRecv::~CFileListRecv(){}
- BEGIN_MESSAGE_MAP(CFileListRecv, CListView)
- //{{AFX_MSG_MAP(CFileListRecv)
- ON_WM_CONTEXTMENU()
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_KEYDOWN()
- ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
- ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBeginDrag)
- ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
- //}}AFX_MSG_MAP
- ON_COMMAND(ID_FLEOPER_OPEN, OnFileOperForOpen)
- ON_COMMAND(ID_FLEOPER_COPY, OnFileOperForCopy)
- ON_COMMAND(ID_FLEOPER_MOVEFILE, OnFileOperForMove)
- ON_COMMAND(ID_FLEOPER_RENAME, OnFileOperForRename)
- ON_COMMAND(ID_FLEOPER_DELFILE, OnFileOperForDelete)
- ON_COMMAND(ID_FLEOPER_DELLINK, OnFileOperForDelLink)
- ON_COMMAND(ID_FLEOPER_VIEWFLEPROP, OnFileOperForViewProp)
- ON_COMMAND(ID_FLEOPER_TODESTDIR, OnFileOperForViewDestDir)
- ON_COMMAND(ID_FLEOPER_RESENDFLE, OnFileOperForResendFiles)
- END_MESSAGE_MAP()
- void CFileListRecv::OnDraw(CDC* pDC)
- {
- CDocument* pDoc = GetDocument();
- }
- #ifdef _DEBUG
- void CFileListRecv::AssertValid() const
- {
- CListView::AssertValid();
- }
- void CFileListRecv::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
- #endif //_DEBUG
- int CFileListRecv::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- lpCreateStruct->style |= (LVS_REPORT | LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS);
- if (CListView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- //初始化ListView的显示风格
- this->GetListCtrl().SetExtendedStyle(LVS_EX_FULLROWSELECT);
-
- //设置EDIT的字体
- this->SetFont(CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT)));
-
- //设置ImageList
- if(!m_imgView.Create(IDB_OUTPUTSPACE, 16, 3, RGB (255, 0, 0)))
- {
- TRACE0("Failed to create the m_imgViewn");
- return -1;
- }
- this->GetListCtrl().SetImageList(&m_imgView, LVSIL_SMALL);
-
- //初始化ListView的列头
- CString strColCap = _T("");
-
- //列头的宽度和风格
- static const struct
- {
- UINT nColHdrId;
- int nFormat;
- int nWidth;
- }colRecvData[] = {
- {IDS_LIST_FILENAME, LVCFMT_LEFT, 120},
- {IDS_LIST_FILESIZE, LVCFMT_RIGHT, 100},
- {IDS_LIST_LEFTFILESIZE, LVCFMT_RIGHT, 100},
- {IDS_LIST_SRCADDR, LVCFMT_LEFT, 120},
- {IDS_LIST_SRCDIR, LVCFMT_LEFT, 150},
- {IDS_LIST_DESTDIR, LVCFMT_LEFT, 100},
- {IDS_LIST_RECVTIME, LVCFMT_LEFT, 100}
- };
-
- for(int i = 0; i < G_FILELISTVIEW_COL_COUNTS; ++i){
- VERIFY(strColCap.LoadString(colRecvData[i].nColHdrId));
- this->GetListCtrl().InsertColumn(i, LPCTSTR(strColCap), colRecvData[i].nFormat, colRecvData[i].nWidth);
- }
-
- //初始化时以文件名来对列表数据进行排序
- this->GetListCtrl().SortItems(LVCompareProc, static_cast<DWORD>(this->m_sortedBy));
- if(!m_popmenu.LoadMenu(IDR_FILEOPERS))
- {
- MSGBOX_ERROR(_LoadString(IDS_LOADRESFAILED).GetBuffer(0));
- return -1;
- }
- return 0;
- }
- void CFileListRecv::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
- VERIFY(m_DropTarget.Register(this));
- }
- void CFileListRecv::OnDestroy()
- {
- //dispose all items' data.
-
- this->SelectAllItems();
- this->CleanupSelectedItems();
-
- CListView::OnDestroy();
- }
- void CFileListRecv::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- if(m_nRecvRefCnt > 0 || NULL == m_popmenu.GetSafeHmenu()) return ;
- CMenu *psubmenu = m_popmenu.GetSubMenu(0);
- if(NULL == psubmenu) return ;
- psubmenu->TrackPopupMenu(TPM_RIGHTBUTTON, point.x, point.y, pWnd);
- }
- void CFileListRecv::SelectAllItems()
- {
- int index = 0;
- CListCtrl &pListCtrl = this->GetListCtrl();
-
- for(index = 0; index < pListCtrl.GetItemCount(); ++index)
- {
- ListView_SetItemState (this->GetSafeHwnd(),
- index,
- LVIS_FOCUSED | LVIS_SELECTED,
- 0x000F);
- }
- }
- void CFileListRecv::EnableSort(const BOOL bCanSort)
- {
- this->m_bCanSort = bCanSort;
- }
- //return the item index.
- int CFileListRecv::AddFile(LPNETFILEHDR lpfilehdr)
- {
- assert(NULL != lpfilehdr);
-
- CListCtrl &pListCtrl = this->GetListCtrl();
- int nitem = pListCtrl.GetItemCount();
- CString *plpsz = new CString[G_FILELISTVIEW_COL_COUNTS];
- //file name
- char lpfilename[_MAX_PATH];
- memset(lpfilename, 0x0, sizeof(lpfilename));
- ExtractFileName(lpfilehdr->szFileName, lpfilename, sizeof(lpfilename));
- nitem = pListCtrl.InsertItem(nitem, lpfilename, 6);
- plpsz[0].Format("%s", lpfilename);
- //file size
- char lpfilesize[32+1];
- memset(lpfilesize, 0x0, sizeof(lpfilesize));
- DWORD dwsize = lpfilehdr->fiFileInfo.nFileSizeLow;
- _snprintf(lpfilesize, 32, "%d", dwsize);
- pListCtrl.SetItemText(nitem, 1, lpfilesize);
- plpsz[1].Format("%s", lpfilesize);
- //file left size
- _snprintf(lpfilesize, 32, "%d", 0);
- pListCtrl.SetItemText(nitem, 2, lpfilesize);
- plpsz[2].Format("%s", lpfilesize);
- //file source address
- _snprintf(lpfilename, sizeof(lpfilename), "%s / %s", lpfilehdr->szSenderHost, lpfilehdr->szSenderIp);
- pListCtrl.SetItemText(nitem, 3, lpfilename);
- plpsz[3].Format("%s", lpfilename);
- //file source directory
- memset(lpfilename, 0x0, sizeof(lpfilename));
- ExtractFilePath(lpfilehdr->szFileName, lpfilename, sizeof(lpfilename));
- pListCtrl.SetItemText(nitem, 4, lpfilename);
- plpsz[4].Format("%s", lpfilename);
- //file dest directory
- pListCtrl.SetItemText(nitem, 5, lpfilehdr->szInLocalFileName);
- plpsz[5].Format("%s", lpfilehdr->szInLocalFileName);
- //file recved time.
-
- CTime now = CTime::GetCurrentTime();
- _snprintf(lpfilename, sizeof(lpfilename), SYS_TIMEFORMAT,
- now.GetYear(), now.GetMonth(), now.GetDay(),
- now.GetHour(), now.GetMinute(), now.GetSecond());
- pListCtrl.SetItemText(nitem, 6, lpfilename);
- plpsz[6].Format("%s", lpfilename);
- pListCtrl.SetItemData(nitem, (DWORD)plpsz);
- return nitem;
- }
- void CFileListRecv::CleanupSelectedItems()
- {
- int index = 0;
- CListCtrl &pListCtrl = this->GetListCtrl();
-
- if(0 == pListCtrl.GetSelectedCount()) return ;
- POSITION pos = pListCtrl.GetFirstSelectedItemPosition();
-
- while(pos)
- {
- index = pListCtrl.GetNextSelectedItem(pos);
-
- CString *p = reinterpret_cast<CString*>(pListCtrl.GetItemData(index));
- if(NULL != p) delete []p;
- pListCtrl.DeleteItem(index);
- pos = pListCtrl.GetFirstSelectedItemPosition();
- }
- }
- void CFileListRecv::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMLISTVIEW pNMListView = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
-
- *pResult = 0;
-
- //@1---if current state is in sending state then disable sort function.
-
- if(!this->m_bCanSort) return;
-
- //@2---sorting action
-
- SORT_BY sortBy = FILE_NAME;
-
- switch (pNMListView->iSubItem)
- {
- case 0:
- sortBy = FILE_NAME;
- break;
-
- case 1:
- sortBy = FILE_SIZE;
- break;
-
- case 2:
- sortBy = FILE_LEFTSIZE;
- break;
- case 3:
- sortBy = FILE_SRCADDR;
- break;
-
- case 4:
- sortBy = FILE_SRCDIR;
- break;
- case 5:
- sortBy = FILE_DESTDIR;
- break;
- case 6:
- sortBy = FILE_RECVTIME;
- break;
-
- default: ASSERT(false);
- /**/
- }
-
- if (m_sortedBy == sortBy)
- m_sortedBy= static_cast<SORT_BY>(-sortBy);
- else
- m_sortedBy = sortBy;
-
- //开始对列表进行排序
- this->GetListCtrl().SortItems(LVCompareProc, static_cast<DWORD>(this->m_sortedBy));
- }
- int CALLBACK CFileListRecv::LVCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
- {
- bool bComplementResult = false;
- int result = 0;
-
- //如果是要求反向排序
- if (lParamSort < 0)
- {
- //那么下次应该正向排序(前提:用户对同一列点击两次)
- lParamSort = -lParamSort;
- bComplementResult = true;
- }
- else{
- bComplementResult = false;
- }
- SORT_BY sortedBy = static_cast<SORT_BY>(lParamSort);
-
- //获取当前需要排序的两个SubItem的Text串
- CString *plpsz1 = reinterpret_cast<CString *>(lParam1);
- CString *plpsz2 = reinterpret_cast<CString *>(lParam2);
-
- ASSERT(NULL != plpsz1);
- ASSERT(NULL != plpsz2);
-
- CString psz1 = plpsz1[sortedBy - 1],
- psz2 = plpsz2[sortedBy - 1];
-
- //FILE_NAME = 1, FILE_SIZE, FILE_SRCADDR, FILE_DESTDIR, FILE_RECVTIME
- switch (sortedBy)
- {
- case FILE_NAME:
- case FILE_SRCADDR:
- case FILE_SRCDIR:
- case FILE_DESTDIR:
- case FILE_RECVTIME:
- {
- result = psz1.CompareNoCase(psz2);
- break;
- }
- case FILE_SIZE:
- case FILE_LEFTSIZE:
- {
- int nSize1, nSize2;
- nSize1 = atoi(psz1.GetBuffer(0));
- nSize2 = atoi(psz2.GetBuffer(0));
-
- result = nSize1 - nSize2;
- break;
- }
-
- default:
- ASSERT(false);
- break;
- }
- if (bComplementResult) result = -result;
-
- return result;
- }
- void CFileListRecv::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- #define CTRLED 0x8000
-
- switch(nChar) {
- case 'A':
- {
- //select all items.
- SHORT ks = GetKeyState(VK_CONTROL);
- if(ks & CTRLED)
- {
- this->SelectAllItems();
- }
- }
- break;
- case VK_DELETE:
- {
- //delete file links.
- this->SendMessage(WM_COMMAND, MAKEWORD(ID_FLEOPER_DELLINK, 0), NULL);
- }
- break;
- }
- CListView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CFileListRecv::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
- {
- this->OnFileOperForOpen();
-
- *pResult = 0;
- }
- //////////////////////////////////////////////////////////////////////////
- void CFileListRecv::OnFileOperForOpen()
- {
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
- CString sfilename;
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
-
- for(; NULL != pos; )
- {
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
- sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
- if(FileExists(sfilename.GetBuffer(0)))
- {
- ShellExecute(this->GetSafeHwnd(), "open", sfilename.GetBuffer(0), NULL, NULL, SW_SHOW);
- }
- }
- }
- void CFileListRecv::OnFileOperForCopy()
- {
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
- CXShell shell;
- CString sdestdir, sfilename;
-
- //////////////////////////////////////////////////////////////////////////
- //select dest directory.
- if(!shell.GetFolder(&sdestdir, "Select Destination Directory", this->GetSafeHwnd(), NULL, NULL))
- {
- //MSGBOX_ERROR(_LoadString(IDS_OPENSHELLFLODERFAILED).GetBuffer(0));
- return ;
- }
- //////////////////////////////////////////////////////////////////////////
- BOOL bdontaskme = FALSE;
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
- for(; NULL != pos; )
- {
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
- sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
-
- if(FileExists(sfilename.GetBuffer(0)))
- {
- char extractname[_MAX_PATH];
- memset(extractname, 0x0, sizeof(extractname));
- ExtractFileName(sfilename.GetBuffer(0), extractname, sizeof(extractname));
- CString destfilename;
- destfilename.Format("%s\%s", sdestdir.GetBuffer(0), extractname);
- if(!bdontaskme && FileExists(destfilename.GetBuffer(0)))
- {
- CFileNameInputDlg dlg;
- dlg.m_sfilename = extractname;
- int nmodle = dlg.DoModal();
- if(nmodle == IDOK)
- {
- bdontaskme = dlg.m_bdontask;
- if(!bdontaskme)
- {
- destfilename.Format("%s\%s", sdestdir.GetBuffer(0), dlg.m_sfilename.GetBuffer(0));
- }
- }
- else return ;
- }
- CopyFile(sfilename.GetBuffer(0), destfilename.GetBuffer(0), FALSE);
- }
- }
- }
- void CFileListRecv::OnFileOperForMove()
- {
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
- //////////////////////////////////////////////////////////////////////////
- CXShell shell;
- CString sdestdir;
-
- //select dest directory.
-
- if(!shell.GetFolder(&sdestdir, "Select Destination Directory", this->GetSafeHwnd(), NULL, NULL))
- {
- //MSGBOX_ERROR(_LoadString(IDS_OPENSHELLFLODERFAILED).GetBuffer(0));
- return ;
- }
- //////////////////////////////////////////////////////////////////////////
- CString sfilename;
-
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
-
- for(; NULL != pos; )
- {
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
- sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
-
- if(FileExists(sfilename.GetBuffer(0)))
- {
- char extractname[_MAX_PATH];
- CString sdestfile, strlog;
- memset(extractname, 0x0, sizeof(extractname));
- ExtractFileName(sfilename.GetBuffer(0), extractname, sizeof(extractname));
-
- sdestfile.Format("%s\%s", sdestdir.GetBuffer(0), extractname);
- strlog.Format(_LoadString(IDS_FOMOVEFILEFAIL).GetBuffer(0), sfilename.GetBuffer(0), sdestfile.GetBuffer(0));
- if(MoveFile(sfilename.GetBuffer(0), sdestfile.GetBuffer(0)))
- {
- //modify the db contents.
- if(ModifyFileNameFromIndex(nitem, sdestfile.GetBuffer(0)))
- {
- //update UI display.
- this->GetListCtrl().SetItemText(nitem, 5, sdestfile.GetBuffer(0));
- //record the system log.
- strlog.Format(_LoadString(IDS_FOMOVEFILEOK).GetBuffer(0), sfilename.GetBuffer(0), sdestfile.GetBuffer(0));
- }
- }
- //add log info.
- ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- }
- }
- }
- void CFileListRecv::OnFileOperForRename()
- {
- //get the selected file full name, if user selected multiple items
- //but we only show the first selected file's propertys.
-
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
-
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
-
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
-
- CString sfilename;
- sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
- char extractpath[_MAX_PATH], extractname[_MAX_PATH];
- memset(extractpath, 0x0, sizeof(extractpath));
- ExtractFilePath(sfilename.GetBuffer(0), extractpath, sizeof(extractpath));
- memset(extractname, 0x0, sizeof(extractname));
- ExtractFileName(sfilename.GetBuffer(0), extractname, sizeof(extractname));
- //////////////////////////////////////////////////////////////////////////
- CFileRenameDlg dlg;
- dlg.m_sfilename = extractname;
- if(dlg.DoModal() == IDCANCEL) return ;
-
- //////////////////////////////////////////////////////////////////////////
-
- CString newfilename;
- newfilename.Format("%s\%s", extractpath, dlg.m_sfilename.GetBuffer(0));
- //////////////////////////////////////////////////////////////////////////
-
- CString strlog;
- strlog.Format(_LoadString(IDS_FORENAMEFAIL).GetBuffer(0), sfilename.GetBuffer(0), newfilename.GetBuffer(0));
- if(MoveFile(sfilename.GetBuffer(0), newfilename.GetBuffer(0)))
- {
- strlog.Format(_LoadString(IDS_FOMOVEFILEOK).GetBuffer(0), sfilename.GetBuffer(0), newfilename.GetBuffer(0));
-
- if(this->ModifyFileNameFromIndex(nitem, newfilename.GetBuffer(0)))
- {
- //udpate UI display.
- this->GetListCtrl().SetItemText(nitem, 5, newfilename.GetBuffer(0));
- strlog.Format(_LoadString(IDS_FORENAMEOK).GetBuffer(0), sfilename.GetBuffer(0), newfilename.GetBuffer(0));
- }
- }
- ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- }
- void CFileListRecv::OnFileOperForDelete()
- {
- int index = 0;
- CString strlog;
- CListCtrl &pListCtrl = this->GetListCtrl();
-
- if(pListCtrl.GetSelectedCount() == 0) return ;
- //////////////////////////////////////////////////////////////////////////
-
- POSITION pos = pListCtrl.GetFirstSelectedItemPosition();
-
- while(pos)
- {
- index = pListCtrl.GetNextSelectedItem(pos);
- CString sfilename = this->GetListCtrl().GetItemText(index, 5);
- //first, delete file link from database.
-
- strlog.Format(_LoadString(IDS_FODELFILEFAIL).GetBuffer(0), sfilename.GetBuffer(0));
- if(this->DeleteFileLinkFromIndex(index))
- {
- //delete file from disk.
- if(DeleteFile(sfilename.GetBuffer(0)))
- {
- //record system log information.
- strlog.Format(_LoadString(IDS_FODELFILEOK).GetBuffer(0), sfilename.GetBuffer(0));
- }
- //delete item from list.
-
- CString *p = reinterpret_cast<CString*>(pListCtrl.GetItemData(index));
- if(NULL != p) delete []p;
-
- pListCtrl.DeleteItem(index);
- }
- else break;
- ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- //next item looping.
- pos = pListCtrl.GetFirstSelectedItemPosition();
- }
- }
- void CFileListRecv::OnFileOperForDelLink()
- {
- int index = 0;
- CString strlog;
- CListCtrl &pListCtrl = this->GetListCtrl();
-
- if(pListCtrl.GetSelectedCount() == 0) return ;
-
- //////////////////////////////////////////////////////////////////////////
-
- POSITION pos = pListCtrl.GetFirstSelectedItemPosition();
-
- while(pos)
- {
- index = pListCtrl.GetNextSelectedItem(pos);
- CString sfilename = pListCtrl.GetItemText(index, 5);
- //first, delete file link from database.
-
- strlog.Format(_LoadString(IDS_FODELFILELNKFAIL).GetBuffer(0), sfilename.GetBuffer(0));
- if(this->DeleteFileLinkFromIndex(index))
- {
- strlog.Format(_LoadString(IDS_FODELFILELNKOK).GetBuffer(0), sfilename.GetBuffer(0));
- }
- //update UI display.
- ::AfxGetMainWnd()->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- //delete item from list.
-
- CString *p = reinterpret_cast<CString*>(pListCtrl.GetItemData(index));
- if(NULL != p) delete []p;
- pListCtrl.DeleteItem(index);
-
- pos = pListCtrl.GetFirstSelectedItemPosition();
- }
- }
- void CFileListRecv::OnFileOperForViewProp()
- {
- //get the selected file full name, if user selected multiple items
- //but we only show the first selected file's propertys.
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
-
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
-
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
- CString sfilename;
- sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
- //show property window.
- CXShell shell;
- shell.ShowFileProperties(sfilename.GetBuffer(0));
- }
- void CFileListRecv::OnFileOperForViewDestDir()
- {
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
-
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
-
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
- char lpath[_MAX_PATH];
- memset(lpath, 0x0, sizeof(lpath));
- ExtractFilePath(this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0), lpath, sizeof(lpath));
- ShellExecute(this->GetSafeHwnd(), "open", lpath, NULL, NULL, SW_SHOW);
- }
- void CFileListRecv::OnFileOperForResendFiles()
- {
- //get file full name.
-
- if(this->GetListCtrl().GetSelectedCount() == 0) return ;
-
- CString sfilename;
- CMainFrame *pmanfrm = (CMainFrame*)::AfxGetMainWnd();
- POSITION pos = this->GetListCtrl().GetFirstSelectedItemPosition();
- for(; NULL != pos; )
- {
- int nitem = this->GetListCtrl().GetNextSelectedItem(pos);
-
- sfilename = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
-
- //add this file into send file list.
-
- CFileListSend *pSndFileList = pmanfrm->m_pwndWorkFrame->m_pwndFileTransView->m_pwndFileSendList;
- pSndFileList->AddFile(sfilename.GetBuffer(0));
- //delete the item from recved file list.
- CString *p = reinterpret_cast<CString*>(this->GetListCtrl().GetItemData(nitem));
- if(NULL != p) delete []p;
- this->GetListCtrl().DeleteItem(nitem);
- pos = this->GetListCtrl().GetFirstSelectedItemPosition();
- }
- }
- //insert one record into db contents.
- BOOL CFileListRecv::AddRecvRecord(const int nitem, const BOOL bsuccessful)
- {
- //////////////////////////////////////////////////////////////////////////
-
- CMainFrame *pmanfrm = (CMainFrame*)::AfxGetMainWnd();
- CFileListSend *pSndFileList = pmanfrm->m_pwndWorkFrame->m_pwndFileTransView->m_pwndFileSendList;
- //////////////////////////////////////////////////////////////////////////
-
- CGuiRecordSet rs;
- char sql[MAX_SQL_SIZE];
-
- try
- {
- int maxid = pSndFileList->GetMaxTransID();
- if(0 == maxid) return FALSE;
-
- int transtype = SQL::TRANSTYP_RECOR | (bsuccessful ? SQL::TRANSTYP_RECVOK : 0x0);
-
- CString srcfile, destfile, peeraddr, stranstime;
- srcfile.Format("%s\%s",
- this->GetListCtrl().GetItemText(nitem, 4).GetBuffer(0),
- this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
- destfile = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
- peeraddr = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
- stranstime = this->GetListCtrl().GetItemText(nitem, 6).GetBuffer(0);
-
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_ADDLOG,
- maxid, destfile.GetBuffer(0), srcfile.GetBuffer(0),
- peeraddr.GetBuffer(0), stranstime.GetBuffer(0), transtype);
-
- return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
- }
- catch (...)
- {
- return FALSE;
- }
- }
- //display history recved or send file records.
- void CFileListRecv::AddHistoryFromRS(CGuiRecordSet &rs)
- {
- if(!rs.IsOpen()) return ;
- //////////////////////////////////////////////////////////////////////////
-
- this->SelectAllItems();
- this->CleanupSelectedItems();
- if(rs.IsEof()) return ;
- //////////////////////////////////////////////////////////////////////////
-
- NETFILEHDR lpfilehdr;
- CVariantEx varex;
-
- try
- {
- for(rs.MoveFirst(); !rs.IsEof(); rs.MoveNext())
- {
- memset(&lpfilehdr, 0x0, sizeof(lpfilehdr));
- rs.GetCollect("srcfilename", varex.get_variant_t());
- if(strchr(varex.AsString().c_str(), ':'))
- strncpy(lpfilehdr.szFileName, varex.AsString().c_str(), sizeof(lpfilehdr.szFileName));
- else
- {
- rs.GetCollect("filename", varex.get_variant_t());
- strncpy(lpfilehdr.szFileName, varex.AsString().c_str(), sizeof(lpfilehdr.szFileName));
- }
- rs.GetCollect("filename", varex.get_variant_t());
- strncpy(lpfilehdr.szInLocalFileName, varex.AsString().c_str(), sizeof(lpfilehdr.szInLocalFileName));
- rs.GetCollect("peername", varex.get_variant_t());
- sscanf(varex.AsString().c_str(), "%s / %s", lpfilehdr.szSenderHost, lpfilehdr.szSenderIp);
- HANDLE hfile = ::CreateFile(lpfilehdr.szInLocalFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(INVALID_HANDLE_VALUE != hfile)
- {
- DWORD dwhigh = 0;
- lpfilehdr.fiFileInfo.nFileSizeLow = GetFileSize(hfile, &dwhigh);
- ::CloseHandle(hfile);
- }
- this->AddFile(&lpfilehdr);
- //////////////////////////////////////////////////////////////////////////
- rs.GetCollect("transtime", varex.get_variant_t());
- int nitem = this->GetListCtrl().GetItemCount() - 1;
- this->GetListCtrl().SetItemText(nitem, 6, varex.AsString().c_str());
- }
- }
- catch(...)
- {
- return ;
- }
- }
- //modify file name or move file into another directory.
- BOOL CFileListRecv::ModifyFileNameFromIndex(const int nitem, const char *psnewname)
- {
- CGuiRecordSet rs;
- char sql[MAX_SQL_SIZE];
-
- try
- {
- CString srcfile, destfile, peeraddr, stranstime;
-
- srcfile.Format("%s\%s",
- this->GetListCtrl().GetItemText(nitem, 4).GetBuffer(0),
- this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
-
- destfile = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
- peeraddr = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
- stranstime = this->GetListCtrl().GetItemText(nitem, 6).GetBuffer(0);
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_MODIFYLOG,
- psnewname, destfile.GetBuffer(0), srcfile.GetBuffer(0),
- peeraddr.GetBuffer(0), stranstime.GetBuffer(0));
-
- return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
- }
- catch (...)
- {
- return FALSE;
- }
- }
- //delete file link from database.
- BOOL CFileListRecv::DeleteFileLinkFromIndex(const int nitem)
- {
- CGuiRecordSet rs;
- char sql[MAX_SQL_SIZE];
-
- try
- {
- CString srcfile, destfile, peeraddr, stranstime;
-
- srcfile.Format("%s\%s",
- this->GetListCtrl().GetItemText(nitem, 4).GetBuffer(0),
- this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
-
- destfile = this->GetListCtrl().GetItemText(nitem, 5).GetBuffer(0);
- peeraddr = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
- stranstime = this->GetListCtrl().GetItemText(nitem, 6).GetBuffer(0);
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_DELLOG,
- destfile.GetBuffer(0), srcfile.GetBuffer(0),
- peeraddr.GetBuffer(0), stranstime.GetBuffer(0));
- return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
- }
- catch (...)
- {
- return FALSE;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- /*
- * support drag-drop operation from shell system.
- */
- BOOL CFileListRecv::PrepareFileBuff(CDropFiles& DropFiles) const
- {
- CListCtrl& mylist = GetListCtrl();
- POSITION pos = mylist.GetFirstSelectedItemPosition();
-
- if( pos == NULL)
- {
- return FALSE;
- }
- while (pos)
- {
- int index = mylist.GetNextSelectedItem(pos);
- CString sfilename = mylist.GetItemText(index, 5);
- if(FileExists(sfilename.GetBuffer(0)))
- {
- DropFiles.AddFile(sfilename);
- }
- }
- DropFiles.CreateBuffer();
- return TRUE;
- }
- void CFileListRecv::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- CDropFiles DropFiles;
-
- if(!PrepareFileBuff(DropFiles)) return ;
-
- COleDropSource DropSource;
- COleDataSource DropData;
-
- HGLOBAL hMem = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE, DropFiles.GetBuffSize());
- memcpy( (char*)::GlobalLock(hMem), DropFiles.GetBuffer(), DropFiles.GetBuffSize() );
- ::GlobalUnlock(hMem);
-
- DropData.CacheGlobalData( CF_HDROP, hMem );
- DROPEFFECT de = DropData.DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE,NULL);
-
- if(de == DROPEFFECT_COPY){
- // Copy files if CTRL btn is hold;
- }
- else{
- // Move files, as default;
- }
- *pResult = 0;
- }
- DROPEFFECT CFileListRecv::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
- {
- if( ! pDataObject->IsDataAvailable( CF_HDROP ) ){
- return DROPEFFECT_NONE;
- }
-
- DROPEFFECT de = DROPEFFECT_COPY;
- /*
- if(dwKeyState & MK_CONTROL)
- {
- de = DROPEFFECT_MOVE;
- }
- else
- {
- de = DROPEFFECT_COPY;
- }
- */
- return de;
- }