[RAPPS] unuglify icons. CORE-10642
[reactos.git] / reactos / base / applications / rapps / loaddlg.c
1 /* PROJECT: ReactOS Applications Manager
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/applications/rapps/loaddlg.c
4 * PURPOSE: Displaying a download dialog
5 * COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
6 * Copyright 2004 Mike McCormack (for CodeWeavers)
7 * Copyright 2005 Ge van Geldorp (gvg@reactos.org)
8 * Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
9 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
10 */
11 /*
12 * Based on Wine dlls/shdocvw/shdocvw_main.c
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 #include "rapps.h"
30 #include <wininet.h>
31 #include <shellapi.h>
32 #include <windowsx.h>
33
34 static PAPPLICATION_INFO AppInfo;
35
36 typedef struct _IBindStatusCallbackImpl
37 {
38 const IBindStatusCallbackVtbl *vtbl;
39 LONG ref;
40 HWND hDialog;
41 BOOL *pbCancelled;
42 BOOL UrlHasBeenCopied;
43 WCHAR ProgressText[MAX_PATH];
44 } IBindStatusCallbackImpl;
45
46 static
47 HRESULT WINAPI
48 dlQueryInterface(IBindStatusCallback* This, REFIID riid, void** ppvObject)
49 {
50 if (!ppvObject) return E_POINTER;
51
52 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IBindStatusCallback))
53 {
54 IBindStatusCallback_AddRef(This);
55 *ppvObject = This;
56 return S_OK;
57 }
58
59 return E_NOINTERFACE;
60 }
61
62 static
63 ULONG WINAPI
64 dlAddRef(IBindStatusCallback* iface)
65 {
66 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl*) iface;
67 return InterlockedIncrement(&This->ref);
68 }
69
70 static
71 ULONG WINAPI
72 dlRelease(IBindStatusCallback* iface)
73 {
74 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl*) iface;
75 DWORD ref = InterlockedDecrement(&This->ref);
76
77 if (!ref)
78 {
79 DestroyWindow(This->hDialog);
80 HeapFree(GetProcessHeap(), 0, This);
81 }
82
83 return ref;
84 }
85
86 static
87 HRESULT WINAPI
88 dlOnStartBinding(IBindStatusCallback* iface, DWORD dwReserved, IBinding* pib)
89 {
90 return S_OK;
91 }
92
93 static
94 HRESULT WINAPI
95 dlGetPriority(IBindStatusCallback* iface, LONG* pnPriority)
96 {
97 return S_OK;
98 }
99
100 static
101 HRESULT WINAPI
102 dlOnLowResource( IBindStatusCallback* iface, DWORD reserved)
103 {
104 return S_OK;
105 }
106
107 static
108 HRESULT WINAPI
109 dlOnProgress(IBindStatusCallback* iface,
110 ULONG ulProgress,
111 ULONG ulProgressMax,
112 ULONG ulStatusCode,
113 LPCWSTR szStatusText)
114 {
115 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
116 HWND Item;
117 LONG r;
118
119 Item = GetDlgItem(This->hDialog, IDC_DOWNLOAD_PROGRESS);
120 if (Item && ulProgressMax)
121 {
122 WCHAR szProgress[100];
123 WCHAR szProgressMax[100];
124 UINT uiPercentage = ((ULONGLONG)ulProgress * 100) / ulProgressMax;
125
126 /* send the current progress to the progress bar */
127 SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
128
129 /* format the bits and bytes into pretty and accesible units... */
130 StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
131 StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
132
133 /* ...and post all of it to our subclassed progress bar text subroutine */
134 StringCbPrintfW(This->ProgressText,
135 sizeof(This->ProgressText),
136 L"%u%% \x2014 %ls / %ls",
137 uiPercentage,
138 szProgress,
139 szProgressMax);
140 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM)This->ProgressText);
141 }
142
143 Item = GetDlgItem(This->hDialog, IDC_DOWNLOAD_STATUS);
144 if (Item && szStatusText && wcslen(szStatusText) > 0 && This->UrlHasBeenCopied == FALSE)
145 {
146 DWORD len = wcslen(szStatusText) + 1;
147 PWSTR buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
148
149 if (buf)
150 {
151 /* beautify our url for display purposes */
152 InternetCanonicalizeUrl(szStatusText, buf, &len, ICU_DECODE | ICU_NO_ENCODE);
153 }
154 else
155 {
156 /* just use the original */
157 buf = (PWSTR)szStatusText;
158 }
159
160 /* paste it into our dialog and don't do it again in this instance */
161 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM)buf);
162 This->UrlHasBeenCopied = TRUE;
163
164 if (buf != szStatusText)
165 {
166 HeapFree(GetProcessHeap(), 0, buf);
167 }
168 }
169
170 SetLastError(0);
171 r = GetWindowLongPtrW(This->hDialog, GWLP_USERDATA);
172 if (0 != r || 0 != GetLastError())
173 {
174 *This->pbCancelled = TRUE;
175 return E_ABORT;
176 }
177
178 return S_OK;
179 }
180
181 static
182 HRESULT WINAPI
183 dlOnStopBinding(IBindStatusCallback* iface, HRESULT hresult, LPCWSTR szError)
184 {
185 return S_OK;
186 }
187
188 static
189 HRESULT WINAPI
190 dlGetBindInfo(IBindStatusCallback* iface, DWORD* grfBINDF, BINDINFO* pbindinfo)
191 {
192 return S_OK;
193 }
194
195 static
196 HRESULT WINAPI
197 dlOnDataAvailable(IBindStatusCallback* iface, DWORD grfBSCF,
198 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
199 {
200 return S_OK;
201 }
202
203 static
204 HRESULT WINAPI
205 dlOnObjectAvailable(IBindStatusCallback* iface, REFIID riid, IUnknown* punk)
206 {
207 return S_OK;
208 }
209
210 static const IBindStatusCallbackVtbl dlVtbl =
211 {
212 dlQueryInterface,
213 dlAddRef,
214 dlRelease,
215 dlOnStartBinding,
216 dlGetPriority,
217 dlOnLowResource,
218 dlOnProgress,
219 dlOnStopBinding,
220 dlGetBindInfo,
221 dlOnDataAvailable,
222 dlOnObjectAvailable
223 };
224
225 static IBindStatusCallback*
226 CreateDl(HWND Dlg, BOOL *pbCancelled)
227 {
228 IBindStatusCallbackImpl *This;
229
230 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IBindStatusCallbackImpl));
231 if (!This)
232 return NULL;
233
234 This->vtbl = &dlVtbl;
235 This->ref = 1;
236 This->hDialog = Dlg;
237 This->pbCancelled = pbCancelled;
238
239 return (IBindStatusCallback*) This;
240 }
241
242 #ifdef USE_CERT_PINNING
243 static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
244 {
245 HINTERNET hConnect;
246 HINTERNET hRequest;
247 DWORD certInfoLength;
248 BOOL Ret = FALSE;
249 INTERNET_CERTIFICATE_INFOW certInfo;
250
251 hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
252 if (hConnect)
253 {
254 hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
255 if (hRequest != NULL)
256 {
257 Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0);
258 if (Ret)
259 {
260 certInfoLength = sizeof(INTERNET_CERTIFICATE_INFOW);
261 Ret = InternetQueryOptionW(hRequest,
262 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
263 &certInfo,
264 &certInfoLength);
265 if (Ret)
266 {
267 if (certInfo.lpszEncryptionAlgName)
268 LocalFree(certInfo.lpszEncryptionAlgName);
269 if (certInfo.lpszIssuerInfo)
270 {
271 if (strcmp((LPSTR)certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
272 Ret = FALSE;
273 LocalFree(certInfo.lpszIssuerInfo);
274 }
275 if (certInfo.lpszProtocolName)
276 LocalFree(certInfo.lpszProtocolName);
277 if (certInfo.lpszSignatureAlgName)
278 LocalFree(certInfo.lpszSignatureAlgName);
279 if (certInfo.lpszSubjectInfo)
280 {
281 if (strcmp((LPSTR)certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
282 Ret = FALSE;
283 LocalFree(certInfo.lpszSubjectInfo);
284 }
285 }
286 }
287 InternetCloseHandle(hRequest);
288 }
289 InternetCloseHandle(hConnect);
290 }
291 return Ret;
292 }
293 #endif
294
295 static
296 DWORD WINAPI
297 ThreadFunc(LPVOID Context)
298 {
299 IBindStatusCallback *dl = NULL;
300 WCHAR path[MAX_PATH];
301 PWSTR p, q;
302 HWND Dlg = (HWND) Context;
303 DWORD dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
304 DWORD dwCurrentBytesRead = 0;
305 DWORD dwStatusLen = sizeof(dwStatus);
306 BOOL bCancelled = FALSE;
307 BOOL bTempfile = FALSE;
308 BOOL bCab = FALSE;
309 HINTERNET hOpen = NULL;
310 HINTERNET hFile = NULL;
311 HANDLE hOut = INVALID_HANDLE_VALUE;
312 unsigned char lpBuffer[4096];
313 const LPWSTR lpszAgent = L"RApps/1.0";
314 URL_COMPONENTS urlComponents;
315 size_t urlLength, filenameLength;
316
317 /* build the path for the download */
318 p = wcsrchr(AppInfo->szUrlDownload, L'/');
319 q = wcsrchr(AppInfo->szUrlDownload, L'?');
320
321 /* do we have a final slash separator? */
322 if (!p)
323 goto end;
324
325 /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
326 filenameLength = wcslen(p) * sizeof(WCHAR);
327
328 /* do we have query arguments in the target URL after the filename? account for them
329 (e.g. https://example.org/myfile.exe?no_adware_plz) */
330 if (q && q > p && (q - p) > 0)
331 filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
332
333 /* is this URL an update package for RAPPS? if so store it in a different place */
334 if (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0)
335 {
336 bCab = TRUE;
337 if (!GetStorageDirectory(path, _countof(path)))
338 goto end;
339 }
340 else
341 {
342 if (FAILED(StringCbCopyW(path, sizeof(path), SettingsInfo.szDownloadDir)))
343 goto end;
344 }
345
346 /* is the path valid? can we access it? */
347 if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
348 {
349 if (!CreateDirectoryW(path, NULL))
350 goto end;
351 }
352
353 /* append a \ to the provided file system path, and the filename portion from the URL after that */
354 if (FAILED(StringCbCatW(path, sizeof(path), L"\\")))
355 goto end;
356 if (FAILED(StringCbCatNW(path, sizeof(path), p + 1, filenameLength)))
357 goto end;
358
359 /* is the file already there? let's avoid having to download it */
360 if (!bCab && AppInfo->szSHA1[0] != 0 && GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
361 {
362 /* only open it in case of total correctness */
363 if (VerifyInteg(AppInfo->szSHA1, path))
364 goto run;
365 }
366
367 /* create an async download context for it */
368 bTempfile = TRUE;
369 dl = CreateDl(Context, &bCancelled);
370
371 if (dl == NULL)
372 goto end;
373
374 /* FIXME: this should just be using the system-wide proxy settings */
375 switch(SettingsInfo.Proxy)
376 {
377 case 0: /* preconfig */
378 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
379 break;
380 case 1: /* direct (no proxy) */
381 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
382 break;
383 case 2: /* use proxy */
384 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
385 break;
386 default: /* preconfig */
387 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
388 break;
389 }
390
391 if (!hOpen)
392 goto end;
393
394 hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
395 if (!hFile)
396 goto end;
397
398 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
399 goto end;
400
401 if(dwStatus != HTTP_STATUS_OK)
402 {
403 WCHAR szMsgText[MAX_STR_LEN];
404
405 if (!LoadStringW(hInst, IDS_UNABLE_TO_DOWNLOAD, szMsgText, sizeof(szMsgText) / sizeof(WCHAR)))
406 goto end;
407
408 MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR);
409 goto end;
410 }
411
412 dwStatusLen = sizeof(dwStatus);
413
414 memset(&urlComponents, 0, sizeof(urlComponents));
415 urlComponents.dwStructSize = sizeof(urlComponents);
416
417 if(FAILED(StringCbLengthW(AppInfo->szUrlDownload, sizeof(AppInfo->szUrlDownload), &urlLength)))
418 goto end;
419
420 urlLength /= sizeof(WCHAR);
421 urlComponents.dwSchemeLength = urlLength + 1;
422 urlComponents.lpszScheme = malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
423 urlComponents.dwHostNameLength = urlLength + 1;
424 urlComponents.lpszHostName = malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
425
426 if(!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength+1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
427 goto end;
428
429 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
430 HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
431
432 if(urlComponents.nScheme == INTERNET_SCHEME_FTP)
433 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
434
435 #ifdef USE_CERT_PINNING
436 /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
437 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
438 (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
439 (!CertIsValid(hOpen, urlComponents.lpszHostName)))
440 {
441 WCHAR szMsgText[MAX_STR_LEN];
442
443 if (!LoadStringW(hInst, IDS_CERT_DOES_NOT_MATCH, szMsgText, sizeof(szMsgText) / sizeof(WCHAR)))
444 goto end;
445
446 MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
447 goto end;
448 }
449 #endif
450
451 free(urlComponents.lpszScheme);
452 free(urlComponents.lpszHostName);
453
454 hOut = CreateFileW(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
455
456 if (hOut == INVALID_HANDLE_VALUE)
457 goto end;
458
459 do
460 {
461 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead)) goto end;
462 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL)) goto end;
463 dwCurrentBytesRead += dwBytesRead;
464 IBindStatusCallback_OnProgress(dl, dwCurrentBytesRead, dwContentLen, 0, AppInfo->szUrlDownload);
465 }
466 while (dwBytesRead && !bCancelled);
467
468 CloseHandle(hOut);
469 hOut = INVALID_HANDLE_VALUE;
470
471 if (bCancelled)
472 goto end;
473
474 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
475 verify its integrity by using the native advapi32.A_SHA1 functions */
476 if (!bCab && AppInfo->szSHA1[0] != 0)
477 {
478 WCHAR szMsgText[MAX_STR_LEN];
479
480 /* change a few strings in the download dialog to reflect the verification process */
481 LoadStringW(hInst, IDS_INTEG_CHECK_TITLE, szMsgText, _countof(szMsgText));
482
483 SetWindowText(Dlg, szMsgText);
484 SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM)path);
485
486 /* this may take a while, depending on the file size */
487 if (!VerifyInteg(AppInfo->szSHA1, path))
488 {
489 if (!LoadStringW(hInst, IDS_INTEG_CHECK_FAIL, szMsgText, _countof(szMsgText)))
490 goto end;
491
492 MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
493 goto end;
494 }
495 }
496
497 ShowWindow(Dlg, SW_HIDE);
498
499 run:
500 /* run it */
501 if (!bCab)
502 ShellExecuteW( NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL );
503
504 end:
505 if (hOut != INVALID_HANDLE_VALUE)
506 CloseHandle(hOut);
507
508 InternetCloseHandle(hFile);
509 InternetCloseHandle(hOpen);
510
511 if (dl)
512 IBindStatusCallback_Release(dl);
513
514 if (bTempfile)
515 {
516 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
517 DeleteFileW(path);
518 }
519
520 EndDialog(Dlg, 0);
521
522 return 0;
523 }
524
525
526 LRESULT CALLBACK
527 DownloadProgressProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
528 {
529 static WCHAR szProgressText[MAX_STR_LEN] = {0};
530
531 switch (uMsg)
532 {
533 case WM_SETTEXT:
534 {
535 if (lParam)
536 {
537 StringCbCopyW(szProgressText,
538 sizeof(szProgressText),
539 (PCWSTR)lParam);
540 }
541 }
542
543 case WM_ERASEBKGND:
544 case WM_PAINT:
545 {
546 PAINTSTRUCT ps;
547 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
548 HBITMAP hbmMem;
549 HANDLE hOld;
550 RECT myRect;
551 UINT win_width, win_height;
552
553 GetClientRect(hWnd, &myRect);
554
555 /* grab the progress bar rect size */
556 win_width = myRect.right - myRect.left;
557 win_height = myRect.bottom - myRect.top;
558
559 /* create an off-screen DC for double-buffering */
560 hdcMem = CreateCompatibleDC(hDC);
561 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
562
563 hOld = SelectObject(hdcMem, hbmMem);
564
565 /* call the original draw code and redirect it to our memory buffer */
566 DefSubclassProc(hWnd, uMsg, (WPARAM)hdcMem, lParam);
567
568 /* draw our nifty progress text over it */
569 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
570 DrawShadowText(hdcMem, szProgressText, wcslen(szProgressText),
571 &myRect,
572 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
573 GetSysColor(COLOR_CAPTIONTEXT),
574 GetSysColor(COLOR_3DSHADOW),
575 1, 1);
576
577 /* transfer the off-screen DC to the screen */
578 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
579
580 /* free the off-screen DC */
581 SelectObject(hdcMem, hOld);
582 DeleteObject(hbmMem);
583 DeleteDC(hdcMem);
584
585 EndPaint(hWnd, &ps);
586 return 0;
587 }
588
589 /* Raymond Chen says that we should safely unsubclass all the things!
590 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
591 case WM_NCDESTROY:
592 {
593 ZeroMemory(szProgressText, sizeof(szProgressText));
594 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
595 }
596
597 default:
598 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
599 }
600 }
601
602 static
603 INT_PTR CALLBACK
604 DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
605 {
606 HANDLE Thread;
607 DWORD ThreadId;
608 HWND Item;
609
610 switch (uMsg)
611 {
612 case WM_INITDIALOG:
613 {
614 HICON hIconSm = NULL, hIconBg = NULL;
615
616 hIconBg = (HICON)GetClassLongPtr(hMainWnd, GCLP_HICON);
617 hIconSm = (HICON)GetClassLongPtr(hMainWnd, GCLP_HICONSM);
618
619 if (hIconBg && hIconSm)
620 {
621 SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
622 SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
623 }
624
625 SetWindowLongPtrW(Dlg, GWLP_USERDATA, 0);
626 Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
627 if (Item)
628 {
629 /* initialize the default values for our nifty progress bar
630 and subclass it so that it learns to print a status text */
631 SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
632 SendMessageW(Item, PBM_SETPOS, 0, 0);
633
634 SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
635 }
636
637 /* add a neat placeholder until the download URL is retrieved */
638 Item = GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS);
639 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM)L"\x2022 \x2022 \x2022");
640
641 Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
642 if (!Thread)
643 return FALSE;
644
645 CloseHandle(Thread);
646 return TRUE;
647 }
648 case WM_COMMAND:
649 if (wParam == IDCANCEL)
650 {
651 SetWindowLongPtrW(Dlg, GWLP_USERDATA, 1);
652 PostMessageW(Dlg, WM_CLOSE, 0, 0);
653 }
654 return FALSE;
655
656 case WM_CLOSE:
657 EndDialog(Dlg, 0);
658 return TRUE;
659
660 default:
661 return FALSE;
662 }
663 }
664
665 BOOL
666 DownloadApplication(INT Index)
667 {
668 if (!IS_AVAILABLE_ENUM(SelectedEnumType))
669 return FALSE;
670
671 AppInfo = (PAPPLICATION_INFO) ListViewGetlParam(Index);
672 if (!AppInfo) return FALSE;
673
674 WriteLogMessage(EVENTLOG_SUCCESS, MSG_SUCCESS_INSTALL, AppInfo->szName);
675
676 DialogBoxW(hInst,
677 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
678 hMainWnd,
679 DownloadDlgProc);
680
681 return TRUE;
682 }
683
684 VOID
685 DownloadApplicationsDB(LPWSTR lpUrl)
686 {
687 APPLICATION_INFO IntInfo;
688
689 ZeroMemory(&IntInfo, sizeof(APPLICATION_INFO));
690 if (FAILED(StringCbCopyW(IntInfo.szUrlDownload,
691 sizeof(IntInfo.szUrlDownload),
692 lpUrl)))
693 {
694 return;
695 }
696
697 AppInfo = &IntInfo;
698
699 DialogBoxW(hInst,
700 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
701 hMainWnd,
702 DownloadDlgProc);
703 }
704