diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-04-24 16:33:29 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-04-24 16:38:25 -0700 |
commit | 92a5a5c35b39543a2feaeea247eb7390cd3a9e94 (patch) | |
tree | c48388fd9cd08a0196855367112f9cbf4493a6d4 | |
parent | 02d822924cd13ad6935e167d9a7597af91445a60 (diff) | |
download | subsurface-92a5a5c35b39543a2feaeea247eb7390cd3a9e94.tar.gz |
Split report_dives into UI and logic and move to divelist files
Functionality is unchanged, except we now have a nice process_dives
function that deals with all the logic and that gets called from
report_dives from the Gtk code.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | dive.h | 1 | ||||
-rw-r--r-- | divelist-gtk.c | 7 | ||||
-rw-r--r-- | divelist.c | 121 | ||||
-rw-r--r-- | divelist.h | 5 | ||||
-rw-r--r-- | main.c | 127 |
5 files changed, 132 insertions, 129 deletions
@@ -589,7 +589,6 @@ extern struct sample *prepare_sample(struct divecomputer *dc); extern void finish_sample(struct divecomputer *dc); extern void sort_table(struct dive_table *table); -extern void report_dives(gboolean imported, gboolean prefer_imported); extern struct dive *fixup_dive(struct dive *dive); extern unsigned int dc_airtemp(struct divecomputer *dc); extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, gboolean prefer_downloaded); diff --git a/divelist-gtk.c b/divelist-gtk.c index 189b75ba1..e1482ee06 100644 --- a/divelist-gtk.c +++ b/divelist-gtk.c @@ -801,7 +801,6 @@ static gint gtk_dive_nr_sort(GtkTreeModel *model, return dive_nr_sort(idx_a, idx_b, when_a, when_b); } - static struct divelist_column { const char *header; data_func_t data; @@ -895,6 +894,12 @@ static void row_activated_cb(GtkTreeView *tree_view, edit_dive_info(get_dive(index), FALSE); } +void report_dives(bool is_imported, bool prefer_imported) +{ + process_dives(is_imported, prefer_imported); + dive_list_update_dives(); +} + void add_dive_cb(GtkWidget *menuitem, gpointer data) { struct dive *dive; diff --git a/divelist.c b/divelist.c index 577750399..bd1e90224 100644 --- a/divelist.c +++ b/divelist.c @@ -935,3 +935,124 @@ void remove_autogen_trips() } } +/* + * When adding dives to the dive table, we try to renumber + * the new dives based on any old dives in the dive table. + * + * But we only do it if: + * + * - there are no dives in the dive table + * + * OR + * + * - the last dive in the old dive table was numbered + * + * - all the new dives are strictly at the end (so the + * "last dive" is at the same location in the dive table + * after re-sorting the dives. + * + * - none of the new dives have any numbers + * + * This catches the common case of importing new dives from + * a dive computer, and gives them proper numbers based on + * your old dive list. But it tries to be very conservative + * and not give numbers if there is *any* question about + * what the numbers should be - in which case you need to do + * a manual re-numbering. + */ +static void try_to_renumber(struct dive *last, int preexisting) +{ + int i, nr; + + /* + * If the new dives aren't all strictly at the end, + * we're going to expect the user to do a manual + * renumbering. + */ + if (preexisting && get_dive(preexisting-1) != last) + return; + + /* + * If any of the new dives already had a number, + * we'll have to do a manual renumbering. + */ + for (i = preexisting; i < dive_table.nr; i++) { + struct dive *dive = get_dive(i); + if (dive->number) + return; + } + + /* + * Ok, renumber.. + */ + if (last) + nr = last->number; + else + nr = 0; + for (i = preexisting; i < dive_table.nr; i++) { + struct dive *dive = get_dive(i); + dive->number = ++nr; + } +} + +void process_dives(bool is_imported, bool prefer_imported) +{ + int i; + int preexisting = dive_table.preexisting; + struct dive *last; + + /* check if we need a nickname for the divecomputer for newly downloaded dives; + * since we know they all came from the same divecomputer we just check for the + * first one */ + if (preexisting < dive_table.nr && dive_table.dives[preexisting]->downloaded) + set_dc_nickname(dive_table.dives[preexisting]); + else + /* they aren't downloaded, so record / check all new ones */ + for (i = preexisting; i < dive_table.nr; i++) + set_dc_nickname(dive_table.dives[i]); + + /* This does the right thing for -1: NULL */ + last = get_dive(preexisting-1); + + sort_table(&dive_table); + + for (i = 1; i < dive_table.nr; i++) { + struct dive **pp = &dive_table.dives[i-1]; + struct dive *prev = pp[0]; + struct dive *dive = pp[1]; + struct dive *merged; + + /* only try to merge overlapping dives - or if one of the dives has + * zero duration (that might be a gps marker from the webservice) */ + if (prev->duration.seconds && dive->duration.seconds && + prev->when + prev->duration.seconds < dive->when) + continue; + + merged = try_to_merge(prev, dive, prefer_imported); + if (!merged) + continue; + + /* careful - we might free the dive that last points to. Oops... */ + if (last == prev || last == dive) + last = merged; + + /* Redo the new 'i'th dive */ + i--; + add_single_dive(i, merged); + delete_single_dive(i+1); + delete_single_dive(i+1); + } + /* make sure no dives are still marked as downloaded */ + for (i = 1; i < dive_table.nr; i++) + dive_table.dives[i]->downloaded = FALSE; + + if (is_imported) { + /* If there are dives in the table, are they numbered */ + if (!last || last->number) + try_to_renumber(last, preexisting); + + /* did we add dives to the dive table? */ + if (preexisting != dive_table.nr) + mark_divelist_changed(TRUE); + } +} diff --git a/divelist.h b/divelist.h index 014c4e0ee..e3ff1be09 100644 --- a/divelist.h +++ b/divelist.h @@ -4,8 +4,12 @@ #ifdef __cplusplus extern "C" { #endif + +#include <stdbool.h> + struct dive; +extern void report_dives(bool imported, bool prefer_imported); extern void dive_list_update_dives(void); extern void update_dive_list_col_visibility(void); extern void update_dive_list_units(void); @@ -21,6 +25,7 @@ extern double init_decompression(struct dive * dive); extern void export_all_dives_uddf_cb(); /* divelist core logic functions */ +extern void process_dives(bool imported, bool prefer_imported); extern char *get_dive_date_string(struct tm *tm); extern char *get_trip_date_string(struct tm *tm, int nr); extern void clear_trip_indexes(void); @@ -81,137 +81,10 @@ const char *monthname(int mon) } /* - * When adding dives to the dive table, we try to renumber - * the new dives based on any old dives in the dive table. - * - * But we only do it if: - * - * - there are no dives in the dive table - * - * OR - * - * - the last dive in the old dive table was numbered - * - * - all the new dives are strictly at the end (so the - * "last dive" is at the same location in the dive table - * after re-sorting the dives. - * - * - none of the new dives have any numbers - * - * This catches the common case of importing new dives from - * a dive computer, and gives them proper numbers based on - * your old dive list. But it tries to be very conservative - * and not give numbers if there is *any* question about - * what the numbers should be - in which case you need to do - * a manual re-numbering. - */ -static void try_to_renumber(struct dive *last, int preexisting) -{ - int i, nr; - - /* - * If the new dives aren't all strictly at the end, - * we're going to expect the user to do a manual - * renumbering. - */ - if (preexisting && get_dive(preexisting-1) != last) - return; - - /* - * If any of the new dives already had a number, - * we'll have to do a manual renumbering. - */ - for (i = preexisting; i < dive_table.nr; i++) { - struct dive *dive = get_dive(i); - if (dive->number) - return; - } - - /* - * Ok, renumber.. - */ - if (last) - nr = last->number; - else - nr = 0; - for (i = preexisting; i < dive_table.nr; i++) { - struct dive *dive = get_dive(i); - dive->number = ++nr; - } -} - -/* * track whether we switched to importing dives */ static gboolean imported = FALSE; -/* - * This doesn't really report anything at all. We just sort the - * dives, the GUI does the reporting - */ -void report_dives(gboolean is_imported, gboolean prefer_imported) -{ - int i; - int preexisting = dive_table.preexisting; - struct dive *last; - - /* check if we need a nickname for the divecomputer for newly downloaded dives; - * since we know they all came from the same divecomputer we just check for the - * first one */ - if (preexisting < dive_table.nr && dive_table.dives[preexisting]->downloaded) - set_dc_nickname(dive_table.dives[preexisting]); - else - /* they aren't downloaded, so record / check all new ones */ - for (i = preexisting; i < dive_table.nr; i++) - set_dc_nickname(dive_table.dives[i]); - - /* This does the right thing for -1: NULL */ - last = get_dive(preexisting-1); - - sort_table(&dive_table); - - for (i = 1; i < dive_table.nr; i++) { - struct dive **pp = &dive_table.dives[i-1]; - struct dive *prev = pp[0]; - struct dive *dive = pp[1]; - struct dive *merged; - - /* only try to merge overlapping dives - or if one of the dives has - * zero duration (that might be a gps marker from the webservice) */ - if (prev->duration.seconds && dive->duration.seconds && - prev->when + prev->duration.seconds < dive->when) - continue; - - merged = try_to_merge(prev, dive, prefer_imported); - if (!merged) - continue; - - /* careful - we might free the dive that last points to. Oops... */ - if (last == prev || last == dive) - last = merged; - - /* Redo the new 'i'th dive */ - i--; - add_single_dive(i, merged); - delete_single_dive(i+1); - delete_single_dive(i+1); - } - /* make sure no dives are still marked as downloaded */ - for (i = 1; i < dive_table.nr; i++) - dive_table.dives[i]->downloaded = FALSE; - - if (is_imported) { - /* If there are dives in the table, are they numbered */ - if (!last || last->number) - try_to_renumber(last, preexisting); - - /* did we add dives to the dive table? */ - if (preexisting != dive_table.nr) - mark_divelist_changed(TRUE); - } - dive_list_update_dives(); -} - static void parse_argument(const char *arg) { const char *p = arg+1; |