]> git.street.me.uk Git - andy/viking.git/blobdiff - src/babel.c
Ensure calculation of the waypoint bounds even when the layer is not currently visible.
[andy/viking.git] / src / babel.c
index 93ad52e32cbf00f7d0c73634a1bf66c6ca5ba898..24a9dae4f752d16617659858ceffc8ba263777b0 100644 (file)
@@ -4,6 +4,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>
+ * Copyright (C) 2015, Rob Norris <rw_norris@hotmail.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
@@ -25,9 +26,8 @@
  * SECTION:babel
  * @short_description: running external programs and redirecting to TRWLayers.
  *
- * GPSBabel may not be necessary for everything -- for instance,
- *   use a_babel_convert_from_shellcommand() with input_file_type == %NULL
- *   for an external program that outputs GPX.
+ * GPSBabel may not be necessary for everything,
+ *  one can use shell_command option but this will be OS platform specific
  */
 
 #ifdef HAVE_CONFIG_H
@@ -37,6 +37,7 @@
 #include "viking.h"
 #include "gpx.h"
 #include "babel.h"
+#include "preferences.h"
 #include <stdio.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #include <string.h>
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <glib/gi18n.h>
 
 /* 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
  */
@@ -98,6 +95,30 @@ void a_babel_foreach_file_with_mode (BabelMode mode, GFunc func, gpointer user_d
   }
 }
 
+/**
+ * a_babel_foreach_file_read_any:
+ * @func:      The function to be called on any file format with a read method
+ * @user_data: Data passed into the function
+ *
+ * Run a function on all file formats with any kind of read method
+ *  (which is almost all but not quite - e.g. with GPSBabel v1.4.4 - PalmDoc is write only waypoints)
+ */
+void a_babel_foreach_file_read_any (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;
+    // Call function when any read mode found
+    if ( currentFile->mode.waypointsRead ||
+         currentFile->mode.tracksRead ||
+         currentFile->mode.routesRead)
+      func (currentFile, user_data);
+  }
+}
+
 /**
  * a_babel_convert:
  * @vt:        The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs.
@@ -115,20 +136,14 @@ void a_babel_foreach_file_with_mode (BabelMode mode, GFunc func, gpointer user_d
  */
 gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
 {
-  int fd_src;
-  FILE *f;
-  gchar *name_src = NULL;
   gboolean ret = FALSE;
   gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
+  gchar *name_src = a_gpx_write_tmp_file ( vt, 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, NULL);
-    fclose(f);
-    f = NULL;
-    ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used );
-    g_remove(name_src);
+  if ( name_src ) {
+    ProcessOptions po = { bargs, name_src, NULL, NULL, NULL };
+    ret = a_babel_convert_from ( vt, &po, cb, user_data, not_used );
+    (void)g_remove(name_src);
     g_free(name_src);
   }
 
@@ -164,6 +179,11 @@ static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointe
   GError *error = NULL;
   gint babel_stdout;
 
+  if ( vik_debug ) {
+    for ( guint i=0; args[i]; i++ )
+      g_debug ("%s: %s", __FUNCTION__, args[i] );
+  }
+
   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);
@@ -233,13 +253,15 @@ static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb,
 }
 
 /**
- * a_babel_convert_from:
- * @vt:        The TRW layer to place data into. Duplicate items will be overwritten.
- * @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
+ * a_babel_convert_from_filter:
+ * @vt:           The TRW layer to place data into. Duplicate items will be overwritten.
+ * @babelargs:    A string containing gpsbabel command line options. This string
+ *                must include the input file type (-i) option.
+ * @from          the file name to convert from
+ * @babelfilters: A string containing gpsbabel filter command line options 
+ * @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
@@ -247,7 +269,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, gpointer not_used )
+gboolean a_babel_convert_from_filter( VikTrwLayer *vt, const char *babelargs, const char *from, const char *babelfilters, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
 {
   int i,j;
   int fd_dst;
@@ -261,6 +283,7 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
 
     if (gpsbabel_loc ) {
       gchar **sub_args = g_strsplit(babelargs, " ", 0);
+      gchar **sub_filters = NULL;
 
       i = 0;
       if (unbuffer_loc)
@@ -271,10 +294,18 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
         if (sub_args[j][0] != '\0')
           args[i++] = sub_args[j];
       }
-      args[i++] = "-o";
-      args[i++] = "gpx";
       args[i++] = "-f";
       args[i++] = (char *)from;
+      if (babelfilters) {
+        sub_filters = g_strsplit(babelfilters, " ", 0);
+        for (j = 0; sub_filters[j]; j++) {
+          /* some version of gpsbabel can not take extra blank arg */
+          if (sub_filters[j][0] != '\0')
+            args[i++] = sub_filters[j];
+        }
+      }
+      args[i++] = "-o";
+      args[i++] = "gpx";
       args[i++] = "-F";
       args[i++] = name_dst;
       args[i] = NULL;
@@ -282,9 +313,11 @@ gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const cha
       ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
 
       g_strfreev(sub_args);
+      if (sub_filters)
+          g_strfreev(sub_filters);
     } else
       g_critical("gpsbabel not found in PATH");
-    g_remove(name_dst);
+    (void)g_remove(name_dst);
     g_free(name_dst);
   }
 
@@ -295,6 +328,7 @@ 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
+ * @input_file_type:
  * @cb:               Optional callback function. Same usage as in a_babel_convert().
  * @user_data: passed along to cb
  * @not_used:  Must use NULL
