Can run cleanup without using recycle bin and add dry run option
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blob1347bb08cac249c5751c16fcfc42e26fdb277ddb
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"
36 #include "UniCodeUtils.h"
38 // Link with Wintrust.lib
39 #pragma comment (lib, "wintrust")
41 #define WM_USER_DISPLAYSTATUS (WM_USER + 1)
42 #define WM_USER_ENDDOWNLOAD (WM_USER + 2)
43 #define WM_USER_FILLCHANGELOG (WM_USER + 3)
45 IMPLEMENT_DYNAMIC(CCheckForUpdatesDlg, CStandAloneDialog)
46 CCheckForUpdatesDlg::CCheckForUpdatesDlg(CWnd* pParent /*=NULL*/)
47 : CStandAloneDialog(CCheckForUpdatesDlg::IDD, pParent)
48 , m_bShowInfo(FALSE)
49 , m_bForce(FALSE)
50 , m_bVisible(FALSE)
51 , m_pDownloadThread(NULL)
53 m_sUpdateDownloadLink = _T("http://code.google.com/p/tortoisegit/wiki/Download?tm=2");
56 CCheckForUpdatesDlg::~CCheckForUpdatesDlg()
60 void CCheckForUpdatesDlg::DoDataExchange(CDataExchange* pDX)
62 CStandAloneDialog::DoDataExchange(pDX);
63 DDX_Control(pDX, IDC_LINK, m_link);
64 DDX_Control(pDX, IDC_PROGRESSBAR, m_progress);
65 DDX_Control(pDX, IDC_LIST_DOWNLOADS, m_ctrlFiles);
66 DDX_Control(pDX, IDC_BUTTON_UPDATE, m_ctrlUpdate);
67 DDX_Control(pDX, IDC_LOGMESSAGE, m_cLogMessage);
70 BEGIN_MESSAGE_MAP(CCheckForUpdatesDlg, CStandAloneDialog)
71 ON_STN_CLICKED(IDC_CHECKRESULT, OnStnClickedCheckresult)
72 ON_WM_TIMER()
73 ON_WM_WINDOWPOSCHANGING()
74 ON_WM_SETCURSOR()
75 ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnBnClickedButtonUpdate)
76 ON_MESSAGE(WM_USER_DISPLAYSTATUS, OnDisplayStatus)
77 ON_MESSAGE(WM_USER_ENDDOWNLOAD, OnEndDownload)
78 ON_MESSAGE(WM_USER_FILLCHANGELOG, OnFillChangelog)
79 END_MESSAGE_MAP()
81 BOOL CCheckForUpdatesDlg::OnInitDialog()
83 CStandAloneDialog::OnInitDialog();
84 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
86 CString temp;
87 temp.Format(IDS_CHECKNEWER_YOURVERSION, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
88 SetDlgItemText(IDC_YOURVERSION, temp);
90 DialogEnableWindow(IDOK, FALSE);
92 // hide download controls
93 m_ctrlFiles.ShowWindow(SW_HIDE);
94 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_HIDE);
95 RECT rectWindow, rectGroupDownloads, rectOKButton;
96 GetWindowRect(&rectWindow);
97 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
98 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
99 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
100 OffsetRect(&rectOKButton, 0, rectGroupDownloads.top - rectOKButton.top);
101 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
102 MoveWindow(&rectWindow);
103 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
104 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
106 temp.LoadString(IDS_STATUSLIST_COLFILE);
107 m_ctrlFiles.InsertColumn(0, temp, 0, -1);
108 m_ctrlFiles.InsertColumn(1, temp, 0, -1);
109 m_ctrlFiles.SetExtendedStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_CHECKBOXES);
110 m_ctrlFiles.SetColumnWidth(0, 350);
111 m_ctrlFiles.SetColumnWidth(1, 200);
113 ProjectProperties pp;
114 pp.SetCheckRe(_T("[Ii]ssues?:?(\\s*(,|and)?\\s*#?\\d+)+"));
115 pp.SetBugIDRe(_T("(\\d+)"));
116 pp.lProjectLanguage = -1;
117 pp.sUrl = _T("http://code.google.com/p/tortoisegit/issues/detail?id=%BUGID%");
118 m_cLogMessage.Init(pp);
119 m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
120 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
122 if (AfxBeginThread(CheckThreadEntry, this)==NULL)
124 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
127 SetTimer(100, 1000, NULL);
128 return TRUE;
131 void CCheckForUpdatesDlg::OnOK()
133 if (m_bThreadRunning || m_pDownloadThread != NULL)
134 return; // Don't exit while downloading
136 CStandAloneDialog::OnOK();
139 void CCheckForUpdatesDlg::OnCancel()
141 if (m_bThreadRunning || m_pDownloadThread != NULL)
142 return; // Don't exit while downloading
143 CStandAloneDialog::OnCancel();
146 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
148 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
151 UINT CCheckForUpdatesDlg::CheckThread()
153 m_bThreadRunning = TRUE;
155 CString temp;
156 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
158 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
159 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
160 CString sCheckURL = checkurluser;
161 if (sCheckURL.IsEmpty())
163 sCheckURL = checkurlmachine;
164 if (sCheckURL.IsEmpty())
166 bool checkPreview = false;
167 #if PREVIEW
168 checkPreview = true;
169 #else
170 CRegStdDWORD regCheckPreview(L"Software\\TortoiseGit\\VersionCheckPreview", FALSE);
171 if (DWORD(regCheckPreview))
172 checkPreview = true;
173 #endif
174 if (checkPreview)
175 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version-preview.txt");
176 else
177 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version.txt");
180 CoInitialize(NULL);
181 HRESULT res = URLDownloadToFile(NULL, sCheckURL, tempfile, 0, NULL);
182 if (res == S_OK)
186 CStdioFile file(tempfile, CFile::modeRead | CFile::shareDenyWrite);
187 CString ver;
188 unsigned int major,minor,micro,build;
189 major=minor=micro=build=0;
190 unsigned __int64 version=0;
192 if (file.ReadString(ver))
194 CString vertemp = ver;
195 // another versionstring for the filename can be provided after a semicolon
196 // this is needed for preview releases
197 int differentFilenamePos = vertemp.Find(_T(";"));
198 if (differentFilenamePos > 0)
200 vertemp = vertemp.Left(differentFilenamePos);
201 ver = ver.Mid(differentFilenamePos + 1);
204 major = _ttoi(vertemp);
205 vertemp = vertemp.Mid(vertemp.Find('.')+1);
206 minor = _ttoi(vertemp);
207 vertemp = vertemp.Mid(vertemp.Find('.')+1);
208 micro = _ttoi(vertemp);
209 vertemp = vertemp.Mid(vertemp.Find('.')+1);
210 build = _ttoi(vertemp);
211 version = major;
212 version <<= 16;
213 version += minor;
214 version <<= 16;
215 version += micro;
216 version <<= 16;
217 version += build;
221 BOOL bNewer = FALSE;
222 if (m_bForce)
223 bNewer = TRUE;
224 if (major > TGIT_VERMAJOR)
225 bNewer = TRUE;
226 else if ((minor > TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
227 bNewer = TRUE;
228 else if ((micro > TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
229 bNewer = TRUE;
230 else if ((build > TGIT_VERBUILD)&&(micro == TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
231 bNewer = TRUE;
233 if (version != 0)
235 CString version;
236 version.Format(_T("%d.%d.%d.%d"),major,minor,micro,build);
237 if (version != ver)
238 version += _T(" (") + ver + _T(")");
239 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)version);
240 SetDlgItemText(IDC_CURRENTVERSION, temp);
243 if (version == 0)
245 temp.LoadString(IDS_CHECKNEWER_NETERROR);
246 SetDlgItemText(IDC_CHECKRESULT, temp);
248 else if (bNewer)
250 if(file.ReadString(temp) && !temp.IsEmpty())
251 { // Read the next line, it could contain a message for the user
252 CString tempLink;
253 if(file.ReadString(tempLink) && !tempLink.IsEmpty())
254 { // Read another line to find out the download link-URL, if any
255 m_sUpdateDownloadLink = tempLink;
258 else
260 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
261 CString tempLink;
262 file.ReadString(tempLink);
264 SetDlgItemText(IDC_CHECKRESULT, temp);
265 m_bShowInfo = TRUE;
267 FillChangelog(file);
268 FillDownloads(file, ver);
270 // Show download controls
271 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
272 GetWindowRect(&rectWindow);
273 m_progress.GetWindowRect(&rectProgress);
274 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
275 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
276 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
277 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
278 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
279 MoveWindow(&rectWindow);
280 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
281 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
282 m_ctrlFiles.ShowWindow(SW_SHOW);
283 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
284 CenterWindow();
286 else
288 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
289 SetDlgItemText(IDC_CHECKRESULT, temp);
290 file.ReadString(temp);
291 file.ReadString(temp);
292 FillChangelog(file);
296 catch (CException * e)
298 e->Delete();
299 temp.LoadString(IDS_CHECKNEWER_NETERROR);
300 SetDlgItemText(IDC_CHECKRESULT, temp);
303 else
305 // Try to cache web page;
307 temp.LoadString(IDS_CHECKNEWER_NETERROR);
308 SetDlgItemText(IDC_CHECKRESULT, temp);
310 if (!m_sUpdateDownloadLink.IsEmpty())
312 m_link.ShowWindow(SW_SHOW);
313 m_link.SetURL(m_sUpdateDownloadLink);
316 DeleteFile(tempfile);
317 m_bThreadRunning = FALSE;
318 DialogEnableWindow(IDOK, TRUE);
319 return 0;
322 void CCheckForUpdatesDlg::FillDownloads(CStdioFile &file, CString version)
324 #if WIN64
325 const CString x86x64 = _T("64");
326 #else
327 const CString x86x64 = _T("32");
328 #endif
330 if (!file.ReadString(m_sFilesURL) || m_sFilesURL.IsEmpty())
331 m_sFilesURL = _T("http://tortoisegit.googlecode.com/files/");
333 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
334 CString filename;
335 filename.Format(_T("TortoiseGit-%s-%sbit.msi"), version, x86x64);
336 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
337 m_ctrlFiles.SetCheck(0 , TRUE);
339 std::vector<DWORD> m_installedLangs;
341 // set up the language selecting combobox
342 CString path = CPathUtils::GetAppParentDirectory();
343 path = path + _T("Languages\\");
344 CSimpleFileFind finder(path, _T("*.dll"));
345 while (finder.FindNextFileNoDirectories())
347 CString file = finder.GetFilePath();
348 CString filename = finder.GetFileName();
349 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
351 CString sVer = _T(STRPRODUCTVER);
352 sVer = sVer.Left(sVer.ReverseFind('.'));
353 CString sFileVer = CPathUtils::GetVersionFromFile(file);
354 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
355 CString sLoc = filename.Mid(12);
356 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
357 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
358 continue;
359 DWORD loc = _tstoi(filename.Mid(12));
360 m_installedLangs.push_back(loc);
365 CString langs;
366 while (file.ReadString(langs) && !langs.IsEmpty())
368 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
370 DWORD loc = _tstoi(langs.Mid(0, 4));
371 TCHAR buf[MAX_PATH];
372 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
373 CString sLang2(buf);
374 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
375 if (buf[0])
377 sLang2 += _T(" (");
378 sLang2 += buf;
379 sLang2 += _T(")");
382 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), sLang);
383 m_ctrlFiles.SetItemText(pos, 1, sLang2);
385 CString filename;
386 filename.Format(_T("TortoiseGit-LanguagePack-%s-%sbit-%s.msi"), version, x86x64, langs.Mid(5));
387 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
389 if (std::find(m_installedLangs.begin(), m_installedLangs.end(), loc) != m_installedLangs.end())
390 m_ctrlFiles.SetCheck(pos , TRUE);
392 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
395 void CCheckForUpdatesDlg::FillChangelog(CStdioFile &file)
397 CString sChangelogURL;
398 if (!file.ReadString(sChangelogURL) || sChangelogURL.IsEmpty())
399 sChangelogURL = _T("http://tortoisegit.googlecode.com/git/src/Changelog.txt");
401 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
402 HRESULT res = URLDownloadToFile(NULL, sChangelogURL, tempchangelogfile, 0, NULL);
403 if (SUCCEEDED(res))
405 CString temp;
406 CStdioFile file(tempchangelogfile, CFile::modeRead|CFile::typeText);
407 CString str;
408 bool first = true;
409 while (file.ReadString(str))
411 if (first)
413 first = false;
414 if (str.GetLength() > 2 && str.GetAt(0) == 0xEF && str.GetAt(1) == 0xBB)
416 if (str.GetAt(2) == 0xBF)
418 str = str.Mid(3);
420 else
422 str = str.Mid(2);
426 str = CUnicodeUtils::GetUnicode(CStringA(str), CP_UTF8);
427 temp += str + _T("\n");
429 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(temp.GetBuffer()));
431 else
432 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not load changelog.")));
435 void CCheckForUpdatesDlg::OnStnClickedCheckresult()
437 // user clicked on the label, start the browser with our web page
438 HINSTANCE result = ShellExecute(NULL, _T("opennew"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
439 if ((UINT)result <= HINSTANCE_ERROR)
441 result = ShellExecute(NULL, _T("open"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
445 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
447 if (nIDEvent == 100)
449 if (m_bThreadRunning == FALSE)
451 if (m_bShowInfo)
453 m_bVisible = TRUE;
454 ShowWindow(SW_SHOWNORMAL);
456 else
458 EndDialog(0);
462 CStandAloneDialog::OnTimer(nIDEvent);
465 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
467 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
468 if (m_bVisible == FALSE)
469 lpwndpos->flags &= ~SWP_SHOWWINDOW;
472 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
474 if ((!m_sUpdateDownloadLink.IsEmpty())&&(pWnd)&&(pWnd == GetDlgItem(IDC_CHECKRESULT)))
476 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
477 SetCursor(hCur);
478 return TRUE;
481 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
482 SetCursor(hCur);
483 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
486 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
488 CString title;
489 m_ctrlUpdate.GetWindowText(title);
490 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
492 bool isOneSelected = false;
493 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
495 if (m_ctrlFiles.GetCheck(i))
497 isOneSelected = true;
498 break;
501 if (!isOneSelected)
502 return;
504 m_eventStop.ResetEvent();
506 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
507 if (m_pDownloadThread != NULL)
509 m_pDownloadThread->m_bAutoDelete = FALSE;
510 m_pDownloadThread->ResumeThread();
512 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
514 else
516 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
519 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
521 // Abort
522 m_eventStop.SetEvent();
524 else
526 CString folder = GetDownloadsDirectory();
527 if (m_ctrlUpdate.GetCurrentEntry() == 0)
529 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
531 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
532 if (m_ctrlFiles.GetCheck(i) == TRUE)
533 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
535 CStandAloneDialog::OnOK();
537 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
539 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
542 m_ctrlUpdate.SetCurrentEntry(0);
546 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
548 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
551 bool CCheckForUpdatesDlg::Download(CString filename)
553 CString destFilename = GetDownloadsDirectory() + filename;
554 if (PathFileExists(destFilename))
556 if (VerifySignature(destFilename))
557 return true;
558 else
559 DeleteFile(destFilename);
562 CBSCallbackImpl bsc(this->GetSafeHwnd(), m_eventStop);
564 CString url = m_sFilesURL + filename;
566 m_progress.SetRange32(0, 1);
567 m_progress.SetPos(0);
568 m_progress.ShowWindow(SW_SHOW);
570 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
571 HRESULT res = URLDownloadToFile(NULL, url, tempfile, 0, &bsc);
572 if (res == S_OK)
574 if (VerifySignature(tempfile))
576 if (PathFileExists(destFilename))
577 DeleteFile(destFilename);
578 MoveFile(tempfile, destFilename);
579 return true;
582 return false;
585 UINT CCheckForUpdatesDlg::DownloadThread()
587 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
589 BOOL result = TRUE;
590 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
592 m_ctrlFiles.EnsureVisible(i, FALSE);
593 CRect rect;
594 m_ctrlFiles.GetItemRect(i, &rect, LVIR_BOUNDS);
595 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
596 if (m_ctrlFiles.GetCheck(i) == TRUE)
598 data->m_status = CUpdateListCtrl::STATUS_DOWNLOADING;
599 m_ctrlFiles.InvalidateRect(rect);
600 if (Download(data->m_filename))
601 data->m_status = CUpdateListCtrl::STATUS_SUCCESS;
602 else
604 data->m_status = CUpdateListCtrl::STATUS_FAIL;
605 result = FALSE;
608 else
609 data->m_status = CUpdateListCtrl::STATUS_IGNORE;
610 m_ctrlFiles.InvalidateRect(rect);
613 ::PostMessage(GetSafeHwnd(), WM_USER_ENDDOWNLOAD, 0, 0);
615 return result;
618 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
620 ASSERT(m_pDownloadThread != NULL);
622 // wait until the thread terminates
623 DWORD dwExitCode;
624 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
625 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
627 // make sure we always have the correct exit code
628 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
630 delete m_pDownloadThread;
631 m_pDownloadThread = NULL;
633 m_progress.ShowWindow(SW_HIDE);
635 if (dwExitCode == TRUE)
637 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
638 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
639 m_ctrlUpdate.Invalidate();
641 else
643 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
644 CMessageBox::Show(NULL, IDS_ERR_FAILEDUPDATEDOWNLOAD, IDS_APPNAME, MB_ICONERROR);
647 return 0;
650 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
652 ASSERT(lParam != NULL);
654 TCHAR * changelog = reinterpret_cast<TCHAR *>(lParam);
655 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
656 m_cLogMessage.SetText(changelog);
657 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
658 m_cLogMessage.Call(SCI_GOTOPOS, 0);
660 return 0;
663 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
665 CString folder;
667 if (SysInfo::Instance().IsVistaOrLater())
669 CAutoLibrary hShell = ::LoadLibrary(_T("shell32.dll"));
670 if (hShell)
672 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
673 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
674 if (pfnSHGetKnownFolderPath)
676 wchar_t * wcharPtr = 0;
677 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
678 if (SUCCEEDED(hr))
680 folder = wcharPtr;
681 CoTaskMemFree(static_cast<void*>(wcharPtr));
682 return folder.TrimRight(_T("\\")) + _T("\\");
688 TCHAR szPath[MAX_PATH];
689 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
690 folder = szPath;
691 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
692 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
693 return downloads;
695 return folder;
698 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
700 const DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<DOWNLOADSTATUS *>(lParam);
701 if (pDownloadStatus != NULL)
703 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(DOWNLOADSTATUS)));
705 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
706 m_progress.SetPos(pDownloadStatus->ulProgress);
709 return 0;
712 CBSCallbackImpl::CBSCallbackImpl(HWND hWnd, HANDLE hEventStop)
714 m_hWnd = hWnd;
715 m_hEventStop = hEventStop;
716 m_ulObjRefCount = 1;
719 // IUnknown
720 STDMETHODIMP CBSCallbackImpl::QueryInterface(REFIID riid, void **ppvObject)
722 *ppvObject = NULL;
724 // IUnknown
725 if (::IsEqualIID(riid, __uuidof(IUnknown)))
726 *ppvObject = this;
728 // IBindStatusCallback
729 else if (::IsEqualIID(riid, __uuidof(IBindStatusCallback)))
730 *ppvObject = static_cast<IBindStatusCallback *>(this);
732 if (*ppvObject)
734 (*reinterpret_cast<LPUNKNOWN *>(ppvObject))->AddRef();
735 return S_OK;
738 return E_NOINTERFACE;
741 STDMETHODIMP_(ULONG) CBSCallbackImpl::AddRef()
743 return ++m_ulObjRefCount;
746 STDMETHODIMP_(ULONG) CBSCallbackImpl::Release()
748 return --m_ulObjRefCount;
751 // IBindStatusCallback
752 STDMETHODIMP CBSCallbackImpl::OnStartBinding(DWORD, IBinding *)
754 return S_OK;
757 STDMETHODIMP CBSCallbackImpl::GetPriority(LONG *)
759 return E_NOTIMPL;
761 STDMETHODIMP CBSCallbackImpl::OnLowResource(DWORD)
763 return S_OK;
766 STDMETHODIMP CBSCallbackImpl::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR /* szStatusText */)
768 TRACE(_T("IBindStatusCallback::OnProgress\n"));
769 TRACE(_T("ulProgress: %lu, ulProgressMax: %lu\n"), ulProgress, ulProgressMax);
770 UNREFERENCED_PARAMETER(ulStatusCode);
771 TRACE(_T("ulStatusCode: %lu "), ulStatusCode);
773 if (m_hWnd != NULL)
775 // inform the dialog box to display current status,
776 // don't use PostMessage
777 CCheckForUpdatesDlg::DOWNLOADSTATUS downloadStatus = { ulProgress, ulProgressMax };
778 ::SendMessage(m_hWnd, WM_USER_DISPLAYSTATUS, 0, reinterpret_cast<LPARAM>(&downloadStatus));
781 if (m_hEventStop != NULL && ::WaitForSingleObject(m_hEventStop, 0) == WAIT_OBJECT_0)
783 return E_ABORT; // canceled by the user
786 return S_OK;
789 STDMETHODIMP CBSCallbackImpl::OnStopBinding(HRESULT, LPCWSTR)
791 return S_OK;
794 STDMETHODIMP CBSCallbackImpl::GetBindInfo(DWORD *, BINDINFO *)
796 return S_OK;
799 STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD, FORMATETC *, STGMEDIUM *)
801 return S_OK;
804 STDMETHODIMP CBSCallbackImpl::OnObjectAvailable(REFIID, IUnknown *)
806 return S_OK;
809 BOOL CCheckForUpdatesDlg::VerifySignature(CString fileName)
811 WINTRUST_FILE_INFO fileInfo;
812 memset(&fileInfo, 0, sizeof(fileInfo));
813 fileInfo.cbStruct = sizeof(WINTRUST_FILE_INFO);
814 fileInfo.pcwszFilePath = fileName;
816 WINTRUST_DATA data;
817 memset(&data, 0, sizeof(data));
818 data.cbStruct = sizeof(data);
820 data.dwUIChoice = WTD_UI_NONE;
821 data.fdwRevocationChecks = WTD_REVOKE_NONE;
822 data.dwUnionChoice = WTD_CHOICE_FILE;
823 data.pFile = &fileInfo;
825 GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
827 return (WinVerifyTrust(NULL, &policyGUID, &data) == ERROR_SUCCESS);