summaryrefslogtreecommitdiffstats
path: root/core/dive.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-07-21 18:28:33 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-10-11 16:22:27 -0700
commit014c04f8bd30740e7711f3b3a01619fd27b5b613 (patch)
tree0f7e505cc36e377bed4e24fb6f94adc93db22fb2 /core/dive.c
parent302f6adb79681da3fe53336f1e4c7525f46fd47d (diff)
downloadsubsurface-014c04f8bd30740e7711f3b3a01619fd27b5b613.tar.gz
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>
Diffstat (limited to 'core/dive.c')
-rw-r--r--core/dive.c42
1 files changed, 23 insertions, 19 deletions
diff --git a/core/dive.c b/core/dive.c
index 1e8f23623..48547182c 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -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, ", ");