]> git.street.me.uk Git - andy/viking.git/commitdiff
Generalize Acquire Directions
authorGuilhem Bonnefille <guilhem.bonnefille@gmail.com>
Sun, 23 Jun 2013 07:29:26 +0000 (09:29 +0200)
committerGuilhem Bonnefille <guilhem.bonnefille@gmail.com>
Sun, 23 Jun 2013 07:29:26 +0000 (09:29 +0200)
Since we have a generic routing framework, we can replace
Google Routing feature by a more generic solution.

The user is now able to select the engine for the route
computation.

Signed-off-by: Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
16 files changed:
help/C/viking.xml
po/POTFILES.in
src/Makefile.am
src/babel.c
src/babel.h
src/datasource_google.c [deleted file]
src/datasource_routing.c [new file with mode: 0644]
src/datasources.h
src/google.c
src/menu.xml.h
src/vikrouting.h
src/vikroutingengine.c
src/vikroutingengine.h
src/vikroutingwebengine.c
src/viktrwlayer.c
src/vikwindow.c

index b35c2b0d8f2c14aafbe9b8c16ad1fac5d539d6d9..177104bfb65e03376d481474996dfb4d754f7ec1 100644 (file)
@@ -547,6 +547,16 @@ This gets <emphasis>interesting</emphasis> points from Wikipedia for the specifi
 </para>
 </section>
 
+<section>
+<title>From Routing</title>
+<para>
+<menuchoice><guimenu>File</guimenu><guimenuitem>Acquire</guimenuitem><guimenuitem>From Directions</guimenuitem></menuchoice>
+</para>
+<para>
+This gets a route from given directions.
+</para>
+</section>
+
 </section> <!-- End Acquire -->
 
 <section><title>Print</title>
@@ -2474,6 +2484,16 @@ Accept: */*
               <term>url-stop-ll</term>
               <listitem><para>the part of the URL setting the end point location, parametrized in the spirit of C printf format, with 2 "%s" for coordinates (eg. "&amp;start=%s,%s")</para></listitem>
             </varlistentry>
+            <varlistentry>
+              <term>url-start-dir</term>
+              <listitem><para>the part of the URL setting the starting point location for direction based routing, parametrized in the spirit of C printf format, with one "%s" for direction (eg. "&amp;start=%s")</para>
+                                               <para>(Optional)</para></listitem>
+            </varlistentry>
+            <varlistentry>
+              <term>url-stop-dir</term>
+              <listitem><para>the part of the URL setting the end point location for direction based routing, parametrized in the spirit of C printf format, with one "%s" for direction (eg. "&amp;start=%s")</para>
+                                               <para>(Optional)</para></listitem>
+            </varlistentry>
             <varlistentry>
               <term>referer</term>
               <listitem><para>an URL to serve as referer for the HTTP request (eg. "http://hostname/")</para></listitem>
index 968dbe21c18bcbbab19b66ea95cf1ec944ed6015..bc53f2696b009168df57dbaac28da510e77662da 100644 (file)
@@ -13,10 +13,10 @@ src/googlesearch.c
 src/datasource_file.c
 src/datasource_gc.c
 src/datasource_geotag.c
-src/datasource_google.c
 src/datasource_gps.c
 src/datasource_osm.c
 src/datasource_osm_my_traces.c
+src/datasource_routing.c
 src/datasource_wikipedia.c
 src/dem.c
 src/download.c
index 7c98e47a20ed719a986669b40552eb4da40faec0..799e92c65830118e66c6c5360430b7c2cb2598cf 100644 (file)
@@ -108,7 +108,7 @@ libviking_a_SOURCES = \
        babel.c babel.h \
        datasource_file.c \
        datasource_gps.c datasource_gps.h \
-       datasource_google.c \
+       datasource_routing.c \
        datasource_gc.c \
        datasource_bfilter.c \
        datasource_wikipedia.c \
index c5f8c615143cb966ddb5e3562f05c1a9ad31396a..5d30af91334d71a0471d61cda4ee59f6cdc458f1 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
  * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com>
+ * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */
 #define BASH_LOCATION "/bin/bash"
 
