InternetUpdate.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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) & 0x0ff,
  94. (lVersion >> 16) & 0x0ff,
  95. ((lVersion >> 8) & 0x0ff),
  96. lVersion & 0x0ff);
  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. csPath = "ditto-cp.sourceforge.net/Update3/DittoVersion.txt";
  142. }
  143. CString csUrl = "http://" + csPath;
  144. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  145. csFile += "DittoVersion.txt";
  146. bool bError = false;
  147. CStdioFile *remotefile = NULL;
  148. long lReturn = -1;
  149. try
  150. {
  151. CInternetSession mysession;
  152. remotefile = mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  153. if(!remotefile)
  154. return 0;
  155. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  156. UINT unBytesRead = 0;
  157. UINT unTotalBytes = 0;
  158. while (unBytesRead = remotefile->Read(httpbuff, HTTPBUFLEN))
  159. {
  160. unTotalBytes += unBytesRead;
  161. myfile.Write(httpbuff, unBytesRead);
  162. if(!remotefile)
  163. {
  164. unTotalBytes = 0;
  165. break;
  166. }
  167. }
  168. myfile.Close();
  169. if(unTotalBytes)
  170. {
  171. CStdioFile file;
  172. if(file.Open(csFile, CFile::modeRead|CFile::typeText))
  173. {
  174. CString csVersion;
  175. if(file.ReadString(csVersion))
  176. {
  177. file.Close();
  178. lReturn = ATOL(csVersion);
  179. }
  180. }
  181. }
  182. }
  183. catch(CInternetException *pEX)
  184. {
  185. bError = true;
  186. pEX->Delete();
  187. }
  188. catch(CFileException *e)
  189. {
  190. bError = true;
  191. e->Delete();
  192. }
  193. catch(...)
  194. {
  195. bError = true;
  196. csFile.Empty();
  197. }
  198. if(bError)
  199. {
  200. if(m_bShowMessages)
  201. {
  202. MessageBox(m_hParent, _T("Error Connecting."), _T("Ditto"), MB_OK);
  203. m_bShowMessages = FALSE;
  204. }
  205. }
  206. if(remotefile)
  207. {
  208. remotefile->Close();
  209. delete remotefile;
  210. remotefile = NULL;
  211. }
  212. if(FileExists(csFile))
  213. CFile::Remove(csFile);
  214. return lReturn;
  215. }
  216. CString CInternetUpdate::DownloadUpdate()
  217. {
  218. char httpbuff[HTTPBUFLEN];
  219. //Try to get a path from the regestry
  220. CString csPath = CGetSetOptions::GetUpdateInstallPath();
  221. //if nothing there get the default
  222. if(csPath.IsEmpty())
  223. {
  224. csPath = "ditto-cp.sourceforge.net/U3/DittoSetup.exe";
  225. }
  226. CString csUrl = "http://" + csPath;
  227. CString csFile = CGetSetOptions::GetPath(PATH_UPDATE_FILE);
  228. csFile += "DittoSetup.exe";
  229. long lReturn = -1;
  230. CHttpFile *RemoteFile = NULL;
  231. try
  232. {
  233. CInternetSession mysession;
  234. RemoteFile = (CHttpFile*)mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  235. if(!RemoteFile)
  236. return "";
  237. //Get the file size
  238. DWORD dFileSize;
  239. RemoteFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dFileSize);
  240. //Set up the progress wnd
  241. CProgressWnd progress;
  242. progress.Create(CWnd::FromHandlePermanent(m_hParent), _T("Ditto Update"));
  243. progress.SetRange(0, dFileSize, HTTPBUFLEN);
  244. progress.SetText(_T("Downloading Ditto Update ..."));
  245. //Create the file to put the info in
  246. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  247. UINT unBytesRead = 0;
  248. UINT unTotalBytes = 0;
  249. //Read in the file
  250. while (unBytesRead = RemoteFile->Read(httpbuff, HTTPBUFLEN))
  251. {
  252. progress.StepIt();
  253. progress.PeekAndPump();
  254. if(progress.Cancelled())
  255. {
  256. csFile.Empty();
  257. break;
  258. }
  259. unTotalBytes += unBytesRead;
  260. myfile.Write(httpbuff, unBytesRead);
  261. if(!RemoteFile)
  262. {
  263. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  264. csFile = "";
  265. break;
  266. }
  267. }
  268. myfile.Close();
  269. }
  270. catch(CInternetException *pEX)
  271. {
  272. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  273. csFile.Empty();
  274. pEX->Delete();
  275. }
  276. catch(CFileException *e)
  277. {
  278. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  279. csFile.Empty();
  280. e->Delete();
  281. }
  282. catch(...)
  283. {
  284. MessageBox(m_hParent, _T("Error Downloading update."), _T("Ditto"), MB_OK);
  285. csFile.Empty();
  286. }
  287. if(RemoteFile)
  288. {
  289. RemoteFile->Close();
  290. delete RemoteFile;
  291. RemoteFile = NULL;
  292. }
  293. return csFile;
  294. }