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

CtrlLibraryCollectionView.cpp

Go to the documentation of this file.
00001 //
00002 // CtrlLibraryCollectionView.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 
00026 #include "Network.h"
00027 #include "Library.h"
00028 #include "AlbumFolder.h"
00029 #include "SharedFile.h"
00030 #include "Transfers.h"
00031 #include "Downloads.h"
00032 #include "Download.h"
00033 #include "FileExecutor.h"
00034 #include "ShareazaURL.h"
00035 #include "IEProtocol.h"
00036 #include "CollectionFile.h"
00037 #include "SHA.h"
00038 
00039 #include "Skin.h"
00040 #include "CtrlWeb.h"
00041 #include "CtrlLibraryTip.h"
00042 #include "CtrlLibraryFrame.h"
00043 #include "CtrlLibraryTree.h"
00044 #include "CtrlLibraryCollectionView.h"
00045 
00046 IMPLEMENT_DYNCREATE(CLibraryCollectionView, CLibraryFileView)
00047 
00048 BEGIN_INTERFACE_MAP(CLibraryCollectionView::External, CComObject)
00049         INTERFACE_PART(CLibraryCollectionView::External, IID_ICollectionHtmlView, View)
00050 END_INTERFACE_MAP()
00051 
00052 BEGIN_MESSAGE_MAP(CLibraryCollectionView, CLibraryFileView)
00053         ON_WM_CREATE()
00054         ON_WM_DESTROY()
00055         ON_WM_SIZE()
00056         ON_NOTIFY(WVN_CONTEXTMENU, AFX_IDW_PANE_FIRST, OnWebContextMenu)
00057         ON_UPDATE_COMMAND_UI(ID_LIBRARY_FOLDER_DOWNLOAD, OnUpdateLibraryFolderDownload)
00058         ON_COMMAND(ID_LIBRARY_FOLDER_DOWNLOAD, OnLibraryFolderDownload)
00059 END_MESSAGE_MAP()
00060 
00061 
00063 // CLibraryCollectionView construction
00064 
00065 CLibraryCollectionView::CLibraryCollectionView()
00066 {
00067         m_nCommandID = ID_LIBRARY_VIEW_COLLECTION;
00068 
00069         m_pWebCtrl              = NULL;
00070         m_nWebIndex             = 0;
00071         m_pCollection   = new CCollectionFile();
00072 
00073         m_xExternal.m_pView = this;
00074         m_pszToolBar = _T("CLibraryCollectionView");
00075 }
00076 
00077 CLibraryCollectionView::~CLibraryCollectionView()
00078 {
00079         delete m_pCollection;
00080 }
00081 
00083 // CLibraryCollectionView view operations
00084 
00085 BOOL CLibraryCollectionView::CheckAvailable(CLibraryTreeItem* pSel)
00086 {
00087         BOOL bAvailable = FALSE;
00088 
00089         if ( CAlbumFolder* pFolder = GetSelectedAlbum( pSel ) )
00090         {
00091                 if ( pFolder->m_bCollSHA1 )
00092                 {
00093                         if ( LibraryMaps.LookupFileBySHA1( &pFolder->m_pCollSHA1, FALSE, TRUE ) )
00094                         {
00095                                 bAvailable = TRUE;
00096                         }
00097                         else
00098                         {
00099                                 CQuickLock oLock( Library.m_pSection );
00100                                 pFolder->m_bCollSHA1 = FALSE;
00101                                 Library.Update();
00102                         }
00103                 }
00104         }
00105 
00106         if ( bAvailable != m_bAvailable )
00107         {
00108                 m_bAvailable = bAvailable;
00109                 m_xExternal.m_bLockdown = FALSE;
00110         }
00111 
00112         return m_bAvailable;
00113 }
00114 
00115 void CLibraryCollectionView::Update()
00116 {
00117         if ( CAlbumFolder* pFolder = GetSelectedAlbum() )
00118         {
00119                 if ( pFolder->m_bCollSHA1 && m_pWebCtrl != NULL )
00120                 {
00121                         if ( CLibraryFile* pFile = LibraryMaps.LookupFileBySHA1( &pFolder->m_pCollSHA1, FALSE, TRUE ) )
00122                         {
00123                                 ShowCollection( pFile );
00124                                 return;
00125                         }
00126                 }
00127         }
00128 
00129         ShowCollection( NULL );
00130 }
00131 
00132 BOOL CLibraryCollectionView::ShowCollection(CLibraryFile* pFile)
00133 {
00134         if ( pFile != NULL )
00135         {
00136                 if ( m_pCollection->IsOpen() && m_pSHA1 == pFile->m_pSHA1 ) return TRUE;
00137 
00138                 if ( m_pCollection->Open( pFile->GetPath() ) )
00139                 {
00140                         CString strIndex, strURL;
00141                         IEProtocol.SetCollection( &pFile->m_pSHA1, pFile->GetPath(), &strIndex );
00142 
00143                         strURL.Format( _T("p2p-col://%s/%s"),
00144                                 (LPCTSTR)CSHA::HashToString( &pFile->m_pSHA1 ),
00145                                 (LPCTSTR)strIndex );
00146                         m_pWebCtrl->Navigate( strURL );
00147 
00148                         m_pSHA1 = pFile->m_pSHA1;
00149                         return TRUE;
00150                 }
00151         }
00152 
00153         if ( m_pCollection->IsOpen() )
00154         {
00155                 m_pCollection->Close();
00156                 if ( m_pWebCtrl != NULL ) m_pWebCtrl->Navigate( _T("about:blank") );
00157         }
00158 
00159         return FALSE;
00160 }
00161 
00163 // CLibraryCollectionView message handlers
00164 
00165 int CLibraryCollectionView::OnCreate(LPCREATESTRUCT lpCreateStruct)
00166 {
00167         if ( CLibraryFileView::OnCreate( lpCreateStruct ) == -1 ) return -1;
00168 
00169         CWaitCursor pCursor;
00170         m_pWebCtrl = new CWebCtrl();
00171 
00172         if ( m_pWebCtrl->Create( 0, this ) != -1 )
00173         {
00174                 m_pWebCtrl->EnableCoolMenu();
00175                 m_pWebCtrl->EnableSandbox();
00176                 m_pWebCtrl->SetExternal( m_xExternal.GetDispatch() );
00177                 m_pWebCtrl->Navigate( _T("about:blank") );
00178         }
00179         else
00180         {
00181                 delete m_pWebCtrl;
00182                 m_pWebCtrl = NULL;
00183         }
00184 
00185         return 0;
00186 }
00187 
00188 void CLibraryCollectionView::OnDestroy()
00189 {
00190         if ( m_pWebCtrl != NULL )
00191         {
00192                 m_pWebCtrl->DestroyWindow();
00193                 delete m_pWebCtrl;
00194                 m_pWebCtrl = NULL;
00195         }
00196 
00197         IEProtocol.SetCollection( NULL, NULL );
00198         m_pCollection->Close();
00199 
00200         CLibraryFileView::OnDestroy();
00201 }
00202 
00203 void CLibraryCollectionView::OnSize(UINT nType, int cx, int cy)
00204 {
00205         CLibraryFileView::OnSize( nType, cx, cy );
00206 
00207         if ( m_pWebCtrl != NULL && m_pWebCtrl->GetSafeHwnd() != NULL )
00208         {
00209                 m_pWebCtrl->SetWindowPos( NULL, 0, 0, cx, cy, SWP_SHOWWINDOW );
00210         }
00211 }
00212 
00213 void CLibraryCollectionView::OnWebContextMenu(NMHDR* pNMHDR, LPARAM* pResult)
00214 {
00215         WVNCONTEXTMENU* pNotify = (WVNCONTEXTMENU*)pNMHDR;
00216 
00217         if ( m_nWebIndex != 0 )
00218         {
00219                 *pResult = TRUE;
00220                 GetToolTip()->Hide();
00221 
00222                 SelClear( FALSE );
00223                 SelAdd( m_nWebIndex );
00224 
00225                 CPoint point( pNotify->ptMouse );
00226 
00227                 CString strName = _T("CLibraryFileView");
00228                 strName += Settings.Library.ShowVirtual ? _T(".Virtual") : _T(".Physical");
00229                 UINT nCmdID = Skin.TrackPopupMenu( strName, point, ID_LIBRARY_LAUNCH, TPM_RETURNCMD );
00230 
00231                 if ( nCmdID != 0 ) GetFrame()->SendMessage( WM_COMMAND, nCmdID );
00232 
00233                 SelClear( TRUE );
00234         }
00235 }
00236 
00237 void CLibraryCollectionView::OnUpdateLibraryFolderDownload(CCmdUI *pCmdUI)
00238 {
00239         pCmdUI->Enable( m_pCollection->GetMissingCount() > 0 );
00240 }
00241 
00242 void CLibraryCollectionView::OnLibraryFolderDownload()
00243 {
00244         for ( POSITION pos = m_pCollection->GetFileIterator() ; pos ; )
00245         {
00246                 CCollectionFile::File* pFile = m_pCollection->GetNextFile( pos );
00247                 pFile->Download();
00248         }
00249 
00250         if ( ! Network.IsWellConnected() ) Network.Connect( TRUE );
00251         PostUpdate();
00252 }
00253 
00255 // CLibraryCollectionView::External construction
00256 
00257 CLibraryCollectionView::External::External()
00258 {
00259         EnableDispatch( IID_ICollectionHtmlView );
00260         m_bLockdown = FALSE;
00261 }
00262 
00264 // CLibraryCollectionView::External ICollectionHtmlView implementation
00265 
00266 IMPLEMENT_DISPATCH(CLibraryCollectionView::External, View)
00267 
00268 STDMETHODIMP CLibraryCollectionView::External::XView::get_Application(IApplication **ppApplication)
00269 {
00270         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00271         *ppApplication = NULL;
00272         return E_NOTIMPL;
00273 }
00274 
00275 STDMETHODIMP CLibraryCollectionView::External::XView::Detect(BSTR sURN, BSTR *psState)
00276 {
00277         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00278 
00279         if ( pThis->m_bLockdown )
00280         {
00281                 CString( _T("Lockdown") ).SetSysString( psState );
00282         }
00283         else if ( pThis->m_pView->m_pCollection->FindByURN( CString( sURN ) ) == NULL )
00284         {
00285                 CString( _T("NotInCollection") ).SetSysString( psState );
00286         }
00287         else if ( LibraryMaps.LookupFileByURN( CString( sURN ), FALSE, TRUE ) )
00288         {
00289                 CString( _T("Complete") ).SetSysString( psState );
00290         }
00291         else
00292         {
00293                 CSingleLock pLock( &Transfers.m_pSection, TRUE );
00294 
00295                 if ( CDownload* pDownload = Downloads.FindByURN( CString( sURN ) ) )
00296                 {
00297                         CString str;
00298                         str.Format( _T("%.2f%%"), pDownload->GetProgress() * 100.0 );
00299                         str.SetSysString( psState );
00300                 }
00301                 else
00302                 {
00303                         CString().SetSysString( psState );
00304                 }
00305         }
00306 
00307         return S_OK;
00308 }
00309 
00310 STDMETHODIMP CLibraryCollectionView::External::XView::Hover(BSTR sURN)
00311 {
00312         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00313         if ( pThis->m_bLockdown ) return S_OK;
00314 
00315         CLibraryCollectionView* pView = pThis->m_pView;
00316         if ( pView->m_pWebCtrl == NULL ) return S_OK;
00317         if ( pView->GetFrame() == NULL ) return S_OK;
00318 
00319         pView->m_nWebIndex = 0;
00320 
00321         if ( sURN != NULL && wcslen( sURN ) != 0 )
00322         {
00323                 if ( pThis->m_pView->m_pCollection->FindByURN( CString( sURN ) ) != NULL )
00324                 {
00325                         CQuickLock oLock( Library.m_pSection );
00326                         if ( CLibraryFile* pFile = LibraryMaps.LookupFileByURN( CString( sURN ), FALSE, TRUE ) )
00327                         {
00328                                 pView->m_nWebIndex = pFile->m_nIndex;
00329                         }
00330                 }
00331         }
00332 
00333         if ( pView->m_nWebIndex != 0 )
00334         {
00335                 HWND hWnd = pView->m_pWebCtrl->GetSafeHwnd();
00336                 pView->GetToolTip()->Show( (LPVOID)pView->m_nWebIndex, hWnd );
00337         }
00338         else
00339         {
00340                 pView->GetToolTip()->Hide();
00341         }
00342 
00343         return S_OK;
00344 }
00345 
00346 STDMETHODIMP CLibraryCollectionView::External::XView::Open(BSTR sURN, VARIANT_BOOL *pbResult)
00347 {
00348         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00349         *pbResult = VARIANT_FALSE;
00350         if ( pThis->m_bLockdown ) return S_OK;
00351 
00352         if ( pThis->m_pView->m_pCollection->FindByURN( CString( sURN ) ) != NULL )
00353         {
00354                 CSingleLock oLock( &Library.m_pSection, TRUE );
00355                 if ( CLibraryFile* pFile = LibraryMaps.LookupFileByURN( CString( sURN ), FALSE, TRUE ) )
00356                 {
00357                         if ( pFile->m_pFolder )
00358                         {
00359                                 CString strPath = pFile->GetPath();
00360                                 oLock.Unlock();
00361                                 *pbResult = CFileExecutor::Execute( strPath, FALSE ) ? VARIANT_TRUE : VARIANT_FALSE;
00362                         }
00363                         else
00364                         {
00365                                 *pbResult = VARIANT_FALSE;
00366                         }
00367                 }
00368         }
00369 
00370         return S_OK;
00371 }
00372 
00373 STDMETHODIMP CLibraryCollectionView::External::XView::Enqueue(BSTR sURN, VARIANT_BOOL *pbResult)
00374 {
00375         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00376         *pbResult = VARIANT_FALSE;
00377         if ( pThis->m_bLockdown ) return S_OK;
00378 
00379         if ( pThis->m_pView->m_pCollection->FindByURN( CString( sURN ) ) != NULL )
00380         {
00381                 CSingleLock oLock( &Library.m_pSection, TRUE );
00382                 if ( CLibraryFile* pFile = LibraryMaps.LookupFileByURN( CString( sURN ), FALSE, TRUE ) )
00383                 {
00384                         CString strPath = pFile->GetPath();
00385                         oLock.Unlock();
00386                         *pbResult = CFileExecutor::Enqueue( strPath ) ? VARIANT_TRUE : VARIANT_FALSE;
00387                 }
00388         }
00389 
00390         return S_OK;
00391 }
00392 
00393 STDMETHODIMP CLibraryCollectionView::External::XView::Download(BSTR sURN, VARIANT_BOOL *pbResult)
00394 {
00395         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00396         *pbResult = VARIANT_FALSE;
00397         if ( pThis->m_bLockdown ) return S_OK;
00398 
00399         if ( CCollectionFile::File* pFile = pThis->m_pView->m_pCollection->FindByURN( CString( sURN ) ) )
00400         {
00401                 if ( ! pFile->IsComplete() )
00402                 {
00403                         CString strFormat, strMessage;
00404                         LoadString( strFormat, IDS_LIBRARY_COLLECTION_DOWNLOAD_FILE );
00405                         strMessage.Format( strFormat, (LPCTSTR)pFile->m_sName );
00406 
00407                         UINT nResponse = AfxMessageBox( strMessage, MB_ICONQUESTION|MB_YESNOCANCEL );
00408 
00409                         if ( nResponse == IDYES )
00410                         {
00411                                 *pbResult = pFile->Download() ? VARIANT_TRUE : VARIANT_FALSE;
00412 
00413                                 if ( *pbResult == VARIANT_TRUE )
00414                                 {
00415                                         if ( ! Network.IsWellConnected() ) Network.Connect( TRUE );
00416                                 }
00417                         }
00418                         else if ( nResponse == IDCANCEL )
00419                         {
00420                                 pThis->CheckLockdown();
00421                         }
00422                 }
00423         }
00424 
00425         return S_OK;
00426 }
00427 
00428 STDMETHODIMP CLibraryCollectionView::External::XView::DownloadAll()
00429 {
00430         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00431         if ( pThis->m_bLockdown ) return S_OK;
00432 
00433         CString strMessage;
00434         LoadString( strMessage, IDS_LIBRARY_COLLECTION_DOWNLOAD_ALL );
00435         UINT nResponse = AfxMessageBox( strMessage, MB_ICONQUESTION|MB_YESNOCANCEL );
00436 
00437         if ( nResponse == IDYES )
00438                 pThis->m_pView->PostMessage( WM_COMMAND, ID_LIBRARY_FOLDER_DOWNLOAD );
00439         else if ( nResponse == IDCANCEL )
00440                 pThis->CheckLockdown();
00441 
00442         return S_OK;
00443 }
00444 
00445 STDMETHODIMP CLibraryCollectionView::External::XView::get_MissingCount(LONG *pnCount)
00446 {
00447         METHOD_PROLOGUE(CLibraryCollectionView::External, View)
00448         if ( pThis->m_bLockdown ) return S_OK;
00449 
00450         *pnCount = pThis->m_pView->m_pCollection->GetMissingCount();
00451 
00452         return S_OK;
00453 }
00454 
00455 void CLibraryCollectionView::External::CheckLockdown()
00456 {
00457         CString strMessage;
00458         LoadString( strMessage, IDS_LIBRARY_COLLECTION_LOCKDOWN );
00459         if ( AfxMessageBox( strMessage, MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION ) == IDYES ) m_bLockdown = TRUE;
00460 }

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