@@ -333,7 +367,7 @@ gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_
     ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
     g_free ( args );
     g_free ( shell_command );
-    g_remove(name_dst);
+    (void)g_remove(name_dst);
     g_free(name_dst);
   }
 
@@ -341,20 +375,22 @@ gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_
 }
 
 /**
- * a_babel_convert_from_url:
+ * a_babel_convert_from_url_filter:
  * @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.
+ * @input_type:   If input_type is %NULL, input must be GPX.
+ * @babelfilters: The filter arguments to pass to gpsbabel
+ * @cb:                  Optional callback function. Same usage as in a_babel_convert().
+ * @user_data:    Passed along to cb
+ * @options:      Download options. If %NULL then default download options will be used.
  *
- * 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.
+ * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_type.
+ * If input_type and babelfilters are %NULL, gpsbabel is not used.
  *
  * 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 )
+gboolean 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 )
 {
   // If no download options specified, use defaults:
   DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL };
@@ -371,13 +407,13 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
   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);
+    (void)g_remove(name_src);
 
     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 );
+    if (fetch_ret == DOWNLOAD_SUCCESS) {
+      if (input_type != NULL || babelfilters != NULL) {
+        babelargs = (input_type) ? g_strdup_printf(" -i %s", input_type) : g_strdup("");
+        ret = a_babel_convert_from_filter( vt, babelargs, name_src, babelfilters, NULL, NULL, NULL );
       } else {
         /* Process directly the retrieved file */
         g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src);
@@ -389,7 +425,7 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
         }
       }
     }
-    g_remove(name_src);
+    (void)util_remove(name_src);
     g_free(babelargs);
     g_free(name_src);
   }
@@ -398,40 +434,29 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
 }
 
 /**
- * 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.
+ * a_babel_convert_from:
+ * @vt:               The TRW layer to place data into. Duplicate items will be overwritten.
+ * @process_options:  The options to control the appropriate processing function. See #ProcessOptions for more detail
+ * @cb:               Optional callback function. Same usage as in a_babel_convert().
+ * @user_data:        passed along to cb
+ * @download_options: If downloading from a URL use these options (may be NULL)
  *
- * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
+ * 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
+ * this routine from a worker thread.
  *
+ * Returns: %TRUE on success
  */
-gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
+gboolean a_babel_convert_from ( VikTrwLayer *vt, ProcessOptions *process_options, BabelStatusFunc cb, gpointer user_data, gpointer download_options )
 {
-  
-  /* 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);
+  if ( !process_options ) return FALSE;
+  if ( process_options->url )
+    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 );
+  if ( process_options->babelargs )
+    return a_babel_convert_from_filter ( vt, process_options->babelargs, process_options->filename, process_options->babel_filters, cb, user_data, download_options );
+  if ( process_options->shell_command )
+    return a_babel_convert_from_shellcommand ( vt, process_options->shell_command, process_options->filename, cb, user_data, download_options );
+  return FALSE;
 }
 
 static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
@@ -497,7 +522,7 @@ gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babel
       g_strfreev(sub_args);
     } else
       g_critical("gpsbabel not found in PATH");
-    g_remove(name_src);
+    (void)g_remove(name_src);
     g_free(name_src);
   }
 
@@ -597,19 +622,52 @@ static gboolean load_feature ()
   return ret;
 }
 
+static VikLayerParam prefs[] = {
+  { 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,
+      N_("Allow setting the specific instance of GPSBabel. You must restart Viking for this value to take effect."), NULL, NULL, NULL },
+};
+
 /**
  * a_babel_init:
  * 
+ * Just setup preferences first
+ */
+void a_babel_init ()
+{
+  // Set the defaults
+  VikLayerParamData vlpd;
+#ifdef WINDOWS
+  // Basic guesses - could use %ProgramFiles% but this is simpler:
+  if ( g_file_test ( "C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe", G_FILE_TEST_EXISTS ) )
+    // 32 bit location on a 64 bit system
+    vlpd.s = "C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe";
+  else
+    vlpd.s = "C:\\Program Files\\GPSBabel\\gpsbabel.exe";
+#else
+  vlpd.s = "gpsbabel";
+#endif
+  a_preferences_register(&prefs[0], vlpd, VIKING_PREFERENCES_IO_GROUP_KEY);
+}
+
+/**
+ * a_babel_post_init:
+ *
  * Initialises babel module.
  * Mainly check existence of gpsbabel progam
- * and load all features available in ths version.
+ * and load all features available in that version.
  */
-void a_babel_init ()
+void a_babel_post_init ()
 {
-  /* TODO allow to set gpsbabel path via command line */
-  gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
-  if ( !gpsbabel_loc )
-    g_critical( "gpsbabel not found in PATH" );
+  // Read the current preference
+  const gchar *gpsbabel = a_preferences_get(VIKING_PREFERENCES_IO_NAMESPACE "gpsbabel")->s;
+  // If setting is still the UNIX default then lookup in the path - otherwise attempt to use the specified value directly.
+  if ( g_strcmp0 ( gpsbabel, "gpsbabel" ) == 0 ) {
+    gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
+    if ( !gpsbabel_loc )
+      g_critical( "gpsbabel not found in PATH" );
+  }
+  else
+    gpsbabel_loc = (gchar*)gpsbabel;
 
   // Unlikely to package unbuffer on Windows so ATM don't even bother trying
   // Highly unlikely unbuffer is available on a Windows system otherwise