2 * Viking - GPS data editor
3 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
4 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
6 * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Ideally dependencies should just be on Glib, Gtk,
23 * see vikutils for things that further depend on other Viking types
29 #include <glib/gstdio.h>
30 #include <glib/gi18n.h>
31 #include <glib/gprintf.h>
42 static gboolean spawn_command_line_async(const gchar * cmd,
45 gchar *cmdline = NULL;
48 cmdline = g_strdup_printf("%s '%s'", cmd, arg);
49 g_debug("Running: %s", cmdline);
51 status = g_spawn_command_line_async(cmdline, NULL);
60 // Annoyingly gtk_show_uri() doesn't work so resort to ShellExecute method
61 // (non working at least in our Windows build with GTK+2.24.10 on Windows 7)
63 void open_url(GtkWindow *parent, const gchar * url)
66 ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
69 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
71 a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
72 g_error_free ( error );
77 void new_email(GtkWindow *parent, const gchar * address)
79 gchar *uri = g_strdup_printf("mailto:%s", address);
81 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
83 a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
84 g_error_free ( error );
88 ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
90 if (!spawn_command_line_async("xdg-email", uri))
91 a_dialog_error_msg ( parent, _("Could not create new email.") );
97 gchar *uri_escape(gchar *str)
99 gchar *esc_str = g_malloc(3*strlen(str));
100 gchar *dst = esc_str;
103 for (src = str; *src; src++) {
106 else if (g_ascii_isalnum(*src))
109 g_sprintf(dst, "%%%02hhX", *src);
119 GList * str_array_to_glist(gchar* data[])
123 for (p = (gpointer)data; *p; p++)
124 gl = g_list_prepend(gl, *p);
125 return g_list_reverse(gl);
129 * split_string_from_file_on_equals:
131 * @buf: the input string
132 * @key: newly allocated string that is before the '='
133 * @val: newly allocated string after the '='
135 * Designed for file line processing, so it ignores strings beginning with special
136 * characters, such as '#'; returns false in such situations.
137 * Also returns false if no equals character is found
139 * e.g. if buf = "GPS.parameter=42"
140 * key = "GPS.parameter"
143 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
145 // comments, special characters in viking file format
146 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
149 if ( ! strchr ( buf, '=' ) )
152 gchar **strv = g_strsplit ( buf, "=", 2 );
155 gchar *str = strv[gi];
158 *key = g_strdup ( str );
160 *val = g_strdup ( str );
167 // Remove newline from val and also any other whitespace
168 *key = g_strstrip ( *key );
169 *val = g_strstrip ( *val );
174 static GSList* deletion_list = NULL;
177 * util_add_to_deletion_list:
179 * Add a name of a file into the list that is to be deleted on program exit
180 * Normally this is for files that get used asynchronously,
181 * so we don't know when it's time to delete them - other than at this program's end
183 void util_add_to_deletion_list ( const gchar* filename )
185 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
189 * util_remove_all_in_deletion_list:
191 * Delete all the files in the deletion list
192 * This should only be called on program exit
194 void util_remove_all_in_deletion_list ( void )
196 while ( deletion_list )
198 g_remove ( deletion_list->data );
199 g_free ( deletion_list->data );
200 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );