InternetUpdate.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. return 0;
  102. CString csFileName = CGetSetOptions::GetExeFileName();
  103. DWORD dwSize, dwHandle;
  104. LPBYTE lpData;
  105. UINT iBuffSize;
  106. VS_FIXEDFILEINFO *lpFFI;
  107. long ver;
  108. //dwSize = GetFileVersionInfoSize(csFileName.GetBuffer(csFileName.GetLength()), &dwHandle);
  109. if(dwSize != 0)
  110. {
  111. csFileName.ReleaseBuffer();
  112. if((lpData=(unsigned char *)malloc(dwSize)) != NULL)
  113. {
  114. //if(GetFileVersionInfo(csFileName.GetBuffer(csFileName.GetLength()), dwHandle, dwSize, lpData) != 0)
  115. {
  116. //if(VerQueryValue(lpData, _T("\\"), (LPVOID*)&lpFFI, &iBuffSize) != 0)
  117. {
  118. if(iBuffSize > 0)
  119. {
  120. ver = (HIWORD(lpFFI->dwFileVersionMS) & 0x00ff) << 24;
  121. ver = ver + ((LOWORD(lpFFI->dwFileVersionMS) & 0x00ff) << 16);
  122. ver = ver + ((HIWORD(lpFFI->dwFileVersionLS) & 0x00ff) << 8);
  123. ver = ver + LOWORD(lpFFI->dwFileVersionLS);
  124. free(lpData);
  125. return(ver);
  126. }
  127. }
  128. }
  129. free(lpData);
  130. }
  131. }
  132. return(0);
  133. }
  134. long CInternetUpdate::GetUpdateVersion()
  135. {
  136. char httpbuff[HTTPBUFLEN];
  137. //Try to get a path from the regestry
  138. CString csPath = CGetSetOptions::GetUpdateFilePath();
  139. //if nothing there get the default
  140. if(csPath.IsEmpty())
  141. {
  142. if(g_Opt.m_bU3)
  143. {
  144. csPath = "ditto-cp.sourceforge.net/U3/DittoVersion.txt";
  145. }
  146. else
  147. {
  148. csPath = "ditto-cp.sourceforge.net/Update3/DittoVersion.txt";
  149. }
  150. }
  151. CString csUrl = "http://" + csPath;
  152. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  153. csFile += "DittoVersion.txt";
  154. bool bError = false;
  155. CStdioFile *remotefile = NULL;
  156. long lReturn = -1;
  157. try
  158. {
  159. CInternetSession mysession;
  160. remotefile = mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  161. if(!remotefile)
  162. return 0;
  163. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  164. UINT unBytesRead = 0;
  165. UINT unTotalBytes = 0;
  166. while (unBytesRead = remotefile->Read(httpbuff, HTTPBUFLEN))
  167. {
  168. unTotalBytes += unBytesRead;
  169. myfile.Write(httpbuff, unBytesRead);
  170. if(!remotefile)
  171. {
  172. unTotalBytes = 0;
  173. break;
  174. }
  175. }
  176. myfile.Close();
  177. if(unTotalBytes)
  178. {
  179. CStdioFile file;
  180. if(file.Open(csFile, CFile::modeRead|CFile::typeText))
  181. {
  182. CString csVersion;
  183. if(file.ReadString(csVersion))
  184. {
  185. file.Close();
  186. lReturn = ATOL(csVersion);
  187. }
  188. }
  189. }
  190. }
  191. catch(CInternetException *pEX)
  192. {
  193. bError = true;
  194. pEX->Delete();
  195. }
  196. catch(CFileException *e)
  197. {
  198. bError = true;
  199. e->Delete();
  200. }
  201. catch(...)
  202. {
  203. bError = true;
  204. csFile.Empty();
  205. }
  206. if(bError)
  207. {
  208. if(m_bShowMessages)
  209. {
  210. MessageBox(m_hParent, _T("Error Connecting."), _T("Ditto"), MB_OK);
  211. m_bShowMessages = FALSE;
  212. }
  213. }
  214. if(remotefile)
  215. {
  216. remotefile->Close();
  217. delete remotefile;
  218. remotefile = NULL;
  219. }
  220. if(FileExists(csFile))
  221. CFile::Remove(csFile);
  222. return lReturn;
  223. }
  224. CString CInternetUpdate::DownloadUpdate()
  225. {
  226. char httpbuff[HTTPBUFLEN];
  227. //Try to get a path from the regestry
  228. CString csPath = CGetSetOptions::GetUpdateInstallPath();
  229. //if nothing there get the default
  230. if(csPath.IsEmpty())
  231. {
  232. if(g_Opt.m_bU3)
  233. {
  234. csPath = "ditto-cp.sourceforge.net/U3/DittoSetup.exe";
  235. }
  236. else
  237. {
  238. csPath = "ditto-cp.sourceforge.net/U3/DittoSetup.exe";
  239. }
  240. }
  241. CString csUrl = "http://" + csPath;
  242. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  243. csFile += "DittoSetup.exe";
  244. long lReturn = -1;
  245. CHttpFile *RemoteFile = NULL;
  246. try
  247. {
  248. CInternetSession mysession;
  249. RemoteFile = (CHttpFile*)mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  250. if(!RemoteFile)
  251. return "";
  252. //Get the file size
  253. DWORD dFileSize;
  254. RemoteFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dFileSize);
  255. //Set up the progress wnd
  256. CProgressWnd progress;
  257. progress.Create(CWnd::FromHandlePermanent(m_hParent), _T("Ditto Update"));
  258. progress.SetRange(0, dFileSize, HTTPBUFLEN);
  259. progress.SetText(_T("Downloading Ditto Update ..."));
  260. //Create the file to put the info in
  261. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  262. UINT unBytesRead = 0;
  263. UINT unTotalBytes = 0;
  264. //Read in the file
  265. while (unBytesRead = RemoteFile->Read(httpbuff, HTTPBUFLEN))
  266. {
  267. progress.StepIt();
  268. progress.PeekAndPump();
  269. if(progress.Cancelled())
  270. {
  271. csFile.Empty();
  272. break;
  273. }
  274. unTotalBytes += unBytesRead;
  275. myfile.Write(httpbuff, unBytesRead);
  276. if(!RemoteFile)
  277. {
  278. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  279. csFile = "";
  280. break;
  281. }
  282. }
  283. myfile.Close();
  284. }
  285. catch(CInternetException *pEX)
  286. {
  287. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  288. csFile.Empty();
  289. pEX->Delete();
  290. }
  291. catch(CFileException *e)
  292. {
  293. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  294. csFile.Empty();
  295. e->Delete();
  296. }
  297. catch(...)
  298. {
  299. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  300. csFile.Empty();
  301. }
  302. if(RemoteFile)
  303. {
  304. RemoteFile->Close();
  305. delete RemoteFile;
  306. RemoteFile = NULL;
  307. }
  308. return csFile;
  309. }