diff options
Diffstat (limited to 'dive.c')
-rw-r--r-- | dive.c | 130 |
1 files changed, 77 insertions, 53 deletions
@@ -13,6 +13,7 @@ * it's used in the UI, but it seems to make the most sense to have it * here */ struct dive displayed_dive; +struct dive_site displayed_dive_site; struct tag_entry *g_tag_list = NULL; @@ -391,6 +392,7 @@ static void copy_pl(struct picture *sp, struct picture *dp) { *dp = *sp; dp->filename = copy_string(sp->filename); + dp->hash = copy_string(sp->hash); } /* copy an element in a list of tags */ @@ -437,7 +439,6 @@ void clear_dive(struct dive *d) /* free the strings */ free(d->buddy); free(d->divemaster); - free(d->location); free(d->notes); free(d->suit); /* free tags, additional dive computers, and pictures */ @@ -463,7 +464,6 @@ void copy_dive(struct dive *s, struct dive *d) *d = *s; d->buddy = copy_string(s->buddy); d->divemaster = copy_string(s->divemaster); - d->location = copy_string(s->location); d->notes = copy_string(s->notes); d->suit = copy_string(s->suit); for (int i = 0; i < MAX_CYLINDERS; i++) @@ -500,7 +500,6 @@ void selective_copy_dive(struct dive *s, struct dive *d, struct dive_components { if (clear) clear_dive(d); - CONDITIONAL_COPY_STRING(location); CONDITIONAL_COPY_STRING(notes); CONDITIONAL_COPY_STRING(divemaster); CONDITIONAL_COPY_STRING(buddy); @@ -509,10 +508,8 @@ void selective_copy_dive(struct dive *s, struct dive *d, struct dive_components d->rating = s->rating; if (what.visibility) d->visibility = s->visibility; - if (what.gps) { - d->longitude = s->longitude; - d->latitude = s->latitude; - } + if (what.divesite) + d->dive_site_uuid = s->dive_site_uuid; if (what.tags) STRUCTURED_LIST_COPY(struct tag_entry, s->tag_list, d->tag_list, copy_tl); if (what.cylinders) @@ -1403,7 +1400,7 @@ struct dive *fixup_dive(struct dive *dive) #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n) #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n -static struct sample *add_sample(struct sample *sample, int time, struct divecomputer *dc) +struct sample *add_sample(struct sample *sample, int time, struct divecomputer *dc) { struct sample *p = prepare_sample(dc); @@ -1761,7 +1758,7 @@ static void add_initial_gaschange(struct dive *dive, struct divecomputer *dc) add_gas_switch_event(dive, dc, 0, 0); } -static void dc_cylinder_renumber(struct dive *dive, struct divecomputer *dc, int mapping[]) +void dc_cylinder_renumber(struct dive *dive, struct divecomputer *dc, int mapping[]) { int i; struct event *ev; @@ -2262,7 +2259,7 @@ struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded return NULL; } -static void free_events(struct event *ev) +void free_events(struct event *ev) { while (ev) { struct event *next = ev->next; @@ -2721,7 +2718,7 @@ int count_dives_with_location(const char *location) struct dive *d; for_each_dive (i, d) { - if (same_string(d->location, location)) + if (same_string(get_dive_location(d), location)) counter++; } return counter; @@ -2766,9 +2763,6 @@ struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer res->when = dl ? dl->when : a->when; res->selected = a->selected || b->selected; merge_trip(res, a, b); - MERGE_NONZERO(res, a, b, latitude.udeg); - MERGE_NONZERO(res, a, b, longitude.udeg); - MERGE_TXT(res, a, b, location); MERGE_TXT(res, a, b, notes); MERGE_TXT(res, a, b, buddy); MERGE_TXT(res, a, b, divemaster); @@ -2788,7 +2782,7 @@ struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer interleave_dive_computers(&res->dc, &a->dc, &b->dc, offset); else join_dive_computers(&res->dc, &a->dc, &b->dc, 0); - + res->dive_site_uuid = a->dive_site_uuid ?: b->dive_site_uuid; fixup_dive(res); return res; } @@ -2866,15 +2860,14 @@ void set_userid(char *rUserId) prefs.userid[30]='\0'; } -int average_depth(struct diveplan *dive) +void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_depth) { int integral = 0; int last_time = 0; int last_depth = 0; struct divedatapoint *dp = dive->dp; - if (!dp) - return 0; + *max_depth = 0; while (dp) { if (dp->time) { @@ -2882,13 +2875,15 @@ int average_depth(struct diveplan *dive) integral += (dp->depth + last_depth) * (dp->time - last_time) / 2; last_time = dp->time; last_depth = dp->depth; + if (dp->depth > *max_depth) + *max_depth = dp->depth; } dp = dp->next; } if (last_time) - return integral / last_time; + *avg_depth = integral / last_time; else - return 0; + *avg_depth = *max_depth = 0; } struct picture *alloc_picture() @@ -2912,29 +2907,51 @@ static bool new_picture_for_dive(struct dive *d, char *filename) // only add pictures that have timestamps between 30 minutes before the dive and // 30 minutes after the dive ends #define D30MIN (30 * 60) -void dive_create_picture(struct dive *d, char *filename, int shift_time) +bool dive_check_picture_time(struct dive *d, int shift_time, timestamp_t timestamp) { - timestamp_t timestamp; - if (!new_picture_for_dive(d, filename)) - return; - struct picture *p = alloc_picture(); - p->filename = filename; - picture_load_exif_data(p, ×tamp); + offset_t offset; if (timestamp) { - p->offset.seconds = timestamp - d->when + shift_time; - if (p->offset.seconds < -D30MIN || p->offset.seconds > (int)d->duration.seconds + D30MIN) { - // this picture doesn't belong to this dive - free(p); - return; + offset.seconds = timestamp - d->when + shift_time; + if (offset.seconds > -D30MIN && offset.seconds < (int)d->duration.seconds + D30MIN) { + // this picture belongs to this dive + return true; } } - dive_add_picture(d, p); - dive_set_geodata_from_picture(d, p); + return false; +} + +bool picture_check_valid(char *filename, int shift_time) +{ + int i; + struct dive *dive; + + timestamp_t timestamp = picture_get_timestamp(filename); + for_each_dive (i, dive) + if (dive->selected && dive_check_picture_time(dive, shift_time, timestamp)) + return true; + return false; } -void dive_add_picture(struct dive *d, struct picture *newpic) +void dive_create_picture(struct dive *dive, char *filename, int shift_time) { - struct picture **pic_ptr = &d->picture_list; + timestamp_t timestamp = picture_get_timestamp(filename); + if (!new_picture_for_dive(dive, filename)) + return; + if (!dive_check_picture_time(dive, shift_time, timestamp)) + return; + + struct picture *picture = alloc_picture(); + picture->filename = strdup(filename); + picture->offset.seconds = timestamp - dive->when + shift_time; + picture_load_exif_data(picture); + + dive_add_picture(dive, picture); + dive_set_geodata_from_picture(dive, picture); +} + +void dive_add_picture(struct dive *dive, struct picture *newpic) +{ + struct picture **pic_ptr = &dive->picture_list; /* let's keep the list sorted by time */ while (*pic_ptr && (*pic_ptr)->offset.seconds <= newpic->offset.seconds) pic_ptr = &(*pic_ptr)->next; @@ -2943,38 +2960,45 @@ void dive_add_picture(struct dive *d, struct picture *newpic) return; } -unsigned int dive_get_picture_count(struct dive *d) +unsigned int dive_get_picture_count(struct dive *dive) { unsigned int i = 0; - FOR_EACH_PICTURE (d) + FOR_EACH_PICTURE (dive) i++; return i; } -void dive_set_geodata_from_picture(struct dive *d, struct picture *pic) +void dive_set_geodata_from_picture(struct dive *dive, struct picture *picture) { - if (!d->latitude.udeg && pic->latitude.udeg) { - d->latitude = pic->latitude; - d->longitude = pic->longitude; + struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid); + if (!dive_site_has_gps_location(ds) && (picture->latitude.udeg || picture->longitude.udeg)) { + if (ds) { + ds->latitude = picture->latitude; + ds->longitude = picture->longitude; + } else { + dive->dive_site_uuid = create_dive_site_with_gps("", picture->latitude, picture->longitude); + } } } -static void picture_free(struct picture *p) +static void picture_free(struct picture *picture) { - if (!p) + if (!picture) return; - free(p->filename); - free(p); + free(picture->filename); + free(picture->hash); + free(picture); } + void dive_remove_picture(char *filename) { - struct picture **ep = ¤t_dive->picture_list; - while (ep && !same_string((*ep)->filename, filename)) - ep = &(*ep)->next; - if (ep) { - struct picture *temp = (*ep)->next; - picture_free(*ep); - *ep = temp; + struct picture **picture = ¤t_dive->picture_list; + while (picture && !same_string((*picture)->filename, filename)) + picture = &(*picture)->next; + if (picture) { + struct picture *temp = (*picture)->next; + picture_free(*picture); + *picture = temp; } } |