]> git.tdb.fi Git - ext/subsurface.git/commitdiff
Report errors when opening files
authorNathan Samson <nathansamson@gmail.com>
Mon, 5 Sep 2011 20:14:53 +0000 (22:14 +0200)
committerNathan Samson <nathansamson@gmail.com>
Mon, 5 Sep 2011 20:15:30 +0000 (22:15 +0200)
Signed-off-by: Nathan Samson <nathansamson@gmail.com>
Makefile
dive.c
dive.h
main.c
parse-xml.c

index 00658b6a0ecab340cdec7557d059e16870ec969b..b47a531bbe0f631ac73c5cd8e9953c7b7005e42e 100644 (file)
--- 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 5039abbbd58ef29880af4f7f2c978bb03137b757..ba5200f51af744c90dc3e86595c6c320e3956288 100644 (file)
--- 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 65337303a05d893e7e894ac764ff46a444c0ab61..82b336e5356cadaaf447060ec1d7c51d756e3956 100644 (file)
--- a/dive.h
+++ b/dive.h
@@ -4,6 +4,8 @@
 #include <stdlib.h>
 #include <time.h>
 
+#include <glib.h>
+
 /*
  * 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 a0bc5b7b67a6a0eb7d75265ccf80b180751f10f6..14c4f26fe2952e8a1ac4324584c86fac87edb18e 100644 (file)
--- 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;
index 700f0d80fd1ab355524e1afcc5f3a6b3bcf6d064..26e322f80827dbfdc08678593880ef4f3f46a6fb 100644 (file)
@@ -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;
        }