1 /* The GIMP -- an image manipulation program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
5 * Copyright (C) 2003 Henrik Brix Andersen <brix@gimp.org>
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.
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.
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.
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
28 * It was modified to suit the Wireshark environment (#if 0)!
33 #include <string.h> /* strlen, strstr */
37 #include <epan/filesystem.h>
39 #include <epan/prefs.h>
40 #include "webbrowser.h"
41 #include "compat_macros.h"
42 #include "simple_dialog.h"
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
50 #if defined(G_OS_WIN32)
51 /* Win32 - use Windows shell services to start a browser */
53 /* We're using Unicode */
55 #include <epan/unicode-utils.h>
56 /* if WIN32_LEAN_AND_MEAN is defined, shellapi.h is needed too */
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>
65 /* Everything else - launch the browser ourselves */
66 #define MUST_LAUNCH_BROWSER_OURSELVES
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].
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 */
79 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
80 static gchar* strreplace (const gchar *string,
81 const gchar *delimiter,
82 const gchar *replacement);
86 browser_needs_pref(void)
88 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
97 browser_open_url (const gchar *url)
99 #if defined(G_OS_WIN32)
101 return ((gint) ShellExecute (HWND_DESKTOP, _T("open"), utf_8to16(url), NULL, NULL, SW_SHOWNORMAL) > 32);
103 #elif defined(HAVE_OS_X_FRAMEWORKS)
105 CFStringRef url_CFString;
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.
114 url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
115 url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
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.
122 status = LSOpenCFURLRef(url_CFURL, NULL);
123 CFRelease(url_CFURL);
124 CFRelease(url_CFString);
125 return (status == 0);
127 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
129 GError *error = NULL;
136 g_return_val_if_fail (url != NULL, FALSE);
138 /* browser = gimp_gimprc_query ("web-browser");*/
139 browser = g_strdup(prefs.gui_webbrowser);
141 if (browser == NULL || ! strlen (browser))
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.");
150 /* quote the url since it might contains special chars */
151 argument = g_shell_quote (url);
153 /* replace %s with URL */
154 if (strstr (browser, "%s"))
155 cmd = strreplace (browser, "%s", argument);
157 cmd = g_strconcat (browser, " ", argument, NULL);
161 /* parse the cmd line */
162 if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
164 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
165 PRIMARY_TEXT_START "Could not parse web browser command: \"%s\"" PRIMARY_TEXT_END
167 browser, error->message,
168 "Please correct the web browser setting in the Preferences dialog.");
169 g_error_free (error);
173 retval = g_spawn_async (NULL, argv, NULL,
180 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
181 PRIMARY_TEXT_START "Could not execute web browser: \"%s\"" PRIMARY_TEXT_END
183 browser, error->message,
184 "Please correct the web browser setting in the Preferences dialog.");
185 g_error_free (error);
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"
208 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
211 strreplace (const gchar *string,
212 const gchar *delimiter,
213 const gchar *replacement)
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);
222 tmp = g_strsplit (string, delimiter, 0);
223 ret = g_strjoinv (replacement, tmp);
229 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
231 /** Convert local absolute path to uri.
233 * @param filename to (absolute pathed) filename to convert
234 * @return a newly allocated uri, you must g_free it later
237 filename2uri(gchar *filename)
244 filestr = g_string_sized_new(200);
246 /* this escaping is somewhat slow but should working fine */
247 for(i=0; filename[i]; i++) {
248 switch(filename[i]) {
250 g_string_append(filestr, "%20");
253 g_string_append(filestr, "%%");
256 g_string_append_c(filestr, '/');
258 /* XXX - which other chars need to be escaped? */
260 g_string_append_c(filestr, filename[i]);
265 /* prepend URI header "file:" appropriate for the system */
267 /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
268 g_string_prepend(filestr, "file:///");
270 g_string_prepend(filestr, "file://");
273 file_tmp = filestr->str;
275 g_string_free(filestr, FALSE /* don't free segment data */);
280 /* browse a file relative to the data dir */
282 browser_open_data_file(const gchar *filename)
288 file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
290 /* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
292 /* convert filename to uri */
293 uri = filename2uri(file_path);
296 browser_open_url (uri);