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

@ -38,11 +38,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleDiveSite(false)
checked: manager.pasteDiveSite
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleDiveSite(true)
}
}
Controls.Label {
text: qsTr("Notes")
@ -50,11 +47,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleNotes(false)
checked: manager.pasteNotes
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleNotes(true)
}
}
Controls.Label {
text: qsTr("Dive guide")
@ -62,11 +56,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleDiveGuide(false)
checked: manager.pasteDiveGuide
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleDiveGuide(true)
}
}
Controls.Label {
text: qsTr("Buddy")
@ -74,11 +65,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleBuddy(false)
checked: manager.pasteBuddy
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleBuddy(true)
}
}
Controls.Label {
text: qsTr("Suit")
@ -86,11 +74,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleSuit(false)
checked: manager.pasteSuit
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleSuit(true)
}
}
Controls.Label {
text: qsTr("Rating")
@ -98,11 +83,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleRating(false)
checked: manager.pasteRating
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleRating(true)
}
}
Controls.Label {
text: qsTr("Visibility")
@ -110,11 +92,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleVisibility(false)
checked: manager.pasteVisibility
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleVisibility(true)
}
}
Controls.Label {
text: qsTr("Tags")
@ -122,11 +101,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleTags(false)
checked: manager.pasteTags
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleTags(true)
}
}
Controls.Label {
text: qsTr("Cylinders")
@ -134,11 +110,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleCylinders(false)
checked: manager.pasteCylinders
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleCylinders(true)
}
}
Controls.Label {
text: qsTr("Weights")
@ -146,11 +119,8 @@ Kirigami.ScrollablePage {
Layout.preferredWidth: gridWidth * 0.75
}
SsrfSwitch {
checked: manager.toggleWeights(false)
checked: manager.pasteWeights
Layout.preferredWidth: gridWidth * 0.25
onClicked: {
manager.toggleWeights(true)
}
}
}

View file

