]> git.street.me.uk Git - andy/viking.git/blame - src/datasource_file.c
Allow uploading a single track via the GPS layer interface.
[andy/viking.git] / src / datasource_file.c
CommitLineData
31349009
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2011, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24#include <string.h>
25
26#include <glib/gprintf.h>
27#include <glib/gi18n.h>
28
29#include <gtk/gtk.h>
30
31#include "viking.h"
32#include "babel.h"
33#include "gpx.h"
34#include "acquire.h"
35
36typedef struct {
37 GtkWidget *file;
38 GtkWidget *type;
39} datasource_file_widgets_t;
40
41/* The last used directory */
42static gchar *last_folder_uri = NULL;
43
44/* The last used file filter */
45/* Nb: we use a complex strategy for this because the UI is rebuild each
46 time, so it is not possible to reuse directly the GtkFileFilter as they are
47 differents. */
48static BabelFile *last_file_filter = NULL;
49
50/* The last file format selected */
51static int last_type = 0;
52
53static gpointer datasource_file_init( );
54static void datasource_file_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
55static void datasource_file_get_cmd_string ( datasource_file_widgets_t *widgets, gchar **cmd, gchar **input_file_type );
56static void datasource_file_cleanup ( gpointer data );
57
58VikDataSourceInterface vik_datasource_file_interface = {
59 N_("Import file with GPSBabel"),
60 N_("Imported file"),
61 VIK_DATASOURCE_GPSBABEL_DIRECT,
62 VIK_DATASOURCE_ADDTOLAYER,
63 VIK_DATASOURCE_INPUTTYPE_NONE,
64 TRUE,
65 TRUE,
66 (VikDataSourceInitFunc) datasource_file_init,
67 (VikDataSourceCheckExistenceFunc) NULL,
68 (VikDataSourceAddSetupWidgetsFunc) datasource_file_add_setup_widgets,
69 (VikDataSourceGetCmdStringFunc) datasource_file_get_cmd_string,
0944940f 70 (VikDataSourceProcessFunc) NULL,
31349009
GB
71 (VikDataSourceProgressFunc) NULL,
72 (VikDataSourceAddProgressWidgetsFunc) NULL,
73 (VikDataSourceCleanupFunc) datasource_file_cleanup,
74 (VikDataSourceOffFunc) NULL,
75};
76
77/* See VikDataSourceInterface */
78static gpointer datasource_file_init ( )
79{
80 datasource_file_widgets_t *widgets = g_malloc(sizeof(*widgets));
81 return widgets;
82}
83
84static void fill_combo_box (gpointer data, gpointer user_data)
85{
31349009
GB
86 const gchar *label = ((BabelFile*) data)->label;
87#if GTK_CHECK_VERSION (2,24,0)
4a050412 88 GtkComboBoxText *combo = GTK_COMBO_BOX_TEXT (user_data);
31349009
GB
89 gtk_combo_box_text_append_text (combo, label);
90#else
4a050412 91 GtkComboBox *combo = GTK_COMBO_BOX (user_data);
31349009
GB
92 gtk_combo_box_append_text (combo, label);
93#endif
94}
95
96static void add_file_filter (gpointer data, gpointer user_data)
97{
98 GtkFileChooser *chooser = GTK_FILE_CHOOSER ( user_data );
99 const gchar *label = ((BabelFile*) data)->label;
100 const gchar *ext = ((BabelFile*) data)->ext;
101 if ( ext == NULL || ext[0] == '\0' )
102 /* No file extension => no filter */
103 return;
104 gchar *pattern = g_strdup_printf ( "*.%s", ext );
105
106 GtkFileFilter *filter = gtk_file_filter_new ();
107 gtk_file_filter_add_pattern ( filter, pattern );
108 if ( strstr ( label, pattern+1 ) ) {
109 gtk_file_filter_set_name ( filter, label );
110 } else {
111 /* Ensure displayed label contains file pattern */
112 /* NB: we skip the '*' in the pattern */
113 gchar *name = g_strdup_printf ( "%s (%s)", label, pattern+1 );
114 gtk_file_filter_set_name ( filter, name );
115 g_free ( name );
116 }
117 g_object_set_data ( G_OBJECT(filter), "Babel", data );
118 gtk_file_chooser_add_filter ( chooser, filter );
119 if ( last_file_filter == data )
120 /* Previous selection used this filter */
121 gtk_file_chooser_set_filter ( chooser, filter );
122
123 g_free ( pattern );
124}
125
126/* See VikDataSourceInterface */
127static void datasource_file_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
128{
129 datasource_file_widgets_t *widgets = (datasource_file_widgets_t *)user_data;
130 GtkWidget *filename_label, *type_label;
131
132 /* The file selector */
133 filename_label = gtk_label_new (_("File:"));
134 widgets->file = gtk_file_chooser_button_new (_("File to import"), GTK_FILE_CHOOSER_ACTION_OPEN);
135 if (last_folder_uri)
136 gtk_file_chooser_set_current_folder_uri ( GTK_FILE_CHOOSER(widgets->file), last_folder_uri);
137 /* Add filters */
138 g_list_foreach ( a_babel_file_list, add_file_filter, widgets->file );
139 GtkFileFilter *all_filter = gtk_file_filter_new ();
140 gtk_file_filter_add_pattern ( all_filter, "*" );
141 gtk_file_filter_set_name ( all_filter, _("All files") );
142 gtk_file_chooser_add_filter ( GTK_FILE_CHOOSER(widgets->file), all_filter );
143 if ( last_file_filter == NULL )
144 /* No previously selected filter or 'All files' selected */
145 gtk_file_chooser_set_filter ( GTK_FILE_CHOOSER(widgets->file), all_filter );
146
147 /* The file format selector */
148 type_label = gtk_label_new (_("File type:"));
149#if GTK_CHECK_VERSION (2,24,0)
150 widgets->type = gtk_combo_box_text_new ();
151#else
152 widgets->type = gtk_combo_box_new_text ();
153#endif
154 g_list_foreach (a_babel_file_list, fill_combo_box, widgets->type);
155 gtk_combo_box_set_active (GTK_COMBO_BOX (widgets->type), last_type);
156
157 /* Packing all these widgets */
158 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), filename_label, FALSE, FALSE, 5 );
159 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->file, FALSE, FALSE, 5 );
160 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), type_label, FALSE, FALSE, 5 );
161 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->type, FALSE, FALSE, 5 );
162 gtk_widget_show_all(dialog);
163}
164
165/* See VikDataSourceInterface */
166static void datasource_file_get_cmd_string ( datasource_file_widgets_t *widgets, gchar **cmd, gchar **input_file )
167{
31349009 168 /* Retrieve the file selected */
2d8277f1 169 gchar *filename = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(widgets->file) );
31349009
GB
170
171 /* Memorize the directory for later use */
172 g_free (last_folder_uri);
173 last_folder_uri = gtk_file_chooser_get_current_folder_uri ( GTK_FILE_CHOOSER(widgets->file) );
174 last_folder_uri = g_strdup (last_folder_uri);
175
176 /* Memorize the file filter for later use */
177 GtkFileFilter *filter = gtk_file_chooser_get_filter ( GTK_FILE_CHOOSER(widgets->file) );
178 last_file_filter = g_object_get_data ( G_OBJECT(filter), "Babel" );
179
180 /* Retrieve and memorize file format selected */
2d8277f1 181 gchar *type = NULL;
31349009 182 last_type = gtk_combo_box_get_active ( GTK_COMBO_BOX (widgets->type) );
8ad7a81e
RN
183 if ( a_babel_file_list )
184 type = ((BabelFile*)g_list_nth_data (a_babel_file_list, last_type))->name;
31349009
GB
185
186 /* Build the string */
187 *cmd = g_strdup_printf( "-i %s", type);
188 *input_file = g_strdup(filename);
189
190 /* Free memory */
191 g_free (filename);
192
193 g_debug(_("using babel args '%s' and file '%s'"), *cmd, *input_file);
194}
195
196/* See VikDataSourceInterface */
197static void datasource_file_cleanup ( gpointer data )
198{
199 g_free ( data );
200}
201