Add support for "project" config level
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blobaf8a28882e876d5c4527116e1392838082f72a61
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2013 - 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 "SmartHandle.h"
30 #include "SysInfo.h"
31 #include "PathUtils.h"
32 #include "DirFileEnum.h"
33 #include "UnicodeUtils.h"
34 #include "UpdateCrypto.h"
36 #define SIGNATURE_FILE_ENDING _T(".asc")
38 #define WM_USER_DISPLAYSTATUS (WM_USER + 1)
39 #define WM_USER_ENDDOWNLOAD (WM_USER + 2)
40 #define WM_USER_FILLCHANGELOG (WM_USER + 3)
42 IMPLEMENT_DYNAMIC(CCheckForUpdatesDlg, CStandAloneDialog)
43 CCheckForUpdatesDlg::CCheckForUpdatesDlg(CWnd* pParent /*=NULL*/)
44 : CStandAloneDialog(CCheckForUpdatesDlg::IDD, pParent)
45 , m_bShowInfo(FALSE)
46 , m_bForce(FALSE)
47 , m_bVisible(FALSE)
48 , m_pDownloadThread(NULL)
49 , m_bThreadRunning(FALSE)
51 m_sUpdateDownloadLink = _T("http://redir.tortoisegit.org/download");
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_WM_TIMER()
70 ON_WM_WINDOWPOSCHANGING()
71 ON_WM_SETCURSOR()
72 ON_WM_DESTROY()
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 = -1;
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::OnDestroy()
131 for (int i = 0; i < m_ctrlFiles.GetItemCount(); ++i)
132 delete (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
134 CStandAloneDialog::OnDestroy();
137 void CCheckForUpdatesDlg::OnOK()
139 if (m_bThreadRunning || m_pDownloadThread != NULL)
140 return; // Don't exit while downloading
142 CStandAloneDialog::OnOK();
145 void CCheckForUpdatesDlg::OnCancel()
147 if (m_bThreadRunning || m_pDownloadThread != NULL)
148 return; // Don't exit while downloading
149 CStandAloneDialog::OnCancel();
152 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
154 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
157 UINT CCheckForUpdatesDlg::CheckThread()
159 m_bThreadRunning = TRUE;
161 CString temp;
162 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
164 bool official = false;
166 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
167 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
168 CString sCheckURL = checkurluser;
169 if (sCheckURL.IsEmpty())
171 sCheckURL = checkurlmachine;
172 if (sCheckURL.IsEmpty())
174 official = true;
175 bool checkPreview = false;
176 #if PREVIEW
177 checkPreview = true;
178 #else
179 CRegStdDWORD regCheckPreview(L"Software\\TortoiseGit\\VersionCheckPreview", FALSE);
180 if (DWORD(regCheckPreview))
181 checkPreview = true;
182 #endif
183 if (checkPreview)
185 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version-preview.txt");
186 SetDlgItemText(IDC_SOURCE, _T("Using preview release channel"));
188 else
189 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version.txt");
193 if (!official)
194 SetDlgItemText(IDC_SOURCE, _T("Using (unofficial) release channel: ") + sCheckURL);
196 CoInitialize(NULL);
197 if (m_bForce)
198 DeleteUrlCacheEntry(sCheckURL);
199 HRESULT res = URLDownloadToFile(NULL, sCheckURL, tempfile, 0, NULL);
200 if (res == S_OK && official)
202 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
203 if (m_bForce)
204 DeleteUrlCacheEntry(sCheckURL + SIGNATURE_FILE_ENDING);
205 res = URLDownloadToFile(nullptr, sCheckURL + SIGNATURE_FILE_ENDING, signatureTempfile, 0, nullptr);
206 if (res == S_OK && VerifyIntegrity(tempfile, signatureTempfile))
208 SetDlgItemText(IDC_CHECKRESULT, _T("Could not verify digital signature."));
209 DeleteUrlCacheEntry(sCheckURL);
210 DeleteUrlCacheEntry(sCheckURL + SIGNATURE_FILE_ENDING);
211 goto finish;
214 if (res == S_OK)
218 CStdioFile file(tempfile, CFile::modeRead | CFile::shareDenyWrite);
219 CString ver;
220 unsigned int major,minor,micro,build;
221 major=minor=micro=build=0;
222 unsigned __int64 version=0;
224 if (file.ReadString(ver))
226 CString vertemp = ver;
227 // another versionstring for the filename can be provided after a semicolon
228 // this is needed for preview releases
229 int differentFilenamePos = vertemp.Find(_T(";"));
230 if (differentFilenamePos > 0)
232 vertemp = vertemp.Left(differentFilenamePos);
233 ver = ver.Mid(differentFilenamePos + 1);
236 major = _ttoi(vertemp);
237 vertemp = vertemp.Mid(vertemp.Find('.')+1);
238 minor = _ttoi(vertemp);
239 vertemp = vertemp.Mid(vertemp.Find('.')+1);
240 micro = _ttoi(vertemp);
241 vertemp = vertemp.Mid(vertemp.Find('.')+1);
242 build = _ttoi(vertemp);
243 version = major;
244 version <<= 16;
245 version += minor;
246 version <<= 16;
247 version += micro;
248 version <<= 16;
249 version += build;
253 BOOL bNewer = FALSE;
254 if (m_bForce)
255 bNewer = TRUE;
256 if (major > TGIT_VERMAJOR)
257 bNewer = TRUE;
258 else if ((minor > TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
259 bNewer = TRUE;
260 else if ((micro > TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
261 bNewer = TRUE;
262 else if ((build > TGIT_VERBUILD)&&(micro == TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
263 bNewer = TRUE;
265 if (version != 0)
267 CString version;
268 version.Format(_T("%d.%d.%d.%d"),major,minor,micro,build);
269 if (version != ver)
270 version += _T(" (") + ver + _T(")");
271 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)version);
272 SetDlgItemText(IDC_CURRENTVERSION, temp);
275 if (version == 0)
277 temp.LoadString(IDS_CHECKNEWER_NETERROR);
278 SetDlgItemText(IDC_CHECKRESULT, temp);
280 else if (bNewer)
282 if(file.ReadString(temp) && !temp.IsEmpty())
283 { // Read the next line, it could contain a message for the user
284 CString tempLink;
285 if(file.ReadString(tempLink) && !tempLink.IsEmpty())
286 { // Read another line to find out the download link-URL, if any
287 m_sUpdateDownloadLink = tempLink;
290 else
292 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
293 CString tempLink;
294 file.ReadString(tempLink);
296 SetDlgItemText(IDC_CHECKRESULT, temp);
297 m_bShowInfo = TRUE;
299 FillChangelog(file);
300 FillDownloads(file, ver);
302 // Show download controls
303 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
304 GetWindowRect(&rectWindow);
305 m_progress.GetWindowRect(&rectProgress);
306 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
307 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
308 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
309 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
310 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
311 MoveWindow(&rectWindow);
312 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
313 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
314 m_ctrlFiles.ShowWindow(SW_SHOW);
315 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
316 CenterWindow();
318 else
320 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
321 SetDlgItemText(IDC_CHECKRESULT, temp);
322 file.ReadString(temp);
323 file.ReadString(temp);
324 FillChangelog(file);
328 catch (CException * e)
330 e->Delete();
331 temp.LoadString(IDS_CHECKNEWER_NETERROR);
332 SetDlgItemText(IDC_CHECKRESULT, temp);
335 else
337 // Try to cache web page;
339 temp.LoadString(IDS_CHECKNEWER_NETERROR);
340 SetDlgItemText(IDC_CHECKRESULT, temp);
342 if (!m_sUpdateDownloadLink.IsEmpty())
344 m_link.ShowWindow(SW_SHOW);
345 m_link.SetURL(m_sUpdateDownloadLink);
348 finish:
349 CoUninitialize();
350 m_bThreadRunning = FALSE;
351 DialogEnableWindow(IDOK, TRUE);
352 return 0;
355 void CCheckForUpdatesDlg::FillDownloads(CStdioFile &file, CString version)
357 #if WIN64
358 const CString x86x64 = _T("64");
359 #else
360 const CString x86x64 = _T("32");
361 #endif
363 if (!file.ReadString(m_sFilesURL) || m_sFilesURL.IsEmpty())
364 m_sFilesURL = _T("http://tortoisegit.googlecode.com/files/");
366 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
367 CString filename;
368 filename.Format(_T("TortoiseGit-%s-%sbit.msi"), version, x86x64);
369 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
370 m_ctrlFiles.SetCheck(0 , TRUE);
372 std::vector<DWORD> m_installedLangs;
374 // set up the language selecting combobox
375 CString path = CPathUtils::GetAppParentDirectory();
376 path = path + _T("Languages\\");
377 CSimpleFileFind finder(path, _T("*.dll"));
378 while (finder.FindNextFileNoDirectories())
380 CString file = finder.GetFilePath();
381 CString filename = finder.GetFileName();
382 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
384 CString sVer = _T(STRPRODUCTVER);
385 sVer = sVer.Left(sVer.ReverseFind('.'));
386 CString sFileVer = CPathUtils::GetVersionFromFile(file);
387 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
388 CString sLoc = filename.Mid(12);
389 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
390 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
391 continue;
392 DWORD loc = _tstoi(filename.Mid(12));
393 m_installedLangs.push_back(loc);
398 CString langs;
399 while (file.ReadString(langs) && !langs.IsEmpty())
401 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
403 DWORD loc = _tstoi(langs.Mid(0, 4));
404 TCHAR buf[MAX_PATH];
405 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
406 CString sLang2(buf);
407 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
408 if (buf[0])
410 sLang2 += _T(" (");
411 sLang2 += buf;
412 sLang2 += _T(")");
415 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), sLang);
416 m_ctrlFiles.SetItemText(pos, 1, sLang2);
418 CString filename;
419 filename.Format(_T("TortoiseGit-LanguagePack-%s-%sbit-%s.msi"), version, x86x64, langs.Mid(5));
420 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
422 if (std::find(m_installedLangs.begin(), m_installedLangs.end(), loc) != m_installedLangs.end())
423 m_ctrlFiles.SetCheck(pos , TRUE);
425 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
428 void CCheckForUpdatesDlg::FillChangelog(CStdioFile &file)
430 CString sChangelogURL;
431 if (!file.ReadString(sChangelogURL) || sChangelogURL.IsEmpty())
432 sChangelogURL = _T("http://tortoisegit.googlecode.com/git/src/Changelog.txt");
434 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
435 HRESULT res = URLDownloadToFile(NULL, sChangelogURL, tempchangelogfile, 0, NULL);
436 if (SUCCEEDED(res))
438 CString temp;
439 CStdioFile file(tempchangelogfile, CFile::modeRead|CFile::typeText);
440 CString str;
441 bool first = true;
442 while (file.ReadString(str))
444 if (first)
446 first = false;
447 if (str.GetLength() > 2 && str.GetAt(0) == 0xEF && str.GetAt(1) == 0xBB)
449 if (str.GetAt(2) == 0xBF)
451 str = str.Mid(3);
453 else
455 str = str.Mid(2);
459 str = CUnicodeUtils::GetUnicode(CStringA(str), CP_UTF8);
460 temp += str + _T("\n");
462 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(temp.GetBuffer()));
464 else
465 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not load changelog.")));
468 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
470 if (nIDEvent == 100)
472 if (m_bThreadRunning == FALSE)
474 if (m_bShowInfo)
476 m_bVisible = TRUE;
477 ShowWindow(SW_SHOWNORMAL);
479 else
481 EndDialog(0);
485 CStandAloneDialog::OnTimer(nIDEvent);
488 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
490 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
491 if (m_bVisible == FALSE)
492 lpwndpos->flags &= ~SWP_SHOWWINDOW;
495 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
497 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
498 SetCursor(hCur);
499 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
502 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
504 CString title;
505 m_ctrlUpdate.GetWindowText(title);
506 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
508 bool isOneSelected = false;
509 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
511 if (m_ctrlFiles.GetCheck(i))
513 isOneSelected = true;
514 break;
517 if (!isOneSelected)
518 return;
520 m_eventStop.ResetEvent();
522 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
523 if (m_pDownloadThread != NULL)
525 m_pDownloadThread->m_bAutoDelete = FALSE;
526 m_pDownloadThread->ResumeThread();
528 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
530 else
532 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
535 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
537 // Abort
538 m_eventStop.SetEvent();
540 else
542 CString folder = GetDownloadsDirectory();
543 if (m_ctrlUpdate.GetCurrentEntry() == 0)
545 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
547 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
548 if (m_ctrlFiles.GetCheck(i) == TRUE)
549 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
551 CStandAloneDialog::OnOK();
553 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
555 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
558 m_ctrlUpdate.SetCurrentEntry(0);
562 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
564 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
567 bool CCheckForUpdatesDlg::Download(CString filename)
569 CString url = m_sFilesURL + filename;
570 CString destFilename = GetDownloadsDirectory() + filename;
571 if (PathFileExists(destFilename) && PathFileExists(destFilename + SIGNATURE_FILE_ENDING))
573 if (VerifyIntegrity(destFilename, destFilename + SIGNATURE_FILE_ENDING) == 0)
574 return true;
575 else
577 DeleteFile(destFilename);
578 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
579 DeleteUrlCacheEntry(url);
580 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
584 CBSCallbackImpl bsc(this->GetSafeHwnd(), m_eventStop);
586 m_progress.SetRange32(0, 1);
587 m_progress.SetPos(0);
588 m_progress.ShowWindow(SW_SHOW);
590 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
591 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
592 if (m_bForce)
593 DeleteUrlCacheEntry(url);
594 HRESULT res = URLDownloadToFile(NULL, url, tempfile, 0, &bsc);
595 if (res == S_OK)
597 if (m_bForce)
598 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
599 res = URLDownloadToFile(NULL, url + SIGNATURE_FILE_ENDING, signatureTempfile, 0, nullptr);
600 m_progress.SetPos(m_progress.GetPos() + 1);
602 if (res == S_OK)
604 if (VerifyIntegrity(tempfile, signatureTempfile) == 0)
606 DeleteFile(destFilename);
607 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
608 MoveFile(tempfile, destFilename);
609 MoveFile(signatureTempfile, destFilename + SIGNATURE_FILE_ENDING);
610 return true;
612 DeleteUrlCacheEntry(url);
613 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
615 return false;
618 UINT CCheckForUpdatesDlg::DownloadThread()
620 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
622 CoInitialize(NULL);
624 BOOL result = TRUE;
625 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
627 m_ctrlFiles.EnsureVisible(i, FALSE);
628 CRect rect;
629 m_ctrlFiles.GetItemRect(i, &rect, LVIR_BOUNDS);
630 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
631 if (m_ctrlFiles.GetCheck(i) == TRUE)
633 data->m_status = CUpdateListCtrl::STATUS_DOWNLOADING;
634 m_ctrlFiles.InvalidateRect(rect);
635 if (Download(data->m_filename))
636 data->m_status = CUpdateListCtrl::STATUS_SUCCESS;
637 else
639 data->m_status = CUpdateListCtrl::STATUS_FAIL;
640 result = FALSE;
643 else
644 data->m_status = CUpdateListCtrl::STATUS_IGNORE;
645 m_ctrlFiles.InvalidateRect(rect);
648 ::PostMessage(GetSafeHwnd(), WM_USER_ENDDOWNLOAD, 0, 0);
650 CoUninitialize();
652 return result;
655 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
657 ASSERT(m_pDownloadThread != NULL);
659 // wait until the thread terminates
660 DWORD dwExitCode;
661 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
662 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
664 // make sure we always have the correct exit code
665 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
667 delete m_pDownloadThread;
668 m_pDownloadThread = NULL;
670 m_progress.ShowWindow(SW_HIDE);
672 if (dwExitCode == TRUE)
674 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
675 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
676 m_ctrlUpdate.Invalidate();
678 else
680 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
681 CMessageBox::Show(NULL, IDS_ERR_FAILEDUPDATEDOWNLOAD, IDS_APPNAME, MB_ICONERROR);
684 return 0;
687 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
689 ASSERT(lParam != NULL);
691 TCHAR * changelog = reinterpret_cast<TCHAR *>(lParam);
692 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
693 m_cLogMessage.SetText(changelog);
694 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
695 m_cLogMessage.Call(SCI_GOTOPOS, 0);
697 return 0;
700 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
702 CString folder;
704 if (SysInfo::Instance().IsVistaOrLater())
706 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
707 if (hShell)
709 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
710 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
711 if (pfnSHGetKnownFolderPath)
713 wchar_t * wcharPtr = 0;
714 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
715 if (SUCCEEDED(hr))
717 folder = wcharPtr;
718 CoTaskMemFree(static_cast<void*>(wcharPtr));
719 return folder.TrimRight(_T("\\")) + _T("\\");
725 TCHAR szPath[MAX_PATH];
726 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
727 folder = szPath;
728 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
729 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
730 return downloads;
732 return folder;
735 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
737 const DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<DOWNLOADSTATUS *>(lParam);
738 if (pDownloadStatus != NULL)
740 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(DOWNLOADSTATUS)));
742 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
743 m_progress.SetPos(pDownloadStatus->ulProgress);
746 return 0;
749 CBSCallbackImpl::CBSCallbackImpl(HWND hWnd, HANDLE hEventStop)
751 m_hWnd = hWnd;
752 m_hEventStop = hEventStop;
753 m_ulObjRefCount = 1;
756 // IUnknown
757 STDMETHODIMP CBSCallbackImpl::QueryInterface(REFIID riid, void **ppvObject)
759 *ppvObject = NULL;
761 // IUnknown
762 if (::IsEqualIID(riid, __uuidof(IUnknown)))
763 *ppvObject = this;
765 // IBindStatusCallback
766 else if (::IsEqualIID(riid, __uuidof(IBindStatusCallback)))
767 *ppvObject = static_cast<IBindStatusCallback *>(this);
769 if (*ppvObject)
771 (*reinterpret_cast<LPUNKNOWN *>(ppvObject))->AddRef();
772 return S_OK;
775 return E_NOINTERFACE;
778 STDMETHODIMP_(ULONG) CBSCallbackImpl::AddRef()
780 return ++m_ulObjRefCount;
783 STDMETHODIMP_(ULONG) CBSCallbackImpl::Release()
785 return --m_ulObjRefCount;
788 // IBindStatusCallback
789 STDMETHODIMP CBSCallbackImpl::OnStartBinding(DWORD, IBinding *)
791 return S_OK;
794 STDMETHODIMP CBSCallbackImpl::GetPriority(LONG *)
796 return E_NOTIMPL;
798 STDMETHODIMP CBSCallbackImpl::OnLowResource(DWORD)
800 return S_OK;
803 STDMETHODIMP CBSCallbackImpl::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR /* szStatusText */)
805 TRACE(_T("IBindStatusCallback::OnProgress\n"));
806 TRACE(_T("ulProgress: %lu, ulProgressMax: %lu\n"), ulProgress, ulProgressMax);
807 UNREFERENCED_PARAMETER(ulStatusCode);
808 TRACE(_T("ulStatusCode: %lu "), ulStatusCode);
810 if (m_hWnd != NULL)
812 // inform the dialog box to display current status,
813 // don't use PostMessage
814 CCheckForUpdatesDlg::DOWNLOADSTATUS downloadStatus = { ulProgress, ulProgressMax + 1 }; // + 1 for download of signature file
815 ::SendMessage(m_hWnd, WM_USER_DISPLAYSTATUS, 0, reinterpret_cast<LPARAM>(&downloadStatus));
818 if (m_hEventStop != NULL && ::WaitForSingleObject(m_hEventStop, 0) == WAIT_OBJECT_0)
820 return E_ABORT; // canceled by the user
823 return S_OK;
826 STDMETHODIMP CBSCallbackImpl::OnStopBinding(HRESULT, LPCWSTR)
828 return S_OK;
831 STDMETHODIMP CBSCallbackImpl::GetBindInfo(DWORD *, BINDINFO *)
833 return S_OK;
836 STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD, FORMATETC *, STGMEDIUM *)
838 return S_OK;
841 STDMETHODIMP CBSCallbackImpl::OnObjectAvailable(REFIID, IUnknown *)
843 return S_OK;