OSDN Git Service

Add Git submodule update and fix ProgressDlg show Okay when command still running.
[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)\r
15 {\r
16 \r
17 }\r
18 \r
19 CProgressDlg::~CProgressDlg()\r
20 {\r
21         if(m_pThread != NULL)\r
22         {\r
23                 delete m_pThread;\r
24         }\r
25 }\r
26 \r
27 void CProgressDlg::DoDataExchange(CDataExchange* pDX)\r
28 {\r
29         CDialog::DoDataExchange(pDX);\r
30         DDX_Control(pDX, IDC_CURRENT, this->m_CurrentWork);\r
31         DDX_Control(pDX, IDC_TITLE_ANIMATE, this->m_Animate);\r
32         DDX_Control(pDX, IDC_RUN_PROGRESS, this->m_Progress);\r
33         DDX_Control(pDX, IDC_LOG, this->m_Log);\r
34 }\r
35 \r
36 \r
37 BEGIN_MESSAGE_MAP(CProgressDlg, CResizableStandAloneDialog)\r
38         ON_MESSAGE(MSG_PROGRESSDLG_UPDATE_UI, OnProgressUpdateUI)\r
39 END_MESSAGE_MAP()\r
40 \r
41 BOOL CProgressDlg::OnInitDialog()\r
42 {\r
43         CResizableStandAloneDialog::OnInitDialog();\r
44 \r
45         AddAnchor(IDC_TITLE_ANIMATE, TOP_LEFT, TOP_RIGHT);\r
46         AddAnchor(IDC_RUN_PROGRESS, TOP_LEFT,TOP_RIGHT);\r
47         AddAnchor(IDC_LOG, TOP_LEFT,BOTTOM_RIGHT);\r
48 \r
49         AddAnchor(IDOK,BOTTOM_RIGHT);\r
50         AddAnchor(IDCANCEL,BOTTOM_RIGHT);\r
51 \r
52         m_Animate.Open(IDR_DOWNLOAD);\r
53         \r
54         CString InitialText;\r
55         if ( !m_PreText.IsEmpty() )\r
56         {\r
57                 InitialText = m_PreText + _T("\r\n");\r
58         }\r
59         if (m_bShowCommand && (!m_GitCmd.IsEmpty() ))\r
60         {\r
61                 InitialText += m_GitCmd+_T("\r\n\r\n");\r
62         }\r
63         m_Log.SetWindowTextW(InitialText);\r
64         m_CurrentWork.SetWindowTextW(_T(""));\r
65 \r
66         m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);\r
67         if (m_pThread==NULL)\r
68         {\r
69 //              ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));\r
70         }\r
71         else\r
72         {\r
73                 m_pThread->m_bAutoDelete = FALSE;\r
74                 m_pThread->ResumeThread();\r
75         }\r
76 \r
77         if(!m_Title.IsEmpty())\r
78                 this->SetWindowText(m_Title);\r
79         return TRUE;\r
80 }\r
81 \r
82 UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)\r
83 {\r
84         return ((CProgressDlg*)pVoid)->ProgressThread();\r
85 }\r
86 \r
87 UINT CProgressDlg::ProgressThread()\r
88 {\r
89         PROCESS_INFORMATION pi;\r
90         HANDLE hRead;\r
91 \r
92         this->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_START,0);\r
93 \r
94         CString *pfilename;\r
95         if(m_LogFile.IsEmpty())\r
96                 pfilename=NULL;\r
97         else\r
98                 pfilename=&m_LogFile;\r
99 \r
100         m_GitCmdList.push_back(m_GitCmd);\r
101 \r
102         m_GitStatus =0;\r
103 \r
104         for(int i=0;i<m_GitCmdList.size();i++)\r
105         {\r
106                 if(m_GitCmdList[i].IsEmpty())\r
107                         continue;\r
108 \r
109                 if (m_bShowCommand && m_GitCmdList[i]!= m_GitCmd)\r
110                 {\r
111                         CString str;\r
112                         str+= m_GitCmdList[i]+_T("\r\n\r\n");\r
113                         for(int j=0;j<str.GetLength();j++)\r
114                                 this->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,str[j]);\r
115                 }\r
116 \r
117                 g_Git.RunAsync(this->m_GitCmdList[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                         this->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))\r
134                 {\r
135                         return GIT_ERROR_GET_EXIT_CODE;\r
136                 }\r
137                 m_GitStatus |= status;\r
138         }\r
139 \r
140         CloseHandle(pi.hProcess);\r
141 \r
142         CloseHandle(hRead);\r
143 \r
144         this->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_END,0);\r
145 \r
146         return 0;\r
147 }\r
148 \r
149 LRESULT CProgressDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)\r
150 {\r
151         if(wParam == MSG_PROGRESSDLG_START)\r
152         {\r
153                 m_Animate.Play(0,-1,-1);\r
154                 this->DialogEnableWindow(IDOK,FALSE);\r
155         }\r
156         if(wParam == MSG_PROGRESSDLG_END)\r
157         {\r
158                 m_Animate.Stop();\r
159                 m_Progress.SetPos(100);\r
160                 this->DialogEnableWindow(IDOK,TRUE);\r
161         }\r
162 \r
163         if(lParam != 0)\r
164                 ParserCmdOutput((TCHAR)lParam);\r
165 \r
166         return 0;\r
167 }\r
168 int CProgressDlg::FindPercentage(CString &log)\r
169 {\r
170         int s1=log.Find(_T('%'));\r
171         if(s1<0)\r
172                 return -1;\r
173 \r
174         int s2=s1-1;\r
175         for(int i=s1-1;i>=0;i--)\r
176         {\r
177                 if(log[i]>=_T('0') && log[i]<=_T('9'))\r
178                         s2=i;\r
179                 else\r
180                         break;\r
181         }\r
182         return _ttol(log.Mid(s2,s1-s2));\r
183 }\r
184 \r
185 void CProgressDlg::ParserCmdOutput(TCHAR ch)\r
186 {\r
187         TRACE(_T("%c"),ch);\r
188         if( ch == _T('\r') || ch == _T('\n'))\r
189         {\r
190                 TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));\r
191                 TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));\r
192 \r
193                 CString text;\r
194                 m_Log.GetWindowTextW(text);\r
195                 if(ch == _T('\r'))\r
196                 {\r
197                         RemoveLastLine(text);\r
198                 }\r
199                 text+=_T("\r\n")+m_LogText;\r
200                 m_Log.SetWindowTextW(text);\r
201                 \r
202                 m_Log.LineScroll(m_Log.GetLineCount());\r
203 \r
204                 int s1=m_LogText.Find(_T(':'));\r
205                 int s2=m_LogText.Find(_T('%'));\r
206                 if(s1>0 && s2>0)\r
207                 {\r
208                         this->m_CurrentWork.SetWindowTextW(m_LogText.Left(s1));\r
209                         int pos=FindPercentage(m_LogText);\r
210                         TRACE(_T("Pos %d\r\n"),pos);\r
211                         if(pos>0)\r
212                                 this->m_Progress.SetPos(pos);\r
213                 }\r
214 \r
215                 m_LogText=_T("");\r
216 \r
217         }else\r
218         {\r
219                 m_LogText+=ch;\r
220         }\r
221 \r
222 }\r
223 void CProgressDlg::RemoveLastLine(CString &str)\r
224 {\r
225         int start;\r
226         start=str.ReverseFind(_T('\n'));\r
227         if(start>0)\r
228                 str=str.Left(start);\r
229         return;\r
230 }\r
231 // CProgressDlg message handlers\r