]> git.street.me.uk Git - andy/viking.git/blob - src/datasource_url.c
Fix small memory leak - free dirpath in all circumstances.
[andy/viking.git] / src / datasource_url.c
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 #include "ui_util.h"
29
30 // Initially was just going to be a URL and always in GPX format
31 // But might as well specify the file type as per datasource_file.c
32 // However in this version we'll cope with no GPSBabel available and in this case just try GPX
33
34 typedef struct {
35         GtkWidget *url;
36         GtkWidget *type;
37 } datasource_url_widgets_t;
38
39 /* The last file format selected */
40 static int last_type = -1;
41
42 static gpointer datasource_url_init ( acq_vik_t *avt );
43 static void datasource_url_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
44 static void datasource_url_get_process_options ( datasource_url_widgets_t *widgets, ProcessOptions *po, DownloadFileOptions *download_options, const gchar *not_used2, const gchar *not_used3 );
45 static void datasource_url_cleanup ( gpointer data );
46
47 VikDataSourceInterface vik_datasource_url_interface = {
48         N_("Acquire from URL"),
49         N_("URL"),
50         VIK_DATASOURCE_AUTO_LAYER_MANAGEMENT,
51         VIK_DATASOURCE_INPUTTYPE_NONE,
52         TRUE,
53         TRUE,
54         TRUE,
55         (VikDataSourceInitFunc)               datasource_url_init,
56         (VikDataSourceCheckExistenceFunc)     NULL,
57         (VikDataSourceAddSetupWidgetsFunc)    datasource_url_add_setup_widgets,
58         (VikDataSourceGetProcessOptionsFunc)  datasource_url_get_process_options,
59         (VikDataSourceProcessFunc)            a_babel_convert_from,
60         (VikDataSourceProgressFunc)           NULL,
61         (VikDataSourceAddProgressWidgetsFunc) NULL,
62         (VikDataSourceCleanupFunc)            datasource_url_cleanup,
63         (VikDataSourceOffFunc)                NULL,
64         NULL,
65         0,
66         NULL,
67         NULL,
68         0
69 };
70
71 static gpointer datasource_url_init ( acq_vik_t *avt )
72 {
73         datasource_url_widgets_t *widgets = g_malloc(sizeof(*widgets));
74         return widgets;
75 }
76
77 static void fill_combo_box (gpointer data, gpointer user_data)
78 {
79         const gchar *label = ((BabelFile*) data)->label;
80         vik_combo_box_text_append (GTK_WIDGET(user_data), label);
81 }
82
83 static gint find_entry = -1;
84 static gint wanted_entry = -1;
85
86 static void find_type (gpointer elem, gpointer user_data)
87 {
88         const gchar *name = ((BabelFile*)elem)->name;
89         const gchar *type_name = user_data;
90         find_entry++;
91         if (!g_strcmp0(name, type_name)) {
92                 wanted_entry = find_entry;
93         }
94 }
95
96 #define VIK_SETTINGS_URL_FILE_DL_TYPE "url_file_download_type"
97
98 static void datasource_url_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
99 {
100         datasource_url_widgets_t *widgets = (datasource_url_widgets_t *)user_data;
101         GtkWidget *label = gtk_label_new (_("URL:"));
102         widgets->url = ui_entry_new ( NULL, GTK_ENTRY_ICON_SECONDARY );
103         // 'ok' when press return in the entry
104         g_signal_connect_swapped ( widgets->url, "activate", G_CALLBACK(a_dialog_response_accept), dialog );
105
106         GtkWidget *type_label = gtk_label_new (_("File type:"));
107
108         if ( last_type < 0 ) {
109                 find_entry = -1;
110                 wanted_entry = -1;
111                 gchar *type = NULL;
112                 if ( a_settings_get_string ( VIK_SETTINGS_URL_FILE_DL_TYPE, &type ) ) {
113                         // Use setting
114                         if ( type )
115                                 g_list_foreach (a_babel_file_list, find_type, type);
116                 }
117                 else {
118                         // Default to GPX if possible
119                         g_list_foreach (a_babel_file_list, find_type, "gpx");
120                 }
121                 // If not found set it to the first entry, otherwise use the entry
122                 last_type = ( wanted_entry < 0 ) ? 0 : wanted_entry;
123         }
124
125         if ( a_babel_available() ) {
126                 widgets->type = vik_combo_box_text_new ();
127                 g_list_foreach (a_babel_file_list, fill_combo_box, widgets->type);
128                 gtk_combo_box_set_active (GTK_COMBO_BOX (widgets->type), last_type);
129         }
130         else {
131                 // Only GPX (not using GPSbabel)
132                 widgets->type = gtk_label_new (_("GPX"));
133         }
134
135         /* Packing all widgets */
136         GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
137         gtk_box_pack_start ( box, label, FALSE, FALSE, 5 );
138         gtk_box_pack_start ( box, widgets->url, FALSE, FALSE, 5 );
139         gtk_box_pack_start ( box, type_label, FALSE, FALSE, 5 );
140         gtk_box_pack_start ( box, widgets->type, FALSE, FALSE, 5 );
141
142         gtk_widget_show_all(dialog);
143 }
144
145 static void datasource_url_get_process_options ( datasource_url_widgets_t *widgets, ProcessOptions *po, DownloadFileOptions *download_options, const gchar *not_used2, const gchar *not_used3 )
146 {
147         // Retrieve the user entered value
148         const gchar *value = gtk_entry_get_text ( GTK_ENTRY(widgets->url) );
149
150         if (GTK_IS_COMBO_BOX (widgets->type) )
151                 last_type = gtk_combo_box_get_active ( GTK_COMBO_BOX (widgets->type) );
152
153         po->input_file_type = NULL; // Default to gpx
154         if ( a_babel_file_list )
155                 po->input_file_type = g_strdup ( ((BabelFile*)g_list_nth_data (a_babel_file_list, last_type))->name );
156
157         po->url = g_strdup ( value );
158
159         // Support .zip + bzip2 files directly
160         download_options->convert_file = a_try_decompress_file;
161         download_options->follow_location = 5;
162 }
163
164 static void datasource_url_cleanup ( gpointer data )
165 {
166         g_free ( data );
167 }