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