OSDN Git Service

Merge remove x86 build
[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 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         CString InitialText;\r
54         if ( !m_PreText.IsEmpty() )\r
55         {\r
56                 InitialText = m_PreText + _T("\r\n");\r
57         }\r
58         if (m_bShowCommand)\r
59         {\r
60                 InitialText += m_GitCmd+_T("\r\n\r\n");\r
61         }\r
62         m_Log.SetWindowTextW(InitialText);\r
63         m_CurrentWork.SetWindowTextW(_T(""));\r
64 \r
65         m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);\r
66         if (m_pThread==NULL)\r
67         {\r
68 //              ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));\r
69         }\r
70         else\r
71         {\r
72                 m_pThread->m_bAutoDelete = FALSE;\r
73                 m_pThread->ResumeThread();\r
74         }\r
75         return TRUE;\r
76 }\r
77 \r
78 UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)\r
79 {\r
80         return ((CProgressDlg*)pVoid)->ProgressThread();\r
81 }\r
82 \r
83 UINT CProgressDlg::ProgressThread()\r
84 {\r
85         PROCESS_INFORMATION pi;\r
86         HANDLE hRead;\r
87 \r
88         m_Animate.Play(0,-1,-1);\r
89 \r
90         CString *pfilename;\r
91         if(m_LogFile.IsEmpty())\r
92                 pfilename=NULL;\r
93         else\r
94                 pfilename=&m_LogFile;\r
95 \r
96         g_Git.RunAsync(this->m_GitCmd,&pi, &hRead,pfilename);\r
97         this->DialogEnableWindow(IDOK,FALSE);\r
98 \r
99         DWORD readnumber;\r
100         char buffer[2];\r
101         CString output;\r
102         while(ReadFile(hRead,buffer,1,&readnumber,NULL))\r
103         {\r
104                 buffer[readnumber]=0;\r
105                 ParserCmdOutput((TCHAR)buffer[0]);\r
106         }\r
107 \r
108         CloseHandle(pi.hThread);\r
109 \r
110         WaitForSingleObject(pi.hProcess, INFINITE);\r
111         m_GitStatus =0;\r
112 \r
113         if(!GetExitCodeProcess(pi.hProcess,&m_GitStatus))\r
114         {\r
115                 return GIT_ERROR_GET_EXIT_CODE;\r
116         }\r
117 \r
118         CloseHandle(pi.hProcess);\r
119 \r
120         CloseHandle(hRead);\r
121 \r
122         m_Progress.SetPos(100);\r
123         this->DialogEnableWindow(IDOK,TRUE);\r
124 \r
125         m_Animate.Stop();\r
126         return 0;\r
127 }\r
128 \r
129 int CProgressDlg::FindPercentage(CString &log)\r
130 {\r
131         int s1=log.Find(_T('%'));\r
132         if(s1<0)\r
133                 return -1;\r
134 \r
135         int s2=s1-1;\r
136         for(int i=s1-1;i>=0;i--)\r
137         {\r
138                 if(log[i]>=_T('0') && log[i]<=_T('9'))\r
139                         s2=i;\r
140                 else\r
141                         break;\r
142         }\r
143         return _ttol(log.Mid(s2,s1-s2));\r
144 }\r
145 \r
146 void CProgressDlg::ParserCmdOutput(TCHAR ch)\r
147 {\r
148         TRACE(_T("%c"),ch);\r
149         if( ch == _T('\r') || ch == _T('\n'))\r
150         {\r
151                 TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));\r
152                 TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));\r
153 \r
154                 CString text;\r
155                 m_Log.GetWindowTextW(text);\r
156                 if(ch == _T('\r'))\r
157                 {\r
158                         RemoveLastLine(text);\r
159                 }\r
160                 text+=_T("\r\n")+m_LogText;\r
161                 m_Log.SetWindowTextW(text);\r
162                 \r
163                 m_Log.LineScroll(m_Log.GetLineCount());\r
164 \r
165                 int s1=m_LogText.Find(_T(':'));\r
166                 int s2=m_LogText.Find(_T('%'));\r
167                 if(s1>0 && s2>0)\r
168                 {\r
169                         this->m_CurrentWork.SetWindowTextW(m_LogText.Left(s1));\r
170                         int pos=FindPercentage(m_LogText);\r
171                         TRACE(_T("Pos %d\r\n"),pos);\r
172                         if(pos>0)\r
173                                 this->m_Progress.SetPos(pos);\r
174                 }\r
175 \r
176                 m_LogText=_T("");\r
177 \r
178         }else\r
179         {\r
180                 m_LogText+=ch;\r
181         }\r
182 \r
183 }\r
184 void CProgressDlg::RemoveLastLine(CString &str)\r
185 {\r
186         int start;\r
187         start=str.ReverseFind(_T('\n'));\r
188         if(start>0)\r
189                 str=str.Left(start);\r
190         return;\r
191 }\r
192 // CProgressDlg message handlers\r