Ask before disabling auto update check
[TortoiseGit.git] / src / TortoiseProc / Settings / SetMainPage.cpp
blobf7a06d04084a0f77a3deaba93413ecfc1ba20c70
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
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 "SetMainPage.h"
23 #include "AppUtils.h"
24 #include "PathUtils.h"
25 #include "DirFileEnum.h"
26 #include "GitProgressDlg.h"
27 #include "..\version.h"
28 #include "Git.h"
29 #include "MessageBox.h"
30 #include "GitForWindows.h"
31 #include "BrowseFolder.h"
32 #include "Libraries.h"
34 IMPLEMENT_DYNAMIC(CSetMainPage, ISettingsPropPage)
35 CSetMainPage::CSetMainPage()
36 : ISettingsPropPage(CSetMainPage::IDD)
37 , m_bCheckNewer(TRUE)
38 , m_dwLanguage(0)
40 m_regLanguage = CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
41 CString temp = CRegString(REG_MSYSGIT_INSTALL, _T(""), FALSE, HKEY_LOCAL_MACHINE);
42 if(!temp.IsEmpty())
43 temp+=_T("bin");
44 m_regMsysGitPath = CRegString(REG_MSYSGIT_PATH,temp,FALSE);
46 m_regMsysGitExtranPath =CRegString(REG_MSYSGIT_EXTRA_PATH);
48 m_sMsysGitPath = m_regMsysGitPath;
49 m_sMsysGitExtranPath = m_regMsysGitExtranPath;
51 m_regCheckNewer = CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck"), TRUE);
54 CSetMainPage::~CSetMainPage()
58 void CSetMainPage::DoDataExchange(CDataExchange* pDX)
60 ISettingsPropPage::DoDataExchange(pDX);
61 DDX_Control(pDX, IDC_LANGUAGECOMBO, m_LanguageCombo);
62 m_dwLanguage = (DWORD)m_LanguageCombo.GetItemData(m_LanguageCombo.GetCurSel());
63 DDX_Text(pDX, IDC_MSYSGIT_PATH, m_sMsysGitPath);
64 DDX_Text(pDX, IDC_MSYSGIT_EXTERN_PATH, m_sMsysGitExtranPath);
65 DDX_Check(pDX, IDC_CHECKNEWERVERSION, m_bCheckNewer);
69 BEGIN_MESSAGE_MAP(CSetMainPage, ISettingsPropPage)
70 ON_CBN_SELCHANGE(IDC_LANGUAGECOMBO, OnModified)
71 // ON_EN_CHANGE(IDC_TEMPEXTENSIONS, OnModified)
72 ON_BN_CLICKED(IDC_CHECKNEWERVERSION, OnClickVersioncheck)
73 ON_BN_CLICKED(IDC_CHECKNEWERBUTTON, OnBnClickedChecknewerbutton)
74 ON_BN_CLICKED(IDC_MSYSGIT_BROWSE,OnBrowseDir)
75 ON_BN_CLICKED(IDC_MSYSGIT_CHECK,OnCheck)
76 ON_EN_CHANGE(IDC_MSYSGIT_PATH, OnMsysGitPathModify)
77 ON_EN_CHANGE(IDC_MSYSGIT_EXTERN_PATH, OnModified)
78 ON_BN_CLICKED(IDC_BUTTON_SHOW_ENV, &CSetMainPage::OnBnClickedButtonShowEnv)
79 ON_BN_CLICKED(IDC_CREATELIB, &CSetMainPage::OnBnClickedCreatelib)
80 END_MESSAGE_MAP()
82 BOOL CSetMainPage::OnInitDialog()
84 ISettingsPropPage::OnInitDialog();
86 EnableToolTips();
87 AdjustControlSize(IDC_CHECKNEWERVERSION);
89 m_dwLanguage = m_regLanguage;
90 m_bCheckNewer = m_regCheckNewer;
92 m_tooltips.AddTool(IDC_MSYSGIT_PATH,IDS_MSYSGIT_PATH_TT);
93 m_tooltips.AddTool(IDC_CHECKNEWERVERSION, IDS_SETTINGS_CHECKNEWER_TT);
94 m_tooltips.AddTool(IDC_CREATELIB, IDS_SETTINGS_CREATELIB_TT);
96 SHAutoComplete(GetDlgItem(IDC_MSYSGIT_PATH)->m_hWnd, SHACF_FILESYSTEM);
98 // set up the language selecting combobox
99 TCHAR buf[MAX_PATH] = {0};
100 GetLocaleInfo(1033, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
101 m_LanguageCombo.AddString(buf);
102 m_LanguageCombo.SetItemData(0, 1033);
103 CString path = CPathUtils::GetAppParentDirectory();
104 path = path + _T("Languages\\");
105 CSimpleFileFind finder(path, _T("*.dll"));
106 int langcount = 1;
107 while (finder.FindNextFileNoDirectories())
109 CString file = finder.GetFilePath();
110 CString filename = finder.GetFileName();
111 if (filename.Left(12).CompareNoCase(_T("TortoiseProc"))==0)
113 CString sVer = _T(STRPRODUCTVER);
114 sVer = sVer.Left(sVer.ReverseFind('.'));
115 CString sFileVer = CPathUtils::GetVersionFromFile(file);
116 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
117 if (sFileVer.Compare(sVer)!=0)
118 continue;
119 CString sLoc = filename.Mid(12);
120 sLoc = sLoc.Left(sLoc.GetLength()-4); // cut off ".dll"
121 if ((sLoc.Left(2) == L"32")&&(sLoc.GetLength() > 5))
122 continue;
123 DWORD loc = _tstoi(filename.Mid(12));
124 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
125 CString sLang = buf;
126 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
127 if (buf[0])
129 sLang += _T(" (");
130 sLang += buf;
131 sLang += _T(")");
133 m_LanguageCombo.AddString(sLang);
134 m_LanguageCombo.SetItemData(langcount++, loc);
138 for (int i=0; i<m_LanguageCombo.GetCount(); i++)
140 if (m_LanguageCombo.GetItemData(i) == m_dwLanguage)
141 m_LanguageCombo.SetCurSel(i);
144 UpdateData(FALSE);
145 return TRUE;
148 void CSetMainPage::OnClickVersioncheck()
150 if (m_bCheckNewer && CMessageBox::Show(GetSafeHwnd(), IDS_DISABLEUPDATECHECKS, IDS_APPNAME, 2, IDI_QUESTION, IDS_DISABLEUPDATECHECKSBUTTON, IDS_ABORTBUTTON) != 1)
151 return;
152 m_bCheckNewer = !m_bCheckNewer;
153 UpdateData(FALSE);
154 SetModified();
157 void CSetMainPage::OnModified()
159 SetModified();
162 static void PerformCommonGitPathCleanup(CString &path)
164 path.Trim(L"\"'");
166 if (path.Find(L"%") >= 0)
168 int neededSize = ExpandEnvironmentStrings(path, nullptr, 0);
169 CString origPath(path);
170 ExpandEnvironmentStrings(origPath, path.GetBufferSetLength(neededSize), neededSize);
171 path.ReleaseBuffer();
174 path.Replace(L"/", L"\\");
175 path.Replace(L"\\\\", L"\\");
177 if (path.GetLength() > 7 && path.Right(7) == _T("git.exe"))
178 path = path.Left(path.GetLength() - 7);
180 path.TrimRight(L"\\");
182 // prefer git.exe in cmd-directory for Git for Windows based on msys2
183 if (path.GetLength() > 12 && (path.Right(12) == _T("\\mingw32\\bin") || path.Right(12) == _T("\\mingw64\\bin")) && PathFileExists(path.Left(path.GetLength() - 12) + _T("\\cmd\\git.exe")))
184 path = path.Left(path.GetLength() - 12) + _T("\\cmd");
186 // prefer git.exe in bin-directory, see https://github.com/msysgit/msysgit/issues/103
187 if (path.GetLength() > 5 && path.Right(4) == _T("\\cmd") && PathFileExists(path.Left(path.GetLength() - 4) + _T("\\bin\\git.exe")))
188 path = path.Left(path.GetLength() - 4) + _T("\\bin");
191 void CSetMainPage::OnMsysGitPathModify()
193 this->UpdateData();
194 CString str=this->m_sMsysGitPath;
195 if(!str.IsEmpty())
197 PerformCommonGitPathCleanup(str);
199 if(str.GetLength()>=3 && str.Find(_T("bin"), str.GetLength()-3) >=0)
201 str=str.Left(str.GetLength()-3);
202 str += _T("mingw\\bin");
203 if(::PathFileExists(str))
205 str+=_T(";");
206 if(this->m_sMsysGitExtranPath.Find(str)<0)
208 m_sMsysGitExtranPath = str + m_sMsysGitExtranPath;
209 this->UpdateData(FALSE);
214 SetModified();
217 BOOL CSetMainPage::OnApply()
219 UpdateData();
221 PerformCommonGitPathCleanup(m_sMsysGitPath);
222 UpdateData(FALSE);
224 Store(m_dwLanguage, m_regLanguage);
225 if (m_sMsysGitPath.Compare(CString(m_regMsysGitPath)) ||
226 this->m_sMsysGitExtranPath.Compare(CString(m_regMsysGitExtranPath)))
228 Store(m_sMsysGitPath, m_regMsysGitPath);
229 Store(m_sMsysGitExtranPath, m_regMsysGitExtranPath);
230 m_restart = Restart_Cache;
232 Store(m_bCheckNewer, m_regCheckNewer);
234 // only complete if the msysgit directory is ok
235 g_Git.m_bInitialized = FALSE;
236 if (g_Git.CheckMsysGitDir(FALSE))
238 SetModified(FALSE);
239 return ISettingsPropPage::OnApply();
241 else
243 if (CMessageBox::Show(GetSafeHwnd(), L"Invalid git.exe path.\nCheck help file for \"Git.exe Path\".", L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
244 OnHelp();
245 return 0;
249 void CSetMainPage::OnBnClickedChecknewerbutton()
251 CAppUtils::RunTortoiseGitProc(_T("/command:updatecheck /visible"), false, false);
254 void CSetMainPage::OnBrowseDir()
256 CBrowseFolder browseFolder;
257 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
258 CString dir;
259 this->UpdateData(TRUE);
260 dir=this->m_sMsysGitPath;
261 if (browseFolder.Show(GetSafeHwnd(), dir) == CBrowseFolder::OK)
263 m_sMsysGitPath=dir;
264 PerformCommonGitPathCleanup(m_sMsysGitPath);
265 this->UpdateData(FALSE);
266 OnMsysGitPathModify();
268 SetModified(TRUE);
271 void CSetMainPage::OnCheck()
273 GetDlgItem(IDC_MSYSGIT_VER)->SetWindowText(L"");
275 this->UpdateData(TRUE);
277 PerformCommonGitPathCleanup(m_sMsysGitPath);
278 UpdateData(FALSE);
280 OnMsysGitPathModify();
282 CString oldpath = m_regMsysGitPath;
283 CString oldextranpath = this->m_regMsysGitExtranPath;
285 Store(m_sMsysGitPath, m_regMsysGitPath);
286 Store(m_sMsysGitExtranPath, m_regMsysGitExtranPath);
288 g_Git.m_bInitialized = false;
290 if (g_Git.CheckMsysGitDir(FALSE))
292 CString cmd;
293 CString out;
294 cmd=_T("git.exe --version");
295 int ret = g_Git.Run(cmd,&out,CP_UTF8);
296 this->GetDlgItem(IDC_MSYSGIT_VER)->SetWindowText(out);
297 if (out.IsEmpty())
299 if (ret == 0xC0000135 && CMessageBox::Show(GetSafeHwnd(), L"Could not start git.exe. A dynamic library (dll) is missing.\nCheck help file for \"Extern DLL Path\".", L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
300 OnHelp();
301 else if (CMessageBox::Show(GetSafeHwnd(), L"Could not get read version information from git.exe.\nCheck help file for \"Git.exe Path\".", L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
302 OnHelp();
304 else if (!(CGit::ms_bCygwinGit || CGit::ms_bMsys2Git) && out.Find(L"msysgit") == -1 && out.Find(L"windows") == -1 && CMessageBox::Show(GetSafeHwnd(), L"Could not find \"msysgit\" or \"windows\" in versionstring of git.exe.\nIf you are using git of the cygwin or msys2 environment please read the help file for the keyword \"cygwin git\" or \"msys2 git\".", L"TortoiseGit", 1, IDI_INFORMATION, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
305 OnHelp();
307 else
309 if (CMessageBox::Show(GetSafeHwnd(), L"Invalid git.exe path.\nCheck help file for \"Git.exe Path\".", L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
310 OnHelp();
313 Store(oldpath, m_regMsysGitPath);
314 Store(oldextranpath, m_regMsysGitExtranPath);
317 void CSetMainPage::OnBnClickedButtonShowEnv()
319 CString cmd, err;
320 CString tempfile=::GetTempFile();
322 cmd=_T("cmd /c set");
323 if (g_Git.RunLogFile(cmd, tempfile, &err))
325 CMessageBox::Show(GetSafeHwnd(), _T("Could not get environment variables:\n") + err, _T("TortoiseGit"), MB_OK);
326 return;
328 CAppUtils::LaunchAlternativeEditor(tempfile);
331 void CSetMainPage::OnBnClickedCreatelib()
333 CoInitialize(nullptr);
334 EnsureGitLibrary();
335 CoUninitialize();