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