]> git.street.me.uk Git - andy/viking.git/blobdiff - src/babel.c
Fix excluding own self track when creating lists of other tracks.
[andy/viking.git] / src / babel.c
index 3d63400d5018295ddab7114ecb5cfb9843c012f2..93ad52e32cbf00f7d0c73634a1bf66c6ca5ba898 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
  * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com>
+ * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,9 +38,6 @@
 #include "gpx.h"
 #include "babel.h"
 #include <stdio.h>
-#ifdef HAVE_SYS_WAIT_H
-#include <sys/wait.h>
-#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */
 #define BASH_LOCATION "/bin/bash"
 
+/**
+ * List of supported protocols.
+ */
+const gchar *PROTOS[] = { "http://", "https://", "ftp://", NULL };
+
 /**
  * Path to gpsbabel
  */
@@ -70,12 +73,39 @@ GList *a_babel_file_list;
  */
 GList *a_babel_device_list;
 
+/**
+ * Run a function on all file formats supporting a given mode.
+ */
+void a_babel_foreach_file_with_mode (BabelMode mode, GFunc func, gpointer user_data)
+{
+  GList *current;
+  for ( current = g_list_first (a_babel_file_list) ;
+        current != NULL ;
+        current = g_list_next (current) )
+  {
+    BabelFile *currentFile = current->data;
+    /* Check compatibility of modes */
+    gboolean compat = TRUE;
+    if (mode.waypointsRead  && ! currentFile->mode.waypointsRead)  compat = FALSE;
+    if (mode.waypointsWrite && ! currentFile->mode.waypointsWrite) compat = FALSE;
+    if (mode.tracksRead     && ! currentFile->mode.tracksRead)     compat = FALSE;
+    if (mode.tracksWrite    && ! currentFile->mode.tracksWrite)    compat = FALSE;
+    if (mode.routesRead     && ! currentFile->mode.routesRead)     compat = FALSE;
+    if (mode.routesWrite    && ! currentFile->mode.routesWrite)    compat = FALSE;
+    /* Do call */
+    if (compat)
+      func (currentFile, user_data);
+  }
+}
+
 /**
  * a_babel_convert:
  * @vt:        The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs.
  * @babelargs: A string containing gpsbabel command line filter options. No file types or names should
  *             be specified.
  * @cb:        A callback function.
+ * @user_data: passed along to cb
+ * @not_used:  Must use NULL
  *
  * This function modifies data in a trw layer using gpsbabel filters.  This routine is synchronous;
  * that is, it will block the calling program until the conversion is done. To avoid blocking, call
@@ -83,7 +113,7 @@ GList *a_babel_device_list;
  *
  * Returns: %TRUE on success
  */
-gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data )
+gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
 {
   int fd_src;
   FILE *f;
@@ -92,11 +122,12 @@ gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFun
   gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
 
   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
+    g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
     f = fdopen(fd_src, "w");
-    a_gpx_write_file(vt, f);
+    a_gpx_write_file(vt, f, NULL);
     fclose(f);
     f = NULL;
-    ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data );
+    ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used );
     g_remove(name_src);
     g_free(name_src);
   }
@@ -105,68 +136,27 @@ gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFun
   return ret;
 }
 
-#ifdef WINDOWS
-static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
+/**
+ * Perform any cleanup actions once GPSBabel has completed running
+ */
+static void babel_watch ( GPid pid,
+                          gint status,
+                          gpointer user_data )
 {
-  gboolean ret;
-  FILE *f;
-  gchar *cmd;
-  gchar **args2;
-  
-  STARTUPINFO si;
-  PROCESS_INFORMATION pi;
-
-  ZeroMemory( &si, sizeof(si) );
-  ZeroMemory( &pi, sizeof(pi) );
-  si.cb = sizeof(si);
-  si.dwFlags = STARTF_USESHOWWINDOW;
-  si.wShowWindow = SW_HIDE;
-  
-  cmd = g_strjoinv( " ", args);
-  args2 = g_strsplit(cmd, "\\", 0);
-  g_free(cmd);
-  cmd = g_strjoinv( "\\\\", args2);
-  g_free(args2);
-  args2 = g_strsplit(cmd, "/", 0);
-  g_free(cmd);
-  cmd = g_strjoinv( "\\\\", args2);
-
-  if( !CreateProcess(
-             NULL,                   // No module name (use command line).
-        (LPTSTR)cmd,           // Command line.
-        NULL,                   // Process handle not inheritable.
-        NULL,                   // Thread handle not inheritable.
-        FALSE,                  // Set handle inheritance to FALSE.
-        0,                      // No creation flags.
-        NULL,                   // Use parent's environment block.
-        NULL,                   // Use parent's starting directory.
-        &si,                    // Pointer to STARTUPINFO structure.
-        &pi )                   // Pointer to PROCESS_INFORMATION structure.
-    ){
-    g_error ( "CreateProcess failed" );
-    ret = FALSE;
-  }
-  else {
-    WaitForSingleObject(pi.hProcess, INFINITE);
-    WaitForSingleObject(pi.hThread, INFINITE);
-    
-    CloseHandle(pi.hThread);
-    CloseHandle(pi.hProcess);
-    
-    if ( cb )
-      cb(BABEL_DONE, NULL, user_data);
-    
-    ret = TRUE;
-  }
-
-  g_strfreev( args2 );
-  g_free( cmd );
-  return ret;
+  g_spawn_close_pid ( pid );
 }
