]> git.street.me.uk Git - andy/viking.git/blobdiff - src/datasource_google.c
[MAPS] Add OpenSeaMap source
[andy/viking.git] / src / datasource_google.c
index a352882b69563ec28add5d6d17a0e4cc0e7cc7c4..10d4991a73ce7fa7831866a7ab8dd95ef8371713 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <string.h>
+
 #include <glib/gprintf.h>
+#include <glib/gi18n.h>
 
 #include "viking.h"
 #include "babel.h"
 #include "gpx.h"
 #include "acquire.h"
 
-#define GOOGLE_DIRECTIONS_STRING "(wget -O - \"http://maps.google.com/maps?q=%s to %s&output=xml\" 2>/dev/null) | head -3 | tail -1 | sed 's/.*<page>\\(.*\\)<\\/page>.*/<page>\\1<\\/page>/'"
+#define GOOGLE_DIRECTIONS_STRING "maps.google.com/maps?q=from:%s+to:%s&output=kml"
 
 typedef struct {
   GtkWidget *from_entry, *to_entry;
 } datasource_google_widgets_t;
 
+static gchar *last_from_str = NULL;
+static gchar *last_to_str = NULL;
 
-gpointer datasource_google_add_widgets ( GtkWidget *dialog );
-static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_type );        
-static void datasource_google_first_cleanup ( gpointer data );
+static gpointer datasource_google_init( );
+static void datasource_google_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
+static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_file_type );   
+static void datasource_google_cleanup ( gpointer data );
 
 VikDataSourceInterface vik_datasource_google_interface = {
-  "Acquire from Google",
-  VIK_DATASOURCE_SHELL_CMD,
+  N_("Google Directions"),
+  N_("Google Directions"),
+  VIK_DATASOURCE_URL,
   VIK_DATASOURCE_ADDTOLAYER,
-  (VikDataSourceAddWidgetsFunc)                datasource_google_add_widgets,
+  VIK_DATASOURCE_INPUTTYPE_NONE,
+  TRUE,
+  TRUE,
+  (VikDataSourceInitFunc)              datasource_google_init,
+  (VikDataSourceCheckExistenceFunc)    NULL,
+  (VikDataSourceAddSetupWidgetsFunc)   datasource_google_add_setup_widgets,
   (VikDataSourceGetCmdStringFunc)      datasource_google_get_cmd_string,
-  (VikDataSourceFirstCleanupFunc)      datasource_google_first_cleanup,
+  (VikDataSourceProcessFunc)           NULL,
   (VikDataSourceProgressFunc)          NULL,
   (VikDataSourceAddProgressWidgetsFunc)        NULL,
-  (VikDataSourceCleanupFunc)           NULL
+  (VikDataSourceCleanupFunc)           datasource_google_cleanup,
+  (VikDataSourceOffFunc)                NULL,
 };
 
-
-gpointer datasource_google_add_widgets ( GtkWidget *dialog )
+static gpointer datasource_google_init ( )
 {
   datasource_google_widgets_t *widgets = g_malloc(sizeof(*widgets));
+  return widgets;
+}
+
+static void datasource_google_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
+{
+  datasource_google_widgets_t *widgets = (datasource_google_widgets_t *)user_data;
   GtkWidget *from_label, *to_label;
-  from_label = gtk_label_new ("From:");
+  from_label = gtk_label_new (_("From:"));
   widgets->from_entry = gtk_entry_new();
-  to_label = gtk_label_new ("To:");
+  to_label = gtk_label_new (_("To:"));
   widgets->to_entry = gtk_entry_new();
+  if (last_from_str)
+    gtk_entry_set_text(GTK_ENTRY(widgets->from_entry), last_from_str);
+  if (last_to_str)
+    gtk_entry_set_text(GTK_ENTRY(widgets->to_entry), last_to_str);
   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), from_label, FALSE, FALSE, 5 );
   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->from_entry, FALSE, FALSE, 5 );
   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), to_label, FALSE, FALSE, 5 );
   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->to_entry, FALSE, FALSE, 5 );
   gtk_widget_show_all(dialog);
-  return widgets;
 }
 
-static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_type )
+static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_file_type )
 {
   /* TODO: special characters handling!!! */
-  *cmd = g_strdup_printf( GOOGLE_DIRECTIONS_STRING, gtk_entry_get_text ( GTK_ENTRY(widgets->from_entry) ), gtk_entry_get_text ( GTK_ENTRY(widgets->to_entry) ) );
-  *input_type = g_strdup("google");
+  gchar *from_quoted, *to_quoted;
+  gchar **from_split, **to_split;
+  from_quoted = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->from_entry) ) );
+  to_quoted = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->to_entry) ) );
+
+  from_split = g_strsplit( from_quoted, " ", 0);
+  to_split = g_strsplit( to_quoted, " ", 0);
+  from_quoted = g_strjoinv( "%20", from_split);
+  to_quoted = g_strjoinv( "%20", to_split);
+
+  *cmd = g_strdup_printf( GOOGLE_DIRECTIONS_STRING, from_quoted, to_quoted );
+  *input_file_type = g_strdup("kml");
+
+  g_free(last_from_str);
+  g_free(last_to_str);
+
+  last_from_str = g_strdup( gtk_entry_get_text ( GTK_ENTRY(widgets->from_entry) ));
+  last_to_str = g_strdup( gtk_entry_get_text ( GTK_ENTRY(widgets->to_entry) ));
+
+  g_free(from_quoted);
+  g_free(to_quoted);
+  g_strfreev(from_split);
+  g_strfreev(to_split);
 
 }
 
-static void datasource_google_first_cleanup ( gpointer data )
+static void datasource_google_cleanup ( gpointer data )
 {
   g_free ( data );
 }