Rough "Add new dive" infrastructure in the divelist

Do a right-click to get a menu with the "Add dive" entry.  Should do
delete too, but that's for later.

What's also apparently for later is to make this *useful*.  It's the
butt-ugliest time entry field ever, and there's no way to set depth for
the dive either.  So this is more of a RFC than anything truly useful.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Add depth entry to new dive edit dialog

Christ, if you look up "Ugly dialog" on Wikipedia, I think it has a
picture of this "New dive" thing.  Or it should have.

But it kind of works.  Although with only a "max depth" entry, you can't
currently set average depths etc, so SAC-rates etc cannot be calculated
for these kinds of dives.

And the dive numbering is wrong.  We do auto-number new dives that get
added at the end, but we do it as we add them, so when you *edit* the
dive information (before it has been added) the dive number shows up as
"#0".

So there's certainly room for improvement here.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Make it possible to do "Add Dive" from just the main dive menu

No need for right-clicks.  It's inconvenient on lots of laptops etc, so
allow just using the Dive menu as an alternative.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Make the 'Add Dive' dialog at least slightly less butt-ugly

I still suspect that using spinbuttons for the time handling is the
wrong way, and I'm a bit surprised the Calendar widget doesn't have a
mode where you can see/set the time too.

But this makes things at least minimally prettier, and initializes the
time entries to the current time (which is obviously not what anybody
really wants, but looks a lot better than defaulting to "midnight" or
some other random time that *also* won't be what anybody actually
wants).

I think this might be something we can live with, although I hope
somebody with good taste comes along and say "don't use spinbuttons, do
this: xyzzy" and makes things look better yet.

Also, I have this suspicion that I should put the time/depth/duration
stuff to the right of the calendar.  Most displays are wider than they
are tall, so tall and skinny dialogs are bad especially if you have
limited vertical pixels.  I still have flashbacks to my netbook-using
days, hating applictions that did that.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Fix a couple of possible divide-by-zero conditions in statistics

Several people reported the average time problem, but there's another
one lurking there too: if the dive duration is zero, you get bogus
average depth information too (but because that one was a floating point
divide, and by default they are unsignalling on x86, it didn't crash, it
just resulted in bogus results).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Update for libdivecomputer pkg-config include file changes

Subsurface doesn't compile on OS X any more, because libdivecomputer
changed the way the header inclusion works: the include path from
pkg-config no longer includes the final "libdivecomputer" component, and
instead of doing

  #include <header.h>

for libdivecomputer headers, we're now supposed to do

  #include <libdivecomputer/header.h>

instead. Which is cleaner anyway.

The reason this only bit us on OS X is that I never trusted pkg-config
that much for non-system libraries on Linux (maybe it works, maybe it
doesn't, I've seen it go both ways), so on Linux we just used our own
version of the include path, and thus weren't affected by the
libdivecomputer config change.

Clean up the includes while at it - we no longer need (or want) the
device-specific header files, since we just use the generic functions.

Reported-by: Grischa Toedt <toedt@embl.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
file.c: Fix a file descriptor leak in readfile()

In file.c::readfile() the file was being opened once at fd declaration
time and then again a few lines later and only being closed once. Remove
the open() at fd declaration time leaving the later one where the fd check
is done.

Signed-off-by: Andrew Clayton <andrew@digital-domain.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Divide the panes evenly in view_three

There was a note by Linus that he doesn't know how to get the size, so
I'm fixing that.

Signed-off-by: Mikko Rasa <tdb@tdb.fi>
Improved depth info for dives without samples

This calculates a mean depth for the dive with a fixed ascent/descent
rate and an assumption that all of the bottom time is at the maximum
depth.  It's not much, but it allows some derived values such as SAC to
make more sense.

The depth profile for such dives is now also generated with the same
assumptions instead of putting the samples at fixed percentages of the
dive duration.

Signed-off-by: Mikko Rasa <tdb@tdb.fi>
Changes to menu icons

