winebrowser: Support MULTI_SZ strings for browsers and mailers configuration.
[wine.git] / programs / winebrowser / main.c
blob0a443c1715de660e68a3a702f585ece8354bc3c9
1 /*
2 * winebrowser - winelib app to launch native OS browser or mail client.
4 * Copyright (C) 2004 Chris Morgan
5 * Copyright (C) 2005 Hans Leidekker
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.
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.
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
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.
27 * The application to launch is chosen from a default set or, if set,
28 * taken from a registry key.
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:
35 * mailto:[E-MAIL]?subject=[TOPIC]&cc=[E-MAIL]&bcc=[E-MAIL]&body=[TEXT]
38 #define WIN32_LEAN_AND_MEAN
39 #define COBJMACROS
41 #include "config.h"
42 #include "wine/port.h"
43 #include "wine/debug.h"
44 #include "wine/unicode.h"
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>
54 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
56 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
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};
62 static char *strdup_unixcp( const WCHAR *str )
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;
71 /* try to launch a unix app from a comma separated string of app names */
72 static int launch_app( const WCHAR *candidates, const WCHAR *argv1 )
74 char *app, *cmdline;
75 const char *argv_new[3];
77 if (!(cmdline = strdup_unixcp( argv1 ))) return 1;
79 while (*candidates)
81 if (!(app = strdup_unixcp( candidates ))) break;
83 WINE_TRACE( "Considering: %s\n", wine_dbgstr_a(app) );
84 WINE_TRACE( "argv[1]: %s\n", wine_dbgstr_a(cmdline) );
86 argv_new[0] = app;
87 argv_new[1] = cmdline;
88 argv_new[2] = NULL;
90 _spawnvp( _P_OVERLAY, app, argv_new ); /* only returns on error */
91 HeapFree( GetProcessHeap(), 0, app );
92 candidates += strlenW( candidates ) + 1; /* grab the next app */
94 WINE_ERR( "could not find a suitable app to run\n" );
96 HeapFree( GetProcessHeap(), 0, cmdline );
97 return 1;
100 static LSTATUS get_commands( HKEY key, const WCHAR *value, WCHAR *buffer, DWORD size )
102 DWORD type;
103 LSTATUS res;
105 size -= sizeof(WCHAR);
106 if (!(res = RegQueryValueExW( key, value, 0, &type, (LPBYTE)buffer, &size )) && (type == REG_SZ))
108 /* convert to REG_MULTI_SZ type */
109 WCHAR *p = buffer;
110 p[strlenW(p) + 1] = 0;
111 while ((p = strchrW( p, ',' ))) *p++ = 0;
113 return res;
116 static int open_http_url( const WCHAR *url )
118 #ifdef __APPLE__
119 static const WCHAR defaultbrowsers[] =
120 { '/','u','s','r','/','b','i','n','/','o','p','e','n',0,0 };
121 #else
122 static const WCHAR defaultbrowsers[] =
123 {'x','d','g','-','o','p','e','n',0,
124 'f','i','r','e','f','o','x',0,
125 'k','o','n','q','u','e','r','o','r',0,
126 'm','o','z','i','l','l','a',0,
127 'n','e','t','s','c','a','p','e',0,
128 'g','a','l','e','o','n',0,
129 'o','p','e','r','a',0,
130 'd','i','l','l','o',0,0};
131 #endif
132 static const WCHAR browsersW[] =
133 {'B','r','o','w','s','e','r','s',0};
135 WCHAR browsers[256];
136 HKEY key;
137 LONG r;
139 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
140 if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
142 r = get_commands( key, browsersW, browsers, sizeof(browsers) );
143 RegCloseKey( key );
145 if (r != ERROR_SUCCESS)
146 memcpy( browsers, defaultbrowsers, sizeof(defaultbrowsers) );
148 return launch_app( browsers, url );
151 static int open_mailto_url( const WCHAR *url )
153 #ifdef __APPLE__
154 static const WCHAR defaultmailers[] =
155 { '/','u','s','r','/','b','i','n','/','o','p','e','n',0,0 };
156 #else
157 static const WCHAR defaultmailers[] =
158 {'x','d','g','-','e','m','a','i','l',0,
159 'm','o','z','i','l','l','a','-','t','h','u','n','d','e','r','b','i','r','d',0,
160 't','h','u','n','d','e','r','b','i','r','d',0,
161 'e','v','o','l','u','t','i','o','n',0,0 };
162 #endif
163 static const WCHAR mailersW[] =
164 {'M','a','i','l','e','r','s',0};
166 WCHAR mailers[256];
167 HKEY key;
168 LONG r;
170 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
171 if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
173 r = get_commands( key, mailersW, mailers, sizeof(mailers) );
174 RegCloseKey( key );
176 if (r != ERROR_SUCCESS)
177 memcpy( mailers, defaultmailers, sizeof(defaultmailers) );
179 return launch_app( mailers, url );
182 /*****************************************************************************
183 * DDE helper functions.
186 static WCHAR *ddeString = NULL;
187 static HSZ hszTopic = 0, hszReturn = 0;
188 static DWORD ddeInst = 0;
190 /* Dde callback, save the execute or request string for processing */
191 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
192 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
193 ULONG_PTR dwData1, ULONG_PTR dwData2)
195 DWORD size = 0, ret = 0;
197 WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
198 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
200 switch (uType)
202 case XTYP_CONNECT:
203 if (!DdeCmpStringHandles(hsz1, hszTopic))
204 return (HDDEDATA)TRUE;
205 return (HDDEDATA)FALSE;
207 case XTYP_EXECUTE:
208 if (!(size = DdeGetData(hData, NULL, 0, 0)))
209 WINE_ERR("DdeGetData returned zero size of execute string\n");
210 else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size)))
211 WINE_ERR("Out of memory\n");
212 else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
213 WINE_WARN("DdeGetData did not return %d bytes\n", size);
214 DdeFreeDataHandle(hData);
215 return (HDDEDATA)DDE_FACK;
217 case XTYP_REQUEST:
218 ret = -3; /* error */
219 if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
220 WINE_ERR("DdeQueryString returned zero size of request string\n");
221 else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))))
222 WINE_ERR("Out of memory\n");
223 else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
224 WINE_WARN("DdeQueryString did not return %d characters\n", size);
225 else
226 ret = -2; /* acknowledgment */
227 return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
228 hszReturn, CF_TEXT, 0);
230 default:
231 return NULL;
235 static WCHAR *get_url_from_dde(void)
237 static const WCHAR szApplication[] = {'I','E','x','p','l','o','r','e',0};
238 static const WCHAR szTopic[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
239 static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
241 HSZ hszApplication = 0;
242 UINT_PTR timer = 0;
243 int rc;
244 WCHAR *ret = NULL;
246 rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
247 if (rc != DMLERR_NO_ERROR)
249 WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
250 goto done;
253 hszApplication = DdeCreateStringHandleW(ddeInst, szApplication, CP_WINUNICODE);
254 if (!hszApplication)
256 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
257 goto done;
260 hszTopic = DdeCreateStringHandleW(ddeInst, szTopic, CP_WINUNICODE);
261 if (!hszTopic)
263 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
264 goto done;
267 hszReturn = DdeCreateStringHandleW(ddeInst, szReturn, CP_WINUNICODE);
268 if (!hszReturn)
270 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
271 goto done;
274 if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
276 WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
277 goto done;
280 timer = SetTimer(NULL, 0, 5000, NULL);
281 if (!timer)
283 WINE_ERR("SetTimer failed to create timer\n");
284 goto done;
287 while (!ddeString)
289 MSG msg;
290 if (!GetMessageW(&msg, NULL, 0, 0)) break;
291 if (msg.message == WM_TIMER) break;
292 DispatchMessageW(&msg);
295 if (ddeString)
297 if (*ddeString == '"')
299 WCHAR *endquote = strchrW(ddeString + 1, '"');
300 if (!endquote)
302 WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
303 goto done;
305 *endquote = 0;
306 ret = ddeString+1;
308 else
309 ret = ddeString;
312 done:
313 if (timer) KillTimer(NULL, timer);
314 if (ddeInst)
316 if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
317 if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
318 if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
319 if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
320 DdeUninitialize(ddeInst);
322 return ret;
325 static WCHAR *encode_unix_path(const char *src)
327 const char *tmp_src;
328 WCHAR *dst, *tmp_dst;
329 const char safe_chars[] = "/-_.~@&=+$,:";
330 const char hex_digits[] = "0123456789ABCDEF";
331 const WCHAR schema[] = {'f','i','l','e',':','/','/',0};
332 int len = sizeof(schema)/sizeof(schema[0]);
334 tmp_src = src;
336 while (*tmp_src != 0)
338 if ((*tmp_src >= 'a' && *tmp_src <= 'z') ||
339 (*tmp_src >= 'A' && *tmp_src <= 'Z') ||
340 (*tmp_src >= '0' && *tmp_src <= '9') ||
341 strchr(safe_chars, *tmp_src))
342 len += 1;
343 else
344 len += 3;
345 tmp_src++;
348 dst = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
350 if (!dst)
351 return NULL;
353 strcpyW(dst, schema);
355 tmp_src = src;
356 tmp_dst = dst + strlenW(dst);
358 while (*tmp_src != 0)
360 if ((*tmp_src >= 'a' && *tmp_src <= 'z') ||
361 (*tmp_src >= 'A' && *tmp_src <= 'Z') ||
362 (*tmp_src >= '0' && *tmp_src <= '9') ||
363 strchr(safe_chars, *tmp_src))
365 *tmp_dst++ = *tmp_src;
367 else
369 *tmp_dst++ = '%';
370 *tmp_dst++ = hex_digits[*(unsigned char*)(tmp_src) / 16];
371 *tmp_dst++ = hex_digits[*tmp_src & 0xf];
373 tmp_src++;
376 *tmp_dst = 0;
378 return dst;
381 static WCHAR *convert_file_uri(IUri *uri)
383 wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
384 struct stat dummy;
385 WCHAR *new_path;
386 char *unixpath;
387 BSTR filename;
388 HRESULT hres;
390 /* check if the argument is a local file */
391 wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
392 GetProcAddress( GetModuleHandleA( "KERNEL32" ), "wine_get_unix_file_name" );
393 if(!wine_get_unix_file_name_ptr)
394 return NULL;
396 hres = IUri_GetPath(uri, &filename);
397 if(FAILED(hres))
398 return NULL;
400 WINE_TRACE("Windows path: %s\n", wine_dbgstr_w(filename));
402 unixpath = wine_get_unix_file_name_ptr(filename);
403 SysFreeString(filename);
404 if(unixpath && stat(unixpath, &dummy) >= 0) {
405 WINE_TRACE("Unix path: %s\n", wine_dbgstr_a(unixpath));
406 new_path = encode_unix_path(unixpath);
407 HeapFree(GetProcessHeap(), 0, unixpath);
408 }else {
409 WINE_WARN("File %s does not exist\n", wine_dbgstr_a(unixpath));
410 HeapFree(GetProcessHeap(), 0, unixpath);
411 new_path = NULL;
414 WINE_TRACE("New path: %s\n", wine_dbgstr_w(new_path));
416 return new_path;
419 /*****************************************************************************
420 * Main entry point. This is a console application so we have a wmain() not a
421 * winmain().
423 int wmain(int argc, WCHAR *argv[])
425 static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e',0};
427 WCHAR *url = argv[1];
428 BSTR display_uri = NULL;
429 DWORD scheme;
430 IUri *uri;
431 HRESULT hres;
432 int ret = 1;
434 /* DDE used only if -nohome is specified; avoids delay in printing usage info
435 * when no parameters are passed */
436 if (url && !strcmpiW( url, nohomeW ))
437 url = argc > 2 ? argv[2] : get_url_from_dde();
439 if (!url) {
440 WINE_ERR( "Usage: winebrowser URL\n" );
441 return -1;
444 hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
445 if(FAILED(hres)) {
446 WINE_ERR("Failed to parse URL\n");
447 ret = open_http_url(url);
448 HeapFree(GetProcessHeap(), 0, ddeString);
449 return ret;
452 HeapFree(GetProcessHeap(), 0, ddeString);
453 IUri_GetScheme(uri, &scheme);
455 if(scheme == URL_SCHEME_FILE) {
456 display_uri = convert_file_uri(uri);
457 if(!display_uri) {
458 WINE_ERR("Failed to convert file URL to unix path\n");
462 if (!display_uri)
463 hres = IUri_GetDisplayUri(uri, &display_uri);
464 IUri_Release(uri);
465 if(FAILED(hres))
466 return -1;
468 WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
470 if(scheme == URL_SCHEME_MAILTO)
471 ret = open_mailto_url(display_uri);
472 else
473 /* let the browser decide how to handle the given url */
474 ret = open_http_url(display_uri);
476 SysFreeString(display_uri);
477 return ret;