core: introduce divelog structure

The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.

Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).

The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.

To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.

The whole commit is large, but was mostly an automatic
conversion.

One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-08 21:31:08 +01:00 committed by bstoeger
parent eebb47ec22
commit 9c253ee6c5
81 changed files with 661 additions and 698 deletions

View file

@ -14,6 +14,7 @@
#include "core/filterpreset.h"
#include "core/qthelper.h"
#include "core/divesite.h"
#include "core/divelog.h"
#include "core/device.h"
#include "core/trip.h"
#include "core/import-csv.h"
@ -879,19 +880,15 @@ void DiveLogImportDialog::parseTxtHeader(QString fileName, xml_params &params)
void DiveLogImportDialog::on_buttonBox_accepted()
{
struct dive_table table = empty_dive_table;
struct trip_table trips = empty_trip_table;
struct dive_site_table sites = empty_dive_site_table;
struct device_table devices;
struct filter_preset_table filter_presets;
struct divelog log;
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]), &table, &trips, &sites, &devices, &filter_presets);
parse_seabear_log(qPrintable(fileNames[i]), &log);
} else if (ui->knownImports->currentText() == "Poseidon MkVI") {
QPair<QString, QString> pair = poseidonFileNames(fileNames[i]);
parse_txt_file(qPrintable(pair.second), qPrintable(pair.first), &table, &trips, &sites, &devices);
parse_txt_file(qPrintable(pair.second), qPrintable(pair.first), &log);
} else {
xml_params params;
@ -908,7 +905,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
setup_csv_params(r, params);
parse_csv_file(qPrintable(fileNames[i]), &params,
specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv",
&table, &trips, &sites, &devices, &filter_presets);
&log);
}
}
} else {
@ -944,7 +941,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
xml_params_add_int(&params, "visibilityField", r.indexOf(tr("Visibility")));
xml_params_add_int(&params, "ratingField", r.indexOf(tr("Rating")));
parse_manual_file(qPrintable(fileNames[i]), &params, &table, &trips, &sites, &devices, &filter_presets);
parse_manual_file(qPrintable(fileNames[i]), &params, &log);
} else {
xml_params params;
@ -962,13 +959,13 @@ void DiveLogImportDialog::on_buttonBox_accepted()
setup_csv_params(r, params);
parse_csv_file(qPrintable(fileNames[i]), &params,
specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv",
&table, &trips, &sites, &devices, &filter_presets);
&log);
}
}
}
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
Command::importDives(&table, &trips, &sites, &devices, nullptr, IMPORT_MERGE_ALL_TRIPS, source);
Command::importDives(log.dives, log.trips, log.sites, log.devices, nullptr, IMPORT_MERGE_ALL_TRIPS, source);
}
TagDragDelegate::TagDragDelegate(QObject *parent) : QStyledItemDelegate(parent)

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "divesitelistview.h"
#include "core/subsurface-qt/divelistnotifier.h"
#include "core/divelog.h"
#include "core/divesite.h"
#include "core/divefilter.h"
#include "qt-models/divelocationmodel.h"
@ -96,7 +97,7 @@ void DiveSiteListView::diveSiteAdded(struct dive_site *, int idx)
void DiveSiteListView::diveSiteChanged(struct dive_site *ds, int field)
{
int idx = get_divesite_idx(ds, &dive_site_table);
int idx = get_divesite_idx(ds, divelog.sites);
if (idx < 0)
return;
QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, field);

View file

@ -3,6 +3,7 @@
#include "commands/command.h"
#include "core/qthelper.h"
#include "core/divelist.h"
#include "core/divelog.h"
#include "core/settings/qPrefDiveComputer.h"
#include "core/subsurface-float.h"
#include "core/subsurface-string.h"
@ -423,7 +424,7 @@ void DownloadFromDCWidget::on_downloadCancelRetryButton_clicked()
qPrefDiveComputer::set_device(data->devName());
// before we start, remember where the dive_table ended
previousLast = dive_table.nr;
previousLast = divelog.dives->nr;
diveImportedModel->startDownload();
// FIXME: We should get the _actual_ device info instead of whatever

View file

