view: Show the filename in the window title.
[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 <urlmon.h>
49 #include <ddeml.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <errno.h>
53
54 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
55
56 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
57
58 static const WCHAR browser_key[] =
59     {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
60      'W','i','n','e','B','r','o','w','s','e','r',0};
61
62 static char *strdup_unixcp( const WCHAR *str )
63 {
64     char *ret;
65     int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
66     if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
67         WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
68     return ret;
69 }
70
71 /* try to launch a unix app from a comma separated string of app names */
72 static int launch_app( WCHAR *candidates, const WCHAR *argv1 )
73 {
74     char *app, *applist, *cmdline;
75     const char *argv_new[3];
76
77     if (!(applist = strdup_unixcp( candidates ))) return 1;
78     if (!(cmdline = strdup_unixcp( argv1 )))
79     {
80         HeapFree( GetProcessHeap(), 0, applist );
81         return 1;
82     }
83     app = strtok( applist, "," );
84     while (app)
85     {
86         WINE_TRACE( "Considering: %s\n", wine_dbgstr_a(app) );
87         WINE_TRACE( "argv[1]: %s\n", wine_dbgstr_a(cmdline) );
88
89         argv_new[0] = app;
90         argv_new[1] = cmdline;
91         argv_new[2] = NULL;
92
93         spawnvp( _P_OVERLAY, app, argv_new );  /* only returns on error */
94         app = strtok( NULL, "," );  /* grab the next app */
95     }
96     WINE_ERR( "could not find a suitable app to run\n" );
97
98     HeapFree( GetProcessHeap(), 0, applist );
99     HeapFree( GetProcessHeap(), 0, cmdline );
100     return 1;
101 }
102
103 static int open_http_url( const WCHAR *url )
104 {
105 #ifdef __APPLE__
106     static const WCHAR defaultbrowsers[] =
107         { '/', 'u', 's', 'r', '/', 'b', 'i', 'n', '/', 'o', 'p', 'e', 'n', 0 };
108 #else
109     static const WCHAR defaultbrowsers[] =
110         {'x','d','g','-','o','p','e','n',',','f','i','r','e','f','o','x',',',
111          'k','o','n','q','u','e','r','o','r',',','m','o','z','i','l','l','a',',',
112          'n','e','t','s','c','a','p','e',',','g','a','l','e','o','n',',',
113          'o','p','e','r','a',',','d','i','l','l','o',0};
114 #endif
115     static const WCHAR browsersW[] =
116         {'B','r','o','w','s','e','r','s',0};
117
118     WCHAR browsers[256];
119     DWORD length, type;
120     HKEY key;
121     LONG r;
122
123     length = sizeof(browsers);
124     /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
125     if  (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
126     {
127         r = RegQueryValueExW( key, browsersW, 0, &type, (LPBYTE)browsers, &length );
128         RegCloseKey( key );
129     }
130     if (r != ERROR_SUCCESS)
131         strcpyW( browsers, defaultbrowsers );
132
133     return launch_app( browsers, url );
134 }
135
136 static int open_mailto_url( const WCHAR *url )
137 {
138 #ifdef __APPLE__
139     static const WCHAR defaultmailers[] =
140         { '/', 'u', 's', 'r', '/', 'b', 'i', 'n', '/', 'o', 'p', 'e', 'n', 0 };
141 #else
142     static const WCHAR defaultmailers[] =
143         {'x','d','g','-','e','m','a','i','l',',',
144          'm','o','z','i','l','l','a','-','t','h','u','n','d','e','r','b','i','r','d',',',
145          't','h','u','n','d','e','r','b','i','r','d',',',
146          'e','v','o','l','u','t','i','o','n',0};
147 #endif
148     static const WCHAR mailersW[] =
149         {'M','a','i','l','e','r','s',0};
150
151     WCHAR mailers[256];
152     DWORD length, type;
153     HKEY key;
154     LONG r;
155
156     length = sizeof(mailers);
157     /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
158     if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
159     {
160         r = RegQueryValueExW( key, mailersW, 0, &type, (LPBYTE)mailers, &length );
161         RegCloseKey( key );
162     }
163     if (r != ERROR_SUCCESS)
164         strcpyW( mailers, defaultmailers );
165
166     return launch_app( mailers, url );
167 }
168
169 /*****************************************************************************
170  * DDE helper functions.
171  */
172
173 static WCHAR *ddeString = NULL;
174 static HSZ hszTopic = 0, hszReturn = 0;
175 static DWORD ddeInst = 0;
176
177 /* Dde callback, save the execute or request string for processing */
178 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
179                                 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
180                                 ULONG_PTR dwData1, ULONG_PTR dwData2)
181 {
182     DWORD size = 0, ret = 0;
183
184     WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
185                uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
186
187     switch (uType)
188     {
189         case XTYP_CONNECT:
190             if (!DdeCmpStringHandles(hsz1, hszTopic))
191                 return (HDDEDATA)TRUE;
192             return (HDDEDATA)FALSE;
193
194         case XTYP_EXECUTE:
195             if (!(size = DdeGetData(hData, NULL, 0, 0)))
196                 WINE_ERR("DdeGetData returned zero size of execute string\n");
197             else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size)))
198                 WINE_ERR("Out of memory\n");
199             else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
200                 WINE_WARN("DdeGetData did not return %d bytes\n", size);
201             DdeFreeDataHandle(hData);
202             return (HDDEDATA)DDE_FACK;
203
204         case XTYP_REQUEST:
205             ret = -3; /* error */
206             if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
207                 WINE_ERR("DdeQueryString returned zero size of request string\n");
208             else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))))
209                 WINE_ERR("Out of memory\n");
210             else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
211                 WINE_WARN("DdeQueryString did not return %d characters\n", size);
212             else
213                 ret = -2; /* acknowledgment */
214             return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
215                                        hszReturn, CF_TEXT, 0);
216
217         default:
218             return NULL;
219     }
220 }
221
222 static WCHAR *get_url_from_dde(void)
223 {
224     static const WCHAR szApplication[] = {'I','E','x','p','l','o','r','e',0};
225     static const WCHAR szTopic[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
226     static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
227
228     HSZ hszApplication = 0;
229     UINT_PTR timer = 0;
230     int rc;
231     WCHAR *ret = NULL;
232
233     rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
234     if (rc != DMLERR_NO_ERROR)
235     {
236         WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
237         goto done;
238     }
239
240     hszApplication = DdeCreateStringHandleW(ddeInst, szApplication, CP_WINUNICODE);
241     if (!hszApplication)
242     {
243         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
244         goto done;
245     }
246
247     hszTopic = DdeCreateStringHandleW(ddeInst, szTopic, CP_WINUNICODE);
248     if (!hszTopic)
249     {
250         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
251         goto done;
252     }
253
254     hszReturn = DdeCreateStringHandleW(ddeInst, szReturn, CP_WINUNICODE);
255     if (!hszReturn)
256     {
257         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
258         goto done;
259     }
260
261     if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
262     {
263         WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
264         goto done;
265     }
266
267     timer = SetTimer(NULL, 0, 5000, NULL);
268     if (!timer)
269     {
270         WINE_ERR("SetTimer failed to create timer\n");
271         goto done;
272     }
273
274     while (!ddeString)
275     {
276         MSG msg;
277         if (!GetMessageW(&msg, NULL, 0, 0)) break;
278         if (msg.message == WM_TIMER) break;
279         DispatchMessageW(&msg);
280     }
281
282     if (ddeString)
283     {
284         if (*ddeString == '"')
285         {
286             WCHAR *endquote = strchrW(ddeString + 1, '"');
287             if (!endquote)
288             {
289                 WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
290                 goto done;
291             }
292             *endquote = 0;
293             ret = ddeString+1;
294         }
295         else
296             ret = ddeString;
297     }
298
299 done:
300     if (timer) KillTimer(NULL, timer);
301     if (ddeInst)
302     {
303         if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
304         if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
305         if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
306         if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
307         DdeUninitialize(ddeInst);
308     }
309     return ret;
310 }
311
312 static IUri *convert_file_uri(IUri *uri)
313 {
314     wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
315     IUriBuilder *uri_builder;
316     struct stat dummy;
317     WCHAR *new_path;
318     char *unixpath;
319     BSTR filename;
320     IUri *new_uri;
321     HRESULT hres;
322
323     /* check if the argument is a local file */
324     wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
325            GetProcAddress( GetModuleHandleA( "KERNEL32" ), "wine_get_unix_file_name" );
326     if(!wine_get_unix_file_name_ptr)
327         return NULL;
328
329     hres = IUri_GetPath(uri, &filename);
330     if(FAILED(hres))
331         return NULL;
332
333     unixpath = wine_get_unix_file_name_ptr(filename);
334     SysFreeString(filename);
335     if(unixpath && stat(unixpath, &dummy) >= 0) {
336         int len;
337
338         len = MultiByteToWideChar(CP_UNIXCP, 0, unixpath, -1, NULL, 0);
339         new_path = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
340         if(new_path)
341             MultiByteToWideChar(CP_UNIXCP, 0, unixpath, -1, new_path, len);
342         HeapFree(GetProcessHeap(), 0, unixpath);
343     }else {
344         WINE_WARN("File %s does not exist\n", wine_dbgstr_a(unixpath));
345         HeapFree(GetProcessHeap(), 0, unixpath);
346         new_path = NULL;
347     }
348
349     hres = CreateIUriBuilder(uri, 0, 0, &uri_builder);
350     if(SUCCEEDED(hres) && new_path)
351         hres = IUriBuilder_SetPath(uri_builder, new_path);
352     HeapFree(GetProcessHeap(), 0, new_path);
353     if(FAILED(hres))
354         return NULL;
355
356     hres = IUriBuilder_CreateUri(uri_builder, 0, 0, 0, &new_uri);
357     IUriBuilder_Release(uri_builder);
358     if(FAILED(hres))
359         return NULL;
360
361     return new_uri;
362 }
363
364 /*****************************************************************************
365  * Main entry point. This is a console application so we have a wmain() not a
366  * winmain().
367  */
368 int wmain(int argc, WCHAR *argv[])
369 {
370     static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e',0};
371
372     WCHAR *url = argv[1];
373     BSTR display_uri;
374     DWORD scheme;
375     IUri *uri;
376     HRESULT hres;
377     int ret = 1;
378
379     /* DDE used only if -nohome is specified; avoids delay in printing usage info
380      * when no parameters are passed */
381     if (url && !strcmpiW( url, nohomeW ))
382         url = argc > 2 ? argv[2] : get_url_from_dde();
383
384     if (!url) {
385         WINE_ERR( "Usage: winebrowser URL\n" );
386         return -1;
387     }
388
389     hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
390     if(FAILED(hres)) {
391         WINE_ERR("Failed to parse URL\n");
392         ret = open_http_url(url);
393         HeapFree(GetProcessHeap(), 0, ddeString);
394         return ret;
395     }
396
397     HeapFree(GetProcessHeap(), 0, ddeString);
398     IUri_GetScheme(uri, &scheme);
399
400     if(scheme == URL_SCHEME_FILE) {
401         IUri *file_uri;
402
403         file_uri = convert_file_uri(uri);
404         if(file_uri) {
405             IUri_Release(uri);
406             uri = file_uri;
407         }else {
408             WINE_ERR("Failed to convert file URL to unix path\n");
409         }
410     }
411
412     hres = IUri_GetDisplayUri(uri, &display_uri);
413     IUri_Release(uri);
414     if(FAILED(hres))
415         return -1;
416
417     WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
418
419     if(scheme == URL_SCHEME_MAILTO)
420         ret = open_mailto_url(display_uri);
421     else
422         /* let the browser decide how to handle the given url */
423         ret = open_http_url(display_uri);
424
425     SysFreeString(display_uri);
426     return ret;
427 }