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>
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.
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.
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.
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
26 * It was modified to suit the Ethereal environment (#if 0)!
31 #include <string.h> /* strlen, strstr */
35 #include <epan/filesystem.h>
38 #include "webbrowser.h"
39 #include "compat_macros.h"
40 #include "simple_dialog.h"
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
48 #if defined(G_OS_WIN32)
49 /* Win32 - use Windows shell services to start a browser */
51 /* if WIN32_LEAN_AND_MEAN is defined, shellapi.h is needed too */
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>
60 /* Everything else - launch the browser ourselves */
61 #define MUST_LAUNCH_BROWSER_OURSELVES
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].
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 */
74 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
75 static gchar* strreplace (const gchar *string,
76 const gchar *delimiter,
77 const gchar *replacement);
81 browser_needs_pref(void)
83 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
92 browser_open_url (const gchar *url)
94 #if defined(G_OS_WIN32)
96 return ((gint) ShellExecute (HWND_DESKTOP, "open", url, NULL, NULL, SW_SHOWNORMAL) > 32);
98 #elif defined(HAVE_OS_X_FRAMEWORKS)
100 CFStringRef url_CFString;
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.
109 url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
110 url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
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.
117 status = LSOpenCFURLRef(url_CFURL, NULL);
118 CFRelease(url_CFURL);
119 CFRelease(url_CFString);
120 return (status == 0);
122 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
124 GError *error = NULL;
131 g_return_val_if_fail (url != NULL, FALSE);
133 /* browser = gimp_gimprc_query ("web-browser");*/
134 browser = g_strdup(prefs.gui_webbrowser);
136 if (browser == NULL || ! strlen (browser))
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.");
145 /* quote the url since it might contains special chars */
146 argument = g_shell_quote (url);
148 /* replace %s with URL */
149 if (strstr (browser, "%s"))
150 cmd = strreplace (browser, "%s", argument);
152 cmd = g_strconcat (browser, " ", argument, NULL);
156 /* parse the cmd line */
157 if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
159 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
160 PRIMARY_TEXT_START "Could not parse web browser command: \"%s\"" PRIMARY_TEXT_END
162 browser, error->message,
163 "Please correct the web browser setting in the Preferences dialog.");
164 g_error_free (error);
168 retval = g_spawn_async (NULL, argv, NULL,
175 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
176 PRIMARY_TEXT_START "Could not execute web browser: \"%s\"" PRIMARY_TEXT_END
178 browser, error->message,
179 "Please correct the web browser setting in the Preferences dialog.");
180 g_error_free (error);
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 */
197 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
200 strreplace (const gchar *string,
201 const gchar *delimiter,
202 const gchar *replacement)
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);
211 tmp = g_strsplit (string, delimiter, 0);
212 ret = g_strjoinv (replacement, tmp);
218 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
220 /** Convert local absolute path to uri.
222 * @param filename to (absolute pathed) filename to convert
223 * @return a newly allocated uri, you must g_free it later
226 filename2uri(gchar *filename)
233 filestr = g_string_sized_new(200);
235 /* this escaping is somewhat slow but should working fine */
236 for(i=0; filename[i]; i++) {
237 switch(filename[i]) {
239 g_string_append(filestr, "%20");
242 g_string_append(filestr, "%%");
245 g_string_append_c(filestr, '/');
247 /* XXX - which other chars need to be escaped? */
249 g_string_append_c(filestr, filename[i]);
254 /* prepend URI header "file:" appropriate for the system */
256 /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
257 g_string_prepend(filestr, "file:///");
259 g_string_prepend(filestr, "file://");
262 file_tmp = filestr->str;
264 g_string_free(filestr, FALSE /* don't free segment data */);
269 /* browse a file relative to the data dir */
271 browser_open_data_file(const gchar *filename)
277 file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
279 /* convert filename to uri */
280 uri = filename2uri(file_path);
283 browser_open_url (uri);