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