00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "CoolInterface.h"
00026 #include "FragmentBar.h"
00027
00028 #include "Download.h"
00029 #include "DownloadSource.h"
00030 #include "DownloadTransfer.h"
00031 #include "DownloadTransferHTTP.h"
00032 #include "DownloadTransferED2K.h"
00033 #include "DownloadTransferBT.h"
00034 #include "UploadFile.h"
00035 #include "UploadTransfer.h"
00036 #include "UploadTransferHTTP.h"
00037 #include "UploadTransferED2K.h"
00038 #include "FragmentedFile.h"
00039
00040 #ifdef _DEBUG
00041 #undef THIS_FILE
00042 static char THIS_FILE[]=__FILE__;
00043 #define new DEBUG_NEW
00044 #endif
00045
00046
00048
00049
00050 void CFragmentBar::DrawFragment(CDC* pDC, CRect* prcBar, QWORD nTotal, QWORD nOffset, QWORD nLength, COLORREF crFill, BOOL b3D)
00051 {
00052 CRect rcArea;
00053
00054 if ( theApp.m_bRTL )
00055 nOffset = nTotal - nOffset - nLength;
00056
00057 rcArea.left = prcBar->left + (int)( (double)( prcBar->Width() + 1 ) / (double)nTotal * (double)nOffset );
00058 rcArea.right = prcBar->left + (int)( (double)( prcBar->Width() + 1 ) / (double)nTotal * (double)( nOffset + nLength ) );
00059
00060 rcArea.top = prcBar->top;
00061 rcArea.bottom = prcBar->bottom;
00062
00063 rcArea.left = max( rcArea.left, prcBar->left );
00064 rcArea.right = min( rcArea.right, prcBar->right );
00065
00066 if ( rcArea.right <= rcArea.left ) return;
00067
00068 if ( b3D && rcArea.Width() > 2 )
00069 {
00070 pDC->Draw3dRect( &rcArea, CCoolInterface::CalculateColour( crFill, RGB(255,255,255), 75 ),
00071 CCoolInterface::CalculateColour( crFill, RGB(0,0,0), 75 ) );
00072
00073 rcArea.DeflateRect( 1, 1 );
00074 pDC->FillSolidRect( &rcArea, crFill );
00075 rcArea.InflateRect( 1, 1 );
00076 }
00077 else
00078 {
00079 pDC->FillSolidRect( &rcArea, crFill );
00080 }
00081
00082 pDC->ExcludeClipRect( &rcArea );
00083 }
00084
00086
00087
00088 void CFragmentBar::DrawStateBar(CDC* pDC, CRect* prcBar, QWORD nTotal, QWORD nOffset, QWORD nLength, COLORREF crFill, BOOL bTop)
00089 {
00090 CRect rcArea;
00091 if ( theApp.m_bRTL )
00092 nOffset = nTotal - nOffset - nLength;
00093
00094 rcArea.left = prcBar->left + (int)( (double)( prcBar->Width() + 1 ) / (double)nTotal * (double)nOffset );
00095 rcArea.right = prcBar->left + (int)( (double)( prcBar->Width() + 1 ) / (double)nTotal * (double)( nOffset + nLength ) );
00096 rcArea.left = max( rcArea.left, prcBar->left );
00097 rcArea.right = min( rcArea.right, prcBar->right );
00098
00099 if ( bTop )
00100 {
00101 rcArea.top = prcBar->top;
00102 rcArea.bottom = min( ( prcBar->top + prcBar->bottom ) / 2, prcBar->top + 2 ) - 1;
00103 }
00104 else
00105 {
00106 rcArea.top = max( ( prcBar->top + prcBar->bottom ) / 2, prcBar->bottom - 3 ) + 1;
00107 rcArea.bottom = prcBar->bottom;
00108 }
00109
00110 if ( rcArea.right <= rcArea.left ) return;
00111
00112 if ( rcArea.Width() > 2 )
00113 {
00114 rcArea.DeflateRect( 1, 0 );
00115 pDC->FillSolidRect( &rcArea, crFill );
00116 rcArea.InflateRect( 1, 0 );
00117
00118 pDC->FillSolidRect( rcArea.left, rcArea.top, 1, rcArea.Height(),
00119 CCoolInterface::CalculateColour( crFill, RGB(255,255,255), 100 ) );
00120 pDC->FillSolidRect( rcArea.right - 1, rcArea.top, 1, rcArea.Height(),
00121 CCoolInterface::CalculateColour( crFill, RGB(0,0,0), 75 ) );
00122 }
00123 else
00124 {
00125 pDC->FillSolidRect( &rcArea, crFill );
00126 }
00127
00128 if ( bTop )
00129 {
00130 pDC->FillSolidRect( rcArea.left, rcArea.bottom, rcArea.Width(), 1,
00131 CCoolInterface::CalculateColour( crFill, RGB(0,0,0), 100 ) );
00132 rcArea.bottom ++;
00133 }
00134 else
00135 {
00136 rcArea.top --;
00137 pDC->FillSolidRect( rcArea.left, rcArea.top, rcArea.Width(), 1,
00138 CCoolInterface::CalculateColour( crFill, RGB(255,255,255), 100 ) );
00139 }
00140
00141 pDC->ExcludeClipRect( &rcArea );
00142 }
00143
00145
00146
00147 void CFragmentBar::DrawDownload(CDC* pDC, CRect* prcBar, CDownload* pDownload, COLORREF crNatural)
00148 {
00149 QWORD nvOffset, nvLength;
00150 BOOL bvSuccess;
00151
00152 if ( Settings.Downloads.ShowPercent && pDownload->IsStarted() )
00153 {
00154 DrawStateBar( pDC, prcBar, pDownload->m_nSize, 0, pDownload->GetVolumeComplete(),
00155 RGB( 0, 255, 0 ), TRUE );
00156 }
00157
00158 for ( nvOffset = 0 ; pDownload->GetNextVerifyRange( nvOffset, nvLength, bvSuccess ) ; )
00159 {
00160 DrawStateBar( pDC, prcBar, pDownload->m_nSize, nvOffset, nvLength,
00161 bvSuccess ? RGB( 0, 220, 0 ) : RGB( 220, 0, 0 ) );
00162 nvOffset += nvLength;
00163 }
00164
00165 for ( FF::SimpleFragmentList::ConstIterator pFragment = pDownload->GetEmptyFragmentList().begin();
00166 pFragment != pDownload->GetEmptyFragmentList().end(); ++pFragment )
00167 {
00168 DrawFragment( pDC, prcBar, pDownload->m_nSize,
00169 pFragment->begin(), pFragment->length(), crNatural, FALSE );
00170 }
00171
00172 for ( CDownloadSource* pSource = pDownload->GetFirstSource() ; pSource ; pSource = pSource->m_pNext )
00173 {
00174 DrawSourceImpl( pDC, prcBar, pSource );
00175 }
00176
00177 pDC->FillSolidRect( prcBar, pDownload->IsStarted() ? GetSysColor( COLOR_ACTIVECAPTION ) : crNatural );
00178 }
00179
00180
00181 void CFragmentBar::DrawDownloadSimple(CDC* pDC, CRect* prcBar, CDownload* pDownload, COLORREF crNatural)
00182 {
00183 pDC->FillSolidRect( prcBar, crNatural );
00184
00185 if ( pDownload->IsStarted() )
00186 {
00187 DrawFragment( pDC, prcBar, pDownload->m_nSize,0, pDownload->GetVolumeComplete(),
00188 GetSysColor( COLOR_ACTIVECAPTION ), FALSE );
00189 }
00190 }
00191
00192
00194
00195
00196 void CFragmentBar::DrawSource(CDC* pDC, CRect* prcBar, CDownloadSource* pSource, COLORREF crNatural)
00197 {
00198 if ( pSource->m_pTransfer != NULL )
00199 {
00200 if ( pSource->m_pTransfer->m_nLength < SIZE_UNKNOWN )
00201 {
00202 DrawStateBar( pDC, prcBar, pSource->m_pDownload->m_nSize,
00203 pSource->m_pTransfer->m_nOffset, pSource->m_pTransfer->m_nLength,
00204 RGB( 255, 255, 0 ), TRUE );
00205 }
00206
00207 switch( pSource->m_pTransfer->m_nProtocol )
00208 {
00209 case PROTOCOL_ED2K:
00210 for ( FF::SimpleFragmentQueue::ConstIterator pRequested
00211 = static_cast< CDownloadTransferED2K* >( pSource->m_pTransfer )->m_oRequested.begin();
00212 pRequested
00213 != static_cast< CDownloadTransferED2K* >( pSource->m_pTransfer )->m_oRequested.end();
00214 ++pRequested )
00215 {
00216 DrawStateBar( pDC, prcBar, pSource->m_pDownload->m_nSize,
00217 pRequested->begin(), pRequested->length(), RGB( 255, 255, 0 ), TRUE );
00218 }
00219 break;
00220 case PROTOCOL_BT:
00221 for ( FF::SimpleFragmentQueue::ConstIterator pRequested
00222 = static_cast< CDownloadTransferBT* >( pSource->m_pTransfer )->m_oRequested.begin();
00223 pRequested
00224 != static_cast< CDownloadTransferBT* >( pSource->m_pTransfer )->m_oRequested.end();
00225 ++pRequested )
00226 {
00227 DrawStateBar( pDC, prcBar, pSource->m_pDownload->m_nSize,
00228 pRequested->begin(), pRequested->length(), RGB( 255, 255, 0 ), TRUE );
00229 }
00230 }
00231 }
00232
00233 DrawSourceImpl( pDC, prcBar, pSource );
00234
00235 if ( !pSource->m_oAvailable.empty() )
00236 {
00237 for ( FF::SimpleFragmentList::ConstIterator pFragment
00238 = pSource->m_oAvailable.begin();
00239 pFragment != pSource->m_oAvailable.end(); ++pFragment )
00240 {
00241 DrawFragment( pDC, prcBar, pSource->m_pDownload->m_nSize,
00242 pFragment->begin(), pFragment->length(), crNatural, FALSE );
00243 }
00244
00245 pDC->FillSolidRect( prcBar, GetSysColor( COLOR_BTNFACE ) );
00246 }
00247 else
00248 {
00249 pDC->FillSolidRect( prcBar, crNatural );
00250 }
00251 }
00252
00253 void CFragmentBar::DrawSourceImpl(CDC* pDC, CRect* prcBar, CDownloadSource* pSource)
00254 {
00255 static COLORREF crFill[] =
00256 {
00257 RGB( 0, 153, 255 ), RGB( 0, 153, 0 ), RGB( 255, 51, 0 ),
00258 RGB( 255, 204, 0 ), RGB( 153, 153, 255 ), RGB( 204, 153, 0 )
00259 };
00260
00261 COLORREF crTransfer;
00262
00263 if ( pSource->m_bReadContent )
00264 {
00265 crTransfer = crFill[ pSource->GetColour() ];
00266 }
00267 else
00268 {
00269 crTransfer = GetSysColor( COLOR_ACTIVECAPTION );
00270 }
00271
00272 crTransfer = CCoolInterface::CalculateColour( crTransfer, CoolInterface.m_crHighlight, 90 );
00273
00274 if ( pSource->m_pTransfer != NULL )
00275 {
00276 if ( pSource->m_pTransfer->m_nState == dtsDownloading &&
00277 pSource->m_pTransfer->m_nOffset < SIZE_UNKNOWN )
00278 {
00279 if ( pSource->m_pTransfer->m_bRecvBackwards )
00280 {
00281 DrawFragment( pDC, prcBar, pSource->m_pDownload->m_nSize,
00282 pSource->m_pTransfer->m_nOffset + pSource->m_pTransfer->m_nLength - pSource->m_pTransfer->m_nPosition,
00283 pSource->m_pTransfer->m_nPosition, crTransfer, TRUE );
00284 }
00285 else
00286 {
00287 DrawFragment( pDC, prcBar, pSource->m_pDownload->m_nSize,
00288 pSource->m_pTransfer->m_nOffset,
00289 pSource->m_pTransfer->m_nPosition, crTransfer, TRUE );
00290 }
00291 }
00292 }
00293
00294 for ( FF::SimpleFragmentList::ConstIterator pFragment
00295 = pSource->m_oPastFragments.begin();
00296 pFragment != pSource->m_oPastFragments.end(); ++pFragment )
00297 {
00298 DrawFragment( pDC, prcBar, pSource->m_pDownload->m_nSize,
00299 pFragment->begin(), pFragment->length(), crTransfer, TRUE );
00300 }
00301 }
00302
00304
00305
00306 void CFragmentBar::DrawUpload(CDC* pDC, CRect* prcBar, CUploadFile* pFile, COLORREF crNatural)
00307 {
00308 CUploadTransfer* pUpload = pFile->GetActive();
00309 if ( pUpload == NULL ) return;
00310
00311 for ( FF::SimpleFragmentList::ConstIterator pFragment
00312 = pFile->m_oFragments.begin();
00313 pFragment != pFile->m_oFragments.end(); ++pFragment )
00314 {
00315 DrawFragment( pDC, prcBar, pFile->m_nSize, pFragment->begin(),
00316 pFragment->length(), GetSysColor( COLOR_ACTIVECAPTION ), TRUE );
00317 }
00318
00319 if ( pFile == pUpload->m_pBaseFile )
00320 {
00321 if ( pUpload->m_nProtocol == PROTOCOL_HTTP && ((CUploadTransferHTTP*)pUpload)->IsBackwards() )
00322 {
00323 DrawFragment( pDC, prcBar, pFile->m_nSize,
00324 pUpload->m_nOffset + pUpload->m_nLength - pUpload->m_nPosition,
00325 pUpload->m_nPosition, GetSysColor( COLOR_ACTIVECAPTION ), TRUE );
00326
00327 DrawFragment( pDC, prcBar, pFile->m_nSize,
00328 pUpload->m_nOffset,
00329 pUpload->m_nLength - pUpload->m_nPosition, crNatural, FALSE );
00330 }
00331 else
00332 {
00333 DrawFragment( pDC, prcBar, pFile->m_nSize,
00334 pUpload->m_nOffset, pUpload->m_nPosition,
00335 GetSysColor( COLOR_ACTIVECAPTION ), TRUE );
00336
00337 DrawFragment( pDC, prcBar, pFile->m_nSize,
00338 pUpload->m_nOffset + pUpload->m_nPosition,
00339 pUpload->m_nLength - pUpload->m_nPosition, crNatural, FALSE );
00340 }
00341 }
00342
00343 pDC->FillSolidRect( prcBar, ( pFile == pUpload->m_pBaseFile )
00344 ? GetSysColor( COLOR_BTNFACE ) : crNatural );
00345 }
00346