]> git.street.me.uk Git - andy/viking.git/blame - src/viktrwlayer_export.c
[QA] Use labs() instead of abs() in comparison of timestamps to avoid truncation...
[andy/viking.git] / src / viktrwlayer_export.c
CommitLineData
e4a11fbe
GB
1
2/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3/*
4 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 *
6 * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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#include <string.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <glib.h>
27#include <glib/gstdio.h>
28#include <glib/gi18n.h>
29
30#include "babel.h"
17720e63 31#include "babel_ui.h"
e4a11fbe
GB
32#include "viking.h"
33#include "viktrwlayer_export.h"
12ed2b58 34#include "gpx.h"
e4a11fbe 35
bb3c38af 36void vik_trw_layer_export ( VikTrwLayer *vtl, const gchar *title, const gchar* default_name, VikTrack* trk, VikFileType_t file_type )
e4a11fbe
GB
37{
38 GtkWidget *file_selector;
39 const gchar *fn;
40 gboolean failed = FALSE;
41 file_selector = gtk_file_chooser_dialog_new (title,
42 NULL,
43 GTK_FILE_CHOOSER_ACTION_SAVE,
44 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
45 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
46 NULL);
47 gchar *cwd = g_get_current_dir();
48 if ( cwd ) {
49 gtk_file_chooser_set_current_folder ( GTK_FILE_CHOOSER(file_selector), cwd );
50 g_free ( cwd );
51 }
52
53 gtk_file_chooser_set_current_name ( GTK_FILE_CHOOSER(file_selector), default_name );
54
55 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
56 {
57 fn = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) );
58 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE ||
59 a_dialog_yes_or_no ( GTK_WINDOW(file_selector), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
60 {
61 gtk_widget_hide ( file_selector );
62 vik_window_set_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
978cd696
RN
63 // Don't Export invisible items - unless requested on this specific track
64 failed = ! a_file_export ( vtl, fn, file_type, trk, trk ? TRUE : FALSE );
e4a11fbe
GB
65 vik_window_clear_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
66 break;
67 }
68 }
69 gtk_widget_destroy ( file_selector );
70 if ( failed )
71 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("The filename you requested could not be opened for writing.") );
72}
73
74
75/**
76 * Convert the given TRW layer into a temporary GPX file and open it with the specified program
77 *
78 */
79void vik_trw_layer_export_external_gpx ( VikTrwLayer *vtl, const gchar* external_program )
80{
978cd696
RN
81 // Don't Export invisible items
82 static GpxWritingOptions options = { TRUE, TRUE, FALSE, FALSE };
83 gchar *name_used = a_gpx_write_tmp_file ( vtl, &options );
e4a11fbe 84
12ed2b58
RN
85 if ( name_used ) {
86 GError *err = NULL;
87 gchar *quoted_file = g_shell_quote ( name_used );
88 gchar *cmd = g_strdup_printf ( "%s %s", external_program, quoted_file );
89 g_free ( quoted_file );
90 if ( ! g_spawn_command_line_async ( cmd, &err ) ) {
91 a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_LAYER( vtl), _("Could not launch %s."), external_program );
92 g_error_free ( err );
e4a11fbe 93 }
12ed2b58 94 g_free ( cmd );
44871dd1 95 util_add_to_deletion_list ( name_used );
e4a11fbe
GB
96 g_free ( name_used );
97 }
12ed2b58
RN
98 else
99 a_dialog_error_msg (VIK_GTK_WINDOW_FROM_LAYER(vtl), _("Could not create temporary file for export.") );
e4a11fbe
GB
100}
101
17720e63
GB
102
103void vik_trw_layer_export_gpsbabel ( VikTrwLayer *vtl, const gchar *title, const gchar* default_name )
104{
105 BabelMode mode = { 0, 0, 0, 0, 0, 0 };
106 if ( g_hash_table_size (vik_trw_layer_get_routes(vtl)) ) {
107 mode.routesWrite = 1;
108 }
109 if ( g_hash_table_size (vik_trw_layer_get_tracks(vtl)) ) {
110 mode.tracksWrite = 1;
111 }
112 if ( g_hash_table_size (vik_trw_layer_get_waypoints(vtl)) ) {
113 mode.waypointsWrite = 1;
114 }
115
116 GtkWidget *file_selector;
117 const gchar *fn;
118 gboolean failed = FALSE;
119 file_selector = gtk_file_chooser_dialog_new (title,
120 NULL,
121 GTK_FILE_CHOOSER_ACTION_SAVE,
122 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
123 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
124 NULL);
125 gchar *cwd = g_get_current_dir();
126 if ( cwd ) {
127 gtk_file_chooser_set_current_folder ( GTK_FILE_CHOOSER(file_selector), cwd );
128 g_free ( cwd );
129 }
130
131 /* Build the extra part of the widget */
132 GtkWidget *babel_selector = a_babel_ui_file_type_selector_new ( mode );
133 GtkWidget *label = gtk_label_new(_("File format:"));
134 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
135 gtk_box_pack_start ( GTK_BOX(hbox), label, TRUE, TRUE, 0 );
136 gtk_box_pack_start ( GTK_BOX(hbox), babel_selector, TRUE, TRUE, 0 );
137 gtk_widget_show (babel_selector);
138 gtk_widget_show (label);
139 gtk_widget_show_all (hbox);
140
141 gtk_widget_set_tooltip_text( babel_selector, _("Select the file format.") );
142
143 GtkWidget *babel_modes = a_babel_ui_modes_new(mode.tracksWrite, mode.routesWrite, mode.waypointsWrite);
144 gtk_widget_show (babel_modes);
145
146 gtk_widget_set_tooltip_text( babel_modes, _("Select the information to process.\n"
147 "Warning: the behavior of these switches is highly dependent of the file format selected.\n"
148 "Please, refer to GPSbabel if unsure.") );
149
150 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
151 gtk_box_pack_start ( GTK_BOX(vbox), hbox, TRUE, TRUE, 0 );
152 gtk_box_pack_start ( GTK_BOX(vbox), babel_modes, TRUE, TRUE, 0 );
153 gtk_widget_show_all (vbox);
154
155 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(file_selector), vbox);
156
157 /* Add some dynamic: only allow dialog's validation when format selection is done */
158 g_signal_connect (babel_selector, "changed", G_CALLBACK(a_babel_ui_type_selector_dialog_sensitivity_cb), file_selector);
159 /* Manually call the callback to fix the state */
211f4b8a 160 a_babel_ui_type_selector_dialog_sensitivity_cb ( GTK_COMBO_BOX(babel_selector), file_selector);
17720e63
GB
161
162 /* Set possible name of the file */
163 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(file_selector), default_name);
164
165 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
166 {
167 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(file_selector) );
168 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE ||
169 a_dialog_yes_or_no ( GTK_WINDOW(file_selector), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
170 {
171 BabelFile *active = a_babel_ui_file_type_selector_get(babel_selector);
172 if (active == NULL) {
173 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("You did not select a valid file format.") );
174 } else {
175 gtk_widget_hide ( file_selector );
176 vik_window_set_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
177 gboolean tracks, routes, waypoints;
178 a_babel_ui_modes_get( babel_modes, &tracks, &routes, &waypoints );
179 failed = ! a_file_export_babel ( vtl, fn, active->name, tracks, routes, waypoints );
180 vik_window_clear_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
181 break;
182 }
183 }
184 }
185 //babel_ui_selector_destroy(babel_selector);
186 gtk_widget_destroy ( file_selector );
187 if ( failed )
188 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("The filename you requested could not be opened for writing.") );
189}