summaryrefslogtreecommitdiffstats
path: root/core
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
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')
-rw-r--r--core/dive.c42
-rw-r--r--core/dive.h2
-rw-r--r--core/divelist.c4
3 files changed, 26 insertions, 22 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, ", ");
diff --git a/core/dive.h b/core/dive.h
index 340bc7ec8..6fd53fba9 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -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);
diff --git a/core/divelist.c b/core/divelist.c
index a25927e90..63bb7aa07 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -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;