OSDN Git Service

Git Process Dialog basic worked but can't catch git clone output
[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)\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 END_MESSAGE_MAP()\r
39 \r
40 BOOL CProgressDlg::OnInitDialog()\r
41 {\r
42         CResizableStandAloneDialog::OnInitDialog();\r
43 \r
44         AddAnchor(IDC_TITLE_ANIMATE, TOP_LEFT, TOP_RIGHT);\r
45         AddAnchor(IDC_RUN_PROGRESS, TOP_LEFT,TOP_RIGHT);\r
46         AddAnchor(IDC_LOG, TOP_LEFT,BOTTOM_RIGHT);\r
47 \r
48         AddAnchor(IDOK,BOTTOM_RIGHT);\r
49         AddAnchor(IDCANCEL,BOTTOM_RIGHT);\r
50 \r
51         m_Animate.Open(IDR_DOWNLOAD);\r
52         \r
53         m_Log.SetWindowTextW(this->m_GitCmd+_T("\r\n\r\n"));\r
54         m_CurrentWork.SetWindowTextW(_T(""));\r
55 \r
56         m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);\r
57         if (m_pThread==NULL)\r
58         {\r
59 //              ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));\r
60         }\r
61         else\r
62         {\r
63                 m_pThread->m_bAutoDelete = FALSE;\r
64                 m_pThread->ResumeThread();\r
65         }\r
66         return TRUE;\r
67 }\r
68 \r
69 UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)\r
70 {\r
71         return ((CProgressDlg*)pVoid)->ProgressThread();\r
72 }\r
73 \r
74 UINT CProgressDlg::ProgressThread()\r
75 {\r
76         PROCESS_INFORMATION pi;\r
77         HANDLE hRead;\r
78 \r
79         m_Animate.Play(0,-1,-1);\r
80 \r
81         g_Git.RunAsync(this->m_GitCmd,&pi, &hRead);\r
82         this->DialogEnableWindow(IDOK,FALSE);\r
83 \r
84         DWORD readnumber;\r
85         char buffer[2];\r
86         CString output;\r
87         while(ReadFile(hRead,buffer,1,&readnumber,NULL))\r
88         {\r
89                 buffer[readnumber]=0;\r
90                 ParserCmdOutput((TCHAR)buffer[0]);\r
91         }\r
92 \r
93         CloseHandle(pi.hThread);\r
94 \r
95         WaitForSingleObject(pi.hProcess, INFINITE);\r
96         m_GitStatus =0;\r
97 \r
98         if(!GetExitCodeProcess(pi.hProcess,&m_GitStatus))\r
99         {\r
100                 return GIT_ERROR_GET_EXIT_CODE;\r
101         }\r
102 \r
103         CloseHandle(pi.hProcess);\r
104 \r
105         CloseHandle(hRead);\r
106 \r
107         m_Progress.SetPos(100);\r
108         this->DialogEnableWindow(IDOK,TRUE);\r
109 \r
110         m_Animate.Stop();\r
111         return 0;\r
112 }\r
113 \r
114 int CProgressDlg::FindPercentage(CString &log)\r
115 {\r
116         int s1=log.Find(_T('%'));\r
117         if(s1<0)\r
118                 return -1;\r
119 \r
120         int s2=s1-1;\r
121         for(int i=s1-1;i>=0;i--)\r
122         {\r
123                 if(log[i]>=_T('0') && log[i]<=_T('9'))\r
124                         s2=i;\r
125                 else\r
126                         break;\r
127         }\r
128         return _ttol(log.Mid(s2,s1-s2));\r
129 }\r
130 \r
131 void CProgressDlg::ParserCmdOutput(TCHAR ch)\r
132 {\r
133         TRACE(_T("%c"),ch);\r
134         if( ch == _T('\r') || ch == _T('\n'))\r
135         {\r
136                 TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));\r
137                 TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));\r
138 \r
139                 CString text;\r
140                 m_Log.GetWindowTextW(text);\r
141                 if(ch == _T('\r'))\r
142                 {\r
143                         RemoveLastLine(text);\r
144                 }\r
145                 text+=_T("\r\n")+m_LogText;\r
146                 m_Log.SetWindowTextW(text);\r
147                 \r
148                 int s1=m_LogText.Find(_T(':'));\r
149                 int s2=m_LogText.Find(_T('%'));\r
150                 if(s1>0 && s2>0)\r
151                 {\r
152                         this->m_CurrentWork.SetWindowTextW(m_LogText.Left(s1));\r
153                         int pos=FindPercentage(m_LogText);\r
154                         TRACE(_T("Pos %d\r\n"),pos);\r
155                         if(pos>0)\r
156                                 this->m_Progress.SetPos(pos);\r
157                 }\r
158 \r
159                 m_LogText=_T("");\r
160 \r
161         }else\r
162         {\r
163                 m_LogText+=ch;\r
164         }\r
165 \r
166 }\r
167 void CProgressDlg::RemoveLastLine(CString &str)\r
168 {\r
169         int start;\r
170         start=str.ReverseFind(_T('\n'));\r
171         if(start>0);\r
172                 str=str.Left(start);\r
173         return;\r
174 }\r
175 // CProgressDlg message handlers\r