]> git.street.me.uk Git - andy/viking.git/blob - src/babel.c
Read OSM Metatile capability and basic test with single example metatile.
[andy/viking.git] / src / babel.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com>
6  * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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
24 /**
25  * SECTION:babel
26  * @short_description: running external programs and redirecting to TRWLayers.
27  *
28  * GPSBabel may not be necessary for everything -- for instance,
29  *   use a_babel_convert_from_shellcommand() with input_file_type == %NULL
30  *   for an external program that outputs GPX.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "viking.h"
38 #include "gpx.h"
39 #include "babel.h"
40 #include <stdio.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #include <string.h>
45 #include <glib.h>
46 #include <glib/gstdio.h>
47
48 /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */
49 #define BASH_LOCATION "/bin/bash"
50
51 /**
52  * List of supported protocols.
53  */
54 const gchar *PROTOS[] = { "http://", "https://", "ftp://", NULL };
55
56 /**
57  * Path to gpsbabel
58  */
59 static gchar *gpsbabel_loc = NULL;
60
61 /**
62  * Path to unbuffer
63  */
64 static gchar *unbuffer_loc = NULL;
65
66 /**
67  * List of file formats supported by gpsbabel.
68  */
69 GList *a_babel_file_list;
70
71 /**
72  * List of device supported by gpsbabel.
73  */
74 GList *a_babel_device_list;
75
76 /**
77  * Run a function on all file formats supporting a given mode.
78  */
79 void a_babel_foreach_file_with_mode (BabelMode mode, GFunc func, gpointer user_data)
80 {
81   GList *current;
82   for ( current = g_list_first (a_babel_file_list) ;
83         current != NULL ;
84         current = g_list_next (current) )
85   {
86     BabelFile *currentFile = current->data;
87     /* Check compatibility of modes */
88     gboolean compat = TRUE;
89     if (mode.waypointsRead  && ! currentFile->mode.waypointsRead)  compat = FALSE;
90     if (mode.waypointsWrite && ! currentFile->mode.waypointsWrite) compat = FALSE;
91     if (mode.tracksRead     && ! currentFile->mode.tracksRead)     compat = FALSE;
92     if (mode.tracksWrite    && ! currentFile->mode.tracksWrite)    compat = FALSE;
93     if (mode.routesRead     && ! currentFile->mode.routesRead)     compat = FALSE;
94     if (mode.routesWrite    && ! currentFile->mode.routesWrite)    compat = FALSE;
95     /* Do call */
96     if (compat)
97       func (currentFile, user_data);
98   }
99 }
100
101 /**
102  * a_babel_foreach_file_read_any:
103  * @func:      The function to be called on any file format with a read method
104  * @user_data: Data passed into the function
105  *
106  * Run a function on all file formats with any kind of read method
107  *  (which is almost all but not quite - e.g. with GPSBabel v1.4.4 - PalmDoc is write only waypoints)
108  */
109 void a_babel_foreach_file_read_any (GFunc func, gpointer user_data)
110 {
111   GList *current;
112   for ( current = g_list_first (a_babel_file_list) ;
113         current != NULL ;
114         current = g_list_next (current) )
115   {
116     BabelFile *currentFile = current->data;
117     // Call function when any read mode found
118     if ( currentFile->mode.waypointsRead ||
119          currentFile->mode.tracksRead ||
120          currentFile->mode.routesRead)
121       func (currentFile, user_data);
122   }
123 }
124
125 /**
126  * a_babel_convert:
127  * @vt:        The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs.
128  * @babelargs: A string containing gpsbabel command line filter options. No file types or names should
129  *             be specified.
130  * @cb:        A callback function.
131  * @user_data: passed along to cb
132  * @not_used:  Must use NULL
133  *
134  * This function modifies data in a trw layer using gpsbabel filters.  This routine is synchronous;
135  * that is, it will block the calling program until the conversion is done. To avoid blocking, call
136  * this routine from a worker thread.
137  *
138  * Returns: %TRUE on success
139  */
140 gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
141 {
142   gboolean ret = FALSE;
143   gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
144   gchar *name_src = a_gpx_write_tmp_file ( vt, NULL );
145
146   if ( name_src ) {
147     ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used );
148     g_remove(name_src);
149     g_free(name_src);
150   }
151
152   g_free(bargs);
153   return ret;
154 }
155
156 /**
157  * Perform any cleanup actions once GPSBabel has completed running
158  */
159 static void babel_watch ( GPid pid,
160                           gint status,
161                           gpointer user_data )
162 {
163   g_spawn_close_pid ( pid );
164 }
165
166 /**
167  * babel_general_convert:
168  * @args: The command line arguments passed to GPSBabel
169  * @cb: callback that is run for each line of GPSBabel output and at completion of the run
170  *      callback may be NULL
171  * @user_data: passed along to cb
172  *
173  * The function to actually invoke the GPSBabel external command
174  *
175  * Returns: %TRUE on successful invocation of GPSBabel command
176  */
177 static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
178 {
179   gboolean ret = FALSE;
180   GPid pid;
181   GError *error = NULL;
182   gint babel_stdout;
183
184   if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
185     g_warning ("Async command failed: %s", error->message);
186     g_error_free(error);
187     ret = FALSE;
188   } else {
189
190     gchar line[512];
191     FILE *diag;
192     diag = fdopen(babel_stdout, "r");
193     setvbuf(diag, NULL, _IONBF, 0);
194
195     while (fgets(line, sizeof(line), diag)) {
196       if ( cb )
197         cb(BABEL_DIAG_OUTPUT, line, user_data);
198     }
199     if ( cb )
200       cb(BABEL_DONE, NULL, user_data);
201     fclose(diag);
202     diag = NULL;
203
204     g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL );
205
206     ret = TRUE;
207   }
208     
209   return ret;
210 }
211
212 /**
213  * babel_general_convert_from:
214  * @vtl: The TrackWaypoint Layer to save the data into
215  *   If it is null it signifies that no data is to be processed,
216  *    however the gpsbabel command is still ran as it can be for non-data related options eg:
217  *    for use with the power off command - 'command_off'
218  * @cb: callback that is run upon new data from STDOUT (?)
219  *     (TODO: STDERR would be nice since we usually redirect STDOUT)
220  * @user_data: passed along to cb
221  *
222  * Runs args[0] with the arguments and uses the GPX module
223  * to import the GPX data into layer vt. Assumes that upon
224  * running the command, the data will appear in the (usually
225  * temporary) file name_dst.
226  *
227  * Returns: %TRUE on success
228  */
229 static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
230 {
231   gboolean ret = FALSE;
232   FILE *f = NULL;
233     
234   if (babel_general_convert(cb, args, user_data)) {
235
236     /* No data actually required but still need to have run gpsbabel anyway
237        - eg using the device power command_off */
238     if ( vt == NULL )
239       return TRUE;
240
241     f = g_fopen(name_dst, "r");
242     if (f) {
243       ret = a_gpx_read_file ( vt, f );
244       fclose(f);
245       f = NULL;
246     }
247   }
248     
249   return ret;
250 }
251
252 /**
253  * a_babel_convert_from:
254  * @vt:        The TRW layer to place data into. Duplicate items will be overwritten.
255  * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
256  *             must include the input file type (-i) option.
257  * @cb:        Optional callback function. Same usage as in a_babel_convert().
258  * @user_data: passed along to cb
259  * @not_used:  Must use NULL
260  *
261  * Loads data into a trw layer from a file, using gpsbabel.  This routine is synchronous;
262  * that is, it will block the calling program until the conversion is done. To avoid blocking, call
263  * this routine from a worker thread.
264  *
265  * Returns: %TRUE on success
266  */
267 gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
268 {
269   int i,j;
270   int fd_dst;
271   gchar *name_dst = NULL;
272   gboolean ret = FALSE;
273   gchar *args[64];
274
275   if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
276     g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
277     close(fd_dst);
278
279     if (gpsbabel_loc ) {
280       gchar **sub_args = g_strsplit(babelargs, " ", 0);
281
282       i = 0;
283       if (unbuffer_loc)
284         args[i++] = unbuffer_loc;
285       args[i++] = gpsbabel_loc;
286       for (j = 0; sub_args[j]; j++) {
287         /* some version of gpsbabel can not take extra blank arg */
288         if (sub_args[j][0] != '\0')
289           args[i++] = sub_args[j];
290       }
291       args[i++] = "-o";
292       args[i++] = "gpx";
293       args[i++] = "-f";
294       args[i++] = (char *)from;
295       args[i++] = "-F";
296       args[i++] = name_dst;
297       args[i] = NULL;
298
299       ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
300
301       g_strfreev(sub_args);
302     } else
303       g_critical("gpsbabel not found in PATH");
304     g_remove(name_dst);
305     g_free(name_dst);
306   }
307
308   return ret;
309 }
310
311 /**
312  * a_babel_convert_from_shellcommand:
313  * @vt: The #VikTrwLayer where to insert the collected data
314  * @input_cmd: the command to run
315  * @cb:        Optional callback function. Same usage as in a_babel_convert().
316  * @user_data: passed along to cb
317  * @not_used:  Must use NULL
318  *
319  * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
320  * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
321  *
322  * Uses babel_general_convert_from() to actually run the command. This function
323  * prepares the command and temporary file, and sets up the arguments for bash.
324  */
325 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 )
326 {
327   int fd_dst;
328   gchar *name_dst = NULL;
329   gboolean ret = FALSE;
330   gchar **args;  
331
332   if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
333     g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
334     gchar *shell_command;
335     if ( input_file_type )
336       shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
337         input_cmd, gpsbabel_loc, input_file_type, name_dst);
338     else
339       shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
340
341     g_debug("%s: %s", __FUNCTION__, shell_command);
342     close(fd_dst);
343
344     args = g_malloc(sizeof(gchar *)*4);
345     args[0] = BASH_LOCATION;
346     args[1] = "-c";
347     args[2] = shell_command;
348     args[3] = NULL;
349
350     ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
351     g_free ( args );
352     g_free ( shell_command );
353     g_remove(name_dst);
354     g_free(name_dst);
355   }
356
357   return ret;
358 }
359
360 /**
361  * a_babel_convert_from_url:
362  * @vt: The #VikTrwLayer where to insert the collected data
363  * @url: the URL to fetch
364  * @cb:        Optional callback function. Same usage as in a_babel_convert().
365  * @user_data: passed along to cb
366  * @options:   download options. Maybe NULL.
367  *
368  * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type.
369  * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX.
370  *
371  * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
372  *
373  */
374 gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
375 {
376   // If no download options specified, use defaults:
377   DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL };
378   if ( options )
379     myoptions = *options;
380   gint fd_src;
381   int fetch_ret;
382   gboolean ret = FALSE;
383   gchar *name_src = NULL;
384   gchar *babelargs = NULL;
385
386   g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
387
388   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
389     g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
390     close(fd_src);
391     g_remove(name_src);
392
393     fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL);
394     if (fetch_ret == DOWNLOAD_SUCCESS) {
395       if (input_type != NULL) {
396         babelargs = g_strdup_printf(" -i %s", input_type);
397         ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL );
398       } else {
399         /* Process directly the retrieved file */
400         g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src);
401         FILE *f = g_fopen(name_src, "r");
402         if (f) {
403           ret = a_gpx_read_file ( vt, f );
404           fclose(f);
405           f = NULL;
406         }
407       }
408     }
409     g_remove(name_src);
410     g_free(babelargs);
411     g_free(name_src);
412   }
413
414   return ret;
415 }
416
417 /**
418  * a_babel_convert_from_url_or_shell:
419  * @vt: The #VikTrwLayer where to insert the collected data
420  * @url: the URL to fetch
421  * @cb:        Optional callback function. Same usage as in a_babel_convert().
422  * @user_data: passed along to cb
423  * @options:   download options. Maybe NULL.
424  *
425  * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type.
426  * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX.
427  *
428  * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
429  *
430  */
431 gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
432 {
433   
434   /* Check nature of input */
435   gboolean isUrl = FALSE;
436   int i = 0;
437   for (i = 0 ; PROTOS[i] != NULL ; i++)
438   {
439     const gchar *proto = PROTOS[i];
440     if (strncmp (input, proto, strlen(proto)) == 0)
441     {
442       /* Procotol matches: save result */
443       isUrl = TRUE;
444     }
445   }
446   
447   /* Do the job */
448   if (isUrl)
449     return a_babel_convert_from_url (vt, input, input_type, cb, user_data, options);
450   else
451     return a_babel_convert_from_shellcommand (vt, input, input_type, cb, user_data, options);
452 }
453
454 static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
455 {
456   // Now strips out invisible tracks and waypoints
457   if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) {
458     g_critical("Error exporting to %s", name_src);
459     return FALSE;
460   }
461        
462   return babel_general_convert (cb, args, user_data);
463 }
464
465 /**
466  * a_babel_convert_to:
467  * @vt:             The TRW layer from which data is taken.
468  * @track:          Operate on the individual track if specified. Use NULL when operating on a TRW layer
469  * @babelargs:      A string containing gpsbabel command line options.  In addition to any filters, this string
470  *                 must include the input file type (-i) option.
471  * @to:             Filename or device the data is written to.
472  * @cb:            Optional callback function. Same usage as in a_babel_convert.
473  * @user_data: passed along to cb
474  *
475  * Exports data using gpsbabel.  This routine is synchronous;
476  * that is, it will block the calling program until the conversion is done. To avoid blocking, call
477  * this routine from a worker thread.
478  *
479  * Returns: %TRUE on successful invocation of GPSBabel command
480  */
481 gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
482 {
483   int i,j;
484   int fd_src;
485   gchar *name_src = NULL;
486   gboolean ret = FALSE;
487   gchar *args[64];  
488
489   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
490     g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
491     close(fd_src);
492
493     if (gpsbabel_loc ) {
494       gchar **sub_args = g_strsplit(babelargs, " ", 0);
495
496       i = 0;
497       if (unbuffer_loc)
498         args[i++] = unbuffer_loc;
499       args[i++] = gpsbabel_loc;
500       args[i++] = "-i";
501       args[i++] = "gpx";
502       for (j = 0; sub_args[j]; j++)
503         /* some version of gpsbabel can not take extra blank arg */
504         if (sub_args[j][0] != '\0')
505           args[i++] = sub_args[j];
506       args[i++] = "-f";
507       args[i++] = name_src;
508       args[i++] = "-F";
509       args[i++] = (char *)to;
510       args[i] = NULL;
511
512       ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data );
513
514       g_strfreev(sub_args);
515     } else
516       g_critical("gpsbabel not found in PATH");
517     g_remove(name_src);
518     g_free(name_src);
519   }
520
521   return ret;
522 }
523
524 static void set_mode(BabelMode *mode, gchar *smode)
525 {
526   mode->waypointsRead  = smode[0] == 'r';
527   mode->waypointsWrite = smode[1] == 'w';
528   mode->tracksRead     = smode[2] == 'r';
529   mode->tracksWrite    = smode[3] == 'w';
530   mode->routesRead     = smode[4] == 'r';
531   mode->routesWrite    = smode[5] == 'w';
532 }
533
534 /**
535  * load_feature_parse_line:
536  * 
537  * Load a single feature stored in the given line.
538  */
539 static void load_feature_parse_line (gchar *line)
540 {
541   gchar **tokens = g_strsplit ( line, "\t", 0 );
542   if ( tokens != NULL
543        && tokens[0] != NULL ) {
544     if ( strcmp("serial", tokens[0]) == 0 ) {
545       if ( tokens[1] != NULL
546            && tokens[2] != NULL
547            && tokens[3] != NULL
548            && tokens[4] != NULL ) {
549         BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
550         set_mode (&(device->mode), tokens[1]);
551         device->name = g_strdup (tokens[2]);
552         device->label = g_strndup (tokens[4], 50); // Limit really long label text
553         a_babel_device_list = g_list_append (a_babel_device_list, device);
554         g_debug ("New gpsbabel device: %s, %d%d%d%d%d%d(%s)",
555                         device->name,
556                         device->mode.waypointsRead, device->mode.waypointsWrite,
557                         device->mode.tracksRead, device->mode.tracksWrite,
558                         device->mode.routesRead, device->mode.routesWrite,
559                                 tokens[1]);
560       } else {
561         g_warning ( "Unexpected gpsbabel format string: %s", line);
562       }
563     } else if ( strcmp("file", tokens[0]) == 0 ) {
564       if ( tokens[1] != NULL
565            && tokens[2] != NULL
566            && tokens[3] != NULL
567            && tokens[4] != NULL ) {
568         BabelFile *file = g_malloc ( sizeof (BabelFile) );
569         set_mode (&(file->mode), tokens[1]);
570         file->name = g_strdup (tokens[2]);
571         file->ext = g_strdup (tokens[3]);
572         file->label = g_strdup (tokens[4]);
573         a_babel_file_list = g_list_append (a_babel_file_list, file);
574         g_debug ("New gpsbabel file: %s, %d%d%d%d%d%d(%s)",
575                         file->name,
576                         file->mode.waypointsRead, file->mode.waypointsWrite,
577                         file->mode.tracksRead, file->mode.tracksWrite,
578                         file->mode.routesRead, file->mode.routesWrite,
579                         tokens[1]);
580       } else {
581         g_warning ( "Unexpected gpsbabel format string: %s", line);
582       }
583     } /* else: ignore */
584   } else {
585     g_warning ( "Unexpected gpsbabel format string: %s", line);
586   }
587   g_strfreev ( tokens );
588 }
589
590 static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data)
591 {
592   if (line != NULL)
593     load_feature_parse_line (line);
594 }
595
596 static gboolean load_feature ()
597 {
598   int i;
599   gboolean ret = FALSE;
600   gchar *args[4];  
601
602   if ( gpsbabel_loc ) {
603     i = 0;
604     if ( unbuffer_loc )
605       args[i++] = unbuffer_loc;
606     args[i++] = gpsbabel_loc;
607     args[i++] = "-^3";
608     args[i] = NULL;
609
610     ret = babel_general_convert (load_feature_cb, args, NULL);
611   } else
612     g_critical("gpsbabel not found in PATH");
613
614   return ret;
615 }
616
617 /**
618  * a_babel_init:
619  * 
620  * Initialises babel module.
621  * Mainly check existence of gpsbabel progam
622  * and load all features available in ths version.
623  */
624 void a_babel_init ()
625 {
626   /* TODO allow to set gpsbabel path via command line */
627   gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
628   if ( !gpsbabel_loc )
629     g_critical( "gpsbabel not found in PATH" );
630
631   // Unlikely to package unbuffer on Windows so ATM don't even bother trying
632   // Highly unlikely unbuffer is available on a Windows system otherwise
633 #ifndef WINDOWS
634   unbuffer_loc = g_find_program_in_path( "unbuffer" );
635   if ( !unbuffer_loc )
636     g_warning( "unbuffer not found in PATH" );
637 #endif
638
639   load_feature ();
640 }
641
642 /**
643  * a_babel_uninit:
644  * 
645  * Free resources acquired by a_babel_init.
646  */
647 void a_babel_uninit ()
648 {
649   g_free ( gpsbabel_loc );
650   g_free ( unbuffer_loc );
651
652   if ( a_babel_file_list ) {
653     GList *gl;
654     for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
655       BabelFile *file = gl->data;
656       g_free ( file->name );
657       g_free ( file->ext );
658       g_free ( file->label );
659       g_free ( gl->data );
660     }
661     g_list_free ( a_babel_file_list );
662   }
663
664   if ( a_babel_device_list ) {
665     GList *gl;
666     for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
667       BabelDevice *device = gl->data;
668       g_free ( device->name );
669       g_free ( device->label );
670       g_free ( gl->data );
671     }
672     g_list_free ( a_babel_device_list );
673   }
674
675 }
676
677 /**
678  * a_babel_available:
679  *
680  * Indicates if babel is available or not.
681  *
682  * Returns: true if babel available
683  */
684 gboolean a_babel_available ()
685 {
686   return a_babel_device_list != NULL;
687 }