DlgMakeMetafile.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:9k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // DlgMakeMetafile.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "testbt.h"
  5. #include "DlgMakeMetafile.h"
  6. #include <afxinet.h>
  7. #include "filebase.h"
  8. #include "testbtdoc.h"
  9. #include "CategoryBarWnd.h"
  10. #include "CategoryCombobox.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. const string strMakeMetafileSection = "MakeMeta";
  17. const string strMakeMetafileUrl = "Url";
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CDlgMakeMetafile dialog
  20. CDlgMakeMetafile::CDlgMakeMetafile(CTestBTDoc* pTestBTDoc, CWnd* pParent /*=NULL*/)
  21. : CDialog(CDlgMakeMetafile::IDD, pParent)
  22. {
  23. //{{AFX_DATA_INIT(CDlgMakeMetafile)
  24. m_strFilePath = _T("");
  25. m_strComment = _T("");
  26. //}}AFX_DATA_INIT
  27. m_strSaveDBpath = "d:\aabb";
  28. m_pTestBTDoc = pTestBTDoc;
  29. m_bMakeAddExit = false;
  30. }
  31. void CDlgMakeMetafile::DoDataExchange(CDataExchange* pDX)
  32. {
  33. CDialog::DoDataExchange(pDX);
  34. //{{AFX_DATA_MAP(CDlgMakeMetafile)
  35. DDX_Control(pDX, IDCANCEL, m_btnCancel);
  36. DDX_Control(pDX, IDOK, m_btnAddExit);
  37. DDX_Control(pDX, IDC_BUTTON_ADD_EXIT, m_btnAdd);
  38. DDX_Control(pDX, IDC_BUTTON_MAKE, m_btnMake);
  39. DDX_Control(pDX, IDC_COMBO_CATEGORY, m_cbCategory);
  40. DDX_Control(pDX, IDC_COMBOBOXEX_URL, m_cbUrl);
  41. DDX_Control(pDX, IDC_COMBO_PIECELENTH, m_cbPieceLength);
  42. DDX_Control(pDX, IDC_PROGRESS_MAKE_METAFILE, m_progMakeMetafile);
  43. DDX_Text(pDX, IDC_EDIT_FILE_PATH, m_strFilePath);
  44. DDX_Text(pDX, IDC_EDIT_COMMENT, m_strComment);
  45. DDX_Control(pDX, IDC_STATIC_CURMETAFILE, m_edCurMetaFile);
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CDlgMakeMetafile, CDialog)
  49. //{{AFX_MSG_MAP(CDlgMakeMetafile)
  50. ON_WM_DESTROY()
  51. ON_BN_CLICKED(IDC_BUTTON_SELECT_PATH, OnButtonSelectPath)
  52. ON_BN_CLICKED(IDC_BUTTON_SELECT_FILE, OnButtonSelectFile)
  53. ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
  54. ON_BN_CLICKED(IDC_BUTTON_OPEN_FOLDER, OnButtonOpenFolder)
  55. ON_BN_CLICKED(IDC_BUTTON_ADD_EXIT, OnButtonAddExit)
  56. ON_BN_CLICKED(IDC_BUTTON_MAKE, OnButtonMake)
  57. //}}AFX_MSG_MAP
  58. ON_MESSAGE(MAKE_METAFILE_PROGRESS, OnMakeProgress)
  59. ON_MESSAGE(MAKE_METAFILE_FINISHED, OnMakeFinished)
  60. END_MESSAGE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CDlgMakeMetafile message handlers
  63. BOOL CDlgMakeMetafile::OnInitDialog() 
  64. {
  65. CDialog::OnInitDialog();
  66. //
  67. //
  68. //
  69. m_progMakeMetafile.SetRange(0, 100);
  70. m_progMakeMetafile.SetPos(0);
  71. // url history comboBox.
  72. m_cbUrl.LoadHistory(strMakeMetafileSection.data(), strMakeMetafileUrl.data());
  73. if (m_cbUrl.GetCount() <= 0)
  74. {
  75. // m_cbUrl.GetEditCtrl()->ReplaceSel("http://www.voracity.net:6969/announce");
  76. m_cbUrl.AddString("http://www.voracity.net:6969/announce");
  77. m_cbUrl.SetCurSel(0);
  78. }
  79. // Piece length comboBox.
  80. m_cbPieceLength.AddString("自动");
  81. m_cbPieceLength.SetItemData(0, 21);
  82. for (int i=0; i<7; i++)
  83. {
  84. m_cbPieceLength.AddString(FormatSize(pow(2, 21 - i)).data());
  85. m_cbPieceLength.SetItemData(i+1, 21 - i);
  86. }
  87. m_cbPieceLength.SetCurSel(0);
  88. // Category comboBox
  89. m_cbCategory.m_pimgList = &m_pTestBTDoc->GetCategoryBarWnd().m_ctlImage;
  90. Copy(m_pTestBTDoc->GetCategoryBarWnd().GetRootItem());
  91. m_cbCategory.SetCurSel(0);
  92. UpdateCommandUI();
  93. return TRUE;  // return TRUE unless you set the focus to a control
  94.               // EXCEPTION: OCX Property Pages should return FALSE
  95. }
  96. void CDlgMakeMetafile::UpdateCommandUI()
  97. {
  98. GetDlgItem(IDC_BUTTON_ADD_EXIT)->EnableWindow(m_MakeMetaFile.GetResult());
  99. GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(m_MakeMetaFile.GetResult());
  100. GetDlgItem(IDC_BUTTON_OPEN_FOLDER)->EnableWindow(m_MakeMetaFile.GetResult());
  101. m_cbCategory.EnableWindow(m_MakeMetaFile.GetResult());
  102. if (m_MakeMetaFile.GetResult())
  103. m_edCurMetaFile.SetWindowText(m_MakeMetaFile.GetSrcPath().data());
  104. }
  105. void CDlgMakeMetafile::OnMakeProgress(long lParam, long wParam)
  106. {
  107. m_progMakeMetafile.SetPos(lParam);
  108. }
  109. void CDlgMakeMetafile::OnMakeFinished(long lParam, long wParam)
  110. {
  111. m_MakeMetaFile.Close();
  112. assert(!m_MakeMetaFile.IsMaking());
  113. GetDlgItem(IDOK)->EnableWindow(true);
  114. m_progMakeMetafile.SetPos(100);
  115. UpdateCommandUI();
  116. // SetWindowText(m_MakeMetaFile.GetResult() ? "completed" : "error");
  117. if (!m_MakeMetaFile.GetResult())
  118. {
  119. AfxMessageBox(CString("make file error : ") + m_MakeMetaFile.GetErrorMsg().data());
  120. }
  121. if (m_bMakeAddExit)
  122. OnButtonAddExit();
  123. }
  124. void CDlgMakeMetafile::OnButtonMake() 
  125. {
  126. UpdateData();
  127. // check file src path.
  128. DWORD dwAttr = GetFileAttributes(m_strFilePath);
  129. if (dwAttr ==  0xffffffff)
  130. {
  131. AfxMessageBox("file src invalid");
  132. ((CEdit*)GetDlgItem(IDC_EDIT_FILE_PATH))->SetSel(0, -1);
  133. GetDlgItem(IDC_EDIT_FILE_PATH)->SetFocus();
  134. return ;
  135. }
  136. // check to see if this is a reasonable URL
  137. CString strUrl;
  138. m_cbUrl.GetWindowText(strUrl);
  139. CString strServerName;
  140. CString strObject;
  141. WORD nPort;
  142. DWORD dwServiceType;
  143. if (!AfxParseURL(strUrl, dwServiceType, strServerName, strObject, nPort) ||
  144. dwServiceType != INTERNET_SERVICE_HTTP)
  145. {
  146. AfxMessageBox("url is invalid");
  147. // m_cbUrl.GetEditCtrl()->SetSel(0, -1);
  148. // m_cbUrl.GetEditCtrl()->SetFocus();
  149. m_cbUrl.SetEditSel(0, -1);
  150. m_cbUrl.SetFocus();
  151. return;
  152. }
  153. m_cbUrl.SaveHistory();
  154. // Piece length.
  155. int iPieceLengthExp = m_cbPieceLength.GetItemData(m_cbPieceLength.GetCurSel());
  156. // Get save path.
  157. string strPath, strName;
  158. if (!SplitPathName(m_strFilePath.GetBuffer(0), strPath, strName))
  159. {
  160. AfxMessageBox("can't make driver root");
  161. return ;
  162. }
  163. assert(!strPath.empty() && !strName.empty());
  164. if (strPath[strPath.size()-1] != '\')
  165. strPath += '\';
  166. string strSaveAs = strPath + strName + ".torrent";
  167. // if savas torrent file exist and is directory, alert user.
  168. dwAttr = GetFileAttributes(strSaveAs.data());
  169. if (dwAttr != 0xffffffff && dwAttr & FILE_ATTRIBUTE_DIRECTORY)
  170. {
  171. AfxMessageBox(CString("directory same name (") + strSaveAs.data() + ") already exist");
  172. return;
  173. }
  174. // begin create meta file.
  175. if (!m_MakeMetaFile.MakeMetaFileEx(m_strFilePath.GetBuffer(0), strSaveAs, 
  176. strUrl.GetBuffer(0), m_strComment.GetBuffer(0),iPieceLengthExp, GetSafeHwnd()))
  177. {
  178. AfxMessageBox("some file is making");
  179. return ;
  180. }
  181. GetDlgItem(IDOK)->EnableWindow(false);
  182. m_progMakeMetafile.SetPos(0);
  183. // CDialog::OnOK();
  184. }
  185. void CDlgMakeMetafile::OnOK() 
  186. {
  187. m_bMakeAddExit = true;
  188. OnButtonMake();
  189. // OnButtonAddExit();
  190. }
  191. void CDlgMakeMetafile::OnDestroy() 
  192. {
  193. m_MakeMetaFile.Close();
  194. CDialog::OnDestroy();
  195. }
  196. void CDlgMakeMetafile::OnButtonSelectPath() 
  197. {
  198. CString strDef, strFolder;
  199. if (!SelectFolder(strDef, strFolder))
  200. return;
  201. UpdateData();
  202. m_strFilePath = strFolder;
  203. UpdateData(false);
  204. ((CEdit*)GetDlgItem(IDC_EDIT_FILE_PATH))->SetSel(0, -1);
  205. GetDlgItem(IDC_EDIT_FILE_PATH)->SetFocus();
  206. }
  207. void CDlgMakeMetafile::OnButtonSelectFile() 
  208. {
  209. CFileDialog fileDialog(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "All Files (*.*)|*.*|");
  210. int iRet = fileDialog.DoModal();
  211. if (iRet != IDOK)
  212. return ;
  213. UpdateData();
  214. m_strFilePath = fileDialog.GetPathName();
  215. UpdateData(false);
  216. ((CEdit*)GetDlgItem(IDC_EDIT_FILE_PATH))->SetSel(0, -1);
  217. GetDlgItem(IDC_EDIT_FILE_PATH)->SetFocus();
  218. }
  219. void CDlgMakeMetafile::OnButtonAddExit() 
  220. {
  221. OnButtonAdd();
  222. OnCancel();
  223. }
  224. void CDlgMakeMetafile::OnButtonAdd() 
  225. {
  226. int iCurSel = m_cbCategory.GetCurSel();
  227. SFOLDER* pCategory= (SFOLDER*)m_cbCategory.GetItemDataPtr(iCurSel);
  228. assert(pCategory && pCategory->m_hItem);
  229. assert(m_pTestBTDoc);
  230. m_pTestBTDoc->GetCategoryBarWnd().OpenTorrentFile(m_MakeMetaFile.GetSavePath(), pCategory->m_hItem, m_MakeMetaFile.GetSrcPath(), true);
  231. }
  232. void CDlgMakeMetafile::OnButtonOpenFolder() 
  233. {
  234. string strName;
  235. string strSavePath;
  236. SplitPathName(m_MakeMetaFile.GetSavePath(), strSavePath, strName);
  237. ShellExecute(NULL, "open" , strSavePath.data(), 0, 0, SW_SHOW);
  238. }
  239. void CDlgMakeMetafile::Copy(CTreeCursor& curItem, int iIndent)
  240. {
  241. if (!curItem) return;
  242. // don't show downloading category.
  243. if (curItem == m_pTestBTDoc->GetCategoryBarWnd().m_curDownloading)
  244. return;
  245. SFOLDER* pFolder = new SFOLDER;
  246. pFolder->m_iImageIndex = curItem.GetImageID();
  247. pFolder->m_iIndent = iIndent;
  248. pFolder->m_hItem = curItem;
  249. CString sztext = curItem.GetText();
  250. int iItem = m_cbCategory.AddString(sztext);
  251. m_cbCategory.SetItemDataPtr(iItem, pFolder);
  252. if (curItem.HasChildren())
  253. {
  254. CTreeCursor curChild = curItem.GetChild();
  255. while (curChild)
  256. {
  257. Copy(curChild, iIndent+1);
  258. curChild = curChild.GetNext(TVGN_NEXT);
  259. }
  260. }
  261. }