00001 // 00002 // DownloadGroup.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 "DownloadGroup.h" 00025 #include "DownloadGroups.h" 00026 #include "Downloads.h" 00027 #include "Download.h" 00028 00029 #include "Schema.h" 00030 #include "SchemaCache.h" 00031 #include "ShellIcons.h" 00032 #include "QuerySearch.h" 00033 00034 #ifdef _DEBUG 00035 #undef THIS_FILE 00036 static char THIS_FILE[]=__FILE__; 00037 #define new DEBUG_NEW 00038 #endif 00039 00040 00042 // CDownloadGroup construction 00043 00044 CDownloadGroup::CDownloadGroup() 00045 { 00046 m_nImage = SHI_FOLDER_OPEN; 00047 m_bRemoteSelected = TRUE; 00048 } 00049 00050 CDownloadGroup::~CDownloadGroup() 00051 { 00052 } 00053 00055 // CDownloadGroup add and remove 00056 00057 void CDownloadGroup::Add(CDownload* pDownload) 00058 { 00059 if ( m_pDownloads.Find( pDownload ) == NULL ) 00060 { 00061 m_pDownloads.AddTail( pDownload ); 00062 DownloadGroups.m_nBaseCookie ++; 00063 } 00064 } 00065 00066 void CDownloadGroup::Remove(CDownload* pDownload) 00067 { 00068 if ( POSITION pos = m_pDownloads.Find( pDownload ) ) 00069 { 00070 m_pDownloads.RemoveAt( pos ); 00071 DownloadGroups.m_nBaseCookie ++; 00072 } 00073 } 00074 00076 // CDownloadGroup set selection cookie 00077 00078 void CDownloadGroup::SetCookie(int nCookie) 00079 { 00080 for ( POSITION pos = GetIterator() ; pos ; ) 00081 { 00082 GetNext( pos )->m_nGroupCookie = nCookie; 00083 } 00084 } 00085 00087 // CDownloadGroup list copy 00088 00089 void CDownloadGroup::CopyList(CPtrList* pList) 00090 { 00091 for ( POSITION pos = GetIterator() ; pos ; ) 00092 { 00093 pList->AddTail( GetNext( pos ) ); 00094 } 00095 } 00096 00098 // CDownloadGroup conditional add 00099 00100 BOOL CDownloadGroup::Link(CDownload* pDownload) 00101 { 00102 if ( m_pFilters.IsEmpty() ) return FALSE; 00103 00104 for ( POSITION pos = m_pFilters.GetHeadPosition() ; pos ; ) 00105 { 00106 CString strFilter = m_pFilters.GetNext( pos ); 00107 00108 if ( ( pDownload->m_bBTH && strFilter.CompareNoCase( _T("torrent") ) == 0 ) || 00109 CQuerySearch::WordMatch( pDownload->m_sRemoteName, strFilter ) ) 00110 { 00111 Add( pDownload ); 00112 return TRUE; 00113 } 00114 } 00115 00116 return FALSE; 00117 } 00118 00120 // CDownloadGroup conditional add all downloads 00121 00122 int CDownloadGroup::LinkAll() 00123 { 00124 if ( m_pFilters.IsEmpty() ) return 0; 00125 00126 int nCount = 0; 00127 00128 for ( POSITION pos = Downloads.GetIterator() ; pos ; ) 00129 { 00130 nCount += Link( Downloads.GetNext( pos ) ); 00131 } 00132 00133 return nCount; 00134 } 00135 00137 // CDownloadGroup add a filter 00138 00139 void CDownloadGroup::AddFilter(LPCTSTR pszFilter) 00140 { 00141 m_pFilters.AddTail( pszFilter ); 00142 } 00143 00145 // CDownloadGroup schema 00146 00147 void CDownloadGroup::SetSchema(LPCTSTR pszURI) 00148 { 00149 if ( m_sSchemaURI != pszURI ) m_sSchemaURI = pszURI; 00150 00151 if ( CSchema* pSchema = SchemaCache.Get( pszURI ) ) 00152 { 00153 m_nImage = pSchema->m_nIcon16; 00154 } 00155 else 00156 { 00157 m_nImage = SHI_FOLDER_OPEN; 00158 } 00159 } 00160 00162 // CDownloadGroup serialise 00163 00164 void CDownloadGroup::Serialize(CArchive& ar, int nVersion) 00165 { 00166 if ( ar.IsStoring() ) 00167 { 00168 ar << m_sName; 00169 ar << m_sSchemaURI; 00170 ar << m_sFolder; 00171 00172 ar.WriteCount( m_pFilters.GetCount() ); 00173 00174 for ( POSITION pos = m_pFilters.GetHeadPosition() ; pos ; ) 00175 { 00176 ar << m_pFilters.GetNext( pos ); 00177 } 00178 00179 ar.WriteCount( GetCount() ); 00180 00181 for ( POSITION pos = GetIterator() ; pos ; ) 00182 { 00183 DWORD nDownload = GetNext( pos )->m_nSerID; 00184 ar << nDownload; 00185 } 00186 } 00187 else 00188 { 00189 ar >> m_sName; 00190 ar >> m_sSchemaURI; 00191 ar >> m_sFolder; 00192 00193 if ( nVersion >= 3 ) 00194 { 00195 for ( int nCount = ar.ReadCount() ; nCount > 0 ; nCount-- ) 00196 { 00197 CString strFilter; 00198 ar >> strFilter; 00199 m_pFilters.AddTail( strFilter ); 00200 } 00201 } 00202 else 00203 { 00204 CString strFilters; 00205 ar >> strFilters; 00206 00207 for ( strFilters += '|' ; strFilters.GetLength() ; ) 00208 { 00209 CString strFilter = strFilters.SpanExcluding( _T(" |") ); 00210 strFilters = strFilters.Mid( strFilter.GetLength() + 1 ); 00211 strFilter.TrimLeft(); strFilter.TrimRight(); 00212 if ( strFilter.GetLength() ) m_pFilters.AddTail( strFilter ); 00213 } 00214 } 00215 00216 for ( int nCount = ar.ReadCount() ; nCount > 0 ; nCount-- ) 00217 { 00218 DWORD nDownload; 00219 ar >> nDownload; 00220 if ( CDownload* pDownload = Downloads.FindBySID( nDownload ) ) 00221 Add( pDownload ); 00222 } 00223 00224 SetSchema( m_sSchemaURI ); 00225 } 00226 } 00227