]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
e516d38c8bdafbc526d1f6a4f693b3b44ba9993e
[ext/subsurface.git] / main.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 #include "dive.h"
7 #include "divelist.h"
8 #include "display.h"
9
10 GtkWidget *main_window;
11 GtkWidget *main_vbox;
12 GtkWidget *error_info_bar;
13 GtkWidget *error_label;
14 int        error_count;
15 struct DiveList   dive_list;
16
17 struct units output_units;
18
19 static int sortfn(const void *_a, const void *_b)
20 {
21         const struct dive *a = *(void **)_a;
22         const struct dive *b = *(void **)_b;
23
24         if (a->when < b->when)
25                 return -1;
26         if (a->when > b->when)
27                 return 1;
28         return 0;
29 }
30
31 /*
32  * This doesn't really report anything at all. We just sort the
33  * dives, the GUI does the reporting
34  */
35 static void report_dives(void)
36 {
37         int i;
38
39         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
40
41         for (i = 1; i < dive_table.nr; i++) {
42                 struct dive **pp = &dive_table.dives[i-1];
43                 struct dive *prev = pp[0];
44                 struct dive *dive = pp[1];
45                 struct dive *merged;
46
47                 if (prev->when + prev->duration.seconds < dive->when)
48                         continue;
49
50                 merged = try_to_merge(prev, dive);
51                 if (!merged)
52                         continue;
53
54                 free(prev);
55                 free(dive);
56                 *pp = merged;
57                 dive_table.nr--;
58                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
59
60                 /* Redo the new 'i'th dive */
61                 i--;
62         }
63 }
64
65 static void parse_argument(const char *arg)
66 {
67         const char *p = arg+1;
68
69         do {
70                 switch (*p) {
71                 case 'v':
72                         verbose++;
73                         continue;
74                 default:
75                         fprintf(stderr, "Bad argument '%s'\n", arg);
76                         exit(1);
77                 }
78         } while (*++p);
79 }
80
81 static void on_destroy(GtkWidget* w, gpointer data)
82 {
83         gtk_main_quit();
84 }
85
86 static GtkWidget *dive_profile;
87
88 void repaint_dive(void)
89 {
90         update_dive_info(current_dive);
91         gtk_widget_queue_draw(dive_profile);
92 }
93
94 static char *existing_filename;
95
96 static void on_info_bar_response(GtkWidget *widget, gint response,
97                                  gpointer data)
98 {
99         if (response == GTK_RESPONSE_OK)
100         {
101                 gtk_widget_destroy(widget);
102                 error_info_bar = NULL;
103         }
104 }
105
106 static void report_error(GError* error)
107 {
108         if (error == NULL)
109         {
110                 return;
111         }
112         
113         if (error_info_bar == NULL)
114         {
115                 error_count = 1;
116                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
117                                                                GTK_RESPONSE_OK,
118                                                                NULL);
119                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
120                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
121                                               GTK_MESSAGE_ERROR);
122                 
123                 error_label = gtk_label_new(error->message);
124                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
125                 gtk_container_add(GTK_CONTAINER(container), error_label);
126                 
127                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
128                 gtk_widget_show_all(main_vbox);
129         }
130         else
131         {
132                 error_count++;
133                 char buffer[256];
134                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
135                 gtk_label_set(GTK_LABEL(error_label), buffer);
136         }
137 }
138
139 static void file_open(GtkWidget *w, gpointer data)
140 {
141         GtkWidget *dialog;
142         dialog = gtk_file_chooser_dialog_new("Open File",
143                 GTK_WINDOW(main_window),
144                 GTK_FILE_CHOOSER_ACTION_OPEN,
145                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
146                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
147                 NULL);
148         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
149
150         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
151                 GSList *filenames;
152                 char *filename;
153                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
154                 
155                 GError *error = NULL;
156                 while(filenames != NULL) {
157                         filename = (char *)filenames->data;
158                         parse_xml_file(filename, &error);
159                         if (error != NULL)
160                         {
161                                 report_error(error);
162                                 g_error_free(error);
163                                 error = NULL;
164                         }
165                         
166                         g_free(filename);
167                         filenames = g_slist_next(filenames);
168                 }
169                 g_slist_free(filenames);
170                 report_dives();
171                 dive_list_update_dives(dive_list);
172         }
173         gtk_widget_destroy(dialog);
174 }
175
176 static void file_save(GtkWidget *w, gpointer data)
177 {
178         GtkWidget *dialog;
179         dialog = gtk_file_chooser_dialog_new("Save File",
180                 GTK_WINDOW(main_window),
181                 GTK_FILE_CHOOSER_ACTION_SAVE,
182                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
183                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
184                 NULL);
185         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
186         if (!existing_filename) {
187                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
188         } else
189                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
190
191         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
192                 char *filename;
193                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
194                 save_dives(filename);
195                 g_free(filename);
196         }
197         gtk_widget_destroy(dialog);
198 }
199
200 static void quit(GtkWidget *w, gpointer data)
201 {
202         gtk_main_quit();
203 }
204
205 static void imperial(GtkWidget *w, gpointer data)
206 {
207         output_units = IMPERIAL_units;
208         repaint_dive();
209 }
210
211 static void metric(GtkWidget *w, gpointer data)
212 {
213         output_units = SI_units;
214         repaint_dive();
215 }
216
217 static GtkActionEntry menu_items[] = {
218         { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
219         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
220         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
221         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
222         { "Metric",         NULL, "Metric", NULL, NULL, G_CALLBACK(metric) },
223         { "Imperial",       NULL, "Imperial", NULL, NULL, G_CALLBACK(imperial) },
224 };
225 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
226
227 static const gchar* ui_string = " \
228         <ui> \
229                 <menubar name=\"MainMenu\"> \
230                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
231                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
232                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
233                                 <separator name=\"Seperator\"/> \
234                                 <menuitem name=\"Metric\" action=\"Metric\" /> \
235                                 <menuitem name=\"Imperial\" action=\"Imperial\" /> \
236                                 <separator name=\"Seperator\"/> \
237                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
238                         </menu> \
239                 </menubar> \
240         </ui> \
241 ";
242
243 static GtkWidget *get_menubar_menu(GtkWidget *window)
244 {
245         GtkActionGroup *action_group = gtk_action_group_new("Menu");
246         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
247
248         GtkUIManager *ui_manager = gtk_ui_manager_new();
249         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
250         GError* error = 0;
251         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
252
253         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
254         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
255
256         return menu;
257 }
258
259 int main(int argc, char **argv)
260 {
261         int i;
262         GtkWidget *win;
263         GtkWidget *paned;
264         GtkWidget *info_box;
265         GtkWidget *notebook;
266         GtkWidget *frame;
267         GtkWidget *dive_info;
268         GtkWidget *menubar;
269         GtkWidget *vbox;
270
271         output_units = SI_units;
272         parse_xml_init();
273
274         gtk_init(&argc, &argv);
275
276         error_info_bar = NULL;
277         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
278         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
279         main_window = win;
280
281         vbox = gtk_vbox_new(FALSE, 0);
282         gtk_container_add(GTK_CONTAINER(win), vbox);
283         main_vbox = vbox;
284
285         menubar = get_menubar_menu(win);
286         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
287
288         /* HPane for left the dive list, and right the dive info */
289         paned = gtk_hpaned_new();
290         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
291
292         /* Create the actual divelist */
293         dive_list = dive_list_create();
294         gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
295
296         /* VBox for dive info, and tabs */
297         info_box = gtk_vbox_new(FALSE, 6);
298         gtk_paned_add2(GTK_PANED(paned), info_box);
299
300         /* Frame for minimal dive info */
301         frame = dive_info_frame();
302         gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
303
304         /* Notebook for dive info vs profile vs .. */
305         notebook = gtk_notebook_new();
306         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
307
308         /* Frame for dive profile */
309         dive_profile = dive_profile_widget();
310         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
311
312         /* Frame for extended dive info */
313         dive_info = extended_dive_info_widget();
314         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Extended Dive Info"));
315
316         gtk_widget_set_app_paintable(win, TRUE);
317         gtk_widget_show_all(win);
318         
319         for (i = 1; i < argc; i++) {
320                 const char *a = argv[i];
321
322                 if (a[0] == '-') {
323                         parse_argument(a);
324                         continue;
325                 }
326                 GError *error = NULL;
327                 parse_xml_file(a, &error);
328                 
329                 if (error != NULL)
330                 {
331                         report_error(error);
332                         g_error_free(error);
333                         error = NULL;
334                 }
335         }
336
337         report_dives();
338         dive_list_update_dives(dive_list);
339
340         gtk_main();
341         return 0;
342 }