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)
12 * Based on Wine dlls/shdocvw/shdocvw_main.c
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.
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.
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
33 static PAPPLICATION_INFO AppInfo
;
35 typedef struct _IBindStatusCallbackImpl
37 const IBindStatusCallbackVtbl
*vtbl
;
41 BOOL UrlHasBeenCopied
;
42 } IBindStatusCallbackImpl
;
46 dlQueryInterface(IBindStatusCallback
* This
, REFIID riid
, void** ppvObject
)
48 if (!ppvObject
) return E_POINTER
;
50 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IBindStatusCallback
))
52 IBindStatusCallback_AddRef(This
);
62 dlAddRef(IBindStatusCallback
* iface
)
64 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
65 return InterlockedIncrement(&This
->ref
);
70 dlRelease(IBindStatusCallback
* iface
)
72 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
73 DWORD ref
= InterlockedDecrement(&This
->ref
);
77 DestroyWindow(This
->hDialog
);
78 HeapFree(GetProcessHeap(), 0, This
);
86 dlOnStartBinding(IBindStatusCallback
* iface
, DWORD dwReserved
, IBinding
* pib
)
93 dlGetPriority(IBindStatusCallback
* iface
, LONG
* pnPriority
)
100 dlOnLowResource( IBindStatusCallback
* iface
, DWORD reserved
)
107 dlOnProgress(IBindStatusCallback
* iface
,
111 LPCWSTR szStatusText
)
113 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
117 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_PROGRESS
);
118 if (Item
&& ulProgressMax
)
120 SendMessageW(Item
, PBM_SETPOS
, ((ULONGLONG
)ulProgress
* 100) / ulProgressMax
, 0);
123 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_STATUS
);
124 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && This
->UrlHasBeenCopied
== FALSE
)
126 DWORD len
= wcslen(szStatusText
) * sizeof(WCHAR
);
127 PWSTR buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
131 /* beautify our url for display purposes */
132 InternetCanonicalizeUrl(szStatusText
, buf
, &len
, ICU_DECODE
| ICU_NO_ENCODE
);
134 /* paste it into our dialog, free the temp buffer
135 and don't do it again in this instance */
136 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)buf
);
137 HeapFree(GetProcessHeap(), 0, buf
);
141 /* our computer is old and rusty and does not have enough ram for this,
142 use the ugly version and call it a day */
143 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)szStatusText
);
146 This
->UrlHasBeenCopied
= TRUE
;
150 r
= GetWindowLongPtrW(This
->hDialog
, GWLP_USERDATA
);
151 if (0 != r
|| 0 != GetLastError())
153 *This
->pbCancelled
= TRUE
;
162 dlOnStopBinding(IBindStatusCallback
* iface
, HRESULT hresult
, LPCWSTR szError
)
169 dlGetBindInfo(IBindStatusCallback
* iface
, DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
176 dlOnDataAvailable(IBindStatusCallback
* iface
, DWORD grfBSCF
,
177 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
184 dlOnObjectAvailable(IBindStatusCallback
* iface
, REFIID riid
, IUnknown
* punk
)
189 static const IBindStatusCallbackVtbl dlVtbl
=
204 static IBindStatusCallback
*
205 CreateDl(HWND Dlg
, BOOL
*pbCancelled
)
207 IBindStatusCallbackImpl
*This
;
209 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IBindStatusCallbackImpl
));
213 This
->vtbl
= &dlVtbl
;
216 This
->pbCancelled
= pbCancelled
;
218 return (IBindStatusCallback
*) This
;
221 #ifdef USE_CERT_PINNING
222 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
226 DWORD certInfoLength
;
228 INTERNET_CERTIFICATE_INFOW certInfo
;
230 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
233 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
234 if (hRequest
!= NULL
)
236 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
239 certInfoLength
= sizeof(INTERNET_CERTIFICATE_INFOW
);
240 Ret
= InternetQueryOptionW(hRequest
,
241 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
246 if (certInfo
.lpszEncryptionAlgName
)
247 LocalFree(certInfo
.lpszEncryptionAlgName
);
248 if (certInfo
.lpszIssuerInfo
)
250 if (strcmp((LPSTR
)certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
252 LocalFree(certInfo
.lpszIssuerInfo
);
254 if (certInfo
.lpszProtocolName
)
255 LocalFree(certInfo
.lpszProtocolName
);
256 if (certInfo
.lpszSignatureAlgName
)
257 LocalFree(certInfo
.lpszSignatureAlgName
);
258 if (certInfo
.lpszSubjectInfo
)
260 if (strcmp((LPSTR
)certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
262 LocalFree(certInfo
.lpszSubjectInfo
);
266 InternetCloseHandle(hRequest
);
275 ThreadFunc(LPVOID Context
)
277 IBindStatusCallback
*dl
= NULL
;
278 WCHAR path
[MAX_PATH
];
280 HWND Dlg
= (HWND
) Context
;
281 DWORD dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
282 DWORD dwCurrentBytesRead
= 0;
283 DWORD dwStatusLen
= sizeof(dwStatus
);
284 BOOL bCancelled
= FALSE
;
285 BOOL bTempfile
= FALSE
;
287 HINTERNET hOpen
= NULL
;
288 HINTERNET hFile
= NULL
;
289 HANDLE hOut
= INVALID_HANDLE_VALUE
;
290 unsigned char lpBuffer
[4096];
291 const LPWSTR lpszAgent
= L
"RApps/1.0";
292 URL_COMPONENTS urlComponents
;
295 /* built the path for the download */
296 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
301 if (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0)
304 if (!GetStorageDirectory(path
, sizeof(path
) / sizeof(path
[0])))
309 if (FAILED(StringCbCopyW(path
, sizeof(path
), SettingsInfo
.szDownloadDir
)))
314 if (GetFileAttributesW(path
) == INVALID_FILE_ATTRIBUTES
)
316 if (!CreateDirectoryW(path
, NULL
))
320 if (FAILED(StringCbCatW(path
, sizeof(path
), L
"\\")))
322 if (FAILED(StringCbCatW(path
, sizeof(path
), p
+ 1)))
327 dl
= CreateDl(Context
, &bCancelled
);
332 switch(SettingsInfo
.Proxy
)
334 case 0: /* preconfig */
335 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
337 case 1: /* direct (no proxy) */
338 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
340 case 2: /* use proxy */
341 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
343 default: /* preconfig */
344 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
351 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
|INTERNET_FLAG_KEEP_CONNECTION
, 0);
355 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
358 if(dwStatus
!= HTTP_STATUS_OK
)
360 WCHAR szMsgText
[MAX_STR_LEN
];
362 if (!LoadStringW(hInst
, IDS_UNABLE_TO_DOWNLOAD
, szMsgText
, sizeof(szMsgText
) / sizeof(WCHAR
)))
365 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
369 dwStatusLen
= sizeof(dwStatus
);
371 memset(&urlComponents
, 0, sizeof(urlComponents
));
372 urlComponents
.dwStructSize
= sizeof(urlComponents
);
374 if(FAILED(StringCbLengthW(AppInfo
->szUrlDownload
, sizeof(AppInfo
->szUrlDownload
), &urlLength
)))
377 urlComponents
.dwSchemeLength
= urlLength
*sizeof(WCHAR
);
378 urlComponents
.lpszScheme
= malloc(urlComponents
.dwSchemeLength
);
379 urlComponents
.dwHostNameLength
= urlLength
*sizeof(WCHAR
);
380 urlComponents
.lpszHostName
= malloc(urlComponents
.dwHostNameLength
);
382 if(!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
385 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
386 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
388 if(urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
389 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
391 #ifdef USE_CERT_PINNING
392 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) && (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
394 WCHAR szMsgText
[MAX_STR_LEN
];
396 if (!LoadStringW(hInst
, IDS_CERT_DOES_NOT_MATCH
, szMsgText
, sizeof(szMsgText
) / sizeof(WCHAR
)))
399 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
404 free(urlComponents
.lpszScheme
);
405 free(urlComponents
.lpszHostName
);
407 hOut
= CreateFileW(path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
409 if (hOut
== INVALID_HANDLE_VALUE
)
414 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
)) goto end
;
415 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
)) goto end
;
416 dwCurrentBytesRead
+= dwBytesRead
;
417 IBindStatusCallback_OnProgress(dl
, dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
422 hOut
= INVALID_HANDLE_VALUE
;
427 ShowWindow(Dlg
, SW_HIDE
);
431 ShellExecuteW( NULL
, L
"open", path
, NULL
, NULL
, SW_SHOWNORMAL
);
434 if (hOut
!= INVALID_HANDLE_VALUE
)
437 InternetCloseHandle(hFile
);
438 InternetCloseHandle(hOpen
);
441 IBindStatusCallback_Release(dl
);
445 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
456 DownloadDlgProc(HWND Dlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
466 HICON hIconSm
= NULL
, hIconBg
= NULL
;
468 hIconBg
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICON
);
469 hIconSm
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICONSM
);
471 if (hIconBg
&& hIconSm
)
473 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
474 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
477 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
478 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
481 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
482 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
485 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
486 if (!Thread
) return FALSE
;
492 if (wParam
== IDCANCEL
)
494 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
495 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
509 DownloadApplication(INT Index
)
511 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
514 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
515 if (!AppInfo
) return FALSE
;
517 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
);
520 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
528 DownloadApplicationsDB(LPWSTR lpUrl
)
530 APPLICATION_INFO IntInfo
;
532 ZeroMemory(&IntInfo
, sizeof(APPLICATION_INFO
));
533 if (FAILED(StringCbCopyW(IntInfo
.szUrlDownload
,
534 sizeof(IntInfo
.szUrlDownload
),
543 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),