+/**
+ * List of supported protocols.
+ */
+const gchar *PROTOS[] = { "http://", "https://", "ftp://", NULL };
+
 /**
  * Path to gpsbabel
  */
@@ -366,6 +372,43 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
   return ret;
 }
 
+/**
+ * a_babel_convert_from_url_or_shell:
+ * @vt: The #VikTrwLayer where to insert the collected data
+ * @url: the URL to fetch
+ * @cb:               Optional callback function. Same usage as in a_babel_convert().
+ * @user_data: passed along to cb
+ * @options:   download options. Maybe NULL.
+ *
+ * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type.
+ * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX.
+ *
+ * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
+ *
+ */
+gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
+{
+  
+  /* Check nature of input */
+  gboolean isUrl = FALSE;
+  int i = 0;
+  for (i = 0 ; PROTOS[i] != NULL ; i++)
+  {
+    const gchar *proto = PROTOS[i];
+    if (strncmp (input, proto, strlen(proto)) == 0)
+    {
+      /* Procotol matches: save result */
+      isUrl = TRUE;
+    }
+  }
+  
+  /* Do the job */
+  if (isUrl)
+    return a_babel_convert_from_url (vt, input, input_type, cb, user_data, options);
+  else
+    return a_babel_convert_from_shellcommand (vt, input, input_type, cb, user_data, options);
+}
+
 static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
 {
   // Now strips out invisible tracks and waypoints
index a627d05531ee07d2e3588b7ab00829e6c5c4346a..6fe4a079b51ab199681725acd2fc3d4a30dcdf39 100644 (file)
@@ -99,6 +99,7 @@ gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFun
 gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *file, BabelStatusFunc cb, gpointer user_data, gpointer options );
 gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data, gpointer options );
 gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options );
+gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options );
 gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *file, BabelStatusFunc cb, gpointer user_data );
 
 void a_babel_init ();
