Restore CWD after calling CFileDialog
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitRemote.cpp
blob67bad622cb033ea7c90c1fe1728fba2d232365a0
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 STRING_VECTOR remotes;
83 g_Git.GetRemoteList(remotes);
84 m_ctrlRemoteList.ResetContent();
85 for (size_t i = 0; i < remotes.size(); i++)
86 m_ctrlRemoteList.AddString(remotes[i]);
88 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
89 this->UpdateData(FALSE);
90 return TRUE;
92 // CSettingGitRemote message handlers
94 void CSettingGitRemote::OnBnClickedButtonBrowse()
96 CFileDialog dlg(TRUE,NULL,
97 NULL,
98 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
99 CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER)));
101 this->UpdateData();
102 INT_PTR ret = dlg.DoModal();
103 SetCurrentDirectory(g_Git.m_CurrentDir);
104 if (ret == IDOK)
106 this->m_strPuttyKeyfile = dlg.GetPathName();
107 this->UpdateData(FALSE);
108 OnEnChangeEditPuttyKey();
112 void CSettingGitRemote::OnBnClickedButtonAdd()
114 this->UpdateData();
116 if(m_strRemote.IsEmpty())
118 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
119 return;
121 if(m_strUrl.IsEmpty())
123 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
124 return;
127 m_ChangedMask = REMOTE_NAME |REMOTE_URL |REMOTE_PUTTYKEY;
128 if(IsRemoteExist(m_strRemote))
130 CString msg;
131 msg.Format(IDS_PROC_GITCONFIG_OVERWRITEREMOTE, m_strRemote);
132 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES)
134 m_ChangedMask &= ~REMOTE_NAME;
136 else
137 return;
140 this->OnApply();
143 BOOL CSettingGitRemote::IsRemoteExist(CString &remote)
145 CString str;
146 for(int i=0;i<m_ctrlRemoteList.GetCount();i++)
148 m_ctrlRemoteList.GetText(i,str);
149 if(str == remote)
151 return true;
154 return false;
157 void CSettingGitRemote::OnLbnSelchangeListRemote()
159 CWaitCursor wait;
161 if(m_ChangedMask)
163 if(CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_SAVEREMOTE, IDS_APPNAME, 1, IDI_QUESTION, IDS_SAVEBUTTON, IDS_DISCARDBUTTON) == 1)
164 OnApply();
166 SetModified(FALSE);
168 CString cmd,output;
169 int index;
170 index = this->m_ctrlRemoteList.GetCurSel();
171 if(index<0)
173 m_strUrl.Empty();
174 m_strRemote.Empty();
175 m_strPuttyKeyfile.Empty();
176 this->UpdateData(FALSE);
177 return;
179 CString remote;
180 m_ctrlRemoteList.GetText(index,remote);
181 this->m_strRemote=remote;
183 cmd.Format(_T("remote.%s.url"),remote);
184 m_strUrl.Empty();
185 m_strUrl = g_Git.GetConfigValue(cmd, CP_UTF8);
187 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
189 this->m_strPuttyKeyfile = g_Git.GetConfigValue(cmd, CP_UTF8);
191 m_ChangedMask=0;
193 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
194 GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(TRUE);
195 this->UpdateData(FALSE);
199 void CSettingGitRemote::OnEnChangeEditRemote()
201 m_ChangedMask|=REMOTE_NAME;
203 this->UpdateData();
204 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
205 this->SetModified();
206 else
207 this->SetModified(0);
210 void CSettingGitRemote::OnEnChangeEditUrl()
212 m_ChangedMask|=REMOTE_URL;
214 this->UpdateData();
215 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
216 this->SetModified();
217 else
218 this->SetModified(0);
221 void CSettingGitRemote::OnEnChangeEditPuttyKey()
223 m_ChangedMask|=REMOTE_PUTTYKEY;
225 this->UpdateData();
226 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
227 this->SetModified();
228 else
229 this->SetModified(0);
231 void CSettingGitRemote::Save(CString key,CString value)
233 CString cmd,out;
235 cmd.Format(_T("remote.%s.%s"),this->m_strRemote,key);
236 if (g_Git.SetConfigValue(cmd, value, CONFIG_LOCAL, CP_UTF8, &g_Git.m_CurrentDir))
238 CString msg;
239 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
240 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
243 BOOL CSettingGitRemote::OnApply()
245 CWaitCursor wait;
246 this->UpdateData();
247 if(m_ChangedMask & REMOTE_NAME)
249 //Add Remote
250 if(m_strRemote.IsEmpty())
252 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
253 return FALSE;
255 if(m_strUrl.IsEmpty())
257 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
258 return FALSE;
260 m_strUrl.Replace(L'\\', L'/');
261 CString cmd,out;
262 cmd.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote,m_strUrl);
263 if (g_Git.Run(cmd, &out, CP_UTF8))
265 CMessageBox::Show(NULL,out,_T("TorotiseGit"),MB_OK|MB_ICONERROR);
266 return FALSE;
268 m_ChangedMask &= ~REMOTE_URL;
270 this->m_ctrlRemoteList.AddString(m_strRemote);
271 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
273 if(m_ChangedMask & REMOTE_URL)
275 m_strUrl.Replace(L'\\', L'/');
276 Save(_T("url"),this->m_strUrl);
279 if(m_ChangedMask & REMOTE_PUTTYKEY)
281 Save(_T("puttykeyfile"),this->m_strPuttyKeyfile);
284 SetModified(FALSE);
286 m_ChangedMask = 0;
287 return ISettingsPropPage::OnApply();
289 void CSettingGitRemote::OnBnClickedButtonRemove()
291 int index;
292 index=m_ctrlRemoteList.GetCurSel();
293 if(index>=0)
295 CString str;
296 m_ctrlRemoteList.GetText(index,str);
297 CString msg;
298 msg.Format(IDS_PROC_GITCONFIG_DELETEREMOTE, str);
299 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION) == IDYES)
301 CString cmd,out;
302 cmd.Format(_T("git.exe remote rm %s"),str);
303 if (g_Git.Run(cmd, &out, CP_UTF8))
305 CMessageBox::Show(NULL, out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
306 return;
309 m_ctrlRemoteList.DeleteString(index);
310 OnLbnSelchangeListRemote();