Move prune (all remotes) setting to git config page
[TortoiseGit.git] / src / TortoiseProc / Settings / SetProxyPage.cpp
blob68d0aa4a084a4103ba1504a5b3e496e3b3b6f28e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2010-2017, 2019 - TortoiseGit
4 // Copyright (C) 2003-2007 - 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 "SetProxyPage.h"
23 #include "AppUtils.h"
24 #include "StringUtils.h"
25 #include "Git.h"
26 #include "SetProxyPage.h"
28 IMPLEMENT_DYNAMIC(CSetProxyPage, ISettingsPropPage)
29 CSetProxyPage::CSetProxyPage()
30 : ISettingsPropPage(CSetProxyPage::IDD)
31 , m_serverport(0)
32 , m_isEnabled(FALSE)
34 m_regServeraddress = CRegString(L"Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-host", L"");
35 m_regServerport = CRegString(L"Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-port", L"");
36 m_regUsername = CRegString(L"Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-username", L"");
37 m_regPassword = CRegString(L"Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-password", L"");
38 m_regSSHClient = CRegString(L"Software\\TortoiseGit\\SSH");
39 m_SSHClient = m_regSSHClient;
42 CSetProxyPage::~CSetProxyPage()
46 void CSetProxyPage::DoDataExchange(CDataExchange* pDX)
48 ISettingsPropPage::DoDataExchange(pDX);
49 DDX_Text(pDX, IDC_SERVERADDRESS, m_serveraddress);
50 DDX_Text(pDX, IDC_SERVERPORT, m_serverport);
51 DDX_Text(pDX, IDC_USERNAME, m_username);
52 DDX_Text(pDX, IDC_PASSWORD, m_password);
53 DDX_Check(pDX, IDC_ENABLE, m_isEnabled);
54 DDX_Text(pDX, IDC_SSHCLIENT, m_SSHClient);
55 DDX_Control(pDX, IDC_SSHCLIENT, m_cSSHClientEdit);
59 BEGIN_MESSAGE_MAP(CSetProxyPage, ISettingsPropPage)
60 ON_EN_CHANGE(IDC_SERVERADDRESS, OnChange)
61 ON_EN_CHANGE(IDC_SERVERPORT, OnChange)
62 ON_EN_CHANGE(IDC_USERNAME, OnChange)
63 ON_EN_CHANGE(IDC_PASSWORD, OnChange)
64 ON_EN_CHANGE(IDC_SSHCLIENT, OnChange)
65 ON_BN_CLICKED(IDC_ENABLE, OnBnClickedEnable)
66 ON_BN_CLICKED(IDC_SSHBROWSE, OnBnClickedSshbrowse)
67 END_MESSAGE_MAP()
69 HRESULT StringEscape(const CString& str_in, CString* escaped_string) {
70 // http://tools.ietf.org/html/rfc3986#section-3.2.1
71 if (!escaped_string)
72 return E_INVALIDARG;
74 DWORD buf_len = INTERNET_MAX_URL_LENGTH + 1;
75 HRESULT hr = ::UrlEscape(str_in, CStrBuf(*escaped_string, buf_len), &buf_len, URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY);
77 escaped_string->Replace(L"@", L"%40");
78 escaped_string->Replace(L":", L"%3a");
80 return hr;
83 HRESULT StringUnescape(const CString& str_in, CString* unescaped_string) {
84 if (!unescaped_string)
85 return E_INVALIDARG;
87 DWORD buf_len = INTERNET_MAX_URL_LENGTH + 1;
88 ATL::CComBSTR temp(str_in);
89 return ::UrlUnescape(temp, CStrBuf(*unescaped_string, buf_len), &buf_len, 0);
92 BOOL CSetProxyPage::OnInitDialog()
94 ISettingsPropPage::OnInitDialog();
96 AdjustControlSize(IDC_ENABLE);
98 m_tooltips.AddTool(IDC_SERVERADDRESS, IDS_SETTINGS_PROXYSERVER_TT);
100 CString proxy = g_Git.GetConfigValue(L"http.proxy");
102 m_SSHClient = m_regSSHClient;
103 if (m_SSHClient.IsEmpty())
104 m_SSHClient = CRegString(L"Software\\TortoiseGit\\SSH", L"", FALSE, HKEY_LOCAL_MACHINE);
105 if (m_SSHClient.IsEmpty())
107 TCHAR sPlink[MAX_PATH] = {0};
108 GetModuleFileName(nullptr, sPlink, _countof(sPlink));
109 LPTSTR ptr = wcsrchr(sPlink, L'\\');
110 if (ptr)
112 wcscpy_s(ptr + 1, _countof(sPlink) - (ptr - sPlink + 1), L"TortoiseGitPlink.exe");
113 m_SSHClient = CString(sPlink);
116 m_serveraddress = m_regServeraddress;
117 m_serverport = _wtoi(static_cast<LPCTSTR>(static_cast<CString>(m_regServerport)));
118 m_username = m_regUsername;
119 m_password = m_regPassword;
121 if (proxy.IsEmpty())
123 m_isEnabled = FALSE;
124 EnableGroup(FALSE);
126 else
128 int start=0;
129 start = proxy.Find(L"://", start);
130 if(start<0)
131 start =0;
132 else
133 start+=3;
135 int at = proxy.Find(L'@');
136 int port;
138 if(at<0)
140 m_username.Empty();
141 m_password.Empty();
142 port = proxy.Find(L':', start);
143 if(port<0)
144 m_serveraddress = proxy.Mid(start);
145 else
146 m_serveraddress = proxy.Mid(start, port-start);
149 else
151 int username = proxy.Find(L':', start);
152 if(username<=0 || username >at)
154 StringUnescape(proxy.Mid(start, at - start), &m_username);
155 m_password.Empty();
157 else if(username < at)
159 StringUnescape(proxy.Mid(start, username - start), &m_username);
160 StringUnescape(proxy.Mid(username + 1, at - username - 1), &m_password);
163 port = proxy.Find(L':', at);
164 if(port<0)
165 m_serveraddress = proxy.Mid(at+1);
166 else
167 m_serveraddress = proxy.Mid(at+1, port-at-1);
170 if(port<0)
171 m_serverport= 0;
172 else
173 m_serverport = _wtoi(proxy.Mid(port + 1));
175 m_isEnabled = TRUE;
176 EnableGroup(TRUE);
179 SHAutoComplete(::GetDlgItem(m_hWnd, IDC_SSHCLIENT), SHACF_FILESYSTEM | SHACF_FILESYS_ONLY);
181 UpdateData(FALSE);
183 return TRUE;
186 void CSetProxyPage::OnBnClickedEnable()
188 UpdateData();
189 if (m_isEnabled)
190 EnableGroup(TRUE);
191 else
192 EnableGroup(FALSE);
193 SetModified();
196 void CSetProxyPage::EnableGroup(BOOL b)
198 GetDlgItem(IDC_SERVERADDRESS)->EnableWindow(b);
199 GetDlgItem(IDC_SERVERPORT)->EnableWindow(b);
200 GetDlgItem(IDC_USERNAME)->EnableWindow(b);
201 GetDlgItem(IDC_PASSWORD)->EnableWindow(b);
202 GetDlgItem(IDC_PROXYLABEL1)->EnableWindow(b);
203 GetDlgItem(IDC_PROXYLABEL2)->EnableWindow(b);
204 GetDlgItem(IDC_PROXYLABEL3)->EnableWindow(b);
205 GetDlgItem(IDC_PROXYLABEL6)->EnableWindow(b);
208 void CSetProxyPage::OnChange()
210 SetModified();
213 BOOL CSetProxyPage::OnApply()
215 UpdateData();
217 CString temp;
218 Store(m_serveraddress, m_regServeraddress);
219 temp.Format(L"%u", m_serverport);
220 Store(temp, m_regServerport);
221 Store(m_username, m_regUsername);
222 Store(m_password, m_regPassword);
225 CString http_proxy;
226 if(!m_serveraddress.IsEmpty())
228 if (m_serveraddress.Find(L"://") == -1)
229 http_proxy = L"http://";
231 if(!m_username.IsEmpty())
233 CString escapedUsername;
235 if (FAILED(StringEscape(m_username, &escapedUsername)))
237 ::MessageBox(nullptr, L"Could not encode username.", L"TortoiseGit", MB_ICONERROR);
238 return FALSE;
241 http_proxy += escapedUsername;
243 if(!m_password.IsEmpty())
245 CString escapedPassword;
246 if (FAILED(StringEscape(m_password, &escapedPassword)))
248 ::MessageBox(nullptr, L"Could not encode password.", L"TortoiseGit", MB_ICONERROR);
249 return FALSE;
251 http_proxy += L':' + escapedPassword;
254 http_proxy += L'@';
256 http_proxy+=m_serveraddress;
258 if(m_serverport)
260 http_proxy += L':';
261 http_proxy.AppendFormat(L"%u", m_serverport);
265 if (m_isEnabled)
267 if (g_Git.SetConfigValue(L"http.proxy",http_proxy,CONFIG_GLOBAL))
268 return FALSE;
270 else
271 g_Git.UnsetConfigValue(L"http.proxy", CONFIG_GLOBAL);
272 m_regSSHClient = m_SSHClient;
273 SetModified(FALSE);
274 return ISettingsPropPage::OnApply();
277 void CSetProxyPage::OnBnClickedSshbrowse()
279 UpdateData();
280 CString filename = m_SSHClient;
281 if (!PathFileExists(filename))
282 filename.Empty();
283 if (CAppUtils::FileOpenSave(filename, nullptr, IDS_SETTINGS_SELECTSSH, IDS_PROGRAMSFILEFILTER, true, m_hWnd))
285 m_SSHClient = filename;
286 UpdateData(FALSE);
287 SetModified();