Remote list box selects newly added remote
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitRemote.cpp
blob0880a2487e817e64e60d1907ab7a730310bb889f
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 "AppUtils.h"
29 #include "git.h"
31 // CSettingGitRemote dialog
33 IMPLEMENT_DYNAMIC(CSettingGitRemote, ISettingsPropPage)
35 CSettingGitRemote::CSettingGitRemote(CString cmdPath)
36 : ISettingsPropPage(CSettingGitRemote::IDD)
37 , m_strRemote(_T(""))
38 , m_strUrl(_T(""))
39 , m_strPuttyKeyfile(_T(""))
40 , m_cmdPath(cmdPath)
41 , m_bNoFetch(false)
44 m_ChangedMask = 0;
47 CSettingGitRemote::~CSettingGitRemote()
51 void CSettingGitRemote::DoDataExchange(CDataExchange* pDX)
53 CPropertyPage::DoDataExchange(pDX);
54 DDX_Control(pDX, IDC_LIST_REMOTE, m_ctrlRemoteList);
55 DDX_Text(pDX, IDC_EDIT_REMOTE, m_strRemote);
56 DDX_Text(pDX, IDC_EDIT_URL, m_strUrl);
57 DDX_Text(pDX, IDC_EDIT_PUTTY_KEY, m_strPuttyKeyfile);
58 DDX_Control(pDX, IDC_COMBO_TAGOPT, m_ctrlTagOpt);
62 BEGIN_MESSAGE_MAP(CSettingGitRemote, CPropertyPage)
63 ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CSettingGitRemote::OnBnClickedButtonBrowse)
64 ON_BN_CLICKED(IDC_BUTTON_ADD, &CSettingGitRemote::OnBnClickedButtonAdd)
65 ON_LBN_SELCHANGE(IDC_LIST_REMOTE, &CSettingGitRemote::OnLbnSelchangeListRemote)
66 ON_EN_CHANGE(IDC_EDIT_REMOTE, &CSettingGitRemote::OnEnChangeEditRemote)
67 ON_EN_CHANGE(IDC_EDIT_URL, &CSettingGitRemote::OnEnChangeEditUrl)
68 ON_EN_CHANGE(IDC_EDIT_PUTTY_KEY, &CSettingGitRemote::OnEnChangeEditPuttyKey)
69 ON_CBN_SELCHANGE(IDC_COMBO_TAGOPT, &CSettingGitRemote::OnCbnSelchangeComboTagOpt)
70 ON_BN_CLICKED(IDC_BUTTON_REMOVE, &CSettingGitRemote::OnBnClickedButtonRemove)
71 ON_BN_CLICKED(IDC_BUTTON_ORIGIN, &CSettingGitRemote::OnBnClickedButtonOrigin)
72 END_MESSAGE_MAP()
74 BOOL CSettingGitRemote::OnInitDialog()
76 ISettingsPropPage::OnInitDialog();
78 //CString str=((CSettings*)GetParent())->m_CmdPath.GetWinPath();
79 CString proj;
80 if( g_GitAdminDir.HasAdminDir(m_cmdPath,&proj) )
82 CString title;
83 this->GetWindowText(title);
84 this->SetWindowText(title + _T(" - ") + proj);
87 STRING_VECTOR remotes;
88 g_Git.GetRemoteList(remotes);
89 m_ctrlRemoteList.ResetContent();
90 for (size_t i = 0; i < remotes.size(); i++)
91 m_ctrlRemoteList.AddString(remotes[i]);
93 m_ctrlTagOpt.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_REACHABLE)));
94 m_ctrlTagOpt.AddString(CString(MAKEINTRESOURCE(IDS_NONE)));
95 m_ctrlTagOpt.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_TAGS_ONLY)));
96 m_ctrlTagOpt.SetCurSel(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 CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER)));
111 this->UpdateData();
112 INT_PTR ret = dlg.DoModal();
113 SetCurrentDirectory(g_Git.m_CurrentDir);
114 if (ret == 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_ChangedMask = REMOTE_NAME | REMOTE_URL | REMOTE_PUTTYKEY | REMOTE_TAGOPT;
138 if(IsRemoteExist(m_strRemote))
140 CString msg;
141 msg.Format(IDS_PROC_GITCONFIG_OVERWRITEREMOTE, m_strRemote);
142 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES)
144 m_ChangedMask &= ~REMOTE_NAME;
146 else
147 return;
150 this->OnApply();
153 BOOL CSettingGitRemote::IsRemoteExist(CString &remote)
155 CString str;
156 for(int i=0;i<m_ctrlRemoteList.GetCount();i++)
158 m_ctrlRemoteList.GetText(i,str);
159 if(str == remote)
161 return true;
164 return false;
167 void CSettingGitRemote::OnLbnSelchangeListRemote()
169 CWaitCursor wait;
171 if(m_ChangedMask)
173 if(CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_SAVEREMOTE, IDS_APPNAME, 1, IDI_QUESTION, IDS_SAVEBUTTON, IDS_DISCARDBUTTON) == 1)
174 OnApply();
176 SetModified(FALSE);
178 CString cmd,output;
179 int index;
180 index = this->m_ctrlRemoteList.GetCurSel();
181 if(index<0)
183 m_strUrl.Empty();
184 m_strRemote.Empty();
185 m_strPuttyKeyfile.Empty();
186 this->UpdateData(FALSE);
187 return;
189 CString remote;
190 m_ctrlRemoteList.GetText(index,remote);
191 this->m_strRemote=remote;
193 cmd.Format(_T("remote.%s.url"),remote);
194 m_strUrl.Empty();
195 m_strUrl = g_Git.GetConfigValue(cmd, CP_UTF8);
197 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
199 this->m_strPuttyKeyfile = g_Git.GetConfigValue(cmd, CP_UTF8);
201 m_ChangedMask=0;
204 cmd.Format(_T("remote.%s.tagopt"), remote);
205 CString tagopt = g_Git.GetConfigValue(cmd, CP_UTF8);
206 index = 0;
207 if (tagopt == "--no-tags")
208 index = 1;
209 else if (tagopt == "--tags")
210 index = 2;
211 m_ctrlTagOpt.SetCurSel(index);
213 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
214 GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(TRUE);
215 this->UpdateData(FALSE);
219 void CSettingGitRemote::OnEnChangeEditRemote()
221 m_ChangedMask|=REMOTE_NAME;
223 this->UpdateData();
224 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
225 this->SetModified();
226 else
227 this->SetModified(0);
230 void CSettingGitRemote::OnEnChangeEditUrl()
232 m_ChangedMask|=REMOTE_URL;
234 this->UpdateData();
235 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
236 this->SetModified();
237 else
238 this->SetModified(0);
241 void CSettingGitRemote::OnEnChangeEditPuttyKey()
243 m_ChangedMask|=REMOTE_PUTTYKEY;
245 this->UpdateData();
246 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
247 this->SetModified();
248 else
249 this->SetModified(0);
252 void CSettingGitRemote::OnCbnSelchangeComboTagOpt()
254 m_ChangedMask |= REMOTE_TAGOPT;
256 this->UpdateData();
257 if (this->m_ctrlTagOpt.GetCurSel() > 0)
258 this->SetModified();
259 else
260 this->SetModified(0);
263 void CSettingGitRemote::Save(CString key,CString value)
265 CString cmd,out;
267 cmd.Format(_T("remote.%s.%s"),this->m_strRemote,key);
268 if (value.IsEmpty())
270 // don't check result code. it fails if the entry not exist
271 g_Git.UnsetConfigValue(cmd, CONFIG_LOCAL, CP_UTF8, &g_Git.m_CurrentDir);
272 if (!g_Git.GetConfigValue(cmd).IsEmpty())
274 CString msg;
275 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
276 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
278 return;
281 if (g_Git.SetConfigValue(cmd, value, CONFIG_LOCAL, CP_UTF8, &g_Git.m_CurrentDir))
283 CString msg;
284 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
285 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
288 BOOL CSettingGitRemote::OnApply()
290 CWaitCursor wait;
291 this->UpdateData();
292 if(m_ChangedMask & REMOTE_NAME)
294 //Add Remote
295 if(m_strRemote.IsEmpty())
297 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
298 return FALSE;
300 if(m_strUrl.IsEmpty())
302 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
303 return FALSE;
305 m_strUrl.Replace(L'\\', L'/');
306 CString cmd,out;
307 cmd.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote,m_strUrl);
308 if (g_Git.Run(cmd, &out, CP_UTF8))
310 CMessageBox::Show(NULL,out,_T("TorotiseGit"),MB_OK|MB_ICONERROR);
311 return FALSE;
313 m_ChangedMask &= ~REMOTE_URL;
315 m_ctrlRemoteList.SetCurSel(m_ctrlRemoteList.AddString(m_strRemote));
316 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
317 if (!m_bNoFetch && CMessageBox::Show(NULL, IDS_SETTINGS_FETCH_ADDEDREMOTE, IDS_APPNAME, MB_ICONQUESTION | MB_YESNO) == IDYES)
318 CCommonAppUtils::RunTortoiseGitProc(_T("/command:fetch /path:\"") + g_Git.m_CurrentDir + _T("\" /remote:\"") + m_strRemote + _T("\""));
320 if(m_ChangedMask & REMOTE_URL)
322 m_strUrl.Replace(L'\\', L'/');
323 Save(_T("url"),this->m_strUrl);
326 if(m_ChangedMask & REMOTE_PUTTYKEY)
328 Save(_T("puttykeyfile"),this->m_strPuttyKeyfile);
331 if (m_ChangedMask & REMOTE_TAGOPT)
333 CString tagopt;
334 int index = m_ctrlTagOpt.GetCurSel();
335 if (index == 1)
336 tagopt = "--no-tags";
337 else if (index == 2)
338 tagopt = "--tags";
339 Save(_T("tagopt"), tagopt);
342 SetModified(FALSE);
344 m_ChangedMask = 0;
345 return ISettingsPropPage::OnApply();
347 void CSettingGitRemote::OnBnClickedButtonRemove()
349 int index;
350 index=m_ctrlRemoteList.GetCurSel();
351 if(index>=0)
353 CString str;
354 m_ctrlRemoteList.GetText(index,str);
355 CString msg;
356 msg.Format(IDS_PROC_GITCONFIG_DELETEREMOTE, str);
357 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION) == IDYES)
359 CString cmd,out;
360 cmd.Format(_T("git.exe remote rm %s"),str);
361 if (g_Git.Run(cmd, &out, CP_UTF8))
363 CMessageBox::Show(NULL, out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
364 return;
367 m_ctrlRemoteList.DeleteString(index);
368 OnLbnSelchangeListRemote();
373 void CSettingGitRemote::OnBnClickedButtonOrigin()
375 m_strRemote = _T("origin");
376 GetDlgItem(IDC_EDIT_URL)->SetFocus();
377 UpdateData(FALSE);