No need to explicitly initialize CString and use Empty() for clearing CString
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitConfig.cpp
blob6510c937744522f9d2fadb7e49c4a2a1714112f8
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // SettingGitConfig.cpp : implementation file
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "SettingGitConfig.h"
25 #include "Settings.h"
26 #include "GitAdminDir.h"
27 #include "AppUtils.h"
28 // CSettingGitConfig dialog
30 IMPLEMENT_DYNAMIC(CSettingGitConfig, ISettingsPropPage)
32 CSettingGitConfig::CSettingGitConfig()
33 : ISettingsPropPage(CSettingGitConfig::IDD)
34 , m_bAutoCrlf(FALSE)
35 , m_bNeedSave(false)
36 , m_bQuotePath(TRUE)
37 , m_bInheritUserName(FALSE)
38 , m_bInheritEmail(FALSE)
39 , m_bInheritSigningKey(FALSE)
43 CSettingGitConfig::~CSettingGitConfig()
47 void CSettingGitConfig::DoDataExchange(CDataExchange* pDX)
49 CPropertyPage::DoDataExchange(pDX);
50 DDX_Text(pDX, IDC_GIT_USERNAME, m_UserName);
51 DDX_Text(pDX, IDC_GIT_USEREMAIL, m_UserEmail);
52 DDX_Text(pDX, IDC_GIT_USERESINGNINGKEY, m_UserSigningKey);
53 DDX_Check(pDX, IDC_CHECK_AUTOCRLF, m_bAutoCrlf);
54 DDX_Check(pDX, IDC_CHECK_QUOTEPATH, m_bQuotePath);
55 DDX_Check(pDX, IDC_CHECK_INHERIT_NAME, m_bInheritUserName);
56 DDX_Check(pDX, IDC_CHECK_INHERIT_EMAIL, m_bInheritEmail);
57 DDX_Check(pDX, IDC_CHECK_INHERIT_KEYID, m_bInheritSigningKey);
58 DDX_Control(pDX, IDC_COMBO_SAFECRLF, m_cSafeCrLf);
59 GITSETTINGS_DDX
62 BEGIN_MESSAGE_MAP(CSettingGitConfig, CPropertyPage)
63 ON_EN_CHANGE(IDC_GIT_USERNAME, &CSettingGitConfig::OnChange)
64 ON_EN_CHANGE(IDC_GIT_USEREMAIL, &CSettingGitConfig::OnChange)
65 ON_EN_CHANGE(IDC_GIT_USERESINGNINGKEY, &CSettingGitConfig::OnChange)
66 ON_BN_CLICKED(IDC_CHECK_AUTOCRLF, &CSettingGitConfig::OnChange)
67 ON_BN_CLICKED(IDC_CHECK_QUOTEPATH, &CSettingGitConfig::OnChange)
68 ON_CBN_SELCHANGE(IDC_COMBO_SAFECRLF, &CSettingGitConfig::OnChange)
69 ON_BN_CLICKED(IDC_CHECK_INHERIT_NAME, &CSettingGitConfig::OnChange)
70 ON_BN_CLICKED(IDC_CHECK_INHERIT_EMAIL, &CSettingGitConfig::OnChange)
71 ON_BN_CLICKED(IDC_CHECK_INHERIT_KEYID, &CSettingGitConfig::OnChange)
72 ON_BN_CLICKED(IDC_EDITGLOBALGITCONFIG, &CSettingGitConfig::OnBnClickedEditglobalgitconfig)
73 ON_BN_CLICKED(IDC_EDITGLOBALXDGGITCONFIG, &CSettingGitConfig::OnBnClickedEditglobalxdggitconfig)
74 ON_BN_CLICKED(IDC_EDITLOCALGITCONFIG, &CSettingGitConfig::OnBnClickedEditlocalgitconfig)
75 ON_BN_CLICKED(IDC_EDITTGITCONFIG, &CSettingGitConfig::OnBnClickedEdittgitconfig)
76 ON_BN_CLICKED(IDC_EDITSYSTEMGITCONFIG, &CSettingGitConfig::OnBnClickedEditsystemgitconfig)
77 ON_BN_CLICKED(IDC_VIEWSYSTEMGITCONFIG, &CSettingGitConfig::OnBnClickedViewsystemgitconfig)
78 GITSETTINGS_RADIO_EVENT
79 END_MESSAGE_MAP()
81 BOOL CSettingGitConfig::OnInitDialog()
83 ISettingsPropPage::OnInitDialog();
85 m_cSafeCrLf.AddString(_T(""));
86 m_cSafeCrLf.AddString(_T("false"));
87 m_cSafeCrLf.AddString(_T("true"));
88 m_cSafeCrLf.AddString(_T("warn"));
90 AdjustControlSize(IDC_CHECK_AUTOCRLF);
91 AdjustControlSize(IDC_CHECK_QUOTEPATH);
92 AdjustControlSize(IDC_CHECK_INHERIT_NAME);
93 AdjustControlSize(IDC_CHECK_INHERIT_EMAIL);
94 AdjustControlSize(IDC_CHECK_INHERIT_KEYID);
95 GITSETTINGS_ADJUSTCONTROLSIZE
97 InitGitSettings(this, false, &m_tooltips);
99 if (!m_bGlobal || m_bIsBareRepo)
100 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->EnableWindow(TRUE);
101 else
102 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->EnableWindow(FALSE);
104 if (m_bIsBareRepo)
106 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_GITCONFIG_EDITLOCALGONCFIG)));
107 this->GetDlgItem(IDC_EDITTGITCONFIG)->SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_GITCONFIG_VIEWTGITCONFIG)));
110 if (!CAppUtils::IsAdminLogin())
112 ((CButton *)this->GetDlgItem(IDC_EDITSYSTEMGITCONFIG))->SetShield(TRUE);
113 this->GetDlgItem(IDC_VIEWSYSTEMGITCONFIG)->ShowWindow(SW_SHOW);
116 if (PathIsDirectory(g_Git.GetGitGlobalXDGConfigPath()))
117 this->GetDlgItem(IDC_EDITGLOBALXDGGITCONFIG)->ShowWindow(SW_SHOW);
119 this->UpdateData(FALSE);
121 if (m_UserName.IsEmpty() && m_UserEmail.IsEmpty())
123 // preselect "global" and remove check in "inherit" checkboxes if no username and email are set on first open
124 m_iConfigSource = 3;
125 CheckRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE, IDC_RADIO_SETTINGS_SYSTEM, IDC_RADIO_SETTINGS_EFFECTIVE + m_iConfigSource);
126 m_cSaveTo.SelectString(0, CString(MAKEINTRESOURCE(IDS_CONFIG_GLOBAL)));
127 LoadData();
128 m_bInheritUserName = FALSE;
129 m_bInheritEmail = FALSE;
130 EnDisableControls();
132 return TRUE;
134 // CSettingGitConfig message handlers
136 void CSettingGitConfig::LoadDataImpl(CAutoConfig& config)
138 m_bInheritSigningKey = (config.GetString(_T("user.signingkey"), m_UserSigningKey) == GIT_ENOTFOUND);
140 // special handling for UserName and UserEmail, because these can also be defined as environment variables for effective settings
141 if (m_iConfigSource == 0)
143 m_UserName = g_Git.GetUserName();
144 m_UserEmail = g_Git.GetUserEmail();
145 m_bInheritUserName = FALSE;
146 m_bInheritEmail = FALSE;
147 m_bInheritSigningKey = FALSE;
149 else
151 m_bInheritUserName = (config.GetString(_T("user.name"), m_UserName) == GIT_ENOTFOUND);
152 m_bInheritEmail = (config.GetString(_T("user.email"), m_UserEmail) == GIT_ENOTFOUND);
155 if (git_config_get_bool(&m_bAutoCrlf, config, "core.autocrlf") == GIT_ENOTFOUND)
156 m_bAutoCrlf = BST_INDETERMINATE;
158 if (git_config_get_bool(&m_bQuotePath, config, "core.quotepath") == GIT_ENOTFOUND)
160 if (m_iConfigSource == 0)
161 m_bQuotePath = BST_CHECKED;
162 else
163 m_bQuotePath = BST_INDETERMINATE;
166 BOOL bSafeCrLf = FALSE;
167 if (git_config_get_bool(&bSafeCrLf, config, "core.safecrlf") == GIT_ENOTFOUND)
168 m_cSafeCrLf.SetCurSel(0);
169 else if (bSafeCrLf)
170 m_cSafeCrLf.SetCurSel(2);
171 else
173 CString sSafeCrLf;
174 config.GetString(_T("core.safecrlf"), sSafeCrLf);
175 sSafeCrLf = sSafeCrLf.MakeLower().Trim();
176 if (sSafeCrLf == _T("warn"))
177 m_cSafeCrLf.SetCurSel(3);
178 else
179 m_cSafeCrLf.SetCurSel(1);
182 m_bNeedSave = false;
183 SetModified(FALSE);
184 UpdateData(FALSE);
187 void CSettingGitConfig::EnDisableControls()
189 GetDlgItem(IDC_GIT_USERNAME)->SendMessage(EM_SETREADONLY, m_iConfigSource == 0, 0);
190 GetDlgItem(IDC_GIT_USEREMAIL)->SendMessage(EM_SETREADONLY, m_iConfigSource == 0, 0);
191 GetDlgItem(IDC_GIT_USERESINGNINGKEY)->SendMessage(EM_SETREADONLY, m_iConfigSource == 0, 0);
192 GetDlgItem(IDC_CHECK_AUTOCRLF)->EnableWindow(m_iConfigSource != 0);
193 GetDlgItem(IDC_CHECK_QUOTEPATH)->EnableWindow(m_iConfigSource != 0);
194 GetDlgItem(IDC_COMBO_SAFECRLF)->EnableWindow(m_iConfigSource != 0);
195 GetDlgItem(IDC_COMBO_SETTINGS_SAFETO)->EnableWindow(m_iConfigSource != 0);
196 GetDlgItem(IDC_CHECK_INHERIT_NAME)->EnableWindow(m_iConfigSource != 0);
197 GetDlgItem(IDC_CHECK_INHERIT_EMAIL)->EnableWindow(m_iConfigSource != 0);
198 GetDlgItem(IDC_CHECK_INHERIT_KEYID)->EnableWindow(m_iConfigSource != 0);
200 GetDlgItem(IDC_GIT_USERNAME)->EnableWindow(!m_bInheritUserName);
201 GetDlgItem(IDC_GIT_USEREMAIL)->EnableWindow(!m_bInheritEmail);
202 GetDlgItem(IDC_GIT_USERESINGNINGKEY)->EnableWindow(!m_bInheritSigningKey);
203 UpdateData(FALSE);
206 void CSettingGitConfig::OnChange()
208 UpdateData();
209 EnDisableControls();
210 m_bNeedSave = true;
211 SetModified();
214 BOOL CSettingGitConfig::SafeDataImpl(CAutoConfig& config)
216 if (!Save(config, _T("user.name"), m_UserName, m_bInheritUserName == TRUE))
217 return FALSE;
219 if (!Save(config, _T("user.email"), m_UserEmail, m_bInheritEmail == TRUE))
220 return FALSE;
222 if (!Save(config, _T("user.signingkey"), this->m_UserSigningKey, m_bInheritSigningKey == TRUE))
223 return FALSE;
225 if (!Save(config, _T("core.quotepath"), m_bQuotePath ? _T("true") : _T("false"), m_bQuotePath == BST_INDETERMINATE))
226 return FALSE;
228 if (!Save(config, _T("core.autocrlf"), m_bAutoCrlf ? _T("true") : _T("false"), m_bAutoCrlf == BST_INDETERMINATE))
229 return FALSE;
232 CString safecrlf;
233 this->m_cSafeCrLf.GetWindowText(safecrlf);
234 if (!Save(config, _T("core.safecrlf"), safecrlf, safecrlf.IsEmpty()))
235 return FALSE;
238 return TRUE;
241 BOOL CSettingGitConfig::OnApply()
243 if (!m_bNeedSave)
244 return TRUE;
245 UpdateData();
246 if (!SafeData())
247 return FALSE;
248 m_bNeedSave = false;
249 SetModified(FALSE);
250 return ISettingsPropPage::OnApply();
253 void CSettingGitConfig::OnBnClickedEditglobalgitconfig()
255 // use alternative editor because of LineEndings
256 CAppUtils::LaunchAlternativeEditor(g_Git.GetGitGlobalConfig());
259 void CSettingGitConfig::OnBnClickedEditglobalxdggitconfig()
261 // use alternative editor because of LineEndings
262 CAppUtils::LaunchAlternativeEditor(g_Git.GetGitGlobalXDGConfig());
265 void CSettingGitConfig::OnBnClickedEditlocalgitconfig()
267 // use alternative editor because of LineEndings
268 CAppUtils::LaunchAlternativeEditor(g_Git.GetGitLocalConfig());
271 void CSettingGitConfig::OnBnClickedEdittgitconfig()
273 // use alternative editor because of LineEndings
274 if (GitAdminDir::IsBareRepo(g_Git.m_CurrentDir))
276 CString tmpFile = GetTempFile();
277 CTGitPath path(_T(".tgitconfig"));
278 if (g_Git.GetOneFile(_T("HEAD"), path, tmpFile) == 0)
280 CAppUtils::LaunchAlternativeEditor(tmpFile);
283 else
285 CAppUtils::LaunchAlternativeEditor(g_Git.m_CurrentDir + _T("\\.tgitconfig"));
289 void CSettingGitConfig::OnBnClickedEditsystemgitconfig()
291 CString filename = g_Git.GetGitSystemConfig();
292 if (filename.IsEmpty())
294 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_NOMSYSGIT, IDS_APPNAME, MB_ICONERROR);
295 return;
297 // use alternative editor because of LineEndings
298 CAppUtils::LaunchAlternativeEditor(filename, true);
301 void CSettingGitConfig::OnBnClickedViewsystemgitconfig()
303 CString filename = g_Git.GetGitSystemConfig();
304 if (filename.IsEmpty())
306 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_NOMSYSGIT, IDS_APPNAME, MB_ICONERROR);
307 return;
309 // use alternative editor because of LineEndings
310 CAppUtils::LaunchAlternativeEditor(filename);