@ -8,6 +8,7 @@
#include "desktop-widgets/mapwidget.h"
#include "core/color.h"
#include "core/divefilter.h"
#include "core/divelog.h"
#include "core/divesite.h"
#include "core/divesitehelpers.h"
#include "desktop-widgets/modeldelegates.h"
@ -367,8 +368,8 @@ bool DiveLocationFilterProxyModel::lessThan(const QModelIndex &source_left, cons
// If there is a current location, sort by that - otherwise use the provided column
if (has_location(&currentLocation)) {
// The dive sites are -2 because of the first two items.
struct dive_site *ds1 = get_dive_site(source_left.row() - 2, &dive_site_table);
struct dive_site *ds2 = get_dive_site(source_right.row() - 2, &dive_site_table);
struct dive_site *ds1 = get_dive_site(source_left.row() - 2, divelog.sites);
struct dive_site *ds2 = get_dive_site(source_right.row() - 2, divelog.sites);
return get_distance(&ds1->location, &currentLocation) < get_distance(&ds2->location, &currentLocation);
}
return source_left.data().toString().compare(source_right.data().toString(), Qt::CaseInsensitive) < 0;
@ -407,7 +408,7 @@ QVariant DiveLocationModel::data(const QModelIndex &index, int role) const
}
// The dive sites are -2 because of the first two items.
struct dive_site *ds = get_dive_site(index.row() - 2, &dive_site_table);
struct dive_site *ds = get_dive_site(index.row() - 2, divelog.sites);
return LocationInformationModel::getDiveSiteData(ds, index.column(), role);
}
@ -418,7 +419,7 @@ int DiveLocationModel::columnCount(const QModelIndex&) const
int DiveLocationModel::rowCount(const QModelIndex&) const
{
return dive_site_table.nr + 2;
return divelog.sites->nr + 2;
}
Qt::ItemFlags DiveLocationModel::flags(const QModelIndex &index) const
@ -541,11 +542,10 @@ static struct dive_site *get_dive_site_name_start_which_str(const QString &str)
{
struct dive_site *ds;
int i;
for_each_dive_site (i, ds, &dive_site_table) {
for_each_dive_site (i, ds, divelog.sites) {
QString dsName(ds->name);
if (dsName.toLower().startsWith(str.toLower())) {
if (dsName.toLower().startsWith(str.toLower()))
return ds;
}
}
return NULL;
}

View file

@ -19,6 +19,7 @@
#include "core/color.h"
#include "core/device.h"
#include "core/divelog.h"
#include "core/divesitehelpers.h"
#include "core/errorhelper.h"
#include "core/file.h"
@ -413,7 +414,7 @@ void MainWindow::on_actionCloudstorageopen_triggered()
showProgressBar();
QByteArray fileNamePtr = QFile::encodeName(filename);
if (!parse_file(fileNamePtr.data(), &dive_table, &trip_table, &dive_site_table, &device_table, &filter_preset_table))
if (!parse_file(fileNamePtr.data(), &divelog))
setCurrentFile(fileNamePtr.data());
process_loaded_dives();
hideProgressBar();
@ -423,7 +424,7 @@ void MainWindow::on_actionCloudstorageopen_triggered()
// Return whether saving to cloud is OK. If it isn't, show an error return false.
static bool saveToCloudOK()
{
if (!dive_table.nr) {
if (divelog.dives->nr) {
report_error(qPrintable(gettextFromC::tr("Don't save an empty log to the cloud")));
return false;
}
@ -1276,18 +1277,14 @@ void MainWindow::importFiles(const QStringList fileNames)
return;
QByteArray fileNamePtr;
struct dive_table table = empty_dive_table;
struct trip_table trips = empty_trip_table;
struct dive_site_table sites = empty_dive_site_table;
struct device_table devices;
struct filter_preset_table filter_presets;
struct divelog log;
for (int i = 0; i < fileNames.size(); ++i) {
fileNamePtr = QFile::encodeName(fileNames.at(i));
parse_file(fileNamePtr.data(), &table, &trips, &sites, &devices, &filter_presets);
parse_file(fileNamePtr.data(), &log);
}
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
Command::importDives(&table, &trips, &sites, &devices, &filter_presets, IMPORT_MERGE_ALL_TRIPS, source);
Command::importDives(log.dives, log.trips, log.sites, log.devices, log.filter_presets, IMPORT_MERGE_ALL_TRIPS, source);
}
void MainWindow::loadFiles(const QStringList fileNames)
@ -1301,7 +1298,7 @@ void MainWindow::loadFiles(const QStringList fileNames)
showProgressBar();
for (int i = 0; i < fileNames.size(); ++i) {
fileNamePtr = QFile::encodeName(fileNames.at(i));
if (!parse_file(fileNamePtr.data(), &dive_table, &trip_table, &dive_site_table, &device_table, &filter_preset_table)) {
if (!parse_file(fileNamePtr.data(), &divelog)) {
setCurrentFile(fileNamePtr.data());
addRecentFile(fileNamePtr, false);
}
@ -1372,28 +1369,19 @@ void MainWindow::on_actionImportDiveSites_triggered()
return;
updateLastUsedDir(QFileInfo(fileNames[0]).dir().path());
struct dive_table table = empty_dive_table;
struct trip_table trips = empty_trip_table;
struct dive_site_table sites = empty_dive_site_table;
struct device_table devices;
struct filter_preset_table filter_presets;
struct divelog log;
for (const QString &s: fileNames) {
QByteArray fileNamePtr = QFile::encodeName(s);
parse_file(fileNamePtr.data(), &table, &trips, &sites, &devices, &filter_presets);
parse_file(fileNamePtr.data(), &log);
}
// The imported dive sites still have pointers to imported dives - remove them
for (int i = 0; i < sites.nr; ++i)
sites.dive_sites[i]->dives.nr = 0;
// Now we can clear the imported dives and trips.
clear_dive_table(&table);
clear_trip_table(&trips);
for (int i = 0; i < log.sites->nr; ++i)
log.sites->dive_sites[i]->dives.nr = 0;
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
// sites table will be cleared by DivesiteImportDialog constructor
DivesiteImportDialog divesiteImport(sites, source, this);
DivesiteImportDialog divesiteImport(*log.sites, source, this);
divesiteImport.exec();
}

View file

@ -7,8 +7,8 @@
#include "desktop-widgets/mainwindow.h"
#include "commands/command.h"
#include "core/device.h"
#include "core/divesite.h"
#include "core/trip.h"
#include "core/divelist.h" // For IMPORT_MERGE_ALL_TRIPS
#include "core/divelog.h"
#include "core/errorhelper.h"
#include "core/file.h"
#include "desktop-widgets/mapwidget.h"
@ -463,13 +463,9 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button)
break;
}
/* parse file and import dives */
struct dive_table table = empty_dive_table;
struct trip_table trips = empty_trip_table;
struct dive_site_table sites = empty_dive_site_table;
struct device_table devices;
struct filter_preset_table filter_presets;
parse_file(QFile::encodeName(zipFile.fileName()), &table, &trips, &sites, &devices, &filter_presets);
Command::importDives(&table, &trips, &sites, &devices, nullptr, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));
struct divelog log;
parse_file(QFile::encodeName(zipFile.fileName()), &log);
Command::importDives(log.dives, log.trips, log.sites, log.devices, nullptr, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));
/* store last entered user/pass in config */
qPrefCloudStorage::set_divelogde_user(ui.userID->text());

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "tripselectiondialog.h"
#include "core/divelog.h"
#include "core/string-format.h"
#include "core/trip.h"
#include <QShortcut>
@ -17,9 +18,9 @@ TripSelectionDialog::TripSelectionDialog(QWidget *parent) : QDialog(parent)
// We could use a model, but it seems barely worth the hassle.
QStringList list;
list.reserve(trip_table.nr);
for (int i = 0; i < trip_table.nr; ++i)
list.push_back(formatTripTitleWithDives(trip_table.trips[i]));
list.reserve(divelog.trips->nr);
for (int i = 0; i < divelog.trips->nr; ++i)
list.push_back(formatTripTitleWithDives(divelog.trips->trips[i]));
ui.trips->addItems(list);
}
@ -36,9 +37,9 @@ dive_trip *TripSelectionDialog::selectedTrip() const
if (rows.size() != 1)
return nullptr;
int idx = rows[0].row();
if (idx < 0 || idx >= trip_table.nr)
if (idx < 0 || idx >= divelog.trips->nr)
return nullptr;
return trip_table.trips[idx];
return divelog.trips->trips[idx];
}
dive_trip *TripSelectionDialog::getTrip()