fix copy/paste of dive-site

The copy/pasting of dive-sites was fundamentally broken in at least two
ways:

1) The dive-site pointer in struct dive was simply overwritten, which
   breaks internal consistency. Also, no dive-site changed signals where
   sent.

2) The copied dive-site was stored as a pointer in a struct dive. Thus,
   the user could copy a dive, then delete the dive-site and paste.
   This would lead to a dangling pointer and ultimately crash the
   application.

Fix this by storing the UUID of the dive-site, not a pointer.
To do that, don't store a copy of the dive, but collect all
the data in a `dive_paste_data` structure.
If the dive site has been deleted on paste, do nothing.
Send the appropriate signals on pasting.

The mobile version had an additional bug: It kept a pointer to the
dive to be copied, which might become stale by undo.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-08-13 07:04:52 +02:00 committed by Michael Keller
parent 48b4308a7d
commit 152e6966c9
17 changed files with 359 additions and 425 deletions

View file

@ -203,40 +203,6 @@ void copy_dive(const struct dive *s, struct dive *d)
d->invalidate_cache();
}
#define CONDITIONAL_COPY_STRING(_component) \
if (what._component) \
d->_component = s->_component
// copy elements, depending on bits in what that are set
void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear)
{
if (clear)
d->clear();
CONDITIONAL_COPY_STRING(notes);
CONDITIONAL_COPY_STRING(diveguide);
CONDITIONAL_COPY_STRING(buddy);
CONDITIONAL_COPY_STRING(suit);
if (what.rating)
d->rating = s->rating;
if (what.visibility)
d->visibility = s->visibility;
if (what.divesite) {
unregister_dive_from_dive_site(d);
s->dive_site->add_dive(d);
}
if (what.tags)
d->tags = s->tags;
if (what.cylinders)
copy_cylinder_types(s, d);
if (what.weights)
d->weightsystems = s->weightsystems;
if (what.number)
d->number = s->number;
if (what.when)
d->when = s->when;
}
#undef CONDITIONAL_COPY_STRING
/* copies all events from the given dive computer before a given time
this is used when editing a dive in the planner to preserve the events
of the old dive */

View file

@ -12,6 +12,7 @@
#include <array>
#include <memory>
#include <optional>
#include <string>
#include <vector>
@ -146,24 +147,25 @@ struct dive_or_trip {
extern void cylinder_renumber(struct dive &dive, int mapping[]);
extern int same_gasmix_cylinder(const cylinder_t &cyl, int cylid, const struct dive *dive, bool check_unused);
/* when selectively copying dive information, which parts should be copied? */
struct dive_components {
unsigned int divesite : 1;
unsigned int notes : 1;
unsigned int diveguide : 1;
unsigned int buddy : 1;
unsigned int suit : 1;
unsigned int rating : 1;
unsigned int visibility : 1;
unsigned int wavesize : 1;
unsigned int current : 1;
unsigned int surge : 1;
unsigned int chill : 1;
unsigned int tags : 1;
unsigned int cylinders : 1;
unsigned int weights : 1;
unsigned int number : 1;
unsigned int when : 1;
/* Data stored when copying a dive */
struct dive_paste_data {
std::optional<uint32_t> divesite; // We save the uuid not a pointer, because the
// user might copy and then delete the dive site.
std::optional<std::string> notes;
std::optional<std::string> diveguide;
std::optional<std::string> buddy;
std::optional<std::string> suit;
std::optional<int> rating;
std::optional<int> visibility;
std::optional<int> wavesize;
std::optional<int> current;
std::optional<int> surge;
std::optional<int> chill;
std::optional<tag_list> tags;
std::optional<cylinder_table> cylinders;
std::optional<weightsystem_table> weights;
std::optional<int> number;
std::optional<timestamp_t> when;
};
extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number);
@ -179,7 +181,6 @@ struct membuffer;
extern void save_one_dive_to_mb(struct membuffer *b, const struct dive &dive, bool anonymize);
extern void copy_dive(const struct dive *s, struct dive *d);
extern void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear);
extern int legacy_format_o2pressures(const struct dive *dive, const struct divecomputer *dc);

View file

@ -181,13 +181,23 @@ bool range_contains(const Range &v, const Element &item)
// Insert into an already sorted range
template<typename Range, typename Element, typename Comp>
void range_insert_sorted(Range &v, Element &item, Comp &comp)
void range_insert_sorted(Range &v, Element &item, Comp comp)
{
auto it = std::lower_bound(std::begin(v), std::end(v), item,
[&comp](auto &a, auto &b) { return comp(a, b) < 0; });
v.insert(it, std::move(item));
}
// Insert into an already sorted range, but don't add an item twice
template<typename Range, typename Element, typename Comp>
void range_insert_sorted_unique(Range &v, Element &item, Comp comp)
{
auto it = std::lower_bound(std::begin(v), std::end(v), item,
[&comp](auto &a, auto &b) { return comp(a, b) < 0; });
if (it == std::end(v) || comp(item, *it) != 0)
v.insert(it, std::move(item));
}
template<typename Range, typename Element>
void range_remove(Range &v, const Element &item)
{