]>
Commit | Line | Data |
---|---|---|
7d02a0b0 GB |
1 | /* |
2 | * Viking - GPS data editor | |
a482007a | 3 | * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
44871dd1 | 4 | * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com> |
7d02a0b0 | 5 | * Based on: |
a482007a | 6 | * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br> |
7d02a0b0 GB |
7 | * |
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. | |
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
5ab2942c RN |
21 | /* |
22 | * Ideally dependencies should just be on Glib, Gtk, | |
23 | * see vikutils for things that further depend on other Viking types | |
24 | */ | |
4c77d5e0 GB |
25 | #ifdef HAVE_CONFIG_H |
26 | #include "config.h" | |
27 | #endif | |
28 | ||
91c46f90 | 29 | #include <glib/gstdio.h> |
4c77d5e0 | 30 | #include <glib/gi18n.h> |
c612e922 | 31 | #include <glib/gprintf.h> |
7d02a0b0 | 32 | |
8e562a70 | 33 | #include "util.h" |
7d02a0b0 GB |
34 | #include "dialog.h" |
35 | ||
7d384035 RN |
36 | #ifdef WINDOWS |
37 | #include <windows.h> | |
38 | #endif | |
39 | ||
5d034940 | 40 | /* |
02d0c367 | 41 | #ifndef WINDOWS |
3530c91e GB |
42 | static gboolean spawn_command_line_async(const gchar * cmd, |
43 | const gchar * arg) | |
44 | { | |
45 | gchar *cmdline = NULL; | |
46 | gboolean status; | |
47 | ||
48 | cmdline = g_strdup_printf("%s '%s'", cmd, arg); | |
49 | g_debug("Running: %s", cmdline); | |
50 | ||
51 | status = g_spawn_command_line_async(cmdline, NULL); | |
52 | ||
53 | g_free(cmdline); | |
54 | ||
a0f4c917 | 55 | return status; |
3530c91e | 56 | } |
02d0c367 | 57 | #endif |
7d384035 | 58 | */ |
3530c91e | 59 | |
5d034940 RN |
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) | |
62 | ||
7d02a0b0 GB |
63 | void open_url(GtkWindow *parent, const gchar * url) |
64 | { | |
5d034940 RN |
65 | #ifdef WINDOWS |
66 | ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0); | |
67 | #else | |
7d384035 RN |
68 | GError *error = NULL; |
69 | gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error ); | |
70 | if ( error ) { | |
71 | a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message ); | |
72 | g_error_free ( error ); | |
7d02a0b0 | 73 | } |
5d034940 | 74 | #endif |
7d02a0b0 | 75 | } |
3530c91e GB |
76 | |
77 | void new_email(GtkWindow *parent, const gchar * address) | |
78 | { | |
79 | gchar *uri = g_strdup_printf("mailto:%s", address); | |
7d384035 RN |
80 | GError *error = NULL; |
81 | gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error ); | |
82 | if ( error ) { | |
83 | a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message ); | |
84 | g_error_free ( error ); | |
85 | } | |
86 | /* | |
3530c91e GB |
87 | #ifdef WINDOWS |
88 | ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0); | |
7d384035 | 89 | #else |
3530c91e GB |
90 | if (!spawn_command_line_async("xdg-email", uri)) |
91 | a_dialog_error_msg ( parent, _("Could not create new email.") ); | |
7d384035 RN |
92 | #endif |
93 | */ | |
3530c91e GB |
94 | g_free(uri); |
95 | uri = NULL; | |
96 | } | |
ba4a5e11 GB |
97 | gchar *uri_escape(gchar *str) |
98 | { | |
99 | gchar *esc_str = g_malloc(3*strlen(str)); | |
100 | gchar *dst = esc_str; | |
101 | gchar *src; | |
102 | ||
103 | for (src = str; *src; src++) { | |
104 | if (*src == ' ') | |
105 | *dst++ = '+'; | |
106 | else if (g_ascii_isalnum(*src)) | |
107 | *dst++ = *src; | |
108 | else { | |
7705a76f | 109 | g_sprintf(dst, "%%%02hhX", *src); |
ba4a5e11 GB |
110 | dst += 3; |
111 | } | |
112 | } | |
113 | *dst = '\0'; | |
114 | ||
115 | return(esc_str); | |
116 | } | |
117 | ||
70434be3 GB |
118 | |
119 | GList * str_array_to_glist(gchar* data[]) | |
120 | { | |
121 | GList *gl = NULL; | |
122 | gpointer * p; | |
123 | for (p = (gpointer)data; *p; p++) | |
124 | gl = g_list_prepend(gl, *p); | |
125 | return g_list_reverse(gl); | |
126 | } | |
9106934d | 127 | |
0da89d90 RN |
128 | /** |
129 | * split_string_from_file_on_equals: | |
130 | * | |
131 | * @buf: the input string | |
132 | * @key: newly allocated string that is before the '=' | |
133 | * @val: newly allocated string after the '=' | |
134 | * | |
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 | |
138 | * | |
139 | * e.g. if buf = "GPS.parameter=42" | |
140 | * key = "GPS.parameter" | |
141 | * val = "42" | |
142 | */ | |
143 | gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val ) | |
9106934d | 144 | { |
9106934d RN |
145 | // comments, special characters in viking file format |
146 | if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' ) | |
147 | return FALSE; | |
0da89d90 RN |
148 | |
149 | if ( ! strchr ( buf, '=' ) ) | |
9106934d | 150 | return FALSE; |
0da89d90 RN |
151 | |
152 | gchar **strv = g_strsplit ( buf, "=", 2 ); | |
153 | ||
154 | gint gi = 0; | |
155 | gchar *str = strv[gi]; | |
156 | while ( str ) { | |
157 | if ( gi == 0 ) | |
158 | *key = g_strdup ( str ); | |
159 | else | |
160 | *val = g_strdup ( str ); | |
161 | gi++; | |
162 | str = strv[gi]; | |
163 | } | |
164 | ||
165 | g_strfreev ( strv ); | |
166 | ||
167 | // Remove newline from val and also any other whitespace | |
168 | *key = g_strstrip ( *key ); | |
169 | *val = g_strstrip ( *val ); | |
170 | ||
9106934d RN |
171 | return TRUE; |
172 | } | |
44871dd1 RN |
173 | |
174 | static GSList* deletion_list = NULL; | |
175 | ||
176 | /** | |
177 | * util_add_to_deletion_list: | |
178 | * | |
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 | |
182 | */ | |
183 | void util_add_to_deletion_list ( const gchar* filename ) | |
184 | { | |
185 | deletion_list = g_slist_append ( deletion_list, g_strdup (filename) ); | |
186 | } | |
187 | ||
188 | /** | |
189 | * util_remove_all_in_deletion_list: | |
190 | * | |
191 | * Delete all the files in the deletion list | |
192 | * This should only be called on program exit | |
193 | */ | |
194 | void util_remove_all_in_deletion_list ( void ) | |
195 | { | |
196 | while ( deletion_list ) | |
197 | { | |
198 | g_remove ( deletion_list->data ); | |
199 | g_free ( deletion_list->data ); | |
200 | deletion_list = g_slist_delete_link ( deletion_list, deletion_list ); | |
201 | } | |
202 | } |