]>
Commit | Line | Data |
---|---|---|
1d1bc3c1 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
a482007a | 5 | * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com> |
7f95fd54 | 6 | * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
1d1bc3c1 EB |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
072cbb13 GB |
24 | /** |
25 | * SECTION:babel | |
26 | * @short_description: running external programs and redirecting to TRWLayers. | |
27 | * | |
28c82d8b | 28 | * GPSBabel may not be necessary for everything -- for instance, |
072cbb13 | 29 | * use a_babel_convert_from_shellcommand() with input_file_type == %NULL |
28c82d8b EB |
30 | * for an external program that outputs GPX. |
31 | */ | |
32 | ||
45acf79e MA |
33 | #ifdef HAVE_CONFIG_H |
34 | #include "config.h" | |
35 | #endif | |
36 | ||
1d1bc3c1 EB |
37 | #include "viking.h" |
38 | #include "gpx.h" | |
39 | #include "babel.h" | |
8c060406 | 40 | #include <stdio.h> |
45acf79e MA |
41 | #ifdef HAVE_UNISTD_H |
42 | #include <unistd.h> | |
43 | #endif | |
79e0f36e | 44 | #include <string.h> |
45acf79e MA |
45 | #include <glib.h> |
46 | #include <glib/gstdio.h> | |
1d1bc3c1 | 47 | |
072cbb13 | 48 | /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */ |
8cf048bd EB |
49 | #define BASH_LOCATION "/bin/bash" |
50 | ||
7f95fd54 GB |
51 | /** |
52 | * List of supported protocols. | |
53 | */ | |
54 | const gchar *PROTOS[] = { "http://", "https://", "ftp://", NULL }; | |
55 | ||
18ec873d GB |
56 | /** |
57 | * Path to gpsbabel | |
58 | */ | |
59 | static gchar *gpsbabel_loc = NULL; | |
60 | ||
61 | /** | |
62 | * Path to unbuffer | |
63 | */ | |
64 | static gchar *unbuffer_loc = NULL; | |
65 | ||
79e0f36e GB |
66 | /** |
67 | * List of file formats supported by gpsbabel. | |
68 | */ | |
69 | GList *a_babel_file_list; | |
70 | ||
71 | /** | |
72 | * List of device supported by gpsbabel. | |
73 | */ | |
74 | GList *a_babel_device_list; | |
75 | ||
b666a8ba GB |
76 | /** |
77 | * Run a function on all file formats supporting a given mode. | |
78 | */ | |
79 | void a_babel_foreach_file_with_mode (BabelMode mode, GFunc func, gpointer user_data) | |
80 | { | |
81 | GList *current; | |
82 | for ( current = g_list_first (a_babel_file_list) ; | |
83 | current != NULL ; | |
84 | current = g_list_next (current) ) | |
85 | { | |
86 | BabelFile *currentFile = current->data; | |
87 | /* Check compatibility of modes */ | |
88 | gboolean compat = TRUE; | |
89 | if (mode.waypointsRead && ! currentFile->mode.waypointsRead) compat = FALSE; | |
90 | if (mode.waypointsWrite && ! currentFile->mode.waypointsWrite) compat = FALSE; | |
91 | if (mode.tracksRead && ! currentFile->mode.tracksRead) compat = FALSE; | |
92 | if (mode.tracksWrite && ! currentFile->mode.tracksWrite) compat = FALSE; | |
93 | if (mode.routesRead && ! currentFile->mode.routesRead) compat = FALSE; | |
94 | if (mode.routesWrite && ! currentFile->mode.routesWrite) compat = FALSE; | |
95 | /* Do call */ | |
96 | if (compat) | |
97 | func (currentFile, user_data); | |
98 | } | |
99 | } | |
100 | ||
072cbb13 GB |
101 | /** |
102 | * a_babel_convert: | |
103 | * @vt: The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs. | |
104 | * @babelargs: A string containing gpsbabel command line filter options. No file types or names should | |
105 | * be specified. | |
106 | * @cb: A callback function. | |
ed691ed1 RN |
107 | * @user_data: passed along to cb |
108 | * @not_used: Must use NULL | |
072cbb13 GB |
109 | * |
110 | * This function modifies data in a trw layer using gpsbabel filters. This routine is synchronous; | |
111 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
112 | * this routine from a worker thread. | |
113 | * | |
114 | * Returns: %TRUE on success | |
115 | */ | |
ed691ed1 | 116 | gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
1d1bc3c1 EB |
117 | { |
118 | int fd_src; | |
119 | FILE *f; | |
18ec873d | 120 | gchar *name_src = NULL; |
1d1bc3c1 | 121 | gboolean ret = FALSE; |
3adc68b0 | 122 | gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL); |
1d1bc3c1 | 123 | |
2ecf5998 | 124 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 125 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
1d1bc3c1 | 126 | f = fdopen(fd_src, "w"); |
208d2084 | 127 | a_gpx_write_file(vt, f, NULL); |
1d1bc3c1 | 128 | fclose(f); |
8c060406 | 129 | f = NULL; |
ed691ed1 | 130 | ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used ); |
18ec873d GB |
131 | g_remove(name_src); |
132 | g_free(name_src); | |
1d1bc3c1 EB |
133 | } |
134 | ||
135 | g_free(bargs); | |
1d1bc3c1 EB |
136 | return ret; |
137 | } | |
138 | ||
c3ac9df8 RN |
139 | /** |
140 | * Perform any cleanup actions once GPSBabel has completed running | |
141 | */ | |
142 | static void babel_watch ( GPid pid, | |
143 | gint status, | |
144 | gpointer user_data ) | |
70548902 | 145 | { |
c3ac9df8 | 146 | g_spawn_close_pid ( pid ); |
70548902 | 147 | } |
c3ac9df8 RN |
148 | |
149 | /** | |
150 | * babel_general_convert: | |
151 | * @args: The command line arguments passed to GPSBabel | |
152 | * @cb: callback that is run for each line of GPSBabel output and at completion of the run | |
153 | * callback may be NULL | |
154 | * @user_data: passed along to cb | |
155 | * | |
156 | * The function to actually invoke the GPSBabel external command | |
157 | * | |
158 | * Returns: %TRUE on successful invocation of GPSBabel command | |
159 | */ | |
883e5ed6 | 160 | static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data ) |
8cf048bd | 161 | { |
2ecf5998 | 162 | gboolean ret = FALSE; |
8cf048bd | 163 | GPid pid; |
6c641b1a MA |
164 | GError *error = NULL; |
165 | gint babel_stdout; | |
8cf048bd | 166 | |
c3ac9df8 | 167 | if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) { |
f2131761 | 168 | g_warning ("Async command failed: %s", error->message); |
6c641b1a | 169 | g_error_free(error); |
8cf048bd EB |
170 | ret = FALSE; |
171 | } else { | |
2b756ea0 | 172 | |
8cf048bd EB |
173 | gchar line[512]; |
174 | FILE *diag; | |
175 | diag = fdopen(babel_stdout, "r"); | |
176 | setvbuf(diag, NULL, _IONBF, 0); | |
177 | ||
178 | while (fgets(line, sizeof(line), diag)) { | |
7b3479e3 EB |
179 | if ( cb ) |
180 | cb(BABEL_DIAG_OUTPUT, line, user_data); | |
8cf048bd | 181 | } |
7b3479e3 EB |
182 | if ( cb ) |
183 | cb(BABEL_DONE, NULL, user_data); | |
8cf048bd | 184 | fclose(diag); |
8c060406 | 185 | diag = NULL; |
c3ac9df8 RN |
186 | |
187 | g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL ); | |
8cf048bd | 188 | |
2634ad33 GB |
189 | ret = TRUE; |
190 | } | |
191 | ||
192 | return ret; | |
193 | } | |
2634ad33 GB |
194 | |
195 | /** | |
196 | * babel_general_convert_from: | |
733949a2 RN |
197 | * @vtl: The TrackWaypoint Layer to save the data into |
198 | * If it is null it signifies that no data is to be processed, | |
199 | * however the gpsbabel command is still ran as it can be for non-data related options eg: | |
200 | * for use with the power off command - 'command_off' | |
2634ad33 GB |
201 | * @cb: callback that is run upon new data from STDOUT (?) |
202 | * (TODO: STDERR would be nice since we usually redirect STDOUT) | |
203 | * @user_data: passed along to cb | |
204 | * | |
205 | * Runs args[0] with the arguments and uses the GPX module | |
206 | * to import the GPX data into layer vt. Assumes that upon | |
207 | * running the command, the data will appear in the (usually | |
208 | * temporary) file name_dst. | |
209 | * | |
210 | * Returns: %TRUE on success | |
211 | */ | |
212 | static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data ) | |
213 | { | |
214 | gboolean ret = FALSE; | |
215 | FILE *f = NULL; | |
216 | ||
883e5ed6 | 217 | if (babel_general_convert(cb, args, user_data)) { |
2634ad33 | 218 | |
733949a2 RN |
219 | /* No data actually required but still need to have run gpsbabel anyway |
220 | - eg using the device power command_off */ | |
221 | if ( vt == NULL ) | |
222 | return TRUE; | |
223 | ||
8c060406 MA |
224 | f = g_fopen(name_dst, "r"); |
225 | if (f) { | |
efe9a9c3 | 226 | ret = a_gpx_read_file ( vt, f ); |
8c060406 MA |
227 | fclose(f); |
228 | f = NULL; | |
2ecf5998 | 229 | } |
8cf048bd EB |
230 | } |
231 | ||
232 | return ret; | |
233 | } | |
234 | ||
072cbb13 GB |
235 | /** |
236 | * a_babel_convert_from: | |
237 | * @vt: The TRW layer to place data into. Duplicate items will be overwritten. | |
238 | * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string | |
239 | * must include the input file type (-i) option. | |
240 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
ce4f6212 | 241 | * @user_data: passed along to cb |
ed691ed1 | 242 | * @not_used: Must use NULL |
072cbb13 GB |
243 | * |
244 | * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous; | |
245 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
246 | * this routine from a worker thread. | |
247 | * | |
248 | * Returns: %TRUE on success | |
249 | */ | |
ed691ed1 | 250 | gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
1d1bc3c1 | 251 | { |
f3cd9987 | 252 | int i,j; |
1d1bc3c1 | 253 | int fd_dst; |
18ec873d | 254 | gchar *name_dst = NULL; |
1d1bc3c1 | 255 | gboolean ret = FALSE; |
f3cd9987 | 256 | gchar *args[64]; |
1d1bc3c1 | 257 | |
2ecf5998 | 258 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) { |
e45b3da1 | 259 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst); |
1d1bc3c1 EB |
260 | close(fd_dst); |
261 | ||
92255687 | 262 | if (gpsbabel_loc ) { |
f3cd9987 QT |
263 | gchar **sub_args = g_strsplit(babelargs, " ", 0); |
264 | ||
265 | i = 0; | |
266 | if (unbuffer_loc) | |
267 | args[i++] = unbuffer_loc; | |
268 | args[i++] = gpsbabel_loc; | |
e208e3c0 QT |
269 | for (j = 0; sub_args[j]; j++) { |
270 | /* some version of gpsbabel can not take extra blank arg */ | |
271 | if (sub_args[j][0] != '\0') | |
272 | args[i++] = sub_args[j]; | |
273 | } | |
f3cd9987 QT |
274 | args[i++] = "-o"; |
275 | args[i++] = "gpx"; | |
e208e3c0 | 276 | args[i++] = "-f"; |
8398d878 | 277 | args[i++] = (char *)from; |
e208e3c0 | 278 | args[i++] = "-F"; |
f3cd9987 QT |
279 | args[i++] = name_dst; |
280 | args[i] = NULL; | |
281 | ||
282 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); | |
283 | ||
f3cd9987 | 284 | g_strfreev(sub_args); |
374b7f45 | 285 | } else |
24ff9c7b | 286 | g_critical("gpsbabel not found in PATH"); |
18ec873d GB |
287 | g_remove(name_dst); |
288 | g_free(name_dst); | |
1d1bc3c1 EB |
289 | } |
290 | ||
1d1bc3c1 EB |
291 | return ret; |
292 | } | |
8cf048bd | 293 | |
072cbb13 GB |
294 | /** |
295 | * a_babel_convert_from_shellcommand: | |
ce4f6212 GB |
296 | * @vt: The #VikTrwLayer where to insert the collected data |
297 | * @input_cmd: the command to run | |
298 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
299 | * @user_data: passed along to cb | |
ed691ed1 | 300 | * @not_used: Must use NULL |
072cbb13 GB |
301 | * |
302 | * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type. | |
303 | * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc) | |
28c82d8b | 304 | * |
072cbb13 | 305 | * Uses babel_general_convert_from() to actually run the command. This function |
28c82d8b EB |
306 | * prepares the command and temporary file, and sets up the arguments for bash. |
307 | */ | |
ed691ed1 | 308 | 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 ) |
8cf048bd EB |
309 | { |
310 | int fd_dst; | |
18ec873d | 311 | gchar *name_dst = NULL; |
8cf048bd EB |
312 | gboolean ret = FALSE; |
313 | gchar **args; | |
314 | ||
2ecf5998 | 315 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) { |
e45b3da1 | 316 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst); |
28c82d8b EB |
317 | gchar *shell_command; |
318 | if ( input_file_type ) | |
18ec873d GB |
319 | shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s", |
320 | input_cmd, gpsbabel_loc, input_file_type, name_dst); | |
28c82d8b EB |
321 | else |
322 | shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst); | |
323 | ||
29a5a545 | 324 | g_debug("%s: %s", __FUNCTION__, shell_command); |
8cf048bd EB |
325 | close(fd_dst); |
326 | ||
327 | args = g_malloc(sizeof(gchar *)*4); | |
328 | args[0] = BASH_LOCATION; | |
329 | args[1] = "-c"; | |
330 | args[2] = shell_command; | |
331 | args[3] = NULL; | |
332 | ||
7b3479e3 | 333 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); |
8cf048bd EB |
334 | g_free ( args ); |
335 | g_free ( shell_command ); | |
18ec873d GB |
336 | g_remove(name_dst); |
337 | g_free(name_dst); | |
8cf048bd EB |
338 | } |
339 | ||
8cf048bd EB |
340 | return ret; |
341 | } | |
342 | ||
ce4f6212 GB |
343 | /** |
344 | * a_babel_convert_from_url: | |
345 | * @vt: The #VikTrwLayer where to insert the collected data | |
346 | * @url: the URL to fetch | |
347 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
348 | * @user_data: passed along to cb | |
ed691ed1 | 349 | * @options: download options. Maybe NULL. |
ce4f6212 | 350 | * |
cc2e600e GB |
351 | * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type. |
352 | * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX. | |
ed691ed1 RN |
353 | * |
354 | * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX | |
355 | * | |
ce4f6212 | 356 | */ |
ed691ed1 | 357 | gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options ) |
533bbf34 | 358 | { |
ed691ed1 | 359 | // If no download options specified, use defaults: |
a3697549 | 360 | DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL }; |
ed691ed1 RN |
361 | if ( options ) |
362 | myoptions = *options; | |
533bbf34 | 363 | gint fd_src; |
8bd9fe17 GB |
364 | int fetch_ret; |
365 | gboolean ret = FALSE; | |
18ec873d GB |
366 | gchar *name_src = NULL; |
367 | gchar *babelargs = NULL; | |
533bbf34 MA |
368 | |
369 | g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url); | |
370 | ||
8bd9fe17 | 371 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 372 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
533bbf34 MA |
373 | close(fd_src); |
374 | g_remove(name_src); | |
375 | ||
ed691ed1 | 376 | fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL); |
cc2e600e GB |
377 | if (fetch_ret == 0) { |
378 | if (input_type != NULL) { | |
379 | babelargs = g_strdup_printf(" -i %s", input_type); | |
ed691ed1 | 380 | ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL ); |
cc2e600e GB |
381 | } else { |
382 | /* Process directly the retrieved file */ | |
383 | g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src); | |
384 | FILE *f = g_fopen(name_src, "r"); | |
385 | if (f) { | |
386 | ret = a_gpx_read_file ( vt, f ); | |
387 | fclose(f); | |
388 | f = NULL; | |
389 | } | |
390 | } | |
391 | } | |
533bbf34 MA |
392 | g_remove(name_src); |
393 | g_free(babelargs); | |
394 | g_free(name_src); | |
395 | } | |
396 | ||
397 | return ret; | |
398 | } | |
399 | ||
7f95fd54 GB |
400 | /** |
401 | * a_babel_convert_from_url_or_shell: | |
402 | * @vt: The #VikTrwLayer where to insert the collected data | |
403 | * @url: the URL to fetch | |
404 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
405 | * @user_data: passed along to cb | |
406 | * @options: download options. Maybe NULL. | |
407 | * | |
408 | * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type. | |
409 | * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX. | |
410 | * | |
411 | * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX | |
412 | * | |
413 | */ | |
414 | gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options ) | |
415 | { | |
416 | ||
417 | /* Check nature of input */ | |
418 | gboolean isUrl = FALSE; | |
419 | int i = 0; | |
420 | for (i = 0 ; PROTOS[i] != NULL ; i++) | |
421 | { | |
422 | const gchar *proto = PROTOS[i]; | |
423 | if (strncmp (input, proto, strlen(proto)) == 0) | |
424 | { | |
425 | /* Procotol matches: save result */ | |
426 | isUrl = TRUE; | |
427 | } | |
428 | } | |
429 | ||
430 | /* Do the job */ | |
431 | if (isUrl) | |
432 | return a_babel_convert_from_url (vt, input, input_type, cb, user_data, options); | |
433 | else | |
434 | return a_babel_convert_from_shellcommand (vt, input, input_type, cb, user_data, options); | |
435 | } | |
436 | ||
6f94db6d | 437 | static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data ) |
70548902 | 438 | { |
208d2084 RN |
439 | // Now strips out invisible tracks and waypoints |
440 | if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) { | |
24ff9c7b | 441 | g_critical("Error exporting to %s", name_src); |
18ec873d | 442 | return FALSE; |
70548902 MA |
443 | } |
444 | ||
883e5ed6 | 445 | return babel_general_convert (cb, args, user_data); |
b364d6bc QT |
446 | } |
447 | ||
6f94db6d RN |
448 | /** |
449 | * a_babel_convert_to: | |
ce4f6212 GB |
450 | * @vt: The TRW layer from which data is taken. |
451 | * @track: Operate on the individual track if specified. Use NULL when operating on a TRW layer | |
452 | * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string | |
6f94db6d | 453 | * must include the input file type (-i) option. |
ce4f6212 GB |
454 | * @to: Filename or device the data is written to. |
455 | * @cb: Optional callback function. Same usage as in a_babel_convert. | |
456 | * @user_data: passed along to cb | |
6f94db6d RN |
457 | * |
458 | * Exports data using gpsbabel. This routine is synchronous; | |
459 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
460 | * this routine from a worker thread. | |
461 | * | |
462 | * Returns: %TRUE on successful invocation of GPSBabel command | |
463 | */ | |
464 | gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data ) | |
b364d6bc | 465 | { |
f3cd9987 | 466 | int i,j; |
b364d6bc | 467 | int fd_src; |
18ec873d | 468 | gchar *name_src = NULL; |
b364d6bc | 469 | gboolean ret = FALSE; |
f3cd9987 | 470 | gchar *args[64]; |
b364d6bc | 471 | |
2ecf5998 | 472 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 473 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
b364d6bc QT |
474 | close(fd_src); |
475 | ||
b364d6bc | 476 | if (gpsbabel_loc ) { |
f3cd9987 QT |
477 | gchar **sub_args = g_strsplit(babelargs, " ", 0); |
478 | ||
479 | i = 0; | |
480 | if (unbuffer_loc) | |
481 | args[i++] = unbuffer_loc; | |
482 | args[i++] = gpsbabel_loc; | |
483 | args[i++] = "-i"; | |
484 | args[i++] = "gpx"; | |
485 | for (j = 0; sub_args[j]; j++) | |
e208e3c0 QT |
486 | /* some version of gpsbabel can not take extra blank arg */ |
487 | if (sub_args[j][0] != '\0') | |
488 | args[i++] = sub_args[j]; | |
489 | args[i++] = "-f"; | |
f3cd9987 | 490 | args[i++] = name_src; |
e208e3c0 | 491 | args[i++] = "-F"; |
8398d878 | 492 | args[i++] = (char *)to; |
f3cd9987 QT |
493 | args[i] = NULL; |
494 | ||
6f94db6d | 495 | ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data ); |
f3cd9987 | 496 | |
f3cd9987 | 497 | g_strfreev(sub_args); |
374b7f45 | 498 | } else |
24ff9c7b | 499 | g_critical("gpsbabel not found in PATH"); |
18ec873d GB |
500 | g_remove(name_src); |
501 | g_free(name_src); | |
b364d6bc QT |
502 | } |
503 | ||
b364d6bc QT |
504 | return ret; |
505 | } | |
18ec873d | 506 | |
6f2551f1 | 507 | static void set_mode(BabelMode *mode, gchar *smode) |
79e0f36e | 508 | { |
6f2551f1 GB |
509 | mode->waypointsRead = smode[0] == 'r'; |
510 | mode->waypointsWrite = smode[1] == 'w'; | |
511 | mode->tracksRead = smode[2] == 'r'; | |
512 | mode->tracksWrite = smode[3] == 'w'; | |
513 | mode->routesRead = smode[4] == 'r'; | |
514 | mode->routesWrite = smode[5] == 'w'; | |
79e0f36e GB |
515 | } |
516 | ||
517 | /** | |
ce4f6212 | 518 | * load_feature_parse_line: |
79e0f36e GB |
519 | * |
520 | * Load a single feature stored in the given line. | |
521 | */ | |
522 | static void load_feature_parse_line (gchar *line) | |
523 | { | |
524 | gchar **tokens = g_strsplit ( line, "\t", 0 ); | |
525 | if ( tokens != NULL | |
526 | && tokens[0] != NULL ) { | |
527 | if ( strcmp("serial", tokens[0]) == 0 ) { | |
528 | if ( tokens[1] != NULL | |
529 | && tokens[2] != NULL | |
530 | && tokens[3] != NULL | |
531 | && tokens[4] != NULL ) { | |
532 | BabelDevice *device = g_malloc ( sizeof (BabelDevice) ); | |
6f2551f1 | 533 | set_mode (&(device->mode), tokens[1]); |
79e0f36e | 534 | device->name = g_strdup (tokens[2]); |
431624c5 | 535 | device->label = g_strndup (tokens[4], 50); // Limit really long label text |
79e0f36e | 536 | a_babel_device_list = g_list_append (a_babel_device_list, device); |
6f2551f1 GB |
537 | g_debug ("New gpsbabel device: %s, %d%d%d%d%d%d(%s)", |
538 | device->name, | |
539 | device->mode.waypointsRead, device->mode.waypointsWrite, | |
540 | device->mode.tracksRead, device->mode.tracksWrite, | |
541 | device->mode.routesRead, device->mode.routesWrite, | |
542 | tokens[1]); | |
79e0f36e GB |
543 | } else { |
544 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
545 | } | |
546 | } else if ( strcmp("file", tokens[0]) == 0 ) { | |
547 | if ( tokens[1] != NULL | |
548 | && tokens[2] != NULL | |
549 | && tokens[3] != NULL | |
550 | && tokens[4] != NULL ) { | |
551 | BabelFile *file = g_malloc ( sizeof (BabelFile) ); | |
6f2551f1 | 552 | set_mode (&(file->mode), tokens[1]); |
79e0f36e GB |
553 | file->name = g_strdup (tokens[2]); |
554 | file->ext = g_strdup (tokens[3]); | |
555 | file->label = g_strdup (tokens[4]); | |
556 | a_babel_file_list = g_list_append (a_babel_file_list, file); | |
6f2551f1 | 557 | g_debug ("New gpsbabel file: %s, %d%d%d%d%d%d(%s)", |
17720e63 GB |
558 | file->name, |
559 | file->mode.waypointsRead, file->mode.waypointsWrite, | |
560 | file->mode.tracksRead, file->mode.tracksWrite, | |
561 | file->mode.routesRead, file->mode.routesWrite, | |
562 | tokens[1]); | |
79e0f36e GB |
563 | } else { |
564 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
565 | } | |
566 | } /* else: ignore */ | |
567 | } else { | |
568 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
569 | } | |
570 | g_strfreev ( tokens ); | |
571 | } | |
572 | ||
573 | static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data) | |
574 | { | |
575 | if (line != NULL) | |
576 | load_feature_parse_line (line); | |
577 | } | |
578 | ||
579 | static gboolean load_feature () | |
580 | { | |
581 | int i; | |
582 | gboolean ret = FALSE; | |
583 | gchar *args[4]; | |
584 | ||
585 | if ( gpsbabel_loc ) { | |
586 | i = 0; | |
587 | if ( unbuffer_loc ) | |
588 | args[i++] = unbuffer_loc; | |
589 | args[i++] = gpsbabel_loc; | |
590 | args[i++] = "-^3"; | |
591 | args[i] = NULL; | |
592 | ||
593 | ret = babel_general_convert (load_feature_cb, args, NULL); | |
594 | } else | |
24ff9c7b | 595 | g_critical("gpsbabel not found in PATH"); |
79e0f36e GB |
596 | |
597 | return ret; | |
598 | } | |
18ec873d | 599 | |
ce4f6212 GB |
600 | /** |
601 | * a_babel_init: | |
602 | * | |
603 | * Initialises babel module. | |
604 | * Mainly check existence of gpsbabel progam | |
605 | * and load all features available in ths version. | |
606 | */ | |
18ec873d GB |
607 | void a_babel_init () |
608 | { | |
609 | /* TODO allow to set gpsbabel path via command line */ | |
610 | gpsbabel_loc = g_find_program_in_path( "gpsbabel" ); | |
611 | if ( !gpsbabel_loc ) | |
24ff9c7b | 612 | g_critical( "gpsbabel not found in PATH" ); |
3684f362 RN |
613 | |
614 | // Unlikely to package unbuffer on Windows so ATM don't even bother trying | |
615 | // Highly unlikely unbuffer is available on a Windows system otherwise | |
616 | #ifndef WINDOWS | |
18ec873d GB |
617 | unbuffer_loc = g_find_program_in_path( "unbuffer" ); |
618 | if ( !unbuffer_loc ) | |
619 | g_warning( "unbuffer not found in PATH" ); | |
3684f362 | 620 | #endif |
18ec873d | 621 | |
79e0f36e | 622 | load_feature (); |
18ec873d GB |
623 | } |
624 | ||
ce4f6212 GB |
625 | /** |
626 | * a_babel_uninit: | |
627 | * | |
628 | * Free resources acquired by a_babel_init. | |
629 | */ | |
18ec873d GB |
630 | void a_babel_uninit () |
631 | { | |
632 | g_free ( gpsbabel_loc ); | |
633 | g_free ( unbuffer_loc ); | |
67c32f00 RN |
634 | |
635 | if ( a_babel_file_list ) { | |
636 | GList *gl; | |
637 | for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) { | |
638 | BabelFile *file = gl->data; | |
639 | g_free ( file->name ); | |
640 | g_free ( file->ext ); | |
641 | g_free ( file->label ); | |
642 | g_free ( gl->data ); | |
643 | } | |
644 | g_list_free ( a_babel_file_list ); | |
645 | } | |
646 | ||
647 | if ( a_babel_device_list ) { | |
648 | GList *gl; | |
649 | for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) { | |
650 | BabelDevice *device = gl->data; | |
651 | g_free ( device->name ); | |
652 | g_free ( device->label ); | |
653 | g_free ( gl->data ); | |
654 | } | |
655 | g_list_free ( a_babel_device_list ); | |
656 | } | |
657 | ||
18ec873d | 658 | } |
430a37a9 GB |
659 | |
660 | /** | |
661 | * a_babel_available: | |
662 | * | |
663 | * Indicates if babel is available or not. | |
664 | * | |
665 | * Returns: true if babel available | |
666 | */ | |
667 | gboolean a_babel_available () | |
668 | { | |
669 | return a_babel_device_list != NULL; | |
670 | } |