]> err.no Git - mapper/blobdiff - src/route.c
Add header and cast properly.
[mapper] / src / route.c
index 07772aa6aab3bab0be85e44b133a896872a4a5b4..3144f60650eddf3a704738d10bfd6e6e75ed2d40 100644 (file)
@@ -1,7 +1,5 @@
 #include <config.h>
 
-#define _GNU_SOURCE
-
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include "map-download.h"
 #include "iap.h"
 
-void route_find_nearest_point();
+#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)))
+
+void route_find_nearest_point(void);
 void cancel_autoroute(gboolean temporary);
+void route_show_distance_to_last(void);
+void route_set_destination_from_last(void);
 
 typedef struct _OriginToggleInfo OriginToggleInfo;
 struct _OriginToggleInfo {
@@ -66,11 +72,12 @@ confirm = hildon_note_new_confirmation(GTK_WINDOW(_window), _("Really clear the
 
 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
        cancel_autoroute(FALSE);
-       MACRO_PATH_FREE(_route);
-       MACRO_PATH_INIT(_route);
+       route_deinit();
+       route_init();
        route_find_nearest_point();
        map_force_redraw();
 }
+_dest.valid=FALSE;
 gtk_widget_destroy(confirm);
 }
 
@@ -80,16 +87,16 @@ route_open_file(void)
 gchar *buffer;
 gint size;
 
-if (open_file(&buffer, NULL, &size, &_route_dir_uri, NULL, GTK_FILE_CHOOSER_ACTION_OPEN)) {
+if (file_open_get_contents(&_route_dir_uri, &buffer, &size)) {
        /* If auto is enabled, append the route, otherwise replace it. */
        if (parse_gpx(&_route, buffer, size, _autoroute_data.enabled ? 0 : 1)) {
                cancel_autoroute(FALSE);
 
+               MACRO_BANNER_SHOW_INFO(_window, _("Route Opened"));
                /* Find the nearest route point, if we're connected. */
                route_find_nearest_point();
-
                map_force_redraw();
-               MACRO_BANNER_SHOW_INFO(_window, _("Route Opened"));
+               route_set_destination_from_last();
                return TRUE;
        } else {
                popup_error(_window, _("Error parsing GPX file."));
@@ -131,9 +138,9 @@ if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle))) {
                gchar buffer[80];
                gchar strlat[32];
                gchar strlon[32];
-               g_ascii_formatd(strlat, 32, "%.06f", _gps.lat);
-               g_ascii_formatd(strlon, 32, "%.06f", _gps.lon);
-               snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
+               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];
@@ -149,7 +156,7 @@ if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle))) {
                unit2latlon(p->unitx, p->unity, lat, lon);
                g_ascii_formatd(strlat, 32, "%.06f", lat);
                g_ascii_formatd(strlon, 32, "%.06f", lon);
-               snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
+               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);
@@ -211,20 +218,17 @@ auto_route_dl_idle()
 {
 gchar latstr[32], lonstr[32], *latlonstr;
 
-g_ascii_dtostr(latstr, 32, _gps.lat);
-g_ascii_dtostr(lonstr, 32, _gps.lon);
+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_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);
 if (!_curl_multi) {
        /* Initialize CURL. */
        _curl_multi = curl_multi_init();
@@ -271,10 +275,7 @@ dialog = gtk_dialog_new_with_buttons(_("Download Route"),
                             GTK_STOCK_CANCEL,
                             GTK_RESPONSE_REJECT, NULL);
 
-#ifdef WITH_OSSO_HELP
-/* Enable the help button. */
-ossohelp_dialog_help_enable(GTK_DIALOG(dialog), HELP_ID_DOWNROUTE, _osso);
-#endif
+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);
@@ -400,7 +401,7 @@ while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
 
        from_escaped = gnome_vfs_escape_string(from);
        to_escaped = gnome_vfs_escape_string(to);
-       snprintf(buffer, sizeof(buffer), source_url, from_escaped, to_escaped);
+       g_snprintf(buffer, sizeof(buffer), source_url, from_escaped, to_escaped);
        g_free(from_escaped);
        g_free(to_escaped);
 
