DlgMakeMetafile.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:9k
源码类别:
P2P编程
开发平台:
Visual C++
- // DlgMakeMetafile.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "DlgMakeMetafile.h"
- #include <afxinet.h>
- #include "filebase.h"
- #include "testbtdoc.h"
- #include "CategoryBarWnd.h"
- #include "CategoryCombobox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- const string strMakeMetafileSection = "MakeMeta";
- const string strMakeMetafileUrl = "Url";
- /////////////////////////////////////////////////////////////////////////////
- // CDlgMakeMetafile dialog
- CDlgMakeMetafile::CDlgMakeMetafile(CTestBTDoc* pTestBTDoc, CWnd* pParent /*=NULL*/)
- : CDialog(CDlgMakeMetafile::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDlgMakeMetafile)
- m_strFilePath = _T("");
- m_strComment = _T("");
- //}}AFX_DATA_INIT
- m_strSaveDBpath = "d:\aabb";
- m_pTestBTDoc = pTestBTDoc;
- m_bMakeAddExit = false;
- }
- void CDlgMakeMetafile::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgMakeMetafile)
- DDX_Control(pDX, IDCANCEL, m_btnCancel);
- DDX_Control(pDX, IDOK, m_btnAddExit);
- DDX_Control(pDX, IDC_BUTTON_ADD_EXIT, m_btnAdd);
- DDX_Control(pDX, IDC_BUTTON_MAKE, m_btnMake);
- DDX_Control(pDX, IDC_COMBO_CATEGORY, m_cbCategory);
- DDX_Control(pDX, IDC_COMBOBOXEX_URL, m_cbUrl);
- DDX_Control(pDX, IDC_COMBO_PIECELENTH, m_cbPieceLength);
- DDX_Control(pDX, IDC_PROGRESS_MAKE_METAFILE, m_progMakeMetafile);
- DDX_Text(pDX, IDC_EDIT_FILE_PATH, m_strFilePath);
- DDX_Text(pDX, IDC_EDIT_COMMENT, m_strComment);
- DDX_Control(pDX, IDC_STATIC_CURMETAFILE, m_edCurMetaFile);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgMakeMetafile, CDialog)
- //{{AFX_MSG_MAP(CDlgMakeMetafile)
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_BUTTON_SELECT_PATH, OnButtonSelectPath)
- ON_BN_CLICKED(IDC_BUTTON_SELECT_FILE, OnButtonSelectFile)
- ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
- ON_BN_CLICKED(IDC_BUTTON_OPEN_FOLDER, OnButtonOpenFolder)
- ON_BN_CLICKED(IDC_BUTTON_ADD_EXIT, OnButtonAddExit)
- ON_BN_CLICKED(IDC_BUTTON_MAKE, OnButtonMake)
- //}}AFX_MSG_MAP
- ON_MESSAGE(MAKE_METAFILE_PROGRESS, OnMakeProgress)
- ON_MESSAGE(MAKE_METAFILE_FINISHED, OnMakeFinished)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgMakeMetafile message handlers
- BOOL CDlgMakeMetafile::OnInitDialog()
- {
- CDialog::OnInitDialog();
- //
- //
- //
- m_progMakeMetafile.SetRange(0, 100);
- m_progMakeMetafile.SetPos(0);
- // url history comboBox.
- m_cbUrl.LoadHistory(strMakeMetafileSection.data(), strMakeMetafileUrl.data());
- if (m_cbUrl.GetCount() <= 0)
- {
- // m_cbUrl.GetEditCtrl()->ReplaceSel("http://www.voracity.net:6969/announce");
- m_cbUrl.AddString("http://www.voracity.net:6969/announce");
- m_cbUrl.SetCurSel(0);
- }
- // Piece length comboBox.
- m_cbPieceLength.AddString("自动");
- m_cbPieceLength.SetItemData(0, 21);
- for (int i=0; i<7; i++)
- {
- m_cbPieceLength.AddString(FormatSize(pow(2, 21 - i)).data());
- m_cbPieceLength.SetItemData(i+1, 21 - i);
- }
- m_cbPieceLength.SetCurSel(0);
- // Category comboBox
- m_cbCategory.m_pimgList = &m_pTestBTDoc->GetCategoryBarWnd().m_ctlImage;
- Copy(m_pTestBTDoc->GetCategoryBarWnd().GetRootItem());
- m_cbCategory.SetCurSel(0);
- UpdateCommandUI();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CDlgMakeMetafile::UpdateCommandUI()
- {
- GetDlgItem(IDC_BUTTON_ADD_EXIT)->EnableWindow(m_MakeMetaFile.GetResult());
- GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(m_MakeMetaFile.GetResult());
- GetDlgItem(IDC_BUTTON_OPEN_FOLDER)->EnableWindow(m_MakeMetaFile.GetResult());
- m_cbCategory.EnableWindow(m_MakeMetaFile.GetResult());
- if (m_MakeMetaFile.GetResult())
- m_edCurMetaFile.SetWindowText(m_MakeMetaFile.GetSrcPath().data());
- }
- void CDlgMakeMetafile::OnMakeProgress(long lParam, long wParam)
- {
- m_progMakeMetafile.SetPos(lParam);
- }
- void CDlgMakeMetafile::OnMakeFinished(long lParam, long wParam)
- {
- m_MakeMetaFile.Close();
- assert(!m_MakeMetaFile.IsMaking());
- GetDlgItem(IDOK)->EnableWindow(true);
- m_progMakeMetafile.SetPos(100);
- UpdateCommandUI();
- // SetWindowText(m_MakeMetaFile.GetResult() ? "completed" : "error");
- if (!m_MakeMetaFile.GetResult())
- {
- AfxMessageBox(CString("make file error : ") + m_MakeMetaFile.GetErrorMsg().data());
- }
- if (m_bMakeAddExit)
- OnButtonAddExit();
- }
- void CDlgMakeMetafile::OnButtonMake()
- {
- UpdateData();
- // check file src path.
- DWORD dwAttr = GetFileAttributes(m_strFilePath);
- if (dwAttr == 0xffffffff)
- {
- AfxMessageBox("file src invalid");
- ((CEdit*)GetDlgItem(IDC_EDIT_FILE_PATH))->SetSel(0, -1);
- GetDlgItem(IDC_EDIT_FILE_PATH)->SetFocus();
- return ;
- }
- // check to see if this is a reasonable URL
- CString strUrl;
- m_cbUrl.GetWindowText(strUrl);
- CString strServerName;
- CString strObject;
- WORD nPort;
- DWORD dwServiceType;
- if (!AfxParseURL(strUrl, dwServiceType, strServerName, strObject, nPort) ||
- dwServiceType != INTERNET_SERVICE_HTTP)
- {
- AfxMessageBox("url is invalid");
- // m_cbUrl.GetEditCtrl()->SetSel(0, -1);
- // m_cbUrl.GetEditCtrl()->SetFocus();
- m_cbUrl.SetEditSel(0, -1);
- m_cbUrl.SetFocus();
- return;
- }
- m_cbUrl.SaveHistory();
- // Piece length.
- int iPieceLengthExp = m_cbPieceLength.GetItemData(m_cbPieceLength.GetCurSel());
- // Get save path.
- string strPath, strName;
- if (!SplitPathName(m_strFilePath.GetBuffer(0), strPath, strName))
- {
- AfxMessageBox("can't make driver root");
- return ;
- }
- assert(!strPath.empty() && !strName.empty());
- if (strPath[strPath.size()-1] != '\')
- strPath += '\';
- string strSaveAs = strPath + strName + ".torrent";
- // if savas torrent file exist and is directory, alert user.
- dwAttr = GetFileAttributes(strSaveAs.data());
- if (dwAttr != 0xffffffff && dwAttr & FILE_ATTRIBUTE_DIRECTORY)
- {
- AfxMessageBox(CString("directory same name (") + strSaveAs.data() + ") already exist");
- return;
- }
- // begin create meta file.
- if (!m_MakeMetaFile.MakeMetaFileEx(m_strFilePath.GetBuffer(0), strSaveAs,
- strUrl.GetBuffer(0), m_strComment.GetBuffer(0),iPieceLengthExp, GetSafeHwnd()))
- {
- AfxMessageBox("some file is making");
- return ;
- }
- GetDlgItem(IDOK)->EnableWindow(false);
- m_progMakeMetafile.SetPos(0);
- // CDialog::OnOK();
- }
- void CDlgMakeMetafile::OnOK()
- {
- m_bMakeAddExit = true;
- OnButtonMake();
- // OnButtonAddExit();
- }
- void CDlgMakeMetafile::OnDestroy()
- {
- m_MakeMetaFile.Close();
- CDialog::OnDestroy();
- }
- void CDlgMakeMetafile::OnButtonSelectPath()
- {
- CString strDef, strFolder;
- if (!SelectFolder(strDef, strFolder))
- return;
- UpdateData();
- m_strFilePath = strFolder;
- UpdateData(false);
- ((CEdit*)GetDlgItem(IDC_EDIT_FILE_PATH))->SetSel(0, -1);
- GetDlgItem(IDC_EDIT_FILE_PATH)->SetFocus();
- }
- void CDlgMakeMetafile::OnButtonSelectFile()
- {
- CFileDialog fileDialog(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "All Files (*.*)|*.*|");
- int iRet = fileDialog.DoModal();
- if (iRet != IDOK)
- return ;
- UpdateData();
- m_strFilePath = fileDialog.GetPathName();
- UpdateData(false);
- ((CEdit*)GetDlgItem(IDC_EDIT_FILE_PATH))->SetSel(0, -1);
- GetDlgItem(IDC_EDIT_FILE_PATH)->SetFocus();
- }
- void CDlgMakeMetafile::OnButtonAddExit()
- {
- OnButtonAdd();
- OnCancel();
- }
- void CDlgMakeMetafile::OnButtonAdd()
- {
- int iCurSel = m_cbCategory.GetCurSel();
- SFOLDER* pCategory= (SFOLDER*)m_cbCategory.GetItemDataPtr(iCurSel);
- assert(pCategory && pCategory->m_hItem);
- assert(m_pTestBTDoc);
- m_pTestBTDoc->GetCategoryBarWnd().OpenTorrentFile(m_MakeMetaFile.GetSavePath(), pCategory->m_hItem, m_MakeMetaFile.GetSrcPath(), true);
- }
- void CDlgMakeMetafile::OnButtonOpenFolder()
- {
- string strName;
- string strSavePath;
- SplitPathName(m_MakeMetaFile.GetSavePath(), strSavePath, strName);
- ShellExecute(NULL, "open" , strSavePath.data(), 0, 0, SW_SHOW);
- }
- void CDlgMakeMetafile::Copy(CTreeCursor& curItem, int iIndent)
- {
- if (!curItem) return;
- // don't show downloading category.
- if (curItem == m_pTestBTDoc->GetCategoryBarWnd().m_curDownloading)
- return;
- SFOLDER* pFolder = new SFOLDER;
- pFolder->m_iImageIndex = curItem.GetImageID();
- pFolder->m_iIndent = iIndent;
- pFolder->m_hItem = curItem;
- CString sztext = curItem.GetText();
- int iItem = m_cbCategory.AddString(sztext);
- m_cbCategory.SetItemDataPtr(iItem, pFolder);
- if (curItem.HasChildren())
- {
- CTreeCursor curChild = curItem.GetChild();
- while (curChild)
- {
- Copy(curChild, iIndent+1);
- curChild = curChild.GetNext(TVGN_NEXT);
- }
- }
- }