aboutsummaryrefslogtreecommitdiffstats
path: root/core/divelist.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-03-03 15:12:22 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 18:19:07 +0300
commit82af1b2377cec03a989f86b8009d4ac226c6541e (patch)
treea875f194f7174c8673b2f7bd5cc43aa8ca3c51c6 /core/divelist.c
parent37146c5742503becf75468fb07aab56011cb9101 (diff)
downloadsubsurface-82af1b2377cec03a989f86b8009d4ac226c6541e.tar.gz
Undo: make undo-system dive site-aware
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>
Diffstat (limited to 'core/divelist.c')
-rw-r--r--core/divelist.c65
1 files changed, 53 insertions, 12 deletions
diff --git a/core/divelist.c b/core/divelist.c
index 8e1ba1628..09f08acd0 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -1518,17 +1518,18 @@ 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"
* 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, int flags)
+void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, struct dive_site_table *import_sites_table, int flags)
{
int i, idx;
struct dive_table dives_to_add = { 0 };
struct dive_table dives_to_remove = { 0 };
struct trip_table trips_to_add = { 0 };
+ struct dive_site_table dive_sites_to_add = { 0 };
/* Process imported dives and generate lists of dives
* to-be-added and to-be-removed */
- process_imported_dives(import_table, import_trip_table, flags,
- &dives_to_add, &dives_to_remove, &trips_to_add);
+ process_imported_dives(import_table, import_trip_table, import_sites_table, flags,
+ &dives_to_add, &dives_to_remove, &trips_to_add, &dive_sites_to_add);
/* Add new dives to trip, so that trips don't get deleted
* on deletion of old dives */
@@ -1560,6 +1561,11 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
insert_trip(trips_to_add.trips[i], &trip_table);
trips_to_add.nr = 0;
+ /* Add new dive sites */
+ for (i = 0; i < dive_sites_to_add.nr; i++)
+ register_dive_site(dive_sites_to_add.dive_sites[i]);
+ dive_sites_to_add.nr = 0;
+
/* We might have deleted the old selected dive.
* Choose the newest dive as selected (if any) */
current_dive = dive_table.nr > 0 ? dive_table.dives[dive_table.nr - 1] : NULL;
@@ -1600,21 +1606,23 @@ bool try_to_merge_trip(struct dive_trip *trip_import, struct dive_table *import_
}
/* Process imported dives: take a table of dives to be imported and
- * generate three lists:
+ * generate four lists:
* 1) Dives to be added
* 2) Dives to be removed
* 3) Trips to be added
+ * 4) Dive sites to be added
* The dives to be added are owning (i.e. the caller is responsible
* for freeing them).
- * The dives and trips in "import_table" and "import_trip_table" are
- * consumed. On return, both tables have size 0.
- * "import_trip_table" may be NULL if all dives are not associated
+ * The dives, trips and sites in "import_table", "import_trip_table"
+ * and "import_sites_table" are consumed. On return, the tables have
+ * size 0. "import_trip_table" may be NULL if all dives are not associated
* with a trip.
- * The output parameters should be empty - if not, their content
+ * The output tables should be empty - if not, their content
* will be cleared!
*
- * Note: The new dives will have their divetrip-field set, but will
- * *not* be part of the trip. The caller has to add them to the trip.
+ * Note: The new dives will have their divetrip- and divesites-fields
+ * set, but will *not* be part of the trip and site. The caller has to
+ * add them to the trip and site.
*
* The lists are generated by merging dives if possible. This is
* performed trip-wise. Finer control on merging is provided by
@@ -1629,10 +1637,11 @@ bool try_to_merge_trip(struct dive_trip *trip_import, struct dive_table *import_
* - 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, int flags,
+void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
+ struct dive_site_table *import_sites_table, int flags,
/* output parameters: */
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
- struct trip_table *trips_to_add)
+ struct trip_table *trips_to_add, struct dive_site_table *sites_to_add)
{
int i, j, nr, start_renumbering_at = 0;
struct dive_trip *trip_import, *new_trip;
@@ -1651,6 +1660,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
clear_table(dives_to_add);
clear_table(dives_to_remove);
clear_trip_table(trips_to_add);
+ clear_dive_site_table(sites_to_add);
/* Check if any of the new dives has a number. This will be
* important later to decide if we want to renumber the added
@@ -1687,6 +1697,37 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
preexisting = dive_table.nr; /* Remember old size for renumbering */
+ /* If dive sites already exist, use the existing versions. */
+ for (i = 0; i < import_sites_table->nr; i++) {
+ struct dive_site *new_ds = import_sites_table->dive_sites[i];
+ struct dive_site *old_ds = get_same_dive_site(new_ds);
+
+ /* Check if it dive site is actually used by new dives. */
+ for (j = 0; j < import_table->nr; j++) {
+ if (import_table->dives[j]->dive_site == new_ds)
+ break;
+ }
+
+ if (j == import_table->nr) {
+ /* Dive site not even used - free it and go to next. */
+ free_dive_site(new_ds);
+ continue;
+ }
+
+ if (!old_ds) {
+ /* Dive site doesn't exist. Add it to list of dive sites to be added. */
+ add_dive_site_to_table(new_ds, sites_to_add);
+ continue;
+ }
+ /* Dive site already exists - use the old and free the new. */
+ for (j = 0; j < import_table->nr; j++) {
+ if (import_table->dives[j]->dive_site == new_ds)
+ import_table->dives[j]->dive_site = old_ds;
+ }
+ free_dive_site(new_ds);
+ }
+ import_sites_table->nr = 0; /* All dive sites were consumed */
+
/* Merge overlapping trips. Since both trip tables are sorted, we
* could be smarter here, but realistically not a whole lot of trips
* will be imported so do a simple n*m loop until someone complains.