1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
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.
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.
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
22 #include <glib/gi18n.h>
29 // Initially was just going to be a URL and always in GPX format
30 // But might as well specify the file type as per datasource_file.c
31 // However in this version we'll cope with no GPSBabel available and in this case just try GPX
36 } datasource_url_widgets_t;
38 /* The last file format selected */
39 static int last_type = -1;
41 static gpointer datasource_url_init ( acq_vik_t *avt );
42 static void datasource_url_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
43 static void datasource_url_get_process_options ( datasource_url_widgets_t *widgets, ProcessOptions *po, DownloadMapOptions *download_options, const gchar *not_used2, const gchar *not_used3 );
44 static void datasource_url_cleanup ( gpointer data );
46 VikDataSourceInterface vik_datasource_url_interface = {
47 N_("Acquire from URL"),
49 VIK_DATASOURCE_AUTO_LAYER_MANAGEMENT,
50 VIK_DATASOURCE_INPUTTYPE_NONE,
54 (VikDataSourceInitFunc) datasource_url_init,
55 (VikDataSourceCheckExistenceFunc) NULL,
56 (VikDataSourceAddSetupWidgetsFunc) datasource_url_add_setup_widgets,
57 (VikDataSourceGetProcessOptionsFunc) datasource_url_get_process_options,
58 (VikDataSourceProcessFunc) a_babel_convert_from,
59 (VikDataSourceProgressFunc) NULL,
60 (VikDataSourceAddProgressWidgetsFunc) NULL,
61 (VikDataSourceCleanupFunc) datasource_url_cleanup,
62 (VikDataSourceOffFunc) NULL,
70 static gpointer datasource_url_init ( acq_vik_t *avt )
72 datasource_url_widgets_t *widgets = g_malloc(sizeof(*widgets));
76 static void fill_combo_box (gpointer data, gpointer user_data)
78 const gchar *label = ((BabelFile*) data)->label;
79 vik_combo_box_text_append (GTK_WIDGET(user_data), label);
82 static gint find_entry = -1;
83 static gint wanted_entry = -1;
85 static void find_type (gpointer elem, gpointer user_data)
87 const gchar *name = ((BabelFile*)elem)->name;
88 const gchar *type_name = user_data;
90 if (!g_strcmp0(name, type_name)) {
91 wanted_entry = find_entry;
95 #define VIK_SETTINGS_URL_FILE_DL_TYPE "url_file_download_type"
97 static void datasource_url_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
99 datasource_url_widgets_t *widgets = (datasource_url_widgets_t *)user_data;
100 GtkWidget *label = gtk_label_new (_("URL:"));
101 widgets->url = gtk_entry_new ( );
103 GtkWidget *type_label = gtk_label_new (_("File type:"));
105 if ( last_type < 0 ) {
109 if ( a_settings_get_string ( VIK_SETTINGS_URL_FILE_DL_TYPE, &type ) ) {
112 g_list_foreach (a_babel_file_list, find_type, type);
115 // Default to GPX if possible
116 g_list_foreach (a_babel_file_list, find_type, "gpx");
118 // If not found set it to the first entry, otherwise use the entry
119 last_type = ( wanted_entry < 0 ) ? 0 : wanted_entry;
122 if ( a_babel_available() ) {
123 widgets->type = vik_combo_box_text_new ();
124 g_list_foreach (a_babel_file_list, fill_combo_box, widgets->type);
125 gtk_combo_box_set_active (GTK_COMBO_BOX (widgets->type), last_type);
128 // Only GPX (not using GPSbabel)
129 widgets->type = gtk_label_new (_("GPX"));
132 /* Packing all widgets */
133 GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
134 gtk_box_pack_start ( box, label, FALSE, FALSE, 5 );
135 gtk_box_pack_start ( box, widgets->url, FALSE, FALSE, 5 );
136 gtk_box_pack_start ( box, type_label, FALSE, FALSE, 5 );
137 gtk_box_pack_start ( box, widgets->type, FALSE, FALSE, 5 );
139 gtk_widget_show_all(dialog);
142 static void datasource_url_get_process_options ( datasource_url_widgets_t *widgets, ProcessOptions *po, DownloadMapOptions *download_options, const gchar *not_used2, const gchar *not_used3 )
144 // Retrieve the user entered value
145 const gchar *value = gtk_entry_get_text ( GTK_ENTRY(widgets->url) );
147 if (GTK_IS_COMBO_BOX (widgets->type) )
148 last_type = gtk_combo_box_get_active ( GTK_COMBO_BOX (widgets->type) );
150 po->input_file_type = NULL; // Default to gpx
151 if ( a_babel_file_list )
152 po->input_file_type = g_strdup ( ((BabelFile*)g_list_nth_data (a_babel_file_list, last_type))->name );
154 po->url = g_strdup ( value );
155 download_options = NULL;
158 static void datasource_url_cleanup ( gpointer data )