InternetUpdate.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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( "%s, %s\n"
  52. "%s, %s\n\n"
  53. "%s",
  54. theApp.m_Language.GetString("Updates_Available", "Updates available for Ditto.\nVisit ditto-cp.sourceforge.net for details\n\nRunning Version"),
  55. GetVersionString(m_lRunningVersion),
  56. theApp.m_Language.GetString("Update_Version", "Update Version"),
  57. GetVersionString(m_lUpdateVersion),
  58. theApp.m_Language.GetString("Download_Update", "Download updated version?"));
  59. if(MessageBox(hParent, csMessage, "Ditto", MB_YESNO) == IDYES)
  60. {
  61. CString csFile = DownloadUpdate();
  62. if(!csFile.IsEmpty())
  63. {
  64. CloseHandle(theApp.m_hMutex);
  65. Sleep(100);
  66. ShellExecute(NULL, NULL, csFile, NULL, NULL, SW_SHOWNORMAL);
  67. }
  68. bRet = TRUE;
  69. }
  70. }
  71. else if(m_bShowMessages)
  72. {
  73. MessageBox(hParent, theApp.m_Language.GetString("No_Updates", "No updates available"), "Ditto", MB_OK);
  74. }
  75. return bRet;
  76. }
  77. BOOL CInternetUpdate::RemoveOldUpdateFile()
  78. {
  79. CString csFile = CGetSetOptions::GetExeFileName();
  80. csFile = GetFilePath(csFile);
  81. csFile += "DittoSetup.exe";
  82. BOOL bRet = TRUE;
  83. if(_access(csFile, 0) != -1)
  84. {
  85. bRet = ::DeleteFile(csFile);
  86. }
  87. return bRet;
  88. }
  89. CString CInternetUpdate::GetVersionString(long lVersion)
  90. {
  91. CString csLine;
  92. csLine.Format("%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, "\\", (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. csPath = "ditto-cp.sourceforge.net/Update/DittoVersion.txt";
  141. CString csUrl = "http://" + csPath;
  142. CString csFile = CGetSetOptions::GetExeFileName();
  143. csFile = GetFilePath(csFile);
  144. csFile += "DittoVersion.txt";
  145. bool bError = false;
  146. CStdioFile *remotefile = NULL;
  147. long lReturn = -1;
  148. try
  149. {
  150. CInternetSession mysession;
  151. remotefile = mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  152. if(!remotefile)
  153. return 0;
  154. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  155. UINT unBytesRead = 0;
  156. UINT unTotalBytes = 0;
  157. while (unBytesRead = remotefile->Read(httpbuff, HTTPBUFLEN))
  158. {
  159. unTotalBytes += unBytesRead;
  160. myfile.Write(httpbuff, unBytesRead);
  161. if(!remotefile)
  162. {
  163. unTotalBytes = 0;
  164. break;
  165. }
  166. }
  167. myfile.Close();
  168. if(unTotalBytes)
  169. {
  170. CStdioFile file;
  171. if(file.Open(csFile, CFile::modeRead|CFile::typeText))
  172. {
  173. CString csVersion;
  174. if(file.ReadString(csVersion))
  175. {
  176. file.Close();
  177. lReturn = atol(csVersion);
  178. }
  179. }
  180. }
  181. }
  182. catch(CInternetException *pEX)
  183. {
  184. bError = true;
  185. pEX->Delete();
  186. }
  187. catch(CFileException *e)
  188. {
  189. bError = true;
  190. e->Delete();
  191. }
  192. catch(...)
  193. {
  194. bError = true;
  195. csFile.Empty();
  196. }
  197. if(bError)
  198. {
  199. if(m_bShowMessages)
  200. {
  201. MessageBox(m_hParent, "Error Connecting.", "Ditto", MB_OK);
  202. m_bShowMessages = FALSE;
  203. }
  204. }
  205. if(remotefile)
  206. {
  207. remotefile->Close();
  208. delete remotefile;
  209. remotefile = NULL;
  210. }
  211. if(access(csFile, 0) != -1)
  212. CFile::Remove(csFile);
  213. return lReturn;
  214. }
  215. CString CInternetUpdate::DownloadUpdate()
  216. {
  217. char httpbuff[HTTPBUFLEN];
  218. //Try to get a path from the regestry
  219. CString csPath = CGetSetOptions::GetUpdateInstallPath();
  220. //if nothing there get the default
  221. if(csPath.IsEmpty())
  222. csPath = "ditto-cp.sourceforge.net/Update/DittoSetup.exe";
  223. CString csUrl = "http://" + csPath;
  224. CString csFile = CGetSetOptions::GetExeFileName();
  225. csFile = GetFilePath(csFile);
  226. csFile += "DittoSetup.exe";
  227. long lReturn = -1;
  228. CHttpFile *RemoteFile = NULL;
  229. try
  230. {
  231. CInternetSession mysession;
  232. RemoteFile = (CHttpFile*)mysession.OpenURL(csUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
  233. if(!RemoteFile)
  234. return "";
  235. //Get the file size
  236. DWORD dFileSize;
  237. RemoteFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dFileSize);
  238. //Set up the progress wnd
  239. CProgressWnd progress;
  240. progress.Create(CWnd::FromHandlePermanent(m_hParent), "Ditto Update");
  241. progress.SetRange(0, dFileSize, HTTPBUFLEN);
  242. progress.SetText("Downloading Ditto Update ...");
  243. //Create the file to put the info in
  244. CFile myfile(csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  245. UINT unBytesRead = 0;
  246. UINT unTotalBytes = 0;
  247. //Read in the file
  248. while (unBytesRead = RemoteFile->Read(httpbuff, HTTPBUFLEN))
  249. {
  250. progress.StepIt();
  251. progress.PeekAndPump();
  252. if(progress.Cancelled())
  253. {
  254. csFile.Empty();
  255. break;
  256. }
  257. unTotalBytes += unBytesRead;
  258. myfile.Write(httpbuff, unBytesRead);
  259. if(!RemoteFile)
  260. {
  261. MessageBox(m_hParent, "Error Downloading update.", "Ditto", MB_OK);
  262. csFile = "";
  263. break;
  264. }
  265. }
  266. myfile.Close();
  267. }
  268. catch(CInternetException *pEX)
  269. {
  270. MessageBox(m_hParent, "Error Downloading update.", "Ditto", MB_OK);
  271. csFile.Empty();
  272. pEX->Delete();
  273. }
  274. catch(CFileException *e)
  275. {
  276. MessageBox(m_hParent, "Error Downloading update.", "Ditto", MB_OK);
  277. csFile.Empty();
  278. e->Delete();
  279. }
  280. catch(...)
  281. {
  282. MessageBox(m_hParent, "Error Downloading update.", "Ditto", MB_OK);
  283. csFile.Empty();
  284. }
  285. if(RemoteFile)
  286. {
  287. RemoteFile->Close();
  288. delete RemoteFile;
  289. RemoteFile = NULL;
  290. }
  291. return csFile;
  292. }