testBTDoc.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:14k
源码类别:
P2P编程
开发平台:
Visual C++
- // testBTDoc.cpp : implementation of the CTestBTDoc class
- //
- #include "stdafx.h"
- #include "testBT.h"
- #include "testBTDoc.h"
- #include "TestBTView.h"
- #include "MainFrm.h"
- #include "MakeMetaFile.h"
- #include "DlgMakeMetafile.h"
- #include "Storage.h"
- #include "StorageWrapper.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTDoc
- IMPLEMENT_DYNCREATE(CTestBTDoc, CDocument)
- BEGIN_MESSAGE_MAP(CTestBTDoc, CDocument)
- //{{AFX_MSG_MAP(CTestBTDoc)
- ON_COMMAND(ID_MENUITEM_MAKE_METAFILE, OnMenuitemMakeMetafile)
- //}}AFX_MSG_MAP
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- ON_COMMAND(ID_MENUITEM_FIND_FILE, OnMenuitemFindFile)
- ON_COMMAND(ID_MENUITEM_FIND_NEXT, OnMenuitemFindNext)
- ON_COMMAND(ID_MENUITEM_SELECT_ALL, OnMenuitemSelectAll)
- ON_COMMAND(ID_FILE_NEW_DATABASE, OnFileNewDatabase)
- ON_COMMAND(ID_FILE_SAVE_AS_DATABASE, OnFileSaveAsDatabase)
- ON_COMMAND(ID_MENUITEM_OPEN_DATABASE, OnMenuitemOpenDatabase)
- ON_COMMAND(ID_FILE_SAVE_DATABASE, OnFileSaveDatabase)
- ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_STOP, ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, OnUpdateMenuitem)
- ON_COMMAND_RANGE(ID_MENUITEM_STOP, ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY, OnCommandMenuitem)
- END_MESSAGE_MAP()
- //ON_UPDATE_COMMAND_UI(ID_MENUITEM_DOWNLOAD, OnUpdateMenuitem)
- // ON_COMMAND(ID_MENUITEM_DOWNLOAD, OnCommandMenuitem)
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTDoc construction/destruction
- /*
- class temp
- {
- public :
- temp& operator = (const temp& other)
- {
- memcpy(szBuf, other.szBuf, 20);
- return *this;
- }
- bool operator == (const temp& other) const
- {
- return memcmp(szBuf, other.szBuf, 20) == 0;
- return true;
- }
- bool operator < (const temp& other) const
- {
- return memcmp(szBuf, other.szBuf, 20) < 0;
- }
- char szBuf[20];
- };
- //*/
- // #include "Sha.h"
- CTestBTDoc::CTestBTDoc()
- {
- m_iOpendFileCount = 0;
- /*
- char szText[11] = "abcdefghij";
- unsigned char shaPart[20] = {0};
- CSHA sha;
- sha.start();
- sha.update((unsigned char*)szText, 5);
- // sha.finish(shaPart);
- sha.update((unsigned char*)szText + 5, 5);
- sha.finish(shaPart);
- TRACE("rn{");
- for (int i=0; i<20; i++)
- {
- TRACE("%d, ", shaPart[i]);
- }
- TRACE("}rn");
- CSHA sha2;
- sha2.start();
- sha2.update((unsigned char*)szText, 10);
- sha2.finish(shaPart);
- TRACE("rn{");
- for (i=0; i<20; i++)
- {
- TRACE("%d, ", shaPart[i]);
- }
- TRACE("}rn");
- //*/
- /*
- temp t;
- memset(t.szBuf, 0, 20);
- vector<temp> pvPieces;
- for (int i=0; i<10; i++)
- pvPieces.push_back(t);
- assert(find(pvPieces.begin(), pvPieces.end(), t) != pvPieces.end());
- map<temp, long> mPieces;
- assert(mPieces.find(t) == mPieces.end());
- mPieces[t] = 100;
- assert(mPieces.begin() != mPieces.end());
- long l = mPieces.size();
- long zz = mPieces[t];
- zz = (*mPieces.begin()).second;
- temp tx = (*mPieces.begin()).first;
- assert(mPieces.find(t) != mPieces.end());
- //*/
- // pvPieces.push_back(szBuf);
- /*
- try
- {
- CFileTorrent fileTorrernt;
- if (!fileTorrernt.OpenFile("F:\Documents and Settings\道理\桌面\IEButton.torrent", "d:\za"))
- {
- assert(false);
- return ;
- }
- vector<CFileInfo>& vFiles = fileTorrernt.GetFileInfo();
- CStorageEx storage;
- storage.Create(0, vFiles, 0, 10, true, 0);
- CVal* pInfo = fileTorrernt.GetInfo();
- vector<char*>& vPieces = fileTorrernt.GetPieces();
- CStorageWrapperEx storageWrapper;
- storageWrapper.Create(0, 0, 0, &storage, vPieces, true, pow(2, 14), (*pInfo)["piece length"]->lVal);
- /*
- bool bpre = storage.was_preallocated(10, 1000000);
- long lTotal = storage.GetTotalLength();
- for (int i=0; i<100; i++)
- {
- storage.write("asdfdsafda", i*10, 10);
- }
- storage.SetReadOnly();
- storage.write("asdfdsafda", 1023667, 10);
- char szText[20] = {0};
- storage.read(szText, 1023667, 10);
- storage.top_off();
- storage.Close();
- //*/
- /*
- }
- catch (string e)
- {
- AfxMessageBox(e.data());
- }
- //*/
- m_bInitiated = false;
- }
- CTestBTDoc::~CTestBTDoc()
- {
- }
- BOOL CTestBTDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- CWnd* pwnd = AfxGetMainWnd();
- POSITION pos = GetFirstViewPosition();
- CView* pFirstView = GetNextView( pos );
- if (!m_bInitiated)
- {
- SetTitle(GetCategoryBarWnd().GetDatabaseName().data());
- m_bInitiated = true;
- }
- return TRUE;
- }
- CFilesListCtrl& CTestBTDoc::GetFileListCtrl() const
- {
- POSITION Pos = GetFirstViewPosition();
- assert(Pos);
- CTestBTView* pView = (CTestBTView*)GetNextView(Pos);
- return pView->m_FilesList;
- }
- CCategoryBarWnd& CTestBTDoc::GetCategoryBarWnd() const
- {
- assert(((CTestBTApp*)AfxGetApp())->m_pMainFrame);
- CFrameWnd* pFrame = ((CTestBTApp*)AfxGetApp())->m_pMainFrame;
- return ((CMainFrame*)pFrame)->m_CategoryBar.m_CategoryBarWnd;
- return ((CMainFrame*)AfxGetMainWnd())->m_CategoryBar.m_CategoryBarWnd;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTDoc serialization
- void CTestBTDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTDoc diagnostics
- #ifdef _DEBUG
- void CTestBTDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CTestBTDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTDoc commands
- void CTestBTDoc::MakeTorrent(CString strFilePath)
- {
- CDlgMakeMetafile dlg(this);
- dlg.m_strFilePath = strFilePath;
- dlg.DoModal();
- }
- BOOL CTestBTDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- DWORD dwAttr = GetFileAttributes(lpszPathName);
- if (dwAttr == 0xffffffff)
- {
- AfxMessageBox(CString ("文件(") + lpszPathName + ") 不存在");
- return FALSE;
- }
- bool bOpenDB = false;
- bool bOpenTorrent = false;
- if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
- {
- bOpenTorrent = false;
- bOpenDB = false;
- }
- else
- {
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- _splitpath(lpszPathName, drive, dir, fname, ext);
- CString strTorrentExt(".torrent");
- CString strDBExt(".bt");
- if (strlen(ext))
- {
- if (!strTorrentExt.CompareNoCase(ext))
- {
- bOpenTorrent = true;
- }
- else if (!strDBExt.CompareNoCase(ext))
- {
- bOpenDB = true;
- }
- }
- }
- if (bOpenTorrent)
- {
- GetCategoryBarWnd().OpenTorrentFile(lpszPathName);
- if (((CMainFrame*)AfxGetMainWnd())->m_setupRegister.m_bSaveDBAuto &&
- ++m_iOpendFileCount >= ((CMainFrame*)AfxGetMainWnd())->m_setupRegister.m_lSaveDBIntval)
- {
- if (!GetCategoryBarWnd().IsModifyed())
- {
- m_iOpendFileCount = 0;
- GetCategoryBarWnd().Save();
- }
- }
- }
- else if (bOpenDB)
- {
- if (!GetCategoryBarWnd().OpenDBFile(lpszPathName))
- return false;
- SetTitle(GetCategoryBarWnd().GetDatabaseName().data());
- return TRUE;
- // AfxGetApp()->AddToRecentFileList(lpszPathName);
- }
- else
- {
- CDlgMakeMetafile dlg(this);
- dlg.m_strFilePath = lpszPathName;
- dlg.DoModal();
- }
- // if (!CDocument::OnOpenDocument(lpszPathName))
- // return FALSE;
- // return TRUE;
- return FALSE;
- }
- //////////////////////////////////////////////////////////////////////
- // find files
- void CTestBTDoc::OnMenuitemFindFile()
- {
- DbgGuiLeak m_dbgGui;
- GetCategoryBarWnd().OnFindFile();
- }
- void CTestBTDoc::OnMenuitemFindNext()
- {
- GetCategoryBarWnd().OnFindNext();
- }
- void CTestBTDoc::OnMenuitemSelectAll()
- {
- GetFileListCtrl().SelectAllItems();
- }
- ////////////////////////////////////////////////////////////////////////////////
- // database.
- bool CTestBTDoc::PromptToSaveDatabase(bool bExit)
- {
- if (GetCategoryBarWnd().IsModifyed())
- {
- int iRet = AfxMessageBox("是否保存变化到无标题?"/*Save changes to Untitled?"//*/, MB_YESNOCANCEL);
- if (iRet == IDCANCEL)
- return false;
- else if (iRet == IDNO)
- {
- return true;
- }
- else if (iRet == IDYES)
- {
- if (!SaveAsDatabase())
- return false;
- return true;
- }
- }
- else
- {
- if (bExit)
- GetCategoryBarWnd().StopAllCompleteds();
- GetCategoryBarWnd().Save();
- }
- return true;
- }
- void CTestBTDoc::OnFileNewDatabase()
- {
- if (GetCategoryBarWnd().IsDownloading())
- {
- if (AfxMessageBox("有任务正在进行中, 继续关闭吗?", MB_YESNO | MB_DEFBUTTON2) == IDNO)
- return ;
- }
- if (!PromptToSaveDatabase())
- return;
- GetCategoryBarWnd().NewDatabase();
- GetCategoryBarWnd().GetRootItem().Select();
- SetTitle(GetCategoryBarWnd().GetDatabaseName().data());
- }
- void CTestBTDoc::OnMenuitemOpenDatabase()
- {
- if (GetCategoryBarWnd().IsDownloading())
- {
- if (AfxMessageBox("有任务正在进行中, 继续关闭吗?", MB_YESNO|MB_DEFBUTTON2) == IDNO)
- return ;
- }
- if (!PromptToSaveDatabase())
- return;
- int structsize = 76;
- DWORD dwVersion,dwWindowsMajorVersion,dwWindowsMinorVersion;
- dwVersion = GetVersion();
- dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
- dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
- if (dwVersion < 0x80000000)
- structsize =88;
- CFileDialog fileDialog(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Database (*.bt)|*.bt|All Files (*.*)|*.*|");
- fileDialog.m_ofn.lStructSize = structsize;
- int iRet = fileDialog.DoModal();
- if (iRet != IDOK) return ;
- CString strFileName = fileDialog.GetPathName();
- if (!GetCategoryBarWnd().OpenDBFile(strFileName.GetBuffer(0)))
- return;
- SetTitle(GetCategoryBarWnd().GetDatabaseName().data());
- AfxGetApp()->AddToRecentFileList(strFileName);
- }
- void CTestBTDoc::OnFileSaveDatabase()
- {
- if (GetCategoryBarWnd().IsModifyed())
- OnFileSaveAsDatabase();
- else
- GetCategoryBarWnd().Save();
- SetTitle(GetCategoryBarWnd().GetDatabaseName().data());
- }
- void CTestBTDoc::OnFileSaveAsDatabase()
- {
- SaveAsDatabase();
- }
- bool CTestBTDoc::SaveAsDatabase(bool bExit)
- {
- CString strDataBaseFullPath = GetCategoryBarWnd().GetDatabaseFullPath().data();
- int structsize = 76;
- DWORD dwVersion,dwWindowsMajorVersion,dwWindowsMinorVersion;
- dwVersion = GetVersion();
- dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
- dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
- if (dwVersion < 0x80000000)
- structsize =88;
- CFileDialog fileDialog(FALSE, 0, strDataBaseFullPath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Database (*.bt)|*.bt|");
- // if (!strDataBaseFullPath.IsEmpty())
- // fileDialog.m_ofn.lpstrFile = (char*)(LPCTSTR)strDataBaseFullPath;
- fileDialog.m_ofn.lStructSize = structsize;
- int iRet = fileDialog.DoModal();
- if (iRet != IDOK)
- return false;
- if (bExit)
- GetCategoryBarWnd().StopAllCompleteds();
- CString strFileName = fileDialog.GetPathName();
- GetCategoryBarWnd().SaveAs(strFileName.GetBuffer(0));
- SetTitle(GetCategoryBarWnd().GetDatabaseName().data());
- AfxGetApp()->AddToRecentFileList(strFileName);
- return true;
- }
- BOOL CTestBTDoc::CanCloseFrame(CFrameWnd* pFrame)
- {
- if (GetCategoryBarWnd().IsMovingFiles())
- {
- AfxMessageBox("正在移动文件, 不能关闭系统");
- return FALSE;
- }
- if (GetCategoryBarWnd().IsDownloading())
- {
- if (AfxMessageBox("有任务正在进行中, 继续关闭吗?", MB_YESNO|MB_DEFBUTTON2) == IDNO)
- return FALSE;
- }
- if (!PromptToSaveDatabase(true))
- return FALSE;
- if (pFrame && pFrame->IsWindowVisible())
- pFrame->SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
- return CDocument::CanCloseFrame(pFrame);
- }
- /////////////////////////////////////////////////////////////
- // handle menu and toolbar message.
- void CTestBTDoc::OnUpdateMenuitem(CCmdUI* pCmdUI)
- {
- switch (pCmdUI->m_nID)
- {
- case ID_MENUITEM_DOWNLOAD:
- case ID_MENUITEM_PAUSE:
- case ID_MENUITEM_STOP:
- case ID_MENUITEM_DEL:
- case ID_MENUITEM_FILE_PROPERTY:
- case ID_MENUITEM_OPEN_DOWNLOADED_FILE:
- case ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY:
- case ID_MENUITEM_MOVETO:
- case ID_MENUITEM_RUN_PARAMETERS:
- case ID_MENUITEM_COPY_TORRENT_FILE_PATH:
- {
- GetFileListCtrl().OnUpdateMenuitem(pCmdUI);
- return;
- }
- break;
- case ID_MENUITEM_FIND_NEXT:
- case ID_MENUITEM_OPEN_CATEGORY:
- case ID_MENUITEM_MOVETO_CATEGORY:
- case ID_MENUITEM_NEW_CATEGORY:
- case ID_MENUITEM_DEL_CATEGORY:
- case ID_MENUITEM_PORPERTY_CATEGORY:
- GetCategoryBarWnd().OnUpdateMenuitem(pCmdUI);
- return;
- break;
- }
- pCmdUI->ContinueRouting();
- }
- void CTestBTDoc::OnCommandMenuitem(UINT iID)
- {
- switch (iID)
- {
- case ID_MENUITEM_DOWNLOAD:
- case ID_MENUITEM_PAUSE:
- case ID_MENUITEM_STOP:
- case ID_MENUITEM_DEL:
- case ID_MENUITEM_FILE_PROPERTY:
- case ID_MENUITEM_OPEN_DOWNLOADED_FILE:
- case ID_MENUITEM_OPEN_DOWNLOADED_DIRECTORY:
- case ID_MENUITEM_MOVETO:
- case ID_MENUITEM_RUN_PARAMETERS:
- case ID_MENUITEM_COPY_TORRENT_FILE_PATH:
- GetFileListCtrl().OnCommandMenuitem(iID);
- break;
- // case ID_MENUITEM_FIND_NEXT:
- case ID_MENUITEM_OPEN_CATEGORY:
- case ID_MENUITEM_MOVETO_CATEGORY:
- case ID_MENUITEM_NEW_CATEGORY:
- case ID_MENUITEM_DEL_CATEGORY:
- case ID_MENUITEM_PORPERTY_CATEGORY:
- GetCategoryBarWnd().OnCommandMenuitem(iID);
- break;
- }
- }
- void CTestBTDoc::OnMenuitemMakeMetafile()
- {
- CDlgMakeMetafile dlg(this);
- dlg.DoModal();
- }
- void CTestBTDoc::OnFileOpen()
- {
- int structsize = 76;
- DWORD dwVersion,dwWindowsMajorVersion,dwWindowsMinorVersion;
- dwVersion = GetVersion();
- dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
- dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
- if (dwVersion < 0x80000000)
- structsize =88;
- CFileDialog fileDialog(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Torrent File (*.torrent)|*.torrent|All Files (*.*)|*.*|");
- fileDialog.m_ofn.lStructSize = structsize;
- int iRet = fileDialog.DoModal();
- if (iRet != IDOK) return ;
- CString strFileName = fileDialog.GetPathName();
- OnOpenDocument(strFileName);
- }