Try to load the correct sheet
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 6 Jan 2013 13:10:22 +0000 (14:10 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 16 Feb 2013 14:02:48 +0000 (15:02 +0100)
src/data/gnumeric-reader.c
src/ui/gui/page-assistant.c
src/ui/gui/page-file.c
src/ui/gui/page-sheet-spec.c
src/ui/gui/text-data-import-dialog.c
src/ui/gui/text-data-import-dialog.h

index 6cc7b3e1c254c0d920a93f2a1e1275b95a3b20d4..9a059b85460d38301778ff017c5e29420a5b71f1 100644 (file)
@@ -169,6 +169,7 @@ process_node (struct gnumeric_reader *r)
        {
          if ( r->target_sheet != NULL)
            {
+             printf ("%s:%d\n", __FILE__, __LINE__);
              xmlChar *value = xmlTextReaderValue (r->xtr);
              if ( 0 == xmlStrcmp (value, r->target_sheet))
                r->state = STATE_SHEET_FOUND;
@@ -176,6 +177,7 @@ process_node (struct gnumeric_reader *r)
            }
          else if (r->target_sheet_index == r->sheet_index)
            {
+             printf ("%s:%d %d\n", __FILE__, __LINE__, r->sheet_index);
              r->state = STATE_SHEET_FOUND;
            }
        }
index 436c78458e84eb8c6f8284e5e461e68bde931e78..70c658d371e04c7957b9745de63f2a52b22964a0 100644 (file)
@@ -47,8 +47,6 @@
 #include "ui/gui/psppire-empty-list-store.h"
 #include "ui/gui/psppire-var-sheet.h"
 #include "ui/gui/psppire-var-store.h"
-#include "ui/gui/psppire-scanf.h"
-#include "ui/syntax-gen.h"
 
 #include "gl/error.h"
 #include "gl/intprops.h"
@@ -150,6 +148,10 @@ static void
 on_prepare (GtkAssistant *assistant, GtkWidget *page,
             struct import_assistant *ia)
 {
+  int pn = gtk_assistant_get_current_page (assistant);
+
+  if (pn == 1 && ia->file.type != FTYPE_TEXT)
+    post_sheet_spec_page (ia);
 
   if (gtk_assistant_get_page_type (assistant, page)
       == GTK_ASSISTANT_PAGE_CONFIRM)
@@ -207,6 +209,8 @@ on_reset (GtkButton *button, struct import_assistant *ia)
     reset_separators_page (ia);
   else if (page == ia->formats.page)
     reset_formats_page (ia);
+  else if (page == ia->sheet_spec.page)
+    reset_sheet_spec_page (ia);
 }
 
 /* Causes the assistant to close, returning RESPONSE for
index ccdf37dcdf796e3edb1cd848c24d6b3411646cf4..6422fa6ab463c49cda1a8f75b90089d067fecef9 100644 (file)
 
 struct import_assistant;
 
-/* Choosing a file and reading it. */
-
+/* Choose a file */
 static char *choose_file (GtkWindow *parent_window, gchar **encodingp);
+enum { MAX_PREVIEW_LINES = 1000 }; /* Max number of lines to read. */
+
+
+/*
+  Update IA according to the contents of DICT and CREADER.
+  CREADER will be destroyed by this function.
+*/
+void 
+update_assistant (struct import_assistant *ia)
+{
+  struct sheet_spec_page *ssp = &ia->sheet_spec;
+
+  struct file *file = &ia->file;
+  struct separators_page *sepp = &ia->separators;
+  int col;
+  int rows = 0;
+  struct ccase *c;
+  
+  sepp->column_cnt = dict_get_var_cnt (ssp->dict);
+  sepp->columns = xcalloc (sepp->column_cnt, sizeof (*sepp->columns));
+  for (col = 0; col < sepp->column_cnt ; ++col)
+    {
+      const struct variable *var = dict_get_var (ssp->dict, col);
+      sepp->columns[col].name = xstrdup (var_get_name (var));
+      sepp->columns[col].contents = NULL;
+    }
+
+  for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
+    {
+      rows++;
+      for (col = 0; col < sepp->column_cnt ; ++col)
+       {
+         char *ss;
+         const struct variable *var = dict_get_var (ssp->dict, col);
+
+         sepp->columns[col].contents = xrealloc (sepp->columns[col].contents,
+                                                 sizeof (struct substring) * rows);
+
+         ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict), 
+                        var_get_print_format (var));
+
+         sepp->columns[col].contents[rows - 1] = ss_cstr (ss);
+       }
+
+      if (rows > MAX_PREVIEW_LINES)
+       {
+         case_unref (c);
+         break;
+       }
+    }
+
+  file->line_cnt = rows;
+  casereader_destroy (ssp->reader);
+  ssp->reader = NULL;
+}
+
 
 /* Obtains the file to import from the user and initializes IA's
    file substructure.  PARENT_WINDOW must be the window to use
@@ -77,10 +132,8 @@ static char *choose_file (GtkWindow *parent_window, gchar **encodingp);
 bool
 init_file (struct import_assistant *ia, GtkWindow *parent_window)
 {
-  enum { MAX_PREVIEW_LINES = 1000 }; /* Max number of lines to read. */
   enum { MAX_LINE_LEN = 16384 }; /* Max length of an acceptable line. */
   struct file *file = &ia->file;
