]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Add XML file import back and treat open and import differently
[ext/subsurface.git] / main.c
1 /* main.c */
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include <gconf/gconf-client.h>
8
9 #include "dive.h"
10 #include "divelist.h"
11
12 GConfClient *gconf;
13 struct units output_units;
14
15 #define GCONF_NAME(x) "/apps/subsurface/" #x
16
17 /* random helper functions, used here or elsewhere */
18 static int sortfn(const void *_a, const void *_b)
19 {
20         const struct dive *a = *(void **)_a;
21         const struct dive *b = *(void **)_b;
22
23         if (a->when < b->when)
24                 return -1;
25         if (a->when > b->when)
26                 return 1;
27         return 0;
28 }
29
30 const char *weekday(int wday)
31 {
32         static const char wday_array[7][4] = {
33                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
34         };
35         return wday_array[wday];
36 }
37
38 const char *monthname(int mon)
39 {
40         static const char month_array[12][4] = {
41                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
43         };
44         return month_array[mon];
45 }
46
47 /*
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.
50  *
51  * But we only do it if:
52  *
53  *  - the last dive in the old dive table was numbered
54  *
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.
58  *
59  *  - none of the new dives have any numbers
60  *
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.
67  */
68 static void try_to_renumber(struct dive *last, int preexisting)
69 {
70         int i, nr;
71
72         /*
73          * If the new dives aren't all strictly at the end,
74          * we're going to expect the user to do a manual
75          * renumbering.
76          */
77         if (get_dive(preexisting-1) != last)
78                 return;
79
80         /*
81          * If any of the new dives already had a number,
82          * we'll have to do a manual renumbering.
83          */
84         for (i = preexisting; i < dive_table.nr; i++) {
85                 struct dive *dive = get_dive(i);
86                 if (dive->number)
87                         return;
88         }
89
90         /*
91          * Ok, renumber..
92          */
93         nr = last->number;
94         for (i = preexisting; i < dive_table.nr; i++) {
95                 struct dive *dive = get_dive(i);
96                 dive->number = ++nr;
97         }
98 }
99
100 /*
101  * This doesn't really report anything at all. We just sort the
102  * dives, the GUI does the reporting
103  */
104 void report_dives(gboolean imported)
105 {
106         int i;
107         int preexisting = dive_table.preexisting;
108         struct dive *last;
109
110         /* This does the right thing for -1: NULL */
111         last = get_dive(preexisting-1);
112
113         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
114
115         for (i = 1; i < dive_table.nr; i++) {
116                 struct dive **pp = &dive_table.dives[i-1];
117                 struct dive *prev = pp[0];
118                 struct dive *dive = pp[1];
119                 struct dive *merged;
120
121                 if (prev->when + prev->duration.seconds < dive->when)
122                         continue;
123
124                 merged = try_to_merge(prev, dive);
125                 if (!merged)
126                         continue;
127
128                 free(prev);
129                 free(dive);
130                 *pp = merged;
131                 dive_table.nr--;
132                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
133
134                 /* Redo the new 'i'th dive */
135                 i--;
136         }
137
138         if (imported) {
139                 /* Was the previous dive table state numbered? */
140                 if (last && last->number)
141                         try_to_renumber(last, preexisting);
142
143                 /* did we have dives in the table and added more? */
144                 if (last && preexisting != dive_table.nr)
145                         mark_divelist_changed(TRUE);
146         }
147         dive_table.preexisting = dive_table.nr;
148         dive_list_update_dives();
149 }
150
151 static void parse_argument(const char *arg)
152 {
153         const char *p = arg+1;
154
155         do {
156                 switch (*p) {
157                 case 'v':
158                         verbose++;
159                         continue;
160                 case '-':
161                         /* long options with -- */
162                         if (strcmp(arg,"--import") == 0) {
163                                 /* mark the dives so far as the base,
164                                  * everything after is imported */
165                                 report_dives(TRUE);
166                                 return;
167                         }
168                         /* fallthrough */
169                 default:
170                         fprintf(stderr, "Bad argument '%s'\n", arg);
171                         exit(1);
172                 }
173         } while (*++p);
174 }
175
176 void update_dive(struct dive *new_dive)
177 {
178         static struct dive *buffered_dive;
179         struct dive *old_dive = buffered_dive;
180
181         if (old_dive) {
182                 flush_dive_info_changes(old_dive);
183                 flush_dive_equipment_changes(old_dive);
184                 flush_divelist(old_dive);
185         }
186         if (new_dive) {
187                 show_dive_info(new_dive);
188                 show_dive_equipment(new_dive);
189         }
190         buffered_dive = new_dive;
191 }
192
193 void renumber_dives(int nr)
194 {
195         int i;
196
197         for (i = 0; i < dive_table.nr; i++) {
198                 struct dive *dive = dive_table.dives[i];
199                 dive->number = nr + i;
200         }
201         mark_divelist_changed(TRUE);
202 }
203
204 int main(int argc, char **argv)
205 {
206         int i;
207
208         output_units = SI_units;
209
210         parse_xml_init();
211
212         init_ui(argc, argv);
213         
214         for (i = 1; i < argc; i++) {
215                 const char *a = argv[i];
216
217                 if (a[0] == '-') {
218                         parse_argument(a);
219                         continue;
220                 }
221                 GError *error = NULL;
222                 parse_xml_file(a, &error);
223                 
224                 if (error != NULL)
225                 {
226                         report_error(error);
227                         g_error_free(error);
228                         error = NULL;
229                 }
230         }
231
232         report_dives(FALSE);
233
234         run_ui();
235         return 0;
236 }