diff --git a/src/datasource_google.c b/src/datasource_google.c
deleted file mode 100644 (file)
index 8f557d3..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
- *
- * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * 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 "maps.google.com/maps?q=from:%s+to:%s&output=js"
-
-typedef struct {
-  GtkWidget *from_entry, *to_entry;
-} datasource_google_widgets_t;
-
-static gchar *last_from_str = NULL;
-static gchar *last_to_str = NULL;
-
-static gpointer datasource_google_init ( acq_vik_t *avt );
-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, DownloadMapOptions *options );
-static void datasource_google_cleanup ( gpointer data );
-
-VikDataSourceInterface vik_datasource_google_interface = {
-  N_("Google Directions"),
-  N_("Google Directions"),
-  VIK_DATASOURCE_ADDTOLAYER,
-  VIK_DATASOURCE_INPUTTYPE_NONE,
-  TRUE,
-  TRUE,
-  TRUE,
-  (VikDataSourceInitFunc)              datasource_google_init,
-  (VikDataSourceCheckExistenceFunc)    NULL,
-  (VikDataSourceAddSetupWidgetsFunc)   datasource_google_add_setup_widgets,
-  (VikDataSourceGetCmdStringFunc)      datasource_google_get_cmd_string,
-  (VikDataSourceProcessFunc)            a_babel_convert_from_url,
-  (VikDataSourceProgressFunc)          NULL,
-  (VikDataSourceAddProgressWidgetsFunc)        NULL,
-  (VikDataSourceCleanupFunc)           datasource_google_cleanup,
-  (VikDataSourceOffFunc)                NULL,
-};
-
-static gpointer datasource_google_init ( acq_vik_t *avt )
-{
-  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:"));
-  widgets->from_entry = gtk_entry_new();
-  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);
-
-  /* Packing all these widgets */
-  GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
-  gtk_box_pack_start ( box, from_label, FALSE, FALSE, 5 );
-  gtk_box_pack_start ( box, widgets->from_entry, FALSE, FALSE, 5 );
-  gtk_box_pack_start ( box, to_label, FALSE, FALSE, 5 );
-  gtk_box_pack_start ( box, widgets->to_entry, FALSE, FALSE, 5 );
-  gtk_widget_show_all(dialog);
-}
-
-static void datasource_google_get_cmd_string ( datasource_google_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options )
-{
-  /* TODO: special characters handling!!! */
-  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("google");
-  options = NULL;
-
-  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_cleanup ( gpointer data )
-{
-  g_free ( data );
-}
diff --git a/src/datasource_routing.c b/src/datasource_routing.c
new file mode 100644 (file)
index 0000000..c3cd2de
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
+ *
+ * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * 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"
+#include "vikrouting.h"
+
+typedef struct {
+  GtkWidget *engines_combo;
+  GtkWidget *from_entry, *to_entry;
+} datasource_routing_widgets_t;
+
+/* Memory of previous selection */
+static gint last_engine = 0;
+static gchar *last_from_str = NULL;
+static gchar *last_to_str = NULL;
+
+static gpointer datasource_routing_init ( acq_vik_t *avt );
+static void datasource_routing_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
+static void datasource_routing_get_cmd_string ( datasource_routing_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options );
+static void datasource_routing_cleanup ( gpointer data );
+
+VikDataSourceInterface vik_datasource_routing_interface = {
+  N_("Directions"),
+  N_("Directions"),
+  VIK_DATASOURCE_ADDTOLAYER,
+  VIK_DATASOURCE_INPUTTYPE_NONE,
+  TRUE,
+  TRUE,
+  TRUE,
+  (VikDataSourceInitFunc)              datasource_routing_init,
+  (VikDataSourceCheckExistenceFunc)    NULL,
+  (VikDataSourceAddSetupWidgetsFunc)   datasource_routing_add_setup_widgets,
+  (VikDataSourceGetCmdStringFunc)      datasource_routing_get_cmd_string,
+  (VikDataSourceProcessFunc)            a_babel_convert_from_url_or_shell,
+  (VikDataSourceProgressFunc)          NULL,
+  (VikDataSourceAddProgressWidgetsFunc)        NULL,
+  (VikDataSourceCleanupFunc)           datasource_routing_cleanup,
+  (VikDataSourceOffFunc)                NULL,
+};
+
+static gpointer datasource_routing_init ( acq_vik_t *avt )
+{
+  datasource_routing_widgets_t *widgets = g_malloc(sizeof(*widgets));
+  return widgets;
+}
+
+static void datasource_routing_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
+{
+  datasource_routing_widgets_t *widgets = (datasource_routing_widgets_t *)user_data;
+  GtkWidget *engine_label, *from_label, *to_label;
+  
+  /* Engine selector */
+  engine_label = gtk_label_new (_("Engine:"));
+  widgets->engines_combo = vik_routing_ui_selector_new ((Predicate)vik_routing_engine_supports_direction, NULL);
+  gtk_combo_box_set_active (GTK_COMBO_BOX (widgets->engines_combo), last_engine);
+  
+  /* From and To entries */
+  from_label = gtk_label_new (_("From:"));
+  widgets->from_entry = gtk_entry_new();
+  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);
+  
+  /* Packing all these widgets */
+  GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
+  gtk_box_pack_start ( box, engine_label, FALSE, FALSE, 5 );
+  gtk_box_pack_start ( box, widgets->engines_combo, FALSE, FALSE, 5 );
+  gtk_box_pack_start ( box, from_label, FALSE, FALSE, 5 );
+  gtk_box_pack_start ( box, widgets->from_entry, FALSE, FALSE, 5 );
+  gtk_box_pack_start ( box, to_label, FALSE, FALSE, 5 );
+  gtk_box_pack_start ( box, widgets->to_entry, FALSE, FALSE, 5 );
+  gtk_widget_show_all(dialog);
+}
+
+static void datasource_routing_get_cmd_string ( datasource_routing_widgets_t *widgets, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options )
+{
+  const gchar *from, *to;
+  
+  /* Retrieve directions */
+  from = gtk_entry_get_text ( GTK_ENTRY(widgets->from_entry) );
+  to = gtk_entry_get_text ( GTK_ENTRY(widgets->to_entry) );
+
+  /* Retrieve engine */
+  last_engine = gtk_combo_box_get_active ( GTK_COMBO_BOX(widgets->engines_combo) );
+  VikRoutingEngine *engine = vik_routing_ui_selector_get_nth ( widgets->engines_combo, last_engine );
+  
+  *cmd = vik_routing_engine_get_cmd_from_directions ( engine, from, to );
+  *input_file_type = g_strdup ( vik_routing_engine_get_format (engine) );
+  options = NULL;
+
+  /* Save last selection */
+  g_free ( last_from_str );
+  g_free ( last_to_str );
+
+  last_from_str = g_strdup( from );
+  last_to_str = g_strdup( to );
+}
+
+static void datasource_routing_cleanup ( gpointer data )
+{
+  g_free ( data );
+}
index 445abce300fef30f89ed89c49ff0ef2247104d55..10df1a6fe341d706617deae2ab43b93b3471198b 100644 (file)
@@ -27,9 +27,7 @@ G_BEGIN_DECLS
 
 extern VikDataSourceInterface vik_datasource_gps_interface;
 extern VikDataSourceInterface vik_datasource_file_interface;