-  struct separators_page *sepp = &ia->separators;
   struct casereader *creader = NULL;
   struct dictionary *dict = NULL;
   struct spreadsheet_read_info sri;
@@ -113,45 +166,11 @@ init_file (struct import_assistant *ia, GtkWindow *parent_window)
     
   if (creader)
     {
-      int col;
-      int rows = 0;
-      struct ccase *c;
-  
-      sepp->column_cnt = dict_get_var_cnt (dict);
-      sepp->columns = xcalloc (sepp->column_cnt, sizeof (*sepp->columns));
-      for (col = 0; col < sepp->column_cnt ; ++col)
-       {
-         const struct variable *var = dict_get_var (dict, col);
-         sepp->columns[col].name = xstrdup (var_get_name (var));
-         sepp->columns[col].contents = NULL;
-       }
-
-      for (; (c = casereader_read (creader)) != NULL; case_unref (c))
-       {
-         rows++;
-         for (col = 0; col < sepp->column_cnt ; ++col)
-           {
-             char *ss;
-             const struct variable *var = dict_get_var (dict, col);
-
-             sepp->columns[col].contents = xrealloc (sepp->columns[col].contents,
-                                                     sizeof (struct substring) * rows);
-
-             ss = data_out (case_data (c, var), dict_get_encoding (dict), 
-                            var_get_print_format (var));
-
-             sepp->columns[col].contents[rows - 1] = ss_cstr (ss);
-           }
-
-         if (rows > MAX_PREVIEW_LINES)
-           {
-             case_unref (c);
-             break;
-           }
-       }
+      struct sheet_spec_page *ssp = &ia->sheet_spec;
+      ssp->dict = dict;
+      ssp->reader = creader;
 
-      file->line_cnt = rows;
-      casereader_destroy (creader);
+      update_assistant (ia);
     }
   else
     {
index 4905a78494f8261348229f71634d057c9079ff35..249084150c3cdcb4aff6ae6c29e56e3f63d7f955 100644 (file)
@@ -30,6 +30,9 @@
 #include "data/data-out.h"
 #include "data/format-guesser.h"
 #include "data/value-labels.h"
+#include "data/gnumeric-reader.h"
+#include "data/ods-reader.h"
+#include "data/spreadsheet-reader.h"
 #include "language/data-io/data-parser.h"
 #include "language/lexer/lexer.h"
 #include "libpspp/assertion.h"
@@ -81,5 +84,52 @@ init_sheet_spec_page (struct import_assistant *ia)
 void
 reset_sheet_spec_page (struct import_assistant *ia)
 {
+  printf ("%s\n", __FUNCTION__);
+}
+
+/* Called when the Forward button is clicked, 
+   but before displaying the new page.
+*/
+void
+post_sheet_spec_page (struct import_assistant *ia)
+{
+  GtkBuilder *builder = ia->asst.builder;
+
+  struct file *file = &ia->file;
+  struct sheet_spec_page *ssp = &ia->sheet_spec;
+  struct casereader *creader;
+  struct dictionary *dict;
+
+  GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
+
+  gint num = atoi (gtk_entry_get_text (sheet_entry));
+  
+  printf ("%s sheet number %d\n", __FUNCTION__, num);
+
+  ssp->opts.sheet_name = NULL;
+  ssp->opts.cell_range = NULL;
+  ssp->opts.sheet_index = num;
+
+  ssp->sri.file_name = file->file_name;
+  ssp->sri.read_names = true;
+  ssp->sri.asw = -1;
+
+  switch (ia->file.type)
+    {
+    case FTYPE_ODS:
+      creader = ods_open_reader (&ssp->sri, &ssp->opts, &dict);
+      break;
+    case FTYPE_GNUMERIC:
+      creader = gnumeric_open_reader (&ssp->sri, &ssp->opts, &dict);
+      break;
+    default:
+      g_assert_not_reached ();
+      break;
+    }
+
+  ssp->dict = dict;
+  ssp->reader = creader;
+
+  update_assistant (ia);
 }
 
index 494de27389089e9e9ba1725f558c7f9174c2826f..0841dd184e2136aafad409e2cf8e04f37a096619 100644 (file)
@@ -280,26 +280,22 @@ generate_syntax (const struct import_assistant *ia)
       }
       break;
     case FTYPE_GNUMERIC:
