No need to explicitly initialize CString and use Empty() for clearing CString
[TortoiseGit.git] / src / TortoiseProc / Settings / SetProxyPage.cpp
blobdeaf4a33079b6a383d6a020b49056127140d33d4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2010-2015 - 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"
27 #include "MessageBox.h"
29 IMPLEMENT_DYNAMIC(CSetProxyPage, ISettingsPropPage)
30 CSetProxyPage::CSetProxyPage()
31 : ISettingsPropPage(CSetProxyPage::IDD)
32 , m_serverport(0)
33 , m_isEnabled(FALSE)
35 m_regServeraddress = CRegString(_T("Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-host"), _T(""));
36 m_regServerport = CRegString(_T("Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-port"), _T(""));
37 m_regUsername = CRegString(_T("Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-username"), _T(""));
38 m_regPassword = CRegString(_T("Software\\TortoiseGit\\Git\\Servers\\global\\http-proxy-password"), _T(""));
39 m_regSSHClient = CRegString(_T("Software\\TortoiseGit\\SSH"));
40 m_SSHClient = m_regSSHClient;
44 CSetProxyPage::~CSetProxyPage()
48 void CSetProxyPage::DoDataExchange(CDataExchange* pDX)
50 ISettingsPropPage::DoDataExchange(pDX);
51 DDX_Text(pDX, IDC_SERVERADDRESS, m_serveraddress);
52 DDX_Text(pDX, IDC_SERVERPORT, m_serverport);
53 DDX_Text(pDX, IDC_USERNAME, m_username);
54 DDX_Text(pDX, IDC_PASSWORD, m_password);
55 DDX_Check(pDX, IDC_ENABLE, m_isEnabled);
56 DDX_Text(pDX, IDC_SSHCLIENT, m_SSHClient);
57 DDX_Control(pDX, IDC_SSHCLIENT, m_cSSHClientEdit);
61 BEGIN_MESSAGE_MAP(CSetProxyPage, ISettingsPropPage)
62 ON_EN_CHANGE(IDC_SERVERADDRESS, OnChange)
63 ON_EN_CHANGE(IDC_SERVERPORT, OnChange)
64 ON_EN_CHANGE(IDC_USERNAME, OnChange)
65 ON_EN_CHANGE(IDC_PASSWORD, OnChange)
66 ON_EN_CHANGE(IDC_SSHCLIENT, OnChange)
67 ON_BN_CLICKED(IDC_ENABLE, OnBnClickedEnable)
68 ON_BN_CLICKED(IDC_SSHBROWSE, OnBnClickedSshbrowse)
69 END_MESSAGE_MAP()
71 HRESULT StringEscape(const CString& str_in, CString* escaped_string) {
72 // http://tools.ietf.org/html/rfc3986#section-3.2.1
73 if (!escaped_string)
74 return E_INVALIDARG;
76 DWORD buf_len = INTERNET_MAX_URL_LENGTH + 1;
77 HRESULT hr = ::UrlEscape(str_in, escaped_string->GetBufferSetLength(buf_len), &buf_len, URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY);
78 if (SUCCEEDED(hr)) {
79 escaped_string->ReleaseBuffer();
82 escaped_string->Replace(_T("@"), _T("%40"));
83 escaped_string->Replace(_T(":"), _T("%3a"));
85 return hr;
88 HRESULT StringUnescape(const CString& str_in, CString* unescaped_string) {
89 if (!unescaped_string)
90 return E_INVALIDARG;
92 DWORD buf_len = INTERNET_MAX_URL_LENGTH + 1;
93 ATL::CComBSTR temp(str_in);
94 HRESULT hr = ::UrlUnescape(temp, unescaped_string->GetBufferSetLength(buf_len), &buf_len, 0);
95 if (SUCCEEDED(hr)) {
96 unescaped_string->ReleaseBuffer();
99 return hr;
102 BOOL CSetProxyPage::OnInitDialog()
104 ISettingsPropPage::OnInitDialog();
106 AdjustControlSize(IDC_ENABLE);
108 m_tooltips.AddTool(IDC_SERVERADDRESS, IDS_SETTINGS_PROXYSERVER_TT);
110 CString proxy = g_Git.GetConfigValue(_T("http.proxy"));
112 m_SSHClient = m_regSSHClient;
113 if (m_SSHClient.IsEmpty())
114 m_SSHClient = CRegString(_T("Software\\TortoiseGit\\SSH"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
115 if (m_SSHClient.IsEmpty())
117 TCHAR sPlink[MAX_PATH] = {0};
118 GetModuleFileName(NULL, sPlink, _countof(sPlink));
119 LPTSTR ptr = _tcsrchr(sPlink, _T('\\'));
120 if (ptr)
122 _tcscpy_s(ptr + 1, MAX_PATH - (ptr - sPlink + 1), _T("TortoiseGitPlink.exe"));
123 m_SSHClient = CString(sPlink);
126 m_serveraddress = m_regServeraddress;
127 m_serverport = _ttoi((LPCTSTR)(CString)m_regServerport);
128 m_username = m_regUsername;
129 m_password = m_regPassword;
131 if (proxy.IsEmpty())
133 m_isEnabled = FALSE;
134 EnableGroup(FALSE);
136 else
138 int start=0;
139 start = proxy.Find(_T("://"),start);
140 if(start<0)
141 start =0;
142 else
143 start+=3;
145 int at = proxy.Find(_T("@"), 0);
146 int port;
148 if(at<0)
150 m_username=_T("");
151 m_password=_T("");
152 port=proxy.Find(_T(":"),start);
153 if(port<0)
154 m_serveraddress = proxy.Mid(start);
155 else
156 m_serveraddress = proxy.Mid(start, port-start);
159 else
161 int username;
162 username = proxy.Find(_T(":"),start);
163 if(username<=0 || username >at)
165 StringUnescape(proxy.Mid(start, at - start), &m_username);
166 m_password=_T("");
168 else if(username < at)
170 StringUnescape(proxy.Mid(start, username - start), &m_username);
171 StringUnescape(proxy.Mid(username + 1, at - username - 1), &m_password);
174 port=proxy.Find(_T(":"),at);
175 if(port<0)
176 m_serveraddress = proxy.Mid(at+1);
177 else
178 m_serveraddress = proxy.Mid(at+1, port-at-1);
181 if(port<0)
183 m_serverport= 0;
185 else
186 m_serverport = _ttoi(proxy.Mid(port+1));
188 m_isEnabled = TRUE;
189 EnableGroup(TRUE);
192 SHAutoComplete(::GetDlgItem(m_hWnd, IDC_SSHCLIENT), SHACF_FILESYSTEM | SHACF_FILESYS_ONLY);
194 UpdateData(FALSE);
196 return TRUE;
199 void CSetProxyPage::OnBnClickedEnable()
201 UpdateData();
202 if (m_isEnabled)
204 EnableGroup(TRUE);
206 else
208 EnableGroup(FALSE);
210 SetModified();
213 void CSetProxyPage::EnableGroup(BOOL b)
215 GetDlgItem(IDC_SERVERADDRESS)->EnableWindow(b);
216 GetDlgItem(IDC_SERVERPORT)->EnableWindow(b);
217 GetDlgItem(IDC_USERNAME)->EnableWindow(b);
218 GetDlgItem(IDC_PASSWORD)->EnableWindow(b);
219 GetDlgItem(IDC_PROXYLABEL1)->EnableWindow(b);
220 GetDlgItem(IDC_PROXYLABEL2)->EnableWindow(b);
221 GetDlgItem(IDC_PROXYLABEL3)->EnableWindow(b);
222 GetDlgItem(IDC_PROXYLABEL6)->EnableWindow(b);
225 void CSetProxyPage::OnChange()
227 SetModified();
230 BOOL CSetProxyPage::OnApply()
232 UpdateData();
234 CString temp;
235 Store(m_serveraddress, m_regServeraddress);
236 temp.Format(_T("%u"), m_serverport);
237 Store(temp, m_regServerport);
238 Store(m_username, m_regUsername);
239 Store(m_password, m_regPassword);
242 CString http_proxy;
243 if(!m_serveraddress.IsEmpty())
245 if (m_serveraddress.Find(_T("://")) == -1)
246 http_proxy=_T("http://");
248 if(!m_username.IsEmpty())
250 CString escapedUsername;
252 if (StringEscape(m_username, &escapedUsername))
254 ::MessageBox(NULL, _T("Could not encode username."), _T("TortoiseGit"), MB_ICONERROR);
255 return FALSE;
258 http_proxy += escapedUsername;
260 if(!m_password.IsEmpty())
262 CString escapedPassword;
263 if (StringEscape(m_password, &escapedPassword))
265 ::MessageBox(NULL, _T("Could not encode password."), _T("TortoiseGit"), MB_ICONERROR);
266 return FALSE;
268 http_proxy += _T(":") + escapedPassword;
271 http_proxy += _T("@");
273 http_proxy+=m_serveraddress;
275 if(m_serverport)
277 temp.Format(_T("%u"), m_serverport);
278 http_proxy += _T(":")+temp;
282 if (m_isEnabled)
284 g_Git.SetConfigValue(_T("http.proxy"),http_proxy,CONFIG_GLOBAL);
286 else
288 g_Git.UnsetConfigValue(_T("http.proxy"), CONFIG_GLOBAL);
290 m_regSSHClient = m_SSHClient;
291 SetModified(FALSE);
292 return ISettingsPropPage::OnApply();
295 void CSetProxyPage::OnBnClickedSshbrowse()
297 CString openPath;
298 if (CAppUtils::FileOpenSave(openPath, NULL, IDS_SETTINGS_SELECTSSH, IDS_PROGRAMSFILEFILTER, true, m_hWnd))
300 UpdateData();
301 m_SSHClient = openPath;
302 UpdateData(FALSE);
303 SetModified();