Don't destroy something if it doesn't exist.
[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 /* Wireshark - 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 Wireshark 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 Wireshark as well as a GTK+-only
47  * Wireshark).
48  */
49
50 #if defined(G_OS_WIN32)
51 /* Win32 - use Windows shell services to start a browser */
52 #include <windows.h>
53 /* We're using Unicode */
54 #include <tchar.h>
55 #include <epan/unicode-utils.h>
56 /* if WIN32_LEAN_AND_MEAN is defined, shellapi.h is needed too */
57 #include <shellapi.h>
58 #elif defined (HAVE_OS_X_FRAMEWORKS)
59 /* Mac OS X - use Launch Services to start a browser */
60 #include <CoreFoundation/CFBase.h>
61 #include <CoreFoundation/CFString.h>
62 #include <CoreFoundation/CFURL.h>
63 #include <ApplicationServices/ApplicationServices.h>
64 #else
65 /* Everything else - launch the browser ourselves */
66 #define MUST_LAUNCH_BROWSER_OURSELVES
67 #endif
68
69 /*
70  * XXX - we use GLib 2.x routines to launch the browser ourselves, so we
71  * can't do it if we're using GLib 1.2[.x].
72  */
73 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
74 #if (GLIB_MAJOR_VERSION < 2)
75 #undef MUST_LAUNCH_BROWSER_OURSELVES    /* *can't* launch browser ourselves */
76 #endif /* (GLIB_MAJOR_VERSION < 2) */
77 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
78
79 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
80 static gchar*   strreplace       (const gchar      *string,
81                                   const gchar      *delimiter,
82                                   const gchar      *replacement);
83 #endif
84
85 gboolean
86 browser_needs_pref(void)
87 {
88 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
89     return TRUE;
90 #else
91     return FALSE;
92 #endif
93 }
94
95
96 gboolean
97 browser_open_url (const gchar *url)
98 {
99 #if defined(G_OS_WIN32)
100
101   return ((gint) ShellExecute (HWND_DESKTOP, _T("open"), utf_8to16(url), NULL, NULL, SW_SHOWNORMAL) > 32);
102
103 #elif defined(HAVE_OS_X_FRAMEWORKS)
104
105   CFStringRef url_CFString;
106   CFURLRef url_CFURL;
107   OSStatus status;
108
109   /*
110    * XXX - if URLs passed to "browser_open_url()" contain non-ASCII
111    * characters, we'd have to choose an appropriate value from the
112    * CFStringEncodings enum.
113    */
114   url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
115   url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
116   /*
117    * XXX - this is a Launch Services result code, and we should probably
118    * display a dialog box if it's not 0, describing what the error was.
119    * Then again, we should probably do the same for the ShellExecute call,
120    * unless that call itself happens to pop up a dialog box for all errors.
121    */
122   status = LSOpenCFURLRef(url_CFURL, NULL);
123   CFRelease(url_CFURL);
124   CFRelease(url_CFString);
125   return (status == 0);
126
127 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
128
129   GError    *error = NULL;
130   gchar     *browser;
131   gchar     *argument;
132   gchar     *cmd;
133   gchar    **argv;
134   gboolean   retval;
135
136   g_return_val_if_fail (url != NULL, FALSE);
137
138   /*  browser = gimp_gimprc_query ("web-browser");*/
139   browser = g_strdup(prefs.gui_webbrowser);
140
141   if (browser == NULL || ! strlen (browser))
142     {
143       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
144           "Web browser not specified.\n"
145           "Please correct the web browser setting in the Preferences dialog.");
146       g_free (browser);
147       return FALSE;
148     }
149
150   /* quote the url since it might contains special chars */
151   argument = g_shell_quote (url);
152
153   /* replace %s with URL */
154   if (strstr (browser, "%s"))
155     cmd = strreplace (browser, "%s", argument);
156   else
157     cmd = g_strconcat (browser, " ", argument, NULL);
158
159   g_free (argument);
160
161   /* parse the cmd line */
162   if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
163     {
164       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
165           PRIMARY_TEXT_START "Could not parse web browser command: \"%s\"" PRIMARY_TEXT_END
166           "\n\n\"%s\"\n\n%s",
167           browser, error->message,
168           "Please correct the web browser setting in the Preferences dialog.");
169       g_error_free (error);
170       return FALSE;
171     }
172
173   retval = g_spawn_async (NULL, argv, NULL,
174                           G_SPAWN_SEARCH_PATH,
175                           NULL, NULL,
176                           NULL, &error);
177
178   if (! retval)
179     {
180       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
181           PRIMARY_TEXT_START "Could not execute web browser: \"%s\"" PRIMARY_TEXT_END
182           "\n\n\"%s\"\n\n%s",
183           browser, error->message,
184           "Please correct the web browser setting in the Preferences dialog.");
185       g_error_free (error);
186     }
187
188   g_free (browser);
189   g_free (cmd);
190   g_strfreev (argv);
191
192   return retval;
193
194 #else
195   /* GLIB version 1.x doesn't support the functions used above,
196      so simply do nothing for now, to be able to compile.
197      XXX - has to be improved */
198   simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
199       PRIMARY_TEXT_START "Web browser access not implemented." PRIMARY_TEXT_END
200       "\n\nThis Wireshark version (using the GLib 1.x toolkit) can't access web browsers. "
201       "\n\nYou may try to open the following URL in your web browser: \n\n"
202       "%s",
203       url);
204   return FALSE;
205 #endif
206 }
207
208 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
209
210 static gchar*
211 strreplace (const gchar *string,
212             const gchar *delimiter,
213             const gchar *replacement)
214 {
215   gchar  *ret;
216   gchar **tmp;
217
218   g_return_val_if_fail (string != NULL, NULL);
219   g_return_val_if_fail (delimiter != NULL, NULL);
220   g_return_val_if_fail (replacement != NULL, NULL);
221
222   tmp = g_strsplit (string, delimiter, 0);
223   ret = g_strjoinv (replacement, tmp);
224   g_strfreev (tmp);
225
226   return ret;
227 }
228
229 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
230
231 /** Convert local absolute path to uri.
232  *
233  * @param filename to (absolute pathed) filename to convert
234  * @return a newly allocated uri, you must g_free it later
235  */
236 static gchar *
237 filename2uri(gchar *filename)
238 {
239     int i = 0;
240     gchar *file_tmp;
241     GString *filestr;
242
243
244     filestr = g_string_sized_new(200);
245
246     /* this escaping is somewhat slow but should working fine */
247     for(i=0; filename[i]; i++) {
248         switch(filename[i]) {
249         case(' '):
250             g_string_append(filestr, "%20");
251             break;
252         case('%'):
253             g_string_append(filestr, "%%");
254             break;
255         case('\\'):
256             g_string_append_c(filestr, '/');
257             break;
258             /* XXX - which other chars need to be escaped? */
259         default:
260             g_string_append_c(filestr, filename[i]);
261         }
262     }
263
264
265     /* prepend URI header "file:" appropriate for the system */
266 #ifdef G_OS_WIN32
267     /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
268     g_string_prepend(filestr, "file:///");
269 #else
270     g_string_prepend(filestr, "file://");
271 #endif
272
273     file_tmp = filestr->str;
274
275     g_string_free(filestr, FALSE /* don't free segment data */);
276
277     return file_tmp;
278 }
279
280 /* browse a file relative to the data dir */
281 void
282 browser_open_data_file(const gchar *filename)
283 {
284     gchar *file_path;
285     gchar *uri;
286
287     /* build filename */
288     file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
289
290     /* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
291
292     /* convert filename to uri */
293     uri = filename2uri(file_path);
294
295     /* show the uri */
296     browser_open_url (uri);
297
298     g_free(file_path);
299     g_free(uri);
300 }