Show subpath in Clean Dialog title
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blob952209f8d71bc93e246fa9e5a2b65a5505c94477
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2014 - 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"
35 #include "Win7.h"
37 #define SIGNATURE_FILE_ENDING _T(".rsa.asc")
39 #define WM_USER_DISPLAYSTATUS (WM_USER + 1)
40 #define WM_USER_ENDDOWNLOAD (WM_USER + 2)
41 #define WM_USER_FILLCHANGELOG (WM_USER + 3)
43 IMPLEMENT_DYNAMIC(CCheckForUpdatesDlg, CStandAloneDialog)
44 CCheckForUpdatesDlg::CCheckForUpdatesDlg(CWnd* pParent /*=NULL*/)
45 : CStandAloneDialog(CCheckForUpdatesDlg::IDD, pParent)
46 , m_bShowInfo(FALSE)
47 , m_bForce(FALSE)
48 , m_bVisible(FALSE)
49 , m_pDownloadThread(NULL)
50 , m_bThreadRunning(FALSE)
51 , m_updateDownloader(nullptr)
53 m_sUpdateDownloadLink = _T("http://redir.tortoisegit.org/download");
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_WM_TIMER()
72 ON_WM_WINDOWPOSCHANGING()
73 ON_WM_SETCURSOR()
74 ON_WM_DESTROY()
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 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
80 ON_BN_CLICKED(IDC_DONOTASKAGAIN, &CCheckForUpdatesDlg::OnBnClickedDonotaskagain)
81 END_MESSAGE_MAP()
83 BOOL CCheckForUpdatesDlg::OnInitDialog()
85 CStandAloneDialog::OnInitDialog();
86 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
88 CString temp;
89 temp.Format(IDS_CHECKNEWER_YOURVERSION, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
90 SetDlgItemText(IDC_YOURVERSION, temp);
92 DialogEnableWindow(IDOK, FALSE);
94 m_pTaskbarList.Release();
95 if (FAILED(m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList)))
96 m_pTaskbarList = nullptr;
98 // hide download controls
99 m_ctrlFiles.ShowWindow(SW_HIDE);
100 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_HIDE);
101 RECT rectWindow, rectGroupDownloads, rectOKButton;
102 GetWindowRect(&rectWindow);
103 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
104 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
105 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
106 OffsetRect(&rectOKButton, 0, rectGroupDownloads.top - rectOKButton.top);
107 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
108 MoveWindow(&rectWindow);
109 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
110 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
112 temp.LoadString(IDS_STATUSLIST_COLFILE);
113 m_ctrlFiles.InsertColumn(0, temp, 0, -1);
114 m_ctrlFiles.InsertColumn(1, temp, 0, -1);
115 m_ctrlFiles.SetExtendedStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_CHECKBOXES);
116 m_ctrlFiles.SetColumnWidth(0, 350);
117 m_ctrlFiles.SetColumnWidth(1, 200);
119 m_cLogMessage.Init(-1);
120 m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
121 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
123 m_updateDownloader = new CUpdateDownloader(GetSafeHwnd(), m_bForce == TRUE, WM_USER_DISPLAYSTATUS, &m_eventStop);
125 if (AfxBeginThread(CheckThreadEntry, this)==NULL)
127 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
130 SetTimer(100, 1000, NULL);
131 return TRUE;
134 void CCheckForUpdatesDlg::OnDestroy()
136 for (int i = 0; i < m_ctrlFiles.GetItemCount(); ++i)
137 delete (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
139 if (m_updateDownloader)
140 delete m_updateDownloader;
142 CStandAloneDialog::OnDestroy();
145 void CCheckForUpdatesDlg::OnOK()
147 if (m_bThreadRunning || m_pDownloadThread != NULL)
148 return; // Don't exit while downloading
150 CStandAloneDialog::OnOK();
153 void CCheckForUpdatesDlg::OnCancel()
155 if (m_bThreadRunning || m_pDownloadThread != NULL)
156 return; // Don't exit while downloading
157 CStandAloneDialog::OnCancel();
160 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
162 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
165 UINT CCheckForUpdatesDlg::CheckThread()
167 m_bThreadRunning = TRUE;
169 CString temp;
170 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
172 bool official = false;
174 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
175 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
176 CString sCheckURL = checkurluser;
177 if (sCheckURL.IsEmpty())
179 sCheckURL = checkurlmachine;
180 if (sCheckURL.IsEmpty())
182 official = true;
183 bool checkPreview = false;
184 #if PREVIEW
185 checkPreview = true;
186 #else
187 CRegStdDWORD regCheckPreview(L"Software\\TortoiseGit\\VersionCheckPreview", FALSE);
188 if (DWORD(regCheckPreview))
189 checkPreview = true;
190 #endif
191 if (checkPreview)
193 sCheckURL = _T("://versioncheck.tortoisegit.org/version-preview.txt");
194 SetDlgItemText(IDC_SOURCE, _T("Using preview release channel"));
196 else
197 sCheckURL = _T("://versioncheck.tortoisegit.org/version.txt");
198 if (SysInfo::Instance().IsVistaOrLater()) // we need SNI support
199 sCheckURL = _T("https") + sCheckURL;
200 else
201 sCheckURL = _T("http") + sCheckURL;
205 if (!official)
206 SetDlgItemText(IDC_SOURCE, _T("Using (unofficial) release channel: ") + sCheckURL);
208 CString ver;
209 CAutoConfig versioncheck(true);
210 CString errorText;
211 DWORD ret = m_updateDownloader->DownloadFile(sCheckURL, tempfile, false);
212 if (!ret && official)
214 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
215 ret = m_updateDownloader->DownloadFile(sCheckURL + SIGNATURE_FILE_ENDING, signatureTempfile, false);
216 if (ret || VerifyIntegrity(tempfile, signatureTempfile, m_updateDownloader))
218 CString error = _T("Could not verify digital signature.");
219 if (ret)
220 error += _T("\r\nError: ") + GetWinINetError(ret) + _T(" (on ") + sCheckURL + SIGNATURE_FILE_ENDING + _T(")");
221 SetDlgItemText(IDC_CHECKRESULT, error);
222 DeleteUrlCacheEntry(sCheckURL);
223 DeleteUrlCacheEntry(sCheckURL + SIGNATURE_FILE_ENDING);
224 goto finish;
227 else if (ret)
229 DeleteUrlCacheEntry(sCheckURL);
230 if (CRegDWORD(_T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\GlobalUserOffline"), 0))
231 errorText.LoadString(IDS_OFFLINEMODE); // offline mode enabled
232 else
233 errorText.Format(IDS_CHECKNEWER_NETERROR_FORMAT, GetWinINetError(ret) + _T(" URL: ") + sCheckURL, ret);
234 SetDlgItemText(IDC_CHECKRESULT, errorText);
235 goto finish;
238 git_config_add_file_ondisk(versioncheck, CUnicodeUtils::GetUTF8(tempfile), GIT_CONFIG_LEVEL_GLOBAL, 0);
240 unsigned int major, minor, micro, build;
241 major = minor = micro = build = 0;
242 unsigned __int64 version = 0;
244 if (!versioncheck.GetString(_T("tortoisegit.version"), ver))
246 CString vertemp = ver;
247 major = _ttoi(vertemp);
248 vertemp = vertemp.Mid(vertemp.Find('.') + 1);
249 minor = _ttoi(vertemp);
250 vertemp = vertemp.Mid(vertemp.Find('.') + 1);
251 micro = _ttoi(vertemp);
252 vertemp = vertemp.Mid(vertemp.Find('.') + 1);
253 build = _ttoi(vertemp);
254 version = major;
255 version <<= 16;
256 version += minor;
257 version <<= 16;
258 version += micro;
259 version <<= 16;
260 version += build;
262 if (version == 0)
264 temp.LoadString(IDS_CHECKNEWER_NETERROR);
265 SetDlgItemText(IDC_CHECKRESULT, temp);
266 goto finish;
269 // another versionstring for the filename can be provided
270 // this is needed for preview releases
271 vertemp.Empty();
272 versioncheck.GetString(_T("tortoisegit.versionstring"), vertemp);
273 if (!vertemp.IsEmpty())
274 ver = vertemp;
276 else
278 errorText = _T("Could not parse version check file: ") + g_Git.GetLibGit2LastErr();
279 SetDlgItemText(IDC_CHECKRESULT, errorText);
280 DeleteUrlCacheEntry(sCheckURL);
281 goto finish;
285 BOOL bNewer = FALSE;
286 if (m_bForce)
287 bNewer = TRUE;
288 else if (major > TGIT_VERMAJOR)
289 bNewer = TRUE;
290 else if ((minor > TGIT_VERMINOR) && (major == TGIT_VERMAJOR))
291 bNewer = TRUE;
292 else if ((micro > TGIT_VERMICRO) && (minor == TGIT_VERMINOR) && (major == TGIT_VERMAJOR))
293 bNewer = TRUE;
294 else if ((build > TGIT_VERBUILD) && (micro == TGIT_VERMICRO) && (minor == TGIT_VERMINOR) && (major == TGIT_VERMAJOR))
295 bNewer = TRUE;
297 CString versionstr;
298 versionstr.Format(_T("%u.%u.%u.%u"), major, minor, micro, build);
299 if (versionstr != ver)
300 versionstr += _T(" (") + ver + _T(")");
301 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)versionstr);
302 SetDlgItemText(IDC_CURRENTVERSION, temp);
304 if (bNewer)
306 versioncheck.GetString(_T("tortoisegit.infotext"), temp);
307 if (!temp.IsEmpty())
309 CString tempLink;
310 versioncheck.GetString(_T("tortoisegit.infotexturl"), tempLink);
311 if (!tempLink.IsEmpty()) // find out the download link-URL, if any
312 m_sUpdateDownloadLink = tempLink;
314 else
315 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
316 SetDlgItemText(IDC_CHECKRESULT, temp);
318 FillChangelog(versioncheck, official);
319 FillDownloads(versioncheck, ver);
321 // Show download controls
322 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
323 GetWindowRect(&rectWindow);
324 m_progress.GetWindowRect(&rectProgress);
325 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
326 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
327 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
328 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
329 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
330 MoveWindow(&rectWindow);
331 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
332 if (CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck"), TRUE) != FALSE && !m_bForce && !m_bShowInfo)
334 GetDlgItem(IDC_DONOTASKAGAIN)->ShowWindow(SW_SHOW);
335 rectOKButton.left += 60;
336 CString temp;
337 temp.LoadString(IDS_REMINDMELATER);
338 GetDlgItem(IDOK)->SetWindowText(temp);
339 rectOKButton.right += 160;
341 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
342 m_ctrlFiles.ShowWindow(SW_SHOW);
343 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
344 CenterWindow();
345 m_bShowInfo = TRUE;
347 else if (m_bShowInfo)
349 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
350 SetDlgItemText(IDC_CHECKRESULT, temp);
351 FillChangelog(versioncheck, official);
355 finish:
356 if (!m_sUpdateDownloadLink.IsEmpty())
358 m_link.ShowWindow(SW_SHOW);
359 m_link.SetURL(m_sUpdateDownloadLink);
361 m_bThreadRunning = FALSE;
362 DialogEnableWindow(IDOK, TRUE);
363 return 0;
366 void CCheckForUpdatesDlg::FillDownloads(CAutoConfig& versioncheck, const CString version)
368 #if WIN64
369 const CString x86x64 = _T("64");
370 #else
371 const CString x86x64 = _T("32");
372 #endif
374 versioncheck.GetString(_T("tortoisegit.baseurl"), m_sFilesURL);
375 if (m_sFilesURL.IsEmpty())
376 m_sFilesURL.Format(_T("http://updater.download.tortoisegit.org/tgit/%s/"), version);
378 bool isHotfix = false;
379 versioncheck.GetBool(_T("tortoisegit.hotfix"), isHotfix);
381 if (isHotfix)
382 m_ctrlFiles.InsertItem(0, _T("TortoiseGit Hotfix"));
383 else
384 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
385 CString filenameMain, filenamePattern;
386 versioncheck.GetString(_T("tortoisegit.mainfilename"), filenamePattern);
387 if (filenamePattern.IsEmpty())
388 filenamePattern = _T("TortoiseGit-%1!s!-%2!s!bit.msi");
389 filenameMain.FormatMessage(filenamePattern, version, x86x64);
390 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filenameMain, CUpdateListCtrl::STATUS_NONE)));
391 m_ctrlFiles.SetCheck(0 , TRUE);
393 if (isHotfix)
395 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
396 return;
399 struct LangPack
401 CString m_PackName;
402 CString m_LangName;
403 DWORD m_LocaleID;
404 CString m_LangCode;
405 bool m_Installed;
407 struct LanguagePacks
409 std::vector<LangPack> availableLangs;
410 std::vector<DWORD> installedLangs;
411 } languagePacks;
413 // set up the language selecting combobox
414 CString path = CPathUtils::GetAppParentDirectory();
415 path = path + _T("Languages\\");
416 CSimpleFileFind finder(path, _T("*.dll"));
417 while (finder.FindNextFileNoDirectories())
419 CString file = finder.GetFilePath();
420 CString filename = finder.GetFileName();
421 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
423 CString sVer = _T(STRPRODUCTVER);
424 sVer = sVer.Left(sVer.ReverseFind('.'));
425 CString sFileVer = CPathUtils::GetVersionFromFile(file);
426 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
427 CString sLoc = filename.Mid(12);
428 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
429 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
430 continue;
431 DWORD loc = _tstoi(filename.Mid(12));
432 languagePacks.installedLangs.push_back(loc);
437 git_config_get_multivar_foreach(versioncheck, "tortoisegit.langs", nullptr, [](const git_config_entry* configentry, void* payload) -> int
439 LanguagePacks* languagePacks = (LanguagePacks*)payload;
440 CString langs = CUnicodeUtils::GetUnicode(configentry->value);
442 int nextTokenPos = langs.Find(_T(";"), 5); // be extensible for the future
443 if (nextTokenPos > 0)
444 langs = langs.Left(nextTokenPos);
445 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
447 DWORD loc = _tstoi(langs.Mid(0, 4));
448 TCHAR buf[MAX_PATH] = { 0 };
449 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
450 CString sLang2(buf);
451 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
452 if (buf[0])
454 sLang2 += _T(" (");
455 sLang2 += buf;
456 sLang2 += _T(")");
459 bool installed = std::find(languagePacks->installedLangs.begin(), languagePacks->installedLangs.end(), loc) != languagePacks->installedLangs.end();
460 LangPack pack = { sLang, sLang2, loc, langs.Mid(5), installed };
461 languagePacks->availableLangs.push_back(pack);
463 return 0;
464 }, &languagePacks);
465 std::stable_sort(languagePacks.availableLangs.begin(), languagePacks.availableLangs.end(), [&](const LangPack& a, const LangPack& b) -> int
467 return (a.m_Installed && !b.m_Installed) ? 1 : (!a.m_Installed && b.m_Installed) ? 0 : (a.m_PackName.Compare(b.m_PackName) < 0);
469 filenamePattern.Empty();
470 versioncheck.GetString(_T("tortoisegit.languagepackfilename"), filenamePattern);
471 if (filenamePattern.IsEmpty())
472 filenamePattern = _T("TortoiseGit-LanguagePack-%1!s!-%2!s!bit-%3!s!.msi");
473 for (auto langs : languagePacks.availableLangs)
475 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), langs.m_PackName);
476 m_ctrlFiles.SetItemText(pos, 1, langs.m_LangName);
478 CString filename;
479 filename.FormatMessage(filenamePattern, version, x86x64, langs.m_LangCode, langs.m_LocaleID);
480 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
482 if (langs.m_Installed)
483 m_ctrlFiles.SetCheck(pos , TRUE);
485 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
488 void CCheckForUpdatesDlg::FillChangelog(CAutoConfig& versioncheck, bool official)
490 ProjectProperties pp;
491 pp.lProjectLanguage = -1;
492 if (versioncheck.GetString(_T("tortoisegit.issuesurl"), pp.sUrl))
493 pp.sUrl = _T("https://code.google.com/p/tortoisegit/issues/detail?id=%BUGID%");
494 if (!pp.sUrl.IsEmpty())
496 pp.SetCheckRe(_T("[Ii]ssues?:?(\\s*(,|and)?\\s*#?\\d+)+"));
497 pp.SetBugIDRe(_T("(\\d+)"));
499 m_cLogMessage.Init(pp);
501 CString sChangelogURL;
502 versioncheck.GetString(_T("TortoiseGit.changelogurl"), sChangelogURL);
503 if (sChangelogURL.IsEmpty())
504 sChangelogURL = _T("https://versioncheck.tortoisegit.org/changelog.txt");
505 else
507 CString tmp(sChangelogURL);
508 sChangelogURL.FormatMessage(tmp, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, m_updateDownloader->m_sWindowsPlatform, m_updateDownloader->m_sWindowsVersion, m_updateDownloader->m_sWindowsServicePack);
511 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
512 DWORD err;
513 if ((err = m_updateDownloader->DownloadFile(sChangelogURL, tempchangelogfile, false)) != ERROR_SUCCESS)
515 CString msg = _T("Could not load changelog.\r\nError: ") + GetWinINetError(err) + _T(" (on ") + sChangelogURL + _T(")");
516 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>((LPCTSTR)msg));
517 return;
519 if (official)
521 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
522 if ((err = m_updateDownloader->DownloadFile(sChangelogURL + SIGNATURE_FILE_ENDING, signatureTempfile, false)) != ERROR_SUCCESS || VerifyIntegrity(tempchangelogfile, signatureTempfile, m_updateDownloader))
524 CString error = _T("Could not verify digital signature.");
525 if (err)
526 error += _T("\r\nError: ") + GetWinINetError(err) + _T(" (on ") + sChangelogURL + SIGNATURE_FILE_ENDING + _T(")");
527 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>((LPCTSTR)error));
528 DeleteUrlCacheEntry(sChangelogURL);
529 DeleteUrlCacheEntry(sChangelogURL + SIGNATURE_FILE_ENDING);
530 return;
534 CString temp;
535 CStdioFile file;
536 if (file.Open(tempchangelogfile, CFile::modeRead | CFile::typeBinary))
538 std::unique_ptr<BYTE[]> buf(new BYTE[(UINT)file.GetLength()]);
539 UINT read = file.Read(buf.get(), (UINT)file.GetLength());
540 bool skipBom = read >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF;
541 g_Git.StringAppend(&temp, buf.get() + (skipBom ? 3 : 0), CP_UTF8, read - (skipBom ? 3 : 0));
543 else
544 temp = _T("Could not open downloaded changelog file.");
545 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>((LPCTSTR)temp));
548 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
550 if (nIDEvent == 100)
552 if (m_bThreadRunning == FALSE)
554 if (m_bShowInfo)
556 m_bVisible = TRUE;
557 ShowWindow(SW_SHOWNORMAL);
559 else
561 EndDialog(0);
565 CStandAloneDialog::OnTimer(nIDEvent);
568 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
570 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
571 if (m_bVisible == FALSE)
572 lpwndpos->flags &= ~SWP_SHOWWINDOW;
575 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
577 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
578 SetCursor(hCur);
579 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
582 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
584 CString title;
585 m_ctrlUpdate.GetWindowText(title);
586 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
588 bool isOneSelected = false;
589 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
591 if (m_ctrlFiles.GetCheck(i))
593 isOneSelected = true;
594 break;
597 if (!isOneSelected)
598 return;
600 m_eventStop.ResetEvent();
602 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
603 if (m_pDownloadThread != NULL)
605 m_pDownloadThread->m_bAutoDelete = FALSE;
606 m_pDownloadThread->ResumeThread();
608 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
610 else
612 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
615 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
617 // Abort
618 m_eventStop.SetEvent();
620 else
622 CString folder = GetDownloadsDirectory();
623 if (m_ctrlUpdate.GetCurrentEntry() == 0)
625 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
627 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
628 if (m_ctrlFiles.GetCheck(i) == TRUE)
629 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
631 CStandAloneDialog::OnOK();
633 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
635 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
638 m_ctrlUpdate.SetCurrentEntry(0);
642 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
644 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
647 bool CCheckForUpdatesDlg::Download(CString filename)
649 CString url = m_sFilesURL + filename;
650 CString destFilename = GetDownloadsDirectory() + filename;
651 if (PathFileExists(destFilename) && PathFileExists(destFilename + SIGNATURE_FILE_ENDING))
653 if (VerifyIntegrity(destFilename, destFilename + SIGNATURE_FILE_ENDING, m_updateDownloader) == 0)
654 return true;
655 else
657 DeleteFile(destFilename);
658 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
659 DeleteUrlCacheEntry(url);
660 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
664 m_progress.SetRange32(0, 1);
665 m_progress.SetPos(0);
666 m_progress.ShowWindow(SW_SHOW);
667 if (m_pTaskbarList)
669 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
670 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 1);
673 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
674 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
675 DWORD ret = m_updateDownloader->DownloadFile(url, tempfile, true);
676 if (!ret)
678 ret = m_updateDownloader->DownloadFile(url + SIGNATURE_FILE_ENDING, signatureTempfile, true);
679 if (ret)
680 m_sErrors += url + SIGNATURE_FILE_ENDING + _T(": ") + GetWinINetError(ret) + _T("\r\n");
681 m_progress.SetPos(m_progress.GetPos() + 1);
682 if (m_pTaskbarList)
684 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
685 int minValue, maxValue;
686 m_progress.GetRange(minValue, maxValue);
687 m_pTaskbarList->SetProgressValue(m_hWnd, m_progress.GetPos(), maxValue);
690 else
691 m_sErrors += url + _T(": ") + GetWinINetError(ret) + _T("\r\n");
692 if (!ret)
694 if (VerifyIntegrity(tempfile, signatureTempfile, m_updateDownloader) == 0)
696 DeleteFile(destFilename);
697 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
698 MoveFile(tempfile, destFilename);
699 MoveFile(signatureTempfile, destFilename + SIGNATURE_FILE_ENDING);
700 return true;
702 m_sErrors += url + SIGNATURE_FILE_ENDING + _T(": Broken digital signature.\r\n");
703 DeleteUrlCacheEntry(url);
704 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
706 return false;
709 UINT CCheckForUpdatesDlg::DownloadThread()
711 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
712 m_sErrors.Empty();
713 BOOL result = TRUE;
714 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
716 m_ctrlFiles.EnsureVisible(i, FALSE);
717 CRect rect;
718 m_ctrlFiles.GetItemRect(i, &rect, LVIR_BOUNDS);
719 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
720 if (m_ctrlFiles.GetCheck(i) == TRUE)
722 data->m_status = CUpdateListCtrl::STATUS_DOWNLOADING;
723 m_ctrlFiles.InvalidateRect(rect);
724 if (Download(data->m_filename))
725 data->m_status = CUpdateListCtrl::STATUS_SUCCESS;
726 else
728 data->m_status = CUpdateListCtrl::STATUS_FAIL;
729 result = FALSE;
732 else
733 data->m_status = CUpdateListCtrl::STATUS_IGNORE;
734 m_ctrlFiles.InvalidateRect(rect);
737 ::PostMessage(GetSafeHwnd(), WM_USER_ENDDOWNLOAD, 0, 0);
739 return result;
742 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
744 ASSERT(m_pDownloadThread != NULL);
746 // wait until the thread terminates
747 DWORD dwExitCode;
748 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
749 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
751 // make sure we always have the correct exit code
752 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
754 delete m_pDownloadThread;
755 m_pDownloadThread = NULL;
757 m_progress.ShowWindow(SW_HIDE);
759 if (dwExitCode == TRUE)
761 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
762 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
763 m_ctrlUpdate.Invalidate();
764 if (m_pTaskbarList)
765 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
767 else
769 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
770 if (m_pTaskbarList)
771 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
772 CString tmp;
773 tmp.LoadString(IDS_ERR_FAILEDUPDATEDOWNLOAD);
774 if (!m_sErrors.IsEmpty())
775 tmp += _T("\r\n\r\nErrors:\r\n") + m_sErrors;
776 CMessageBox::Show(NULL, tmp, _T("TortoiseGit"), MB_ICONERROR);
777 if (m_pTaskbarList)
778 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
781 return 0;
784 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
786 ASSERT(lParam != NULL);
788 LPCTSTR changelog = reinterpret_cast<LPCTSTR>(lParam);
789 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
790 m_cLogMessage.SetText(changelog);
791 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
792 m_cLogMessage.Call(SCI_GOTOPOS, 0);
794 return 0;
797 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
799 CString folder;
801 if (SysInfo::Instance().IsVistaOrLater())
803 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
804 if (hShell)
806 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
807 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
808 if (pfnSHGetKnownFolderPath)
810 wchar_t * wcharPtr = 0;
811 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
812 if (SUCCEEDED(hr))
814 folder = wcharPtr;
815 CoTaskMemFree(static_cast<void*>(wcharPtr));
816 return folder.TrimRight(_T("\\")) + _T("\\");
822 TCHAR szPath[MAX_PATH] = {0};
823 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
824 folder = szPath;
825 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
826 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
827 return downloads;
829 return folder;
832 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
834 const CUpdateDownloader::DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<CUpdateDownloader::DOWNLOADSTATUS *>(lParam);
835 if (pDownloadStatus != NULL)
837 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(CUpdateDownloader::DOWNLOADSTATUS)));
839 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
840 m_progress.SetPos(pDownloadStatus->ulProgress);
841 if (m_pTaskbarList)
843 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
844 m_pTaskbarList->SetProgressValue(m_hWnd, pDownloadStatus->ulProgress, pDownloadStatus->ulProgressMax);
848 return 0;
851 LRESULT CCheckForUpdatesDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
853 m_pTaskbarList.Release();
854 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
855 return 0;
858 CString CCheckForUpdatesDlg::GetWinINetError(DWORD err)
860 CString readableError = CFormatMessageWrapper(err);
861 if (readableError.IsEmpty())
863 CString modules[] = { _T("wininet.dll"), _T("urlmon.dll") };
864 for (auto module : modules)
866 LPTSTR buffer;
867 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE, GetModuleHandle(module), err, 0, (LPTSTR)&buffer, 0, NULL);
868 readableError = buffer;
869 LocalFree(buffer);
870 if (!readableError.IsEmpty())
871 break;
874 return readableError.Trim();
877 void CCheckForUpdatesDlg::OnBnClickedDonotaskagain()
879 if (CMessageBox::Show(GetSafeHwnd(), IDS_DISABLEUPDATECHECKS, IDS_APPNAME, 2, IDI_QUESTION, IDS_DISABLEUPDATECHECKSBUTTON, IDS_ABORTBUTTON) == 1)
881 CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck")) = FALSE;
882 OnOK();