summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-01-13 08:12:47 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-01-19 13:48:17 -0800
commit1cd0863cca678cf54dfa8a71f3ca9f94bfc4f693 (patch)
tree6c93ffdab364c718bb9876ee9ac463b773e8ea5d
parent31eb86c73365deefc2bcf51f7ad86bf4ba314379 (diff)
downloadsubsurface-1cd0863cca678cf54dfa8a71f3ca9f94bfc4f693.tar.gz
Import: add add_to_new_trip flag to process_imported_dives()
If this flag is set, dives that are not assigned to a trip will be assigned to a new trip. This flag is set if the user checked "add to new trip" in the download dialog of the desktop version. Currently this is a no-op as the dives will already have been added to a new trip by the downloading code. This will be removed in a subsequent commit. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--core/divelist.c53
-rw-r--r--core/divelist.h4
-rw-r--r--desktop-widgets/command.cpp4
-rw-r--r--desktop-widgets/command.h2
-rw-r--r--desktop-widgets/command_divelist.cpp4
-rw-r--r--desktop-widgets/command_divelist.h2
-rw-r--r--desktop-widgets/divelogimportdialog.cpp2
-rw-r--r--desktop-widgets/downloadfromdivecomputer.cpp2
-rw-r--r--desktop-widgets/mainwindow.cpp2
-rw-r--r--desktop-widgets/subsurfacewebservices.cpp2
-rw-r--r--mobile-widgets/qmlmanager.cpp2
-rw-r--r--qt-models/diveimportedmodel.cpp3
-rw-r--r--tests/testmerge.cpp8
-rw-r--r--tests/testrenumber.cpp4
14 files changed, 58 insertions, 36 deletions
diff --git a/core/divelist.c b/core/divelist.c
index 99d570065..fc4e58877 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -1487,7 +1487,7 @@ static bool dive_is_after_last(struct dive *d)
/* Merge dives from "dives_from" into "dives_to". Overlapping dives will be merged,
* non-overlapping dives will be moved. The results will be added to the "dives_to_add"
* table. Dives that were merged are added to the "dives_to_remove" table.
- * Any newly added (not merged) dive will be assigned to the trip from the "trip"
+ * Any newly added (not merged) dive will be assigned to the trip of the "trip"
* paremeter. If "delete_from" is non-null dives will be removed from this table.
* This function supposes that all input tables are sorted.
* Returns true if any dive was added (not merged) that is not past the
@@ -1571,7 +1571,8 @@ static bool merge_dive_tables(struct dive_table *dives_from, struct dive_table *
* and dive_table "dives_to". If "prefer_imported" is true, dive data of "from" takes
* precedence */
void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
- bool prefer_imported, bool downloaded, bool merge_all_trips)
+ bool prefer_imported, bool downloaded, bool merge_all_trips,
+ bool add_to_new_trip)
{
int i, idx;
struct dive_table dives_to_add = { 0 };
@@ -1581,7 +1582,7 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
/* Process imported dives and generate lists of dives
* to-be-added and to-be-removed */
process_imported_dives(import_table, import_trip_table,
- prefer_imported, downloaded, merge_all_trips,
+ prefer_imported, downloaded, merge_all_trips, add_to_new_trip,
&dives_to_add, &dives_to_remove, &trips_to_add);
/* Add new dives to trip, so that trips don't get deleted
@@ -1625,7 +1626,7 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
* The bool pointed to by "sequence_changed" is set to true, if the sequence of
* the existing dives changes.
* The int pointed to by "start_renumbering_at" keeps track of the first dive
- * to be renumbered.
+ * to be renumbered in the dives_to_add table.
* For other parameters see process_imported_dives()
* Returns true if trip was merged. In this case, the trip will be
* freed.
@@ -1669,21 +1670,22 @@ bool try_to_merge_trip(struct dive_trip *trip_import, struct dive_table *import_
* *not* be part of the trip. The caller has to add them to the trip.
*
* The lists are generated by merging dives if possible. This is
- * performed trip-wise. If prefer_imported is true, data of the
- * new dives are prioritized in such a case. If merge_all_trips is
+ * performed trip-wise. If "prefer_imported" is true, data of the
+ * new dives are prioritized in such a case. If "merge_all_trips" is
* true, all overlapping trips will be merged, not only non-autogenerated
- * trips. If downloaded is true, only the divecomputer of the first dive
+ * trips. If "downloaded" is true, only the divecomputer of the first dive
* will be considered, as it is assumed that all dives come from
- * the same computer.
+ * the same computer. If "add_to_new_trip" is true, dives that are not
+ * assigned to a trip will be added to a newly generated trip.
*/
void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
- bool prefer_imported, bool downloaded, bool merge_all_trips,
+ bool prefer_imported, bool downloaded, bool merge_all_trips, bool add_to_new_trip,
/* output parameters: */
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
struct trip_table *trips_to_add)
{
int i, nr, start_renumbering_at = 0;
- struct dive_trip *trip_import;
+ struct dive_trip *trip_import, *new_trip;
int preexisting;
bool sequence_changed = false;
bool new_dive_has_number = false;
@@ -1721,8 +1723,10 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
sort_dive_table(import_table);
merge_imported_dives(import_table);
- /* Autogroup dives if desired by user. */
- autogroup_dives(import_table, import_trip_table);
+ /* Autogroup tripless dives if desired by user. But don't autogroup
+ * if tripless dives should be added to a new trip. */
+ if (!add_to_new_trip)
+ autogroup_dives(import_table, import_trip_table);
preexisting = dive_table.nr; /* Remember old size for renumbering */
@@ -1756,10 +1760,27 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
}
import_trip_table->nr = 0; /* All trips were consumed */
- /* The remaining dives in import_table are those that don't belong to
- * a trip. Merge them into the global table. */
- sequence_changed |= merge_dive_tables(import_table, NULL, &dive_table, prefer_imported, NULL,
- dives_to_add, dives_to_remove, &start_renumbering_at);
+ if (add_to_new_trip && import_table->nr > 0) {
+ /* Create a new trip for unassigned dives, if desired. */
+ new_trip = create_trip_from_dive(import_table->dives[0]);
+ insert_trip(new_trip, trips_to_add);
+
+ /* Add all remaining dives to this trip */
+ for (i = 0; i < import_table->nr; i++) {
+ struct dive *d = import_table->dives[i];
+ d->divetrip = new_trip;
+ insert_dive(dives_to_add, d);
+ sequence_changed |= !dive_is_after_last(d);
+ }
+
+ import_table->nr = 0; /* All dives were consumed */
+ } else if (import_table->nr > 0) {
+ /* The remaining dives in import_table are those that don't belong to
+ * a trip and the caller does not want them to be associated to a
+ * new trip. Merge them into the global table. */
+ sequence_changed |= merge_dive_tables(import_table, NULL, &dive_table, prefer_imported, NULL,
+ dives_to_add, dives_to_remove, &start_renumbering_at);
+ }
/* If new dives were only added at the end, renumber the added dives.
* But only if
diff --git a/core/divelist.h b/core/divelist.h
index d94fedc38..5c1667314 100644
--- a/core/divelist.h
+++ b/core/divelist.h
@@ -19,9 +19,9 @@ extern int init_decompression(struct deco_state *ds, struct dive *dive);
/* divelist core logic functions */
extern void process_loaded_dives();
extern void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
- bool prefer_imported, bool downloaded, bool merge_all_trips);
+ bool prefer_imported, bool downloaded, bool merge_all_trips, bool add_to_new_trip);
extern void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
- bool prefer_imported, bool downloaded, bool merge_all_trips,
+ bool prefer_imported, bool downloaded, bool merge_all_trips, bool add_to_new_trip,
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
struct trip_table *trips_to_add);
extern char *get_dive_gas_string(const struct dive *dive);
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp
index 1fc6fbe1c..e297f70d0 100644
--- a/desktop-widgets/command.cpp
+++ b/desktop-widgets/command.cpp
@@ -13,9 +13,9 @@ void addDive(dive *d, bool autogroup, bool newNumber)
void importDives(struct dive_table *dives, struct trip_table *trips,
bool prefer_imported, bool downloaded, bool merge_all_trips,
- const QString &source)
+ bool add_to_new_trip, const QString &source)
{
- execute(new ImportDives(dives, trips, prefer_imported, downloaded, merge_all_trips, source));
+ execute(new ImportDives(dives, trips, prefer_imported, downloaded, merge_all_trips, add_to_new_trip, source));
}
void deleteDive(const QVector<struct dive*> &divesToDelete)
diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h
index 83bde3b3a..cbc416e23 100644
--- a/desktop-widgets/command.h
+++ b/desktop-widgets/command.h
@@ -23,7 +23,7 @@ void addDive(dive *d, bool autogroup, bool newNumber); // If d->dive_trip is nul
// insertion position.
void importDives(struct dive_table *dives, struct trip_table *trips,
bool prefer_imported, bool downloaded, bool merge_all_trips,
- const QString &source);
+ bool add_to_new_trip, const QString &source);
void deleteDive(const QVector<struct dive*> &divesToDelete);
void shiftTime(const QVector<dive *> &changedDives, int amount);
void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber);
diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp
index 4e985cf28..e7edaaa54 100644
--- a/desktop-widgets/command_divelist.cpp
+++ b/desktop-widgets/command_divelist.cpp
@@ -554,7 +554,7 @@ void AddDive::undoit()
ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips,
bool prefer_imported, bool downloaded, bool merge_all_trips,
- const QString &source)
+ bool add_to_new_trip, const QString &source)
{
setText(tr("import %n dive(s) from %1", "", dives->nr).arg(source));
@@ -562,7 +562,7 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips,
struct dive_table dives_to_remove = { 0 };
struct trip_table trips_to_add = { 0 };
process_imported_dives(dives, trips, prefer_imported, downloaded, merge_all_trips,
- &dives_to_add, &dives_to_remove, &trips_to_add);
+ add_to_new_trip, &dives_to_add, &dives_to_remove, &trips_to_add);
// Add trips to the divesToAdd.trips structure
divesToAdd.trips.reserve(trips_to_add.nr);
diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h
index 8eca0bb57..b823da7e7 100644
--- a/desktop-widgets/command_divelist.h
+++ b/desktop-widgets/command_divelist.h
@@ -100,7 +100,7 @@ public:
// Note: dives and trips are consumed - after the call they will be empty.
ImportDives(struct dive_table *dives, struct trip_table *trips,
bool prefer_imported, bool downloaded, bool merge_all_trips,
- const QString &source);
+ bool add_to_new_trip, const QString &source);
private:
void undoit() override;
void redoit() override;
diff --git a/desktop-widgets/divelogimportdialog.cpp b/desktop-widgets/divelogimportdialog.cpp
index 638cf6094..869fb9d25 100644
--- a/desktop-widgets/divelogimportdialog.cpp
+++ b/desktop-widgets/divelogimportdialog.cpp
@@ -1012,7 +1012,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
}
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
- Command::importDives(&table, &trips, false, false, true, source);
+ Command::importDives(&table, &trips, false, false, true, false, source);
}
TagDragDelegate::TagDragDelegate(QObject *parent) : QStyledItemDelegate(parent)
diff --git a/desktop-widgets/downloadfromdivecomputer.cpp b/desktop-widgets/downloadfromdivecomputer.cpp
index 052b71c44..a3bc6287c 100644
--- a/desktop-widgets/downloadfromdivecomputer.cpp
+++ b/desktop-widgets/downloadfromdivecomputer.cpp
@@ -518,7 +518,7 @@ void DownloadFromDCWidget::on_ok_clicked()
if (table->nr > 0) {
auto data = thread.data();
- Command::importDives(table, trips, preferDownloaded(), true, false, data->devName());
+ Command::importDives(table, trips, preferDownloaded(), true, false, ui.createNewTrip->isChecked(), data->devName());
}
if (ostcFirmwareCheck && currentState == DONE)
diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp
index 64cdc6cb3..18b1bd5fa 100644
--- a/desktop-widgets/mainwindow.cpp
+++ b/desktop-widgets/mainwindow.cpp
@@ -1716,7 +1716,7 @@ void MainWindow::importFiles(const QStringList fileNames)
parse_file(fileNamePtr.data(), &table, &trips);
}
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
- Command::importDives(&table, &trips, false, false, true, source);
+ Command::importDives(&table, &trips, false, false, true, false, source);
}
void MainWindow::loadFiles(const QStringList fileNames)
diff --git a/desktop-widgets/subsurfacewebservices.cpp b/desktop-widgets/subsurfacewebservices.cpp
index e6ef6668e..9dac4dab3 100644
--- a/desktop-widgets/subsurfacewebservices.cpp
+++ b/desktop-widgets/subsurfacewebservices.cpp
@@ -772,7 +772,7 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button)
struct dive_table table = { 0 };
struct trip_table trips = { 0 };
parse_file(QFile::encodeName(zipFile.fileName()), &table, &trips);
- Command::importDives(&table, &trips, false, false, true, QStringLiteral("divelogs.de"));
+ Command::importDives(&table, &trips, false, false, true, false, QStringLiteral("divelogs.de"));
/* store last entered user/pass in config */
QSettings s;
diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp
index 3a38fb90d..5e858f9cf 100644
--- a/mobile-widgets/qmlmanager.cpp
+++ b/mobile-widgets/qmlmanager.cpp
@@ -341,7 +341,7 @@ void QMLManager::mergeLocalRepo()
struct dive_table table = { 0 };
struct trip_table trips = { 0 };
parse_file(filename, &table, &trips);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
}
void QMLManager::copyAppLogToClipboard()
diff --git a/qt-models/diveimportedmodel.cpp b/qt-models/diveimportedmodel.cpp
index 566bda173..ff93e1792 100644
--- a/qt-models/diveimportedmodel.cpp
+++ b/qt-models/diveimportedmodel.cpp
@@ -158,7 +158,8 @@ void DiveImportedModel::recordDives()
delete_dive_from_table(diveTable, j);
}
- add_imported_dives(diveTable, tripTable, true, true, false);
+ // TODO: Might want to let the user select "add_to_new_trip"
+ add_imported_dives(diveTable, tripTable, true, true, false, false);
}
QHash<int, QByteArray> DiveImportedModel::roleNames() const {
diff --git a/tests/testmerge.cpp b/tests/testmerge.cpp
index 130c91b6d..dc5ab5a2d 100644
--- a/tests/testmerge.cpp
+++ b/tests/testmerge.cpp
@@ -24,9 +24,9 @@ void TestMerge::testMergeEmpty()
struct dive_table table = { 0 };
struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips), 0);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips), 0);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
QCOMPARE(save_dives("./testmerge47+48.ssrf"), 0);
QFile org(SUBSURFACE_TEST_DATA "/dives/test47+48.xml");
org.open(QFile::ReadOnly);
@@ -49,9 +49,9 @@ void TestMerge::testMergeBackwards()
struct dive_table table = { 0 };
struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips), 0);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips), 0);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
QCOMPARE(save_dives("./testmerge47+48.ssrf"), 0);
QFile org(SUBSURFACE_TEST_DATA "/dives/test47+48.xml");
org.open(QFile::ReadOnly);
diff --git a/tests/testrenumber.cpp b/tests/testrenumber.cpp
index 6d3ac7fa5..e7589fc0a 100644
--- a/tests/testrenumber.cpp
+++ b/tests/testrenumber.cpp
@@ -16,7 +16,7 @@ void TestRenumber::testMerge()
struct dive_table table = { 0 };
struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47b.xml", &table, &trip_table), 0);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
QCOMPARE(dive_table.nr, 1);
QCOMPARE(unsaved_changes(), 1);
mark_divelist_changed(false);
@@ -27,7 +27,7 @@ void TestRenumber::testMergeAndAppend()
struct dive_table table = { 0 };
struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47c.xml", &table, &trip_table), 0);
- add_imported_dives(&table, &trips, false, false, true);
+ add_imported_dives(&table, &trips, false, false, true, false);
QCOMPARE(dive_table.nr, 2);
QCOMPARE(unsaved_changes(), 1);
struct dive *d = get_dive(1);