Import: control process_imported_dives() by flags

process_imported_dives() takes four boolean parameters. Replace these
by flags. This makes the function calls much more descriptive. Morover,
it becomes easier to add or remove flags.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-01-13 22:53:57 +02:00 committed by Dirk Hohndel
parent ff9506b21b
commit 891fcbf520
14 changed files with 51 additions and 52 deletions

View file

@ -1570,9 +1570,7 @@ static bool merge_dive_tables(struct dive_table *dives_from, struct dive_table *
/* Merge the dives of the trip "from" and the dive_table "dives_from" into the trip "to" /* Merge the dives of the trip "from" and the dive_table "dives_from" into the trip "to"
* and dive_table "dives_to". If "prefer_imported" is true, dive data of "from" takes * and dive_table "dives_to". If "prefer_imported" is true, dive data of "from" takes
* precedence */ * precedence */
void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, int flags)
bool prefer_imported, bool downloaded, bool merge_all_trips,
bool add_to_new_trip)
{ {
int i, idx; int i, idx;
struct dive_table dives_to_add = { 0 }; struct dive_table dives_to_add = { 0 };
@ -1581,8 +1579,7 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
/* Process imported dives and generate lists of dives /* Process imported dives and generate lists of dives
* to-be-added and to-be-removed */ * to-be-added and to-be-removed */
process_imported_dives(import_table, import_trip_table, process_imported_dives(import_table, import_trip_table, flags,
prefer_imported, downloaded, merge_all_trips, add_to_new_trip,
&dives_to_add, &dives_to_remove, &trips_to_add); &dives_to_add, &dives_to_remove, &trips_to_add);
/* Add new dives to trip, so that trips don't get deleted /* Add new dives to trip, so that trips don't get deleted
@ -1672,16 +1669,19 @@ 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. * *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 * The lists are generated by merging dives if possible. This is
* performed trip-wise. If "prefer_imported" is true, data of the * performed trip-wise. Finer control on merging is provided by
* new dives are prioritized in such a case. If "merge_all_trips" is * the "flags" parameter:
* true, all overlapping trips will be merged, not only non-autogenerated * - If IMPORT_PREFER_IMPORTED is set, data of the new dives are
* trips. If "downloaded" is true, only the divecomputer of the first dive * prioritized on merging.
* will be considered, as it is assumed that all dives come from * - If IMPORT_MERGE_ALL_TRIPS is set, all overlapping trips will
* the same computer. If "add_to_new_trip" is true, dives that are not * be merged, not only non-autogenerated trips.
* assigned to a trip will be added to a newly generated trip. * - If IMPORT_IS_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.
* - If IMPORT_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, void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, int flags,
bool prefer_imported, bool downloaded, bool merge_all_trips, bool add_to_new_trip,
/* output parameters: */ /* output parameters: */
struct dive_table *dives_to_add, struct dive_table *dives_to_remove, struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
struct trip_table *trips_to_add) struct trip_table *trips_to_add)
@ -1721,7 +1721,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
/* check if we need a nickname for the divecomputer for newly downloaded dives; /* check if we need a nickname for the divecomputer for newly downloaded dives;
* since we know they all came from the same divecomputer we just check for the * since we know they all came from the same divecomputer we just check for the
* first one */ * first one */
if (downloaded) if (flags & IMPORT_IS_DOWNLOADED)
set_dc_nickname(import_table->dives[0]); set_dc_nickname(import_table->dives[0]);
else else
/* they aren't downloaded, so record / check all new ones */ /* they aren't downloaded, so record / check all new ones */
@ -1734,7 +1734,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
/* Autogroup tripless dives if desired by user. But don't autogroup /* Autogroup tripless dives if desired by user. But don't autogroup
* if tripless dives should be added to a new trip. */ * if tripless dives should be added to a new trip. */
if (!add_to_new_trip) if (!(flags & IMPORT_ADD_TO_NEW_TRIP))
autogroup_dives(import_table, import_trip_table); autogroup_dives(import_table, import_trip_table);
preexisting = dive_table.nr; /* Remember old size for renumbering */ preexisting = dive_table.nr; /* Remember old size for renumbering */
@ -1745,8 +1745,8 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
*/ */
for (i = 0; i < import_trip_table->nr; i++) { for (i = 0; i < import_trip_table->nr; i++) {
trip_import = import_trip_table->trips[i]; trip_import = import_trip_table->trips[i];
if (merge_all_trips || trip_import->autogen) { if ((flags & IMPORT_MERGE_ALL_TRIPS) || trip_import->autogen) {
if (try_to_merge_trip(trip_import, import_table, prefer_imported, dives_to_add, dives_to_remove, if (try_to_merge_trip(trip_import, import_table, flags & IMPORT_PREFER_IMPORTED, dives_to_add, dives_to_remove,
&sequence_changed, &start_renumbering_at)) &sequence_changed, &start_renumbering_at))
continue; continue;
} }
@ -1769,7 +1769,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
} }
import_trip_table->nr = 0; /* All trips were consumed */ import_trip_table->nr = 0; /* All trips were consumed */
if (add_to_new_trip && import_table->nr > 0) { if ((flags & IMPORT_ADD_TO_NEW_TRIP) && import_table->nr > 0) {
/* Create a new trip for unassigned dives, if desired. */ /* Create a new trip for unassigned dives, if desired. */
new_trip = create_trip_from_dive(import_table->dives[0]); new_trip = create_trip_from_dive(import_table->dives[0]);
insert_trip(new_trip, trips_to_add); insert_trip(new_trip, trips_to_add);
@ -1787,7 +1787,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
/* The remaining dives in import_table are those that don't belong to /* 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 * a trip and the caller does not want them to be associated to a
* new trip. Merge them into the global table. */ * new trip. Merge them into the global table. */
sequence_changed |= merge_dive_tables(import_table, NULL, &dive_table, prefer_imported, NULL, sequence_changed |= merge_dive_tables(import_table, NULL, &dive_table, flags & IMPORT_PREFER_IMPORTED, NULL,
dives_to_add, dives_to_remove, &start_renumbering_at); dives_to_add, dives_to_remove, &start_renumbering_at);
} }

