remove MSVC compiler warning (required a type cast)
[obnox/wireshark/wip.git] / gtk / webbrowser.c
1 /* The GIMP -- an image manipulation program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * Web Browser Plug-in
5  * Copyright (C) 2003  Henrik Brix Andersen <brix@gimp.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 /* Ethereal - this file is copied from "The GIMP" V2.0.2
23  * You will find the original file in the gimp distribution zip under:
24  * \plug-ins\common\webbrowser.c
25  *
26  * It was modified to suit the Ethereal environment (#if 0)!
27  */
28
29 #include "config.h"
30
31 #include <string.h> /* strlen, strstr */
32
33 #include <gtk/gtk.h>
34
35 #include <epan/filesystem.h>
36
37 #include "prefs.h"
38 #include "webbrowser.h"
39 #include "compat_macros.h"
40 #include "simple_dialog.h"
41
42 /*
43  * For GNOME 2.x, we might be able to use "gnome_url_show()" (when we offer
44  * the ability to build a GNOMEified Ethereal as well as a GTK+-only
45  * Ethereal).
46  */
47
48 #if defined(G_OS_WIN32)
49 /* Win32 - use Windows shell services to start a browser */
50 #include <windows.h>
51 /* if WIN32_LEAN_AND_MEAN is defined, shellapi.h is needed too */
52 #include <shellapi.h>
53 #elif defined (HAVE_OS_X_FRAMEWORKS)
54 /* Mac OS X - use Launch Services to start a browser */
55 #include <CoreFoundation/CFBase.h>
56 #include <CoreFoundation/CFString.h>
57 #include <CoreFoundation/CFURL.h>
58 #include <ApplicationServices/ApplicationServices.h>
59 #else
60 /* Everything else - launch the browser ourselves */
61 #define MUST_LAUNCH_BROWSER_OURSELVES
62 #endif
63
64 /*
65  * XXX - we use GLib 2.x routines to launch the browser ourselves, so we
66  * can't do it if we're using GLib 1.2[.x].
67  */
68 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
69 #if (GLIB_MAJOR_VERSION < 2)
70 #undef MUST_LAUNCH_BROWSER_OURSELVES    /* *can't* launch browser ourselves */
71 #endif /* (GLIB_MAJOR_VERSION < 2) */
72 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
73
74 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
75 static gchar*   strreplace       (const gchar      *string,
76                                   const gchar      *delimiter,
77                                   const gchar      *replacement);
78 #endif
79
80 gboolean
81 browser_needs_pref(void)
82 {
83 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
84     return TRUE;
85 #else
86     return FALSE;
87 #endif
88 }
89
90
91 gboolean
92 browser_open_url (const gchar *url)
93 {
94 #if defined(G_OS_WIN32)
95
96   return ((gint) ShellExecute (HWND_DESKTOP, "open", url, NULL, NULL, SW_SHOWNORMAL) > 32);
97
98 #elif defined(HAVE_OS_X_FRAMEWORKS)
99
100   CFStringRef url_CFString;
101   CFURLRef url_CFURL;
102   OSStatus status;
103
104   /*
105    * XXX - if URLs passed to "browser_open_url()" contain non-ASCII
106    * characters, we'd have to choose an appropriate value from the
107    * CFStringEncodings enum.
108    */
109   url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
110   url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
111   /*
112    * XXX - this is a Launch Services result code, and we should probably
113    * display a dialog box if it's not 0, describing what the error was.
114    * Then again, we should probably do the same for the ShellExecute call,
115    * unless that call itself happens to pop up a dialog box for all errors.
116    */
117   status = LSOpenCFURLRef(url_CFURL, NULL);
118   CFRelease(url_CFURL);
119   CFRelease(url_CFString);
120   return (status == 0);
121
122 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
123
124   GError    *error = NULL;
125   gchar     *browser;
126   gchar     *argument;
127   gchar     *cmd;
128   gchar    **argv;
129   gboolean   retval;
130
131   g_return_val_if_fail (url != NULL, FALSE);
132
133   /*  browser = gimp_gimprc_query ("web-browser");*/
134   browser = g_strdup(prefs.gui_webbrowser);
135
136   if (browser == NULL || ! strlen (browser))
137     {
138       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, 
139           "Web browser not specified.\n"
140           "Please correct the web browser setting in the Preferences dialog.");
141       g_free (browser);
142       return FALSE;
143     }
144
145   /* quote the url since it might contains special chars */
146   argument = g_shell_quote (url);
147
148   /* replace %s with URL */
149   if (strstr (browser, "%s"))
150     cmd = strreplace (browser, "%s", argument);
151   else
152     cmd = g_strconcat (browser, " ", argument, NULL);
153
154   g_free (argument);
155
156   /* parse the cmd line */
157   if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
158     {
159       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, 
160           PRIMARY_TEXT_START "Could not parse web browser command: \"%s\"" PRIMARY_TEXT_END
161           "\n\n\"%s\"\n\n%s", 
162           browser, error->message,
163           "Please correct the web browser setting in the Preferences dialog.");
164       g_error_free (error);
165       return FALSE;
166     }
167
168   retval = g_spawn_async (NULL, argv, NULL,
169                           G_SPAWN_SEARCH_PATH,
170                           NULL, NULL,
171                           NULL, &error);
172
173   if (! retval)
174     {
175       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, 
176           PRIMARY_TEXT_START "Could not execute web browser: \"%s\"" PRIMARY_TEXT_END
177           "\n\n\"%s\"\n\n%s", 
178           browser, error->message,
179           "Please correct the web browser setting in the Preferences dialog.");
180       g_error_free (error);
181     }
182
183   g_free (browser);
184   g_free (cmd);
185   g_strfreev (argv);
186
187   return retval;
188
189 #else
190   /* GLIB version 1.x doesn't support the functions used above,
191      so simply do nothing for now, to be able to compile.
192      XXX - has to be improved */
193   return FALSE;
194 #endif
195 }
196
197 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
198
199 static gchar*
200 strreplace (const gchar *string,
201             const gchar *delimiter,
202             const gchar *replacement)
203 {
204   gchar  *ret;
205   gchar **tmp;
206
207   g_return_val_if_fail (string != NULL, NULL);
208   g_return_val_if_fail (delimiter != NULL, NULL);
209   g_return_val_if_fail (replacement != NULL, NULL);
210
211   tmp = g_strsplit (string, delimiter, 0);
212   ret = g_strjoinv (replacement, tmp);
213   g_strfreev (tmp);
214
215   return ret;
216 }
217
218 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
219
220 /** Convert local absolute path to uri.
221  *
222  * @param filename to (absolute pathed) filename to convert
223  * @return a newly allocated uri, you must g_free it later
224  */
225 static gchar *
226 filename2uri(gchar *filename)
227 {
228     int i = 0;
229     gchar *file_tmp;
230     GString *filestr;
231
232
233     filestr = g_string_sized_new(200);
234
235     /* this escaping is somewhat slow but should working fine */
236     for(i=0; filename[i]; i++) {
237         switch(filename[i]) {
238         case(' '):
239             g_string_append(filestr, "%20");
240             break;
241         case('%'):
242             g_string_append(filestr, "%%");
243             break;
244         case('\\'):
245             g_string_append_c(filestr, '/');
246             break;
247             /* XXX - which other chars need to be escaped? */
248         default:
249             g_string_append_c(filestr, filename[i]);
250         }
251     }
252
253
254     /* prepend URI header "file:" appropriate for the system */
255 #ifdef G_OS_WIN32
256     /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
257     g_string_prepend(filestr, "file:///");
258 #else
259     g_string_prepend(filestr, "file://");
260 #endif
261
262     file_tmp = filestr->str;
263
264     g_string_free(filestr, FALSE /* don't free segment data */);
265
266     return file_tmp;
267 }
268
269 /* browse a file relative to the data dir */
270 void
271 browser_open_data_file(const gchar *filename)
272 {
273     gchar *file_path;
274     gchar *uri;
275
276     /* build filename */
277     file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
278
279     /* convert filename to uri */
280     uri = filename2uri(file_path);
281
282     /* show the uri */
283     browser_open_url (uri);
284
285     g_free(file_path);
286     g_free(uri);
287 }