-      {
-       syntax_gen_pspp (&s,
-                        "GET DATA\n"
-                        "  /TYPE=GNM\n"
-                        "  /FILE=%sq\n",
-                        ia->file.file_name);
-      }
-      break;
-    
     case FTYPE_ODS:
       {
+       const struct sheet_spec_page *ssp = &ia->sheet_spec;
+
        syntax_gen_pspp (&s,
                         "GET DATA\n"
-                        "  /TYPE=ODS\n"
-                        "  /FILE=%sq\n",
-                        ia->file.file_name);
+                        "  /TYPE=%s\n"
+                        "  /FILE=%sq\n"
+                        "  /SHEET=index %d\n"
+                        ".",
+                        (ia->file.type == FTYPE_GNUMERIC) ? "GNM" : "ODS",
+                        ia->file.file_name,                     
+                        ssp->opts.sheet_index);
       }
       break;
-
-
+    
     default:
       g_assert_not_reached ();
     }
index 9f5590303b2cbce51995335876de13680551d14c..b465d04ddf9ec646b8f333cc622ba46833a661cc 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <glib-object.h>
 #include "ui/gui/psppire-data-window.h"
+#include "data/spreadsheet-reader.h"
 
 #include "libpspp/str.h"
 
@@ -67,6 +68,11 @@ struct assistant
 struct sheet_spec_page
   {
     GtkWidget *page;
+    struct casereader *reader;
+    struct dictionary *dict;
+
+    struct spreadsheet_read_info sri;
+    struct spreadsheet_read_options opts;
   };
 
 
@@ -196,6 +202,9 @@ GtkTreeViewColumn *make_data_column (struct import_assistant *,
                                             gint column_idx);
 
 
+void  update_assistant (struct import_assistant *ia);
+
+
 bool init_file (struct import_assistant *ia, GtkWindow *parent_window);
 void destroy_file (struct import_assistant *ia);
 
@@ -205,11 +214,11 @@ void reset_intro_page (struct import_assistant *);
 
 void init_sheet_spec_page (struct import_assistant *);
 void reset_sheet_spec_page (struct import_assistant *);
+void post_sheet_spec_page (struct import_assistant *ia);
 
 void init_first_line_page (struct import_assistant *ia);
 void prepare_first_line_page (struct import_assistant *ia);
 void reset_first_line_page (struct import_assistant *);
-void destroy_first_line_page (struct import_assistant *ia);
 
 void init_separators_page (struct import_assistant *ia);
 void prepare_separators_page (struct import_assistant *ia);
@@ -221,7 +230,6 @@ void prepare_formats_page (struct import_assistant *ia);
 void reset_formats_page (struct import_assistant *);
 void destroy_formats_page (struct import_assistant *ia);
 
-
 void init_assistant (struct import_assistant *, GtkWindow *);
 void destroy_assistant (struct import_assistant *);