InternetUpdate.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // InternetUpdate.cpp: implementation of the CInternetUpdate class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "InternetUpdate.h"
  7. #include <afxinet.h>
  8. #include "ProgressWnd.h"
  9. #include "io.h"
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[]=__FILE__;
  13. #define new DEBUG_NEW
  14. #endif
  15. #define HTTPBUFLEN 512 // Size of HTTP Buffer...
  16. //////////////////////////////////////////////////////////////////////
  17. // Construction/Destruction
  18. //////////////////////////////////////////////////////////////////////
  19. CInternetUpdate::CInternetUpdate()
  20. {
  21. }
  22. CInternetUpdate::~CInternetUpdate()
  23. {
  24. }
  25. BOOL CInternetUpdate::CheckForUpdate(HWND hParent, BOOL bCheckForPrevUpdate, BOOL bShowNoUpdatesDlg)
  26. {
  27. m_bShowMessages = bShowNoUpdatesDlg;
  28. m_hParent = hParent;
  29. CTime Now = CTime::GetCurrentTime();
  30. struct tm ptmTemp;
  31. tm tmNow = *(Now.GetLocalTm(&ptmTemp));
  32. long lCurrentDayOfYear = tmNow.tm_yday;
  33. RemoveOldUpdateFile();
  34. if(bCheckForPrevUpdate)
  35. {
  36. if(!CGetSetOptions::GetCheckForUpdates())
  37. return FALSE;
  38. long lLastUpdateDay = CGetSetOptions::GetLastUpdate();
  39. if(lCurrentDayOfYear - lLastUpdateDay < 10)
  40. return FALSE;
  41. //if the last time we check was today return
  42. if(lLastUpdateDay == lCurrentDayOfYear)
  43. return FALSE;
  44. }
  45. CGetSetOptions::SetLastUpdate(lCurrentDayOfYear);
  46. BOOL bRet = FALSE;
  47. m_lRunningVersion = GetRunningVersion();
  48. m_lUpdateVersion = GetUpdateVersion();
  49. if(m_lUpdateVersion > m_lRunningVersion)
  50. {
  51. CString csMessage;
  52. csMessage.Format( _T("%s, %s\n")
  53. _T("%s, %s\n\n")
  54. _T("%s"),
  55. theApp.m_Language.GetString("Updates_Available", "Updates available for Ditto.\nVisit ditto-cp.sourceforge.net for details\n\nRunning Version"),
  56. GetVersionString(m_lRunningVersion),
  57. theApp.m_Language.GetString("Update_Version", "Update Version"),
  58. GetVersionString(m_lUpdateVersion),
  59. theApp.m_Language.GetString("Download_Update", "Download updated version?"));
  60. if(MessageBox(hParent, csMessage, _T("Ditto"), MB_YESNO) == IDYES)
  61. {
  62. CString csFile = DownloadUpdate();
  63. if(!csFile.IsEmpty())
  64. {
  65. CloseHandle(theApp.m_hMutex);
  66. Sleep(100);
  67. ShellExecute(NULL, NULL, csFile, NULL, NULL, SW_SHOWNORMAL);
  68. }
  69. bRet = TRUE;
  70. }
  71. }
  72. else if(m_bShowMessages)
  73. {
  74. MessageBox(hParent, theApp.m_Language.GetString("No_Updates", "No updates available"), _T("Ditto"), MB_OK);
  75. }
  76. return bRet;
  77. }
  78. BOOL CInternetUpdate::RemoveOldUpdateFile()
  79. {
  80. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  81. csFile += "DittoSetup.exe";
  82. BOOL bRet = TRUE;
  83. if(FileExists(csFile))
  84. {
  85. bRet = ::DeleteFile(csFile);
  86. }
  87. return bRet;
  88. }
  89. CString CInternetUpdate::GetVersionString(long lVersion)
  90. {
  91. CString csLine;
  92. csLine.Format(_T("%02i.%02i.%02i.%02i"),
  93. (lVersion >> 24) & 0x03f,
  94. (lVersion >> 16) & 0x03f,
  95. ((lVersion >> 8) & 0x07f),
  96. lVersion & 0x07f);
  97. return csLine;
  98. }
  99. long CInternetUpdate::GetRunningVersion()
  100. {
  101. CString csFileName = CGetSetOptions::GetExeFileName();
  102. DWORD dwSize, dwHandle;
  103. LPBYTE lpData;
  104. UINT iBuffSize;
  105. VS_FIXEDFILEINFO *lpFFI;
  106. long ver;
  107. dwSize = GetFileVersionInfoSize(csFileName.GetBuffer(csFileName.GetLength()), &dwHandle);
  108. if(dwSize != 0)
  109. {
  110. csFileName.ReleaseBuffer();
  111. if((lpData=(unsigned char *)malloc(dwSize)) != NULL)
  112. {
  113. if(GetFileVersionInfo(csFileName.GetBuffer(csFileName.GetLength()), dwHandle, dwSize, lpData) != 0)
  114. {
  115. if(VerQueryValue(lpData, _T("\\"), (LPVOID*)&lpFFI, &iBuffSize) != 0)
  116. {
  117. if(iBuffSize > 0)
  118. {
  119. ver = (HIWORD(lpFFI->dwFileVersionMS) & 0x00ff) << 24;
  120. ver = ver + ((LOWORD(lpFFI->dwFileVersionMS) & 0x00ff) << 16);
  121. ver = ver + ((HIWORD(lpFFI->dwFileVersionLS) & 0x00ff) << 8);
  122. ver = ver + LOWORD(lpFFI->dwFileVersionLS);
  123. free(lpData);
  124. return(ver);
  125. }
  126. }
  127. }
  128. free(lpData);
  129. }
  130. }
  131. return(0);
  132. }
  133. long CInternetUpdate::GetUpdateVersion()
  134. {
  135. char httpbuff[HTTPBUFLEN];
  136. //Try to get a path from the regestry
  137. CString csPath = CGetSetOptions::GetUpdateFilePath();
  138. //if nothing there get the default
  139. if(csPath.IsEmpty())
  140. {
  141. if(g_Opt.m_bU3)
  142. {
  143. csPath = "ditto-cp.sourceforge.net/U3/DittoVersion.txt";
  144. }
  145. else
  146. {
  147. csPath = "ditto-cp.sourceforge.net/Update3/DittoVersion.txt";
  148. }
  149. }
  150. CString csUrl = "http://" + csPath;
  151. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  152. csFile += "DittoVersion.txt";
  153. bool bError = false;
  154. CStdioFile *remotefile = NULL;
  155. long lReturn = -1;
  156. try
  157. {
  158. CInternetSession mysession;
  159. remotefile = mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  160. if(!remotefile)
  161. return 0;
  162. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  163. UINT unBytesRead = 0;
  164. UINT unTotalBytes = 0;
  165. while (unBytesRead = remotefile->Read(httpbuff, HTTPBUFLEN))
  166. {
  167. unTotalBytes += unBytesRead;
  168. myfile.Write(httpbuff, unBytesRead);
  169. if(!remotefile)
  170. {
  171. unTotalBytes = 0;
  172. break;
  173. }
  174. }
  175. myfile.Close();
  176. if(unTotalBytes)
  177. {
  178. CStdioFile file;
  179. if(file.Open(csFile, CFile::modeRead|CFile::typeText))
  180. {
  181. CString csVersion;
  182. if(file.ReadString(csVersion))
  183. {
  184. file.Close();
  185. lReturn = ATOL(csVersion);
  186. }
  187. }
  188. }
  189. }
  190. catch(CInternetException *pEX)
  191. {
  192. bError = true;
  193. pEX->Delete();
  194. }
  195. catch(CFileException *e)
  196. {
  197. bError = true;
  198. e->Delete();
  199. }
  200. catch(...)
  201. {
  202. bError = true;
  203. csFile.Empty();
  204. }
  205. if(bError)
  206. {
  207. if(m_bShowMessages)
  208. {
  209. MessageBox(m_hParent, _T("Error Connecting."), _T("Ditto"), MB_OK);
  210. m_bShowMessages = FALSE;
  211. }
  212. }
  213. if(remotefile)
  214. {
  215. remotefile->Close();
  216. delete remotefile;
  217. remotefile = NULL;
  218. }
  219. if(FileExists(csFile))
  220. CFile::Remove(csFile);
  221. return lReturn;
  222. }
  223. CString CInternetUpdate::DownloadUpdate()
  224. {
  225. char httpbuff[HTTPBUFLEN];
  226. //Try to get a path from the regestry
  227. CString csPath = CGetSetOptions::GetUpdateInstallPath();
  228. //if nothing there get the default
  229. if(csPath.IsEmpty())
  230. {
  231. if(g_Opt.m_bU3)
  232. {
  233. csPath = "ditto-cp.sourceforge.net/U3/DittoSetup.exe";
  234. }
  235. else
  236. {
  237. csPath = "ditto-cp.sourceforge.net/U3/DittoSetup.exe";
  238. }
  239. }
  240. CString csUrl = "http://" + csPath;
  241. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  242. csFile += "DittoSetup.exe";
  243. long lReturn = -1;
  244. CHttpFile *RemoteFile = NULL;
  245. try
  246. {
  247. CInternetSession mysession;
  248. RemoteFile = (CHttpFile*)mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  249. if(!RemoteFile)
  250. return "";
  251. //Get the file size
  252. DWORD dFileSize;
  253. RemoteFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dFileSize);
  254. //Set up the progress wnd
  255. CProgressWnd progress;
  256. progress.Create(CWnd::FromHandlePermanent(m_hParent), _T("Ditto Update"));
  257. progress.SetRange(0, dFileSize, HTTPBUFLEN);
  258. progress.SetText(_T("Downloading Ditto Update ..."));
  259. //Create the file to put the info in
  260. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  261. UINT unBytesRead = 0;
  262. UINT unTotalBytes = 0;
  263. //Read in the file
  264. while (unBytesRead = RemoteFile->Read(httpbuff, HTTPBUFLEN))
  265. {
  266. progress.StepIt();
  267. progress.PeekAndPump();
  268. if(progress.Cancelled())
  269. {
  270. csFile.Empty();
  271. break;
  272. }
  273. unTotalBytes += unBytesRead;
  274. myfile.Write(httpbuff, unBytesRead);
  275. if(!RemoteFile)
  276. {
  277. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  278. csFile = "";
  279. break;
  280. }
  281. }
  282. myfile.Close();
  283. }
  284. catch(CInternetException *pEX)
  285. {
  286. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  287. csFile.Empty();
  288. pEX->Delete();
  289. }
  290. catch(CFileException *e)
  291. {
  292. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  293. csFile.Empty();
  294. e->Delete();
  295. }
  296. catch(...)
  297. {
  298. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  299. csFile.Empty();
  300. }
  301. if(RemoteFile)
  302. {
  303. RemoteFile->Close();
  304. delete RemoteFile;
  305. RemoteFile = NULL;
  306. }
  307. return csFile;
  308. }