aboutsummaryrefslogtreecommitdiffstats
path: root/core/divelist.c
AgeCommit message (Collapse)Author
2019-10-28Cleanup: avoid dereferencing NULLGravatar Dirk Hohndel
We should call this function with two well defined dive_or_trip structures which means that exactly one of the two values is set in each argument. But in order to not have bugs elsewhere leed to crashes here, be more tolerant of malformed argumnts. Fixes CID 350100 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-10-28Cleanup: prevent NULL dereferenceGravatar Dirk Hohndel
This should never happen based on the logic in the callers, but just to be on the safe side. Should fix CID 350128 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-10-02Core: implement move functions for dive and dive_site tableGravatar Berthold Stoeger
To allow efficient moving of downloaded dives from the download thread to the model, implement a general move function that moves table data. Instantiate that function for the dive and dive_site tables. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-08-28Fix new CNS calculation - remove error with factor of 10Gravatar Stefan Fuchs
Fix a typo in the new CNS calculation which introduced a error of factor 10. Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
2019-08-20Replace table interpolation by two line fit for CNSGravatar Robert C. Helling
We used a table lookup for CNS equivalent times. Turns out the log of this table falls pretty much on a straight line for po2 <= 1.5bar. We now fit this tabel two two lines, one for <= 1.5 bar and one above. This four parameter fit has half the sum of errors squared than the five parameter fit using a fourth order polynomial. Fitting the log has the advantage that this never crosses 0, which would have the bad effect of resulting in negative CNS values as we divide by the table value. We don't adopt a maximum pO2 cut-off for the CNS calculation but rather live with the large values that the interpolation formula produces when extrapolating. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2019-08-04Cleanup: use clear_git_id() instead of setting saved_git_idGravatar Berthold Stoeger
For better encapsulation, use clear_git_id() in clear_dive_file_data() instead of setting saved_git_id directly. Thus, memory management of the saved_git_id value is encapsulated and can be modified more easily. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-07-18Core: dynamically resize weight tableGravatar Berthold Stoeger
Replace the fixed-size weightsystem table by a dynamically relocated table. Reuse the table-macros used in other parts of the code. The table stores weightsystem entries, not pointers to weightsystems. Thus, ownership of the description string is taken when adding a weightsystem. An extra function adds a cloned weightsystem at the end of the table. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-07-18Cleanup: move deco function declarations to deco.hGravatar Berthold Stoeger
Another tiny step in making dive.h smaller: move function declarations to deco.h if these functions are defined in deco.c and don't directly concern dives. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-28Import: use TRIP_THRESHOLD when checking for trip-overlapGravatar Berthold Stoeger
When checking for trip-overlap on import, only really overlapping trips have been considered, i.e. when dives had overlapping times. Instead use the TRIP_THRESHOLD so that on download dives are added to the same trip if in a two-days time frame. Reported-by: Miika Turkia <miika.turkia@gmail.com> Reported-by: Dirk Hohndel <dirk@hohndel.org> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: use add_to_dive_table() in record_dive_to_table()Gravatar Berthold Stoeger
This was reimplementing functionality that was already there. Simply call the already existing function. Thus, we don't have to export the grow_dive_table function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: generate clear_*_table() functions by macroGravatar Berthold Stoeger
In analogy to the other table functions, generate these by a macro as well. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: localize enum and table in divelist.cGravatar Berthold Stoeger
The cns_table was only used in divelist.c. Make it of static linkage accordingly. The cns_table_headers enum is likewise only used in divelist.c. Therefore move it from the header to the .c file. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: move trip-related functions into own translation unitGravatar Berthold Stoeger
These functions were spread out over dive.c and divelist.c. Move them into their own file to make all this a bit less monolithic. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19Cleanup: move TRIP_THRESHOLD into divelist.cGravatar Berthold Stoeger
This macro was not used elsewhere. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-05-17Core: consider dive computers when sorting divesGravatar Berthold Stoeger
When splitting out dive computers, the dives were sorted in an arbitrary way (according to an internal id), since all data are identical. Therefore, consider the dive-computer model names when sorting dives. Equal dives are now sorted alphabetically by model. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-05-15Cleanup: small coding style fixesGravatar Dirk Hohndel
And addressing a cut and paste error in a comment. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-05-12Core: consider dive-number on sortingGravatar Berthold Stoeger
A user reports a problem when dives have the same time but different numbers. The dives appear sorted randomly (effectively they are sorted by an internal unique-id). Try to sort by number for dives at the same date in this case. Fixes #2086 Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-05-12Cleanup: remove UTF8 macrosGravatar Berthold Stoeger
At some places we use UTF8 string literals. Therefore, we effectively only support UTF8 build systems. We might just as well remove all the other UTF_* macros and use direct string literals. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-05-06Add "Import dive sites" menu to mainwindowGravatar Doug Junkins
Adds "Import->Import dive sites" menu to mainwindow.cpp and adds the on_actionImportDiveSites_triggered() method to prompt for the filename to import from. The files are parsed and then any dive and trip data is cleared before opening a dialog box to select which sites are to be imported. Signed-off-by: Doug Junkins <junkins@foghead.com>
2019-04-15Core: turn add_single_dive() to append_dive()Gravatar Berthold Stoeger
The only external caller of add_single_dive() used it to append a dive to the global dive list. Rename the function accordingly and remove the index parameter. The internal caller can use the local insert_dive() function, which doesn't consider selection. That shouldn't be a problem, as the caller is doing import. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-15Cleanup: free dive table in trips and dive sitesGravatar Berthold Stoeger
Trips and dive sites were changed to use dive tables instead of linked lists. But the memory used for the tables wasn't freed. Do this. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Undo: use QUndoStack::isClean() to determine unsaved changesGravatar Berthold Stoeger
Properly implement the unsaved-changes flag(s). Since we currently have two kinds of changes, there are two flags: 1) dive_list_changed in divelist.c marks non-undoable changes. This flag is only cleared on save or load. 2) QUndoStack::isClean() is used to determine the state of undoable changes. Every time the user returns to the state where they saved, this flag is cleared. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Cleanup: move table-macros into own headerGravatar Berthold Stoeger
dive- and trip-table functions are generated in dive.c by macros. Move this macros to a new "core/table.h" header file. Thus, these functions can be used for other tables (e.g. dive site) and the trip function can be moved to a separate translation unit (divelist.c being quite large already). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Cleanup: autogenerate remove_dive() and remove_trip()Gravatar Berthold Stoeger
The other dive- and trip-table functions were already autogenerated. Let's do the same for these two. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Cleanup: rename unregister_trip() to remove_trip()Gravatar Berthold Stoeger
For consistency with remove_dive(). Moreover, swap parameter order in remove_dive() so that both functions use the same parameter order. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Dive site: add dive site ref-countingGravatar Berthold Stoeger
Instead of setting dive->dive_site directly, call the add_dive_to_dive_site() and unregister_dive_from_dive_site() functions. In the parser this turned out to be a bit tricky. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Dive sites: prepare for dive site ref-countingGravatar Berthold Stoeger
Add a dive site table to each dive site to keep track of dives that have been added to a dive site. Add two functions to add dives to / remove dives from dive sites. Since dive sites now contain a dive table, the order of includes had to be changed: "divesite.h" now includes "dive.h" and not vice-versa. This caused some include churn. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Undo: make undo-system dive site-awareGravatar Berthold Stoeger
As opposed to dive trips, dive sites were always directly added to the global table, even on import. Instead, parse the divesites into a distinct table and merge them on import. Currently, this does not do any merging of dive sites, i.e. dive sites are considered as either equal or different. Nevertheless, merging of data should be rather easy to implement and simply follow the code of the dive merging. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Parser: parse into custom dive site tableGravatar Berthold Stoeger
To extend the undo system to dive sites, the importers and downloaders must not parse directly into the global dive site table. Instead, pass a dive_site_table argument to parse into. For now, always pass the global dive_site_table so that this commit should not cause any functional change. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-04-12Dive site: add dive site table parameter to dive site functionsGravatar Berthold Stoeger
To enable undo of dive site functions, it is crucial to work with different dive site tables. Therefore add a dive site table parameter to dive site functions. For now, always pass the global dive site table. Thus, this commit shouldn't alter any functionality. After this change, a simple search for dive_site_table reveals all places where the global dive site table is accessed. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-03-17Core: remove variable name conflictGravatar Dirk Hohndel
Having a parameter with the same name as a global variable is potentially confusing. Found via LGTM.com Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-03-17Core: remove function list commentGravatar Dirk Hohndel
We don't do this anywhere else. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-03-05Import: fix infinite loopGravatar Berthold Stoeger
Owing to a variable reuse in a nested loop, importing dive logs with new trips could lead to an infinite loop. Use a fresh index "j". Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-02-28Cleanup: move get_divesite_idx() to divesite.cGravatar Berthold Stoeger
This function was defined in divelist.c, whereas it's better located in divesite.c. Move it. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-19Import: control process_imported_dives() by flagsGravatar Berthold Stoeger
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>
2019-01-19Import: don't add to new trip while downloadingGravatar Berthold Stoeger
Since process_imported_dives() can add dives to a newly generated trip, this need not be done in the downloading code. This makes data flow distinctly simpler, as no trip table and no add-new-trip flag has to be passed down to the libdivecomputer glue code. Moreover, since now the trip creation is done at the import step rather than the download step, the latest status of the "add to new trip" checkbox will be considered. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-19Import: add add_to_new_trip flag to process_imported_dives()Gravatar Berthold Stoeger
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>
2019-01-19Dive import: fix logic of process_imported_dives()Gravatar Berthold Stoeger
The logic in process_imported_dives() was faulty: Dives are merged trip-wise in a loop. But if only autogenerated trips were supposed to be merged, the trip would not be added. Change the logic to always add the trip if it is not merged. To make the loop easier to read, factor out the merge-trip-into-existing-trips logic into a separate function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Import: split process_imported_dives() functionGravatar Berthold Stoeger
Split the process_imported_dives() function in two: 1) process_imported_dives() processes the dives and generates a list of dives and trips to be added and removed. 2) add_imported_dives() calls process_imported_dives() and does the actual removal / addition of dives and trips. The goal is to split preparation and actual work, to make dive import undo-able. The code adds extra checks to never merge into the same dive twice, as this would lead to a double-free() bug. This should in principle never happen, as dives that compare equal according to is_same_dive() are merged in the imported-dives list, but perhaps in some pathologival corner-cases is_same_dive() turns out to be non-transitive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: don't copy dive-id on merge diveGravatar Berthold Stoeger
The installment of the divelist-undo system has made it unnecessary to adopt the uniq-id of the merged-into dive. On the contrary, we want to avoid two dives with the same dive-id in the divelist at all costs, since get_divenr() still uses the id and thus may fetch the wrong dive. Therefore, don't copy the dive-id on merge. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Import: add merge_all_trips parameter to process_imported_dives()Gravatar Berthold Stoeger
When importing log-files we generally want to merge trips. But when downloading and the user chose "generate new trip", that new trip should not be merged into existing trips. Therefore, add a "merge_all_trips" parameter to process_imported_dives(). If false only autogenerated trips [via autogroup] will be merged. In the future we might want to let the user choose if trips should be merged when importing log-files. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Import: merge dives trip-wiseGravatar Berthold Stoeger
The old way of merging log-files was not well defined: Trips were recognized as the same if and only if the first dives started at the same instant. Later dives did not matter. Change this to merge dives if they are overlapping. Moreover, on parsing and download generate trips in a separate trip-table. This will be fundamental for undo of dive-import: Firstly, we don't want to mix trips of imported and not-yet imported dives. Secondly, by merging trip-wise, we can autogroup the dives in the import-data to trips and merge these at once. This will simplify the code to decide to which trip dives should be autogrouped. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: add trip_table parameter to trip-functionsGravatar Berthold Stoeger
Currently trips are added to the global trip table. If we want to make dive-import undoable, we should be able to parse trips of a log-file into a distinct table. Therefore, add a trip_table parameter to - insert_trip() - create_and_hookup_trip_from_dive() - autogroup_dives() - unregister_trip() - remove_dive_from_trip() Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: keep trips in table(s)Gravatar Berthold Stoeger
Currently, all trips are kept in a linked list. Replace the list by a table in analogy to dive_table. Use this to keep the trip_table sorted as suggested by dump_trip_list(). When inserting a trip into the table do that after adding the dives, to avoid warnings coming out of dump_trip_list(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: fix dump_trip_list() functionGravatar Berthold Stoeger
In 64e6e435f82801f4f440ef5b1caf58a91a7c9929 the trip->when field was replaced by a function. This forgot to adapt dump_trip_list(), which is only compiled if DEBUG_TRIP is defined. Fix the function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: macroize dive_table functionsGravatar Berthold Stoeger
Generate dive table functions by macros so that they can be reused for trip tables. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: Rename functions to more generic namesGravatar Berthold Stoeger
Rename - dive_get_insertion_index() -> dive_table_get_insertion_index() - unregister_dive_from_table() -> remove_from_dive_table() - get_idx_in_table() -> get_idx_in_dive_table() - sort_table() -> sort_dive_table() This will make it more straight-forward to generate these functions from macros. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Cleanup: Make add_dive_to_table local to divelist.cGravatar Berthold Stoeger
This function was not used outside of divelist.c, therefore make it local. Moreover rename it to add_to_divetable so that the name is generic and can be generated by a macro. Moreover, remove the special case idx = -1, which would determine the insertion index. Instead let the single caller who used this feature do this. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Dive list: add table parameter to autogroup_dives()Gravatar Berthold Stoeger
Currently autogroup_dives() groups all dives in the global dive list. Add a table parameter so that dives in any table can be grouped. Thus it will be possible to pre-group dives on import, which will be used for undo of import. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-09Core: move autogroup() into divelist.cGravatar Berthold Stoeger
After loading or importing, the caller usually called autogroup() to autogroup dives if so wished by the user. This has already led to bugs, when autogroup() was forgotten. Instead, call autogroup() directly in the process_loaded_dives() and process_imported_dives() functions. Not only does this prevent forgetting the call - it also means that autogrouping can be changed without changing every caller. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>