]> git.tdb.fi Git - ext/subsurface.git/commitdiff
Add XML file import back and treat open and import differently
authorDirk Hohndel <dirk@hohndel.org>
Wed, 5 Oct 2011 18:36:15 +0000 (11:36 -0700)
committerDirk Hohndel <dirk@hohndel.org>
Wed, 5 Oct 2011 20:36:17 +0000 (13:36 -0700)
Open (or adding a file name on the command line) means that this is just
one of the files that you consider part of your dive history. So dives
don't get automagically numbered and the dive_list is not considered
"changed" just because another file was opened.

Import (or adding a file on the command line after --import) means that
you are importing the content of this file to your dive history. So if the
imported file has un-numbered dives that are newer than everything else,
those get correctly renumbered. And importing marks the dive_list as
changed.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
dive.h
gtk-gui.c
main.c

diff --git a/dive.h b/dive.h
index 5b7f4658b90c4a7002f0888ee954b741fe43590c..f29712e2c382e437e66db5a7cc2e460e992bc482 100644 (file)
--- a/dive.h
+++ b/dive.h
@@ -243,7 +243,7 @@ extern void record_dive(struct dive *dive);
 extern struct sample *prepare_sample(struct dive **divep);
 extern void finish_sample(struct dive *dive, struct sample *sample);
 
-extern void report_dives(void);
+extern void report_dives(gboolean imported);
 extern struct dive *fixup_dive(struct dive *dive);
 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
 
index be5adb9bdd28fe521723e4a45d5caa1f281eaf47..151f0fa1798e67c5462ec4e17dcf661238d8c0a0 100644 (file)
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -129,7 +129,7 @@ static void file_open(GtkWidget *w, gpointer data)
                        filenames = g_slist_next(filenames);
                }
                g_slist_free(filenames);
-               report_dives();
+               report_dives(FALSE);
        }
        gtk_widget_destroy(dialog);
 }
@@ -836,12 +836,60 @@ static GtkEntry *dive_computer_device(GtkWidget *vbox)
        return GTK_ENTRY(entry);
 }
 
+static GtkWidget *xml_file_selector(GtkWidget *vbox)
+{
+       GtkWidget *hbox, *frame, *chooser, *dialog;
+       GtkFileFilter *filter;
+
+       hbox = gtk_hbox_new(FALSE, 6);
+       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
+
+       frame = gtk_frame_new("XML file name");
+       gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
+       dialog = gtk_file_chooser_dialog_new("Open XML File",
+               GTK_WINDOW(main_window),
+               GTK_FILE_CHOOSER_ACTION_OPEN,
+               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+               GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+               NULL);
+       gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
+
+       filter = gtk_file_filter_new();
+       gtk_file_filter_add_pattern(filter, "*.xml");
+       gtk_file_filter_add_pattern(filter, "*.XML");
+       gtk_file_filter_add_pattern(filter, "*.sda");
+       gtk_file_filter_add_pattern(filter, "*.SDA");
+       gtk_file_filter_add_mime_type(filter, "text/xml");
+       gtk_file_filter_set_name(filter, "XML file");
+       gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
+
+       chooser = gtk_file_chooser_button_new_with_dialog(dialog);
+       gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
+       gtk_container_add(GTK_CONTAINER(frame), chooser);
+
+       return chooser;
+}
+
+static void do_import_file(gpointer data, gpointer user_data)
+{
+       GError *error = NULL;
+       parse_xml_file((char *)data, &error);
+
+       if (error != NULL)
+       {
+               report_error(error);
+               g_error_free(error);
+               error = NULL;
+       }
+}
+
 void import_dialog(GtkWidget *w, gpointer data)
 {
        int result;
-       GtkWidget *dialog, *hbox, *vbox;
+       GtkWidget *dialog, *hbox, *vbox, *label;
        GtkComboBox *computer;
        GtkEntry *device;
+       GtkWidget *XMLchooser;
        device_data_t devicedata = {
                .devname = NULL,
        };
@@ -854,10 +902,11 @@ void import_dialog(GtkWidget *w, gpointer data)
                NULL);
 
        vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
-
+       label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
+       gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
+       XMLchooser = xml_file_selector(vbox);
        computer = dive_computer_selector(vbox);
        device = dive_computer_device(vbox);
-
        hbox = gtk_hbox_new(FALSE, 6);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
        devicedata.progress.bar = gtk_progress_bar_new();
@@ -870,25 +919,34 @@ void import_dialog(GtkWidget *w, gpointer data)
                GtkTreeIter iter;
                GtkTreeModel *model;
                const char *comp;
+               GSList *list;
        case GTK_RESPONSE_ACCEPT:
-               if (!gtk_combo_box_get_active_iter(computer, &iter))
-                       break;
-               model = gtk_combo_box_get_model(computer);
-               gtk_tree_model_get(model, &iter,
-                       0, &comp,
-                       1, &type,
-                       -1);
-               devicedata.type = type;
-               devicedata.name = comp;
-               devicedata.devname = gtk_entry_get_text(device);
-               do_import(&devicedata);
+               /* what happened - did the user pick a file? In that case
+                * we ignore whether a dive computer model was picked */
+               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))
+                               break;
+                       model = gtk_combo_box_get_model(computer);
+                       gtk_tree_model_get(model, &iter,
+                                       0, &comp,
+                                       1, &type,
+                                       -1);
+                       devicedata.type = type;
+                       devicedata.name = comp;
+                       devicedata.devname = gtk_entry_get_text(device);
+                       do_import(&devicedata);
+               } else {
+                       g_slist_foreach(list,do_import_file,NULL);
+                       g_slist_free(list);
+               }
                break;
        default:
                break;
        }
        gtk_widget_destroy(dialog);
 
-       report_dives();
+       report_dives(TRUE);
 }
 
 void update_progressbar(progressbar_t *progress, double value)
diff --git a/main.c b/main.c
index 642e0727182f75de5fb03047594462ab4f3b0f8e..26d2eee220bcbdcc8b4d4096aeadee336a025178 100644 (file)
--- a/main.c
+++ b/main.c
@@ -101,7 +101,7 @@ static void try_to_renumber(struct dive *last, int preexisting)
  * This doesn't really report anything at all. We just sort the
  * dives, the GUI does the reporting
  */
-void report_dives(void)
+void report_dives(gboolean imported)
 {
        int i;
        int preexisting = dive_table.preexisting;
@@ -135,13 +135,15 @@ void report_dives(void)
                i--;
        }
 
-       /* Was the previous dive table state numbered? */
-       if (last && last->number)
-               try_to_renumber(last, preexisting);
+       if (imported) {
+               /* Was the previous dive table state numbered? */
+               if (last && last->number)
+                       try_to_renumber(last, preexisting);
 
-       /* did we have dives in the table and added more? */
-       if (last && preexisting != dive_table.nr)
-               mark_divelist_changed(TRUE);
+               /* did we have dives in the table and added more? */
+               if (last && preexisting != dive_table.nr)
+                       mark_divelist_changed(TRUE);
+       }
        dive_table.preexisting = dive_table.nr;
        dive_list_update_dives();
 }
@@ -160,7 +162,7 @@ static void parse_argument(const char *arg)
                        if (strcmp(arg,"--import") == 0) {
                                /* mark the dives so far as the base,
                                 * everything after is imported */
-                               report_dives();
+                               report_dives(TRUE);
                                return;
                        }
                        /* fallthrough */
@@ -227,7 +229,7 @@ int main(int argc, char **argv)
                }
        }
 
-       report_dives();
+       report_dives(FALSE);
 
        run_ui();
        return 0;