Switch to a ini/git config style version check file
[TortoiseGit.git] / src / TortoiseProc / CheckForUpdatesDlg.cpp
blob8de40c2917cd814a79929853752f168a9963027d
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 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("https://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("://versioncheck.tortoisegit.org/version-preview.txt");
198 SetDlgItemText(IDC_SOURCE, _T("Using preview release channel"));
200 else
201 sCheckURL = _T("://versioncheck.tortoisegit.org/version.txt");
202 if (SysInfo::Instance().IsVistaOrLater()) // we need SNI support
203 sCheckURL = _T("https") + sCheckURL;
204 else
205 sCheckURL = _T("http") + sCheckURL;
209 if (!official)
210 SetDlgItemText(IDC_SOURCE, _T("Using (unofficial) release channel: ") + sCheckURL);
212 CString ver;
213 CAutoConfig versioncheck(true);
214 CString errorText;
215 BOOL ret = m_updateDownloader->DownloadFile(sCheckURL, tempfile, false);
216 if (!ret && official)
218 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
219 ret = m_updateDownloader->DownloadFile(sCheckURL + SIGNATURE_FILE_ENDING, signatureTempfile, false);
220 if (!ret && VerifyIntegrity(tempfile, signatureTempfile, m_updateDownloader))
222 SetDlgItemText(IDC_CHECKRESULT, _T("Could not verify digital signature."));
223 DeleteUrlCacheEntry(sCheckURL);
224 DeleteUrlCacheEntry(sCheckURL + SIGNATURE_FILE_ENDING);
225 goto finish;
228 else if (ret)
230 DeleteUrlCacheEntry(sCheckURL);
231 if (CRegDWORD(_T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\GlobalUserOffline"), 0))
232 errorText.LoadString(IDS_OFFLINEMODE); // offline mode enabled
233 else
234 errorText.Format(IDS_CHECKNEWER_NETERROR_FORMAT, ret);
235 SetDlgItemText(IDC_CHECKRESULT, errorText);
236 goto finish;
239 git_config_add_file_ondisk(versioncheck, CUnicodeUtils::GetUTF8(tempfile), GIT_CONFIG_LEVEL_GLOBAL, 0);
241 unsigned int major, minor, micro, build;
242 major = minor = micro = build = 0;
243 unsigned __int64 version = 0;
245 if (!versioncheck.GetString(_T("tortoisegit.version"), ver))
247 CString vertemp = ver;
248 major = _ttoi(vertemp);
249 vertemp = vertemp.Mid(vertemp.Find('.') + 1);
250 minor = _ttoi(vertemp);
251 vertemp = vertemp.Mid(vertemp.Find('.') + 1);
252 micro = _ttoi(vertemp);
253 vertemp = vertemp.Mid(vertemp.Find('.') + 1);
254 build = _ttoi(vertemp);
255 version = major;
256 version <<= 16;
257 version += minor;
258 version <<= 16;
259 version += micro;
260 version <<= 16;
261 version += build;
263 if (version == 0)
265 temp.LoadString(IDS_CHECKNEWER_NETERROR);
266 SetDlgItemText(IDC_CHECKRESULT, temp);
267 goto finish;
270 // another versionstring for the filename can be provided
271 // this is needed for preview releases
272 vertemp.Empty();
273 versioncheck.GetString(_T("tortoisegit.versionstring"), vertemp);
274 if (!vertemp.IsEmpty())
275 ver = vertemp;
277 else
279 errorText = _T("Could not parse version check file: ") + g_Git.GetLibGit2LastErr();
280 SetDlgItemText(IDC_CHECKRESULT, errorText);
281 DeleteUrlCacheEntry(sCheckURL);
282 goto finish;
286 BOOL bNewer = FALSE;
287 if (m_bForce)
288 bNewer = TRUE;
289 else if (major > TGIT_VERMAJOR)
290 bNewer = TRUE;
291 else if ((minor > TGIT_VERMINOR) && (major == TGIT_VERMAJOR))
292 bNewer = TRUE;
293 else if ((micro > TGIT_VERMICRO) && (minor == TGIT_VERMINOR) && (major == TGIT_VERMAJOR))
294 bNewer = TRUE;
295 else if ((build > TGIT_VERBUILD) && (micro == TGIT_VERMICRO) && (minor == TGIT_VERMINOR) && (major == TGIT_VERMAJOR))
296 bNewer = TRUE;
298 CString versionstr;
299 versionstr.Format(_T("%u.%u.%u.%u"), major, minor, micro, build);
300 if (versionstr != ver)
301 versionstr += _T(" (") + ver + _T(")");
302 temp.Format(IDS_CHECKNEWER_CURRENTVERSION, (LPCTSTR)versionstr);
303 SetDlgItemText(IDC_CURRENTVERSION, temp);
305 if (bNewer)
307 versioncheck.GetString(_T("tortoisegit.infotext"), temp);
308 if (!temp.IsEmpty())
310 CString tempLink;
311 versioncheck.GetString(_T("tortoisegit.infotexturl"), tempLink);
312 if (!tempLink.IsEmpty()) // find out the download link-URL, if any
313 m_sUpdateDownloadLink = tempLink;
315 else
316 temp.LoadString(IDS_CHECKNEWER_NEWERVERSIONAVAILABLE);
317 SetDlgItemText(IDC_CHECKRESULT, temp);
319 FillChangelog(versioncheck, official);
320 FillDownloads(versioncheck, ver);
322 // Show download controls
323 RECT rectWindow, rectProgress, rectGroupDownloads, rectOKButton;
324 GetWindowRect(&rectWindow);
325 m_progress.GetWindowRect(&rectProgress);
326 GetDlgItem(IDC_GROUP_DOWNLOADS)->GetWindowRect(&rectGroupDownloads);
327 GetDlgItem(IDOK)->GetWindowRect(&rectOKButton);
328 LONG bottomDistance = rectWindow.bottom - rectOKButton.bottom;
329 OffsetRect(&rectOKButton, 0, (rectGroupDownloads.bottom + (rectGroupDownloads.bottom - rectProgress.bottom)) - rectOKButton.top);
330 rectWindow.bottom = rectOKButton.bottom + bottomDistance;
331 MoveWindow(&rectWindow);
332 ::MapWindowPoints(NULL, GetSafeHwnd(), (LPPOINT)&rectOKButton, 2);
333 GetDlgItem(IDOK)->MoveWindow(&rectOKButton);
334 m_ctrlFiles.ShowWindow(SW_SHOW);
335 GetDlgItem(IDC_GROUP_DOWNLOADS)->ShowWindow(SW_SHOW);
336 CenterWindow();
337 m_bShowInfo = TRUE;
339 else if (m_bShowInfo)
341 temp.LoadString(IDS_CHECKNEWER_YOURUPTODATE);
342 SetDlgItemText(IDC_CHECKRESULT, temp);
343 FillChangelog(versioncheck, official);
347 finish:
348 if (!m_sUpdateDownloadLink.IsEmpty())
350 m_link.ShowWindow(SW_SHOW);
351 m_link.SetURL(m_sUpdateDownloadLink);
353 m_bThreadRunning = FALSE;
354 DialogEnableWindow(IDOK, TRUE);
355 return 0;
358 void CCheckForUpdatesDlg::FillDownloads(CAutoConfig& versioncheck, const CString version)
360 #if WIN64
361 const CString x86x64 = _T("64");
362 #else
363 const CString x86x64 = _T("32");
364 #endif
366 versioncheck.GetString(_T("tortoisegit.baseurl"), m_sFilesURL);
367 if (m_sFilesURL.IsEmpty())
368 m_sFilesURL.Format(_T("http://updater.download.tortoisegit.org/tgit/%s/"), version);
370 m_ctrlFiles.InsertItem(0, _T("TortoiseGit"));
371 CString filename;
372 filename.Format(_T("TortoiseGit-%s-%sbit.msi"), version, x86x64);
373 m_ctrlFiles.SetItemData(0, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
374 m_ctrlFiles.SetCheck(0 , TRUE);
376 struct LangPack
378 CString m_PackName;
379 CString m_LangName;
380 DWORD m_LocaleID;
381 CString m_LangCode;
382 bool m_Installed;
384 struct LanguagePacks
386 std::vector<LangPack> availableLangs;
387 std::vector<DWORD> installedLangs;
388 } languagePacks;
390 // set up the language selecting combobox
391 CString path = CPathUtils::GetAppParentDirectory();
392 path = path + _T("Languages\\");
393 CSimpleFileFind finder(path, _T("*.dll"));
394 while (finder.FindNextFileNoDirectories())
396 CString file = finder.GetFilePath();
397 CString filename = finder.GetFileName();
398 if (filename.Left(12).CompareNoCase(_T("TortoiseProc")) == 0)
400 CString sVer = _T(STRPRODUCTVER);
401 sVer = sVer.Left(sVer.ReverseFind('.'));
402 CString sFileVer = CPathUtils::GetVersionFromFile(file);
403 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
404 CString sLoc = filename.Mid(12);
405 sLoc = sLoc.Left(sLoc.GetLength() - 4); // cut off ".dll"
406 if ((sLoc.Left(2) == L"32") && (sLoc.GetLength() > 5))
407 continue;
408 DWORD loc = _tstoi(filename.Mid(12));
409 languagePacks.installedLangs.push_back(loc);
414 git_config_get_multivar_foreach(versioncheck, "tortoisegit.langs", nullptr, [](const git_config_entry* configentry, void* payload) -> int
416 LanguagePacks* languagePacks = (LanguagePacks*)payload;
417 CString langs = CUnicodeUtils::GetUnicode(configentry->value);
419 int nextTokenPos = langs.Find(_T(";"), 5); // be extensible for the future
420 if (nextTokenPos > 0)
421 langs = langs.Left(nextTokenPos);
422 CString sLang = _T("TortoiseGit Language Pack ") + langs.Mid(5);
424 DWORD loc = _tstoi(langs.Mid(0, 4));
425 TCHAR buf[MAX_PATH] = { 0 };
426 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
427 CString sLang2(buf);
428 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
429 if (buf[0])
431 sLang2 += _T(" (");
432 sLang2 += buf;
433 sLang2 += _T(")");
436 bool installed = std::find(languagePacks->installedLangs.begin(), languagePacks->installedLangs.end(), loc) != languagePacks->installedLangs.end();
437 LangPack pack = { sLang, sLang2, loc, langs.Mid(5), installed };
438 languagePacks->availableLangs.push_back(pack);
440 return 0;
441 }, &languagePacks);
442 std::stable_sort(languagePacks.availableLangs.begin(), languagePacks.availableLangs.end(), [&](const LangPack& a, const LangPack& b) -> int
444 return (a.m_Installed && !b.m_Installed) ? 1 : (!a.m_Installed && b.m_Installed) ? 0 : (a.m_PackName.Compare(b.m_PackName) < 0);
446 for (auto langs : languagePacks.availableLangs)
448 int pos = m_ctrlFiles.InsertItem(m_ctrlFiles.GetItemCount(), langs.m_PackName);
449 m_ctrlFiles.SetItemText(pos, 1, langs.m_LangName);
451 CString filename;
452 filename.Format(_T("TortoiseGit-LanguagePack-%s-%sbit-%s.msi"), version, x86x64, langs.m_LangCode);
453 m_ctrlFiles.SetItemData(pos, (DWORD_PTR)(new CUpdateListCtrl::Entry(filename, CUpdateListCtrl::STATUS_NONE)));
455 if (langs.m_Installed)
456 m_ctrlFiles.SetCheck(pos , TRUE);
458 DialogEnableWindow(IDC_BUTTON_UPDATE, TRUE);
461 void CCheckForUpdatesDlg::FillChangelog(CAutoConfig& versioncheck, bool official)
463 CString sChangelogURL;
464 versioncheck.GetString(_T("TortoiseGit.changelogurl"), sChangelogURL);
465 if (sChangelogURL.IsEmpty())
466 sChangelogURL = _T("https://versioncheck.tortoisegit.org/changelog.txt");
468 CString tempchangelogfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
469 if (m_updateDownloader->DownloadFile(sChangelogURL, tempchangelogfile, false))
471 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not load changelog.")));
472 return;
474 if (official)
476 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
477 if (m_updateDownloader->DownloadFile(sChangelogURL + SIGNATURE_FILE_ENDING, signatureTempfile, false) || VerifyIntegrity(tempchangelogfile, signatureTempfile, m_updateDownloader))
479 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>(_T("Could not verify digital signature.")));
480 DeleteUrlCacheEntry(sChangelogURL);
481 DeleteUrlCacheEntry(sChangelogURL + SIGNATURE_FILE_ENDING);
482 return;
486 CString temp;
487 CStdioFile file;
488 if (file.Open(tempchangelogfile, CFile::modeRead | CFile::typeBinary))
490 std::unique_ptr<BYTE[]> buf(new BYTE[(UINT)file.GetLength()]);
491 UINT read = file.Read(buf.get(), (UINT)file.GetLength());
492 bool skipBom = read >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF;
493 g_Git.StringAppend(&temp, buf.get() + (skipBom ? 3 : 0), CP_UTF8, read - (skipBom ? 3 : 0));
495 ::SendMessage(m_hWnd, WM_USER_FILLCHANGELOG, 0, reinterpret_cast<LPARAM>((LPCTSTR)temp));
498 void CCheckForUpdatesDlg::OnTimer(UINT_PTR nIDEvent)
500 if (nIDEvent == 100)
502 if (m_bThreadRunning == FALSE)
504 if (m_bShowInfo)
506 m_bVisible = TRUE;
507 ShowWindow(SW_SHOWNORMAL);
509 else
511 EndDialog(0);
515 CStandAloneDialog::OnTimer(nIDEvent);
518 void CCheckForUpdatesDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
520 CStandAloneDialog::OnWindowPosChanging(lpwndpos);
521 if (m_bVisible == FALSE)
522 lpwndpos->flags &= ~SWP_SHOWWINDOW;
525 BOOL CCheckForUpdatesDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
527 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
528 SetCursor(hCur);
529 return CStandAloneDialogTmpl<CDialog>::OnSetCursor(pWnd, nHitTest, message);
532 void CCheckForUpdatesDlg::OnBnClickedButtonUpdate()
534 CString title;
535 m_ctrlUpdate.GetWindowText(title);
536 if (m_pDownloadThread == NULL && title == CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)))
538 bool isOneSelected = false;
539 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
541 if (m_ctrlFiles.GetCheck(i))
543 isOneSelected = true;
544 break;
547 if (!isOneSelected)
548 return;
550 m_eventStop.ResetEvent();
552 m_pDownloadThread = ::AfxBeginThread(DownloadThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
553 if (m_pDownloadThread != NULL)
555 m_pDownloadThread->m_bAutoDelete = FALSE;
556 m_pDownloadThread->ResumeThread();
558 GetDlgItem(IDC_BUTTON_UPDATE)->SetWindowText(CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
560 else
562 CMessageBox::Show(NULL, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
565 else if (title == CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)))
567 // Abort
568 m_eventStop.SetEvent();
570 else
572 CString folder = GetDownloadsDirectory();
573 if (m_ctrlUpdate.GetCurrentEntry() == 0)
575 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
577 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
578 if (m_ctrlFiles.GetCheck(i) == TRUE)
579 ShellExecute(NULL, _T("open"), folder + data->m_filename, NULL, NULL, SW_SHOWNORMAL);
581 CStandAloneDialog::OnOK();
583 else if (m_ctrlUpdate.GetCurrentEntry() == 1)
585 ShellExecute(NULL, _T("open"), folder, NULL, NULL, SW_SHOWNORMAL);
588 m_ctrlUpdate.SetCurrentEntry(0);
592 UINT CCheckForUpdatesDlg::DownloadThreadEntry(LPVOID pVoid)
594 return ((CCheckForUpdatesDlg*)pVoid)->DownloadThread();
597 bool CCheckForUpdatesDlg::Download(CString filename)
599 CString url = m_sFilesURL + filename;
600 CString destFilename = GetDownloadsDirectory() + filename;
601 if (PathFileExists(destFilename) && PathFileExists(destFilename + SIGNATURE_FILE_ENDING))
603 if (VerifyIntegrity(destFilename, destFilename + SIGNATURE_FILE_ENDING, m_updateDownloader) == 0)
604 return true;
605 else
607 DeleteFile(destFilename);
608 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
609 DeleteUrlCacheEntry(url);
610 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
614 m_progress.SetRange32(0, 1);
615 m_progress.SetPos(0);
616 m_progress.ShowWindow(SW_SHOW);
617 if (m_pTaskbarList)
619 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
620 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 1);
623 CString tempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
624 CString signatureTempfile = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
625 BOOL ret = m_updateDownloader->DownloadFile(url, tempfile, true);
626 if (!ret)
628 ret = m_updateDownloader->DownloadFile(url + SIGNATURE_FILE_ENDING, signatureTempfile, true);
629 m_progress.SetPos(m_progress.GetPos() + 1);
630 if (m_pTaskbarList)
632 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
633 int minValue, maxValue;
634 m_progress.GetRange(minValue, maxValue);
635 m_pTaskbarList->SetProgressValue(m_hWnd, m_progress.GetPos(), maxValue);
638 if (!ret)
640 if (VerifyIntegrity(tempfile, signatureTempfile, m_updateDownloader) == 0)
642 DeleteFile(destFilename);
643 DeleteFile(destFilename + SIGNATURE_FILE_ENDING);
644 MoveFile(tempfile, destFilename);
645 MoveFile(signatureTempfile, destFilename + SIGNATURE_FILE_ENDING);
646 return true;
648 DeleteUrlCacheEntry(url);
649 DeleteUrlCacheEntry(url + SIGNATURE_FILE_ENDING);
651 return false;
654 UINT CCheckForUpdatesDlg::DownloadThread()
656 m_ctrlFiles.SetExtendedStyle(m_ctrlFiles.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);
658 BOOL result = TRUE;
659 for (int i = 0; i < (int)m_ctrlFiles.GetItemCount(); ++i)
661 m_ctrlFiles.EnsureVisible(i, FALSE);
662 CRect rect;
663 m_ctrlFiles.GetItemRect(i, &rect, LVIR_BOUNDS);
664 CUpdateListCtrl::Entry *data = (CUpdateListCtrl::Entry *)m_ctrlFiles.GetItemData(i);
665 if (m_ctrlFiles.GetCheck(i) == TRUE)
667 data->m_status = CUpdateListCtrl::STATUS_DOWNLOADING;
668 m_ctrlFiles.InvalidateRect(rect);
669 if (Download(data->m_filename))
670 data->m_status = CUpdateListCtrl::STATUS_SUCCESS;
671 else
673 data->m_status = CUpdateListCtrl::STATUS_FAIL;
674 result = FALSE;
677 else
678 data->m_status = CUpdateListCtrl::STATUS_IGNORE;
679 m_ctrlFiles.InvalidateRect(rect);
682 ::PostMessage(GetSafeHwnd(), WM_USER_ENDDOWNLOAD, 0, 0);
684 return result;
687 LRESULT CCheckForUpdatesDlg::OnEndDownload(WPARAM, LPARAM)
689 ASSERT(m_pDownloadThread != NULL);
691 // wait until the thread terminates
692 DWORD dwExitCode;
693 if (::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
694 ::WaitForSingleObject(m_pDownloadThread->m_hThread, INFINITE);
696 // make sure we always have the correct exit code
697 ::GetExitCodeThread(m_pDownloadThread->m_hThread, &dwExitCode);
699 delete m_pDownloadThread;
700 m_pDownloadThread = NULL;
702 m_progress.ShowWindow(SW_HIDE);
704 if (dwExitCode == TRUE)
706 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_INSTALL)));
707 m_ctrlUpdate.AddEntry(CString(MAKEINTRESOURCE(IDS_CHECKUPDATE_DESTFOLDER)));
708 m_ctrlUpdate.Invalidate();
709 if (m_pTaskbarList)
710 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
712 else
714 m_ctrlUpdate.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_DOWNLOAD)));
715 if (m_pTaskbarList)
716 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
717 CMessageBox::Show(NULL, IDS_ERR_FAILEDUPDATEDOWNLOAD, IDS_APPNAME, MB_ICONERROR);
718 if (m_pTaskbarList)
719 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
722 return 0;
725 LRESULT CCheckForUpdatesDlg::OnFillChangelog(WPARAM, LPARAM lParam)
727 ASSERT(lParam != NULL);
729 LPCTSTR changelog = reinterpret_cast<LPCTSTR>(lParam);
730 m_cLogMessage.Call(SCI_SETREADONLY, FALSE);
731 m_cLogMessage.SetText(changelog);
732 m_cLogMessage.Call(SCI_SETREADONLY, TRUE);
733 m_cLogMessage.Call(SCI_GOTOPOS, 0);
735 return 0;
738 CString CCheckForUpdatesDlg::GetDownloadsDirectory()
740 CString folder;
742 if (SysInfo::Instance().IsVistaOrLater())
744 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
745 if (hShell)
747 typedef HRESULT STDAPICALLTYPE SHGetKnownFolderPathFN(__in REFKNOWNFOLDERID rfid, __in DWORD dwFlags, __in_opt HANDLE hToken, __deref_out PWSTR *ppszPath);
748 SHGetKnownFolderPathFN *pfnSHGetKnownFolderPath = (SHGetKnownFolderPathFN*)GetProcAddress(hShell, "SHGetKnownFolderPath");
749 if (pfnSHGetKnownFolderPath)
751 wchar_t * wcharPtr = 0;
752 HRESULT hr = pfnSHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_CREATE, NULL, &wcharPtr);
753 if (SUCCEEDED(hr))
755 folder = wcharPtr;
756 CoTaskMemFree(static_cast<void*>(wcharPtr));
757 return folder.TrimRight(_T("\\")) + _T("\\");
763 TCHAR szPath[MAX_PATH] = {0};
764 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
765 folder = szPath;
766 CString downloads = folder.TrimRight(_T("\\")) + _T("\\Downloads\\");
767 if ((PathFileExists(downloads) && PathIsDirectory(downloads)) || (!PathFileExists(downloads) && CreateDirectory(downloads, NULL)))
768 return downloads;
770 return folder;
773 LRESULT CCheckForUpdatesDlg::OnDisplayStatus(WPARAM, LPARAM lParam)
775 const CUpdateDownloader::DOWNLOADSTATUS *const pDownloadStatus = reinterpret_cast<CUpdateDownloader::DOWNLOADSTATUS *>(lParam);
776 if (pDownloadStatus != NULL)
778 ASSERT(::AfxIsValidAddress(pDownloadStatus, sizeof(CUpdateDownloader::DOWNLOADSTATUS)));
780 m_progress.SetRange32(0, pDownloadStatus->ulProgressMax);
781 m_progress.SetPos(pDownloadStatus->ulProgress);
782 if (m_pTaskbarList)
784 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
785 m_pTaskbarList->SetProgressValue(m_hWnd, pDownloadStatus->ulProgress, pDownloadStatus->ulProgressMax);
789 return 0;
792 LRESULT CCheckForUpdatesDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
794 m_pTaskbarList.Release();
795 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
796 return 0;