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