-#ifdef VIK_CONFIG_GOOGLE
-extern VikDataSourceInterface vik_datasource_google_interface;
-#endif
+extern VikDataSourceInterface vik_datasource_routing_interface;
 #ifdef VIK_CONFIG_OPENSTREETMAP
 extern VikDataSourceInterface vik_datasource_osm_interface;
 extern VikDataSourceInterface vik_datasource_osm_my_traces_interface;
index 32f5efb85ec5c7ebc03ec55d92d09e1b3c017fd0..2e1aa61631274e0a43c72982d3aaf9456e565bf2 100644 (file)
@@ -59,6 +59,8 @@ void google_init () {
     "url-base", "http://maps.google.com/maps?output=js&q=",
     "url-start-ll", "from:%s,%s",
     "url-stop-ll", "+to:%s,%s",
+    "url-start-dir", "from:%s",
+    "url-stop-dir", "+to:%s",
     "referer", "http://maps.google.com/",
     NULL);
   vik_routing_register ( VIK_ROUTING_ENGINE ( routing ) );
index 792910e50c10bd054e28973b66df127defa78dee..d5f8eaf5fcc1835087d653073731fb87ce4ddfe0 100644 (file)
@@ -19,9 +19,7 @@ static const char *menu_xml =
        "      <menu action='Acquire'>"
        "        <menuitem action='AcquireGPS'/>"
        "        <menuitem action='AcquireGPSBabel'/>"
-#ifdef VIK_CONFIG_GOOGLE
-       "        <menuitem action='AcquireGoogle'/>"
-#endif
+       "        <menuitem action='AcquireRouting'/>"
 #ifdef VIK_CONFIG_OPENSTREETMAP
        "        <menuitem action='AcquireOSM'/>"
        "        <menuitem action='AcquireMyOSM'/>"
index 9c018cdda6e01e7418b7b8e1c40d4cec92fbfceb..8e9aa2d56f0c0b9db724f2c0a8c88f48d502bd62 100644 (file)
@@ -27,7 +27,6 @@
 
 G_BEGIN_DECLS
 
-
 /* Default */
 void vik_routing_default_find ( VikTrwLayer *vt, struct LatLon start, struct LatLon end );
 
index 7099dfdfe227b91305a176db950ab44d7ebb4518..5e20b4a1c02b70b03159a99371585412a17b90bc 100644 (file)
@@ -141,7 +141,8 @@ vik_routing_engine_class_init ( VikRoutingEngineClass *klass )
 
   routing_class = VIK_ROUTING_ENGINE_CLASS ( klass );
   routing_class->find = NULL;
-
+  routing_class->supports_direction = NULL;
+  routing_class->get_cmd_from_directions = NULL;
 
   pspec = g_param_spec_string ("id",
                                "Identifier",
@@ -257,3 +258,42 @@ vik_routing_engine_get_format ( VikRoutingEngine *self )
 
   return priv->format;
 }
