]>
Commit | Line | Data |
---|---|---|
1d1bc3c1 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
a482007a | 5 | * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com> |
7f95fd54 | 6 | * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
17acdaec | 7 | * Copyright (C) 2015, Rob Norris <rw_norris@hotmail.com> |
1d1bc3c1 EB |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | * | |
23 | */ | |
24 | ||
072cbb13 GB |
25 | /** |
26 | * SECTION:babel | |
27 | * @short_description: running external programs and redirecting to TRWLayers. | |
28 | * | |
17acdaec RN |
29 | * GPSBabel may not be necessary for everything, |
30 | * one can use shell_command option but this will be OS platform specific | |
28c82d8b EB |
31 | */ |
32 | ||
45acf79e MA |
33 | #ifdef HAVE_CONFIG_H |
34 | #include "config.h" | |
35 | #endif | |
36 | ||
1d1bc3c1 EB |
37 | #include "viking.h" |
38 | #include "gpx.h" | |
39 | #include "babel.h" | |
cd0a5362 | 40 | #include "preferences.h" |
8c060406 | 41 | #include <stdio.h> |
45acf79e MA |
42 | #ifdef HAVE_UNISTD_H |
43 | #include <unistd.h> | |
44 | #endif | |
79e0f36e | 45 | #include <string.h> |
45acf79e MA |
46 | #include <glib.h> |
47 | #include <glib/gstdio.h> | |
cd0a5362 | 48 | #include <glib/gi18n.h> |
1d1bc3c1 | 49 | |
072cbb13 | 50 | /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */ |
8cf048bd EB |
51 | #define BASH_LOCATION "/bin/bash" |
52 | ||
18ec873d GB |
53 | /** |
54 | * Path to gpsbabel | |
55 | */ | |
56 | static gchar *gpsbabel_loc = NULL; | |
57 | ||
58 | /** | |
59 | * Path to unbuffer | |
60 | */ | |
61 | static gchar *unbuffer_loc = NULL; | |
62 | ||
79e0f36e GB |
63 | /** |
64 | * List of file formats supported by gpsbabel. | |
65 | */ | |
66 | GList *a_babel_file_list; | |
67 | ||
68 | /** | |
69 | * List of device supported by gpsbabel. | |
70 | */ | |
71 | GList *a_babel_device_list; | |
72 | ||
b666a8ba GB |
73 | /** |
74 | * Run a function on all file formats supporting a given mode. | |
75 | */ | |
76 | void a_babel_foreach_file_with_mode (BabelMode mode, GFunc func, gpointer user_data) | |
77 | { | |
78 | GList *current; | |
79 | for ( current = g_list_first (a_babel_file_list) ; | |
80 | current != NULL ; | |
81 | current = g_list_next (current) ) | |
82 | { | |
83 | BabelFile *currentFile = current->data; | |
84 | /* Check compatibility of modes */ | |
85 | gboolean compat = TRUE; | |
86 | if (mode.waypointsRead && ! currentFile->mode.waypointsRead) compat = FALSE; | |
87 | if (mode.waypointsWrite && ! currentFile->mode.waypointsWrite) compat = FALSE; | |
88 | if (mode.tracksRead && ! currentFile->mode.tracksRead) compat = FALSE; | |
89 | if (mode.tracksWrite && ! currentFile->mode.tracksWrite) compat = FALSE; | |
90 | if (mode.routesRead && ! currentFile->mode.routesRead) compat = FALSE; | |
91 | if (mode.routesWrite && ! currentFile->mode.routesWrite) compat = FALSE; | |
92 | /* Do call */ | |
93 | if (compat) | |
94 | func (currentFile, user_data); | |
95 | } | |
96 | } | |
97 | ||
2f5f0fb8 RN |
98 | /** |
99 | * a_babel_foreach_file_read_any: | |
100 | * @func: The function to be called on any file format with a read method | |
101 | * @user_data: Data passed into the function | |
102 | * | |
103 | * Run a function on all file formats with any kind of read method | |
104 | * (which is almost all but not quite - e.g. with GPSBabel v1.4.4 - PalmDoc is write only waypoints) | |
105 | */ | |
106 | void a_babel_foreach_file_read_any (GFunc func, gpointer user_data) | |
107 | { | |
108 | GList *current; | |
109 | for ( current = g_list_first (a_babel_file_list) ; | |
110 | current != NULL ; | |
111 | current = g_list_next (current) ) | |
112 | { | |
113 | BabelFile *currentFile = current->data; | |
114 | // Call function when any read mode found | |
115 | if ( currentFile->mode.waypointsRead || | |
116 | currentFile->mode.tracksRead || | |
117 | currentFile->mode.routesRead) | |
118 | func (currentFile, user_data); | |
119 | } | |
120 | } | |
121 | ||
072cbb13 GB |
122 | /** |
123 | * a_babel_convert: | |
124 | * @vt: The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs. | |
125 | * @babelargs: A string containing gpsbabel command line filter options. No file types or names should | |
126 | * be specified. | |
127 | * @cb: A callback function. | |
ed691ed1 RN |
128 | * @user_data: passed along to cb |
129 | * @not_used: Must use NULL | |
072cbb13 GB |
130 | * |
131 | * This function modifies data in a trw layer using gpsbabel filters. This routine is synchronous; | |
132 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
133 | * this routine from a worker thread. | |
134 | * | |
135 | * Returns: %TRUE on success | |
136 | */ | |
ed691ed1 | 137 | gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
1d1bc3c1 | 138 | { |
1d1bc3c1 | 139 | gboolean ret = FALSE; |
3adc68b0 | 140 | gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL); |
12ed2b58 | 141 | gchar *name_src = a_gpx_write_tmp_file ( vt, NULL ); |
1d1bc3c1 | 142 | |
12ed2b58 | 143 | if ( name_src ) { |
17acdaec RN |
144 | ProcessOptions po = { bargs, name_src, NULL, NULL, NULL }; |
145 | ret = a_babel_convert_from ( vt, &po, cb, user_data, not_used ); | |
80586339 | 146 | (void)g_remove(name_src); |
18ec873d | 147 | g_free(name_src); |
1d1bc3c1 EB |
148 | } |
149 | ||
150 | g_free(bargs); | |
1d1bc3c1 EB |
151 | return ret; |
152 | } | |
153 | ||
c3ac9df8 RN |
154 | /** |
155 | * Perform any cleanup actions once GPSBabel has completed running | |
156 | */ | |
157 | static void babel_watch ( GPid pid, | |
158 | gint status, | |
159 | gpointer user_data ) | |
70548902 | 160 | { |
c3ac9df8 | 161 | g_spawn_close_pid ( pid ); |
70548902 | 162 | } |
c3ac9df8 RN |
163 | |
164 | /** | |
165 | * babel_general_convert: | |
166 | * @args: The command line arguments passed to GPSBabel | |
167 | * @cb: callback that is run for each line of GPSBabel output and at completion of the run | |
168 | * callback may be NULL | |
169 | * @user_data: passed along to cb | |
170 | * | |
171 | * The function to actually invoke the GPSBabel external command | |
172 | * | |
173 | * Returns: %TRUE on successful invocation of GPSBabel command | |
174 | */ | |
883e5ed6 | 175 | static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data ) |
8cf048bd | 176 | { |
2ecf5998 | 177 | gboolean ret = FALSE; |
8cf048bd | 178 | GPid pid; |
6c641b1a MA |
179 | GError *error = NULL; |
180 | gint babel_stdout; | |
8cf048bd | 181 | |
17acdaec | 182 | if ( vik_debug ) { |
d5819fad | 183 | (void)g_printf ( "%s:", __FUNCTION__ ); |
17acdaec | 184 | for ( guint i=0; args[i]; i++ ) |
d5819fad RN |
185 | (void)g_printf ( " %s", args[i] ); |
186 | (void)g_printf ( "\n" ); | |
17acdaec RN |
187 | } |
188 | ||
c3ac9df8 | 189 | if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) { |
f2131761 | 190 | g_warning ("Async command failed: %s", error->message); |
6c641b1a | 191 | g_error_free(error); |
8cf048bd EB |
192 | ret = FALSE; |
193 | } else { | |
2b756ea0 | 194 | |
8cf048bd EB |
195 | gchar line[512]; |
196 | FILE *diag; | |
197 | diag = fdopen(babel_stdout, "r"); | |
198 | setvbuf(diag, NULL, _IONBF, 0); | |
199 | ||
200 | while (fgets(line, sizeof(line), diag)) { | |
7b3479e3 EB |
201 | if ( cb ) |
202 | cb(BABEL_DIAG_OUTPUT, line, user_data); | |
8cf048bd | 203 | } |
7b3479e3 EB |
204 | if ( cb ) |
205 | cb(BABEL_DONE, NULL, user_data); | |
8cf048bd | 206 | fclose(diag); |
8c060406 | 207 | diag = NULL; |
c3ac9df8 RN |
208 | |
209 | g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL ); | |
8cf048bd | 210 | |
eaa45678 RN |
211 | // Useful to see in case of any errors, |
212 | // although they don't always occur on the last line output | |
213 | g_debug ( "%s: last received line is=\"%s\"", __FUNCTION__, line ); | |
2634ad33 GB |
214 | ret = TRUE; |
215 | } | |
216 | ||
217 | return ret; | |
218 | } | |
2634ad33 GB |
219 | |
220 | /** | |
221 | * babel_general_convert_from: | |
733949a2 RN |
222 | * @vtl: The TrackWaypoint Layer to save the data into |
223 | * If it is null it signifies that no data is to be processed, | |
224 | * however the gpsbabel command is still ran as it can be for non-data related options eg: | |
225 | * for use with the power off command - 'command_off' | |
2634ad33 GB |
226 | * @cb: callback that is run upon new data from STDOUT (?) |
227 | * (TODO: STDERR would be nice since we usually redirect STDOUT) | |
228 | * @user_data: passed along to cb | |
229 | * | |
230 | * Runs args[0] with the arguments and uses the GPX module | |
231 | * to import the GPX data into layer vt. Assumes that upon | |
232 | * running the command, the data will appear in the (usually | |
233 | * temporary) file name_dst. | |
234 | * | |
235 | * Returns: %TRUE on success | |
236 | */ | |
237 | static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data ) | |
238 | { | |
239 | gboolean ret = FALSE; | |
240 | FILE *f = NULL; | |
241 | ||
883e5ed6 | 242 | if (babel_general_convert(cb, args, user_data)) { |
2634ad33 | 243 | |
733949a2 RN |
244 | /* No data actually required but still need to have run gpsbabel anyway |
245 | - eg using the device power command_off */ | |
246 | if ( vt == NULL ) | |
247 | return TRUE; | |
248 | ||
8c060406 MA |
249 | f = g_fopen(name_dst, "r"); |
250 | if (f) { | |
8a439f31 RN |
251 | gchar *dirpath = g_path_get_dirname ( name_dst ); |
252 | ret = a_gpx_read_file ( vt, f, dirpath ); | |
253 | g_free ( dirpath ); | |
8c060406 | 254 | fclose(f); |
2ecf5998 | 255 | } |
8cf048bd EB |
256 | } |
257 | ||
258 | return ret; | |
259 | } | |
260 | ||
072cbb13 | 261 | /** |
ef466336 MH |
262 | * a_babel_convert_from_filter: |
263 | * @vt: The TRW layer to place data into. Duplicate items will be overwritten. | |
264 | * @babelargs: A string containing gpsbabel command line options. This string | |
265 | * must include the input file type (-i) option. | |
266 | * @from the file name to convert from | |
267 | * @babelfilters: A string containing gpsbabel filter command line options | |
268 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
269 | * @user_data: passed along to cb | |
270 | * @not_used: Must use NULL | |
072cbb13 GB |
271 | * |
272 | * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous; | |
273 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
274 | * this routine from a worker thread. | |
275 | * | |
276 | * Returns: %TRUE on success | |
277 | */ | |
ef466336 | 278 | gboolean a_babel_convert_from_filter( VikTrwLayer *vt, const char *babelargs, const char *from, const char *babelfilters, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
1d1bc3c1 | 279 | { |
f3cd9987 | 280 | int i,j; |
1d1bc3c1 | 281 | int fd_dst; |
18ec873d | 282 | gchar *name_dst = NULL; |
1d1bc3c1 | 283 | gboolean ret = FALSE; |
f3cd9987 | 284 | gchar *args[64]; |
1d1bc3c1 | 285 | |
2ecf5998 | 286 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) { |
e45b3da1 | 287 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst); |
1d1bc3c1 EB |
288 | close(fd_dst); |
289 | ||
92255687 | 290 | if (gpsbabel_loc ) { |
f3cd9987 | 291 | gchar **sub_args = g_strsplit(babelargs, " ", 0); |
ef466336 | 292 | gchar **sub_filters = NULL; |
f3cd9987 QT |
293 | |
294 | i = 0; | |
295 | if (unbuffer_loc) | |
296 | args[i++] = unbuffer_loc; | |
297 | args[i++] = gpsbabel_loc; | |
e208e3c0 QT |
298 | for (j = 0; sub_args[j]; j++) { |
299 | /* some version of gpsbabel can not take extra blank arg */ | |
300 | if (sub_args[j][0] != '\0') | |
301 | args[i++] = sub_args[j]; | |
302 | } | |
e208e3c0 | 303 | args[i++] = "-f"; |
8398d878 | 304 | args[i++] = (char *)from; |
ef466336 | 305 | if (babelfilters) { |
886e92db | 306 | sub_filters = g_strsplit(babelfilters, " ", 0); |
ef466336 MH |
307 | for (j = 0; sub_filters[j]; j++) { |
308 | /* some version of gpsbabel can not take extra blank arg */ | |
309 | if (sub_filters[j][0] != '\0') | |
310 | args[i++] = sub_filters[j]; | |
311 | } | |
312 | } | |
313 | args[i++] = "-o"; | |
314 | args[i++] = "gpx"; | |
e208e3c0 | 315 | args[i++] = "-F"; |
f3cd9987 QT |
316 | args[i++] = name_dst; |
317 | args[i] = NULL; | |
318 | ||
319 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); | |
320 | ||
f3cd9987 | 321 | g_strfreev(sub_args); |
ef466336 MH |
322 | if (sub_filters) |
323 | g_strfreev(sub_filters); | |
374b7f45 | 324 | } else |
24ff9c7b | 325 | g_critical("gpsbabel not found in PATH"); |
85e9e947 | 326 | (void)g_remove(name_dst); |
18ec873d | 327 | g_free(name_dst); |
1d1bc3c1 EB |
328 | } |
329 | ||
1d1bc3c1 EB |
330 | return ret; |
331 | } | |
8cf048bd | 332 | |
072cbb13 GB |
333 | /** |
334 | * a_babel_convert_from_shellcommand: | |
ce4f6212 GB |
335 | * @vt: The #VikTrwLayer where to insert the collected data |
336 | * @input_cmd: the command to run | |
17acdaec | 337 | * @input_file_type: |
ce4f6212 GB |
338 | * @cb: Optional callback function. Same usage as in a_babel_convert(). |
339 | * @user_data: passed along to cb | |
ed691ed1 | 340 | * @not_used: Must use NULL |
072cbb13 GB |
341 | * |
342 | * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type. | |
343 | * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc) | |
28c82d8b | 344 | * |
072cbb13 | 345 | * Uses babel_general_convert_from() to actually run the command. This function |
28c82d8b EB |
346 | * prepares the command and temporary file, and sets up the arguments for bash. |
347 | */ | |
ed691ed1 | 348 | gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
8cf048bd EB |
349 | { |
350 | int fd_dst; | |
18ec873d | 351 | gchar *name_dst = NULL; |
8cf048bd EB |
352 | gboolean ret = FALSE; |
353 | gchar **args; | |
354 | ||
2ecf5998 | 355 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) { |
e45b3da1 | 356 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst); |
28c82d8b EB |
357 | gchar *shell_command; |
358 | if ( input_file_type ) | |
18ec873d GB |
359 | shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s", |
360 | input_cmd, gpsbabel_loc, input_file_type, name_dst); | |
28c82d8b EB |
361 | else |
362 | shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst); | |
363 | ||
29a5a545 | 364 | g_debug("%s: %s", __FUNCTION__, shell_command); |
8cf048bd EB |
365 | close(fd_dst); |
366 | ||
367 | args = g_malloc(sizeof(gchar *)*4); | |
368 | args[0] = BASH_LOCATION; | |
369 | args[1] = "-c"; | |
370 | args[2] = shell_command; | |
371 | args[3] = NULL; | |
372 | ||
7b3479e3 | 373 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); |
8cf048bd EB |
374 | g_free ( args ); |
375 | g_free ( shell_command ); | |
85e9e947 | 376 | (void)g_remove(name_dst); |
18ec873d | 377 | g_free(name_dst); |
8cf048bd EB |
378 | } |
379 | ||
8cf048bd EB |
380 | return ret; |
381 | } | |
382 | ||
ce4f6212 | 383 | /** |
17acdaec | 384 | * a_babel_convert_from_url_filter: |
ce4f6212 GB |
385 | * @vt: The #VikTrwLayer where to insert the collected data |
386 | * @url: the URL to fetch | |
17acdaec RN |
387 | * @input_type: If input_type is %NULL, input must be GPX. |
388 | * @babelfilters: The filter arguments to pass to gpsbabel | |
389 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
390 | * @user_data: Passed along to cb | |
391 | * @options: Download options. If %NULL then default download options will be used. | |
ce4f6212 | 392 | * |
17acdaec RN |
393 | * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_type. |
394 | * If input_type and babelfilters are %NULL, gpsbabel is not used. | |
ed691ed1 RN |
395 | * |
396 | * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX | |
397 | * | |
ce4f6212 | 398 | */ |
686baff0 | 399 | gboolean a_babel_convert_from_url_filter ( VikTrwLayer *vt, const char *url, const char *input_type, const char *babelfilters, BabelStatusFunc cb, gpointer user_data, DownloadFileOptions *options ) |
533bbf34 | 400 | { |
ed691ed1 | 401 | // If no download options specified, use defaults: |
686baff0 | 402 | DownloadFileOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL }; |
ed691ed1 RN |
403 | if ( options ) |
404 | myoptions = *options; | |
533bbf34 | 405 | gint fd_src; |
8bd9fe17 GB |
406 | int fetch_ret; |
407 | gboolean ret = FALSE; | |
18ec873d GB |
408 | gchar *name_src = NULL; |
409 | gchar *babelargs = NULL; | |
533bbf34 MA |
410 | |
411 | g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url); | |
412 | ||
8bd9fe17 | 413 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 414 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
533bbf34 | 415 | close(fd_src); |
85e9e947 | 416 | (void)g_remove(name_src); |
533bbf34 | 417 | |
ed691ed1 | 418 | fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL); |
4e815e90 | 419 | if (fetch_ret == DOWNLOAD_SUCCESS) { |
ef466336 MH |
420 | if (input_type != NULL || babelfilters != NULL) { |
421 | babelargs = (input_type) ? g_strdup_printf(" -i %s", input_type) : g_strdup(""); | |
422 | ret = a_babel_convert_from_filter( vt, babelargs, name_src, babelfilters, NULL, NULL, NULL ); | |
cc2e600e GB |
423 | } else { |
424 | /* Process directly the retrieved file */ | |
425 | g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src); | |
426 | FILE *f = g_fopen(name_src, "r"); | |
427 | if (f) { | |
8a439f31 RN |
428 | gchar *dirpath = g_path_get_dirname ( name_src ); |
429 | ret = a_gpx_read_file ( vt, f, dirpath ); | |
430 | g_free ( dirpath ); | |
cc2e600e | 431 | fclose(f); |
cc2e600e GB |
432 | } |
433 | } | |
434 | } | |
85e9e947 | 435 | (void)util_remove(name_src); |
533bbf34 MA |
436 | g_free(babelargs); |
437 | g_free(name_src); | |
438 | } | |
439 | ||
440 | return ret; | |
441 | } | |
442 | ||
ef466336 | 443 | /** |
17acdaec RN |
444 | * a_babel_convert_from: |
445 | * @vt: The TRW layer to place data into. Duplicate items will be overwritten. | |
446 | * @process_options: The options to control the appropriate processing function. See #ProcessOptions for more detail | |
447 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
448 | * @user_data: passed along to cb | |
449 | * @download_options: If downloading from a URL use these options (may be NULL) | |
7f95fd54 | 450 | * |
17acdaec RN |
451 | * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous; |
452 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
453 | * this routine from a worker thread. | |
7f95fd54 | 454 | * |
17acdaec | 455 | * Returns: %TRUE on success |
7f95fd54 | 456 | */ |
155f558c | 457 | gboolean a_babel_convert_from ( VikTrwLayer *vt, ProcessOptions *process_options, BabelStatusFunc cb, gpointer user_data, DownloadFileOptions *download_options ) |
7f95fd54 | 458 | { |
17acdaec RN |
459 | if ( !process_options ) return FALSE; |
460 | if ( process_options->url ) | |
461 | return a_babel_convert_from_url_filter ( vt, process_options->url, process_options->input_file_type, process_options->babel_filters, cb, user_data, download_options ); | |
462 | if ( process_options->babelargs ) | |
463 | return a_babel_convert_from_filter ( vt, process_options->babelargs, process_options->filename, process_options->babel_filters, cb, user_data, download_options ); | |
464 | if ( process_options->shell_command ) | |
465 | return a_babel_convert_from_shellcommand ( vt, process_options->shell_command, process_options->filename, cb, user_data, download_options ); | |
466 | return FALSE; | |
7f95fd54 GB |
467 | } |
468 | ||
6f94db6d | 469 | static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data ) |
70548902 | 470 | { |
208d2084 RN |
471 | // Now strips out invisible tracks and waypoints |
472 | if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) { | |
24ff9c7b | 473 | g_critical("Error exporting to %s", name_src); |
18ec873d | 474 | return FALSE; |
70548902 MA |
475 | } |
476 | ||
883e5ed6 | 477 | return babel_general_convert (cb, args, user_data); |
b364d6bc QT |
478 | } |
479 | ||
6f94db6d RN |
480 | /** |
481 | * a_babel_convert_to: | |
ce4f6212 GB |
482 | * @vt: The TRW layer from which data is taken. |
483 | * @track: Operate on the individual track if specified. Use NULL when operating on a TRW layer | |
484 | * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string | |
6f94db6d | 485 | * must include the input file type (-i) option. |
ce4f6212 GB |
486 | * @to: Filename or device the data is written to. |
487 | * @cb: Optional callback function. Same usage as in a_babel_convert. | |
488 | * @user_data: passed along to cb | |
6f94db6d RN |
489 | * |
490 | * Exports data using gpsbabel. This routine is synchronous; | |
491 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
492 | * this routine from a worker thread. | |
493 | * | |
494 | * Returns: %TRUE on successful invocation of GPSBabel command | |
495 | */ | |
496 | gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data ) | |
b364d6bc | 497 | { |
f3cd9987 | 498 | int i,j; |
b364d6bc | 499 | int fd_src; |
18ec873d | 500 | gchar *name_src = NULL; |
b364d6bc | 501 | gboolean ret = FALSE; |
f3cd9987 | 502 | gchar *args[64]; |
b364d6bc | 503 | |
2ecf5998 | 504 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 505 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
b364d6bc QT |
506 | close(fd_src); |
507 | ||
b364d6bc | 508 | if (gpsbabel_loc ) { |
f3cd9987 QT |
509 | gchar **sub_args = g_strsplit(babelargs, " ", 0); |
510 | ||
511 | i = 0; | |
512 | if (unbuffer_loc) | |
513 | args[i++] = unbuffer_loc; | |
514 | args[i++] = gpsbabel_loc; | |
515 | args[i++] = "-i"; | |
516 | args[i++] = "gpx"; | |
517 | for (j = 0; sub_args[j]; j++) | |
e208e3c0 QT |
518 | /* some version of gpsbabel can not take extra blank arg */ |
519 | if (sub_args[j][0] != '\0') | |
520 | args[i++] = sub_args[j]; | |
521 | args[i++] = "-f"; | |
f3cd9987 | 522 | args[i++] = name_src; |
e208e3c0 | 523 | args[i++] = "-F"; |
8398d878 | 524 | args[i++] = (char *)to; |
f3cd9987 QT |
525 | args[i] = NULL; |
526 | ||
6f94db6d | 527 | ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data ); |
f3cd9987 | 528 | |
f3cd9987 | 529 | g_strfreev(sub_args); |
374b7f45 | 530 | } else |
24ff9c7b | 531 | g_critical("gpsbabel not found in PATH"); |
85e9e947 | 532 | (void)g_remove(name_src); |
18ec873d | 533 | g_free(name_src); |
b364d6bc QT |
534 | } |
535 | ||
b364d6bc QT |
536 | return ret; |
537 | } | |
18ec873d | 538 | |
6f2551f1 | 539 | static void set_mode(BabelMode *mode, gchar *smode) |
79e0f36e | 540 | { |
6f2551f1 GB |
541 | mode->waypointsRead = smode[0] == 'r'; |
542 | mode->waypointsWrite = smode[1] == 'w'; | |
543 | mode->tracksRead = smode[2] == 'r'; | |
544 | mode->tracksWrite = smode[3] == 'w'; | |
545 | mode->routesRead = smode[4] == 'r'; | |
546 | mode->routesWrite = smode[5] == 'w'; | |
79e0f36e GB |
547 | } |
548 | ||
549 | /** | |
ce4f6212 | 550 | * load_feature_parse_line: |
79e0f36e GB |
551 | * |
552 | * Load a single feature stored in the given line. | |
553 | */ | |
554 | static void load_feature_parse_line (gchar *line) | |
555 | { | |
556 | gchar **tokens = g_strsplit ( line, "\t", 0 ); | |
557 | if ( tokens != NULL | |
558 | && tokens[0] != NULL ) { | |
559 | if ( strcmp("serial", tokens[0]) == 0 ) { | |
560 | if ( tokens[1] != NULL | |
561 | && tokens[2] != NULL | |
562 | && tokens[3] != NULL | |
563 | && tokens[4] != NULL ) { | |
564 | BabelDevice *device = g_malloc ( sizeof (BabelDevice) ); | |
6f2551f1 | 565 | set_mode (&(device->mode), tokens[1]); |
79e0f36e | 566 | device->name = g_strdup (tokens[2]); |
431624c5 | 567 | device->label = g_strndup (tokens[4], 50); // Limit really long label text |
79e0f36e | 568 | a_babel_device_list = g_list_append (a_babel_device_list, device); |
6f2551f1 GB |
569 | g_debug ("New gpsbabel device: %s, %d%d%d%d%d%d(%s)", |
570 | device->name, | |
571 | device->mode.waypointsRead, device->mode.waypointsWrite, | |
572 | device->mode.tracksRead, device->mode.tracksWrite, | |
573 | device->mode.routesRead, device->mode.routesWrite, | |
574 | tokens[1]); | |
79e0f36e GB |
575 | } else { |
576 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
577 | } | |
578 | } else if ( strcmp("file", tokens[0]) == 0 ) { | |
579 | if ( tokens[1] != NULL | |
580 | && tokens[2] != NULL | |
581 | && tokens[3] != NULL | |
582 | && tokens[4] != NULL ) { | |
583 | BabelFile *file = g_malloc ( sizeof (BabelFile) ); | |
6f2551f1 | 584 | set_mode (&(file->mode), tokens[1]); |
79e0f36e GB |
585 | file->name = g_strdup (tokens[2]); |
586 | file->ext = g_strdup (tokens[3]); | |
587 | file->label = g_strdup (tokens[4]); | |
588 | a_babel_file_list = g_list_append (a_babel_file_list, file); | |
6f2551f1 | 589 | g_debug ("New gpsbabel file: %s, %d%d%d%d%d%d(%s)", |
17720e63 GB |
590 | file->name, |
591 | file->mode.waypointsRead, file->mode.waypointsWrite, | |
592 | file->mode.tracksRead, file->mode.tracksWrite, | |
593 | file->mode.routesRead, file->mode.routesWrite, | |
594 | tokens[1]); | |
79e0f36e GB |
595 | } else { |
596 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
597 | } | |
598 | } /* else: ignore */ | |
599 | } else { | |
600 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
601 | } | |
602 | g_strfreev ( tokens ); | |
603 | } | |
604 | ||
605 | static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data) | |
606 | { | |
607 | if (line != NULL) | |
608 | load_feature_parse_line (line); | |
609 | } | |
610 | ||
611 | static gboolean load_feature () | |
612 | { | |
613 | int i; | |
614 | gboolean ret = FALSE; | |
615 | gchar *args[4]; | |
616 | ||
617 | if ( gpsbabel_loc ) { | |
618 | i = 0; | |
619 | if ( unbuffer_loc ) | |
620 | args[i++] = unbuffer_loc; | |
621 | args[i++] = gpsbabel_loc; | |
622 | args[i++] = "-^3"; | |
623 | args[i] = NULL; | |
624 | ||
625 | ret = babel_general_convert (load_feature_cb, args, NULL); | |
626 | } else | |
24ff9c7b | 627 | g_critical("gpsbabel not found in PATH"); |
79e0f36e GB |
628 | |
629 | return ret; | |
630 | } | |
18ec873d | 631 | |
cd0a5362 RN |
632 | static VikLayerParam prefs[] = { |
633 | { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_IO_NAMESPACE "gpsbabel", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("GPSBabel:"), VIK_LAYER_WIDGET_FILEENTRY, NULL, NULL, | |
634 | N_("Allow setting the specific instance of GPSBabel. You must restart Viking for this value to take effect."), NULL, NULL, NULL }, | |
635 | }; | |
636 | ||
ce4f6212 GB |
637 | /** |
638 | * a_babel_init: | |
639 | * | |
e530cc9f | 640 | * Just setup preferences first |
ce4f6212 | 641 | */ |
18ec873d GB |
642 | void a_babel_init () |
643 | { | |
cd0a5362 RN |
644 | // Set the defaults |
645 | VikLayerParamData vlpd; | |
f928e7c4 RN |
646 | #ifdef WINDOWS |
647 | // Basic guesses - could use %ProgramFiles% but this is simpler: | |
648 | if ( g_file_test ( "C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe", G_FILE_TEST_EXISTS ) ) | |
649 | // 32 bit location on a 64 bit system | |
449b71f2 | 650 | vlpd.s = "C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe"; |
f928e7c4 RN |
651 | else |
652 | vlpd.s = "C:\\Program Files\\GPSBabel\\gpsbabel.exe"; | |
653 | #else | |
cd0a5362 | 654 | vlpd.s = "gpsbabel"; |
f928e7c4 | 655 | #endif |
cd0a5362 | 656 | a_preferences_register(&prefs[0], vlpd, VIKING_PREFERENCES_IO_GROUP_KEY); |
e530cc9f | 657 | } |
cd0a5362 | 658 | |
e530cc9f RN |
659 | /** |
660 | * a_babel_post_init: | |
661 | * | |
662 | * Initialises babel module. | |
663 | * Mainly check existence of gpsbabel progam | |
664 | * and load all features available in that version. | |
665 | */ | |
666 | void a_babel_post_init () | |
667 | { | |
cd0a5362 RN |
668 | // Read the current preference |
669 | const gchar *gpsbabel = a_preferences_get(VIKING_PREFERENCES_IO_NAMESPACE "gpsbabel")->s; | |
670 | // If setting is still the UNIX default then lookup in the path - otherwise attempt to use the specified value directly. | |
671 | if ( g_strcmp0 ( gpsbabel, "gpsbabel" ) == 0 ) { | |
672 | gpsbabel_loc = g_find_program_in_path( "gpsbabel" ); | |
673 | if ( !gpsbabel_loc ) | |
674 | g_critical( "gpsbabel not found in PATH" ); | |
675 | } | |
676 | else | |
7058d141 | 677 | gpsbabel_loc = g_strdup ( gpsbabel ); |
3684f362 RN |
678 | |
679 | // Unlikely to package unbuffer on Windows so ATM don't even bother trying | |
680 | // Highly unlikely unbuffer is available on a Windows system otherwise | |
681 | #ifndef WINDOWS | |
18ec873d GB |
682 | unbuffer_loc = g_find_program_in_path( "unbuffer" ); |
683 | if ( !unbuffer_loc ) | |
684 | g_warning( "unbuffer not found in PATH" ); | |
3684f362 | 685 | #endif |
18ec873d | 686 | |
79e0f36e | 687 | load_feature (); |
18ec873d GB |
688 | } |
689 | ||
ce4f6212 GB |
690 | /** |
691 | * a_babel_uninit: | |
692 | * | |
693 | * Free resources acquired by a_babel_init. | |
694 | */ | |
18ec873d GB |
695 | void a_babel_uninit () |
696 | { | |
697 | g_free ( gpsbabel_loc ); | |
698 | g_free ( unbuffer_loc ); | |
67c32f00 RN |
699 | |
700 | if ( a_babel_file_list ) { | |
701 | GList *gl; | |
702 | for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) { | |
703 | BabelFile *file = gl->data; | |
704 | g_free ( file->name ); | |
705 | g_free ( file->ext ); | |
706 | g_free ( file->label ); | |
707 | g_free ( gl->data ); | |
708 | } | |
709 | g_list_free ( a_babel_file_list ); | |
710 | } | |
711 | ||
712 | if ( a_babel_device_list ) { | |
713 | GList *gl; | |
714 | for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) { | |
715 | BabelDevice *device = gl->data; | |
716 | g_free ( device->name ); | |
717 | g_free ( device->label ); | |
718 | g_free ( gl->data ); | |
719 | } | |
720 | g_list_free ( a_babel_device_list ); | |
721 | } | |
722 | ||
18ec873d | 723 | } |
430a37a9 GB |
724 | |
725 | /** | |
726 | * a_babel_available: | |
727 | * | |
728 | * Indicates if babel is available or not. | |
729 | * | |
730 | * Returns: true if babel available | |
731 | */ | |
732 | gboolean a_babel_available () | |
733 | { | |
734 | return a_babel_device_list != NULL; | |
735 | } |