Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

LibraryHistory.cpp

Go to the documentation of this file.
00001 //
00002 // LibraryHistory.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Library.h"
00026 #include "LibraryHistory.h"
00027 #include "SharedFile.h"
00028 
00029 #ifdef _DEBUG
00030 #undef THIS_FILE
00031 static char THIS_FILE[]=__FILE__;
00032 #define new DEBUG_NEW
00033 #endif
00034 
00035 CLibraryHistory LibraryHistory;
00036 
00037 
00039 // CLibraryHistory construction
00040 
00041 CLibraryHistory::CLibraryHistory()
00042 {
00043         LastSeededTorrent.m_sName.Empty();
00044         LastSeededTorrent.m_sPath.Empty();
00045         ZeroMemory( &LastSeededTorrent.m_pBTH, sizeof(SHA1) );
00046         LastSeededTorrent.m_tLastSeeded         = 0;
00047         LastSeededTorrent.m_nUploaded           = 0;
00048         LastSeededTorrent.m_nDownloaded         = 0;
00049 
00050         LastCompletedTorrent.m_sName.Empty();
00051         LastCompletedTorrent.m_sPath.Empty();
00052         ZeroMemory( &LastCompletedTorrent.m_pBTH, sizeof(SHA1) );
00053         LastCompletedTorrent.m_tLastSeeded      = 0;
00054         LastCompletedTorrent.m_nUploaded        = 0;
00055         LastCompletedTorrent.m_nDownloaded      = 0;
00056 }
00057 
00058 CLibraryHistory::~CLibraryHistory()
00059 {
00060         Clear();
00061 }
00062 
00064 // CLibraryHistory file list
00065 
00066 POSITION CLibraryHistory::GetIterator() const
00067 {
00068         return m_pList.GetHeadPosition();
00069 }
00070 
00071 CLibraryRecent* CLibraryHistory::GetNext(POSITION& pos) const
00072 {
00073         return (CLibraryRecent*)m_pList.GetNext( pos );
00074 }
00075 
00076 int CLibraryHistory::GetCount() const
00077 {
00078         return m_pList.GetCount();
00079 }
00080 
00082 // CLibraryHistory clear
00083 
00084 void CLibraryHistory::Clear()
00085 {
00086         for ( POSITION pos = GetIterator() ; pos ; ) delete GetNext( pos );
00087         m_pList.RemoveAll();
00088 }
00089 
00091 // CLibraryHistory check
00092 
00093 BOOL CLibraryHistory::Check(CLibraryRecent* pRecent, int nScope) const
00094 {
00095         if ( nScope == 0 ) return m_pList.Find( pRecent ) != NULL;
00096 
00097         for ( POSITION pos = m_pList.GetHeadPosition() ; pos && nScope > 0 ; )
00098         {
00099                 CLibraryRecent* pExisting = (CLibraryRecent*)m_pList.GetNext( pos );
00100                 if ( pRecent == pExisting ) return TRUE;
00101                 if ( pExisting->m_pFile != NULL ) nScope--;
00102         }
00103 
00104         return FALSE;
00105 }
00106 
00108 // CLibraryHistory lookups
00109 
00110 CLibraryRecent* CLibraryHistory::GetByPath(LPCTSTR pszPath) const
00111 {
00112         for ( POSITION pos = GetIterator() ; pos ; )
00113         {
00114                 CLibraryRecent* pRecent = GetNext( pos );
00115                 if ( pRecent->m_sPath.CompareNoCase( pszPath ) == 0 ) return pRecent;
00116         }
00117 
00118         return NULL;
00119 }
00120 
00122 // CLibraryHistory add new download
00123 
00124 CLibraryRecent* CLibraryHistory::Add(LPCTSTR pszPath, const SHA1* pSHA1, const MD4* pED2K, LPCTSTR pszSources)
00125 {
00126         CSingleLock pLock( &Library.m_pSection );
00127         if ( ! pLock.Lock( 500 ) ) return NULL;
00128 
00129         CLibraryRecent* pRecent = GetByPath( pszPath );
00130         if ( pRecent != NULL ) return pRecent;
00131 
00132         pRecent = new CLibraryRecent( pszPath, pSHA1, pED2K, pszSources );
00133         m_pList.AddHead( pRecent );
00134 
00135         Prune();
00136 
00137         return pRecent;
00138 }
00139 
00141 // CLibraryHistory submit a library file
00142 
00143 BOOL CLibraryHistory::Submit(CLibraryFile* pFile)
00144 {
00145         CLibraryRecent* pRecent = GetByPath( pFile->GetPath() );
00146         if ( pRecent == NULL ) return FALSE;
00147 
00148         pRecent->RunVerify( pFile );
00149 
00150         Prune();
00151 
00152         return TRUE;
00153 }
00154 
00156 // CLibraryHistory clear today flags
00157 
00158 void CLibraryHistory::ClearTodays()
00159 {
00160         for ( POSITION pos = GetIterator() ; pos ; )
00161         {
00162                 GetNext( pos )->m_bToday = FALSE;
00163         }
00164 }
00165 
00167 // CLibraryHistory prune list to a fixed size
00168 
00169 int CLibraryHistory::Prune()
00170 {
00171         LONGLONG tNow, tRecent;
00172         SYSTEMTIME pNow;
00173         int nCount = 0;
00174 
00175         GetSystemTime( &pNow );
00176         SystemTimeToFileTime( &pNow, (FILETIME*)&tNow );
00177 
00178         for ( POSITION pos = m_pList.GetTailPosition() ; pos ; )
00179         {
00180                 POSITION posCur = pos;
00181                 CLibraryRecent* pRecent = (CLibraryRecent*)m_pList.GetPrev( pos );
00182 
00183                 CopyMemory( &tRecent, &pRecent->m_tAdded, sizeof(LONGLONG) );
00184 
00185                 if ( tNow - tRecent > (LONGLONG)Settings.Library.HistoryDays * 0xC92A69C000 )
00186                 {
00187                         delete pRecent;
00188                         m_pList.RemoveAt( posCur );
00189                         nCount++;
00190                 }
00191         }
00192 
00193         while ( GetCount() > (int)Settings.Library.HistoryTotal )
00194         {
00195                 delete (CLibraryRecent*)m_pList.RemoveTail();
00196                 nCount++;
00197         }
00198 
00199         return nCount;
00200 }
00201 
00203 // CLibraryHistory file delete handler
00204 
00205 void CLibraryHistory::OnFileDelete(CLibraryFile* pFile)
00206 {
00207         for ( POSITION pos = GetIterator() ; pos ; )
00208         {
00209                 POSITION posCur = pos;
00210                 CLibraryRecent* pRecent = GetNext( pos );
00211 
00212                 if ( pRecent->m_pFile == pFile )
00213                 {
00214                         delete pRecent;
00215                         m_pList.RemoveAt( posCur );
00216                         break;
00217                 }
00218         }
00219 }
00220 
00222 // CLibraryHistory serialize
00223 
00224 void CLibraryHistory::Serialize(CArchive& ar, int nVersion)
00225 {
00226         if ( nVersion < 7 ) return;
00227 
00228         int nCount = 0;
00229         POSITION pos;
00230 
00231         if ( ar.IsStoring() )
00232         {
00233                 for ( pos = GetIterator() ; pos ; )
00234                 {
00235                         if ( GetNext( pos )->m_pFile != NULL ) nCount ++;
00236                 }
00237 
00238                 ar.WriteCount( nCount );
00239 
00240                 for ( pos = GetIterator() ; pos ; )
00241                 {
00242                         CLibraryRecent* pRecent = GetNext( pos );
00243                         if ( pRecent->m_pFile != NULL ) pRecent->Serialize( ar, nVersion );
00244                 }
00245 
00246                 ar << LastSeededTorrent.m_sPath;
00247                 if ( LastSeededTorrent.m_sPath.GetLength() )
00248                 {
00249                         ar << LastSeededTorrent.m_sName;
00250                         ar << LastSeededTorrent.m_tLastSeeded;
00251                         ar.Write( &LastSeededTorrent.m_pBTH, sizeof(SHA1) );
00252                 }
00253         }
00254         else
00255         {
00256                 Clear();
00257 
00258                 for ( nCount = ar.ReadCount() ; nCount > 0 ; nCount-- )
00259                 {
00260                         CLibraryRecent* pRecent = new CLibraryRecent();
00261                         pRecent->Serialize( ar, nVersion );
00262 
00263                         if ( pRecent->m_pFile != NULL )
00264                         {
00265                                 m_pList.AddTail( pRecent );
00266                         }
00267                         else
00268                         {
00269                                 delete pRecent;
00270                         }
00271                 }
00272 
00273                 if ( nVersion > 22 )
00274                 {
00275                         ar >> LastSeededTorrent.m_sPath;
00276                         if ( LastSeededTorrent.m_sPath.GetLength() )
00277                         {
00278                                 ar >> LastSeededTorrent.m_sName;
00279                                 ar >> LastSeededTorrent.m_tLastSeeded;
00280                                 ar.Read( &LastSeededTorrent.m_pBTH, sizeof(SHA1) );
00281                         }
00282                 }
00283         }
00284 }
00285 
00286 
00288 // CLibraryRecent construction
00289 
00290 CLibraryRecent::CLibraryRecent()
00291 {
00292         ZeroMemory( &m_tAdded, sizeof(FILETIME) );
00293 
00294         m_bToday        = FALSE;
00295         m_pFile         = NULL;
00296         m_bSHA1         = FALSE;
00297         m_bED2K         = FALSE;
00298 }
00299 
00300 CLibraryRecent::CLibraryRecent(LPCTSTR pszPath, const SHA1* pSHA1, const MD4* pED2K, LPCTSTR pszSources)
00301 {
00302         SYSTEMTIME pTime;
00303         GetSystemTime( &pTime );
00304         SystemTimeToFileTime( &pTime, &m_tAdded );
00305 
00306         m_pFile         = NULL;
00307         m_sPath         = pszPath;
00308         m_sSources      = pszSources;
00309         m_bSHA1         = pSHA1 != NULL;
00310         m_bED2K         = pED2K != NULL;
00311 
00312         if ( m_bSHA1 ) m_pSHA1 = *pSHA1;
00313         if ( m_bED2K ) m_pED2K = *pED2K;
00314 }
00315 
00316 CLibraryRecent::~CLibraryRecent()
00317 {
00318 }
00319 
00321 // CLibraryRecent verification
00322 
00323 void CLibraryRecent::RunVerify(CLibraryFile* pFile)
00324 {
00325         if ( m_pFile == NULL )
00326         {
00327                 m_pFile = pFile;
00328                 m_pFile->OnVerifyDownload( m_bSHA1 ? &m_pSHA1 : NULL,
00329                         m_bED2K ? &m_pED2K : NULL, m_sSources );
00330         }
00331 }
00332 
00334 // CLibraryRecent serialize
00335 
00336 void CLibraryRecent::Serialize(CArchive& ar, int nVersion)
00337 {
00338         if ( ar.IsStoring() )
00339         {
00340                 ASSERT( m_pFile != NULL );
00341 
00342                 ar.Write( &m_tAdded, sizeof(FILETIME) );
00343                 ar << m_pFile->m_nIndex;
00344         }
00345         else
00346         {
00347                 DWORD nIndex;
00348 
00349                 ar.Read( &m_tAdded, sizeof(FILETIME) );
00350                 ar >> nIndex;
00351 
00352                 if ( m_pFile = Library.LookupFile( nIndex ) )
00353                 {
00354                         m_sPath = m_pFile->GetPath();
00355                         m_bSHA1 = m_pFile->m_bSHA1;
00356                         m_bED2K = m_pFile->m_bED2K;
00357                         if ( m_bSHA1 ) m_pSHA1 = m_pFile->m_pSHA1;
00358                         if ( m_bED2K ) m_pED2K = m_pFile->m_pED2K;
00359                 }
00360         }
00361 }
00362 

Generated on Thu Dec 15 10:39:44 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2