gdi32: Test the bit depth of compatible bitmaps explicitly.
[wine] / dlls / wininet / dialogs.c
1 /*
2  * Wininet
3  *
4  * Copyright 2003 Mike McCormack for CodeWeavers Inc.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25
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"
36
37 #include "internet.h"
38
39 #include "wine/unicode.h"
40
41 #include "resource.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
44
45 struct WININET_ErrorDlgParams
46 {
47     HWND       hWnd;
48     HINTERNET  hRequest;
49     DWORD      dwError;
50     DWORD      dwFlags;
51     LPVOID*    lppvData;
52 };
53
54 /***********************************************************************
55  *         WININET_GetProxyServer
56  *
57  *  Determine the name of the proxy server the request is using
58  */
59 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
60 {
61     LPWININETHTTPREQW lpwhr;
62     LPWININETHTTPSESSIONW lpwhs = NULL;
63     LPWININETAPPINFOW hIC = NULL;
64     LPWSTR p;
65
66     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
67     if (NULL == lpwhr)
68         return FALSE;
69
70     lpwhs = lpwhr->lpHttpSession;
71     if (NULL == lpwhs)
72         return FALSE;
73
74     hIC = lpwhs->lpAppInfo;
75     if (NULL == hIC)
76         return FALSE;
77
78     lstrcpynW(szBuf, hIC->lpszProxy, sz);
79
80     /* FIXME: perhaps it would be better to use InternetCrackUrl here */
81     p = strchrW(szBuf, ':');
82     if (p)
83         *p = 0;
84
85     return TRUE;
86 }
87
88 /***********************************************************************
89  *         WININET_GetAuthRealm
90  *
91  *  Determine the name of the (basic) Authentication realm
92  */
93 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
94 {
95     LPWSTR p, q;
96     DWORD index;
97     static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
98
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;
104
105     /*
106      * FIXME: maybe we should check that we're
107      * dealing with 'Basic' Authentication
108      */
109     p = strchrW( szBuf, ' ' );
110     if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
111     {
112         ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
113         return FALSE;
114     }
115
116
117     /* remove quotes */
118     p += 7;
119     if( *p == '"' )
120     {
121         p++;
122         q = strrchrW( p, '"' );
123         if( q )
124             *q = 0;
125     }
126     strcpyW( szBuf, p );
127
128     return TRUE;
129 }
130
131 /***********************************************************************
132  *         WININET_GetSetPassword
133  */
134 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer, 
135                                     LPCWSTR szRealm, BOOL bSet )
136 {
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 };
145
146     hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
147     hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
148
149     /* now try fetch the username and password */
150     lstrcpyW( szResource, szServer);
151     lstrcatW( szResource, szbs);
152     lstrcatW( szResource, szRealm);
153
154     /*
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.
159      */
160     if( bSet )
161     {
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 );
169
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 );
174
175         return ( r == WN_SUCCESS );
176     }
177
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;
184
185     p = strchrW( szUserPass, ':' );
186     if( p )
187     {
188         *p = 0;
189         SetWindowTextW( hUserItem, szUserPass );
190         SetWindowTextW( hPassItem, p+1 );
191     }
192
193     return TRUE;
194 }
195
196 /***********************************************************************
197  *         WININET_SetProxyAuthorization
198  */
199 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
200                                          LPWSTR username, LPWSTR password )
201 {
202     LPWININETHTTPREQW lpwhr;
203     LPWININETHTTPSESSIONW lpwhs;
204     LPWININETAPPINFOW hIC;
205     LPWSTR p;
206
207     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
208     if( !lpwhr )
209         return FALSE;
210         
211     lpwhs = lpwhr->lpHttpSession;
212     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
213     {
214         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
215         return FALSE;
216     }
217
218     hIC = lpwhs->lpAppInfo;
219
220     p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
221     if( !p )
222         return FALSE;
223     
224     lstrcpyW( p, username );
225     hIC->lpszProxyUsername = p;
226
227     p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
228     if( !p )
229         return FALSE;
230     
231     lstrcpyW( p, password );
232     hIC->lpszProxyPassword = p;
233
234     return TRUE;
235 }
236
237 /***********************************************************************
238  *         WININET_ProxyPasswordDialog
239  */
240 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
241     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
242 {
243     HWND hitem;
244     struct WININET_ErrorDlgParams *params;
245     WCHAR szRealm[0x80], szServer[0x80];
246
247     if( uMsg == WM_INITDIALOG )
248     {
249         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
250
251         /* save the parameter list */
252         params = (struct WININET_ErrorDlgParams*) lParam;
253         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
254
255         /* extract the Realm from the proxy response and show it */
256         if( WININET_GetAuthRealm( params->hRequest,
257                                   szRealm, sizeof szRealm/sizeof(WCHAR)) )
258         {
259             hitem = GetDlgItem( hdlg, IDC_REALM );
260             SetWindowTextW( hitem, szRealm );
261         }
262
263         /* extract the name of the proxy server */
264         if( WININET_GetProxyServer( params->hRequest, 
265                                     szServer, sizeof szServer/sizeof(WCHAR)) )
266         {
267             hitem = GetDlgItem( hdlg, IDC_PROXY );
268             SetWindowTextW( hitem, szServer );
269         }
270
271         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
272
273         return TRUE;
274     }
275
276     params = (struct WININET_ErrorDlgParams*)
277                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
278
279     switch( uMsg )
280     {
281     case WM_COMMAND:
282         if( wParam == IDOK )
283         {
284             WCHAR username[0x20], password[0x20];
285
286             username[0] = 0;
287             hitem = GetDlgItem( hdlg, IDC_USERNAME );
288             if( hitem )
289                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
290             
291             password[0] = 0;
292             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
293             if( hitem )
294                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
295
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)) )
303             {
304                 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
305             }
306             WININET_SetProxyAuthorization( params->hRequest, username, password );
307
308             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
309             return TRUE;
310         }
311         if( wParam == IDCANCEL )
312         {
313             EndDialog( hdlg, 0 );
314             return TRUE;
315         }
316         break;
317     }
318     return FALSE;
319 }
320
321 /***********************************************************************
322  *         WININET_GetConnectionStatus
323  */
324 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
325 {
326     WCHAR szStatus[0x20];
327     DWORD sz, index, dwStatus;
328
329     TRACE("%p\n", hRequest );
330
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 );
337
338     TRACE("request %p status = %d\n", hRequest, dwStatus );
339
340     return dwStatus;
341 }
342
343
344 /***********************************************************************
345  *         InternetErrorDlg
346  */
347 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
348                  DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
349 {
350     struct WININET_ErrorDlgParams params;
351     HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
352     INT dwStatus;
353
354     TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
355
356     params.hWnd = hWnd;
357     params.hRequest = hRequest;
358     params.dwError = dwError;
359     params.dwFlags = dwFlags;
360     params.lppvData = lppvData;
361
362     switch( dwError )
363     {
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 );
372
373     case ERROR_INTERNET_INCORRECT_PASSWORD:
374         return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
375                     hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
376
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;
384     }
385     return ERROR_INVALID_PARAMETER;
386 }