]> git.street.me.uk Git - andy/viking.git/blame - src/babel.c
Enable a clear icon on entry boxes used for search like input.
[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>
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 */
56static gchar *gpsbabel_loc = NULL;
57
58/**
59 * Path to unbuffer
60 */
61static gchar *unbuffer_loc = NULL;
62
79e0f36e
GB
63/**
64 * List of file formats supported by gpsbabel.
65 */
66GList *a_babel_file_list;
67
68/**
69 * List of device supported by gpsbabel.
70 */
71GList *a_babel_device_list;
72
b666a8ba
GB
73/**
74 * Run a function on all file formats supporting a given mode.
75 */
76void 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 */
106void 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 137gboolean 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 */
157static 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 175static 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
RN
182 if ( vik_debug ) {
183 for ( guint i=0; args[i]; i++ )
184 g_debug ("%s: %s", __FUNCTION__, args[i] );
185 }
186
c3ac9df8 187 if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
f2131761 188 g_warning ("Async command failed: %s", error->message);
6c641b1a 189 g_error_free(error);
8cf048bd
EB
190 ret = FALSE;
191 } else {
2b756ea0 192
8cf048bd
EB
193 gchar line[512];
194 FILE *diag;
195 diag = fdopen(babel_stdout, "r");
196 setvbuf(diag, NULL, _IONBF, 0);
197
198 while (fgets(line, sizeof(line), diag)) {
7b3479e3
EB
199 if ( cb )
200 cb(BABEL_DIAG_OUTPUT, line, user_data);
8cf048bd 201 }
7b3479e3
EB
202 if ( cb )
203 cb(BABEL_DONE, NULL, user_data);
8cf048bd 204 fclose(diag);
8c060406 205 diag = NULL;
c3ac9df8
RN
206
207 g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL );
8cf048bd 208
2634ad33
GB
209 ret = TRUE;
210 }
211
212 return ret;
213}
2634ad33
GB
214
215/**
216 * babel_general_convert_from:
733949a2
RN
217 * @vtl: The TrackWaypoint Layer to save the data into
218 * If it is null it signifies that no data is to be processed,
219 * however the gpsbabel command is still ran as it can be for non-data related options eg:
220 * for use with the power off command - 'command_off'
2634ad33
GB
221 * @cb: callback that is run upon new data from STDOUT (?)
222 * (TODO: STDERR would be nice since we usually redirect STDOUT)
223 * @user_data: passed along to cb
224 *
225 * Runs args[0] with the arguments and uses the GPX module
226 * to import the GPX data into layer vt. Assumes that upon
227 * running the command, the data will appear in the (usually
228 * temporary) file name_dst.
229 *
230 * Returns: %TRUE on success
231 */
232static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
233{
234 gboolean ret = FALSE;
235 FILE *f = NULL;
236
883e5ed6 237 if (babel_general_convert(cb, args, user_data)) {
2634ad33 238
733949a2
RN
239 /* No data actually required but still need to have run gpsbabel anyway
240 - eg using the device power command_off */
241 if ( vt == NULL )
242 return TRUE;
243
8c060406
MA
244 f = g_fopen(name_dst, "r");
245 if (f) {
efe9a9c3 246 ret = a_gpx_read_file ( vt, f );
8c060406
MA
247 fclose(f);
248 f = NULL;
2ecf5998 249 }
8cf048bd
EB
250 }
251
252 return ret;
253}
254
072cbb13 255/**
ef466336
MH
256 * a_babel_convert_from_filter:
257 * @vt: The TRW layer to place data into. Duplicate items will be overwritten.
258 * @babelargs: A string containing gpsbabel command line options. This string
259 * must include the input file type (-i) option.
260 * @from the file name to convert from
261 * @babelfilters: A string containing gpsbabel filter command line options
262 * @cb: Optional callback function. Same usage as in a_babel_convert().
263 * @user_data: passed along to cb
264 * @not_used: Must use NULL
072cbb13
GB
265 *
266 * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous;
267 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
268 * this routine from a worker thread.
269 *
270 * Returns: %TRUE on success
271 */
ef466336 272gboolean 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 273{
f3cd9987 274 int i,j;
1d1bc3c1 275 int fd_dst;
18ec873d 276 gchar *name_dst = NULL;
1d1bc3c1 277 gboolean ret = FALSE;
f3cd9987 278 gchar *args[64];
1d1bc3c1 279
2ecf5998 280 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
e45b3da1 281 g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
1d1bc3c1
EB
282 close(fd_dst);
283
92255687 284 if (gpsbabel_loc ) {
f3cd9987 285 gchar **sub_args = g_strsplit(babelargs, " ", 0);
ef466336 286 gchar **sub_filters = NULL;
f3cd9987
QT
287
288 i = 0;
289 if (unbuffer_loc)
290 args[i++] = unbuffer_loc;
291 args[i++] = gpsbabel_loc;
e208e3c0
QT
292 for (j = 0; sub_args[j]; j++) {
293 /* some version of gpsbabel can not take extra blank arg */
294 if (sub_args[j][0] != '\0')
295 args[i++] = sub_args[j];
296 }
e208e3c0 297 args[i++] = "-f";
8398d878 298 args[i++] = (char *)from;
ef466336 299 if (babelfilters) {
886e92db 300 sub_filters = g_strsplit(babelfilters, " ", 0);
ef466336
MH
301 for (j = 0; sub_filters[j]; j++) {
302 /* some version of gpsbabel can not take extra blank arg */
303 if (sub_filters[j][0] != '\0')
304 args[i++] = sub_filters[j];
305 }
306 }
307 args[i++] = "-o";
308 args[i++] = "gpx";
e208e3c0 309 args[i++] = "-F";
f3cd9987
QT
310 args[i++] = name_dst;
311 args[i] = NULL;
312
313 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
314
f3cd9987 315 g_strfreev(sub_args);
ef466336
MH
316 if (sub_filters)
317 g_strfreev(sub_filters);
374b7f45 318 } else
24ff9c7b 319 g_critical("gpsbabel not found in PATH");
85e9e947 320 (void)g_remove(name_dst);
18ec873d 321 g_free(name_dst);
1d1bc3c1
EB
322 }
323
1d1bc3c1
EB
324 return ret;
325}
8cf048bd 326
072cbb13
GB
327/**
328 * a_babel_convert_from_shellcommand:
ce4f6212
GB
329 * @vt: The #VikTrwLayer where to insert the collected data
330 * @input_cmd: the command to run
17acdaec 331 * @input_file_type:
ce4f6212
GB
332 * @cb: Optional callback function. Same usage as in a_babel_convert().
333 * @user_data: passed along to cb
ed691ed1 334 * @not_used: Must use NULL
072cbb13
GB
335 *
336 * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
337 * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
28c82d8b 338 *
072cbb13 339 * Uses babel_general_convert_from() to actually run the command. This function
28c82d8b
EB
340 * prepares the command and temporary file, and sets up the arguments for bash.
341 */
ed691ed1 342gboolean 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
343{
344 int fd_dst;
18ec873d 345 gchar *name_dst = NULL;
8cf048bd
EB
346 gboolean ret = FALSE;
347 gchar **args;
348
2ecf5998 349 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
e45b3da1 350 g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
28c82d8b
EB
351 gchar *shell_command;
352 if ( input_file_type )
18ec873d
GB
353 shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
354 input_cmd, gpsbabel_loc, input_file_type, name_dst);
28c82d8b
EB
355 else
356 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
357
29a5a545 358 g_debug("%s: %s", __FUNCTION__, shell_command);
8cf048bd
EB
359 close(fd_dst);
360
361 args = g_malloc(sizeof(gchar *)*4);
362 args[0] = BASH_LOCATION;
363 args[1] = "-c";
364 args[2] = shell_command;
365 args[3] = NULL;
366
7b3479e3 367 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
8cf048bd
EB
368 g_free ( args );
369 g_free ( shell_command );
85e9e947 370 (void)g_remove(name_dst);
18ec873d 371 g_free(name_dst);
8cf048bd
EB
372 }
373
8cf048bd
EB
374 return ret;
375}
376
ce4f6212 377/**
17acdaec 378 * a_babel_convert_from_url_filter:
ce4f6212
GB
379 * @vt: The #VikTrwLayer where to insert the collected data
380 * @url: the URL to fetch
17acdaec
RN
381 * @input_type: If input_type is %NULL, input must be GPX.
382 * @babelfilters: The filter arguments to pass to gpsbabel
383 * @cb: Optional callback function. Same usage as in a_babel_convert().
384 * @user_data: Passed along to cb
385 * @options: Download options. If %NULL then default download options will be used.
ce4f6212 386 *
17acdaec
RN
387 * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_type.
388 * If input_type and babelfilters are %NULL, gpsbabel is not used.
ed691ed1
RN
389 *
390 * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
391 *
ce4f6212 392 */
686baff0 393gboolean 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 394{
ed691ed1 395 // If no download options specified, use defaults:
686baff0 396 DownloadFileOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL };
ed691ed1
RN
397 if ( options )
398 myoptions = *options;
533bbf34 399 gint fd_src;
8bd9fe17
GB
400 int fetch_ret;
401 gboolean ret = FALSE;
18ec873d
GB
402 gchar *name_src = NULL;
403 gchar *babelargs = NULL;
533bbf34
MA
404
405 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
406
8bd9fe17 407 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
e45b3da1 408 g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
533bbf34 409 close(fd_src);
85e9e947 410 (void)g_remove(name_src);
533bbf34 411
ed691ed1 412 fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL);
4e815e90 413 if (fetch_ret == DOWNLOAD_SUCCESS) {
ef466336
MH
414 if (input_type != NULL || babelfilters != NULL) {
415 babelargs = (input_type) ? g_strdup_printf(" -i %s", input_type) : g_strdup("");
416 ret = a_babel_convert_from_filter( vt, babelargs, name_src, babelfilters, NULL, NULL, NULL );
cc2e600e
GB
417 } else {
418 /* Process directly the retrieved file */
419 g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src);
420 FILE *f = g_fopen(name_src, "r");
421 if (f) {
422 ret = a_gpx_read_file ( vt, f );
423 fclose(f);
424 f = NULL;
425 }
426 }
427 }
85e9e947 428 (void)util_remove(name_src);
533bbf34
MA
429 g_free(babelargs);
430 g_free(name_src);
431 }
432
433 return ret;
434}
435
ef466336 436/**
17acdaec
RN
437 * a_babel_convert_from:
438 * @vt: The TRW layer to place data into. Duplicate items will be overwritten.
439 * @process_options: The options to control the appropriate processing function. See #ProcessOptions for more detail
440 * @cb: Optional callback function. Same usage as in a_babel_convert().
441 * @user_data: passed along to cb
442 * @download_options: If downloading from a URL use these options (may be NULL)
7f95fd54 443 *
17acdaec
RN
444 * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous;
445 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
446 * this routine from a worker thread.
7f95fd54 447 *
17acdaec 448 * Returns: %TRUE on success
7f95fd54 449 */
155f558c 450gboolean a_babel_convert_from ( VikTrwLayer *vt, ProcessOptions *process_options, BabelStatusFunc cb, gpointer user_data, DownloadFileOptions *download_options )
7f95fd54 451{
17acdaec
RN
452 if ( !process_options ) return FALSE;
453 if ( process_options->url )
454 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 );
455 if ( process_options->babelargs )
456 return a_babel_convert_from_filter ( vt, process_options->babelargs, process_options->filename, process_options->babel_filters, cb, user_data, download_options );
457 if ( process_options->shell_command )
458 return a_babel_convert_from_shellcommand ( vt, process_options->shell_command, process_options->filename, cb, user_data, download_options );
459 return FALSE;
7f95fd54
GB
460}
461
6f94db6d 462static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
70548902 463{
208d2084
RN
464 // Now strips out invisible tracks and waypoints
465 if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) {
24ff9c7b 466 g_critical("Error exporting to %s", name_src);
18ec873d 467 return FALSE;
70548902
MA
468 }
469
883e5ed6 470 return babel_general_convert (cb, args, user_data);
b364d6bc
QT
471}
472
6f94db6d
RN
473/**
474 * a_babel_convert_to:
ce4f6212
GB
475 * @vt: The TRW layer from which data is taken.
476 * @track: Operate on the individual track if specified. Use NULL when operating on a TRW layer
477 * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
6f94db6d 478 * must include the input file type (-i) option.
ce4f6212
GB
479 * @to: Filename or device the data is written to.
480 * @cb: Optional callback function. Same usage as in a_babel_convert.
481 * @user_data: passed along to cb
6f94db6d
RN
482 *
483 * Exports data using gpsbabel. This routine is synchronous;
484 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
485 * this routine from a worker thread.
486 *
487 * Returns: %TRUE on successful invocation of GPSBabel command
488 */
489gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
b364d6bc 490{
f3cd9987 491 int i,j;
b364d6bc 492 int fd_src;
18ec873d 493 gchar *name_src = NULL;
b364d6bc 494 gboolean ret = FALSE;
f3cd9987 495 gchar *args[64];
b364d6bc 496
2ecf5998 497 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
e45b3da1 498 g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
b364d6bc
QT
499 close(fd_src);
500
b364d6bc 501 if (gpsbabel_loc ) {
f3cd9987
QT
502 gchar **sub_args = g_strsplit(babelargs, " ", 0);
503
504 i = 0;
505 if (unbuffer_loc)
506 args[i++] = unbuffer_loc;
507 args[i++] = gpsbabel_loc;
508 args[i++] = "-i";
509 args[i++] = "gpx";
510 for (j = 0; sub_args[j]; j++)
e208e3c0
QT
511 /* some version of gpsbabel can not take extra blank arg */
512 if (sub_args[j][0] != '\0')
513 args[i++] = sub_args[j];
514 args[i++] = "-f";
f3cd9987 515 args[i++] = name_src;
e208e3c0 516 args[i++] = "-F";
8398d878 517 args[i++] = (char *)to;
f3cd9987
QT
518 args[i] = NULL;
519
6f94db6d 520 ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data );
f3cd9987 521
f3cd9987 522 g_strfreev(sub_args);
374b7f45 523 } else
24ff9c7b 524 g_critical("gpsbabel not found in PATH");
85e9e947 525 (void)g_remove(name_src);
18ec873d 526 g_free(name_src);
b364d6bc
QT
527 }
528
b364d6bc
QT
529 return ret;
530}
18ec873d 531
6f2551f1 532static void set_mode(BabelMode *mode, gchar *smode)
79e0f36e 533{
6f2551f1
GB
534 mode->waypointsRead = smode[0] == 'r';
535 mode->waypointsWrite = smode[1] == 'w';
536 mode->tracksRead = smode[2] == 'r';
537 mode->tracksWrite = smode[3] == 'w';
538 mode->routesRead = smode[4] == 'r';
539 mode->routesWrite = smode[5] == 'w';
79e0f36e
GB
540}
541
542/**
ce4f6212 543 * load_feature_parse_line:
79e0f36e
GB
544 *
545 * Load a single feature stored in the given line.
546 */
547static void load_feature_parse_line (gchar *line)
548{
549 gchar **tokens = g_strsplit ( line, "\t", 0 );
550 if ( tokens != NULL
551 && tokens[0] != NULL ) {
552 if ( strcmp("serial", tokens[0]) == 0 ) {
553 if ( tokens[1] != NULL
554 && tokens[2] != NULL
555 && tokens[3] != NULL
556 && tokens[4] != NULL ) {
557 BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
6f2551f1 558 set_mode (&(device->mode), tokens[1]);
79e0f36e 559 device->name = g_strdup (tokens[2]);
431624c5 560 device->label = g_strndup (tokens[4], 50); // Limit really long label text
79e0f36e 561 a_babel_device_list = g_list_append (a_babel_device_list, device);
6f2551f1
GB
562 g_debug ("New gpsbabel device: %s, %d%d%d%d%d%d(%s)",
563 device->name,
564 device->mode.waypointsRead, device->mode.waypointsWrite,
565 device->mode.tracksRead, device->mode.tracksWrite,
566 device->mode.routesRead, device->mode.routesWrite,
567 tokens[1]);
79e0f36e
GB
568 } else {
569 g_warning ( "Unexpected gpsbabel format string: %s", line);
570 }
571 } else if ( strcmp("file", tokens[0]) == 0 ) {
572 if ( tokens[1] != NULL
573 && tokens[2] != NULL
574 && tokens[3] != NULL
575 && tokens[4] != NULL ) {
576 BabelFile *file = g_malloc ( sizeof (BabelFile) );
6f2551f1 577 set_mode (&(file->mode), tokens[1]);
79e0f36e
GB
578 file->name = g_strdup (tokens[2]);
579 file->ext = g_strdup (tokens[3]);
580 file->label = g_strdup (tokens[4]);
581 a_babel_file_list = g_list_append (a_babel_file_list, file);
6f2551f1 582 g_debug ("New gpsbabel file: %s, %d%d%d%d%d%d(%s)",
17720e63
GB
583 file->name,
584 file->mode.waypointsRead, file->mode.waypointsWrite,
585 file->mode.tracksRead, file->mode.tracksWrite,
586 file->mode.routesRead, file->mode.routesWrite,
587 tokens[1]);
79e0f36e
GB
588 } else {
589 g_warning ( "Unexpected gpsbabel format string: %s", line);
590 }
591 } /* else: ignore */
592 } else {
593 g_warning ( "Unexpected gpsbabel format string: %s", line);
594 }
595 g_strfreev ( tokens );
596}
597
598static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data)
599{
600 if (line != NULL)
601 load_feature_parse_line (line);
602}
603
604static gboolean load_feature ()
605{
606 int i;
607 gboolean ret = FALSE;
608 gchar *args[4];
609
610 if ( gpsbabel_loc ) {
611 i = 0;
612 if ( unbuffer_loc )
613 args[i++] = unbuffer_loc;
614 args[i++] = gpsbabel_loc;
615 args[i++] = "-^3";
616 args[i] = NULL;
617
618 ret = babel_general_convert (load_feature_cb, args, NULL);
619 } else
24ff9c7b 620 g_critical("gpsbabel not found in PATH");
79e0f36e
GB
621
622 return ret;
623}
18ec873d 624
cd0a5362
RN
625static VikLayerParam prefs[] = {
626 { 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,
627 N_("Allow setting the specific instance of GPSBabel. You must restart Viking for this value to take effect."), NULL, NULL, NULL },
628};
629
ce4f6212
GB
630/**
631 * a_babel_init:
632 *
e530cc9f 633 * Just setup preferences first
ce4f6212 634 */
18ec873d
GB
635void a_babel_init ()
636{
cd0a5362
RN
637 // Set the defaults
638 VikLayerParamData vlpd;
f928e7c4
RN
639#ifdef WINDOWS
640 // Basic guesses - could use %ProgramFiles% but this is simpler:
641 if ( g_file_test ( "C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe", G_FILE_TEST_EXISTS ) )
642 // 32 bit location on a 64 bit system
449b71f2 643 vlpd.s = "C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe";
f928e7c4
RN
644 else
645 vlpd.s = "C:\\Program Files\\GPSBabel\\gpsbabel.exe";
646#else
cd0a5362 647 vlpd.s = "gpsbabel";
f928e7c4 648#endif
cd0a5362 649 a_preferences_register(&prefs[0], vlpd, VIKING_PREFERENCES_IO_GROUP_KEY);
e530cc9f 650}
cd0a5362 651
e530cc9f
RN
652/**
653 * a_babel_post_init:
654 *
655 * Initialises babel module.
656 * Mainly check existence of gpsbabel progam
657 * and load all features available in that version.
658 */
659void a_babel_post_init ()
660{
cd0a5362
RN
661 // Read the current preference
662 const gchar *gpsbabel = a_preferences_get(VIKING_PREFERENCES_IO_NAMESPACE "gpsbabel")->s;
663 // If setting is still the UNIX default then lookup in the path - otherwise attempt to use the specified value directly.
664 if ( g_strcmp0 ( gpsbabel, "gpsbabel" ) == 0 ) {
665 gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
666 if ( !gpsbabel_loc )
667 g_critical( "gpsbabel not found in PATH" );
668 }
669 else
670 gpsbabel_loc = (gchar*)gpsbabel;
3684f362
RN
671
672 // Unlikely to package unbuffer on Windows so ATM don't even bother trying
673 // Highly unlikely unbuffer is available on a Windows system otherwise
674#ifndef WINDOWS
18ec873d
GB
675 unbuffer_loc = g_find_program_in_path( "unbuffer" );
676 if ( !unbuffer_loc )
677 g_warning( "unbuffer not found in PATH" );
3684f362 678#endif
18ec873d 679
79e0f36e 680 load_feature ();
18ec873d
GB
681}
682
ce4f6212
GB
683/**
684 * a_babel_uninit:
685 *
686 * Free resources acquired by a_babel_init.
687 */
18ec873d
GB
688void a_babel_uninit ()
689{
690 g_free ( gpsbabel_loc );
691 g_free ( unbuffer_loc );
67c32f00
RN
692
693 if ( a_babel_file_list ) {
694 GList *gl;
695 for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
696 BabelFile *file = gl->data;
697 g_free ( file->name );
698 g_free ( file->ext );
699 g_free ( file->label );
700 g_free ( gl->data );
701 }
702 g_list_free ( a_babel_file_list );
703 }
704
705 if ( a_babel_device_list ) {
706 GList *gl;
707 for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
708 BabelDevice *device = gl->data;
709 g_free ( device->name );
710 g_free ( device->label );
711 g_free ( gl->data );
712 }
713 g_list_free ( a_babel_device_list );
714 }
715
18ec873d 716}
430a37a9
GB
717
718/**
719 * a_babel_available:
720 *
721 * Indicates if babel is available or not.
722 *
723 * Returns: true if babel available
724 */
725gboolean a_babel_available ()
726{
727 return a_babel_device_list != NULL;
728}