@@ -451,21 +452,18 @@ while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
                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);
+                       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);
+                       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();
 
                /* Success! Get out of the while loop. */
                break;
@@ -490,7 +488,7 @@ guint64 nearest_squared;
 Point pos = { unitx, unity, 0, NAN };
 
 wcurr = wnear = _route.whead;
-if (wcurr && wcurr != _route.wtail) {
+if (wcurr && wcurr->point && wcurr != _route.wtail) {
        nearest_squared = DISTANCE_SQUARED(pos, *(wcurr->point));
 
        while (wcurr++ != _route.wtail) {
@@ -502,13 +500,11 @@ if (wcurr && wcurr != _route.wtail) {
        }
 }
 
-if (wnear) {
+if (wnear && wnear->point) {
        /* Only use the waypoint if it is within a 6*_draw_width square drawn
         * around the position. This is consistent with select_poi(). */
-       if (abs(unitx - wnear->point->unitx) <
-           pixel2unit(3 * _draw_width)
-           && abs(unity - wnear->point->unity) <
-           pixel2unit(3 * _draw_width))
+       if (abs(unitx - wnear->point->unitx) < pixel2unit(3 * _draw_width) &&
+                       abs(unity - wnear->point->unity) < pixel2unit(3 * _draw_width))
                return wnear;
 }
 
@@ -539,14 +535,14 @@ if (_next_way) {
        /* First, set near_dist_squared with the new distance from
         * _near_point. */
        near = _near_point;
-       near_dist_squared = DISTANCE_SQUARED(_pos, *near);
+       near_dist_squared = DISTANCE_SQUARED(_gps->data, *near);
 
        /* Now, search _route for a closer point.  If quick is TRUE, then we'll
         * only search forward, only as long as we keep finding closer points.
         */
        for (curr = _near_point; curr++ != _route.tail;) {
                if (curr->unity) {
-                       guint dist_squared = DISTANCE_SQUARED(_pos, *curr);
+                       guint dist_squared = DISTANCE_SQUARED(_gps->data, *curr);
                        if (dist_squared <= near_dist_squared) {
                                near = curr;
                                near_dist_squared = dist_squared;
@@ -560,27 +556,25 @@ if (_next_way) {
        _near_point_dist_squared = near_dist_squared;
 
        for (wnext = wcurr = _next_way; wcurr != _route.wtail; wcurr++) {
-               if (wcurr->point < near
+               if (wcurr->point < near || (wcurr->point == near && quick 
+                               && (_next_wpt && (DISTANCE_SQUARED(_gps->data, *near) > _next_way_dist_squared
+                               && DISTANCE_SQUARED(_gps->data, *_next_wpt) < _next_wpt_dist_squared))))
                    /* Okay, this else if expression warrants explanation.  If the
                     * nearest track point happens to be a waypoint, then we want to
                     * check if we have "passed" that waypoint.  To check this, we
-                    * test the distance from _pos to the waypoint and from _pos to
+                    * test the distance from _gps to the waypoint and from _gps to
                     * _next_wpt, and if the former is increasing and the latter is
                     * decreasing, then we have passed the waypoint, and thus we
                     * should skip it.  Note that if there is no _next_wpt, then
                     * there is no next waypoint, so we do not skip it in that case. */
-                   || (wcurr->point == near && quick
-                       && (_next_wpt && (DISTANCE_SQUARED(_pos, *near) > _next_way_dist_squared
-                               && DISTANCE_SQUARED(_pos, *_next_wpt) < _next_wpt_dist_squared))))
                        wnext = wcurr + 1;
                else
                        break;
        }
 
-       if (wnext == _route.wtail && (wnext->point < near
-                                     || (wnext->point == near && quick
-                                         && (_next_wpt && (DISTANCE_SQUARED (_pos, *near) > _next_way_dist_squared
-                                              && DISTANCE_SQUARED(_pos, *_next_wpt) < _next_wpt_dist_squared)))))
+       if (wnext == _route.wtail && (wnext->point < near || (wnext->point == near && quick
+                                         && (_next_wpt && (DISTANCE_SQUARED (_gps->data, *near) > _next_way_dist_squared
+                                              && DISTANCE_SQUARED(_gps->data, *_next_wpt) < _next_wpt_dist_squared)))))
        {
                _next_way = NULL;
                _next_wpt = NULL;
@@ -606,9 +600,9 @@ if (_next_way) {
                        }
                        ret = TRUE;
                }
-               _next_way_dist_squared = DISTANCE_SQUARED(_pos, *wnext->point);
+               _next_way_dist_squared = DISTANCE_SQUARED(_gps->data, *wnext->point);
                if (_next_wpt)
-                       _next_wpt_dist_squared = DISTANCE_SQUARED(_pos, *_next_wpt);
+                       _next_wpt_dist_squared = DISTANCE_SQUARED(_gps->data, *_next_wpt);
        }
 }
 return ret;
@@ -627,8 +621,7 @@ while (!_near_point->unity && _near_point != _route.tail)
        _near_point++;
 
 /* Initialize _next_way. */
-if (_route.wtail == _route.whead - 1
-    || (_autoroute_data.enabled && _route.wtail == _route.whead))
+if (_route.wtail == _route.whead - 1 || (_autoroute_data.enabled && _route.wtail == _route.whead))
        _next_way = NULL;
 else
        /* We have at least one waypoint. */
@@ -663,7 +656,7 @@ if (point == NULL && _next_way)
 if (point == NULL)
        return FALSE;
 
-unit2latlon(_pos.unitx, _pos.unity, lat1, lon1);
+unit2latlon(_gps->data.unitx, _gps->data.unity, lat1, lon1);
 if (point > _near_point) {
        Point *curr;
        /* Skip _near_point in case we have already passed it. */
@@ -706,12 +699,30 @@ if (!route_show_distance_to(NULL)) {
 }
 }
 
+void
+route_set_destination_from_last(void)
+{
+Point *p;
+gdouble lat,lon;
+if (_route.head == _route.tail)
+       return;
+
+/* Find last non-zero point. */
+for (p = _route.tail; !p->unity; p--) {
+}
+unit2latlon(p->unitx, p->unity, lat, lon);
+_dest.valid=TRUE;
+_dest.lat=lat;
+_dest.lon=lon;
+}
+
 void 
-route_show_distance_to_last()
+route_show_distance_to_last(void)
 {
+Point *p;
+
 if (_route.head != _route.tail) {
        /* Find last non-zero point. */
-       Point *p;
        for (p = _route.tail; !p->unity; p--) {
        }
        route_show_distance_to(p);
@@ -722,45 +733,3 @@ if (_route.head != _route.tail) {
 
 /***/
 
-GtkListStore *
-route_generate_store(void)
-{
-WayPoint *wcurr;
-GtkTreeIter iter;
-GtkListStore *store;
-gchar buffer1[80];
-gchar buffer2[32];
-gdouble lat1, lon1, lat2, lon2;
-gfloat sum=0.0;
-
-if (_route.whead==_route.wtail)
-       return NULL;
-
-store = gtk_list_store_new(ROUTE_NUM_COLUMNS,G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
-wcurr = _route.whead;
-unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1);
-
-while (wcurr!=_route.wtail) {
-       if (!wcurr)
-               break;
-
-       unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2);
-       g_snprintf(buffer1, sizeof(buffer1), "%.05f,%.05f", lat2, lon2);
-       sum += calculate_distance(lat1, lon1, lat2, lon2);
-       g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
-
-       lat1=lat2;
-       lon1=lon2;
-
-       gtk_list_store_append(store, &iter);
-       gtk_list_store_set(store, &iter,
-        ROUTE_LATLON, buffer1,
-               ROUTE_DISTANCE, buffer2,
-               ROUTE_WAYPOINT, wcurr->desc,
-               -1);
-
-       wcurr++;
-}
-
-return store;
-}