⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlgdownloadmonitor.cpp

📁 著名的下载软件核心Shareaza
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// DlgDownloadMonitor.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "Transfers.h"
#include "Downloads.h"
#include "Download.h"
#include "FragmentedFile.h"
#include "FragmentBar.h"
#include "GraphLine.h"
#include "GraphItem.h"
#include "Library.h"
#include "FileExecutor.h"
#include "CoolInterface.h"
#include "CoolMenu.h"
#include "ShellIcons.h"
#include "Plugins.h"
#include "Skin.h"
#include "DlgDownloadMonitor.h"
#include "WndMain.h"
#include "WndDownloads.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CDownloadMonitorDlg, CSkinDialog)
	//{{AFX_MSG_MAP(CDownloadMonitorDlg)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_DOWNLOAD_CANCEL, OnDownloadCancel)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_DOWNLOAD_LAUNCH, OnDownloadLaunch)
	ON_BN_CLICKED(IDC_DOWNLOAD_LIBRARY, OnDownloadLibrary)
	ON_BN_CLICKED(IDC_DOWNLOAD_STOP, OnDownloadStop)
	ON_WM_CLOSE()
	ON_WM_SYSCOMMAND()
	ON_WM_CTLCOLOR()
	ON_WM_CONTEXTMENU()
	ON_WM_INITMENUPOPUP()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_TRAY, OnTray)
	ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnNeedText)
END_MESSAGE_MAP()

CPtrList CDownloadMonitorDlg::m_pWindows;


/////////////////////////////////////////////////////////////////////////////
// CDownloadMonitorDlg dialog

CDownloadMonitorDlg::CDownloadMonitorDlg(CDownload* pDownload) : CSkinDialog( CDownloadMonitorDlg::IDD, NULL )
{
	//{{AFX_DATA_INIT(CDownloadMonitorDlg)
	//}}AFX_DATA_INIT
	
	m_pDownload		= pDownload;
	m_pGraph		= NULL;
	m_bTray			= FALSE;
	m_bCompleted	= FALSE;
	
	CreateReal( IDD );
	
	m_pWindows.AddTail( this );
}

CDownloadMonitorDlg::~CDownloadMonitorDlg()
{
	if ( m_pGraph != NULL ) delete m_pGraph;
	if ( POSITION pos = m_pWindows.Find( this ) ) m_pWindows.RemoveAt( pos );
}
 
