*
* 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
#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
*/
* @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
*
* 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;
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);
}
gint babel_stdout;
if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
- g_error("Async command failed: %s", error->message);
+ g_warning ("Async command failed: %s", error->message);
g_error_free(error);
ret = FALSE;
} else {
* 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
*
* 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;
* @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)
* 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;
* @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
*
- * Download the file pointed by the URL.
*/
-gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
+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, NULL};
+ // 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;
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);
return ret;
}
+/**
+ * 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 )
+{
+
+ /* 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
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';
}
/**
&& 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_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);
}
&& 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);
}
gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
if ( !gpsbabel_loc )
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 ();
}