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 "Transfers.h"
00026 #include "DownloadWithExtras.h"
00027 #include "DlgFilePreview.h"
00028 #include "DlgDownloadMonitor.h"
00029 #include "Plugins.h"
00030
00031 #ifdef _DEBUG
00032 #undef THIS_FILE
00033 static char THIS_FILE[]=__FILE__;
00034 #define new DEBUG_NEW
00035 #endif
00036
00037
00039
00040
00041 CDownloadWithExtras::CDownloadWithExtras()
00042 {
00043 m_pMonitorWnd = NULL;
00044 m_pPreviewWnd = NULL;
00045 m_pReviewFirst = NULL;
00046 m_pReviewLast = NULL;
00047 m_nReviewCount = 0;
00048 }
00049
00050 CDownloadWithExtras::~CDownloadWithExtras()
00051 {
00052 DeletePreviews();
00053 DeleteReviews();
00054 }
00055
00057
00058
00059 BOOL CDownloadWithExtras::Preview(CSingleLock* pLock)
00060 {
00061 DeletePreviews();
00062
00063 if ( ! CanPreview() ) return FALSE;
00064
00065 ASSERT( m_pPreviewWnd == NULL );
00066 m_pPreviewWnd = new CFilePreviewDlg( (CDownload*)this );
00067
00068 if ( pLock ) pLock->Unlock();
00069
00070 m_pPreviewWnd->Create();
00071 m_pPreviewWnd->ShowWindow( SW_SHOWNORMAL );
00072 m_pPreviewWnd->BringWindowToTop();
00073
00074 if ( pLock ) pLock->Lock();
00075
00076 return TRUE;
00077 }
00078
00079 BOOL CDownloadWithExtras::IsPreviewVisible() const
00080 {
00081 return m_pPreviewWnd != NULL;
00082 }
00083
00084 BOOL CDownloadWithExtras::CanPreview()
00085 {
00086 if ( m_pPreviewWnd != NULL ) return FALSE;
00087
00088 LPCTSTR pszType = _tcsrchr( m_sLocalName, '.' );
00089 if ( pszType == NULL )
00090 {
00091 pszType = _tcsrchr( m_sRemoteName, '.' );
00092 if ( pszType == NULL ) return FALSE;
00093 }
00094
00095 CString strType( pszType );
00096 CharLower( strType.GetBuffer() );
00097 strType.ReleaseBuffer();
00098
00099 CLSID pCLSID;
00100 return Plugins.LookupCLSID( _T("DownloadPreview"), strType, pCLSID );
00101 }
00102
00104
00105
00106 void CDownloadWithExtras::AddPreviewName(LPCTSTR pszFile)
00107 {
00108 m_pPreviews.AddTail( pszFile );
00109 theApp.WriteProfileString( _T("Delete"), pszFile, _T("") );
00110 SetModified();
00111 }
00112
00113 void CDownloadWithExtras::DeletePreviews()
00114 {
00115 for ( POSITION pos = m_pPreviews.GetHeadPosition() ; pos ; )
00116 {
00117 POSITION posRemove = pos;
00118 CString strPath = m_pPreviews.GetNext( pos );
00119
00120 if ( ::DeleteFile( strPath ) )
00121 {
00122 m_pPreviews.RemoveAt( posRemove );
00123 theApp.WriteProfileString( _T("Delete"), strPath, NULL );
00124 }
00125 }
00126
00127 SetModified();
00128 }
00129
00131
00132
00133 BOOL CDownloadWithExtras::AddReview(IN_ADDR* pIP, int nClientID, int nRating, LPCTSTR pszUserName, LPCTSTR pszComment)
00134 {
00135
00136 if ( m_nReviewCount > Settings.Downloads.MaxReviews )
00137 {
00138 theApp.Message( MSG_DEBUG, _T("Maximum number of reviews reached") );
00139 return FALSE;
00140 }
00141
00142
00143 if ( FindReview( pIP ) )
00144 {
00145 theApp.Message( MSG_DEBUG, _T("Ignoring multiple reviews from %s"), inet_ntoa( *pIP ) );
00146 return FALSE;
00147 }
00148
00149
00150 if ( FindReview( nRating, pszUserName, pszComment ) )
00151 {
00152 theApp.Message( MSG_DEBUG, _T("Ignoring duplicate review from %s"), inet_ntoa( *pIP ) );
00153 return FALSE;
00154 }
00155
00156
00157 CDownloadReview* pReview = new CDownloadReview(pIP, nClientID, nRating, pszUserName, pszComment);
00158 m_nReviewCount++;
00159
00160 pReview->m_pPrev = m_pReviewLast;
00161 pReview->m_pNext = NULL;
00162
00163 if ( m_pReviewLast != NULL )
00164 {
00165 m_pReviewLast->m_pNext = pReview;
00166 m_pReviewLast = pReview;
00167 }
00168 else
00169 {
00170 m_pReviewFirst = m_pReviewLast = pReview;
00171 }
00172
00173 return TRUE;
00174 }
00175
00176
00177 BOOL CDownloadWithExtras::AddReview(CDownloadReview* pReview)
00178 {
00179
00180 if ( m_nReviewCount > Settings.Downloads.MaxReviews )
00181 {
00182 theApp.Message( MSG_DEBUG, _T("Maximum number of reviews reached") );
00183 delete pReview;
00184 return FALSE;
00185 }
00186
00187
00188 m_nReviewCount++;
00189
00190 pReview->m_pPrev = m_pReviewLast;
00191 pReview->m_pNext = NULL;
00192
00193 if ( m_pReviewLast != NULL )
00194 {
00195 m_pReviewLast->m_pNext = pReview;
00196 m_pReviewLast = pReview;
00197 }
00198 else
00199 {
00200 m_pReviewFirst = m_pReviewLast = pReview;
00201 }
00202
00203 return TRUE;
00204 }
00205
00206
00207 void CDownloadWithExtras::DeleteReview(CDownloadReview *pReview)
00208 {
00209 if ( pReview == NULL ) return;
00210
00211 if ( m_nReviewCount ) m_nReviewCount--;
00212
00213 if ( pReview->m_pNext == NULL )
00214 {
00215 if ( pReview->m_pPrev == NULL )
00216 {
00217
00218 m_pReviewFirst = m_pReviewLast = NULL;
00219 delete pReview;
00220 return;
00221 }
00222 else
00223 {
00224
00225 pReview->m_pPrev->m_pNext = NULL;
00226 m_pReviewLast = pReview->m_pPrev;
00227 delete pReview;
00228 return;
00229 }
00230 }
00231 else if ( pReview->m_pPrev == NULL )
00232 {
00233
00234 pReview->m_pNext->m_pPrev = NULL;
00235 m_pReviewFirst = pReview->m_pNext;
00236 delete pReview;
00237 return;
00238
00239 }
00240 else
00241 {
00242
00243 pReview->m_pPrev->m_pNext = pReview->m_pNext;
00244 pReview->m_pNext->m_pPrev = pReview->m_pPrev;
00245 delete pReview;
00246 return;
00247 }
00248 }
00249
00250
00251 void CDownloadWithExtras::DeleteReviews()
00252 {
00253 CDownloadReview *pNext = NULL, *pReview = m_pReviewFirst;
00254
00255 while ( pReview )
00256 {
00257 pNext = pReview->m_pNext;
00258 delete pReview;
00259 pReview = pNext;
00260 }
00261
00262 m_pReviewFirst = NULL;
00263 m_pReviewLast = NULL;
00264 m_nReviewCount = 0;
00265 }
00266
00267
00268 CDownloadReview* CDownloadWithExtras::FindReview(IN_ADDR* pIP) const
00269 {
00270 CDownloadReview *pReview = m_pReviewFirst;
00271 if ( pIP == NULL ) return NULL;
00272
00273 while ( pReview )
00274 {
00275 if ( pReview->m_pUserIP.S_un.S_addr == pIP->S_un.S_addr )
00276 return pReview;
00277 pReview = pReview->m_pNext;
00278 }
00279
00280 return NULL;
00281 }
00282
00283
00284 CDownloadReview* CDownloadWithExtras::FindReview(LPCTSTR pszUserName) const
00285 {
00286 CDownloadReview *pReview = m_pReviewFirst;
00287 if ( _tcsclen( pszUserName ) == 0 ) return NULL;
00288
00289 while ( pReview )
00290 {
00291 if ( _tcscmp( pReview->m_sUserName, pszUserName ) == 0 )
00292 return pReview;
00293 pReview = pReview->m_pNext;
00294 }
00295
00296 return NULL;
00297 }
00298
00299
00300 CDownloadReview* CDownloadWithExtras::FindReview(int nRating, LPCTSTR pszName, LPCTSTR pszComment) const
00301 {
00302 CDownloadReview *pReview = m_pReviewFirst;
00303 CString strName, strComment;
00304 strName = pszName;
00305 strComment = pszComment;
00306
00307 while ( pReview )
00308 {
00309 if ( ( pReview->m_nFileRating == nRating ) &&
00310 ( _tcscmp( pReview->m_sUserName, pszName ) == 0 ) &&
00311 ( _tcscmp( pReview->m_sFileComments, pszComment ) == 0 ) )
00312 return pReview;
00313 pReview = pReview->m_pNext;
00314 }
00315
00316 return NULL;
00317 }
00318
00319
00320 int CDownloadWithExtras::GetReviewAverage() const
00321 {
00322 int nAverageRating = 0, nCount = 0;
00323
00324 CDownloadReview *pNext = NULL, *pReview = m_pReviewFirst;
00325
00326 while ( pReview )
00327 {
00328 pNext = pReview->m_pNext;
00329 if ( pReview->m_nFileRating > 0 )
00330 {
00331 nAverageRating += pReview->m_nFileRating;
00332 nCount ++;
00333 }
00334 pReview = pNext;
00335 }
00336
00337 if ( nCount ) nAverageRating /= nCount;
00338
00339 return nAverageRating;
00340 }
00341
00342
00344
00345
00346 void CDownloadWithExtras::ShowMonitor(CSingleLock* pLock)
00347 {
00348 if ( pLock ) pLock->Unlock();
00349
00350 if ( m_pMonitorWnd == NULL )
00351 {
00352 m_pMonitorWnd = new CDownloadMonitorDlg( (CDownload*)this );
00353 }
00354
00355 m_pMonitorWnd->ShowWindow( SW_SHOWNORMAL );
00356 m_pMonitorWnd->BringWindowToTop();
00357
00358 if ( pLock ) pLock->Lock();
00359 }
00360
00361 BOOL CDownloadWithExtras::IsMonitorVisible() const
00362 {
00363 return m_pMonitorWnd != NULL;
00364 }
00365
00367
00368
00369 void CDownloadWithExtras::Serialize(CArchive& ar, int nVersion)
00370 {
00371 CDownloadWithSearch::Serialize( ar, nVersion );
00372
00373 if ( ar.IsStoring() )
00374 {
00375 ar.WriteCount( m_pPreviews.GetCount() );
00376
00377 for ( POSITION pos = m_pPreviews.GetHeadPosition() ; pos ; )
00378 {
00379 ar << m_pPreviews.GetNext( pos );
00380 }
00381
00382 ar.WriteCount( GetReviewCount() );
00383
00384 CDownloadReview *pReview = m_pReviewFirst;
00385 while ( pReview )
00386 {
00387 pReview->Serialize( ar, nVersion );
00388 pReview = pReview->m_pNext;
00389 }
00390
00391
00392 }
00393 else
00394 {
00395 for ( int nCount = ar.ReadCount() ; nCount ; nCount-- )
00396 {
00397 CString str;
00398 ar >> str;
00399 m_pPreviews.AddTail( str );
00400 }
00401
00402 if ( nVersion >= 32 )
00403 {
00404
00405 for ( int nCount = ar.ReadCount() ; nCount ; nCount-- )
00406 {
00407 CDownloadReview *pReview = new CDownloadReview;
00408 pReview->Serialize( ar, nVersion );
00409 AddReview( pReview );
00410 }
00411 }
00412 }
00413 }
00414
00416
00417
00418 CDownloadReview::CDownloadReview()
00419 {
00420 m_pNext = NULL;
00421 m_pPrev = NULL;
00422
00423 m_nUserPicture = 0;
00424 m_nFileRating = 0;
00425 }
00426
00427 CDownloadReview::CDownloadReview(in_addr *pIP, int nUserPicture, int nRating, LPCTSTR pszUserName, LPCTSTR pszComment)
00428 {
00429 m_pNext = NULL;
00430 m_pPrev = NULL;
00431
00432 m_nUserPicture = nUserPicture;
00433 m_sUserName = pszUserName;
00434 m_nFileRating = nRating;
00435
00436 m_sFileComments = pszComment;
00437
00438 if ( pIP != NULL )
00439 {
00440 m_pUserIP = *pIP;
00441 if ( m_sUserName.IsEmpty() ) m_sUserName = inet_ntoa( *pIP );
00442 }
00443 else
00444 {
00445 m_pUserIP.S_un.S_addr = 0;
00446 }
00447 }
00448
00449 CDownloadReview::~CDownloadReview()
00450 {
00451
00452
00453
00454 }
00455
00457
00458
00459 void CDownloadReview::Serialize(CArchive& ar, int nVersion)
00460 {
00461 if ( ar.IsStoring() )
00462 {
00463 ar << m_pUserIP.S_un.S_addr;
00464 ar << m_nUserPicture;
00465 ar << m_sUserName;
00466 ar << m_nFileRating;
00467 ar << m_sFileComments;
00468 }
00469 else
00470 {
00471 ar >> m_pUserIP.S_un.S_addr;
00472 ar >> m_nUserPicture;
00473 ar >> m_sUserName;
00474 ar >> m_nFileRating;
00475 ar >> m_sFileComments;
00476 }
00477 }