From 810903bdb9db84997dc3d32bb8e934e320784a4d Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 28 Sep 2018 10:21:23 +0200 Subject: Import: pass a dive table to process_imported_dives() Dives were directly imported into the global dive table and then merged in process_imported_dives(). Make this interface more flexible, by passing an independent dive table. The dive table of the to-be-imported dives will be sorted and merged. Then each dive is inserted in a one-by-one manner to into the global dive table. This actually introduces (at least) two functional changes: 1) If a new dive spans two old dives, it will only be merged to the first dive. But this seems like a pathological case, which is of dubious value anyway. 2) Dives unrelated to the import will not be merged. The old code would happily merge dives that were not even close to the newly imported dives. A surprising behavior. Signed-off-by: Berthold Stoeger --- desktop-widgets/divelogimportdialog.cpp | 13 +++++++------ desktop-widgets/downloadfromdivecomputer.cpp | 28 ++++++++++++---------------- desktop-widgets/mainwindow.cpp | 5 +++-- desktop-widgets/subsurfacewebservices.cpp | 5 +++-- 4 files changed, 25 insertions(+), 26 deletions(-) (limited to 'desktop-widgets') diff --git a/desktop-widgets/divelogimportdialog.cpp b/desktop-widgets/divelogimportdialog.cpp index 5df477349..874c33adc 100644 --- a/desktop-widgets/divelogimportdialog.cpp +++ b/desktop-widgets/divelogimportdialog.cpp @@ -896,14 +896,15 @@ int DiveLogImportDialog::parseTxtHeader(QString fileName, char **params, int pnr void DiveLogImportDialog::on_buttonBox_accepted() { + struct dive_table table = { 0 }; QStringList r = resultModel->result(); if (ui->knownImports->currentText() != "Manual import") { for (int i = 0; i < fileNames.size(); ++i) { if (ui->knownImports->currentText() == "Seabear CSV") { - parse_seabear_log(qPrintable(fileNames[i]), &dive_table); + parse_seabear_log(qPrintable(fileNames[i]), &table); } else if (ui->knownImports->currentText() == "Poseidon MkVI") { QPair pair = poseidonFileNames(fileNames[i]); - parse_txt_file(qPrintable(pair.second), qPrintable(pair.first), &dive_table); + parse_txt_file(qPrintable(pair.second), qPrintable(pair.first), &table); } else { char *params[49]; int pnr = 0; @@ -920,7 +921,7 @@ void DiveLogImportDialog::on_buttonBox_accepted() pnr = setup_csv_params(r, params, pnr); parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1, specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv", - &dive_table); + &table); } } } else { @@ -984,7 +985,7 @@ void DiveLogImportDialog::on_buttonBox_accepted() params[pnr++] = intdup(r.indexOf(tr("Rating"))); params[pnr++] = NULL; - parse_manual_file(qPrintable(fileNames[i]), params, pnr - 1, &dive_table); + parse_manual_file(qPrintable(fileNames[i]), params, pnr - 1, &table); } else { char *params[51]; int pnr = 0; @@ -1001,12 +1002,12 @@ void DiveLogImportDialog::on_buttonBox_accepted() pnr = setup_csv_params(r, params, pnr); parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1, specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv", - &dive_table); + &table); } } } - process_imported_dives(false); + process_imported_dives(&table, false); MainWindow::instance()->refreshDisplay(); } diff --git a/desktop-widgets/downloadfromdivecomputer.cpp b/desktop-widgets/downloadfromdivecomputer.cpp index 561ff2998..46cb0418b 100644 --- a/desktop-widgets/downloadfromdivecomputer.cpp +++ b/desktop-widgets/downloadfromdivecomputer.cpp @@ -487,34 +487,30 @@ void DownloadFromDCWidget::on_cancel_clicked() void DownloadFromDCWidget::on_ok_clicked() { - struct dive *dive; - if (currentState != DONE && currentState != ERROR) return; - // record all the dives in the 'real' dive_table - for (int i = 0; i < downloadTable.nr; i++) { + // delete non-selected dives + int total = downloadTable.nr; + int j = 0; + for (int i = 0; i < total; i++) { if (diveImportedModel->data(diveImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) - record_dive(downloadTable.dives[i]); + j++; else - clear_dive(downloadTable.dives[i]); - downloadTable.dives[i] = NULL; + delete_dive_from_table(&downloadTable, j); } - downloadTable.nr = 0; - int uniqId, idx; - // remember the last downloaded dive (on most dive computers this will be the chronologically - // first new dive) and select it again after processing all the dives MainWindow::instance()->dive_list()->unselectDives(); - dive = get_dive(dive_table.nr - 1); - if (dive != NULL) { - uniqId = get_dive(dive_table.nr - 1)->id; - process_imported_dives(preferDownloaded()); + if (downloadTable.nr > 0) { + // remember the last downloaded dive (on most dive computers this will be the chronologically + // first new dive) and select it again after processing all the dives + int uniqId = downloadTable.dives[downloadTable.nr - 1]->id; + process_imported_dives(&downloadTable, preferDownloaded()); // after process_imported_dives does any merging or resorting needed, we need // to recreate the model for the dive list so we can select the newest dive MainWindow::instance()->recreateDiveList(); - idx = get_idx_by_uniq_id(uniqId); + int idx = get_idx_by_uniq_id(uniqId); // this shouldn't be necessary - but there are reports that somehow existing dives stay selected // (but not visible as selected) MainWindow::instance()->dive_list()->unselectDives(); diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index e37c7a7bd..9049b741b 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -1733,12 +1733,13 @@ void MainWindow::importFiles(const QStringList fileNames) return; QByteArray fileNamePtr; + struct dive_table table = { 0 }; for (int i = 0; i < fileNames.size(); ++i) { fileNamePtr = QFile::encodeName(fileNames.at(i)); - parse_file(fileNamePtr.data(), &dive_table); + parse_file(fileNamePtr.data(), &table); } - process_imported_dives(false); + process_imported_dives(&table, false); refreshDisplay(); } diff --git a/desktop-widgets/subsurfacewebservices.cpp b/desktop-widgets/subsurfacewebservices.cpp index a7586c128..693463bf1 100644 --- a/desktop-widgets/subsurfacewebservices.cpp +++ b/desktop-widgets/subsurfacewebservices.cpp @@ -767,8 +767,9 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button) break; } /* parse file and import dives */ - parse_file(QFile::encodeName(zipFile.fileName()), &dive_table); - process_imported_dives(false); + struct dive_table table = { 0 }; + parse_file(QFile::encodeName(zipFile.fileName()), &table); + process_imported_dives(&table, false); MainWindow::instance()->refreshDisplay(); /* store last entered user/pass in config */ -- cgit v1.2.3-70-g09d2