Clean up warning
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blob5d1dba327fe6061157af7291be4e2414163cab4d
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "TortoiseProc.h"
21 #include "CommonResource.h"
22 #include "..\version.h"
23 #include "MessageBox.h"
24 #include ".\checkforupdatesdlg.h"
25 #include "Registry.h"
26 #include "AppUtils.h"
27 #include "TempFile.h"
30 IMPLEMENT_DYNAMIC(CCheckForUpdatesDlg, CStandAloneDialog)
31 CCheckForUpdatesDlg::CCheckForUpdatesDlg(CWnd* pParent /*=NULL*/)
32 : CStandAloneDialog(CCheckForUpdatesDlg::IDD, pParent)
33 , m_bShowInfo(FALSE)
34 , m_bVisible(FALSE)
36 m_sUpdateDownloadLink = _T("http://code.google.com/p/tortoisegit/downloads");
37 m_sUpdateChangeLogLink = _T("http://code.google.com/p/tortoisegit/wiki/ReleaseNotes");
40 CCheckForUpdatesDlg::~CCheckForUpdatesDlg()
44 void CCheckForUpdatesDlg::DoDataExchange(CDataExchange* pDX)
46 CStandAloneDialog::DoDataExchange(pDX);
47 DDX_Control(pDX, IDC_LINK, m_link);
48 DDX_Control(pDX, IDC_LINK_CHANGE_LOG, m_ChangeLogLink);
52 BEGIN_MESSAGE_MAP(CCheckForUpdatesDlg, CStandAloneDialog)
53 ON_STN_CLICKED(IDC_CHECKRESULT, OnStnClickedCheckresult)
54 ON_WM_TIMER()
55 ON_WM_WINDOWPOSCHANGING()
56 ON_WM_SETCURSOR()
57 END_MESSAGE_MAP()
59 BOOL CCheckForUpdatesDlg::OnInitDialog()
61 CStandAloneDialog::OnInitDialog();
63 CString temp;
64 temp.Format(IDS_CHECKNEWER_YOURVERSION, TSVN_VERMAJOR, TSVN_VERMINOR, TSVN_VERMICRO, TSVN_VERBUILD);
65 SetDlgItemText(IDC_YOURVERSION, temp);
67 DialogEnableWindow(IDOK, FALSE);
69 if (AfxBeginThread(CheckThreadEntry, this)==NULL)
71 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
74 SetTimer(100, 1000, NULL);
75 return TRUE;
78 void CCheckForUpdatesDlg::OnOK()
80 if (m_bThreadRunning)
81 return;
82 CStandAloneDialog::OnOK();
85 void CCheckForUpdatesDlg::OnCancel()
87 if (m_bThreadRunning)
88 return;
89 CStandAloneDialog::OnCancel();
92 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
94 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
97 UINT CCheckForUpdatesDlg::CheckThread()
99 m_bThreadRunning = TRUE;
101 CString temp;
102 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
104 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
105 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
106 CString sCheckURL = checkurluser;
107 if (sCheckURL.IsEmpty())
109 sCheckURL = checkurlmachine;
110 if (sCheckURL.IsEmpty())
111 sCheckURL = _T("http://code.google.com/p/tortoisegit/downloads/list");
113 CoInitialize(NULL);
114 HRESULT res = URLDownloadToFile(NULL, sCheckURL, tempfile, 0, NULL);
115 if (res == S_OK)
119 CStdioFile file(tempfile, CFile::modeRead | CFile::shareDenyWrite);
120 CString ver;
121 __int64 major,minor,micro,build;
122 major=minor=micro=build=0;
123 unsigned __int64 version=0;
125 if(file.GetLength()>100)
127 while(file.ReadString(ver))
129 int start;
130 while( (start=ver.Find(_T("TortoiseGit-"))) > 0 )
132 ver = ver.Mid(start+CString(_T("TortoiseGit-")).GetLength());
133 int x1,x2,x3,x4;
134 x1=_ttoi(ver)&0xFFFF;
135 ver = ver.Mid(ver.Find('.')+1);
136 x2=_ttoi(ver)&0xFFFF;
137 ver = ver.Mid(ver.Find('.')+1);
138 x3=_ttoi(ver)&0xFFFF;
139 ver = ver.Mid(ver.Find('.')+1);
140 x4=_ttoi(ver)&0xFFFF;
142 unsigned __int64 newversion;
143 newversion = x1;
144 newversion <<= 16;
145 newversion += x2;
146 newversion <<= 16;
147 newversion += x3;
148 newversion <<=16;
149 newversion += build;
151 if(newversion>version)
153 major=x1;
154 minor=x2;
155 micro=x3;
156 build=x4;
157 version = newversion;
162 }else if (file.ReadString(ver))
164 CString vertemp = ver;
165 major = _ttoi(vertemp);
166 vertemp = vertemp.Mid(vertemp.Find('.')+1);
167 minor = _ttoi(vertemp);
168 vertemp = vertemp.Mid(vertemp.Find('.')+1);
169 micro = _ttoi(vertemp);
170 vertemp = vertemp.Mid(vertemp.Find('.')+1);
171 build = _ttoi(vertemp);
172 version = (major<<48) + (minor<<32) + (micro<<16)+build;
176 BOOL bNewer = FALSE;
177 if (major > TSVN_VERMAJOR)
178 bNewer = TRUE;
179 else if ((minor > TSVN_VERMINOR)&&(major == TSVN_VERMAJOR))
180 bNewer = TRUE;
181 else if ((micro > TSVN_VERMICRO)&&(minor == TSVN_VERMINOR)&&(major == TSVN_VERMAJOR))
182 bNewer = TRUE;
183 else if ((build > TSVN_VERBUILD)&&(micro == TSVN_VERMICRO)&&(minor == TSVN_VERMINOR)&&(major == TSVN_VERMAJOR))
184 bNewer = TRUE;
186 if (version != 0)
188 ver.Format(_T("%d.%d.%d.%d"),major,minor,micro,build);
189 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)ver);
190 SetDlgItemText(IDC_CURRENTVERSION, temp);
191 temp.Format(_T("%d.%d.%d.%d"), TSVN_VERMAJOR, TSVN_VERMINOR, TSVN_VERMICRO, TSVN_VERBUILD);
194 if (version == 0)
196 temp.LoadString(IDS_CHECKNEWER_NETERROR);
197 SetDlgItemText(IDC_CHECKRESULT, temp);
199 else if (bNewer)
201 if(file.ReadString(temp) && !temp.IsEmpty())
202 { // Read the next line, it could contain a message for the user
203 CString tempLink;
204 if(file.ReadString(tempLink) && !tempLink.IsEmpty())
205 { // Read another line to find out the download link-URL, if any
206 m_sUpdateDownloadLink = tempLink;
210 else
212 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
214 SetDlgItemText(IDC_CHECKRESULT, temp);
215 m_bShowInfo = TRUE;
217 else
219 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
220 SetDlgItemText(IDC_CHECKRESULT, temp);
224 catch (CException * e)
226 e->Delete();
227 temp.LoadString(IDS_CHECKNEWER_NETERROR);
228 SetDlgItemText(IDC_CHECKRESULT, temp);
231 else
233 // Try to cache web page;
235 temp.LoadString(IDS_CHECKNEWER_NETERROR);
236 SetDlgItemText(IDC_CHECKRESULT, temp);
238 if (!m_sUpdateDownloadLink.IsEmpty())
240 m_link.ShowWindow(SW_SHOW);
241 m_link.SetURL(m_sUpdateDownloadLink);
243 if (!m_sUpdateDownloadLink.IsEmpty())
245 m_ChangeLogLink.ShowWindow(SW_SHOW);
246 m_ChangeLogLink.SetURL(m_sUpdateChangeLogLink);
249 DeleteFile(tempfile);
250 m_bThreadRunning = FALSE;
251 DialogEnableWindow(IDOK, TRUE);
252 return 0;
255 void CCheckForUpdatesDlg::OnStnClickedCheckresult()
257 // user clicked on the label, start the browser with our web page
258 HINSTANCE result = ShellExecute(NULL, _T("opennew"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
259 if ((UINT)result <= HINSTANCE_ERROR)
261 result = ShellExecute(NULL, _T("open"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
265 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
267 if (nIDEvent == 100)
269 if (m_bThreadRunning == FALSE)
271 if (m_bShowInfo)
273 m_bVisible = TRUE;
274 ShowWindow(SW_SHOWNORMAL);
276 else
278 EndDialog(0);
282 CStandAloneDialog::OnTimer(nIDEvent);
285 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
287 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
288 if (m_bVisible == FALSE)
289 lpwndpos->flags &= ~SWP_SHOWWINDOW;
292 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
294 if ((!m_sUpdateDownloadLink.IsEmpty())&&(pWnd)&&(pWnd == GetDlgItem(IDC_CHECKRESULT)))
296 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
297 SetCursor(hCur);
298 return TRUE;
301 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
302 SetCursor(hCur);
303 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);