Core: remove "when" field of struct dive_trip

The when field gives the time of the first dive. Instead of keeping
this field in sync, replace it by a function that determines the time
of the first dive.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-11-11 13:09:51 +01:00 committed by Dirk Hohndel
parent 431b2bb845
commit 64e6e435f8
11 changed files with 23 additions and 52 deletions

View file

@ -293,7 +293,6 @@ struct dive_table {
typedef struct dive_trip
{
timestamp_t when;
char *location;
char *notes;
struct dive_table dives;
@ -432,6 +431,7 @@ extern void delete_single_dive(int idx);
extern void insert_trip(dive_trip_t *trip);
extern void unregister_trip(dive_trip_t *trip);
extern void free_trip(dive_trip_t *trip);
extern timestamp_t trip_date(const struct dive_trip *trip);
extern const struct units SI_units, IMPERIAL_units;

View file

@ -739,7 +739,7 @@ void dump_trip_list(void)
for (trip = dive_trip_list; trip; trip = trip->next) {
struct tm tm;
utc_mkdate(trip->when, &tm);
utc_mkdate(trip_date(trip), &tm);
if (trip->when < last_time)
printf("\n\ndive_trip_list OUT OF ORDER!!!\n\n\n");
printf("%s trip %d to \"%s\" on %04u-%02u-%02u %02u:%02u:%02u (%d dives - %p)\n",
@ -805,9 +805,12 @@ static void delete_trip(dive_trip_t *trip)
free_trip(trip);
}
void find_new_trip_start_time(dive_trip_t *trip)
timestamp_t trip_date(const struct dive_trip *trip)
{
trip->when = trip->dives.nr > 0 ? trip->dives.dives[0]->when : 0;
if (!trip || trip->dives.nr == 0)
return 0;
return trip->dives.dives[0]->when;
}
/* check if we have a trip right before / after this dive */
@ -876,8 +879,6 @@ struct dive_trip *unregister_dive_from_trip(struct dive *dive, short was_autogen
dive->tripflag = TF_NONE;
else
dive->tripflag = NO_TRIP;
if (trip->dives.nr > 0 && trip->when == dive->when)
find_new_trip_start_time(trip);
return trip;
}
@ -908,7 +909,6 @@ dive_trip_t *create_trip_from_dive(struct dive *dive)
dive_trip_t *trip;
trip = alloc_trip();
trip->when = dive->when;
trip->location = copy_string(get_dive_location(dive));
return trip;
@ -1345,7 +1345,6 @@ dive_trip_t *combine_trips_create(struct dive_trip *trip_a, struct dive_trip *tr
dive_trip_t *trip;
trip = alloc_trip();
trip->when = trip_a->when;
trip->location = copy_non_empty_string(trip_a->location, trip_b->location);
trip->notes = copy_non_empty_string(trip_a->notes, trip_b->notes);
@ -1726,9 +1725,9 @@ static int comp_dives(const struct dive *a, const struct dive *b)
return -1;
if (!a->divetrip)
return 1;
if (a->divetrip->when < b->divetrip->when)
if (trip_date(a->divetrip) < trip_date(b->divetrip))
return -1;
if (a->divetrip->when > b->divetrip->when)
if (trip_date(a->divetrip) > trip_date(b->divetrip))
return 1;
}
if (a->id < b->id)

View file

@ -46,7 +46,6 @@ extern void deselect_dives_in_trip(struct dive_trip *trip);
extern void filter_dive(struct dive *d, bool shown);
extern void combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b);
extern dive_trip_t *combine_trips_create(struct dive_trip *trip_a, struct dive_trip *trip_b);
extern void find_new_trip_start_time(dive_trip_t *trip);
extern struct dive *first_selected_dive();
extern struct dive *last_selected_dive();
extern bool is_trip_before_after(const struct dive *dive, bool before);

View file

@ -799,12 +799,6 @@ static void parse_dc_event(char *line, struct membuffer *str, void *_dc)
}
}
static void parse_trip_date(char *line, struct membuffer *str, void *_trip)
{ UNUSED(str); dive_trip_t *trip = _trip; update_date(&trip->when, line); }
static void parse_trip_time(char *line, struct membuffer *str, void *_trip)
{ UNUSED(str); dive_trip_t *trip = _trip; update_time(&trip->when, line); }
static void parse_trip_location(char *line, struct membuffer *str, void *_trip)
{ UNUSED(line); dive_trip_t *trip = _trip; trip->location = get_utf8(str); }
@ -1007,7 +1001,7 @@ static void site_parser(char *line, struct membuffer *str, void *_ds)
struct keyword_action trip_action[] = {
#undef D
#define D(x) { #x, parse_trip_ ## x }
D(date), D(location), D(notes), D(time),
D(location), D(notes),
};
static void trip_parser(char *line, struct membuffer *str, void *_trip)
@ -1202,20 +1196,6 @@ static struct dive *create_new_dive(timestamp_t when)
return dive;
}
static dive_trip_t *create_new_trip(int yyyy, int mm, int dd)
{
dive_trip_t *trip = alloc_trip();
struct tm tm = { 0 };
/* We'll fill in the real data from the trip descriptor file */
tm.tm_year = yyyy;
tm.tm_mon = mm-1;
tm.tm_mday = dd;
trip->when = utc_mktime(&tm);
return trip;
}
static bool validate_date(int yyyy, int mm, int dd)
{
return yyyy > 1930 && yyyy < 3000 &&
@ -1243,7 +1223,7 @@ static int dive_trip_directory(const char *root, const char *name)
if (!validate_date(yyyy, mm, dd))
return GIT_WALK_SKIP;
finish_active_trip();
active_trip = create_new_trip(yyyy, mm, dd);
active_trip = alloc_trip();
return GIT_WALK_OK;
}

