FilesListCtrl.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:43k
源码类别:
P2P编程
开发平台:
Visual C++
- // FilesListCtrl.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "FilesListCtrl.h"
- #include "FileDB.h"
- #include "FileTorrent.h"
- #include "Download.h"
- #include "MonitorWnd.h"
- #include "FileBase.h"
- #include "DlgNewDownload.h"
- #include "CategoryNewDlg.h"
- #include "CategoryBarWnd.h"
- #include "SetupRegister.h"
- #include "DlgRunParameters.h"
- #include "DlgDeleteFileDBItem.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define DISPLAY_TIMEOUT_ID 1
- #define DISPLAY_TIMEOUT 3000
- #define ID_MENUITEM_SHOW_COLUMN 52826
- /////////////////////////////////////////////////////////////////////////////
- // CFilesListCtrl
- CFilesListCtrl::CFilesListCtrl() :
- CListCtrlBaseEx("filelistctrl", true), m_menuspawnPopup(true)
- {
- m_bDeleteing = false;
- m_iSortedCol = eModifiedTime + 1;
- m_pMonitorWnd = 0;
- m_pCategoryBarWnd = 0;
- m_pSetupRegister = 0;
- //
- // column infos.
- //
- CString m_strArrToolTip[eAllSize];
- CString m_strArrFieldNames[eAllSize];
- bool m_bColumnNumeric[eAllSize];
- m_strArrToolTip[eIcon] = "箭头向下:下载没有完成.rn箭头向上:下载已经完成,正在做种.rn灰色:尚未连接到服务器rn红色:没有连接到到其他下载者rn蓝色:在你连接的其他客户端中没有种子rn黄色:你没有收到任何外部连接,可能是内网rn绿色:运行良好";
- m_strArrToolTip[eFileName] = "文件名";
- m_strArrToolTip[eCompleted] = "已下载完成数据的比例";
- m_strArrToolTip[eFilesize] = "文件大小";
- m_strArrToolTip[eTimeleft] = "完全完成下载的剩余时间";
- m_strArrToolTip[eTimerunned] = "已经运行的时间";
- m_strArrToolTip[eDownloadrate] = "下载速度";
- m_strArrToolTip[eUploadrate] = "上传速度";
- m_strArrToolTip[eSeeds] = "种子数量/所有连接数量";
- m_strArrToolTip[eCreateTime] = "创建时间";
- m_strArrToolTip[eModifiedTime] = "最后一次运行的时间";
- m_strArrFieldNames[eIcon] = "状态";
- m_strArrFieldNames[eFileName] = "文件名";
- m_strArrFieldNames[eCompleted] = "已完成";
- m_strArrFieldNames[eFilesize] = "文件大小";
- m_strArrFieldNames[eTimeleft] = "剩余时间";
- m_strArrFieldNames[eTimerunned] = "已运行时间";
- m_strArrFieldNames[eDownloadrate] = "下载速度";
- m_strArrFieldNames[eUploadrate] = "上传速度";
- m_strArrFieldNames[eSeeds] = "种子/连接";
- m_strArrFieldNames[eCreateTime] = "创建时间";
- m_strArrFieldNames[eModifiedTime] = "修改时间";
- m_bColumnNumeric[eIcon] = false;
- m_bColumnNumeric[eFileName] = false;
- m_bColumnNumeric[eCompleted] = true;
- m_bColumnNumeric[eFilesize] = true;
- m_bColumnNumeric[eTimeleft] = true;
- m_bColumnNumeric[eTimerunned] = true;
- m_bColumnNumeric[eDownloadrate] = true;
- m_bColumnNumeric[eUploadrate] = true;
- m_bColumnNumeric[eSeeds] = true;
- m_bColumnNumeric[eCreateTime] = false;
- m_bColumnNumeric[eModifiedTime] = false;
- for (int i=0; i<eAllSize; i++)
- AddColumn(m_strArrFieldNames[i], m_bColumnNumeric[i], 100, m_strArrToolTip[i]);
- }
- CFilesListCtrl::~CFilesListCtrl()
- {
- }
- BEGIN_MESSAGE_MAP(CFilesListCtrl, CListCtrlBaseEx)
- //{{AFX_MSG_MAP(CFilesListCtrl)
- ON_WM_CREATE()
- ON_WM_CONTEXTMENU()
- ON_COMMAND(ID_MENUITEM_DEL, OnMenuitemDel)
- ON_COMMAND(ID_MENUITEM_DOWNLOAD, OnMenuitemDownload)
- ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
- ON_COMMAND(ID_MENUITEM_STOP, OnMenuitemStop)
- ON_WM_TIMER()
- ON_WM_DESTROY()
- ON_COMMAND(ID_MENUITEM_MOVETO, OnMenuitemMoveto)
- ON_COMMAND(ID_MENUITEM_FILE_PROPERTY, OnMenuitemFileProperty)
- ON_COMMAND(ID_MENUITEM_COPY_TORRENT_FILE_PATH, OnMenuitemCopyTorrentFilePath)
- ON_WM_MEASUREITEM()
- ON_WM_DRAWITEM()
- //}}AFX_MSG_MAP
- // ON_NOTIFY_REFLECT(HDN_ITEMCLICK, OnItemclickSort)
- // ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickSort)
- ON_NOTIFY_REFLECT(NM_DBLCLK, OnItemDblclk)
- // ON_NOTIFY_REFLECT_EX(LVN_COLUMNCLICK, OnItemclickSort)
- ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBegindrag)
- ON_COMMAND(ID_MENUITEM_PAUSE, OnMenuitemPause)
- ON_COMMAND(ID_MENUITEM_OPEN_DOWNLOADED_FILE, OnMenuitemOpenDownloadedFile)
- ON_COMMAND(ID_MENUITEM_RUN_PARAMETERS, OnMenuitemRunParamter)
- ON_COMMAND(ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, OnMenuitemOpenDownloadedDirectory)
- ON_COMMAND(ID_MENUITEM_RESTORE, OnMenuitemRestore)
- ON_COMMAND_RANGE(ID_MENUITEM_MONITOR_INFORMATION, ID_MENUITEM_MONITOR_LOGS, OnMenuitemMonitorSub)
- ON_COMMAND_RANGE(ID_MENUITEM_SHOW_COLUMN, ID_MENUITEM_SHOW_COLUMN + eAllSize, OnMenuitemShowColumn)
- ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
- // ON_MESSAGE(DOWNLOAD_TERMINATED, OnDownloadTerminated)
- // ON_COMMAND(ID_MENUITEM_LOCATE_CATEGORY, OnMenuitemLocateCategory)
- // ON_COMMAND(ID_MENUITEM_DEL_LINK, OnMenuitemDelLink)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFilesListCtrl message handlers
- void CFilesListCtrl::OnShowColumn(int iItem)
- {
- ShowFileDBItemText(iItem);
- ShowFileDBItemActive(iItem);
- }
- void CFilesListCtrl::SetSetupParameter()
- {
- assert(m_pSetupRegister);
- BOOL bRet = KillTimer(DISPLAY_TIMEOUT_ID);
- assert(bRet);
- bRet = SetTimer(DISPLAY_TIMEOUT_ID, m_pSetupRegister->m_lDisplayItval, 0);
- assert(bRet);
- }
- BOOL CFilesListCtrl::CreateEx(CWnd *pParent, CRect &rc, long lParam, CMonitorWnd* pMonitorWnd, CCategoryBarWnd* pCategoryBarWnd, CSetupRegister* pSetupRegister)
- {
- assert(pMonitorWnd && pMonitorWnd->GetSafeHwnd() && pCategoryBarWnd && pSetupRegister);
- m_pCategoryBarWnd = pCategoryBarWnd;
- m_pCategoryBarWnd->SetFilesListCtrl(this);
- m_pMonitorWnd = pMonitorWnd;
- m_pMonitorWnd->SetCateGoryBarWnd(m_pCategoryBarWnd);
- m_pSetupRegister = pSetupRegister;
- return Create(WS_VISIBLE|WS_CHILD|WS_BORDER|LVS_REPORT|LVS_SHOWSELALWAYS, rc, pParent, 10025);
- // return Create(WS_VISIBLE|WS_CHILD|WS_BORDER|LVS_REPORT, rc, pParent, 10025);
- // return Create(WS_VISIBLE|WS_CHILD|WS_BORDER|LVS_REPORT|LVS_OWNERDRAWFIXED, rc, pParent, 10025);
- }
- int CFilesListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrlBaseEx::OnCreate(lpCreateStruct) == -1)
- return -1;
- //
- // for bitmap menu
- //
- ULONG ulCmdID[] = {CMenuSpawn::eUnusedImgCmdID,
- CMenuSpawn::eUnusedImgCmdID,
- CMenuSpawn::eUnusedImgCmdID,
- CMenuSpawn::eUnusedImgCmdID,
- ID_MENUITEM_DOWNLOAD,
- ID_MENUITEM_PAUSE,
- ID_MENUITEM_STOP,
- ID_MENUITEM_DEL,
- CMenuSpawn::eUnusedImgCmdID,
- CMenuSpawn::eUnusedImgCmdID,
- CMenuSpawn::eNormalImgCmdID
- };
- m_menuspawnPopup.Create(IDB_IMAGELIST_MENU, IDB_IMAGELIST_MENU_DISABLED, sizeof(ulCmdID)/sizeof(ULONG), ulCmdID);
- //
- // Modify style.
- //
- SetExtendedStyle(LVS_EX_FULLROWSELECT ); // | LVS_EX_HEADERDRAGDROP);
- //
- // ImageList.
- //
- m_ctlImageDelete.Create(16, 17, ILC_COLOR8|ILC_MASK, 4, 4);
- m_ctlImageDelete.Add(AfxGetApp()->LoadIcon(IDI_ICON_DELETE));
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_IMAGELIST_DOWNLOAD),
- IMAGE_BITMAP,
- 0,0,
- LR_CREATEDIBSECTION);
- CBitmap bm;
- bm.Attach(hbm);
- m_ctlImage.Create(16, 17, ILC_COLOR8|ILC_MASK, 11, 4);
- m_ctlImage.SetBkColor(::GetSysColor(COLOR_WINDOW));
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- SetImageList(&m_ctlImage, LVSIL_SMALL);
- SetSortColumn(eIcon + 1);
- //
- // Set Timer.
- //
- SetTimer(DISPLAY_TIMEOUT_ID, m_pSetupRegister->m_lDisplayItval, 0);
- return 0;
- }
- void CFilesListCtrl::OnDestroy()
- {
- assert(!m_bDeleteing);
- m_bDeleteing = true;
- CListCtrlBaseEx::OnDestroy();
- }
- void CFilesListCtrl::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- if (m_bDeleteing)
- return;
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- if (pNMListView->uChanged == LVIF_STATE &&
- pNMListView->uNewState & LVIS_FOCUSED &&
- !(pNMListView->uOldState & LVIS_FOCUSED))
- {
- int nItem = pNMListView->iItem;
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- m_pMonitorWnd->Monitor(pItem);
- }
- }
- void CFilesListCtrl::OnItemDblclk(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- if (!m_pSetupRegister)
- {
- assert(false);
- return;
- }
- if (GetSelectedCount() == 1)
- {
- switch (m_pSetupRegister->m_eOperDbclickItem)
- {
- case CSetupRegister::eProperty:
- {
- OnMenuitemFileProperty();
- }
- break;
- case CSetupRegister::eRunning:
- {
- POSITION pos = GetFirstSelectedItemPosition();
- if (!pos)
- {
- assert(false);
- return;
- }
- int index = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- if (!pItem)
- {
- assert(false);
- return;
- }
- if (pItem->m_pDownload && !pItem->m_pDownload->IsPause())
- {
- OnMenuitemPause();
- }
- else
- {
- OnMenuitemDownload();
- }
- }
- break;
- default:
- assert(false);
- break;
- }
- }
- }
- /**********************************************************************
- refresh if some download corrupt and the download in current view.
- parameter:
- lParam : pointer to download object.
- **********************************************************************/
- void CFilesListCtrl::OnDownloadTerminated(long lParam, bool bCompletedMove)
- {
- bool bfind = false;
- for (int i=0; i<GetItemCount(); i++)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(i);
- if (pItem && pItem == (CFileDBItem*)lParam)
- {
- if (bCompletedMove)
- {
- DeleteItem(i);
- }
- else
- {
- bfind = true;
- ShowFileDBItemText(i);
- m_pMonitorWnd->Monitor(pItem);
- }
- break;
- }
- }
- if (!bfind)
- {
- m_pMonitorWnd->Monitor(0);
- }
- // assert(bfind);
- }
- void CFilesListCtrl::LoadCatetory(CTreeCursor curItem, CFileDBItem* pFileDBItemSel) //const vector<CFileDBItem*>& vFileDBItems)
- {
- if (!GetSafeHwnd())
- return;
- m_curCursor = curItem;
- if (!m_curCursor)
- {
- assert(false);
- return;
- }
- // begin load.
- assert(!m_bDeleteing);
- m_bDeleteing = true;
- SetRedraw(false);
- DeleteAllItems();
- int iMonitorItem = -1;
- // m_pMonitorWnd->Monitor(0);
- CCategoryBarWnd::CCategoryDBItem* pItem = (CCategoryBarWnd::CCategoryDBItem*)m_curCursor.GetData();
- assert(pItem);
- vector<CFileDBItem*>& vFileDBItems = pItem->m_vFileDBItems;
- for (int i=0; i<vFileDBItems.size(); i++)
- {
- CFileDBItem* pItem = vFileDBItems[i];
- InsertItem(i, ""); // ctime(&pItem->m_tTime)); //, iImageID);
- SetItemData(i, (long)pItem);
- ShowFileDBItemText(i);
- if (pFileDBItemSel && pFileDBItemSel == pItem)
- iMonitorItem = i;
- }
- if (iMonitorItem != -1)
- {
- ClearSelectItems();
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(iMonitorItem);
- if (pItem)
- {
- SetItemState(iMonitorItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
- EnsureVisible(iMonitorItem, true);
- m_pMonitorWnd->Monitor(pItem);
- }
- }
- SortItems(Compare1, m_iSortedCol);
- if (iMonitorItem == -1)
- {
- if (!vFileDBItems.empty())
- {
- iMonitorItem = 0;
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(iMonitorItem);
- if (pItem)
- {
- SetItemState(iMonitorItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
- EnsureVisible(iMonitorItem, true);
- m_pMonitorWnd->Monitor(pItem);
- }
- }
- else
- {
- m_pMonitorWnd->Monitor(0);
- }
- }
- // refresh the status.
- OnTimer(DISPLAY_TIMEOUT_ID);
- SetRedraw(true);
- if (pFileDBItemSel)
- SetFocus();
- m_bDeleteing = false;
- }
- /************************************************************
- popup context menu, and menu command handler.
- *************************************************************/
- void CFilesListCtrl::OnUpdateMenuitem(CCmdUI* pCmdUI)
- {
- bool bstart = false,
- bstop = false,
- bPause = false,
- bdelete = false,
- bMoveto = false,
- bProperty = false;
- int iSelectedCount = 0;
- int iFocusItem = -1;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- if (GetItemState(index, LVIS_FOCUSED) & LVIS_FOCUSED)
- iFocusItem = index;
- iSelectedCount++;
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- if (pItem->m_pDownload)
- {
- bstop = true;
- if (!pItem->m_pDownload->IsPause())
- bPause = true;
- else
- bstart = true;
- }
- else
- {
- bstart = true;
- }
- bdelete = true;
- bMoveto = true;
- }
- if (iSelectedCount == 1)
- bProperty = true;
- if (m_curCursor == m_pCategoryBarWnd->m_curRecycle)
- bstart = bstop = bPause = false;
- switch (pCmdUI->m_nID)
- {
- case ID_MENUITEM_DOWNLOAD:
- pCmdUI->Enable(bstart);
- break;
- case ID_MENUITEM_PAUSE:
- pCmdUI->Enable(bPause);
- break;
- case ID_MENUITEM_STOP:
- pCmdUI->Enable(bstop);
- break;
- case ID_MENUITEM_DEL:
- pCmdUI->Enable(bdelete);
- break;
- case ID_MENUITEM_FILE_PROPERTY:
- pCmdUI->Enable(bProperty);
- case ID_MENUITEM_OPEN_DOWNLOADED_FILE:
- case ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY:
- pCmdUI->Enable(bProperty);
- break;
- case ID_MENUITEM_MOVETO:
- pCmdUI->Enable(bMoveto);
- break;
- case ID_MENUITEM_RUN_PARAMETERS:
- pCmdUI->Enable(bProperty);
- break;
- case ID_MENUITEM_COPY_TORRENT_FILE_PATH:
- pCmdUI->Enable(bProperty);
- break;
- break;
- }
- }
- bool CFilesListCtrl::OnContextMenuRecycle(CWnd* pWnd, CPoint point)
- {
- if (m_curCursor != m_pCategoryBarWnd->m_curRecycle)
- return false;
- bool bEnable = GetSelectedCount() > 0;
- CMenu menu;
- if (menu.CreatePopupMenu())
- {
- menu.AppendMenu(MF_STRING, ID_MENUITEM_RESTORE, "还原(&O)");
- menu.AppendMenu(MF_STRING, ID_MENUITEM_DEL, "删除(&D)tDel");
- menu.AppendMenu(MF_STRING, ID_MENUITEM_MOVETO, "移动到(&M)...");
- menu.AppendMenu(MF_STRING, ID_MENUITEM_FILE_PROPERTY, "属性(&R)");
- menu.EnableMenuItem(ID_MENUITEM_RESTORE, MF_BYCOMMAND |
- (bEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu.EnableMenuItem(ID_MENUITEM_DEL, MF_BYCOMMAND |
- (bEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu.EnableMenuItem(ID_MENUITEM_MOVETO, MF_BYCOMMAND |
- (bEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu.EnableMenuItem(ID_MENUITEM_FILE_PROPERTY, MF_BYCOMMAND |
- (bEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x,
- point.y, this);
- }
- return true;
- }
- void CFilesListCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- if (CListCtrlBaseEx::OnContextMenu(pWnd, point))
- return;
- if (OnContextMenuRecycle(pWnd, point))
- return;
- bool bstart = false,
- bPause = false,
- bstop = false,
- bdelete = false,
- bMoveto = false,
- bProperty = false;
- int iSelectedCount = 0;
- int iFocusItem = -1;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- if (GetItemState(index, LVIS_FOCUSED) & LVIS_FOCUSED)
- iFocusItem = index;
- iSelectedCount++;
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- if (pItem->m_pDownload)
- {
- bstop = true;
- if (!pItem->m_pDownload->IsPause())
- bPause = true;
- else
- bstart = true;
- }
- else
- {
- bstart = true;
- }
- bdelete = true;
- bMoveto = true;
- }
- if (iSelectedCount == 1)
- bProperty = true;
- if (m_curCursor == m_pCategoryBarWnd->m_curRecycle)
- bstart = bstop = bPause = false;
- // trackpopupmenu.
- CMenu menu_bar;
- menu_bar.LoadMenu(IDR_MENU_CATEGORY);
- CMenu* file_menu = menu_bar.GetSubMenu(1);
- CMenu* pMonitorMenu = file_menu->GetSubMenu(6);
- pMonitorMenu->EnableMenuItem(ID_MENUITEM_MONITOR_INFORMATION, MF_BYCOMMAND |
- (bProperty ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- pMonitorMenu->EnableMenuItem(ID_MENUITEM_MONITOR_SUBFILES, MF_BYCOMMAND |
- (bProperty ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- pMonitorMenu->EnableMenuItem(ID_MENUITEM_MONITOR_RUNSTATICS, MF_BYCOMMAND |
- (bProperty ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- pMonitorMenu->EnableMenuItem(ID_MENUITEM_MONITOR_PEERS, MF_BYCOMMAND |
- (bProperty ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- pMonitorMenu->EnableMenuItem(ID_MENUITEM_MONITOR_LOGS, MF_BYCOMMAND |
- (bProperty ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- //
- // command ui.
- //
- file_menu->EnableMenuItem(ID_MENUITEM_DOWNLOAD, MF_BYCOMMAND |
- (bstart ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_PAUSE, MF_BYCOMMAND |
- (bPause ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_STOP, MF_BYCOMMAND |
- (bstop ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_DEL, MF_BYCOMMAND |
- (bdelete ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_MOVETO, MF_BYCOMMAND |
- (bMoveto? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_MONITOR, MF_BYCOMMAND |
- (bProperty? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_COPY_TORRENT_FILE_PATH, MF_BYCOMMAND |
- (bProperty? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_FILE_PROPERTY, MF_BYCOMMAND |
- (bProperty? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_RUN_PARAMETERS, MF_BYCOMMAND |
- (bProperty? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- // shell excute
- file_menu->EnableMenuItem(ID_MENUITEM_OPEN_DOWNLOADED_FILE, MF_BYCOMMAND |
- (bProperty? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- file_menu->EnableMenuItem(ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, MF_BYCOMMAND |
- (bProperty? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- if (iFocusItem != -1)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(iFocusItem);
- long lCount = GetSelectedCount();
- if (lCount == 1 && pItem->m_pDownload)
- file_menu->EnableMenuItem(ID_MENUITEM_MONITOR, MF_BYCOMMAND | MF_ENABLED);
- else
- file_menu->EnableMenuItem(ID_MENUITEM_MONITOR, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
- }
- m_menuspawnPopup.RemapMenuEx(file_menu);
- file_menu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x,
- point.y, this);
- }
- //
- // for bitmap menu trackpopupmenu.
- //
- void CFilesListCtrl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
- {
- if (!m_menuspawnPopup.MeasureItem(lpMeasureItemStruct))
- CListCtrlBaseEx::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
- }
- void CFilesListCtrl::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- if (!m_menuspawnPopup.DrawItem(lpDrawItemStruct))
- CListCtrlBaseEx::OnDrawItem(nIDCtl, lpDrawItemStruct);
- }
- void CFilesListCtrl::OnCommandMenuitem(UINT iID)
- {
- switch (iID)
- {
- case ID_MENUITEM_DOWNLOAD:
- OnMenuitemDownload();
- break;
- case ID_MENUITEM_PAUSE:
- OnMenuitemPause();
- break;
- case ID_MENUITEM_STOP:
- OnMenuitemStop();
- break;
- case ID_MENUITEM_DEL:
- OnMenuitemDel();
- break;
- case ID_MENUITEM_FILE_PROPERTY:
- OnMenuitemFileProperty();
- break;
- case ID_MENUITEM_OPEN_DOWNLOADED_FILE:
- OnMenuitemOpenDownloadedFile();
- break;
- case ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY:
- OnMenuitemOpenDownloadedDirectory();
- break;
- case ID_MENUITEM_MOVETO:
- OnMenuitemMoveto();
- break;
- case ID_MENUITEM_RUN_PARAMETERS:
- OnMenuitemRunParamter();
- break;
- case ID_MENUITEM_COPY_TORRENT_FILE_PATH:
- OnMenuitemCopyTorrentFilePath();
- break;
- }
- }
- void CFilesListCtrl::OnMenuitemRestore()
- {
- assert(m_curCursor == m_pCategoryBarWnd->m_curRecycle);
- //
- // Get move items.
- //
- long lCount = GetSelectedCount();
- if (lCount <= 0) return;
- int* index = new int[lCount];
- int i = 0;
- bool bMonitorChange = false;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- index[i++] = nItem;
- }
- //
- // Move items.
- //
- for (i=lCount-1; i>=0; i--)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index[i]);
- assert(pItem && m_curCursor && m_pCategoryBarWnd);
- CTreeCursor curMove = m_pCategoryBarWnd->findCursorByDirid(pItem->m_lDirectoryID);
- assert(curMove);
- m_pCategoryBarWnd->FileDBItemRestore(pItem);
- }
- m_bDeleteing = true;
- for (i=lCount-1; i>=0; i--)
- {
- BOOL bRet = DeleteItem(index[i]);
- assert(bRet);
- }
- m_bDeleteing = false;
- delete index;
- }
- void CFilesListCtrl::OnMenuitemMonitorSub(UINT uID)
- {
- if (uID >= ID_MENUITEM_MONITOR_INFORMATION &&
- uID <= ID_MENUITEM_MONITOR_LOGS)
- {
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int index = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- m_pMonitorWnd->Monitor(pItem, uID - ID_MENUITEM_MONITOR_INFORMATION);
- }
- }
- }
- void CFilesListCtrl::OnMenuitemPause()
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- if (pItem->m_pDownload && !pItem->m_pDownload->IsPause())
- {
- m_pCategoryBarWnd->PauseDBFiles(pItem);
- // pItem->m_pDownload->Pause();
- ShowFileDBItemText(nItem);
- }
- }
- }
- void CFilesListCtrl::OnMenuitemDel()
- {
- DeleteSelectedItem(false) ;
- }
- void CFilesListCtrl::DeleteSelectedItem(bool bForceDelete)
- {
- bool bDownloading = false;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- if (pItem && pItem->m_pDownload)
- {
- bDownloading = true;
- break;
- }
- }
- bool bDelUncompleted = false, bDelCompleted = false;
- if (m_curCursor == m_pCategoryBarWnd->m_curRecycle || bForceDelete)
- {
- CDlgDeleteFileDBItem dlg;
- if (dlg.DoModal() != IDOK)
- {
- SetFocus();
- return;
- }
- bForceDelete = true;
- bDelUncompleted = dlg.m_bDelUncompleted != 0;
- bDelCompleted = dlg.m_bDelCompleted != 0;
- }
- else if (bDownloading)
- {
- if (AfxMessageBox("有下载正在进行中, 您是否确定删除?", MB_YESNO) != IDYES) //Are you sure you wish to permanently delete these item(s)?", MB_YESNO) != IDYES)
- {
- SetFocus();
- return;
- }
- }
- // get index to be deleted arrray. and to stop vector.
- long lCount = GetSelectedCount();
- if (lCount <= 0) return;
- int* index = new int[lCount];
- int i = 0;
- bool bMonitorChange = false;
- vector<CFileDBItem*> vToStopFileDBItems;
- pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- index[i++] = nItem;
- if (GetItemState(nItem, LVIS_FOCUSED) & LVIS_FOCUSED)
- {
- bMonitorChange = true;
- m_pMonitorWnd->Monitor(0);
- }
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- if (pItem->m_pDownload)
- vToStopFileDBItems.push_back(pItem);
- }
- // if is downloading, popup it from starting engine deque.
- m_pCategoryBarWnd->StopDBFiles(vToStopFileDBItems);
- // delete items.
- assert(!m_bDeleteing);
- m_bDeleteing = true;
- for (i=lCount-1; i>=0; i--)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index[i]);
- assert(pItem && m_curCursor && m_pCategoryBarWnd);
- m_pCategoryBarWnd->DeleteDBFile(m_curCursor, pItem, bForceDelete, bDelUncompleted, bDelCompleted);
- BOOL bRet = DeleteItem(index[i]);
- assert(bRet);
- }
- m_bDeleteing = false;
- delete index;
- if (bMonitorChange && GetItemCount())
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(0);
- if (pItem)
- m_pMonitorWnd->Monitor(pItem);
- }
- SetFocus();
- }
- void CFilesListCtrl::OnMenuitemDownload()
- {
- vector<long> vToDelIndexs;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- if (!pItem->m_pDownload)
- {
- SetItem(nItem, 0, LVIF_IMAGE, 0, IID_PREPARE, 0, 0, 0);
- SetItemTextEx(nItem, eCompleted, "Waiting...");
- if (!m_pCategoryBarWnd->Download(m_curCursor, pItem))
- continue;
- vToDelIndexs.push_back(nItem);
- }
- else
- {
- if (pItem->m_pDownload->IsPause())
- {
- pItem->m_pDownload->Pause(false);
- ShowFileDBItemActive(nItem);
- }
- }
- }
- //
- // if download , move to downloading category.
- //
- for (int i=vToDelIndexs.size()-1; i>=0; i--)
- {
- BOOL bRet = DeleteItem(vToDelIndexs[i]);
- assert(bRet);
- }
- }
- void CFilesListCtrl::OnMenuitemStop()
- {
- if (AfxMessageBox("你确定要停止吗?", MB_YESNO) == IDNO)
- return;
- vector<CFileDBItem*> vToStopFileDBItems;
- vector<long> vToStopIndexs;
- CFileDBItem* pMonitorItem = 0;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- if (!pItem) {ASSERT(pItem); return;}
- if (pItem->m_pDownload)
- {
- vToStopIndexs.push_back(nItem);
- vToStopFileDBItems.push_back(pItem);
- }
- if (GetItemState(nItem, LVIS_FOCUSED) & LVIS_FOCUSED)
- pMonitorItem = pItem;
- }
- //
- // stop download object.
- //
- m_pCategoryBarWnd->StopDBFiles(vToStopFileDBItems);
- //
- // if completed, move it to category assigned.
- //
- m_bDeleteing = true;
- for (int i=vToStopIndexs.size()-1; i>=0; i--)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(vToStopIndexs[i]);
- assert(pItem && m_pCategoryBarWnd);
- if (!m_pCategoryBarWnd->StopDBFilesMoveComplete(pItem))
- {
- ShowFileDBItemText(vToStopIndexs[i]);
- }
- else
- {
- BOOL bRet = DeleteItem(vToStopIndexs[i]);
- assert(bRet);
- if (pMonitorItem == pItem) pMonitorItem = 0;
- }
- }
- m_bDeleteing = false;
- // refresh the item monitored and not moved.
- if (pMonitorItem)
- m_pMonitorWnd->Monitor(pMonitorItem);
- else
- {
- for (int i=0; i<GetItemCount(); i++)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(i);
- if (!pItem) {ASSERT(pItem); return;}
- if (GetItemState(i, LVIS_FOCUSED) & LVIS_FOCUSED)
- {
- pMonitorItem = pItem;
- break;
- }
- }
- if (pMonitorItem)
- m_pMonitorWnd->Monitor(pMonitorItem);
- else
- m_pMonitorWnd->Monitor(0);
- }
- }
- void CFilesListCtrl::OnMenuitemMoveto()
- {
- CTreeCursor curSelected = m_curCursor;
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(nItem);
- assert(pItem);
- CTreeCursor curRet = m_pCategoryBarWnd->findCursorByDirid(pItem->m_lDirectoryID);
- if (curRet) curSelected = curRet;
- }
- CCategoryNewDlg dlg(&m_pCategoryBarWnd->m_ctlImage, m_pCategoryBarWnd, false, curSelected);
- if (dlg.DoModal() != IDOK)
- return;
- //
- // if move to self, cancel operation.
- //
- CTreeCursor curMovetoItem(dlg.m_ItemSel, m_pCategoryBarWnd);
- if (curMovetoItem == m_curCursor)
- return;
- if (curMovetoItem == m_pCategoryBarWnd->m_curRecycle)
- {
- DeleteSelectedItem(false);
- return;
- }
- //
- // Get move items.
- //
- long lCount = GetSelectedCount();
- if (lCount <= 0) return;
- int* index = new int[lCount];
- int i = 0;
- bool bMonitorChange = false;
- pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- index[i++] = nItem;
- // if monitoring, cancel it.
- if (GetItemState(nItem, LVIS_FOCUSED) & LVIS_FOCUSED)
- {
- bMonitorChange = true;
- m_pMonitorWnd->Monitor(0);
- }
- }
- //
- // Move items.
- //
- vector<CFileDBItem*> vFileDBItems;
- for (i=lCount-1; i>=0; i--)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index[i]);
- assert(pItem && m_curCursor && m_pCategoryBarWnd);
- vFileDBItems.push_back(pItem);
- }
- bool bRet = m_pCategoryBarWnd->FileDBItemMovetoArray(m_curCursor, curMovetoItem, vFileDBItems);
- if (bRet)
- {
- m_bDeleteing = true;
- for (i=lCount-1; i>=0; i--)
- {
- BOOL bRet = DeleteItem(index[i]);
- assert(bRet);
- }
- m_bDeleteing = false;
- }
- delete index;
- //
- // Monitor next item.
- //
- if (bMonitorChange && GetItemCount())
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(0);
- if (pItem)
- m_pMonitorWnd->Monitor(pItem);
- }
- }
- void CFilesListCtrl::OnMenuitemCopyTorrentFilePath()
- {
- assert(GetSelectedCount() == 1);
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int index = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- assert(pItem);
- CString strSource = (m_pCategoryBarWnd->GetDBSavePath() + pItem->m_strTorrentFileName).data();
- if (!CopyTextToClipboard(strSource))
- AfxMessageBox("复制Torrent路径失败");
- }
- }
- void CFilesListCtrl::OnMenuitemFileProperty()
- {
- assert(GetSelectedCount() == 1);
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int index = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- assert(pItem);
- try
- {
- CFileTorrent filetorrent;
- if (!filetorrent.OpenFile(m_pCategoryBarWnd->GetDBSavePath() + pItem->m_strTorrentFileName))
- {
- AfxMessageBox(filetorrent.GetBadMsg().data());
- return;
- }
- filetorrent.QueryDir();
- CDlgNewDownload dlg(&m_pCategoryBarWnd->m_ctlImage,
- &filetorrent,
- m_pCategoryBarWnd,
- false, pItem,
- pItem->m_lDirectoryID,
- pItem->m_strFileName);
- if (dlg.DoModal() == IDOK)
- {
- //
- // if download immediately, start it.
- //
- pItem->m_strComment = dlg.m_strComment.GetBuffer(0);
- if (dlg.m_iStartMode == 0) // start immediately.
- {
- if (!pItem->m_pDownload)
- {
- SetItem(index, 0, LVIF_IMAGE, 0, IID_PREPARE, 0, 0, 0);
- SetItemTextEx(index, eCompleted, "Waiting...");
- if (m_pCategoryBarWnd->Download(m_curCursor, pItem))
- {
- BOOL bRet = DeleteItem(index);
- ASSERT(bRet);
- }
- }
- }
- }
- }
- catch (string e)
- {
- AfxMessageBox(e.data());
- return;
- }
- }
- }
- void CFilesListCtrl::OnMenuitemRunParamter()
- {
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int index = GetNextSelectedItem(pos);
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- if (!pItem)
- {
- assert(false);
- return;
- }
- CDlgRunParameters dlgParamters(pItem, m_pSetupRegister);
- dlgParamters.DoModal();
- m_pMonitorWnd->Monitor(pItem);
- }
- }
- void CFilesListCtrl::OnMenuitemOpenDownloadedFile()
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- if (GetItemState(index, LVIS_FOCUSED) & LVIS_FOCUSED)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- HINSTANCE iRet = ShellExecute(NULL, "open" , pItem->m_strFileName.data(), 0, 0, SW_SHOW);
- if ((UINT)iRet <= HINSTANCE_ERROR)
- AfxMessageBox(WSAShowError().data());
- break;
- }
- }
- }
- void CFilesListCtrl::OnMenuitemOpenDownloadedDirectory()
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- if (GetItemState(index, LVIS_FOCUSED) & LVIS_FOCUSED)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- // Get Parent folder as the directory, no matter that the fileitem is dir or file.
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- _splitpath(pItem->m_strFileName.data(), drive, dir, fname, ext);
- string strPath = string(drive) + dir;
- HINSTANCE iRet = ShellExecute(NULL, "open" , strPath.data(), 0, 0, SW_SHOW);
- if ((UINT)iRet <= HINSTANCE_ERROR)
- AfxMessageBox(WSAShowError().data());
- break;
- }
- }
- }
- /************************************************************
- show file filedbitem
- *************************************************************/
- void CFilesListCtrl::OnTimer(UINT nIDEvent)
- {
- if (DISPLAY_TIMEOUT_ID != nIDEvent)
- return;
- int iCount = GetItemCount();
- for (int i=0; i<GetItemCount(); i++)
- {
- ShowFileDBItemActive(i);
- }
- m_pMonitorWnd->Refresh();
- // CListCtrl::OnTimer(nIDEvent);
- }
- void CFilesListCtrl::ShowFileDBItemText(int iItem)
- {
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(iItem);
- string strName;
- string strSavePath;
- SplitPathName(pItem->m_strFileName, strSavePath, strName);
- SetItemTextEx(iItem, eFileName, strName.data());
- char szText[1024] = {0};
- sprintf(szText, "%d%%", (long)((pItem->m_fComplete)*100));
- SetItemTextEx(iItem, eCompleted, szText);
- sprintf(szText, "%s", FormatSize(pItem->m_lFileSize).data());
- SetItemTextEx(iItem, eFilesize, szText);
- SetItemTextEx(iItem, eTimeleft, "");
- SetItemTextEx(iItem, eTimerunned, "");
- SetItemTextEx(iItem, eDownloadrate, "");
- SetItemTextEx(iItem, eUploadrate, "");
- SetItemTextEx(iItem, eSeeds, "");
- CTime tCreatedTime(pItem->m_tTime);
- CString strCreatedTime = tCreatedTime.Format("%Y-%m-%d %H:%M");
- SetItemTextEx(iItem, eCreateTime, strCreatedTime); // ctime(&pItem->m_tTime));
- tCreatedTime = pItem->m_tModifiedTime;
- strCreatedTime = tCreatedTime.Format("%Y-%m-%d %H:%M");
- SetItemTextEx(iItem, eModifiedTime, strCreatedTime); // ctime(&pItem->m_tTime));
- int iImageID = (pItem->m_fComplete>= 1) ? IID_COMPLETE : IID_UNCOMPLETE;
- if (!pItem->m_vUnneededFileInx.empty())
- {
- iImageID = (pItem->m_fCompletePart >= 1) ? IID_COMPLETE : IID_UNCOMPLETE;
- }
- if (pItem->m_pDownload && pItem->m_pDownload->IsPause())
- iImageID = IID_PAUSE;
- if (pItem->m_bBad == true)
- iImageID = IID_BAD;
- SetItem(iItem, 0, LVIF_IMAGE, 0, iImageID, 0, 0, 0);
- }
- void CFilesListCtrl::ShowFileDBItemActive(int iItem)
- {
- if (iItem > GetItemCount() || iItem < 0)
- {
- assert(false);
- return;
- }
- int i = iItem;
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(i);
- if (!pItem)
- {
- assert(false);
- return;
- }
- if (pItem->m_pDownload)
- {
- // the download is terminated.
- if (pItem->m_pDownload->IsBad())
- {
- SetItem(i, 0, LVIF_IMAGE, 0, IID_BAD, 0, 0, 0);
- return;
- }
- if (pItem->m_pDownload->IsPause())
- {
- SetItem(i, 0, LVIF_IMAGE, 0, IID_PAUSE, 0, 0, 0);
- return;
- }
- // download maybe not begin yet.
- if (!pItem->m_pDownload->IsDownloading())
- {
- SetItem(i, 0, LVIF_IMAGE, 0, IID_PREPARE, 0, 0, 0);
- SetItemTextEx(i, eCompleted, "Waiting...");
- return;
- }
- CStatics statics = pItem->m_pDownload->GetStatics();
- char szText[1024] = {0};
- if (statics.m_bAllocating)
- {
- SetItem(i, 0, LVIF_IMAGE, 0, IID_PREPARE, 0, 0, 0);
- sprintf(szText, "Allocate(%.2f)", statics.m_fAllocatingPercent);
- SetItemTextEx(i, eCompleted, szText);
- }
- else
- {
- if (statics.m_bStorageChecking)
- {
- SetItem(i, 0, LVIF_IMAGE, 0, IID_PREPARE, 0, 0, 0);
- sprintf(szText, "Checking(%.2f)", statics.m_fCheckingPercent);
- SetItemTextEx(i, eCompleted, szText);
- }
- else
- {
- float fCompleted = statics.m_fCompleted;
- if (!pItem->m_vUnneededFileInx.empty())
- fCompleted = statics.m_fCompletedPart;
- sprintf(szText, "%d%%", (long)((fCompleted)*100));
- SetItemTextEx(i, eCompleted, szText);
- if (statics.m_tTimeLeft < 0)
- {
- SetItemTextEx(i, eTimeleft, "-");
- }
- else
- {
- CTimeSpan TimeLeft(statics.m_tTimeLeft);
- SetItemTextEx(i, eTimeleft, TimeLeft.Format("%D:%H:%M:%S"));
- }
- CTimeSpan TimeRunned(statics.m_tTimeRunned);
- SetItemTextEx(i, eTimerunned, TimeRunned.Format("%D:%H:%M:%S"));
- sprintf(szText, "%s", FormatSize(statics.m_lDownRate).data());
- SetItemTextEx(i, eDownloadrate, szText);
- sprintf(szText, "%s", FormatSize(statics.m_lUpRate).data());
- SetItemTextEx(i, eUploadrate, szText);
- sprintf(szText, "%d/%d", statics.m_lSeedsCount, statics.m_lPeersCount);
- SetItemTextEx(i, eSeeds, szText);
- // set data
- // pItem->m_fComplete = statics.m_fCompleted;
- // set image
- bool bCompleted = fCompleted >= 1;
- int iImageID = 0;
- if (!statics.m_bTrackerConnected)
- {
- iImageID = bCompleted ? IID_UPLOAD_TRACKER : IID_DOWNLOAD_TRACKER;
- }
- else if(statics.m_lPeersCount == 0)
- {
- iImageID = bCompleted ? IID_UPLOAD_OTHERS: IID_DOWNLOAD_OTHERS;
- }
- else if (statics.m_bFired)
- {
- iImageID = bCompleted ? IID_UPLOAD_FIRE: IID_DOWNLOAD_FIRE;
- }
- else if (statics.m_lSeedsCount == 0)
- {
- iImageID = bCompleted ? IID_UPLOAD_SEED : IID_DOWNLOAD_SEED;
- }
- else
- {
- iImageID = bCompleted ? IID_UPLOAD : IID_DOWNLOAD;
- }
- SetItem(i, 0, LVIF_IMAGE, 0, iImageID, 0, 0, 0);
- }
- }
- } // end if (pItem->m_pDownload)
- else
- {
- LV_ITEM lvItem;
- lvItem.iItem = i;
- lvItem.iSubItem = 0;
- lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
- lvItem.cchTextMax = 0;
- lvItem.pszText = "";
- GetItem(&lvItem);
- if (lvItem.iImage != IID_COMPLETE &&
- lvItem.iImage != IID_UNCOMPLETE &&
- lvItem.iImage != IID_BAD)
- {
- ShowFileDBItemText(i);
- }
- }
- }
- /************************************************************
- sort item.
- *************************************************************/
- void CFilesListCtrl::OnColumnSort(int iColumn)
- {
- assert(abs(iColumn) <= eAllSize && abs(iColumn) > 0);
- m_iSortedCol = iColumn;
- SortItems(Compare1, m_iSortedCol);
- }
- void CFilesListCtrl::OnItemclickSort(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- NMLISTVIEW* pNMListView = reinterpret_cast<NMLISTVIEW*>(pNMHDR);
- int iSubItem = GetLogicalIndex(pNMListView->iSubItem);
- if (iSubItem == -1)
- {
- assert(false);
- return;
- }
- int nAbsSortColumn = abs(m_iSortedCol);
- if (nAbsSortColumn > 0)
- {
- if (iSubItem == nAbsSortColumn-1)
- m_iSortedCol = -m_iSortedCol;
- else
- m_iSortedCol = iSubItem + 1;
- }
- else
- m_iSortedCol= iSubItem + 1;
- SortItems(Compare1, m_iSortedCol);
- }
- int CALLBACK CFilesListCtrl::Compare1(LPARAM lParam1, LPARAM lParam2, LPARAM
- lParamSort)
- {
- CFileDBItem* pItem1 = (CFileDBItem*) lParam1;
- CFileDBItem* pItem2 = (CFileDBItem*) lParam2;
- if (!pItem1 || !pItem2)
- {
- ASSERT(pItem1 && pItem2);
- return 0;
- }
- long sOrder = lParamSort < 0 ? -1 : 1;
- eColumns eCol = (eColumns)((lParamSort * sOrder) - 1);
- ////////////////////////////////////////////////////////////
- // for dynamic data.
- CStatics statics1, statics2;
- int iStat1 = 0, iStat2 = 0;
- if ((eCol >= eTimeleft && eCol <= eSeeds) || (eCol == eIcon))
- {
- if (!pItem1->m_pDownload - !pItem2->m_pDownload)
- {
- return (!pItem1->m_pDownload - !pItem2->m_pDownload) * sOrder * -1;
- }
- if (pItem1->m_pDownload && !pItem1->m_pDownload->IsBad() && // !pItem1->m_pDownload->IsPause() &&
- pItem1->m_pDownload->IsDownloading())
- {
- statics1 = pItem1->m_pDownload->GetStatics();
- iStat1 = 1;
- }
- if (pItem2->m_pDownload && !pItem2->m_pDownload->IsBad() && // !pItem2->m_pDownload->IsPause() &&
- pItem2->m_pDownload->IsDownloading())
- {
- statics2 = pItem2->m_pDownload->GetStatics();
- iStat2 = 1;
- }
- // if start and silent then return;
- int idif = (iStat1 -iStat2) * sOrder;
- if (idif) return idif;
- // both silent .
- if (!iStat1 && eCol != eIcon)
- return 0;
- // both running
- if (iStat1 && iStat2)
- {
- idif = pItem1->m_pDownload->IsPause() - pItem2->m_pDownload->IsPause();
- if (idif) return -idif * sOrder;
- }
- }
- ////////////////////////////////////////////////////////////////
- // compare sort column.
- switch (eCol)
- {
- case eIcon:
- {
- if (!iStat1 && !iStat2)
- sOrder *= -1;
- return (pItem1->m_fComplete - pItem2->m_fComplete) * 100 * sOrder;
- }
- break;
- case eFileName:
- return strcmp(pItem1->m_strFileName.data(), pItem2->m_strFileName.data()) * sOrder;
- break;
- case eCompleted:
- return (pItem1->m_fComplete - pItem2->m_fComplete) * 100 * sOrder;
- break;
- case eFilesize:
- return (pItem1->m_lFileSize - pItem2->m_lFileSize) * sOrder;
- break;
- case eTimeleft:
- // return (pItem1->t< pItem1->m_lFileSize) * sOrder;
- return (statics1.m_tTimeLeft - statics2.m_tTimeLeft) * sOrder;
- break;
- case eTimerunned:
- return (statics1.m_tTimeRunned - statics2.m_tTimeRunned) * sOrder;
- break;
- case eDownloadrate:
- return (statics1.m_lDownRate - statics2.m_lDownRate) * sOrder;
- break;
- case eUploadrate:
- return (statics1.m_lUpRate - statics2.m_lUpRate) * sOrder;
- break;
- case eSeeds:
- return (statics1.m_lSeedsCount - statics2.m_lSeedsCount) * sOrder;
- break;
- case eCreateTime:
- return (pItem1->m_tTime - pItem2->m_tTime) * sOrder;
- break;
- case eModifiedTime:
- return (pItem1->m_tModifiedTime - pItem2->m_tModifiedTime) * sOrder;
- break;
- default:
- assert(false);
- break;
- }
- return 0;
- }
- /************************************************************
- drag and drop
- *************************************************************/
- void CFilesListCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- //
- // if is downloading or no selected item return.
- //
- // if (m_curCursor == m_pCategoryBarWnd->m_curDownloading)
- // return;
- long lCount = GetSelectedCount();
- if (lCount <= 0) return;
- //
- // Get selected items.
- //
- int* indexarr = new int[lCount];
- auto_ptr<int> auarr(indexarr);
- int i=0;
- bool bStopMonitor = false;
- m_vDragFileDBItems.clear();
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- indexarr[i++] = index;
- if (GetItemState(index, LVIS_FOCUSED) & LVIS_FOCUSED)
- bStopMonitor = true;
- CFileDBItem* pItem = (CFileDBItem*)GetItemData(index);
- m_vDragFileDBItems.push_back(pItem);
- }
- //
- // Register clipboard data.
- //
- CSharedFile globFile;
- globFile.Write("1", 1);
- COleDataSource srcItem;
- srcItem.CacheGlobalData(m_pCategoryBarWnd->m_nIDClipFormat, globFile.Detach());
- DWORD dwRet = srcItem.DoDragDrop();
- if (dwRet & DROPEFFECT_MOVE)
- {
- if (bStopMonitor)
- m_pMonitorWnd->Monitor(0);
- assert(!m_bDeleteing);
- m_bDeleteing = true;
- for (i=lCount-1; i>=0; i--)
- DeleteItem(indexarr[i]);
- m_bDeleteing = false;
- }
- m_vDragFileDBItems.clear();
- }
- /************************************************************
- handler hot key.
- *************************************************************/
- BOOL CFilesListCtrl::PreTranslateMessage(MSG* pMsg)
- {
- if (pMsg->message == WM_KEYDOWN)
- {
- switch (pMsg->wParam)
- {
- case VK_DELETE:
- {
- DeleteSelectedItem(GetKeyState(VK_SHIFT) < 0);
- return true;
- }
- break;
- }
- }
- return CListCtrlBaseEx::PreTranslateMessage(pMsg);
- }
- void CFilesListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
- switch(lplvcd->nmcd.dwDrawStage)
- {
- case CDDS_PREPAINT:
- *pResult = CDRF_NOTIFYITEMDRAW; // ask for subitem notifications.
- break;
- case CDDS_ITEMPREPAINT:
- {
- *pResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NOTIFYPOSTPAINT;
- }
- break;
- case CDDS_ITEMPREPAINT|CDDS_SUBITEM: // recd when CDRF_NOTIFYSUBITEMDRAW is returned in
- { // response to CDDS_ITEMPREPAINT.
- *pResult = CDRF_SKIPDEFAULT;
- *pResult = CDRF_NOTIFYPOSTPAINT;
- }
- break;
- case CDDS_ITEMPOSTPAINT|CDDS_SUBITEM:
- {
- *pResult = CDRF_DODEFAULT;
- int iCol = lplvcd->iSubItem;
- int iRow = lplvcd->nmcd.dwItemSpec;
- CRect rc;
- if (iCol == 0)
- {
- CFileDBItem* pItem = (CFileDBItem*)lplvcd->nmcd.lItemlParam;
- if (!pItem)
- {
- assert(false);
- return;
- }
- if (pItem->m_vUnneededFileInx.size())
- {
- *pResult = CDRF_SKIPDEFAULT;
- CDC dc;
- dc.Attach(lplvcd->nmcd.hdc);
- GetSubItemRect(iRow, iCol, LVIR_ICON, rc);
- CPoint pt(rc.left , rc.top);
- pt.Offset(5, 5);
- m_ctlImageDelete.Draw(&dc, 0, pt, ILD_TRANSPARENT);
- dc.Detach();
- }
- }
- }
- default:// it wasn't a notification that was interesting to us.
- *pResult = CDRF_DODEFAULT;
- }
- }