]> git.street.me.uk Git - andy/viking.git/blob - src/datasource_google.c
Merge branch 'GTK+-Updates'
[andy/viking.git] / src / datasource_google.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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 "viking.h"
30 #include "babel.h"
31 #include "gpx.h"
32 #include "acquire.h"
33
34 #define GOOGLE_DIRECTIONS_STRING "maps.google.com/maps?q=from:%s+to:%s&output=js"
35
36 typedef struct {
37   GtkWidget *from_entry, *to_entry;
38 } datasource_google_widgets_t;
39
40 static gchar *last_from_str = NULL;
41 static gchar *last_to_str = NULL;
42
43 static gpointer datasource_google_init( );
44 static void datasource_google_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
45 static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options );
46 static void datasource_google_cleanup ( gpointer data );
47
48 VikDataSourceInterface vik_datasource_google_interface = {
49   N_("Google Directions"),
50   N_("Google Directions"),
51   VIK_DATASOURCE_ADDTOLAYER,
52   VIK_DATASOURCE_INPUTTYPE_NONE,
53   TRUE,
54   TRUE,
55   TRUE,
56   (VikDataSourceInitFunc)               datasource_google_init,
57   (VikDataSourceCheckExistenceFunc)     NULL,
58   (VikDataSourceAddSetupWidgetsFunc)    datasource_google_add_setup_widgets,
59   (VikDataSourceGetCmdStringFunc)       datasource_google_get_cmd_string,
60   (VikDataSourceProcessFunc)            a_babel_convert_from_url,
61   (VikDataSourceProgressFunc)           NULL,
62   (VikDataSourceAddProgressWidgetsFunc) NULL,
63   (VikDataSourceCleanupFunc)            datasource_google_cleanup,
64   (VikDataSourceOffFunc)                NULL,
65 };
66
67 static gpointer datasource_google_init ( )
68 {
69   datasource_google_widgets_t *widgets = g_malloc(sizeof(*widgets));
70   return widgets;
71 }
72
73 static void datasource_google_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
74 {
75   datasource_google_widgets_t *widgets = (datasource_google_widgets_t *)user_data;
76   GtkWidget *from_label, *to_label;
77   from_label = gtk_label_new (_("From:"));
78   widgets->from_entry = gtk_entry_new();
79   to_label = gtk_label_new (_("To:"));
80   widgets->to_entry = gtk_entry_new();
81   if (last_from_str)
82     gtk_entry_set_text(GTK_ENTRY(widgets->from_entry), last_from_str);
83   if (last_to_str)
84     gtk_entry_set_text(GTK_ENTRY(widgets->to_entry), last_to_str);
85   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), from_label, FALSE, FALSE, 5 );
86   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), widgets->from_entry, FALSE, FALSE, 5 );
87   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), to_label, FALSE, FALSE, 5 );
88   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), widgets->to_entry, FALSE, FALSE, 5 );
89   gtk_widget_show_all(dialog);
90 }
91
92 static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options )
93 {
94   /* TODO: special characters handling!!! */
95   gchar *from_quoted, *to_quoted;
96   gchar **from_split, **to_split;
97   from_quoted = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->from_entry) ) );
98   to_quoted = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->to_entry) ) );
99
100   from_split = g_strsplit( from_quoted, " ", 0);
101   to_split = g_strsplit( to_quoted, " ", 0);
102   from_quoted = g_strjoinv( "%20", from_split);
103   to_quoted = g_strjoinv( "%20", to_split);
104
105   *cmd = g_strdup_printf( GOOGLE_DIRECTIONS_STRING, from_quoted, to_quoted );
106   *input_file_type = g_strdup("google");
107   options = NULL;
108
109   g_free(last_from_str);
110   g_free(last_to_str);
111
112   last_from_str = g_strdup( gtk_entry_get_text ( GTK_ENTRY(widgets->from_entry) ));
113   last_to_str = g_strdup( gtk_entry_get_text ( GTK_ENTRY(widgets->to_entry) ));
114
115   g_free(from_quoted);
116   g_free(to_quoted);
117   g_strfreev(from_split);
118   g_strfreev(to_split);
119
120 }
121
122 static void datasource_google_cleanup ( gpointer data )
123 {
124   g_free ( data );
125 }