Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / programs / winebrowser / main.c
blobba34768e2bce1e0e6b23470280c5f25f2e373ff1
1 /*
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
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.
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
40 #include "config.h"
41 #include "wine/port.h"
43 #include <windows.h>
44 #include <shlwapi.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <errno.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 )
54 char *app;
55 const char *argv_new[3];
57 app = strtok( candidates, "," );
58 while (app)
60 argv_new[0] = app;
61 argv_new[1] = argv1;
62 argv_new[2] = NULL;
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" );
71 return 1;
74 static int open_http_url( const char *url )
76 static const char *defaultbrowsers =
77 "xdg-open,firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
78 char browsers[256];
80 DWORD length, type;
81 HKEY key;
82 LONG r;
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" );
90 return 1;
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, (const BYTE *)defaultbrowsers,
98 lstrlen( defaultbrowsers ) + 1 );
99 strcpy( browsers, defaultbrowsers );
101 RegCloseKey( key );
103 return launch_app( browsers, url );
106 static int open_mailto_url( const char *url )
108 #ifdef __APPLE__
109 static const char *defaultmailers =
110 "/usr/bin/open";
111 #else
112 static const char *defaultmailers =
113 "xdg-email,mozilla-thunderbird,thunderbird,evolution";
114 #endif
115 char mailers[256];
117 DWORD length, type;
118 HKEY key;
119 LONG r;
121 length = sizeof(mailers);
122 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
123 if (RegCreateKeyEx( HKEY_CURRENT_USER, "Software\\Wine\\WineBrowser", 0, NULL,
124 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, NULL ))
126 fprintf( stderr, "winebrowser: cannot create config key\n" );
127 return 1;
130 r = RegQueryValueExA( key, "Mailers", 0, &type, (LPBYTE)mailers, &length );
131 if (r != ERROR_SUCCESS)
133 /* set value to the default */
134 RegSetValueExA( key, "Mailers", 0, REG_SZ, (const BYTE *)defaultmailers,
135 lstrlen( defaultmailers ) + 1 );
136 strcpy( mailers, defaultmailers );
138 RegCloseKey( key );
140 return launch_app( mailers, url );
143 /*****************************************************************************
144 * Main entry point. This is a console application so we have a main() not a
145 * winmain().
147 int main(int argc, char *argv[])
149 char *url = argv[1];
150 wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
152 if (argc == 1)
154 fprintf( stderr, "Usage: winebrowser URL\n" );
155 return 1;
158 /* handle an RFC1738 file URL */
159 if (!strncasecmp( url, "file:", 5 ))
161 char *p;
162 DWORD len = lstrlenA( url ) + 1;
164 if (UrlUnescapeA( url, NULL, &len, URL_UNESCAPE_INPLACE ) != S_OK)
166 fprintf( stderr, "winebrowser: unescaping URL failed: %s\n", url );
167 return 1;
170 /* look for a Windows path after 'file:' */
171 p = url + 5;
172 while (*p)
174 if (isalpha( p[0] ) && (p[1] == ':' || p[1] == '|')) break;
175 p++;
177 if (!*p)
179 fprintf( stderr, "winebrowser: no valid Windows path in: %s\n", url );
180 return 1;
183 if (p[1] == '|') p[1] = ':';
184 url = p;
186 while (*p)
188 if (*p == '/') *p = '\\';
189 p++;
193 /* check if the argument is a local file */
194 wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
195 GetProcAddress( GetModuleHandle( "KERNEL32" ), "wine_get_unix_file_name" );
197 if (wine_get_unix_file_name_ptr == NULL)
199 fprintf( stderr,
200 "winebrowser: cannot get the address of 'wine_get_unix_file_name'\n" );
202 else
204 char *unixpath;
205 WCHAR unixpathW[MAX_PATH];
207 MultiByteToWideChar( CP_UNIXCP, 0, url, -1, unixpathW, MAX_PATH );
208 if ((unixpath = wine_get_unix_file_name_ptr( unixpathW )))
210 struct stat dummy;
212 if (stat( unixpath, &dummy ) >= 0)
213 return open_http_url( unixpath );
217 if (!strncasecmp( url, "mailto:", 7 ))
218 return open_mailto_url( url );
220 /* let the browser decide how to handle the given url */
221 return open_http_url( url );