]>
Commit | Line | Data |
---|---|---|
c0c5893f RN |
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* | |
3 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
4 | * | |
17acdaec | 5 | * Copyright (C) 2014-2015, Rob Norris <rw_norris@hotmail.com> |
c0c5893f RN |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | #include <glib/gi18n.h> | |
23 | #include <glib/gstdio.h> | |
24 | #include <gtk/gtk.h> | |
25 | ||
26 | #include "viking.h" | |
27 | #include "acquire.h" | |
28 | #include "babel.h" | |
29 | #include "geojson.h" | |
30 | ||
31 | typedef struct { | |
32 | GtkWidget *files; | |
33 | GSList *filelist; // Files selected | |
34 | } datasource_geojson_user_data_t; | |
35 | ||
36 | // The last used directory | |
37 | static gchar *last_folder_uri = NULL; | |
38 | ||
39 | static gpointer datasource_geojson_init ( acq_vik_t *avt ); | |
40 | static void datasource_geojson_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ); | |
17acdaec | 41 | static void datasource_geojson_get_process_options ( datasource_geojson_user_data_t *user_data, ProcessOptions *po, gpointer not_used, const gchar *not_used2, const gchar *not_used3 ); |
686baff0 | 42 | static gboolean datasource_geojson_process ( VikTrwLayer *vtl, ProcessOptions *process_options, BabelStatusFunc status_cb, acq_dialog_widgets_t *adw, DownloadFileOptions *options_unused ); |
c0c5893f RN |
43 | static void datasource_geojson_cleanup ( gpointer data ); |
44 | ||
45 | VikDataSourceInterface vik_datasource_geojson_interface = { | |
46 | N_("Acquire from GeoJSON"), | |
47 | N_("GeoJSON"), | |
48 | VIK_DATASOURCE_AUTO_LAYER_MANAGEMENT, | |
49 | VIK_DATASOURCE_INPUTTYPE_NONE, | |
50 | TRUE, | |
51 | FALSE, // We should be able to see the data on the screen so no point in keeping the dialog open | |
52 | FALSE, // Not thread method - open each file in the main loop | |
53 | (VikDataSourceInitFunc) datasource_geojson_init, | |
54 | (VikDataSourceCheckExistenceFunc) NULL, | |
55 | (VikDataSourceAddSetupWidgetsFunc) datasource_geojson_add_setup_widgets, | |
17acdaec | 56 | (VikDataSourceGetProcessOptionsFunc) datasource_geojson_get_process_options, |
c0c5893f RN |
57 | (VikDataSourceProcessFunc) datasource_geojson_process, |
58 | (VikDataSourceProgressFunc) NULL, | |
59 | (VikDataSourceAddProgressWidgetsFunc) NULL, | |
60 | (VikDataSourceCleanupFunc) datasource_geojson_cleanup, | |
61 | (VikDataSourceOffFunc) NULL, | |
62 | NULL, | |
63 | 0, | |
64 | NULL, | |
65 | NULL, | |
66 | 0 | |
67 | }; | |
68 | ||
69 | static gpointer datasource_geojson_init ( acq_vik_t *avt ) | |
70 | { | |
71 | datasource_geojson_user_data_t *user_data = g_malloc(sizeof(datasource_geojson_user_data_t)); | |
72 | user_data->filelist = NULL; | |
73 | return user_data; | |
74 | } | |
75 | ||
76 | static void datasource_geojson_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ) | |
77 | { | |
78 | datasource_geojson_user_data_t *ud = (datasource_geojson_user_data_t *)user_data; | |
79 | ||
80 | ud->files = gtk_file_chooser_widget_new ( GTK_FILE_CHOOSER_ACTION_OPEN ); | |
81 | ||
82 | // try to make it a nice size - otherwise seems to default to something impractically small | |
83 | gtk_window_set_default_size ( GTK_WINDOW (dialog) , 600, 300 ); | |
84 | ||
85 | if ( last_folder_uri ) | |
86 | gtk_file_chooser_set_current_folder_uri ( GTK_FILE_CHOOSER(ud->files), last_folder_uri ); | |
87 | ||
88 | GtkFileChooser *chooser = GTK_FILE_CHOOSER ( ud->files ); | |
89 | ||
90 | // Add filters | |
91 | GtkFileFilter *filter; | |
92 | filter = gtk_file_filter_new (); | |
93 | gtk_file_filter_set_name ( filter, _("All") ); | |
94 | gtk_file_filter_add_pattern ( filter, "*" ); | |
95 | gtk_file_chooser_add_filter ( chooser, filter ); | |
96 | ||
97 | filter = gtk_file_filter_new (); | |
98 | gtk_file_filter_set_name ( filter, _("GeoJSON") ); | |
99 | gtk_file_filter_add_pattern ( filter, "*.geojson" ); | |
100 | gtk_file_chooser_add_filter ( chooser, filter ); | |
101 | ||
102 | // Default to geojson | |
103 | gtk_file_chooser_set_filter ( chooser, filter ); | |
104 | ||
105 | // Allow selecting more than one | |
106 | gtk_file_chooser_set_select_multiple ( chooser, TRUE ); | |
107 | ||
108 | // Packing all widgets | |
109 | GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))); | |
110 | gtk_box_pack_start ( box, ud->files, TRUE, TRUE, 0 ); | |
111 | ||
112 | gtk_widget_show_all ( dialog ); | |
113 | } | |
114 | ||
17acdaec | 115 | static void datasource_geojson_get_process_options ( datasource_geojson_user_data_t *userdata, ProcessOptions *po, gpointer not_used, const gchar *not_used2, const gchar *not_used3 ) |
c0c5893f RN |
116 | { |
117 | // Retrieve the files selected | |
118 | userdata->filelist = gtk_file_chooser_get_filenames ( GTK_FILE_CHOOSER(userdata->files) ); // Not reusable !! | |
119 | ||
120 | // Memorize the directory for later reuse | |
121 | g_free ( last_folder_uri ); | |
122 | last_folder_uri = gtk_file_chooser_get_current_folder_uri ( GTK_FILE_CHOOSER(userdata->files) ); | |
c0c5893f RN |
123 | |
124 | // TODO Memorize the file filter for later reuse? | |
125 | //GtkFileFilter *filter = gtk_file_chooser_get_filter ( GTK_FILE_CHOOSER(userdata->files) ); | |
126 | ||
127 | // return some value so *thread* processing will continue | |
17acdaec | 128 | po->babelargs = g_strdup ("fake command"); // Not really used, thus no translations |
c0c5893f RN |
129 | } |
130 | ||
131 | /** | |
132 | * Process selected files and try to generate waypoints storing them in the given vtl | |
133 | */ | |
686baff0 | 134 | static gboolean datasource_geojson_process ( VikTrwLayer *vtl, ProcessOptions *process_options, BabelStatusFunc status_cb, acq_dialog_widgets_t *adw, DownloadFileOptions *options_unused ) |
c0c5893f RN |
135 | { |
136 | datasource_geojson_user_data_t *user_data = (datasource_geojson_user_data_t *)adw->user_data; | |
137 | ||
138 | // Process selected files | |
139 | GSList *cur_file = user_data->filelist; | |
140 | while ( cur_file ) { | |
141 | gchar *filename = cur_file->data; | |
142 | ||
143 | gchar *gpx_filename = a_geojson_import_to_gpx ( filename ); | |
144 | if ( gpx_filename ) { | |
145 | // Important that this process is run in the main thread | |
146 | vik_window_open_file ( adw->vw, gpx_filename, FALSE ); | |
147 | // Delete the temporary file | |
85e9e947 | 148 | (void)g_remove (gpx_filename); |
c0c5893f RN |
149 | g_free (gpx_filename); |
150 | } | |
151 | else { | |
152 | gchar* msg = g_strdup_printf ( _("Unable to import from: %s"), filename ); | |
153 | vik_window_statusbar_update ( adw->vw, msg, VIK_STATUSBAR_INFO ); | |
154 | g_free (msg); | |
155 | } | |
156 | ||
157 | g_free ( filename ); | |
158 | cur_file = g_slist_next ( cur_file ); | |
159 | } | |
160 | ||
161 | // Free memory | |
162 | g_slist_free ( user_data->filelist ); | |
163 | ||
164 | // No failure | |
165 | return TRUE; | |
166 | } | |
167 | ||
168 | static void datasource_geojson_cleanup ( gpointer data ) | |
169 | { | |
170 | g_free ( data ); | |
171 | } |