From 21204926dfda996c47b4d638e30fb0b1f9968c63 Mon Sep 17 00:00:00 2001 From: Nathan Samson Date: Mon, 5 Sep 2011 21:12:58 +0200 Subject: [PATCH 1/3] Open File works. I refactored the code and introduced a new type. I never used it as a pointer (their was no real reason), but I'm not really satisfied. Signed-off-by: Nathan Samson --- Makefile | 8 ++++---- display.h | 4 ---- divelist.c | 41 +++++++++++++++++++++++------------------ divelist.h | 18 ++++++++++++++++++ info.c | 1 + main.c | 11 +++++++---- profile.c | 1 + 7 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 divelist.h diff --git a/Makefile b/Makefile index 8b5fd220f..00658b6a0 100644 --- a/Makefile +++ b/Makefile @@ -17,14 +17,14 @@ save-xml.o: save-xml.c dive.h dive.o: dive.c dive.h $(CC) $(CFLAGS) -c dive.c -main.o: main.c dive.h display.h +main.o: main.c dive.h display.h divelist.h $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c main.c -profile.o: profile.c dive.h display.h +profile.o: profile.c dive.h display.h divelist.h $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c profile.c -info.o: info.c dive.h display.h +info.o: info.c dive.h display.h divelist.h $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c info.c -divelist.o: divelist.c dive.h display.h +divelist.o: divelist.c dive.h display.h divelist.h $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c divelist.c diff --git a/display.h b/display.h index 96fa29a5d..e2e217ff0 100644 --- a/display.h +++ b/display.h @@ -5,13 +5,9 @@ #include #include -extern int selected_dive; -#define current_dive (get_dive(selected_dive)) - extern GtkWidget *dive_profile_widget(void); extern GtkWidget *dive_info_frame(void); extern GtkWidget *extended_dive_info_widget(void); -extern GtkWidget *create_dive_list(void); extern void update_dive_info(struct dive *dive); extern void repaint_dive(void); diff --git a/divelist.c b/divelist.c index b2ad39d0e..5dc45657b 100644 --- a/divelist.c +++ b/divelist.c @@ -3,6 +3,7 @@ #include #include +#include "divelist.h" #include "dive.h" #include "display.h" @@ -62,27 +63,31 @@ static void fill_dive_list(GtkListStore *store) } } -GtkWidget *create_dive_list(void) +void dive_list_update_dives(struct DiveList dive_list) { - GtkListStore *model; - GtkWidget *tree_view; + gtk_list_store_clear(GTK_LIST_STORE(dive_list.model)); + fill_dive_list(GTK_LIST_STORE(dive_list.model)); +} + +struct DiveList dive_list_create(void) +{ + struct DiveList dive_list; GtkTreeSelection *selection; GtkCellRenderer *renderer; GtkTreeViewColumn *col; - GtkWidget *scroll_window; - model = gtk_list_store_new(7, + dive_list.model = gtk_list_store_new(7, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT); - tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); - selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)); + dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model)); + selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view)); gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE); - gtk_widget_set_size_request(tree_view, 200, 100); + gtk_widget_set_size_request(dive_list.tree_view, 200, 100); - fill_dive_list(model); + fill_dive_list(dive_list.model); renderer = gtk_cell_renderer_text_new(); col = gtk_tree_view_column_new(); @@ -91,7 +96,7 @@ GtkWidget *create_dive_list(void) gtk_tree_view_column_set_resizable (col, TRUE); gtk_tree_view_column_pack_start(col, renderer, TRUE); gtk_tree_view_column_add_attribute(col, renderer, "text", 0); - gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col); + gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col); renderer = gtk_cell_renderer_text_new(); col = gtk_tree_view_column_new(); @@ -99,7 +104,7 @@ GtkWidget *create_dive_list(void) gtk_tree_view_column_set_sort_column_id(col, 3); gtk_tree_view_column_pack_start(col, renderer, FALSE); gtk_tree_view_column_add_attribute(col, renderer, "text", 2); - gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col); + gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col); gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL); gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5); @@ -109,21 +114,21 @@ GtkWidget *create_dive_list(void) gtk_tree_view_column_set_sort_column_id(col, 5); gtk_tree_view_column_pack_start(col, renderer, FALSE); gtk_tree_view_column_add_attribute(col, renderer, "text", 4); - gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col); + gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col); gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL); gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5); - g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE, + g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE, "search-column", 0, "rules-hint", TRUE, NULL); - g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model); + g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model); - scroll_window = gtk_scrolled_window_new(NULL, NULL); - gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), + dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); - gtk_container_add(GTK_CONTAINER(scroll_window), tree_view); + gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view); - return scroll_window; + return dive_list; } diff --git a/divelist.h b/divelist.h new file mode 100644 index 000000000..dc573d770 --- /dev/null +++ b/divelist.h @@ -0,0 +1,18 @@ +#ifndef DIVELIST_H +#define DIVELIST_H + +#include + +struct DiveList { + GtkWidget *tree_view; + GtkWidget *container_widget; + GtkListStore *model; +}; + +extern int selected_dive; +#define current_dive (get_dive(selected_dive)) + +extern struct DiveList dive_list_create(void); +extern void dive_list_update_dives(struct DiveList); + +#endif diff --git a/info.c b/info.c index c3b514091..94dcd3485 100644 --- a/info.c +++ b/info.c @@ -5,6 +5,7 @@ #include "dive.h" #include "display.h" +#include "divelist.h" static GtkWidget *divedate, *divetime, *depth, *duration, *temperature, *locationnote; static GtkEntry *location; diff --git a/main.c b/main.c index 269218518..83794ca06 100644 --- a/main.c +++ b/main.c @@ -4,9 +4,11 @@ #include #include "dive.h" +#include "divelist.h" #include "display.h" GtkWidget *main_window; +struct DiveList dive_list; static int sortfn(const void *_a, const void *_b) { @@ -98,8 +100,10 @@ static void file_open(GtkWidget *w, gpointer data) if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char *filename; filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); - printf("Open: '%s'\n", filename); + parse_xml_file(filename); g_free(filename); + report_dives(); + dive_list_update_dives(dive_list); } gtk_widget_destroy(dialog); } @@ -174,7 +178,6 @@ int main(int argc, char **argv) { int i; GtkWidget *win; - GtkWidget *divelist; GtkWidget *paned; GtkWidget *info_box; GtkWidget *notebook; @@ -214,8 +217,8 @@ int main(int argc, char **argv) gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0); /* Create the actual divelist */ - divelist = create_dive_list(); - gtk_paned_add1(GTK_PANED(paned), divelist); + dive_list = dive_list_create(); + gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget); /* VBox for dive info, and tabs */ info_box = gtk_vbox_new(FALSE, 6); diff --git a/profile.c b/profile.c index 5c30fbb5e..032bbd2c5 100644 --- a/profile.c +++ b/profile.c @@ -4,6 +4,7 @@ #include "dive.h" #include "display.h" +#include "divelist.h" int selected_dive = 0; From 65ef1bae20e9c908745115d0d82b7232dc5d1b26 Mon Sep 17 00:00:00 2001 From: Nathan Samson Date: Mon, 5 Sep 2011 21:23:38 +0200 Subject: [PATCH 2/3] Make it possible to load multiple files at once. Signed-off-by: Nathan Samson --- main.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 83794ca06..a0bc5b7b6 100644 --- a/main.c +++ b/main.c @@ -96,12 +96,20 @@ static void file_open(GtkWidget *w, gpointer data) GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); + gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + GSList *filenames; char *filename; - filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); - parse_xml_file(filename); - g_free(filename); + filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); + + while(filenames != NULL) { + filename = (char *)filenames->data; + parse_xml_file(filename); + g_free(filename); + filenames = g_slist_next(filenames); + } + g_slist_free(filenames); report_dives(); dive_list_update_dives(dive_list); } From 11becb87505b8cdf6fbf2f10941f87d394e49f80 Mon Sep 17 00:00:00 2001 From: Nathan Samson Date: Mon, 5 Sep 2011 22:14:53 +0200 Subject: [PATCH 3/3] Report errors when opening files Signed-off-by: Nathan Samson --- Makefile | 16 +++++----- dive.c | 2 -- dive.h | 6 +++- main.c | 92 +++++++++++++++++++++++++++++++++++++++++++++-------- parse-xml.c | 9 +++++- 5 files changed, 100 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 00658b6a0..b47a531bb 100644 --- a/Makefile +++ b/Makefile @@ -6,25 +6,25 @@ OBJS=main.o dive.o profile.o info.o divelist.o parse-xml.o save-xml.o divelog: $(OBJS) $(CC) $(LDFLAGS) -o divelog $(OBJS) \ `xml2-config --libs` \ - `pkg-config --libs gtk+-2.0` + `pkg-config --libs gtk+-2.0 glib-2.0` parse-xml.o: parse-xml.c dive.h - $(CC) $(CFLAGS) -c `xml2-config --cflags` parse-xml.c + $(CC) $(CFLAGS) `pkg-config --cflags glib-2.0` -c `xml2-config --cflags` parse-xml.c save-xml.o: save-xml.c dive.h - $(CC) $(CFLAGS) -c save-xml.c + $(CC) $(CFLAGS) `pkg-config --cflags glib-2.0` -c save-xml.c dive.o: dive.c dive.h - $(CC) $(CFLAGS) -c dive.c + $(CC) $(CFLAGS) `pkg-config --cflags glib-2.0` -c dive.c main.o: main.c dive.h display.h divelist.h - $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c main.c + $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0 glib-2.0` -c main.c profile.o: profile.c dive.h display.h divelist.h - $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c profile.c + $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0 glib-2.0` -c profile.c info.o: info.c dive.h display.h divelist.h - $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c info.c + $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0 glib-2.0` -c info.c divelist.o: divelist.c dive.h display.h divelist.h - $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0` -c divelist.c + $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0 glib-2.0` -c divelist.c diff --git a/dive.c b/dive.c index 5039abbbd..ba5200f51 100644 --- a/dive.c +++ b/dive.c @@ -130,8 +130,6 @@ struct dive *fixup_dive(struct dive *dive) } /* Don't pick a zero for MERGE_MIN() */ -#define MIN(a,b) ((a)<(b)?(a):(b)) -#define MAX(a,b) ((a)>(b)?(a):(b)) #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n) #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n) diff --git a/dive.h b/dive.h index 65337303a..82b336e53 100644 --- a/dive.h +++ b/dive.h @@ -4,6 +4,8 @@ #include #include +#include + /* * Some silly typedefs to make our units very explicit. * @@ -141,7 +143,7 @@ static inline struct dive *get_dive(unsigned int nr) } extern void parse_xml_init(void); -extern void parse_xml_file(const char *filename); +extern void parse_xml_file(const char *filename, GError **error); extern void flush_dive_info_changes(void); extern void save_dives(const char *filename); @@ -154,4 +156,6 @@ static inline unsigned int dive_size(int samples) extern struct dive *fixup_dive(struct dive *dive); extern struct dive *try_to_merge(struct dive *a, struct dive *b); +#define DIVE_ERROR_PARSE 1 + #endif /* DIVE_H */ diff --git a/main.c b/main.c index a0bc5b7b6..14c4f26fe 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,10 @@ #include "display.h" GtkWidget *main_window; +GtkWidget *main_vbox; +GtkWidget *error_info_bar; +GtkWidget *error_label; +int error_count; struct DiveList dive_list; static int sortfn(const void *_a, const void *_b) @@ -87,6 +91,49 @@ void repaint_dive(void) static char *existing_filename; +static void on_info_bar_response(GtkWidget *widget, gint response, + gpointer data) +{ + if (response == GTK_RESPONSE_OK) + { + gtk_widget_destroy(widget); + error_info_bar = NULL; + } +} + +static void report_error(GError* error) +{ + if (error == NULL) + { + return; + } + + if (error_info_bar == NULL) + { + error_count = 1; + error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK, + GTK_RESPONSE_OK, + NULL); + g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL); + gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar), + GTK_MESSAGE_ERROR); + + error_label = gtk_label_new(error->message); + GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar)); + gtk_container_add(GTK_CONTAINER(container), error_label); + + gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0); + gtk_widget_show_all(main_vbox); + } + else + { + error_count++; + char buffer[256]; + snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count); + gtk_label_set(GTK_LABEL(error_label), buffer); + } +} + static void file_open(GtkWidget *w, gpointer data) { GtkWidget *dialog; @@ -103,9 +150,17 @@ static void file_open(GtkWidget *w, gpointer data) char *filename; filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); + GError *error = NULL; while(filenames != NULL) { filename = (char *)filenames->data; - parse_xml_file(filename); + parse_xml_file(filename, &error); + if (error != NULL) + { + report_error(error); + g_error_free(error); + error = NULL; + } + g_free(filename); filenames = g_slist_next(filenames); } @@ -198,24 +253,14 @@ int main(int argc, char **argv) gtk_init(&argc, &argv); - for (i = 1; i < argc; i++) { - const char *a = argv[i]; - - if (a[0] == '-') { - parse_argument(a); - continue; - } - parse_xml_file(a); - } - - report_dives(); - + error_info_bar = NULL; win = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL); main_window = win; vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(win), vbox); + main_vbox = vbox; menubar = get_menubar_menu(win); gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0); @@ -250,6 +295,27 @@ int main(int argc, char **argv) gtk_widget_set_app_paintable(win, TRUE); gtk_widget_show_all(win); + + for (i = 1; i < argc; i++) { + const char *a = argv[i]; + + if (a[0] == '-') { + parse_argument(a); + continue; + } + GError *error = NULL; + parse_xml_file(a, &error); + + if (error != NULL) + { + report_error(error); + g_error_free(error); + error = NULL; + } + } + + report_dives(); + dive_list_update_dives(dive_list); gtk_main(); return 0; diff --git a/parse-xml.c b/parse-xml.c index 700f0d80f..26e322f80 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -1119,13 +1119,20 @@ static void reset_all(void) uemis = 0; } -void parse_xml_file(const char *filename) +void parse_xml_file(const char *filename, GError **error) { xmlDoc *doc; doc = xmlReadFile(filename, NULL, 0); if (!doc) { fprintf(stderr, "Failed to parse '%s'.\n", filename); + if (error != NULL) + { + *error = g_error_new(g_quark_from_string("divelog"), + DIVE_ERROR_PARSE, + "Failed to parse '%s'", + filename); + } return; }