FilelistSend.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:13k
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "FilelistSend.h"
- #include "variantex.h"
- //文件接收和发送列表的列头数
- static const int G_FILELISTVIEW_COL_COUNTS = 4;
- IMPLEMENT_DYNCREATE(CFileListSend, CListView)
- CFileListSend::CFileListSend()
- {
- this->m_bSendingBusy = FALSE;
- this->m_sortedBy = FILE_NAME;
- this->m_bCanSort = TRUE;
- }
- CFileListSend::~CFileListSend(){}
- BEGIN_MESSAGE_MAP(CFileListSend, CListView)
- //{{AFX_MSG_MAP(CFileListSend)
- ON_WM_CREATE()
- ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
- ON_WM_KEYDOWN()
- ON_WM_DESTROY()
- ON_WM_SETCURSOR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CFileListSend::OnDraw(CDC* pDC)
- {
- CDocument* pDoc = GetDocument();
- }
- #ifdef _DEBUG
- void CFileListSend::AssertValid() const
- {
- CListView::AssertValid();
- }
- void CFileListSend::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
- #endif //_DEBUG
- void CFileListSend::AddFile(const char *lpszFilename, const int index)
- {
- ASSERT(NULL != lpszFilename);
- if(!FileExists(lpszFilename)) return ;
- CListCtrl &pListCtrl = this->GetListCtrl();
-
- //连续获取所有选择的文件信息
- CString strItem;
-
- CString *plpsz = new CString[G_FILELISTVIEW_COL_COUNTS];
-
- //打开该文件
- CFile file(lpszFilename, CFile::modeRead);
-
- //文件名
- int nLastIndex = (index == -1) ? pListCtrl.GetItemCount() : index;
- pListCtrl.InsertItem(nLastIndex, file.GetFileName(), 7);
- plpsz[0].Format("%s", file.GetFileName());
-
- //文件大小
- strItem.Empty();
- strItem.Format("%d", file.GetLength());
- pListCtrl.SetItemText(nLastIndex, 1, strItem);
- plpsz[1].Format("%s", strItem.GetBuffer(0));
-
- //文件所在目录
- strItem.Empty();
- strItem = file.GetFilePath();
- strItem = strItem.Left(strItem.ReverseFind('\'));
- pListCtrl.SetItemText(nLastIndex, 2, strItem);
- plpsz[2].Format("%s", strItem.GetBuffer(0));
-
- //发送文件时间
- strItem.Empty();
- pListCtrl.SetItemText(nLastIndex, 3, strItem.GetBuffer(0));
- plpsz[3].Format("%s", strItem.GetBuffer(0));
-
- pListCtrl.SetItemData(nLastIndex, (DWORD)plpsz);
-
- //关闭文件
- file.Close();
- }
- void CFileListSend::AddFiles(void)
- {
- LPCTSTR lpszDefExt = _T(""),
- lpszFilter = _T("Word文档(*.doc)|*.doc|文本文档(*.txt)|*.txt|执行代码(*.exe;*.dll;*.ocx)|*.exe;*.dll;*.ocx|压缩文件(*.zip;*.rar)|*.zip;*.rar|All Files(*.*)|*.*||");
-
- CFileDialog dlg(TRUE, "*.*", NULL,
- OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST |
- OFN_EXPLORER | OFN_ALLOWMULTISELECT | OFN_LONGNAMES | OFN_ENABLESIZING,
- lpszFilter);
-
- TCHAR *tcharFile = new TCHAR[32767];
- ZeroMemory(tcharFile, 32767);
- dlg.m_ofn.lpstrFile = tcharFile;
- dlg.m_ofn.nMaxFile = 32767;
- dlg.m_ofn.nFileOffset = 0;
- if( dlg.DoModal() == IDOK){
-
- CListCtrl &pListCtrl = this->GetListCtrl();
-
- //连续获取所有选择的文件信息
- CString strItem("");
- POSITION pos = dlg.GetStartPosition();
- for(; pos ; )
- {
- strItem = dlg.GetNextPathName(pos);
- this->AddFile(strItem.GetBuffer(0));
- }
- }
- delete []tcharFile;
- }
- void CFileListSend::DelFiles(void)
- {
- 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 CFileListSend::DeleteFile(const int index)
- {
- if(index > -1 && index < this->GetListCtrl().GetItemCount())
- {
- CString *p = reinterpret_cast<CString*>(this->GetListCtrl().GetItemData(index));
- if(NULL != p) delete []p;
- this->GetListCtrl().DeleteItem(index);
- }
- }
- void CFileListSend::SelectAllItems(void)
- {
- 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 CFileListSend::SelectOneItem(const int index)
- {
- CListCtrl &pListCtrl = this->GetListCtrl();
- if(index < 0 || index > pListCtrl.GetItemCount() - 1) return ;
-
- ListView_SetItemState (this->GetSafeHwnd(),
- index,
- LVIS_FOCUSED | LVIS_SELECTED,
- 0x000F);
- }
- void CFileListSend::EnableSort(const BOOL bCanSort)
- {
- this->m_bCanSort = bCanSort;
- }
- int CFileListSend::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;
- }colData[] = {
- {IDS_LIST_FILENAME, LVCFMT_LEFT, 120},
- {IDS_LIST_FILESIZE, LVCFMT_RIGHT, 100},
- {IDS_LIST_SRCDIR, LVCFMT_LEFT, 200},
- {IDS_LIST_SNDTIME, LVCFMT_LEFT, 100}
- };
-
- for(int i = 0; i < G_FILELISTVIEW_COL_COUNTS; ++i){
- VERIFY(strColCap.LoadString(colData[i].nColHdrId));
- this->GetListCtrl().InsertColumn(i, LPCTSTR(strColCap), colData[i].nFormat, colData[i].nWidth);
- }
- //初始化时以文件名来对列表数据进行排序
- this->GetListCtrl().SortItems(LVCompareProc, static_cast<DWORD>(this->m_sortedBy));
-
- return 0;
- }
- void CFileListSend::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
-
- VERIFY(m_DropTarget.Register(this));
- }
- int CALLBACK CFileListSend::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_SRCDIR, FILE_SNDTIME
- switch (sortedBy)
- {
- case FILE_NAME:
-
- case FILE_SRCDIR:
-
- case FILE_SNDTIME:
- {
- result = psz1.CompareNoCase(psz2);
- break;
- }
- case FILE_SIZE:
- {
- 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 CFileListSend::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_SRCDIR;
- break;
-
- case 3:
- sortBy = FILE_SNDTIME;
- 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));
- }
- void CFileListSend::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- #define CTRLED 0x8000
- switch(nChar) {
- case 'A':
- {
- SHORT ks = GetKeyState(VK_CONTROL);
- if(ks & CTRLED)
- {
- this->SelectAllItems();
- }
- }
- break;
- case VK_DELETE:
- {
- this->DelFiles();
- }
- break;
- }
- CListView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CFileListSend::OnDestroy()
- {
- this->SelectAllItems();
- this->DelFiles();
- CListView::OnDestroy();
- }
- BOOL CFileListSend::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- return CListView::OnSetCursor(pWnd, nHitTest, message);
- }
- //get current max trans.db table's max id.
- int CFileListSend::GetMaxTransID(void)
- {
- CGuiRecordSet rs;
- CVariantEx varex;
- char sql[MAX_SQL_SIZE];
-
- try
- {
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_COUNT);
-
- if(rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- rs.GetCollect("RECCNTS", varex.get_variant_t());
- int maxid = 1, counts = varex.AsInteger();
- rs.Close();
-
- if(counts > 0)
- {
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_MAXID);
-
- if(rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- rs.GetCollect("MAXID", varex.get_variant_t());
- maxid = varex.AsInteger();
- rs.Close();
- }
- }
- return maxid;
- }
- return 0;
- }
- catch (...)
- {
- return 0;
- }
- }
- BOOL CFileListSend::AddSendRecord(const int nitem, const char *tohost, const BOOL bsuccessful)
- {
- CGuiRecordSet rs;
- char sql[MAX_SQL_SIZE];
-
- try
- {
- int maxid = this->GetMaxTransID();
- if(0 == maxid) return FALSE;
- int transtype = SQL::TRANSTYP_SNDER | (bsuccessful ? SQL::TRANSTYP_SNDOK : 0x0);
- CString stranstime = this->GetListCtrl().GetItemText(nitem, 3).GetBuffer(0);
- CString sfilename;
- sfilename.Format("%s\%s",
- this->GetListCtrl().GetItemText(nitem, 2).GetBuffer(0),
- this->GetListCtrl().GetItemText(nitem, 0).GetBuffer(0));
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_TRANS_ADDLOG,
- maxid, sfilename.GetBuffer(0), "---", tohost, stranstime.GetBuffer(0), transtype);
- return rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
- }
- catch (...)
- {
- return FALSE;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- /*
- * support drag-drop operation from shell system.
- */
- BOOL CFileListSend::FileNamesToList(HDROP hDrop)
- {
- UINT cFiles = ::DragQueryFile(hDrop, (UINT)-1, NULL, 0);
-
- if(cFiles <= 0)
- {
- return FALSE;
- }
- char szFile[MAX_PATH];
-
- for( UINT count = 0; count < cFiles; count++ )
- {
- ::DragQueryFile(hDrop, count, szFile, sizeof(szFile));
- this->AddFile(szFile);
- }
- return TRUE;
- }
- BOOL CFileListSend::DataObjectToList(COleDataObject* const pDataObject)
- {
- if(pDataObject->IsDataAvailable(CF_HDROP))
- {
- STGMEDIUM StgMed;
- FORMATETC fmte =
- {
- CF_HDROP,
- (DVTARGETDEVICE FAR *)NULL,
- DVASPECT_CONTENT, -1,
- TYMED_HGLOBAL
- };
- if( pDataObject->GetData( CF_HDROP, &StgMed, &fmte ) )
- {
- if(!this->FileNamesToList((HDROP)StgMed.hGlobal))
- {
- return FALSE;
- }
-
- if (StgMed.pUnkForRelease)
- {
- StgMed.pUnkForRelease->Release();
- }
- else
- {
- ::GlobalFree(StgMed.hGlobal);
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- DROPEFFECT CFileListSend::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_COPY;
- }
- else{
- de = DROPEFFECT_MOVE;
- }
- */
- return de;
- }
- BOOL CFileListSend::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
- {
- return this->DataObjectToList(pDataObject);
- }
- //determine the specialed file whether is or not a file that on ftp-server host.
- BOOL CFileListSend::IsFtpFile(const int index)
- {
- CListCtrl &pListCtrl = this->GetListCtrl();
- if(index > -1 && index < pListCtrl.GetItemCount())
- {
- TCHAR chFirst = pListCtrl.GetItemText(index, 2).GetAt(0);
- return (chFirst == '/');
- }
- return FALSE;
- }
- //insert someone ftp file into sending listview.
- void CFileListSend::AddFtpFileIntoSendList(LPCSTR lpszFtpFileDir, LPCSTR lpszFtpFileName, DWORD dwFtpFilesize)
- {
- CListCtrl &pListCtrl = this->GetListCtrl();
- CString *plpsz = new CString[G_FILELISTVIEW_COL_COUNTS];
- //文件名
- int nLastIndex = pListCtrl.GetItemCount();
- pListCtrl.InsertItem(nLastIndex, lpszFtpFileName, 21);
- plpsz[0].Format("%s", lpszFtpFileName);
-
- //文件大小
- CString strItem;
- strItem.Format("%d", dwFtpFilesize);
- pListCtrl.SetItemText(nLastIndex, 1, strItem);
- plpsz[1].Format("%s", strItem);
-
- //文件所在目录
- pListCtrl.SetItemText(nLastIndex, 2, lpszFtpFileDir);
- plpsz[2].Format("%s", lpszFtpFileDir);
-
- //发送文件时间
- pListCtrl.SetItemText(nLastIndex, 3, "");
- plpsz[3].Format("%s", "");
-
- pListCtrl.SetItemData(nLastIndex, (DWORD)plpsz);
- }
- //remove all ftp file from sending listview.
- void CFileListSend::RemoveAllFtpFiles(void)
- {
- CListCtrl &pListCtrl = this->GetListCtrl();
- for(int i = pListCtrl.GetItemCount() - 1; i > -1; --i)
- {
- TCHAR chFirst = pListCtrl.GetItemText(i, 2).GetAt(0);
- if(chFirst == '/')
- {
- CString *plpsz = (CString*)pListCtrl.GetItemData(i);
-
- if(NULL != plpsz) delete []plpsz;
-
- pListCtrl.DeleteItem(i);
- }
- }
- }