void CRemote::PageDownloads() { if ( CheckCookie() ) return; m_nTab = tabDownloads; CString str; str.Format( L"%i", GetRandomNum( 0i32, _I32_MAX ) ); CSingleLock pLock( &DownloadGroups.m_pSection, TRUE ); Prepare(); // Header Add( L"random", str ); Output( L"downloadsHeader" ); BOOL bExclusive = ! GetKey( L"group_exclusive" ).IsEmpty(); BOOL bReveal = ! GetKey( L"group_reveal" ).IsEmpty(); for ( POSITION posGroup = DownloadGroups.GetIterator(); posGroup != NULL; ) { CDownloadGroup* pGroup = DownloadGroups.GetNext( posGroup ); CString strGroupID; strGroupID.Format( L"%p", pGroup ); Add( L"group_id", strGroupID ); if ( bExclusive ) pGroup->m_bRemoteSelected = ( GetKey( L"group_exclusive" ) == strGroupID ); else if ( bReveal ) pGroup->m_bRemoteSelected = TRUE; else if ( GetKey( L"group_select" ) == strGroupID ) pGroup->m_bRemoteSelected = TRUE; else if ( GetKey( L"group_deselect" ) == strGroupID ) pGroup->m_bRemoteSelected = FALSE; Add( L"group_caption", pGroup->m_sName ); if ( pGroup->m_bRemoteSelected ) Add( L"group_selected", L"true" ); Output( L"downloadsTab" ); Prepare( L"group_" ); } if ( ! GetKey( L"filter_set" ).IsEmpty() ) { Settings.Downloads.FilterMask &= ~( DLF_ACTIVE | DLF_PAUSED | DLF_QUEUED | DLF_SOURCES | DLF_SEED ); if ( GetKey( L"filter_active" ) == L"1" ) Settings.Downloads.FilterMask |= DLF_ACTIVE; if ( GetKey( L"filter_paused" ) == L"1" ) Settings.Downloads.FilterMask |= DLF_PAUSED; if ( GetKey( L"filter_queued" ) == L"1" ) Settings.Downloads.FilterMask |= DLF_QUEUED; if ( GetKey( L"filter_sources" ) == L"1" ) Settings.Downloads.FilterMask |= DLF_SOURCES; if ( GetKey( L"filter_seeds" ) == L"1" ) Settings.Downloads.FilterMask |= DLF_SEED; Settings.Downloads.ShowSources = ( GetKey( L"filter_show_all" ) == L"1" ); } Add( L"filter_active", ( Settings.Downloads.FilterMask & DLF_ACTIVE ) ? L"checked=\"checked\"" : L"" ); Add( L"filter_paused", ( Settings.Downloads.FilterMask & DLF_PAUSED ) ? L"checked=\"checked\"" : L"" ); Add( L"filter_queued", ( Settings.Downloads.FilterMask & DLF_QUEUED ) ? L"checked=\"checked\"" : L"" ); Add( L"filter_sources", ( Settings.Downloads.FilterMask & DLF_SOURCES ) ? L"checked=\"checked\"" : L"" ); Add( L"filter_seeds", ( Settings.Downloads.FilterMask & DLF_PAUSED ) ? L"checked=\"checked\"" : L"" ); Add( L"filter_show_all", Settings.Downloads.ShowSources ? L"checked=\"checked\"" : L"" ); Output( L"downloadsTop" ); for ( POSITION posDownload = Downloads.GetIterator(); posDownload != NULL; ) { CDownload* pDownload = Downloads.GetNext( posDownload ); CString strDownloadID; strDownloadID.Format( L"%p", pDownload ); if ( GetKey( L"modify_id" ) == strDownloadID ) { CString strAction = GetKey( L"modify_action" ); strAction.MakeLower(); if ( strAction == L"expand" ) { if ( CDownloadsCtrl::IsExpandable( pDownload ) ) pDownload->m_bExpanded = TRUE; } else if ( strAction == L"collapse" ) { if ( CDownloadsCtrl::IsExpandable( pDownload ) ) pDownload->m_bExpanded = FALSE; } else if ( strAction == L"resume" ) { pDownload->Resume(); } else if ( strAction == L"pause" ) { if ( ! pDownload->IsPaused() && ! pDownload->IsTasking() ) pDownload->Pause(); } else if ( strAction == L"cancel" ) { if ( ! pDownload->IsTasking() ) pDownload->Remove(); continue; } else if ( strAction == L"clear" ) { if ( pDownload->IsCompleted() && ! pDownload->IsPreviewVisible() ) { pDownload->Remove(); continue; } } else if ( strAction == L"more_sources" ) { // roo_koo_too improvement pDownload->FindMoreSources(); } } if ( CDownloadsCtrl::IsFiltered( pDownload ) ) continue; CDownloadGroup* pGroup = NULL; for ( POSITION posGroup = DownloadGroups.GetIterator(); posGroup != NULL; ) { pGroup = DownloadGroups.GetNext( posGroup ); if ( pGroup->m_bRemoteSelected && pGroup->Contains( pDownload ) ) break; pGroup = NULL; } if ( pGroup == NULL ) continue; Add( L"download_id", strDownloadID ); Add( L"download_filename", pDownload->GetDisplayName() ); Add( L"download_size", ( pDownload->m_nSize == SIZE_UNKNOWN ) ? LoadString( IDS_STATUS_UNKNOWN ) : Settings.SmartVolume( pDownload->m_nSize ) ); int nProgress = int( pDownload->GetProgress() ); str.Format( L"%i", nProgress ); Add( L"download_percent", str ); str.Format( L"%i", 100 - nProgress ); Add( L"download_percent_inverse", str ); Add( L"download_speed", Settings.SmartSpeed( pDownload->GetMeasuredSpeed() ) ); if ( CDownloadsCtrl::IsExpandable( pDownload ) ) { if ( pDownload->m_bExpanded ) Add( L"download_is_expanded", L"true" ); else Add( L"download_is_collapsed", L"true" ); } if ( pDownload->IsCompleted() ) Add( L"download_is_complete", L"true" ); else if ( pDownload->IsPaused() ) Add( L"download_is_paused", L"true" ); Add( L"download_status", pDownload->GetDownloadStatus() ); Add( L"download_sources", pDownload->GetDownloadSources() ); Output( L"downloadsDownload" ); if ( pDownload->m_bExpanded && CDownloadsCtrl::IsExpandable( pDownload ) ) { for ( POSITION posSource = pDownload->GetIterator(); posSource; ) { CDownloadSource* pSource = pDownload->GetNext( posSource ); ASSERT( pSource->m_pDownload == pDownload ); CString strSourceID; strSourceID.Format( L"%p", pSource ); if ( GetKey( L"modify_id" ) == strSourceID ) { CString strModifyAction = GetKey( L"modify_action" ); strModifyAction.MakeLower(); if ( strModifyAction == L"access" ) { // Only create a new Transfer if there isn't already one if ( pSource->IsIdle() && pSource->m_nProtocol != PROTOCOL_ED2K ) { if ( pDownload->IsPaused() ) pDownload->Resume(); // Workaround duplicate pDownload->Resume(); if ( pSource->m_bPushOnly ) pSource->PushRequest(); else if ( CDownloadTransfer* pTransfer = pSource->CreateTransfer() ) pTransfer->Initiate(); } } else if ( strModifyAction == L"forget" ) { pSource->Remove( TRUE, TRUE ); continue; } } if ( Settings.Downloads.ShowSources || pSource->IsConnected() ) { Add( L"source_id", strSourceID ); Add( L"source_agent", pSource->m_sServer ); Add( L"source_nick", pSource->m_sNick ); if ( ! pSource->IsIdle() ) { Add( L"source_status", pSource->GetState( FALSE ) ); Add( L"source_volume", Settings.SmartVolume( pSource->GetDownloaded() ) ); if ( DWORD nSpeed = pSource->GetMeasuredSpeed() ) Add( L"source_speed", Settings.SmartSpeed( nSpeed ) ); Add( L"source_address", pSource->GetAddress() ); Add( L"source_caption", pSource->GetAddress() + L" - " + pSource->m_sNick ); } else // No transfer { Add( L"source_address", CString( inet_ntoa( pSource->m_pAddress ) ) ); Add( L"source_caption", CString( inet_ntoa( pSource->m_pAddress ) ) + L" - " + pSource->m_sNick ); if ( pSource->m_tAttempt > 0 ) { DWORD tNow = GetTickCount(); if ( pSource->m_tAttempt >= tNow ) { tNow = ( pSource->m_tAttempt - tNow ) / 1000; CString strSourceStatus; strSourceStatus.Format( L"%.2u:%.2u", tNow / 60, tNow % 60 ); Add( L"source_status", strSourceStatus ); } } } Output( L"downloadsSource" ); Prepare( L"source_" ); } } } Prepare( L"download_" ); } // for POSITION loop Output( L"downloadsBottom" ); Output( L"downloadsFooter" ); }
BOOL CDownloadWithSources::AddSourceInternal(CDownloadSource* pSource) { if ( ! pSource ) return FALSE; // Out of memory if ( GetEffectiveSourceCount() > Settings.Downloads.SourcesWanted ) { delete pSource; return FALSE; // Too many sources } // Check/Reject if source is invalid if ( ! pSource->m_bPushOnly ) { // Reject invalid IPs (Sometimes ed2k sends invalid 0.x.x.x sources) if ( pSource->m_pAddress.S_un.S_un_b.s_b1 == 0 || pSource->m_nPort == 0 ) { delete pSource; return FALSE; } // Reject if source is the local IP/port if ( Network.IsSelfIP( pSource->m_pAddress ) ) { if ( Settings.Connection.IgnoreOwnIP || ( pSource->m_nServerPort == 0 && Settings.Connection.InPort == pSource->m_nPort ) ) { delete pSource; return FALSE; } } } else if ( pSource->m_nProtocol == PROTOCOL_ED2K ) { // Reject invalid server IPs (Sometimes ed2k sends invalid 0.x.x.x sources) if ( pSource->m_pServerAddress.S_un.S_un_b.s_b1 == 0 ) { delete pSource; return FALSE; } } CQuickLock pLock( Transfers.m_pSection ); if ( pSource->m_nRedirectionCount == 0 ) // Don't check for existing sources if source is a redirection { bool bDeleteSource = false; bool bHTTPSource = pSource->IsHTTPSource(); bool bNeedHTTPSource = ! bHTTPSource && Settings.Gnutella2.Enabled && VendorCache.IsExtended( pSource->m_sServer ); // Remove unneeded sources for ( POSITION posSource = GetIterator() ; posSource ; ) { CDownloadSource* pExisting = GetNext( posSource ); ASSERT( pSource != pExisting ); if ( pExisting->Equals( pSource ) ) // IPs and ports are equal { bool bExistingHTTPSource = pExisting->IsHTTPSource(); if ( bNeedHTTPSource && bExistingHTTPSource ) bNeedHTTPSource = false; if ( ( pExisting->m_nProtocol == pSource->m_nProtocol ) || ( bExistingHTTPSource && bHTTPSource ) ) { bDeleteSource = true; // Same protocol } else if ( ! pExisting->IsIdle() ) { // Already downloading so we can remove new non-HTTP source if ( bExistingHTTPSource && ! bHTTPSource ) bDeleteSource = true; } else // We are not downloading { // ...So we can replace non-HTTP source with a new one if ( ! bExistingHTTPSource && bHTTPSource ) { // Set connection delay the same as for the old source pSource->m_tAttempt = pExisting->m_tAttempt; pExisting->Remove( TRUE, FALSE ); } } } } if ( bDeleteSource ) { delete pSource; SetModified(); return FALSE; } // Make additional G2 source from the existing non-HTTP PeerProject source if ( bNeedHTTPSource ) { CString strURL = GetURL( pSource->m_pAddress, pSource->m_nPort ); if ( ! strURL.IsEmpty() ) { if ( CDownloadSource* pG2Source = new CDownloadSource( (CDownload*)this, strURL ) ) { pG2Source->m_sServer = pSource->m_sServer; // Copy user-agent pG2Source->m_tAttempt = pSource->m_tAttempt; // Set the same connection delay pG2Source->m_nProtocol = PROTOCOL_HTTP; AddSourceInternal( pG2Source ); } } } } InternalAdd( pSource ); SetModified(); return TRUE; }