00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "fsUpdateMgr.h"
00009 #include "MainFrm.h"
00010 #include "mfchelp.h"
00011
00012 #ifdef _DEBUG
00013 #undef THIS_FILE
00014 static char THIS_FILE[]=__FILE__;
00015 #define new DEBUG_NEW
00016 #endif
00017
00018 fsUpdateMgr::fsUpdateMgr()
00019 {
00020 m_bRunning = FALSE;
00021 m_pfnEvents = NULL;
00022 m_pfnDescEvents= NULL;
00023 }
00024
00025 fsUpdateMgr::~fsUpdateMgr()
00026 {
00027 SAFE_DELETE (m_dldr);
00028 }
00029
00030 void fsUpdateMgr::ReadSettings()
00031 {
00032 m_enAUT = _App.Update_AutoUpdateType ();
00033 m_strDlFullInstallPath = m_strDlUpgradesPath = m_strUpdateUrl = _App.Update_URL ();
00034 }
00035
00036 void fsUpdateMgr::SaveSettings()
00037 {
00038 _App.Update_AutoUpdateType (m_enAUT);
00039 if (m_strUpdateUrl.GetLength ())
00040 _App.Update_URL (m_strUpdateUrl);
00041 }
00042
00043 BOOL fsUpdateMgr::StartUpdater()
00044 {
00045 STARTUPINFO si;
00046 PROCESS_INFORMATION pi;
00047
00048 ZeroMemory (&si, sizeof (si));
00049 si.cb = sizeof (si);
00050 ZeroMemory (&pi, sizeof (pi));
00051
00052 CString strCmdLine;
00053 strCmdLine.Format ("\"%s\" \"%s\" \"%s\" \"%s\" \"/silent\" \"0\"",
00054 vmsGetAppFolder () + "updater.exe",
00055 ((CFdmApp*)AfxGetApp ())->m_strAppPath + "fdm.exe",
00056 _pszAppMutex,
00057 m_strUpdateFile);
00058
00059 if (FALSE == CreateProcess (NULL, (LPSTR)(LPCSTR)strCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
00060 return FALSE;
00061
00062 return TRUE;
00063 }
00064
00065 void fsUpdateMgr::CheckForUpdate(bool bByUser)
00066 {
00067 if (m_bRunning || IS_PORTABLE_MODE)
00068 return;
00069
00070 m_bCheckingByUser = bByUser;
00071
00072 SAFE_DELETE (m_dldr);
00073 fsnew1 (m_dldr, fsDownloadMgr (NULL));
00074 m_dldr->SetEventFunc (_DownloadMgrEvents, this);
00075 m_dldr->SetEventDescFunc (_DownloadMgrDescEvents, this);
00076
00077 m_bRunning = TRUE;
00078 m_bChecking = TRUE;
00079
00080 CMainFrame* pFrm = (CMainFrame*) AfxGetApp ()->m_pMainWnd;
00081
00082 CString strUrl = m_strUpdateUrl;
00083
00084 strUrl += "proupd.lst";
00085
00086 LPCSTR pszCustomizer = pFrm->get_Customizations ()->get_Customizer ();
00087 if (pszCustomizer != NULL && lstrlen (pszCustomizer) != 0)
00088 {
00089 strUrl += "?edition=";
00090 strUrl += pszCustomizer;
00091 }
00092
00093 m_dldr->CreateByUrl (strUrl);
00094
00095 fsDownload_Properties *dp = m_dldr->GetDP ();
00096 fsDownload_NetworkProperties *dnp = m_dldr->GetDNP ();
00097
00098 SAFE_DELETE_ARRAY (dp->pszFileName);
00099 CString strFile = fsGetDataFilePath ("Update\\");
00100 fsnew (dp->pszFileName, char, strFile.GetLength () + 1);
00101 strcpy (dp->pszFileName, strFile);
00102 dp->uMaxSections = 1;
00103 dp->uMaxAttempts = 1;
00104 dp->dwFlags |= DPF_DONTRESTARTIFNORESUME;
00105 dp->enSCR = SCR_STOP;
00106 dp->enAER = AER_REWRITE;
00107 *dp->pszAdditionalExt = 0;
00108
00109 SAFE_DELETE_ARRAY (dnp->pszReferer);
00110
00111 fsnew (dnp->pszReferer, char, 100);
00112 sprintf (dnp->pszReferer, "FDM - Build number = %d", PRG_BUILD_NUMBER);
00113
00114 Event (UME_CONNECTING);
00115
00116 m_dldr->StartDownloading ();
00117 }
00118
00119 DWORD fsUpdateMgr::_DownloadMgrEvents(fsDownloadMgr* pMgr, fsDownloaderEvent ev, UINT uInfo, LPVOID lp)
00120 {
00121 fsUpdateMgr *pThis = (fsUpdateMgr*) lp;
00122
00123 switch (ev)
00124 {
00125 case DE_SECTIONSTARTED:
00126 if (pMgr->GetDownloader ()->GetRunningSectionCount ())
00127 {
00128 if (pThis->m_bChecking)
00129 pThis->Event (UME_RETREIVINGUPDLST);
00130 else
00131 pThis->Event (UME_RETREIVINGUPDATE);
00132 }
00133 break;
00134
00135 case DE_EXTERROR:
00136 if (uInfo == DMEE_STOPPEDORDONE)
00137 {
00138 if (pMgr->IsDone ())
00139 {
00140 if (pThis->m_bChecking)
00141 {
00142 SYSTEMTIME time;
00143 GetLocalTime (&time);
00144 _App.NewVerExists (TRUE);
00145 _App.Update_LastCheck (time);
00146
00147 pThis->Event (UME_UPDLSTDONE);
00148 pThis->m_bChecking = FALSE;
00149 pThis->ProcessUpdateLstFile ();
00150 }
00151 else
00152 {
00153 pThis->m_strUpdateFile = pThis->m_dldr->GetDP ()->pszFileName;
00154 pThis->Event (UME_UPDATEDONE);
00155 pThis->m_bRunning = FALSE;
00156 }
00157 }
00158 else
00159 {
00160 pThis->Event (UME_FATALERROR);
00161 pThis->m_bRunning = FALSE;
00162 }
00163 }
00164 else
00165 {
00166 pThis->Event (UME_ERROR);
00167 }
00168 break;
00169
00170 case DE_ERRFROMSERVER:
00171 case DE_ERROROCCURED:
00172 case DE_FAILCONNECT:
00173 case DE_WRITEERROR:
00174 pThis->Event (UME_ERROR);
00175 break;
00176 }
00177
00178 return TRUE;
00179 }
00180
00181 void fsUpdateMgr::ProcessUpdateLstFile()
00182 {
00183 char szSections [10000];
00184 *szSections = 0;
00185 char szValues [10000];
00186
00187 if (::GetVersion () & 0x80000000)
00188 FixIniFileFor9x (fsGetDataFilePath ("Update\\proupd.lst"));
00189
00190
00191
00192 if (0 == GetPrivateProfileSectionNames (szSections, sizeof (szSections),
00193 fsGetDataFilePath ("Update\\proupd.lst")) ||
00194 atoi (szSections) <= PRG_BUILD_NUMBER )
00195 {
00196 ASSERT (GetPrivateProfileSectionNames (szSections, sizeof (szSections), fsGetDataFilePath ("Update\\proupd.lst")));
00197
00198 _App.NewVerExists (FALSE);
00199 Event (UME_NEWVERSIONNOTAVAIL);
00200 m_bRunning = FALSE;
00201 return;
00202 }
00203
00204 m_strBN = szSections;
00205
00206 CString strCurBN;
00207 strCurBN.Format ("%d", PRG_BUILD_NUMBER);
00208
00209 m_strUpgSize = "";
00210 m_strUpgFileName = "";
00211 m_vWN.clear ();
00212
00213 LPCSTR pszSect = szSections;
00214
00215 while (*pszSect)
00216 {
00217
00218 GetPrivateProfileSection (pszSect, szValues, sizeof (szValues),
00219 fsGetDataFilePath ("Update\\proupd.lst"));
00220 LPSTR pszValue = szValues;
00221
00222 BOOL bCommon = stricmp (pszSect, "Common") == 0;
00223
00224 BOOL bNewBNNow = bCommon == FALSE && strcmp (pszSect, m_strBN) == 0;
00225
00226 BOOL bBiggerBNNow = bCommon == FALSE && atoi (pszSect) > PRG_BUILD_NUMBER;
00227
00228 while (*pszValue)
00229 {
00230
00231 LPSTR pszVVal = strchr (pszValue, '=');
00232 *pszVVal = 0;
00233 pszVVal++;
00234
00235 if (bCommon)
00236 {
00237
00238 if (stricmp (pszValue, "DownloadPathForFullInstall") == 0)
00239 {
00240 m_strDlFullInstallPath = pszVVal;
00241 if (m_strDlFullInstallPath [m_strDlFullInstallPath.GetLength () - 1] != '\\' &&
00242 m_strDlFullInstallPath [m_strDlFullInstallPath.GetLength () - 1] != '/')
00243 m_strDlFullInstallPath += '/';
00244 }
00245
00246 if (stricmp (pszValue, "DownloadPathForUpgrades") == 0)
00247 {
00248 m_strDlUpgradesPath = pszVVal;
00249 if (m_strDlUpgradesPath [m_strDlUpgradesPath.GetLength () - 1] != '\\' &&
00250 m_strDlUpgradesPath [m_strDlUpgradesPath.GetLength () - 1] != '/')
00251 m_strDlUpgradesPath += '/';
00252 }
00253 }
00254
00255 if (bNewBNNow)
00256 {
00257 if (stricmp (pszValue, "Version") == 0)
00258 m_strVersion = pszVVal;
00259 else if (stricmp (pszValue, "FullSize") == 0)
00260 m_strFullSize = pszVVal;
00261 else if (stricmp (pszValue, strCurBN) == 0)
00262 m_strUpgSize = pszVVal;
00263 else if (strnicmp (pszValue, strCurBN, strCurBN.GetLength ()) == 0)
00264 {
00265
00266 if (stricmp (pszValue + strCurBN.GetLength (), "-name") == 0)
00267 m_strUpgFileName = pszVVal;
00268
00269
00270 else if (stricmp (pszValue + strCurBN.GetLength (), "-size") == 0)
00271 m_strUpgSize = pszVVal;
00272 }
00273 else if (stricmp (pszValue, "FrmtVer") == 0)
00274 {
00275
00276 int nVer = atoi (pszVVal);
00277 if (nVer != 1)
00278 {
00279
00280 _App.NewVerExists (FALSE);
00281 Event (UME_NEWVERSIONNOTAVAIL);
00282 m_bRunning = FALSE;
00283 return;
00284 }
00285 }
00286 }
00287
00288 if (bBiggerBNNow && strncmp (pszValue, "WN", 2) == 0)
00289 m_vWN.add (pszVVal);
00290
00291 pszValue = pszVVal;
00292 while (*pszValue++);
00293 }
00294
00295 while (*pszSect++);
00296 }
00297
00298 Event (UME_NEWVERSIONAVAIL);
00299 }
00300
00301 void fsUpdateMgr::Event(fsUpdateMgrEvent ev)
00302 {
00303 if (m_pfnEvents)
00304 m_pfnEvents (ev, m_lpEventsParam);
00305 }
00306
00307 BOOL fsUpdateMgr::IsRunning()
00308 {
00309 return m_bRunning;
00310 }
00311
00312 void fsUpdateMgr::SetEventsFunc(fntUpdateMgrEventsFunc pfn, LPVOID lp)
00313 {
00314 m_pfnEvents = pfn;
00315 m_lpEventsParam = lp;
00316 }
00317
00318 LPCSTR fsUpdateMgr::GetVersion()
00319 {
00320 return m_strVersion;
00321 }
00322
00323 LPCSTR fsUpdateMgr::GetBuildNumber()
00324 {
00325 return m_strBN;
00326 }
00327
00328 void fsUpdateMgr::Stop()
00329 {
00330 if (m_dldr && m_dldr->IsRunning ())
00331 m_dldr->StopDownloading ();
00332 else
00333 m_bRunning = FALSE;
00334 }
00335
00336 LPCSTR fsUpdateMgr::GetFullSize()
00337 {
00338 return m_strFullSize;
00339 }
00340
00341 LPCSTR fsUpdateMgr::GetUpgSize()
00342 {
00343 return m_strUpgSize;
00344 }
00345
00346 fs::list <CString>* fsUpdateMgr::GetWhatNew()
00347 {
00348 return &m_vWN;
00349 }
00350
00351 void fsUpdateMgr::Update(BOOL bByFull)
00352 {
00353 if (m_bRunning == FALSE || m_bChecking)
00354 return;
00355
00356 SAFE_DELETE (m_dldr);
00357 fsnew1 (m_dldr, fsDownloadMgr (NULL));
00358 m_dldr->SetEventFunc (_DownloadMgrEvents, this);
00359 m_dldr->SetEventDescFunc (_DownloadMgrDescEvents, this);
00360
00361 m_bChecking = FALSE;
00362
00363 CString strUrl;
00364 if (bByFull)
00365 strUrl.Format ("%sfdminst.exe", m_strDlFullInstallPath);
00366 else
00367 {
00368
00369 if (m_strUpgFileName.GetLength () == 0)
00370
00371 strUrl.Format ("%sfdm%dto%supg.exe", m_strDlUpgradesPath, PRG_BUILD_NUMBER, m_strBN);
00372 else
00373
00374 strUrl.Format ("%s%s", m_strDlUpgradesPath, m_strUpgFileName);
00375 }
00376
00377 m_dldr->CreateByUrl (strUrl);
00378
00379 fsDownload_Properties *dp = m_dldr->GetDP ();
00380
00381 SAFE_DELETE_ARRAY (dp->pszFileName);
00382 CString strFile = fsGetDataFilePath ("Update\\");
00383 fsnew (dp->pszFileName, char, strFile.GetLength () + 1);
00384 strcpy (dp->pszFileName, strFile);
00385 dp->enAER = AER_REWRITE;
00386 dp->uMaxAttempts = 1;
00387 *dp->pszAdditionalExt = 0;
00388
00389 Event (UME_CONNECTING);
00390
00391 m_dldr->StartDownloading ();
00392 }
00393
00394 void fsUpdateMgr::_DownloadMgrDescEvents(fsDownloadMgr* , fsDownloadMgr_EventDescType , LPCSTR pszDesc, LPVOID lp)
00395 {
00396 fsUpdateMgr *pThis = (fsUpdateMgr*) lp;
00397 pThis->Event (pszDesc);
00398 }
00399
00400 void fsUpdateMgr::SetDescEventsFunc(fntUpdateMgrDescEvents pfn, LPVOID lpParam)
00401 {
00402 m_pfnDescEvents = pfn;
00403 m_lpDescEventsParam = lpParam;
00404 }
00405
00406 void fsUpdateMgr::Event(LPCSTR pszEvent)
00407 {
00408 if (m_pfnDescEvents)
00409 m_pfnDescEvents (pszEvent, m_lpDescEventsParam);
00410 }
00411
00412 void fsUpdateMgr::UpdateOnNextStart()
00413 {
00414 AfxGetApp ()->WriteProfileString ("Settings\\Update", "UpdateFile", m_strUpdateFile);
00415 }
00416
00417 BOOL fsUpdateMgr::IsStartUpdaterNeeded (BOOL bUpdaterWillBeLaunchedNow)
00418 {
00419 m_strUpdateFile = AfxGetApp ()->GetProfileString ("Settings\\Update", "UpdateFile", "");
00420 if (bUpdaterWillBeLaunchedNow)
00421 AfxGetApp ()->WriteProfileString ("Settings\\Update", "UpdateFile", "");
00422 return m_strUpdateFile.GetLength () != 0;
00423 }
00424
00425 void fsUpdateMgr::FixIniFileFor9x(LPCSTR pszIni)
00426 {
00427 HANDLE hFile = CreateFile (pszIni, GENERIC_READ | GENERIC_WRITE, 0, NULL,
00428 OPEN_EXISTING, 0, NULL);
00429 if (hFile == INVALID_HANDLE_VALUE)
00430 return;
00431
00432 DWORD dwLen = GetFileSize (hFile, NULL);
00433 LPSTR psz = new char [dwLen + 1];
00434 DWORD dw;
00435 if (FALSE == ReadFile (hFile, psz, dwLen, &dw, NULL))
00436 return;
00437 psz [dwLen] = 0;
00438
00439 CString str = psz;
00440 delete [] psz;
00441
00442 str.Replace ("\r\n", "\n");
00443 str.Replace ("\n\r", "\n");
00444 str.Replace ("\n", "\r\n");
00445
00446 SetFilePointer (hFile, 0, NULL, FILE_BEGIN);
00447 WriteFile (hFile, (LPCSTR)str, str.GetLength (), &dw, NULL);
00448 SetEndOfFile (hFile);
00449
00450 CloseHandle (hFile);
00451 }