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)!
30 * For the UNIX+X11 launcher, see this blog post:
32 * http://blogs.gnome.org/timj/2006/11/24/24112006-how-to-start-a-web-browser/
34 * for a discussion of how Beast launches a browser, a link that shows
35 * the rather complicated code it uses, and some information on why it
36 * goes through all that pain. See also Kevin Krammer's comment, which
37 * notes that the problem might be that the GNOME, KDE, and XFCE
38 * launcher programs always cause the window to be opened in the background,
39 * regardless of whether an instance of the app is running or not (the
40 * app gets launched - in the background - if it's not already running,
41 * and is told to open a new window/tab if it's already running), while
42 * launchers such as sensible-browser, which xdg-open falls back to,
43 * launch the app in the foreground if it's not already running, leading
44 * to the "first window is in the foreground, subsequent windows are in
45 * the background" behavior in non-GNOME/KDE/XFCE environments.
47 * Perhaps the right strategy is to:
49 * Check whether we're in a GNOME/KDE/XFCE session and, if
50 * we are, try xdg-open, as it works around, among other things,
51 * some kfmclient bugs, and run it synchronously (that will fail
52 * if we detect a GNOME/KDE/XFCE session but the launcher is
53 * missing, but so it goes). If we don't have xdg-open, try
54 * the appropriate launcher for the environment, but ignore
55 * the return code from kfmclient, as it might be bogus (that's
56 * the bug xdg-open works around).
58 * Otherwise, try the "broken/unpredictable browser launchers",
59 * but run them in the background and leave them running, and
60 * ignore the exit code, and then try x-www-browser, and then
61 * try directly launching a user-specified browser. (Beast tries
62 * a bunch of browsers, with the user not being allowed to
63 * specify which one they want.)
65 * On the other hand, see bug 2699, in which xdg-open is itself buggy.
71 #include <string.h> /* strlen, strstr */
75 #include <epan/filesystem.h>
76 #include <epan/prefs.h>
78 #include "../simple_dialog.h"
80 #include "gtk/webbrowser.h"
82 #if defined(G_OS_WIN32)
83 /* Win32 - use Windows shell services to start a browser */
85 /* We're using Unicode */
87 #include <wsutil/unicode-utils.h>
88 /* if WIN32_LEAN_AND_MEAN is defined, shellapi.h is needed too */
90 #elif defined (HAVE_OS_X_FRAMEWORKS)
91 /* Mac OS X - use Launch Services to start a browser */
92 #include <CoreFoundation/CoreFoundation.h>
93 #include <ApplicationServices/ApplicationServices.h>
94 #elif defined(HAVE_XDG_OPEN)
95 /* UNIX+X11 desktop with Portland Group stuff - use xdg-open to start a browser */
97 /* Everything else - launch the browser ourselves */
98 #define MUST_LAUNCH_BROWSER_OURSELVES
101 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
102 static gchar* strreplace (const gchar *string,
103 const gchar *delimiter,
104 const gchar *replacement);
108 browser_needs_pref(void)
110 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
119 browser_open_url (const gchar *url)
121 #if defined(G_OS_WIN32)
123 return ((gint) ShellExecute (HWND_DESKTOP, _T("open"), utf_8to16(url), NULL, NULL, SW_SHOWNORMAL) > 32);
125 #elif defined(HAVE_OS_X_FRAMEWORKS)
127 CFStringRef url_CFString;
132 * XXX - if URLs passed to "browser_open_url()" contain non-ASCII
133 * characters, we'd have to choose an appropriate value from the
134 * CFStringEncodings enum.
136 url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
137 if (url_CFString == NULL)
139 url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
140 CFRelease(url_CFString);
141 if (url_CFURL == NULL) {
143 * XXX - this could mean that the url_CFString wasn't a valid URL,
144 * or that memory allocation failed. We can't determine which,
145 * except perhaps by providing our own allocator and somehow
146 * flagging allocation failures.
151 * XXX - this is a Launch Services result code, and we should probably
152 * display a dialog box if it's not 0, describing what the error was.
153 * Then again, we should probably do the same for the ShellExecute call,
154 * unless that call itself happens to pop up a dialog box for all errors.
156 status = LSOpenCFURLRef(url_CFURL, NULL);
157 CFRelease(url_CFURL);
158 return (status == 0);
160 #elif defined(HAVE_XDG_OPEN)
162 GError *error = NULL;
166 g_return_val_if_fail (url != NULL, FALSE);
168 argv[0] = "xdg-open";
169 argv[1] = (char *)url; /* Grr - g_spawn_async() shouldn't modify this */
173 * XXX - use g_spawn_on_screen() so the browser window shows up on
176 retval = g_spawn_async (NULL, argv, NULL,
183 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
184 "%sCould not execute xdg-open: %s\n\n\"%s\"",
185 simple_dialog_primary_start(), simple_dialog_primary_end(),
187 g_error_free (error);
192 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
194 GError *error = NULL;
201 g_return_val_if_fail (url != NULL, FALSE);
203 /* browser = gimp_gimprc_query ("web-browser");*/
204 browser = g_strdup(prefs.gui_webbrowser);
206 if (browser == NULL || ! strlen (browser))
208 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
209 "Web browser not specified.\n"
210 "Please correct the web browser setting in the Preferences dialog.");
215 /* quote the url since it might contains special chars */
216 argument = g_shell_quote (url);
218 /* replace %s with URL */
219 if (strstr (browser, "%s"))
220 cmd = strreplace (browser, "%s", argument);
222 cmd = g_strconcat (browser, " ", argument, NULL);
226 /* parse the cmd line */
227 if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
229 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
230 "%sCould not parse web browser command: \"%s\"%s\n\n\"%s\"\n\n%s",
231 simple_dialog_primary_start(), browser, simple_dialog_primary_end(),
233 "Please correct the web browser setting in the Preferences dialog.");
234 g_error_free (error);
239 * XXX - use g_spawn_on_screen() so the browser window shows up on
242 retval = g_spawn_async (NULL, argv, NULL,
249 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
250 "%sCould not execute web browser: \"%s\"%s\n\n\"%s\"\n\n%s",
251 simple_dialog_primary_start(), browser, simple_dialog_primary_end(),
253 "Please correct the web browser setting in the Preferences dialog.");
254 g_error_free (error);
265 /** Convert local absolute path to uri.
267 * @param filename to (absolute pathed) filename to convert
268 * @return a newly allocated uri, you must g_free it later
271 filename2uri(const gchar *filename)
278 filestr = g_string_sized_new(200);
280 /* this escaping is somewhat slow but should working fine */
281 for(i=0; filename[i]; i++) {
282 switch(filename[i]) {
284 g_string_append(filestr, "%20");
287 g_string_append(filestr, "%%");
290 g_string_append_c(filestr, '/');
292 /* XXX - which other chars need to be escaped? */
294 g_string_append_c(filestr, filename[i]);
299 /* prepend URI header "file:" appropriate for the system */
301 /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
302 g_string_prepend(filestr, "file:///");
304 g_string_prepend(filestr, "file://");
307 file_tmp = filestr->str;
309 g_string_free(filestr, FALSE /* don't free segment data */);
315 filemanager_open_directory (const gchar *path)
317 #if defined(G_OS_WIN32)
318 /* ShellExecute(...,"explore",...) needs path to be explicitly a directory;
319 Otherwise 'explore' will fail if a file exists with a basename matching
320 the provided directory path.
321 (eg: wireshak-gtk2.exe exists in the same directory as a wireshark-gtk2
326 xpath = g_strconcat(path,
327 g_str_has_suffix(path, "\\") ? "" : "\\",
329 ret = (gint) ShellExecute (HWND_DESKTOP, _T("explore"), utf_8to16(xpath), NULL, NULL, SW_SHOWNORMAL);
333 #elif defined(HAVE_OS_X_FRAMEWORKS)
335 CFStringRef path_CFString;
339 path_CFString = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
340 if (path_CFString == NULL)
342 path_CFURL = CFURLCreateWithFileSystemPath(NULL, path_CFString,
343 kCFURLPOSIXPathStyle, true);
344 CFRelease(path_CFString);
345 if (path_CFURL == NULL) {
347 * XXX - does this always mean that that memory allocation failed?
352 * XXX - this is a Launch Services result code, and we should probably
353 * display a dialog box if it's not 0, describing what the error was.
354 * Then again, we should probably do the same for the ShellExecute call,
355 * unless that call itself happens to pop up a dialog box for all errors.
357 status = LSOpenCFURLRef(path_CFURL, NULL);
358 CFRelease(path_CFURL);
359 return (status == 0);
361 #elif defined(HAVE_XDG_OPEN)
363 GError *error = NULL;
367 g_return_val_if_fail (path != NULL, FALSE);
369 argv[0] = "xdg-open";
370 argv[1] = (char *)path; /* Grr - g_spawn_async() shouldn't modify this */
374 * XXX - use g_spawn_on_screen() so the file managaer window shows up on
377 retval = g_spawn_async (NULL, argv, NULL,
384 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
385 "%sCould not execute xdg-open: %s\n\n\"%s\"",
386 simple_dialog_primary_start(), simple_dialog_primary_end(),
388 g_error_free (error);
393 #elif defined(MUST_LAUNCH_BROWSER_OURSELVES)
395 GError *error = NULL;
402 g_return_val_if_fail (path != NULL, FALSE);
404 /* browser = gimp_gimprc_query ("web-browser");*/
405 browser = g_strdup(prefs.gui_webbrowser);
407 if (browser == NULL || ! strlen (browser))
409 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
410 "Web browser not specified.\n"
411 "Please correct the web browser setting in the Preferences dialog.");
416 /* conver the path to a URI */
417 argument = filename2uri (path);
419 /* replace %s with URL */
420 if (strstr (browser, "%s"))
421 cmd = strreplace (browser, "%s", argument);
423 cmd = g_strconcat (browser, " ", argument, NULL);
427 /* parse the cmd line */
428 if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
430 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
431 "%sCould not parse web browser command: \"%s\"%s\n\n\"%s\"\n\n%s",
432 simple_dialog_primary_start(), browser, simple_dialog_primary_end(),
434 "Please correct the web browser setting in the Preferences dialog.");
435 g_error_free (error);
440 * XXX - use g_spawn_on_screen() so the browser window shows up on
443 retval = g_spawn_async (NULL, argv, NULL,
450 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
451 "%sCould not execute web browser: \"%s\"%s\n\n\"%s\"\n\n%s",
452 simple_dialog_primary_start(), browser, simple_dialog_primary_end(),
454 "Please correct the web browser setting in the Preferences dialog.");
455 g_error_free (error);
466 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
469 strreplace (const gchar *string,
470 const gchar *delimiter,
471 const gchar *replacement)
476 g_return_val_if_fail (string != NULL, NULL);
477 g_return_val_if_fail (delimiter != NULL, NULL);
478 g_return_val_if_fail (replacement != NULL, NULL);
480 tmp = g_strsplit (string, delimiter, 0);
481 ret = g_strjoinv (replacement, tmp);
487 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
490 /* browse a file relative to the data dir */
492 browser_open_data_file(const gchar *filename)
499 if((strlen(filename) > 2) && (filename[1] == ':'))
500 file_path = g_strdup(filename);
502 /* XXX: is this correct for MacOS/Linux ? */
503 if((strlen(filename) > 1) && (filename[0] == '/'))
504 file_path = g_strdup(filename);
508 file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
510 /* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
512 /* convert filename to uri */
513 uri = filename2uri(file_path);
516 browser_open_url (uri);