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.
21 #include "TortoiseProc.h"
22 #include "SetProxyPage.h"
24 #include "StringUtils.h"
26 #include "SetProxyPage.h"
27 #include "MessageBox.h"
29 IMPLEMENT_DYNAMIC(CSetProxyPage
, ISettingsPropPage
)
30 CSetProxyPage::CSetProxyPage()
31 : ISettingsPropPage(CSetProxyPage::IDD
)
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
)
71 HRESULT
StringEscape(const CString
& str_in
, CString
* escaped_string
) {
72 // http://tools.ietf.org/html/rfc3986#section-3.2.1
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
);
79 escaped_string
->ReleaseBuffer();
82 escaped_string
->Replace(_T("@"), _T("%40"));
83 escaped_string
->Replace(_T(":"), _T("%3a"));
88 HRESULT
StringUnescape(const CString
& str_in
, CString
* unescaped_string
) {
89 if (!unescaped_string
)
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);
96 unescaped_string
->ReleaseBuffer();
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('\\'));
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
;
139 start
= proxy
.Find(_T("://"),start
);
145 int at
= proxy
.Find(_T("@"), 0);
152 port
=proxy
.Find(_T(":"),start
);
154 m_serveraddress
= proxy
.Mid(start
);
156 m_serveraddress
= proxy
.Mid(start
, port
-start
);
162 username
= proxy
.Find(_T(":"),start
);
163 if(username
<=0 || username
>at
)
165 StringUnescape(proxy
.Mid(start
, at
- start
), &m_username
);
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
);
176 m_serveraddress
= proxy
.Mid(at
+1);
178 m_serveraddress
= proxy
.Mid(at
+1, port
-at
-1);
186 m_serverport
= _ttoi(proxy
.Mid(port
+1));
192 SHAutoComplete(::GetDlgItem(m_hWnd
, IDC_SSHCLIENT
), SHACF_FILESYSTEM
| SHACF_FILESYS_ONLY
);
199 void CSetProxyPage::OnBnClickedEnable()
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()
230 BOOL
CSetProxyPage::OnApply()
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
);
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
);
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
);
268 http_proxy
+= _T(":") + escapedPassword
;
271 http_proxy
+= _T("@");
273 http_proxy
+=m_serveraddress
;
277 temp
.Format(_T("%u"), m_serverport
);
278 http_proxy
+= _T(":")+temp
;
284 g_Git
.SetConfigValue(_T("http.proxy"),http_proxy
,CONFIG_GLOBAL
);
288 g_Git
.UnsetConfigValue(_T("http.proxy"), CONFIG_GLOBAL
);
290 m_regSSHClient
= m_SSHClient
;
292 return ISettingsPropPage::OnApply();
295 void CSetProxyPage::OnBnClickedSshbrowse()
298 if (CAppUtils::FileOpenSave(openPath
, NULL
, IDS_SETTINGS_SELECTSSH
, IDS_PROGRAMSFILEFILTER
, true, m_hWnd
))
301 m_SSHClient
= openPath
;