server: Don't round up the header size for image mappings.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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, 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:
34  *
35  *   mailto:[E-MAIL]?subject=[TOPIC]&cc=[E-MAIL]&bcc=[E-MAIL]&body=[TEXT]
36  */
37
38 #include "config.h"
39 #include "wine/port.h"
40
41 #include <windows.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <errno.h>
45
46 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
47
48 /* try to launch an app from a comma separated string of app names */
49 static int launch_app( char *candidates, const char *argv1 )
50 {
51     char *app;
52     const char *argv_new[3];
53
54     app = strtok( candidates, "," );
55     while (app)
56     {
57         argv_new[0] = app;
58         argv_new[1] = argv1;
59         argv_new[2] = NULL;
60
61         fprintf( stderr, "Considering: %s\n", app );
62         fprintf( stderr, "argv[1]: %s\n", argv1 );
63
64         spawnvp( _P_OVERLAY, app, argv_new );  /* only returns on error */
65         app = strtok( NULL, "," );  /* grab the next app */
66     }
67     fprintf( stderr, "winebrowser: could not find a suitable app to run\n" );
68     return 1;
69 }
70
71 static int open_http_url( const char *url )
72 {
73     static const char *defaultbrowsers =
74         "firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
75     char browsers[256];
76
77     DWORD length, type;
78     HKEY key;
79     LONG r;
80
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))
85     {
86         fprintf( stderr, "winebrowser: cannot create config key\n" );
87         return 1;
88     }
89
90     r = RegQueryValueExA( key, "Browsers", 0, &type, (LPBYTE)browsers, &length );
91     if (r != ERROR_SUCCESS)
92     {
93         /* set value to the default */
94         RegSetValueExA( key, "Browsers", 0, REG_SZ, (LPBYTE)defaultbrowsers,
95                         lstrlen( defaultbrowsers ) + 1 );
96         strcpy( browsers, defaultbrowsers );
97     }
98     RegCloseKey( key );
99
100     return launch_app( browsers, url );
101 }
102
103 static int open_mailto_url( const char *url )
104 {
105     static const char *defaultmailers =
106         "mozilla-thunderbird,thunderbird,evolution";
107     char mailers[256];
108
109     DWORD length, type;
110     HKEY key;
111     LONG r;
112
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 ))
117     {
118         fprintf( stderr, "winebrowser: cannot create config key\n" );
119         return 1;
120     }
121
122     r = RegQueryValueExA( key, "Mailers", 0, &type, (LPBYTE)mailers, &length );
123     if (r != ERROR_SUCCESS)
124     {
125         /* set value to the default */
126         RegSetValueExA( key, "Mailers", 0, REG_SZ, (LPBYTE)defaultmailers,
127                         lstrlen( defaultmailers ) + 1 );
128         strcpy( mailers, defaultmailers );
129     }
130     RegCloseKey( key );
131
132     return launch_app( mailers, url );
133 }
134
135 /*****************************************************************************
136  * Main entry point. This is a console application so we have a main() not a
137  * winmain().
138  */
139 int main(int argc, char *argv[])
140 {
141     wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
142
143     if (argc == 1)
144     {
145         fprintf( stderr, "Usage: winebrowser URL\n" );
146         return 1;
147     }
148
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" );
152
153     if (wine_get_unix_file_name_ptr == NULL)
154     {
155         fprintf( stderr,
156             "winebrowser: cannot get the address of 'wine_get_unix_file_name'\n" );
157     }
158     else
159     {
160         char *unixpath;
161         WCHAR unixpathW[MAX_PATH];
162
163         MultiByteToWideChar( CP_ACP, 0, argv[1], -1, unixpathW, MAX_PATH );
164         if ((unixpath = wine_get_unix_file_name_ptr( unixpathW )))
165         {
166             struct stat dummy;
167
168             if (stat( unixpath, &dummy ) >= 0)
169                 return open_http_url( unixpath );
170         }
171     }
172
173     if (!strncasecmp( argv[1], "http:", 5 ) || !strncasecmp( argv[1], "https:", 6 ))
174         return open_http_url( argv[1] );
175
176     if (!strncasecmp( argv[1], "mailto:", 7 ))
177         return open_mailto_url( argv[1] );
178
179     fprintf( stderr, "winebrowser: cannot handle this type of URL\n" );
180     return 1;
181 }