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]
39 #include "wine/port.h"
46 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
48 /* try to launch an app from a comma separated string of app names */
49 static int launch_app( char *candidates, const char *argv1 )
52 const char *argv_new[3];
54 app = strtok( candidates, "," );
61 fprintf( stderr, "Considering: %s\n", app );
62 fprintf( stderr, "argv[1]: %s\n", argv1 );
64 spawnvp( _P_OVERLAY, app, argv_new ); /* only returns on error */
65 app = strtok( NULL, "," ); /* grab the next app */
67 fprintf( stderr, "winebrowser: could not find a suitable app to run\n" );
71 static int open_http_url( const char *url )
73 static const char *defaultbrowsers =
74 "firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
81 length = sizeof(browsers);
82 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
83 if (RegCreateKeyEx( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", 0, NULL,
84 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, NULL))
86 fprintf( stderr, "winebrowser: cannot create config key\n" );
90 r = RegQueryValueExA( key, "Browsers", 0, &type, (LPBYTE)browsers, &length );
91 if (r != ERROR_SUCCESS)
93 /* set value to the default */
94 RegSetValueExA( key, "Browsers", 0, REG_SZ, (LPBYTE)defaultbrowsers,
95 lstrlen( defaultbrowsers ) + 1 );
96 strcpy( browsers, defaultbrowsers );
100 return launch_app( browsers, url );
103 static int open_mailto_url( const char *url )
105 static const char *defaultmailers =
106 "mozilla-thunderbird,thunderbird,evolution";
113 length = sizeof(mailers);
114 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
115 if (RegCreateKeyEx( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", 0, NULL,
116 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, NULL ))
118 fprintf( stderr, "winebrowser: cannot create config key\n" );
122 r = RegQueryValueExA( key, "Mailers", 0, &type, (LPBYTE)mailers, &length );
123 if (r != ERROR_SUCCESS)
125 /* set value to the default */
126 RegSetValueExA( key, "Mailers", 0, REG_SZ, (LPBYTE)defaultmailers,
127 lstrlen( defaultmailers ) + 1 );
128 strcpy( mailers, defaultmailers );
132 return launch_app( mailers, url );
135 /*****************************************************************************
136 * Main entry point. This is a console application so we have a main() not a
139 int main(int argc, char *argv[])
141 wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
145 fprintf( stderr, "Usage: winebrowser URL\n" );
149 /* check if the argument is a local file */
150 wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
151 GetProcAddress( GetModuleHandle( "KERNEL32" ), "wine_get_unix_file_name" );
153 if (wine_get_unix_file_name_ptr == NULL)
156 "winebrowser: cannot get the address of 'wine_get_unix_file_name'\n" );
161 WCHAR unixpathW[MAX_PATH];
163 MultiByteToWideChar( CP_ACP, 0, argv[1], -1, unixpathW, MAX_PATH );
164 if ((unixpath = wine_get_unix_file_name_ptr( unixpathW )))
168 if (stat( unixpath, &dummy ) >= 0)
169 return open_http_url( unixpath );
173 if (!strncasecmp( argv[1], "http:", 5 ) || !strncasecmp( argv[1], "https:", 6 ))
174 return open_http_url( argv[1] );
176 if (!strncasecmp( argv[1], "mailto:", 7 ))
177 return open_mailto_url( argv[1] );
179 fprintf( stderr, "winebrowser: cannot handle this type of URL\n" );