00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "VersionChecker.h"
00026 #include "Transfer.h"
00027 #include "Network.h"
00028 #include "GProfile.h"
00029 #include "DiscoveryServices.h"
00030 #include "SHA.h"
00031
00032 #ifdef _DEBUG
00033 #undef THIS_FILE
00034 static char THIS_FILE[]=__FILE__;
00035 #define new DEBUG_NEW
00036 #endif
00037
00038 CVersionChecker VersionChecker;
00039
00040 #define VERSIONCHECKER_FREQUENCY 7
00041
00042
00044
00045
00046 CVersionChecker::CVersionChecker()
00047 {
00048 m_bUpgrade = FALSE;
00049 m_hThread = NULL;
00050 m_hWndNotify = NULL;
00051 }
00052
00053 CVersionChecker::~CVersionChecker()
00054 {
00055 Stop();
00056 }
00057
00059
00060
00061 BOOL CVersionChecker::NeedToCheck()
00062 {
00063 m_sUpgradeVersion = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradeVersion"), _T("") );
00064 DWORD nNextCheck = theApp.GetProfileInt( _T("VersionCheck"), _T("NextCheck"), 0 );
00065
00066 if ( theApp.m_sVersion.Compare( m_sUpgradeVersion ) >= 0 )
00067 {
00068 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradePrompt"), NULL );
00069 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeFile"), NULL );
00070 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeSHA1"), NULL );
00071 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeTiger"), NULL);
00072 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeSize"), NULL );
00073 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeSources"), NULL );
00074 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeVersion"), NULL );
00075 return (DWORD)CTime::GetCurrentTime().GetTime() >= nNextCheck;
00076 }
00077
00078 if ( !Settings.General.UpdateCheck ) return FALSE;
00079
00080 m_sQuote = theApp.GetProfileString( _T("VersionCheck"), _T("Quote"), _T("") );
00081
00082 m_sUpgradePrompt = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradePrompt"), _T("") );
00083 m_sUpgradeFile = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradeFile"), _T("") );
00084 m_sUpgradeSHA1 = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradeSHA1"), _T("") );
00085 m_sUpgradeTiger = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradeTiger"), _T("") );
00086 m_sUpgradeSize = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradeSize"), _T("") );
00087 m_sUpgradeSources = theApp.GetProfileString( _T("VersionCheck"), _T("UpgradeSources"), _T("") );
00088 m_bUpgrade = ( m_sUpgradePrompt.GetLength() > 0 );
00089
00090 if ( ! nNextCheck ) return TRUE;
00091 if ( nNextCheck == 0xFFFFFFFF ) return FALSE;
00092
00093 return (DWORD)CTime::GetCurrentTime().GetTime() >= nNextCheck;
00094 }
00095
00097
00098
00099 BOOL CVersionChecker::Start(HWND hWndNotify)
00100 {
00101
00102
00103 Stop();
00104
00105 m_pRequest.Clear();
00106
00107 m_hWndNotify = hWndNotify;
00108 m_bUpgrade = FALSE;
00109
00110 CWinThread* pThread = AfxBeginThread( ThreadStart, this, THREAD_PRIORITY_IDLE );
00111 m_hThread = pThread->m_hThread;
00112
00113 return TRUE;
00114 }
00115
00117
00118
00119 void CVersionChecker::Stop()
00120 {
00121 m_pRequest.Cancel();
00122 CHttpRequest::CloseThread( &m_hThread, _T("CVersionChecker") );
00123 }
00124
00126
00127
00128 UINT CVersionChecker::ThreadStart(LPVOID pParam)
00129 {
00130 CVersionChecker* pClass = (CVersionChecker*)pParam;
00131 pClass->OnRun();
00132 return 0;
00133 }
00134
00136
00137
00138 void CVersionChecker::OnRun()
00139 {
00140 CString strRequest;
00141
00142 BuildRequest( strRequest );
00143
00144 BOOL bRequest = UndertakeRequest( strRequest );
00145
00146 if ( bRequest )
00147 {
00148 ProcessResponse();
00149 m_pResponse.RemoveAll();
00150 }
00151 else
00152 {
00153 SetNextCheck( VERSIONCHECKER_FREQUENCY );
00154 }
00155 }
00156
00158
00159
00160 void CVersionChecker::BuildRequest(CString& strRequest)
00161 {
00162 strRequest += _T("Version=");
00163 strRequest += theApp.m_sVersion;
00164 }
00165
00167
00168
00169 BOOL CVersionChecker::UndertakeRequest(CString& strPost)
00170 {
00171 m_pRequest.SetURL( _T("http://update.trillinux.org/version/beta.php?") + strPost );
00172
00173
00174 if ( ! m_pRequest.Execute( FALSE ) ) return FALSE;
00175
00176 int nStatusCode = m_pRequest.GetStatusCode();
00177 if ( nStatusCode < 200 || nStatusCode > 299 ) return FALSE;
00178
00179 CString strResponse = m_pRequest.GetResponseString();
00180 CString strHack = theApp.GetProfileString( _T("VersionCheck"), _T("TestResponse"), _T("") );
00181 if ( strHack.GetLength() ) strResponse = strHack;
00182
00183 for ( strResponse += '&' ; strResponse.GetLength() ; )
00184 {
00185 CString strItem = strResponse.SpanExcluding( _T("&") );
00186 strResponse = strResponse.Mid( strItem.GetLength() + 1 );
00187
00188 CString strKey = strItem.SpanExcluding( _T("=") );
00189 if ( strKey.GetLength() == strItem.GetLength() ) continue;
00190 strItem = CTransfer::URLDecode( strItem.Mid( strKey.GetLength() + 1 ) );
00191
00192 strItem.TrimLeft();
00193 strItem.TrimRight();
00194
00195 m_pResponse.SetAt( strKey, strItem );
00196 }
00197
00198 return TRUE;
00199 }
00200
00202
00203
00204 void CVersionChecker::ProcessResponse()
00205 {
00206 int nDays = VERSIONCHECKER_FREQUENCY;
00207 CString strValue;
00208
00209 if ( m_pResponse.Lookup( _T("Message"), strValue ) || m_pResponse.Lookup( _T("MessageBox"), strValue ) )
00210 {
00211 m_sMessage = strValue;
00212 }
00213
00214 if ( m_pResponse.Lookup( _T("Quote"), strValue ) )
00215 {
00216 m_sQuote = strValue;
00217 theApp.WriteProfileString( _T("VersionCheck"), _T("Quote"), m_sQuote );
00218 }
00219
00220 if ( m_pResponse.Lookup( _T("SystemMsg"), strValue ) )
00221 {
00222 for ( strValue += '\n' ; strValue.GetLength() ; )
00223 {
00224 CString strLine = strValue.SpanExcluding( _T("\r\n") );
00225 strValue = strValue.Mid( strLine.GetLength() + 1 );
00226 if ( strLine.GetLength() ) theApp.Message( MSG_SYSTEM, strLine );
00227 }
00228 }
00229
00230 if ( m_pResponse.Lookup( _T("UpgradePrompt"), strValue ) )
00231 {
00232 m_sUpgradePrompt = strValue;
00233
00234 m_pResponse.Lookup( _T("UpgradeFile"), m_sUpgradeFile );
00235 m_pResponse.Lookup( _T("UpgradeSHA1"), m_sUpgradeSHA1 );
00236 m_pResponse.Lookup( _T("UpgradeTiger"), m_sUpgradeTiger );
00237 m_pResponse.Lookup( _T("UpgradeSize"), m_sUpgradeSize );
00238 m_pResponse.Lookup( _T("UpgradeSources"), m_sUpgradeSources );
00239 m_pResponse.Lookup( _T("UpgradeVersion"), m_sUpgradeVersion );
00240
00241
00242 if ( ! m_sUpgradeSHA1.GetLength() )
00243 m_pResponse.Lookup( _T("UpgradeHash"), m_sUpgradeSHA1 );
00244
00245 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradePrompt"), m_sUpgradePrompt );
00246 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeFile"), m_sUpgradeFile );
00247 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeSHA1"), m_sUpgradeSHA1 );
00248 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeTiger"), m_sUpgradeTiger);
00249 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeSize"), m_sUpgradeSize );
00250 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeSources"), m_sUpgradeSources );
00251 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradeVersion"), m_sUpgradeVersion );
00252
00253 m_bUpgrade = TRUE;
00254 }
00255 else
00256 {
00257 theApp.WriteProfileString( _T("VersionCheck"), _T("UpgradePrompt"), _T("") );
00258 m_bUpgrade = FALSE;
00259 }
00260
00261 if ( m_pResponse.Lookup( _T("AddDiscovery"), strValue ) )
00262 {
00263 strValue.TrimLeft();
00264 strValue.TrimRight();
00265 DiscoveryServices.Add( strValue, CDiscoveryService::dsWebCache );
00266 }
00267
00268 if ( m_pResponse.Lookup( _T("NextCheck"), strValue ) )
00269 {
00270 _stscanf( strValue, _T("%lu"), &nDays );
00271 }
00272
00273 SetNextCheck( nDays );
00274
00275 PostMessage( m_hWndNotify, WM_VERSIONCHECK, 0, 0 );
00276 }
00277
00279
00280
00281 void CVersionChecker::SetNextCheck(int nDays)
00282 {
00283 CTimeSpan tPeriod( nDays, 0, 0, 0 );
00284 CTime tNextCheck = CTime::GetCurrentTime() + tPeriod;
00285 theApp.WriteProfileInt( _T("VersionCheck"), _T("NextCheck"), (DWORD)tNextCheck.GetTime() );
00286 }
00287
00289
00290
00291 BOOL CVersionChecker::CheckUpgradeHash(const SHA1* pHash, LPCTSTR pszPath)
00292 {
00293 if ( ! m_bUpgrade ) return FALSE;
00294
00295 if ( CSHA::HashToString( pHash ) != m_sUpgradeSHA1 ) return FALSE;
00296
00297 if ( _tcsstr( pszPath, _T(".exe") ) == NULL ) return FALSE;
00298
00299 m_sUpgradePath = pszPath;
00300
00301 AfxGetMainWnd()->PostMessage( WM_VERSIONCHECK, 2 );
00302
00303 return TRUE;
00304 }