-/* Windows */
-#else
-/* Posix */
+
+/**
+ * babel_general_convert:
+ * @args: The command line arguments passed to GPSBabel
+ * @cb: callback that is run for each line of GPSBabel output and at completion of the run
+ *      callback may be NULL
+ * @user_data: passed along to cb
+ *
+ * The function to actually invoke the GPSBabel external command
+ *
+ * Returns: %TRUE on successful invocation of GPSBabel command
+ */
 static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
 {
   gboolean ret = FALSE;
@@ -174,8 +164,8 @@ static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointe
   GError *error = NULL;
   gint babel_stdout;
 
-  if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
-    g_error("Async command failed: %s", error->message);
+  if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
+    g_warning ("Async command failed: %s", error->message);
     g_error_free(error);
     ret = FALSE;
   } else {
@@ -193,15 +183,14 @@ static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointe
       cb(BABEL_DONE, NULL, user_data);
     fclose(diag);
     diag = NULL;
-    waitpid(pid, NULL, 0);
-    g_spawn_close_pid(pid);
+
+    g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL );
 
     ret = TRUE;
   }
     
   return ret;
 }
-#endif /* Posix */
 
 /**
  * babel_general_convert_from:
@@ -234,10 +223,9 @@ static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb,
 
     f = g_fopen(name_dst, "r");
     if (f) {
-      a_gpx_read_file ( vt, f );
+      ret = a_gpx_read_file ( vt, f );
       fclose(f);
       f = NULL;
-      ret = TRUE;
     }
   }
     
@@ -250,6 +238,8 @@ static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb,
  * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
  *             must include the input file type (-i) option.
  * @cb:               Optional callback function. Same usage as in a_babel_convert().
+ * @user_data: passed along to cb
+ * @not_used:  Must use NULL
  *
  * Loads data into a trw layer from a file, using gpsbabel.  This routine is synchronous;
  * that is, it will block the calling program until the conversion is done. To avoid blocking, call
@@ -257,7 +247,7 @@ static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb,
  *
  * Returns: %TRUE on success
  */
-gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data )
+gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
 {
   int i,j;
   int fd_dst;
@@ -266,6 +256,7 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
   gchar *args[64];
 
   if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
+    g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
     close(fd_dst);
 
     if (gpsbabel_loc ) {
@@ -292,7 +283,7 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
 
       g_strfreev(sub_args);
     } else
-      g_error("gpsbabel not found in PATH");
+      g_critical("gpsbabel not found in PATH");
     g_remove(name_dst);
     g_free(name_dst);
   }
@@ -302,6 +293,11 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
 
 /**
  * a_babel_convert_from_shellcommand:
+ * @vt: The #VikTrwLayer where to insert the collected data
+ * @input_cmd: the command to run
+ * @cb:               Optional callback function. Same usage as in a_babel_convert().
+ * @user_data: passed along to cb
+ * @not_used:  Must use NULL
  *
  * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
  * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
@@ -309,7 +305,7 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
  * Uses babel_general_convert_from() to actually run the command. This function
  * prepares the command and temporary file, and sets up the arguments for bash.
  */
-gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
+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 )
 {
   int fd_dst;
   gchar *name_dst = NULL;
@@ -317,6 +313,7 @@ gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_
   gchar **args;  
 
   if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
+    g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
     gchar *shell_command;
     if ( input_file_type )
       shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
@@ -343,9 +340,26 @@ gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_
   return ret;
 }
 
-gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
+/**
+ * a_babel_convert_from_url:
+ * @vt: The #VikTrwLayer where to insert the collected data
+ * @url: the URL to fetch
+ * @cb:               Optional callback function. Same usage as in a_babel_convert().
+ * @user_data: passed along to cb
+ * @options:   download options. Maybe NULL.
+ *
+ * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type.
+ * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX.
+ *
+ * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
+ *
+ */
+gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
 {
-  static DownloadMapOptions options = { FALSE, FALSE, NULL, 0, a_check_kml_file};
+  // If no download options specified, use defaults:
+  DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL };
+  if ( options )
+    myoptions = *options;
   gint fd_src;
   int fetch_ret;
   gboolean ret = FALSE;
