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

FileExecutor.cpp

Go to the documentation of this file.
00001 //
00002 // FileExecutor.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 "FileExecutor.h"
00026 #include "Plugins.h"
00027 #include "Skin.h"
00028 
00029 #include "Library.h"
00030 #include "SharedFile.h"
00031 #include "SHA.h"
00032 #include "TigerTree.h"
00033 #include "Connection.h"
00034 
00035 #include "WindowManager.h"
00036 #include "WndMain.h"
00037 #include "WndMedia.h"
00038 #include "WndLibrary.h"
00039 
00040 #ifdef _DEBUG
00041 #undef THIS_FILE
00042 static char THIS_FILE[]=__FILE__;
00043 #define new DEBUG_NEW
00044 #endif
00045 
00046 
00048 // CFileExecutor utilities
00049 
00050 void CFileExecutor::GetFileComponents(LPCTSTR pszFile, CString& strPath, CString& strType)
00051 {
00052         CString strFile = pszFile;
00053         int nPos = strFile.ReverseFind( '\\' );
00054         if ( nPos >= 0 ) strPath = strFile.Left( nPos );
00055         nPos = strFile.ReverseFind( '.' );
00056         if ( nPos >= 0 ) strType = strFile.Mid( nPos + 1 );
00057         if ( strType.GetLength() ) strType = _T("|") + strType + _T("|");
00058 }
00059 
00060 CMediaWnd* CFileExecutor::GetMediaWindow(BOOL bFocus)
00061 {
00062         CMainWnd* pMainWnd = (CMainWnd*)theApp.m_pSafeWnd;
00063         if ( pMainWnd == NULL ) return NULL;
00064         if ( pMainWnd->IsKindOf( RUNTIME_CLASS(CMainWnd) ) == FALSE ) return NULL;
00065         return (CMediaWnd*)pMainWnd->m_pWindows.Open( RUNTIME_CLASS(CMediaWnd), FALSE, bFocus );
00066 }
00067 
00068 CLibraryWnd* CFileExecutor::GetLibraryWindow()
00069 {
00070         CMainWnd* pMainWnd = (CMainWnd*)theApp.m_pSafeWnd;
00071         if ( pMainWnd == NULL ) return NULL;
00072         if ( pMainWnd->IsKindOf( RUNTIME_CLASS(CMainWnd) ) == FALSE ) return NULL;
00073         return (CLibraryWnd*)pMainWnd->m_pWindows.Open( RUNTIME_CLASS(CLibraryWnd), FALSE, TRUE );
00074 }
00075 
00077 // CFileExecutor execute
00078 
00079 BOOL CFileExecutor::Execute(LPCTSTR pszFile, BOOL bForce)
00080 {
00081         CString strPath, strType;
00082         CWaitCursor pCursor;
00083 
00084         GetFileComponents( pszFile, strPath, strType );
00085 
00086         if ( strType.GetLength() > 0 && _tcsistr( _T("|co|collection|"), strType ) != NULL )
00087         {
00088                 if ( CLibraryWnd* pWnd = GetLibraryWindow() )
00089                 {
00090                         pWnd->OnCollection( pszFile );
00091                         return TRUE;
00092                 }
00093         }
00094 
00095         if ( bForce == NULL && strType.GetLength() &&
00096                 _tcsistr( Settings.Library.SafeExecute, strType ) == NULL )
00097         {
00098                 CString strFormat, strPrompt;
00099 
00100                 Skin.LoadString( strFormat, IDS_LIBRARY_CONFIRM_EXECUTE );
00101                 strPrompt.Format( strFormat, pszFile );
00102 
00103                 int nResult = AfxMessageBox( strPrompt,
00104                         MB_ICONQUESTION|MB_YESNOCANCEL|MB_DEFBUTTON2 );
00105 
00106                 if ( nResult == IDCANCEL ) return FALSE;
00107                 else if ( nResult == IDNO ) return TRUE;
00108         }
00109 
00110         BOOL bShiftKey = ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) != 0;
00111 
00112         if ( Settings.MediaPlayer.EnablePlay && strType.GetLength() && ! bShiftKey )
00113         {
00114                 if ( _tcsistr( Settings.MediaPlayer.FileTypes, strType ) != NULL )
00115                 {
00116                         BOOL bAudio = _tcsistr( _T("|ape|mid|mp3|ogg|wav|wma|"), strType ) != NULL;
00117 
00118                         if ( CMediaWnd* pWnd = GetMediaWindow( ! bAudio ) )
00119                         {
00120                                 pWnd->PlayFile( pszFile );
00121                                 return TRUE;
00122                         }
00123                 }
00124         }
00125 
00126         if ( ! bShiftKey )
00127                 if ( Plugins.OnExecuteFile( pszFile ) ) return TRUE;
00128 
00129         ShellExecute( AfxGetMainWnd()->GetSafeHwnd(),
00130                 NULL, pszFile, NULL, strPath, SW_SHOWNORMAL );
00131 
00132         return TRUE;
00133 }
00134 
00136 // CFileExecutor enqueue
00137 
00138 BOOL CFileExecutor::Enqueue(LPCTSTR pszFile, BOOL bForce)
00139 {
00140         CString strPath, strType;
00141         CWaitCursor pCursor;
00142 
00143         GetFileComponents( pszFile, strPath, strType );
00144 
00145         if ( Plugins.OnEnqueueFile( pszFile ) ) return TRUE;
00146 
00147         if ( Settings.MediaPlayer.EnableEnqueue && strType.GetLength() &&
00148                  ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) == 0 )
00149         {
00150                 if ( _tcsistr( Settings.MediaPlayer.FileTypes, strType ) != NULL )
00151                 {
00152                         if ( CMediaWnd* pWnd = GetMediaWindow( FALSE ) )
00153                         {
00154                                 pWnd->EnqueueFile( pszFile );
00155                                 return TRUE;
00156                         }
00157                 }
00158         }
00159 
00160         ShellExecute( AfxGetMainWnd()->GetSafeHwnd(),
00161                 _T("Enqueue"), pszFile, NULL, strPath, SW_SHOWNORMAL );
00162 
00163         return TRUE;
00164 }
00165 
00167 // CFileExecutor show Bitzi ticket
00168 
00169 BOOL CFileExecutor::ShowBitziTicket(DWORD nIndex)
00170 {
00171         CString str;
00172 
00173         if ( ! Settings.Library.BitziOkay )
00174         {
00175                 Skin.LoadString( str, IDS_LIBRARY_BITZI_MESSAGE );
00176                 if ( AfxMessageBox( str, MB_ICONQUESTION|MB_YESNO ) != IDYES ) return FALSE;
00177                 Settings.Library.BitziOkay = TRUE;
00178                 Settings.Save();
00179         }
00180 
00181         CSingleLock pLock( &Library.m_pSection, TRUE );
00182 
00183         CLibraryFile* pFile = Library.LookupFile( nIndex );
00184         if ( pFile == NULL ) return FALSE;
00185 
00186         if ( pFile->m_bSHA1 == FALSE || pFile->m_bTiger == FALSE )
00187         {
00188                 CString strFormat;
00189                 Skin.LoadString( strFormat, IDS_LIBRARY_BITZI_HASHED );
00190                 str.Format( strFormat, (LPCTSTR)pFile->m_sName );
00191                 pLock.Unlock();
00192                 AfxMessageBox( str, MB_ICONINFORMATION );
00193                 return FALSE;
00194         }
00195 
00196         CString strURL = Settings.Library.BitziWebView;
00197         CFile hFile;
00198 
00199         if ( hFile.Open( pFile->GetPath(), CFile::modeRead|CFile::shareDenyNone ) && hFile.GetLength() > 0 )
00200         {
00201                 static LPCTSTR pszHex = _T("0123456789ABCDEF");
00202                 BYTE nBuffer[20];
00203                 int nPeek = hFile.Read( nBuffer, 20 );
00204                 hFile.Close();
00205                 str.Empty();
00206 
00207                 for ( int nByte = 0 ; nByte < nPeek ; nByte++ )
00208                 {
00209                         str += pszHex[ (BYTE)nBuffer[ nByte ] >> 4 ];
00210                         str += pszHex[ (BYTE)nBuffer[ nByte ] & 15 ];
00211                 }
00212 
00213                 strURL = Settings.Library.BitziWebSubmit;
00214                 Replace( strURL, _T("(FIRST20)"), str );
00215         }
00216 
00217         Replace( strURL, _T("(NAME)"), CConnection::URLEncode( pFile->m_sName ) );
00218         Replace( strURL, _T("(SHA1)"), CSHA::HashToString( &pFile->m_pSHA1 ) );
00219         Replace( strURL, _T("(TTH)"), CTigerNode::HashToString( &pFile->m_pTiger ) );
00220         Replace( strURL, _T("(AGENT)"), CConnection::URLEncode( Settings.SmartAgent() ) );
00221 
00222         str.Format( _T("%I64i"), pFile->GetSize() );
00223         Replace( strURL, _T("(SIZE)"), str );
00224 
00225         pLock.Unlock();
00226 
00227         DisplayURL( strURL );
00228 
00229         return TRUE;
00230 }
00231 
00233 // CFileExecutor display a URL
00234 
00235 BOOL CFileExecutor::DisplayURL(LPCTSTR pszURL)
00236 {
00237         ShellExecute( AfxGetMainWnd()->GetSafeHwnd(), _T("open"), pszURL, NULL, NULL, SW_SHOWNORMAL );
00238         return TRUE;
00239 
00240 #if 0
00241         DWORD dwFilterFlags = 0;
00242         BOOL bSuccess = FALSE;
00243         DWORD hInstance = 0;
00244 
00245         UINT uiResult = DdeInitialize( &hInstance, DDECallback, dwFilterFlags, 0 );
00246         if ( uiResult != DMLERR_NO_ERROR ) return FALSE;
00247 
00248         HSZ hszService  = DdeCreateStringHandle( hInstance, L"IExplore", CP_WINUNICODE );
00249         HSZ hszTopic    = DdeCreateStringHandle( hInstance, L"WWW_OpenURL", CP_WINUNICODE );
00250 
00251         if ( HCONV hConv = DdeConnect( hInstance, hszService, hszTopic, NULL ) )
00252         {
00253                 CString strCommand;
00254                 USES_CONVERSION;
00255 
00256                 strCommand.Format( _T("\"%s\",,0"), pszURL );
00257                 LPCSTR pszCommand = T2CA( (LPCTSTR)strCommand );
00258 
00259                 DdeClientTransaction( (LPBYTE)pszCommand, pszCommand,
00260                          hConv, 0, 0, XTYP_EXECUTE, 4000, NULL );
00261 
00262                 DdeDisconnect( hConv );
00263         }
00264 
00265         DdeFreeStringHandle( hInstance, hszTopic );
00266         DdeFreeStringHandle( hInstance, hszService );
00267 
00268         DdeUninitialize( hInstance );
00269 
00270         return bSuccess;
00271 #endif
00272 }
00273 
00274 HDDEDATA CALLBACK CFileExecutor::DDECallback(UINT wType, UINT wFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2)
00275 {
00276         return (HDDEDATA)NULL;
00277 }

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