View file

@ -18,10 +18,13 @@ extern int init_decompression(struct deco_state *ds, struct dive *dive);
/* divelist core logic functions */ /* divelist core logic functions */
extern void process_loaded_dives(); extern void process_loaded_dives();
extern void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, /* flags for process_imported_dives() */
bool prefer_imported, bool downloaded, bool merge_all_trips, bool add_to_new_trip); #define IMPORT_PREFER_IMPORTED (1 << 0)
extern void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, #define IMPORT_IS_DOWNLOADED (1 << 1)
bool prefer_imported, bool downloaded, bool merge_all_trips, bool add_to_new_trip, #define IMPORT_MERGE_ALL_TRIPS (1 << 2)
#define IMPORT_ADD_TO_NEW_TRIP (1 << 3)
extern void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, int flags);
extern void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, int flags,
struct dive_table *dives_to_add, struct dive_table *dives_to_remove, struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
struct trip_table *trips_to_add); struct trip_table *trips_to_add);
extern char *get_dive_gas_string(const struct dive *dive); extern char *get_dive_gas_string(const struct dive *dive);

View file

@ -11,11 +11,9 @@ void addDive(dive *d, bool autogroup, bool newNumber)
execute(new AddDive(d, autogroup, newNumber)); execute(new AddDive(d, autogroup, newNumber));
} }
void importDives(struct dive_table *dives, struct trip_table *trips, void importDives(struct dive_table *dives, struct trip_table *trips, int flags, const QString &source)
bool prefer_imported, bool downloaded, bool merge_all_trips,
bool add_to_new_trip, const QString &source)
{ {
execute(new ImportDives(dives, trips, prefer_imported, downloaded, merge_all_trips, add_to_new_trip, source)); execute(new ImportDives(dives, trips, flags, source));
} }
void deleteDive(const QVector<struct dive*> &divesToDelete) void deleteDive(const QVector<struct dive*> &divesToDelete)

View file