+
+/**
+ * vik_routing_engine_supports_direction:
+ * 
+ * Returns: %TRUE if this engine supports the route finding based on directions
+ */
+gboolean
+vik_routing_engine_supports_direction ( VikRoutingEngine *self )
+{
+  VikRoutingEngineClass *klass;
+
+  g_return_val_if_fail ( VIK_IS_ROUTING_ENGINE (self), FALSE );
+  klass = VIK_ROUTING_ENGINE_GET_CLASS( self );
+  g_return_val_if_fail ( klass->supports_direction != NULL, FALSE );
+
+  return klass->supports_direction( self );
+}
+
+/**
+ * vik_routing_engine_get_cmd_from_directions:
+ * @self: routing engine
+ * @start: the start direction
+ * @end: the end direction
+ *
+ * Compute a "cmd" for acquire framework.
+ *
+ * Returns: the computed cmd
+ */
+gchar *
+vik_routing_engine_get_cmd_from_directions ( VikRoutingEngine *self, const gchar *start, const gchar *end )
+{
+  VikRoutingEngineClass *klass;
+
+  g_return_val_if_fail ( VIK_IS_ROUTING_ENGINE (self), NULL );
+  klass = VIK_ROUTING_ENGINE_GET_CLASS( self );
+  g_return_val_if_fail ( klass->get_cmd_from_directions != NULL, NULL );
+
+  return klass->get_cmd_from_directions( self, start, end );
+}
index ab707f695b6a241d813105c09d90185f443f0794..a5da970994a0854f603944614cae9bdc4a7d6e3f 100644 (file)
@@ -46,6 +46,8 @@ struct _VikRoutingEngineClass
 {
   GObjectClass object_class;
   int (*find)(VikRoutingEngine *self, VikTrwLayer *vt, struct LatLon start, struct LatLon end);
+  gchar *(*get_cmd_from_directions)(VikRoutingEngine *self, const gchar *start, const gchar *end);
+  gboolean (*supports_direction)(VikRoutingEngine *self);
 };
 
 GType vik_routing_engine_get_type ();
@@ -55,12 +57,15 @@ struct _VikRoutingEngine {
 };
 
 int vik_routing_engine_find ( VikRoutingEngine *self, VikTrwLayer *vt, struct LatLon start, struct LatLon end );
+gchar *vik_routing_engine_get_cmd_from_directions ( VikRoutingEngine *self, const gchar *start, const gchar *end );
 
 /* Acessors */
 gchar *vik_routing_engine_get_id ( VikRoutingEngine *self );
 gchar *vik_routing_engine_get_label ( VikRoutingEngine *self );
 gchar *vik_routing_engine_get_format ( VikRoutingEngine *self );
 
+gboolean vik_routing_engine_supports_direction ( VikRoutingEngine *self );
+
 G_END_DECLS
 
 #endif
index b070c87f75602ccdfe5ba72418e69280d5ae0469..4377a2232f436c1b21007254cbcb60eb2939dad7 100644 (file)
 static void vik_routing_web_engine_finalize ( GObject *gob );
 
 static int vik_routing_web_engine_find ( VikRoutingEngine *self, VikTrwLayer *vtl, struct LatLon start, struct LatLon end );
+static gchar *vik_routing_web_engine_get_cmd_from_directions(VikRoutingEngine *self, const gchar *start, const gchar *end);
+static gboolean vik_routing_web_engine_supports_direction(VikRoutingEngine *self);
 
 typedef struct _VikRoutingWebEnginePrivate VikRoutingWebEnginePrivate;
 struct _VikRoutingWebEnginePrivate
 {
        gchar *url_base;
+       
+       /* LatLon */
        gchar *url_start_ll_fmt;
        gchar *url_stop_ll_fmt;
        gchar *url_via_ll_fmt;
 
+       /* Directions */
+       gchar *url_start_dir_fmt;
+       gchar *url_stop_dir_fmt;
+
        DownloadMapOptions options;
 };
 
@@ -63,10 +71,16 @@ enum
   PROP_0,
 
   PROP_URL_BASE,
+  
+  /* LatLon */
   PROP_URL_START_LL,
   PROP_URL_STOP_LL,
   PROP_URL_VIA_LL,
 
+  /* Direction */
+  PROP_URL_START_DIR,
+  PROP_URL_STOP_DIR,
+
   PROP_REFERER,
   PROP_FOLLOW_LOCATION,
 };
