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

Shareaza.cpp

Go to the documentation of this file.
00001 //
00002 // Shareaza.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 "CoolInterface.h"
00026 #include "Network.h"
00027 #include "Security.h"
00028 #include "HostCache.h"
00029 #include "DiscoveryServices.h"
00030 #include "VersionChecker.h"
00031 #include "SchemaCache.h"
00032 #include "VendorCache.h"
00033 #include "EDClients.h"
00034 #include "BTClients.h"
00035 #include "Library.h"
00036 #include "Transfers.h"
00037 #include "DownloadGroups.h"
00038 #include "Downloads.h"
00039 #include "Uploads.h"
00040 #include "UploadQueues.h"
00041 #include "QueryHashMaster.h"
00042 #include "DDEServer.h"
00043 #include "IEProtocol.h"
00044 #include "ShareazaURL.h"
00045 #include "GProfile.h"
00046 #include "SharedFile.h"
00047 #include "Emoticons.h"
00048 #include "ShellIcons.h"
00049 #include "Skin.h"
00050 #include "Scheduler.h"
00051 #include "FileExecutor.h"
00052 
00053 #include "WndMain.h"
00054 #include "WndSystem.h"
00055 #include "DlgSplash.h"
00056 #include "DlgHelp.h"
00057 
00058 #ifdef _DEBUG
00059 #define new DEBUG_NEW
00060 #undef THIS_FILE
00061 static char THIS_FILE[] = __FILE__;
00062 #endif
00063 
00064 BEGIN_MESSAGE_MAP(CShareazaApp, CWinApp)
00065         //{{AFX_MSG_MAP(CShareazaApp)
00066         //}}AFX_MSG
00067         ON_COMMAND(ID_HELP, CWinApp::OnHelp)
00068 END_MESSAGE_MAP()
00069 
00070 CShareazaApp theApp;
00071 
00073 // CShareazaApp construction
00074 
00075 CShareazaApp::CShareazaApp() : m_pMutex( FALSE, _T("Shareaza") )
00076 {
00077         m_pSafeWnd      = NULL;
00078         m_bLive         = FALSE;
00079 }
00080 
00082 // CShareazaApp initialization
00083 
00084 BOOL CShareazaApp::InitInstance()
00085 {
00086         CWaitCursor pCursor;
00087         
00088         if ( ! m_pMutex.Lock( 0 ) )
00089         {
00090                 if ( CWnd* pWnd = CWnd::FindWindow( _T("ShareazaMainWnd"), NULL ) )
00091                 {
00092                         pWnd->SendMessage( WM_SYSCOMMAND, SC_RESTORE );
00093                         pWnd->ShowWindow( SW_SHOWNORMAL );
00094                         pWnd->BringWindowToTop();
00095                         pWnd->SetForegroundWindow();
00096                 }
00097                 
00098                 return FALSE;
00099         }
00100 
00101         // **********************
00102 
00103 /*
00104         // Beta expiry. Remember to re-compile to update the time, and remove this 
00105         // section for final releases and public betas.
00106         COleDateTime tCompileTime; 
00107         tCompileTime.ParseDateTime( _T(__DATE__), LOCALE_NOUSEROVERRIDE, 1033 );
00108         COleDateTime tCurrent = COleDateTime::GetCurrentTime();
00109         COleDateTimeSpan tTimeOut( 28, 0, 0, 0);
00110         if ( ( tCompileTime + tTimeOut )  < tCurrent )
00111         {
00112                 CString strMessage;
00113                 LoadString( strMessage, IDS_BETA_EXPIRED);
00114                 AfxMessageBox( strMessage, MB_SYSTEMMODAL|MB_ICONQUESTION|MB_OK );
00115                 //return FALSE;
00116         }
00117 */
00118 
00119 /*
00120         // Alpha warning nag message. Remember to remove this section for final releases and public betas.
00121         if ( AfxMessageBox( _T("WARNING: This is an ALPHA TEST version of Shareaza.\n\nIt it NOT FOR GENERAL USE, and is only for testing specific features in a controlled environment. It will frequently stop running, or display debug information to assist testing.\n\nIf you wish to actually use this software, you should download the current stable release from www.shareaza.com\nIf you continue past this point, you may experience system instability, lose downloads, or corrupt system files. Corrupted downloads/files may not be recoverable. Do you wish to continue?"), MB_SYSTEMMODAL|MB_ICONEXCLAMATION|MB_YESNO ) == IDNO )
00122                 return FALSE;
00123 */
00124         // **********************
00125 
00126 
00127 
00128         // Enable3dControls();
00129         SetRegistryKey( _T("Shareaza") );
00130         GetVersionNumber();
00131         InitResources();
00132         
00133         BOOL bSilentTray = ( m_lpCmdLine && _tcsistr( m_lpCmdLine, _T("-tray") ) != NULL );
00134         
00135                 AfxOleInit();
00136                 AfxEnableControlContainer();
00137         
00138         CSplashDlg* dlgSplash = new CSplashDlg( 18, bSilentTray );
00139         
00140         dlgSplash->Step( _T("Winsock") );
00141                 WSADATA wsaData;
00142                 if ( WSAStartup( 0x0101, &wsaData ) ) return FALSE;
00143         
00144         dlgSplash->Step( _T("Settings Database") );
00145                 Settings.Load();
00146         dlgSplash->Step( _T("P2P URIs") );
00147                 CShareazaURL::Register( TRUE );
00148         dlgSplash->Step( _T("Shell Icons") );
00149                 ShellIcons.Clear();
00150         dlgSplash->Step( _T("Metadata Schemas") );
00151                 SchemaCache.Load();
00152         dlgSplash->Step( _T("Vendor Data") );
00153                 VendorCache.Load();
00154         dlgSplash->Step( _T("Profile") );
00155                 MyProfile.Load();
00156         dlgSplash->Step( _T("Library") );
00157                 Library.Load();
00158         dlgSplash->Step( _T("Query Manager") );
00159                 QueryHashMaster.Create();
00160         dlgSplash->Step( _T("Host Cache") );
00161                 HostCache.Load();
00162         dlgSplash->Step( _T("Discovery Services") );
00163                 DiscoveryServices.Load();
00164         dlgSplash->Step( _T("Security Services") );
00165                 Security.Load();
00166                 AdultFilter.Load();
00167                 MessageFilter.Load();
00168         dlgSplash->Step( _T("Scheduler") );
00169                 Schedule.Load();
00170         dlgSplash->Step( _T("Rich Documents") );
00171                 Emoticons.Load();
00172         dlgSplash->Step( _T("GUI") );
00173         
00174         if ( bSilentTray ) WriteProfileInt( _T("Windows"), _T("CMainWnd.ShowCmd"), 0 );
00175         
00176         m_pMainWnd = new CMainWnd();
00177         CoolMenu.EnableHook();
00178         
00179         if ( bSilentTray )
00180         {
00181                 ((CMainWnd*)m_pMainWnd)->CloseToTray();
00182         }
00183         else
00184         {
00185                 dlgSplash->Topmost();
00186                 m_pMainWnd->ShowWindow( SW_SHOW );
00187                 m_pMainWnd->UpdateWindow();
00188         }
00189         // From this point translations are available and LoadString returns correct strings
00190         dlgSplash->Step( _T("Download Manager") ); 
00191                 Downloads.Load();
00192         dlgSplash->Step( _T("Upload Manager") );
00193                 UploadQueues.Load();
00194 
00195         dlgSplash->Step( _T("IPC") );
00196                 DDEServer.Create();
00197                 IEProtocol.Create();
00198 
00199         dlgSplash->Step( _T("Upgrade Manager") );
00200         if ( VersionChecker.NeedToCheck() ) VersionChecker.Start( m_pMainWnd->GetSafeHwnd() );
00201         
00202         pCursor.Restore();
00203         
00204         dlgSplash->Hide();
00205         m_bLive = TRUE;
00206         
00207         return TRUE;
00208 }
00209 
00211 // CShareazaApp termination
00212 
00213 int CShareazaApp::ExitInstance() 
00214 {
00215         CWaitCursor pCursor;
00216         
00217         DDEServer.Close();
00218         IEProtocol.Close();
00219         VersionChecker.Stop();
00220         DiscoveryServices.Stop();
00221         Network.Disconnect();
00222         Library.StopThread();
00223         
00224         Transfers.StopThread();
00225         Downloads.CloseTransfers();
00226         Uploads.Clear( FALSE );
00227         EDClients.Clear();
00228         BTClients.Clear();
00229         
00230         if ( m_bLive )
00231         {
00232                 Downloads.Save();
00233                 DownloadGroups.Save();
00234                 Library.Save();
00235                 Security.Save();
00236                 HostCache.Save();
00237                 UploadQueues.Save();
00238                 DiscoveryServices.Save();
00239         }
00240         
00241         Downloads.Clear( TRUE );
00242         Library.Clear();
00243         Skin.Clear();
00244         
00245         if ( m_bLive ) Settings.Save( TRUE );
00246 
00247         if ( m_hUser32 != NULL ) FreeLibrary( m_hUser32 );
00248         
00249         WSACleanup();
00250         
00251         return CWinApp::ExitInstance();
00252 }
00253 
00255 // CShareazaApp help (suppress F1)
00256 
00257 void CShareazaApp::WinHelp(DWORD dwData, UINT nCmd) 
00258 {
00259 }
00260 
00262 // CShareazaApp version
00263 
00264 void CShareazaApp::GetVersionNumber()
00265 {
00266         TCHAR szPath[128];
00267         DWORD dwSize;
00268 
00269         m_nVersion[0] = m_nVersion[1] = m_nVersion[2] = m_nVersion[3] = 0;
00270 
00271         GetModuleFileName( NULL, szPath, 128 );
00272         dwSize = GetFileVersionInfoSize( szPath, &dwSize );
00273 
00274         if ( dwSize )
00275         {
00276                 BYTE* pBuffer = new BYTE[ dwSize ];
00277 
00278                 if ( GetFileVersionInfo( szPath, NULL, dwSize, pBuffer ) )
00279                 {
00280                         VS_FIXEDFILEINFO* pTable;
00281 
00282                         if ( VerQueryValue( pBuffer, _T("\\"), (VOID**)&pTable, (UINT*)&dwSize ) )
00283                         {
00284                                 m_nVersion[0] = (WORD)( pTable->dwFileVersionMS >> 16 );
00285                                 m_nVersion[1] = (WORD)( pTable->dwFileVersionMS & 0xFFFF );
00286                                 m_nVersion[2] = (WORD)( pTable->dwFileVersionLS >> 16 );
00287                                 m_nVersion[3] = (WORD)( pTable->dwFileVersionLS & 0xFFFF );
00288                         }
00289                 }
00290 
00291                 delete [] pBuffer;
00292         }
00293 
00294         m_sVersion.Format( _T("%i.%i.%i.%i"),
00295                 m_nVersion[0], m_nVersion[1],
00296                 m_nVersion[2], m_nVersion[3] );
00297 }
00298 
00300 // CShareazaApp resources
00301 
00302 void CShareazaApp::InitResources()
00303 {
00304         //Determine the version of Windows
00305         OSVERSIONINFO pVersion;
00306         pVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00307         GetVersionEx( &pVersion );
00308         
00309         //Networking is poor under Win9x based operating systems. (95/98/Me)
00310         m_bNT = ( pVersion.dwPlatformId == VER_PLATFORM_WIN32_NT );
00311 
00312         //Win 95/98/Me/NT (<5) do not support some functions
00313         m_dwWindowsVersion = pVersion.dwMajorVersion; 
00314 
00315         //Win2000 = 0 WinXP = 1
00316         m_dwWindowsVersionMinor = pVersion.dwMinorVersion; 
00317 
00318         m_bLimitedConnections = FALSE;
00319         VER_PLATFORM_WIN32s;
00320         VER_PLATFORM_WIN32_WINDOWS;
00321         VER_PLATFORM_WIN32_NT;
00322 
00323         if ( m_dwWindowsVersion == 5 && m_dwWindowsVersionMinor == 1 )
00324         {       //Windows XP - Test for SP2
00325                 TCHAR* sp = _tcsstr( pVersion.szCSDVersion, _T("Service Pack ") );
00326                 if( sp && sp[ 13 ] >= '2' )
00327                 {       //XP SP2 - Limit the networking.
00328                         //AfxMessageBox(_T("Warning - Windows XP Service Pack 2 detected. Performance may be reduced."), MB_OK );
00329                         m_bLimitedConnections = TRUE;
00330                 }
00331         }
00332         else if ( m_dwWindowsVersion == 5 && m_dwWindowsVersionMinor == 2
00333                 && _tcsstr( pVersion.szCSDVersion, _T("Service Pack") ) )
00334         {
00335                 // Windows 2003 or Win XP x64
00336                 m_bLimitedConnections = TRUE;
00337         }
00338 
00339         //Get the amount of installed memory.
00340         m_nPhysicalMemory = 0;
00341         if ( m_hUser32 = LoadLibrary( _T("User32.dll") ) )
00342         {       //Use GlobalMemoryStatusEx if possible (WinXP)
00343                 void (WINAPI *m_pfnGlobalMemoryStatus)( LPMEMORYSTATUSEX );
00344                 MEMORYSTATUSEX pMemory;
00345 
00346                 (FARPROC&)m_pfnGlobalMemoryStatus = GetProcAddress(
00347                         m_hUser32, "GlobalMemoryStatusEx" );
00348 
00349                 if ( m_pfnGlobalMemoryStatus )
00350                 {
00351                         m_pfnGlobalMemoryStatus( &pMemory ); 
00352                         m_nPhysicalMemory = pMemory.ullTotalPhys;
00353                 }
00354         }
00355 
00356         if ( m_nPhysicalMemory == 0 )
00357         {       //Fall back to GlobalMemoryStatusEx (always available)
00358                 MEMORYSTATUS pMemory;
00359                 GlobalMemoryStatus( &pMemory ); 
00360                 m_nPhysicalMemory = pMemory.dwTotalPhys;
00361         }
00362         
00363         //Get pointers to some functions that don't exist under 95/NT
00364         if ( m_hUser32 = LoadLibrary( _T("User32.dll") ) )
00365         {
00366                 (FARPROC&)m_pfnSetLayeredWindowAttributes = GetProcAddress(
00367                         m_hUser32, "SetLayeredWindowAttributes" );
00368                    
00369                 (FARPROC&)m_pfnGetMonitorInfoA = GetProcAddress( 
00370                         m_hUser32, "GetMonitorInfoA" ); 
00371     
00372                 (FARPROC&)m_pfnMonitorFromRect = GetProcAddress( 
00373                         m_hUser32, "MonitorFromRect" ); 
00374 
00375                 (FARPROC&)m_pfnMonitorFromWindow = GetProcAddress( 
00376                         m_hUser32, "MonitorFromWindow" ); 
00377         }
00378         else
00379         {
00380                 m_pfnSetLayeredWindowAttributes = NULL;
00381                 m_pfnGetMonitorInfoA = NULL; 
00382         m_pfnMonitorFromRect = NULL; 
00383                 m_pfnMonitorFromWindow = NULL;
00384         }
00385 
00386         if ( m_hGDI32 = LoadLibrary( _T("gdi32.dll") ) )
00387                 (FARPROC&)m_pfnSetLayout = GetProcAddress( m_hGDI32, "SetLayout" );
00388         else
00389                 m_pfnSetLayout = NULL;
00390 
00391         // Get the fonts from the registry
00392         theApp.m_sDefaultFont           = theApp.GetProfileString( _T("Fonts"), _T("DefaultFont"), _T("Tahoma") );
00393         theApp.m_sPacketDumpFont        = theApp.GetProfileString( _T("Fonts"), _T("PacketDumpFont"), _T("Lucida Console") );
00394         theApp.m_sSystemLogFont         = theApp.GetProfileString( _T("Fonts"), _T("SystemLogFont"), _T("Tahoma") );
00395         theApp.m_nDefaultFontSize       = theApp.GetProfileInt( _T("Fonts"), _T("FontSize"), 11 );
00396         
00397         // Set up the default font
00398         m_gdiFont.CreateFont( -theApp.m_nDefaultFontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
00399                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00400                 DEFAULT_PITCH|FF_DONTCARE, theApp.m_sDefaultFont );
00401         
00402         m_gdiFontBold.CreateFont( -theApp.m_nDefaultFontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
00403                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00404                 DEFAULT_PITCH|FF_DONTCARE, theApp.m_sDefaultFont );
00405         
00406         m_gdiFontLine.CreateFont( -theApp.m_nDefaultFontSize, 0, 0, 0, FW_NORMAL, FALSE, TRUE, FALSE,
00407                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00408                 DEFAULT_PITCH|FF_DONTCARE, theApp.m_sDefaultFont );
00409 
00410         theApp.m_bRTL = theApp.GetProfileInt( _T("Settings"), _T("LanguageRTL"), 0 );
00411 
00412         srand( GetTickCount() );
00413 }
00414 
00416 // CShareazaApp safe main window
00417 
00418 CMainWnd* CShareazaApp::SafeMainWnd()
00419 {
00420         CMainWnd* pMainWnd = (CMainWnd*)theApp.m_pSafeWnd;
00421         if ( pMainWnd == NULL ) return NULL;
00422         ASSERT_KINDOF( CMainWnd, pMainWnd );
00423         return IsWindow( pMainWnd->m_hWnd ) ? pMainWnd : NULL;
00424 }
00425 
00427 // CShareazaApp message
00428 
00429 TCHAR CShareazaApp::szMessageBuffer[16384];
00430 
00431 void CShareazaApp::Message(int nType, UINT nID, ...)
00432 {
00433         if ( nType == MSG_DEBUG && ! Settings.General.Debug ) return;
00434         if ( nType == MSG_TEMP && ! Settings.General.DebugLog ) return;
00435         
00436         CSingleLock pLock( &m_csMessage, TRUE );
00437         CString strFormat;
00438         va_list pArgs;
00439         
00440         LoadString( strFormat, nID );
00441         va_start( pArgs, nID );
00442         
00443         if ( strFormat.Find( _T("%1") ) >= 0 )
00444         {
00445                 LPTSTR lpszTemp;
00446                 if ( ::FormatMessage( FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
00447                         strFormat, 0, 0, (LPTSTR)&lpszTemp, 0, &pArgs ) != 0 && lpszTemp != NULL )
00448                 {
00449                         PrintMessage( nType, lpszTemp );
00450                         if ( Settings.General.DebugLog ) LogMessage( lpszTemp );
00451                         LocalFree( lpszTemp );
00452                 }
00453         }
00454         else
00455         {
00456                 _vsntprintf( szMessageBuffer, 16380, strFormat, pArgs );
00457                 PrintMessage( nType, szMessageBuffer );
00458                 if ( Settings.General.DebugLog ) LogMessage( szMessageBuffer );
00459         }
00460 
00461         va_end( pArgs );
00462 }
00463 
00464 void CShareazaApp::Message(int nType, LPCTSTR pszFormat, ...)
00465 {
00466         if ( nType == MSG_DEBUG && ! Settings.General.Debug ) return;
00467         if ( nType == MSG_TEMP && ! Settings.General.DebugLog ) return;
00468         
00469         CSingleLock pLock( &m_csMessage, TRUE );
00470         CString strFormat;
00471         va_list pArgs;
00472         
00473         va_start( pArgs, pszFormat );
00474         _vsntprintf( szMessageBuffer, 16380, pszFormat, pArgs );
00475         va_end( pArgs );
00476         
00477         PrintMessage( nType, szMessageBuffer );
00478         if ( Settings.General.DebugLog ) LogMessage( szMessageBuffer );
00479 }
00480 
00481 void CShareazaApp::PrintMessage(int nType, LPCTSTR pszLog)
00482 {
00483         if ( HWND hWnd = m_pSafeWnd->GetSafeHwnd() )
00484         {
00485                 PostMessage( hWnd, WM_LOG, nType, (LPARAM)_tcsdup( pszLog ) );
00486         }
00487 }
00488 
00489 void CShareazaApp::LogMessage(LPCTSTR pszLog)
00490 {
00491         CFile pFile;
00492         
00493         if ( pFile.Open( _T("\\Shareaza.log"), CFile::modeReadWrite ) )
00494         {
00495                 if ( ( Settings.General.MaxDebugLogSize ) &&                                    // If log rotation is on 
00496                         ( pFile.GetLength() > Settings.General.MaxDebugLogSize ) )      // and file is too long...
00497                 {       
00498                         // Close the file
00499                         pFile.Close();                          
00500                         // Rotate the logs 
00501                         DeleteFile(  _T("\\Shareaza.old.log") );
00502                         MoveFile( _T("\\Shareaza.log"), _T("\\Shareaza.old.log") );
00503                         // Start a new log
00504                         if ( ! pFile.Open( _T("\\Shareaza.log"), CFile::modeWrite|CFile::modeCreate ) ) return;
00505                         // Unicode marker
00506                         WORD nByteOrder = 0xFEFF;
00507                         pFile.Write( &nByteOrder, 2 );
00508                 }
00509                 else
00510                 {
00511                         pFile.Seek( 0, CFile::end ); // Otherwise, go to the end of the file to add entires.
00512                 }
00513         }
00514         else
00515         {
00516                 if ( ! pFile.Open( _T("\\Shareaza.log"), CFile::modeWrite|CFile::modeCreate ) ) return;
00517 
00518                 // Unicode marker
00519                 WORD nByteOrder = 0xFEFF;
00520                 pFile.Write( &nByteOrder, 2 );
00521         }
00522         
00523         if ( Settings.General.ShowTimestamp )
00524         {
00525                 CTime pNow = CTime::GetCurrentTime();
00526                 CString strLine;
00527                 
00528                 strLine.Format( _T("[%.2i:%.2i:%.2i] %s\r\n"),
00529                         pNow.GetHour(), pNow.GetMinute(), pNow.GetSecond(), pszLog );
00530                 
00531                 pFile.Write( (LPCTSTR)strLine, sizeof(TCHAR) * strLine.GetLength() );
00532         }
00533         else
00534         {
00535                 pFile.Write( pszLog, sizeof(TCHAR) * _tcslen(pszLog) );
00536                 pFile.Write( _T("\r\n"), sizeof(TCHAR) * 2 );
00537         }
00538         
00539         pFile.Close();
00540 }
00541 
00543 // CShareazaApp get error string
00544 
00545 CString CShareazaApp::GetErrorString()
00546 {
00547         LPTSTR pszMessage = NULL;
00548         CString strMessage;
00549         
00550         FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,
00551                 NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00552                 (LPTSTR)&pszMessage, 0, NULL );
00553         
00554         if ( pszMessage != NULL )
00555         {
00556                 strMessage = pszMessage;
00557                 LocalFree( pszMessage );
00558         }
00559         
00560         return strMessage;
00561 }
00562 
00564 // CShareazaApp process an internal URI
00565 
00566 BOOL CShareazaApp::InternalURI(LPCTSTR pszURI)
00567 {
00568         if ( m_pSafeWnd == NULL ) return FALSE;
00569         CMainWnd* pMainWnd = (CMainWnd*)m_pSafeWnd;
00570         
00571         CString strURI( pszURI );
00572         
00573         if ( strURI.Find( _T("raza:command:") ) == 0 )
00574         {
00575                 if ( UINT nCmdID = CoolInterface.NameToID( pszURI + 13 ) )
00576                 {
00577                         pMainWnd->PostMessage( WM_COMMAND, nCmdID );
00578                 }
00579         }
00580         else if ( strURI.Find( _T("raza:windowptr:") ) == 0 )
00581         {
00582                 CChildWnd* pChild = NULL;
00583                 _stscanf( (LPCTSTR)strURI + 15, _T("%lu"), &pChild );
00584                 if ( pMainWnd->m_pWindows.Check( pChild ) ) pChild->MDIActivate();
00585         }
00586         else if ( strURI.Find( _T("raza:launch:") ) == 0 )
00587         {
00588                 DWORD nIndex = 0;
00589                 _stscanf( (LPCTSTR)strURI + 12, _T("%lu"), &nIndex );
00590 
00591                 CSingleLock oLock( &Library.m_pSection, TRUE );
00592                 if ( CLibraryFile* pFile = Library.LookupFile( nIndex ) )
00593                 {
00594                         if ( pFile->m_pFolder )
00595                         {
00596                                 CString strPath = pFile->GetPath();
00597                                 oLock.Unlock();
00598                                 CFileExecutor::Execute( strPath, FALSE );
00599                         }
00600                 }
00601         }
00602         else if (       strURI.Find( _T("http://") ) == 0 ||
00603                                 strURI.Find( _T("ftp://") ) == 0 ||
00604                                 strURI.Find( _T("mailto:") ) == 0 ||
00605                                 strURI.Find( _T("aim:") ) == 0 ||
00606                                 strURI.Find( _T("magnet:") ) == 0 ||
00607                                 strURI.Find( _T("gnutella:") ) == 0 ||
00608                                 strURI.Find( _T("shareaza:") ) == 0 ||
00609                                 strURI.Find( _T("gnet:") ) == 0 ||
00610                                 strURI.Find( _T("ed2k:") ) == 0 ||
00611                                 strURI.Find( _T("sig2dat:") ) == 0 )
00612         {
00613                 ShellExecute( pMainWnd->GetSafeHwnd(), _T("open"), strURI,
00614                         NULL, NULL, SW_SHOWNORMAL );
00615         }
00616         else if ( strURI == _T("raza:connect") )
00617         {
00618                 pMainWnd->PostMessage( WM_COMMAND, ID_NETWORK_CONNECT );
00619         }
00620         else if ( strURI == _T("raza:disconnect") )
00621         {
00622                 pMainWnd->PostMessage( WM_COMMAND, ID_NETWORK_DISCONNECT );
00623         }
00624         else if ( strURI == _T("raza:search") )
00625         {
00626                 pMainWnd->PostMessage( WM_COMMAND, ID_TAB_SEARCH );
00627         }
00628         else if ( strURI == _T("raza:neighbours") )
00629         {
00630                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_NEIGHBOURS );
00631         }
00632         else if ( strURI == _T("raza:downloads") )
00633         {
00634                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_DOWNLOADS );
00635         }
00636         else if ( strURI == _T("raza:uploads") )
00637         {
00638                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_UPLOADS );
00639         }
00640         else if ( strURI == _T("raza:shell:downloads") )
00641         {
00642                 ShellExecute( pMainWnd->GetSafeHwnd(), _T("open"),
00643                         Settings.Downloads.CompletePath, NULL, NULL, SW_SHOWNORMAL );
00644         }
00645         else if ( strURI == _T("raza:upgrade") )
00646         {
00647                 pMainWnd->PostMessage( WM_VERSIONCHECK, 1 );
00648         }
00649         else if ( strURI == _T("raza:options") )
00650         {
00651                 pMainWnd->PostMessage( WM_COMMAND, ID_TOOLS_SETTINGS );
00652         }
00653         else if ( strURI == _T("raza:options:skins") )
00654         {
00655                 pMainWnd->PostMessage( WM_COMMAND, ID_TOOLS_SKIN );
00656         }
00657         else if ( strURI == _T("raza:wizard") )
00658         {
00659                 pMainWnd->PostMessage( WM_COMMAND, ID_TOOLS_WIZARD );
00660         }
00661         else if ( strURI == _T("raza:library") )
00662         {
00663                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_LIBRARY );
00664         }
00665         else if ( strURI == _T("raza:library:downloads") )
00666         {
00667                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_LIBRARY );
00668         }
00669         else if ( strURI == _T("raza:library:history") )
00670         {
00671                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_LIBRARY );
00672                 pMainWnd->PostMessage( WM_COMMAND, ID_LIBRARY_TREE_VIRTUAL );
00673         }
00674         else if ( strURI.Find( _T("raza:library:/") ) == 0 )
00675         {
00676                 pMainWnd->PostMessage( WM_COMMAND, ID_VIEW_LIBRARY );
00677                 pMainWnd->PostMessage( WM_COMMAND, ID_LIBRARY_TREE_VIRTUAL );
00678         }
00679         else
00680         {
00681                 return FALSE;
00682         }
00683 
00684         return TRUE;
00685 }
00686 
00688 // Runtime class lookup
00689 
00690 void AFXAPI AfxLockGlobals(int nLockType);
00691 void AFXAPI AfxUnlockGlobals(int nLockType);
00692 
00693 CRuntimeClass* AfxClassForName(LPCTSTR pszClass)
00694 {
00695         AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
00696 
00697         AfxLockGlobals( 0 );
00698 
00699         for ( CRuntimeClass* pClass = pModuleState->m_classList ; pClass != NULL ; pClass = pClass->m_pNextClass )
00700         {
00701                 if ( CString( pClass->m_lpszClassName ).CompareNoCase( pszClass ) == 0 )
00702                 {
00703                         AfxUnlockGlobals( 0 );
00704                         return pClass;
00705                 }
00706         }
00707 
00708         AfxUnlockGlobals( 0 );
00709 
00710         return NULL;
00711 }
00712 
00714 // String functions
00715 
00716 void Split(CString strSource, LPCTSTR pszDelimiter, CStringArray& pAddIt, BOOL bAddFirstEmpty)
00717 {
00718         CString         strNew = strSource;
00719         CString         strTemp = strSource;
00720         CString         strAdd;
00721         BOOL            bFirstChecked = FALSE;
00722 
00723         int nPos1;
00724         int nPos = 0;
00725 
00726         if ( ! _tcslen( pszDelimiter ) )
00727                 pszDelimiter = _T("|"); 
00728 
00729         do
00730         {
00731                 nPos1 = 0;
00732                 nPos = strNew.Find( pszDelimiter, nPos1 );
00733                 if ( nPos != -1 ) 
00734                 {
00735                         CString strAdd = strTemp = strNew.Left( nPos );
00736                         if ( ! strAdd.IsEmpty() && ! strTemp.Trim().IsEmpty() ) 
00737                         {
00738                                 pAddIt.Add( strAdd );
00739                         }
00740                         else if ( bAddFirstEmpty && ! bFirstChecked ) 
00741                         {
00742                                 pAddIt.Add( strAdd.Trim() );
00743                         }
00744                         strNew = strTemp = strNew.Mid( nPos + _tcslen( pszDelimiter ) );
00745                 }
00746                 bFirstChecked = TRUE; // Allow only the first item empty and ignore trailing empty items 
00747         } while ( nPos != -1 );
00748         
00749         if ( ! strTemp.Trim().IsEmpty() )
00750                 pAddIt.Add( strNew );
00751 }
00752 
00753 BOOL LoadString(CString& str, UINT nID)
00754 {
00755         return Skin.LoadString( str, nID );
00756 }
00757 
00758 void Replace(CString& strBuffer, LPCTSTR pszFind, LPCTSTR pszReplace)
00759 {
00760         while ( TRUE )
00761         {
00762                 int nPos = strBuffer.Find( pszFind );
00763                 if ( nPos < 0 ) break;
00764 
00765                 strBuffer = strBuffer.Left( nPos ) + pszReplace + strBuffer.Mid( nPos + _tcslen( pszFind ) );
00766         }
00767 }
00768 
00769 BOOL LoadSourcesString(CString& str, DWORD num)
00770 {
00771         if (num == 0)
00772         {
00773                 return Skin.LoadString( str, IDS_STATUS_NOSOURCES );
00774         }
00775         else if (num == 1)
00776         {
00777                 return Skin.LoadString( str, IDS_STATUS_SOURCE );
00778         }
00779         else if ( ( (num % 100) > 10) && ( (num % 100) < 20) )
00780         {
00781                 return Skin.LoadString( str, IDS_STATUS_SOURCES11TO19 );
00782         }
00783         else
00784         {
00785                 switch (num % 10)
00786                 {
00787                         case 0: 
00788                                 return Skin.LoadString( str, IDS_STATUS_SOURCESTENS );
00789                         case 1:
00790                                 return Skin.LoadString( str, IDS_STATUS_SOURCES );                              
00791                         case 2:
00792                         case 3:
00793                         case 4:
00794                                 return Skin.LoadString( str, IDS_STATUS_SOURCES2TO4 );
00795                         default:
00796                                 return Skin.LoadString( str, IDS_STATUS_SOURCES5TO9 );
00797                 }
00798         }
00799 }
00800 
00801 void ToLower(CString& strSource)
00802 {
00803         const int nLength = strSource.GetLength();
00804         const LPTSTR str = strSource.GetBuffer() + nLength;
00805         for ( int i = -nLength; i; ++i ) str[ i ] = ToLowerCase( str[ i ] );
00806         if ( str[ -1 ] == 0x3C3 ) str[ -1 ]--; // last greek sigma fix
00807         strSource.ReleaseBuffer( nLength );
00808 }
00809 
00811 // Case independent string search
00812 
00813 LPCTSTR _tcsistr(LPCTSTR pszString, LPCTSTR pszPattern)
00814 {
00815         if ( !*pszString || !*pszPattern ) return NULL;
00816 
00817         const TCHAR cFirstPatternChar = ToLowerCase[ *pszPattern ];
00818 
00819         for ( ; ; ++pszString )
00820         {
00821                 while ( *pszString && ToLowerCase[ *pszString ] != cFirstPatternChar ) ++pszString;
00822 
00823                 if ( !*pszString ) return NULL;
00824 
00825                 int i = 0;
00826                 while ( const TCHAR cPatternChar = ToLowerCase[ pszPattern[ ++i ] ] )
00827                 {
00828                         if ( const TCHAR cStringChar = ToLowerCase[ pszString[ i ] ] )
00829                         {
00830                                 if ( cStringChar != cPatternChar ) break;
00831                         }
00832                         else
00833                         {
00834                                 return NULL;
00835                         }
00836                 }
00837 
00838                 if ( !pszPattern[ i ] ) return pszString;
00839         }
00840 }
00841 
00842 LPCTSTR _tcsnistr(LPCTSTR pszString, LPCTSTR pszPattern, DWORD plen)
00843 {
00844         if ( !*pszString || !*pszPattern || !plen ) return NULL;
00845 
00846         const TCHAR cFirstPatternChar = ToLowerCase[ *pszPattern ];
00847 
00848         for ( ; ; ++pszString )
00849         {
00850                 while ( *pszString && ToLowerCase[ *pszString ] != cFirstPatternChar ) ++pszString;
00851 
00852                 if ( !*pszString ) return NULL;
00853 
00854                 DWORD i = 0;
00855                 while ( ++i < plen )
00856                 {
00857                         if ( const TCHAR cStringChar = ToLowerCase[ pszString[ i ] ] )
00858                         {
00859                                 if ( cStringChar != ToLowerCase[ pszPattern[ i ] ] ) break;
00860                         }
00861                         else
00862                         {
00863                                 return NULL;
00864                         }
00865                 }
00866 
00867                 if ( i == plen ) return pszString;
00868         }
00869 }
00870 
00872 // Time Management Functions (C-runtime)
00873 
00874 DWORD TimeFromString(LPCTSTR pszTime)
00875 {
00876         // 2002-04-30T08:30Z
00877         
00878         if ( _tcslen( pszTime ) != 17 ) return 0;
00879         if ( pszTime[4] != '-' || pszTime[7] != '-' ) return 0;
00880         if ( pszTime[10] != 'T' || pszTime[13] != ':' || pszTime[16] != 'Z' ) return 0;
00881         
00882         struct tm pTime;
00883         LPCTSTR psz;
00884         int nTemp;
00885         
00886         ZeroMemory( &pTime, sizeof(pTime) );
00887         
00888         if ( _stscanf( pszTime, _T("%i"), &nTemp ) != 1 ) return 0;
00889         pTime.tm_year = nTemp - 1900;
00890         for ( psz = pszTime + 5 ; *psz == '0' ; psz++ );
00891         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return 0;
00892         pTime.tm_mon = nTemp - 1;
00893         for ( psz = pszTime + 8 ; *psz == '0' ; psz++ );
00894         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return 0;
00895         pTime.tm_mday = nTemp;
00896         for ( psz = pszTime + 11 ; *psz == '0' ; psz++ );
00897         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return 0;
00898         pTime.tm_hour = nTemp;
00899         for ( psz = pszTime + 14 ; *psz == '0' ; psz++ );
00900         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return 0;
00901         pTime.tm_min = nTemp;
00902         
00903         time_t tGMT = mktime( &pTime );
00904         // check for invalid dates
00905         if (tGMT == -1) 
00906         {
00907                 theApp.Message( MSG_ERROR, _T("Invalid Date/Time"), pszTime );
00908                 return 0;
00909         }
00910         struct tm* pGM = gmtime( &tGMT );
00911         time_t tSub = mktime( pGM );
00912         
00913         if (tSub == -1) 
00914         {
00915                 theApp.Message( MSG_ERROR, _T("Invalid Date/Time"), pszTime );
00916                 return 0;
00917         }
00918         
00919         return tGMT + ( tGMT - tSub );
00920 }
00921 
00922 CString TimeToString(DWORD tVal)
00923 {
00924         struct tm* pTime = gmtime( (time_t*)&tVal );
00925         CString str;
00926 
00927         str.Format( _T("%.4i-%.2i-%.2iT%.2i:%.2iZ"),
00928                 pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday,
00929                 pTime->tm_hour, pTime->tm_min );
00930 
00931         return str;
00932 }
00933 
00935 // Time Management Functions (FILETIME)
00936 
00937 BOOL TimeFromString(LPCTSTR pszTime, FILETIME* pTime)
00938 {
00939         // 2002-04-30T08:30Z
00940         
00941         if ( _tcslen( pszTime ) != 17 ) return FALSE;
00942         if ( pszTime[4] != '-' || pszTime[7] != '-' ) return FALSE;
00943         if ( pszTime[10] != 'T' || pszTime[13] != ':' || pszTime[16] != 'Z' ) return FALSE;
00944         
00945         SYSTEMTIME pOut;
00946         LPCTSTR psz;
00947         int nTemp;
00948 
00949         ZeroMemory( &pOut, sizeof(pOut) );
00950 
00951         if ( _stscanf( pszTime, _T("%i"), &nTemp ) != 1 ) return FALSE;
00952         pOut.wYear = nTemp;
00953         for ( psz = pszTime + 5 ; *psz == '0' ; psz++ );
00954         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return FALSE;
00955         pOut.wMonth = nTemp;
00956         for ( psz = pszTime + 8 ; *psz == '0' ; psz++ );
00957         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return FALSE;
00958         pOut.wDay = nTemp;
00959         for ( psz = pszTime + 11 ; *psz == '0' ; psz++ );
00960         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return FALSE;
00961         pOut.wHour = nTemp;
00962         for ( psz = pszTime + 14 ; *psz == '0' ; psz++ );
00963         if ( _stscanf( psz, _T("%i"), &nTemp ) != 1 ) return FALSE;
00964         pOut.wMinute = nTemp;
00965 
00966         return SystemTimeToFileTime( &pOut, pTime );
00967 }
00968 
00969 CString TimeToString(FILETIME* pTime)
00970 {
00971         SYSTEMTIME pOut;
00972         CString str;
00973 
00974         FileTimeToSystemTime( pTime, &pOut );
00975 
00976         str.Format( _T("%.4i-%.2i-%.2iT%.2i:%.2iZ"),
00977                 pOut.wYear, pOut.wMonth, pOut.wDay,
00978                 pOut.wHour, pOut.wMinute );
00979 
00980         return str;
00981 }
00982 
00984 // Automatic dropdown list width adjustment (to fit translations)
00985 // Use in ON_CBN_DROPDOWN events
00986 
00987 void RecalcDropWidth(CComboBox* pWnd)
00988 {
00989     // Reset the dropped width
00990     int nNumEntries = pWnd->GetCount();
00991     int nWidth = 0;
00992     CString str;
00993 
00994     CClientDC dc( pWnd );
00995     int nSave = dc.SaveDC();
00996     dc.SelectObject( pWnd->GetFont() );
00997 
00998     int nScrollWidth = GetSystemMetrics( SM_CXVSCROLL );
00999     for ( int nEntry = 0; nEntry < nNumEntries; nEntry++ )
01000     {
01001         pWnd->GetLBText( nEntry, str );
01002         int nLength = dc.GetTextExtent( str ).cx + nScrollWidth;
01003         nWidth = max( nWidth, nLength );
01004     }
01005     
01006     // Add margin space to the calculations
01007     nWidth += dc.GetTextExtent( _T("0") ).cx;
01008 
01009     dc.RestoreDC( nSave );
01010     pWnd->SetDroppedWidth( nWidth );
01011 }
01012 
01013 HICON CreateMirroredIcon(HICON hIconOrig)
01014 {
01015         HDC hdcScreen, hdcBitmap, hdcMask = NULL;
01016         HBITMAP hbm, hbmMask, hbmOld,hbmOldMask;
01017         BITMAP bm;
01018         ICONINFO ii;
01019         HICON hIcon = NULL;
01020         hdcBitmap = CreateCompatibleDC( NULL );
01021         if ( hdcBitmap )
01022         {
01023                 hdcMask = CreateCompatibleDC( NULL );
01024                 if( hdcMask )
01025                 {
01026                         theApp.m_pfnSetLayout( hdcBitmap, LAYOUT_RTL );
01027                         theApp.m_pfnSetLayout( hdcMask, LAYOUT_RTL );
01028                 }
01029                 else
01030                 {
01031                         DeleteDC( hdcBitmap );
01032                         hdcBitmap = NULL;
01033                 }
01034         }
01035         hdcScreen = GetDC( NULL );
01036         if ( hdcScreen )
01037         {
01038                 if ( hdcBitmap && hdcMask )
01039                 {
01040                         if ( hIconOrig )
01041                         {
01042                                 if ( GetIconInfo( hIconOrig, &ii ) && GetObject( ii.hbmColor, sizeof(BITMAP), &bm ) )
01043                                 {
01044                                         // Do the cleanup for the bitmaps.
01045                                         DeleteObject( ii.hbmMask );
01046                                         DeleteObject( ii.hbmColor );
01047                                         ii.hbmMask = ii.hbmColor = NULL;
01048                                         hbm = CreateCompatibleBitmap( hdcScreen, bm.bmWidth, bm.bmHeight );
01049                                         hbmMask = CreateBitmap( bm.bmWidth, bm.bmHeight, 1, 1, NULL );
01050                                         hbmOld = (HBITMAP)SelectObject( hdcBitmap, hbm );
01051                                         hbmOldMask = (HBITMAP)SelectObject( hdcMask,hbmMask );
01052                                         DrawIconEx( hdcBitmap, 0, 0, hIconOrig, bm.bmWidth, bm.bmHeight, 0, NULL, DI_IMAGE );
01053                                         DrawIconEx( hdcMask, 0, 0, hIconOrig, bm.bmWidth, bm.bmHeight, 0, NULL, DI_MASK );
01054                                         SelectObject( hdcBitmap, hbmOld );
01055                                         SelectObject( hdcMask, hbmOldMask );
01056                                         // Create the new mirrored icon and delete bitmaps
01057 
01058                                         ii.hbmMask = hbmMask;
01059                                         ii.hbmColor = hbm;
01060                                         hIcon = CreateIconIndirect( &ii );
01061                                         DeleteObject( hbm );
01062                                         DeleteObject( hbmMask );
01063                                 }
01064                         }
01065                 }
01066         }
01067         ReleaseDC( NULL, hdcScreen );
01068 
01069         if ( hdcBitmap ) DeleteDC( hdcBitmap );
01070         if ( hdcMask ) DeleteDC( hdcMask );
01071         return hIcon;
01072 }
01073 
01074 HBITMAP CreateMirroredBitmap(HBITMAP hbmOrig)
01075 {
01076         HDC hdc, hdcMem1, hdcMem2;
01077         HBITMAP hbm = NULL, hOld_bm1, hOld_bm2;
01078         BITMAP bm;
01079         if ( !hbmOrig ) return NULL;
01080         if ( !GetObject( hbmOrig, sizeof(BITMAP), &bm ) ) return NULL;
01081 
01082         hdc = GetDC( NULL );
01083         if ( hdc )
01084         {
01085                 hdcMem1 = CreateCompatibleDC( hdc );
01086                 if ( !hdcMem1 )
01087                 {
01088                         ReleaseDC( NULL, hdc );
01089                         return NULL;
01090                 }
01091                 hdcMem2 = CreateCompatibleDC( hdc );
01092                 if ( !hdcMem2 )
01093                 {
01094                         DeleteDC( hdcMem1 );
01095                         ReleaseDC( NULL, hdc );
01096                         return NULL;
01097                 }
01098                 hbm = CreateCompatibleBitmap( hdc, bm.bmWidth, bm.bmHeight );
01099                 if (!hbm)
01100                 {
01101                         ReleaseDC( NULL, hdc );
01102                         DeleteDC( hdcMem1 );
01103                         DeleteDC( hdcMem2 );
01104                         return NULL;
01105                 }
01106                 // Flip the bitmap.
01107                 hOld_bm1 = (HBITMAP)SelectObject( hdcMem1, hbmOrig );
01108                 hOld_bm2 = (HBITMAP)SelectObject( hdcMem2, hbm );
01109                 theApp.m_pfnSetLayout( hdcMem2, LAYOUT_RTL );
01110                 BitBlt( hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem1, 0, 0, SRCCOPY );
01111                 SelectObject( hdcMem1, hOld_bm1 );
01112                 SelectObject( hdcMem1, hOld_bm2 );
01113                 DeleteDC( hdcMem1 );
01114                 DeleteDC( hdcMem2 );
01115                 ReleaseDC( NULL, hdc );
01116         }
01117         return hbm;
01118 }

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