Undo: implement rudimentary support for undo of dive-merging

For this, an output-parameter was added to the backend merge_dives()
function. When non-zero, instead of adding the merged dive to
the preferred trip, the preferred trip is returned to the caller.

Since the new UndoObject, just like the delete-dives UndoObject,
needs to remove/readd a set of dives, the corresponding functionality
was split-off in a helper function.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-21 18:28:33 +02:00 committed by Dirk Hohndel
parent 302f6adb79
commit 014c04f8bd
7 changed files with 220 additions and 50 deletions

View file

@ -2459,7 +2459,7 @@ static void pick_trip(struct dive *res, const struct dive *pick)
/*
* Pick a trip for a dive
*/
static void merge_trip(struct dive *res, struct dive *a, struct dive *b)
static struct dive *get_preferred_trip(struct dive *a, struct dive *b)
{
dive_trip_t *atrip, *btrip;
@ -2469,39 +2469,34 @@ static void merge_trip(struct dive *res, struct dive *a, struct dive *b)
* ones.
*/
if (a->tripflag > b->tripflag)
goto pick_a;
return a;
if (a->tripflag < b->tripflag)
goto pick_b;
return b;
/* Otherwise, look at the trip data and pick the "better" one */
atrip = a->divetrip;
btrip = b->divetrip;
if (!atrip)
goto pick_b;
return b;
if (!btrip)
goto pick_a;
return a;
if (!atrip->location)
goto pick_b;
return b;
if (!btrip->location)
goto pick_a;
return a;
if (!atrip->notes)
goto pick_b;
return b;
if (!btrip->notes)
goto pick_a;
return a;
/*
* Ok, so both have location and notes.
* Pick the earlier one.
*/
if (a->when < b->when)
goto pick_a;
goto pick_b;
pick_a:
b = a;
pick_b:
pick_trip(res, b);
return a;
return b;
}
#if CURRENTLY_NOT_USED
@ -2844,7 +2839,7 @@ static int likely_same_dive(const struct dive *a, const struct dive *b)
struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded)
{
if (likely_same_dive(a, b))
return merge_dives(a, b, 0, prefer_downloaded);
return merge_dives(a, b, 0, prefer_downloaded, NULL);
return NULL;
}
@ -3335,10 +3330,15 @@ int count_dives_with_suit(const char *suit)
* be the old dive and dive b is supposed to be the newly imported
* dive. If the flag "prefer_downloaded" is set, data of the latter
* will take priority over the former.
*
* The trip the new dive should be associated with (if any) is returned
* in the "trip" output paramater. If "trip" is NULL, then the dive will
* instead be added to this trip.
*/
struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded)
struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded, struct dive_trip **trip)
{
struct dive *res = alloc_dive();
struct dive *preferred_trip;
if (offset) {
/*
@ -3358,7 +3358,11 @@ struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer
}
res->when = prefer_downloaded ? b->when : a->when;
res->selected = a->selected || b->selected;
merge_trip(res, a, b);
preferred_trip = get_preferred_trip(a, b);
if (trip)
*trip = preferred_trip->divetrip;
else
pick_trip(res, preferred_trip);
MERGE_TXT(res, a, b, notes, "\n--\n");
MERGE_TXT(res, a, b, buddy, ", ");
MERGE_TXT(res, a, b, divemaster, ", ");

View file

@ -573,7 +573,7 @@ extern int split_dive_dont_insert(const struct dive *dive, struct dive **new1, s
extern void split_dive(struct dive *);
extern int split_dive_at_time_dont_insert(const struct dive *dive, duration_t time, struct dive **new1, struct dive **new2);
extern void split_dive_at_time(struct dive *dive, duration_t time);
extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded);
extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded, struct dive_trip **trip);
extern struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded);
extern struct event *clone_event(const struct event *src_ev);
extern void copy_events(const struct divecomputer *s, struct divecomputer *d);

View file

@ -26,7 +26,7 @@
* struct dive *unregister_dive(int idx)
* void delete_single_dive(int idx)
* void add_single_dive(int idx, struct dive *dive)
* void merge_two_dives(struct dive *a, struct dive *b)
* struct dive *merge_two_dives(struct dive *a, struct dive *b)
* void select_dive(int idx)
* void deselect_dive(int idx)
* void mark_divelist_changed(int changed)
@ -1060,7 +1060,7 @@ struct dive *merge_two_dives(struct dive *a, struct dive *b)
if (i < 0 || j < 0)
// something is wrong with those dives. Bail
return NULL;
res = merge_dives(a, b, b->when - a->when, false);
res = merge_dives(a, b, b->when - a->when, false, NULL);
if (!res)
return NULL;