@@ -355,15 +369,26 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
   g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
 
   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
+    g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
     close(fd_src);
     g_remove(name_src);
 
-    babelargs = g_strdup_printf(" -i %s", input_type);
-
-    fetch_ret = a_http_download_get_url(url, "", name_src, &options, NULL);
-    if (fetch_ret == 0)
-      ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL);
+    fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL);
+    if (fetch_ret == 0) {
+      if (input_type != NULL) {
+        babelargs = g_strdup_printf(" -i %s", input_type);
+        ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL );
+      } else {
+        /* Process directly the retrieved file */
+        g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src);
+        FILE *f = g_fopen(name_src, "r");
+        if (f) {
+          ret = a_gpx_read_file ( vt, f );
+          fclose(f);
+          f = NULL;
+        }
+      }
+    }
     g_remove(name_src);
     g_free(babelargs);
     g_free(name_src);
@@ -372,17 +397,71 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
   return ret;
 }
 
-static gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
+/**
+ * a_babel_convert_from_url_or_shell:
+ * @vt: The #VikTrwLayer where to insert the collected data
+ * @url: the URL to fetch
+ * @cb:               Optional callback function. Same usage as in a_babel_convert().
+ * @user_data: passed along to cb
+ * @options:   download options. Maybe NULL.
+ *
+ * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type.
+ * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX.
+ *
+ * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
+ *
+ */
+gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
 {
-  if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
-    g_error("Error exporting to %s", name_src);
+  
+  /* Check nature of input */
+  gboolean isUrl = FALSE;
+  int i = 0;
+  for (i = 0 ; PROTOS[i] != NULL ; i++)
+  {
+    const gchar *proto = PROTOS[i];
+    if (strncmp (input, proto, strlen(proto)) == 0)
+    {
+      /* Procotol matches: save result */
+      isUrl = TRUE;
+    }
+  }
+  
+  /* Do the job */
+  if (isUrl)
+    return a_babel_convert_from_url (vt, input, input_type, cb, user_data, options);
+  else
+    return a_babel_convert_from_shellcommand (vt, input, input_type, cb, user_data, options);
+}
+
+static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
+{
+  // Now strips out invisible tracks and waypoints
+  if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) {
+    g_critical("Error exporting to %s", name_src);
     return FALSE;
   }
        
   return babel_general_convert (cb, args, user_data);
 }
 
-gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
+/**
+ * a_babel_convert_to:
+ * @vt:             The TRW layer from which data is taken.
+ * @track:          Operate on the individual track if specified. Use NULL when operating on a TRW layer
+ * @babelargs:      A string containing gpsbabel command line options.  In addition to any filters, this string
+ *                 must include the input file type (-i) option.
+ * @to:             Filename or device the data is written to.
+ * @cb:                   Optional callback function. Same usage as in a_babel_convert.
+ * @user_data: passed along to cb
+ *
+ * Exports data using gpsbabel.  This routine is synchronous;
+ * that is, it will block the calling program until the conversion is done. To avoid blocking, call
+ * this routine from a worker thread.
+ *
+ * Returns: %TRUE on successful invocation of GPSBabel command
+ */
+gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
 {
   int i,j;
   int fd_src;
@@ -391,6 +470,7 @@ gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char
   gchar *args[64];  
 
   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
+    g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
     close(fd_src);
 
     if (gpsbabel_loc ) {
@@ -412,11 +492,11 @@ gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char
       args[i++] = (char *)to;
       args[i] = NULL;
 
-      ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
+      ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data );
 
       g_strfreev(sub_args);
     } else
-      g_error("gpsbabel not found in PATH");
+      g_critical("gpsbabel not found in PATH");
     g_remove(name_src);
     g_free(name_src);
   }
@@ -424,18 +504,18 @@ gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char
   return ret;
 }
 
-static void set_mode(BabelMode mode, gchar *smode)
+static void set_mode(BabelMode *mode, gchar *smode)
 {
-  mode.waypointsRead  = smode[0] == 'r';
-  mode.waypointsWrite = smode[1] == 'w';
-  mode.tracksRead     = smode[2] == 'r';
-  mode.tracksWrite    = smode[3] == 'w';
-  mode.routesRead     = smode[4] == 'r';
-  mode.routesWrite    = smode[5] == 'w';
+  mode->waypointsRead  = smode[0] == 'r';
+  mode->waypointsWrite = smode[1] == 'w';
+  mode->tracksRead     = smode[2] == 'r';
+  mode->tracksWrite    = smode[3] == 'w';
+  mode->routesRead     = smode[4] == 'r';
+  mode->routesWrite    = smode[5] == 'w';
 }
 
 /**
- * load_feature:
+ * load_feature_parse_line:
  * 
  * Load a single feature stored in the given line.
  */
