No need to explicitly initialize CString and use Empty() for clearing CString
[TortoiseGit.git] / src / TortoiseProc / Settings / SetMainPage.cpp
blobaac5cc4d20841eab4c45c2310e50710a0992a6c7
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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 "SysInfo.h"
33 #include "Libraries.h"
35 IMPLEMENT_DYNAMIC(CSetMainPage, ISettingsPropPage)
36 CSetMainPage::CSetMainPage()
37 : ISettingsPropPage(CSetMainPage::IDD)
38 , m_bCheckNewer(TRUE)
39 , m_dwLanguage(0)
41 m_regLanguage = CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
42 CString temp = CRegString(REG_MSYSGIT_INSTALL, _T(""), FALSE, HKEY_LOCAL_MACHINE);
43 if(!temp.IsEmpty())
44 temp+=_T("bin");
45 m_regMsysGitPath = CRegString(REG_MSYSGIT_PATH,temp,FALSE);
47 m_regMsysGitExtranPath =CRegString(REG_MSYSGIT_EXTRA_PATH);
49 m_sMsysGitPath = m_regMsysGitPath;
50 m_sMsysGitExtranPath = m_regMsysGitExtranPath;
52 m_regCheckNewer = CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck"), TRUE);
55 CSetMainPage::~CSetMainPage()
59 void CSetMainPage::DoDataExchange(CDataExchange* pDX)
61 ISettingsPropPage::DoDataExchange(pDX);
62 DDX_Control(pDX, IDC_LANGUAGECOMBO, m_LanguageCombo);
63 m_dwLanguage = (DWORD)m_LanguageCombo.GetItemData(m_LanguageCombo.GetCurSel());
64 DDX_Text(pDX, IDC_MSYSGIT_PATH, m_sMsysGitPath);
65 DDX_Text(pDX, IDC_MSYSGIT_EXTERN_PATH, m_sMsysGitExtranPath);
66 DDX_Check(pDX, IDC_CHECKNEWERVERSION, m_bCheckNewer);
70 BEGIN_MESSAGE_MAP(CSetMainPage, ISettingsPropPage)
71 ON_CBN_SELCHANGE(IDC_LANGUAGECOMBO, OnModified)
72 // ON_EN_CHANGE(IDC_TEMPEXTENSIONS, OnModified)
73 ON_BN_CLICKED(IDC_CHECKNEWERVERSION, OnModified)
74 ON_BN_CLICKED(IDC_CHECKNEWERBUTTON, OnBnClickedChecknewerbutton)
75 ON_BN_CLICKED(IDC_MSYSGIT_BROWSE,OnBrowseDir)
76 ON_BN_CLICKED(IDC_MSYSGIT_CHECK,OnCheck)
77 ON_EN_CHANGE(IDC_MSYSGIT_PATH, OnMsysGitPathModify)
78 ON_EN_CHANGE(IDC_MSYSGIT_EXTERN_PATH, OnModified)
79 ON_BN_CLICKED(IDC_BUTTON_SHOW_ENV, &CSetMainPage::OnBnClickedButtonShowEnv)
80 ON_BN_CLICKED(IDC_CREATELIB, &CSetMainPage::OnBnClickedCreatelib)
81 END_MESSAGE_MAP()
83 BOOL CSetMainPage::OnInitDialog()
85 ISettingsPropPage::OnInitDialog();
87 EnableToolTips();
88 AdjustControlSize(IDC_CHECKNEWERVERSION);
90 m_dwLanguage = m_regLanguage;
91 m_bCheckNewer = m_regCheckNewer;
93 m_tooltips.AddTool(IDC_MSYSGIT_PATH,IDS_MSYSGIT_PATH_TT);
94 m_tooltips.AddTool(IDC_CHECKNEWERVERSION, IDS_SETTINGS_CHECKNEWER_TT);
95 m_tooltips.AddTool(IDC_CREATELIB, IDS_SETTINGS_CREATELIB_TT);
97 GetDlgItem(IDC_CREATELIB)->EnableWindow(SysInfo::Instance().IsWin7OrLater());
99 SHAutoComplete(GetDlgItem(IDC_MSYSGIT_PATH)->m_hWnd, SHACF_FILESYSTEM);
101 // set up the language selecting combobox
102 TCHAR buf[MAX_PATH] = {0};
103 GetLocaleInfo(1033, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
104 m_LanguageCombo.AddString(buf);
105 m_LanguageCombo.SetItemData(0, 1033);
106 CString path = CPathUtils::GetAppParentDirectory();
107 path = path + _T("Languages\\");
108 CSimpleFileFind finder(path, _T("*.dll"));
109 int langcount = 1;
110 while (finder.FindNextFileNoDirectories())
112 CString file = finder.GetFilePath();
113 CString filename = finder.GetFileName();
114 if (filename.Left(12).CompareNoCase(_T("TortoiseProc"))==0)
116 CString sVer = _T(STRPRODUCTVER);
117 sVer = sVer.Left(sVer.ReverseFind('.'));
118 CString sFileVer = CPathUtils::GetVersionFromFile(file);
119 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
120 if (sFileVer.Compare(sVer)!=0)
121 continue;
122 CString sLoc = filename.Mid(12);
123 sLoc = sLoc.Left(sLoc.GetLength()-4); // cut off ".dll"
124 if ((sLoc.Left(2) == L"32")&&(sLoc.GetLength() > 5))
125 continue;
126 DWORD loc = _tstoi(filename.Mid(12));
127 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
128 CString sLang = buf;
129 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
130 if (buf[0])
132 sLang += _T(" (");
133 sLang += buf;
134 sLang += _T(")");
136 m_LanguageCombo.AddString(sLang);
137 m_LanguageCombo.SetItemData(langcount++, loc);
141 for (int i=0; i<m_LanguageCombo.GetCount(); i++)
143 if (m_LanguageCombo.GetItemData(i) == m_dwLanguage)
144 m_LanguageCombo.SetCurSel(i);
147 UpdateData(FALSE);
148 return TRUE;
151 void CSetMainPage::OnModified()
153 SetModified();
156 static void PerformCommonGitPathCleanup(CString &path)
158 path.Trim(L"\"'");
160 if (path.Find(L"%") >= 0)
162 int neededSize = ExpandEnvironmentStrings(path, nullptr, 0);
163 CString origPath(path);
164 ExpandEnvironmentStrings(origPath, path.GetBufferSetLength(neededSize), neededSize);
165 path.ReleaseBuffer();
168 path.Replace(L"/", L"\\");
169 path.Replace(L"\\\\", L"\\");
171 if (path.GetLength() > 7 && path.Right(7) == _T("git.exe"))
172 path = path.Left(path.GetLength() - 7);
174 path.TrimRight(L"\\");
176 // prefer git.exe in cmd-directory for Git for Windows based on msys2
177 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")))
178 path = path.Left(path.GetLength() - 12) + _T("\\cmd");
180 // prefer git.exe in bin-directory, see https://github.com/msysgit/msysgit/issues/103
181 if (path.GetLength() > 5 && path.Right(4) == _T("\\cmd") && PathFileExists(path.Left(path.GetLength() - 4) + _T("\\bin\\git.exe")))
182 path = path.Left(path.GetLength() - 4) + _T("\\bin");
185 void CSetMainPage::OnMsysGitPathModify()
187 this->UpdateData();
188 CString str=this->m_sMsysGitPath;
189 if(!str.IsEmpty())
191 PerformCommonGitPathCleanup(str);
193 if(str.GetLength()>=3 && str.Find(_T("bin"), str.GetLength()-3) >=0)
195 str=str.Left(str.GetLength()-3);
196 str += _T("mingw\\bin");
197 if(::PathFileExists(str))
199 str+=_T(";");
200 if(this->m_sMsysGitExtranPath.Find(str)<0)
202 m_sMsysGitExtranPath = str + m_sMsysGitExtranPath;
203 this->UpdateData(FALSE);
208 SetModified();
211 BOOL CSetMainPage::OnApply()
213 UpdateData();
215 PerformCommonGitPathCleanup(m_sMsysGitPath);
216 UpdateData(FALSE);
218 Store(m_dwLanguage, m_regLanguage);
219 if (m_sMsysGitPath.Compare(CString(m_regMsysGitPath)) ||
220 this->m_sMsysGitExtranPath.Compare(CString(m_regMsysGitExtranPath)))
222 Store(m_sMsysGitPath, m_regMsysGitPath);
223 Store(m_sMsysGitExtranPath, m_regMsysGitExtranPath);
224 m_restart = Restart_Cache;
226 Store(m_bCheckNewer, m_regCheckNewer);
228 // only complete if the msysgit directory is ok
229 g_Git.m_bInitialized = FALSE;
230 if (g_Git.CheckMsysGitDir(FALSE))
232 SetModified(FALSE);
233 return ISettingsPropPage::OnApply();
235 else
237 if (CMessageBox::Show(NULL, _T("Invalid git.exe path.\nCheck help file for \"Git.exe Path\"."), _T("TortoiseGit"), 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
238 OnHelp();
239 return 0;
243 void CSetMainPage::OnBnClickedChecknewerbutton()
245 CAppUtils::RunTortoiseGitProc(_T("/command:updatecheck /visible"), false, false);
248 void CSetMainPage::OnBrowseDir()
250 CBrowseFolder browseFolder;
251 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
252 CString dir;
253 this->UpdateData(TRUE);
254 dir=this->m_sMsysGitPath;
255 if (browseFolder.Show(GetSafeHwnd(), dir) == CBrowseFolder::OK)
257 m_sMsysGitPath=dir;
258 PerformCommonGitPathCleanup(m_sMsysGitPath);
259 this->UpdateData(FALSE);
260 OnMsysGitPathModify();
262 SetModified(TRUE);
265 void CSetMainPage::OnCheck()
267 GetDlgItem(IDC_MSYSGIT_VER)->SetWindowText(L"");
269 this->UpdateData(TRUE);
271 PerformCommonGitPathCleanup(m_sMsysGitPath);
272 UpdateData(FALSE);
274 OnMsysGitPathModify();
276 CString oldpath = m_regMsysGitPath;
277 CString oldextranpath = this->m_regMsysGitExtranPath;
279 Store(m_sMsysGitPath, m_regMsysGitPath);
280 Store(m_sMsysGitExtranPath, m_regMsysGitExtranPath);
282 g_Git.m_bInitialized = false;
284 if (g_Git.CheckMsysGitDir(FALSE))
286 CString cmd;
287 CString out;
288 cmd=_T("git.exe --version");
289 int ret = g_Git.Run(cmd,&out,CP_UTF8);
290 this->GetDlgItem(IDC_MSYSGIT_VER)->SetWindowText(out);
291 if (out.IsEmpty())
293 if (ret == 0xC0000135 && CMessageBox::Show(NULL, _T("Could not start git.exe. A dynamic library (dll) is missing.\nCheck help file for \"Extern DLL Path\"."), _T("TortoiseGit"), 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
294 OnHelp();
295 else if (CMessageBox::Show(NULL, _T("Could not get read version information from git.exe.\nCheck help file for \"Git.exe Path\"."),_T("TortoiseGit"), 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
296 OnHelp();
298 else if (!(CGit::ms_bCygwinGit || CGit::ms_bMsys2Git) && out.Find(_T("msysgit")) == -1 && out.Find(_T("windows")) == -1 && CMessageBox::Show(NULL, _T("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\"."), _T("TortoiseGit"), 1, IDI_INFORMATION, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
299 OnHelp();
301 else
303 if (CMessageBox::Show(NULL, _T("Invalid git.exe path.\nCheck help file for \"Git.exe Path\"."), _T("TortoiseGit"), 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP))) == 2)
304 OnHelp();
307 Store(oldpath, m_regMsysGitPath);
308 Store(oldextranpath, m_regMsysGitExtranPath);
311 void CSetMainPage::OnBnClickedButtonShowEnv()
313 CString cmd, err;
314 CString tempfile=::GetTempFile();
316 cmd=_T("cmd /c set");
317 if (g_Git.RunLogFile(cmd, tempfile, &err))
319 CMessageBox::Show(GetSafeHwnd(), _T("Could not get environment variables:\n") + err, _T("TortoiseGit"), MB_OK);
320 return;
322 CAppUtils::LaunchAlternativeEditor(tempfile);
325 void CSetMainPage::OnBnClickedCreatelib()
327 CoInitialize(NULL);
328 EnsureGitLibrary();
329 CoUninitialize();