Added CString wrapper for get_windows_home_directory()
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitRemote.cpp
blob5a7324afd5bbaa2ffad61fbb06c62fedcaecf901
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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 CString title;
78 this->GetWindowText(title);
79 this->SetWindowText(title + _T(" - ") + proj);
82 CString cmd, out, err;
83 cmd=_T("git.exe remote");
84 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
86 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
87 return FALSE;
89 int start =0;
90 m_ctrlRemoteList.ResetContent();
93 CString one;
94 one=out.Tokenize(_T("\n"),start);
95 if(!one.IsEmpty())
96 this->m_ctrlRemoteList.AddString(one);
98 }while(start>=0);
100 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
101 this->UpdateData(FALSE);
102 return TRUE;
104 // CSettingGitRemote message handlers
106 void CSettingGitRemote::OnBnClickedButtonBrowse()
108 CFileDialog dlg(TRUE,NULL,
109 NULL,
110 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
111 CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER)));
113 this->UpdateData();
114 if(dlg.DoModal()==IDOK)
116 this->m_strPuttyKeyfile = dlg.GetPathName();
117 this->UpdateData(FALSE);
118 OnEnChangeEditPuttyKey();
122 void CSettingGitRemote::OnBnClickedButtonAdd()
124 this->UpdateData();
126 if(m_strRemote.IsEmpty())
128 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
129 return;
131 if(m_strUrl.IsEmpty())
133 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
134 return;
137 m_strUrl.Replace(L'\\', L'/');
138 UpdateData(FALSE);
140 m_ChangedMask = REMOTE_NAME |REMOTE_URL |REMOTE_PUTTYKEY;
141 if(IsRemoteExist(m_strRemote))
143 CString msg;
144 msg.Format(IDS_PROC_GITCONFIG_OVERWRITEREMOTE, m_strRemote);
145 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES)
147 m_ChangedMask &= ~REMOTE_NAME;
149 else
150 return;
153 this->OnApply();
156 BOOL CSettingGitRemote::IsRemoteExist(CString &remote)
158 CString str;
159 for(int i=0;i<m_ctrlRemoteList.GetCount();i++)
161 m_ctrlRemoteList.GetText(i,str);
162 if(str == remote)
164 return true;
167 return false;
170 void CSettingGitRemote::OnLbnSelchangeListRemote()
172 CWaitCursor wait;
174 if(m_ChangedMask)
176 if(CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_SAVEREMOTE, IDS_APPNAME, 1, IDI_QUESTION, IDS_SAVEBUTTON, IDS_DISCARDBUTTON) == 1)
177 OnApply();
179 SetModified(FALSE);
181 CString cmd,output;
182 int index;
183 index = this->m_ctrlRemoteList.GetCurSel();
184 if(index<0)
186 m_strUrl.Empty();
187 m_strRemote.Empty();
188 m_strPuttyKeyfile.Empty();
189 this->UpdateData(FALSE);
190 return;
192 CString remote;
193 m_ctrlRemoteList.GetText(index,remote);
194 this->m_strRemote=remote;
196 cmd.Format(_T("remote.%s.url"),remote);
197 m_strUrl.Empty();
198 m_strUrl = g_Git.GetConfigValue(cmd, CP_UTF8);
200 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
202 this->m_strPuttyKeyfile = g_Git.GetConfigValue(cmd, CP_UTF8);
204 m_ChangedMask=0;
206 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
207 GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(TRUE);
208 this->UpdateData(FALSE);
212 void CSettingGitRemote::OnEnChangeEditRemote()
214 m_ChangedMask|=REMOTE_NAME;
216 this->UpdateData();
217 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
218 this->SetModified();
219 else
220 this->SetModified(0);
223 void CSettingGitRemote::OnEnChangeEditUrl()
225 m_ChangedMask|=REMOTE_URL;
227 this->UpdateData();
228 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
229 this->SetModified();
230 else
231 this->SetModified(0);
234 void CSettingGitRemote::OnEnChangeEditPuttyKey()
236 m_ChangedMask|=REMOTE_PUTTYKEY;
238 this->UpdateData();
239 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
240 this->SetModified();
241 else
242 this->SetModified(0);
244 void CSettingGitRemote::Save(CString key,CString value)
246 CString cmd,out;
248 cmd.Format(_T("remote.%s.%s"),this->m_strRemote,key);
249 if (g_Git.SetConfigValue(cmd, value, CONFIG_LOCAL, CP_UTF8, &g_Git.m_CurrentDir))
251 CString msg;
252 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
253 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
256 BOOL CSettingGitRemote::OnApply()
258 CWaitCursor wait;
259 this->UpdateData();
260 if(m_ChangedMask & REMOTE_NAME)
262 //Add Remote
263 if(m_strRemote.IsEmpty())
265 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
266 return FALSE;
268 if(m_strUrl.IsEmpty())
270 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
271 return FALSE;
273 CString cmd,out;
274 cmd.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote,m_strUrl);
275 if (g_Git.Run(cmd, &out, CP_UTF8))
277 CMessageBox::Show(NULL,out,_T("TorotiseGit"),MB_OK|MB_ICONERROR);
278 return FALSE;
280 m_ChangedMask &= ~REMOTE_URL;
282 this->m_ctrlRemoteList.AddString(m_strRemote);
283 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
285 if(m_ChangedMask & REMOTE_URL)
287 Save(_T("url"),this->m_strUrl);
290 if(m_ChangedMask & REMOTE_PUTTYKEY)
292 Save(_T("puttykeyfile"),this->m_strPuttyKeyfile);
295 SetModified(FALSE);
297 m_ChangedMask = 0;
298 return ISettingsPropPage::OnApply();
300 void CSettingGitRemote::OnBnClickedButtonRemove()
302 int index;
303 index=m_ctrlRemoteList.GetCurSel();
304 if(index>=0)
306 CString str;
307 m_ctrlRemoteList.GetText(index,str);
308 CString msg;
309 msg.Format(IDS_PROC_GITCONFIG_DELETEREMOTE, str);
310 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION) == IDYES)
312 CString cmd,out;
313 cmd.Format(_T("git.exe remote rm %s"),str);
314 if (g_Git.Run(cmd, &out, CP_UTF8))
316 CMessageBox::Show(NULL, out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
317 return;
320 m_ctrlRemoteList.DeleteString(index);
321 OnLbnSelchangeListRemote();