Make bugtraq settings dialog hierarchy aware
[TortoiseGit.git] / src / TortoiseProc / Settings / GitSettings.h
blob04dcea3f9b990f78cfae6a88f4569c32345d7bee
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013 - 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.
20 #pragma once
21 #include "../TortoiseProc.h"
22 #include "Git.h"
23 #include "../../Utils/UnicodeUtils.h"
25 class CSettings;
27 class CGitSettings
29 public:
30 CGitSettings()
31 : m_iConfigSource(0)
32 , m_bGlobal(false)
33 , m_bIsBareRepo(false)
34 , m_bHonorProjectConfig(false)
38 protected:
39 CComboBox m_cSaveTo;
40 int m_iConfigSource;
41 bool m_bGlobal;
42 bool m_bIsBareRepo;
43 bool m_bHonorProjectConfig;
45 void InitGitSettings(ISettingsPropPage *page, bool honorProjectConfig)
47 m_bHonorProjectConfig = honorProjectConfig;
49 CString str = g_Git.m_CurrentDir;
50 m_bIsBareRepo = g_GitAdminDir.IsBareRepo(str);
51 CString proj;
52 if (g_GitAdminDir.HasAdminDir(str, &proj) || m_bIsBareRepo)
54 CString title;
55 page->GetWindowText(title);
56 page->SetWindowText(title + _T(" - ") + proj);
57 page->GetDlgItem(IDC_RADIO_SETTINGS_LOCAL)->EnableWindow(TRUE);
59 m_cSaveTo.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_LOCAL)));
61 if (page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT) != nullptr)
63 page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT)->EnableWindow(honorProjectConfig);
64 if (honorProjectConfig && !m_bIsBareRepo)
65 m_cSaveTo.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_PROJECT)));
68 else
70 m_bGlobal = true;
71 page->GetDlgItem(IDC_RADIO_SETTINGS_LOCAL)->EnableWindow(FALSE);
72 if (page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT) != nullptr)
73 page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT)->EnableWindow(FALSE);
75 m_cSaveTo.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_GLOBAL)));
76 m_cSaveTo.SetCurSel(0);
78 page->CheckRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE, IDC_RADIO_SETTINGS_SYSTEM, IDC_RADIO_SETTINGS_EFFECTIVE + m_iConfigSource);
80 LoadData();
83 bool Save(git_config * config, const CString &key, const CString &value)
85 CStringA keyA = CUnicodeUtils::GetUTF8(key);
86 int err = 0;
87 if (value.IsEmpty())
89 const git_config_entry * entry = nullptr;
90 if (git_config_get_entry(&entry, config, keyA) == GIT_ENOTFOUND)
91 return true;
92 err = git_config_delete_entry(config, keyA);
94 else
96 CStringA valueA = CUnicodeUtils::GetMulti(value, CP_UTF8);
97 err = git_config_set_string(config, keyA, valueA);
99 if (err)
101 CString msg;
102 msg.Format(IDS_PROC_SAVECONFIGFAILED, key, value);
103 CMessageBox::Show(nullptr, g_Git.GetLibGit2LastErr(msg), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
104 return false;
106 return true;
109 void LoadData()
111 git_config * config;
112 git_config_new(&config);
113 if (!m_bGlobal && (m_iConfigSource == 0 || m_iConfigSource == 1))
115 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_APP, FALSE)) // this needs to have the highest priority in order to override .tgitconfig settings
116 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
118 if ((m_iConfigSource == 0 && m_bHonorProjectConfig) || m_iConfigSource == 2)
120 if (!m_bIsBareRepo)
122 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.m_CurrentDir) + "\\.tgitconfig", GIT_CONFIG_LEVEL_LOCAL, FALSE)) // this needs to have the second highest priority
123 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
125 else
127 CString tmpFile = GetTempFile();
128 CTGitPath path(_T(".tgitconfig"));
129 if (g_Git.GetOneFile(_T("HEAD"), path, tmpFile) == 0)
131 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(tmpFile), GIT_CONFIG_LEVEL_LOCAL, FALSE)) // this needs to have the second highest priority
132 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
136 if (m_iConfigSource == 0 || m_iConfigSource == 3)
138 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE))
139 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
140 else
142 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE))
143 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
146 if (m_iConfigSource == 0 || m_iConfigSource == 4)
148 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM, FALSE))
149 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
152 LoadDataImpl(config);
154 git_config_free(config);
157 BOOL SafeData()
159 git_config * config;
160 git_config_new(&config);
162 int err = 0;
163 if (m_bGlobal || (m_cSaveTo.GetCurSel() == 1 && (!m_bHonorProjectConfig || m_bIsBareRepo)) || m_cSaveTo.GetCurSel() == 2)
165 if (PathIsDirectory(g_Git.GetGitGlobalXDGConfigPath()))
166 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE);
167 else
168 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
170 else if (m_cSaveTo.GetCurSel() == 1 && !m_bIsBareRepo && m_bHonorProjectConfig)
172 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.m_CurrentDir) + "\\.tgitconfig", GIT_CONFIG_LEVEL_APP, FALSE);
174 else
176 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetMulti(g_Git.GetGitLocalConfig(), CP_UTF8), GIT_CONFIG_LEVEL_LOCAL, FALSE);
178 if (err)
180 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
181 return FALSE;
184 int ret = SafeDataImpl(config);
186 git_config_free(config);
188 return ret;
191 virtual void LoadDataImpl(git_config * config) = 0;
192 virtual BOOL SafeDataImpl(git_config * config) = 0;
194 static void AddTrueFalseToComboBox(CComboBox &combobox)
196 combobox.AddString(_T(""));
197 combobox.AddString(_T("true"));
198 combobox.AddString(_T("false"));
201 static void GetBoolConfigValueComboBox(git_config * config, const CString &key, CComboBox &combobox)
203 CStringA keyA = CUnicodeUtils::GetUTF8(key);
204 BOOL out = 0;
205 if (git_config_get_bool(&out, config, keyA) == GIT_ENOTFOUND)
206 combobox.SetCurSel(0);
207 else if (out)
208 combobox.SetCurSel(1);
209 else
210 combobox.SetCurSel(2);
213 static int GetConfigValue(git_config * config, const CString &key, CString &value)
215 CStringA keyA = CUnicodeUtils::GetUTF8(key);
216 const char *out = nullptr;
217 int retval = git_config_get_string(&out, config, keyA);
218 value = CUnicodeUtils::GetUnicode((CStringA)out);
219 return retval;
223 #define GITSETTINGS_DDX \
224 DDX_Control(pDX, IDC_COMBO_SETTINGS_SAFETO, m_cSaveTo);
226 #define GITSETTINGS_RADIO_EVENT \
227 ON_CBN_SELCHANGE(IDC_COMBO_SETTINGS_SAFETO, &OnChange) \
228 ON_BN_CLICKED(IDC_RADIO_SETTINGS_EFFECTIVE, &OnBnClickedChangedConfigSource) \
229 ON_BN_CLICKED(IDC_RADIO_SETTINGS_LOCAL, &OnBnClickedChangedConfigSource) \
230 ON_BN_CLICKED(IDC_RADIO_SETTINGS_PROJECT, &OnBnClickedChangedConfigSource) \
231 ON_BN_CLICKED(IDC_RADIO_SETTINGS_GLOBAL, &OnBnClickedChangedConfigSource) \
232 ON_BN_CLICKED(IDC_RADIO_SETTINGS_SYSTEM, &OnBnClickedChangedConfigSource)
234 #define GITSETTINGS_RADIO_EVENT_HANDLE \
235 afx_msg void OnBnClickedChangedConfigSource() \
237 m_iConfigSource = GetCheckedRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE, IDC_RADIO_SETTINGS_SYSTEM) - IDC_RADIO_SETTINGS_EFFECTIVE; \
238 LoadData(); \