View file

@ -1352,10 +1352,6 @@ static void try_to_fill_trip(dive_trip_t **dive_trip_p, const char *name, char *
dive_trip_t *dive_trip = *dive_trip_p;
if (MATCH_STATE("date", divedate, &dive_trip->when))
return;
if (MATCH_STATE("time", divetime, &dive_trip->when))
return;
if (MATCH("location", utf8_string, &dive_trip->location))
return;
if (MATCH("notes", utf8_string, &dive_trip->notes))

View file

@ -956,7 +956,7 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
}
/* Create the date-based hierarchy */
utc_mkdate(trip ? trip->when : dive->when, &tm);
utc_mkdate(trip ? trip_date(trip) : dive->when, &tm);
tree = mktree(repo, root, "%04d", tm.tm_year);
tree = mktree(repo, tree, "%02d", tm.tm_mon + 1);

View file

@ -520,7 +520,7 @@ static void save_trip(struct membuffer *b, dive_trip_t *trip, bool anonymize)
struct dive *dive;
put_format(b, "<trip");
show_date(b, trip->when);
show_date(b, trip_date(trip));
show_utf8(b, trip->location, " location=\'", "\'", 1);
put_format(b, ">\n");
show_utf8(b, trip->notes, "<notes>", "</notes>\n", 0);

View file

@ -343,7 +343,7 @@ QString DiveObjectHelper::tripMeta() const
if (dt) {
QString numDives = tr("(%n dive(s))", "", dt->showndives);
QString title(dt->location);
QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*dt->when, Qt::UTC);
QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(dt), Qt::UTC);
QString firstMonth = firstTime.toString("MMM");
QString tripDate = QStringLiteral("%1@%2").arg(firstMonth,firstTime.toString("yy"));

View file

@ -909,10 +909,6 @@ void MainTab::acceptChanges()
Command::shiftTime(selectedDives, (int)offset);
}
}
if (editMode != TRIP && current_dive->divetrip) {
current_dive->divetrip->when = current_dive->when;
find_new_trip_start_time(current_dive->divetrip);
}
if (editMode == MANUALLY_ADDED_DIVE) {
// we just added or edited the dive, let fixup_dive() make
// sure we get the max. depth right

View file

@ -62,14 +62,14 @@ QVariant DiveTripModel::tripData(const dive_trip *trip, int column, int role)
struct dive *d = trip->dives.dives[i];
if (!d->hidden_by_filter)
countShown++;
oneDayTrip &= is_same_day (trip->when, d->when);
oneDayTrip &= is_same_day(trip_date(trip), d->when);
}
if (countShown < trip->dives.nr)
shownText = tr("(%1 shown)").arg(countShown);
if (!empty_string(trip->location))
return QString(trip->location) + ", " + get_trip_date_string(trip->when, trip->dives.nr, oneDayTrip) + " "+ shownText;
return QString(trip->location) + ", " + get_trip_date_string(trip_date(trip), trip->dives.nr, oneDayTrip) + " "+ shownText;
else
return get_trip_date_string(trip->when, trip->dives.nr, oneDayTrip) + shownText;
return get_trip_date_string(trip_date(trip), trip->dives.nr, oneDayTrip) + shownText;
}
}
@ -463,7 +463,7 @@ dive *DiveTripModel::Item::getDive() const
timestamp_t DiveTripModel::Item::when() const
{
return d_or_t.trip ? d_or_t.trip->when : d_or_t.dive->when;
return d_or_t.trip ? trip_date(d_or_t.trip) : d_or_t.dive->when;
}
// Find a range of matching elements in a vector.
@ -678,10 +678,11 @@ int DiveTripModel::findDiveInTrip(int tripIdx, const dive *d) const
return -1;
}
int DiveTripModel::findInsertionIndex(timestamp_t when) const
int DiveTripModel::findInsertionIndex(const dive_trip *trip) const
{
dive_or_trip d_or_t{ nullptr, (dive_trip *)trip };
for (int i = 0; i < (int)items.size(); ++i) {
if (when <= items[i].when())
if (dive_or_trip_less_than(d_or_t, items[i].d_or_t))
return i;
}
return items.size();
@ -765,7 +766,7 @@ void DiveTripModel::divesAdded(dive_trip *trip, bool addTrip, const QVector<dive
});
} else if (addTrip) {
// We're supposed to add the whole trip. Just insert the trip.
int idx = findInsertionIndex(trip->when); // Find the place where we have to insert the thing
int idx = findInsertionIndex(trip); // Find the place where to insert the trip
beginInsertRows(QModelIndex(), idx, idx);
items.insert(items.begin() + idx, { trip, dives });
endInsertRows();

View file

@ -104,7 +104,7 @@ private:
int findTripIdx(const dive_trip *trip) const;
int findDiveIdx(const dive *d) const; // Find _top_level_ dive
int findDiveInTrip(int tripIdx, const dive *d) const; // Find dive inside trip. Second parameter is index of trip
int findInsertionIndex(timestamp_t when) const; // Where to insert item with timestamp "when"
int findInsertionIndex(const dive_trip *trip) const; // Where to insert trip
// Access trip and dive data
static QVariant diveData(const struct dive *d, int column, int role);