Provide more information to user when there is a problem with git.exe
[TortoiseGit.git] / src / TortoiseProc / Settings / SetMainPage.cpp
blob663279afb87b6a66e72919a246e69fcf01c2ec3b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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 ".\setmainpage.h"
29 #include "Git.h"
30 #include "MessageBox.h"
31 #include "GitConfig.h"
32 #include "BrowseFolder.h"
33 #include "SysInfo.h"
34 #include "Libraries.h"
36 IMPLEMENT_DYNAMIC(CSetMainPage, ISettingsPropPage)
37 CSetMainPage::CSetMainPage()
38 : ISettingsPropPage(CSetMainPage::IDD)
39 , m_sMsysGitPath(_T(""))
40 , m_bCheckNewer(TRUE)
41 , m_dwLanguage(0)
43 m_regLanguage = CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
44 CString temp=CRegString(REG_MSYSGIT_INSTALL,_T(""),FALSE,HKEY_LOCAL_MACHINE);;
45 if(!temp.IsEmpty())
46 temp+=_T("bin");
47 m_regMsysGitPath = CRegString(REG_MSYSGIT_PATH,temp,FALSE);
49 m_regMsysGitExtranPath =CRegString(REG_MSYSGIT_EXTRA_PATH);
51 m_sMsysGitPath = m_regMsysGitPath;
52 m_sMsysGitExtranPath = m_regMsysGitExtranPath;
54 m_regCheckNewer = CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck"), TRUE);
57 CSetMainPage::~CSetMainPage()
61 void CSetMainPage::DoDataExchange(CDataExchange* pDX)
63 ISettingsPropPage::DoDataExchange(pDX);
64 DDX_Control(pDX, IDC_LANGUAGECOMBO, m_LanguageCombo);
65 m_dwLanguage = (DWORD)m_LanguageCombo.GetItemData(m_LanguageCombo.GetCurSel());
66 DDX_Text(pDX, IDC_MSYSGIT_PATH, m_sMsysGitPath);
67 DDX_Text(pDX, IDC_MSYSGIT_EXTERN_PATH, m_sMsysGitExtranPath);
68 DDX_Check(pDX, IDC_CHECKNEWERVERSION, m_bCheckNewer);
72 BEGIN_MESSAGE_MAP(CSetMainPage, ISettingsPropPage)
73 ON_CBN_SELCHANGE(IDC_LANGUAGECOMBO, OnModified)
74 // ON_EN_CHANGE(IDC_TEMPEXTENSIONS, OnModified)
75 ON_BN_CLICKED(IDC_EDITCONFIG, OnBnClickedEditconfig)
76 ON_BN_CLICKED(IDC_CHECKNEWERVERSION, OnModified)
77 ON_BN_CLICKED(IDC_CHECKNEWERBUTTON, OnBnClickedChecknewerbutton)
78 ON_BN_CLICKED(IDC_SOUNDS, OnBnClickedSounds)
79 ON_BN_CLICKED(IDC_MSYSGIT_BROWSE,OnBrowseDir)
80 ON_BN_CLICKED(IDC_MSYSGIT_CHECK,OnCheck)
81 ON_EN_CHANGE(IDC_MSYSGIT_PATH, OnMsysGitPathModify)
82 ON_EN_CHANGE(IDC_MSYSGIT_EXTERN_PATH, OnModified)
83 ON_BN_CLICKED(IDC_BUTTON_SHOW_ENV, &CSetMainPage::OnBnClickedButtonShowEnv)
84 ON_BN_CLICKED(IDC_CREATELIB, &CSetMainPage::OnBnClickedCreatelib)
85 END_MESSAGE_MAP()
87 BOOL CSetMainPage::OnInitDialog()
89 ISettingsPropPage::OnInitDialog();
91 // disable features that have not yet been implemented
92 GetDlgItem(IDC_SOUNDS)->EnableWindow( FALSE );
93 GetDlgItem(IDC_SOUNDS_TEXT)->EnableWindow( FALSE );
95 EnableToolTips();
98 m_dwLanguage = m_regLanguage;
99 m_bCheckNewer = m_regCheckNewer;
101 m_tooltips.Create(this);
102 m_tooltips.AddTool(IDC_MSYSGIT_PATH,IDS_MSYSGIT_PATH_TT);
103 m_tooltips.AddTool(IDC_CHECKNEWERVERSION, IDS_SETTINGS_CHECKNEWER_TT);
104 m_tooltips.AddTool(IDC_CREATELIB, IDS_SETTINGS_CREATELIB_TT);
106 GetDlgItem(IDC_CREATELIB)->EnableWindow(SysInfo::Instance().IsWin7OrLater());
108 SHAutoComplete(GetDlgItem(IDC_MSYSGIT_PATH)->m_hWnd, SHACF_FILESYSTEM);
110 // set up the language selecting combobox
111 TCHAR buf[MAX_PATH];
112 GetLocaleInfo(1033, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
113 m_LanguageCombo.AddString(buf);
114 m_LanguageCombo.SetItemData(0, 1033);
115 CString path = CPathUtils::GetAppParentDirectory();
116 path = path + _T("Languages\\");
117 CSimpleFileFind finder(path, _T("*.dll"));
118 int langcount = 1;
119 while (finder.FindNextFileNoDirectories())
121 CString file = finder.GetFilePath();
122 CString filename = finder.GetFileName();
123 if (filename.Left(12).CompareNoCase(_T("TortoiseProc"))==0)
125 CString sVer = _T(STRPRODUCTVER);
126 sVer = sVer.Left(sVer.ReverseFind('.'));
127 CString sFileVer = CPathUtils::GetVersionFromFile(file);
128 sFileVer = sFileVer.Left(sFileVer.ReverseFind('.'));
129 if (sFileVer.Compare(sVer)!=0)
130 continue;
131 CString sLoc = filename.Mid(12);
132 sLoc = sLoc.Left(sLoc.GetLength()-4); // cut off ".dll"
133 if ((sLoc.Left(2) == L"32")&&(sLoc.GetLength() > 5))
134 continue;
135 DWORD loc = _tstoi(filename.Mid(12));
136 GetLocaleInfo(loc, LOCALE_SNATIVELANGNAME, buf, _countof(buf));
137 CString sLang = buf;
138 GetLocaleInfo(loc, LOCALE_SNATIVECTRYNAME, buf, _countof(buf));
139 if (buf[0])
141 sLang += _T(" (");
142 sLang += buf;
143 sLang += _T(")");
145 m_LanguageCombo.AddString(sLang);
146 m_LanguageCombo.SetItemData(langcount++, loc);
150 for (int i=0; i<m_LanguageCombo.GetCount(); i++)
152 if (m_LanguageCombo.GetItemData(i) == m_dwLanguage)
153 m_LanguageCombo.SetCurSel(i);
156 UpdateData(FALSE);
157 return TRUE;
160 BOOL CSetMainPage::PreTranslateMessage(MSG* pMsg)
162 m_tooltips.RelayEvent(pMsg);
163 return ISettingsPropPage::PreTranslateMessage(pMsg);
166 void CSetMainPage::OnModified()
168 SetModified();
171 void CSetMainPage::OnMsysGitPathModify()
173 this->UpdateData();
174 CString str=this->m_sMsysGitPath;
175 if(!str.IsEmpty())
177 if(str[str.GetLength()-1]==_T('\'') || str[str.GetLength()-1]==_T('/'))
178 str=str.Left(str.GetLength()-1);
180 if(str.GetLength()>=3 && str.Find(_T("bin"), str.GetLength()-3) >=0)
182 str=str.Left(str.GetLength()-3);
183 str += _T("mingw\\bin");
184 if(::PathFileExists(str))
186 str+=_T(";");
187 if(this->m_sMsysGitExtranPath.Find(str)<0)
189 m_sMsysGitExtranPath = str + m_sMsysGitExtranPath;
190 this->UpdateData(FALSE);
195 SetModified();
198 BOOL CSetMainPage::OnApply()
200 UpdateData();
201 if (!m_sMsysGitPath.IsEmpty() && (m_sMsysGitPath.Right(1) == _T("\\") || m_sMsysGitPath.Right(1) == _T("/")))
203 m_sMsysGitPath = m_sMsysGitPath.Left(m_sMsysGitPath.GetLength() - 1);
204 UpdateData(FALSE);
206 Store (m_dwLanguage, m_regLanguage);
207 if (m_sMsysGitPath.Compare(CString(m_regMsysGitPath)) ||
208 this->m_sMsysGitExtranPath.Compare(CString(m_regMsysGitExtranPath)))
210 Store (m_sMsysGitPath, m_regMsysGitPath);
211 Store (m_sMsysGitExtranPath, m_regMsysGitExtranPath);
212 m_restart = Restart_Cache;
214 Store (m_bCheckNewer, m_regCheckNewer);
216 // only complete if the msysgit directory is ok
217 if(g_Git.CheckMsysGitDir())
219 SetModified(FALSE);
220 return ISettingsPropPage::OnApply();
222 else
224 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)
225 OnHelp();
226 return 0;
230 void CSetMainPage::OnBnClickedEditconfig()
232 #if 0
233 TCHAR buf[MAX_PATH];
234 SVN::EnsureConfigFile();
235 SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, buf);
236 CString path = buf;
237 path += _T("\\Subversion\\config");
238 CAppUtils::StartTextViewer(path);
239 #endif
242 void CSetMainPage::OnBnClickedChecknewerbutton()
244 CAppUtils::RunTortoiseGitProc(_T("/command:updatecheck /visible"), false, false);
247 void CSetMainPage::OnBnClickedSounds()
249 if (SysInfo::Instance().IsVistaOrLater())
250 CAppUtils::LaunchApplication(_T("RUNDLL32 Shell32,Control_RunDLL mmsys.cpl,,2"), NULL, false);
251 else
252 CAppUtils::LaunchApplication(_T("RUNDLL32 Shell32,Control_RunDLL mmsys.cpl,,1"), NULL, false);
255 void CSetMainPage::OnBrowseDir()
257 CBrowseFolder browseFolder;
258 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
259 CString dir;
260 this->UpdateData(TRUE);
261 dir=this->m_sMsysGitPath;
262 if (browseFolder.Show(GetSafeHwnd(), dir) == CBrowseFolder::OK)
264 m_sMsysGitPath=dir;
265 this->UpdateData(FALSE);
266 OnMsysGitPathModify();
268 SetModified(TRUE);
271 void CSetMainPage::OnCheck()
273 this->UpdateData(TRUE);
275 CString oldpath = m_regMsysGitPath;
276 CString oldextranpath = this->m_regMsysGitExtranPath;
278 Store(m_sMsysGitPath, m_regMsysGitPath);
279 Store(m_sMsysGitExtranPath, m_regMsysGitExtranPath);
281 g_Git.m_bInitialized = false;
283 if(g_Git.CheckMsysGitDir())
285 CString cmd;
286 CString out;
287 cmd=_T("git.exe --version");
288 g_Git.Run(cmd,&out,CP_UTF8);
289 this->GetDlgItem(IDC_MSYSGIT_VER)->SetWindowText(out);
290 if (out.IsEmpty())
291 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)
292 OnHelp();
294 else
296 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)
297 OnHelp();
300 Store (oldpath, m_regMsysGitPath);
301 Store (oldextranpath, m_regMsysGitExtranPath);
304 void CSetMainPage::OnBnClickedButtonShowEnv()
306 CString cmd;
307 CString tempfile=::GetTempFile();
309 cmd=_T("cmd /c set");
310 g_Git.RunLogFile(cmd,tempfile);
311 CAppUtils::LaunchAlternativeEditor(tempfile);
314 void CSetMainPage::OnBnClickedCreatelib()
316 CoInitialize(NULL);
317 EnsureGitLibrary();
318 CoUninitialize();