SubFilesWnd.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:17k
源码类别:
P2P编程
开发平台:
Visual C++
- // SubFilesWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "SubFilesWnd.h"
- #include "FileTorrent.h"
- #include "CategoryBarWnd.h"
- #include "FileBase.h"
- #include "MonitorWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define WM_LOAD_COMPLETED WM_USER+1020
- #define ID_MENUITEM_DOWNLOAD_FILE 33528
- #define ID_MENUITEM_DELETE_FILE ID_MENUITEM_DOWNLOAD_FILE +1
- /////////////////////////////////////////////////////////////////////////////
- // CSubFilesWnd
- CSubFilesWnd::CSubFilesWnd()
- {
- m_pFileTorrent = 0;
- m_pCategoryBarWnd = 0;
- m_hFileOpenDone = CreateEvent(0, true, true, 0);
- m_hCancel = CreateEvent(0, true, false, 0);
- m_pFileDBItem = 0;
- m_pMonitorWnd = 0 ;
- m_lCount = 0;
- m_bMonitor = false;
- }
- CSubFilesWnd::~CSubFilesWnd()
- {
- CloseHandle(m_hFileOpenDone);
- CloseHandle(m_hCancel);
- }
- BEGIN_MESSAGE_MAP(CSubFilesWnd, CTreeCtrlEx)
- //{{AFX_MSG_MAP(CSubFilesWnd)
- ON_WM_CREATE()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_RBUTTONDOWN()
- //}}AFX_MSG_MAP
- // ON_WM_CONTEXTMENU()
- ON_NOTIFY_REFLECT(NM_RCLICK,OnRightClick)
- ON_COMMAND(ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, OnOpenDirectory)
- ON_MESSAGE(WM_LOAD_COMPLETED, OnLoadSubfilesCompleted)
- ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
- ON_COMMAND_RANGE(ID_MENUITEM_DOWNLOAD_FILE, ID_MENUITEM_DELETE_FILE, OnMenuStartFile)
- ON_COMMAND_RANGE(ID_MENUITEM_MONITOR_INFORMATION, ID_MENUITEM_MONITOR_LOGS, OnMenuitemMonitorSub)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CSubFilesWnd message handlers
- BOOL CSubFilesWnd::CreateEx(CWnd *pParent, CRect &rc, long lParam, bool bMonitor, CMonitorWnd* pMonitorWnd)
- {
- m_bMonitor = bMonitor;
- m_pMonitorWnd = pMonitorWnd;
- return Create(WS_VISIBLE|WS_CHILD|WS_BORDER|TVS_HASBUTTONS | TVS_LINESATROOT |
- TVS_HASLINES|TVS_SHOWSELALWAYS,
- rc, pParent, lParam);
- }
- int CSubFilesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CTreeCtrlEx::OnCreate(lpCreateStruct) == -1)
- return -1;
- Init();
- return 0;
- }
- void CSubFilesWnd::Init()
- {
- assert(GetSafeHwnd());
- m_ctlImage.Create(16, 17, ILC_COLOR8|ILC_MASK, 4, 4);
- m_ctlImage.Add(AfxGetApp()->LoadIcon(IDI_ICON_DELETE));
- SHFILEINFO ssfi;
- CImageList smallSystemImageList;
- smallSystemImageList.Attach((HIMAGELIST) SHGetFileInfo(_T("c:\"), 0, &ssfi, sizeof(SHFILEINFO), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_SMALLICON));
- SetImageList(&smallSystemImageList);
- smallSystemImageList.Detach();
- }
- void CSubFilesWnd::SetCateGoryBarWnd(CCategoryBarWnd* pCategoryBarWnd)
- {
- m_pCategoryBarWnd = pCategoryBarWnd;
- }
- /******************************************************************************
- parameter :
- strTorrentFileName : torrentFileName if empty then the monitor close.
- strFileName : Empty mean the torrent have not been saved. the param used
- for context menu.
- ******************************************************************************/
- void CSubFilesWnd::Monitor(string strTorrentFileName, string strFileName, CFileDBItem* pFileDBItem)
- {
- m_pFileTorrent = 0;
- m_strFileName = strFileName;
- m_strTorrentFileName = strTorrentFileName;
- m_pFileDBItem = pFileDBItem;
- }
- void CSubFilesWnd::Monitor( CFileTorrent* pFileTorrent, string strFileName, CFileDBItem* pFileDBItem)
- {
- m_strFileName = strFileName;
- m_strTorrentFileName = string();
- m_pFileTorrent = pFileTorrent;
- m_pFileDBItem = pFileDBItem;
- }
- void CSubFilesWnd::Stop()
- {
- if (!IsEventSet(m_hFileOpenDone))
- {
- SetEvent(m_hCancel);
- WaitForSingleObject(m_hFileOpenDone, INFINITE);
- }
- }
- void CSubFilesWnd::OnLoadSubfilesCompleted(long lParam, long wParam)
- {
- }
- void CSubFilesWnd::ShowSubFiles()
- {
- SetRedraw(false);
- LoadSubFiles();
- SetRedraw(true);
- /*
- ResetEvent(m_hFileOpenDone);
- ResetEvent(m_hCancel);
- DWORD dwThreadId = 0;
- ::CreateThread(0, 0, ShowSubFilesProc, this, 0, &dwThreadId);
- //*/
- }
- DWORD WINAPI CSubFilesWnd::ShowSubFilesProc(void *pParam)
- {
- CSubFilesWnd* pSubFilesWnd = (CSubFilesWnd*) pParam;
- HWND hWnd = pSubFilesWnd->GetSafeHwnd();
- pSubFilesWnd->LoadSubFiles();
- ::PostMessage(hWnd, WM_LOAD_COMPLETED, 0, 0);
- return 0;
- }
- void CSubFilesWnd::LoadSubFiles()
- {
- DeleteAllItems();
- if (!m_pFileTorrent && m_strTorrentFileName.empty())
- {
- // assert(false);
- return;
- }
- // get files.
- // subfiles
- bool bDir;
- BLONG lTotalLength = 0;
- vector<CFileInfo> vFiles;
- if (m_pFileTorrent)
- {
- try
- {
- m_pFileTorrent->QuerySubFiles(vFiles, bDir, lTotalLength);
- }
- catch (string& e)
- {
- GetRootItem().AddTail((string("*error") + e).data(), GetIconIndex("error", true));
- return;
- }
- }
- else
- {
- CFileTorrent fileTorrent;
- try
- {
- assert(m_pCategoryBarWnd);
- if (!fileTorrent.OpenFile(m_pCategoryBarWnd->GetDBSavePath() + m_strTorrentFileName))
- {
- GetRootItem().AddTail((string("*error") + fileTorrent.GetBadMsg()).data(), GetIconIndex("error", true));
- return;
- }
- fileTorrent.QuerySubFiles(vFiles, bDir, lTotalLength);
- }
- catch (string& e)
- {
- GetRootItem().AddTail((string("*error") + e).data(), GetIconIndex("error", true));
- return;
- }
- }
- int iCount = 0;
- for (int i=0; i<vFiles.size(); i++)
- {
- CTreeCursor curRoot(0, this);
- CString strFilePath = vFiles[i].m_strFilePath.data();
- for (int j=0; ; j++)
- {
- int iFind = strFilePath.Find('\');
- if (iFind == -1)
- break;
- CString strDir = strFilePath.Left(iFind);
- strFilePath = strFilePath.Right(strFilePath.GetLength() - iFind - 1);
- if (strDir.IsEmpty())
- continue;
- curRoot = AddItem(strDir, curRoot, iCount, true, false);
- assert(curRoot);
- }
- // add size to file.
- CString strSize = FormatSize(vFiles[i].m_lFileLength, false).data();
- if (!strFilePath.IsEmpty())
- {
- strFilePath += " (" + strSize + ')';
- AddItem(strFilePath, curRoot, iCount, false, vFiles[i].m_lFileLength <= 0);
- if (vFiles[i].m_lFileLength > 0)
- iCount ++;
- }
- }
- if (GetRootItem().HasChildren())
- {
- CString strSize = FormatSize(lTotalLength, false).data();
- GetRootItem().SetText(GetRootItem().GetText() + " (共" + strSize + ')');
- }
- if (m_pFileDBItem && GetRootItem())
- TravelThrough(GetRootItem());
- GetRootItem().Expand();
- }
- CTreeCursor CSubFilesWnd::AddItem(CString strItem, CTreeCursor curLayer, int iInx, bool bDir, bool bSizeZero)
- {
- if (curLayer.HasChildren())
- {
- CTreeCursor curChild = curLayer.GetChild();
- while (curChild)
- {
- if (curChild.GetText() == strItem)
- return curChild;
- curChild = curChild.GetNext(TVGN_NEXT);
- }
- }
- CString strTrue = strItem.Left(strItem.ReverseFind('('));
- strTrue.TrimRight();
- CTreeCursor curItem = curLayer.AddTail(strItem, GetIconIndex(strTrue.GetBuffer(0), bDir));
- //
- // cal if the item deleted.
- //
- bool bDelete = false;
- if (m_pFileDBItem && !bDir)
- {
- for (int i=0; i<m_pFileDBItem->m_vUnneededFileInx.size(); i++)
- {
- if (m_pFileDBItem->m_vUnneededFileInx[i].cy <= iInx)
- continue;
- if (m_pFileDBItem->m_vUnneededFileInx[i].cx <= iInx)
- bDelete = true;
- break;
- }
- }
- curItem.SetState(INDEXTOSTATEIMAGEMASK(bDelete ? 2 : 1), TVIS_STATEIMAGEMASK);
- if (bSizeZero)
- curItem.SetData(eSizeZero);
- else
- curItem.SetData(eNormal);
- return curItem;
- }
- int CSubFilesWnd::GetIconIndex(char *filename, BOOL bDirectory)
- {
- SHFILEINFO ssfi;
- DWORD attr;
- if (bDirectory)
- attr = FILE_ATTRIBUTE_DIRECTORY;
- else
- attr = FILE_ATTRIBUTE_NORMAL;
- SHGetFileInfo(filename, attr, &ssfi, sizeof(SHFILEINFO), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
- return ssfi.iIcon;
- }
- void CSubFilesWnd::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- if (m_strFileName.empty() || (!m_pFileTorrent && m_strTorrentFileName.empty()))
- return;
- CTreeCtrlEx::OnLButtonDblClk(nFlags, point);
- m_ItemSel = GetSelectedItem();
- if (m_ItemSel && !m_ItemSel.HasChildren())
- OnOpenDirectory();
- }
- void CSubFilesWnd::OnRightClick(NMHDR *pNotifyStruct,LRESULT *result)
- {
- *result = 0;
- // if (m_strFileName.empty() || (!m_pFileTorrent && m_strTorrentFileName.empty()))
- // return;
- bool bStart = false, bDelete = false, bOpen = false;
- CMenu menu_bar;
- menu_bar.CreatePopupMenu();
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, "打开该文件/文件夹(&O)t");
- menu_bar.AppendMenu(MF_SEPARATOR);
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_DOWNLOAD_FILE, "继续下载该文件/文件夹(&X)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_DELETE_FILE, "停止下载该文件/文件夹(&D)t");
- if (m_pFileTorrent || !m_strTorrentFileName.empty())
- {
- UINT nFlags;
- CPoint curPoint;
- GetCursorPos(&curPoint);
- ScreenToClient(&curPoint);
- m_ItemSel = HitTest(curPoint, &nFlags);
- if (m_ItemSel)
- {
- m_ItemSel.Select();
- bOpen = !m_strFileName.empty();
- bDelete = !GetCheck(m_ItemSel);
- bStart = !bDelete ;
- if (m_ItemSel.GetImageID() == GetIconIndex("a", true))
- bStart = true;
- }
- }
- if (m_pFileDBItem)
- {
- if (m_pFileDBItem->m_pDownload &&
- m_pFileDBItem->m_pDownload->IsDownloading() &&
- !m_pFileDBItem->m_pDownload->IsBad() )
- {
- if (!m_pFileDBItem->m_pDownload->CanSetRunParameters())
- bStart = false, bDelete = false, bOpen = false;
- }
- }
- menu_bar.EnableMenuItem(ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, MF_BYCOMMAND |
- (bOpen ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.EnableMenuItem(ID_MENUITEM_DOWNLOAD_FILE, MF_BYCOMMAND |
- (bStart ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.EnableMenuItem(ID_MENUITEM_DELETE_FILE, MF_BYCOMMAND |
- (bDelete ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- if (m_bMonitor)
- {
- // menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_DELETE_FILE, "删除(&D)t");
- 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 CSubFilesWnd::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 CSubFilesWnd::OnOpenDirectory()
- {
- if ((!m_pFileTorrent && m_strTorrentFileName.empty()) || m_strFileName.empty())
- {
- assert(false);
- return;
- }
- if (!m_ItemSel) return;
- CString strSavepath = m_strFileName.data();
- if (strSavepath.IsEmpty())
- {
- assert(false);
- return;
- }
- if (strSavepath[strSavepath.GetLength() - 1] == '\')
- strSavepath = strSavepath.Left(strSavepath.GetLength() - 1);
- strSavepath = strSavepath.Left(strSavepath.ReverseFind('\') + 1);
- CString strCommand;
- CTreeCursor cur = m_ItemSel;
- while (cur)
- {
- CString strText = cur.GetText();
- if (cur == GetRootItem())
- {
- strText = strText.Left(strText.ReverseFind('('));
- strText.TrimRight();
- }
- if (strCommand.IsEmpty())
- strCommand = strText;
- else
- strCommand = strText + '\' + strCommand;
- cur = cur.GetParent();
- }
- if (m_ItemSel != GetRootItem() && !m_ItemSel.HasChildren())
- {
- int iFind = strCommand.ReverseFind('(');
- if (iFind >= 0)
- {
- strCommand = strCommand.Left(iFind);
- strCommand.TrimRight();
- }
- }
- strCommand = formatDir(strSavepath.GetBuffer(0)).data() + strCommand;
- HINSTANCE iRet = ShellExecute(NULL, "open" , strCommand, 0, 0, SW_SHOW);
- if ((UINT)iRet <= HINSTANCE_ERROR)
- AfxMessageBox(WSAShowError().data());
- }
- /*****************************************************************
- switch open and close mode.
- *****************************************************************/
- void CSubFilesWnd::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMTVCUSTOMDRAW lplvcd = (LPNMTVCUSTOMDRAW)pNMHDR;
- switch(lplvcd->nmcd.dwDrawStage)
- {
- case CDDS_PREPAINT:
- *pResult = CDRF_NOTIFYITEMDRAW; // ask for subitem notifications.
- break;
- case CDDS_ITEMPREPAINT:
- {
- *pResult = CDRF_NOTIFYPOSTPAINT;
- }
- break;
- case CDDS_ITEMPOSTPAINT:
- {
- CDC dc;
- dc.Attach( lplvcd->nmcd.hdc );
- if(GetCheck((HTREEITEM)lplvcd->nmcd.dwItemSpec))
- {
- RECT area ; //= pCustomDraw->nmcd.rc;
- BOOL bRet = GetItemRect((HTREEITEM)lplvcd->nmcd.dwItemSpec, &area, true);
- if (bRet)
- {
- CPoint pt(area.left , area.top);
- pt.Offset(-30, 0);
- m_ctlImage.Draw(&dc, 0, pt, ILD_TRANSPARENT);
- ASSERT(bRet);
- }
- }
- dc.Detach();
- *pResult = CDRF_DODEFAULT;
- }
- default:
- *pResult = CDRF_DODEFAULT;
- }
- }
- void CSubFilesWnd::OnMenuStartFile(UINT nID)
- {
- switch (nID)
- {
- case ID_MENUITEM_DOWNLOAD_FILE:
- {
- if (!m_ItemSel) return;
- SetItemCheck(m_ItemSel, false);
- }
- break;
- case ID_MENUITEM_DELETE_FILE:
- {
- if (!m_ItemSel) return;
- SetItemCheck(m_ItemSel, true);
- }
- break;
- }
- }
- void CSubFilesWnd::SetItemCheck(CTreeCursor curItem, BOOL bCheck)
- {
- SetCheck(curItem, bCheck);
- SetCheck(curItem, bCheck);
- TravelChild(curItem, bCheck);
- TravelSiblingAndParent(curItem);
- if (m_pFileDBItem)
- {
- GetChecks();
- m_pFileDBItem->m_vUnneededFileInx = m_vChecks;
- if (m_pFileDBItem->m_pDownload &&
- m_pFileDBItem->m_pDownload->IsDownloading() &&
- !m_pFileDBItem->m_pDownload->IsBad() &&
- m_pFileDBItem->m_pDownload->CanSetRunParameters())
- {
- m_pFileDBItem->m_pDownload->SetUnNeededFilesInxs(m_pFileDBItem->m_vUnneededFileInx);
- }
- }
- }
- void CSubFilesWnd::TravelChild(HTREEITEM hItem, BOOL bCheck)
- {
- HTREEITEM hChildItem = GetChildItem(hItem);
- while (hChildItem)
- {
- BOOL bChecked = GetCheck(hChildItem);
- if (bChecked != bCheck)
- {
- SetCheck(hChildItem, bCheck);
- }
- TravelChild(hChildItem, bCheck);
- hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
- }
- }
- bool CSubFilesWnd::TravelThrough(HTREEITEM hItem)
- {
- ASSERT(hItem);
- HTREEITEM hChildItem = GetChildItem(hItem);
- if (!hChildItem)
- return GetCheck(hItem) != 0;
- bool bDelete = true;
- while (hChildItem)
- {
- if (!TravelThrough(hChildItem))
- bDelete = false;
- hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
- }
- SetCheck(hItem, bDelete);
- return bDelete;
- }
- void CSubFilesWnd::TravelSiblingAndParent(HTREEITEM hItem)
- {
- HTREEITEM hParentItem = GetParentItem(hItem);
- if (!hParentItem)
- return;
- BOOL bOldChecked = GetCheck(hParentItem);
- ASSERT(bOldChecked != -1);
- BOOL bAllChecked = true;
- HTREEITEM hChildItem;
- hChildItem = GetChildItem(hParentItem);
- while (hChildItem)
- {
- BOOL bChecked = GetCheck(hChildItem);
- ASSERT(bChecked != -1);
- if (!bChecked)
- {
- bAllChecked = false;
- break;
- }
- hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
- }
- if (bOldChecked != bAllChecked)
- {
- SetCheck(hParentItem, bAllChecked);
- TravelSiblingAndParent(hParentItem);
- }
- }
- void CSubFilesWnd::GetChecks()
- {
- m_lCount = 0;
- m_vChecks.clear();
- GetChecksChild(GetRootItem(), false);
- /*
- CString strText;
- for (int i=0; i<m_vChecks.size();i++)
- {
- CString strTemp;
- strTemp.Format("(%d, %d)rn", m_vChecks[i].cx, m_vChecks[i].cy);
- strText += strTemp;
- }
- AfxMessageBox(strText);
- //*/
- }
- void CSubFilesWnd::GetChecksChild(HTREEITEM hItem, BOOL bDelete)
- {
- //*
- long lCount = m_lCount;
- BOOL bDeleteParent = bDelete;
- while (hItem)
- {
- if (!bDeleteParent)
- {
- if (bDelete != GetCheck(hItem))
- {
- bDelete = GetCheck(hItem);
- if (bDelete)
- {
- lCount = m_lCount;
- }
- else
- {
- if (m_lCount > lCount)
- m_vChecks.push_back(CSize(lCount, m_lCount));
- }
- }
- }
- //
- // *** may be empty directory.!!!
- //
- HTREEITEM hChildItem = GetChildItem(hItem);
- if (!hChildItem)
- {
- if (GetItemData(hItem) == eNormal)
- m_lCount ++;
- }
- else
- {
- GetChecksChild(hChildItem, bDelete);
- }
- hItem = GetNextSiblingItem(hItem);
- }
- if (!bDeleteParent)
- {
- if (bDelete)
- {
- if (m_lCount > lCount)
- m_vChecks.push_back(CSize(lCount, m_lCount));
- }
- }
- //*/
- /*
- long lCount = m_lCount;
- BOOL bDeleteParent = bDelete;
- if (!bDeleteParent)
- bDelete = GetCheck(hItem);
- hItem = GetChildItem(hItem);
- if (!hItem)
- {
- m_lCount ++;
- }
- else if (hItem)
- {
- while (hItem)
- {
- GetChecksChild(hItem, bDelete);
- hItem = GetNextItem(hItem, TVGN_NEXT);
- }
- }
- if (!bDeleteParent && bDelete)
- m_vChecks.push_back(CSize(lCount, m_lCount));
- //*/
- }
- void CSubFilesWnd::OnRButtonDown(UINT nFlags, CPoint point)
- {
- CTreeCtrlEx::OnRButtonDown(nFlags, point);
- }