]> git.street.me.uk Git - andy/viking.git/blame - src/viktrwlayer_export.c
[QA] Rename and correct some map cache variables usage for better understanding.
[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"
44871dd1 35#include "util.h"
e4a11fbe 36
bb3c38af 37void vik_trw_layer_export ( VikTrwLayer *vtl, const gchar *title, const gchar* default_name, VikTrack* trk, VikFileType_t file_type )
e4a11fbe
GB
38{
39 GtkWidget *file_selector;
40 const gchar *fn;
41 gboolean failed = FALSE;
42 file_selector = gtk_file_chooser_dialog_new (title,
43 NULL,
44 GTK_FILE_CHOOSER_ACTION_SAVE,
45 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
46 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
47 NULL);
48 gchar *cwd = g_get_current_dir();
49 if ( cwd ) {
50 gtk_file_chooser_set_current_folder ( GTK_FILE_CHOOSER(file_selector), cwd );
51 g_free ( cwd );
52 }
53
54 gtk_file_chooser_set_current_name ( GTK_FILE_CHOOSER(file_selector), default_name );
55
56 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
57 {
58 fn = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) );
59 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE ||
60 a_dialog_yes_or_no ( GTK_WINDOW(file_selector), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
61 {
62 gtk_widget_hide ( file_selector );
63 vik_window_set_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
64 failed = ! a_file_export ( vtl, fn, file_type, trk, TRUE );
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{
12ed2b58 81 gchar *name_used = a_gpx_write_tmp_file ( vtl, NULL );
e4a11fbe 82
12ed2b58
RN
83 if ( name_used ) {
84 GError *err = NULL;
85 gchar *quoted_file = g_shell_quote ( name_used );
86 gchar *cmd = g_strdup_printf ( "%s %s", external_program, quoted_file );
87 g_free ( quoted_file );
88 if ( ! g_spawn_command_line_async ( cmd, &err ) ) {
89 a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_LAYER( vtl), _("Could not launch %s."), external_program );
90 g_error_free ( err );
e4a11fbe 91 }
12ed2b58 92 g_free ( cmd );
44871dd1 93 util_add_to_deletion_list ( name_used );
e4a11fbe
GB
94 g_free ( name_used );
95 }
12ed2b58
RN
96 else
97 a_dialog_error_msg (VIK_GTK_WINDOW_FROM_LAYER(vtl), _("Could not create temporary file for export.") );
e4a11fbe
GB
98}
99
17720e63
GB
100
101void vik_trw_layer_export_gpsbabel ( VikTrwLayer *vtl, const gchar *title, const gchar* default_name )
102{
103 BabelMode mode = { 0, 0, 0, 0, 0, 0 };
104 if ( g_hash_table_size (vik_trw_layer_get_routes(vtl)) ) {
105 mode.routesWrite = 1;
106 }
107 if ( g_hash_table_size (vik_trw_layer_get_tracks(vtl)) ) {
108 mode.tracksWrite = 1;
109 }
110 if ( g_hash_table_size (vik_trw_layer_get_waypoints(vtl)) ) {
111 mode.waypointsWrite = 1;
112 }
113
114 GtkWidget *file_selector;
115 const gchar *fn;
116 gboolean failed = FALSE;
117 file_selector = gtk_file_chooser_dialog_new (title,
118 NULL,
119 GTK_FILE_CHOOSER_ACTION_SAVE,
120 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
121 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
122 NULL);
123 gchar *cwd = g_get_current_dir();
124 if ( cwd ) {
125 gtk_file_chooser_set_current_folder ( GTK_FILE_CHOOSER(file_selector), cwd );
126 g_free ( cwd );
127 }
128
129 /* Build the extra part of the widget */
130 GtkWidget *babel_selector = a_babel_ui_file_type_selector_new ( mode );
131 GtkWidget *label = gtk_label_new(_("File format:"));
132 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
133 gtk_box_pack_start ( GTK_BOX(hbox), label, TRUE, TRUE, 0 );
134 gtk_box_pack_start ( GTK_BOX(hbox), babel_selector, TRUE, TRUE, 0 );
135 gtk_widget_show (babel_selector);
136 gtk_widget_show (label);
137 gtk_widget_show_all (hbox);
138
139 gtk_widget_set_tooltip_text( babel_selector, _("Select the file format.") );
140
141 GtkWidget *babel_modes = a_babel_ui_modes_new(mode.tracksWrite, mode.routesWrite, mode.waypointsWrite);
142 gtk_widget_show (babel_modes);
143
144 gtk_widget_set_tooltip_text( babel_modes, _("Select the information to process.\n"
145 "Warning: the behavior of these switches is highly dependent of the file format selected.\n"
146 "Please, refer to GPSbabel if unsure.") );
147
148 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
149 gtk_box_pack_start ( GTK_BOX(vbox), hbox, TRUE, TRUE, 0 );
150 gtk_box_pack_start ( GTK_BOX(vbox), babel_modes, TRUE, TRUE, 0 );
151 gtk_widget_show_all (vbox);
152
153 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(file_selector), vbox);
154
155 /* Add some dynamic: only allow dialog's validation when format selection is done */
156 g_signal_connect (babel_selector, "changed", G_CALLBACK(a_babel_ui_type_selector_dialog_sensitivity_cb), file_selector);
157 /* Manually call the callback to fix the state */
211f4b8a 158 a_babel_ui_type_selector_dialog_sensitivity_cb ( GTK_COMBO_BOX(babel_selector), file_selector);
17720e63
GB
159
160 /* Set possible name of the file */
161 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(file_selector), default_name);
162
163 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
164 {
165 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(file_selector) );
166 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE ||
167 a_dialog_yes_or_no ( GTK_WINDOW(file_selector), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
168 {
169 BabelFile *active = a_babel_ui_file_type_selector_get(babel_selector);
170 if (active == NULL) {
171 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("You did not select a valid file format.") );
172 } else {
173 gtk_widget_hide ( file_selector );
174 vik_window_set_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
175 gboolean tracks, routes, waypoints;
176 a_babel_ui_modes_get( babel_modes, &tracks, &routes, &waypoints );
177 failed = ! a_file_export_babel ( vtl, fn, active->name, tracks, routes, waypoints );
178 vik_window_clear_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
179 break;
180 }
181 }
182 }
183 //babel_ui_selector_destroy(babel_selector);
184 gtk_widget_destroy ( file_selector );
185 if ( failed )
186 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("The filename you requested could not be opened for writing.") );
187}