PeersListCtrl.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:24k
源码类别:
P2P编程
开发平台:
Visual C++
- // PeersListCtrl.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "PeersListCtrl.h"
- #include "FileDB.h"
- #include "download.h"
- #include "CategoryBarWnd.h"
- #include "FileBase.h"
- #include "SortClass.h"
- #include "MonitorWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define ID_MENUITEM_PAUSE_PEERS 52834
- #define ID_MENUITEM_CONTINUE_PEERS ID_MENUITEM_PAUSE_PEERS+1
- #define ID_MENUITEM_STOP_PEERS ID_MENUITEM_PAUSE_PEERS+2
- #define ID_MENUITEM_SHOW_PEER_INFO ID_MENUITEM_PAUSE_PEERS+3
- /////////////////////////////////////////////////////////////////////////////
- // CPeersListCtrl
- CPeersListCtrl::CPeersListCtrl()
- :CListCtrlBaseEx("peerslistctrl", true)
- {
- m_pFileDBItem = 0;
- m_pSetupRegister = 0;
- m_pMonitorWnd = 0;
- //
- // column infos.
- //
- CString m_strArrToolTip[ePeersColumnCount];
- CString strArrPeersFieldNames[ePeersColumnCount];
- bool bColumnNumeric[ePeersColumnCount];
- m_strArrToolTip[eIcon] = "箭头向右:正在与该客户端建立连接rn箭头向上:正在向该客户端传送数据rn箭头向下:正在从该客户端下载数据rn";
- m_strArrToolTip[eIP] = "客户端的IP地址";
- m_strArrToolTip[eLocale] = "本地主动连接/远程主动连接";
- m_strArrToolTip[eDownRate] = "从客户端下载的速度";
- m_strArrToolTip[eUpRate] = "上传给客户端的速度";
- m_strArrToolTip[eDownCount] = "已经从客户端下载的数据量";
- m_strArrToolTip[eUpCount] = "已经上传给客户端的数据量";
- m_strArrToolTip[eHave] = "在客户端的机器上已经完成下载的比例";
- m_strArrToolTip[eInterested] = "你是否拥有客户端需要的数据";
- m_strArrToolTip[eChoking] = "你是否拒绝传输数据给客户端";
- m_strArrToolTip[eInteresting] = "客户端是否有你需要的数据";
- m_strArrToolTip[eChocked] = "客户端是否拒绝出数数据给你";
- strArrPeersFieldNames[eIcon] = "状态";
- strArrPeersFieldNames[eIP] = "IP";
- strArrPeersFieldNames[eLocale] = "连出/连入";
- strArrPeersFieldNames[eDownRate] = "下载速度";
- strArrPeersFieldNames[eUpRate] = "上传速度";
- strArrPeersFieldNames[eDownCount] = "已下载";
- strArrPeersFieldNames[eUpCount] = "已上传";
- strArrPeersFieldNames[eHave] = "对方已下载";
- strArrPeersFieldNames[eInterested] = "你是否有对方需要的数据";
- strArrPeersFieldNames[eChoking] = "你是否暂时拒绝传输数据给对方对方";
- strArrPeersFieldNames[eInteresting] = "对方是否有你需要的数据";
- strArrPeersFieldNames[eChocked] = "对方是否暂时拒绝传输数据给你";
- for (int i=0; i<ePeersColumnCount; i++)
- bColumnNumeric[i] = true;
- bColumnNumeric[eIcon] = false;
- bColumnNumeric[eIP] = false;
- bColumnNumeric[eDownRate] = true;
- bColumnNumeric[eUpRate] = true;
- bColumnNumeric[eDownCount] = true;
- bColumnNumeric[eUpCount] = true;
- bColumnNumeric[eHave] = false;
- for (i=0; i<ePeersColumnCount; i++)
- AddColumn(strArrPeersFieldNames[i], bColumnNumeric[i], 100, m_strArrToolTip[i]);
- // sort .
- m_iSortedCol = 0; //
- // m_iSortedCol = -1;
- // m_bSortAscending = true;
- m_arrColType[eIcon] = CSortClass::dtSTRING;
- m_arrColType[eIP] = CSortClass::dtSTRING;
- m_arrColType[eLocale] = CSortClass::dtSTRING;
- m_arrColType[eDownRate] = CSortClass::dtSTRING;
- m_arrColType[eUpRate] = CSortClass::dtSTRING;
- m_arrColType[eDownCount] = CSortClass::dtSTRING;
- m_arrColType[eUpCount] = CSortClass::dtSTRING;
- m_arrColType[eHave] = CSortClass::dtINT;
- m_arrColType[eInterested] = CSortClass::dtSTRING;
- m_arrColType[eChoking] = CSortClass::dtSTRING;
- m_arrColType[eInteresting] = CSortClass::dtSTRING;
- m_arrColType[eChocked] = CSortClass::dtSTRING;
- }
- CPeersListCtrl::~CPeersListCtrl()
- {
- }
- // CListCtrlBaseEx
- BEGIN_MESSAGE_MAP(CPeersListCtrl, CListCtrlBaseEx)
- //{{AFX_MSG_MAP(CPeersListCtrl)
- ON_WM_CONTEXTMENU()
- ON_WM_CREATE()
- ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnDeleteitem)
- ON_WM_SIZE()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- // ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
- ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
- ON_COMMAND(ID_EDIT_COPY, OnMenuitemCopy)
- ON_COMMAND(ID_MENUITEM_SELECT_ALL, OnMenuitemSelectAll)
- ON_COMMAND_RANGE(ID_MENUITEM_PAUSE_PEERS, ID_MENUITEM_SHOW_PEER_INFO, OnMenuItemPeers)
- ON_COMMAND_RANGE(ID_MENUITEM_MONITOR_INFORMATION, ID_MENUITEM_MONITOR_LOGS, OnMenuitemMonitorSub)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPeersListCtrl message handlers
- BOOL CPeersListCtrl::CreateEx(CWnd *pParent, CRect rc, long lID, long lParam, CSetupRegister* pSetupRegister, CMonitorWnd* pMonitorWnd)
- {
- assert(pMonitorWnd && pSetupRegister);
- m_pSetupRegister = pSetupRegister;
- m_pMonitorWnd = pMonitorWnd;
- assert(pSetupRegister);
- return Create(WS_VISIBLE|WS_CHILD|WS_BORDER|LVS_REPORT, rc, pParent, lID);
- }
- int CPeersListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrlBaseEx::OnCreate(lpCreateStruct) == -1)
- return -1;
- //
- // ImageList.
- //
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_BITMAP_IMAGELIST_PEER),
- 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));
- // m_ctlImage.Create(16,17, ILC_COLOR8|ILC_MASK, 4, 4);
- // m_ctlImage.Add(AfxGetApp()->LoadIcon(IDI_ICON_ASTERISK1));
- SetImageList(&m_ctlImage, LVSIL_SMALL);
- SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- SetSortColumn(1);
- return 0;
- }
- void CPeersListCtrl::OnSize(UINT nType, int cx, int cy)
- {
- CListCtrlBaseEx::OnSize(nType, cx, cy);
- }
- void CPeersListCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- if (CListCtrlBaseEx::OnContextMenu(pWnd, point))
- return;
- bool bcopy = false, bContinue = false, bPause = false, bClose = false, bShowPeerInfo = false;
- if (GetSelectedCount() == 1)
- bShowPeerInfo = true;
- if (GetSelectedCount())
- bClose = bcopy = true;
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CConncetionData* pData = (CConncetionData*)GetItemData(nItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- if (pData->m_bPaused)
- bContinue = true;
- if (!pData->m_bPaused)
- bPause = true;
- if (bPause && bContinue)
- break;
- }
- CMenu menu_bar;
- menu_bar.CreatePopupMenu();
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_SHOW_PEER_INFO, "显示选中连接的详细信息(&S)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_CONTINUE_PEERS, "继续选中连接(&X)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_PAUSE_PEERS, "暂停选中连接(&P)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_STOP_PEERS, "关闭选中连接(&Q)t");
- menu_bar.AppendMenu(MF_SEPARATOR);
- menu_bar.AppendMenu(MF_STRING, ID_EDIT_COPY, "复制(&C)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_SELECT_ALL, "全选(&A)t");
- menu_bar.EnableMenuItem(ID_MENUITEM_CONTINUE_PEERS, MF_BYCOMMAND |
- (bContinue ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.EnableMenuItem(ID_MENUITEM_PAUSE_PEERS, MF_BYCOMMAND |
- (bPause ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.EnableMenuItem(ID_MENUITEM_STOP_PEERS, MF_BYCOMMAND |
- (bClose ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.EnableMenuItem(ID_EDIT_COPY, MF_BYCOMMAND |
- (bcopy? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.EnableMenuItem(ID_MENUITEM_SHOW_PEER_INFO, MF_BYCOMMAND |
- (bShowPeerInfo? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.AppendMenu(MF_SEPARATOR);
- if (!m_pMonitorWnd)
- {
- assert(false);
- return;
- }
- m_pMonitorWnd->OnContextQuery(menu_bar);
- CPoint posMouse;
- GetCursorPos(&posMouse);
- menu_bar.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,posMouse.x,posMouse.y,this);
- }
- void CPeersListCtrl::OnMenuitemMonitorSub(UINT uID)
- {
- if (!m_pMonitorWnd)
- {
- assert(false);
- return;
- }
- if (uID >= ID_MENUITEM_MONITOR_INFORMATION &&
- uID <= ID_MENUITEM_MONITOR_LOGS)
- {
- m_pMonitorWnd->OnShowNode(uID );
- }
- }
- void CPeersListCtrl::OnColumnSort(int iColumn)
- {
- assert(abs(iColumn) <= ePeersColumnCount && abs(iColumn) > 0);
- m_iSortedCol = iColumn;
- SortItems(Compare1, m_iSortedCol);
- /*
- assert(abs(iColumn) <= ePeersColumnCount && abs(iColumn) > 0);
- m_bSortAscending = iColumn > 0;
- m_iSortedCol = abs(iColumn) - 1;
- CSortClass csc(this, m_iSortedCol);
- csc.Sort(m_bSortAscending, (CSortClass::EDataType)m_arrColType[m_iSortedCol]);
- //*/
- }
- int CALLBACK CPeersListCtrl::Compare1(LPARAM lParam1, LPARAM lParam2, LPARAM
- lParamSort)
- {
- CConncetionData* pItem1 = (CConncetionData*) lParam1;
- CConncetionData* pItem2 = (CConncetionData*) lParam2;
- if (!pItem1 || !pItem2)
- {
- ASSERT(pItem1 && pItem2);
- return 0;
- }
- CConncetionData& conn1 = *pItem1;
- CConncetionData& conn2 = *pItem2;
- long sOrder = lParamSort < 0 ? -1 : 1;
- eColumns eCol = (eColumns)((lParamSort * sOrder) - 1);
- ////////////////////////////////////////////////////////////
- // for dynamic data.
- CStatics statics1, statics2;
- int iStat1 = 0, iStat2 = 0;
- switch (eCol)
- {
- case eIcon:
- {
- int iState1 = 0, iState2 = 0;
- if (conn1.m_lDownRate > 0)
- iState1 = 2;
- else if (conn1.m_lUpRate > 0)
- iState1 = 1;
- if (conn2.m_lDownRate > 0)
- iState2= 2;
- else if (conn2.m_lUpRate > 0)
- iState2 = 1;
- return (iState1 - iState2) * sOrder;
- }
- break;
- case eIP:
- {
- /*
- in_addr inaddress;
- inaddress.S_un.S_addr = conn1.m_lAddr;
- CString str1 = inet_ntoa(inaddress);
- inaddress.S_un.S_addr = conn2.m_lAddr;
- CString str2 = inet_ntoa(inaddress);
- //*/
- ULONG l1 = ntohl(conn1.m_lAddr);
- ULONG l2 = ntohl(conn2.m_lAddr);
- if (l1 == l2) return 0;
- return ((l1 > l2) ? 1 : -1) * sOrder;
- }
- break;
- case eLocale:
- return (conn1.m_bLocallyInitiated- conn2.m_bLocallyInitiated) * sOrder;
- break;
- case eDownRate:
- return (conn1.m_lDownRate - conn2.m_lDownRate) * sOrder;
- break;
- case eUpRate:
- return (conn1.m_lUpRate - conn2.m_lUpRate) * sOrder;
- break;
- case eDownCount:
- return (conn1.m_lDownCount- conn2.m_lDownCount) * sOrder;
- break;
- case eUpCount:
- return (conn1.m_lUpCount - conn2.m_lUpCount) * sOrder;
- break;
- case eHave:
- return (conn1.m_fPeerHavePercent - conn2.m_fPeerHavePercent) * 100 * sOrder;
- break;
- case eInterested:
- return (conn1.m_bInterested - conn2.m_bInterested) * sOrder;
- break;
- case eChoking:
- return (conn1.m_bChoking - conn2.m_bChoking) * sOrder;
- break;
- case eInteresting:
- return (conn1.m_bInteresting - conn2.m_bInteresting) * sOrder;
- break;
- case eChocked:
- return (conn1.m_bChoked - conn2.m_bChoked) * sOrder;
- break;
- default:
- assert(false);
- break;
- }
- return 0;
- }
- /*
- void CPeersListCtrl::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- *pResult = 0;
- int iSubItem = GetLogicalIndex(pNMListView->iSubItem);
- if (iSubItem == -1)
- {
- assert(false);
- return;
- }
- if(iSubItem == m_iSortedCol)
- m_bSortAscending = !m_bSortAscending;
- else
- m_bSortAscending = TRUE;
- m_iSortedCol = iSubItem;
- CSortClass csc(this, m_iSortedCol);
- csc.Sort(m_bSortAscending, (CSortClass::EDataType)m_arrColType[m_iSortedCol]);
- }
- //*/
- void CPeersListCtrl::OnDeleteitem(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- CConncetionData* pData = (CConncetionData*)pNMListView->lParam;;
- if (!pData)
- {
- assert(false);
- return;
- }
- delete pData;
- }
- void CPeersListCtrl::Monitor(CFileDBItem* pFileDBItem)
- {
- m_pFileDBItem = pFileDBItem;
- DeleteAllItems();
- }
- void CPeersListCtrl::ShowAllPeers(bool bRefreshInfo)
- {
- SetRedraw(false);
- _ShowAllPeers(bRefreshInfo);
- SetRedraw(true);
- if (m_PeerInfoWnd.IsMonitoring())
- {
- if (m_pFileDBItem && m_pFileDBItem->m_pDownload && m_pFileDBItem->m_pDownload->IsDownloading())
- {
- bool bFind = false;
- vector<CConncetionData> vConnections = m_pFileDBItem->m_pDownload->GetConnections();
- for (int i=0; i<vConnections.size(); i++)
- {
- if (vConnections[i].m_lAddr == m_PeerInfoWnd.m_conData.m_lAddr &&
- vConnections[i].m_sPort == m_PeerInfoWnd.m_conData.m_sPort)
- {
- bFind = true;
- m_PeerInfoWnd.ShowPeer(vConnections[i]);
- break;
- }
- }
- if (!bFind)
- {
- m_PeerInfoWnd.ClosePeer();
- }
- }
- }
- }
- void CPeersListCtrl::_ShowAllPeers(bool bRefreshInfo)
- {
- if (bRefreshInfo)
- DeleteAllItems();
- if (m_pFileDBItem && m_pFileDBItem->m_pDownload && m_pFileDBItem->m_pDownload->IsDownloading())
- {
- vector<CConncetionData> vConnections = m_pFileDBItem->m_pDownload->GetConnections();
- if (!bRefreshInfo)
- {
- vector<int> arrDelIndex;
- const long lCount = GetItemCount();
- for (int j=0; j<lCount; j++)
- {
- CConncetionData* pData = (CConncetionData*)GetItemData(j);
- if (!pData)
- {
- assert(false);
- for (int k=0; k<lCount; k++)
- {
- pData = (CConncetionData*)GetItemData(k);
- }
- return;
- }
- bool bFind = false;
- for (int i=0; i<vConnections.size(); i++)
- {
- if (vConnections[i].m_lAddr == pData->m_lAddr &&
- vConnections[i].m_sPort == pData->m_sPort)
- {
- if (*pData != vConnections[i])
- {
- RefreshPeerText(j, vConnections[i]);
- *pData = vConnections[i];
- // ShowPeerText(j);
- }
- vConnections.erase(vConnections.begin() + i);
- bFind = true;
- break;
- }
- }
- if (!bFind)
- arrDelIndex.push_back(j);
- }
- for (j=arrDelIndex.size() - 1; j >=0; j--)
- DeleteItem(arrDelIndex[j]);
- }
- char szText[1024] = {0};
- for (int i=0; i<vConnections.size(); i++)
- {
- long lInx = GetItemCount();
- InsertItem(lInx, "");
- CConncetionData* pData = new CConncetionData(vConnections[i]);
- SetItemData(lInx, (long)pData);
- ShowPeerText(lInx);
- }
- }
- }
- void CPeersListCtrl::OnShowColumn(int iItem)
- {
- ShowPeerText(iItem);
- }
- void CPeersListCtrl::RefreshPeerText(int iItem, CConncetionData& Conn)
- {
- CConncetionData* pOldData = (CConncetionData*)GetItemData(iItem);
- if (!pOldData)
- {
- assert(false);
- return;
- }
- CConncetionData& oldConn = *pOldData;
- int iStateOld = 0;
- if (oldConn.m_lDownRate > 0)
- iStateOld = 2;
- else if (oldConn.m_lUpRate > 0)
- iStateOld = 1;
- int iState = 0;
- if (Conn.m_lDownRate > 0)
- iState= 2;
- else if (Conn.m_lUpRate > 0)
- iState = 1;
- if (iStateOld != iState)
- {
- *pOldData = Conn;
- ShowPeerText(iItem);
- return;
- }
- char szText[1024] = {0};
- if (oldConn.m_bLocallyInitiated != Conn.m_bLocallyInitiated)
- {
- sprintf(szText, Conn.m_bLocallyInitiated ? "出" : "入");
- SetItemTextEx(iItem, eLocale, szText);
- }
- if (oldConn.m_bInterested != Conn.m_bInterested)
- SetItemTextEx(iItem, eInterested, Conn.m_bInterested ? "有" : "无");
- if (oldConn.m_bChoking != Conn.m_bChoking)
- SetItemTextEx(iItem, eChoking, Conn.m_bChoking ? "是" : "否");
- if (oldConn.m_bInteresting != Conn.m_bInteresting)
- SetItemTextEx(iItem, eInteresting, Conn.m_bInteresting? "有" : "无");
- if (oldConn.m_bChoked != Conn.m_bChoked)
- SetItemTextEx(iItem, eChocked, Conn.m_bChoked ? "是" : "否");
- if (oldConn.m_lUpRate != Conn.m_lUpRate)
- SetItemTextEx(iItem, eUpRate, FormatSizeK(Conn.m_lUpRate).data());
- if (oldConn.m_lDownRate != Conn.m_lDownRate)
- SetItemTextEx(iItem, eDownRate, FormatSizeK(Conn.m_lDownRate).data());
- if (oldConn.m_lDownCount != Conn.m_lDownCount)
- SetItemTextEx(iItem, eDownCount, FormatSizeM(Conn.m_lDownCount).data());
- if (oldConn.m_lUpCount != Conn.m_lUpCount)
- SetItemTextEx(iItem, eUpCount, FormatSizeM(Conn.m_lUpCount).data());
- if (oldConn.m_fPeerHavePercent != Conn.m_fPeerHavePercent)
- {
- sprintf(szText, "%.0f%%", Conn.m_fPeerHavePercent*100);
- SetItemTextEx(iItem, eHave, szText);
- }
- if (oldConn.m_bPaused != Conn.m_bPaused || oldConn.m_lDownRate != Conn.m_lDownRate)
- {
- if (Conn.m_bPaused)
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_PAUSE, 0, 0, 0);
- else
- {
- if (Conn.m_lDownRate <= 0)
- {
- if (Conn.m_lUpRate <= 0)
- {
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_CONNECTING, 0, 0, 0);
- }
- else
- {
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_UPLOAD, 0, 0, 0);
- }
- }
- else
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_DOWNLOAD, 0, 0, 0);
- }
- }
- }
- void CPeersListCtrl::ShowPeerText(int iItem)
- {
- CConncetionData* pData = (CConncetionData*)GetItemData(iItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- CConncetionData& Conn = *pData;
- char szText[1024] = {0};
- in_addr inaddress;
- inaddress.S_un.S_addr = Conn.m_lAddr;
- sprintf(szText, "%s:%d", inet_ntoa(inaddress), ntohs(Conn.m_sPort));
- SetItemTextEx(iItem, eIP, szText);
- sprintf(szText, Conn.m_bLocallyInitiated ? "出" : "入");
- SetItemTextEx(iItem, eLocale, szText);
- SetItemTextEx(iItem, eIcon, "");
- SetItemTextEx(iItem, eInterested, Conn.m_bInterested ? "有" : "无");
- SetItemTextEx(iItem, eChoking, Conn.m_bChoking ? "是" : "否");
- SetItemTextEx(iItem, eInteresting, Conn.m_bInteresting? "有" : "无");
- SetItemTextEx(iItem, eChocked, Conn.m_bChoked ? "是" : "否");
- SetItemTextEx(iItem, eUpRate, FormatSizeK(Conn.m_lUpRate).data());
- SetItemTextEx(iItem, eDownRate, FormatSizeK(Conn.m_lDownRate).data());
- SetItemTextEx(iItem, eDownCount, FormatSizeM(Conn.m_lDownCount).data());
- SetItemTextEx(iItem, eUpCount, FormatSizeM(Conn.m_lUpCount).data());
- sprintf(szText, "%.0f%%", Conn.m_fPeerHavePercent*100);
- SetItemTextEx(iItem, eHave, szText);
- if (pData->m_bPaused)
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_PAUSE, 0, 0, 0);
- else
- {
- if (pData->m_lDownRate <= 0)
- {
- if (pData->m_lUpRate <= 0)
- {
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_CONNECTING, 0, 0, 0);
- }
- else
- {
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_UPLOAD, 0, 0, 0);
- }
- }
- else
- SetItem(iItem, 0, LVIF_IMAGE, 0, IID_DOWNLOAD, 0, 0, 0);
- }
- }
- void CPeersListCtrl::OnMenuItemPeers(UINT uID)
- {
- if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload || !m_pFileDBItem->m_pDownload->IsDownloading())
- {
- assert(false);
- return;
- }
- switch (uID)
- {
- case ID_MENUITEM_CONTINUE_PEERS:
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CConncetionData* pData = (CConncetionData*)GetItemData(nItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- if (pData->m_bPaused)
- {
- m_pFileDBItem->m_pDownload->PausePeer(CAddrPort(pData->m_lAddr, pData->m_sPort), false);
- pData->m_bPaused = false;
- ShowPeerText(nItem);
- }
- }
- }
- break;
- case ID_MENUITEM_PAUSE_PEERS:
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CConncetionData* pData = (CConncetionData*)GetItemData(nItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- if (!pData->m_bPaused)
- {
- m_pFileDBItem->m_pDownload->PausePeer(CAddrPort(pData->m_lAddr, pData->m_sPort), true);
- pData->m_bPaused = true;
- ShowPeerText(nItem);
- }
- pData = (CConncetionData*)GetItemData(nItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- }
- }
- break;
- case ID_MENUITEM_STOP_PEERS:
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CConncetionData* pData = (CConncetionData*)GetItemData(nItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- m_pFileDBItem->m_pDownload->ClosePeer(CAddrPort(pData->m_lAddr, pData->m_sPort));
- }
- }
- break;
- case ID_MENUITEM_SHOW_PEER_INFO:
- {
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CConncetionData* pData = (CConncetionData*)GetItemData(nItem);
- ShowWindow(false);
- m_PeerInfoWnd.ShowWindow(true);
- m_PeerInfoWnd.ShowPeer(*pData);
- m_PeerInfoWnd.SetFocus();
- }
- }
- break;
- default:
- assert(false);
- }
- }
- void CPeersListCtrl::OnMenuitemCopy()
- {
- CString strSource;
- int nColumnCount = GetHeaderCtrl()->GetItemCount();
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- for (int i=0; i<nColumnCount; i++)
- strSource += GetItemText(index, i) + " ";
- strSource += "rn";
- }
- if (!CopyTextToClipboard(strSource))
- AfxMessageBox("复制失败");
- }
- void CPeersListCtrl::OnMenuitemSelectAll()
- {
- for (int i=0; i<GetItemCount(); i++)
- SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
- SetFocus();
- }
- void CPeersListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
- switch(lplvcd->nmcd.dwDrawStage)
- {
- case CDDS_PREPAINT:
- *pResult = CDRF_NOTIFYSUBITEMDRAW; // ask for subitem notifications.
- *pResult = CDRF_NOTIFYPOSTERASE;
- break;
- case CDDS_ITEMPREERASE:
- {
- int ik = 0;
- }
- break;
- case CDDS_ITEMPREPAINT:
- {
- *pResult = CDRF_DODEFAULT;
- if (!(lplvcd->nmcd.uItemState & CDIS_SELECTED ))
- {
- CConncetionData* pData = (CConncetionData*)lplvcd->nmcd.lItemlParam;
- if (!pData)
- {
- assert(false);
- return;
- }
- CConncetionData& Conn = *pData;
- if (Conn.m_lDownRate > 0)
- {
- lplvcd->clrTextBk = RGB(235, 242, 254);
- }
- else if (Conn.m_lUpRate > 0)
- {
- lplvcd->clrTextBk = RGB(255, 250, 237);
- }
- else
- {
- lplvcd->clrTextBk = RGB(255, 255, 255);
- }
- /*
- CSystemMsg* pData = (CSystemMsg*)lplvcd->nmcd.lItemlParam;
- CSystemMsg& msg = *pData;
- switch (msg.m_eMsgType)
- {
- case CSystemMsg::eMsgOut:
- lplvcd->clrTextBk = RGB(235, 242, 254);
- break;
- case CSystemMsg::eMsgIn:
- lplvcd->clrTextBk = RGB(255, 250, 237); //RGB(230, 230, 250); // 175, 238, 238
- break;
- case CSystemMsg::eCmd:
- lplvcd->clrTextBk = RGB(235, 242, 254);
- break;
- case CSystemMsg::eBad:
- lplvcd->clrTextBk = RGB(254, 238, 231);
- break;
- default:
- assert(false);
- return;
- }
- //*/
- *pResult = CDRF_NEWFONT;
- }
- //*pResult = CDRF_NOTIFYSUBITEMDRAW; // ask for subitem notifications.
- }
- break;
- case CDDS_ITEMPREPAINT|CDDS_SUBITEM: // recd when CDRF_NOTIFYSUBITEMDRAW is returned in
- { // response to CDDS_ITEMPREPAINT.
- *pResult = CDRF_DODEFAULT;
- int iCol = lplvcd->iSubItem;
- int iRow = lplvcd->nmcd.dwItemSpec;
- break;
- }
- default:// it wasn't a notification that was interesting to us.
- *pResult = CDRF_DODEFAULT;
- }
- }
- void CPeersListCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- POSITION pos = GetFirstSelectedItemPosition();
- if (pos)
- {
- int nItem = GetNextSelectedItem(pos);
- CConncetionData* pData = (CConncetionData*)GetItemData(nItem);
- ShowWindow(false);
- m_PeerInfoWnd.ShowWindow(true);
- m_PeerInfoWnd.ShowPeer(*pData);
- m_PeerInfoWnd.SetFocus();
- }
- CListCtrlBaseEx::OnLButtonDblClk(nFlags, point);
- }
- void CPeersListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CListCtrlBaseEx::OnLButtonDown(nFlags, point);
- }
- void CPeersListCtrl::OnMenuItemPeerInfo(const UINT uID, const CConncetionData& conData )
- {
- if (!m_pFileDBItem)
- {
- assert(false);
- return;
- }
- switch (uID)
- {
- case ID_MENUITEM_CONTINUE_PEERS:
- {
- m_pFileDBItem->m_pDownload->PausePeer(CAddrPort(conData.m_lAddr, conData.m_sPort), false);
- }
- break;
- case ID_MENUITEM_PAUSE_PEERS:
- {
- m_pFileDBItem->m_pDownload->PausePeer(CAddrPort(conData.m_lAddr, conData.m_sPort), true);
- }
- break;
- case ID_MENUITEM_STOP_PEERS:
- {
- m_pFileDBItem->m_pDownload->ClosePeer(CAddrPort(conData.m_lAddr, conData.m_sPort));
- }
- break;
- default:
- {
- assert(false);
- return;
- }
- }
- }