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
41 #include "wine/port.h"
49 typedef LPSTR (*wine_get_unix_file_name_t
)(LPCWSTR unixname
);
51 /* try to launch an app from a comma separated string of app names */
52 static int launch_app( char *candidates
, const char *argv1
)
55 const char *argv_new
[3];
57 app
= strtok( candidates
, "," );
64 fprintf( stderr
, "Considering: %s\n", app
);
65 fprintf( stderr
, "argv[1]: %s\n", argv1
);
67 spawnvp( _P_OVERLAY
, app
, argv_new
); /* only returns on error */
68 app
= strtok( NULL
, "," ); /* grab the next app */
70 fprintf( stderr
, "winebrowser: could not find a suitable app to run\n" );
74 static int open_http_url( const char *url
)
76 static const char *defaultbrowsers
=
77 "firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
84 length
= sizeof(browsers
);
85 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
86 if (RegCreateKeyEx( HKEY_CURRENT_USER
, "Software\\Wine\\WineBrowser", 0, NULL
,
87 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, NULL
))
89 fprintf( stderr
, "winebrowser: cannot create config key\n" );
93 r
= RegQueryValueExA( key
, "Browsers", 0, &type
, (LPBYTE
)browsers
, &length
);
94 if (r
!= ERROR_SUCCESS
)
96 /* set value to the default */
97 RegSetValueExA( key
, "Browsers", 0, REG_SZ
, (LPBYTE
)defaultbrowsers
,
98 lstrlen( defaultbrowsers
) + 1 );
99 strcpy( browsers
, defaultbrowsers
);
103 return launch_app( browsers
, url
);
106 static int open_mailto_url( const char *url
)
108 static const char *defaultmailers
=
109 "mozilla-thunderbird,thunderbird,evolution";
116 length
= sizeof(mailers
);
117 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
118 if (RegCreateKeyEx( HKEY_CURRENT_USER
, "Software\\Wine\\WineBrowser", 0, NULL
,
119 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, NULL
))
121 fprintf( stderr
, "winebrowser: cannot create config key\n" );
125 r
= RegQueryValueExA( key
, "Mailers", 0, &type
, (LPBYTE
)mailers
, &length
);
126 if (r
!= ERROR_SUCCESS
)
128 /* set value to the default */
129 RegSetValueExA( key
, "Mailers", 0, REG_SZ
, (LPBYTE
)defaultmailers
,
130 lstrlen( defaultmailers
) + 1 );
131 strcpy( mailers
, defaultmailers
);
135 return launch_app( mailers
, url
);
138 /*****************************************************************************
139 * Main entry point. This is a console application so we have a main() not a
142 int main(int argc
, char *argv
[])
145 wine_get_unix_file_name_t wine_get_unix_file_name_ptr
;
149 fprintf( stderr
, "Usage: winebrowser URL\n" );
153 /* handle an RFC1738 file URL */
154 if (!strncasecmp( url
, "file:", 5 ))
157 DWORD len
= lstrlenA( url
) + 1;
159 if (UrlUnescapeA( url
, NULL
, &len
, URL_UNESCAPE_INPLACE
) != S_OK
)
161 fprintf( stderr
, "winebrowser: unescaping URL failed: %s\n", url
);
165 /* look for a Windows path after 'file:' */
169 if (isalpha( p
[0] ) && (p
[1] == ':' || p
[1] == '|')) break;
174 fprintf( stderr
, "winebrowser: no valid Windows path in: %s\n", url
);
178 if (p
[1] == '|') p
[1] = ':';
183 if (*p
== '/') *p
= '\\';
188 /* check if the argument is a local file */
189 wine_get_unix_file_name_ptr
= (wine_get_unix_file_name_t
)
190 GetProcAddress( GetModuleHandle( "KERNEL32" ), "wine_get_unix_file_name" );
192 if (wine_get_unix_file_name_ptr
== NULL
)
195 "winebrowser: cannot get the address of 'wine_get_unix_file_name'\n" );
200 WCHAR unixpathW
[MAX_PATH
];
202 MultiByteToWideChar( CP_UNIXCP
, 0, url
, -1, unixpathW
, MAX_PATH
);
203 if ((unixpath
= wine_get_unix_file_name_ptr( unixpathW
)))
207 if (stat( unixpath
, &dummy
) >= 0)
208 return open_http_url( unixpath
);
212 if (!strncasecmp( url
, "mailto:", 7 ))
213 return open_mailto_url( url
);
215 /* let the browser decide how to handle the given url */
216 return open_http_url( url
);