]> git.tdb.fi Git - ext/subsurface.git/blobdiff - gtk-gui.c
Show dive import error messages in the import dialog
[ext/subsurface.git] / gtk-gui.c
index 7d0e95c43ecdf5b8885ff44dd4a1693b75b744c6..0bc79954f49124b413cd84dbb08737f659a817df 100644 (file)
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -31,6 +31,23 @@ static GtkWidget *dive_profile;
 
 visible_cols_t visible_cols = {TRUE, FALSE};
 
+static const char *default_dive_computer;
+
+static int is_default_dive_computer(const char *name)
+{
+       return default_dive_computer && !strcmp(name, default_dive_computer);
+}
+
+static void set_default_dive_computer(const char *name)
+{
+       if (!name || !*name)
+               return;
+       if (is_default_dive_computer(name))
+               return;
+       default_dive_computer = name;
+       subsurface_set_conf("dive_computer", PREF_STRING, name);
+}
+
 void repaint_dive(void)
 {
        update_dive(current_dive);
@@ -423,7 +440,9 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
                subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
                subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
                subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
-               subsurface_close_conf();
+
+               /* Flush the changes out to the system */
+               subsurface_flush_conf();
        }
        gtk_widget_destroy(dialog);
 }
@@ -689,6 +708,8 @@ void init_ui(int *argcp, char ***argvp)
 
        divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
 
+       default_dive_computer = subsurface_get_conf("dive_computer", PREF_STRING);
+
        error_info_bar = NULL;
        win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_set_application_name ("subsurface");
@@ -769,6 +790,11 @@ void run_ui(void)
        gtk_main();
 }
 
+void exit_ui(void)
+{
+       subsurface_close_conf();
+}
+
 typedef struct {
        cairo_rectangle_int_t rect;
        const char *text;
@@ -867,19 +893,22 @@ int process_ui_events(void)
        return ret;
 }
 
-
-static void fill_computer_list(GtkListStore *store)
+static int fill_computer_list(GtkListStore *store)
 {
+       int index = -1, i;
        GtkTreeIter iter;
        struct device_list *list = device_list;
 
-       for (list = device_list ; list->name ; list++) {
+       for (list = device_list, i = 0 ; list->name ; list++, i++) {
                gtk_list_store_append(store, &iter);
                gtk_list_store_set(store, &iter,
                        0, list->name,
                        1, list->type,
                        -1);
+               if (is_default_dive_computer(list->name))
+                       index = i;
        }
+       return index;
 }
 
 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
@@ -887,12 +916,13 @@ static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
        GtkWidget *hbox, *combo_box, *frame;
        GtkListStore *model;
        GtkCellRenderer *renderer;
+       int default_index;
 
        hbox = gtk_hbox_new(FALSE, 6);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
 
        model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
-       fill_computer_list(model);
+       default_index = fill_computer_list(model);
 
        frame = gtk_frame_new("Dive computer");
        gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
@@ -904,6 +934,8 @@ static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
        gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
 
+       gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
+
        return GTK_COMBO_BOX(combo_box);
 }
 
@@ -981,10 +1013,27 @@ static void do_import_file(gpointer data, gpointer user_data)
        }
 }
 
+static GtkWidget *import_dive_computer(device_data_t *data, GtkBox *vbox)
+{
+       GError *error;
+       GtkWidget *info, *container, *label;
+
+       error = do_import(data);
+       if (!error)
+               return NULL;
+
+       info = gtk_info_bar_new();
+       container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
+       label = gtk_label_new(error->message);
+       gtk_container_add(GTK_CONTAINER(container), label);
+       gtk_box_pack_start(vbox, info, FALSE, FALSE, 0);
+       return info;
+}
+
 void import_dialog(GtkWidget *w, gpointer data)
 {
        int result;
-       GtkWidget *dialog, *hbox, *vbox, *label;
+       GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
        GtkComboBox *computer;
        GtkEntry *device;
        GtkWidget *XMLchooser;
@@ -1010,6 +1059,7 @@ void import_dialog(GtkWidget *w, gpointer data)
        devicedata.progress.bar = gtk_progress_bar_new();
        gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
 
+repeat:
        gtk_widget_show_all(dialog);
        result = gtk_dialog_run(GTK_DIALOG(dialog));
        switch (result) {
@@ -1021,6 +1071,8 @@ void import_dialog(GtkWidget *w, gpointer data)
        case GTK_RESPONSE_ACCEPT:
                /* what happened - did the user pick a file? In that case
                 * we ignore whether a dive computer model was picked */
+               if (info)
+                       gtk_widget_destroy(info);
                list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
                if (g_slist_length(list) == 0) {
                        if (!gtk_combo_box_get_active_iter(computer, &iter))
@@ -1033,7 +1085,10 @@ void import_dialog(GtkWidget *w, gpointer data)
                        devicedata.type = type;
                        devicedata.name = comp;
                        devicedata.devname = gtk_entry_get_text(device);
-                       do_import(&devicedata);
+                       set_default_dive_computer(devicedata.name);
+                       info = import_dive_computer(&devicedata, GTK_BOX(vbox));
+                       if (info)
+                               goto repeat;
                } else {
                        g_slist_foreach(list,do_import_file,NULL);
                        g_slist_free(list);