+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;
+ gchar *babelargs;
+
+ 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, NULL, name_src, NULL);
+
+ g_remove(name_src);
+ g_free(babelargs);
+ g_free(name_src);
+ }
+
+ return ret;
+}
+
+#ifdef WINDOWS
+gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
+{
+ gboolean ret;
+ gchar *cmd;
+ gchar **args2;
+
+ if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
+ g_warning("%s(): error exporting to %s", __FUNCTION__, name_src);
+ return(FALSE);
+ }
+
+ 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);
+ 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_warning( "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;
+}
+/* Windows */
+#else
+/* Posix */
+gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
+{
+ gboolean ret = FALSE;
+ GPid pid;
+ GError *error = NULL;
+ gint babel_stdout;
+
+ if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
+ g_warning("%s(): error exporting to %s", __FUNCTION__, name_src);
+ return(FALSE);
+ }
+
+ if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
+ g_warning("Error : %s", error->message);
+ g_error_free(error);
+ ret = FALSE;
+ } else {
+ gchar line[512];
+ FILE *diag;
+ diag = fdopen(babel_stdout, "r");
+ setvbuf(diag, NULL, _IONBF, 0);
+
+ while (fgets(line, sizeof(line), diag)) {
+ if ( cb )
+ cb(BABEL_DIAG_OUTPUT, line, user_data);
+ }
+ if ( cb )
+ cb(BABEL_DONE, NULL, user_data);
+ fclose(diag);
+ diag = NULL;
+ waitpid(pid, NULL, 0);
+ g_spawn_close_pid(pid);
+
+ ret = TRUE;
+ }
+
+ return ret;
+}
+#endif /* Posix */
+
+gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *to, gpointer user_data )
+{
+ int i,j;
+ int fd_src;
+ gchar *name_src;
+ gboolean ret = FALSE;
+ gchar *args[64];
+
+ if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
+ gchar *gpsbabel_loc;
+ close(fd_src);
+
+ gpsbabel_loc = g_find_program_in_path("gpsbabel");
+
+ if (gpsbabel_loc ) {
+ gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
+ 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_free ( unbuffer_loc );
+ g_strfreev(sub_args);
+ } else
+ g_warning("gpsbabel not found in PATH");
+ g_free(gpsbabel_loc);
+ }
+
+ g_remove(name_src);
+ g_free(name_src);
+ return ret;
+}