@ -21,9 +21,7 @@ void addDive(dive *d, bool autogroup, bool newNumber); // If d->dive_trip is nul
// distance are added to a trip. dive d is consumed (the structure is reset)! // distance are added to a trip. dive d is consumed (the structure is reset)!
// If newNumber is true, the dive is assigned a new number, depending on the // If newNumber is true, the dive is assigned a new number, depending on the
// insertion position. // insertion position.
void importDives(struct dive_table *dives, struct trip_table *trips, void importDives(struct dive_table *dives, struct trip_table *trips, int flags, const QString &source);
bool prefer_imported, bool downloaded, bool merge_all_trips,
bool add_to_new_trip, const QString &source);
void deleteDive(const QVector<struct dive*> &divesToDelete); void deleteDive(const QVector<struct dive*> &divesToDelete);
void shiftTime(const QVector<dive *> &changedDives, int amount); void shiftTime(const QVector<dive *> &changedDives, int amount);
void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber); void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber);

View file

@ -552,17 +552,14 @@ void AddDive::undoit()
MainWindow::instance()->refreshDisplay(false); MainWindow::instance()->refreshDisplay(false);
} }
ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, int flags, const QString &source)
bool prefer_imported, bool downloaded, bool merge_all_trips,
bool add_to_new_trip, const QString &source)
{ {
setText(tr("import %n dive(s) from %1", "", dives->nr).arg(source)); setText(tr("import %n dive(s) from %1", "", dives->nr).arg(source));
struct dive_table dives_to_add = { 0 }; struct dive_table dives_to_add = { 0 };
struct dive_table dives_to_remove = { 0 }; struct dive_table dives_to_remove = { 0 };
struct trip_table trips_to_add = { 0 }; struct trip_table trips_to_add = { 0 };
process_imported_dives(dives, trips, prefer_imported, downloaded, merge_all_trips, process_imported_dives(dives, trips, flags, &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 // Add trips to the divesToAdd.trips structure
divesToAdd.trips.reserve(trips_to_add.nr); divesToAdd.trips.reserve(trips_to_add.nr);

View file

@ -98,9 +98,7 @@ private:
class ImportDives : public DiveListBase { class ImportDives : public DiveListBase {
public: public:
// Note: dives and trips are consumed - after the call they will be empty. // Note: dives and trips are consumed - after the call they will be empty.
ImportDives(struct dive_table *dives, struct trip_table *trips, ImportDives(struct dive_table *dives, struct trip_table *trips, int flags, const QString &source);
bool prefer_imported, bool downloaded, bool merge_all_trips,
bool add_to_new_trip, const QString &source);
private: private:
void undoit() override; void undoit() override;
void redoit() override; void redoit() override;

View file

@ -1012,7 +1012,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
} }
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files"); QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
Command::importDives(&table, &trips, false, false, true, false, source); Command::importDives(&table, &trips, IMPORT_MERGE_ALL_TRIPS, source);
} }
TagDragDelegate::TagDragDelegate(QObject *parent) : QStyledItemDelegate(parent) TagDragDelegate::TagDragDelegate(QObject *parent) : QStyledItemDelegate(parent)

View file

@ -516,7 +516,12 @@ void DownloadFromDCWidget::on_ok_clicked()
if (table->nr > 0) { if (table->nr > 0) {
auto data = thread.data(); auto data = thread.data();
Command::importDives(table, nullptr, preferDownloaded(), true, false, ui.createNewTrip->isChecked(), data->devName()); int flags = IMPORT_IS_DOWNLOADED;
if (preferDownloaded())
flags |= IMPORT_IS_DOWNLOADED;
if (ui.createNewTrip->isChecked())
flags |= IMPORT_ADD_TO_NEW_TRIP;
Command::importDives(table, nullptr, flags, data->devName());
} }
if (ostcFirmwareCheck && currentState == DONE) if (ostcFirmwareCheck && currentState == DONE)

View file

@ -1716,7 +1716,7 @@ void MainWindow::importFiles(const QStringList fileNames)
parse_file(fileNamePtr.data(), &table, &trips); parse_file(fileNamePtr.data(), &table, &trips);
} }
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files"); QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
Command::importDives(&table, &trips, false, false, true, false, source); Command::importDives(&table, &trips, IMPORT_MERGE_ALL_TRIPS, source);
} }
void MainWindow::loadFiles(const QStringList fileNames) void MainWindow::loadFiles(const QStringList fileNames)

