Core: unify insert_trip() and insert_trip_dont_merge()

There were two versions of the insert_trip() function: one
would merge trips if a trip with the same date already existed,
the other wouldn't. The latter was introduced with the dive-list
undo work.

The problem is that the "date" of a trip (i.e. the first dive)
seems ill-defined as this is a volatile value. Moreover in
the context of making dive-import undoable this is a very
dangerous notion, as the caller needs control over when the dives
are added to a trip.

Therefore, unify these two functions and never merge trips.
The decision on merging dives now has to made by the caller.
This will be implemented in a future commit.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-11-08 17:13:17 +01:00 committed by Dirk Hohndel
parent c48056300d
commit 0618aa737f
6 changed files with 14 additions and 49 deletions

View file

@ -421,8 +421,7 @@ extern void delete_single_dive(int idx);
extern int dive_get_insertion_index(struct dive *dive);
extern void add_single_dive(int idx, struct dive *dive);
extern void insert_trip(dive_trip_t **trip);
extern void insert_trip_dont_merge(dive_trip_t *trip);
extern void insert_trip(dive_trip_t *trip);
extern void unregister_trip(dive_trip_t *trip);
extern void free_trip(dive_trip_t *trip);

View file

@ -16,8 +16,7 @@
* int init_decompression(struct dive *dive)
* void update_cylinder_related_info(struct dive *dive)
* void dump_trip_list(void)
* void insert_trip(dive_trip_t **dive_trip_p)
* void insert_trip_dont_merge(dive_trip_t *dive_trip_p)
* void insert_trip(dive_trip_t *dive_trip_p)
* void unregister_trip(dive_trip_t *trip)
* void free_trip(dive_trip_t *trip)
* void remove_dive_from_trip(struct dive *dive)
@ -742,44 +741,8 @@ void dump_trip_list(void)
}
#endif
/* insert the trip into the dive_trip_list - but ensure you don't have
* two trips for the same date; but if you have, make sure you don't
* keep the one with less information */
void insert_trip(dive_trip_t **dive_trip_p)
{
dive_trip_t *dive_trip = *dive_trip_p;
dive_trip_t **p = &dive_trip_list;
dive_trip_t *trip;
struct dive *divep;
/* Walk the dive trip list looking for the right location.. */
while ((trip = *p) != NULL && trip->when < dive_trip->when)
p = &trip->next;
if (trip && trip->when == dive_trip->when) {
if (!trip->location)
trip->location = dive_trip->location;
if (!trip->notes)
trip->notes = dive_trip->notes;
divep = dive_trip->dives;
while (divep) {
add_dive_to_trip(divep, trip);
divep = divep->next;
}
*dive_trip_p = trip;
} else {
dive_trip->next = trip;
*p = dive_trip;
}
#ifdef DEBUG_TRIP
dump_trip_list();
#endif
}
/* same as insert_trip, but don't merge trips with the same date.
* this is cruical for the merge undo-command, because there we
* add a new trip with the same date and then remove the old one. */
void insert_trip_dont_merge(dive_trip_t *dive_trip)
/* insert the trip into the dive_trip_list */
void insert_trip(dive_trip_t *dive_trip)
{
dive_trip_t **p = &dive_trip_list;
dive_trip_t *trip;
@ -790,6 +753,9 @@ void insert_trip_dont_merge(dive_trip_t *dive_trip)
dive_trip->next = trip;
*p = dive_trip;
#ifdef DEBUG_TRIP
dump_trip_list();
#endif
}
/* free resources associated with a trip structure */
@ -957,7 +923,7 @@ dive_trip_t *create_and_hookup_trip_from_dive(struct dive *dive)
dive_trip_t *dive_trip = alloc_trip();
dive_trip = create_trip_from_dive(dive);
insert_trip(&dive_trip);
insert_trip(dive_trip);
dive->tripflag = IN_TRIP;
add_dive_to_trip(dive, dive_trip);
@ -1072,7 +1038,7 @@ void autogroup_dives(void)
for(i = 0; (trip = get_dives_to_autogroup(i, &from, &to, &alloc)) != NULL; i = to) {
/* If this was newly allocated, add trip to list */
if (alloc)
insert_trip(&trip);
insert_trip(trip);
for (j = from; j < to; ++j)
add_dive_to_trip(get_dive(j), trip);
}

View file

@ -1176,7 +1176,7 @@ static void finish_active_trip(void)
if (trip) {
active_trip = NULL;
insert_trip(&trip);
insert_trip(trip);
}
}

View file

@ -279,7 +279,7 @@ void trip_end(struct parser_state *state)
{
if (!state->cur_trip)
return;
insert_trip(&state->cur_trip);
insert_trip(state->cur_trip);
state->cur_trip = NULL;
}

View file

@ -82,7 +82,7 @@ DiveToAdd DiveListBase::removeDive(struct dive *d)
dive *DiveListBase::addDive(DiveToAdd &d)
{
if (d.tripToAdd)
insert_trip_dont_merge(d.tripToAdd.release()); // Return ownership to backend
insert_trip(d.tripToAdd.release()); // Return ownership to backend
if (d.trip)
add_dive_to_trip(d.dive.get(), d.trip);
dive *res = d.dive.release(); // Give up ownership of dive
@ -259,7 +259,7 @@ static void moveDivesBetweenTrips(DivesToTrip &dives)
for (OwningTripPtr &trip: dives.tripsToAdd) {
dive_trip *t = trip.release(); // Give up ownership
createdTrips.push_back(t);
insert_trip_dont_merge(t); // Return ownership to backend
insert_trip(t); // Return ownership to backend
}
dives.tripsToAdd.clear();

View file

@ -1270,7 +1270,7 @@ bool QMLManager::undoDelete(int id)
return false;
}
if (deletedTrip)
insert_trip(&deletedTrip);
insert_trip(deletedTrip);
if (deletedDive->divetrip) {
struct dive_trip *trip = deletedDive->divetrip;
tripflag_t tripflag = deletedDive->tripflag; // this gets overwritten in add_dive_to_trip()