]>
Commit | Line | Data |
---|---|---|
f77bbb5e 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 | * | |
5 | * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com> | |
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 <gtk/gtk.h> | |
24 | ||
25 | #include "viking.h" | |
26 | #include "acquire.h" | |
27 | #include "babel.h" | |
28 | ||
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 | |
32 | ||
33 | typedef struct { | |
34 | GtkWidget *url; | |
35 | GtkWidget *type; | |
36 | } datasource_url_widgets_t; | |
37 | ||
38 | /* The last file format selected */ | |
39 | static int last_type = -1; | |
40 | ||
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_cmd_string ( datasource_url_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options ); | |
44 | static void datasource_url_cleanup ( gpointer data ); | |
45 | ||
46 | VikDataSourceInterface vik_datasource_url_interface = { | |
47 | N_("Acquire from URL"), | |
48 | N_("URL"), | |
49 | VIK_DATASOURCE_CREATENEWLAYER, | |
50 | VIK_DATASOURCE_INPUTTYPE_NONE, | |
51 | TRUE, | |
52 | TRUE, | |
53 | TRUE, | |
54 | (VikDataSourceInitFunc) datasource_url_init, | |
55 | (VikDataSourceCheckExistenceFunc) NULL, | |
56 | (VikDataSourceAddSetupWidgetsFunc) datasource_url_add_setup_widgets, | |
57 | (VikDataSourceGetCmdStringFunc) datasource_url_get_cmd_string, | |
58 | (VikDataSourceProcessFunc) a_babel_convert_from_url, | |
59 | (VikDataSourceProgressFunc) NULL, | |
60 | (VikDataSourceAddProgressWidgetsFunc) NULL, | |
61 | (VikDataSourceCleanupFunc) datasource_url_cleanup, | |
62 | (VikDataSourceOffFunc) NULL, | |
63 | NULL, | |
64 | 0, | |
65 | NULL, | |
66 | NULL, | |
67 | 0 | |
68 | }; | |
69 | ||
70 | static gpointer datasource_url_init ( acq_vik_t *avt ) | |
71 | { | |
72 | datasource_url_widgets_t *widgets = g_malloc(sizeof(*widgets)); | |
73 | return widgets; | |
74 | } | |
75 | ||
76 | static void fill_combo_box (gpointer data, gpointer user_data) | |
77 | { | |
78 | const gchar *label = ((BabelFile*) data)->label; | |
79 | vik_combo_box_text_append (GTK_WIDGET(user_data), label); | |
80 | } | |
81 | ||
82 | static gint find_entry = -1; | |
83 | static gint wanted_entry = -1; | |
84 | ||
85 | static void find_type (gpointer elem, gpointer user_data) | |
86 | { | |
87 | const gchar *name = ((BabelFile*)elem)->name; | |
88 | const gchar *type_name = user_data; | |
89 | find_entry++; | |
90 | if (!g_strcmp0(name, type_name)) { | |
91 | wanted_entry = find_entry; | |
92 | } | |
93 | } | |
94 | ||
95 | #define VIK_SETTINGS_URL_FILE_DL_TYPE "url_file_download_type" | |
96 | ||
97 | static void datasource_url_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ) | |
98 | { | |
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 ( ); | |
102 | ||
103 | GtkWidget *type_label = gtk_label_new (_("File type:")); | |
104 | ||
105 | if ( last_type < 0 ) { | |
106 | find_entry = -1; | |
107 | wanted_entry = -1; | |
108 | gchar *type = NULL; | |
109 | if ( a_settings_get_string ( VIK_SETTINGS_URL_FILE_DL_TYPE, &type ) ) { | |
110 | // Use setting | |
111 | if ( type ) | |
112 | g_list_foreach (a_babel_file_list, find_type, type); | |
113 | } | |
114 | else { | |
115 | // Default to GPX if possible | |
116 | g_list_foreach (a_babel_file_list, find_type, "gpx"); | |
117 | } | |
118 | // If not found set it to the first entry, otherwise use the entry | |
119 | last_type = ( wanted_entry < 0 ) ? 0 : wanted_entry; | |
120 | } | |
121 | ||
122 | if ( a_babel_file_list ) { | |
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); | |
126 | } | |
127 | else { | |
128 | // Only GPX (not using GPSbabel) | |
129 | widgets->type = gtk_label_new (_("GPX")); | |
130 | } | |
131 | ||
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 ); | |
138 | ||
139 | gtk_widget_show_all(dialog); | |
140 | } | |
141 | ||
142 | static void datasource_url_get_cmd_string ( datasource_url_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options ) | |
143 | { | |
144 | // Retrieve the user entered value | |
145 | const gchar *value = gtk_entry_get_text ( GTK_ENTRY(widgets->url) ); | |
146 | ||
147 | if (GTK_IS_COMBO_BOX (widgets->type) ) | |
148 | last_type = gtk_combo_box_get_active ( GTK_COMBO_BOX (widgets->type) ); | |
149 | ||
150 | *input_file_type = NULL; // Default to gpx | |
151 | if ( a_babel_file_list ) | |
152 | *input_file_type = g_strdup ( ((BabelFile*)g_list_nth_data (a_babel_file_list, last_type))->name ); | |
153 | ||
154 | *cmd = g_strdup ( value ); | |
155 | options = NULL; | |
156 | } | |
157 | ||
158 | static void datasource_url_cleanup ( gpointer data ) | |
159 | { | |
160 | g_free ( data ); | |
161 | } |