2 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
6 * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
32 #include "viktrwlayer_export.h"
34 void vik_trw_layer_export ( VikTrwLayer *vtl, const gchar *title, const gchar* default_name, VikTrack* trk, guint file_type )
36 GtkWidget *file_selector;
38 gboolean failed = FALSE;
39 file_selector = gtk_file_chooser_dialog_new (title,
41 GTK_FILE_CHOOSER_ACTION_SAVE,
42 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
43 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
45 gchar *cwd = g_get_current_dir();
47 gtk_file_chooser_set_current_folder ( GTK_FILE_CHOOSER(file_selector), cwd );
51 gtk_file_chooser_set_current_name ( GTK_FILE_CHOOSER(file_selector), default_name );
53 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
55 fn = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) );
56 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE ||
57 a_dialog_yes_or_no ( GTK_WINDOW(file_selector), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
59 gtk_widget_hide ( file_selector );
60 vik_window_set_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
61 failed = ! a_file_export ( vtl, fn, file_type, trk, TRUE );
62 vik_window_clear_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
66 gtk_widget_destroy ( file_selector );
68 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("The filename you requested could not be opened for writing.") );
73 * Convert the given TRW layer into a temporary GPX file and open it with the specified program
76 void vik_trw_layer_export_external_gpx ( VikTrwLayer *vtl, const gchar* external_program )
78 gchar *name_used = NULL;
81 if ((fd = g_file_open_tmp("tmp-viking.XXXXXX.gpx", &name_used, NULL)) >= 0) {
82 vik_window_set_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
83 gboolean failed = ! a_file_export ( VIK_TRW_LAYER(vtl), name_used, FILE_TYPE_GPX, NULL, TRUE);
84 vik_window_clear_busy_cursor ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)) );
86 a_dialog_error_msg (VIK_GTK_WINDOW_FROM_LAYER(vtl), _("Could not create temporary file for export.") );
90 gchar *quoted_file = g_shell_quote ( name_used );
91 gchar *cmd = g_strdup_printf ( "%s %s", external_program, quoted_file );
92 g_free ( quoted_file );
93 if ( ! g_spawn_command_line_async ( cmd, &err ) )
95 a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_LAYER( vtl), _("Could not launch %s."), external_program );
100 // Note ATM the 'temporary' file is not deleted, as loading via another program is not instantaneous
101 //g_remove ( name_used );
102 // Perhaps should be deleted when the program ends?
103 // For now leave it to the user to delete it / use system temp cleanup methods.
104 g_free ( name_used );