]> git.street.me.uk Git - andy/viking.git/blob - src/util.c
Allow to select between Google and Geonames for searching method
[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
30 #include "dialog.h"
31
32 static gboolean spawn_command_line_async(const gchar * cmd,
33                                          const gchar * arg)
34 {
35   gchar *cmdline = NULL;
36   gboolean status;
37
38   cmdline = g_strdup_printf("%s '%s'", cmd, arg);
39   g_debug("Running: %s", cmdline);
40     
41   status = g_spawn_command_line_async(cmdline, NULL);
42
43   g_free(cmdline);
44  
45   return status;
46 }
47
48 void open_url(GtkWindow *parent, const gchar * url)
49 {
50 #ifdef WINDOWS
51   ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
52 #else /* WINDOWS */
53   const gchar *browsers[] = {
54     "xdg-open", "gnome-open", "kfmclient openURL",
55     "sensible-browser", "firefox", "epiphany",
56     "iceweasel", "seamonkey", "galeon", "mozilla",
57     "opera", "konqueror", "netscape", "links -g",
58     NULL
59   };
60   gint i=0;
61   
62   const gchar *browser = g_getenv("BROWSER");
63   if (browser == NULL || browser[0] == '\0') {
64     /* $BROWSER not set -> use first entry */
65     browser = browsers[i++];
66   }
67   do {
68     if (spawn_command_line_async(browser, url)) {
69       return;
70     }
71
72     browser = browsers[i++];
73   } while(browser);
74   
75   a_dialog_error_msg ( parent, _("Could not launch web browser.") );
76 #endif /* WINDOWS */
77 }
78
79 void new_email(GtkWindow *parent, const gchar * address)
80 {
81   gchar *uri = g_strdup_printf("mailto:%s", address);
82 #ifdef WINDOWS
83   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
84 #else /* WINDOWS */
85   if (!spawn_command_line_async("xdg-email", uri))
86     a_dialog_error_msg ( parent, _("Could not create new email.") );
87 #endif /* WINDOWS */
88   g_free(uri);
89   uri = NULL;
90 }
91
92 gchar *uri_escape(gchar *str)
93 {
94   gchar *esc_str = g_malloc(3*strlen(str));
95   gchar *dst = esc_str;
96   gchar *src;
97
98   for (src = str; *src; src++) {
99     if (*src == ' ')
100      *dst++ = '+';
101     else if (g_ascii_isalnum(*src))
102      *dst++ = *src;
103     else {
104       g_sprintf(dst, "%%%02X", *src);
105       dst += 3;
106     }
107   }
108   *dst = '\0';
109
110   return(esc_str);
111 }
112