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

P2P编程

开发平台:

Visual C++

  1. // DlgNewDownload.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "testbt.h"
  5. #include "DlgNewDownload.h"
  6. #include "CategoryNewDlg.h"
  7. #include "FileBase.h"
  8. #include "FileTorrent.h"
  9. #include "SetupRegister.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. const string CDlgNewDownload::m_strSaveAsSection = "SaveAs";
  16. const string CDlgNewDownload::m_strSaveAsCategoryKey = "Category";
  17. const string CDlgNewDownload::m_strSaveAsDirectoryKey = "DefaultDirectory";
  18. const CString CDlgNewDownload::m_strSection = "DlgNewDownload";
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CDlgNewDownload dialog
  21. CDlgNewDownload::CDlgNewDownload(CImageList* pImageList, 
  22. CFileTorrent* pfiletorrent, CCategoryBarWnd* pCategoryBarWnd, bool bNewOrProperty, CFileDBItem* pFileDBItem, long lDirectoryID, string strDirectorySave, CWnd* pParent /*=NULL*/)
  23. : CExpandingDialog(CDlgNewDownload::IDD, pParent)
  24. {
  25. //{{AFX_DATA_INIT(CDlgNewDownload)
  26. m_strRename = _T("");
  27. m_strInfohash = _T("");
  28. m_strUrl = _T("");
  29. m_strComment = _T("");
  30. m_strTorrentFileName = _T("");
  31. m_iStartMode = -1;
  32. //}}AFX_DATA_INIT
  33. m_pImageList = pImageList;
  34. m_pfiletorrent = pfiletorrent;
  35. m_pCategoryBarWnd = pCategoryBarWnd;
  36. m_bNewOrProperty = bNewOrProperty;
  37. m_lDirectoryID = lDirectoryID;
  38. m_strDirectorySave = strDirectorySave;
  39. m_bNoNotification = false;
  40. m_iStartMode = 0;
  41. m_lSubFilesWndWidth = 300;
  42. m_pFileDBItem = pFileDBItem;
  43. }
  44. void CDlgNewDownload::DoDataExchange(CDataExchange* pDX)
  45. {
  46. CDialog::DoDataExchange(pDX);
  47. //{{AFX_DATA_MAP(CDlgNewDownload)
  48. DDX_Control(pDX, IDC_BUTTON_MORE, m_btnMore);
  49. DDX_Control(pDX, IDC_BUTTON_DOWNLOAD_LATER, m_btnDownloadLater);
  50. DDX_Control(pDX, IDC_BUTTON_DIRECTORY_SELECT, m_btnBrowseDir);
  51. DDX_Control(pDX, IDC_BUTTON_CATEGORY_NEW, m_btnCategoryNew);
  52. DDX_Control(pDX, IDC_BUTTON_SAVE_AS_DEFAULT, m_btnSaveAsDef);
  53. DDX_Control(pDX, IDCANCEL, m_btnCancel);
  54. DDX_Control(pDX, IDOK, m_btnOK);
  55. DDX_Control(pDX, IDC_TREE_SUBFILES, m_treeSubFiles);
  56. DDX_Control(pDX, IDC_COMBO_DIRECTORY_SAVE, m_cbDirectorySave);
  57. DDX_Control(pDX, IDC_COMBO_CATEGORY, m_cbCategory);
  58. DDX_Text(pDX, IDC_EDIT_RENAME, m_strRename);
  59. DDX_Text(pDX, IDC_EDIT_INFOHASH, m_strInfohash);
  60. DDX_Text(pDX, IDC_EDIT_URL, m_strUrl);
  61. DDX_Text(pDX, IDC_EDIT_COMMENT, m_strComment);
  62. DDX_Text(pDX, IDC_EDIT_TORRENT_FILE, m_strTorrentFileName);
  63. DDX_Radio(pDX, IDC_RADIO_DOWNLOAD_IMMEDIATELY, m_iStartMode);
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CDlgNewDownload, CExpandingDialog)
  67. //{{AFX_MSG_MAP(CDlgNewDownload)
  68. ON_CBN_SELCHANGE(IDC_COMBO_CATEGORY, OnSelchangeComboCategory)
  69. ON_BN_CLICKED(IDC_BUTTON_CATEGORY_NEW, OnButtonCategoryNew)
  70. ON_BN_CLICKED(IDC_BUTTON_DIRECTORY_SELECT, OnButtonDirectorySelect)
  71. ON_BN_CLICKED(IDC_BUTTON_SAVE_AS_DEFAULT, OnButtonSaveAsDefault)
  72. ON_WM_DESTROY()
  73. ON_BN_CLICKED(IDC_RADIO_DOWNLOAD_MANUALY, OnRadioDownloadManualy)
  74. ON_BN_CLICKED(IDC_RADIO_DOWNLOAD_IMMEDIATELY, OnRadioDownloadImmediately)
  75. ON_WM_CTLCOLOR()
  76. //}}AFX_MSG_MAP
  77. ON_BN_CLICKED(IDC_BUTTON_DOWNLOAD_LATER, OnButtonDownloadLater)
  78. ON_MESSAGE(WM_SPLITTER_MOVED, OnSplitterMoved)
  79. END_MESSAGE_MAP()
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CDlgNewDownload message handlers
  82. void CDlgNewDownload::LoadState()
  83. {
  84. CString strKey = "width";
  85. // m_lSubFilesWndWidth = AfxGetApp()->GetProfileInt(m_strSection, strKey, m_lSubFilesWndWidth);
  86. strKey = "expand";
  87. bool bExpand = AfxGetApp()->GetProfileInt(m_strSection, strKey, false) != 0;
  88. Expand(bExpand);
  89. strKey = "startMode";
  90. m_iStartMode = AfxGetApp()->GetProfileInt(m_strSection, strKey, m_iStartMode);
  91. }
  92. void CDlgNewDownload::SaveStateWhenDestory()
  93. {
  94. CString strKey = "width";
  95. // AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_lSubFilesWndWidth);
  96. strKey = "expand";
  97. AfxGetApp()->WriteProfileInt(m_strSection, strKey, IsExpanded());
  98. }
  99. void CDlgNewDownload::SaveState()
  100. {
  101. CString strKey = "startMode";
  102. AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_iStartMode);
  103. }
  104. BOOL CDlgNewDownload::OnInitDialog() 
  105. {
  106. CExpandingDialog::OnInitDialog();
  107. //
  108. // Create BitmapList
  109. //
  110. HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
  111. MAKEINTRESOURCE(IDB_BITMAP_DLG_ICONS),
  112. IMAGE_BITMAP,
  113. 0,0, // cx,cy
  114. LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
  115. CBitmap bm;
  116. bm.Attach(hbm);
  117. m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 3, 4);
  118. m_ctlImage.SetBkColor(RGB(255, 255,255)); 
  119. m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
  120. //
  121. // set button image.
  122. //
  123. // m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存路径");
  124. m_btnOK.Create(m_ctlImage.ExtractIcon(0));
  125. m_btnCancel.Create(m_ctlImage.ExtractIcon(1));
  126. m_btnDownloadLater.Create(m_ctlImage.ExtractIcon(3));
  127. m_btnSaveAsDef.Create(m_ctlImage.ExtractIcon(4), true, "保存为缺省设置");
  128. m_btnCategoryNew.Create(m_ctlImage.ExtractIcon(2), true, "添加类别");
  129. m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存目录");
  130. // m_btnDownloadLater.ShowWindow(m_bNewOrProperty);
  131. m_btnDownloadLater.SetWindowText(m_bNewOrProperty ? "添加下载" : "保存属性");
  132. assert(m_pCategoryBarWnd);
  133. SetWindowText(m_bNewOrProperty ? "添加新的下载" : "属性");
  134. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_COMMENT);
  135. if (pEdit)
  136. {
  137. pEdit->SetLimitText(100);
  138. }
  139. //
  140. // load register setup.
  141. //
  142. LoadState();
  143. UpdateData(false);
  144. // load DirectorySave history
  145. m_cbDirectorySave.LoadHistory("DirectorySave", "HistoryComboEx");
  146. //
  147. // load data.
  148. //
  149. m_treeSubFiles.Init();
  150. Load();
  151. LoadTorrent();
  152. /*
  153. // Create splitter.
  154. // m_wndHSplitterBar.Create(WS_CHILD|WS_BORDER|WS_DLGFRAME|WS_VISIBLE, CRect (0, 0, 100, 100), this, 112909, FALSE);
  155. CRect rc;
  156. m_treeSubFiles.GetWindowRect(rc);
  157. ScreenToClient(rc);
  158. rc.right = rc.left + m_lSubFilesWndWidth;
  159. m_treeSubFiles.MoveWindow(rc);
  160. m_wndHSplitterBar.SetPanes(&m_treeSubFiles, GetDlgItem(IDC_EDIT_COMMENT));
  161. //*/
  162. return TRUE;  // return TRUE unless you set the focus to a control
  163.               // EXCEPTION: OCX Property Pages should return FALSE
  164. }
  165. void CDlgNewDownload::OnDestroy() 
  166. {
  167. SaveStateWhenDestory();
  168. CExpandingDialog::OnDestroy();
  169. }
  170. void CDlgNewDownload::OnSplitterMoved(long lParam, long wParam)
  171. {
  172. CRect rc;
  173. m_treeSubFiles.GetClientRect(rc);
  174. long lTreeCX = rc.Width();
  175. m_lSubFilesWndWidth = lTreeCX;
  176. }
  177. void CDlgNewDownload::LoadTorrent()
  178. {
  179. assert(m_pfiletorrent && m_pfiletorrent->GetResponse());
  180. if (!m_bNewOrProperty)
  181. assert(m_pFileDBItem);
  182. m_treeSubFiles.Monitor(m_pfiletorrent, m_bNewOrProperty ? string() : m_strDirectorySave, m_pFileDBItem);
  183. m_treeSubFiles.ShowSubFiles();
  184. if (m_bNewOrProperty)
  185. {
  186. m_strComment = m_pfiletorrent->GetComment().data();
  187. m_strRename = m_pfiletorrent->GetSaveName().data();
  188. }
  189. m_strInfohash = m_pfiletorrent->GetInfohashString().data();
  190. m_strUrl = m_pfiletorrent->GetUrl().data();
  191. m_strTorrentFileName = m_pfiletorrent->GetTorrentFileName().data();
  192. UpdateData(false);
  193. }
  194. void CDlgNewDownload::Load()
  195. {
  196. assert(m_pImageList);
  197. // show the categorys.
  198. m_cbCategory.m_pimgList = m_pImageList; // &(m_pCategoryBarWnd->m_ctlImage);
  199. m_cbCategory.DeleteAllItems();
  200. Copy(m_pCategoryBarWnd->GetRootItem());
  201. //
  202. // get directoryID 
  203. //
  204. CString strDefaultDirectory;
  205. int lDirecotryID = 0;
  206. if (m_bNewOrProperty)
  207. {
  208. // get directoryID from register.
  209. lDirecotryID = AfxGetApp()->GetProfileInt(m_strSaveAsSection.data(), m_strSaveAsCategoryKey.data(), 0);
  210. strDefaultDirectory = AfxGetApp()->GetProfileString(m_strSaveAsSection.data(), m_strSaveAsDirectoryKey.data(), 0);
  211. m_cbDirectorySave.SetWindowText(strDefaultDirectory);
  212. }
  213. else
  214. {
  215. assert(m_lDirectoryID != -1);
  216. lDirecotryID = m_lDirectoryID;
  217. }
  218. // show selected category
  219. int iCurSel = 0;
  220. for (int i=0; i<m_cbCategory.GetCount(); i++)
  221. {
  222. if (GetDBItemData(i)->m_lDirectoryID == lDirecotryID)
  223. {
  224. iCurSel = i;
  225. break;
  226. }
  227. }
  228. m_bNoNotification = true;
  229. m_cbCategory.SetCurSel(iCurSel);
  230. m_bNoNotification = false;
  231. if (strDefaultDirectory.IsEmpty())
  232. OnSelchangeComboCategory();
  233. //
  234. // Show the details.
  235. //
  236. if (m_bNewOrProperty)
  237. {
  238. // OnSelchangeComboCategory();
  239. }
  240. else
  241. {
  242. if (!m_pFileDBItem)
  243. {
  244. assert(false);
  245. return;
  246. }
  247. CString strInputPath = m_strDirectorySave.data();
  248. int iSplit = strInputPath.ReverseFind('\');
  249. m_strRename = strInputPath.Right(strInputPath.GetLength() - iSplit);
  250. m_strComment = m_pFileDBItem->m_strComment.data();
  251. UpdateData(false);
  252. m_cbDirectorySave.SetWindowText(strInputPath.Left(iSplit));
  253. // only load input parameter once.
  254. m_bNewOrProperty = false;
  255. }
  256. }
  257. void CDlgNewDownload::Copy(CTreeCursor& curItem, int iIndent)
  258. {
  259. if (!curItem) return;
  260. // don't show downloading category.
  261. if (curItem == this->m_pCategoryBarWnd->m_curDownloading)
  262. return;
  263. SFOLDER* pFolder = new SFOLDER;
  264. pFolder->m_iImageIndex = curItem.GetImageID();
  265. pFolder->m_iIndent = iIndent;
  266. pFolder->m_hItem = curItem;
  267. CString sztext = curItem.GetText();
  268. int iItem = m_cbCategory.AddString(sztext);
  269. m_cbCategory.SetItemDataPtr(iItem, pFolder);
  270. if (curItem.HasChildren())
  271. {
  272. CTreeCursor curChild = curItem.GetChild();
  273. while (curChild)
  274. {
  275. Copy(curChild, iIndent+1);
  276. curChild = curChild.GetNext(TVGN_NEXT);
  277. }
  278. }
  279. }
  280. void CDlgNewDownload::OnOK() 
  281. {
  282. // start download.
  283. UpdateData();
  284. if (m_bNewOrProperty)
  285. {
  286. if (!ApplyData()) return;
  287. }
  288. else
  289. {
  290. m_treeSubFiles.GetChecks();
  291. }
  292. m_iStartMode = 0;
  293. CDialog::OnOK();
  294. }
  295. void CDlgNewDownload::OnButtonDownloadLater()
  296. {
  297. UpdateData();
  298. if (m_bNewOrProperty)
  299. {
  300. if (!ApplyData()) return;
  301. }
  302. else
  303. {
  304. m_treeSubFiles.GetChecks();
  305. }
  306. // don't start download.
  307. m_iStartMode = 1;
  308. EndDialog(IDOK);
  309. }
  310. bool CDlgNewDownload::ApplyData()
  311. {
  312. assert(m_pfiletorrent);
  313. if (m_strRename.IsEmpty())
  314. m_strRename = m_pfiletorrent->GetSaveName().data();
  315. // save the selected category.
  316. int iCurSel = m_cbCategory.GetCurSel();
  317. assert(iCurSel != -1);
  318. SFOLDER* pCategory= (SFOLDER*)m_cbCategory.GetItemDataPtr(iCurSel);
  319. assert(pCategory);
  320. m_ItemSel = pCategory->m_hItem;
  321. if (!m_ItemSel)
  322. {
  323. AfxMessageBox("invalid input");
  324. return false;
  325. }
  326. // save DirectorySave history and the selected savedirectory.
  327. CString strSave, strSaveDirectory;
  328. m_cbDirectorySave.GetWindowText(strSave);
  329. strSaveDirectory = strSave;
  330. if (strSaveDirectory.IsEmpty())
  331. {
  332. AfxMessageBox("invalid input");
  333. m_cbDirectorySave.SetFocus();
  334. return false;
  335. }
  336. m_cbDirectorySave.SaveHistory();
  337. // check the input save path is valide.
  338. strSaveDirectory = formatDir(strSaveDirectory.GetBuffer(0)).data();
  339. m_strDirectorySave = strSaveDirectory + m_strRename;
  340. DWORD dwAttr = GetFileAttributes(m_strDirectorySave.data());
  341. if (dwAttr !=  0xffffffff)
  342. {
  343. if (!m_pCategoryBarWnd)
  344. {
  345. assert(false);
  346. return false;
  347. }
  348. if (m_pCategoryBarWnd->GetSetupRegister()->m_eOperSavenameExist == CSetupRegister::ePromptSavename)
  349. {
  350. if (AfxMessageBox("同名文件(" + m_strRename + ")已经存在,是否覆盖?", MB_YESNO) != IDYES)
  351. {
  352. ((CEdit*)GetDlgItem(IDC_EDIT_RENAME))->SetSel(0, -1);
  353. GetDlgItem(IDC_EDIT_RENAME)->SetFocus();
  354. return false;
  355. }
  356. }
  357. else if (m_pCategoryBarWnd->GetSetupRegister()->m_eOperSavenameExist == CSetupRegister::eRename)
  358. {
  359. CString strSaveName = m_strRename;
  360. for (int i=0; i<100; i++)
  361. {
  362. strSaveName.Format("%s%d",  m_strRename.GetBuffer(0), i);
  363. m_strDirectorySave = strSaveDirectory + strSaveName;
  364. DWORD dwAttr = GetFileAttributes(m_strDirectorySave.data());
  365. if (dwAttr ==  0xffffffff)
  366. {
  367. m_strRename = strSaveName;
  368. m_strDirectorySave = strSaveDirectory + m_strRename;
  369. break;
  370. }
  371. }
  372. }
  373. else // overwrite.
  374. {
  375. }
  376. }
  377. m_treeSubFiles.GetChecks();
  378. return true;
  379. }
  380. void CDlgNewDownload::OnSelchangeComboCategory() 
  381. {
  382. if (m_bNoNotification)
  383. return;
  384. // show the default directory save.
  385. int iCurSel = m_cbCategory.GetCurSel();
  386. if (iCurSel == -1)
  387. {
  388. assert(iCurSel != -1);
  389. return;
  390. }
  391. CCategoryBarWnd::CCategoryDBItem* pDBItem = GetDBItemData(iCurSel);
  392. m_cbDirectorySave.SetWindowText(pDBItem->m_strDefaultDirectory.data());
  393. }
  394. void CDlgNewDownload::OnButtonCategoryNew() 
  395. {
  396. // get the item current selected. the itemsel is the default selected in new dialog.
  397. int iCurSel = m_cbCategory.GetCurSel();
  398. assert(iCurSel != -1);
  399. SFOLDER* pCategory = (SFOLDER*)m_cbCategory.GetItemDataPtr(iCurSel);
  400. assert(pCategory && pCategory->m_hItem && m_pCategoryBarWnd);
  401. CTreeCursor curNew;
  402. if (m_pCategoryBarWnd->CreateNewCategory(CTreeCursor(pCategory->m_hItem, m_pCategoryBarWnd), curNew))
  403. {
  404. // reload tree data.
  405. Load();
  406. // select the new created category.
  407. for (int i=0; i<m_cbCategory.GetCount(); i++)
  408. {
  409. SFOLDER* pCategory = (SFOLDER*)m_cbCategory.GetItemDataPtr(i);
  410. assert(pCategory && pCategory->m_hItem);
  411. if (pCategory->m_hItem == curNew)
  412. {
  413. m_cbCategory.SetCurSel(i);
  414. break;
  415. }
  416. }
  417. // refresh the directory save edit.
  418. OnSelchangeComboCategory();
  419. }
  420. m_cbCategory.SetFocus();
  421. }
  422. void CDlgNewDownload::OnButtonDirectorySelect() 
  423. {
  424. CString strDef, strFolder;
  425. m_cbDirectorySave.GetWindowText(strDef);
  426. if (SelectFolder(strDef, strFolder))
  427. {
  428. m_cbDirectorySave.SetWindowText(strFolder);
  429. }
  430. m_cbDirectorySave.SetFocus();
  431. }
  432. void CDlgNewDownload::OnButtonSaveAsDefault() 
  433. {
  434. int iCurSel = m_cbCategory.GetCurSel();
  435. CString strDefaultDir;
  436. m_cbDirectorySave.GetWindowText(strDefaultDir);
  437. // write directoryID to register.
  438. AfxGetApp()->WriteProfileInt(m_strSaveAsSection.data(), m_strSaveAsCategoryKey.data(), GetDBItemData(iCurSel)->m_lDirectoryID);
  439. AfxGetApp()->WriteProfileString(m_strSaveAsSection.data(), m_strSaveAsDirectoryKey.data(), strDefaultDir);
  440. UpdateData();
  441. SaveState();
  442. }
  443. CCategoryBarWnd::CCategoryDBItem* CDlgNewDownload::GetDBItemData(int icbSel)
  444. {
  445. assert(icbSel >= 0 && icbSel < m_cbCategory.GetCount());
  446. SFOLDER* pCategory = (SFOLDER*)m_cbCategory.GetItemDataPtr(icbSel);
  447. assert(pCategory && pCategory->m_hItem && m_pCategoryBarWnd);
  448. CTreeCursor curItem = CTreeCursor(pCategory->m_hItem, m_pCategoryBarWnd);
  449. CCategoryBarWnd::CCategoryDBItem* pDBItem = (CCategoryBarWnd::CCategoryDBItem*)curItem.GetData();
  450. assert(pDBItem);
  451. return pDBItem;
  452. }
  453. void CDlgNewDownload::OnRadioDownloadImmediately() 
  454. {
  455. // SetCheck(IDC_RADIO_DOWNLOAD_IMMEDIATELY);
  456. }
  457. void CDlgNewDownload::OnRadioDownloadManualy() 
  458. {
  459. // SetCheck(IDC_RADIO_DOWNLOAD_MANUALY);
  460. }
  461. void CDlgNewDownload::SetCheck(UINT idBtn)
  462. {
  463. /*
  464. static UINT buttons []={IDC_RADIO_DOWNLOAD_IMMEDIATELY, IDC_RADIO_DOWNLOAD_MANUALY};
  465. CButton *pBtn=NULL;
  466. for(int i=0;i<2;i++)
  467. {
  468. pBtn = (CButton*)GetDlgItem(buttons[i]);
  469. if(!pBtn) continue;
  470. if(buttons[i]==idBtn)
  471. pBtn->SetCheck(true);
  472. else
  473. pBtn->SetCheck(0);
  474. }
  475. //*/
  476. }
  477. HBRUSH CDlgNewDownload::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
  478. {
  479. HBRUSH hbr = CExpandingDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  480. if (pWnd)
  481. {
  482. switch (nCtlColor)
  483. {
  484. case CTLCOLOR_EDIT:
  485. case CTLCOLOR_STATIC:
  486. // case CTLCOLOR_BTN:
  487. switch (pWnd->GetDlgCtrlID())
  488. {
  489. case IDC_EDIT_TORRENT_FILE:
  490. // pDC->SetBkMode(TRANSPARENT);  // make text
  491. // pDC->SetTextColor(RGB(255,0,0)); // change the text color to red.
  492. pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  493. hbr = GetSysColorBrush(COLOR_WINDOW); // apply a null brush, so control's rectangle
  494. // hbr = (HBRUSH) GetStockObject(WHITE_BRUSH);
  495. break;
  496. // otherwise, do default handling of OnCtlColor
  497. default:
  498. hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
  499. }
  500. return hbr;    // return brush
  501. }
  502. }
  503. return hbr;
  504. }