summaryrefslogtreecommitdiffstats
path: root/desktop-widgets
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-03-05 22:58:47 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 18:19:07 +0300
commite2df38d868324f2a767d2109cdd8727acb866ca2 (patch)
tree614b560431d2c61f3f2b1e2f986c7806bd175ae9 /desktop-widgets
parentc22fd9f4fd6699333629b8acb1e9c135a9783082 (diff)
downloadsubsurface-e2df38d868324f2a767d2109cdd8727acb866ca2.tar.gz
Dive site: add dive site ref-counting
Instead of setting dive->dive_site directly, call the add_dive_to_dive_site() and unregister_dive_from_dive_site() functions. In the parser this turned out to be a bit tricky. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets')
-rw-r--r--desktop-widgets/command_divelist.cpp28
-rw-r--r--desktop-widgets/command_divelist.h2
-rw-r--r--desktop-widgets/subsurfacewebservices.cpp3
-rw-r--r--desktop-widgets/tab-widgets/maintab.cpp3
4 files changed, 24 insertions, 12 deletions
diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp
index a6f67b090..089fe3ec5 100644
--- a/desktop-widgets/command_divelist.cpp
+++ b/desktop-widgets/command_divelist.cpp
@@ -66,9 +66,10 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<OwningTripPtr> &t
if (res.idx < 0)
qWarning() << "Deletion of unknown dive!";
- // remove dive from trip - if this is the last dive in the trip
+ // remove dive from trip and site - if this is the last dive in the trip
// remove the whole trip.
res.trip = unregister_dive_from_trip(d);
+ res.site = unregister_dive_from_dive_site(d);
if (res.trip && res.trip->dives.nr == 0) {
unregister_trip(res.trip, &trip_table); // Remove trip from backend
tripsToAdd.emplace_back(res.trip); // Take ownership of trip
@@ -86,6 +87,8 @@ dive *DiveListBase::addDive(DiveToAdd &d)
{
if (d.trip)
add_dive_to_trip(d.dive.get(), d.trip);
+ if (d.site)
+ add_dive_to_dive_site(d.dive.get(), d.site);
dive *res = d.dive.release(); // Give up ownership of dive
// Set the filter flag according to current filter settings
@@ -516,9 +519,11 @@ AddDive::AddDive(dive *d, const QString &newDS, bool autogroup, bool newNumber)
// If we alloc a new-trip for autogrouping, get an owning pointer to it.
OwningTripPtr allocTrip;
dive_trip *trip = divePtr->divetrip;
- // We have to delete the pointer-to-trip, because this would prevent the core from adding to the trip
- // and we would get the count-of-dives in the trip wrong. Yes, that's all horribly subtle!
+ dive_site *site = divePtr->dive_site;
+ // We have to delete the pointers to trip and site, because this would prevent the core from adding to the
+ // trip or site and we would get the count-of-dives in the trip or site wrong. Yes, that's all horribly subtle!
divePtr->divetrip = nullptr;
+ divePtr->dive_site = nullptr;
if (!trip && autogroup) {
bool alloc;
trip = get_trip_for_new_dive(divePtr.get(), &alloc);
@@ -530,7 +535,7 @@ AddDive::AddDive(dive *d, const QString &newDS, bool autogroup, bool newNumber)
if (newNumber)
divePtr->number = get_dive_nr_at_idx(idx);
- divesToAdd.dives.push_back({ std::move(divePtr), trip, idx });
+ divesToAdd.dives.push_back({ std::move(divePtr), trip, site, idx });
if (allocTrip)
divesToAdd.trips.push_back(std::move(allocTrip));
}
@@ -599,13 +604,15 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str
divePtr->selected = false; // See above in AddDive::AddDive()
dive_trip *trip = divePtr->divetrip;
divePtr->divetrip = nullptr; // See above in AddDive::AddDive()
+ dive_site *site = divePtr->dive_site;
+ divePtr->dive_site = nullptr; // See above in AddDive::AddDive()
int idx = dive_table_get_insertion_index(&dive_table, divePtr.get());
// Note: The dives are added in reverse order of the divesToAdd array.
// This, and the fact that we populate the array in chronological order
// means that wo do *not* have to manipulated the indices.
// Yes, that's all horribly subtle.
- divesToAdd.dives.push_back({ std::move(divePtr), trip, idx });
+ divesToAdd.dives.push_back({ std::move(divePtr), trip, site, idx });
}
// Add dive to be deleted to the divesToRemove structure
@@ -969,7 +976,8 @@ MergeDives::MergeDives(const QVector <dive *> &dives)
}
dive_trip *preferred_trip;
- OwningDivePtr d(merge_dives(dives[0], dives[1], dives[1]->when - dives[0]->when, false, &preferred_trip));
+ dive_site *preferred_site;
+ OwningDivePtr d(merge_dives(dives[0], dives[1], dives[1]->when - dives[0]->when, false, &preferred_trip, &preferred_site));
// Currently, the core code selects the dive -> this is not what we want, as
// we manually manage the selection post-command.
@@ -979,13 +987,15 @@ MergeDives::MergeDives(const QVector <dive *> &dives)
// Set the preferred dive trip, so that for subsequent merges the better trip can be selected
d->divetrip = preferred_trip;
for (int i = 2; i < dives.count(); ++i) {
- d.reset(merge_dives(d.get(), dives[i], dives[i]->when - d->when, false, &preferred_trip));
- // Set the preferred dive trip, so that for subsequent merges the better trip can be selected
+ d.reset(merge_dives(d.get(), dives[i], dives[i]->when - d->when, false, &preferred_trip, &preferred_site));
+ // Set the preferred dive trip and site, so that for subsequent merges the better trip and site can be selected
d->divetrip = preferred_trip;
+ d->dive_site = preferred_site;
}
- // We got our preferred trip, so now the reference can be deleted from the newly generated dive
+ // We got our preferred trip and site, so now the references can be deleted from the newly generated dive
d->divetrip = nullptr;
+ d->dive_site = nullptr;
// The merged dive gets the number of the first dive
d->number = dives[0]->number;
diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h
index 1672c92f9..9199a2e58 100644
--- a/desktop-widgets/command_divelist.h
+++ b/desktop-widgets/command_divelist.h
@@ -12,10 +12,10 @@
namespace Command {
// This helper structure describes a dive that we want to add.
-// Potentially it also adds a trip (if deletion of the dive resulted in deletion of the trip)
struct DiveToAdd {
OwningDivePtr dive; // Dive to add
dive_trip *trip; // Trip the dive belongs to, may be null
+ dive_site *site; // Site the dive is associated with, may be null
int idx; // Position in divelist
};
diff --git a/desktop-widgets/subsurfacewebservices.cpp b/desktop-widgets/subsurfacewebservices.cpp
index e2c787990..36037f511 100644
--- a/desktop-widgets/subsurfacewebservices.cpp
+++ b/desktop-widgets/subsurfacewebservices.cpp
@@ -47,7 +47,8 @@ static void copy_gps_location(struct dive *from, struct dive *to)
struct dive_site *gds = get_dive_site_for_dive(from);
if (!ds) {
// simply link to the one created for the fake dive
- to->dive_site = gds;
+ unregister_dive_from_dive_site(to);
+ add_dive_to_dive_site(to, gds);
} else {
ds->latitude = gds->latitude;
ds->longitude = gds->longitude;
diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp
index c0a4c5018..d0e131f1e 100644
--- a/desktop-widgets/tab-widgets/maintab.cpp
+++ b/desktop-widgets/tab-widgets/maintab.cpp
@@ -674,7 +674,8 @@ struct dive_site *MainTab::updateDiveSite(struct dive_site *pickedDs, dive *d)
}
}
- d->dive_site = pickedDs;
+ unregister_dive_from_dive_site(d);
+ add_dive_to_dive_site(d, pickedDs);
qDebug() << "Setting the dive site id on the dive:" << pickedDs->uuid;
return pickedDs;
}