OLD | NEW |
| (Empty) |
1 #include "PluginStdAfx.h" | |
2 | |
3 #include "PluginDictionary.h" | |
4 #include "PluginDownloadDialog.h" | |
5 #include "PluginClient.h" | |
6 | |
7 | |
8 #define WM_USER_ENDDOWNLOAD (WM_USER + 1) | |
9 #define WM_USER_DISPLAYSTATUS (WM_USER + 2) | |
10 | |
11 | |
12 // Just a small helper to guarantee that PostMessage succeeded | |
13 inline void _PostMessage(CWindow& wnd, UINT message, WPARAM wParam = 0, LPARAM l
Param = 0) | |
14 { | |
15 while (!wnd.PostMessage(message, wParam, lParam)) | |
16 { | |
17 ::Sleep(11); | |
18 } | |
19 } | |
20 | |
21 | |
22 // CPluginDownloadDialog | |
23 LRESULT CPluginDownloadDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lPa
ram, BOOL& bHandled) | |
24 { | |
25 CAxDialogImpl<CPluginDownloadDialog>::OnInitDialog(uMsg, wParam, lParam, bHand
led); | |
26 | |
27 this->CenterWindow(); | |
28 | |
29 SetTimer(IDT_TIMER, 30, NULL); | |
30 | |
31 try | |
32 { | |
33 CPluginDictionary* dictionary = CPluginDictionary::GetInstance(); | |
34 | |
35 CString text; | |
36 | |
37 text = dictionary->Lookup("DOWNLOAD_UPDATE_TITLE"); | |
38 this->SetWindowTextW(text); | |
39 | |
40 text = dictionary->Lookup("DOWNLOAD_PLEASE_WAIT"); | |
41 SetDlgItemText(IDC_INSTALLMSG, text); | |
42 | |
43 text = dictionary->Lookup("DOWNLOAD_UPDATE_BUTTON"); | |
44 SetDlgItemText(IDC_INSTALLBTN, text); | |
45 | |
46 text = dictionary->Lookup("GENERAL_CANCEL"); | |
47 SetDlgItemText(IDCANCEL, text); | |
48 | |
49 m_errorText = dictionary->Lookup("DOWNLOAD_UPDATE_ERROR_TEXT"); | |
50 m_postText = dictionary->Lookup("DOWNLOAD_UPDATE_SUCCESS_TEXT"); | |
51 } | |
52 catch(std::runtime_error&) | |
53 { | |
54 } | |
55 | |
56 CWindow& pBar = ATL::CWindow::GetDlgItem(IDC_PROGRESS1); | |
57 | |
58 _PostMessage(pBar, PBM_SETPOS, 0); | |
59 | |
60 THREADSTRUCT* param = new THREADSTRUCT; | |
61 | |
62 param->_this = this; | |
63 param->url = m_url; | |
64 param->path = m_path; | |
65 param->hEventStop = m_eventStop; | |
66 param->errortext = m_errorText; | |
67 param->postdownloadtext = m_postText; | |
68 param->pBar = ATL::CWindow::GetDlgItem(IDC_PROGRESS1); | |
69 | |
70 DWORD dwThreadId = 1; | |
71 HANDLE hThread = ::CreateThread( | |
72 NULL, // no security attributes | |
73 0, // use default stack size | |
74 StartThread, // thread function | |
75 param, // argument to thread function | |
76 0, // use default creation flags | |
77 &dwThreadId); // returns the thread identifier | |
78 | |
79 // Check the return value for success. | |
80 if (hThread) | |
81 { | |
82 ::CloseHandle(hThread); | |
83 } | |
84 | |
85 bHandled = TRUE; | |
86 | |
87 return TRUE; // return TRUE unless you set the focus to a control | |
88 } | |
89 | |
90 | |
91 DWORD WINAPI CPluginDownloadDialog::StartThread(LPVOID param) | |
92 { | |
93 THREADSTRUCT* ts = (THREADSTRUCT*)param; | |
94 | |
95 DOWNLOADPARAM *const pDownloadParam = static_cast<DOWNLOADPARAM *>(param); | |
96 | |
97 CBSCallbackImpl bsc(pDownloadParam->hWnd, pDownloadParam->hEventStop, ts->pBar
); | |
98 | |
99 HRESULT hr = ::URLDownloadToFile(NULL, ts->url, ts->path, 0, &bsc); | |
100 | |
101 pDownloadParam->strFileName.ReleaseBuffer(SUCCEEDED(hr) ? -1 : 0); | |
102 | |
103 HWND hWnd; | |
104 | |
105 try | |
106 { | |
107 if (hr != S_OK) | |
108 { | |
109 hWnd = ts->_this->GetDlgItem(IDC_INSTALLMSG); | |
110 ::SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)ts->errortext.GetBuffer()); | |
111 | |
112 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_UPDATER, PLUGIN_ERROR_UPDATER_DOWNLOAD_FI
LE, "DownloadDialog::StartThead - URLDownloadToFile") | |
113 } | |
114 else | |
115 { | |
116 hWnd = ts->_this->GetDlgItem(IDC_INSTALLBTN); | |
117 ::EnableWindow(hWnd, TRUE); | |
118 | |
119 hWnd = ts->_this->GetDlgItem(IDC_INSTALLMSG); | |
120 ::SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)ts->postdownloadtext.GetBuffer(
)); | |
121 | |
122 _PostMessage(ts->pBar, PBM_SETPOS, (LPARAM)100); | |
123 } | |
124 } | |
125 catch(std::runtime_error&) | |
126 { | |
127 } | |
128 | |
129 //you can also call AfxEndThread() here | |
130 delete ts; | |
131 | |
132 return TRUE; | |
133 } | |
134 | |
135 | |
136 void CPluginDownloadDialog::SetUrlAndPath(CString url, CString path) | |
137 { | |
138 m_url = url; | |
139 m_path = path; | |
140 } | |
141 | |
142 LRESULT CPluginDownloadDialog::OnBnClickedInstallbtn(WORD /*wNotifyCode*/, WORD
/*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) | |
143 { | |
144 return 0; | |
145 } | |
146 | |
147 | |
148 CBSCallbackImpl::CBSCallbackImpl(HWND hWnd, HANDLE eventStop, CWindow pBar) | |
149 { | |
150 m_hWnd = hWnd; // the window handle to display status | |
151 m_pBar = pBar; | |
152 m_hEventStop = eventStop; | |
153 m_ulObjRefCount = 1; | |
154 } | |
155 | |
156 STDMETHODIMP CBSCallbackImpl::QueryInterface(REFIID riid, void **ppvObject) | |
157 { | |
158 *ppvObject = NULL; | |
159 | |
160 // IUnknown | |
161 if (::IsEqualIID(riid, __uuidof(IUnknown))) | |
162 { | |
163 *ppvObject = this; | |
164 } | |
165 // IBindStatusCallback | |
166 else if (::IsEqualIID(riid, __uuidof(IBindStatusCallback))) | |
167 { | |
168 *ppvObject = static_cast<IBindStatusCallback *>(this); | |
169 } | |
170 | |
171 if (*ppvObject) | |
172 { | |
173 (*reinterpret_cast<LPUNKNOWN *>(ppvObject))->AddRef(); | |
174 | |
175 return S_OK; | |
176 } | |
177 | |
178 return E_NOINTERFACE; | |
179 } | |
180 | |
181 STDMETHODIMP_(ULONG) CBSCallbackImpl::AddRef() | |
182 { | |
183 return ++m_ulObjRefCount; | |
184 } | |
185 | |
186 STDMETHODIMP_(ULONG) CBSCallbackImpl::Release() | |
187 { | |
188 return --m_ulObjRefCount; | |
189 } | |
190 | |
191 STDMETHODIMP CBSCallbackImpl::OnStartBinding(DWORD, IBinding *) | |
192 { | |
193 return S_OK; | |
194 } | |
195 | |
196 STDMETHODIMP CBSCallbackImpl::GetPriority(LONG *) | |
197 { | |
198 return E_NOTIMPL; | |
199 } | |
200 | |
201 STDMETHODIMP CBSCallbackImpl::OnLowResource(DWORD) | |
202 { | |
203 return S_OK; | |
204 } | |
205 | |
206 STDMETHODIMP CBSCallbackImpl::OnProgress(ULONG ulProgress, ULONG ulProgressMax,
ULONG ulStatusCode, LPCWSTR szStatusText) | |
207 { | |
208 if (m_hWnd != NULL && ::IsWindow(m_pBar)) | |
209 { | |
210 switch (ulStatusCode) | |
211 { | |
212 case 1: // url | |
213 break; | |
214 | |
215 case 2: //ip | |
216 break; | |
217 | |
218 case 4: // progress < 1; | |
219 _PostMessage(m_pBar, PBM_SETPOS, (LPARAM)0); | |
220 break; | |
221 | |
222 case 5: // progress > 0; | |
223 if (ulProgressMax > 0) | |
224 { | |
225 _PostMessage(m_pBar, PBM_SETPOS, (LPARAM)(ULONG)((FLOAT)(100*ulProgress)
/ (FLOAT)ulProgressMax)); | |
226 } | |
227 break; | |
228 | |
229 case 6: | |
230 _PostMessage(m_pBar, PBM_SETPOS, (LPARAM)100); | |
231 break; | |
232 | |
233 case 11: // not text | |
234 break; | |
235 | |
236 case 13: // MIME | |
237 break; | |
238 } | |
239 } | |
240 | |
241 if (m_hEventStop != NULL) | |
242 { | |
243 if (::WaitForSingleObject(m_hEventStop, 0) == WAIT_OBJECT_0) | |
244 { | |
245 return E_ABORT; // canceled by the user | |
246 } | |
247 } | |
248 | |
249 return S_OK; | |
250 } | |
251 | |
252 STDMETHODIMP CBSCallbackImpl::OnStopBinding(HRESULT, LPCWSTR) | |
253 { | |
254 return S_OK; | |
255 } | |
256 | |
257 STDMETHODIMP CBSCallbackImpl::GetBindInfo(DWORD *, BINDINFO *) | |
258 { | |
259 return S_OK; | |
260 } | |
261 | |
262 STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD, FORMATETC *, STGMEDI
UM *) | |
263 { | |
264 return S_OK; | |
265 } | |
266 | |
267 STDMETHODIMP CBSCallbackImpl::OnObjectAvailable(REFIID, IUnknown *) | |
268 { | |
269 return S_OK; | |
270 } | |
OLD | NEW |