View file

@ -772,7 +772,7 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button)
struct dive_table table = { 0 }; struct dive_table table = { 0 };
struct trip_table trips = { 0 }; struct trip_table trips = { 0 };
parse_file(QFile::encodeName(zipFile.fileName()), &table, &trips); parse_file(QFile::encodeName(zipFile.fileName()), &table, &trips);
Command::importDives(&table, &trips, false, false, true, false, QStringLiteral("divelogs.de")); Command::importDives(&table, &trips, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));
/* store last entered user/pass in config */ /* store last entered user/pass in config */
QSettings s; QSettings s;

View file

@ -341,7 +341,7 @@ void QMLManager::mergeLocalRepo()
struct dive_table table = { 0 }; struct dive_table table = { 0 };
struct trip_table trips = { 0 }; struct trip_table trips = { 0 };
parse_file(filename, &table, &trips); parse_file(filename, &table, &trips);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
} }
void QMLManager::copyAppLogToClipboard() void QMLManager::copyAppLogToClipboard()

View file

@ -157,8 +157,8 @@ void DiveImportedModel::recordDives()
delete_dive_from_table(diveTable, j); delete_dive_from_table(diveTable, j);
} }
// TODO: Might want to let the user select "add_to_new_trip" // TODO: Might want to let the user select IMPORT_ADD_TO_NEW_TRIP
add_imported_dives(diveTable, nullptr, true, true, false, false); add_imported_dives(diveTable, nullptr, IMPORT_PREFER_IMPORTED | IMPORT_IS_DOWNLOADED);
} }
QHash<int, QByteArray> DiveImportedModel::roleNames() const { QHash<int, QByteArray> DiveImportedModel::roleNames() const {

View file

@ -24,9 +24,9 @@ void TestMerge::testMergeEmpty()
struct dive_table table = { 0 }; struct dive_table table = { 0 };
struct trip_table trips = { 0 }; struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips), 0);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips), 0);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(save_dives("./testmerge47+48.ssrf"), 0); QCOMPARE(save_dives("./testmerge47+48.ssrf"), 0);
QFile org(SUBSURFACE_TEST_DATA "/dives/test47+48.xml"); QFile org(SUBSURFACE_TEST_DATA "/dives/test47+48.xml");
org.open(QFile::ReadOnly); org.open(QFile::ReadOnly);
@ -49,9 +49,9 @@ void TestMerge::testMergeBackwards()
struct dive_table table = { 0 }; struct dive_table table = { 0 };
struct trip_table trips = { 0 }; struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips), 0);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips), 0);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(save_dives("./testmerge47+48.ssrf"), 0); QCOMPARE(save_dives("./testmerge47+48.ssrf"), 0);
QFile org(SUBSURFACE_TEST_DATA "/dives/test47+48.xml"); QFile org(SUBSURFACE_TEST_DATA "/dives/test47+48.xml");
org.open(QFile::ReadOnly); org.open(QFile::ReadOnly);

View file

@ -16,7 +16,7 @@ void TestRenumber::testMerge()
struct dive_table table = { 0 }; struct dive_table table = { 0 };
struct trip_table trips = { 0 }; struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47b.xml", &table, &trip_table), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47b.xml", &table, &trip_table), 0);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(dive_table.nr, 1); QCOMPARE(dive_table.nr, 1);
QCOMPARE(unsaved_changes(), 1); QCOMPARE(unsaved_changes(), 1);
mark_divelist_changed(false); mark_divelist_changed(false);
@ -27,7 +27,7 @@ void TestRenumber::testMergeAndAppend()
struct dive_table table = { 0 }; struct dive_table table = { 0 };
struct trip_table trips = { 0 }; struct trip_table trips = { 0 };
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47c.xml", &table, &trip_table), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47c.xml", &table, &trip_table), 0);
add_imported_dives(&table, &trips, false, false, true, false); add_imported_dives(&table, &trips, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(dive_table.nr, 2); QCOMPARE(dive_table.nr, 2);
QCOMPARE(unsaved_changes(), 1); QCOMPARE(unsaved_changes(), 1);
struct dive *d = get_dive(1); struct dive *d = get_dive(1);