]>
Commit | Line | Data |
---|---|---|
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" | |
b666a8ba | 34 | #include "babel_ui.h" |
31349009 GB |
35 | #include "acquire.h" |
36 | ||
37 | typedef struct { | |
38 | GtkWidget *file; | |
39 | GtkWidget *type; | |
40 | } datasource_file_widgets_t; | |
41 | ||
42 | /* The last used directory */ | |
43 | static gchar *last_folder_uri = NULL; | |
44 | ||
45 | /* The last used file filter */ | |
46 | /* Nb: we use a complex strategy for this because the UI is rebuild each | |
47 | time, so it is not possible to reuse directly the GtkFileFilter as they are | |
48 | differents. */ | |
49 | static BabelFile *last_file_filter = NULL; | |
50 | ||
51 | /* The last file format selected */ | |
52 | static int last_type = 0; | |
53 | ||
307abf54 | 54 | static gpointer datasource_file_init ( acq_vik_t *avt ); |
31349009 | 55 | static void datasource_file_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ); |
ed691ed1 | 56 | static void datasource_file_get_cmd_string ( datasource_file_widgets_t *widgets, gchar **cmd, gchar **input_file_type, gpointer not_used ); |
31349009 GB |
57 | static void datasource_file_cleanup ( gpointer data ); |
58 | ||
59 | VikDataSourceInterface vik_datasource_file_interface = { | |
60 | N_("Import file with GPSBabel"), | |
61 | N_("Imported file"), | |
31349009 GB |
62 | VIK_DATASOURCE_ADDTOLAYER, |
63 | VIK_DATASOURCE_INPUTTYPE_NONE, | |
64 | TRUE, | |
65 | TRUE, | |
b2aa700f | 66 | TRUE, |
31349009 GB |
67 | (VikDataSourceInitFunc) datasource_file_init, |
68 | (VikDataSourceCheckExistenceFunc) NULL, | |
69 | (VikDataSourceAddSetupWidgetsFunc) datasource_file_add_setup_widgets, | |
70 | (VikDataSourceGetCmdStringFunc) datasource_file_get_cmd_string, | |
eb3f9398 | 71 | (VikDataSourceProcessFunc) a_babel_convert_from, |
31349009 GB |
72 | (VikDataSourceProgressFunc) NULL, |
73 | (VikDataSourceAddProgressWidgetsFunc) NULL, | |
74 | (VikDataSourceCleanupFunc) datasource_file_cleanup, | |
75 | (VikDataSourceOffFunc) NULL, | |
76 | }; | |
77 | ||
78 | /* See VikDataSourceInterface */ | |
307abf54 | 79 | static gpointer datasource_file_init ( acq_vik_t *avt ) |
31349009 GB |
80 | { |
81 | datasource_file_widgets_t *widgets = g_malloc(sizeof(*widgets)); | |
82 | return widgets; | |
83 | } | |
84 | ||
31349009 GB |
85 | static void add_file_filter (gpointer data, gpointer user_data) |
86 | { | |
87 | GtkFileChooser *chooser = GTK_FILE_CHOOSER ( user_data ); | |
88 | const gchar *label = ((BabelFile*) data)->label; | |
89 | const gchar *ext = ((BabelFile*) data)->ext; | |
90 | if ( ext == NULL || ext[0] == '\0' ) | |
91 | /* No file extension => no filter */ | |
92 | return; | |
93 | gchar *pattern = g_strdup_printf ( "*.%s", ext ); | |
94 | ||
95 | GtkFileFilter *filter = gtk_file_filter_new (); | |
96 | gtk_file_filter_add_pattern ( filter, pattern ); | |
97 | if ( strstr ( label, pattern+1 ) ) { | |
98 | gtk_file_filter_set_name ( filter, label ); | |
99 | } else { | |
100 | /* Ensure displayed label contains file pattern */ | |
101 | /* NB: we skip the '*' in the pattern */ | |
102 | gchar *name = g_strdup_printf ( "%s (%s)", label, pattern+1 ); | |
103 | gtk_file_filter_set_name ( filter, name ); | |
104 | g_free ( name ); | |
105 | } | |
106 | g_object_set_data ( G_OBJECT(filter), "Babel", data ); | |
107 | gtk_file_chooser_add_filter ( chooser, filter ); | |
108 | if ( last_file_filter == data ) | |
109 | /* Previous selection used this filter */ | |
110 | gtk_file_chooser_set_filter ( chooser, filter ); | |
111 | ||
112 | g_free ( pattern ); | |
113 | } | |
114 | ||
115 | /* See VikDataSourceInterface */ | |
116 | static void datasource_file_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ) | |
117 | { | |
118 | datasource_file_widgets_t *widgets = (datasource_file_widgets_t *)user_data; | |
119 | GtkWidget *filename_label, *type_label; | |
120 | ||
121 | /* The file selector */ | |
122 | filename_label = gtk_label_new (_("File:")); | |
123 | widgets->file = gtk_file_chooser_button_new (_("File to import"), GTK_FILE_CHOOSER_ACTION_OPEN); | |
124 | if (last_folder_uri) | |
125 | gtk_file_chooser_set_current_folder_uri ( GTK_FILE_CHOOSER(widgets->file), last_folder_uri); | |
126 | /* Add filters */ | |
127 | g_list_foreach ( a_babel_file_list, add_file_filter, widgets->file ); | |
128 | GtkFileFilter *all_filter = gtk_file_filter_new (); | |
129 | gtk_file_filter_add_pattern ( all_filter, "*" ); | |
130 | gtk_file_filter_set_name ( all_filter, _("All files") ); | |
131 | gtk_file_chooser_add_filter ( GTK_FILE_CHOOSER(widgets->file), all_filter ); | |
132 | if ( last_file_filter == NULL ) | |
133 | /* No previously selected filter or 'All files' selected */ | |
134 | gtk_file_chooser_set_filter ( GTK_FILE_CHOOSER(widgets->file), all_filter ); | |
135 | ||
136 | /* The file format selector */ | |
137 | type_label = gtk_label_new (_("File type:")); | |
b666a8ba GB |
138 | /* Propose all readable file */ |
139 | BabelMode mode = { 1, 0, 1, 0, 1, 0 }; | |
140 | widgets->type = a_babel_ui_file_type_selector_new ( mode ); | |
141 | g_signal_connect ( G_OBJECT(widgets->type), "changed", | |
142 | G_CALLBACK(a_babel_ui_type_selector_dialog_sensitivity_cb), dialog ); | |
143 | gtk_combo_box_set_active ( GTK_COMBO_BOX(widgets->type), last_type ); | |
144 | /* Manually call the callback to fix the state */ | |
f31afc42 | 145 | a_babel_ui_type_selector_dialog_sensitivity_cb ( GTK_COMBO_BOX(widgets->type), dialog ); |
31349009 GB |
146 | |
147 | /* Packing all these widgets */ | |
a8367d25 GB |
148 | GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))); |
149 | gtk_box_pack_start ( box, filename_label, FALSE, FALSE, 5 ); | |
150 | gtk_box_pack_start ( box, widgets->file, FALSE, FALSE, 5 ); | |
151 | gtk_box_pack_start ( box, type_label, FALSE, FALSE, 5 ); | |
152 | gtk_box_pack_start ( box, widgets->type, FALSE, FALSE, 5 ); | |
31349009 GB |
153 | gtk_widget_show_all(dialog); |
154 | } | |
155 | ||
156 | /* See VikDataSourceInterface */ | |
ed691ed1 | 157 | static void datasource_file_get_cmd_string ( datasource_file_widgets_t *widgets, gchar **cmd, gchar **input_file, gpointer not_used ) |
31349009 | 158 | { |
31349009 | 159 | /* Retrieve the file selected */ |
2d8277f1 | 160 | gchar *filename = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(widgets->file) ); |
31349009 GB |
161 | |
162 | /* Memorize the directory for later use */ | |
163 | g_free (last_folder_uri); | |
164 | last_folder_uri = gtk_file_chooser_get_current_folder_uri ( GTK_FILE_CHOOSER(widgets->file) ); | |
165 | last_folder_uri = g_strdup (last_folder_uri); | |
166 | ||
167 | /* Memorize the file filter for later use */ | |
168 | GtkFileFilter *filter = gtk_file_chooser_get_filter ( GTK_FILE_CHOOSER(widgets->file) ); | |
169 | last_file_filter = g_object_get_data ( G_OBJECT(filter), "Babel" ); | |
170 | ||
171 | /* Retrieve and memorize file format selected */ | |
2d8277f1 | 172 | gchar *type = NULL; |
31349009 | 173 | last_type = gtk_combo_box_get_active ( GTK_COMBO_BOX (widgets->type) ); |
b666a8ba | 174 | type = (a_babel_ui_file_type_selector_get ( widgets->type ))->name; |
31349009 GB |
175 | |
176 | /* Build the string */ | |
177 | *cmd = g_strdup_printf( "-i %s", type); | |
178 | *input_file = g_strdup(filename); | |
179 | ||
180 | /* Free memory */ | |
181 | g_free (filename); | |
182 | ||
183 | g_debug(_("using babel args '%s' and file '%s'"), *cmd, *input_file); | |
184 | } | |
185 | ||
186 | /* See VikDataSourceInterface */ | |
187 | static void datasource_file_cleanup ( gpointer data ) | |
188 | { | |
189 | g_free ( data ); | |
190 | } | |
191 |