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