]> git.tdb.fi Git - ext/subsurface.git/blob - divelist.c
dc0e256a557c51b341fb9f22a7ef70e264868b87
[ext/subsurface.git] / divelist.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5
6 #include "divelist.h"
7 #include "dive.h"
8 #include "display.h"
9
10 /*
11  * The dive list has the dive data in both string format (for showing)
12  * and in "raw" format (for sorting purposes)
13  */
14 enum {
15         DIVE_INDEX = 0,
16         DIVE_DATESTR,           /* "Thu Jun 17th, 2011" or whatever */
17         DIVE_DATE,              /* time_t: dive->when */
18         DIVE_DEPTHSTR,          /* "67" in ft or whatever */
19         DIVE_DEPTH,             /* int: dive->maxdepth in mm */
20         DIVE_DURATIONSTR,       /* "47" in minutes */
21         DIVE_DURATION,          /* int: in seconds */
22         DIVE_TEMPSTR,           /* "78" in fahrenheit or whatever */
23         DIVE_TEMP,              /* int: in mkelvin */
24         DIVE_NITROXSTR,         /* "32.5" in percent */
25         DIVE_NITROX,            /* int: in permille */
26         DIVE_SACSTR,            /* "0.49" in cuft/min */
27         DIVE_SAC,               /* int: in ml/min or something */
28         DIVELIST_COLUMNS
29 };
30
31
32 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
33 {
34         GtkTreeIter iter;
35         GValue value = {0, };
36
37         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
38                 return;
39
40         gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
41         selected_dive = g_value_get_int(&value);
42         repaint_dive();
43 }
44
45 static const char *weekday(int wday)
46 {
47         static const char wday_array[7][4] = {
48                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
49         };
50         return wday_array[wday];
51 }
52
53 static const char *monthname(int mon)
54 {
55         static const char month_array[12][4] = {
56                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
57                 "Jul", "Aug", "Oct", "Sep", "Nov", "Dec",
58         };
59         return month_array[mon];
60 }
61
62 static void get_date(struct dive *dive, int *val, char **str)
63 {
64         struct tm *tm;
65         time_t when = dive->when;
66         char buffer[40];
67
68         /* 2038 problem */
69         *val = when;
70
71         tm = gmtime(&dive->when);
72         snprintf(buffer, sizeof(buffer),
73                 "%s, %s %d, %d %02d:%02d",
74                 weekday(tm->tm_wday),
75                 monthname(tm->tm_mon),
76                 tm->tm_mday, tm->tm_year + 1900,
77                 tm->tm_hour, tm->tm_min);
78         *str = strdup(buffer);
79 }
80
81 static void get_depth(struct dive *dive, int *val, char **str)
82 {
83         int len;
84         int depth = dive->maxdepth.mm;
85         int integer, frac = -1;
86         char buffer[10];
87
88         *val = depth;
89         *str = "";
90         switch (output_units.length) {
91         case METERS:
92                 /* To tenths of meters */
93                 depth = (depth + 49) / 100;
94                 integer = depth / 10;
95                 frac = depth % 10;
96                 if (integer < 20)
97                         break;
98                 frac = -1;
99                 /* Rounding? */
100                 break;
101         case FEET:
102                 integer = to_feet(dive->maxdepth);
103                 frac = -1;
104                 break;
105         default:
106                 return;
107         }
108         len = snprintf(buffer, sizeof(buffer),
109                        "%d", integer);
110         if (frac >= 0)
111                 len += snprintf(buffer+len, sizeof(buffer)-len,
112                         ".%d", frac);
113         *str = strdup(buffer);
114 }
115
116 static void get_duration(struct dive *dive, int *val, char **str)
117 {
118         unsigned int sec = dive->duration.seconds;
119         char buffer[16];
120
121         *val = sec;
122         snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
123         *str = strdup(buffer);
124 }
125
126 static void get_temp(struct dive *dive, int *val, char **str)
127 {
128         int value = dive->watertemp.mkelvin;
129         char buffer[80];
130
131         *val = value;
132         *str = "";
133         if (value) {
134                 double deg;
135                 switch (output_units.temperature) {
136                 case CELSIUS:
137                         deg = mkelvin_to_C(value);
138                         break;
139                 case FAHRENHEIT:
140                         deg = mkelvin_to_F(value);
141                         break;
142                 default:
143                         return;
144                 }
145                 snprintf(buffer, sizeof(buffer), "%.1f", deg);
146                 *str = strdup(buffer);
147         }
148 }
149
150 static void get_nitrox(struct dive *dive, int *val, char **str)
151 {
152         int value = dive->cylinder[0].gasmix.o2.permille;
153         char buffer[80];
154
155         *val = value;
156         *str = "";
157         if (value) {
158                 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
159                 *str = strdup(buffer);
160         }
161 }
162
163 static void get_sac(struct dive *dive, int *val, char **str)
164 {
165         *val = 0;
166         *str = "";
167 }
168
169 static gboolean set_one_dive(GtkTreeModel *model,
170                              GtkTreePath *path,
171                              GtkTreeIter *iter,
172                              gpointer data)
173 {
174         GValue value = {0, };
175         struct dive *dive;
176         int date, depth, duration, temp, nitrox, sac;
177         char *datestr, *depthstr, *durationstr, *tempstr, *nitroxstr, *sacstr;
178
179         /* Get the dive number */
180         gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
181         dive = get_dive(g_value_get_int(&value));
182         if (!dive)
183                 return TRUE;
184
185         get_date(dive, &date, &datestr);
186         get_depth(dive, &depth, &depthstr);
187         get_duration(dive, &duration, &durationstr);
188         get_temp(dive, &temp, &tempstr);
189         get_nitrox(dive, &nitrox, &nitroxstr);
190         get_sac(dive, &sac, &sacstr);
191
192         /*
193          * We only set the fields that changed: the strings.
194          * The core data itself is unaffected by units
195          */
196         gtk_list_store_set(GTK_LIST_STORE(model), iter,
197                 DIVE_DATESTR, datestr,
198                 DIVE_DEPTHSTR, depthstr,
199                 DIVE_DURATIONSTR, durationstr,
200                 DIVE_TEMPSTR, tempstr,
201                 DIVE_TEMP, temp,
202                 DIVE_NITROXSTR, nitroxstr,
203                 DIVE_NITROX, nitrox,
204                 DIVE_SACSTR, sacstr,
205                 DIVE_NITROX, sac,
206                 -1);
207
208         return FALSE;
209 }
210
211 void update_dive_list_units(struct DiveList *dive_list)
212 {
213         const char *unit;
214         GtkTreeModel *model = GTK_TREE_MODEL(dive_list->model);
215
216         switch (output_units.length) {
217         case METERS:
218                 unit = "m";
219                 break;
220         case FEET:
221                 unit = "ft";
222                 break;
223         }
224         gtk_tree_view_column_set_title(dive_list->depth, unit);
225
226         gtk_tree_model_foreach(model, set_one_dive, NULL);
227 }
228
229 static void fill_dive_list(struct DiveList *dive_list)
230 {
231         int i;
232         GtkTreeIter iter;
233         GtkListStore *store;
234
235         store = GTK_LIST_STORE(dive_list->model);
236
237         for (i = 0; i < dive_table.nr; i++) {
238                 struct dive *dive = dive_table.dives[i];
239
240                 gtk_list_store_append(store, &iter);
241                 gtk_list_store_set(store, &iter,
242                         DIVE_INDEX, i,
243                         DIVE_DATESTR, "date",
244                         DIVE_DATE, dive->when,
245                         DIVE_DEPTHSTR, "depth",
246                         DIVE_DEPTH, dive->maxdepth,
247                         DIVE_DURATIONSTR, "duration",
248                         DIVE_DURATION, dive->duration.seconds,
249                         DIVE_TEMPSTR, "temp",
250                         DIVE_TEMP, dive->watertemp.mkelvin,
251                         DIVE_NITROXSTR, "21.0",
252                         DIVE_NITROX, dive->cylinder[0].gasmix.o2,
253                         DIVE_SACSTR, "sac",
254                         DIVE_SAC, 0,
255                         -1);
256         }
257
258         update_dive_list_units(dive_list);
259 }
260
261 void dive_list_update_dives(struct DiveList dive_list)
262 {
263         gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
264         fill_dive_list(&dive_list);
265         repaint_dive();
266 }
267
268 struct DiveList dive_list_create(void)
269 {
270         struct DiveList    dive_list;
271         GtkTreeSelection  *selection;
272         GtkCellRenderer   *renderer;
273         GtkTreeViewColumn *col;
274
275         dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
276                                 G_TYPE_INT,                     /* index */
277                                 G_TYPE_STRING, G_TYPE_INT,      /* Date */
278                                 G_TYPE_STRING, G_TYPE_INT,      /* Depth */
279                                 G_TYPE_STRING, G_TYPE_INT,      /* Duration */
280                                 G_TYPE_STRING, G_TYPE_INT,      /* Temperature */
281                                 G_TYPE_STRING, G_TYPE_INT,      /* Nitrox */
282                                 G_TYPE_STRING, G_TYPE_INT       /* SAC */
283                                 );
284         dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
285         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
286
287         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
288         gtk_widget_set_size_request(dive_list.tree_view, 200, 100);
289
290         renderer = gtk_cell_renderer_text_new();
291         dive_list.date = col = gtk_tree_view_column_new();
292         gtk_tree_view_column_set_title(col, "Date");
293         gtk_tree_view_column_set_sort_column_id(col, DIVE_DATE);
294         gtk_tree_view_column_set_resizable (col, TRUE);
295         gtk_tree_view_column_pack_start(col, renderer, TRUE);
296         gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DATESTR);
297         gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
298
299         renderer = gtk_cell_renderer_text_new();
300         dive_list.depth = col = gtk_tree_view_column_new();
301         gtk_tree_view_column_set_title(col, "ft");
302         gtk_tree_view_column_set_sort_column_id(col, DIVE_DEPTH);
303         gtk_tree_view_column_pack_start(col, renderer, FALSE);
304         gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DEPTHSTR);
305         gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
306         gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
307         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
308
309         renderer = gtk_cell_renderer_text_new();
310         dive_list.duration = col = gtk_tree_view_column_new();
311         gtk_tree_view_column_set_title(col, "min");
312         gtk_tree_view_column_set_sort_column_id(col, DIVE_DURATION);
313         gtk_tree_view_column_pack_start(col, renderer, FALSE);
314         gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DURATIONSTR);
315         gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
316         gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
317         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
318
319         renderer = gtk_cell_renderer_text_new();
320         dive_list.temperature = col = gtk_tree_view_column_new();
321         gtk_tree_view_column_set_title(col, "deg");
322         gtk_tree_view_column_set_sort_column_id(col, DIVE_TEMP);
323         gtk_tree_view_column_pack_start(col, renderer, FALSE);
324         gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_TEMPSTR);
325         gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
326         gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
327         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
328
329         renderer = gtk_cell_renderer_text_new();
330         dive_list.nitrox = col = gtk_tree_view_column_new();
331         gtk_tree_view_column_set_title(col, "EAD");
332         gtk_tree_view_column_set_sort_column_id(col, DIVE_NITROX);
333         gtk_tree_view_column_pack_start(col, renderer, FALSE);
334         gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_NITROXSTR);
335         gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
336         gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
337         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
338
339         renderer = gtk_cell_renderer_text_new();
340         dive_list.sac = col = gtk_tree_view_column_new();
341         gtk_tree_view_column_set_title(col, "SAC");
342         gtk_tree_view_column_set_sort_column_id(col, DIVE_SAC);
343         gtk_tree_view_column_pack_start(col, renderer, FALSE);
344         gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_SACSTR);
345         gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
346         gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
347         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
348
349         fill_dive_list(&dive_list);
350
351         g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
352                                           "search-column", 0,
353                                           "rules-hint", TRUE,
354                                           NULL);
355
356         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
357
358         dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
359         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
360                                GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
361         gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
362
363         return dive_list;
364 }