InfomationList.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:14k
源码类别:
P2P编程
开发平台:
Visual C++
- // InfomationList.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "InfomationList.h"
- #include "FileDB.h"
- #include "FileBase.h"
- #include "StorageWrapper.h"
- #include "SetupRegister.h"
- #include "MonitorWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CInfomationList
- CInfomationList::CInfomationList():
- CListCtrlBaseEx("InfomationList", false)
- {
- //
- // column infos.
- //
- CString strFieldNames[eColumnCount];
- bool bColumnNumeric[eColumnCount];
- strFieldNames[eTime] = "参数";
- strFieldNames[eInfo] = "值";
- bColumnNumeric[eTime] = false;
- bColumnNumeric[eInfo] = false;
- AddColumn(strFieldNames[0], bColumnNumeric[0]);
- AddColumn(strFieldNames[1], bColumnNumeric[1], 1000);
- m_pFileDBItem = 0;
- m_pSetupRegister = 0;
- m_pMonitorWnd = 0;
- m_lBasicInfoInx = 0;
- m_lRunInfoInx = 8;
- m_lRunParamInx = 17;
- }
- CInfomationList::~CInfomationList()
- {
- }
- BEGIN_MESSAGE_MAP(CInfomationList, CListCtrlBaseEx)
- //{{AFX_MSG_MAP(CInfomationList)
- ON_WM_CREATE()
- ON_WM_CONTEXTMENU()
- //}}AFX_MSG_MAP
- ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
- ON_COMMAND(ID_EDIT_COPY, OnMenuitemCopy)
- ON_COMMAND(ID_MENUITEM_SELECT_ALL, OnMenuitemSelectAll)
- ON_COMMAND_RANGE(ID_MENUITEM_MONITOR_INFORMATION, ID_MENUITEM_MONITOR_LOGS, OnMenuitemMonitorSub)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CInfomationList message handlers
- BOOL CInfomationList::CreateEx(CWnd *pParent, CRect rc, long lID, long lParam, CSetupRegister* pSetupRegister, CMonitorWnd* pMonitorWnd)
- {
- assert(pSetupRegister);
- m_pSetupRegister = pSetupRegister;
- m_pMonitorWnd = pMonitorWnd;
- return Create(WS_CHILD|WS_BORDER|WS_VISIBLE|WS_VSCROLL|LVS_REPORT, rc, pParent, lID);
- }
- int CInfomationList::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrlBaseEx::OnCreate(lpCreateStruct) == -1)
- return -1;
- 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);
- // show title.
- long lBasicInfoInx = m_lBasicInfoInx;
- InsertItem(lBasicInfoInx, "文件信息", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx , "保存文件名", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx , "torrent文件名", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx , "认证码(hash)", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx , "文件大小", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx, "创建时间", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx , "已完成", eASTERISK);
- lBasicInfoInx ++;
- InsertItem(lBasicInfoInx, "", -1);
- lBasicInfoInx ++;
- long lRunInfoInx = m_lRunInfoInx;
- InsertItem(lRunInfoInx, "运行信息:", eASTERISK);
- InsertItem(lRunInfoInx + 1, "当前状态:", eASTERISK);
- InsertItem(lRunInfoInx + 2, "下载速度:", eASTERISK);
- InsertItem(lRunInfoInx + 3, "上传速度:", eASTERISK);
- InsertItem(lRunInfoInx + 4, "剩余时间:", eASTERISK);
- InsertItem(lRunInfoInx + 5, "已运行时间:", eASTERISK);
- InsertItem(lRunInfoInx + 6, "种子/连接数:", eASTERISK);
- InsertItem(lRunInfoInx + 7, "绑定端口:", eASTERISK);
- InsertItem(lRunInfoInx + 8, "", -1);
- long lRunParamInx = m_lRunParamInx;
- InsertItem(lRunParamInx, "运行参数:", eASTERISK);
- InsertItem(lRunParamInx + 1, "最小下载者数:", eASTERISK);
- InsertItem(lRunParamInx + 2, "最大下载者数:", eASTERISK);
- InsertItem(lRunParamInx + 3, "显示间隔:", eASTERISK);
- InsertItem(lRunParamInx + 4, "文件安置模式:", eASTERISK);
- InsertItem(lRunParamInx + 5, "最大打开文件:", eASTERISK);
- InsertItem(lRunParamInx + 6, "最大上传速度:", eASTERISK);
- InsertItem(lRunParamInx + 7, "最大上传连接:", eASTERISK);
- return 0;
- }
- void CInfomationList::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- // if (CListCtrlBaseEx::OnContextMenu(pWnd, point))
- // return;
- CRect rc;
- GetClientRect(&rc);
- ClientToScreen(rc);
- if (!rc.PtInRect(point))
- {
- CWnd::OnContextMenu(pWnd, point);
- return;
- }
- bool bcopy = false;
- if (GetSelectedCount())
- bcopy = true;
- CMenu menu_bar;
- menu_bar.CreatePopupMenu();
- 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_EDIT_COPY, MF_BYCOMMAND |
- (bcopy? 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 CInfomationList::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 CInfomationList::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 CInfomationList::OnMenuitemSelectAll()
- {
- for (int i=0; i<GetItemCount(); i++)
- SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
- SetFocus();
- }
- void CInfomationList::OnShowColumn(int iItem)
- {
- // ShowMsgText(iItem);
- }
- void CInfomationList::Monitor(CFileDBItem* pFileDBItem)
- {
- m_pFileDBItem = pFileDBItem;
- for (int i=0; i<GetItemCount(); i++)
- SetItemText(i, 1, "");
- }
- void CInfomationList::ShowInfomation(bool bRefreshInfo)
- {
- if (bRefreshInfo)
- {
- for (int i=0; i<GetItemCount(); i++)
- SetItemText(i, 1, "");
- // DeleteAllItems();
- }
- if (!m_pFileDBItem) return;
- long lBasicInfoInx = m_lBasicInfoInx;
- long lCompleteInx = lBasicInfoInx + 6;
- if (bRefreshInfo)
- {
- // InsertItem(lBasicInfoInx, "文件信息", eASTERISK);
- lBasicInfoInx ++;
- // InsertItem(lBasicInfoInx , "保存文件名", eASTERISK);
- SetItemText(lBasicInfoInx, 1, m_pFileDBItem->m_strFileName.data());
- lBasicInfoInx ++;
- // InsertItem(lBasicInfoInx , "torrent文件名", eASTERISK);
- SetItemText(lBasicInfoInx, 1, m_pFileDBItem->m_strTorrentFileName.data());
- lBasicInfoInx ++;
- // InsertItem(lBasicInfoInx , "认证码(hash)", eASTERISK);
- SetItemText(lBasicInfoInx, 1, m_pFileDBItem->m_strHash.data());
- lBasicInfoInx ++;
- // InsertItem(lBasicInfoInx , "文件大小", eASTERISK);
- SetItemText(lBasicInfoInx, 1, FormatSize(m_pFileDBItem->m_lFileSize).data());
- lBasicInfoInx ++;
- CTime tCreatedTime(m_pFileDBItem->m_tTime);
- // InsertItem(lBasicInfoInx, "创建时间", eASTERISK);
- SetItemText(lBasicInfoInx, 1, tCreatedTime.Format("%Y-%m-%d %H:%M"));
- lBasicInfoInx ++;
- CString strText;
- strText.Format("%d%%", (long)((m_pFileDBItem->m_fComplete)*100));
- SetItemText(lCompleteInx, 1, strText);
- }
- long lRunInfoInx = m_lRunInfoInx;
- if (m_pFileDBItem && m_pFileDBItem->m_pDownload && m_pFileDBItem->m_pDownload->IsDownloading())
- {
- // the download is terminated.
- CString strStatus;
- if (m_pFileDBItem ->m_pDownload->IsBad())
- {
- if (bRefreshInfo || m_eRunState != eBad)
- {
- strStatus = "错误";
- SetItemText(lRunInfoInx + 1, 1, strStatus);
- SetItemText(lRunInfoInx + 2, 1, "");
- SetItemText(lRunInfoInx + 3, 1, "");
- SetItemText(lRunInfoInx + 4, 1, "");
- SetItemText(lRunInfoInx + 5, 1, "");
- SetItemText(lRunInfoInx + 6, 1, "");
- SetItemText(lRunInfoInx + 7, 1, "");
- }
- m_eRunState = eBad;
- }
- else if (m_pFileDBItem->m_pDownload->IsPause())
- {
- strStatus = "暂停 ";
- SetItemText(lRunInfoInx + 1, 1, strStatus);
- m_eRunState = ePause;
- }
- else if (!m_pFileDBItem->m_pDownload->IsDownloading())// download maybe not begin yet.
- {
- if (bRefreshInfo || m_eRunState != eWait)
- {
- strStatus = "等待";
- SetItemTextEx(lRunInfoInx + 1, 1, strStatus);
- SetItemTextEx(lRunInfoInx + 2, 1, "");
- SetItemTextEx(lRunInfoInx + 3, 1, "");
- SetItemTextEx(lRunInfoInx + 4, 1, "");
- SetItemTextEx(lRunInfoInx + 5, 1, "");
- SetItemTextEx(lRunInfoInx + 6, 1, "");
- SetItemTextEx(lRunInfoInx + 7, 1, "");
- }
- m_eRunState = eWait;
- }
- else
- {
- strStatus = "正在运行 ";
- m_eRunState = eRun;
- }
- if (m_eRunState == eRun || (m_eRunState == ePause && bRefreshInfo))
- {
- CStatics statics = m_pFileDBItem->m_pDownload->GetStatics();
- char szText[1024] = {0};
- if (statics.m_bAllocating)
- {
- sprintf(szText, "分配空间(%.2f)", statics.m_fAllocatingPercent);
- strStatus += szText;
- SetItemText(lRunInfoInx + 1, 1, strStatus);
- if (bRefreshInfo || m_eRunState != eAllocate)
- {
- SetItemTextEx(lRunInfoInx + 2, 1, "");
- SetItemTextEx(lRunInfoInx + 3, 1, "");
- SetItemTextEx(lRunInfoInx + 4, 1, "");
- SetItemTextEx(lRunInfoInx + 5, 1, "");
- SetItemTextEx(lRunInfoInx + 6, 1, "");
- SetItemTextEx(lRunInfoInx + 7, 1, "");
- }
- m_eRunState = eAllocate;
- }
- else if (statics.m_bStorageChecking)
- {
- sprintf(szText, "Checking(%.2f)", statics.m_fCheckingPercent);
- strStatus += szText;
- SetItemText(lRunInfoInx + 1, 1, strStatus);
- if (bRefreshInfo || m_eRunState != eCheck)
- {
- SetItemTextEx(lRunInfoInx + 2, 1, "");
- SetItemTextEx(lRunInfoInx + 3, 1, "");
- SetItemTextEx(lRunInfoInx + 4, 1, "");
- SetItemTextEx(lRunInfoInx + 5, 1, "");
- SetItemTextEx(lRunInfoInx + 6, 1, "");
- SetItemTextEx(lRunInfoInx + 7, 1, "");
- }
- m_eRunState = eCheck;
- }
- else
- {
- if (m_eRunState != ePause)
- {
- SetItemText(lRunInfoInx + 1, 1, strStatus);
- }
- CString strText;
- strText.Format("%d%%", (long)((statics.m_fCompleted)*100));
- SetItemText(lCompleteInx, 1, strText);
- sprintf(szText, "%s", FormatSize(statics.m_lDownRate).data());
- SetItemText(lRunInfoInx + 2, 1, szText);
- sprintf(szText, "%s", FormatSize(statics.m_lUpRate).data());
- SetItemText(lRunInfoInx + 3, 1, szText);
- if (statics.m_tTimeLeft < 0)
- {
- SetItemText(lRunInfoInx + 4, 1, "-");
- }
- else
- {
- CTimeSpan TimeLeft(statics.m_tTimeLeft);
- SetItemText(lRunInfoInx + 4, 1, TimeLeft.Format("%D:%H:%M:%S"));
- }
- CTimeSpan TimeRunned(statics.m_tTimeRunned);
- SetItemText(lRunInfoInx + 5, 1, TimeRunned.Format("%D:%H:%M:%S"));
- sprintf(szText, "%d/%d", statics.m_lSeedsCount, statics.m_lPeersCount);
- SetItemText(lRunInfoInx + 6, 1, szText);
- long lPort = 0, lPeerMin = 0, lPeerMax = 0, lDisplayItval = 0, lAllocType = 0, lFileOpenMax =0;
- m_pFileDBItem->m_pDownload->GetRunParameter(lPort,lPeerMin, lPeerMax, lDisplayItval, lAllocType, lFileOpenMax);
- sprintf(szText, "%d", lPort);
- SetItemText(lRunInfoInx + 7, 1, 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);
- //*/
- }
- }
- }
- long lRunParamInx = m_lRunParamInx;
- if (bRefreshInfo)
- {
- CString strText;
- long lPort = 0, lPeerMin = 0, lPeerMax = 0, lDisplayItval = 0, lAllocType = 0, lFileOpenMax =0;
- if (m_pFileDBItem && m_pFileDBItem->m_pDownload && m_pFileDBItem->m_pDownload->IsDownloading())
- {
- m_pFileDBItem->m_pDownload->GetRunParameter(lPort,lPeerMin, lPeerMax, lDisplayItval, lAllocType, lFileOpenMax);
- }
- else
- {
- if (m_pSetupRegister)
- {
- lPeerMin = m_pSetupRegister->m_lPeersMin;
- lPeerMax = m_pSetupRegister->m_lPeersMax;
- lDisplayItval = m_pSetupRegister->m_lDisplayItval;
- lAllocType = m_pSetupRegister->m_eAllocType;
- lFileOpenMax = m_pSetupRegister->m_lFileOpenMax;
- }
- }
- // lPeerMax = m_pFileDBItem->m_lUploadPeerMax;
- // m_pFileDBItem->m_
- strText.Format("%d", lPeerMin);
- SetItemText(lRunParamInx + 1, 1, strText);
- strText.Format("%d", lPeerMax);
- SetItemText(lRunParamInx + 2, 1, strText);
- strText.Format("%d微秒", lDisplayItval);
- SetItemText(lRunParamInx + 3, 1, strText);
- switch (lAllocType)
- {
- case CStorageWrapperEx::eAllocNormal:
- strText = "普通分配";
- break;
- case CStorageWrapperEx::eAllocSparse:
- strText = "特殊分配";
- break;
- case CStorageWrapperEx::eAllocPreAllocate:
- strText = "预分配";
- break;
- case CStorageWrapperEx::eAllocBackGound:
- strText = "后台分配";
- break;
- }
- SetItemText(lRunParamInx + 4, 1, strText);
- strText.Format("%d", lFileOpenMax);
- SetItemText(lRunParamInx + 5, 1, strText);
- strText.Format("%d(K)", m_pFileDBItem->m_lUploadSpeedMax);
- SetItemText(lRunParamInx + 6, 1, strText);
- strText.Format("%d", m_pFileDBItem->m_lUploadPeerMax);
- SetItemText(lRunParamInx + 7, 1, strText);
- }
- }
- void CInfomationList::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
- switch(lplvcd->nmcd.dwDrawStage)
- {
- case CDDS_PREPAINT:
- *pResult = CDRF_NOTIFYSUBITEMDRAW; // ask for subitem notifications.
- break;
- case CDDS_ITEMPREPAINT:
- {
- *pResult = CDRF_DODEFAULT;
- if (lplvcd->nmcd.dwItemSpec == m_lBasicInfoInx)
- {
- lplvcd->clrTextBk = RGB(235, 242, 254);
- }
- else if (lplvcd->nmcd.dwItemSpec == m_lRunInfoInx)
- {
- lplvcd->clrTextBk = RGB(235, 242, 254);
- }
- else if (lplvcd->nmcd.dwItemSpec == m_lRunParamInx)
- {
- lplvcd->clrTextBk = RGB(235, 242, 254);
- }
- else
- {
- return;
- }
- *pResult = CDRF_NEWFONT;
- }
- break;
- }
- }