diff options
-rw-r--r-- | dive.c | 4 | ||||
-rw-r--r-- | dive.h | 1 | ||||
-rw-r--r-- | divelist.c | 54 | ||||
-rw-r--r-- | save-xml.c | 70 |
4 files changed, 86 insertions, 43 deletions
@@ -1209,6 +1209,10 @@ static int likely_same_dive(struct dive *a, struct dive *b) { int fuzz, match; + /* Don't try to merge dives in different trips */ + if (a->divetrip && b->divetrip && a->divetrip != b->divetrip) + return 0; + /* * Do some basic sanity testing of the values we * have filled in during 'fixup_dive()' @@ -287,6 +287,7 @@ typedef struct dive_trip { char *notes; struct dive *dives; int nrdives; + int index; unsigned expanded:1, selected:1, autogen:1; struct dive_trip *next; } dive_trip_t; diff --git a/divelist.c b/divelist.c index 2e2599abc..316c4d547 100644 --- a/divelist.c +++ b/divelist.c @@ -1199,12 +1199,19 @@ static void autogroup_dives(void) #endif } +static void clear_trip_indexes(void) +{ + dive_trip_t *trip; + + for (trip = dive_trip_list; trip != NULL; trip = trip->next) + trip->index = 0; +} + static void fill_dive_list(void) { - int i; - GtkTreeIter iter, parent_iter, *parent_ptr = NULL; + int i, trip_index = 0; + GtkTreeIter iter, parent_iter, lookup, *parent_ptr = NULL; GtkTreeStore *liststore, *treestore; - dive_trip_t *last_trip = NULL; /* Do we need to create any dive groups automatically? */ if (autogroup) @@ -1213,27 +1220,36 @@ static void fill_dive_list(void) treestore = TREESTORE(dive_list); liststore = LISTSTORE(dive_list); + clear_trip_indexes(); + i = dive_table.nr; while (--i >= 0) { struct dive *dive = get_dive(i); dive_trip_t *trip = dive->divetrip; - if (trip != last_trip) { - last_trip = trip; - if (trip) { - /* create trip entry */ - gtk_tree_store_append(treestore, &parent_iter, NULL); - parent_ptr = &parent_iter; - /* a duration of 0 (and negative index) identifies a group */ - gtk_tree_store_set(treestore, parent_ptr, - DIVE_INDEX, -1, - DIVE_DATE, trip->when, - DIVE_LOCATION, trip->location, - DIVE_DURATION, 0, - -1); - } else { - parent_ptr = NULL; - } + if (!trip) { + parent_ptr = NULL; + } else if (!trip->index) { + trip->index = ++trip_index; + + /* Create new trip entry */ + gtk_tree_store_append(treestore, &parent_iter, NULL); + parent_ptr = &parent_iter; + + /* a duration of 0 (and negative index) identifies a group */ + gtk_tree_store_set(treestore, parent_ptr, + DIVE_INDEX, -trip_index, + DIVE_DATE, trip->when, + DIVE_LOCATION, trip->location, + DIVE_DURATION, 0, + -1); + } else { + int index = trip->index; + GtkTreeModel *model = TREEMODEL(dive_list); + gtk_tree_model_get_iter_first(model, &lookup); + while (--index) + gtk_tree_model_iter_next(model, &lookup); + parent_ptr = &lookup; } /* store dive */ diff --git a/save-xml.c b/save-xml.c index 61469e9ce..0cfc7ccfd 100644 --- a/save-xml.c +++ b/save-xml.c @@ -374,17 +374,6 @@ static void show_date(FILE *f, timestamp_t when) tm.tm_hour, tm.tm_min, tm.tm_sec); } -static void save_trip(FILE *f, dive_trip_t *trip) -{ - fprintf(f, "<trip"); - show_date(f, trip->when); - if (trip->location) - show_utf8(f, trip->location, " location=\'","\'", 1); - fprintf(f, ">\n"); - if (trip->notes) - show_utf8(f, trip->notes, "<notes>","</notes>\n", 0); -} - static void save_samples(FILE *f, int nr, struct sample *s) { static const struct sample empty_sample; @@ -445,6 +434,33 @@ static void save_dive(FILE *f, struct dive *dive) fprintf(f, "</dive>\n"); } +static void save_trip(FILE *f, dive_trip_t *trip) +{ + int i; + struct dive *dive; + + fprintf(f, "<trip"); + show_date(f, trip->when); + if (trip->location) + show_utf8(f, trip->location, " location=\'","\'", 1); + fprintf(f, ">\n"); + if (trip->notes) + show_utf8(f, trip->notes, "<notes>","</notes>\n", 0); + + /* + * Incredibly cheesy: we want to save the dives sorted, and they + * are sorted in the dive array.. So instead of using the dive + * list in the trip, we just traverse the global dive array and + * check the divetrip pointer.. + */ + for_each_dive(i, dive) { + if (dive->divetrip == trip) + save_dive(f, dive); + } + + fprintf(f, "</trip>\n"); +} + static char *add_dc_to_string(char *dc_xml, struct divecomputer *dc) { char *pattern, *tmp; @@ -483,7 +499,7 @@ void save_dives(const char *filename) { int i; struct dive *dive; - dive_trip_t *trip = NULL; + dive_trip_t *trip; char *dc_xml = strdup(""); FILE *f = g_fopen(filename, "w"); @@ -504,22 +520,28 @@ void save_dives(const char *filename) } fprintf(f, dc_xml); fprintf(f, "</settings>\n<dives>\n"); + + for (trip = dive_trip_list; trip != NULL; trip = trip->next) + trip->index = 0; + /* save the dives */ for_each_dive(i, dive) { - dive_trip_t *thistrip = dive->divetrip; - if (trip != thistrip) { - /* Close the old trip? */ - if (trip) - fprintf(f, "</trip>\n"); - /* Open the new one */ - if (thistrip) - save_trip(f, thistrip); - trip = thistrip; + trip = dive->divetrip; + + /* Bare dive without a trip? */ + if (!trip) { + save_dive(f, dive); + continue; } - save_dive(f, get_dive(i)); + + /* Have we already seen this trip (and thus saved this dive?) */ + if (trip->index) + continue; + + /* We haven't seen this trip before - save it and all dives */ + trip->index = 1; + save_trip(f, trip); } - if (trip) - fprintf(f, "</trip>\n"); fprintf(f, "</dives>\n</divelog>\n"); fclose(f); } |