2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2011, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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
26 #include <glib/gprintf.h>
27 #include <glib/gi18n.h>
39 } datasource_file_widgets_t;
41 /* The last used directory */
42 static gchar *last_folder_uri = NULL;
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
48 static BabelFile *last_file_filter = NULL;
50 /* The last file format selected */
51 static int last_type = 0;
53 static gpointer datasource_file_init( );
54 static void datasource_file_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
55 static void datasource_file_get_cmd_string ( datasource_file_widgets_t *widgets, gchar **cmd, gchar **input_file_type, gpointer not_used );
56 static void datasource_file_cleanup ( gpointer data );
58 VikDataSourceInterface vik_datasource_file_interface = {
59 N_("Import file with GPSBabel"),
61 VIK_DATASOURCE_ADDTOLAYER,
62 VIK_DATASOURCE_INPUTTYPE_NONE,
66 (VikDataSourceInitFunc) datasource_file_init,
67 (VikDataSourceCheckExistenceFunc) NULL,
68 (VikDataSourceAddSetupWidgetsFunc) datasource_file_add_setup_widgets,
69 (VikDataSourceGetCmdStringFunc) datasource_file_get_cmd_string,
70 (VikDataSourceProcessFunc) a_babel_convert_from,
71 (VikDataSourceProgressFunc) NULL,
72 (VikDataSourceAddProgressWidgetsFunc) NULL,
73 (VikDataSourceCleanupFunc) datasource_file_cleanup,
74 (VikDataSourceOffFunc) NULL,
77 /* See VikDataSourceInterface */
78 static gpointer datasource_file_init ( )
80 datasource_file_widgets_t *widgets = g_malloc(sizeof(*widgets));
84 static void fill_combo_box (gpointer data, gpointer user_data)
86 const gchar *label = ((BabelFile*) data)->label;
87 vik_combo_box_text_append (GTK_WIDGET(user_data), label);
90 static void add_file_filter (gpointer data, gpointer user_data)
92 GtkFileChooser *chooser = GTK_FILE_CHOOSER ( user_data );
93 const gchar *label = ((BabelFile*) data)->label;
94 const gchar *ext = ((BabelFile*) data)->ext;
95 if ( ext == NULL || ext[0] == '\0' )
96 /* No file extension => no filter */
98 gchar *pattern = g_strdup_printf ( "*.%s", ext );
100 GtkFileFilter *filter = gtk_file_filter_new ();
101 gtk_file_filter_add_pattern ( filter, pattern );
102 if ( strstr ( label, pattern+1 ) ) {
103 gtk_file_filter_set_name ( filter, label );
105 /* Ensure displayed label contains file pattern */
106 /* NB: we skip the '*' in the pattern */
107 gchar *name = g_strdup_printf ( "%s (%s)", label, pattern+1 );
108 gtk_file_filter_set_name ( filter, name );
111 g_object_set_data ( G_OBJECT(filter), "Babel", data );
112 gtk_file_chooser_add_filter ( chooser, filter );
113 if ( last_file_filter == data )
114 /* Previous selection used this filter */
115 gtk_file_chooser_set_filter ( chooser, filter );
120 /* See VikDataSourceInterface */
121 static void datasource_file_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
123 datasource_file_widgets_t *widgets = (datasource_file_widgets_t *)user_data;
124 GtkWidget *filename_label, *type_label;
126 /* The file selector */
127 filename_label = gtk_label_new (_("File:"));
128 widgets->file = gtk_file_chooser_button_new (_("File to import"), GTK_FILE_CHOOSER_ACTION_OPEN);
130 gtk_file_chooser_set_current_folder_uri ( GTK_FILE_CHOOSER(widgets->file), last_folder_uri);
132 g_list_foreach ( a_babel_file_list, add_file_filter, widgets->file );
133 GtkFileFilter *all_filter = gtk_file_filter_new ();
134 gtk_file_filter_add_pattern ( all_filter, "*" );
135 gtk_file_filter_set_name ( all_filter, _("All files") );
136 gtk_file_chooser_add_filter ( GTK_FILE_CHOOSER(widgets->file), all_filter );
137 if ( last_file_filter == NULL )
138 /* No previously selected filter or 'All files' selected */
139 gtk_file_chooser_set_filter ( GTK_FILE_CHOOSER(widgets->file), all_filter );
141 /* The file format selector */
142 type_label = gtk_label_new (_("File type:"));
143 widgets->type = vik_combo_box_text_new ();
144 g_list_foreach (a_babel_file_list, fill_combo_box, widgets->type);
145 gtk_combo_box_set_active (GTK_COMBO_BOX (widgets->type), last_type);
147 /* Packing all these widgets */
148 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), filename_label, FALSE, FALSE, 5 );
149 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), widgets->file, FALSE, FALSE, 5 );
150 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), type_label, FALSE, FALSE, 5 );
151 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), widgets->type, FALSE, FALSE, 5 );
152 gtk_widget_show_all(dialog);
155 /* See VikDataSourceInterface */
156 static void datasource_file_get_cmd_string ( datasource_file_widgets_t *widgets, gchar **cmd, gchar **input_file, gpointer not_used )
158 /* Retrieve the file selected */
159 gchar *filename = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(widgets->file) );
161 /* Memorize the directory for later use */
162 g_free (last_folder_uri);
163 last_folder_uri = gtk_file_chooser_get_current_folder_uri ( GTK_FILE_CHOOSER(widgets->file) );
164 last_folder_uri = g_strdup (last_folder_uri);
166 /* Memorize the file filter for later use */
167 GtkFileFilter *filter = gtk_file_chooser_get_filter ( GTK_FILE_CHOOSER(widgets->file) );
168 last_file_filter = g_object_get_data ( G_OBJECT(filter), "Babel" );
170 /* Retrieve and memorize file format selected */
172 last_type = gtk_combo_box_get_active ( GTK_COMBO_BOX (widgets->type) );
173 if ( a_babel_file_list )
174 type = ((BabelFile*)g_list_nth_data (a_babel_file_list, last_type))->name;
176 /* Build the string */
177 *cmd = g_strdup_printf( "-i %s", type);
178 *input_file = g_strdup(filename);
183 g_debug(_("using babel args '%s' and file '%s'"), *cmd, *input_file);
186 /* See VikDataSourceInterface */
187 static void datasource_file_cleanup ( gpointer data )