mfplat: Only convert MEDIASUBTYPE for the formats which need it.
[wine.git] / programs / winebrowser / main.c
blob24416070152ca3c6c9b24fecdedc89aa71356267
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
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #include <ntstatus.h>
45 #define WIN32_NO_STATUS
46 #include <windows.h>
47 #include <winternl.h>
48 #include <shlwapi.h>
49 #include <shellapi.h>
50 #include <urlmon.h>
51 #include <ddeml.h>
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
57 static char *strdup_unixcp( const WCHAR *str )
59 char *ret;
60 int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
61 if ((ret = malloc( len )))
62 WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
63 return ret;
66 /* try to launch a unix app from a comma separated string of app names */
67 static int launch_app( const WCHAR *candidates, const WCHAR *argv1 )
69 char *cmdline;
70 int i, count;
71 char **argv_new;
73 if (!(cmdline = strdup_unixcp( argv1 ))) return 1;
75 while (*candidates)
77 WCHAR **args = CommandLineToArgvW( candidates, &count );
79 if (!(argv_new = malloc( (count + 2) * sizeof(*argv_new) ))) break;
80 for (i = 0; i < count; i++) argv_new[i] = strdup_unixcp( args[i] );
81 argv_new[count] = cmdline;
82 argv_new[count + 1] = NULL;
84 TRACE( "Trying" );
85 for (i = 0; i <= count; i++) TRACE( " %s", wine_dbgstr_a( argv_new[i] ));
86 TRACE( "\n" );
88 if (!__wine_unix_spawnvp( argv_new, FALSE )) ExitProcess(0);
89 for (i = 0; i < count; i++) free( argv_new[i] );
90 free( argv_new );
91 candidates += lstrlenW( candidates ) + 1; /* grab the next app */
93 WINE_ERR( "could not find a suitable app to open %s\n", debugstr_w( argv1 ));
95 free( cmdline );
96 return 1;
99 static LSTATUS get_commands( HKEY key, const WCHAR *value, WCHAR *buffer, DWORD size )
101 DWORD type;
102 LSTATUS res;
104 size -= sizeof(WCHAR);
105 if (!(res = RegQueryValueExW( key, value, 0, &type, (LPBYTE)buffer, &size )) && (type == REG_SZ))
107 /* convert to REG_MULTI_SZ type */
108 WCHAR *p = buffer;
109 p[lstrlenW(p) + 1] = 0;
110 while ((p = wcschr( p, ',' ))) *p++ = 0;
112 return res;
115 static int open_http_url( const WCHAR *url )
117 static const WCHAR defaultbrowsers[] =
118 L"xdg-open\0"
119 "/usr/bin/open\0"
120 "firefox\0"
121 "konqueror\0"
122 "mozilla\0"
123 "opera\0"
124 "dillo\0";
125 WCHAR browsers[256];
126 HKEY key;
127 LONG r;
129 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
130 if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\WineBrowser", &key )))
132 r = get_commands( key, L"Browsers", browsers, sizeof(browsers) );
133 RegCloseKey( key );
135 if (r != ERROR_SUCCESS)
136 memcpy( browsers, defaultbrowsers, sizeof(defaultbrowsers) );
138 return launch_app( browsers, url );
141 static int open_mailto_url( const WCHAR *url )
143 static const WCHAR defaultmailers[] =
144 L"/usr/bin/open\0"
145 "xdg-email\0"
146 "mozilla-thunderbird\0"
147 "thunderbird\0"
148 "evolution\0";
149 WCHAR mailers[256];
150 HKEY key;
151 LONG r;
153 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
154 if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\WineBrowser", &key )))
156 r = get_commands( key, L"Mailers", mailers, sizeof(mailers) );
157 RegCloseKey( key );
159 if (r != ERROR_SUCCESS)
160 memcpy( mailers, defaultmailers, sizeof(defaultmailers) );
162 return launch_app( mailers, url );
165 static int open_invalid_url( const WCHAR *url )
167 WCHAR *url_prefixed;
168 int ret;
170 url_prefixed = malloc( (ARRAY_SIZE(L"http://") + lstrlenW( url )) * sizeof(WCHAR) );
171 if (!url_prefixed)
173 WINE_ERR("Out of memory\n");
174 return 1;
177 lstrcpyW( url_prefixed, L"http://" );
178 lstrcatW( url_prefixed, url );
180 ret = open_http_url( url_prefixed );
181 free( url_prefixed );
182 return ret;
185 /*****************************************************************************
186 * DDE helper functions.
189 static WCHAR *ddeString = NULL;
190 static HSZ hszTopic = 0, hszReturn = 0;
191 static DWORD ddeInst = 0;
193 /* Dde callback, save the execute or request string for processing */
194 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
195 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
196 ULONG_PTR dwData1, ULONG_PTR dwData2)
198 DWORD size = 0, ret = 0;
200 WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08Ix, %08Ix\n",
201 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
203 switch (uType)
205 case XTYP_CONNECT:
206 if (!DdeCmpStringHandles(hsz1, hszTopic))
207 return (HDDEDATA)TRUE;
208 return (HDDEDATA)FALSE;
210 case XTYP_EXECUTE:
211 if (!(size = DdeGetData(hData, NULL, 0, 0)))
212 WINE_ERR("DdeGetData returned zero size of execute string\n");
213 else if (!(ddeString = malloc(size)))
214 WINE_ERR("Out of memory\n");
215 else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
216 WINE_WARN("DdeGetData did not return %ld bytes\n", size);
217 DdeFreeDataHandle(hData);
218 return (HDDEDATA)DDE_FACK;
220 case XTYP_REQUEST:
221 ret = -3; /* error */
222 if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
223 WINE_ERR("DdeQueryString returned zero size of request string\n");
224 else if (!(ddeString = malloc( (size + 1) * sizeof(WCHAR))))
225 WINE_ERR("Out of memory\n");
226 else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
227 WINE_WARN("DdeQueryString did not return %ld characters\n", size);
228 else
229 ret = -2; /* acknowledgment */
230 return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
231 hszReturn, CF_TEXT, 0);
233 default:
234 return NULL;
238 static WCHAR *get_url_from_dde(void)
240 HSZ hszApplication = 0;
241 UINT_PTR timer = 0;
242 int rc;
243 WCHAR *ret = NULL;
245 rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
246 if (rc != DMLERR_NO_ERROR)
248 WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
249 goto done;
252 hszApplication = DdeCreateStringHandleW(ddeInst, L"IExplore", CP_WINUNICODE);
253 if (!hszApplication)
255 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
256 goto done;
259 hszTopic = DdeCreateStringHandleW(ddeInst, L"WWW_OpenURL", CP_WINUNICODE);
260 if (!hszTopic)
262 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
263 goto done;
266 hszReturn = DdeCreateStringHandleW(ddeInst, L"Return", CP_WINUNICODE);
267 if (!hszReturn)
269 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
270 goto done;
273 if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
275 WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
276 goto done;
279 timer = SetTimer(NULL, 0, 5000, NULL);
280 if (!timer)
282 WINE_ERR("SetTimer failed to create timer\n");
283 goto done;
286 while (!ddeString)
288 MSG msg;
289 if (!GetMessageW(&msg, NULL, 0, 0)) break;
290 if (msg.message == WM_TIMER) break;
291 DispatchMessageW(&msg);
294 if (ddeString)
296 if (*ddeString == '"')
298 WCHAR *endquote = wcschr(ddeString + 1, '"');
299 if (!endquote)
301 WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
302 goto done;
304 *endquote = 0;
305 ret = ddeString+1;
307 else
308 ret = ddeString;
311 done:
312 if (timer) KillTimer(NULL, timer);
313 if (ddeInst)
315 if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
316 if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
317 if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
318 if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
319 DdeUninitialize(ddeInst);
321 return ret;
324 static WCHAR *encode_unix_path(const char *src)
326 WCHAR *dst, *tmp_dst;
327 static const char safe_chars[] = "/-_.~@&=+$,:";
328 static const char hex_digits[] = "0123456789ABCDEF";
330 dst = malloc( sizeof(L"file://") + 3 * strlen(src) * sizeof(WCHAR) );
331 lstrcpyW(dst, L"file://");
333 tmp_dst = dst + lstrlenW(dst);
335 while (*src != 0)
337 if ((*src >= 'a' && *src <= 'z') ||
338 (*src >= 'A' && *src <= 'Z') ||
339 (*src >= '0' && *src <= '9') ||
340 strchr(safe_chars, *src))
342 *tmp_dst++ = *src;
344 else
346 *tmp_dst++ = '%';
347 *tmp_dst++ = hex_digits[*(unsigned char*)src / 16];
348 *tmp_dst++ = hex_digits[*src & 0xf];
350 src++;
353 *tmp_dst = 0;
355 return dst;
358 static WCHAR *convert_file_uri(IUri *uri)
360 UNICODE_STRING nt_name;
361 OBJECT_ATTRIBUTES attr;
362 NTSTATUS status;
363 ULONG size = 256;
364 char *buffer;
365 WCHAR *new_path = NULL;
366 BSTR filename;
367 HRESULT hres;
369 hres = IUri_GetPath(uri, &filename);
370 if(FAILED(hres))
371 return NULL;
373 WINE_TRACE("Windows path: %s\n", wine_dbgstr_w(filename));
375 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, NULL, NULL )) return NULL;
376 InitializeObjectAttributes( &attr, &nt_name, 0, 0, NULL );
377 for (;;)
379 if (!(buffer = malloc( size )))
381 RtlFreeUnicodeString( &nt_name );
382 return NULL;
384 status = wine_nt_to_unix_file_name( &attr, buffer, &size, FILE_OPEN );
385 if (status != STATUS_BUFFER_TOO_SMALL) break;
386 free( buffer );
389 if (!status) new_path = encode_unix_path( buffer );
390 SysFreeString(filename);
391 free( buffer );
393 WINE_TRACE("New path: %s\n", wine_dbgstr_w(new_path));
395 return new_path;
398 /*****************************************************************************
399 * Main entry point.
401 int wmain(int argc, WCHAR *argv[])
403 WCHAR *url = argv[1];
404 BSTR display_uri = NULL;
405 DWORD scheme;
406 IUri *uri;
407 HRESULT hres;
409 /* DDE used only if -nohome is specified; avoids delay in printing usage info
410 * when no parameters are passed */
411 if (url && !wcsicmp( url, L"-nohome" ))
412 url = argc > 2 ? argv[2] : get_url_from_dde();
414 if (!url) {
415 WINE_ERR( "Usage: winebrowser URL\n" );
416 return -1;
419 hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
420 if(FAILED(hres)) {
421 WINE_ERR("Failed to parse URL %s, treating as HTTP\n", wine_dbgstr_w(url));
422 return open_invalid_url(url);
425 free(ddeString);
426 IUri_GetScheme(uri, &scheme);
428 if(scheme == URL_SCHEME_FILE) {
429 display_uri = convert_file_uri(uri);
430 if(!display_uri) {
431 WINE_ERR("Failed to convert file URL to unix path\n");
435 if (!display_uri)
436 hres = IUri_GetDisplayUri(uri, &display_uri);
437 IUri_Release(uri);
438 if(FAILED(hres))
439 return -1;
441 WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
443 if(scheme == URL_SCHEME_MAILTO)
444 return open_mailto_url(display_uri);
445 else
446 /* let the browser decide how to handle the given url */
447 return open_http_url(display_uri);