00001 // 00002 // UploadFiles.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 "UploadFiles.h" 00025 #include "UploadFile.h" 00026 #include "UploadTransfer.h" 00027 00028 #ifdef _DEBUG 00029 #undef THIS_FILE 00030 static char THIS_FILE[]=__FILE__; 00031 #define new DEBUG_NEW 00032 #endif 00033 00034 CUploadFiles UploadFiles; 00035 00036 00038 // CUploadFiles construction 00039 00040 CUploadFiles::CUploadFiles() 00041 { 00042 } 00043 00044 CUploadFiles::~CUploadFiles() 00045 { 00046 Clear(); 00047 } 00048 00050 // CUploadFiles clear 00051 00052 void CUploadFiles::Clear() 00053 { 00054 for ( POSITION pos = GetIterator() ; pos ; ) 00055 { 00056 GetNext( pos )->Remove(); 00057 } 00058 00059 ASSERT( GetCount() == 0 ); 00060 } 00061 00063 // CUploadFiles file allocation 00064 00065 CUploadFile* CUploadFiles::GetFile(CUploadTransfer* pUpload, SHA1* pSHA1, LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize) 00066 { 00067 for ( POSITION pos = GetIterator() ; pos ; ) 00068 { 00069 CUploadFile* pFile = GetNext( pos ); 00070 00071 if ( pFile->m_pAddress.S_un.S_addr == pUpload->m_pHost.sin_addr.S_un.S_addr ) 00072 { 00073 if ( pFile->m_sPath == pszPath ) 00074 { 00075 pFile->Add( pUpload ); 00076 return pFile; 00077 } 00078 } 00079 } 00080 00081 CUploadFile* pFile = new CUploadFile( pUpload, pSHA1, pszName, pszPath, nSize ); 00082 m_pList.AddTail( pFile ); 00083 00084 return pFile; 00085 } 00086 00088 // CUploadFiles remove an upload trasnfer 00089 00090 void CUploadFiles::Remove(CUploadTransfer* pTransfer) 00091 { 00092 for ( POSITION pos = GetIterator() ; pos ; ) 00093 { 00094 POSITION posRemove = pos; 00095 CUploadFile* pFile = GetNext( pos ); 00096 00097 if ( pFile->Remove( pTransfer ) ) 00098 { 00099 delete pFile; 00100 m_pList.RemoveAt( posRemove ); 00101 } 00102 } 00103 } 00104 00106 // CUploadFiles move a file with this transfer to the head/tail. (Cheap BT sorting) 00107 00108 void CUploadFiles::MoveToHead(CUploadTransfer* pTransfer) 00109 { 00110 for ( POSITION pos = GetIterator() ; pos ; ) 00111 { 00112 POSITION posThis = pos; 00113 CUploadFile* pFile = GetNext( pos ); 00114 00115 if ( pFile->GetActive() == pTransfer ) 00116 { 00117 m_pList.RemoveAt( posThis ); 00118 m_pList.AddHead( pFile ); 00119 return; 00120 } 00121 } 00122 } 00123 00124 void CUploadFiles::MoveToTail(CUploadTransfer* pTransfer) 00125 { 00126 for ( POSITION pos = GetIterator() ; pos ; ) 00127 { 00128 POSITION posThis = pos; 00129 CUploadFile* pFile = GetNext( pos ); 00130 00131 if ( pFile->GetActive() == pTransfer ) 00132 { 00133 m_pList.RemoveAt( posThis ); 00134 m_pList.AddTail( pFile ); 00135 return; 00136 } 00137 } 00138 } 00139