void CDownloadMonitorDlg::DoDataExchange(CDataExchange* pDX)
{
	CSkinDialog::DoDataExchange( pDX );
	//{{AFX_DATA_MAP(CDownloadMonitorDlg)
	DDX_Control(pDX, IDC_DOWNLOAD_VOLUME, m_wndVolume);
	DDX_Control(pDX, IDC_DOWNLOAD_CANCEL, m_wndCancel);
	DDX_Control(pDX, IDC_DOWNLOAD_CLOSE, m_wndClose);
	DDX_Control(pDX, IDC_DOWNLOAD_STOP, m_wndStop);
	DDX_Control(pDX, IDC_PROGRESS, m_wndProgress);
	DDX_Control(pDX, IDC_DOWNLOAD_TIME, m_wndTime);
	DDX_Control(pDX, IDC_DOWNLOAD_STATUS, m_wndStatus);
	DDX_Control(pDX, IDC_DOWNLOAD_SPEED, m_wndSpeed);
	DDX_Control(pDX, IDC_DOWNLOAD_SOURCES, m_wndSources);
	DDX_Control(pDX, IDC_DOWNLOAD_LIBRARY, m_wndLibrary);
	DDX_Control(pDX, IDC_DOWNLOAD_LAUNCH, m_wndLaunch);
	DDX_Control(pDX, IDC_DOWNLOAD_ICON, m_wndIcon);
	DDX_Control(pDX, IDC_DOWNLOAD_GRAPH, m_wndGraph);
	DDX_Control(pDX, IDC_DOWNLOAD_FILE, m_wndFile);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CDownloadMonitorDlg operations

BOOL CDownloadMonitorDlg::CreateReal(UINT nID)
{
	LPCTSTR lpszTemplateName = MAKEINTRESOURCE( nID );

	HINSTANCE hInst		= AfxFindResourceHandle( lpszTemplateName, RT_DIALOG );
	HRSRC hResource		= ::FindResource( hInst, lpszTemplateName, RT_DIALOG );
	HGLOBAL hTemplate	= LoadResource( hInst, hResource );

	LPCDLGTEMPLATE lpDialogTemplate = (LPCDLGTEMPLATE)LockResource( hTemplate );

	BOOL bResult = CreateDlgIndirect( lpDialogTemplate, NULL, hInst );

	UnlockResource( hTemplate );

	FreeResource( hTemplate );

	return bResult;
}

void CDownloadMonitorDlg::OnSkinChange(BOOL bSet)
{
	for ( POSITION pos = m_pWindows.GetHeadPosition() ; pos ; )
	{
		CDownloadMonitorDlg* pDlg = (CDownloadMonitorDlg*)m_pWindows.GetNext( pos );

		if ( bSet )
		{
			pDlg->SkinMe( _T("CDownloadMonitorDlg"), IDI_DOWNLOAD_MONITOR );
			pDlg->Invalidate();
		}
		else
		{
			pDlg->m_pSkin = NULL;
		}
	}
}

void CDownloadMonitorDlg::CloseAll()
{
	for ( POSITION pos = m_pWindows.GetHeadPosition() ; pos ; )
	{
		delete (CDownloadMonitorDlg*)m_pWindows.GetNext( pos );
	}
	m_pWindows.RemoveAll();
}

/////////////////////////////////////////////////////////////////////////////
// CDownloadMonitorDlg message handlers

BOOL CDownloadMonitorDlg::OnInitDialog() 
{
	CSkinDialog::OnInitDialog();
	
	SkinMe( _T("CDownloadMonitorDlg"), ID_DOWNLOADS_MONITOR );
	
	CMenu* pMenu = GetSystemMenu( FALSE );
	pMenu->InsertMenu( 0, MF_BYPOSITION|MF_SEPARATOR, ID_SEPARATOR );
	pMenu->InsertMenu( 0, MF_BYPOSITION|MF_STRING, SC_NEXTWINDOW, _T("&Always on Top") );
	
	CSingleLock pLock( &Transfers.m_pSection, TRUE );
	
	if ( Downloads.Check( m_pDownload ) )
	{
		m_sName = m_pDownload->m_sRemoteName;
		CString strType = m_sName;

		int nPeriod = strType.ReverseFind( '.' );

		if ( nPeriod > 0 )
		{
			strType = strType.Mid( nPeriod );
			HICON hIcon;

			if ( ShellIcons.Lookup( strType, NULL, &hIcon, NULL, NULL ) )
				m_wndIcon.SetIcon( hIcon );
		}

		m_wndFile.SetWindowText( m_sName );
	}

	pLock.Unlock();

	m_pGraph	= new CLineGraph();
	m_pItem		= new CGraphItem( 0, 0, RGB( 0xFF, 0, 0 ) );

	m_pGraph->m_bShowLegend		= FALSE;
	m_pGraph->m_bShowAxis		= FALSE;
	m_pGraph->m_crBack			= RGB( 255, 255, 240 );
	m_pGraph->m_crGrid			= RGB( 220, 220, 170 );
	m_pGraph->m_nMinGridVert	= 16;

	m_pGraph->AddItem( m_pItem );

	OnTimer( 1 );

	CenterWindow();
	ShowWindow( SW_SHOW );

	SetTimer( 1, 100, NULL );
	EnableToolTips();
	
	return TRUE;
}

void CDownloadMonitorDlg::OnDestroy() 
{
	KillTimer( 1 );
	
	if ( m_pDownload != NULL )
	{
		CSingleLock pLock( &Transfers.m_pSection );
		
		if ( pLock.Lock( 250 ) )
		{
			if ( Downloads.Check( m_pDownload ) ) m_pDownload->m_pMonitorWnd = NULL;
			m_pDownload = NULL;
			pLock.Unlock();
		}
	}
	
	if ( m_bTray )
	{
		Shell_NotifyIcon( NIM_DELETE, &m_pTray );
		m_bTray = FALSE;
	}
	
	CSkinDialog::OnDestroy();
}

void CDownloadMonitorDlg::PostNcDestroy() 
{
	CSkinDialog::PostNcDestroy();
	delete this;
}

void CDownloadMonitorDlg::OnTimer(UINT nIDEvent) 
{
	CSingleLock pLock( &Transfers.m_pSection );
	if ( ! pLock.Lock( 250 ) ) return;
	
	if ( ! m_pDownload || ! Downloads.Check( m_pDownload ) )
	{
		KillTimer( 1 );
		PostMessage( WM_CLOSE );
		return;
	}
	
	if ( m_bCompleted ) return;
	
	BOOL bCompleted	= m_pDownload->IsCompleted();
	DWORD nSpeed	= m_pDownload->GetMeasuredSpeed() * 8;
	CString strText, strFormat;
	
	m_pItem->Add( nSpeed );
	m_pGraph->m_nUpdates++;
	m_pGraph->m_nMaximum = max( m_pGraph->m_nMaximum, nSpeed );
	
	if ( m_pDownload->IsStarted() )
	{
		strText.Format( _T("%.1f%% of %s : Shareaza"),
			m_pDownload->GetProgress() * 100, (LPCTSTR)m_pDownload->m_sRemoteName );
	}
	else
	{
		strText.Format( _T("%s : Shareaza"),
			(LPCTSTR)m_pDownload->m_sRemoteName );
	}
	
	Update( this, strText );
	
	if ( m_bTray )
	{
		if ( _tcsncmp( m_pTray.szTip, strText, 63 ) )
		{
			m_pTray.uFlags = NIF_TIP;
			_tcsncpy( m_pTray.szTip, strText, 63 );
			Shell_NotifyIcon( NIM_MODIFY, &m_pTray );
		}
	}
	
	if ( bCompleted )
	{
		if ( m_wndClose.GetCheck() )
		{
			PostMessage( WM_CLOSE );
		}
		else
		{
			ShowWindow( SW_SHOWNORMAL );
			SetForegroundWindow();
		}
		
		m_bCompleted = TRUE;
	}
	else
	{
		if ( IsIconic() || m_bTray ) return;
	}
	
	int nSourceCount	= m_pDownload->GetSourceCount();
	int nTransferCount	= m_pDownload->GetTransferCount();
	
	CString strNA;
	LoadString( strNA, IDS_TIP_NA );
	
	if ( bCompleted )
	{
		LoadString( strText, IDS_DLM_COMPLETED );
		Update( &m_wndStatus, strText );
		Update( &m_wndTime, strNA );
		Update( &m_wndSpeed, strNA );
		LoadString( strText, IDS_DLM_COMPLETED_WORD );
		Update( &m_wndSources, strText );
	}
	else if ( m_pDownload->IsMoving() )
	{
		LoadString( strText, IDS_DLM_MOVING );
		Update( &m_wndStatus, strText );
		Update( &m_wndTime, strNA );
		Update( &m_wndSpeed, strNA );
		LoadString( strText, IDS_DLM_COMPLETED_WORD );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -