7 #include <gconf/gconf-client.h>
13 struct units output_units;
15 #define GCONF_NAME(x) "/apps/subsurface/" #x
17 /* random helper functions, used here or elsewhere */
18 static int sortfn(const void *_a, const void *_b)
20 const struct dive *a = *(void **)_a;
21 const struct dive *b = *(void **)_b;
23 if (a->when < b->when)
25 if (a->when > b->when)
30 const char *weekday(int wday)
32 static const char wday_array[7][4] = {
33 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
35 return wday_array[wday];
38 const char *monthname(int mon)
40 static const char month_array[12][4] = {
41 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
44 return month_array[mon];
48 * When adding dives to the dive table, we try to renumber
49 * the new dives based on any old dives in the dive table.
51 * But we only do it if:
53 * - the last dive in the old dive table was numbered
55 * - all the new dives are strictly at the end (so the
56 * "last dive" is at the same location in the dive table
57 * after re-sorting the dives.
59 * - none of the new dives have any numbers
61 * This catches the common case of importing new dives from
62 * a dive computer, and gives them proper numbers based on
63 * your old dive list. But it tries to be very conservative
64 * and not give numbers if there is *any* question about
65 * what the numbers should be - in which case you need to do
66 * a manual re-numbering.
68 static void try_to_renumber(struct dive *last, int preexisting)
73 * If the new dives aren't all strictly at the end,
74 * we're going to expect the user to do a manual
77 if (get_dive(preexisting-1) != last)
81 * If any of the new dives already had a number,
82 * we'll have to do a manual renumbering.
84 for (i = preexisting; i < dive_table.nr; i++) {
85 struct dive *dive = get_dive(i);
94 for (i = preexisting; i < dive_table.nr; i++) {
95 struct dive *dive = get_dive(i);
101 * track whether we switched to importing dives
103 static gboolean imported = FALSE;
106 * This doesn't really report anything at all. We just sort the
107 * dives, the GUI does the reporting
109 void report_dives(gboolean imported)
112 int preexisting = dive_table.preexisting;
115 /* This does the right thing for -1: NULL */
116 last = get_dive(preexisting-1);
118 qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
120 for (i = 1; i < dive_table.nr; i++) {
121 struct dive **pp = &dive_table.dives[i-1];
122 struct dive *prev = pp[0];
123 struct dive *dive = pp[1];
126 if (prev->when + prev->duration.seconds < dive->when)
129 merged = try_to_merge(prev, dive);
137 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
139 /* Redo the new 'i'th dive */
144 /* Was the previous dive table state numbered? */
145 if (last && last->number)
146 try_to_renumber(last, preexisting);
148 /* did we have dives in the table and added more? */
149 if (last && preexisting != dive_table.nr)
150 mark_divelist_changed(TRUE);
152 dive_table.preexisting = dive_table.nr;
153 dive_list_update_dives();
156 static void parse_argument(const char *arg)
158 const char *p = arg+1;
166 /* long options with -- */
167 if (strcmp(arg,"--import") == 0) {
168 /* mark the dives so far as the base,
169 * everything after is imported */
176 fprintf(stderr, "Bad argument '%s'\n", arg);
182 void update_dive(struct dive *new_dive)
184 static struct dive *buffered_dive;
185 struct dive *old_dive = buffered_dive;
188 flush_dive_info_changes(old_dive);
189 flush_dive_equipment_changes(old_dive);
190 flush_divelist(old_dive);
193 show_dive_info(new_dive);
194 show_dive_equipment(new_dive);
196 buffered_dive = new_dive;
199 void renumber_dives(int nr)
203 for (i = 0; i < dive_table.nr; i++) {
204 struct dive *dive = dive_table.dives[i];
205 dive->number = nr + i;
207 mark_divelist_changed(TRUE);
210 int main(int argc, char **argv)
214 output_units = SI_units;
220 for (i = 1; i < argc; i++) {
221 const char *a = argv[i];
227 GError *error = NULL;
228 parse_xml_file(a, &error);
238 report_dives(imported);