Allow to reset filter easily
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blob8218f20c65f458808c2cb70e7c3c866d0b9bed19
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_bVisible(FALSE)
50 , m_pDownloadThread(NULL)
52 m_sUpdateDownloadLink = _T("http://code.google.com/p/tortoisegit/wiki/Download?tm=2");
55 CCheckForUpdatesDlg::~CCheckForUpdatesDlg()
59 void CCheckForUpdatesDlg::DoDataExchange(CDataExchange* pDX)
61 CStandAloneDialog::DoDataExchange(pDX);
62 DDX_Control(pDX, IDC_LINK, m_link);
63 DDX_Control(pDX, IDC_PROGRESSBAR, m_progress);
64 DDX_Control(pDX, IDC_LIST_DOWNLOADS, m_ctrlFiles);
65 DDX_Control(pDX, IDC_BUTTON_UPDATE, m_ctrlUpdate);
66 DDX_Control(pDX, IDC_LOGMESSAGE, m_cLogMessage);
69 BEGIN_MESSAGE_MAP(CCheckForUpdatesDlg, CStandAloneDialog)
70 ON_STN_CLICKED(IDC_CHECKRESULT, OnStnClickedCheckresult)
71 ON_WM_TIMER()
72 ON_WM_WINDOWPOSCHANGING()
73 ON_WM_SETCURSOR()
74 ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnBnClickedButtonUpdate)
75 ON_MESSAGE(WM_USER_DISPLAYSTATUS, OnDisplayStatus)
76 ON_MESSAGE(WM_USER_ENDDOWNLOAD, OnEndDownload)
77 ON_MESSAGE(WM_USER_FILLCHANGELOG, OnFillChangelog)
78 END_MESSAGE_MAP()
80 BOOL CCheckForUpdatesDlg::OnInitDialog()
82 CStandAloneDialog::OnInitDialog();
83 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
85 CString temp;
86 temp.Format(IDS_CHECKNEWER_YOURVERSION, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
87 SetDlgItemText(IDC_YOURVERSION, temp);
89 DialogEnableWindow(IDOK, FALSE);
91 // hide download controls
92 m_ctrlFiles.ShowWindow(SW_HIDE);
93 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_HIDE);
94 RECT rectWindow, rectGroupDownloads, rectOKButton;
95 GetWindowRect(&rectWindow);
96 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
97 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
98 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
99 OffsetRect(&rectOKButton, 0, rectGroupDownloads.top - rectOKButton.top);
100 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
101 MoveWindow(&rectWindow);
102 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
103 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
105 temp.LoadString(IDS_STATUSLIST_COLFILE);
106 m_ctrlFiles.InsertColumn(0, temp, 0, -1);
107 m_ctrlFiles.InsertColumn(1, temp, 0, -1);
108 m_ctrlFiles.SetExtendedStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_CHECKBOXES);
109 m_ctrlFiles.SetColumnWidth(0, 350);
110 m_ctrlFiles.SetColumnWidth(1, 200);
112 ProjectProperties pp;
113 pp.SetCheckRe(_T("[Ii]ssues?:?(\\s*(,|and)?\\s*#?\\d+)+"));
114 pp.SetBugIDRe(_T("(\\d+)"));
115 pp.lProjectLanguage = -1;
116 pp.sUrl = _T("http://code.google.com/p/tortoisegit/issues/detail?id=%BUGID%");
117 m_cLogMessage.Init(pp);
118 m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
119 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
121 if (AfxBeginThread(CheckThreadEntry, this)==NULL)
123 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
126 SetTimer(100, 1000, NULL);
127 return TRUE;
130 void CCheckForUpdatesDlg::OnOK()
132 if (m_bThreadRunning || m_pDownloadThread != NULL)
133 return; // Don't exit while downloading
135 CStandAloneDialog::OnOK();
138 void CCheckForUpdatesDlg::OnCancel()
140 if (m_bThreadRunning || m_pDownloadThread != NULL)
141 return; // Don't exit while downloading
142 CStandAloneDialog::OnCancel();
145 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
147 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
150 UINT CCheckForUpdatesDlg::CheckThread()
152 m_bThreadRunning = TRUE;
154 CString temp;
155 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
157 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
158 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
159 CString sCheckURL = checkurluser;
160 if (sCheckURL.IsEmpty())
162 sCheckURL = checkurlmachine;
163 if (sCheckURL.IsEmpty())
165 bool checkPreview = false;
166 #if PREVIEW
167 checkPreview = true;
168 #else
169 CRegStdDWORD regCheckPreview(L"Software\\TortoiseGit\\VersionCheckPreview", FALSE);
170 if (DWORD(regCheckPreview))
171 checkPreview = true;
172 #endif
173 if (checkPreview)
174 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version-preview.txt");
175 else
176 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version.txt");
179 CoInitialize(NULL);
180 HRESULT res = URLDownloadToFile(NULL, sCheckURL, tempfile, 0, NULL);
181 if (res == S_OK)
185 CStdioFile file(tempfile, CFile::modeRead | CFile::shareDenyWrite);
186 CString ver;
187 unsigned int major,minor,micro,build;
188 major=minor=micro=build=0;
189 unsigned __int64 version=0;
191 if (file.ReadString(ver))
193 CString vertemp = ver;
194 // another versionstring for the filename can be provided after a semicolon
195 // this is needed for preview releases
196 int differentFilenamePos = vertemp.Find(_T(";"));
197 if (differentFilenamePos > 0)
199 vertemp = vertemp.Left(differentFilenamePos);
200 ver = ver.Mid(differentFilenamePos + 1);
203 major = _ttoi(vertemp);
204 vertemp = vertemp.Mid(vertemp.Find('.')+1);
205 minor = _ttoi(vertemp);
206 vertemp = vertemp.Mid(vertemp.Find('.')+1);
207 micro = _ttoi(vertemp);
208 vertemp = vertemp.Mid(vertemp.Find('.')+1);
209 build = _ttoi(vertemp);
210 version = major;
211 version <<= 16;
212 version += minor;
213 version <<= 16;
214 version += micro;
215 version <<= 16;
216 version += build;
220 BOOL bNewer = FALSE;
221 if (major > TGIT_VERMAJOR)
222 bNewer = TRUE;
223 else if ((minor > TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
224 bNewer = TRUE;
225 else if ((micro > TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
226 bNewer = TRUE;
227 else if ((build > TGIT_VERBUILD)&&(micro == TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
228 bNewer = TRUE;
230 if (version != 0)
232 CString version;
233 version.Format(_T("%d.%d.%d.%d"),major,minor,micro,build);
234 if (version != ver)
235 version += _T(" (") + ver + _T(")");
236 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)version);
237 SetDlgItemText(IDC_CURRENTVERSION, temp);
240 if (version == 0)
242 temp.LoadString(IDS_CHECKNEWER_NETERROR);
243 SetDlgItemText(IDC_CHECKRESULT, temp);
245 else if (bNewer)
247 if(file.ReadString(temp) && !temp.IsEmpty())
248 { // Read the next line, it could contain a message for the user
249 CString tempLink;
250 if(file.ReadString(tempLink) && !tempLink.IsEmpty())
251 { // Read another line to find out the download link-URL, if any
252 m_sUpdateDownloadLink = tempLink;
255 else
257 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
258 CString tempLink;
259 file.ReadString(tempLink);
261 SetDlgItemText(IDC_CHECKRESULT, temp);
262 m_bShowInfo = TRUE;
264 FillChangelog(file);
265 FillDownloads(file, ver);
267 // Show download controls
268 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
269 GetWindowRect(&rectWindow);
270 m_progress.GetWindowRect(&rectProgress);
271 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
272 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
273 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
274 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
275 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
276 MoveWindow(&rectWindow);
277 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
278 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
279 m_ctrlFiles.ShowWindow(SW_SHOW);
280 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
281 CenterWindow();
283 else
285 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
286 SetDlgItemText(IDC_CHECKRESULT, temp);
287 file.ReadString(temp);
288 file.ReadString(temp);
289 FillChangelog(file);
293 catch (CException * e)
295 e->Delete();
296 temp.LoadString(IDS_CHECKNEWER_NETERROR);
297 SetDlgItemText(IDC_CHECKRESULT, temp);
300 else
302 // Try to cache web page;
304 temp.LoadString(IDS_CHECKNEWER_NETERROR);
305 SetDlgItemText(IDC_CHECKRESULT, temp);
307 if (!m_sUpdateDownloadLink.IsEmpty())
309 m_link.ShowWindow(SW_SHOW);
310 m_link.SetURL(m_sUpdateDownloadLink);
313 DeleteFile(tempfile);
314 m_bThreadRunning = FALSE;
315 DialogEnableWindow(IDOK, TRUE);
316 return 0;
319 void CCheckForUpdatesDlg::FillDownloads(CStdioFile &file, CString version)
321 #if WIN64
322 const CString x86x64 = _T("64");
323 #else
324 const CString x86x64 = _T("32");
325 #endif
327 if (!file.ReadString(m_sFilesURL) || m_sFilesURL.IsEmpty())
328 m_sFilesURL = _T("http://tortoisegit.googlecode.com/files/");
330 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
331 CString filename;
332 filename.Format(_T("TortoiseGit-%s-%sbit.msi"), version, x86x64);
333 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
334 m_ctrlFiles.SetCheck(0 , TRUE);
336 std::vector<DWORD> m_installedLangs;
338 // set up the language selecting combobox
339 CString path = CPathUtils::GetAppParentDirectory();
340 path = path + _T("Languages\\");
341 CSimpleFileFind finder(path, _T("*.dll"));
342 while (finder.FindNextFileNoDirectories())
344 CString file = finder.GetFilePath();
345 CString filename = finder.GetFileName();
346 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
348 CString sVer = _T(STRPRODUCTVER);
349 sVer = sVer.Left(sVer.ReverseFind('.'));
350 CString sFileVer = CPathUtils::GetVersionFromFile(file);
351 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
352 CString sLoc = filename.Mid(12);
353 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
354 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
355 continue;
356 DWORD loc = _tstoi(filename.Mid(12));
357 m_installedLangs.push_back(loc);
362 CString langs;
363 while (file.ReadString(langs) && !langs.IsEmpty())
365 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
367 DWORD loc = _tstoi(langs.Mid(0, 4));
368 TCHAR buf[MAX_PATH];
369 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
370 CString sLang2(buf);
371 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
372 if (buf[0])
374 sLang2 += _T(" (");
375 sLang2 += buf;
376 sLang2 += _T(")");
379 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), sLang);
380 m_ctrlFiles.SetItemText(pos, 1, sLang2);
382 CString filename;
383 filename.Format(_T("TortoiseGit-LanguagePack-%s-%sbit-%s.msi"), version, x86x64, langs.Mid(5));
384 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
386 if (std::find(m_installedLangs.begin(), m_installedLangs.end(), loc) != m_installedLangs.end())
387 m_ctrlFiles.SetCheck(pos , TRUE);
389 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
392 void CCheckForUpdatesDlg::FillChangelog(CStdioFile &file)
394 CString sChangelogURL;
395 if (!file.ReadString(sChangelogURL) || sChangelogURL.IsEmpty())
396 sChangelogURL = _T("http://tortoisegit.googlecode.com/git/src/Changelog.txt");
398 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
399 HRESULT res = URLDownloadToFile(NULL, sChangelogURL, tempchangelogfile, 0, NULL);
400 if (SUCCEEDED(res))
402 CString temp;
403 CStdioFile file(tempchangelogfile, CFile::modeRead|CFile::typeText);
404 CString str;
405 bool first = true;
406 while (file.ReadString(str))
408 if (first)
410 first = false;
411 if (str.GetLength() > 2 && str.GetAt(0) == 0xEF && str.GetAt(1) == 0xBB)
413 if (str.GetAt(2) == 0xBF)
415 str = str.Mid(3);
417 else
419 str = str.Mid(2);
423 str = CUnicodeUtils::GetUnicode(CStringA(str), CP_UTF8);
424 temp += str + _T("\n");
426 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(temp.GetBuffer()));
428 else
429 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not load changelog.")));
432 void CCheckForUpdatesDlg::OnStnClickedCheckresult()
434 // user clicked on the label, start the browser with our web page
435 HINSTANCE result = ShellExecute(NULL, _T("opennew"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
436 if ((UINT)result <= HINSTANCE_ERROR)
438 result = ShellExecute(NULL, _T("open"), m_sUpdateDownloadLink, NULL,NULL, SW_SHOWNORMAL);
442 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
444 if (nIDEvent == 100)
446 if (m_bThreadRunning == FALSE)
448 if (m_bShowInfo)
450 m_bVisible = TRUE;
451 ShowWindow(SW_SHOWNORMAL);
453 else
455 EndDialog(0);
459 CStandAloneDialog::OnTimer(nIDEvent);
462 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
464 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
465 if (m_bVisible == FALSE)
466 lpwndpos->flags &= ~SWP_SHOWWINDOW;
469 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
471 if ((!m_sUpdateDownloadLink.IsEmpty())&&(pWnd)&&(pWnd == GetDlgItem(IDC_CHECKRESULT)))
473 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
474 SetCursor(hCur);
475 return TRUE;
478 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
479 SetCursor(hCur);
480 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
483 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
485 CString title;
486 m_ctrlUpdate.GetWindowText(title);
487 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
489 bool isOneSelected = false;
490 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
492 if (m_ctrlFiles.GetCheck(i))
494 isOneSelected = true;
495 break;
498 if (!isOneSelected)
499 return;
501 m_eventStop.ResetEvent();
503 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
504 if (m_pDownloadThread != NULL)
506 m_pDownloadThread->m_bAutoDelete = FALSE;
507 m_pDownloadThread->ResumeThread();
509 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
511 else
513 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
516 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
518 // Abort
519 m_eventStop.SetEvent();
521 else
523 CString folder = GetDownloadsDirectory();
524 if (m_ctrlUpdate.GetCurrentEntry() == 0)
526 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
528 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
529 if (m_ctrlFiles.GetCheck(i) == TRUE)
530 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
532 CStandAloneDialog::OnOK();
534 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
536 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
539 m_ctrlUpdate.SetCurrentEntry(0);
543 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
545 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
548 bool CCheckForUpdatesDlg::Download(CString filename)
550 CString destFilename = GetDownloadsDirectory() + filename;
551 if (PathFileExists(destFilename))
553 if (VerifySignature(destFilename))
554 return true;
555 else
556 DeleteFile(destFilename);
559 CBSCallbackImpl bsc(this->GetSafeHwnd(), m_eventStop);
561 CString url = m_sFilesURL + filename;
563 m_progress.SetRange32(0, 1);
564 m_progress.SetPos(0);
565 m_progress.ShowWindow(SW_SHOW);
567 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
568 HRESULT res = URLDownloadToFile(NULL, url, tempfile, 0, &bsc);
569 if (res == S_OK)
571 if (VerifySignature(tempfile))
573 if (PathFileExists(destFilename))
574 DeleteFile(destFilename);
575 MoveFile(tempfile, destFilename);
576 return true;
579 return false;
582 UINT CCheckForUpdatesDlg::DownloadThread()
584 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
586 BOOL result = TRUE;
587 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); i++)
589 m_ctrlFiles.EnsureVisible(i, FALSE);
590 CRect rect;
591 m_ctrlFiles.GetItemRect(i, &rect, LVIR_BOUNDS);
592 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
593 if (m_ctrlFiles.GetCheck(i) == TRUE)
595 data->m_status = CUpdateListCtrl::STATUS_DOWNLOADING;
596 m_ctrlFiles.InvalidateRect(rect);
597 if (Download(data->m_filename))
598 data->m_status = CUpdateListCtrl::STATUS_SUCCESS;
599 else
601 data->m_status = CUpdateListCtrl::STATUS_FAIL;
602 result = FALSE;
605 else
606 data->m_status = CUpdateListCtrl::STATUS_IGNORE;
607 m_ctrlFiles.InvalidateRect(rect);
610 ::PostMessage(GetSafeHwnd(), WM_USER_ENDDOWNLOAD, 0, 0);
612 return result;
615 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
617 ASSERT(m_pDownloadThread != NULL);
619 // wait until the thread terminates
620 DWORD dwExitCode;
621 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
622 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
624 // make sure we always have the correct exit code
625 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
627 delete m_pDownloadThread;
628 m_pDownloadThread = NULL;
630 m_progress.ShowWindow(SW_HIDE);
632 if (dwExitCode == TRUE)
634 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
635 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
636 m_ctrlUpdate.Invalidate();
638 else
640 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
641 CMessageBox::Show(NULL, IDS_ERR_FAILEDUPDATEDOWNLOAD, IDS_APPNAME, MB_ICONERROR);
644 return 0;
647 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
649 ASSERT(lParam != NULL);
651 TCHAR * changelog = reinterpret_cast<TCHAR *>(lParam);
652 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
653 m_cLogMessage.SetText(changelog);
654 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
655 m_cLogMessage.Call(SCI_GOTOPOS, 0);
657 return 0;
660 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
662 CString folder;
664 if (SysInfo::Instance().IsVistaOrLater())
666 CAutoLibrary hShell = ::LoadLibrary(_T("shell32.dll"));
667 if (hShell)
669 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
670 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
671 if (pfnSHGetKnownFolderPath)
673 wchar_t * wcharPtr = 0;
674 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
675 if (SUCCEEDED(hr))
677 folder = wcharPtr;
678 CoTaskMemFree(static_cast<void*>(wcharPtr));
679 return folder.TrimRight(_T("\\")) + _T("\\");
685 TCHAR szPath[MAX_PATH];
686 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
687 folder = szPath;
688 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
689 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
690 return downloads;
692 return folder;
695 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
697 const DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<DOWNLOADSTATUS *>(lParam);
698 if (pDownloadStatus != NULL)
700 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(DOWNLOADSTATUS)));
702 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
703 m_progress.SetPos(pDownloadStatus->ulProgress);
706 return 0;
709 CBSCallbackImpl::CBSCallbackImpl(HWND hWnd, HANDLE hEventStop)
711 m_hWnd = hWnd;
712 m_hEventStop = hEventStop;
713 m_ulObjRefCount = 1;
716 // IUnknown
717 STDMETHODIMP CBSCallbackImpl::QueryInterface(REFIID riid, void **ppvObject)
719 *ppvObject = NULL;
721 // IUnknown
722 if (::IsEqualIID(riid, __uuidof(IUnknown)))
723 *ppvObject = this;
725 // IBindStatusCallback
726 else if (::IsEqualIID(riid, __uuidof(IBindStatusCallback)))
727 *ppvObject = static_cast<IBindStatusCallback *>(this);
729 if (*ppvObject)
731 (*reinterpret_cast<LPUNKNOWN *>(ppvObject))->AddRef();
732 return S_OK;
735 return E_NOINTERFACE;
738 STDMETHODIMP_(ULONG) CBSCallbackImpl::AddRef()
740 return ++m_ulObjRefCount;
743 STDMETHODIMP_(ULONG) CBSCallbackImpl::Release()
745 return --m_ulObjRefCount;
748 // IBindStatusCallback
749 STDMETHODIMP CBSCallbackImpl::OnStartBinding(DWORD, IBinding *)
751 return S_OK;
754 STDMETHODIMP CBSCallbackImpl::GetPriority(LONG *)
756 return E_NOTIMPL;
758 STDMETHODIMP CBSCallbackImpl::OnLowResource(DWORD)
760 return S_OK;
763 STDMETHODIMP CBSCallbackImpl::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR /* szStatusText */)
765 TRACE(_T("IBindStatusCallback::OnProgress\n"));
766 TRACE(_T("ulProgress: %lu, ulProgressMax: %lu\n"), ulProgress, ulProgressMax);
767 UNREFERENCED_PARAMETER(ulStatusCode);
768 TRACE(_T("ulStatusCode: %lu "), ulStatusCode);
770 if (m_hWnd != NULL)
772 // inform the dialog box to display current status,
773 // don't use PostMessage
774 CCheckForUpdatesDlg::DOWNLOADSTATUS downloadStatus = { ulProgress, ulProgressMax };
775 ::SendMessage(m_hWnd, WM_USER_DISPLAYSTATUS, 0, reinterpret_cast<LPARAM>(&downloadStatus));
778 if (m_hEventStop != NULL && ::WaitForSingleObject(m_hEventStop, 0) == WAIT_OBJECT_0)
780 return E_ABORT; // canceled by the user
783 return S_OK;
786 STDMETHODIMP CBSCallbackImpl::OnStopBinding(HRESULT, LPCWSTR)
788 return S_OK;
791 STDMETHODIMP CBSCallbackImpl::GetBindInfo(DWORD *, BINDINFO *)
793 return S_OK;
796 STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD, FORMATETC *, STGMEDIUM *)
798 return S_OK;
801 STDMETHODIMP CBSCallbackImpl::OnObjectAvailable(REFIID, IUnknown *)
803 return S_OK;
806 BOOL CCheckForUpdatesDlg::VerifySignature(CString fileName)
808 WINTRUST_FILE_INFO fileInfo;
809 memset(&fileInfo, 0, sizeof(fileInfo));
810 fileInfo.cbStruct = sizeof(WINTRUST_FILE_INFO);
811 fileInfo.pcwszFilePath = fileName;
813 WINTRUST_DATA data;
814 memset(&data, 0, sizeof(data));
815 data.cbStruct = sizeof(data);
817 data.dwUIChoice = WTD_UI_NONE;
818 data.fdwRevocationChecks = WTD_REVOKE_NONE;
819 data.dwUnionChoice = WTD_CHOICE_FILE;
820 data.pFile = &fileInfo;
822 GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
824 return (WinVerifyTrust(NULL, &policyGUID, &data) == ERROR_SUCCESS);