gdiplus: Implemented GdipIsEmptyClip with tests.
[wine.git] / dlls / wininet / dialogs.c
blobdd5aa315a63017ebc9403715dee882aee35acfd7
1 /*
2 * Wininet
4 * Copyright 2003 Mike McCormack for CodeWeavers Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "wininet.h"
31 #include "winnetwk.h"
32 #include "wine/debug.h"
33 #include "winerror.h"
34 #define NO_SHLWAPI_STREAM
35 #include "shlwapi.h"
37 #include "internet.h"
39 #include "wine/unicode.h"
41 #include "resource.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
45 struct WININET_ErrorDlgParams
47 HWND hWnd;
48 HINTERNET hRequest;
49 DWORD dwError;
50 DWORD dwFlags;
51 LPVOID* lppvData;
54 /***********************************************************************
55 * WININET_GetProxyServer
57 * Determine the name of the proxy server the request is using
59 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
61 LPWININETHTTPREQW lpwhr;
62 LPWININETHTTPSESSIONW lpwhs = NULL;
63 LPWININETAPPINFOW hIC = NULL;
64 LPWSTR p;
66 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
67 if (NULL == lpwhr)
68 return FALSE;
70 lpwhs = lpwhr->lpHttpSession;
71 if (NULL == lpwhs)
72 return FALSE;
74 hIC = lpwhs->lpAppInfo;
75 if (NULL == hIC)
76 return FALSE;
78 lstrcpynW(szBuf, hIC->lpszProxy, sz);
80 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
81 p = strchrW(szBuf, ':');
82 if (p)
83 *p = 0;
85 return TRUE;
88 /***********************************************************************
89 * WININET_GetAuthRealm
91 * Determine the name of the (basic) Authentication realm
93 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
95 LPWSTR p, q;
96 DWORD index;
97 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
99 /* extract the Realm from the proxy response and show it */
100 index = 0;
101 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
102 szBuf, &sz, &index) )
103 return FALSE;
106 * FIXME: maybe we should check that we're
107 * dealing with 'Basic' Authentication
109 p = strchrW( szBuf, ' ' );
110 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
112 ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
113 return FALSE;
117 /* remove quotes */
118 p += 7;
119 if( *p == '"' )
121 p++;
122 q = strrchrW( p, '"' );
123 if( q )
124 *q = 0;
126 strcpyW( szBuf, p );
128 return TRUE;
131 /***********************************************************************
132 * WININET_GetSetPassword
134 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
135 LPCWSTR szRealm, BOOL bSet )
137 WCHAR szResource[0x80], szUserPass[0x40];
138 LPWSTR p;
139 HWND hUserItem, hPassItem;
140 DWORD r, dwMagic = 19;
141 UINT r_len, u_len;
142 WORD sz;
143 static const WCHAR szColon[] = { ':',0 };
144 static const WCHAR szbs[] = { '/', 0 };
146 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
147 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
149 /* now try fetch the username and password */
150 lstrcpyW( szResource, szServer);
151 lstrcatW( szResource, szbs);
152 lstrcatW( szResource, szRealm);
155 * WNetCachePassword is only concerned with the length
156 * of the data stored (which we tell it) and it does
157 * not use strlen() internally so we can add WCHAR data
158 * instead of ASCII data and get it back the same way.
160 if( bSet )
162 szUserPass[0] = 0;
163 GetWindowTextW( hUserItem, szUserPass,
164 (sizeof szUserPass-1)/sizeof(WCHAR) );
165 lstrcatW(szUserPass, szColon);
166 u_len = strlenW( szUserPass );
167 GetWindowTextW( hPassItem, szUserPass+u_len,
168 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
170 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
171 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
172 r = WNetCachePassword( (CHAR*)szResource, r_len,
173 (CHAR*)szUserPass, u_len, dwMagic, 0 );
175 return ( r == WN_SUCCESS );
178 sz = sizeof szUserPass;
179 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
180 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
181 (CHAR*)szUserPass, &sz, dwMagic );
182 if( r != WN_SUCCESS )
183 return FALSE;
185 p = strchrW( szUserPass, ':' );
186 if( p )
188 *p = 0;
189 SetWindowTextW( hUserItem, szUserPass );
190 SetWindowTextW( hPassItem, p+1 );
193 return TRUE;
196 /***********************************************************************
197 * WININET_SetProxyAuthorization
199 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
200 LPWSTR username, LPWSTR password )
202 LPWININETHTTPREQW lpwhr;
203 LPWININETHTTPSESSIONW lpwhs;
204 LPWININETAPPINFOW hIC;
205 LPWSTR p;
207 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
208 if( !lpwhr )
209 return FALSE;
211 lpwhs = lpwhr->lpHttpSession;
212 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
214 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
215 return FALSE;
218 hIC = lpwhs->lpAppInfo;
220 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
221 if( !p )
222 return FALSE;
224 lstrcpyW( p, username );
225 hIC->lpszProxyUsername = p;
227 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
228 if( !p )
229 return FALSE;
231 lstrcpyW( p, password );
232 hIC->lpszProxyPassword = p;
234 return TRUE;
237 /***********************************************************************
238 * WININET_ProxyPasswordDialog
240 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
241 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
243 HWND hitem;
244 struct WININET_ErrorDlgParams *params;
245 WCHAR szRealm[0x80], szServer[0x80];
247 if( uMsg == WM_INITDIALOG )
249 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
251 /* save the parameter list */
252 params = (struct WININET_ErrorDlgParams*) lParam;
253 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
255 /* extract the Realm from the proxy response and show it */
256 if( WININET_GetAuthRealm( params->hRequest,
257 szRealm, sizeof szRealm/sizeof(WCHAR)) )
259 hitem = GetDlgItem( hdlg, IDC_REALM );
260 SetWindowTextW( hitem, szRealm );
263 /* extract the name of the proxy server */
264 if( WININET_GetProxyServer( params->hRequest,
265 szServer, sizeof szServer/sizeof(WCHAR)) )
267 hitem = GetDlgItem( hdlg, IDC_PROXY );
268 SetWindowTextW( hitem, szServer );
271 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
273 return TRUE;
276 params = (struct WININET_ErrorDlgParams*)
277 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
279 switch( uMsg )
281 case WM_COMMAND:
282 if( wParam == IDOK )
284 WCHAR username[0x20], password[0x20];
286 username[0] = 0;
287 hitem = GetDlgItem( hdlg, IDC_USERNAME );
288 if( hitem )
289 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
291 password[0] = 0;
292 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
293 if( hitem )
294 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
296 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
297 if( hitem &&
298 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
299 WININET_GetAuthRealm( params->hRequest,
300 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
301 WININET_GetProxyServer( params->hRequest,
302 szServer, sizeof szServer/sizeof(WCHAR)) )
304 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
306 WININET_SetProxyAuthorization( params->hRequest, username, password );
308 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
309 return TRUE;
311 if( wParam == IDCANCEL )
313 EndDialog( hdlg, 0 );
314 return TRUE;
316 break;
318 return FALSE;
321 /***********************************************************************
322 * WININET_GetConnectionStatus
324 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
326 WCHAR szStatus[0x20];
327 DWORD sz, index, dwStatus;
329 TRACE("%p\n", hRequest );
331 sz = sizeof szStatus;
332 index = 0;
333 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
334 szStatus, &sz, &index))
335 return -1;
336 dwStatus = atoiW( szStatus );
338 TRACE("request %p status = %d\n", hRequest, dwStatus );
340 return dwStatus;
344 /***********************************************************************
345 * InternetErrorDlg
347 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
348 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
350 struct WININET_ErrorDlgParams params;
351 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
352 INT dwStatus;
354 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
356 params.hWnd = hWnd;
357 params.hRequest = hRequest;
358 params.dwError = dwError;
359 params.dwFlags = dwFlags;
360 params.lppvData = lppvData;
362 switch( dwError )
364 case ERROR_SUCCESS:
365 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
366 return 0;
367 dwStatus = WININET_GetConnectionStatus( hRequest );
368 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
369 return ERROR_SUCCESS;
370 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
371 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
373 case ERROR_INTERNET_INCORRECT_PASSWORD:
374 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
375 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
377 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
378 case ERROR_INTERNET_INVALID_CA:
379 case ERROR_INTERNET_POST_IS_NON_SECURE:
380 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
381 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
382 FIXME("Need to display dialog for error %d\n", dwError);
383 return ERROR_SUCCESS;
385 return ERROR_INVALID_PARAMETER;