InternetUpdate.cpp 7.4 KB

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