InternetUpdate.cpp 7.5 KB

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