📄 shareaza.cpp
字号:
//
// Shareaza.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "CoolInterface.h"
#include "Network.h"
#include "Security.h"
#include "HostCache.h"
#include "DiscoveryServices.h"
#include "VersionChecker.h"
#include "SchemaCache.h"
#include "VendorCache.h"
#include "EDClients.h"
#include "BTClients.h"
#include "Library.h"
#include "Transfers.h"
#include "DownloadGroups.h"
#include "Downloads.h"
#include "Uploads.h"
#include "UploadQueues.h"
#include "QueryHashMaster.h"
#include "DDEServer.h"
#include "IEProtocol.h"
#include "ShareazaURL.h"
#include "GProfile.h"
#include "SharedFile.h"
#include "Emoticons.h"
#include "ShellIcons.h"
#include "Skin.h"
#include "WndMain.h"
#include "WndSystem.h"
#include "DlgSplash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CShareazaApp, CWinApp)
//{{AFX_MSG_MAP(CShareazaApp)
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
CShareazaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp construction
CShareazaApp::CShareazaApp() : m_pMutex( FALSE, _T("Shareaza") )
{
m_pSafeWnd = NULL;
m_bLive = FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp initialization
BOOL CShareazaApp::InitInstance()
{
CWaitCursor pCursor;
if ( ! m_pMutex.Lock( 0 ) )
{
if ( CWnd* pWnd = CWnd::FindWindow( _T("ShareazaMainWnd"), NULL ) )
{
pWnd->SendMessage( WM_SYSCOMMAND, SC_RESTORE );
pWnd->ShowWindow( SW_SHOWNORMAL );
pWnd->BringWindowToTop();
pWnd->SetForegroundWindow();
}
return FALSE;
}
// Enable3dControls();
SetRegistryKey( _T("Shareaza") );
GetVersionNumber();
InitResources();
BOOL bSilentTray = ( m_lpCmdLine && _tcsistr( m_lpCmdLine, _T("-tray") ) != NULL );
AfxOleInit();
AfxEnableControlContainer();
CSplashDlg* dlgSplash = new CSplashDlg( 17, bSilentTray );
dlgSplash->Step( _T("Winsock") );
WSADATA wsaData;
if ( WSAStartup( 0x0101, &wsaData ) ) return FALSE;
dlgSplash->Step( _T("Settings Database") );
Settings.Load();
dlgSplash->Step( _T("P2P URIs") );
CShareazaURL::Register();
dlgSplash->Step( _T("Shell Icons") );
ShellIcons.Clear();
dlgSplash->Step( _T("Metadata Schemas") );
SchemaCache.Load();
dlgSplash->Step( _T("Vendor Data") );
VendorCache.Load();
dlgSplash->Step( _T("Profile") );
MyProfile.Load();
dlgSplash->Step( _T("Library") );
Library.Load();
dlgSplash->Step( _T("Query Manager") );
QueryHashMaster.Create();
dlgSplash->Step( _T("Host Cache") );
HostCache.Load();
dlgSplash->Step( _T("Discovery Services") );
DiscoveryServices.Load();
dlgSplash->Step( _T("Security Services") );
Security.Load();
dlgSplash->Step( _T("Download Manager") );
Downloads.Load();
dlgSplash->Step( _T("Upload Manager") );
UploadQueues.Load();
dlgSplash->Step( _T("Rich Documents") );
Emoticons.Load();
dlgSplash->Step( _T("GUI") );
if ( bSilentTray ) WriteProfileInt( _T("Windows"), _T("CMainWnd.ShowCmd"), 0 );
m_pMainWnd = new CMainWnd();
CoolMenu.EnableHook();
if ( bSilentTray )
{
((CMainWnd*)m_pMainWnd)->CloseToTray();
}
else
{
dlgSplash->Topmost();
m_pMainWnd->ShowWindow( SW_SHOW );
m_pMainWnd->UpdateWindow();
}
dlgSplash->Step( _T("IPC") );
DDEServer.Create();
IEProtocol.Create();
dlgSplash->Step( _T("Upgrade Manager") );
if ( VersionChecker.NeedToCheck() ) VersionChecker.Start( m_pMainWnd->GetSafeHwnd() );
pCursor.Restore();
dlgSplash->Hide();
m_bLive = TRUE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp termination
int CShareazaApp::ExitInstance()
{
CWaitCursor pCursor;
DDEServer.Close();
IEProtocol.Close();
VersionChecker.Stop();
DiscoveryServices.Stop();
Network.Disconnect();
Library.StopThread();
Transfers.StopThread();
Downloads.CloseTransfers();
Uploads.Clear( FALSE );
EDClients.Clear();
BTClients.Clear();
if ( m_bLive )
{
Downloads.Save();
DownloadGroups.Save();
Library.Save();
Security.Save();
HostCache.Save();
UploadQueues.Save();
DiscoveryServices.Save();
}
Downloads.Clear( TRUE );
Library.Clear();
Skin.Clear();
if ( m_bLive ) Settings.Save( TRUE );
WSACleanup();
return CWinApp::ExitInstance();
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp help (suppress F1)
void CShareazaApp::WinHelp(DWORD dwData, UINT nCmd)
{
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp version
void CShareazaApp::GetVersionNumber()
{
TCHAR szPath[128];
DWORD dwSize;
m_nVersion[0] = m_nVersion[1] = m_nVersion[2] = m_nVersion[3] = 0;
GetModuleFileName( NULL, szPath, 128 );
dwSize = GetFileVersionInfoSize( szPath, &dwSize );
if ( dwSize )
{
BYTE* pBuffer = new BYTE[ dwSize ];
if ( GetFileVersionInfo( szPath, NULL, dwSize, pBuffer ) )
{
VS_FIXEDFILEINFO* pTable;
if ( VerQueryValue( pBuffer, _T("\\"), (VOID**)&pTable, (UINT*)&dwSize ) )
{
m_nVersion[0] = (WORD)( pTable->dwFileVersionMS >> 16 );
m_nVersion[1] = (WORD)( pTable->dwFileVersionMS & 0xFFFF );
m_nVersion[2] = (WORD)( pTable->dwFileVersionLS >> 16 );
m_nVersion[3] = (WORD)( pTable->dwFileVersionLS & 0xFFFF );
}
}
delete [] pBuffer;
}
m_sVersion.Format( _T("%i.%i.%i.%i"),
m_nVersion[0], m_nVersion[1],
m_nVersion[2], m_nVersion[3] );
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp resources
void CShareazaApp::InitResources()
{
OSVERSIONINFO pVersion;
pVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx( &pVersion );
m_bNT = ( pVersion.dwPlatformId == VER_PLATFORM_WIN32_NT );
m_gdiFont.CreateFont( -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE, _T("Tahoma") );
m_gdiFontBold.CreateFont( -11, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE, _T("Tahoma") );
m_gdiFontLine.CreateFont( -11, 0, 0, 0, FW_NORMAL, FALSE, TRUE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE, _T("Tahoma") );
srand( GetTickCount() );
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp safe main window
CMainWnd* CShareazaApp::SafeMainWnd()
{
CMainWnd* pMainWnd = (CMainWnd*)theApp.m_pSafeWnd;
if ( pMainWnd == NULL ) return NULL;
ASSERT_KINDOF( CMainWnd, pMainWnd );
return IsWindow( pMainWnd->m_hWnd ) ? pMainWnd : NULL;
}
/////////////////////////////////////////////////////////////////////////////
// CShareazaApp message
TCHAR CShareazaApp::szMessageBuffer[16384];
void CShareazaApp::Message(int nType, UINT nID, ...)
{
if ( nType == MSG_DEBUG && ! Settings.General.Debug ) return;
if ( nType == MSG_TEMP && ! Settings.General.DebugLog ) return;
CSingleLock pLock( &m_csMessage, TRUE );
CString strFormat;
va_list pArgs;
LoadString( strFormat, nID );
va_start( pArgs, nID );
if ( strFormat.Find( _T("%1") ) >= 0 )
{
LPTSTR lpszTemp;
if ( ::FormatMessage( FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
strFormat, 0, 0, (LPTSTR)&lpszTemp, 0, &pArgs ) != 0 && lpszTemp != NULL )
{
PrintMessage( nType, lpszTemp );
if ( Settings.General.DebugLog ) LogMessage( lpszTemp );
LocalFree( lpszTemp );
}
}
else
{
_vsntprintf( szMessageBuffer, 16380, strFormat, pArgs );
PrintMessage( nType, szMessageBuffer );
if ( Settings.General.DebugLog ) LogMessage( szMessageBuffer );
}
va_end( pArgs );
}
void CShareazaApp::Message(int nType, LPCTSTR pszFormat, ...)
{
if ( nType == MSG_DEBUG && ! Settings.General.Debug ) return;
if ( nType == MSG_TEMP && ! Settings.General.DebugLog ) return;
CSingleLock pLock( &m_csMessage, TRUE );
CString strFormat;
va_list pArgs;
va_start( pArgs, pszFormat );
_vsntprintf( szMessageBuffer, 16380, pszFormat, pArgs );
va_end( pArgs );
PrintMessage( nType, szMessageBuffer );
if ( Settings.General.DebugLog ) LogMessage( szMessageBuffer );
}
void CShareazaApp::PrintMessage(int nType, LPCTSTR pszLog)
{
if ( HWND hWnd = m_pSafeWnd->GetSafeHwnd() )
{
PostMessage( hWnd, WM_LOG, nType, (LPARAM)_tcsdup( pszLog ) );
}
}
void CShareazaApp::LogMessage(LPCTSTR pszLog)
{
CFile pFile;
if ( pFile.Open( _T("\\Shareaza.log"), CFile::modeReadWrite ) )
{
pFile.Seek( 0, CFile::end );
}
else
{
if ( ! pFile.Open( _T("\\Shareaza.log"), CFile::modeWrite|CFile::modeCreate ) )
return;
#ifdef _UNICODE
WORD nByteOrder = 0xFEFF;
pFile.Write( &nByteOrder, 2 );
#endif
}
if ( Settings.General.ShowTimestamp )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -