add functionality, to show html files from the local installation
[obnox/wireshark/wip.git] / gtk / webbrowser.c
1 /* The GIMP -- an image manipulation program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * Web Browser Plug-in
5  * Copyright (C) 2003  Henrik Brix Andersen <brix@gimp.org>
6  *
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.
11  *
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.
16  *
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.
20  */
21
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
25  *
26  * It was modified to suit the Ethereal environment (#if 0)!
27  */
28
29 #include "config.h"
30
31 #include <string.h> /* strlen, strstr */
32
33 #include <glib.h>
34
35 #if defined(G_OS_WIN32)
36 /* Win32 - use Windows shell services to start a browser */
37 #include <windows.h>
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>
44 #else
45 /* Everything else - launch the browser ourselves */
46 #define MUST_LAUNCH_BROWSER_OURSELVES
47 #endif
48
49 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
50 static gchar*   strreplace       (const gchar      *string,
51                                   const gchar      *delimiter,
52                                   const gchar      *replacement);
53 #endif
54
55
56 gboolean
57 browser_open_url (const gchar *url)
58 {
59 #if defined(G_OS_WIN32)
60
61   return ((gint) ShellExecute (HWND_DESKTOP, "open", url, NULL, NULL, SW_SHOWNORMAL) > 32);
62
63 #elif defined(HAVE_OS_X_FRAMEWORKS)
64
65   CFStringRef url_CFString;
66   CFURLRef url_CFURL;
67   OSStatus status;
68
69   /*
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.
73    */
74   url_CFString = CFStringCreateWithCString(NULL, url, kCFStringEncodingASCII);
75   url_CFURL = CFURLCreateWithString(NULL, url_CFString, NULL);
76   /*
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.
81    */
82   status = LSOpenCFURLRef(url_CFURL, NULL);
83   CFRelease(url_CFURL);
84   CFRelease(url_CFString);
85   return (status == 0);
86
87 #else
88
89   GError    *error = NULL;
90   gchar     *browser;
91   gchar     *argument;
92   gchar     *cmd;
93   gchar    **argv;
94   gboolean   retval;
95
96   g_return_val_if_fail (url != NULL, FALSE);
97
98 /*  browser = gimp_gimprc_query ("web-browser");*/
99   browser = g_strdup("mozilla %s");
100
101   if (browser == NULL || ! strlen (browser))
102     {
103       g_message (("Web browser not specified.\n"
104                    "Please specify a web browser using the Preferences Dialog."));
105       g_free (browser);
106       return FALSE;
107     }
108
109   /* quote the url since it might contains special chars */
110   argument = g_shell_quote (url);
111
112   /* replace %s with URL */
113   if (strstr (browser, "%s"))
114     cmd = strreplace (browser, "%s", argument);
115   else
116     cmd = g_strconcat (browser, " ", argument, NULL);
117
118   g_free (argument);
119
120   /* parse the cmd line */
121   if (! g_shell_parse_argv (cmd, NULL, &argv, &error))
122     {
123       g_message (("Could not parse specified web browser command:\n%s"),
124                  error->message);
125       g_error_free (error);
126       return FALSE;
127     }
128
129   retval = g_spawn_async (NULL, argv, NULL,
130                           G_SPAWN_SEARCH_PATH,
131                           NULL, NULL,
132                           NULL, &error);
133
134   if (! retval)
135     {
136       g_message (("Could not execute specified web browser:\n%s"),
137                  error->message);
138       g_error_free (error);
139     }
140
141   g_free (browser);
142   g_free (cmd);
143   g_strfreev (argv);
144
145   return retval;
146
147 #endif
148 }
149
150 #ifdef MUST_LAUNCH_BROWSER_OURSELVES
151
152 static gchar*
153 strreplace (const gchar *string,
154             const gchar *delimiter,
155             const gchar *replacement)
156 {
157   gchar  *ret;
158   gchar **tmp;
159
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);
163
164   tmp = g_strsplit (string, delimiter, 0);
165   ret = g_strjoinv (replacement, tmp);
166   g_strfreev (tmp);
167
168   return ret;
169 }
170
171 #endif /* MUST_LAUNCH_BROWSER_OURSELVES */
172
173 /** Convert local absolute path to uri.
174  *
175  * @param filename to (absolute pathed) filename to convert
176  * @return a newly allocated uri, you must g_free it later
177  */
178 gchar *
179 filename2uri(gchar *filename)
180 {
181     int i = 0;
182     gchar *file_tmp;
183     GString *filestr;
184
185
186     filestr = g_string_sized_new(200);
187
188     /* this escaping is somewhat slow but should working fine */
189     for(i=0; filename[i]; i++) {
190         switch(filename[i]) {
191         case(' '):
192             g_string_append(filestr, "%20");
193             break;
194         case('%'):
195             g_string_append(filestr, "%%");
196             break;
197         case('\\'):
198             g_string_append_c(filestr, '/');
199             break;
200             /* XXX - which other chars need to be escaped? */
201         default:
202             g_string_append_c(filestr, filename[i]);
203         }
204     }
205
206
207     /* prepend URI header "file:" appropriate for the system */
208 #ifdef G_OS_WIN32
209     /* XXX - how do we handle UNC names (e.g. //servername/sharename/dir1/dir2/capture-file.cap) */
210     g_string_prepend(filestr, "file:///");
211 #else
212     g_string_prepend(filestr, "file:");
213 #endif
214
215     file_tmp = filestr->str;
216
217     g_string_free(filestr, FALSE /* don't free segment data */);
218
219     return file_tmp;
220 }