+ return ret;
+}
+
+/**
+ * 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().
+ *
+ * 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( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data )
+{
+ int i,j;
+ int fd_dst;
+ gchar *name_dst = NULL;
+ gboolean ret = FALSE;
+ gchar *args[64];
+
+ if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
+ close(fd_dst);
+
+ if (gpsbabel_loc ) {
+ gchar **sub_args = g_strsplit(babelargs, " ", 0);
+
+ i = 0;
+ if (unbuffer_loc)
+ args[i++] = unbuffer_loc;
+ args[i++] = gpsbabel_loc;
+ 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++] = "-o";
+ args[i++] = "gpx";
+ args[i++] = "-f";
+ args[i++] = (char *)from;
+ args[i++] = "-F";
+ args[i++] = name_dst;
+ args[i] = NULL;
+
+ ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
+
+ g_strfreev(sub_args);
+ } else
+ g_error("gpsbabel not found in PATH");
+ g_remove(name_dst);
+ g_free(name_dst);
+ }
+
+ return ret;
+}
+
+/**
+ * a_babel_convert_from_shellcommand:
+ *
+ * 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 )
+{
+ int fd_dst;
+ gchar *name_dst = NULL;
+ gboolean ret = FALSE;
+ gchar **args;
+
+ if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
+ gchar *shell_command;
+ if ( input_file_type )
+ shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
+ input_cmd, gpsbabel_loc, input_file_type, name_dst);
+ else
+ shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
+
+ g_debug("%s: %s", __FUNCTION__, shell_command);
+ close(fd_dst);
+
+ args = g_malloc(sizeof(gchar *)*4);
+ args[0] = BASH_LOCATION;
+ args[1] = "-c";
+ args[2] = shell_command;
+ args[3] = NULL;
+
+ ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
+ g_free ( args );
+ g_free ( shell_command );
+ g_remove(name_dst);
+ g_free(name_dst);
+ }
+
+ return ret;
+}
+
+gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
+{
+ static DownloadMapOptions options = { FALSE, FALSE, NULL, 0, a_check_kml_file};
+ 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) {
+ 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);
+
+ g_remove(name_src);
+ g_free(babelargs);
+ g_free(name_src);
+ }
+
+ return ret;
+}
+
+static gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
+{
+ if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
+ g_error("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 )
+{
+ 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) {
+ 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, cb, args, name_src, user_data );
+
+ g_strfreev(sub_args);
+ } else
+ g_error("gpsbabel not found in PATH");
+ g_remove(name_src);
+ g_free(name_src);