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
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
45 #define WIN32_NO_STATUS
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser
);
57 static char *strdup_unixcp( const WCHAR
*str
)
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
);
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
)
73 if (!(cmdline
= strdup_unixcp( argv1
))) return 1;
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
;
85 for (i
= 0; i
<= count
; i
++) TRACE( " %s", wine_dbgstr_a( argv_new
[i
] ));
88 if (!__wine_unix_spawnvp( argv_new
, FALSE
)) ExitProcess(0);
89 for (i
= 0; i
< count
; i
++) free( argv_new
[i
] );
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
));
99 static LSTATUS
get_commands( HKEY key
, const WCHAR
*value
, WCHAR
*buffer
, DWORD size
)
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 */
109 p
[lstrlenW(p
) + 1] = 0;
110 while ((p
= wcschr( p
, ',' ))) *p
++ = 0;
115 static int open_http_url( const WCHAR
*url
)
117 static const WCHAR defaultbrowsers
[] =
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
) );
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
[] =
146 "mozilla-thunderbird\0"
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
) );
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
)
170 url_prefixed
= malloc( (ARRAY_SIZE(L
"http://") + lstrlenW( url
)) * sizeof(WCHAR
) );
173 WINE_ERR("Out of memory\n");
177 lstrcpyW( url_prefixed
, L
"http://" );
178 lstrcatW( url_prefixed
, url
);
180 ret
= open_http_url( url_prefixed
);
181 free( url_prefixed
);
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
);
206 if (!DdeCmpStringHandles(hsz1
, hszTopic
))
207 return (HDDEDATA
)TRUE
;
208 return (HDDEDATA
)FALSE
;
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
;
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
);
229 ret
= -2; /* acknowledgment */
230 return DdeCreateDataHandle(ddeInst
, (LPBYTE
)&ret
, sizeof(ret
), 0,
231 hszReturn
, CF_TEXT
, 0);
238 static WCHAR
*get_url_from_dde(void)
240 HSZ hszApplication
= 0;
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
);
252 hszApplication
= DdeCreateStringHandleW(ddeInst
, L
"IExplore", CP_WINUNICODE
);
255 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
259 hszTopic
= DdeCreateStringHandleW(ddeInst
, L
"WWW_OpenURL", CP_WINUNICODE
);
262 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
266 hszReturn
= DdeCreateStringHandleW(ddeInst
, L
"Return", CP_WINUNICODE
);
269 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
273 if (!DdeNameService(ddeInst
, hszApplication
, 0, DNS_REGISTER
))
275 WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
279 timer
= SetTimer(NULL
, 0, 5000, NULL
);
282 WINE_ERR("SetTimer failed to create timer\n");
289 if (!GetMessageW(&msg
, NULL
, 0, 0)) break;
290 if (msg
.message
== WM_TIMER
) break;
291 DispatchMessageW(&msg
);
296 if (*ddeString
== '"')
298 WCHAR
*endquote
= wcschr(ddeString
+ 1, '"');
301 WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString
));
312 if (timer
) KillTimer(NULL
, timer
);
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
);
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
);
337 if ((*src
>= 'a' && *src
<= 'z') ||
338 (*src
>= 'A' && *src
<= 'Z') ||
339 (*src
>= '0' && *src
<= '9') ||
340 strchr(safe_chars
, *src
))
347 *tmp_dst
++ = hex_digits
[*(unsigned char*)src
/ 16];
348 *tmp_dst
++ = hex_digits
[*src
& 0xf];
358 static WCHAR
*convert_file_uri(IUri
*uri
)
360 UNICODE_STRING nt_name
;
361 OBJECT_ATTRIBUTES attr
;
365 WCHAR
*new_path
= NULL
;
369 hres
= IUri_GetPath(uri
, &filename
);
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
);
379 if (!(buffer
= malloc( size
)))
381 RtlFreeUnicodeString( &nt_name
);
384 status
= wine_nt_to_unix_file_name( &attr
, buffer
, &size
, FILE_OPEN
);
385 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
389 if (!status
) new_path
= encode_unix_path( buffer
);
390 SysFreeString(filename
);
393 WINE_TRACE("New path: %s\n", wine_dbgstr_w(new_path
));
398 /*****************************************************************************
401 int wmain(int argc
, WCHAR
*argv
[])
403 WCHAR
*url
= argv
[1];
404 BSTR display_uri
= NULL
;
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();
415 WINE_ERR( "Usage: winebrowser URL\n" );
419 hres
= CreateUri(url
, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_FILE_USE_DOS_PATH
, 0, &uri
);
421 WINE_ERR("Failed to parse URL %s, treating as HTTP\n", wine_dbgstr_w(url
));
422 return open_invalid_url(url
);
426 IUri_GetScheme(uri
, &scheme
);
428 if(scheme
== URL_SCHEME_FILE
) {
429 display_uri
= convert_file_uri(uri
);
431 WINE_ERR("Failed to convert file URL to unix path\n");
436 hres
= IUri_GetDisplayUri(uri
, &display_uri
);
441 WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri
));
443 if(scheme
== URL_SCHEME_MAILTO
)
444 return open_mailto_url(display_uri
);
446 /* let the browser decide how to handle the given url */
447 return open_http_url(display_uri
);