+++ /dev/null
-#include <config.h>
-
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <stddef.h>
-#include <libintl.h>
-#include <locale.h>
-#include <math.h>
-#include <errno.h>
-#include <sys/wait.h>
-#include <glib/gstdio.h>
-#include <gtk/gtk.h>
-#include <fcntl.h>
-#include <libgnomevfs/gnome-vfs.h>
-#include <curl/multi.h>
-
-#include "utils.h"
-#include "gps.h"
-#include "route.h"
-#include "ui-common.h"
-#include "settings.h"
-#include "mapper-types.h"
-#include "file.h"
-#include "latlon.h"
-#include "map-download.h"
-#include "iap.h"
-#include "help.h"
-
-#define DISTANCE_SQUARED(a, b) \
- ((guint64)((((gint64)(b).unitx)-(a).unitx)*(((gint64)(b).unitx)-(a).unitx))\
- + (guint64)((((gint64)(b).unity)-(a).unity)*(((gint64)(b).unity)-(a).unity)))
-
-static gboolean show_directions=TRUE;
-
-static WayPoint *announced_waypoint=NULL;
-
-typedef struct _OriginToggleInfo OriginToggleInfo;
-struct _OriginToggleInfo {
- GtkWidget *rad_use_gps;
- GtkWidget *rad_use_route;
- GtkWidget *rad_use_text;
- GtkWidget *chk_auto;
- GtkWidget *txt_from;
- GtkWidget *txt_to;
- Path *path;
-};
-
-gboolean
-route_clear(Path *route)
-{
-GtkWidget *confirm;
-gboolean r=FALSE;
-
-g_return_val_if_fail(route, FALSE);
-
-confirm=hildon_note_new_confirmation(GTK_WINDOW(_window), _("Really clear the route?"));
-
-if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
- route_cancel_autoroute(route, FALSE);
- announced_waypoint=NULL;
- path_clear(route);
- path_find_nearest_point(route);
- r=TRUE;
-}
-
-gtk_widget_destroy(confirm);
-return r;
-}
-
-/**
- * Check if we should announce upcoming waypoints.
- */
-void
-route_check_waypoint_announce(Path *route, GpsData *gps)
-{
-guint announce_thres_unsquared;
-
-g_return_if_fail(route);
-g_return_if_fail(gps);
-
-if (!_announce_waypoints)
- return;
-
-if (!route->next_way)
- return;
-
-announce_thres_unsquared=(20+(guint)gps->speed)*_announce_notice_ratio*3;
-
-if (route->next_way_dist_squared<(announce_thres_unsquared * announce_thres_unsquared) && route->next_way!=announced_waypoint) {
- announce_waypoint(route->next_way->desc);
- announced_waypoint=route->next_way;
-}
-
-}
-
-/**
- * Check if we should re-calculate route
- */
-void
-route_autoroute_check(Path *route)
-{
-g_return_if_fail(route);
-
-if (_autoroute_data.enabled && !_autoroute_data.in_progress && route->near_point_dist_squared > 400) {
- MACRO_BANNER_SHOW_INFO(_window, _("Recalculating directions..."));
- _autoroute_data.in_progress = TRUE;
- show_directions = FALSE;
- g_idle_add((GSourceFunc)route_auto_route_dl_idle_cb, NULL);
-}
-}
-
-gboolean
-route_open_file(Path *route)
-{
-gchar *buffer;
-gint size;
-
-g_return_val_if_fail(route, FALSE);
-
-if (file_open_get_contents(&_route_dir_uri, &buffer, &size)) {
- /* If auto is enabled, append the route, otherwise replace it. */
- if (gpx_parse(route, buffer, size, _autoroute_data.enabled ? GPX_PATH_APPEND : GPX_PATH_NEW)) {
- route_cancel_autoroute(route, FALSE);
-
- MACRO_BANNER_SHOW_INFO(_window, _("Route Opened"));
- /* Find the nearest route point, if we're connected. */
- path_find_nearest_point(route);
- route_set_destination_from_last(route, &_dest);
- return TRUE;
- } else {
- popup_error(_window, _("Error parsing GPX file."));
- g_free(buffer);
- return FALSE;
- }
-}
-return FALSE;
-}
-
-/**
- * Ask user to save route
- */
-gboolean
-route_save(Path *route)
-{
-GnomeVFSHandle *handle;
-
-g_return_val_if_fail(route, FALSE);
-
-if (route->head==route->tail) {
- MACRO_BANNER_SHOW_INFO(_window, _("No route exist."));
- return FALSE;
-}
-
-if (file_save(&_route_dir_uri, &_route_dir_uri, &handle)) {
- if (gpx_write(route, handle)) {
- MACRO_BANNER_SHOW_INFO(_window, _("Route Saved"));
- } else {
- popup_error(_window, _("Error writing GPX file."));
- }
- gnome_vfs_close(handle);
- return TRUE;
-}
-return FALSE;
-}
-
-/**
- *
- */
-static gboolean
-route_origin_type_selected_cb(GtkWidget *toggle, OriginToggleInfo *oti)
-{
-if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle))) {
- if (toggle == oti->rad_use_gps) {
- gchar buffer[80];
- gchar strlat[32];
- gchar strlon[32];
- g_ascii_formatd(strlat, 32, "%.06f", _gps->data.lat);
- g_ascii_formatd(strlon, 32, "%.06f", _gps->data.lon);
- g_snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
- gtk_entry_set_text(GTK_ENTRY(oti->txt_from), buffer);
- } else if (toggle == oti->rad_use_route) {
- gchar buffer[80];
- gchar strlat[32];
- gchar strlon[32];
- gdouble lat, lon;
- Point *p;
-
- p=path_find_last_point(oti->path);
- if (p) {
- unit2latlon(p->unitx, p->unity, &lat, &lon);
- g_ascii_formatd(strlat, 32, "%.06f", lat);
- g_ascii_formatd(strlon, 32, "%.06f", lon);
- g_snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
- gtk_entry_set_text(GTK_ENTRY(oti->txt_from), buffer);
- }
- }
- gtk_widget_set_sensitive(oti->txt_from, toggle == oti->rad_use_text);
- gtk_widget_set_sensitive(oti->chk_auto, toggle == oti->rad_use_gps);
-}
-return TRUE;
-}
-
-/**
- * Cancel the current auto-route.
- */
-void
-route_cancel_autoroute(Path *route, gboolean temporary)
-{
-if (_autoroute_data.enabled) {
- if (!temporary) {
- _autoroute_data.enabled = FALSE;
-
- g_free(_autoroute_data.dest);
- _autoroute_data.dest = NULL;
-
- g_free(_autoroute_data.src_str);
- _autoroute_data.src_str = NULL;
- }
-
- if (_autoroute_data.curl_easy) {
- if (_curl_multi)
- curl_multi_remove_handle(_curl_multi, _autoroute_data.curl_easy);
- curl_easy_cleanup(_autoroute_data.curl_easy);
- _autoroute_data.curl_easy = NULL;
- }
-
- g_free(_autoroute_data.rdl_data.bytes);
- _autoroute_data.rdl_data.bytes = NULL;
- _autoroute_data.rdl_data.bytes_read = 0;
-
- _autoroute_data.in_progress = FALSE;
-}
-}
-
-/**
- * Read the data provided by the given handle as GPX data, updating the
- * auto-route with that data.
- */
-size_t
-route_dl_cb_read(void *ptr, size_t size, size_t nmemb, RouteDownloadData * rdl_data)
-{
-size_t old_size = rdl_data->bytes_read;
-
-rdl_data->bytes_read += size * nmemb;
-rdl_data->bytes = g_renew(gchar, rdl_data->bytes, rdl_data->bytes_read);
-g_memmove(rdl_data->bytes + old_size, ptr, size * nmemb);
-
-return (size * nmemb);
-}
-
-gboolean
-route_auto_route_dl_idle_cb()
-{
-gchar latstr[32], lonstr[32], *latlonstr;
-
-g_ascii_dtostr(latstr, 32, _gps->data.lat);
-g_ascii_dtostr(lonstr, 32, _gps->data.lon);
-latlonstr = g_strdup_printf("%s,%s", latstr, lonstr);
-_autoroute_data.src_str = g_strdup_printf(_route_dl_url, latlonstr, _autoroute_data.dest);
-g_free(latlonstr);
-
-MACRO_CURL_EASY_INIT(_autoroute_data.curl_easy);
-curl_easy_setopt(_autoroute_data.curl_easy, CURLOPT_URL, _autoroute_data.src_str);
-curl_easy_setopt(_autoroute_data.curl_easy, CURLOPT_WRITEFUNCTION, route_dl_cb_read);
-curl_easy_setopt(_autoroute_data.curl_easy, CURLOPT_WRITEDATA, &_autoroute_data.rdl_data);
-curl_multi_add_handle(_curl_multi, _autoroute_data.curl_easy);
-
-if (iap_is_connected() && !_curl_sid)
- _curl_sid = g_timeout_add(100, (GSourceFunc)map_download_timeout, NULL);
-
-_autoroute_data.in_progress = TRUE;
-
-return FALSE;
-}
-
-/**
- * Display a dialog box to the user asking them to download a route. The
- * "From" and "To" textfields may be initialized using the first two
- * parameters. The third parameter, if set to TRUE, will cause the "Use GPS
- * Location" checkbox to be enabled, which automatically sets the "From" to the
- * current GPS position (this overrides any value that may have been passed as
- * the "To" initializer).
- * None of the passed strings are freed - that is left to the caller, and it is
- * safe to free either string as soon as this function returns.
- */
-gboolean
-route_download(Path *route, gchar *to)
-{
-GtkWidget *dialog;
-GtkWidget *table;
-GtkWidget *label;
-GtkWidget *txt_source_url;
-GtkWidget *hbox;
-OriginToggleInfo oti;
-GtkEntryCompletion *from_comp;
-GtkEntryCompletion *to_comp;
-gboolean r=FALSE;
-
-oti.path=route;
-
-dialog = gtk_dialog_new_with_buttons(_("Download Route"),
- GTK_WINDOW(_window),
- GTK_DIALOG_MODAL, GTK_STOCK_OK,
- GTK_RESPONSE_ACCEPT,
- GTK_STOCK_CANCEL,
- GTK_RESPONSE_REJECT, NULL);
-
-help_dialog_help_enable(GTK_DIALOG(dialog), HELP_ID_DOWNROUTE);
-gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table = gtk_table_new(2, 5, FALSE), TRUE, TRUE, 0);
-
-from_comp = gtk_entry_completion_new();
-gtk_entry_completion_set_model(from_comp, GTK_TREE_MODEL(_loc_model));
-gtk_entry_completion_set_text_column(from_comp, 0);
-to_comp = gtk_entry_completion_new();
-gtk_entry_completion_set_model(to_comp, GTK_TREE_MODEL(_loc_model));
-gtk_entry_completion_set_text_column(to_comp, 0);
-
-/* Source URL. */
-gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Source URL")), 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
-gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
-gtk_table_attach(GTK_TABLE(table), txt_source_url = gtk_entry_new(), 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
-gtk_entry_set_width_chars(GTK_ENTRY(txt_source_url), 25);
-
-/* Auto. */
-gtk_table_attach(GTK_TABLE(table), hbox = gtk_hbox_new(FALSE, 6), 0, 2, 1, 2, GTK_FILL, 0, 2, 4);
-gtk_box_pack_start(GTK_BOX(hbox), oti.rad_use_gps = gtk_radio_button_new_with_label(NULL, _("Use GPS Location")), TRUE, TRUE, 0);
-gtk_box_pack_start(GTK_BOX(hbox), oti.chk_auto = gtk_check_button_new_with_label(_("Auto-Update")), TRUE, TRUE, 0);
-gtk_widget_set_sensitive(oti.chk_auto, FALSE);
-
-/* Use End of Route. */
-gtk_table_attach(GTK_TABLE(table), hbox = gtk_hbox_new(FALSE, 6), 0, 2, 2, 3, GTK_FILL, 0, 2, 4);
-gtk_box_pack_start(GTK_BOX(hbox), oti.rad_use_route = gtk_radio_button_new_with_label_from_widget
- (GTK_RADIO_BUTTON(oti.rad_use_gps), _("Use End of Route")), TRUE, TRUE, 0);
-gtk_widget_set_sensitive(oti.rad_use_route, route->head != route->tail);
-
-/* Origin. */
-gtk_table_attach(GTK_TABLE(table), oti.rad_use_text = gtk_radio_button_new_with_label_from_widget
- (GTK_RADIO_BUTTON(oti.rad_use_gps), _("Origin")), 0, 1, 3, 4, GTK_FILL, 0, 2, 4);
-gtk_table_attach(GTK_TABLE(table), oti.txt_from = gtk_entry_new(), 1, 2, 3, 4, GTK_EXPAND | GTK_FILL, 0, 2, 4);
-gtk_entry_set_completion(GTK_ENTRY(oti.txt_from), from_comp);
-gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_from), 25);
-
-/* Destination. */
-gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Destination")), 0, 1, 4, 5, GTK_FILL, 0, 2, 4);
-gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
-gtk_table_attach(GTK_TABLE(table), oti.txt_to = gtk_entry_new(), 1, 2, 4, 5, GTK_EXPAND | GTK_FILL, 0, 2, 4);
-gtk_entry_set_completion(GTK_ENTRY(oti.txt_to), to_comp);
-gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_to), 25);
-
-g_signal_connect(G_OBJECT(oti.rad_use_gps), "toggled", G_CALLBACK(route_origin_type_selected_cb), &oti);
-g_signal_connect(G_OBJECT(oti.rad_use_route), "toggled", G_CALLBACK(route_origin_type_selected_cb), &oti);
-g_signal_connect(G_OBJECT(oti.rad_use_text), "toggled", G_CALLBACK(route_origin_type_selected_cb), &oti);
-
-#if defined (WITH_HILDON) && defined (HILDON_AUTOCAP)
-g_object_set(G_OBJECT(oti.txt_from), HILDON_AUTOCAP, FALSE, NULL);
-g_object_set(G_OBJECT(oti.txt_to), HILDON_AUTOCAP, FALSE, NULL);
-#endif
-
-/* Initialize fields. */
-gtk_entry_set_text(GTK_ENTRY(txt_source_url), _route_dl_url);
-gtk_entry_set_text(GTK_ENTRY(oti.txt_to), (to ? to : ""));
-
-gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
-
-if (route->head != route->tail) {
- /* Use "End of Route" by default if they have a route. */
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(oti.rad_use_route), TRUE);
- gtk_widget_grab_focus(oti.rad_use_route);
-} else if (_enable_gps) {
- /* Else use "GPS Location" if they have GPS enabled. */
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(oti.rad_use_gps), TRUE);
- gtk_widget_grab_focus(oti.rad_use_gps);
-} else {
- /* Else use text. */
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
- gtk_widget_grab_focus(oti.txt_from);
-}
-
-gtk_widget_show_all(dialog);
-
-while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
- CURL *curl_easy;
- RouteDownloadData rdl_data = { 0, 0 };
- gchar buffer[BUFFER_SIZE];
- const gchar *source_url, *from, *to;
- gchar *from_escaped, *to_escaped;
-
- source_url = gtk_entry_get_text(GTK_ENTRY(txt_source_url));
- if (!strlen(source_url)) {
- popup_error(dialog, _("Please specify a source URL."));
- continue;
- } else {
- g_free(_route_dl_url);
- _route_dl_url = g_strdup(source_url);
- }
-
- from = gtk_entry_get_text(GTK_ENTRY(oti.txt_from));
- if (!strlen(from)) {
- popup_error(dialog, _("Please specify a start location."));
- continue;
- }
-
- to = gtk_entry_get_text(GTK_ENTRY(oti.txt_to));
- if (!strlen(to)) {
- popup_error(dialog, _("Please specify an end location."));
- continue;
- }
-
- from_escaped = gnome_vfs_escape_string(from);
- to_escaped = gnome_vfs_escape_string(to);
- g_snprintf(buffer, sizeof(buffer), source_url, from_escaped, to_escaped);
- g_free(from_escaped);
- g_free(to_escaped);
-
- iap_connect();
-
- /* Attempt to download the route from the server. */
- MACRO_CURL_EASY_INIT(curl_easy);
- curl_easy_setopt(curl_easy, CURLOPT_URL, buffer);
- curl_easy_setopt(curl_easy, CURLOPT_WRITEFUNCTION, route_dl_cb_read);
- curl_easy_setopt(curl_easy, CURLOPT_WRITEDATA, &rdl_data);
- if (CURLE_OK != curl_easy_perform(curl_easy)) {
- popup_error(dialog, _("Failed to connect to GPX Directions server"));
- curl_easy_cleanup(curl_easy);
- g_free(rdl_data.bytes);
- /* Let them try again */
- continue;
- }
- curl_easy_cleanup(curl_easy);
-
- if (strncmp(rdl_data.bytes, "<?xml", strlen("<?xml"))) {
- /* Not an XML document - must be bad locations. */
- popup_error(dialog, _("Could not generate directions. Make sure your source and destination are valid."));
- g_free(rdl_data.bytes);
- /* Let them try again. */
- }
- /* Else, if GPS is enabled, replace the route, otherwise append it. */
- else if (gpx_parse(route, rdl_data.bytes, rdl_data.bytes_read,
- (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.rad_use_gps)) ? GPX_PATH_NEW : GPX_PATH_APPEND))) {
- GtkTreeIter iter;
-
- /* Find the nearest route point, if we're connected. */
- path_find_nearest_point(route);
-
- /* Cancel any autoroute that might be occurring. */
- route_cancel_autoroute(route, FALSE);
-
- if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.chk_auto))) {
- /* Kick off a timeout to start the first update. */
- _autoroute_data.dest = gnome_vfs_escape_string(to);
- _autoroute_data.enabled = TRUE;
- }
-
- /* Save Origin in Route Locations list if not from GPS. */
- if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.rad_use_gps)) && !g_slist_find_custom(_loc_list, from, (GCompareFunc) strcmp)) {
- _loc_list = g_slist_prepend(_loc_list, g_strdup(from));
- gtk_list_store_insert_with_values(_loc_model, &iter, INT_MAX, 0, from, -1);
- }
-
- /* Save Destination in Route Locations list. */
- if (!g_slist_find_custom(_loc_list, to, (GCompareFunc) strcmp)) {
- _loc_list = g_slist_prepend(_loc_list, g_strdup(to));
- gtk_list_store_insert_with_values(_loc_model, &iter, INT_MAX, 0, to, -1);
- }
-
- MACRO_BANNER_SHOW_INFO(_window, _("Route Downloaded"));
- g_free(rdl_data.bytes);
- route_set_destination_from_last(route, &_dest);
-
- /* Success! Get out of the while loop. */
- r=TRUE;
- break;
- } else {
- popup_error(dialog, _("Error parsing route GPX data."));
- g_free(rdl_data.bytes);
- /* Let them try again. */
- }
-}
-
-gtk_widget_hide(dialog); /* Destroying causes a crash (!?!?!??!) */
-
-return r;
-}
-
-/**
- * Show the distance from the current GPS location to the given point,
- * following the route. If point is NULL, then the distance is shown to the
- * next waypoint.
- */
-gboolean
-route_show_distance_to(Path *route, Point *point)
-{
-gchar buffer[80];
-gdouble lat, lon;
-gdouble sum;
-
-g_return_val_if_fail(route, FALSE);
-g_return_val_if_fail(point, FALSE);
-
-unit2latlon(point->unitx, point->unity, &lat, &lon);
-sum=path_get_distance_to(route, point, lat, lon);
-g_snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"), sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
-MACRO_BANNER_SHOW_INFO(_window, buffer);
-
-return TRUE;
-}
-
-void
-route_show_distance_to_next_waypoint(Path *route)
-{
-g_return_if_fail(route);
-
-if (!route_show_distance_to(route, NULL))
- MACRO_BANNER_SHOW_INFO(_window, _("There is no next waypoint."));
-}
-
-void
-route_show_distance_to_last(Path *route)
-{
-g_return_if_fail(route);
-
-if (route->head != route->tail) {
- route_show_distance_to(route, path_find_last_point(route));
-} else {
- MACRO_BANNER_SHOW_INFO(_window, _("The current route is empty."));
-}
-}