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 #if defined(G_OS_WIN32)
36 /* Win32 - use Windows shell services to start a browser */
38 #elif defined (HAVE_OS_X_FRAMEWORKS)
39 /* Mac OS X - use Launch Services to start a browser */
40 #include <CoreFoundation/CFBase.h>
41 #include <CoreFoundation/CFString.h>
42 #include <CoreFoundation/CFURL.h>
43 #include <ApplicationServices/ApplicationServices.h>
45 /* Everything else - launch the browser ourselves */
46 #define MUST_LAUNCH_BROWSER_OURSELVES
49 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
50 static gchar* strreplace (const gchar *string,
51 const gchar *delimiter,
52 const gchar *replacement);
57 browser_open_url (const gchar *url)
59 #if defined(G_OS_WIN32)
61 return ((gint) ShellExecute (HWND_DESKTOP, "open", url, NULL, NULL, SW_SHOWNORMAL) > 32);
63 #elif defined(HAVE_OS_X_FRAMEWORKS)
65 CFStringRef url_CFString;
70 * XXX - if URLs passed to "browser_open_url()" contain non-ASCII
71 * characters, we'd have to choose an appropriate value from the
72 * CFStringEncodings enum.
74 url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
75 url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
77 * XXX - this is a Launch Services result code, and we should probably
78 * display a dialog box if it's not 0, describing what the error was.
79 * Then again, we should probably do the same for the ShellExecute call,
80 * unless that call itself happens to pop up a dialog box.
82 status = LSOpenCFURLRef(url_CFURL, NULL);
84 CFRelease(url_CFString);
96 g_return_val_if_fail (url != NULL, FALSE);
98 /* browser = gimp_gimprc_query ("web-browser");*/
99 browser = g_strdup("mozilla %s");
101 if (browser == NULL || ! strlen (browser))
103 g_message (("Web browser not specified.\n"
104 "Please specify a web browser using the Preferences Dialog."));
109 /* quote the url since it might contains special chars */
110 argument = g_shell_quote (url);
112 /* replace %s with URL */
113 if (strstr (browser, "%s"))
114 cmd = strreplace (browser, "%s", argument);
116 cmd = g_strconcat (browser, " ", argument, NULL);
120 /* parse the cmd line */
121 if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
123 g_message (("Could not parse specified web browser command:\n%s"),
125 g_error_free (error);
129 retval = g_spawn_async (NULL, argv, NULL,
136 g_message (("Could not execute specified web browser:\n%s"),
138 g_error_free (error);
150 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
153 strreplace (const gchar *string,
154 const gchar *delimiter,
155 const gchar *replacement)
160 g_return_val_if_fail (string != NULL, NULL);
161 g_return_val_if_fail (delimiter != NULL, NULL);
162 g_return_val_if_fail (replacement != NULL, NULL);
164 tmp = g_strsplit (string, delimiter, 0);
165 ret = g_strjoinv (replacement, tmp);
171 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
173 /** Convert local absolute path to uri.
175 * @param filename to (absolute pathed) filename to convert
176 * @return a newly allocated uri, you must g_free it later
179 filename2uri(gchar *filename)
186 filestr = g_string_sized_new(200);
188 /* this escaping is somewhat slow but should working fine */
189 for(i=0; filename[i]; i++) {
190 switch(filename[i]) {
192 g_string_append(filestr, "%20");
195 g_string_append(filestr, "%%");
198 g_string_append_c(filestr, '/');
200 /* XXX - which other chars need to be escaped? */
202 g_string_append_c(filestr, filename[i]);
207 /* prepend URI header "file:" appropriate for the system */
209 /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
210 g_string_prepend(filestr, "file:///");
212 g_string_prepend(filestr, "file:");
215 file_tmp = filestr->str;
217 g_string_free(filestr, FALSE /* don't free segment data */);