OSDN Git Service

22b47c4e8cdd8d85b24f46ecb425c426dc04246d
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / Settings / SettingGitRemote.cpp
1 // SettingGitRemote.cpp : implementation file\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "TortoiseProc.h"\r
6 #include "SettingGitRemote.h"\r
7 #include "Settings.h"\r
8 #include "GitAdminDir.h"\r
9 #include "MessageBox.h"\r
10 #include "git.h"\r
11 \r
12 // CSettingGitRemote dialog\r
13 \r
14 IMPLEMENT_DYNAMIC(CSettingGitRemote, ISettingsPropPage)\r
15 \r
16 CSettingGitRemote::CSettingGitRemote()\r
17         : ISettingsPropPage(CSettingGitRemote::IDD)\r
18     , m_strRemote(_T(""))\r
19     , m_strUrl(_T(""))\r
20     , m_strPuttyKeyfile(_T(""))\r
21 {\r
22 \r
23         m_ChangedMask = 0;\r
24 }\r
25 \r
26 CSettingGitRemote::~CSettingGitRemote()\r
27 {\r
28 }\r
29 \r
30 void CSettingGitRemote::DoDataExchange(CDataExchange* pDX)\r
31 {\r
32     CPropertyPage::DoDataExchange(pDX);\r
33     DDX_Control(pDX, IDC_LIST_REMOTE, m_ctrlRemoteList);\r
34     DDX_Text(pDX, IDC_EDIT_REMOTE, m_strRemote);\r
35     DDX_Text(pDX, IDC_EDIT_URL, m_strUrl);\r
36     DDX_Text(pDX, IDC_EDIT_PUTTY_KEY, m_strPuttyKeyfile);\r
37 }\r
38 \r
39 \r
40 BEGIN_MESSAGE_MAP(CSettingGitRemote, CPropertyPage)\r
41     ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CSettingGitRemote::OnBnClickedButtonBrowse)\r
42     ON_BN_CLICKED(IDC_BUTTON_ADD, &CSettingGitRemote::OnBnClickedButtonAdd)\r
43     ON_LBN_SELCHANGE(IDC_LIST_REMOTE, &CSettingGitRemote::OnLbnSelchangeListRemote)\r
44     ON_EN_CHANGE(IDC_EDIT_REMOTE, &CSettingGitRemote::OnEnChangeEditRemote)\r
45     ON_EN_CHANGE(IDC_EDIT_URL, &CSettingGitRemote::OnEnChangeEditUrl)\r
46     ON_EN_CHANGE(IDC_EDIT_PUTTY_KEY, &CSettingGitRemote::OnEnChangeEditPuttyKey)\r
47 END_MESSAGE_MAP()\r
48 \r
49 BOOL CSettingGitRemote::OnInitDialog()\r
50 {\r
51         ISettingsPropPage::OnInitDialog();\r
52 \r
53         CString str=((CSettings*)GetParent())->m_CmdPath.GetWinPath();\r
54         CString proj;\r
55         if(     g_GitAdminDir.HasAdminDir(str,&proj) )\r
56         {\r
57                 this->SetWindowText(CString(_T("Config - "))+proj);\r
58         }\r
59 \r
60         CString cmd,out;\r
61         cmd=_T("git.exe remote");\r
62         if(g_Git.Run(cmd,&out,CP_ACP))\r
63         {\r
64                 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);\r
65                 return FALSE;\r
66         }\r
67         int start =0;\r
68         m_ctrlRemoteList.ResetContent();\r
69         do\r
70         {\r
71                 CString one;\r
72                 one=out.Tokenize(_T("\n"),start);\r
73                 if(!one.IsEmpty())\r
74                         this->m_ctrlRemoteList.AddString(one);\r
75 \r
76         }while(start>=0);\r
77         \r
78         this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);\r
79         this->UpdateData(FALSE);\r
80         return TRUE;\r
81 }\r
82 // CSettingGitRemote message handlers\r
83 \r
84 void CSettingGitRemote::OnBnClickedButtonBrowse()\r
85 {\r
86     // TODO: Add your control notification handler code here\r
87         CFileDialog dlg(TRUE,NULL,\r
88                                         NULL,\r
89                                         OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,\r
90                                         _T("Putty Private Key(*.ppk)|*.ppk|All Files(*.*)|*.*||"));\r
91         \r
92         this->UpdateData();\r
93         if(dlg.DoModal()==IDOK)\r
94         {\r
95                 this->m_strPuttyKeyfile = dlg.GetPathName();\r
96                 this->UpdateData(FALSE);\r
97                 OnEnChangeEditPuttyKey();\r
98         }\r
99         \r
100 \r
101 }\r
102 \r
103 void CSettingGitRemote::OnBnClickedButtonAdd()\r
104 {\r
105     // TODO: Add your control notification handler code here\r
106         this->m_strRemote.Empty();\r
107         this->m_strUrl.Empty();\r
108         this->m_strPuttyKeyfile.Empty();\r
109         this->UpdateData(FALSE);\r
110         this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(TRUE);\r
111         this->m_ChangedMask |= REMOTE_NAME;\r
112         this->m_ctrlRemoteList.SetCurSel(-1);\r
113         GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(FALSE);\r
114 }\r
115 \r
116 void CSettingGitRemote::OnLbnSelchangeListRemote()\r
117 {\r
118     // TODO: Add your control notification handler code here\r
119         CWaitCursor wait;\r
120 \r
121         if(m_ChangedMask)\r
122         {\r
123                 if(CMessageBox::Show(NULL,_T("Remote Config Changed\nDo you want to save change now or discard change"),\r
124                                                                  _T("TortoiseGit"),MB_YESNO) == IDYES)\r
125                 {\r
126                         OnApply();\r
127                 }\r
128         }\r
129         SetModified(FALSE);\r
130         \r
131         CString cmd,output;\r
132         int index;\r
133         index = this->m_ctrlRemoteList.GetCurSel();\r
134         if(index<0)\r
135                 return;\r
136 \r
137         CString remote;\r
138         m_ctrlRemoteList.GetText(index,remote);\r
139         this->m_strRemote=remote;\r
140 \r
141         cmd.Format(_T("git.exe config remote.%s.url"),remote);\r
142         m_strUrl.Empty();\r
143         if( g_Git.Run(cmd,&m_strUrl,CP_ACP) )\r
144         {\r
145                 //CMessageBox::Show(NULL,output,_T("TortoiseGit"),MB_OK|MB_ICONERROR);\r
146                 //return;\r
147         }\r
148         \r
149         int start=0;\r
150         m_strUrl = m_strUrl.Tokenize(_T("\n"),start);\r
151 \r
152 \r
153         cmd.Format(_T("git.exe config remote.%s.puttykey"),remote);\r
154         this->m_strPuttyKeyfile.Empty();\r
155         if( g_Git.Run(cmd,&m_strPuttyKeyfile,CP_ACP) )\r
156         {\r
157                 //CMessageBox::Show(NULL,output,_T("TortoiseGit"),MB_OK|MB_ICONERROR);\r
158                 //return;\r
159         }\r
160         start=0;\r
161         m_strPuttyKeyfile = m_strPuttyKeyfile.Tokenize(_T("\n"),start);\r
162         \r
163 \r
164         m_ChangedMask=0;\r
165 \r
166         GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);\r
167         this->UpdateData(FALSE);\r
168 \r
169 }\r
170 \r
171 void CSettingGitRemote::OnEnChangeEditRemote()\r
172 {\r
173     // TODO:  If this is a RICHEDIT control, the control will not\r
174     // send this notification unless you override the ISettingsPropPage::OnInitDialog()\r
175     // function and call CRichEditCtrl().SetEventMask()\r
176     // with the ENM_CHANGE flag ORed into the mask.\r
177 \r
178     // TODO:  Add your control notification handler code here\r
179 \r
180         m_ChangedMask|=REMOTE_NAME;\r
181 \r
182         this->SetModified();\r
183 }\r
184 \r
185 void CSettingGitRemote::OnEnChangeEditUrl()\r
186 {\r
187     // TODO:  If this is a RICHEDIT control, the control will not\r
188     // send this notification unless you override the ISettingsPropPage::OnInitDialog()\r
189     // function and call CRichEditCtrl().SetEventMask()\r
190     // with the ENM_CHANGE flag ORed into the mask.\r
191 \r
192     // TODO:  Add your control notification handler code here\r
193         m_ChangedMask|=REMOTE_URL;\r
194         this->SetModified();\r
195 }\r
196 \r
197 void CSettingGitRemote::OnEnChangeEditPuttyKey()\r
198 {\r
199     // TODO:  If this is a RICHEDIT control, the control will not\r
200     // send this notification unless you override the ISettingsPropPage::OnInitDialog()\r
201     // function and call CRichEditCtrl().SetEventMask()\r
202     // with the ENM_CHANGE flag ORed into the mask.\r
203 \r
204     // TODO:  Add your control notification handler code here\r
205         m_ChangedMask|=REMOTE_PUTTYKEY;\r
206         this->SetModified();\r
207 }\r
208 void CSettingGitRemote::Save(CString key,CString value)\r
209 {\r
210         CString cmd,out;\r
211         cmd.Format(_T("git.exe config remote.%s.%s \"%s\""),this->m_strRemote,key,value);\r
212         if(g_Git.Run(cmd,&out,CP_ACP))\r
213         {\r
214                 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);\r
215         }\r
216 }\r
217 BOOL CSettingGitRemote::OnApply()\r
218 {\r
219         CWaitCursor wait;\r
220         this->UpdateData();\r
221         if(m_ChangedMask & REMOTE_NAME)\r
222         {\r
223                 //Add Remote\r
224                 if( this->m_strRemote.IsEmpty() || this->m_strUrl.IsEmpty() )\r
225                 {\r
226                         CMessageBox::Show(NULL,_T("Remote Name and URL can't be Empty"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);\r
227                         return FALSE;\r
228                 }\r
229                 CString cmd,out;\r
230                 cmd.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote,m_strUrl);\r
231                 if(g_Git.Run(cmd,&out,CP_ACP))\r
232                 {\r
233                         CMessageBox::Show(NULL,out,_T("TorotiseGit"),MB_OK|MB_ICONERROR);\r
234                         return FALSE;\r
235                 }\r
236                 m_ChangedMask &= ~REMOTE_URL;\r
237 \r
238                 this->m_ctrlRemoteList.AddString(m_strRemote);\r
239                 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);\r
240         }\r
241         if(m_ChangedMask & REMOTE_URL)\r
242         {\r
243                 Save(_T("url"),this->m_strUrl);\r
244         }\r
245 \r
246         if(m_ChangedMask & REMOTE_PUTTYKEY)\r
247         {\r
248                 Save(_T("puttykey"),this->m_strPuttyKeyfile);\r
249         }\r
250 \r
251         this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);\r
252     SetModified(FALSE);\r
253 \r
254         m_ChangedMask = 0;\r
255         return ISettingsPropPage::OnApply();\r
256 }