]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Merge git://github.com/sirowain/subsurface
[ext/subsurface.git] / main.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 #include <gconf/gconf-client.h>
7
8 #include "dive.h"
9 #include "divelist.h"
10 #include "display.h"
11
12 GtkWidget *main_window;
13 GtkWidget *main_vbox;
14 GtkWidget *error_info_bar;
15 GtkWidget *error_label;
16 int        error_count;
17
18 GConfClient *gconf;
19 struct units output_units;
20
21 #define GCONF_NAME(x) "/apps/subsurface/" #x
22
23 static int sortfn(const void *_a, const void *_b)
24 {
25         const struct dive *a = *(void **)_a;
26         const struct dive *b = *(void **)_b;
27
28         if (a->when < b->when)
29                 return -1;
30         if (a->when > b->when)
31                 return 1;
32         return 0;
33 }
34
35 /*
36  * This doesn't really report anything at all. We just sort the
37  * dives, the GUI does the reporting
38  */
39 void report_dives(void)
40 {
41         int i;
42
43         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
44
45         for (i = 1; i < dive_table.nr; i++) {
46                 struct dive **pp = &dive_table.dives[i-1];
47                 struct dive *prev = pp[0];
48                 struct dive *dive = pp[1];
49                 struct dive *merged;
50
51                 if (prev->when + prev->duration.seconds < dive->when)
52                         continue;
53
54                 merged = try_to_merge(prev, dive);
55                 if (!merged)
56                         continue;
57
58                 free(prev);
59                 free(dive);
60                 *pp = merged;
61                 dive_table.nr--;
62                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
63
64                 /* Redo the new 'i'th dive */
65                 i--;
66         }
67 }
68
69 static void parse_argument(const char *arg)
70 {
71         const char *p = arg+1;
72
73         do {
74                 switch (*p) {
75                 case 'v':
76                         verbose++;
77                         continue;
78                 default:
79                         fprintf(stderr, "Bad argument '%s'\n", arg);
80                         exit(1);
81                 }
82         } while (*++p);
83 }
84
85 static void on_destroy(GtkWidget* w, gpointer data)
86 {
87         gtk_main_quit();
88 }
89
90 static GtkWidget *dive_profile;
91
92 void update_dive(struct dive *new_dive)
93 {
94         static struct dive *buffered_dive;
95         struct dive *old_dive = buffered_dive;
96
97         if (old_dive) {
98                 flush_dive_info_changes(old_dive);
99                 flush_dive_equipment_changes(old_dive);
100                 flush_divelist(old_dive);
101         }
102         if (new_dive) {
103                 show_dive_info(new_dive);
104                 show_dive_equipment(new_dive);
105         }
106         buffered_dive = new_dive;
107 }
108
109 void repaint_dive(void)
110 {
111         update_dive(current_dive);
112         gtk_widget_queue_draw(dive_profile);
113 }
114
115 static char *existing_filename;
116
117 static void on_info_bar_response(GtkWidget *widget, gint response,
118                                  gpointer data)
119 {
120         if (response == GTK_RESPONSE_OK)
121         {
122                 gtk_widget_destroy(widget);
123                 error_info_bar = NULL;
124         }
125 }
126
127 void report_error(GError* error)
128 {
129         if (error == NULL)
130         {
131                 return;
132         }
133         
134         if (error_info_bar == NULL)
135         {
136                 error_count = 1;
137                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
138                                                                GTK_RESPONSE_OK,
139                                                                NULL);
140                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
141                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
142                                               GTK_MESSAGE_ERROR);
143                 
144                 error_label = gtk_label_new(error->message);
145                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
146                 gtk_container_add(GTK_CONTAINER(container), error_label);
147                 
148                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
149                 gtk_widget_show_all(main_vbox);
150         }
151         else
152         {
153                 error_count++;
154                 char buffer[256];
155                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
156                 gtk_label_set(GTK_LABEL(error_label), buffer);
157         }
158 }
159
160 static void file_open(GtkWidget *w, gpointer data)
161 {
162         GtkWidget *dialog;
163         dialog = gtk_file_chooser_dialog_new("Open File",
164                 GTK_WINDOW(main_window),
165                 GTK_FILE_CHOOSER_ACTION_OPEN,
166                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
167                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
168                 NULL);
169         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
170
171         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
172                 GSList *filenames;
173                 char *filename;
174                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
175                 
176                 GError *error = NULL;
177                 while(filenames != NULL) {
178                         filename = (char *)filenames->data;
179                         parse_xml_file(filename, &error);
180                         if (error != NULL)
181                         {
182                                 report_error(error);
183                                 g_error_free(error);
184                                 error = NULL;
185                         }
186                         
187                         g_free(filename);
188                         filenames = g_slist_next(filenames);
189                 }
190                 g_slist_free(filenames);
191                 report_dives();
192                 dive_list_update_dives();
193         }
194         gtk_widget_destroy(dialog);
195 }
196
197 static void file_save(GtkWidget *w, gpointer data)
198 {
199         GtkWidget *dialog;
200         dialog = gtk_file_chooser_dialog_new("Save File",
201                 GTK_WINDOW(main_window),
202                 GTK_FILE_CHOOSER_ACTION_SAVE,
203                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
204                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
205                 NULL);
206         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
207         if (!existing_filename) {
208                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
209         } else
210                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
211
212         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
213                 char *filename;
214                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
215                 save_dives(filename);
216                 g_free(filename);
217         }
218         gtk_widget_destroy(dialog);
219 }
220
221 static void quit(GtkWidget *w, gpointer data)
222 {
223         gtk_main_quit();
224 }
225
226 static void create_radio(GtkWidget *dialog, const char *name, ...)
227 {
228         va_list args;
229         GtkRadioButton *group = NULL;
230         GtkWidget *box, *label;
231
232         box = gtk_hbox_new(TRUE, 10);
233         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), box);
234
235         label = gtk_label_new(name);
236         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
237
238         va_start(args, name);
239         for (;;) {
240                 int enabled;
241                 const char *name;
242                 GtkWidget *button;
243                 void *callback_fn;
244
245                 name = va_arg(args, char *);
246                 if (!name)
247                         break;
248                 callback_fn = va_arg(args, void *);
249                 enabled = va_arg(args, int);
250
251                 button = gtk_radio_button_new_with_label_from_widget(group, name);
252                 group = GTK_RADIO_BUTTON(button);
253                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
254                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
255                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
256         }
257         va_end(args);
258 }
259
260 #define UNITCALLBACK(name, type, value)                         \
261 static void name(GtkWidget *w, gpointer data)                   \
262 {                                                               \
263         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
264                 menu_units.type = value;                        \
265 }
266
267 static struct units menu_units;
268
269 UNITCALLBACK(set_meter, length, METERS)
270 UNITCALLBACK(set_feet, length, FEET)
271 UNITCALLBACK(set_bar, pressure, BAR)
272 UNITCALLBACK(set_psi, pressure, PSI)
273 UNITCALLBACK(set_liter, volume, LITER)
274 UNITCALLBACK(set_cuft, volume, CUFT)
275 UNITCALLBACK(set_celsius, temperature, CELSIUS)
276 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
277
278 static void unit_dialog(GtkWidget *w, gpointer data)
279 {
280         int result;
281         GtkWidget *dialog;
282
283         menu_units = output_units;
284
285         dialog = gtk_dialog_new_with_buttons("Units",
286                 GTK_WINDOW(main_window),
287                 GTK_DIALOG_DESTROY_WITH_PARENT,
288                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
289                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
290                 NULL);
291
292         create_radio(dialog, "Depth:",
293                 "Meter", set_meter, (output_units.length == METERS),
294                 "Feet",  set_feet, (output_units.length == FEET),
295                 NULL);
296
297         create_radio(dialog, "Pressure:",
298                 "Bar", set_bar, (output_units.pressure == BAR),
299                 "PSI",  set_psi, (output_units.pressure == PSI),
300                 NULL);
301
302         create_radio(dialog, "Volume:",
303                 "Liter",  set_liter, (output_units.volume == LITER),
304                 "CuFt", set_cuft, (output_units.volume == CUFT),
305                 NULL);
306
307         create_radio(dialog, "Temperature:",
308                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
309                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
310                 NULL);
311
312         gtk_widget_show_all(dialog);
313         result = gtk_dialog_run(GTK_DIALOG(dialog));
314         if (result == GTK_RESPONSE_ACCEPT) {
315                 /* Make sure to flush any modified old dive data with old units */
316                 update_dive(NULL);
317                 output_units = menu_units;
318                 update_dive_list_units();
319                 repaint_dive();
320                 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
321                 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
322                 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
323                 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
324         }
325         gtk_widget_destroy(dialog);
326 }
327
328 static void renumber_dives(int nr)
329 {
330         int i;
331
332         for (i = 0; i < dive_table.nr; i++) {
333                 struct dive *dive = dive_table.dives[i];
334                 dive->number = nr + i;
335         }
336 }
337
338 static void renumber_dialog(GtkWidget *w, gpointer data)
339 {
340         int result;
341         GtkWidget *dialog, *frame, *button;
342
343         dialog = gtk_dialog_new_with_buttons("Renumber",
344                 GTK_WINDOW(main_window),
345                 GTK_DIALOG_DESTROY_WITH_PARENT,
346                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
347                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
348                 NULL);
349
350         frame = gtk_frame_new("New starting number");
351         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
352
353         button = gtk_spin_button_new_with_range(1, 50000, 1);
354         gtk_container_add(GTK_CONTAINER(frame), button);
355
356         gtk_widget_show_all(dialog);
357         result = gtk_dialog_run(GTK_DIALOG(dialog));
358         if (result == GTK_RESPONSE_ACCEPT) {
359                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
360                 renumber_dives(nr);
361                 repaint_dive();
362         }
363         gtk_widget_destroy(dialog);
364 }
365
366 static GtkActionEntry menu_items[] = {
367         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
368         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
369         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
370         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
371         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
372         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
373         { "Units",          NULL, "Units",    NULL, NULL, G_CALLBACK(unit_dialog) },
374         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
375         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
376 };
377 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
378
379 static const gchar* ui_string = " \
380         <ui> \
381                 <menubar name=\"MainMenu\"> \
382                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
383                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
384                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
385                                 <menuitem name=\"Print\" action=\"Print\" /> \
386                                 <separator name=\"Separator1\"/> \
387                                 <menuitem name=\"Import\" action=\"Import\" /> \
388                                 <separator name=\"Separator2\"/> \
389                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
390                         </menu> \
391                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
392                                 <menuitem name=\"Units\" action=\"Units\" /> \
393                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
394                         </menu> \
395                 </menubar> \
396         </ui> \
397 ";
398
399 static GtkWidget *get_menubar_menu(GtkWidget *window)
400 {
401         GtkActionGroup *action_group = gtk_action_group_new("Menu");
402         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
403
404         GtkUIManager *ui_manager = gtk_ui_manager_new();
405         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
406         GError* error = 0;
407         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
408
409         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
410         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
411
412         return menu;
413 }
414
415 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
416 {
417         repaint_dive();
418 }
419
420 int main(int argc, char **argv)
421 {
422         int i;
423         GtkWidget *win;
424         GtkWidget *paned;
425         GtkWidget *info_box;
426         GtkWidget *notebook;
427         GtkWidget *dive_info;
428         GtkWidget *dive_list;
429         GtkWidget *equipment;
430         GtkWidget *menubar;
431         GtkWidget *vbox;
432
433         output_units = SI_units;
434         parse_xml_init();
435
436         gtk_init(&argc, &argv);
437
438         g_type_init();
439         gconf = gconf_client_get_default();
440
441         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
442                 output_units.length = FEET;
443         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
444                 output_units.pressure = PSI;
445         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
446                 output_units.volume = CUFT;
447         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
448                 output_units.temperature = FAHRENHEIT;
449
450         error_info_bar = NULL;
451         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
452         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
453         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
454         main_window = win;
455
456         vbox = gtk_vbox_new(FALSE, 0);
457         gtk_container_add(GTK_CONTAINER(win), vbox);
458         main_vbox = vbox;
459
460         menubar = get_menubar_menu(win);
461         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
462
463         /* HPane for left the dive list, and right the dive info */
464         paned = gtk_vpaned_new();
465         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
466
467         /* Create the actual divelist */
468         dive_list = dive_list_create();
469         gtk_paned_add2(GTK_PANED(paned), dive_list);
470
471         /* VBox for dive info, and tabs */
472         info_box = gtk_vbox_new(FALSE, 6);
473         gtk_paned_add1(GTK_PANED(paned), info_box);
474
475         /* Notebook for dive info vs profile vs .. */
476         notebook = gtk_notebook_new();
477         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
478         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
479
480         /* Frame for dive profile */
481         dive_profile = dive_profile_widget();
482         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
483
484         /* Frame for extended dive info */
485         dive_info = extended_dive_info_widget();
486         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
487
488         /* Frame for dive equipment */
489         equipment = equipment_widget();
490         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
491
492         gtk_widget_set_app_paintable(win, TRUE);
493         gtk_widget_show_all(win);
494         
495         for (i = 1; i < argc; i++) {
496                 const char *a = argv[i];
497
498                 if (a[0] == '-') {
499                         parse_argument(a);
500                         continue;
501                 }
502                 GError *error = NULL;
503                 parse_xml_file(a, &error);
504                 
505                 if (error != NULL)
506                 {
507                         report_error(error);
508                         g_error_free(error);
509                         error = NULL;
510                 }
511         }
512
513         report_dives();
514         dive_list_update_dives();
515
516         gtk_main();
517         return 0;
518 }