+ g_remove(name_dst);
+ g_free(name_dst);
+ }
+
+ return ret;
+}
+
+/**
+ * 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 )
+{
+ // 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;
+ gchar *name_src = NULL;
+ gchar *babelargs = NULL;
+
+ 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);
+
+ 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
+ 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);
+}
+
+/**
+ * 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;
+ gchar *name_src = NULL;
+ gboolean ret = FALSE;
+ 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 ) {
+ gchar **sub_args = g_strsplit(babelargs, " ", 0);
+
+ i = 0;
+ if (unbuffer_loc)
+ args[i++] = unbuffer_loc;
+ args[i++] = gpsbabel_loc;
+ args[i++] = "-i";
+ args[i++] = "gpx";
+ for (j = 0; sub_args[j]; j++)
+ /* some version of gpsbabel can not take extra blank arg */
+ if (sub_args[j][0] != '\0')
+ args[i++] = sub_args[j];
+ args[i++] = "-f";
+ args[i++] = name_src;
+ args[i++] = "-F";
+ args[i++] = (char *)to;
+ args[i] = NULL;
+
+ ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data );
+
+ g_strfreev(sub_args);
+ } else
+ g_critical("gpsbabel not found in PATH");
+ g_remove(name_src);
+ g_free(name_src);
+ }
+
+ return ret;
+}
+
+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';
+}
+
+/**
+ * load_feature_parse_line:
+ *
+ * Load a single feature stored in the given line.
+ */
+static void load_feature_parse_line (gchar *line)
+{
+ gchar **tokens = g_strsplit ( line, "\t", 0 );
+ if ( tokens != NULL
+ && tokens[0] != NULL ) {
+ if ( strcmp("serial", tokens[0]) == 0 ) {
+ if ( tokens[1] != NULL
+ && tokens[2] != NULL
+ && tokens[3] != NULL
+ && tokens[4] != NULL ) {
+ BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
+ 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);
+ } else {
+ g_warning ( "Unexpected gpsbabel format string: %s", line);
+ }
+ } else if ( strcmp("file", tokens[0]) == 0 ) {
+ if ( tokens[1] != NULL
+ && tokens[2] != NULL
+ && tokens[3] != NULL
+ && tokens[4] != NULL ) {
+ BabelFile *file = g_malloc ( sizeof (BabelFile) );
+ 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);
+ } else {
+ g_warning ( "Unexpected gpsbabel format string: %s", line);
+ }
+ } /* else: ignore */
+ } else {
+ g_warning ( "Unexpected gpsbabel format string: %s", line);