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

@ -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();
}