Can also refresh Rebase dialog by double clicking to delete conflict file
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blob32f29f8472c1ab27f66f1d185f5627e8fb2a6d57
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2012 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseProc.h"
22 #include "LoglistCommonResource.h"
23 #include "..\version.h"
24 #include "MessageBox.h"
25 #include ".\checkforupdatesdlg.h"
26 #include "Registry.h"
27 #include "AppUtils.h"
28 #include "TempFile.h"
29 #include <wintrust.h>
30 #include <Softpub.h>
31 #include "SmartHandle.h"
32 #include "SysInfo.h"
33 #include "PathUtils.h"
34 #include "DirFileEnum.h"
35 #include "ProjectProperties.h"
37 // Link with Wintrust.lib
38 #pragma comment (lib, "wintrust")
40 #define WM_USER_DISPLAYSTATUS (WM_USER + 1)
41 #define WM_USER_ENDDOWNLOAD (WM_USER + 2)
42 #define WM_USER_FILLCHANGELOG (WM_USER + 3)
44 IMPLEMENT_DYNAMIC(CCheckForUpdatesDlg, CStandAloneDialog)
45 CCheckForUpdatesDlg::CCheckForUpdatesDlg(CWnd* pParent /*=NULL*/)
46 : CStandAloneDialog(CCheckForUpdatesDlg::IDD, pParent)
47 , m_bShowInfo(FALSE)
48 , m_bVisible(FALSE)
49 , m_pDownloadThread(NULL)
51 m_sUpdateDownloadLink = _T("http://code.google.com/p/tortoisegit/wiki/Download?tm=2");
54 CCheckForUpdatesDlg::~CCheckForUpdatesDlg()
58 void CCheckForUpdatesDlg::DoDataExchange(CDataExchange* pDX)
60 CStandAloneDialog::DoDataExchange(pDX);
61 DDX_Control(pDX, IDC_LINK, m_link);
62 DDX_Control(pDX, IDC_PROGRESSBAR, m_progress);
63 DDX_Control(pDX, IDC_LIST_DOWNLOADS, m_ctrlFiles);
64 DDX_Control(pDX, IDC_BUTTON_UPDATE, m_ctrlUpdate);
65 DDX_Control(pDX, IDC_LOGMESSAGE, m_cLogMessage);
68 BEGIN_MESSAGE_MAP(CCheckForUpdatesDlg, CStandAloneDialog)
69 ON_STN_CLICKED(IDC_CHECKRESULT, OnStnClickedCheckresult)
70 ON_WM_TIMER()
71 ON_WM_WINDOWPOSCHANGING()
72 ON_WM_SETCURSOR()
73 ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnBnClickedButtonUpdate)
74 ON_MESSAGE(WM_USER_DISPLAYSTATUS, OnDisplayStatus)
75 ON_MESSAGE(WM_USER_ENDDOWNLOAD, OnEndDownload)
76 ON_MESSAGE(WM_USER_FILLCHANGELOG, OnFillChangelog)
77 END_MESSAGE_MAP()
79 BOOL CCheckForUpdatesDlg::OnInitDialog()
81 CStandAloneDialog::OnInitDialog();
82 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
84 CString temp;
85 temp.Format(IDS_CHECKNEWER_YOURVERSION, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
86 SetDlgItemText(IDC_YOURVERSION, temp);
88 DialogEnableWindow(IDOK, FALSE);
90 // hide download controls
91 m_ctrlFiles.ShowWindow(SW_HIDE);
92 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_HIDE);
93 RECT rectWindow, rectGroupDownloads, rectOKButton;
94 GetWindowRect(&rectWindow);
95 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
96 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
97 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
98 OffsetRect(&rectOKButton, 0, rectGroupDownloads.top - rectOKButton.top);
99 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
100 MoveWindow(&rectWindow);
101 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
102 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
104 temp.LoadString(IDS_STATUSLIST_COLFILE);
105 m_ctrlFiles.InsertColumn(0, temp, 0, -1);
106 m_ctrlFiles.InsertColumn(1, temp, 0, -1);
107 m_ctrlFiles.SetExtendedStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_CHECKBOXES);
108 m_ctrlFiles.SetColumnWidth(0, 350);
109 m_ctrlFiles.SetColumnWidth(1, 200);
111 ProjectProperties pp;
112 pp.SetCheckRe(_T("[Ii]ssues?:?(\\s*(,|and)?\\s*#?\\d+)+"));
113 pp.SetBugIDRe(_T("(\\d+)"));
114 pp.lProjectLanguage = 0;
115 pp.sUrl = _T("http://code.google.com/p/tortoisegit/issues/detail?id=%BUGID%");
116 m_cLogMessage.Init(pp);
117 m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
118 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
120 if (AfxBeginThread(CheckThreadEntry, this)==NULL)
122 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
125 SetTimer(100, 1000, NULL);
126 return TRUE;
129 void CCheckForUpdatesDlg::OnOK()
131 if (m_bThreadRunning || m_pDownloadThread != NULL)
132 return; // Don't exit while downloading
134 CStandAloneDialog::OnOK();
137 void CCheckForUpdatesDlg::OnCancel()
139 if (m_bThreadRunning || m_pDownloadThread != NULL)
140 return; // Don't exit while downloading
141 CStandAloneDialog::OnCancel();
144 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
146 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
149 UINT CCheckForUpdatesDlg::CheckThread()
151 m_bThreadRunning = TRUE;
153 CString temp;
154 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
156 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
157 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
158 CString sCheckURL = checkurluser;
159 if (sCheckURL.IsEmpty())
161 sCheckURL = checkurlmachine;
162 if (sCheckURL.IsEmpty())
164 bool checkPreview = false;
165 #if PREVIEW
166 checkPreview = true;
167 #else
168 CRegStdDWORD regCheckPreview(L"Software\\TortoiseGit\\VersionCheckPreview", FALSE);
169 if (DWORD(regCheckPreview))
170 checkPreview = true;
171 #endif
172 if (checkPreview)
173 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version-preview.txt");
174 else
175 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version.txt");
178 CoInitialize(NULL);
179 HRESULT res = URLDownloadToFile(NULL, sCheckURL, tempfile, 0, NULL);
180 if (res == S_OK)
184 CStdioFile file(tempfile, CFile::modeRead | CFile::shareDenyWrite);
185 CString ver;
186 unsigned int major,minor,micro,build;
187 major=minor=micro=build=0;
188 unsigned __int64 version=0;
190 if (file.ReadString(ver))
192 CString vertemp = ver;
193 // another versionstring for the filename can be provided after a semicolon
194 // this is needed for preview releases
195 int differentFilenamePos = vertemp.Find(_T(";"));
196 if (differentFilenamePos > 0)
198 vertemp = vertemp.Left(differentFilenamePos);
199 ver = ver.Mid(differentFilenamePos + 1);
202 major = _ttoi(vertemp);
203 vertemp = vertemp.Mid(vertemp.Find('.')+1);
204 minor = _ttoi(vertemp);
205 vertemp = vertemp.Mid(vertemp.Find('.')+1);
206 micro = _ttoi(vertemp);
207 vertemp = vertemp.Mid(vertemp.Find('.')+1);
208 build = _ttoi(vertemp);
209 version = major;
210 version <<= 16;
211 version += minor;
212 version <<= 16;
213 version += micro;
214 version <<= 16;
215 version += build;
219 BOOL bNewer = FALSE;
220 if (major > TGIT_VERMAJOR)
221 bNewer = TRUE;
222 else if ((minor > TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
223 bNewer = TRUE;
224 else if ((micro > TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
225 bNewer = TRUE;
226 else if ((build > TGIT_VERBUILD)&&(micro == TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
227 bNewer = TRUE;
229 if (version != 0)
231 CString version;
232 version.Format(_T("%d.%d.%d.%d"),major,minor,micro,build);
233 if (version != ver)
234 version += _T(" (") + ver + _T(")");
235 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)version);
236 SetDlgItemText(IDC_CURRENTVERSION, temp);
239 if (version == 0)
241 temp.LoadString(IDS_CHECKNEWER_NETERROR);
242 SetDlgItemText(IDC_CHECKRESULT, temp);
244 else if (bNewer)
246 if(file.ReadString(temp) && !temp.IsEmpty())
247 { // Read the next line, it could contain a message for the user
248 CString tempLink;
249 if(file.ReadString(tempLink) && !tempLink.IsEmpty())
250 { // Read another line to find out the download link-URL, if any
251 m_sUpdateDownloadLink = tempLink;
254 else
256 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
257 CString tempLink;
258 file.ReadString(tempLink);
260 SetDlgItemText(IDC_CHECKRESULT, temp);
261 m_bShowInfo = TRUE;
263 FillChangelog(file);
264 FillDownloads(file, ver);
266 // Show download controls
267 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
268 GetWindowRect(&rectWindow);
269 m_progress.GetWindowRect(&rectProgress);
270 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
271 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
272 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
273 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
274 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
275 MoveWindow(&rectWindow);
276 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
277 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
278 m_ctrlFiles.ShowWindow(SW_SHOW);
279 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
280 CenterWindow();
282 else
284 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
285 SetDlgItemText(IDC_CHECKRESULT, temp);
286 file.ReadString(temp);
287 file.ReadString(temp);
288 FillChangelog(file);
292 catch (CException * e)
294 e->Delete();
295 temp.LoadString(IDS_CHECKNEWER_NETERROR);
296 SetDlgItemText(IDC_CHECKRESULT, temp);
299 else
301 // Try to cache web page;
303 temp.LoadString(IDS_CHECKNEWER_NETERROR);
304 SetDlgItemText(IDC_CHECKRESULT, temp);
306 if (!m_sUpdateDownloadLink.IsEmpty())
308 m_link.ShowWindow(SW_SHOW);
309 m_link.SetURL(m_sUpdateDownloadLink);
312 DeleteFile(tempfile);
313 m_bThreadRunning = FALSE;
314 DialogEnableWindow(IDOK, TRUE);
315 return 0;
318 void CCheckForUpdatesDlg::FillDownloads(CStdioFile &file, CString version)
320 #if WIN64
321 const CString x86x64 = _T("64");
322 #else
323 const CString x86x64 = _T("32");
324 #endif
326 if (!file.ReadString(m_sFilesURL) || m_sFilesURL.IsEmpty())
327 m_sFilesURL = _T("http://tortoisegit.googlecode.com/files/");
329 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
330 CString filename;
331 filename.Format(_T("TortoiseGit-%s-%sbit.msi"), version, x86x64);
332 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
333 m_ctrlFiles.SetCheck(0 , TRUE);
335 std::vector<DWORD> m_installedLangs;
337 // set up the language selecting combobox
338 CString path = CPathUtils::GetAppParentDirectory();
339 path = path + _T("Languages\\");
340 CSimpleFileFind finder(path, _T("*.dll"));
341 while (finder.FindNextFileNoDirectories())
343 CString file = finder.GetFilePath();
344 CString filename = finder.GetFileName();
345 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
347 CString sVer = _T(STRPRODUCTVER);
348 sVer = sVer.Left(sVer.ReverseFind(','));
349 CString sFileVer = CPathUtils::GetVersionFromFile(file);
350 sFileVer = sFileVer.Left(sFileVer.ReverseFind(','));
351 CString sLoc = filename.Mid(12);
352 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
353 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
354 continue;
355 DWORD loc = _tstoi(filename.Mid(12));
356 m_installedLangs.push_back(loc);
361 CString langs;
362 while (file.ReadString(langs) && !langs.IsEmpty())
364 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
366 DWORD loc = _tstoi(langs.Mid(0, 4));
367 TCHAR buf[MAX_PATH];
368 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
369 CString sLang2(buf);
370 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
371 if (buf[0])
373 sLang2 += _T(" (");
374 sLang2 += buf;
375 sLang2 += _T(")");
378 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), sLang);
379 m_ctrlFiles.SetItemText(pos, 1, sLang2);
381 CString filename;
382 filename.Format(_T("TortoiseGit-LanguagePack-%s-%sbit-%s.msi"), version, x86x64, langs.Mid(5));
383 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
385 if (std::find(m_installedLangs.begin(), m_installedLangs.end(), loc) != m_installedLangs.end())
386 m_ctrlFiles.SetCheck(pos , TRUE);
388 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
391 void CCheckForUpdatesDlg::FillChangelog(CStdioFile &file)
393 CString sChangelogURL;
394 if (!file.ReadString(sChangelogURL) || sChangelogURL.IsEmpty())
395 sChangelogURL = _T("http://tortoisegit.googlecode.com/git/src/Changelog.txt");
397 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
398 HRESULT res = URLDownloadToFile(NULL, sChangelogURL, tempchangelogfile, 0, NULL);
399 if (SUCCEEDED(res))
401 CString temp;
402 CStdioFile file(tempchangelogfile, CFile::modeRead|CFile::typeText);
403 CString str;
404 while (file.ReadString(str))
406 temp += str + _T("\n");
408 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(temp.GetBuffer()));
410 else
411 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not load changelog.")));
414 void CCheckForUpdatesDlg::OnStnClickedCheckresult()
416 // user clicked on the label, start the browser with our web page
417 HINSTANCE result = ShellExecute(NULL, _T("opennew"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
418 if ((UINT)result <= HINSTANCE_ERROR)
420 result = ShellExecute(NULL, _T("open"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
424 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
426 if (nIDEvent == 100)
428 if (m_bThreadRunning == FALSE)
430 if (m_bShowInfo)
432 m_bVisible = TRUE;
433 ShowWindow(SW_SHOWNORMAL);
435 else
437 EndDialog(0);
441 CStandAloneDialog::OnTimer(nIDEvent);
444 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
446 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
447 if (m_bVisible == FALSE)
448 lpwndpos->flags &= ~SWP_SHOWWINDOW;
451 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
453 if ((!m_sUpdateDownloadLink.IsEmpty())&&(pWnd)&&(pWnd == GetDlgItem(IDC_CHECKRESULT)))
455 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
456 SetCursor(hCur);
457 return TRUE;
460 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
461 SetCursor(hCur);
462 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
465 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
467 CString title;
468 m_ctrlUpdate.GetWindowText(title);
469 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
471 bool isOneSelected = false;
472 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
474 if (m_ctrlFiles.GetCheck(i))
476 isOneSelected = true;
477 break;
480 if (!isOneSelected)
481 return;
483 m_eventStop.ResetEvent();
485 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
486 if (m_pDownloadThread != NULL)
488 m_pDownloadThread->m_bAutoDelete = FALSE;
489 m_pDownloadThread->ResumeThread();
491 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
493 else
495 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
498 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
500 // Abort
501 m_eventStop.SetEvent();
503 else
505 CString folder = GetDownloadsDirectory();
506 if (m_ctrlUpdate.GetCurrentEntry() == 0)
508 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
510 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
511 if (m_ctrlFiles.GetCheck(i) == TRUE)
512 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
514 CStandAloneDialog::OnOK();
516 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
518 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
521 m_ctrlUpdate.SetCurrentEntry(0);
525 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
527 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
530 bool CCheckForUpdatesDlg::Download(CString filename)
532 CString destFilename = GetDownloadsDirectory() + filename;
533 if (PathFileExists(destFilename))
535 if (VerifySignature(destFilename))
536 return true;
537 else
538 DeleteFile(destFilename);
541 CBSCallbackImpl bsc(this->GetSafeHwnd(), m_eventStop);
543 CString url = m_sFilesURL + filename;
545 m_progress.SetRange32(0, 1);
546 m_progress.SetPos(0);
547 m_progress.ShowWindow(SW_SHOW);
549 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
550 HRESULT res = URLDownloadToFile(NULL, url, tempfile, 0, &bsc);
551 if (res == S_OK)
553 if (VerifySignature(tempfile))
555 if (PathFileExists(destFilename))
556 DeleteFile(destFilename);
557 MoveFile(tempfile, destFilename);
558 return true;
561 return false;
564 UINT CCheckForUpdatesDlg::DownloadThread()
566 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
568 BOOL result = TRUE;
569 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
571 m_ctrlFiles.EnsureVisible(i, FALSE);
572 CRect rect;
573 m_ctrlFiles.GetItemRect(i, &rect, LVIR_BOUNDS);
574 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
575 if (m_ctrlFiles.GetCheck(i) == TRUE)
577 data->m_status = CUpdateListCtrl::STATUS_DOWNLOADING;
578 m_ctrlFiles.InvalidateRect(rect);
579 if (Download(data->m_filename))
580 data->m_status = CUpdateListCtrl::STATUS_SUCCESS;
581 else
583 data->m_status = CUpdateListCtrl::STATUS_FAIL;
584 result = FALSE;
587 else
588 data->m_status = CUpdateListCtrl::STATUS_IGNORE;
589 m_ctrlFiles.InvalidateRect(rect);
592 ::PostMessage(GetSafeHwnd(), WM_USER_ENDDOWNLOAD, 0, 0);
594 return result;
597 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
599 ASSERT(m_pDownloadThread != NULL);
601 // wait until the thread terminates
602 DWORD dwExitCode;
603 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
604 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
606 // make sure we always have the correct exit code
607 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
609 delete m_pDownloadThread;
610 m_pDownloadThread = NULL;
612 m_progress.ShowWindow(SW_HIDE);
614 if (dwExitCode == TRUE)
616 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
617 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
618 m_ctrlUpdate.Invalidate();
620 else
622 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
623 CMessageBox::Show(NULL, IDS_ERR_FAILEDUPDATEDOWNLOAD, IDS_APPNAME, MB_ICONERROR);
626 return 0;
629 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
631 ASSERT(lParam != NULL);
633 TCHAR * changelog = reinterpret_cast<TCHAR *>(lParam);
634 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
635 m_cLogMessage.SetText(changelog);
636 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
637 m_cLogMessage.Call(SCI_GOTOPOS, 0);
639 return 0;
642 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
644 CString folder;
646 if (SysInfo::Instance().IsVistaOrLater())
648 CAutoLibrary hShell = ::LoadLibrary(_T("shell32.dll"));
649 if (hShell)
651 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
652 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
653 if (pfnSHGetKnownFolderPath)
655 wchar_t * wcharPtr = 0;
656 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
657 if (SUCCEEDED(hr))
659 folder = wcharPtr;
660 CoTaskMemFree(static_cast<void*>(wcharPtr));
661 return folder.TrimRight(_T("\\")) + _T("\\");
667 TCHAR szPath[MAX_PATH];
668 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
669 folder = szPath;
670 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
671 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
672 return downloads;
674 return folder;
677 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
679 const DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<DOWNLOADSTATUS *>(lParam);
680 if (pDownloadStatus != NULL)
682 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(DOWNLOADSTATUS)));
684 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
685 m_progress.SetPos(pDownloadStatus->ulProgress);
688 return 0;
691 CBSCallbackImpl::CBSCallbackImpl(HWND hWnd, HANDLE hEventStop)
693 m_hWnd = hWnd;
694 m_hEventStop = hEventStop;
695 m_ulObjRefCount = 1;
698 // IUnknown
699 STDMETHODIMP CBSCallbackImpl::QueryInterface(REFIID riid, void **ppvObject)
701 *ppvObject = NULL;
703 // IUnknown
704 if (::IsEqualIID(riid, __uuidof(IUnknown)))
705 *ppvObject = this;
707 // IBindStatusCallback
708 else if (::IsEqualIID(riid, __uuidof(IBindStatusCallback)))
709 *ppvObject = static_cast<IBindStatusCallback *>(this);
711 if (*ppvObject)
713 (*reinterpret_cast<LPUNKNOWN *>(ppvObject))->AddRef();
714 return S_OK;
717 return E_NOINTERFACE;
720 STDMETHODIMP_(ULONG) CBSCallbackImpl::AddRef()
722 return ++m_ulObjRefCount;
725 STDMETHODIMP_(ULONG) CBSCallbackImpl::Release()
727 return --m_ulObjRefCount;
730 // IBindStatusCallback
731 STDMETHODIMP CBSCallbackImpl::OnStartBinding(DWORD, IBinding *)
733 return S_OK;
736 STDMETHODIMP CBSCallbackImpl::GetPriority(LONG *)
738 return E_NOTIMPL;
740 STDMETHODIMP CBSCallbackImpl::OnLowResource(DWORD)
742 return S_OK;
745 STDMETHODIMP CBSCallbackImpl::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR /* szStatusText */)
747 TRACE(_T("IBindStatusCallback::OnProgress\n"));
748 TRACE(_T("ulProgress: %lu, ulProgressMax: %lu\n"), ulProgress, ulProgressMax);
749 UNREFERENCED_PARAMETER(ulStatusCode);
750 TRACE(_T("ulStatusCode: %lu "), ulStatusCode);
752 if (m_hWnd != NULL)
754 // inform the dialog box to display current status,
755 // don't use PostMessage
756 CCheckForUpdatesDlg::DOWNLOADSTATUS downloadStatus = { ulProgress, ulProgressMax };
757 ::SendMessage(m_hWnd, WM_USER_DISPLAYSTATUS, 0, reinterpret_cast<LPARAM>(&downloadStatus));
760 if (m_hEventStop != NULL && ::WaitForSingleObject(m_hEventStop, 0) == WAIT_OBJECT_0)
762 return E_ABORT; // canceled by the user
765 return S_OK;
768 STDMETHODIMP CBSCallbackImpl::OnStopBinding(HRESULT, LPCWSTR)
770 return S_OK;
773 STDMETHODIMP CBSCallbackImpl::GetBindInfo(DWORD *, BINDINFO *)
775 return S_OK;
778 STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD, FORMATETC *, STGMEDIUM *)
780 return S_OK;
783 STDMETHODIMP CBSCallbackImpl::OnObjectAvailable(REFIID, IUnknown *)
785 return S_OK;
788 BOOL CCheckForUpdatesDlg::VerifySignature(CString fileName)
790 WINTRUST_FILE_INFO fileInfo;
791 memset(&fileInfo, 0, sizeof(fileInfo));
792 fileInfo.cbStruct = sizeof(WINTRUST_FILE_INFO);
793 fileInfo.pcwszFilePath = fileName;
795 WINTRUST_DATA data;
796 memset(&data, 0, sizeof(data));
797 data.cbStruct = sizeof(data);
799 data.dwUIChoice = WTD_UI_NONE;
800 data.fdwRevocationChecks = WTD_REVOKE_NONE;
801 data.dwUnionChoice = WTD_CHOICE_FILE;
802 data.pFile = &fileInfo;
804 GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
806 return (WinVerifyTrust(NULL, &policyGUID, &data) == ERROR_SUCCESS);