InternetUpdate.cpp 7.4 KB

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