Ticket #46: waypoints.patch

File waypoints.patch, 9.0 KB (added by pini, 8 years ago)

Keep ongoing waypoint when restarting Navit

  • navit/bookmarks.c

    old new  
    643643struct former_destination{
    644644        enum item_type type;
    645645        char* description;
    646         struct coord c;
     646        GList* c;
    647647};
    648648
    649649static void free_former_destination(struct former_destination* former_destination){
    650650        g_free(former_destination->description);
     651        g_list_foreach(former_destination->c, (GFunc)g_free, NULL);
     652        g_list_free(former_destination->c);
    651653        g_free(former_destination);
    652654}
    653655
     
    657659        struct item *item;
    658660        struct attr attr;
    659661        struct former_destination *dest;
     662        struct coord c;
     663        int more;
    660664        GList* list = NULL;
    661665        if (map && (mr=map_rect_new(map, NULL))) {
    662666                while ((item=map_rect_get_item(mr))) {
    663667                        if (item->type != type_former_destination) continue;
    664                         dest = g_new(struct former_destination, 1);
     668                        dest = g_new0(struct former_destination, 1);
    665669                        dest->type=item->type;
    666670                        item_attr_get(item, attr_label, &attr);
    667671                        dest->description = g_strdup(attr.u.str);
    668                         item_coord_get(item, &(dest->c), 1);
     672                        more = item_coord_get(item, &c, 1);
     673                        while (more) {
     674                                dest->c = g_list_append(dest->c, g_new(struct coord, 1));
     675                                *(struct coord *)g_list_last(dest->c)->data = c;
     676                                more = item_coord_get(item, &c, 1);
     677                        }
    669678                        list = g_list_prepend(list, dest);
    670679                }
    671680                map_rect_destroy(mr);
     
    678687{
    679688        if ((dest1->type == dest2->type) &&
    680689            (!strcmp(dest1->description, dest2->description)) &&
    681             (coord_equal(&(dest1->c), &(dest2->c)))){
     690            (coord_equal((struct coord *)g_list_last(dest1->c)->data, (struct coord *)g_list_last(dest2->c)->data))){
    682691                return TRUE;
    683692        }
    684693        return FALSE;
     
    709718{
    710719        FILE *f;
    711720        GList* currdest = NULL;
     721        GList* c_list = NULL;
     722        struct coord *c;
    712723        struct former_destination *dest;
    713724        const char* prostr = projection_to_name(proj);
    714725        f=fopen(former_destination_file, "w");
     
    719730                                fprintf(f,"type=%s label=\"%s\"\n", item_to_name(dest->type), dest->description);
    720731                        else
    721732                                fprintf(f,"type=%s\n", item_to_name(dest->type));
    722                         fprintf(f,"%s%s%s0x%x %s0x%x\n",
    723                         prostr, *prostr ? ":" : "",
    724                         dest->c.x >= 0 ? "":"-", dest->c.x >= 0 ? dest->c.x : -dest->c.x,
    725                         dest->c.y >= 0 ? "":"-", dest->c.y >= 0 ? dest->c.y : -dest->c.y);
    726                        
     733                        c_list = dest->c;
     734                        do {
     735                                c = (struct coord *)c_list->data;
     736                                fprintf(f,"%s%s%s0x%x %s0x%x\n",
     737                                        prostr, *prostr ? ":" : "",
     738                                        c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
     739                                        c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y);
     740                                c_list = g_list_next(c_list);
     741                        } while (c_list);
    727742                }
    728743                fclose(f);
    729744        } else {
     
    735750 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
    736751 */
    737752void
    738 bookmarks_append_coord(struct map *former_destination_map, char *former_destination_file,
    739                 struct pcoord *c, enum item_type type, const char *description, int limit)
     753bookmarks_append_destinations(struct map *former_destination_map, char *former_destination_file,
     754                struct pcoord *c, int count, enum item_type type, const char *description, int limit)
    740755{
    741756        struct former_destination *new_dest;
    742757        GList* former_destinations = NULL;
    743758        GList* former_destinations_shortened = NULL;
     759        struct coord* c_dup;
    744760        int no_of_former_destinations;
     761        int i;
    745762
    746763        former_destinations = read_former_destination_map_as_list(former_destination_map);
    747764
    748         new_dest = g_new(struct former_destination, 1);
     765        new_dest = g_new0(struct former_destination, 1);
    749766        new_dest->type = type;
    750767        new_dest->description = g_strdup(description?description:_("Map Point"));
    751         new_dest->c.x = c->x;
    752         new_dest->c.y = c->y;
     768        for (i=0; i<count; i++) {
     769                c_dup = g_new(struct coord, 1);
     770                c_dup->x = c[i].x;
     771                c_dup->y = c[i].y;
     772                new_dest->c = g_list_append(new_dest->c, c_dup);
     773        }
    753774        former_destinations = remove_destination_from_list(new_dest, former_destinations);
    754775        former_destinations = g_list_append(former_destinations, new_dest);
    755776
     
    764785        g_list_free(former_destinations);
    765786}
    766787
     788/**
     789 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
     790 */
     791void
     792bookmarks_replace_destinations(struct map *former_destination_map, char *former_destination_file,
     793                struct pcoord *c, int count, enum item_type type, const char *description, int limit)
     794{
     795        GList* former_destinations = NULL;
     796        struct former_destination *dest;
     797
     798        /* First, remove the last former_destination */
     799        former_destinations = read_former_destination_map_as_list(former_destination_map);
     800        dest = g_list_last(former_destinations)->data;
     801        former_destinations = g_list_remove(former_destinations, dest);
     802        free_former_destination(dest);
     803        write_former_destinations(former_destinations, former_destination_file, map_projection(former_destination_map));
     804
     805        /* Then, append the new one */
     806        bookmarks_append_destinations(former_destination_map, former_destination_file, c, count, type, description, limit);
     807}
  • navit/bookmarks.h

    old new  
    5454void bookmarks_set_center_from_file(struct bookmarks *this_, char *file);
    5555char* bookmarks_get_center_file(gboolean create);
    5656void bookmarks_write_center_to_file(struct bookmarks *this_, char *file);
    57 void bookmarks_append_coord(struct map *former_destination_map, char *file, struct pcoord *c, enum item_type type, const char *description, int limit);
     57void bookmarks_append_destinations(struct map *former_destination_map, char *file, struct pcoord *c, int count, enum item_type type, const char *description, int limit);
     58void bookmarks_replace_destinations(struct map *former_destination_map, char *file, struct pcoord *c, int count, enum item_type type, const char *description, int limit);
    5859/* end of prototypes */
    5960
    6061#ifdef __cplusplus
  • navit/navit.c

    old new  
    15411541navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
    15421542{
    15431543        char *destination_file;
     1544        struct pcoord pc[16];
     1545        int count;
    15441546        destination_file = bookmarks_get_destination_file(TRUE);
    15451547        if (c) {
     1548                dbg(1, "c=(%i,%i)\n", c->x,c->y);
     1549                if (this_->destination_valid && this_->waypoints_flag) {
     1550                        count=route_get_destinations(this_->route, pc, 16);
     1551                        if (count < 16) {
     1552                                pc[count++] = *c;
     1553                                bookmarks_replace_destinations(this_->former_destination, destination_file, pc, count, type_former_destination, description, this_->recentdest_count);
     1554                        }
     1555                } else {
     1556                        bookmarks_append_destinations(this_->former_destination, destination_file, c, 1, type_former_destination, description, this_->recentdest_count);
     1557                }
    15461558                this_->destination=*c;
    15471559                this_->destination_valid=1;
    1548 
    1549                 dbg(1, "c=(%i,%i)\n", c->x,c->y);
    1550                 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_destination, description, this_->recentdest_count);
    15511560        } else {
    15521561                this_->destination_valid=0;
    15531562                navit_mark_navigation_stopped(destination_file);
     
    15851594                this_->destination_valid=1;
    15861595
    15871596                destination_file = bookmarks_get_destination_file(TRUE);
    1588                 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_itinerary, description, this_->recentdest_count);
     1597                bookmarks_append_destinations(this_->former_destination, destination_file, c, count, type_former_itinerary, description, this_->recentdest_count);
    15891598                g_free(destination_file);
    15901599        } else
    15911600                this_->destination_valid=0;
     
    29802989        if (this_->route) {
    29812990                switch(route_destination_reached(this_->route)) {
    29822991                case 1:
    2983                         description=route_get_destination_description(this_->route, 0);
    29842992                        route_remove_waypoint(this_->route);
    29852993                        count=route_get_destinations(this_->route, pc, 16);
     2994                        description=route_get_destination_description(this_->route, count-1);
    29862995                        destination_file = bookmarks_get_destination_file(TRUE);
    2987                         bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, description, this_->recentdest_count);
     2996                        bookmarks_append_destinations(this_->former_destination, destination_file, pc, count, type_former_destination, description, this_->recentdest_count);
    29882997                        g_free(description);
    29892998                        break; 
    29902999                case 2:
    29913000                        description=route_get_destination_description(this_->route, 0);
    29923001                        count=route_get_destinations(this_->route, pc, 1);
    29933002                        destination_file = bookmarks_get_destination_file(TRUE);
    2994                         bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, description, this_->recentdest_count);
     3003                        bookmarks_append_destinations(this_->former_destination, destination_file, pc, 1, type_former_destination, description, this_->recentdest_count);
    29953004                        g_free(description);
    29963005                        navit_set_destination(this_, NULL, NULL, 0);
    29973006                        break;