No need for an extra lookup for HEAD
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blobd3f1e2ac801bba5e20e2359e42c70b882d6214fb
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(".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 END_MESSAGE_MAP()
82 BOOL CCheckForUpdatesDlg::OnInitDialog()
84 CStandAloneDialog::OnInitDialog();
85 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
87 CString temp;
88 temp.Format(IDS_CHECKNEWER_YOURVERSION, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
89 SetDlgItemText(IDC_YOURVERSION, temp);
91 DialogEnableWindow(IDOK, FALSE);
93 m_pTaskbarList.Release();
94 if (FAILED(m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList)))
95 m_pTaskbarList = nullptr;
97 // hide download controls
98 m_ctrlFiles.ShowWindow(SW_HIDE);
99 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_HIDE);
100 RECT rectWindow, rectGroupDownloads, rectOKButton;
101 GetWindowRect(&rectWindow);
102 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
103 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
104 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
105 OffsetRect(&rectOKButton, 0, rectGroupDownloads.top - rectOKButton.top);
106 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
107 MoveWindow(&rectWindow);
108 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
109 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
111 temp.LoadString(IDS_STATUSLIST_COLFILE);
112 m_ctrlFiles.InsertColumn(0, temp, 0, -1);
113 m_ctrlFiles.InsertColumn(1, temp, 0, -1);
114 m_ctrlFiles.SetExtendedStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_CHECKBOXES);
115 m_ctrlFiles.SetColumnWidth(0, 350);
116 m_ctrlFiles.SetColumnWidth(1, 200);
118 ProjectProperties pp;
119 pp.SetCheckRe(_T("[Ii]ssues?:?(\\s*(,|and)?\\s*#?\\d+)+"));
120 pp.SetBugIDRe(_T("(\\d+)"));
121 pp.lProjectLanguage = -1;
122 pp.sUrl = _T("http://code.google.com/p/tortoisegit/issues/detail?id=%BUGID%");
123 m_cLogMessage.Init(pp);
124 m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
125 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
127 m_updateDownloader = new CUpdateDownloader(GetSafeHwnd(), m_bForce == TRUE, WM_USER_DISPLAYSTATUS, &m_eventStop);
129 if (AfxBeginThread(CheckThreadEntry, this)==NULL)
131 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
134 SetTimer(100, 1000, NULL);
135 return TRUE;
138 void CCheckForUpdatesDlg::OnDestroy()
140 for (int i = 0; i < m_ctrlFiles.GetItemCount(); ++i)
141 delete (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
143 if (m_updateDownloader)
144 delete m_updateDownloader;
146 CStandAloneDialog::OnDestroy();
149 void CCheckForUpdatesDlg::OnOK()
151 if (m_bThreadRunning || m_pDownloadThread != NULL)
152 return; // Don't exit while downloading
154 CStandAloneDialog::OnOK();
157 void CCheckForUpdatesDlg::OnCancel()
159 if (m_bThreadRunning || m_pDownloadThread != NULL)
160 return; // Don't exit while downloading
161 CStandAloneDialog::OnCancel();
164 UINT CCheckForUpdatesDlg::CheckThreadEntry(LPVOID pVoid)
166 return ((CCheckForUpdatesDlg*)pVoid)->CheckThread();
169 UINT CCheckForUpdatesDlg::CheckThread()
171 m_bThreadRunning = TRUE;
173 CString temp;
174 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
176 bool official = false;
178 CRegString checkurluser = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""));
179 CRegString checkurlmachine = CRegString(_T("Software\\TortoiseGit\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
180 CString sCheckURL = checkurluser;
181 if (sCheckURL.IsEmpty())
183 sCheckURL = checkurlmachine;
184 if (sCheckURL.IsEmpty())
186 official = true;
187 bool checkPreview = false;
188 #if PREVIEW
189 checkPreview = true;
190 #else
191 CRegStdDWORD regCheckPreview(L"Software\\TortoiseGit\\VersionCheckPreview", FALSE);
192 if (DWORD(regCheckPreview))
193 checkPreview = true;
194 #endif
195 if (checkPreview)
197 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version-preview.txt");
198 SetDlgItemText(IDC_SOURCE, _T("Using preview release channel"));
200 else
201 sCheckURL = _T("http://version.tortoisegit.googlecode.com/git/version.txt");
205 if (!official)
206 SetDlgItemText(IDC_SOURCE, _T("Using (unofficial) release channel: ") + sCheckURL);
208 CString errorText;
209 BOOL ret = m_updateDownloader->DownloadFile(sCheckURL, tempfile, false);
210 if (!ret && official)
212 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
213 ret = m_updateDownloader->DownloadFile(sCheckURL + SIGNATURE_FILE_ENDING, signatureTempfile, false);
214 if (!ret && VerifyIntegrity(tempfile, signatureTempfile, m_updateDownloader))
216 SetDlgItemText(IDC_CHECKRESULT, _T("Could not verify digital signature."));
217 DeleteUrlCacheEntry(sCheckURL);
218 DeleteUrlCacheEntry(sCheckURL + SIGNATURE_FILE_ENDING);
219 goto finish;
222 else if (ret)
224 DeleteUrlCacheEntry(sCheckURL);
225 if (CRegDWORD(_T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\GlobalUserOffline"), 0))
226 errorText.LoadString(IDS_OFFLINEMODE); // offline mode enabled
227 else
228 errorText.Format(IDS_CHECKNEWER_NETERROR_FORMAT, ret);
230 if (!ret)
234 CStdioFile file(tempfile, CFile::modeRead | CFile::shareDenyWrite);
235 CString ver;
236 unsigned int major,minor,micro,build;
237 major=minor=micro=build=0;
238 unsigned __int64 version=0;
240 if (file.ReadString(ver))
242 CString vertemp = ver;
243 // another versionstring for the filename can be provided after a semicolon
244 // this is needed for preview releases
245 int differentFilenamePos = vertemp.Find(_T(";"));
246 if (differentFilenamePos > 0)
248 vertemp = vertemp.Left(differentFilenamePos);
249 ver = ver.Mid(differentFilenamePos + 1);
252 major = _ttoi(vertemp);
253 vertemp = vertemp.Mid(vertemp.Find('.')+1);
254 minor = _ttoi(vertemp);
255 vertemp = vertemp.Mid(vertemp.Find('.')+1);
256 micro = _ttoi(vertemp);
257 vertemp = vertemp.Mid(vertemp.Find('.')+1);
258 build = _ttoi(vertemp);
259 version = major;
260 version <<= 16;
261 version += minor;
262 version <<= 16;
263 version += micro;
264 version <<= 16;
265 version += build;
269 BOOL bNewer = FALSE;
270 if (m_bForce)
271 bNewer = TRUE;
272 if (major > TGIT_VERMAJOR)
273 bNewer = TRUE;
274 else if ((minor > TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
275 bNewer = TRUE;
276 else if ((micro > TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
277 bNewer = TRUE;
278 else if ((build > TGIT_VERBUILD)&&(micro == TGIT_VERMICRO)&&(minor == TGIT_VERMINOR)&&(major == TGIT_VERMAJOR))
279 bNewer = TRUE;
281 if (version != 0)
283 CString version;
284 version.Format(_T("%u.%u.%u.%u"),major,minor,micro,build);
285 if (version != ver)
286 version += _T(" (") + ver + _T(")");
287 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)version);
288 SetDlgItemText(IDC_CURRENTVERSION, temp);
291 if (version == 0)
293 temp.LoadString(IDS_CHECKNEWER_NETERROR);
294 SetDlgItemText(IDC_CHECKRESULT, temp);
296 else if (bNewer)
298 if(file.ReadString(temp) && !temp.IsEmpty())
299 { // Read the next line, it could contain a message for the user
300 CString tempLink;
301 if(file.ReadString(tempLink) && !tempLink.IsEmpty())
302 { // Read another line to find out the download link-URL, if any
303 m_sUpdateDownloadLink = tempLink;
306 else
308 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
309 CString tempLink;
310 file.ReadString(tempLink);
312 SetDlgItemText(IDC_CHECKRESULT, temp);
313 m_bShowInfo = TRUE;
315 FillChangelog(file);
316 FillDownloads(file, ver);
318 // Show download controls
319 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
320 GetWindowRect(&rectWindow);
321 m_progress.GetWindowRect(&rectProgress);
322 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
323 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
324 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
325 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
326 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
327 MoveWindow(&rectWindow);
328 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
329 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
330 m_ctrlFiles.ShowWindow(SW_SHOW);
331 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
332 CenterWindow();
334 else
336 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
337 SetDlgItemText(IDC_CHECKRESULT, temp);
338 file.ReadString(temp);
339 file.ReadString(temp);
340 FillChangelog(file);
344 catch (CException * e)
346 e->Delete();
347 temp.LoadString(IDS_CHECKNEWER_NETERROR);
348 SetDlgItemText(IDC_CHECKRESULT, temp);
351 else
353 SetDlgItemText(IDC_CHECKRESULT, errorText);
355 if (!m_sUpdateDownloadLink.IsEmpty())
357 m_link.ShowWindow(SW_SHOW);
358 m_link.SetURL(m_sUpdateDownloadLink);
361 finish:
362 m_bThreadRunning = FALSE;
363 DialogEnableWindow(IDOK, TRUE);
364 return 0;
367 void CCheckForUpdatesDlg::FillDownloads(CStdioFile &file, CString version)
369 #if WIN64
370 const CString x86x64 = _T("64");
371 #else
372 const CString x86x64 = _T("32");
373 #endif
375 if (!file.ReadString(m_sFilesURL) || m_sFilesURL.IsEmpty())
376 m_sFilesURL.Format(_T("http://updater.download.tortoisegit.org/tgit/%s/"), version);
378 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
379 CString filename;
380 filename.Format(_T("TortoiseGit-%s-%sbit.msi"), version, x86x64);
381 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
382 m_ctrlFiles.SetCheck(0 , TRUE);
384 std::vector<DWORD> m_installedLangs;
386 // set up the language selecting combobox
387 CString path = CPathUtils::GetAppParentDirectory();
388 path = path + _T("Languages\\");
389 CSimpleFileFind finder(path, _T("*.dll"));
390 while (finder.FindNextFileNoDirectories())
392 CString file = finder.GetFilePath();
393 CString filename = finder.GetFileName();
394 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
396 CString sVer = _T(STRPRODUCTVER);
397 sVer = sVer.Left(sVer.ReverseFind('.'));
398 CString sFileVer = CPathUtils::GetVersionFromFile(file);
399 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
400 CString sLoc = filename.Mid(12);
401 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
402 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
403 continue;
404 DWORD loc = _tstoi(filename.Mid(12));
405 m_installedLangs.push_back(loc);
410 CString langs;
411 while (file.ReadString(langs) && !langs.IsEmpty())
413 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
415 DWORD loc = _tstoi(langs.Mid(0, 4));
416 TCHAR buf[MAX_PATH] = {0};
417 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
418 CString sLang2(buf);
419 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
420 if (buf[0])
422 sLang2 += _T(" (");
423 sLang2 += buf;
424 sLang2 += _T(")");
427 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), sLang);
428 m_ctrlFiles.SetItemText(pos, 1, sLang2);
430 CString filename;
431 filename.Format(_T("TortoiseGit-LanguagePack-%s-%sbit-%s.msi"), version, x86x64, langs.Mid(5));
432 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
434 if (std::find(m_installedLangs.begin(), m_installedLangs.end(), loc) != m_installedLangs.end())
435 m_ctrlFiles.SetCheck(pos , TRUE);
437 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
440 void CCheckForUpdatesDlg::FillChangelog(CStdioFile &file)
442 CString sChangelogURL;
443 if (!file.ReadString(sChangelogURL) || sChangelogURL.IsEmpty())
444 sChangelogURL = _T("http://tortoisegit.googlecode.com/git/src/Changelog.txt");
446 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
447 if (!m_updateDownloader->DownloadFile(sChangelogURL, tempchangelogfile, false))
449 CString temp;
450 CStdioFile file;
451 if (file.Open(tempchangelogfile, CFile::modeRead | CFile::typeBinary))
453 std::unique_ptr<BYTE[]> buf(new BYTE[(UINT)file.GetLength()]);
454 UINT read = file.Read(buf.get(), (UINT)file.GetLength());
455 bool skipBom = read >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF;
456 g_Git.StringAppend(&temp, buf.get() + (skipBom ? 3 : 0), CP_UTF8, read - (skipBom ? 3 : 0));
458 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>((LPCTSTR)temp));
460 else
461 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not load changelog.")));
464 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
466 if (nIDEvent == 100)
468 if (m_bThreadRunning == FALSE)
470 if (m_bShowInfo)
472 m_bVisible = TRUE;
473 ShowWindow(SW_SHOWNORMAL);
475 else
477 EndDialog(0);
481 CStandAloneDialog::OnTimer(nIDEvent);
484 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
486 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
487 if (m_bVisible == FALSE)
488 lpwndpos->flags &= ~SWP_SHOWWINDOW;
491 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
493 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
494 SetCursor(hCur);
495 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
498 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
500 CString title;
501 m_ctrlUpdate.GetWindowText(title);
502 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
504 bool isOneSelected = false;
505 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
507 if (m_ctrlFiles.GetCheck(i))
509 isOneSelected = true;
510 break;
513 if (!isOneSelected)
514 return;
516 m_eventStop.ResetEvent();
518 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
519 if (m_pDownloadThread != NULL)
521 m_pDownloadThread->m_bAutoDelete = FALSE;
522 m_pDownloadThread->ResumeThread();
524 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
526 else
528 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
531 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
533 // Abort
534 m_eventStop.SetEvent();
536 else
538 CString folder = GetDownloadsDirectory();
539 if (m_ctrlUpdate.GetCurrentEntry() == 0)
541 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
543 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
544 if (m_ctrlFiles.GetCheck(i) == TRUE)
545 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
547 CStandAloneDialog::OnOK();
549 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
551 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
554 m_ctrlUpdate.SetCurrentEntry(0);
558 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
560 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
563 bool CCheckForUpdatesDlg::Download(CString filename)
565 CString url = m_sFilesURL + filename;
566 CString destFilename = GetDownloadsDirectory() + filename;
567 if (PathFileExists(destFilename) && PathFileExists(destFilename + SIGNATURE_FILE_ENDING))
569 if (VerifyIntegrity(destFilename, destFilename + SIGNATURE_FILE_ENDING, m_updateDownloader) == 0)
570 return true;
571 else
573 DeleteFile(destFilename);
574 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
575 DeleteUrlCacheEntry(url);
576 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
580 m_progress.SetRange32(0, 1);
581 m_progress.SetPos(0);
582 m_progress.ShowWindow(SW_SHOW);
583 if (m_pTaskbarList)
585 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
586 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 1);
589 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
590 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
591 BOOL ret = m_updateDownloader->DownloadFile(url, tempfile, true);
592 if (!ret)
594 ret = m_updateDownloader->DownloadFile(url + SIGNATURE_FILE_ENDING, signatureTempfile, true);
595 m_progress.SetPos(m_progress.GetPos() + 1);
596 if (m_pTaskbarList)
598 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
599 int minValue, maxValue;
600 m_progress.GetRange(minValue, maxValue);
601 m_pTaskbarList->SetProgressValue(m_hWnd, m_progress.GetPos(), maxValue);
604 if (!ret)
606 if (VerifyIntegrity(tempfile, signatureTempfile, m_updateDownloader) == 0)
608 DeleteFile(destFilename);
609 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
610 MoveFile(tempfile, destFilename);
611 MoveFile(signatureTempfile, destFilename + SIGNATURE_FILE_ENDING);
612 return true;
614 DeleteUrlCacheEntry(url);
615 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
617 return false;
620 UINT CCheckForUpdatesDlg::DownloadThread()
622 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
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 return result;
653 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
655 ASSERT(m_pDownloadThread != NULL);
657 // wait until the thread terminates
658 DWORD dwExitCode;
659 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
660 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
662 // make sure we always have the correct exit code
663 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
665 delete m_pDownloadThread;
666 m_pDownloadThread = NULL;
668 m_progress.ShowWindow(SW_HIDE);
670 if (dwExitCode == TRUE)
672 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
673 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
674 m_ctrlUpdate.Invalidate();
675 if (m_pTaskbarList)
676 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
678 else
680 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
681 if (m_pTaskbarList)
682 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
683 CMessageBox::Show(NULL, IDS_ERR_FAILEDUPDATEDOWNLOAD, IDS_APPNAME, MB_ICONERROR);
684 if (m_pTaskbarList)
685 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
688 return 0;
691 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
693 ASSERT(lParam != NULL);
695 LPCTSTR changelog = reinterpret_cast<LPCTSTR>(lParam);
696 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
697 m_cLogMessage.SetText(changelog);
698 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
699 m_cLogMessage.Call(SCI_GOTOPOS, 0);
701 return 0;
704 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
706 CString folder;
708 if (SysInfo::Instance().IsVistaOrLater())
710 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
711 if (hShell)
713 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
714 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
715 if (pfnSHGetKnownFolderPath)
717 wchar_t * wcharPtr = 0;
718 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
719 if (SUCCEEDED(hr))
721 folder = wcharPtr;
722 CoTaskMemFree(static_cast<void*>(wcharPtr));
723 return folder.TrimRight(_T("\\")) + _T("\\");
729 TCHAR szPath[MAX_PATH] = {0};
730 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
731 folder = szPath;
732 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
733 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
734 return downloads;
736 return folder;
739 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
741 const CUpdateDownloader::DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<CUpdateDownloader::DOWNLOADSTATUS *>(lParam);
742 if (pDownloadStatus != NULL)
744 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(CUpdateDownloader::DOWNLOADSTATUS)));
746 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
747 m_progress.SetPos(pDownloadStatus->ulProgress);
748 if (m_pTaskbarList)
750 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
751 m_pTaskbarList->SetProgressValue(m_hWnd, pDownloadStatus->ulProgress, pDownloadStatus->ulProgressMax);
755 return 0;
758 LRESULT CCheckForUpdatesDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
760 m_pTaskbarList.Release();
761 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
762 return 0;