@ -303,17 +303,18 @@ QMLManager::QMLManager() :
// make sure we know if the current cloud repo has been successfully synced
syncLoadFromCloud();
memset(&m_copyPasteDive, 0, sizeof(m_copyPasteDive));
memset(&what, 0, sizeof(what));
// Let's set some defaults to be copied so users don't necessarily need
// to know how to configure this
what.diveguide = true;
what.buddy = true;
what.suit = true;
what.tags = true;
what.cylinders = true;
what.weights = true;
m_pasteDiveSite = false;
m_pasteNotes = false;
m_pasteDiveGuide = true;
m_pasteBuddy = true;
m_pasteSuit = true;
m_pasteRating = false;
m_pasteVisibility = false;
m_pasteTags = true;
m_pasteCylinders = true;
m_pasteWeights = true;
// monitor when dives changed - but only in verbose mode
// careful - changing verbose at runtime isn't enough (of course that could be added if we want it)
@ -1607,104 +1608,40 @@ void QMLManager::toggleDiveInvalid(int id)
changesNeedSaving();
}
bool QMLManager::toggleDiveSite(bool toggle)
template <typename T>
static void assign_paste_data(bool copy, const T &src, std::optional<T> &dest)
{
if (toggle)
what.divesite = what.divesite ? false : true;
return what.divesite;
}
bool QMLManager::toggleNotes(bool toggle)
{
if (toggle)
what.notes = what.notes ? false : true;
return what.notes;
}
bool QMLManager::toggleDiveGuide(bool toggle)
{
if (toggle)
what.diveguide = what.diveguide ? false : true;
return what.diveguide;
}
bool QMLManager::toggleBuddy(bool toggle)
{
if (toggle)
what.buddy = what.buddy ? false : true;
return what.buddy;
}
bool QMLManager::toggleSuit(bool toggle)
{
if (toggle)
what.suit = what.suit ? false : true;
return what.suit;
}
bool QMLManager::toggleRating(bool toggle)
{
if (toggle)
what.rating = what.rating ? false : true;
return what.rating;
}
bool QMLManager::toggleVisibility(bool toggle)
{
if (toggle)
what.visibility = what.visibility ? false : true;
return what.visibility;
}
bool QMLManager::toggleTags(bool toggle)
{
if (toggle)
what.tags = what.tags ? false : true;
return what.tags;
}
bool QMLManager::toggleCylinders(bool toggle)
{
if (toggle)
what.cylinders = what.cylinders ? false : true;
return what.cylinders;
}
bool QMLManager::toggleWeights(bool toggle)
{
if (toggle)
what.weights = what.weights ? false : true;
return what.weights;
if (copy)
dest = src;
else
dest = {};
}
void QMLManager::copyDiveData(int id)
{
m_copyPasteDive = divelog.dives.get_by_uniq_id(id);
if (!m_copyPasteDive) {
const dive *d = divelog.dives.get_by_uniq_id(id);
if (!d) {
appendTextToLog("trying to copy non-existing dive");
return;
}
assign_paste_data(m_pasteDiveSite, d->dive_site ? d->dive_site->uuid : uint32_t(), paste_data.divesite);
assign_paste_data(m_pasteNotes, d->notes, paste_data.notes);
assign_paste_data(m_pasteDiveGuide, d->diveguide, paste_data.diveguide);
assign_paste_data(m_pasteBuddy, d->buddy, paste_data.buddy);
assign_paste_data(m_pasteSuit, d->suit, paste_data.suit);
assign_paste_data(m_pasteRating, d->rating, paste_data.rating);
assign_paste_data(m_pasteVisibility, d->visibility, paste_data.visibility);
assign_paste_data(m_pasteTags, d->tags, paste_data.tags);
assign_paste_data(m_pasteCylinders, d->cylinders, paste_data.cylinders);
assign_paste_data(m_pasteWeights, d->weightsystems, paste_data.weights);
setNotificationText("Copy");
}
void QMLManager::pasteDiveData(int id)
{
if (!m_copyPasteDive) {
appendTextToLog("dive to paste is not selected");
return;
}
Command::pasteDives(m_copyPasteDive, what);
Command::pasteDives(paste_data);
changesNeedSaving();
}

View file

@ -43,6 +43,17 @@ class QMLManager : public QObject {
Q_PROPERTY(QString progressMessage MEMBER m_progressMessage WRITE setProgressMessage NOTIFY progressMessageChanged)
Q_PROPERTY(bool btEnabled MEMBER m_btEnabled WRITE setBtEnabled NOTIFY btEnabledChanged)
Q_PROPERTY(bool pasteDiveSite MEMBER m_pasteDiveSite)
Q_PROPERTY(bool pasteNotes MEMBER m_pasteNotes)
Q_PROPERTY(bool pasteDiveGuide MEMBER m_pasteDiveGuide)
Q_PROPERTY(bool pasteBuddy MEMBER m_pasteBuddy)
Q_PROPERTY(bool pasteSuit MEMBER m_pasteSuit)
Q_PROPERTY(bool pasteRating MEMBER m_pasteRating)
Q_PROPERTY(bool pasteVisibility MEMBER m_pasteVisibility)
Q_PROPERTY(bool pasteTags MEMBER m_pasteTags)
Q_PROPERTY(bool pasteCylinders MEMBER m_pasteCylinders)
Q_PROPERTY(bool pasteWeights MEMBER m_pasteWeights)
Q_PROPERTY(QString DC_vendor READ DC_vendor WRITE DC_setVendor)
Q_PROPERTY(QString DC_product READ DC_product WRITE DC_setProduct)
Q_PROPERTY(QString DC_devName READ DC_devName WRITE DC_setDevName)
@ -187,16 +198,6 @@ public slots:
void toggleDiveInvalid(int id);
void copyDiveData(int id);
void pasteDiveData(int id);
bool toggleDiveSite(bool toggle);
bool toggleNotes(bool toggle);
bool toggleDiveGuide(bool toggle);
bool toggleBuddy(bool toggle);
bool toggleSuit(bool toggle);
bool toggleRating(bool toggle);
bool toggleVisibility(bool toggle);
bool toggleTags(bool toggle);
bool toggleCylinders(bool toggle);
bool toggleWeights(bool toggle);
void undo();
void redo();
int addDive();
@ -250,11 +251,21 @@ private:
void updateAllGlobalLists();
void updateHaveLocalChanges(bool status);
bool m_pasteDiveSite;
bool m_pasteNotes;
bool m_pasteDiveGuide;
bool m_pasteBuddy;
bool m_pasteSuit;
bool m_pasteRating;
bool m_pasteVisibility;
bool m_pasteTags;
bool m_pasteCylinders;
bool m_pasteWeights;
location_t getGps(QString &gps);
QString m_pluggedInDeviceName;
bool m_showNonDiveComputers;
struct dive *m_copyPasteDive = NULL;
struct dive_components what;
struct dive_paste_data paste_data;
QAction *undoAction;
bool verifyCredentials(QString email, QString password, QString pin);