DlgNewDownload.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:16k
源码类别:
P2P编程
开发平台:
Visual C++
- // DlgNewDownload.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "DlgNewDownload.h"
- #include "CategoryNewDlg.h"
- #include "FileBase.h"
- #include "FileTorrent.h"
- #include "SetupRegister.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- const string CDlgNewDownload::m_strSaveAsSection = "SaveAs";
- const string CDlgNewDownload::m_strSaveAsCategoryKey = "Category";
- const string CDlgNewDownload::m_strSaveAsDirectoryKey = "DefaultDirectory";
- const CString CDlgNewDownload::m_strSection = "DlgNewDownload";
- /////////////////////////////////////////////////////////////////////////////
- // CDlgNewDownload dialog
- CDlgNewDownload::CDlgNewDownload(CImageList* pImageList,
- CFileTorrent* pfiletorrent, CCategoryBarWnd* pCategoryBarWnd, bool bNewOrProperty, CFileDBItem* pFileDBItem, long lDirectoryID, string strDirectorySave, CWnd* pParent /*=NULL*/)
- : CExpandingDialog(CDlgNewDownload::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDlgNewDownload)
- m_strRename = _T("");
- m_strInfohash = _T("");
- m_strUrl = _T("");
- m_strComment = _T("");
- m_strTorrentFileName = _T("");
- m_iStartMode = -1;
- //}}AFX_DATA_INIT
- m_pImageList = pImageList;
- m_pfiletorrent = pfiletorrent;
- m_pCategoryBarWnd = pCategoryBarWnd;
- m_bNewOrProperty = bNewOrProperty;
- m_lDirectoryID = lDirectoryID;
- m_strDirectorySave = strDirectorySave;
- m_bNoNotification = false;
- m_iStartMode = 0;
- m_lSubFilesWndWidth = 300;
- m_pFileDBItem = pFileDBItem;
- }
- void CDlgNewDownload::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgNewDownload)
- DDX_Control(pDX, IDC_BUTTON_MORE, m_btnMore);
- DDX_Control(pDX, IDC_BUTTON_DOWNLOAD_LATER, m_btnDownloadLater);
- DDX_Control(pDX, IDC_BUTTON_DIRECTORY_SELECT, m_btnBrowseDir);
- DDX_Control(pDX, IDC_BUTTON_CATEGORY_NEW, m_btnCategoryNew);
- DDX_Control(pDX, IDC_BUTTON_SAVE_AS_DEFAULT, m_btnSaveAsDef);
- DDX_Control(pDX, IDCANCEL, m_btnCancel);
- DDX_Control(pDX, IDOK, m_btnOK);
- DDX_Control(pDX, IDC_TREE_SUBFILES, m_treeSubFiles);
- DDX_Control(pDX, IDC_COMBO_DIRECTORY_SAVE, m_cbDirectorySave);
- DDX_Control(pDX, IDC_COMBO_CATEGORY, m_cbCategory);
- DDX_Text(pDX, IDC_EDIT_RENAME, m_strRename);
- DDX_Text(pDX, IDC_EDIT_INFOHASH, m_strInfohash);
- DDX_Text(pDX, IDC_EDIT_URL, m_strUrl);
- DDX_Text(pDX, IDC_EDIT_COMMENT, m_strComment);
- DDX_Text(pDX, IDC_EDIT_TORRENT_FILE, m_strTorrentFileName);
- DDX_Radio(pDX, IDC_RADIO_DOWNLOAD_IMMEDIATELY, m_iStartMode);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgNewDownload, CExpandingDialog)
- //{{AFX_MSG_MAP(CDlgNewDownload)
- ON_CBN_SELCHANGE(IDC_COMBO_CATEGORY, OnSelchangeComboCategory)
- ON_BN_CLICKED(IDC_BUTTON_CATEGORY_NEW, OnButtonCategoryNew)
- ON_BN_CLICKED(IDC_BUTTON_DIRECTORY_SELECT, OnButtonDirectorySelect)
- ON_BN_CLICKED(IDC_BUTTON_SAVE_AS_DEFAULT, OnButtonSaveAsDefault)
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_RADIO_DOWNLOAD_MANUALY, OnRadioDownloadManualy)
- ON_BN_CLICKED(IDC_RADIO_DOWNLOAD_IMMEDIATELY, OnRadioDownloadImmediately)
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDC_BUTTON_DOWNLOAD_LATER, OnButtonDownloadLater)
- ON_MESSAGE(WM_SPLITTER_MOVED, OnSplitterMoved)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgNewDownload message handlers
- void CDlgNewDownload::LoadState()
- {
- CString strKey = "width";
- // m_lSubFilesWndWidth = AfxGetApp()->GetProfileInt(m_strSection, strKey, m_lSubFilesWndWidth);
- strKey = "expand";
- bool bExpand = AfxGetApp()->GetProfileInt(m_strSection, strKey, false) != 0;
- Expand(bExpand);
- strKey = "startMode";
- m_iStartMode = AfxGetApp()->GetProfileInt(m_strSection, strKey, m_iStartMode);
- }
- void CDlgNewDownload::SaveStateWhenDestory()
- {
- CString strKey = "width";
- // AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_lSubFilesWndWidth);
- strKey = "expand";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, IsExpanded());
- }
- void CDlgNewDownload::SaveState()
- {
- CString strKey = "startMode";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_iStartMode);
- }
- BOOL CDlgNewDownload::OnInitDialog()
- {
- CExpandingDialog::OnInitDialog();
- //
- // Create BitmapList
- //
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_BITMAP_DLG_ICONS),
- IMAGE_BITMAP,
- 0,0, // cx,cy
- LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
- CBitmap bm;
- bm.Attach(hbm);
- m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 3, 4);
- m_ctlImage.SetBkColor(RGB(255, 255,255));
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- //
- // set button image.
- //
- // m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存路径");
- m_btnOK.Create(m_ctlImage.ExtractIcon(0));
- m_btnCancel.Create(m_ctlImage.ExtractIcon(1));
- m_btnDownloadLater.Create(m_ctlImage.ExtractIcon(3));
- m_btnSaveAsDef.Create(m_ctlImage.ExtractIcon(4), true, "保存为缺省设置");
- m_btnCategoryNew.Create(m_ctlImage.ExtractIcon(2), true, "添加类别");
- m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存目录");
- // m_btnDownloadLater.ShowWindow(m_bNewOrProperty);
- m_btnDownloadLater.SetWindowText(m_bNewOrProperty ? "添加下载" : "保存属性");
- assert(m_pCategoryBarWnd);
- SetWindowText(m_bNewOrProperty ? "添加新的下载" : "属性");
- CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_COMMENT);
- if (pEdit)
- {
- pEdit->SetLimitText(100);
- }
- //
- // load register setup.
- //
- LoadState();
- UpdateData(false);
- // load DirectorySave history
- m_cbDirectorySave.LoadHistory("DirectorySave", "HistoryComboEx");
- //
- // load data.
- //
- m_treeSubFiles.Init();
- Load();
- LoadTorrent();
- /*
- // Create splitter.
- // m_wndHSplitterBar.Create(WS_CHILD|WS_BORDER|WS_DLGFRAME|WS_VISIBLE, CRect (0, 0, 100, 100), this, 112909, FALSE);
- CRect rc;
- m_treeSubFiles.GetWindowRect(rc);
- ScreenToClient(rc);
- rc.right = rc.left + m_lSubFilesWndWidth;
- m_treeSubFiles.MoveWindow(rc);
- m_wndHSplitterBar.SetPanes(&m_treeSubFiles, GetDlgItem(IDC_EDIT_COMMENT));
- //*/
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CDlgNewDownload::OnDestroy()
- {
- SaveStateWhenDestory();
- CExpandingDialog::OnDestroy();
- }
- void CDlgNewDownload::OnSplitterMoved(long lParam, long wParam)
- {
- CRect rc;
- m_treeSubFiles.GetClientRect(rc);
- long lTreeCX = rc.Width();
- m_lSubFilesWndWidth = lTreeCX;
- }
- void CDlgNewDownload::LoadTorrent()
- {
- assert(m_pfiletorrent && m_pfiletorrent->GetResponse());
- if (!m_bNewOrProperty)
- assert(m_pFileDBItem);
- m_treeSubFiles.Monitor(m_pfiletorrent, m_bNewOrProperty ? string() : m_strDirectorySave, m_pFileDBItem);
- m_treeSubFiles.ShowSubFiles();
- if (m_bNewOrProperty)
- {
- m_strComment = m_pfiletorrent->GetComment().data();
- m_strRename = m_pfiletorrent->GetSaveName().data();
- }
- m_strInfohash = m_pfiletorrent->GetInfohashString().data();
- m_strUrl = m_pfiletorrent->GetUrl().data();
- m_strTorrentFileName = m_pfiletorrent->GetTorrentFileName().data();
- UpdateData(false);
- }
- void CDlgNewDownload::Load()
- {
- assert(m_pImageList);
- // show the categorys.
- m_cbCategory.m_pimgList = m_pImageList; // &(m_pCategoryBarWnd->m_ctlImage);
- m_cbCategory.DeleteAllItems();
- Copy(m_pCategoryBarWnd->GetRootItem());
- //
- // get directoryID
- //
- CString strDefaultDirectory;
- int lDirecotryID = 0;
- if (m_bNewOrProperty)
- {
- // get directoryID from register.
- lDirecotryID = AfxGetApp()->GetProfileInt(m_strSaveAsSection.data(), m_strSaveAsCategoryKey.data(), 0);
- strDefaultDirectory = AfxGetApp()->GetProfileString(m_strSaveAsSection.data(), m_strSaveAsDirectoryKey.data(), 0);
- m_cbDirectorySave.SetWindowText(strDefaultDirectory);
- }
- else
- {
- assert(m_lDirectoryID != -1);
- lDirecotryID = m_lDirectoryID;
- }
- // show selected category
- int iCurSel = 0;
- for (int i=0; i<m_cbCategory.GetCount(); i++)
- {
- if (GetDBItemData(i)->m_lDirectoryID == lDirecotryID)
- {
- iCurSel = i;
- break;
- }
- }
- m_bNoNotification = true;
- m_cbCategory.SetCurSel(iCurSel);
- m_bNoNotification = false;
- if (strDefaultDirectory.IsEmpty())
- OnSelchangeComboCategory();
- //
- // Show the details.
- //
- if (m_bNewOrProperty)
- {
- // OnSelchangeComboCategory();
- }
- else
- {
- if (!m_pFileDBItem)
- {
- assert(false);
- return;
- }
- CString strInputPath = m_strDirectorySave.data();
- int iSplit = strInputPath.ReverseFind('\');
- m_strRename = strInputPath.Right(strInputPath.GetLength() - iSplit);
- m_strComment = m_pFileDBItem->m_strComment.data();
- UpdateData(false);
- m_cbDirectorySave.SetWindowText(strInputPath.Left(iSplit));
- // only load input parameter once.
- m_bNewOrProperty = false;
- }
- }
- void CDlgNewDownload::Copy(CTreeCursor& curItem, int iIndent)
- {
- if (!curItem) return;
- // don't show downloading category.
- if (curItem == this->m_pCategoryBarWnd->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);
- }
- }
- }
- void CDlgNewDownload::OnOK()
- {
- // start download.
- UpdateData();
- if (m_bNewOrProperty)
- {
- if (!ApplyData()) return;
- }
- else
- {
- m_treeSubFiles.GetChecks();
- }
- m_iStartMode = 0;
- CDialog::OnOK();
- }
- void CDlgNewDownload::OnButtonDownloadLater()
- {
- UpdateData();
- if (m_bNewOrProperty)
- {
- if (!ApplyData()) return;
- }
- else
- {
- m_treeSubFiles.GetChecks();
- }
- // don't start download.
- m_iStartMode = 1;
- EndDialog(IDOK);
- }
- bool CDlgNewDownload::ApplyData()
- {
- assert(m_pfiletorrent);
- if (m_strRename.IsEmpty())
- m_strRename = m_pfiletorrent->GetSaveName().data();
- // save the selected category.
- int iCurSel = m_cbCategory.GetCurSel();
- assert(iCurSel != -1);
- SFOLDER* pCategory= (SFOLDER*)m_cbCategory.GetItemDataPtr(iCurSel);
- assert(pCategory);
- m_ItemSel = pCategory->m_hItem;
- if (!m_ItemSel)
- {
- AfxMessageBox("invalid input");
- return false;
- }
- // save DirectorySave history and the selected savedirectory.
- CString strSave, strSaveDirectory;
- m_cbDirectorySave.GetWindowText(strSave);
- strSaveDirectory = strSave;
- if (strSaveDirectory.IsEmpty())
- {
- AfxMessageBox("invalid input");
- m_cbDirectorySave.SetFocus();
- return false;
- }
- m_cbDirectorySave.SaveHistory();
- // check the input save path is valide.
- strSaveDirectory = formatDir(strSaveDirectory.GetBuffer(0)).data();
- m_strDirectorySave = strSaveDirectory + m_strRename;
- DWORD dwAttr = GetFileAttributes(m_strDirectorySave.data());
- if (dwAttr != 0xffffffff)
- {
- if (!m_pCategoryBarWnd)
- {
- assert(false);
- return false;
- }
- if (m_pCategoryBarWnd->GetSetupRegister()->m_eOperSavenameExist == CSetupRegister::ePromptSavename)
- {
- if (AfxMessageBox("同名文件(" + m_strRename + ")已经存在,是否覆盖?", MB_YESNO) != IDYES)
- {
- ((CEdit*)GetDlgItem(IDC_EDIT_RENAME))->SetSel(0, -1);
- GetDlgItem(IDC_EDIT_RENAME)->SetFocus();
- return false;
- }
- }
- else if (m_pCategoryBarWnd->GetSetupRegister()->m_eOperSavenameExist == CSetupRegister::eRename)
- {
- CString strSaveName = m_strRename;
- for (int i=0; i<100; i++)
- {
- strSaveName.Format("%s%d", m_strRename.GetBuffer(0), i);
- m_strDirectorySave = strSaveDirectory + strSaveName;
- DWORD dwAttr = GetFileAttributes(m_strDirectorySave.data());
- if (dwAttr == 0xffffffff)
- {
- m_strRename = strSaveName;
- m_strDirectorySave = strSaveDirectory + m_strRename;
- break;
- }
- }
- }
- else // overwrite.
- {
- }
- }
- m_treeSubFiles.GetChecks();
- return true;
- }
- void CDlgNewDownload::OnSelchangeComboCategory()
- {
- if (m_bNoNotification)
- return;
- // show the default directory save.
- int iCurSel = m_cbCategory.GetCurSel();
- if (iCurSel == -1)
- {
- assert(iCurSel != -1);
- return;
- }
- CCategoryBarWnd::CCategoryDBItem* pDBItem = GetDBItemData(iCurSel);
- m_cbDirectorySave.SetWindowText(pDBItem->m_strDefaultDirectory.data());
- }
- void CDlgNewDownload::OnButtonCategoryNew()
- {
- // get the item current selected. the itemsel is the default selected in new dialog.
- int iCurSel = m_cbCategory.GetCurSel();
- assert(iCurSel != -1);
- SFOLDER* pCategory = (SFOLDER*)m_cbCategory.GetItemDataPtr(iCurSel);
- assert(pCategory && pCategory->m_hItem && m_pCategoryBarWnd);
- CTreeCursor curNew;
- if (m_pCategoryBarWnd->CreateNewCategory(CTreeCursor(pCategory->m_hItem, m_pCategoryBarWnd), curNew))
- {
- // reload tree data.
- Load();
- // select the new created category.
- for (int i=0; i<m_cbCategory.GetCount(); i++)
- {
- SFOLDER* pCategory = (SFOLDER*)m_cbCategory.GetItemDataPtr(i);
- assert(pCategory && pCategory->m_hItem);
- if (pCategory->m_hItem == curNew)
- {
- m_cbCategory.SetCurSel(i);
- break;
- }
- }
- // refresh the directory save edit.
- OnSelchangeComboCategory();
- }
- m_cbCategory.SetFocus();
- }
- void CDlgNewDownload::OnButtonDirectorySelect()
- {
- CString strDef, strFolder;
- m_cbDirectorySave.GetWindowText(strDef);
- if (SelectFolder(strDef, strFolder))
- {
- m_cbDirectorySave.SetWindowText(strFolder);
- }
- m_cbDirectorySave.SetFocus();
- }
- void CDlgNewDownload::OnButtonSaveAsDefault()
- {
- int iCurSel = m_cbCategory.GetCurSel();
- CString strDefaultDir;
- m_cbDirectorySave.GetWindowText(strDefaultDir);
- // write directoryID to register.
- AfxGetApp()->WriteProfileInt(m_strSaveAsSection.data(), m_strSaveAsCategoryKey.data(), GetDBItemData(iCurSel)->m_lDirectoryID);
- AfxGetApp()->WriteProfileString(m_strSaveAsSection.data(), m_strSaveAsDirectoryKey.data(), strDefaultDir);
- UpdateData();
- SaveState();
- }
- CCategoryBarWnd::CCategoryDBItem* CDlgNewDownload::GetDBItemData(int icbSel)
- {
- assert(icbSel >= 0 && icbSel < m_cbCategory.GetCount());
- SFOLDER* pCategory = (SFOLDER*)m_cbCategory.GetItemDataPtr(icbSel);
- assert(pCategory && pCategory->m_hItem && m_pCategoryBarWnd);
- CTreeCursor curItem = CTreeCursor(pCategory->m_hItem, m_pCategoryBarWnd);
- CCategoryBarWnd::CCategoryDBItem* pDBItem = (CCategoryBarWnd::CCategoryDBItem*)curItem.GetData();
- assert(pDBItem);
- return pDBItem;
- }
- void CDlgNewDownload::OnRadioDownloadImmediately()
- {
- // SetCheck(IDC_RADIO_DOWNLOAD_IMMEDIATELY);
- }
- void CDlgNewDownload::OnRadioDownloadManualy()
- {
- // SetCheck(IDC_RADIO_DOWNLOAD_MANUALY);
- }
- void CDlgNewDownload::SetCheck(UINT idBtn)
- {
- /*
- static UINT buttons []={IDC_RADIO_DOWNLOAD_IMMEDIATELY, IDC_RADIO_DOWNLOAD_MANUALY};
- CButton *pBtn=NULL;
- for(int i=0;i<2;i++)
- {
- pBtn = (CButton*)GetDlgItem(buttons[i]);
- if(!pBtn) continue;
- if(buttons[i]==idBtn)
- pBtn->SetCheck(true);
- else
- pBtn->SetCheck(0);
- }
- //*/
- }
- HBRUSH CDlgNewDownload::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CExpandingDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- if (pWnd)
- {
- switch (nCtlColor)
- {
- case CTLCOLOR_EDIT:
- case CTLCOLOR_STATIC:
- // case CTLCOLOR_BTN:
- switch (pWnd->GetDlgCtrlID())
- {
- case IDC_EDIT_TORRENT_FILE:
- // pDC->SetBkMode(TRANSPARENT); // make text
- // pDC->SetTextColor(RGB(255,0,0)); // change the text color to red.
- pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
- hbr = GetSysColorBrush(COLOR_WINDOW); // apply a null brush, so control's rectangle
- // hbr = (HBRUSH) GetStockObject(WHITE_BRUSH);
- break;
- // otherwise, do default handling of OnCtlColor
- default:
- hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
- }
- return hbr; // return brush
- }
- }
- return hbr;
- }