Added CString wrapper for get_windows_home_directory()
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitConfig.cpp
blob9b1b492eb7edcca2b001a1b1d0fe805899866701
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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 "Git.h"
26 #include "Settings.h"
27 #include "GitAdminDir.h"
28 #include "MessageBox.h"
29 #include "ProjectProperties.h"
30 #include "AppUtils.h"
31 #include "PathUtils.h"
32 // CSettingGitConfig dialog
34 IMPLEMENT_DYNAMIC(CSettingGitConfig, ISettingsPropPage)
36 CSettingGitConfig::CSettingGitConfig()
37 : ISettingsPropPage(CSettingGitConfig::IDD)
38 , m_UserName(_T(""))
39 , m_UserEmail(_T(""))
40 , m_UserSigningKey(_T(""))
41 , m_bGlobal(FALSE)
42 , m_bAutoCrlf(FALSE)
43 , m_bWarnNoSignedOffBy(FALSE)
45 m_ChangeMask=0;
48 CSettingGitConfig::~CSettingGitConfig()
52 void CSettingGitConfig::DoDataExchange(CDataExchange* pDX)
54 CPropertyPage::DoDataExchange(pDX);
55 DDX_Text(pDX, IDC_GIT_USERNAME, m_UserName);
56 DDX_Text(pDX, IDC_GIT_USEREMAIL, m_UserEmail);
57 DDX_Text(pDX, IDC_GIT_USERESINGNINGKEY, m_UserSigningKey);
58 DDX_Check(pDX, IDC_CHECK_GLOBAL, m_bGlobal);
59 DDX_Check(pDX, IDC_CHECK_AUTOCRLF, m_bAutoCrlf);
60 DDX_Control(pDX, IDC_COMBO_SAFECRLF, m_cSafeCrLf);
61 DDX_Check(pDX, IDC_CHECK_WARN_NO_SIGNED_OFF_BY, m_bWarnNoSignedOffBy);
64 BEGIN_MESSAGE_MAP(CSettingGitConfig, CPropertyPage)
65 ON_BN_CLICKED(IDC_CHECK_GLOBAL, &CSettingGitConfig::OnBnClickedCheckGlobal)
66 ON_EN_CHANGE(IDC_GIT_USERNAME, &CSettingGitConfig::OnEnChangeGitUsername)
67 ON_EN_CHANGE(IDC_GIT_USEREMAIL, &CSettingGitConfig::OnEnChangeGitUseremail)
68 ON_EN_CHANGE(IDC_GIT_USERESINGNINGKEY, &CSettingGitConfig::OnEnChangeGitUserSigningKey)
69 ON_BN_CLICKED(IDC_CHECK_AUTOCRLF, &CSettingGitConfig::OnBnClickedCheckAutocrlf)
70 ON_CBN_SELCHANGE(IDC_COMBO_SAFECRLF, &CSettingGitConfig::OnCbnSelchangeSafeCrLf)
71 ON_BN_CLICKED(IDC_EDITGLOBALGITCONFIG, &CSettingGitConfig::OnBnClickedEditglobalgitconfig)
72 ON_BN_CLICKED(IDC_EDITLOCALGITCONFIG, &CSettingGitConfig::OnBnClickedEditlocalgitconfig)
73 ON_BN_CLICKED(IDC_CHECK_WARN_NO_SIGNED_OFF_BY, &CSettingGitConfig::OnBnClickedCheckWarnNoSignedOffBy)
74 ON_BN_CLICKED(IDC_EDITSYSTEMGITCONFIG, &CSettingGitConfig::OnBnClickedEditsystemgitconfig)
75 END_MESSAGE_MAP()
77 BOOL CSettingGitConfig::OnInitDialog()
79 ISettingsPropPage::OnInitDialog();
81 m_cSafeCrLf.AddString(_T("false"));
82 m_cSafeCrLf.AddString(_T("true"));
83 m_cSafeCrLf.AddString(_T("warn"));
85 m_UserName = g_Git.GetUserName();
86 m_UserEmail = g_Git.GetUserEmail();
87 m_UserSigningKey = g_Git.GetConfigValue(_T("user.signingkey"));
89 ProjectProperties::GetBOOLProps(this->m_bAutoCrlf, _T("core.autocrlf"));
90 BOOL bSafeCrLf = FALSE;
91 ProjectProperties::GetBOOLProps(bSafeCrLf, _T("core.safecrlf"));
92 if (bSafeCrLf)
93 m_cSafeCrLf.SetCurSel(1);
94 else
96 CString sSafeCrLf;
97 ProjectProperties::GetStringProps(sSafeCrLf, _T("core.safecrlf"));
98 sSafeCrLf = sSafeCrLf.MakeLower().Trim();
99 if (sSafeCrLf == _T("warn"))
100 m_cSafeCrLf.SetCurSel(2);
101 else
102 m_cSafeCrLf.SetCurSel(0);
104 ProjectProperties::GetBOOLProps(this->m_bWarnNoSignedOffBy, _T("tgit.warnnosignedoffby"));
106 CString str = ((CSettings*)GetParent())->m_CmdPath.GetWinPath();
107 bool isBareRepo = g_GitAdminDir.IsBareRepo(str);
108 CString proj;
109 if (g_GitAdminDir.HasAdminDir(str, &proj) || isBareRepo)
111 CString title;
112 this->GetWindowText(title);
113 this->SetWindowText(title + _T(" - ") + proj);
114 this->GetDlgItem(IDC_CHECK_GLOBAL)->EnableWindow(TRUE);
115 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->EnableWindow(TRUE);
117 else
119 m_bGlobal = TRUE;
120 this->GetDlgItem(IDC_CHECK_GLOBAL)->EnableWindow(FALSE);
121 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->EnableWindow(FALSE);
124 if (isBareRepo)
125 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_GITCONFIG_EDITLOCALGONCFIG)));
127 this->UpdateData(FALSE);
128 return TRUE;
130 // CSettingGitConfig message handlers
132 void CSettingGitConfig::OnBnClickedCheckGlobal()
134 SetModified();
137 void CSettingGitConfig::OnEnChangeGitUsername()
139 m_ChangeMask |= GIT_NAME;
140 SetModified();
143 void CSettingGitConfig::OnEnChangeGitUseremail()
145 m_ChangeMask |= GIT_EMAIL;
146 SetModified();
149 void CSettingGitConfig::OnEnChangeGitUserSigningKey()
151 m_ChangeMask |= GIT_SIGNINGKEY;
152 SetModified();
155 BOOL CSettingGitConfig::OnApply()
157 CString cmd, out;
158 CONFIG_TYPE type=CONFIG_LOCAL;
159 this->UpdateData(FALSE);
161 if(this->m_bGlobal)
162 type = CONFIG_GLOBAL;
164 if(m_ChangeMask&GIT_NAME)
165 if (!Save(_T("user.name"), this->m_UserName, type))
166 return FALSE;
168 if(m_ChangeMask&GIT_EMAIL)
169 if (!Save(_T("user.email"), this->m_UserEmail,type))
170 return FALSE;
172 if(m_ChangeMask&GIT_SIGNINGKEY)
173 if (!Save(_T("user.signingkey"), this->m_UserSigningKey, type))
174 return FALSE;
176 if(m_ChangeMask&GIT_WARNNOSIGNEDOFFBY)
177 if (!Save(_T("tgit.warnnosignedoffby"), this->m_bWarnNoSignedOffBy ? _T("true") : _T("false"), type))
178 return FALSE;
180 if(m_ChangeMask&GIT_CRLF)
181 if (!Save(_T("core.autocrlf"), this->m_bAutoCrlf ? _T("true") : _T("false"), type))
182 return FALSE;
184 if(m_ChangeMask&GIT_SAFECRLF)
186 CString safecrlf;
187 this->m_cSafeCrLf.GetWindowText(safecrlf);
188 if (!Save(_T("core.safecrlf"), safecrlf, type))
189 return FALSE;
192 m_ChangeMask = 0;
193 SetModified(FALSE);
194 return ISettingsPropPage::OnApply();
196 bool CSettingGitConfig::Save(CString key, CString value, CONFIG_TYPE type)
198 CString out;
199 if (g_Git.SetConfigValue(key, value, type, g_Git.GetGitEncode(L"i18n.commitencoding"), &g_Git.m_CurrentDir))
201 CString msg;
202 msg.Format(IDS_PROC_SAVECONFIGFAILED, key, value);
203 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
204 return false;
206 return true;
208 void CSettingGitConfig::OnBnClickedCheckWarnNoSignedOffBy()
210 m_ChangeMask |= GIT_WARNNOSIGNEDOFFBY;
211 SetModified();
213 void CSettingGitConfig::OnBnClickedCheckAutocrlf()
215 m_ChangeMask |= GIT_CRLF;
216 SetModified();
219 void CSettingGitConfig::OnCbnSelchangeSafeCrLf()
221 m_ChangeMask |= GIT_SAFECRLF;
222 SetModified();
225 void CSettingGitConfig::OnBnClickedEditglobalgitconfig()
227 CString filename = g_Git.GetHomeDirectory() + _T("\\.gitconfig");
228 // use alternative editor because of LineEndings
229 CAppUtils::LaunchAlternativeEditor(filename);
232 void CSettingGitConfig::OnBnClickedEditlocalgitconfig()
234 CString path;
235 g_GitAdminDir.GetAdminDirPath(g_Git.m_CurrentDir, path);
236 path += _T("config");
237 // use alternative editor because of LineEndings
238 CAppUtils::LaunchAlternativeEditor(path);
241 void CSettingGitConfig::OnBnClickedEditsystemgitconfig()
243 TCHAR buf[MAX_PATH];
244 const char * systemdir = get_msysgit_etc();
245 if (!systemdir)
247 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_NOMSYSGIT, IDS_APPNAME, MB_ICONERROR);
248 return;
250 _tcscpy_s(buf, MAX_PATH, CA2CT(systemdir));
251 // use alternative editor because of LineEndings
252 CAppUtils::LaunchAlternativeEditor(buf);