some spaces-tabs code cleanup
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitRemote.cpp
blob431168a5567c72c4d56f1255e53994f6e481cfe6
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
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "SettingGitRemote.h"
25 #include "Settings.h"
26 #include "GitAdminDir.h"
27 #include "MessageBox.h"
28 #include "git.h"
30 // CSettingGitRemote dialog
32 IMPLEMENT_DYNAMIC(CSettingGitRemote, ISettingsPropPage)
34 CSettingGitRemote::CSettingGitRemote(CString cmdPath)
35 : ISettingsPropPage(CSettingGitRemote::IDD)
36 , m_strRemote(_T(""))
37 , m_strUrl(_T(""))
38 , m_strPuttyKeyfile(_T(""))
39 , m_cmdPath(cmdPath)
42 m_ChangedMask = 0;
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)
67 END_MESSAGE_MAP()
69 BOOL CSettingGitRemote::OnInitDialog()
71 ISettingsPropPage::OnInitDialog();
73 //CString str=((CSettings*)GetParent())->m_CmdPath.GetWinPath();
74 CString proj;
75 if( g_GitAdminDir.HasAdminDir(m_cmdPath,&proj) )
77 this->SetWindowText(CString(_T("Config - "))+proj);
80 CString cmd,out;
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);
85 return FALSE;
87 int start =0;
88 m_ctrlRemoteList.ResetContent();
91 CString one;
92 one=out.Tokenize(_T("\n"),start);
93 if(!one.IsEmpty())
94 this->m_ctrlRemoteList.AddString(one);
96 }while(start>=0);
98 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
99 this->UpdateData(FALSE);
100 return TRUE;
102 // CSettingGitRemote message handlers
104 void CSettingGitRemote::OnBnClickedButtonBrowse()
106 CFileDialog dlg(TRUE,NULL,
107 NULL,
108 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
109 _T("Putty Private Key(*.ppk)|*.ppk|All Files(*.*)|*.*||"));
111 this->UpdateData();
112 if(dlg.DoModal()==IDOK)
114 this->m_strPuttyKeyfile = dlg.GetPathName();
115 this->UpdateData(FALSE);
116 OnEnChangeEditPuttyKey();
120 void CSettingGitRemote::OnBnClickedButtonAdd()
122 this->UpdateData();
124 if(m_strRemote.IsEmpty())
126 CMessageBox::Show(NULL, _T("Remote name can't empty"), _T("TortoiseGit"),MB_OK|MB_ICONERROR);
127 return;
129 if(m_strUrl.IsEmpty())
131 CMessageBox::Show(NULL, _T("Remote URL can't empty"),_T("TortoiseGit"), MB_OK|MB_ICONERROR);
132 return;
135 m_strUrl.Replace(L'\\', L'/');
136 UpdateData(FALSE);
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;
145 }else
146 return;
149 this->OnApply();
152 BOOL CSettingGitRemote::IsRemoteExist(CString &remote)
154 CString str;
155 for(int i=0;i<m_ctrlRemoteList.GetCount();i++)
157 m_ctrlRemoteList.GetText(i,str);
158 if(str == remote)
160 return true;
163 return false;
166 void CSettingGitRemote::OnLbnSelchangeListRemote()
168 CWaitCursor wait;
170 if(m_ChangedMask)
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)
175 OnApply();
178 SetModified(FALSE);
180 CString cmd,output;
181 int index;
182 index = this->m_ctrlRemoteList.GetCurSel();
183 if(index<0)
185 m_strUrl.Empty();
186 m_strRemote.Empty();
187 m_strPuttyKeyfile.Empty();
188 this->UpdateData(FALSE);
189 return;
191 CString remote;
192 m_ctrlRemoteList.GetText(index,remote);
193 this->m_strRemote=remote;
195 cmd.Format(_T("remote.%s.url"),remote);
196 m_strUrl.Empty();
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);
203 m_ChangedMask=0;
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;
215 this->UpdateData();
216 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
217 this->SetModified();
218 else
219 this->SetModified(0);
222 void CSettingGitRemote::OnEnChangeEditUrl()
224 m_ChangedMask|=REMOTE_URL;
226 this->UpdateData();
227 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
228 this->SetModified();
229 else
230 this->SetModified(0);
233 void CSettingGitRemote::OnEnChangeEditPuttyKey()
235 m_ChangedMask|=REMOTE_PUTTYKEY;
237 this->UpdateData();
238 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
239 this->SetModified();
240 else
241 this->SetModified(0);
243 void CSettingGitRemote::Save(CString key,CString value)
245 CString cmd,out;
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()
255 CWaitCursor wait;
256 this->UpdateData();
257 if(m_ChangedMask & REMOTE_NAME)
259 //Add Remote
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);
263 return FALSE;
265 CString cmd,out;
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);
270 return FALSE;
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);
287 SetModified(FALSE);
289 m_ChangedMask = 0;
290 return ISettingsPropPage::OnApply();
292 void CSettingGitRemote::OnBnClickedButtonRemove()
294 int index;
295 index=m_ctrlRemoteList.GetCurSel();
296 if(index>=0)
298 CString str;
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)
303 CString cmd,out;
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);
308 return;
311 m_ctrlRemoteList.DeleteString(index);
312 OnLbnSelchangeListRemote();