@@ -103,6 +117,16 @@ vik_routing_web_engine_set_property (GObject      *object,
       priv->url_via_ll_fmt = g_strdup(g_value_get_string (value));
       break;
 
+    case PROP_URL_START_DIR:
+      g_free (priv->url_start_dir_fmt);
+      priv->url_start_dir_fmt = g_strdup(g_value_get_string (value));
+      break;
+
+    case PROP_URL_STOP_DIR:
+      g_free (priv->url_stop_dir_fmt);
+      priv->url_stop_dir_fmt = g_strdup(g_value_get_string (value));
+      break;
+
     case PROP_REFERER:
       g_free (priv->options.referer);
       priv->options.referer = g_value_dup_string (value);
@@ -145,6 +169,14 @@ vik_routing_web_engine_get_property (GObject    *object,
       g_value_set_string (value, priv->url_via_ll_fmt);
       break;
 
+    case PROP_URL_START_DIR:
+      g_value_set_string (value, priv->url_start_dir_fmt);
+      break;
+
+    case PROP_URL_STOP_DIR:
+      g_value_set_string (value, priv->url_stop_dir_fmt);
+      break;
+
     case PROP_REFERER:
       g_value_set_string (value, priv->options.referer);
       break;
@@ -175,6 +207,8 @@ static void vik_routing_web_engine_class_init ( VikRoutingWebEngineClass *klass
   parent_class = VIK_ROUTING_ENGINE_CLASS (klass);
 
   parent_class->find = vik_routing_web_engine_find;
+  parent_class->supports_direction = vik_routing_web_engine_supports_direction;
+  parent_class->get_cmd_from_directions = vik_routing_web_engine_get_cmd_from_directions;
 
   /**
    * VikRoutingWebEngine:url-base:
@@ -228,6 +262,32 @@ static void vik_routing_web_engine_class_init ( VikRoutingWebEngineClass *klass
   g_object_class_install_property (object_class, PROP_URL_VIA_LL, pspec);
 
 
+  /**
+   * VikRoutingWebEngine:url-start-dir:
+   *
+   * The part of the request hosting the end point.
+   */
+  pspec = g_param_spec_string ("url-start-dir",
+                               "Start part of the URL",
+                               "The part of the request hosting the start point",
+                               NULL /* default value */,
+                               G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+  g_object_class_install_property (object_class, PROP_URL_START_DIR, pspec);
+    
+
+  /**
+   * VikRoutingWebEngine:url-stop-dir:
+   *
+   * The part of the request hosting the end point.
+   */
+  pspec = g_param_spec_string ("url-stop-dir",
+                               "Stop part of the URL",
+                               "The part of the request hosting the end point",
+                               NULL /* default value */,
+                               G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+  g_object_class_install_property (object_class, PROP_URL_STOP_DIR, pspec);
+
+
   /**
    * VikRoutingWebEngine:referer:
    *
@@ -263,10 +323,16 @@ static void vik_routing_web_engine_init ( VikRoutingWebEngine *self )
   VikRoutingWebEnginePrivate *priv = VIK_ROUTING_WEB_ENGINE_PRIVATE ( self );
 
   priv->url_base = NULL;
+  
+  /* LatLon */
   priv->url_start_ll_fmt = NULL;
   priv->url_stop_ll_fmt = NULL;
   priv->url_via_ll_fmt = NULL;
 
+  /* Directions */
+  priv->url_start_dir_fmt = NULL;
+  priv->url_stop_dir_fmt = NULL;
+
   priv->options.referer = NULL;
   priv->options.follow_location = 0;
   priv->options.check_file = NULL;
@@ -280,6 +346,8 @@ static void vik_routing_web_engine_finalize ( GObject *gob )
 
   g_free (priv->url_base);
   priv->url_base = NULL;
+  
+  /* LatLon */
   g_free (priv->url_start_ll_fmt);
   priv->url_start_ll_fmt = NULL;
   g_free (priv->url_stop_ll_fmt);
@@ -287,6 +355,12 @@ static void vik_routing_web_engine_finalize ( GObject *gob )
   g_free (priv->url_via_ll_fmt);
   priv->url_via_ll_fmt = NULL;
 
+  /* Directions */
+  g_free (priv->url_start_dir_fmt);
+  priv->url_start_dir_fmt = NULL;
+  g_free (priv->url_stop_dir_fmt);
+  priv->url_stop_dir_fmt = NULL;
+
   g_free (priv->options.referer);
   priv->options.referer = NULL;
 
@@ -319,7 +393,7 @@ vik_routing_web_engine_get_url_for_coords ( VikRoutingEngine *self, struct LatLo
        gchar *startURL;
        gchar *endURL;
        gchar *url;
-
+       
        g_return_val_if_fail ( VIK_IS_ROUTING_WEB_ENGINE (self), NULL);
 
        VikRoutingWebEnginePrivate *priv = VIK_ROUTING_WEB_ENGINE_PRIVATE ( self );
@@ -356,3 +430,48 @@ vik_routing_web_engine_find ( VikRoutingEngine *self, VikTrwLayer *vtl, struct L
 
   return ret;
 }
+
+static gchar *
+vik_routing_web_engine_get_cmd_from_directions ( VikRoutingEngine *self, const gchar *start, const gchar *end )
+{
+  g_return_val_if_fail ( VIK_IS_ROUTING_WEB_ENGINE (self), NULL);
+
+  VikRoutingWebEnginePrivate *priv = VIK_ROUTING_WEB_ENGINE_PRIVATE ( self );
+
+  g_return_val_if_fail ( priv->url_base != NULL, NULL);
+  g_return_val_if_fail ( priv->url_start_dir_fmt != NULL, NULL);
+  g_return_val_if_fail ( priv->url_stop_dir_fmt != NULL, NULL);
+
+  gchar *from_quoted, *to_quoted;
+  gchar **from_split, **to_split;
+  from_quoted = g_shell_quote ( start );
+  to_quoted = g_shell_quote ( end );
+
+  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);
+
+  gchar *url_fmt = g_strconcat ( priv->url_base, priv->url_start_dir_fmt, priv->url_stop_dir_fmt, NULL );
+  gchar *url = g_strdup_printf ( url_fmt, from_quoted, to_quoted );
+
+  g_free ( url_fmt );
+
+  g_free(from_quoted);
+  g_free(to_quoted);
+  g_strfreev(from_split);
+  g_strfreev(to_split);
+
+  return url;
+}
+
+static gboolean
+vik_routing_web_engine_supports_direction ( VikRoutingEngine *self )
+{
+  g_return_val_if_fail ( VIK_IS_ROUTING_WEB_ENGINE (self), FALSE);
+
+  VikRoutingWebEnginePrivate *priv = VIK_ROUTING_WEB_ENGINE_PRIVATE ( self );
+
+  return (priv->url_start_dir_fmt) != NULL;
+}
index ef7f8caf6f8d38c6b52df877a386f6bf07d82221..02a157fc4f03160a9e5d5448a6d8971b538b1ea0 100644 (file)
@@ -300,9 +300,7 @@ static void trw_layer_geotagging_track ( gpointer pass_along[6] );
 static void trw_layer_geotagging ( gpointer lav[2] );
 #endif
 static void trw_layer_acquire_gps_cb ( gpointer lav[2] );
-#ifdef VIK_CONFIG_GOOGLE
-static void trw_layer_acquire_google_cb ( gpointer lav[2] );
-#endif
+static void trw_layer_acquire_routing_cb ( gpointer lav[2] );
 #ifdef VIK_CONFIG_OPENSTREETMAP
 static void trw_layer_acquire_osm_cb ( gpointer lav[2] );
 static void trw_layer_acquire_osm_my_traces_cb ( gpointer lav[2] );
@@ -3039,20 +3037,18 @@ static void trw_layer_acquire_gps_cb ( gpointer lav[2] )
   a_acquire ( vw, vlp, vvp, &vik_datasource_gps_interface, NULL, NULL );
 }
 
-#ifdef VIK_CONFIG_GOOGLE
 /*
- * Acquire into this TRW Layer from Google Directions
+ * Acquire into this TRW Layer from Directions
  */
-static void trw_layer_acquire_google_cb ( gpointer lav[2] )
+static void trw_layer_acquire_routing_cb ( gpointer lav[2] )
 {
   VikTrwLayer *vtl = VIK_TRW_LAYER(lav[0]);
   VikLayersPanel *vlp = VIK_LAYERS_PANEL(lav[1]);
   VikWindow *vw = (VikWindow *)(VIK_GTK_WINDOW_FROM_LAYER(vtl));
   VikViewport *vvp =  vik_window_viewport(vw);
 
-  a_acquire ( vw, vlp, vvp, &vik_datasource_google_interface, NULL, NULL );
+  a_acquire ( vw, vlp, vvp, &vik_datasource_routing_interface, NULL, NULL );
 }
-#endif
 
 #ifdef VIK_CONFIG_OPENSTREETMAP
 /*
@@ -3514,12 +3510,11 @@ static void trw_layer_add_menu_items ( VikTrwLayer *vtl, GtkMenu *menu, gpointer
   gtk_menu_shell_append (GTK_MENU_SHELL (acquire_submenu), item);
   gtk_widget_show ( item );
 
-#ifdef VIK_CONFIG_GOOGLE
-  item = gtk_menu_item_new_with_mnemonic ( _("From Google _Directions...") );
-  g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_acquire_google_cb), pass_along );
+  /* FIXME: only add menu when at least a routing engine has support for Directions */
+  item = gtk_menu_item_new_with_mnemonic ( _("From _Directions...") );
+  g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_acquire_routing_cb), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (acquire_submenu), item);
   gtk_widget_show ( item );
-#endif
 
 #ifdef VIK_CONFIG_OPENSTREETMAP
   item = gtk_menu_item_new_with_mnemonic ( _("From _OSM Traces...") );
index c651848cc325392691a71a23fe317c11e5730f77..d480ef9c65d81ddbf4a957658ab6c9f60f1c504f 100644 (file)
@@ -2958,12 +2958,10 @@ static void acquire_from_file ( GtkAction *a, VikWindow *vw )
   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_file_interface, NULL, NULL );
 }
 
