oleaut32: Fix a leak of bmi in OLEPictureImpl_LoadGif.
[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
40 #include "config.h"
41 #include "wine/port.h"
42
43 #include <windows.h>
44 #include <shlwapi.h>
45 #include <ddeml.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
52
53 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
54
55 /* try to launch an app from a comma separated string of app names */
56 static int launch_app( char *candidates, const char *argv1 )
57 {
58     char *app;
59     const char *argv_new[3];
60
61     app = strtok( candidates, "," );
62     while (app)
63     {
64         argv_new[0] = app;
65         argv_new[1] = argv1;
66         argv_new[2] = NULL;
67
68         WINE_TRACE( "Considering: %s\n", app );
69         WINE_TRACE( "argv[1]: %s\n", argv1 );
70
71         spawnvp( _P_OVERLAY, app, argv_new );  /* only returns on error */
72         app = strtok( NULL, "," );  /* grab the next app */
73     }
74     fprintf( stderr, "winebrowser: could not find a suitable app to run\n" );
75     return 1;
76 }
77
78 static int open_http_url( const char *url )
79 {
80     static const char *defaultbrowsers =
81         "xdg-open,firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
82     char browsers[256];
83
84     DWORD length, type;
85     HKEY key;
86     LONG r;
87
88     length = sizeof(browsers);
89     /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
90     if  (!(r = RegOpenKey( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", &key )))
91     {
92         r = RegQueryValueExA( key, "Browsers", 0, &type, (LPBYTE)browsers, &length );
93         RegCloseKey( key );
94     }
95     if (r != ERROR_SUCCESS)
96         strcpy( browsers, defaultbrowsers );
97
98     return launch_app( browsers, url );
99 }
100
101 static int open_mailto_url( const char *url )
102 {
103     static const char *defaultmailers =
104         "xdg-email,mozilla-thunderbird,thunderbird,evolution";
105     char mailers[256];
106
107     DWORD length, type;
108     HKEY key;
109     LONG r;
110
111     length = sizeof(mailers);
112     /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
113     if (!(r = RegOpenKey( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", &key )))
114     {
115         r = RegQueryValueExA( key, "Mailers", 0, &type, (LPBYTE)mailers, &length );
116         RegCloseKey( key );
117     }
118     if (r != ERROR_SUCCESS)
119         strcpy( mailers, defaultmailers );
120
121     return launch_app( mailers, url );
122 }
123
124 /*****************************************************************************
125  * DDE helper functions.
126  */
127
128 static char *ddeString = NULL;
129 static HSZ hszTopic = 0, hszReturn = 0;
130 static DWORD ddeInst = 0;
131
132 /* Dde callback, save the execute or request string for processing */
133 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
134                                 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
135                                 ULONG_PTR dwData1, ULONG_PTR dwData2)
136 {
137     DWORD size = 0, ret = 0;
138
139     WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
140                uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
141
142     switch (uType)
143     {
144         case XTYP_CONNECT:
145             if (!DdeCmpStringHandles(hsz1, hszTopic))
146                 return (HDDEDATA)TRUE;
147             return (HDDEDATA)FALSE;
148
149         case XTYP_EXECUTE:
150             if (!(size = DdeGetData(hData, NULL, 0, 0)))
151                 WINE_ERR("DdeGetData returned zero size of execute string\n");
152             else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size)))
153                 WINE_ERR("Out of memory\n");
154             else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
155                 WINE_WARN("DdeGetData did not return %d bytes\n", size);
156             DdeFreeDataHandle(hData);
157             return (HDDEDATA)DDE_FACK;
158
159         case XTYP_REQUEST:
160             ret = -3; /* error */
161             if (!(size = DdeQueryString(ddeInst, hsz2, NULL, 0, CP_WINANSI)))
162                 WINE_ERR("DdeQueryString returned zero size of request string\n");
163             else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size+1)))
164                 WINE_ERR("Out of memory\n");
165             else if (DdeQueryString(ddeInst, hsz2, ddeString, size+1, CP_WINANSI) != size)
166                 WINE_WARN("DdeQueryString did not return %d bytes\n", size);
167             else
168                 ret = -2; /* acknowledgment */
169             return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
170                                        hszReturn, CF_TEXT, 0);
171
172         default:
173             return NULL;
174     }
175 }
176
177 static char *get_url_from_dde(void)
178 {
179     static const char szApplication[] = "IExplore";
180     static const char szTopic[] = "WWW_OpenURL";
181     static const char szReturn[] = "Return";
182
183     HSZ hszApplication = 0;
184     UINT_PTR timer = 0;
185     int rc;
186     char *ret = NULL;
187
188     rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
189                         CBF_FAIL_POKES, 0);
190     if (rc != DMLERR_NO_ERROR)
191     {
192         WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
193         goto done;
194     }
195
196     hszApplication = DdeCreateStringHandleA(ddeInst, szApplication, CP_WINANSI);
197     if (!hszApplication)
198     {
199         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
200         goto done;
201     }
202
203     hszTopic = DdeCreateStringHandleA(ddeInst, szTopic, CP_WINANSI);
204     if (!hszTopic)
205     {
206         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
207         goto done;
208     }
209
210     hszReturn = DdeCreateStringHandleA(ddeInst, szReturn, CP_WINANSI);
211     if (!hszReturn)
212     {
213         WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
214         goto done;
215     }
216
217     if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
218     {
219         WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
220         goto done;
221     }
222
223     timer = SetTimer(NULL, 0, 5000, NULL);
224     if (!timer)
225     {
226         WINE_ERR("SetTimer failed to create timer\n");
227         goto done;
228     }
229
230     while (!ddeString)
231     {
232         MSG msg;
233         if (!GetMessage(&msg, NULL, 0, 0)) break;
234         if (msg.message == WM_TIMER) break;
235         DispatchMessage(&msg);
236     }
237
238     if (ddeString)
239     {
240         if (*ddeString == '"')
241         {
242             char *endquote = strchr(ddeString+1, '"');
243             if (!endquote)
244             {
245                 WINE_ERR("Unabled to retrieve URL from string '%s'\n", ddeString);
246                 goto done;
247             }
248             *endquote = 0;
249             ret = ddeString+1;
250         }
251         else
252             ret = ddeString;
253     }
254
255 done:
256     if (timer) KillTimer(NULL, timer);
257     if (ddeInst)
258     {
259         if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
260         if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
261         if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
262         if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
263         DdeUninitialize(ddeInst);
264     }
265     return ret;
266 }
267
268 /*****************************************************************************
269  * Main entry point. This is a console application so we have a main() not a
270  * winmain().
271  */
272 int main(int argc, char *argv[])
273 {
274     char *url = argv[1];
275     wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
276     int ret = 1;
277
278     /* DDE used only if -nohome is specified; avoids delay in printing usage info
279      * when no parameters are passed */
280     if (url && !strcasecmp( url, "-nohome" ))
281         url = argc > 2 ? argv[2] : get_url_from_dde();
282
283     if (!url)
284     {
285         fprintf( stderr, "Usage: winebrowser URL\n" );
286         goto done;
287     }
288
289     /* handle an RFC1738 file URL */
290     if (!strncasecmp( url, "file:", 5 ))
291     {
292         char *p;
293         DWORD len = lstrlenA( url ) + 1;
294
295         if (UrlUnescapeA( url, NULL, &len, URL_UNESCAPE_INPLACE ) != S_OK)
296         {
297             fprintf( stderr, "winebrowser: unescaping URL failed: %s\n", url );
298             goto done;
299         }
300
301         /* look for a Windows path after 'file:' */
302         p = url + 5;
303         while (*p)
304         {
305             if (isalpha( p[0] ) && (p[1] == ':' || p[1] == '|')) break;
306             p++;
307         }
308         if (!*p)
309         {
310             fprintf( stderr, "winebrowser: no valid Windows path in: %s\n", url );
311             goto done;
312         }
313
314         if (p[1] == '|') p[1] = ':';
315         url = p;
316  
317         while (*p)
318         {
319             if (*p == '/') *p = '\\';
320             p++;
321         }
322     }
323
324     /* check if the argument is a local file */
325     wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
326         GetProcAddress( GetModuleHandle( "KERNEL32" ), "wine_get_unix_file_name" );
327
328     if (wine_get_unix_file_name_ptr == NULL)
329     {
330         WINE_ERR( "cannot get the address of 'wine_get_unix_file_name'\n" );
331     }
332     else
333     {
334         char *unixpath;
335         WCHAR unixpathW[MAX_PATH];
336
337         MultiByteToWideChar( CP_UNIXCP, 0, url, -1, unixpathW, MAX_PATH );
338         if ((unixpath = wine_get_unix_file_name_ptr( unixpathW )))
339         {
340             struct stat dummy;
341
342             if (stat( unixpath, &dummy ) >= 0)
343             {
344                 ret = open_http_url( unixpath );
345                 goto done;
346             }
347         }
348     }
349
350     if (!strncasecmp( url, "mailto:", 7 ))
351         ret = open_mailto_url( url );
352     else
353         /* let the browser decide how to handle the given url */
354         ret = open_http_url( url );
355
356 done:
357     HeapFree(GetProcessHeap(), 0, ddeString);
358     return ret;
359 }