@@ -450,11 +530,16 @@ static void load_feature_parse_line (gchar *line)
            && tokens[3] != NULL
            && tokens[4] != NULL ) {
         BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
-        set_mode (device->mode, tokens[1]);
+        set_mode (&(device->mode), tokens[1]);
         device->name = g_strdup (tokens[2]);
-        device->label = g_strdup (tokens[4]);
+        device->label = g_strndup (tokens[4], 50); // Limit really long label text
         a_babel_device_list = g_list_append (a_babel_device_list, device);
-        g_debug ("New gpsbabel device: %s", device->name);
+        g_debug ("New gpsbabel device: %s, %d%d%d%d%d%d(%s)",
+                       device->name,
+                       device->mode.waypointsRead, device->mode.waypointsWrite,
+                       device->mode.tracksRead, device->mode.tracksWrite,
+                       device->mode.routesRead, device->mode.routesWrite,
+                               tokens[1]);
       } else {
         g_warning ( "Unexpected gpsbabel format string: %s", line);
       }
@@ -464,12 +549,17 @@ static void load_feature_parse_line (gchar *line)
            && tokens[3] != NULL
            && tokens[4] != NULL ) {
         BabelFile *file = g_malloc ( sizeof (BabelFile) );
-        set_mode (file->mode, tokens[1]);
+        set_mode (&(file->mode), tokens[1]);
         file->name = g_strdup (tokens[2]);
         file->ext = g_strdup (tokens[3]);
         file->label = g_strdup (tokens[4]);
         a_babel_file_list = g_list_append (a_babel_file_list, file);
-        g_debug ("New gpsbabel file: %s", file->name);
+        g_debug ("New gpsbabel file: %s, %d%d%d%d%d%d(%s)",
+                       file->name,
+                       file->mode.waypointsRead, file->mode.waypointsWrite,
+                       file->mode.tracksRead, file->mode.tracksWrite,
+                       file->mode.routesRead, file->mode.routesWrite,
+                       tokens[1]);
       } else {
         g_warning ( "Unexpected gpsbabel format string: %s", line);
       }
@@ -502,26 +592,79 @@ static gboolean load_feature ()
 
     ret = babel_general_convert (load_feature_cb, args, NULL);
   } else
-    g_error("gpsbabel not found in PATH");
+    g_critical("gpsbabel not found in PATH");
 
   return ret;
 }
 
+/**
+ * a_babel_init:
+ * 
+ * Initialises babel module.
+ * Mainly check existence of gpsbabel progam
+ * and load all features available in ths version.
+ */
 void a_babel_init ()
 {
   /* TODO allow to set gpsbabel path via command line */
   gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
   if ( !gpsbabel_loc )
-    g_error( "gpsbabel not found in PATH" );
+    g_critical( "gpsbabel not found in PATH" );
+
+  // Unlikely to package unbuffer on Windows so ATM don't even bother trying
+  // Highly unlikely unbuffer is available on a Windows system otherwise
+#ifndef WINDOWS
   unbuffer_loc = g_find_program_in_path( "unbuffer" );
   if ( !unbuffer_loc )
     g_warning( "unbuffer not found in PATH" );
+#endif
 
   load_feature ();
 }
 
+/**
+ * a_babel_uninit:
+ * 
+ * Free resources acquired by a_babel_init.
+ */
 void a_babel_uninit ()
 {
   g_free ( gpsbabel_loc );
   g_free ( unbuffer_loc );
+
+  if ( a_babel_file_list ) {
+    GList *gl;
+    for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
+      BabelFile *file = gl->data;
+      g_free ( file->name );
+      g_free ( file->ext );
+      g_free ( file->label );
+      g_free ( gl->data );
+    }
+    g_list_free ( a_babel_file_list );
+  }
+
+  if ( a_babel_device_list ) {
+    GList *gl;
+    for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
+      BabelDevice *device = gl->data;
+      g_free ( device->name );
+      g_free ( device->label );
+      g_free ( gl->data );
+    }
+    g_list_free ( a_babel_device_list );
+  }
+
+}
+
+/**
+ * a_babel_available:
+ *
+ * Indicates if babel is available or not.
+ *
+ * Returns: true if babel available
+ */
+gboolean a_babel_available ()
+{
+  return a_babel_device_list != NULL;
 }