Check if trip is NULL before calling DIVE_TRIP

Other places have this check, but for this particular one a crash
can be reproduced:

./subsurface ./dives/*
log -> autogroup
log -> autogroup

Against d14932058f191de2a

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Use a 64-bit 'timestamp_t' for all timestamps, rather than 'time_t'

This makes the time type unambiguous, and we can use G_TYPE_INT64 for it
in the divelist too.

It also implements a portable (and thread-safe) "utc_mkdate()" function
that acts kind of like gmtime_r(), but using the 64-bit timestamp_t.  It
matches our original "utc_mktime()".

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Use long instead of int when retrieving DIVE_DATE via gtk_tree_model_get

The type of the DIVE_DATE field is G_TYPE_LONG, so when accessing it we
have to pass gtk_tree_model_get() the proper "long *" pointer, rather
than an int.  On 64-bit platforms, passing a pointer to an int would
otherwise result in randomly overwriting another four bytes of stack.

Sadly, there are not much safety checks to warn if our passed variable
to the vararg list is of the correct type, which is unlike
gtk_tree_model_get_value(), which will report typed errors.

This solves a bug that may potentially result in undefined behaviour
on some x64 OS (e.g Ubuntu 12.04 x64).

Reported-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Partial rewrite of the dive trip code

This introduces a new data structure for dive trips - reuseing the struct
dive just got way too messy.

The dive_trip_t datastructure now allows the code to remember if the trip
was auto generated or if its time stamp changed when dives where added to
the trip during auto generation.

The algorithm also distinguishes between dives that were intentionally
added to a trip (either in an XML file or by adding them to trip in the
UI) and dives that were added to trips via autogen. Saving dives that were
added to trips via autogen makes that assignment "intentional".

With this partial rewrite several of the oddities of the old code should
be resolved - especially turning autogen on and off again should get the
divelist back to the previous stage.

Also, when dives are merged during file open or import we now try to pick
the correct tripflag (instead of just ignoring the tripflag completely and
resetting it to TF_NONE by mistake).

Finally, the dive trip debugging code got more verbose and is trying
harder to detect issues at the earliest time possible.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Fix the incorrect data type for DIVE_DATE accesses

This is the same bugfix that Lubomir did in the master branch, but now
on top of the new 64-bit timestamp_t model.  So now we also remove the
comment about the year 2038 problem, because it's not true any more.  We
do all the date handling in a 64-bit integer.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Convert FIND_TRIP into function

This helps us deal with the issue that the g_list convenience functions
don't allow us to easily compare 64bit values on 32bit architectures. And
since these convenience functions are truly trivial in nature, it seemed
easier to simply implement our own logic here.

In the process I moved all the dive_trip_list helper functions into the
same spot in divelist.c

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
dive-time widget: fix incorrect use of timestamp_t

I did a global search-and-replace to make all "time_t" users use the
internal subsurface 64-bit "timestamp_t" type instead, but we have one
case that still uses the system time functions: the use of "localtime()"
in the dive_time_widget().

Everywhere else we always just use UTC for all our time handling, and we
don't really ever care about the local timezone etc.  However, for the
dive time widget, we initialize the calendar widget to the current time,
which obviously does want to take the local timezone into account, so
there we end up using the whole system time handling code.

So that one should continue to use time_t, even if it might have the
year-2038 problem.  We also don't care about the fact that it's not
thread-safe, since this is just initializing the widget which definitely
doesn't happen threaded.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
FIND_TRIP: don't cast a timestamp to a pointer

The pointer size may not be large enough to contain a timestamp, so make
FIND_TRIP() just pass the pointer to the timestamp instead.

And use an inline function instead of macros with casts.  That gets us
proper type safety while at it, so that we get a warning if somebody
doesn't pass the expected "timestamp_t *".  Plus the code actually looks
simpler and way more straightforward.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Merge branch 'time-function'

Merge the 64-bit timestamp_t time function branch.

This makes subsurface not only safe against the 2038-year problem, but
also avoids the use of thread-unsafe gmtime() etc.

We still use the system time_t for initializing the calendar widget for
adding a new dive, but that's cosmetic rather than anything fundamental.

* time-function:
  FIND_TRIP: don't cast a timestamp to a pointer
  dive-time widget: fix incorrect use of timestamp_t
  Fix the incorrect data type for DIVE_DATE accesses
  Use a 64-bit 'timestamp_t' for all timestamps, rather than 'time_t'
Merge branch 'divetrip-rewrite' of git://github.com/torvalds/subsurface

Merge the dive trip rewrite by Dirk Hohndel.

This just merges the dive trip changes with the timestamp handling
changes.  There were multiple small data conflicts, along with some
newly added 'time_t' cases in the dive trip handling that needed to be
converted to 'timestamp_t' along the way.

* 'divetrip-rewrite' of git://github.com/torvalds/subsurface:
  Convert FIND_TRIP into function
  Partial rewrite of the dive trip code
  Check if trip is NULL before calling DIVE_TRIP
Fix yearly statistics after 64-bit time changes

One of the "+1900" cases got incorrectly dropped.

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
[ I really wanted the utc_mkdate/mktime functions to use proper years,
  rather than the idiotic "offset by 1900".  However, we still mix
  things with "localtime()" in one place, so for now we have to be
  compatible with the insane tm_year semantics.  - Linus ]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Don't update the progress bar from the dive computer import thread

There's no guarantee that gtk is thread-safe (apparently it can be
broken at least on Windows, even though it should be fine on top of X).

So don't update the progress bar directly from the dive computer import
code, instead just update the progress information in static variables,
and let the GUI thread update it while it does the idle loop polling
anyway.

Reported-by: Jef Driesen <jefdriesen@telenet.be>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Convert the divelist debug helpers to the new time functions as well

This makes things consistent.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Merge remote-tracking branch 'origin/single-threaded-gui'

This brings in the last of the outstanding changes that fix bugs that were
discovered when hunting down the long standing crash with some 64bit OSs.
Removed obsolete debug message

We now handle this case correctly, so don't alert the user to something
that isn't an issue at all.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>