-#ifdef VIK_CONFIG_GOOGLE
-static void acquire_from_google ( GtkAction *a, VikWindow *vw )
+static void acquire_from_routing ( GtkAction *a, VikWindow *vw )
 {
-  a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface, NULL, NULL );
+  a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_routing_interface, NULL, NULL );
 }
-#endif
 
 #ifdef VIK_CONFIG_OPENSTREETMAP
 static void acquire_from_osm ( GtkAction *a, VikWindow *vw )
@@ -3698,9 +3696,7 @@ static GtkActionEntry entries[] = {
   { "Acquire",   GTK_STOCK_GO_DOWN,      N_("A_cquire"),                  NULL,         NULL,                                               (GCallback)NULL },
   { "AcquireGPS",   NULL,                N_("From _GPS..."),                     NULL,         N_("Transfer data from a GPS device"),              (GCallback)acquire_from_gps      },
   { "AcquireGPSBabel",   NULL,                N_("Import File With GPS_Babel..."),               NULL,         N_("Import file via GPSBabel converter"),              (GCallback)acquire_from_file      },
-#ifdef VIK_CONFIG_GOOGLE
-  { "AcquireGoogle",   NULL,             N_("Google _Directions..."),     NULL,         N_("Get driving directions from Google"),           (GCallback)acquire_from_google   },
-#endif
+  { "AcquireRouting",   NULL,             N_("_Directions..."),     NULL,         N_("Get driving directions"),           (GCallback)acquire_from_routing   },
 #ifdef VIK_CONFIG_OPENSTREETMAP
   { "AcquireOSM",   NULL,                 N_("_OSM Traces..."),          NULL,         N_("Get traces from OpenStreetMap"),            (GCallback)acquire_from_osm       },
   { "AcquireMyOSM", NULL,                 N_("_My OSM Traces..."),       NULL,         N_("Get Your Own Traces from OpenStreetMap"),   (GCallback)acquire_from_my_osm    },