It's customary for menu bars to not have icons.

Some items were lacking icons when there's perfectly good stock icons
available.  I was a bit torn between the "new" and "add" icons for the
"add dive" item, since what it really does is create a new dive, but
the "add" icon is an uninteresting sheet of paper in the default icon
theme so I decided to use the "add" icon.

Signed-off-by: Mikko Rasa <tdb@tdb.fi>
Add a separate "Save as" entry to the menu

The "Save" entry will now automatically save over the last used file.  If
no filename has been set, then that entry will also prompt the user for a
filename.

The filename is set when saving as well, so the next save will use the
same filename.

Signed-off-by: Mikko Rasa <tdb@tdb.fi>
Remember the last weight used per weightsystem

With this change, if the user adds a new weightsystem to a dive, on
subsequent edits the weight amount for this weightsystem no longer
defaults to 0 but to the last weight that was used with this weightsystem.

So when the program imports a set of dives from the divecomputer and the
user starts editing them, once they enter the weight for the "integrated"
weightsystem the first time, for each of the consecutive dives that same
weight is the default once "integrated" is selected - which usually will
be the correct amount.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Remove weightsystem entry with no description

This existed in the initial implementation to deal with an implementation
problem that was long since resolved. So now it just created just an ugly
empty line in the drop down menu for weightsystems.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Fill the list of weightsystems from data in existing dives

This was simply an omission in the current implementation. All the
plumbing was there but never got hooked up with the fixup_dive function as
intended.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Add total weight column to divelist

This adds the total weight carried on the dive in different weight systems
to the divelist. The column is by default not shown, which can be changed
in the preferences. The column is sortable.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Don't print a total weight of 0 in the weight column

For consistency with the rest of the dive_list we should interpret "no
weight systems recorded" as "no information" and therefore print nothing
instead of printing a total weight of "0" for these dives.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Allow date based grouping

This is the very first rough cut. It switches things over to a tree model
so we can have date based summary nodes.

It uses a DIVE_INDEX of -1 for summary nodes to easily tell them apart
from actual dives. All the data functions are changed so the summary
nodes only show the date they cover.

The commit also adds a couple of debug functions to be able to easily peek
into the model from the debugger.

Lots of things left to do. There is no longer a first dive selected when
starting subsurface. Sorting by columns other than date is messed up. We
almost certainly want month and year summary entries as well.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Improve tree model implementation

We now support three hierarchy levels: day, month, and year. Each
indicated by a negative DIVE_INDEX for -1 to -3. This allows a nice
compact overview when doing date based sorting (the default).

As indicated in the previous commit, things still go wrong with sorting by
other columns as the entries are only sorted within each day, not globally
across the whole dive list.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Create duplicate list model so sorting by columns works again

One major downside of the switch to a tree model is that sorting by
columns other than date was broken - it would sort the entries within each
date which is not all that useful.

After playing with some Gtk trickery that would allow us to filter out
those rows it quickly became clear that the much easier solution is to
simply maintain TWO models (and therefore two storages). This causes some
overhead and requires some careful tracking of all changes, but it turned
out to be rather straight forward to do.

dive_list now has three model related members:
 model     - current model displayed (which is one of the following two)
 treemodel - the tree model
 listmodel - the list model

One side effect is that the callbacks no longer can pass the model around
(as this could have changed since the callback was registered), but that
seems only a minor drawback and was easily addressed.

The implementation in this commit still has a couple of obvious flaws:
when switching back from the list model to the tree model all the
expansion state of the rows is lost and we end up with just a list of the
different years visible. Also, selections aren't maintained when switching
models.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Maintain selected rows when switching between list model and tree model

We keep track of the DIVE_INDEX of all selected dives and simply re-select
those dives after changing model (date based sort or sort by other
column).

There are a few TODOs left. We lose the sort direction (ascending /
descending) when switching models. We also don't correctly deal with the
user selecting summary rows in the tree model.

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