summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-09-22 20:23:37 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-10-02 08:04:49 -0700
commit8f3c85f58d0693a07df70a87b265cde0cdc860ac (patch)
tree95c9ae3885dc34a0d3c0cb07160b562d554d4b91
parent10d4ba82a21e3648082ddd9bae5010881d71418a (diff)
downloadsubsurface-8f3c85f58d0693a07df70a87b265cde0cdc860ac.tar.gz
Import: get tables from DiveImportedModel not DownloadThread
When importing dives, consume the tables from DiveImportedModel and not the DownloadThread. This appears more logical and avoids an inconsistent state of the DiveImportedModel: On import the tables would be reset, but the DiveImportedModel wasn't informed of that. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--desktop-widgets/downloadfromdivecomputer.cpp14
-rw-r--r--qt-models/diveimportedmodel.cpp20
-rw-r--r--qt-models/diveimportedmodel.h1
3 files changed, 31 insertions, 4 deletions
diff --git a/desktop-widgets/downloadfromdivecomputer.cpp b/desktop-widgets/downloadfromdivecomputer.cpp
index 1e957808f..57f1955ae 100644
--- a/desktop-widgets/downloadfromdivecomputer.cpp
+++ b/desktop-widgets/downloadfromdivecomputer.cpp
@@ -525,21 +525,27 @@ void DownloadFromDCWidget::on_ok_clicked()
{
if (currentState != DONE && currentState != ERRORED)
return;
- struct dive_table *table = diveImportedModel->thread.table();
- struct dive_site_table *sites = diveImportedModel->thread.sites();
// delete non-selected dives
diveImportedModel->deleteDeselected();
- if (table->nr > 0) {
+ // TODO: use structured bindings once we go C++17
+ std::pair<struct dive_table, struct dive_site_table> tables = diveImportedModel->consumeTables();
+ if (tables.first.nr > 0) {
auto data = diveImportedModel->thread.data();
int flags = IMPORT_IS_DOWNLOADED;
if (preferDownloaded())
flags |= IMPORT_PREFER_IMPORTED;
if (ui.createNewTrip->isChecked())
flags |= IMPORT_ADD_TO_NEW_TRIP;
- Command::importDives(table, nullptr, sites, flags, data->devName());
+ Command::importDives(&tables.first, nullptr, &tables.second, flags, data->devName());
+ } else {
+ clear_dive_site_table(&tables.second);
}
+ // The dives and dive sites have been consumed, but the arrays of the tables
+ // still exist. Free them.
+ free(tables.first.dives);
+ free(tables.second.dive_sites);
if (ostcFirmwareCheck && currentState == DONE)
ostcFirmwareCheck->checkLatest(this, diveImportedModel->thread.data()->internalData());
diff --git a/qt-models/diveimportedmodel.cpp b/qt-models/diveimportedmodel.cpp
index 34c247c0c..6342972a7 100644
--- a/qt-models/diveimportedmodel.cpp
+++ b/qt-models/diveimportedmodel.cpp
@@ -155,6 +155,26 @@ void DiveImportedModel::repopulate(dive_table_t *table, struct dive_site_table *
endResetModel();
}
+std::pair<struct dive_table, struct dive_site_table> DiveImportedModel::consumeTables()
+{
+ beginResetModel();
+
+ // Move tables to result
+ struct dive_table dives;
+ struct dive_site_table sites;
+ move_dive_table(diveTable, &dives);
+ move_dive_site_table(sitesTable, &sites);
+
+ // Reset indexes
+ firstIndex = 0;
+ lastIndex = -1;
+ checkStates.clear();
+
+ endResetModel();
+
+ return std::make_pair(dives, sites);
+}
+
// Delete non-selected dives
void DiveImportedModel::deleteDeselected()
{
diff --git a/qt-models/diveimportedmodel.h b/qt-models/diveimportedmodel.h
index 5f2575bce..db0f813b2 100644
--- a/qt-models/diveimportedmodel.h
+++ b/qt-models/diveimportedmodel.h
@@ -22,6 +22,7 @@ public:
Q_INVOKABLE void clearTable();
QHash<int, QByteArray> roleNames() const;
void deleteDeselected();
+ std::pair<struct dive_table, struct dive_site_table> consumeTables(); // Returns dives and sites and resets model.
Q_INVOKABLE void recordDives();
Q_INVOKABLE void startDownload();