OSDN Git Service

Rewrite some progressdlg to share to Sync Dialog
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / ProgressDlg.cpp
1 // ProgressDlg.cpp : implementation file\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "TortoiseProc.h"\r
6 #include "ProgressDlg.h"\r
7 #include "Git.h"\r
8 #include "atlconv.h"\r
9 // CProgressDlg dialog\r
10 \r
11 IMPLEMENT_DYNAMIC(CProgressDlg, CResizableStandAloneDialog)\r
12 \r
13 CProgressDlg::CProgressDlg(CWnd* pParent /*=NULL*/)\r
14         : CResizableStandAloneDialog(CProgressDlg::IDD, pParent), m_bShowCommand(true), m_bAutoCloseOnSuccess(false), m_bAbort(false), m_bDone(false)\r
15 {\r
16         m_pThread = NULL;\r
17         m_bAltAbortPress=false;\r
18 }\r
19 \r
20 CProgressDlg::~CProgressDlg()\r
21 {\r
22         if(m_pThread != NULL)\r
23         {\r
24                 delete m_pThread;\r
25         }\r
26 }\r
27 \r
28 void CProgressDlg::DoDataExchange(CDataExchange* pDX)\r
29 {\r
30         CDialog::DoDataExchange(pDX);\r
31         DDX_Control(pDX, IDC_CURRENT, this->m_CurrentWork);\r
32         DDX_Control(pDX, IDC_TITLE_ANIMATE, this->m_Animate);\r
33         DDX_Control(pDX, IDC_RUN_PROGRESS, this->m_Progress);\r
34         DDX_Control(pDX, IDC_LOG, this->m_Log);\r
35 }\r
36 \r
37 \r
38 BEGIN_MESSAGE_MAP(CProgressDlg, CResizableStandAloneDialog)\r
39         ON_MESSAGE(MSG_PROGRESSDLG_UPDATE_UI, OnProgressUpdateUI)\r
40         ON_BN_CLICKED(IDOK, &CProgressDlg::OnBnClickedOk)\r
41         ON_BN_CLICKED(IDC_PROGRESS_BUTTON1,&CProgressDlg::OnBnClickedButton1)\r
42 END_MESSAGE_MAP()\r
43 \r
44 BOOL CProgressDlg::OnInitDialog()\r
45 {\r
46         CResizableStandAloneDialog::OnInitDialog();\r
47 \r
48         AddAnchor(IDC_TITLE_ANIMATE, TOP_LEFT, TOP_RIGHT);\r
49         AddAnchor(IDC_RUN_PROGRESS, TOP_LEFT,TOP_RIGHT);\r
50         AddAnchor(IDC_LOG, TOP_LEFT,BOTTOM_RIGHT);\r
51 \r
52         AddAnchor(IDOK,BOTTOM_RIGHT);\r
53         AddAnchor(IDCANCEL,BOTTOM_RIGHT);\r
54         AddAnchor(IDC_PROGRESS_BUTTON1,BOTTOM_RIGHT);\r
55 \r
56         this->GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_HIDE);\r
57         m_Animate.Open(IDR_DOWNLOAD);\r
58         \r
59         CString InitialText;\r
60         if ( !m_PreText.IsEmpty() )\r
61         {\r
62                 InitialText = m_PreText + _T("\r\n");\r
63         }\r
64 #if 0\r
65         if (m_bShowCommand && (!m_GitCmd.IsEmpty() ))\r
66         {\r
67                 InitialText += m_GitCmd+_T("\r\n\r\n");\r
68         }\r
69 #endif\r
70         m_Log.SetWindowTextW(InitialText);\r
71         m_CurrentWork.SetWindowTextW(_T(""));\r
72 \r
73         m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);\r
74         if (m_pThread==NULL)\r
75         {\r
76 //              ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));\r
77         }\r
78         else\r
79         {\r
80                 m_pThread->m_bAutoDelete = FALSE;\r
81                 m_pThread->ResumeThread();\r
82         }\r
83 \r
84         if(!m_Title.IsEmpty())\r
85                 this->SetWindowText(m_Title);\r
86         return TRUE;\r
87 }\r
88 \r
89 UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)\r
90 {\r
91         return ((CProgressDlg*)pVoid)->ProgressThread();\r
92 }\r
93 \r
94 //static function, Share with SyncDialog\r
95 UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bShowCommand,CString *pfilename,bool *bAbort)\r
96 {\r
97         UINT ret=0;\r
98 \r
99         PROCESS_INFORMATION pi;\r
100         HANDLE hRead;\r
101 \r
102         pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_START,0);\r
103 \r
104         for(int i=0;i<cmdlist.size();i++)\r
105         {\r
106                 if(cmdlist[i].IsEmpty())\r
107                         continue;\r
108 \r
109                 if (bShowCommand)\r
110                 {\r
111                         CString str;\r
112                         str+= cmdlist[i]+_T("\r\n\r\n");\r
113                         for(int j=0;j<str.GetLength();j++)\r
114                                 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,str[j]);\r
115                 }\r
116 \r
117                 g_Git.RunAsync(cmdlist[i],&pi, &hRead,pfilename);\r
118 \r
119                 DWORD readnumber;\r
120                 char buffer[2];\r
121                 CString output;\r
122                 while(ReadFile(hRead,buffer,1,&readnumber,NULL))\r
123                 {\r
124                         buffer[readnumber]=0;\r
125                         pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,(TCHAR)buffer[0]);\r
126                 }\r
127         \r
128                 CloseHandle(pi.hThread);\r
129 \r
130                 WaitForSingleObject(pi.hProcess, INFINITE);\r
131                 \r
132                 DWORD status=0;\r
133                 if(!GetExitCodeProcess(pi.hProcess,&status) || *bAbort)\r
134                 {\r
135                         CloseHandle(pi.hProcess);\r
136 \r
137                         CloseHandle(hRead);\r
138 \r
139                         pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_FAILED,0);\r
140                         return GIT_ERROR_GET_EXIT_CODE;\r
141                 }\r
142                 ret |= status;\r
143         }\r
144 \r
145         CloseHandle(pi.hProcess);\r
146 \r
147         CloseHandle(hRead);\r
148 \r
149         pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_END,0);\r
150 \r
151         return ret;\r
152 \r
153 }\r
154 \r
155 UINT CProgressDlg::ProgressThread()\r
156 {\r
157         \r
158         m_GitCmdList.push_back(m_GitCmd);\r
159 \r
160         CString *pfilename;\r
161 \r
162         if(m_LogFile.IsEmpty())\r
163                 pfilename=NULL;\r
164         else\r
165                 pfilename=&m_LogFile;   \r
166 \r
167         m_GitStatus = RunCmdList(this,m_GitCmdList,m_bShowCommand,pfilename,&m_bAbort);;\r
168         return 0;\r
169 }\r
170 \r
171 LRESULT CProgressDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)\r
172 {\r
173         if(wParam == MSG_PROGRESSDLG_START)\r
174         {\r
175                 m_Animate.Play(0,-1,-1);\r
176                 this->DialogEnableWindow(IDOK,FALSE);\r
177         }\r
178         if(wParam == MSG_PROGRESSDLG_END || wParam == MSG_PROGRESSDLG_FAILED)\r
179         {\r
180                 m_bDone = true;\r
181                 m_Animate.Stop();\r
182                 m_Progress.SetPos(100);\r
183                 this->DialogEnableWindow(IDOK,TRUE);\r
184 \r
185                 if(wParam == MSG_PROGRESSDLG_END && m_GitStatus == 0)\r
186                 {\r
187                         if(m_bAutoCloseOnSuccess)\r
188                                 EndDialog(IDOK);\r
189 \r
190                         if(!m_changeAbortButtonOnSuccessTo.IsEmpty())\r
191                         {\r
192                                 GetDlgItem(IDC_PROGRESS_BUTTON1)->SetWindowText(m_changeAbortButtonOnSuccessTo);\r
193                                 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);\r
194                                 GetDlgItem(IDCANCEL)->ShowWindow(SW_HIDE);\r
195                                 //Set default button is "close" rather than "push"\r
196                                 this->SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDOK)->m_hWnd, TRUE);\r
197                         }\r
198                         else\r
199                                 DialogEnableWindow(IDCANCEL, FALSE);\r
200                 }\r
201                 else\r
202                         DialogEnableWindow(IDCANCEL, FALSE);\r
203         }\r
204 \r
205         if(lParam != 0)\r
206                 ParserCmdOutput((TCHAR)lParam);\r
207 \r
208         return 0;\r
209 }\r
210 \r
211 //static function, Share with SyncDialog\r
212 int CProgressDlg::FindPercentage(CString &log)\r
213 {\r
214         int s1=log.Find(_T('%'));\r
215         if(s1<0)\r
216                 return -1;\r
217 \r
218         int s2=s1-1;\r
219         for(int i=s1-1;i>=0;i--)\r
220         {\r
221                 if(log[i]>=_T('0') && log[i]<=_T('9'))\r
222                         s2=i;\r
223                 else\r
224                         break;\r
225         }\r
226         return _ttol(log.Mid(s2,s1-s2));\r
227 }\r
228 \r
229 void CProgressDlg::ParserCmdOutput(TCHAR ch)\r
230 {\r
231         TRACE(_T("%c"),ch);\r
232         if( ch == _T('\r') || ch == _T('\n'))\r
233         {\r
234                 TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));\r
235                 TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));\r
236 \r
237                 CString text;\r
238                 m_Log.GetWindowTextW(text);\r
239                 if(ch == _T('\r'))\r
240                 {\r
241                         RemoveLastLine(text);\r
242                 }\r
243                 text+=_T("\r\n")+m_LogText;\r
244                 m_Log.SetWindowTextW(text);\r
245                 \r
246                 m_Log.LineScroll(m_Log.GetLineCount());\r
247 \r
248                 int s1=m_LogText.Find(_T(':'));\r
249                 int s2=m_LogText.Find(_T('%'));\r
250                 if(s1>0 && s2>0)\r
251                 {\r
252                         this->m_CurrentWork.SetWindowTextW(m_LogText.Left(s1));\r
253                         int pos=FindPercentage(m_LogText);\r
254                         TRACE(_T("Pos %d\r\n"),pos);\r
255                         if(pos>0)\r
256                                 this->m_Progress.SetPos(pos);\r
257                 }\r
258 \r
259                 m_LogText=_T("");\r
260 \r
261         }else\r
262         {\r
263                 m_LogText+=ch;\r
264         }\r
265 \r
266 }\r
267 void CProgressDlg::RemoveLastLine(CString &str)\r
268 {\r
269         int start;\r
270         start=str.ReverseFind(_T('\n'));\r
271         if(start>0)\r
272                 str=str.Left(start);\r
273         return;\r
274 }\r
275 // CProgressDlg message handlers\r
276 \r
277 void CProgressDlg::OnBnClickedOk()\r
278 {\r
279         // TODO: Add your control notification handler code here\r
280         m_Log.GetWindowText(this->m_LogText);\r
281         OnOK();\r
282 }\r
283 \r
284 void CProgressDlg::OnBnClickedButton1()\r
285 {\r
286         this->EndDialog(IDC_PROGRESS_BUTTON1);\r
287         \r
288 }\r
289 void CProgressDlg::OnCancel()\r
290 {\r
291         if(m_bDone)\r
292         {\r
293                 CResizableStandAloneDialog::OnCancel();\r
294                 return;\r
295         }\r
296         \r
297         m_bAbort = true;\r
298 }\r