2 * Copyright (C) 2006, 2007 John Costigan.
4 * POI and GPS-Info code originally written by Cezary Jackiewicz.
6 * Default map data provided by http://www.openstreetmap.org/
8 * This file is part of Maemo Mapper.
10 * Maemo Mapper is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * Maemo Mapper is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Maemo Mapper. If not, see <http://www.gnu.org/licenses/>.
37 # include <hildon/hildon-help.h>
38 # include <hildon/hildon-note.h>
39 # include <hildon/hildon-banner.h>
40 # include <hildon/hildon-sound.h>
42 # include <osso-helplib.h>
43 # include <hildon-widgets/hildon-note.h>
44 # include <hildon-widgets/hildon-banner.h>
45 # include <hildon-widgets/hildon-system-sound.h>
46 # include <hildon-widgets/hildon-input-mode-hint.h>
54 #include "gdk-pixbuf-rotate.h"
61 typedef struct _OriginToggleInfo OriginToggleInfo;
62 struct _OriginToggleInfo {
63 GtkWidget *rad_use_gps;
64 GtkWidget *rad_use_route;
65 GtkWidget *rad_use_text;
66 GtkWidget *chk_avoid_highways;
72 /* _near_point is the route point to which we are closest. */
73 static Point *_near_point = NULL;
75 /* _next_way is what we currently interpret to be the next waypoint. */
76 static WayPoint *_next_way;
77 static gint64 _next_way_dist_squared = INT64_MAX;
79 /* _next_wpt is the route point immediately following _next_way. */
80 static Point *_next_wpt = NULL;
81 static gint64 _next_wpt_dist_squared = INT64_MAX;
83 static gfloat _initial_distance_from_waypoint = -1.f;
84 static WayPoint *_initial_distance_waypoint = NULL;
86 static sqlite3 *_path_db = NULL;
87 static sqlite3_stmt *_track_stmt_select = NULL;
88 static sqlite3_stmt *_track_stmt_delete_path = NULL;
89 static sqlite3_stmt *_track_stmt_delete_way = NULL;
90 static sqlite3_stmt *_track_stmt_insert_path = NULL;
91 static sqlite3_stmt *_track_stmt_insert_way = NULL;
92 static sqlite3_stmt *_route_stmt_select = NULL;
93 static sqlite3_stmt *_route_stmt_delete_path = NULL;
94 static sqlite3_stmt *_route_stmt_delete_way = NULL;
95 static sqlite3_stmt *_route_stmt_insert_path = NULL;
96 static sqlite3_stmt *_route_stmt_insert_way = NULL;
97 static sqlite3_stmt *_path_stmt_trans_begin = NULL;
98 static sqlite3_stmt *_path_stmt_trans_commit = NULL;
99 static sqlite3_stmt *_path_stmt_trans_rollback = NULL;
101 static gchar *_last_spoken_phrase;
104 path_resize(Path *path, gint size)
106 printf("%s()\n", __PRETTY_FUNCTION__);
108 if(path->head + size != path->cap)
110 Point *old_head = path->head;
112 path->head = g_renew(Point, old_head, size);
113 path->cap = path->head + size;
114 if(path->head != old_head)
116 path->tail = path->head + (path->tail - old_head);
118 /* Adjust all of the waypoints. */
119 for(curr = path->whead - 1; curr++ != path->wtail; )
120 curr->point = path->head + (curr->point - old_head);
124 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
128 path_wresize(Path *path, gint wsize)
130 printf("%s()\n", __PRETTY_FUNCTION__);
132 if(path->whead + wsize != path->wcap)
134 WayPoint *old_whead = path->whead;
135 path->whead = g_renew(WayPoint, old_whead, wsize);
136 path->wtail = path->whead + (path->wtail - old_whead);
137 path->wcap = path->whead + wsize;
140 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
144 read_path_from_db(Path *path, sqlite3_stmt *select_stmt)
146 printf("%s()\n", __PRETTY_FUNCTION__);
148 MACRO_PATH_INIT(*path);
149 while(SQLITE_ROW == sqlite3_step(select_stmt))
153 MACRO_PATH_INCREMENT_TAIL(*path);
154 path->tail->unitx = sqlite3_column_int(select_stmt, 0);
155 path->tail->unity = sqlite3_column_int(select_stmt, 1);
156 path->tail->time = sqlite3_column_int(select_stmt, 2);
157 path->tail->altitude = sqlite3_column_int(select_stmt, 3);
159 desc = sqlite3_column_text(select_stmt, 4);
162 MACRO_PATH_INCREMENT_WTAIL(*path);
163 path->wtail->point = path->tail;
164 path->wtail->desc = g_strdup(desc);
167 sqlite3_reset(select_stmt);
169 /* If the last point isn't null, then add another null point. */
170 if(path->tail->unity)
172 MACRO_PATH_INCREMENT_TAIL(*path);
173 *path->tail = _point_null;
176 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
179 /* Returns the new next_update_index. */
181 write_path_to_db(Path *path,
182 sqlite3_stmt *delete_path_stmt,
183 sqlite3_stmt *delete_way_stmt,
184 sqlite3_stmt *insert_path_stmt,
185 sqlite3_stmt *insert_way_stmt,
186 gint index_last_saved)
191 gboolean success = TRUE;
192 printf("%s(%d)\n", __PRETTY_FUNCTION__, index_last_saved);
194 /* Start transaction. */
195 sqlite3_step(_path_stmt_trans_begin);
196 sqlite3_reset(_path_stmt_trans_begin);
198 if(index_last_saved == 0)
200 /* Replace the whole thing, so delete the table first. */
201 if(SQLITE_DONE != sqlite3_step(delete_way_stmt)
202 || SQLITE_DONE != sqlite3_step(delete_path_stmt))
204 gchar buffer[BUFFER_SIZE];
205 snprintf(buffer, sizeof(buffer), "%s\n%s",
206 _("Failed to write to path database. "
207 "Tracks and routes may not be saved."),
208 sqlite3_errmsg(_path_db));
209 popup_error(_window, buffer);
212 sqlite3_reset(delete_way_stmt);
213 sqlite3_reset(delete_path_stmt);
216 for(num = index_last_saved, curr = path->head + num, wcurr = path->whead;
217 success && ++curr <= path->tail; ++num)
219 /* If this is the last point, and it is null, don't write it. */
220 if(curr == path->tail && !curr->unity)
223 /* Insert the path point. */
224 if(SQLITE_OK != sqlite3_bind_int(insert_path_stmt, 1, curr->unitx)
225 || SQLITE_OK != sqlite3_bind_int(insert_path_stmt, 2, curr->unity)
226 || SQLITE_OK != sqlite3_bind_int(insert_path_stmt, 3, curr->time)
227 || SQLITE_OK != sqlite3_bind_int(insert_path_stmt, 4, curr->altitude)
228 || SQLITE_DONE != sqlite3_step(insert_path_stmt))
230 gchar buffer[BUFFER_SIZE];
231 snprintf(buffer, sizeof(buffer), "%s\n%s",
232 _("Failed to write to path database. "
233 "Tracks and routes may not be saved."),
234 sqlite3_errmsg(_path_db));
235 popup_error(_window, buffer);
238 sqlite3_reset(insert_path_stmt);
240 /* Now, check if curr is a waypoint. */
241 if(success && wcurr <= path->wtail && wcurr->point == curr)
243 gint num = sqlite3_last_insert_rowid(_path_db);
244 if(SQLITE_OK != sqlite3_bind_int(insert_way_stmt, 1, num)
245 || SQLITE_OK != sqlite3_bind_text(insert_way_stmt, 2, wcurr->desc,
247 || SQLITE_DONE != sqlite3_step(insert_way_stmt))
249 gchar buffer[BUFFER_SIZE];
250 snprintf(buffer, sizeof(buffer), "%s\n%s",
251 _("Failed to write to path database. "
252 "Tracks and routes may not be saved."),
253 sqlite3_errmsg(_path_db));
254 popup_error(_window, buffer);
257 sqlite3_reset(insert_way_stmt);
263 sqlite3_step(_path_stmt_trans_commit);
264 sqlite3_reset(_path_stmt_trans_commit);
265 vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
270 sqlite3_step(_path_stmt_trans_rollback);
271 sqlite3_reset(_path_stmt_trans_rollback);
272 vprintf("%s(): return 0\n", __PRETTY_FUNCTION__);
273 return index_last_saved;
278 path_save_route_to_db()
282 write_path_to_db(&_route,
283 _route_stmt_delete_path,
284 _route_stmt_delete_way,
285 _route_stmt_insert_path,
286 _route_stmt_insert_way,
292 path_save_track_to_db()
296 write_path_to_db(&_track,
297 _track_stmt_delete_path,
298 _track_stmt_delete_way,
299 _track_stmt_insert_path,
300 _track_stmt_insert_way,
306 path_update_track_in_db()
310 _track_index_last_saved = write_path_to_db(&_track,
311 _track_stmt_delete_path,
312 _track_stmt_delete_way,
313 _track_stmt_insert_path,
314 _track_stmt_insert_way,
315 _track_index_last_saved);
320 * Updates _near_point, _next_way, and _next_wpt. If quick is FALSE (as
321 * it is when this function is called from route_find_nearest_point), then
322 * the entire list (starting from _near_point) is searched. Otherwise, we
323 * stop searching when we find a point that is farther away.
326 route_update_nears(gboolean quick)
328 gboolean ret = FALSE;
330 WayPoint *wcurr, *wnext;
331 gint64 near_dist_squared;
332 printf("%s(%d)\n", __PRETTY_FUNCTION__, quick);
334 /* If we have waypoints (_next_way != NULL), then determine the "next
335 * waypoint", which is defined as the waypoint after the nearest point,
336 * UNLESS we've passed that waypoint, in which case the waypoint after
337 * that waypoint becomes the "next" waypoint. */
340 /* First, set near_dist_squared with the new distance from
343 near_dist_squared = DISTANCE_SQUARED(_pos, *near);
345 /* Now, search _route for a closer point. If quick is TRUE, then we'll
346 * only search forward, only as long as we keep finding closer points.
348 for(curr = _near_point; curr++ != _route.tail; )
352 gint64 dist_squared = DISTANCE_SQUARED(_pos, *curr);
353 if(dist_squared <= near_dist_squared)
356 near_dist_squared = dist_squared;
363 /* Update _near_point. */
366 for(wnext = wcurr = _next_way; wcurr < _route.wtail; wcurr++)
368 if(wcurr->point < near
369 /* Okay, this else if expression warrants explanation. If the
370 * nearest track point happens to be a waypoint, then we want to
371 * check if we have "passed" that waypoint. To check this, we
372 * test the distance from _pos to the waypoint and from _pos to
373 * _next_wpt, and if the former is increasing and the latter is
374 * decreasing, then we have passed the waypoint, and thus we
375 * should skip it. Note that if there is no _next_wpt, then
376 * there is no next waypoint, so we do not skip it in that case. */
377 || (wcurr->point == near && quick
379 && (DISTANCE_SQUARED(_pos, *near) > _next_way_dist_squared
380 && DISTANCE_SQUARED(_pos, *_next_wpt)
381 < _next_wpt_dist_squared))))
389 if(wnext == _route.wtail && (wnext->point < near
390 || (wnext->point == near && quick
392 && (DISTANCE_SQUARED(_pos, *near) > _next_way_dist_squared
393 &&DISTANCE_SQUARED(_pos, *_next_wpt)
394 < _next_wpt_dist_squared)))))
398 _next_way_dist_squared = INT64_MAX;
399 _next_wpt_dist_squared = INT64_MAX;
402 /* Only update _next_way (and consequently _next_wpt) if _next_way is
403 * different, and record that fact for return. */
406 if(!quick || _next_way != wnext)
409 _next_wpt = wnext->point;
410 if(_next_wpt == _route.tail)
414 while(!(++_next_wpt)->unity)
416 if(_next_wpt == _route.tail)
425 _next_way_dist_squared = DISTANCE_SQUARED(_pos, *wnext->point);
427 _next_wpt_dist_squared = DISTANCE_SQUARED(_pos, *_next_wpt);
431 vprintf("%s(): return %d\n", __PRETTY_FUNCTION__, ret);
436 * Reset the _near_point data by searching the entire route for the nearest
437 * route point and waypoint.
440 route_find_nearest_point()
442 printf("%s()\n", __PRETTY_FUNCTION__);
444 /* Initialize _near_point to first non-zero point. */
445 _near_point = _route.head;
446 while(!_near_point->unity && _near_point != _route.tail)
449 /* Initialize _next_way. */
450 if(_route.wtail < _route.whead
451 || (_autoroute_data.enabled && _route.wtail == _route.whead))
454 /* We have at least one waypoint. */
455 _next_way = _autoroute_data.enabled ? _route.whead + 1 : _route.whead;
456 _next_way_dist_squared = INT64_MAX;
458 /* Initialize _next_wpt. */
460 _next_wpt_dist_squared = INT64_MAX;
462 route_update_nears(FALSE);
464 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
468 * Show the distance from the current GPS location to the given point,
469 * following the route. If point is NULL, then the distance is shown to the
473 route_show_distance_to(Point *point)
476 gdouble lat1, lon1, lat2, lon2;
478 printf("%s()\n", __PRETTY_FUNCTION__);
480 /* If point is NULL, use the next waypoint. */
481 if(point == NULL && _next_way)
482 point = _next_way->point;
484 /* If point is still NULL, return an error. */
487 printf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
491 unit2latlon(_pos.unitx, _pos.unity, lat1, lon1);
492 if(point > _near_point)
495 /* Skip _near_point in case we have already passed it. */
496 for(curr = _near_point + 1; curr <= point; ++curr)
500 unit2latlon(curr->unitx, curr->unity, lat2, lon2);
501 sum += calculate_distance(lat1, lon1, lat2, lon2);
507 else if(point < _near_point)
510 /* Skip _near_point in case we have already passed it. */
511 for(curr = _near_point - 1; curr >= point; --curr)
515 unit2latlon(curr->unitx, curr->unity, lat2, lon2);
516 sum += calculate_distance(lat1, lon1, lat2, lon2);
524 /* Waypoint _is_ the nearest point. */
525 unit2latlon(_near_point->unitx, _near_point->unity, lat2, lon2);
526 sum += calculate_distance(lat1, lon1, lat2, lon2);
529 snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"),
530 sum * UNITS_CONVERT[_units], UNITS_ENUM_TEXT[_units]);
531 MACRO_BANNER_SHOW_INFO(_window, buffer);
534 printf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
538 route_show_distance_to_next()
540 printf("%s()\n", __PRETTY_FUNCTION__);
542 if(!route_show_distance_to(NULL))
544 MACRO_BANNER_SHOW_INFO(_window, _("There is no next waypoint."));
546 printf("%s(): return\n", __PRETTY_FUNCTION__);
550 route_show_distance_to_last()
552 printf("%s()\n", __PRETTY_FUNCTION__);
554 if(_route.head != _route.tail)
556 /* Find last non-zero point. */
558 for(p = _route.tail; !p->unity; p--) { }
559 route_show_distance_to(p);
563 MACRO_BANNER_SHOW_INFO(_window, _("The current route is empty."));
565 printf("%s(): return\n", __PRETTY_FUNCTION__);
569 track_show_distance_from(Point *point)
572 gdouble lat1, lon1, lat2, lon2;
575 unit2latlon(_pos.unitx, _pos.unity, lat1, lon1);
577 /* Skip _track.tail because that should be _pos. */
578 for(curr = _track.tail; curr > point; --curr)
582 unit2latlon(curr->unitx, curr->unity, lat2, lon2);
583 sum += calculate_distance(lat1, lon1, lat2, lon2);
589 snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"),
590 sum * UNITS_CONVERT[_units], UNITS_ENUM_TEXT[_units]);
591 MACRO_BANNER_SHOW_INFO(_window, buffer);
595 track_show_distance_from_last()
597 printf("%s()\n", __PRETTY_FUNCTION__);
599 /* Find last zero point. */
600 if(_track.head != _track.tail)
603 /* Find last zero point. */
604 for(point = _track.tail; point->unity; point--) { }
605 track_show_distance_from(point);
609 MACRO_BANNER_SHOW_INFO(_window, _("The current track is empty."));
611 printf("%s(): return\n", __PRETTY_FUNCTION__);
615 track_show_distance_from_first()
617 printf("%s()\n", __PRETTY_FUNCTION__);
619 /* Find last zero point. */
620 if(_track.head != _track.tail)
621 track_show_distance_from(_track.head);
624 MACRO_BANNER_SHOW_INFO(_window, _("The current track is empty."));
626 printf("%s(): return\n", __PRETTY_FUNCTION__);
630 route_download_and_setup(GtkWidget *parent, const gchar *source_url,
631 const gchar *from, const gchar *to,
632 gboolean avoid_highways, gint replace_policy)
639 GnomeVFSResult vfs_result;
640 printf("%s(%s, %s)\n", __PRETTY_FUNCTION__, from, to);
642 from_escaped = gnome_vfs_escape_string(from);
643 to_escaped = gnome_vfs_escape_string(to);
644 buffer = g_strdup_printf(source_url, from_escaped, to_escaped);
645 g_free(from_escaped);
651 buffer = g_strconcat(old, "&avoid_highways=on", NULL);
655 /* Attempt to download the route from the server. */
656 if(GNOME_VFS_OK != (vfs_result = gnome_vfs_read_entire_file(
657 buffer, &size, &bytes)))
660 popup_error(parent, gnome_vfs_result_to_string(vfs_result));
661 vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
665 if(strncmp(bytes, "<?xml", strlen("<?xml")))
667 /* Not an XML document - must be bad locations. */
669 _("Invalid source or destination."));
671 vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
674 /* Else, if GPS is enabled, replace the route, otherwise append it. */
675 else if(gpx_path_parse(&_route, bytes, size, replace_policy))
677 path_save_route_to_db();
679 /* Find the nearest route point, if we're connected. */
680 route_find_nearest_point();
684 MACRO_BANNER_SHOW_INFO(_window, _("Route Downloaded"));
687 vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
690 popup_error(parent, _("Error parsing GPX file."));
693 vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
698 * This function is periodically run to download updated auto-route data
699 * from the route source.
704 gchar latstr[32], lonstr[32], latlonstr[80];
705 printf("%s(%f, %f, %s)\n", __PRETTY_FUNCTION__,
706 _gps.lat, _gps.lon, _autoroute_data.dest);
708 g_ascii_dtostr(latstr, 32, _gps.lat);
709 g_ascii_dtostr(lonstr, 32, _gps.lon);
710 snprintf(latlonstr, sizeof(latlonstr), "%s, %s", latstr, lonstr);
712 if(!route_download_and_setup(_window, _autoroute_data.source_url, latlonstr,
713 _autoroute_data.dest, _autoroute_data.avoid_highways,0))
716 _autoroute_data.in_progress = FALSE;
718 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
725 printf("%s()\n", __PRETTY_FUNCTION__);
727 route_find_nearest_point();
728 MACRO_MAP_RENDER_DATA();
729 MACRO_QUEUE_DRAW_AREA();
731 vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
735 * Add a point to the _track list. This function is slightly overloaded,
736 * since it is what houses the check for "have we moved
737 * significantly": it also initiates the re-calculation of the _near_point
738 * data, as well as calling osso_display_state_on() when we have the focus.
740 * If a non-zero time is given, then the current position (as taken from the
741 * _pos variable) is appended to _track with the given time. If time is zero,
742 * then _point_null is appended to _track with time zero (this produces a
743 * "break" in the track).
746 track_add(time_t time, gboolean newly_fixed)
748 gboolean show_directions = TRUE;
749 gint announce_thres_unsquared;
750 gboolean ret = FALSE;
751 printf("%s(%d, %d, %d, %d)\n", __PRETTY_FUNCTION__,
752 (guint)time, newly_fixed, _pos.unitx, _pos.unity);
756 /* This is a null point. */
757 MACRO_PATH_INCREMENT_TAIL(_track);
758 *_track.tail = _point_null;
762 gboolean moving = FALSE;
763 gboolean approaching_waypoint = FALSE;
764 gint xdiff, ydiff, dopcand;
766 announce_thres_unsquared = (20+_gps.speed) * _announce_notice_ratio*32;
768 if(!_track.tail->unity
769 || ((xdiff = _pos.unitx - _track.tail->unitx), /* comma op */
770 (ydiff = _pos.unity - _track.tail->unity), /* comma op */
771 /* Check if xdiff or ydiff are huge. */
772 ((abs(xdiff) >> 12) || (abs(ydiff) >> 12)
773 /* Okay, let's see if we've moved enough to justify adding
774 * to the track. It depends on our error. I'd like to
775 * make the threshold roughly linear with respect to the
776 * P/HDOP (completely arbitrary, I know), but I also
777 * want to keep the threshold at a minimum of 2
778 * zoom-level-4 pixel, and I want dop's of less than 2 to
779 * also have a 1-pixel threshold. I also throw in some
780 * PDOP into the mix, just for fun. */
781 || ((dopcand = 8 * (_gps.pdop - 6 +(_gps.hdop*_gps.hdop))),
782 ((xdiff * xdiff) + (ydiff * ydiff)
783 >= (MAX(2, dopcand) << 8))))))
785 /* We moved enough to actually register a move. */
788 /* Update the nearest-waypoint data. */
789 if(_route.head != _route.tail
790 && (newly_fixed ? (route_find_nearest_point(), TRUE)
791 : route_update_nears(TRUE)))
793 /* Nearest waypoint has changed - re-render paths. */
795 MACRO_QUEUE_DRAW_AREA();
797 if(_show_paths & TRACKS_MASK)
799 /* Instead of calling map_render_paths(), we'll draw the new
800 * line ourselves and call gtk_widget_queue_draw_area(). */
801 gint tx1, ty1, tx2, ty2;
802 map_render_segment(_gc[COLORABLE_TRACK],
803 _gc[COLORABLE_TRACK_BREAK],
804 _track.tail->unitx, _track.tail->unity,
805 _pos.unitx, _pos.unity);
806 if(_track.tail->unity)
808 unit2screen(_track.tail->unitx, _track.tail->unity,
810 unit2screen(_pos.unitx, _pos.unity, tx2, ty2);
811 gtk_widget_queue_draw_area(_map_widget,
812 MIN(tx1, tx2) - _draw_width,
813 MIN(ty1, ty2) - _draw_width,
814 abs(tx1 - tx2) + (2 * _draw_width),
815 abs(ty1 - ty2) + (2 * _draw_width));
818 MACRO_PATH_INCREMENT_TAIL(_track);
822 /* Calculate distance to route. (point to line) */
825 gint route_x1, route_x2, route_y1, route_y2;
826 gint64 route_dist_squared_1 = INT64_MAX;
827 gint64 route_dist_squared_2 = INT64_MAX;
830 route_x1 = _near_point->unitx;
831 route_y1 = _near_point->unity;
833 /* Try previous point first. */
834 if(_near_point != _route.head && _near_point[-1].unity)
836 route_x2 = _near_point[-1].unitx;
837 route_y2 = _near_point[-1].unity;
838 slope = (gfloat)(route_y2 - route_y1)
839 / (gfloat)(route_x2 - route_x1);
841 if(route_x1 == route_x2)
843 /* Vertical line special case. */
844 route_dist_squared_1 = (_pos.unitx - route_x1)
845 * (_pos.unitx - route_x1);
849 route_dist_squared_1 = abs((slope * _pos.unitx)
850 - _pos.unity + (route_y1 - (slope * route_x1)));
851 route_dist_squared_1 =
852 route_dist_squared_1 * route_dist_squared_1
853 / ((slope * slope) + 1);
856 if(_near_point != _route.tail && _near_point[1].unity)
858 route_x2 = _near_point[1].unitx;
859 route_y2 = _near_point[1].unity;
860 slope = (gfloat)(route_y2 - route_y1)
861 / (gfloat)(route_x2 - route_x1);
863 if(route_x1 == route_x2)
865 /* Vertical line special case. */
866 route_dist_squared_2 = (_pos.unitx - route_x1)
867 * (_pos.unitx - route_x1);
871 route_dist_squared_2 = abs((slope * _pos.unitx)
872 - _pos.unity + (route_y1 - (slope * route_x1)));
873 route_dist_squared_2 =
874 route_dist_squared_2 * route_dist_squared_2
875 / ((slope * slope) + 1);
879 /* Check if our distance from the route is large. */
880 if(MIN(route_dist_squared_1, route_dist_squared_2)
883 /* Prevent announcments from occurring. */
884 announce_thres_unsquared = INT_MAX;
886 if(_autoroute_data.enabled && !_autoroute_data.in_progress)
888 MACRO_BANNER_SHOW_INFO(_window,
889 _("Recalculating directions..."));
890 _autoroute_data.in_progress = TRUE;
891 show_directions = FALSE;
892 g_idle_add((GSourceFunc)auto_route_dl_idle, NULL);
896 /* Reset the route to try and find the nearest point.*/
902 /* Keep the display on. */
906 if(_initial_distance_waypoint
907 && (_next_way != _initial_distance_waypoint
908 || _next_way_dist_squared > (_initial_distance_from_waypoint
909 * _initial_distance_from_waypoint)))
911 /* We've moved on to the next waypoint, or we're really far from
912 * the current waypoint. */
915 gtk_widget_destroy(_waypoint_banner);
916 _waypoint_banner = NULL;
918 _initial_distance_from_waypoint = -1.f;
919 _initial_distance_waypoint = NULL;
922 /* Check if we should announce upcoming waypoints. */
924 && (_initial_distance_waypoint || _next_way_dist_squared
925 < (announce_thres_unsquared * announce_thres_unsquared)))
929 if(!_initial_distance_waypoint)
931 /* First time we're close enough to this waypoint. */
933 /* And that we haven't already announced it. */
934 && strcmp(_next_way->desc, _last_spoken_phrase))
936 g_free(_last_spoken_phrase);
937 _last_spoken_phrase = g_strdup(_next_way->desc);
940 /* We are the fork child. Synthesize the voice. */
941 hildon_play_system_sound(
942 "/usr/share/sounds/ui-information_note.wav");
944 # define _voice_synth_path "/usr/bin/flite"
945 printf("%s %s\n", _voice_synth_path,
946 _last_spoken_phrase);
947 execl(_voice_synth_path, _voice_synth_path,
948 "-t", _last_spoken_phrase, (char *)NULL);
952 _initial_distance_from_waypoint
953 = sqrtf(_next_way_dist_squared);
954 _initial_distance_waypoint = _next_way;
955 if(_next_wpt && _next_wpt->unity != 0)
957 /* Create a banner for us the show progress. */
958 _waypoint_banner = hildon_banner_show_progress(
959 _window, NULL, _next_way->desc);
963 /* This is the last point in a segment, i.e.
964 * "Arrive at ..." - just announce. */
965 MACRO_BANNER_SHOW_INFO(_window, _next_way->desc);
968 else if(_waypoint_banner);
970 /* We're already close to this waypoint. */
971 gdouble fraction = 1.f - (sqrtf(_next_way_dist_squared)
972 / _initial_distance_from_waypoint);
973 BOUND(fraction, 0.f, 1.f);
974 hildon_banner_set_fraction(
975 HILDON_BANNER(_waypoint_banner), fraction);
978 approaching_waypoint = TRUE;
980 else if(_next_way_dist_squared > 2 * (_initial_distance_from_waypoint
981 * _initial_distance_from_waypoint))
983 /* We're too far away now - destroy the banner. */
986 UNBLANK_SCREEN(moving, approaching_waypoint);
989 /* Maybe update the track database. */
991 static time_t last_track_db_update = 0;
992 if(!time || (time - last_track_db_update > 60
993 && _track.tail - _track.head + 1 > _track_index_last_saved))
995 path_update_track_in_db();
996 last_track_db_update = time;
1000 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1009 confirm = hildon_note_new_confirmation(GTK_WINDOW(_window),
1010 _("Really clear the track?"));
1012 if(GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
1013 MACRO_PATH_FREE(_track);
1014 MACRO_PATH_INIT(_track);
1015 path_save_track_to_db();
1019 gtk_widget_destroy(confirm);
1023 track_insert_break(gboolean temporary)
1025 printf("%s()\n", __PRETTY_FUNCTION__);
1027 if(_track.tail->unity)
1030 unit2screen(_track.tail->unitx, _track.tail->unity, x1, y1);
1032 MACRO_PATH_INCREMENT_TAIL(_track);
1033 *_track.tail = _point_null;
1035 /* To mark a "waypoint" in a track, we'll add a (0, 0) point and then
1036 * another instance of the most recent track point. */
1039 MACRO_PATH_INCREMENT_TAIL(_track);
1040 *_track.tail = _track.tail[-2];
1043 /** Instead of calling map_render_paths(), we'll just add the waypoint
1045 /* Make sure this circle will be visible. */
1046 if((x1 < _view_width_pixels) && (y1 < _view_height_pixels))
1047 gdk_draw_arc(_map_pixmap, _gc[COLORABLE_TRACK_BREAK],
1048 FALSE, /* FALSE: not filled. */
1053 0, /* start at 0 degrees. */
1058 MACRO_BANNER_SHOW_INFO(_window, _("Break already inserted."));
1061 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1065 * Cancel the current auto-route.
1070 printf("%s()\n", __PRETTY_FUNCTION__);
1072 if(_autoroute_data.enabled)
1074 _autoroute_data.enabled = FALSE;
1075 g_free(_autoroute_data.source_url);
1076 _autoroute_data.source_url = NULL;
1077 g_free(_autoroute_data.dest);
1078 _autoroute_data.dest = NULL;
1079 _autoroute_data.in_progress = FALSE;
1082 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1086 find_nearest_waypoint(gint unitx, gint unity)
1090 gint64 nearest_squared;
1091 Point pos = { unitx, unity, 0, INT_MIN };
1092 printf("%s()\n", __PRETTY_FUNCTION__);
1094 wcurr = wnear = _route.whead;
1095 if(wcurr && wcurr <= _route.wtail)
1097 nearest_squared = DISTANCE_SQUARED(pos, *(wcurr->point));
1099 wnear = _route.whead;
1100 while(++wcurr <= _route.wtail)
1102 gint64 test_squared = DISTANCE_SQUARED(pos, *(wcurr->point));
1103 if(test_squared < nearest_squared)
1106 nearest_squared = test_squared;
1110 /* Only use the waypoint if it is within a 6*_draw_width square drawn
1111 * around the position. This is consistent with select_poi(). */
1112 if(abs(unitx - wnear->point->unitx) < pixel2unit(3 * _draw_width)
1113 && abs(unity - wnear->point->unity) < pixel2unit(3 * _draw_width))
1117 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1122 origin_type_selected(GtkWidget *toggle,
1123 OriginToggleInfo *oti)
1125 printf("%s()\n", __PRETTY_FUNCTION__);
1127 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)))
1129 gtk_widget_set_sensitive(oti->txt_from, toggle == oti->rad_use_text);
1130 gtk_widget_set_sensitive(oti->chk_auto, toggle == oti->rad_use_gps);
1132 vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1137 * Display a dialog box to the user asking them to download a route. The
1138 * "From" and "To" textfields may be initialized using the first two
1139 * parameters. The third parameter, if set to TRUE, will cause the "Use GPS
1140 * Location" checkbox to be enabled, which automatically sets the "From" to the
1141 * current GPS position (this overrides any value that may have been passed as
1142 * the "To" initializer).
1143 * None of the passed strings are freed - that is left to the caller, and it is
1144 * safe to free either string as soon as this function returns.
1147 route_download(gchar *to)
1149 static GtkWidget *dialog = NULL;
1150 static GtkWidget *table = NULL;
1151 static GtkWidget *label = NULL;
1152 static GtkWidget *txt_source_url = NULL;
1153 static OriginToggleInfo oti;
1154 printf("%s()\n", __PRETTY_FUNCTION__);
1156 conic_recommend_connected();
1160 GtkEntryCompletion *from_comp;
1161 GtkEntryCompletion *to_comp;
1162 dialog = gtk_dialog_new_with_buttons(_("Download Route"),
1163 GTK_WINDOW(_window), GTK_DIALOG_MODAL,
1164 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1165 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1168 /* Enable the help button. */
1170 hildon_help_dialog_help_enable(
1172 ossohelp_dialog_help_enable(
1174 GTK_DIALOG(dialog), HELP_ID_DOWNROUTE, _osso);
1176 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1177 table = gtk_table_new(2, 5, FALSE), TRUE, TRUE, 0);
1180 gtk_table_attach(GTK_TABLE(table),
1181 label = gtk_label_new(_("Source URL")),
1182 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
1183 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1184 gtk_table_attach(GTK_TABLE(table),
1185 txt_source_url = gtk_entry_new(),
1186 1, 4, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
1187 gtk_entry_set_width_chars(GTK_ENTRY(txt_source_url), 25);
1189 /* Use GPS Location. */
1190 gtk_table_attach(GTK_TABLE(table),
1191 oti.rad_use_gps = gtk_radio_button_new_with_label(NULL,
1192 _("Use GPS Location")),
1193 0, 2, 1, 2, GTK_FILL, 0, 2, 4);
1195 /* Use End of Route. */
1196 gtk_table_attach(GTK_TABLE(table),
1197 oti.rad_use_route = gtk_radio_button_new_with_label_from_widget(
1198 GTK_RADIO_BUTTON(oti.rad_use_gps), _("Use End of Route")),
1199 0, 2, 2, 3, GTK_FILL, 0, 2, 4);
1201 gtk_table_attach(GTK_TABLE(table),
1202 gtk_vseparator_new(),
1203 2, 3, 1, 3, GTK_FILL, GTK_FILL, 2,4);
1206 gtk_table_attach(GTK_TABLE(table),
1207 oti.chk_auto = gtk_check_button_new_with_label(
1209 3, 4, 1, 2, GTK_FILL, 0, 2, 4);
1211 /* Avoid Highways. */
1212 gtk_table_attach(GTK_TABLE(table),
1213 oti.chk_avoid_highways = gtk_check_button_new_with_label(
1214 _("Avoid Highways")),
1215 3, 4, 2, 3, GTK_FILL, 0, 2, 4);
1219 gtk_table_attach(GTK_TABLE(table),
1220 oti.rad_use_text = gtk_radio_button_new_with_label_from_widget(
1221 GTK_RADIO_BUTTON(oti.rad_use_gps), _("Origin")),
1222 0, 1, 3, 4, GTK_FILL, 0, 2, 4);
1223 gtk_table_attach(GTK_TABLE(table),
1224 oti.txt_from = gtk_entry_new(),
1225 1, 4, 3, 4, GTK_EXPAND | GTK_FILL, 0, 2, 4);
1226 gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_from), 25);
1228 g_object_set(G_OBJECT(oti.txt_from), "hildon-input-mode",
1229 HILDON_GTK_INPUT_MODE_FULL, NULL);
1231 g_object_set(G_OBJECT(oti.txt_from), HILDON_AUTOCAP, FALSE, NULL);
1235 gtk_table_attach(GTK_TABLE(table),
1236 label = gtk_label_new(_("Destination")),
1237 0, 1, 4, 5, GTK_FILL, 0, 2, 4);
1238 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1239 gtk_table_attach(GTK_TABLE(table),
1240 oti.txt_to = gtk_entry_new(),
1241 1, 4, 4, 5, GTK_EXPAND | GTK_FILL, 0, 2, 4);
1242 gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_to), 25);
1244 g_object_set(G_OBJECT(oti.txt_to), "hildon-input-mode",
1245 HILDON_GTK_INPUT_MODE_FULL, NULL);
1247 g_object_set(G_OBJECT(oti.txt_to), HILDON_AUTOCAP, FALSE, NULL);
1251 /* Set up auto-completion. */
1252 from_comp = gtk_entry_completion_new();
1253 gtk_entry_completion_set_model(from_comp, GTK_TREE_MODEL(_loc_model));
1254 gtk_entry_completion_set_text_column(from_comp, 0);
1255 gtk_entry_set_completion(GTK_ENTRY(oti.txt_from), from_comp);
1257 to_comp = gtk_entry_completion_new();
1258 gtk_entry_completion_set_model(to_comp, GTK_TREE_MODEL(_loc_model));
1259 gtk_entry_completion_set_text_column(to_comp, 0);
1260 gtk_entry_set_completion(GTK_ENTRY(oti.txt_to), to_comp);
1263 g_signal_connect(G_OBJECT(oti.rad_use_gps), "toggled",
1264 G_CALLBACK(origin_type_selected), &oti);
1265 g_signal_connect(G_OBJECT(oti.rad_use_route), "toggled",
1266 G_CALLBACK(origin_type_selected), &oti);
1267 g_signal_connect(G_OBJECT(oti.rad_use_text), "toggled",
1268 G_CALLBACK(origin_type_selected), &oti);
1270 gtk_widget_set_sensitive(oti.chk_auto, FALSE);
1273 /* Initialize fields. */
1275 gtk_entry_set_text(GTK_ENTRY(txt_source_url), _route_dl_url);
1277 gtk_entry_set_text(GTK_ENTRY(oti.txt_to), to);
1279 /* Use "End of Route" by default if they have a route. */
1280 if(_route.head != _route.tail)
1282 /* There is no route, so make it the default. */
1283 gtk_widget_set_sensitive(oti.rad_use_route, TRUE);
1284 gtk_toggle_button_set_active(
1285 GTK_TOGGLE_BUTTON(oti.rad_use_route), TRUE);
1286 gtk_widget_grab_focus(oti.rad_use_route);
1288 /* Else use "GPS Location" if they have GPS enabled. */
1291 /* There is no route, so desensitize "Use End of Route." */
1292 gtk_widget_set_sensitive(oti.rad_use_route, FALSE);
1295 gtk_toggle_button_set_active(
1296 GTK_TOGGLE_BUTTON(oti.rad_use_gps), TRUE);
1297 gtk_widget_grab_focus(oti.rad_use_gps);
1299 /* Else use text. */
1302 gtk_toggle_button_set_active(
1303 GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
1304 gtk_widget_grab_focus(oti.txt_from);
1308 gtk_widget_show_all(dialog);
1310 while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
1312 gchar buffer[BUFFER_SIZE];
1313 const gchar *source_url, *from, *to;
1314 gboolean avoid_highways;
1316 source_url = gtk_entry_get_text(GTK_ENTRY(txt_source_url));
1317 if(!strlen(source_url))
1319 popup_error(dialog, _("Please specify a source URL."));
1324 g_free(_route_dl_url);
1325 _route_dl_url = g_strdup(source_url);
1328 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.rad_use_gps)))
1332 g_ascii_formatd(strlat, 32, "%.06f", _gps.lat);
1333 g_ascii_formatd(strlon, 32, "%.06f", _gps.lon);
1334 snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
1337 else if(gtk_toggle_button_get_active(
1338 GTK_TOGGLE_BUTTON(oti.rad_use_route)))
1345 /* Use last non-zero route point. */
1346 for(p = _route.tail; !p->unity; p--) { }
1348 unit2latlon(p->unitx, p->unity, lat, lon);
1349 g_ascii_formatd(strlat, 32, "%.06f", lat);
1350 g_ascii_formatd(strlon, 32, "%.06f", lon);
1351 snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
1356 from = gtk_entry_get_text(GTK_ENTRY(oti.txt_from));
1361 popup_error(dialog, _("Please specify a start location."));
1365 to = gtk_entry_get_text(GTK_ENTRY(oti.txt_to));
1368 popup_error(dialog, _("Please specify an end location."));
1372 avoid_highways = gtk_toggle_button_get_active(
1373 GTK_TOGGLE_BUTTON(oti.chk_avoid_highways));
1374 if(route_download_and_setup(dialog, source_url, from, to,
1375 gtk_toggle_button_get_active(
1376 GTK_TOGGLE_BUTTON(oti.chk_avoid_highways)),
1377 (gtk_toggle_button_get_active(
1378 GTK_TOGGLE_BUTTON(oti.rad_use_gps)) ? 0 : 1)))
1382 /* Cancel any autoroute that might be occurring. */
1385 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.chk_auto)))
1387 _autoroute_data.source_url = g_strdup(source_url);
1388 _autoroute_data.dest = g_strdup(to);
1389 _autoroute_data.avoid_highways = avoid_highways;
1390 _autoroute_data.enabled = TRUE;
1393 /* Save Origin in Route Locations list if not from GPS. */
1394 if(gtk_toggle_button_get_active(
1395 GTK_TOGGLE_BUTTON(oti.rad_use_text))
1396 && !g_slist_find_custom(_loc_list, from,
1397 (GCompareFunc)strcmp))
1399 _loc_list = g_slist_prepend(_loc_list, g_strdup(from));
1400 gtk_list_store_insert_with_values(_loc_model, &iter,
1401 INT_MAX, 0, from, -1);
1404 /* Save Destination in Route Locations list. */
1405 if(!g_slist_find_custom(_loc_list, to,
1406 (GCompareFunc)strcmp))
1408 _loc_list = g_slist_prepend(_loc_list, g_strdup(to));
1409 gtk_list_store_insert_with_values(_loc_model, &iter,
1410 INT_MAX, 0, to, -1);
1413 /* Success! Get out of the while loop. */
1416 /* else let them try again. */
1419 gtk_widget_hide(dialog); /* Destroying causes a crash (!?!?!??!) */
1421 vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1426 route_add_way_dialog(gint unitx, gint unity)
1429 gchar tmp1[LL_FMT_LEN], tmp2[LL_FMT_LEN], *p_latlon;
1430 static GtkWidget *dialog = NULL;
1431 static GtkWidget *table = NULL;
1432 static GtkWidget *label = NULL;
1433 static GtkWidget *txt_scroll = NULL;
1434 static GtkWidget *txt_desc = NULL;
1435 printf("%s()\n", __PRETTY_FUNCTION__);
1439 dialog = gtk_dialog_new_with_buttons(_("Add Waypoint"),
1440 GTK_WINDOW(_window), GTK_DIALOG_MODAL,
1441 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1442 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1445 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1446 table = gtk_table_new(2, 2, FALSE), TRUE, TRUE, 0);
1448 gtk_table_attach(GTK_TABLE(table),
1449 label = gtk_label_new(_("Lat, Lon:")),
1450 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
1451 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1453 unit2latlon(unitx, unity, lat, lon);
1454 lat_format(lat, tmp1);
1455 lon_format(lon, tmp2);
1456 p_latlon = g_strdup_printf("%s, %s", tmp1, tmp2);
1457 gtk_table_attach(GTK_TABLE(table),
1458 label = gtk_label_new(p_latlon),
1459 1, 2, 0, 1, GTK_FILL, 0, 2, 4);
1460 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
1463 gtk_table_attach(GTK_TABLE(table),
1464 label = gtk_label_new(_("Description")),
1465 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
1466 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1468 txt_scroll = gtk_scrolled_window_new(NULL, NULL);
1469 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll),
1471 gtk_table_attach(GTK_TABLE(table),
1473 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
1475 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll),
1476 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1478 txt_desc = gtk_text_view_new ();
1479 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
1481 gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
1482 gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 400, 60);
1485 gtk_text_buffer_set_text(
1486 gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt_desc)), "", 0);
1488 gtk_widget_show_all(dialog);
1490 while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
1492 GtkTextBuffer *tbuf;
1493 GtkTextIter ti1, ti2;
1496 tbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt_desc));
1497 gtk_text_buffer_get_iter_at_offset(tbuf, &ti1, 0);
1498 gtk_text_buffer_get_end_iter(tbuf, &ti2);
1499 desc = gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE);
1503 /* There's a description. Add a waypoint. */
1504 MACRO_PATH_INCREMENT_TAIL(_route);
1505 _route.tail->unitx = unitx;
1506 _route.tail->unity = unity;
1507 _route.tail->time = 0;
1508 _route.tail->altitude = 0;
1510 MACRO_PATH_INCREMENT_WTAIL(_route);
1511 _route.wtail->point = _route.tail;
1513 = gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE);
1521 confirm = hildon_note_new_confirmation(GTK_WINDOW(dialog),
1522 _("Creating a \"waypoint\" with no description actually "
1523 "adds a break point. Is that what you want?"));
1525 if(GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm)))
1527 /* There's no description. Add a break by adding a (0, 0)
1528 * point (if necessary), and then the ordinary route point. */
1529 if(_route.tail->unity)
1531 MACRO_PATH_INCREMENT_TAIL(_route);
1532 *_route.tail = _point_null;
1535 MACRO_PATH_INCREMENT_TAIL(_route);
1536 _route.tail->unitx = unitx;
1537 _route.tail->unity = unity;
1538 _route.tail->time = 0;
1539 _route.tail->altitude = 0;
1542 gtk_widget_destroy(confirm);
1546 gtk_widget_destroy(confirm);
1551 route_find_nearest_point();
1553 MACRO_QUEUE_DRAW_AREA();
1556 gtk_widget_hide(dialog);
1557 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1569 gchar *settings_dir;
1570 printf("%s()\n", __PRETTY_FUNCTION__);
1572 /* Initialize settings_dir. */
1573 settings_dir = gnome_vfs_expand_initial_tilde(CONFIG_DIR_NAME);
1574 g_mkdir_with_parents(settings_dir, 0700);
1576 /* Open path database. */
1578 gchar *path_db_file;
1580 path_db_file = gnome_vfs_uri_make_full_from_relative(
1581 settings_dir, CONFIG_PATH_DB_FILE);
1583 if(!path_db_file || SQLITE_OK != sqlite3_open(path_db_file, &_path_db)
1584 /* Open worked. Now create tables, failing if they already exist. */
1585 || (sqlite3_exec(_path_db,
1586 "create table route_path ("
1587 "num integer primary key, "
1593 "create table route_way ("
1594 "route_point primary key, "
1597 "create table track_path ("
1598 "num integer primary key, "
1604 "create table track_way ("
1605 "track_point primary key, "
1606 "description text)",
1607 NULL, NULL, NULL), FALSE) /* !! Comma operator !! */
1608 /* Create prepared statements - failure here is bad! */
1609 || SQLITE_OK != sqlite3_prepare(_path_db,
1610 "select unitx, unity, time, altitude, description "
1611 "from route_path left join route_way on "
1612 "route_path.num = route_way.route_point "
1613 "order by route_path.num",
1614 -1, &_route_stmt_select, NULL)
1615 || SQLITE_OK != sqlite3_prepare(_path_db,
1616 "select unitx, unity, time, altitude, description "
1617 "from track_path left join track_way on "
1618 "track_path.num = track_way.track_point "
1619 "order by track_path.num",
1620 -1, &_track_stmt_select, NULL)
1621 || SQLITE_OK != sqlite3_prepare(_path_db,
1622 "delete from route_path",
1623 -1, &_route_stmt_delete_path, NULL)
1624 || SQLITE_OK != sqlite3_prepare(_path_db,
1625 "delete from route_way",
1626 -1, &_route_stmt_delete_way, NULL)
1627 || SQLITE_OK != sqlite3_prepare(_path_db,
1628 "insert into route_path "
1629 "(num, unitx, unity, time, altitude) "
1630 "values (NULL, ?, ?, ?, ?)",
1631 -1, &_route_stmt_insert_path, NULL)
1632 || SQLITE_OK != sqlite3_prepare(_path_db,
1633 "insert into route_way (route_point, description) "
1635 -1, &_route_stmt_insert_way, NULL)
1636 || SQLITE_OK != sqlite3_prepare(_path_db,
1637 "delete from track_path",
1638 -1, &_track_stmt_delete_path, NULL)
1639 || SQLITE_OK != sqlite3_prepare(_path_db,
1640 "delete from track_way",
1641 -1, &_track_stmt_delete_way, NULL)
1642 || SQLITE_OK != sqlite3_prepare(_path_db,
1643 "insert into track_path "
1644 "(num, unitx, unity, time, altitude) "
1645 "values (NULL, ?, ?, ?, ?)",
1646 -1, &_track_stmt_insert_path, NULL)
1647 || SQLITE_OK != sqlite3_prepare(_path_db,
1648 "insert into track_way (track_point, description) "
1650 -1, &_track_stmt_insert_way, NULL)
1651 || SQLITE_OK != sqlite3_prepare(_path_db, "begin transaction",
1652 -1, &_path_stmt_trans_begin, NULL)
1653 || SQLITE_OK != sqlite3_prepare(_path_db, "commit transaction",
1654 -1, &_path_stmt_trans_commit, NULL)
1655 || SQLITE_OK != sqlite3_prepare(_path_db, "rollback transaction",
1656 -1, &_path_stmt_trans_rollback, NULL))
1658 gchar buffer[BUFFER_SIZE];
1659 snprintf(buffer, sizeof(buffer), "%s\n%s",
1660 _("Failed to open path database. "
1661 "Tracks and routes will not be saved."),
1662 sqlite3_errmsg(_path_db));
1663 sqlite3_close(_path_db);
1665 popup_error(_window, buffer);
1669 read_path_from_db(&_route, _route_stmt_select);
1670 read_path_from_db(&_track, _track_stmt_select);
1671 _track_index_last_saved = _track.tail - _track.head - 1;
1673 g_free(path_db_file);
1676 g_free(settings_dir);
1678 _last_spoken_phrase = g_strdup("");
1680 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1686 printf("%s()\n", __PRETTY_FUNCTION__);
1689 if(_track.tail->unity)
1690 track_insert_break(FALSE);
1691 path_update_track_in_db();
1692 path_save_route_to_db();
1696 sqlite3_close(_path_db);
1700 MACRO_PATH_FREE(_track);
1701 MACRO_PATH_FREE(_route);
1703 vprintf("%s(): return\n", __PRETTY_FUNCTION__);