wined3d: Use width while converting surfaces.
[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 <stdio.h>
46 #include <stdlib.h>
47 #include <errno.h>
48
49 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
50
51 /* try to launch an app from a comma separated string of app names */
52 static int launch_app( char *candidates, const char *argv1 )
53 {
54     char *app;
55     const char *argv_new[3];
56
57     app = strtok( candidates, "," );
58     while (app)
59     {
60         argv_new[0] = app;
61         argv_new[1] = argv1;
62         argv_new[2] = NULL;
63
64         fprintf( stderr, "Considering: %s\n", app );
65         fprintf( stderr, "argv[1]: %s\n", argv1 );
66
67         spawnvp( _P_OVERLAY, app, argv_new );  /* only returns on error */
68         app = strtok( NULL, "," );  /* grab the next app */
69     }
70     fprintf( stderr, "winebrowser: could not find a suitable app to run\n" );
71     return 1;
72 }
73
74 static int open_http_url( const char *url )
75 {
76     static const char *defaultbrowsers =
77         "xdg-open,firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
78     char browsers[256];
79
80     DWORD length, type;
81     HKEY key;
82     LONG r;
83
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))
88     {
89         fprintf( stderr, "winebrowser: cannot create config key\n" );
90         return 1;
91     }
92
93     r = RegQueryValueExA( key, "Browsers", 0, &type, (LPBYTE)browsers, &length );
94     if (r != ERROR_SUCCESS)
95     {
96         /* set value to the default */
97         RegSetValueExA( key, "Browsers", 0, REG_SZ, (const BYTE *)defaultbrowsers,
98                         lstrlen( defaultbrowsers ) + 1 );
99         strcpy( browsers, defaultbrowsers );
100     }
101     RegCloseKey( key );
102
103     return launch_app( browsers, url );
104 }
105
106 static int open_mailto_url( const char *url )
107 {
108     static const char *defaultmailers =
109         "xdg-email,mozilla-thunderbird,thunderbird,evolution";
110     char mailers[256];
111
112     DWORD length, type;
113     HKEY key;
114     LONG r;
115
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 ))
120     {
121         fprintf( stderr, "winebrowser: cannot create config key\n" );
122         return 1;
123     }
124
125     r = RegQueryValueExA( key, "Mailers", 0, &type, (LPBYTE)mailers, &length );
126     if (r != ERROR_SUCCESS)
127     {
128         /* set value to the default */
129         RegSetValueExA( key, "Mailers", 0, REG_SZ, (const BYTE *)defaultmailers,
130                         lstrlen( defaultmailers ) + 1 );
131         strcpy( mailers, defaultmailers );
132     }
133     RegCloseKey( key );
134
135     return launch_app( mailers, url );
136 }
137
138 /*****************************************************************************
139  * Main entry point. This is a console application so we have a main() not a
140  * winmain().
141  */
142 int main(int argc, char *argv[])
143 {
144     char *url = argv[1];
145     wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
146
147     if (argc == 1)
148     {
149         fprintf( stderr, "Usage: winebrowser URL\n" );
150         return 1;
151     }
152
153     /* handle an RFC1738 file URL */
154     if (!strncasecmp( url, "file:", 5 ))
155     {
156         char *p;
157         DWORD len = lstrlenA( url ) + 1;
158
159         if (UrlUnescapeA( url, NULL, &len, URL_UNESCAPE_INPLACE ) != S_OK)
160         {
161             fprintf( stderr, "winebrowser: unescaping URL failed: %s\n", url );
162             return 1;
163         }
164
165         /* look for a Windows path after 'file:' */
166         p = url + 5;
167         while (*p)
168         {
169             if (isalpha( p[0] ) && (p[1] == ':' || p[1] == '|')) break;
170             p++;
171         }
172         if (!*p)
173         {
174             fprintf( stderr, "winebrowser: no valid Windows path in: %s\n", url );
175             return 1;
176         }
177
178         if (p[1] == '|') p[1] = ':';
179         url = p;
180  
181         while (*p)
182         {
183             if (*p == '/') *p = '\\';
184             p++;
185         }
186     }
187
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" );
191
192     if (wine_get_unix_file_name_ptr == NULL)
193     {
194         fprintf( stderr,
195             "winebrowser: cannot get the address of 'wine_get_unix_file_name'\n" );
196     }
197     else
198     {
199         char *unixpath;
200         WCHAR unixpathW[MAX_PATH];
201
202         MultiByteToWideChar( CP_UNIXCP, 0, url, -1, unixpathW, MAX_PATH );
203         if ((unixpath = wine_get_unix_file_name_ptr( unixpathW )))
204         {
205             struct stat dummy;
206
207             if (stat( unixpath, &dummy ) >= 0)
208                 return open_http_url( unixpath );
209         }
210     }
211
212     if (!strncasecmp( url, "mailto:", 7 ))
213         return open_mailto_url( url );
214
215     /* let the browser decide how to handle the given url */
216     return open_http_url( url );
217 }