Fix tagopt not saved if clicking OK/Apply directly instead of "Add New/Save"
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitRemote.cpp
blobd3345dab18982d7765d3133f4af29258eeb4f936
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 END_MESSAGE_MAP()
73 BOOL CSettingGitRemote::OnInitDialog()
75 ISettingsPropPage::OnInitDialog();
77 //CString str=((CSettings*)GetParent())->m_CmdPath.GetWinPath();
78 CString proj;
79 if( g_GitAdminDir.HasAdminDir(m_cmdPath,&proj) )
81 CString title;
82 this->GetWindowText(title);
83 this->SetWindowText(title + _T(" - ") + proj);
86 STRING_VECTOR remotes;
87 g_Git.GetRemoteList(remotes);
88 m_ctrlRemoteList.ResetContent();
89 for (size_t i = 0; i < remotes.size(); i++)
90 m_ctrlRemoteList.AddString(remotes[i]);
92 m_ctrlTagOpt.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_REACHABLE)));
93 m_ctrlTagOpt.AddString(CString(MAKEINTRESOURCE(IDS_NONE)));
94 m_ctrlTagOpt.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_TAGS_ONLY)));
95 m_ctrlTagOpt.SetCurSel(0);
97 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
98 this->UpdateData(FALSE);
99 return TRUE;
101 // CSettingGitRemote message handlers
103 void CSettingGitRemote::OnBnClickedButtonBrowse()
105 CFileDialog dlg(TRUE,NULL,
106 NULL,
107 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
108 CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER)));
110 this->UpdateData();
111 INT_PTR ret = dlg.DoModal();
112 SetCurrentDirectory(g_Git.m_CurrentDir);
113 if (ret == IDOK)
115 this->m_strPuttyKeyfile = dlg.GetPathName();
116 this->UpdateData(FALSE);
117 OnEnChangeEditPuttyKey();
121 void CSettingGitRemote::OnBnClickedButtonAdd()
123 this->UpdateData();
125 if(m_strRemote.IsEmpty())
127 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
128 return;
130 if(m_strUrl.IsEmpty())
132 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
133 return;
136 m_ChangedMask = REMOTE_NAME | REMOTE_URL | REMOTE_PUTTYKEY | REMOTE_TAGOPT;
137 if(IsRemoteExist(m_strRemote))
139 CString msg;
140 msg.Format(IDS_PROC_GITCONFIG_OVERWRITEREMOTE, m_strRemote);
141 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES)
143 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, IDS_PROC_GITCONFIG_SAVEREMOTE, IDS_APPNAME, 1, IDI_QUESTION, IDS_SAVEBUTTON, IDS_DISCARDBUTTON) == 1)
173 OnApply();
175 SetModified(FALSE);
177 CString cmd,output;
178 int index;
179 index = this->m_ctrlRemoteList.GetCurSel();
180 if(index<0)
182 m_strUrl.Empty();
183 m_strRemote.Empty();
184 m_strPuttyKeyfile.Empty();
185 this->UpdateData(FALSE);
186 return;
188 CString remote;
189 m_ctrlRemoteList.GetText(index,remote);
190 this->m_strRemote=remote;
192 cmd.Format(_T("remote.%s.url"),remote);
193 m_strUrl.Empty();
194 m_strUrl = g_Git.GetConfigValue(cmd, CP_UTF8);
196 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
198 this->m_strPuttyKeyfile = g_Git.GetConfigValue(cmd, CP_UTF8);
200 m_ChangedMask=0;
203 cmd.Format(_T("remote.%s.tagopt"), remote);
204 CString tagopt = g_Git.GetConfigValue(cmd, CP_UTF8);
205 index = 0;
206 if (tagopt == "--no-tags")
207 index = 1;
208 else if (tagopt == "--tags")
209 index = 2;
210 m_ctrlTagOpt.SetCurSel(index);
212 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
213 GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(TRUE);
214 this->UpdateData(FALSE);
218 void CSettingGitRemote::OnEnChangeEditRemote()
220 m_ChangedMask|=REMOTE_NAME;
222 this->UpdateData();
223 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
224 this->SetModified();
225 else
226 this->SetModified(0);
229 void CSettingGitRemote::OnEnChangeEditUrl()
231 m_ChangedMask|=REMOTE_URL;
233 this->UpdateData();
234 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
235 this->SetModified();
236 else
237 this->SetModified(0);
240 void CSettingGitRemote::OnEnChangeEditPuttyKey()
242 m_ChangedMask|=REMOTE_PUTTYKEY;
244 this->UpdateData();
245 if( (!this->m_strRemote.IsEmpty())&&(!this->m_strUrl.IsEmpty()) )
246 this->SetModified();
247 else
248 this->SetModified(0);
251 void CSettingGitRemote::OnCbnSelchangeComboTagOpt()
253 m_ChangedMask |= REMOTE_TAGOPT;
255 this->UpdateData();
256 if (this->m_ctrlTagOpt.GetCurSel() > 0)
257 this->SetModified();
258 else
259 this->SetModified(0);
262 void CSettingGitRemote::Save(CString key,CString value)
264 CString cmd,out;
266 cmd.Format(_T("remote.%s.%s"),this->m_strRemote,key);
267 if (value.IsEmpty())
269 // don't check result code. it fails if the entry not exist
270 g_Git.UnsetConfigValue(cmd, CONFIG_LOCAL, CP_UTF8, &g_Git.m_CurrentDir);
271 if (!g_Git.GetConfigValue(cmd).IsEmpty())
273 CString msg;
274 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
275 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
277 return;
280 if (g_Git.SetConfigValue(cmd, value, CONFIG_LOCAL, CP_UTF8, &g_Git.m_CurrentDir))
282 CString msg;
283 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
284 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
287 BOOL CSettingGitRemote::OnApply()
289 CWaitCursor wait;
290 this->UpdateData();
291 if(m_ChangedMask & REMOTE_NAME)
293 //Add Remote
294 if(m_strRemote.IsEmpty())
296 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
297 return FALSE;
299 if(m_strUrl.IsEmpty())
301 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
302 return FALSE;
304 m_strUrl.Replace(L'\\', L'/');
305 CString cmd,out;
306 cmd.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote,m_strUrl);
307 if (g_Git.Run(cmd, &out, CP_UTF8))
309 CMessageBox::Show(NULL,out,_T("TorotiseGit"),MB_OK|MB_ICONERROR);
310 return FALSE;
312 m_ChangedMask &= ~REMOTE_URL;
314 this->m_ctrlRemoteList.AddString(m_strRemote);
315 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
316 if (!m_bNoFetch && CMessageBox::Show(NULL, IDS_SETTINGS_FETCH_ADDEDREMOTE, IDS_APPNAME, MB_ICONQUESTION | MB_YESNO) == IDYES)
317 CCommonAppUtils::RunTortoiseGitProc(_T("/command:fetch /path:\"") + g_Git.m_CurrentDir + _T("\" /remote:\"") + m_strRemote + _T("\""));
319 if(m_ChangedMask & REMOTE_URL)
321 m_strUrl.Replace(L'\\', L'/');
322 Save(_T("url"),this->m_strUrl);
325 if(m_ChangedMask & REMOTE_PUTTYKEY)
327 Save(_T("puttykeyfile"),this->m_strPuttyKeyfile);
330 if (m_ChangedMask & REMOTE_TAGOPT)
332 CString tagopt;
333 int index = m_ctrlTagOpt.GetCurSel();
334 if (index == 1)
335 tagopt = "--no-tags";
336 else if (index == 2)
337 tagopt = "--tags";
338 Save(_T("tagopt"), tagopt);
341 SetModified(FALSE);
343 m_ChangedMask = 0;
344 return ISettingsPropPage::OnApply();
346 void CSettingGitRemote::OnBnClickedButtonRemove()
348 int index;
349 index=m_ctrlRemoteList.GetCurSel();
350 if(index>=0)
352 CString str;
353 m_ctrlRemoteList.GetText(index,str);
354 CString msg;
355 msg.Format(IDS_PROC_GITCONFIG_DELETEREMOTE, str);
356 if(CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION) == IDYES)
358 CString cmd,out;
359 cmd.Format(_T("git.exe remote rm %s"),str);
360 if (g_Git.Run(cmd, &out, CP_UTF8))
362 CMessageBox::Show(NULL, out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
363 return;
366 m_ctrlRemoteList.DeleteString(index);
367 OnLbnSelchangeListRemote();