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 /* 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
28 * It was modified to suit the Ethereal 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 Ethereal as well as a GTK+-only
50 #if defined(G_OS_WIN32)
51 /* Win32 - use Windows shell services to start a browser */
53 /* if WIN32_LEAN_AND_MEAN is defined, shellapi.h is needed too */
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>
62 /* Everything else - launch the browser ourselves */
63 #define MUST_LAUNCH_BROWSER_OURSELVES
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].
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 */
76 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
77 static gchar* strreplace (const gchar *string,
78 const gchar *delimiter,
79 const gchar *replacement);
83 browser_needs_pref(void)
85 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
94 browser_open_url (const gchar *url)
96 #if defined(G_OS_WIN32)
98 return ((gint) ShellExecute (HWND_DESKTOP, "open", url, NULL, NULL, SW_SHOWNORMAL) > 32);
100 #elif defined(HAVE_OS_X_FRAMEWORKS)
102 CFStringRef url_CFString;
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.
111 url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
112 url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
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.
119 status = LSOpenCFURLRef(url_CFURL, NULL);
120 CFRelease(url_CFURL);
121 CFRelease(url_CFString);
122 return (status == 0);
124 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
126 GError *error = NULL;
133 g_return_val_if_fail (url != NULL, FALSE);
135 /* browser = gimp_gimprc_query ("web-browser");*/
136 browser = g_strdup(prefs.gui_webbrowser);
138 if (browser == NULL || ! strlen (browser))
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.");
147 /* quote the url since it might contains special chars */
148 argument = g_shell_quote (url);
150 /* replace %s with URL */
151 if (strstr (browser, "%s"))
152 cmd = strreplace (browser, "%s", argument);
154 cmd = g_strconcat (browser, " ", argument, NULL);
158 /* parse the cmd line */
159 if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
161 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
162 PRIMARY_TEXT_START "Could not parse web browser command: \"%s\"" PRIMARY_TEXT_END
164 browser, error->message,
165 "Please correct the web browser setting in the Preferences dialog.");
166 g_error_free (error);
170 retval = g_spawn_async (NULL, argv, NULL,
177 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
178 PRIMARY_TEXT_START "Could not execute web browser: \"%s\"" PRIMARY_TEXT_END
180 browser, error->message,
181 "Please correct the web browser setting in the Preferences dialog.");
182 g_error_free (error);
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"
205 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
208 strreplace (const gchar *string,
209 const gchar *delimiter,
210 const gchar *replacement)
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);
219 tmp = g_strsplit (string, delimiter, 0);
220 ret = g_strjoinv (replacement, tmp);
226 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
228 /** Convert local absolute path to uri.
230 * @param filename to (absolute pathed) filename to convert
231 * @return a newly allocated uri, you must g_free it later
234 filename2uri(gchar *filename)
241 filestr = g_string_sized_new(200);
243 /* this escaping is somewhat slow but should working fine */
244 for(i=0; filename[i]; i++) {
245 switch(filename[i]) {
247 g_string_append(filestr, "%20");
250 g_string_append(filestr, "%%");
253 g_string_append_c(filestr, '/');
255 /* XXX - which other chars need to be escaped? */
257 g_string_append_c(filestr, filename[i]);
262 /* prepend URI header "file:" appropriate for the system */
264 /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
265 g_string_prepend(filestr, "file:///");
267 g_string_prepend(filestr, "file://");
270 file_tmp = filestr->str;
272 g_string_free(filestr, FALSE /* don't free segment data */);
277 /* browse a file relative to the data dir */
279 browser_open_data_file(const gchar *filename)
285 file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
287 /* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
289 /* convert filename to uri */
290 uri = filename2uri(file_path);
293 browser_open_url (uri);