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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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, an http(s) URL or a
31 * mailto URL. In the first two cases the argument will be fed to a web
32 * browser. In the third case the argument is fed to a mail client.
33 * 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
41 #include "wine/port.h"
48 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
50 /* try to launch an app from a comma separated string of app names */
51 static int launch_app( char *candidates, const char *argv1 )
54 const char *argv_new[3];
56 app = strtok( candidates, "," );
63 fprintf( stderr, "Considering: %s\n", app );
64 fprintf( stderr, "argv[1]: %s\n", argv1 );
66 spawnvp( _P_OVERLAY, app, argv_new ); /* only returns on error */
67 app = strtok( NULL, "," ); /* grab the next app */
69 fprintf( stderr, "winebrowser: could not find a suitable app to run\n" );
73 static int open_http_url( const char *url )
75 static const char *defaultbrowsers =
76 "firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
83 length = sizeof(browsers);
84 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
85 if (RegCreateKeyEx( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", 0, NULL,
86 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, NULL))
88 fprintf( stderr, "winebrowser: cannot create config key\n" );
92 r = RegQueryValueExA( key, "Browsers", 0, &type, (LPBYTE)browsers, &length );
93 if (r != ERROR_SUCCESS)
95 /* set value to the default */
96 RegSetValueExA( key, "Browsers", 0, REG_SZ, (LPBYTE)defaultbrowsers,
97 lstrlen( defaultbrowsers ) + 1 );
98 strcpy( browsers, defaultbrowsers );
102 return launch_app( browsers, url );
105 static int open_mailto_url( const char *url )
107 static const char *defaultmailers =
108 "mozilla-thunderbird,thunderbird,evolution";
115 length = sizeof(mailers);
116 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
117 if (RegCreateKeyEx( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", 0, NULL,
118 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, NULL ))
120 fprintf( stderr, "winebrowser: cannot create config key\n" );
124 r = RegQueryValueExA( key, "Mailers", 0, &type, (LPBYTE)mailers, &length );
125 if (r != ERROR_SUCCESS)
127 /* set value to the default */
128 RegSetValueExA( key, "Mailers", 0, REG_SZ, (LPBYTE)defaultmailers,
129 lstrlen( defaultmailers ) + 1 );
130 strcpy( mailers, defaultmailers );
134 return launch_app( mailers, url );
137 /*****************************************************************************
138 * Main entry point. This is a console application so we have a main() not a
141 int main(int argc, char *argv[])
143 wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
147 fprintf( stderr, "Usage: winebrowser URL\n" );
151 /* check if the argument is a local file */
152 wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
153 GetProcAddress( GetModuleHandle( "KERNEL32" ), "wine_get_unix_file_name" );
155 if (wine_get_unix_file_name_ptr == NULL)
158 "winebrowser: cannot get the address of 'wine_get_unix_file_name'\n" );
163 WCHAR unixpathW[MAX_PATH];
165 MultiByteToWideChar( CP_ACP, 0, argv[1], -1, unixpathW, MAX_PATH );
166 if ((unixpath = wine_get_unix_file_name_ptr( unixpathW )))
170 if (stat( unixpath, &dummy ) >= 0)
171 return open_http_url( unixpath );
175 if (!strncasecmp( argv[1], "http:", 5 ) || !strncasecmp( argv[1], "https:", 6 ))
176 return open_http_url( argv[1] );
178 if (!strncasecmp( argv[1], "mailto:", 7 ))
179 return open_mailto_url( argv[1] );
181 fprintf( stderr, "winebrowser: cannot handle this type of URL\n" );