1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - 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 // SettingGitRemote.cpp : implementation file
23 #include "TortoiseProc.h"
24 #include "SettingGitRemote.h"
26 #include "GitAdminDir.h"
27 #include "MessageBox.h"
30 // CSettingGitRemote dialog
32 IMPLEMENT_DYNAMIC(CSettingGitRemote
, ISettingsPropPage
)
34 CSettingGitRemote::CSettingGitRemote(CString cmdPath
)
35 : ISettingsPropPage(CSettingGitRemote::IDD
)
38 , m_strPuttyKeyfile(_T(""))
45 CSettingGitRemote::~CSettingGitRemote()
49 void CSettingGitRemote::DoDataExchange(CDataExchange
* pDX
)
51 CPropertyPage::DoDataExchange(pDX
);
52 DDX_Control(pDX
, IDC_LIST_REMOTE
, m_ctrlRemoteList
);
53 DDX_Text(pDX
, IDC_EDIT_REMOTE
, m_strRemote
);
54 DDX_Text(pDX
, IDC_EDIT_URL
, m_strUrl
);
55 DDX_Text(pDX
, IDC_EDIT_PUTTY_KEY
, m_strPuttyKeyfile
);
59 BEGIN_MESSAGE_MAP(CSettingGitRemote
, CPropertyPage
)
60 ON_BN_CLICKED(IDC_BUTTON_BROWSE
, &CSettingGitRemote::OnBnClickedButtonBrowse
)
61 ON_BN_CLICKED(IDC_BUTTON_ADD
, &CSettingGitRemote::OnBnClickedButtonAdd
)
62 ON_LBN_SELCHANGE(IDC_LIST_REMOTE
, &CSettingGitRemote::OnLbnSelchangeListRemote
)
63 ON_EN_CHANGE(IDC_EDIT_REMOTE
, &CSettingGitRemote::OnEnChangeEditRemote
)
64 ON_EN_CHANGE(IDC_EDIT_URL
, &CSettingGitRemote::OnEnChangeEditUrl
)
65 ON_EN_CHANGE(IDC_EDIT_PUTTY_KEY
, &CSettingGitRemote::OnEnChangeEditPuttyKey
)
66 ON_BN_CLICKED(IDC_BUTTON_REMOVE
, &CSettingGitRemote::OnBnClickedButtonRemove
)
69 BOOL
CSettingGitRemote::OnInitDialog()
71 ISettingsPropPage::OnInitDialog();
73 //CString str=((CSettings*)GetParent())->m_CmdPath.GetWinPath();
75 if( g_GitAdminDir
.HasAdminDir(m_cmdPath
,&proj
) )
77 this->SetWindowText(CString(_T("Config - "))+proj
);
81 cmd
=_T("git.exe remote");
82 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
84 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
88 m_ctrlRemoteList
.ResetContent();
92 one
=out
.Tokenize(_T("\n"),start
);
94 this->m_ctrlRemoteList
.AddString(one
);
98 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
99 this->UpdateData(FALSE
);
102 // CSettingGitRemote message handlers
104 void CSettingGitRemote::OnBnClickedButtonBrowse()
106 CFileDialog
dlg(TRUE
,NULL
,
108 OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
,
109 _T("Putty Private Key(*.ppk)|*.ppk|All Files(*.*)|*.*||"));
112 if(dlg
.DoModal()==IDOK
)
114 this->m_strPuttyKeyfile
= dlg
.GetPathName();
115 this->UpdateData(FALSE
);
116 OnEnChangeEditPuttyKey();
120 void CSettingGitRemote::OnBnClickedButtonAdd()
124 if(m_strRemote
.IsEmpty())
126 CMessageBox::Show(NULL
, _T("Remote name can't empty"), _T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
129 if(m_strUrl
.IsEmpty())
131 CMessageBox::Show(NULL
, _T("Remote URL can't empty"),_T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
135 m_strUrl
.Replace(L
'\\', L
'/');
138 m_ChangedMask
= REMOTE_NAME
|REMOTE_URL
|REMOTE_PUTTYKEY
;
139 if(IsRemoteExist(m_strRemote
))
141 if(CMessageBox::Show(NULL
, m_strRemote
+ _T(" is existed\n Do you want to overwrite it"),
142 _T("TortoiseGit"), MB_YESNO
|MB_ICONQUESTION
|MB_DEFBUTTON2
) == IDYES
)
144 m_ChangedMask
&= ~REMOTE_NAME
;
152 BOOL
CSettingGitRemote::IsRemoteExist(CString
&remote
)
155 for(int i
=0;i
<m_ctrlRemoteList
.GetCount();i
++)
157 m_ctrlRemoteList
.GetText(i
,str
);
166 void CSettingGitRemote::OnLbnSelchangeListRemote()
172 if(CMessageBox::Show(NULL
,_T("Remote Config Changed\nDo you want to save change now or discard change"),
173 _T("TortoiseGit"),1,NULL
,_T("Save"),_T("Discard")) == 1)
182 index
= this->m_ctrlRemoteList
.GetCurSel();
187 m_strPuttyKeyfile
.Empty();
188 this->UpdateData(FALSE
);
192 m_ctrlRemoteList
.GetText(index
,remote
);
193 this->m_strRemote
=remote
;
195 cmd
.Format(_T("remote.%s.url"),remote
);
197 m_strUrl
= g_Git
.GetConfigValue(cmd
,CP_ACP
);
199 cmd
.Format(_T("remote.%s.puttykeyfile"),remote
);
201 this->m_strPuttyKeyfile
=g_Git
.GetConfigValue(cmd
,CP_ACP
);
205 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
206 GetDlgItem(IDC_BUTTON_REMOVE
)->EnableWindow(TRUE
);
207 this->UpdateData(FALSE
);
211 void CSettingGitRemote::OnEnChangeEditRemote()
213 m_ChangedMask
|=REMOTE_NAME
;
216 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
219 this->SetModified(0);
222 void CSettingGitRemote::OnEnChangeEditUrl()
224 m_ChangedMask
|=REMOTE_URL
;
227 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
230 this->SetModified(0);
233 void CSettingGitRemote::OnEnChangeEditPuttyKey()
235 m_ChangedMask
|=REMOTE_PUTTYKEY
;
238 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
241 this->SetModified(0);
243 void CSettingGitRemote::Save(CString key
,CString value
)
247 cmd
.Format(_T("remote.%s.%s"),this->m_strRemote
,key
);
248 if(g_Git
.SetConfigValue(cmd
, value
, CONFIG_LOCAL
, CP_ACP
, &g_Git
.m_CurrentDir
))
250 CMessageBox::Show(NULL
,_T("Fail to save config"),_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
253 BOOL
CSettingGitRemote::OnApply()
257 if(m_ChangedMask
& REMOTE_NAME
)
260 if( this->m_strRemote
.IsEmpty() || this->m_strUrl
.IsEmpty() )
262 CMessageBox::Show(NULL
,_T("Remote Name and URL can't be Empty"),_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
266 cmd
.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote
,m_strUrl
);
267 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
269 CMessageBox::Show(NULL
,out
,_T("TorotiseGit"),MB_OK
|MB_ICONERROR
);
272 m_ChangedMask
&= ~REMOTE_URL
;
274 this->m_ctrlRemoteList
.AddString(m_strRemote
);
275 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
277 if(m_ChangedMask
& REMOTE_URL
)
279 Save(_T("url"),this->m_strUrl
);
282 if(m_ChangedMask
& REMOTE_PUTTYKEY
)
284 Save(_T("puttykeyfile"),this->m_strPuttyKeyfile
);
290 return ISettingsPropPage::OnApply();
292 void CSettingGitRemote::OnBnClickedButtonRemove()
295 index
=m_ctrlRemoteList
.GetCurSel();
299 m_ctrlRemoteList
.GetText(index
,str
);
300 if(CMessageBox::Show(NULL
,str
+ _T(" will be removed\n Are you sure?"),_T("TortoiseGit"),
301 MB_YESNO
|MB_ICONQUESTION
) == IDYES
)
304 cmd
.Format(_T("git.exe remote rm %s"),str
);
305 if(g_Git
.Run(cmd
, &out
, CP_ACP
))
307 CMessageBox::Show(NULL
, out
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
311 m_ctrlRemoteList
.DeleteString(index
);
312 OnLbnSelchangeListRemote();