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

DownloadGroups.cpp

Go to the documentation of this file.
00001 //
00002 // DownloadGroups.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 "DownloadGroup.h"
00026 #include "DownloadGroups.h"
00027 #include "Downloads.h"
00028 #include "Download.h"
00029 #include "Schema.h"
00030 
00031 #ifdef _DEBUG
00032 #undef THIS_FILE
00033 static char THIS_FILE[]=__FILE__;
00034 #define new DEBUG_NEW
00035 #endif
00036 
00037 CDownloadGroups DownloadGroups;
00038 
00039 
00041 // CDownloadGroups construction
00042 
00043 CDownloadGroups::CDownloadGroups()
00044 {
00045         m_pSuper                = NULL;
00046         m_nBaseCookie   = 1;
00047         m_nSaveCookie   = 0;
00048         m_nGroupCookie  = 0;
00049 }
00050 
00051 CDownloadGroups::~CDownloadGroups()
00052 {
00053         Clear();
00054 }
00055 
00057 // CDownloadGroups supergroup
00058 
00059 CDownloadGroup* CDownloadGroups::GetSuperGroup()
00060 {
00061         CSingleLock pLock( &m_pSection, TRUE );
00062 
00063         if ( m_pSuper != NULL ) return m_pSuper;
00064 
00065         return m_pSuper = Add( _T("All") );
00066 }
00067 
00069 // CDownloadGroups add group
00070 
00071 CDownloadGroup* CDownloadGroups::Add(LPCTSTR pszName)
00072 {
00073         CSingleLock pLock( &m_pSection, TRUE );
00074 
00075         CDownloadGroup* pGroup = new CDownloadGroup();
00076         if ( pszName != NULL ) pGroup->m_sName = pszName;
00077         m_pList.AddTail( pGroup );
00078 
00079         m_nBaseCookie ++;
00080         m_nGroupCookie ++;
00081 
00082         return pGroup;
00083 }
00084 
00086 // CDownloadGroups remove group
00087 
00088 void CDownloadGroups::Remove(CDownloadGroup* pGroup)
00089 {
00090         CSingleLock pLock( &m_pSection, TRUE );
00091 
00092         if ( POSITION pos = m_pList.Find( pGroup ) )
00093         {
00094                 if ( pGroup == m_pSuper ) return;
00095                 m_pList.RemoveAt( pos );
00096                 delete pGroup;
00097 
00098                 m_nBaseCookie ++;
00099                 m_nGroupCookie ++;
00100         }
00101 }
00102 
00104 // CDownloadGroups link a download to the appropriate groups
00105 
00106 void CDownloadGroups::Link(CDownload* pDownload)
00107 {
00108         CSingleLock pLock( &m_pSection, TRUE );
00109 
00110         GetSuperGroup()->Add( pDownload );
00111 
00112         for ( POSITION pos = GetIterator() ; pos ; )
00113         {
00114                 CDownloadGroup* pGroup = GetNext( pos );
00115                 pGroup->Link( pDownload );
00116         }
00117 }
00118 
00120 // CDownloadGroups unlink a download from all groups
00121 
00122 void CDownloadGroups::Unlink(CDownload* pDownload, BOOL bAndSuper)
00123 {
00124         CSingleLock pLock( &m_pSection, TRUE );
00125 
00126         for ( POSITION pos = GetIterator() ; pos ; )
00127         {
00128                 CDownloadGroup* pGroup = GetNext( pos );
00129                 if ( bAndSuper || pGroup != m_pSuper ) pGroup->Remove( pDownload );
00130         }
00131 }
00132 
00134 // CDownloadGroups default
00135 
00136 void CDownloadGroups::CreateDefault()
00137 {
00138         CSingleLock pLock( &m_pSection, TRUE );
00139 
00140         CDownloadGroup* pGroup  = GetSuperGroup();
00141 
00142         pGroup = Add( _T("Audio") );
00143         pGroup->AddFilter( _T(".mp3") );
00144         pGroup->AddFilter( _T(".ogg") );
00145         pGroup->AddFilter( _T(".wav") );
00146         pGroup->AddFilter( _T(".wma") );
00147         pGroup->SetSchema( CSchema::uriMusicAlbum );
00148 
00149         pGroup = Add( _T("Video") );
00150         pGroup->AddFilter( _T(".asf") );
00151         pGroup->AddFilter( _T(".avi") );
00152         pGroup->AddFilter( _T(".mov") );
00153         pGroup->AddFilter( _T(".mpg") );
00154         pGroup->AddFilter( _T(".mpeg") );
00155         pGroup->AddFilter( _T(".ogm") );
00156         pGroup->AddFilter( _T(".wmv") );
00157         pGroup->SetSchema( CSchema::uriVideo );
00158 
00159         pGroup = Add( _T("BitTorrent") );
00160         pGroup->AddFilter( _T("torrent") );
00161         pGroup->SetSchema( CSchema::uriROM );
00162 }
00163 
00165 // CDownloadGroups completed path
00166 
00167 CString CDownloadGroups::GetCompletedPath(CDownload* pDownload)
00168 {
00169         CSingleLock pLock( &m_pSection, TRUE );
00170 
00171         for ( POSITION pos = GetIterator() ; pos ; )
00172         {
00173                 CDownloadGroup* pGroup = GetNext( pos );
00174 
00175                 if ( pGroup != m_pSuper && pGroup->Contains( pDownload ) )
00176                 {
00177                         if ( pGroup->m_sFolder.GetLength() ) return pGroup->m_sFolder;
00178                 }
00179         }
00180 
00181         return Settings.Downloads.CompletePath;
00182 }
00183 
00185 // CDownloadGroups clear
00186 
00187 void CDownloadGroups::Clear()
00188 {
00189         CSingleLock pLock( &m_pSection, TRUE );
00190 
00191         for ( POSITION pos = GetIterator() ; pos ; ) delete GetNext( pos );
00192         m_pList.RemoveAll();
00193 
00194         m_pSuper = NULL;
00195         m_nBaseCookie ++;
00196         m_nGroupCookie ++;
00197 }
00198 
00200 // CDownloadGroups load and save
00201 
00202 BOOL CDownloadGroups::Load()
00203 {
00204         CSingleLock pLock( &m_pSection, TRUE );
00205 
00206         CFile pFile;
00207         CString strPath = Settings.General.UserPath + _T("\\Data\\DownloadGroups.dat");
00208         if ( ! pFile.Open( strPath, CFile::modeRead ) ) return FALSE;
00209 
00210         try
00211         {
00212                 CArchive ar( &pFile, CArchive::load );
00213                 Serialize( ar );
00214         }
00215         catch ( CException* pException )
00216         {
00217                 pException->Delete();
00218                 return FALSE;
00219         }
00220 
00221         m_nSaveCookie = m_nBaseCookie;
00222 
00223         return TRUE;
00224 }
00225 
00226 BOOL CDownloadGroups::Save(BOOL bForce)
00227 {
00228         CSingleLock pLock( &m_pSection, TRUE );
00229 
00230         if ( ! bForce && m_nBaseCookie == m_nSaveCookie ) return FALSE;
00231         m_nSaveCookie = m_nBaseCookie;
00232 
00233         CString strPath = Settings.General.UserPath + _T("\\Data\\DownloadGroups.dat");
00234         DeleteFile( strPath + _T(".tmp") );
00235 
00236         CFile pFile;
00237         if ( ! pFile.Open( strPath + _T(".tmp"), CFile::modeWrite | CFile::modeCreate ) ) return FALSE;
00238 
00239         BYTE* pBuffer = new BYTE[ 4096 ];
00240 
00241         try
00242         {
00243                 CArchive ar( &pFile, CArchive::store, 4096, pBuffer );
00244                 Serialize( ar );
00245         }
00246         catch ( CException* pException )
00247         {
00248                 delete [] pBuffer;
00249                 pException->Delete();
00250                 return FALSE;
00251         }
00252 
00253         delete [] pBuffer;
00254 
00255         pFile.Close();
00256 
00257         DeleteFile( strPath );
00258         MoveFile( strPath + _T(".tmp"), strPath );
00259 
00260         return TRUE;
00261 }
00262 
00264 // CDownloadGroups serialize
00265 
00266 #define GROUPS_SER_VERSION      3
00267 
00268 void CDownloadGroups::Serialize(CArchive& ar)
00269 {
00270         int nVersion = GROUPS_SER_VERSION;
00271         BYTE nState;
00272 
00273         if ( ar.IsStoring() )
00274         {
00275                 ar << nVersion;
00276 
00277                 ar.WriteCount( Downloads.GetCount() );
00278 
00279                 for ( POSITION pos = Downloads.GetIterator() ; pos ; )
00280                 {
00281                         ar << Downloads.GetNext( pos )->m_nSerID;
00282                 }
00283 
00284                 ar.WriteCount( GetCount() );
00285 
00286                 for ( POSITION pos = GetIterator() ; pos ; )
00287                 {
00288                         CDownloadGroup* pGroup = GetNext( pos );
00289 
00290                         nState = ( pGroup == m_pSuper ) ? 1 : 0;
00291                         ar << nState;
00292 
00293                         pGroup->Serialize( ar, nVersion );
00294                 }
00295         }
00296         else
00297         {
00298                 ar >> nVersion;
00299                 if ( nVersion <= 1 || nVersion > GROUPS_SER_VERSION ) AfxThrowUserException();
00300 
00301                 int nCount = ar.ReadCount();
00302 
00303                 for ( ; nCount > 0 ; nCount-- )
00304                 {
00305                         DWORD nDownload;
00306                         ar >> nDownload;
00307                         if ( CDownload* pDownload = Downloads.FindBySID( nDownload ) )
00308                                 Downloads.Reorder( pDownload, NULL );
00309                 }
00310 
00311                 if ( nCount = ar.ReadCount() ) Clear();
00312 
00313                 for ( ; nCount > 0 ; nCount-- )
00314                 {
00315                         CDownloadGroup* pGroup = Add();
00316 
00317                         ar >> nState;
00318                         if ( nState == 1 ) m_pSuper = pGroup;
00319 
00320                         pGroup->Serialize( ar, nVersion );
00321                 }
00322 
00323                 GetSuperGroup();
00324 
00325                 for ( POSITION pos = Downloads.GetIterator() ; pos ; )
00326                 {
00327                         m_pSuper->Add( Downloads.GetNext( pos ) );
00328                 }
00329         }
00330 }

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