]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Add preference option to chose if SAC and/or OTU should be in divelist
[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  * This doesn't really report anything at all. We just sort the
49  * dives, the GUI does the reporting
50  */
51 void report_dives(void)
52 {
53         int i;
54
55         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
56
57         for (i = 1; i < dive_table.nr; i++) {
58                 struct dive **pp = &dive_table.dives[i-1];
59                 struct dive *prev = pp[0];
60                 struct dive *dive = pp[1];
61                 struct dive *merged;
62
63                 if (prev->when + prev->duration.seconds < dive->when)
64                         continue;
65
66                 merged = try_to_merge(prev, dive);
67                 if (!merged)
68                         continue;
69
70                 free(prev);
71                 free(dive);
72                 *pp = merged;
73                 dive_table.nr--;
74                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
75
76                 /* Redo the new 'i'th dive */
77                 i--;
78         }
79 }
80
81 static void parse_argument(const char *arg)
82 {
83         const char *p = arg+1;
84
85         do {
86                 switch (*p) {
87                 case 'v':
88                         verbose++;
89                         continue;
90                 default:
91                         fprintf(stderr, "Bad argument '%s'\n", arg);
92                         exit(1);
93                 }
94         } while (*++p);
95 }
96
97 void update_dive(struct dive *new_dive)
98 {
99         static struct dive *buffered_dive;
100         struct dive *old_dive = buffered_dive;
101
102         if (old_dive) {
103                 flush_dive_info_changes(old_dive);
104                 flush_dive_equipment_changes(old_dive);
105                 flush_divelist(old_dive);
106         }
107         if (new_dive) {
108                 show_dive_info(new_dive);
109                 show_dive_equipment(new_dive);
110         }
111         buffered_dive = new_dive;
112 }
113
114 void renumber_dives(int nr)
115 {
116         int i;
117
118         for (i = 0; i < dive_table.nr; i++) {
119                 struct dive *dive = dive_table.dives[i];
120                 dive->number = nr + i;
121         }
122 }
123
124 int main(int argc, char **argv)
125 {
126         int i;
127
128         output_units = SI_units;
129
130         parse_xml_init();
131
132         init_ui(argc, argv);
133         
134         for (i = 1; i < argc; i++) {
135                 const char *a = argv[i];
136
137                 if (a[0] == '-') {
138                         parse_argument(a);
139                         continue;
140                 }
141                 GError *error = NULL;
142                 parse_xml_file(a, &error);
143                 
144                 if (error != NULL)
145                 {
146                         report_error(error);
147                         g_error_free(error);
148                         error = NULL;
149                 }
150         }
151
152         report_dives();
153         dive_list_update_dives();
154
155         run_ui();
156         return 0;
157 }