]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_export.c
Extract trw export module
[andy/viking.git] / src / viktrwlayer_export.c
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"
31 #include "viking.h"
32 #include "viktrwlayer_export.h"
33
34 void vik_trw_layer_export ( VikTrwLayer *vtl, const gchar *title, const gchar* default_name, VikTrack* trk, guint file_type )
35 {
36   GtkWidget *file_selector;
37   const gchar *fn;
38   gboolean failed = FALSE;
39   file_selector = gtk_file_chooser_dialog_new (title,
40                                                NULL,
41                                                GTK_FILE_CHOOSER_ACTION_SAVE,
42                                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
43                                                GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
44                                                NULL);
45   gchar *cwd = g_get_current_dir();
46   if ( cwd ) {
47     gtk_file_chooser_set_current_folder ( GTK_FILE_CHOOSER(file_selector), cwd );
48     g_free ( cwd );
49   }
50
51   gtk_file_chooser_set_current_name ( GTK_FILE_CHOOSER(file_selector), default_name );
52
53   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
54   {
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 ) ) )
58     {
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)) );
63       break;
64     }
65   }
66   gtk_widget_destroy ( file_selector );
67   if ( failed )
68     a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), _("The filename you requested could not be opened for writing.") );
69 }
70
71
72 /**
73  * Convert the given TRW layer into a temporary GPX file and open it with the specified program
74  *
75  */
76 void vik_trw_layer_export_external_gpx ( VikTrwLayer *vtl, const gchar* external_program )
77 {
78   gchar *name_used = NULL;
79   int fd;
80
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)) );
85     if (failed) {
86       a_dialog_error_msg (VIK_GTK_WINDOW_FROM_LAYER(vtl), _("Could not create temporary file for export.") );
87     }
88     else {
89       GError *err = NULL;
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 ) )
94         {
95           a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_LAYER( vtl), _("Could not launch %s."), external_program );
96           g_error_free ( err );
97         }
98       g_free ( cmd );
99     }
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 );
105   }
106 }
107