Refactored: Do not use Format if not necessary
[TortoiseGit.git] / src / TortoiseProc / CloneDlg.cpp
blob961003396cf7b9f6ddf362e3777befcc17a51aa1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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 // CloneDlg.cpp : implementation file
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "CloneDlg.h"
25 #include "BrowseFolder.h"
26 #include "MessageBox.h"
27 #include "AppUtils.h"
28 // CCloneDlg dialog
30 IMPLEMENT_DYNCREATE(CCloneDlg, CHorizontalResizableStandAloneDialog)
32 CCloneDlg::CCloneDlg(CWnd* pParent /*=NULL*/)
33 : CHorizontalResizableStandAloneDialog(CCloneDlg::IDD, pParent)
34 , m_Directory(_T(""))
36 m_bRecursive = FALSE;
37 m_bBare = FALSE;
38 m_bBranch = FALSE;
39 m_bOrigin = FALSE;
40 m_bNoCheckout = FALSE;
41 m_bSVN = FALSE;
42 m_bSVNTrunk = FALSE;
43 m_bSVNTags = FALSE;
44 m_bSVNBranch = FALSE;
45 m_bSVNFrom = FALSE;
46 m_bSVNUserName = FALSE;
47 m_bExactPath = FALSE;
49 m_strSVNTrunk = _T("trunk");
50 m_strSVNTags = _T("tags");
51 m_strSVNBranchs = _T("branches");
53 m_regBrowseUrl = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\CloneBrowse"),0);
54 m_regCloneDir = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\CloneDir"));
55 m_regUseSSHKey = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\CloneUseSSHKey"), TRUE);
56 m_nSVNFrom = 0;
58 m_bAutoloadPuttyKeyFile = m_regUseSSHKey && CAppUtils::IsSSHPutty();
60 m_nDepth = 1;
61 m_bDepth = false;
62 m_bSaving = false;
65 CCloneDlg::~CCloneDlg()
69 void CCloneDlg::DoDataExchange(CDataExchange* pDX)
71 CHorizontalResizableStandAloneDialog::DoDataExchange(pDX);
72 DDX_Control(pDX, IDC_URLCOMBO, m_URLCombo);
73 DDX_Control(pDX, IDC_PUTTYKEYFILE, m_PuttyKeyCombo);
74 DDX_Control(pDX, IDC_CLONE_BROWSE_URL, m_BrowseUrl);
75 DDX_Text(pDX, IDC_CLONE_DIR, m_Directory);
76 DDX_Check(pDX,IDC_PUTTYKEY_AUTOLOAD, m_bAutoloadPuttyKeyFile);
78 DDX_Check(pDX,IDC_CHECK_SVN, m_bSVN);
79 DDX_Check(pDX,IDC_CHECK_SVN_TRUNK, m_bSVNTrunk);
80 DDX_Check(pDX,IDC_CHECK_SVN_TAG, m_bSVNTags);
81 DDX_Check(pDX,IDC_CHECK_SVN_BRANCH, m_bSVNBranch);
82 DDX_Check(pDX,IDC_CHECK_SVN_FROM, m_bSVNFrom);
83 DDX_Check(pDX,IDC_CHECK_USERNAME, m_bSVNUserName);
85 DDX_Text(pDX, IDC_EDIT_SVN_TRUNK, m_strSVNTrunk);
86 DDX_Text(pDX, IDC_EDIT_SVN_TAG, m_strSVNTags);
87 DDX_Text(pDX, IDC_EDIT_SVN_BRANCH, m_strSVNBranchs);
88 DDX_Text(pDX, IDC_EDIT_SVN_FROM, this->m_nSVNFrom);
89 DDX_Text(pDX, IDC_EDIT_USERNAME,m_strUserName);
91 DDX_Check(pDX, IDC_CHECK_DEPTH, m_bDepth);
92 DDX_Check(pDX, IDC_CHECK_BARE, m_bBare);
93 DDX_Check(pDX, IDC_CHECK_RECURSIVE, m_bRecursive);
94 DDX_Text(pDX, IDC_EDIT_DEPTH,m_nDepth);
95 DDX_Check(pDX,IDC_CHECK_BRANCH, m_bBranch);
96 DDX_Text(pDX, IDC_EDIT_BRANCH, m_strBranch);
97 DDX_Check(pDX, IDC_CHECK_ORIGIN, m_bOrigin);
98 DDX_Text(pDX, IDC_EDIT_ORIGIN, m_strOrigin);
99 DDX_Check(pDX,IDC_CHECK_NOCHECKOUT, m_bNoCheckout);
102 BOOL CCloneDlg::OnInitDialog()
104 CHorizontalResizableStandAloneDialog::OnInitDialog();
105 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
107 AddAnchor(IDC_URLCOMBO, TOP_LEFT, TOP_RIGHT);
108 AddAnchor(IDC_CLONE_BROWSE_URL, TOP_RIGHT);
109 AddAnchor(IDC_CLONE_DIR, TOP_LEFT,TOP_RIGHT);
110 AddAnchor(IDC_CLONE_DIR_BROWSE, TOP_RIGHT);
111 AddAnchor(IDOK,BOTTOM_RIGHT);
112 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
114 AddAnchor(IDC_GROUP_CLONE,TOP_LEFT,TOP_RIGHT);
115 AddAnchor(IDC_PUTTYKEYFILE_BROWSE,TOP_RIGHT);
116 AddAnchor(IDC_PUTTYKEY_AUTOLOAD,TOP_LEFT);
117 AddAnchor(IDC_PUTTYKEYFILE,TOP_LEFT,TOP_RIGHT);
118 AddAnchor(IDC_CLONE_GROUP_SVN,TOP_LEFT,TOP_RIGHT);
119 AddAnchor(IDHELP, BOTTOM_RIGHT);
121 AdjustControlSize(IDC_CHECK_DEPTH);
122 AdjustControlSize(IDC_CHECK_RECURSIVE);
123 AdjustControlSize(IDC_CHECK_BARE);
124 AdjustControlSize(IDC_PUTTYKEY_AUTOLOAD);
125 AdjustControlSize(IDC_CHECK_SVN);
126 AdjustControlSize(IDC_CHECK_SVN_TRUNK);
127 AdjustControlSize(IDC_CHECK_SVN_TAG);
128 AdjustControlSize(IDC_CHECK_SVN_BRANCH);
129 AdjustControlSize(IDC_CHECK_SVN_FROM);
130 AdjustControlSize(IDC_CHECK_USERNAME);
132 CString tt;
133 tt.LoadString(IDS_CLONE_DEPTH_TT);
134 m_tooltips.AddTool(IDC_EDIT_DEPTH,tt);
135 m_tooltips.AddTool(IDC_CHECK_DEPTH,tt);
137 this->AddOthersToAnchor();
139 if (m_Directory.IsEmpty())
141 CString dir = m_regCloneDir;
142 int index = dir.ReverseFind('\\');
143 if (index >= 0)
144 dir = dir.Left(index);
145 m_Directory = dir;
147 if (m_Directory.IsEmpty())
149 TCHAR szPath[MAX_PATH] = {0};
150 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath)))
151 m_Directory = szPath;
153 UpdateData(FALSE);
155 m_URLCombo.SetCaseSensitive(TRUE);
156 m_URLCombo.SetURLHistory(TRUE);
157 m_URLCombo.LoadHistory(_T("Software\\TortoiseGit\\History\\repoURLS"), _T("url"));
158 if(m_URL.IsEmpty())
160 CString str = CAppUtils::GetClipboardLink(_T("git clone "));
161 str.Trim();
162 if(str.IsEmpty())
163 m_URLCombo.SetCurSel(0);
164 else
165 m_URLCombo.SetWindowText(str);
167 else
168 m_URLCombo.SetWindowText(m_URL);
170 CWnd *window=this->GetDlgItem(IDC_CLONE_DIR);
171 if(window)
172 SHAutoComplete(window->m_hWnd, SHACF_FILESYSTEM);
174 this->m_BrowseUrl.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_CLONE_DIR)));
175 this->m_BrowseUrl.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_CLONE_WEB)));
176 m_BrowseUrl.SetCurrentEntry(m_regBrowseUrl);
178 m_PuttyKeyCombo.SetPathHistory(TRUE);
179 m_PuttyKeyCombo.LoadHistory(_T("Software\\TortoiseGit\\History\\puttykey"), _T("key"));
180 m_PuttyKeyCombo.SetCurSel(0);
182 this->GetDlgItem(IDC_PUTTYKEY_AUTOLOAD)->EnableWindow( CAppUtils::IsSSHPutty() );
183 this->GetDlgItem(IDC_PUTTYKEYFILE)->EnableWindow(m_bAutoloadPuttyKeyFile);
184 this->GetDlgItem(IDC_PUTTYKEYFILE_BROWSE)->EnableWindow(m_bAutoloadPuttyKeyFile);
186 EnableSaveRestore(_T("CloneDlg"));
188 OnBnClickedCheckSvn();
189 OnBnClickedCheckDepth();
190 OnBnClickedCheckBranch();
191 OnBnClickedCheckOrigin();
192 return TRUE; // return TRUE unless you set the focus to a control
195 BEGIN_MESSAGE_MAP(CCloneDlg, CHorizontalResizableStandAloneDialog)
196 ON_BN_CLICKED(IDC_CLONE_BROWSE_URL, &CCloneDlg::OnBnClickedCloneBrowseUrl)
197 ON_BN_CLICKED(IDC_CLONE_DIR_BROWSE, &CCloneDlg::OnBnClickedCloneDirBrowse)
198 ON_BN_CLICKED(IDC_CHECK_BRANCH, &CCloneDlg::OnBnClickedCheckBranch)
199 ON_BN_CLICKED(IDC_CHECK_ORIGIN, &CCloneDlg::OnBnClickedCheckOrigin)
200 ON_BN_CLICKED(IDC_PUTTYKEYFILE_BROWSE, &CCloneDlg::OnBnClickedPuttykeyfileBrowse)
201 ON_BN_CLICKED(IDC_PUTTYKEY_AUTOLOAD, &CCloneDlg::OnBnClickedPuttykeyAutoload)
202 ON_CBN_EDITCHANGE(IDC_URLCOMBO, &CCloneDlg::OnCbnEditchangeUrlcombo)
203 ON_BN_CLICKED(IDC_CHECK_SVN, &CCloneDlg::OnBnClickedCheckSvn)
204 ON_BN_CLICKED(IDC_CHECK_SVN_TRUNK, &CCloneDlg::OnBnClickedCheckSvnTrunk)
205 ON_BN_CLICKED(IDC_CHECK_SVN_TAG, &CCloneDlg::OnBnClickedCheckSvnTag)
206 ON_BN_CLICKED(IDC_CHECK_SVN_BRANCH, &CCloneDlg::OnBnClickedCheckSvnBranch)
207 ON_BN_CLICKED(IDC_CHECK_SVN_FROM, &CCloneDlg::OnBnClickedCheckSvnFrom)
208 ON_BN_CLICKED(IDC_CHECK_DEPTH, &CCloneDlg::OnBnClickedCheckDepth)
209 ON_BN_CLICKED(IDC_CHECK_BARE, &CCloneDlg::OnBnClickedCheckBare)
210 ON_BN_CLICKED(IDC_CHECK_RECURSIVE, &CCloneDlg::OnBnClickedCheckRecursive)
211 ON_BN_CLICKED(IDC_CHECK_NOCHECKOUT, &CCloneDlg::OnBnClickedCheckRecursive)
212 ON_BN_CLICKED(IDC_CHECK_USERNAME, &CCloneDlg::OnBnClickedCheckUsername)
213 END_MESSAGE_MAP()
215 // CCloneDlg message handlers
217 void CCloneDlg::OnOK()
219 m_bSaving = true;
220 this->m_URLCombo.GetWindowTextW(m_URL);
221 m_URL.Trim();
222 UpdateData(TRUE);
223 if(m_URL.IsEmpty() || m_Directory.IsEmpty())
225 CMessageBox::Show(NULL, IDS_PROC_CLONE_URLDIREMPTY, IDS_APPNAME, MB_OK);
226 m_bSaving = false;
227 return;
230 if (m_bBranch && !g_Git.IsBranchNameValid(m_strBranch))
232 ShowEditBalloon(IDC_EDIT_BRANCH, IDS_B_T_NOTEMPTY, IDS_ERR_ERROR, TTI_ERROR);
233 m_bSaving = false;
234 return;
237 if (m_bOrigin && m_strOrigin.GetLength() == 0 && !m_bSVN)
239 ShowEditBalloon(IDC_EDIT_ORIGIN, IDS_B_T_NOTEMPTY, IDS_ERR_ERROR, TTI_ERROR);
240 m_bSaving = false;
241 return;
244 m_URLCombo.SaveHistory();
245 m_PuttyKeyCombo.SaveHistory();
246 m_regCloneDir = m_Directory;
247 m_regUseSSHKey = m_bAutoloadPuttyKeyFile;
249 this->m_PuttyKeyCombo.GetWindowText(m_strPuttyKeyFile);
250 CResizableDialog::OnOK();
251 m_bSaving = false;
255 void CCloneDlg::OnCancel()
257 CResizableDialog::OnCancel();
260 void CCloneDlg::OnBnClickedCloneBrowseUrl()
262 CBrowseFolder browseFolder;
263 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
264 CString strCloneDirectory;
266 int sel = (int)this->m_BrowseUrl.GetCurrentEntry();
267 this->m_regBrowseUrl = sel;
269 if( sel == 1 )
271 CString str;
272 m_URLCombo.GetWindowText(str);
273 ShellExecute(NULL, _T("open"), str.Trim(), NULL,NULL, SW_SHOW);
274 return ;
277 this->m_URLCombo.GetWindowTextW(strCloneDirectory);
278 if (browseFolder.Show(GetSafeHwnd(), strCloneDirectory) == CBrowseFolder::OK)
280 this->m_URLCombo.SetWindowTextW(strCloneDirectory);
284 void CCloneDlg::OnBnClickedCloneDirBrowse()
286 CBrowseFolder browseFolder;
287 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
288 CString strCloneDirectory = this->m_Directory;
289 if (browseFolder.Show(GetSafeHwnd(), strCloneDirectory) == CBrowseFolder::OK)
291 UpdateData(TRUE);
292 m_Directory = strCloneDirectory;
293 UpdateData(FALSE);
297 void CCloneDlg::OnBnClickedPuttykeyfileBrowse()
299 CFileDialog dlg(TRUE,NULL,
300 NULL,
301 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
302 CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER)));
304 this->UpdateData();
305 if(dlg.DoModal()==IDOK)
307 this->m_PuttyKeyCombo.SetWindowText( dlg.GetPathName() );
312 void CCloneDlg::OnBnClickedPuttykeyAutoload()
314 this->UpdateData();
315 this->GetDlgItem(IDC_PUTTYKEYFILE)->EnableWindow(m_bAutoloadPuttyKeyFile);
316 this->GetDlgItem(IDC_PUTTYKEYFILE_BROWSE)->EnableWindow(m_bAutoloadPuttyKeyFile);
319 void CCloneDlg::OnCbnEditchangeUrlcombo()
321 // do not update member variables from UI while saving
322 if (m_bSaving || m_bExactPath)
323 return;
325 this->UpdateData();
326 CString url;
327 m_URLCombo.GetWindowText(url);
328 url.Trim();
330 if(m_OldURL == url )
331 return;
333 m_OldURL=url;
335 //if(url.IsEmpty())
336 // return;
338 CString old;
339 old=m_ModuleName;
341 url.Replace(_T('\\'),_T('/'));
343 // add compatibility for Google Code git urls
344 url.TrimRight(L"/");
346 int start=url.ReverseFind(_T('/'));
347 if(start<0)
349 start = url.ReverseFind(_T(':'));
350 if(start <0)
351 start = url.ReverseFind(_T('@'));
353 if(start<0)
354 start = 0;
356 CString temp;
357 temp=url.Mid(start+1);
359 temp=temp.MakeLower();
361 // we've to check whether the URL ends with .git (instead of using the first .git)
362 int end = temp.Right(4) == _T(".git") ? (temp.GetLength() - 4) : temp.GetLength();
364 //CString modulename;
365 m_ModuleName=url.Mid(start+1,end);
367 start = m_Directory.ReverseFind(_T('\\'));
368 if(start <0 )
369 start = m_Directory.ReverseFind(_T('/'));
370 if(start <0 )
371 start =0;
373 int dirstart=m_Directory.Find(old,start);
374 if(dirstart>=0 && (dirstart+old.GetLength() == m_Directory.GetLength()) )
375 m_Directory=m_Directory.Left(dirstart);
377 m_Directory.TrimRight(L"\\/");
378 m_Directory += _T('\\');
379 m_Directory += m_ModuleName;
381 // check if URL starts with http://, https:// or git:// in those cases loading putty keys is only
382 // asking for passwords for keys that are never used
383 if (url.Find(L"http://", 0) >= 0 || url.Find(L"https://", 0) >= 0 || url.Find(L"git://", 0) >= 0)
384 m_bAutoloadPuttyKeyFile = false;
385 else
386 m_bAutoloadPuttyKeyFile = m_regUseSSHKey && CAppUtils::IsSSHPutty();
389 this->UpdateData(FALSE);
392 void CCloneDlg::OnBnClickedCheckSvn()
394 this->UpdateData();
396 if(this->m_bSVN)
398 CString str;
399 m_URLCombo.GetWindowText(str);
401 str.TrimRight(L"\\/");
402 if(str.GetLength()>=5 && (str.Right(5).MakeLower() == _T("trunk") ))
404 this->m_bSVNBranch=this->m_bSVNTags=this->m_bSVNTrunk = FALSE;
406 else
408 this->m_bSVNBranch=this->m_bSVNTags=this->m_bSVNTrunk = TRUE;
410 m_bDepth = false;
411 m_bBare = false;
412 m_bRecursive = false;
413 m_bBranch = FALSE;
414 this->UpdateData(FALSE);
415 OnBnClickedCheckDepth();
417 this->GetDlgItem(IDC_CHECK_DEPTH)->EnableWindow(!m_bSVN);
418 this->GetDlgItem(IDC_CHECK_BARE)->EnableWindow(!m_bSVN);
419 this->GetDlgItem(IDC_CHECK_RECURSIVE)->EnableWindow(!m_bSVN);
420 this->GetDlgItem(IDC_CHECK_BRANCH)->EnableWindow(!m_bSVN);
421 this->GetDlgItem(IDC_EDIT_BRANCH)->EnableWindow(!m_bSVN);
422 OnBnClickedCheckSvnTrunk();
423 OnBnClickedCheckSvnTag();
424 OnBnClickedCheckSvnBranch();
425 OnBnClickedCheckSvnFrom();
426 OnBnClickedCheckUsername();
429 void CCloneDlg::OnBnClickedCheckSvnTrunk()
431 UpdateData(TRUE);
432 this->GetDlgItem(IDC_CHECK_SVN_TRUNK)->EnableWindow(this->m_bSVN);
433 this->GetDlgItem(IDC_EDIT_SVN_TRUNK)->EnableWindow(this->m_bSVNTrunk&&this->m_bSVN);
436 void CCloneDlg::OnBnClickedCheckSvnTag()
438 UpdateData(TRUE);
439 this->GetDlgItem(IDC_CHECK_SVN_TAG)->EnableWindow(this->m_bSVN);
440 this->GetDlgItem(IDC_EDIT_SVN_TAG)->EnableWindow(this->m_bSVNTags&&this->m_bSVN);
443 void CCloneDlg::OnBnClickedCheckSvnBranch()
445 UpdateData(TRUE);
446 this->GetDlgItem(IDC_CHECK_SVN_BRANCH)->EnableWindow(this->m_bSVN);
447 this->GetDlgItem(IDC_EDIT_SVN_BRANCH)->EnableWindow(this->m_bSVNBranch&&this->m_bSVN);
450 void CCloneDlg::OnBnClickedCheckSvnFrom()
452 UpdateData(TRUE);
453 this->GetDlgItem(IDC_CHECK_SVN_FROM)->EnableWindow(this->m_bSVN);
454 this->GetDlgItem(IDC_EDIT_SVN_FROM)->EnableWindow(this->m_bSVNFrom&&this->m_bSVN);
457 void CCloneDlg::OnBnClickedCheckRecursive()
459 UpdateData(TRUE);
460 if (m_bRecursive || m_bNoCheckout)
462 m_bBare = FALSE;
463 GetDlgItem(IDC_CHECK_BARE)->EnableWindow(FALSE);
464 UpdateData(FALSE);
466 else
467 GetDlgItem(IDC_CHECK_BARE)->EnableWindow(TRUE);
470 void CCloneDlg::OnBnClickedCheckBare()
472 UpdateData(TRUE);
473 if (m_bBare)
475 m_bRecursive = FALSE;
476 m_bNoCheckout = FALSE;
477 GetDlgItem(IDC_CHECK_RECURSIVE)->EnableWindow(FALSE);
478 GetDlgItem(IDC_CHECK_NOCHECKOUT)->EnableWindow(FALSE);
479 UpdateData(FALSE);
481 else
483 GetDlgItem(IDC_CHECK_RECURSIVE)->EnableWindow(TRUE);
484 GetDlgItem(IDC_CHECK_NOCHECKOUT)->EnableWindow(TRUE);
487 void CCloneDlg::OnBnClickedCheckDepth()
489 UpdateData(TRUE);
490 this->GetDlgItem(IDC_EDIT_DEPTH)->EnableWindow(this->m_bDepth);
493 void CCloneDlg::OnBnClickedCheckBranch()
495 UpdateData(TRUE);
496 this->GetDlgItem(IDC_EDIT_BRANCH)->EnableWindow(this->m_bBranch);
499 void CCloneDlg::OnBnClickedCheckOrigin()
501 UpdateData(TRUE);
502 this->GetDlgItem(IDC_EDIT_ORIGIN)->EnableWindow(this->m_bOrigin);
505 void CCloneDlg::OnBnClickedCheckUsername()
507 UpdateData(TRUE);
508 this->GetDlgItem(IDC_CHECK_USERNAME)->EnableWindow(this->m_bSVN);
509 this->GetDlgItem(IDC_EDIT_USERNAME)->EnableWindow(this->m_bSVNUserName && this->m_bSVN);