msvcp90: Use macro to define RTTI data.
[wine] / programs / winebrowser / main.c
1 /*
2  * winebrowser - winelib app to launch native OS browser or mail client.
3  *
4  * Copyright (C) 2004 Chris Morgan
5  * Copyright (C) 2005 Hans Leidekker
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES:
22  *  Winebrowser is a winelib application that will start the appropriate
23  *  native browser or mail client for a wine installation that lacks a 
24  *  windows browser/mail client. For example, you will be able to open
25  *  URLs via native mozilla if no browser has yet been installed in wine.
26  *
27  *  The application to launch is chosen from a default set or, if set,
28  *  taken from a registry key.
29  *  
30  *  The argument may be a regular Windows file name, a file URL, an
31  *  URL or a mailto URL. In the first three cases the argument
32  *  will be fed to a web browser. In the last case the argument is fed
33  *  to a mail client. A mailto URL is composed as follows:
34  *
35  *   mailto:[E-MAIL]?subject=[TOPIC]&cc=[E-MAIL]&bcc=[E-MAIL]&body=[TEXT]
36  */
37
38 #define WIN32_LEAN_AND_MEAN
39 #define COBJMACROS
40
41 #include "config.h"
42 #include "wine/port.h"
43 #include "wine/debug.h"
44 #include "wine/unicode.h"
45
46 #include <windows.h>
47 #include <shlwapi.h>
48 #include <ddeml.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <errno.h>
52
53 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
54
55 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
56
57 static const WCHAR browser_key[] =
58     {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
59      'W','i','n','e','B','r','o','w','s','e','r',0};
60
61 static char *strdup_unixcp( const WCHAR *str )
62 {
63     char *ret;
64     int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
65     if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
66         WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
67     return ret;
68 }
69
70 /* try to launch a unix app from a comma separated string of app names */
71 static int launch_app( WCHAR *candidates, const WCHAR *argv1 )
72 {
73     char *app, *applist, *cmdline;
74     const char *argv_new[3];
75
76     if (!(applist = strdup_unixcp( candidates ))) return 1;
77     if (!(cmdline = strdup_unixcp( argv1 )))
78     {
79         HeapFree( GetProcessHeap(), 0, applist );
80         return 1;
81     }
82     app = strtok( applist, "," );
83     while (app)
84     {
85         WINE_TRACE( "Considering: %s\n", wine_dbgstr_a(app) );
86         WINE_TRACE( "argv[1]: %s\n", wine_dbgstr_a(cmdline) );
87
88         argv_new[0] = app;
89         argv_new[1] = cmdline;
90         argv_new[2] = NULL;
91
92         spawnvp( _P_OVERLAY, app, argv_new );  /* only returns on error */
93         app = strtok( NULL, "," );  /* grab the next app */
94     }
95     WINE_ERR( "could not find a suitable app to run\n" );
96
97     HeapFree( GetProcessHeap(), 0, applist );
98     HeapFree( GetProcessHeap(), 0, cmdline );
99     return 1;
100 }
101
102 static int open_http_url( const WCHAR *url )
103 {
104 #ifdef __APPLE__
105     static const WCHAR defaultbrowsers[] =
106         { '/', 'u', 's', 'r', '/', 'b', 'i', 'n', '/', 'o', 'p', 'e', 'n', 0 };
107 #else
108     static const WCHAR defaultbrowsers[] =
109         {'x','d','g','-','o','p','e','n',',','f','i','r','e','f','o','x',',',
110          'k','o','n','q','u','e','r','o','r',',','m','o','z','i','l','l','a',',',
111          'n','e','t','s','c','a','p','e',',','g','a','l','e','o','n',',',
112          'o','p','e','r','a',',','d','i','l','l','o',0};
113 #endif
114     static const WCHAR browsersW[] =
115         {'B','r','o','w','s','e','r','s',0};
116
117     WCHAR browsers[256];
118     DWORD length, type;
119     HKEY key;
120     LONG r;
121
122     length = sizeof(browsers);
123     /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
124     if  (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
125     {
126         r = RegQueryValueExW( key, browsersW, 0, &type, (LPBYTE)browsers, &length );
127         RegCloseKey( key );
128     }
129     if (r != ERROR_SUCCESS)
130         strcpyW( browsers, defaultbrowsers );
131
132     return launch_app( browsers, url );
133 }
134
135 static int open_mailto_url( const WCHAR *url )
136 {
137 #ifdef __APPLE__
138     static const WCHAR defaultmailers[] =
139         { '/', 'u', 's', 'r', '/', 'b', 'i', 'n', '/', 'o', 'p', 'e', 'n', 0 };
140 #else
141     static const WCHAR defaultmailers[] =
142         {'x','d','g','-','e','m','a','i','l',',',
143          'm','o','z','i','l','l','a','-','t','h','u','n','d','e','r','b','i','r','d',',',
144          't','h','u','n','d','e','r','b','i','r','d',',',
145          'e','v','o','l','u','t','i','o','n',0};
146 #endif
147     static const WCHAR mailersW[] =
148         {'M','a','i','l','e','r','s',0};
149
150     WCHAR mailers[256];
151     DWORD length, type;
152     HKEY key;
153     LONG r;
154
155     length = sizeof(mailers);
156     /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
157     if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
158     {
159         r = RegQueryValueExW( key, mailersW, 0, &type, (LPBYTE)mailers, &length );
160         RegCloseKey( key );
161     }
162     if (r != ERROR_SUCCESS)
163         strcpyW( mailers, defaultmailers );
164
165     return launch_app( mailers, url );
166 }
167
168 /*****************************************************************************
169  * DDE helper functions.
170  */
171
172 static WCHAR *ddeString = NULL;
173 static HSZ hszTopic = 0, hszReturn = 0;
174 static DWORD ddeInst = 0;
175
176 /* Dde callback, save the execute or request string for processing */
177 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
178                                 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
179                                 ULONG_PTR dwData1, ULONG_PTR dwData2)
180 {
181     DWORD size = 0, ret = 0;
182
183     WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
184                uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
185
186     switch (uType)
187     {
188         case XTYP_CONNECT:
189             if (!DdeCmpStringHandles(hsz1, hszTopic))
190                 return (HDDEDATA)TRUE;
191             return (HDDEDATA)FALSE;
192
193         case XTYP_EXECUTE:
194             if (!(size = DdeGetData(hData, NULL, 0, 0)))
195                 WINE_ERR("DdeGetData returned zero size of execute string\n");
196             else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size)))
197                 WINE_ERR("Out of memory\n");
198             else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
199                 WINE_WARN("DdeGetData did not return %d bytes\n", size);
200             DdeFreeDataHandle(hData);
201             return (HDDEDATA)DDE_FACK;
202
203         case XTYP_REQUEST:
204             ret = -3; /* error */
205             if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
206                 WINE_ERR("DdeQueryString returned zero size of request string\n");
207             else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))))
208                 WINE_ERR("Out of memory\n");
209             else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
210                 WINE_WARN("DdeQueryString did not return %d characters\n", size);
211             else
212                 ret = -2; /* acknowledgment */
213             return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
214                                        hszReturn, CF_TEXT, 0);
215
216         default:
217             return NULL;
218     }
219 }
220
221 static WCHAR *get_url_from_dde(void)
222 {
223     static const WCHAR szApplication[] = {'I','E','x','p','l','o','r','e',0};
224     static const WCHAR szTopic[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
225     static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
226
227     HSZ hszApplication = 0;
228     UINT_PTR timer = 0;
229     int rc;
230     WCHAR *ret = NULL;
231
232     rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
233     if (rc != DMLERR_NO_ERROR)
234     {
235         WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
236         goto done;
237     }
238
239     hszApplication = DdeCreateStringHandleW(ddeInst, szApplication, CP_WINUNICODE);
240     if (!hszApplication)
241     {
242         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
243         goto done;
244     }
245
246     hszTopic = DdeCreateStringHandleW(ddeInst, szTopic, CP_WINUNICODE);
247     if (!hszTopic)
248     {
249         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
250         goto done;
251     }
252
253     hszReturn = DdeCreateStringHandleW(ddeInst, szReturn, CP_WINUNICODE);
254     if (!hszReturn)
255     {
256         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
257         goto done;
258     }
259
260     if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
261     {
262         WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
263         goto done;
264     }
265
266     timer = SetTimer(NULL, 0, 5000, NULL);
267     if (!timer)
268     {
269         WINE_ERR("SetTimer failed to create timer\n");
270         goto done;
271     }
272
273     while (!ddeString)
274     {
275         MSG msg;
276         if (!GetMessageW(&msg, NULL, 0, 0)) break;
277         if (msg.message == WM_TIMER) break;
278         DispatchMessageW(&msg);
279     }
280
281     if (ddeString)
282     {
283         if (*ddeString == '"')
284         {
285             WCHAR *endquote = strchrW(ddeString + 1, '"');
286             if (!endquote)
287             {
288                 WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
289                 goto done;
290             }
291             *endquote = 0;
292             ret = ddeString+1;
293         }
294         else
295             ret = ddeString;
296     }
297
298 done:
299     if (timer) KillTimer(NULL, timer);
300     if (ddeInst)
301     {
302         if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
303         if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
304         if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
305         if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
306         DdeUninitialize(ddeInst);
307     }
308     return ret;
309 }
310
311 static IUri *convert_file_uri(IUri *uri)
312 {
313     wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
314     IUriBuilder *uri_builder;
315     struct stat dummy;
316     WCHAR *new_path;
317     char *unixpath;
318     BSTR filename;
319     IUri *new_uri;
320     HRESULT hres;
321
322     /* check if the argument is a local file */
323     wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
324            GetProcAddress( GetModuleHandleA( "KERNEL32" ), "wine_get_unix_file_name" );
325     if(!wine_get_unix_file_name_ptr)
326         return NULL;
327
328     hres = IUri_GetPath(uri, &filename);
329     if(FAILED(hres))
330         return NULL;
331
332     unixpath = wine_get_unix_file_name_ptr(filename);
333     SysFreeString(filename);
334     if(unixpath && stat(unixpath, &dummy) >= 0) {
335         int len;
336
337         len = MultiByteToWideChar(CP_UNIXCP, 0, unixpath, -1, NULL, 0);
338         new_path = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
339         if(new_path)
340             MultiByteToWideChar(CP_UNIXCP, 0, unixpath, -1, new_path, len);
341         HeapFree(GetProcessHeap(), 0, unixpath);
342     }else {
343         WINE_WARN("File %s does not exist\n", wine_dbgstr_a(unixpath));
344         HeapFree(GetProcessHeap(), 0, unixpath);
345         new_path = NULL;
346     }
347
348     hres = CreateIUriBuilder(uri, 0, 0, &uri_builder);
349     if(SUCCEEDED(hres) && new_path)
350         hres = IUriBuilder_SetPath(uri_builder, new_path);
351     HeapFree(GetProcessHeap(), 0, new_path);
352     if(FAILED(hres))
353         return NULL;
354
355     hres = IUriBuilder_CreateUri(uri_builder, 0, 0, 0, &new_uri);
356     IUriBuilder_Release(uri_builder);
357     if(FAILED(hres))
358         return NULL;
359
360     return new_uri;
361 }
362
363 /*****************************************************************************
364  * Main entry point. This is a console application so we have a wmain() not a
365  * winmain().
366  */
367 int wmain(int argc, WCHAR *argv[])
368 {
369     static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e',0};
370
371     WCHAR *url = argv[1];
372     BSTR display_uri;
373     DWORD scheme;
374     IUri *uri;
375     HRESULT hres;
376     int ret = 1;
377
378     /* DDE used only if -nohome is specified; avoids delay in printing usage info
379      * when no parameters are passed */
380     if (url && !strcmpiW( url, nohomeW ))
381         url = argc > 2 ? argv[2] : get_url_from_dde();
382
383     if (!url) {
384         WINE_ERR( "Usage: winebrowser URL\n" );
385         return -1;
386     }
387
388     hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
389     if(FAILED(hres)) {
390         WINE_ERR("Failed to parse URL\n");
391         ret = open_http_url(url);
392         HeapFree(GetProcessHeap(), 0, ddeString);
393         return ret;
394     }
395
396     HeapFree(GetProcessHeap(), 0, ddeString);
397     IUri_GetScheme(uri, &scheme);
398
399     if(scheme == URL_SCHEME_FILE) {
400         IUri *file_uri;
401
402         file_uri = convert_file_uri(uri);
403         if(file_uri) {
404             IUri_Release(uri);
405             uri = file_uri;
406         }else {
407             WINE_ERR("Failed to convert file URL to unix path\n");
408         }
409     }
410
411     hres = IUri_GetDisplayUri(uri, &display_uri);
412     IUri_Release(uri);
413     if(FAILED(hres))
414         return -1;
415
416     WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
417
418     if(scheme == URL_SCHEME_MAILTO)
419         ret = open_mailto_url(display_uri);
420     else
421         /* let the browser decide how to handle the given url */
422         ret = open_http_url(display_uri);
423
424     SysFreeString(display_uri);
425     return ret;
426 }