]> git.street.me.uk Git - andy/viking.git/blob - src/util.c
Add backup support for launching URLs with Google Chrome based browsers.
[andy/viking.git] / src / util.c
1 /*
2  *    Viking - GPS data editor
3  *    Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
4  *    Based on:
5  *    Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
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, version 2.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with this program; if not, write to the Free Software
18  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef WINDOWS
25 #include <windows.h>
26 #endif
27
28 #include <glib/gi18n.h>
29 #include <glib/gprintf.h>
30
31 #include "util.h"
32 #include "dialog.h"
33
34 static gboolean spawn_command_line_async(const gchar * cmd,
35                                          const gchar * arg)
36 {
37   gchar *cmdline = NULL;
38   gboolean status;
39
40   cmdline = g_strdup_printf("%s '%s'", cmd, arg);
41   g_debug("Running: %s", cmdline);
42     
43   status = g_spawn_command_line_async(cmdline, NULL);
44
45   g_free(cmdline);
46  
47   return status;
48 }
49
50 void open_url(GtkWindow *parent, const gchar * url)
51 {
52 #ifdef WINDOWS
53   ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
54 #else /* WINDOWS */
55   const gchar *browsers[] = {
56     "xdg-open", "gnome-open", "kfmclient openURL",
57     "sensible-browser", "firefox", "epiphany",
58     "iceweasel", "seamonkey", "galeon", "mozilla",
59     "opera", "konqueror", "netscape", "links -g",
60     "chromium-browser", "chromium", "chrome",
61     NULL
62   };
63   gint i=0;
64   
65   const gchar *browser = g_getenv("BROWSER");
66   if (browser == NULL || browser[0] == '\0') {
67     /* $BROWSER not set -> use first entry */
68     browser = browsers[i++];
69   }
70   do {
71     if (spawn_command_line_async(browser, url)) {
72       return;
73     }
74
75     browser = browsers[i++];
76   } while(browser);
77   
78   a_dialog_error_msg ( parent, _("Could not launch web browser.") );
79 #endif /* WINDOWS */
80 }
81
82 void new_email(GtkWindow *parent, const gchar * address)
83 {
84   gchar *uri = g_strdup_printf("mailto:%s", address);
85 #ifdef WINDOWS
86   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
87 #else /* WINDOWS */
88   if (!spawn_command_line_async("xdg-email", uri))
89     a_dialog_error_msg ( parent, _("Could not create new email.") );
90 #endif /* WINDOWS */
91   g_free(uri);
92   uri = NULL;
93 }
94
95 gchar *uri_escape(gchar *str)
96 {
97   gchar *esc_str = g_malloc(3*strlen(str));
98   gchar *dst = esc_str;
99   gchar *src;
100
101   for (src = str; *src; src++) {
102     if (*src == ' ')
103      *dst++ = '+';
104     else if (g_ascii_isalnum(*src))
105      *dst++ = *src;
106     else {
107       g_sprintf(dst, "%%%02hhX", *src);
108       dst += 3;
109     }
110   }
111   *dst = '\0';
112
113   return(esc_str);
114 }
115