From: John Darrington Date: Wed, 27 Mar 2013 19:07:58 +0000 (+0100) Subject: Merge remote branch 'origin/master' into import-gui X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbc9e4f23035409965750a6970f888a3dfe649c6;hp=c62c4c98e0b104bf21484880f634f3d77809b414;p=pspp Merge remote branch 'origin/master' into import-gui --- diff --git a/src/data/casereader.c b/src/data/casereader.c index c8050c6cde..c0968669a2 100644 --- a/src/data/casereader.c +++ b/src/data/casereader.c @@ -239,13 +239,12 @@ static casenumber casereader_count_cases__ (const struct casereader *reader, casenumber max_cases) { - struct casereader *clone; - casenumber n_cases; - - clone = casereader_clone (reader); - n_cases = casereader_advance (clone, max_cases); + struct casereader *clone = casereader_clone (reader); + casenumber n_cases = casereader_advance (clone, max_cases); +#if (__GNUC__ == 4 ) && (__GNUC_MINOR__ == 4) + volatile int x = 1; +#endif casereader_destroy (clone); - return n_cases; } diff --git a/src/data/ods-reader.c b/src/data/ods-reader.c index b3586b82ec..cbd7feaa17 100644 --- a/src/data/ods-reader.c +++ b/src/data/ods-reader.c @@ -100,31 +100,35 @@ enum reader_state STATE_CELL_CONTENT /* Found a the text within a cell */ }; -struct ods_reader +struct state_data { - struct spreadsheet spreadsheet; - struct zip_reader *zreader; xmlTextReaderPtr xtr; - int ref_cnt; - + int node_type; enum reader_state state; int row; int col; - int node_type; int current_sheet; xmlChar *current_sheet_name; - xmlChar *target_sheet_name; - int target_sheet_index; + int col_span; +}; +struct ods_reader +{ + struct spreadsheet spreadsheet; + struct zip_reader *zreader; + int ref_cnt; + int target_sheet_index; + xmlChar *target_sheet_name; + + struct state_data foo; + struct state_data rsd; int start_row; int start_col; int stop_row; int stop_col; - int col_span; - struct sheet_detail *sheets; int n_allocated_sheets; @@ -140,6 +144,7 @@ struct ods_reader void ods_destroy (struct spreadsheet *s) { +#if 0 struct ods_reader *r = (struct ods_reader *) s; if (--r->ref_cnt == 0) @@ -155,38 +160,40 @@ ods_destroy (struct spreadsheet *s) free (r->sheets); free (r); } +#endif } static bool -reading_target_sheet (const struct ods_reader *r) +reading_target_sheet (const struct ods_reader *r, const struct state_data *foo) { if (r->target_sheet_name != NULL) { - if ( 0 == xmlStrcmp (r->target_sheet_name, r->current_sheet_name)) + if ( 0 == xmlStrcmp (r->target_sheet_name, foo->current_sheet_name)) return true; } - if (r->target_sheet_index == r->current_sheet + 1) + if (r->target_sheet_index == foo->current_sheet + 1) return true; return false; } -static void process_node (struct ods_reader *r); +static void process_node (struct ods_reader *or, struct state_data *r); const char * ods_get_sheet_name (struct spreadsheet *s, int n) { - struct ods_reader *or = (struct ods_reader *) s; - + struct ods_reader *r = (struct ods_reader *) s; + struct state_data *or = &r->foo; + assert (n < s->n_sheets); while ( - (or->n_allocated_sheets <= n) + (r->n_allocated_sheets <= n) || or->state != STATE_SPREADSHEET ) { @@ -194,22 +201,23 @@ ods_get_sheet_name (struct spreadsheet *s, int n) if ( ret != 1) break; - process_node (or); + process_node (r, or); } - return or->sheets[n].name; + return r->sheets[n].name; } char * ods_get_sheet_range (struct spreadsheet *s, int n) { - struct ods_reader *or = (struct ods_reader *) s; + struct ods_reader *r = (struct ods_reader *) s; + struct state_data *or = &r->foo; assert (n < s->n_sheets); while ( - (or->n_allocated_sheets <= n) - || (or->sheets[n].stop_row == -1) + (r->n_allocated_sheets <= n) + || (r->sheets[n].stop_row == -1) || or->state != STATE_SPREADSHEET ) { @@ -217,20 +225,21 @@ ods_get_sheet_range (struct spreadsheet *s, int n) if ( ret != 1) break; - process_node (or); + process_node (r, or); } return create_cell_ref ( - or->sheets[n].start_col, - or->sheets[n].start_row, - or->sheets[n].stop_col, - or->sheets[n].stop_row); + r->sheets[n].start_col, + r->sheets[n].start_row, + r->sheets[n].stop_col, + r->sheets[n].stop_row); } static void ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) { +#if 0 struct ods_reader *r = r_; if ( r == NULL) return ; @@ -253,6 +262,7 @@ ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) xmlFree (r->target_sheet_name); ods_destroy (&r->spreadsheet); +#endif } @@ -260,7 +270,7 @@ ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) static void -process_node (struct ods_reader *r) +process_node (struct ods_reader *or, struct state_data *r) { xmlChar *name = xmlTextReaderName (r->xtr); if (name == NULL) @@ -290,15 +300,15 @@ process_node (struct ods_reader *r) ++r->current_sheet; - if (r->current_sheet >= r->n_allocated_sheets) + if (r->current_sheet >= or->n_allocated_sheets) { - assert (r->current_sheet == r->n_allocated_sheets); - r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets); - r->sheets[r->n_allocated_sheets - 1].start_col = -1; - r->sheets[r->n_allocated_sheets - 1].stop_col = -1; - r->sheets[r->n_allocated_sheets - 1].start_row = -1; - r->sheets[r->n_allocated_sheets - 1].stop_row = -1; - r->sheets[r->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name)); + assert (r->current_sheet == or->n_allocated_sheets); + or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets); + or->sheets[or->n_allocated_sheets - 1].start_col = -1; + or->sheets[or->n_allocated_sheets - 1].stop_col = -1; + or->sheets[or->n_allocated_sheets - 1].start_row = -1; + or->sheets[or->n_allocated_sheets - 1].stop_row = -1; + or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name)); } r->col = 0; @@ -379,22 +389,22 @@ process_node (struct ods_reader *r) break; case STATE_CELL_CONTENT: assert (r->current_sheet >= 0); - assert (r->current_sheet < r->n_allocated_sheets); + assert (r->current_sheet < or->n_allocated_sheets); - if (r->sheets[r->current_sheet].start_row == -1) - r->sheets[r->current_sheet].start_row = r->row - 1; + if (or->sheets[r->current_sheet].start_row == -1) + or->sheets[r->current_sheet].start_row = r->row - 1; if ( - (r->sheets[r->current_sheet].start_col == -1) + (or->sheets[r->current_sheet].start_col == -1) || - (r->sheets[r->current_sheet].start_col >= r->col - 1) + (or->sheets[r->current_sheet].start_col >= r->col - 1) ) - r->sheets[r->current_sheet].start_col = r->col - 1; + or->sheets[r->current_sheet].start_col = r->col - 1; - r->sheets[r->current_sheet].stop_row = r->row - 1; + or->sheets[r->current_sheet].stop_row = r->row - 1; - if ( r->sheets[r->current_sheet].stop_col < r->col - 1) - r->sheets[r->current_sheet].stop_col = r->col - 1; + if ( or->sheets[r->current_sheet].stop_col < r->col - 1) + or->sheets[r->current_sheet].stop_col = r->col - 1; if (XML_READER_TYPE_END_ELEMENT == r->node_type) r->state = STATE_CELL; @@ -543,17 +553,14 @@ ods_error_handler (void *ctx, const char *mesg, } -static bool +static xmlTextReaderPtr init_reader (struct ods_reader *r, bool report_errors) { struct zip_member *content = zip_member_open (r->zreader, "content.xml"); xmlTextReaderPtr xtr; if ( content == NULL) - return false; - - if (r->xtr) - xmlFreeTextReader (r->xtr); + return NULL; zip_member_ref (content); xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read, @@ -564,17 +571,13 @@ init_reader (struct ods_reader *r, bool report_errors) if ( xtr == NULL) return false; - r->xtr = xtr; + r->spreadsheet.type = SPREADSHEET_ODS; - r->row = 0; - r->col = 0; - r->current_sheet = 0; - r->state = STATE_INIT; if (report_errors) xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r); - return true; + return xtr; } @@ -585,6 +588,7 @@ ods_probe (const char *filename, bool report_errors) struct string errs = DS_EMPTY_INITIALIZER; int sheet_count; struct zip_reader *zr = zip_reader_create (filename, &errs); + xmlTextReaderPtr xtr; if (zr == NULL) { @@ -602,10 +606,17 @@ ods_probe (const char *filename, bool report_errors) r->zreader = zr; r->ref_cnt = 1; - if (! init_reader (r, report_errors)) + xtr = init_reader (r, report_errors); + if (xtr == NULL) { goto error; } + r->foo.xtr = xtr; + r->foo.row = 0; + r->foo.col = 0; + r->foo.current_sheet = 0; + r->foo.state = STATE_INIT; + r->spreadsheet.n_sheets = sheet_count; r->n_allocated_sheets = 0; @@ -634,6 +645,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, int i; struct var_spec *var_spec = NULL; int n_var_specs = 0; + xmlTextReaderPtr xtr; struct ods_reader *r = (struct ods_reader *) spreadsheet; xmlChar *val_string = NULL; @@ -643,9 +655,17 @@ ods_make_reader (struct spreadsheet *spreadsheet, ds_init_empty (&r->ods_errs); ++r->ref_cnt; - if ( !init_reader (r, true)) + xtr = init_reader (r, true); + if ( xtr == NULL) goto error; + r->rsd.xtr = xtr; + r->rsd.row = 0; + r->rsd.col = 0; + r->rsd.current_sheet = 0; + r->rsd.state = STATE_INIT; + + if (opts->cell_range) { if ( ! convert_cell_ref (opts->cell_range, @@ -665,20 +685,17 @@ ods_make_reader (struct spreadsheet *spreadsheet, r->stop_row = -1; } - r->state = STATE_INIT; r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name); r->target_sheet_index = opts->sheet_index; - r->row = r->col = 0; - /* Advance to the start of the cells for the target sheet */ - while ( ! reading_target_sheet (r) - || r->state != STATE_ROW || r->row <= r->start_row ) + while ( ! reading_target_sheet (r, &r->rsd) + || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row ) { - if (1 != (ret = xmlTextReaderRead (r->xtr))) + if (1 != (ret = xmlTextReaderRead (r->rsd.xtr))) break; - process_node (r); + process_node (r, &r->rsd); } if (ret < 1) @@ -690,17 +707,17 @@ ods_make_reader (struct spreadsheet *spreadsheet, if ( opts->read_names) { - while (1 == (ret = xmlTextReaderRead (r->xtr))) + while (1 == (ret = xmlTextReaderRead (r->rsd.xtr))) { int idx; - process_node (r); + process_node (r, &r->rsd); /* If the row is finished then stop for now */ - if (r->state == STATE_TABLE && r->row > r->start_row) + if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row) break; - idx = r->col - r->start_col -1 ; + idx = r->rsd.col - r->start_col -1 ; if ( idx < 0) continue; @@ -708,11 +725,11 @@ ods_make_reader (struct spreadsheet *spreadsheet, if (r->stop_col != -1 && idx > r->stop_col - r->start_col) continue; - if (r->state == STATE_CELL_CONTENT + if (r->rsd.state == STATE_CELL_CONTENT && - XML_READER_TYPE_TEXT == r->node_type) + XML_READER_TYPE_TEXT == r->rsd.node_type) { - xmlChar *value = xmlTextReaderValue (r->xtr); + xmlChar *value = xmlTextReaderValue (r->rsd.xtr); if ( idx >= n_var_specs) { @@ -736,35 +753,35 @@ ods_make_reader (struct spreadsheet *spreadsheet, } /* Read in the first row of data */ - while (1 == xmlTextReaderRead (r->xtr)) + while (1 == xmlTextReaderRead (r->rsd.xtr)) { int idx; - process_node (r); + process_node (r, &r->rsd); - if ( ! reading_target_sheet (r) ) + if ( ! reading_target_sheet (r, &r->rsd) ) break; /* If the row is finished then stop for now */ - if (r->state == STATE_TABLE && - r->row > r->start_row + (opts->read_names ? 1 : 0)) + if (r->rsd.state == STATE_TABLE && + r->rsd.row > r->start_row + (opts->read_names ? 1 : 0)) break; - idx = r->col - r->start_col - 1; + idx = r->rsd.col - r->start_col - 1; if (idx < 0) continue; if (r->stop_col != -1 && idx > r->stop_col - r->start_col) continue; - if ( r->state == STATE_CELL && - XML_READER_TYPE_ELEMENT == r->node_type) + if ( r->rsd.state == STATE_CELL && + XML_READER_TYPE_ELEMENT == r->rsd.node_type) { - type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type")); - val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value")); + type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type")); + val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value")); } - if ( r->state == STATE_CELL_CONTENT && - XML_READER_TYPE_TEXT == r->node_type) + if ( r->rsd.state == STATE_CELL_CONTENT && + XML_READER_TYPE_TEXT == r->rsd.node_type) { if (idx >= n_var_specs) { @@ -778,7 +795,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, } var_spec [idx].firstval.type = type; - var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr); + var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr); var_spec [idx].firstval.value = val_string; val_string = NULL; @@ -789,7 +806,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, /* Create the dictionary and populate it */ r->spreadsheet.dict = r->dict = dict_create ( - CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr))); + CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr))); for (i = 0; i < n_var_specs ; ++i ) { @@ -836,11 +853,11 @@ ods_make_reader (struct spreadsheet *spreadsheet, } /* Read in the first row of data */ - while (1 == xmlTextReaderRead (r->xtr)) + while (1 == xmlTextReaderRead (r->rsd.xtr)) { - process_node (r); + process_node (r, &r->rsd); - if (r->state == STATE_ROW) + if (r->rsd.state == STATE_ROW) break; } @@ -900,17 +917,17 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) /* Advance to the start of a row. (If there is one) */ - while (r->state != STATE_ROW - && 1 == xmlTextReaderRead (r->xtr) + while (r->rsd.state != STATE_ROW + && 1 == xmlTextReaderRead (r->rsd.xtr) ) { - process_node (r); + process_node (r, &r->rsd); } - if ( ! reading_target_sheet (r) - || r->state < STATE_TABLE - || (r->stop_row != -1 && r->row > r->stop_row + 1) + if ( ! reading_target_sheet (r, &r->rsd) + || r->rsd.state < STATE_TABLE + || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1) ) { return NULL; @@ -919,34 +936,34 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) c = case_create (r->proto); case_set_missing (c); - while (1 == xmlTextReaderRead (r->xtr)) + while (1 == xmlTextReaderRead (r->rsd.xtr)) { - process_node (r); + process_node (r, &r->rsd); - if ( r->stop_row != -1 && r->row > r->stop_row + 1) + if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1) break; - if (r->state == STATE_CELL && - r->node_type == XML_READER_TYPE_ELEMENT) + if (r->rsd.state == STATE_CELL && + r->rsd.node_type == XML_READER_TYPE_ELEMENT) { - type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type")); - val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value")); + type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type")); + val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value")); } - if (r->state == STATE_CELL_CONTENT && - r->node_type == XML_READER_TYPE_TEXT) + if (r->rsd.state == STATE_CELL_CONTENT && + r->rsd.node_type == XML_READER_TYPE_TEXT) { int col; struct xml_value *xmv = xzalloc (sizeof *xmv); - xmv->text = xmlTextReaderValue (r->xtr); + xmv->text = xmlTextReaderValue (r->rsd.xtr); xmv->value = val_string; xmv->type = type; val_string = NULL; - for (col = 0; col < r->col_span; ++col) + for (col = 0; col < r->rsd.col_span; ++col) { const struct variable *var; - const int idx = r->col - col - r->start_col - 1; + const int idx = r->rsd.col - col - r->start_col - 1; if (idx < 0) continue; if (r->stop_col != -1 && idx > r->stop_col - r->start_col ) @@ -963,7 +980,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) xmlFree (xmv->type); free (xmv); } - if ( r->state <= STATE_TABLE) + if ( r->rsd.state <= STATE_TABLE) break; } diff --git a/src/ui/gui/automake.mk b/src/ui/gui/automake.mk index e29e30bb71..941a7fea1c 100644 --- a/src/ui/gui/automake.mk +++ b/src/ui/gui/automake.mk @@ -328,6 +328,8 @@ src_ui_gui_psppire_SOURCES = \ src/ui/gui/page-formats.h \ src/ui/gui/page-separators.c \ src/ui/gui/page-separators.h \ + src/ui/gui/page-sheet-spec.c \ + src/ui/gui/page-sheet-spec.h \ src/ui/gui/text-data-import-dialog.c \ src/ui/gui/text-data-import-dialog.h \ src/ui/gui/transpose-dialog.c \ diff --git a/src/ui/gui/page-assistant.c b/src/ui/gui/page-assistant.c index 88314734e2..cad23763de 100644 --- a/src/ui/gui/page-assistant.c +++ b/src/ui/gui/page-assistant.c @@ -160,10 +160,12 @@ on_prepare (GtkAssistant *assistant, GtkWidget *page, if ( ia->spreadsheet) { if (pn == 0) - { - } + { + prepare_sheet_spec_page (ia); + } else if (pn == 1) { + post_sheet_spec_page (ia); prepare_formats_page (ia); } } @@ -230,6 +232,8 @@ on_reset (GtkButton *button, struct import_assistant *ia) reset_separators_page (ia); else if (page == assist_get_page ((struct assist_page *) ia->formats)) reset_formats_page (ia); + else if (page == assist_get_page ((struct assist_page *) ia->sheet_spec)) + reset_sheet_spec_page (ia); } /* Causes the assistant to close, returning RESPONSE for diff --git a/src/ui/gui/page-file.c b/src/ui/gui/page-file.c index 2dd9f9ca16..84b7928a27 100644 --- a/src/ui/gui/page-file.c +++ b/src/ui/gui/page-file.c @@ -230,6 +230,21 @@ choose_file (GtkWindow *parent_window, gchar **encodingp) gtk_file_filter_add_mime_type (filter, "text/tab-separated-values"); gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter); + filter = gtk_file_filter_new (); + gtk_file_filter_set_name (filter, _("Gnumeric Spreadsheet Files")); + gtk_file_filter_add_mime_type (filter, "application/x-gnumeric"); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_set_name (filter, _("OpenDocument Spreadsheet Files")); + gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet"); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_set_name (filter, _("All Spreadsheet Files")); + gtk_file_filter_add_mime_type (filter, "application/x-gnumeric"); + gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet"); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter); gtk_file_chooser_set_extra_widget ( GTK_FILE_CHOOSER (dialog), psppire_encoding_selector_new ("Auto", true)); diff --git a/src/ui/gui/page-sheet-spec.c b/src/ui/gui/page-sheet-spec.c new file mode 100644 index 0000000000..3d92bfe410 --- /dev/null +++ b/src/ui/gui/page-sheet-spec.c @@ -0,0 +1,305 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2013 Free Software Foundation + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include "page-sheet-spec.h" + +#include "ui/gui/text-data-import-dialog.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "data/data-in.h" +#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" +#include "libpspp/i18n.h" +#include "libpspp/line-reader.h" +#include "libpspp/message.h" +#include "ui/gui/checkbox-treeview.h" +#include "ui/gui/dialog-common.h" +#include "ui/gui/executor.h" +#include "ui/gui/helper.h" +#include "ui/gui/builder-wrapper.h" +#include "ui/gui/psppire-data-window.h" +#include "ui/gui/psppire-dialog.h" +#include "ui/gui/psppire-encoding-selector.h" +#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 "ui/gui/psppire-spreadsheet-model.h" + +#include + +#include "gl/error.h" +#include "gl/intprops.h" +#include "gl/xalloc.h" + +#include "gettext.h" +#define _(msgid) gettext (msgid) +#define N_(msgid) msgid + +struct import_assistant; + +/* The "sheet-spec" page of the assistant. */ + +/* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */ +struct sheet_spec_page + { + GtkWidget *page; + struct casereader *reader; + struct dictionary *dict; + + struct spreadsheet_read_options opts; + }; + + +char * +sheet_spec_gen_syntax (const struct import_assistant *ia) +{ + const struct sheet_spec_page *ssp = ia->sheet_spec; + GtkBuilder *builder = ia->asst.builder; + GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry"); + const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry)); + + struct string s = DS_EMPTY_INITIALIZER; + + syntax_gen_pspp (&s, + "GET DATA" + "\n /TYPE=%ss" + "\n /FILE=%sq" + "\n /SHEET=index %d" + "\n /READNAMES=%ss", + (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS", + ia->file.file_name, + ssp->opts.sheet_index, + ssp->opts.read_names ? "ON" : "OFF"); + + + if (range && 0 != strcmp ("", range)) + { + syntax_gen_pspp (&s, + "\n /CELLRANGE=RANGE %sq", range); + } + else + { + syntax_gen_pspp (&s, + "\n /CELLRANGE=FULL"); + } + + + syntax_gen_pspp (&s, "."); + + + return ds_cstr (&s); +} + + +static void +on_sheet_combo_changed (GtkComboBox *cb, struct import_assistant *ia) +{ + GtkTreeIter iter; + gchar *range = NULL; + GtkTreeModel *model = gtk_combo_box_get_model (cb); + GtkBuilder *builder = ia->asst.builder; + GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry"); + + gtk_combo_box_get_active_iter (cb, &iter); + gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1); + gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : ""); + g_free (range); +} + +/* Initializes IA's sheet_spec substructure. */ +struct sheet_spec_page * +sheet_spec_page_create (struct import_assistant *ia) +{ + GtkBuilder *builder = ia->asst.builder; + struct sheet_spec_page *p = xzalloc (sizeof (*p)); + + GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry"); + GtkCellRenderer *renderer = gtk_cell_renderer_text_new (); + 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); + + g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia); + + add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"), + GTK_ASSISTANT_PAGE_INTRO); + + return p; +} + +/* Prepares IA's sheet_spec page. */ +void +prepare_sheet_spec_page (struct import_assistant *ia) +{ + GtkBuilder *builder = ia->asst.builder; + GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry"); + + gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry), + psppire_spreadsheet_model_new (ia->spreadsheet)); + + gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0); +} + + +/* Resets IA's sheet_spec page to its initial state. */ +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) +{ + int row_start = -1; + int row_stop = -1; + int col_start = -1; + int col_stop = -1; + + GtkBuilder *builder = ia->asst.builder; + + struct sheet_spec_page *ssp = ia->sheet_spec; + struct casereader *creader = NULL; + struct dictionary *dict = NULL; + + GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox"); + GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry"); + const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry)); + GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry"); + + gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box)); + + ssp->opts.sheet_name = NULL; + ssp->opts.cell_range = NULL; + ssp->opts.sheet_index = num + 1; + + if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop)) + { + ssp->opts.cell_range = range; + } + + ssp->opts.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox)); + ssp->opts.asw = -1; + + switch (ia->spreadsheet->type) + { + case SPREADSHEET_ODS: + case SPREADSHEET_GNUMERIC: + { + creader = spreadsheet_make_reader (ia->spreadsheet, &ssp->opts); + dict = ia->spreadsheet->dict; + } + break; + default: + g_assert_not_reached (); + break; + } + + ssp->dict = dict; + ssp->reader = creader; + + if (creader && dict) + { + update_assistant (ia); + } + else + { + GtkWidget * dialog = gtk_message_dialog_new (NULL, + GTK_DIALOG_MODAL, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + _("An error occurred reading the spreadsheet file.")); + + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + + +/* + 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; + int rows = 0; + + if (ssp->dict) + { + struct ccase *c; + int col; + + ia->column_cnt = dict_get_var_cnt (ssp->dict); + ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns)); + for (col = 0; col < ia->column_cnt ; ++col) + { + const struct variable *var = dict_get_var (ssp->dict, col); + ia->columns[col].name = xstrdup (var_get_name (var)); + ia->columns[col].contents = NULL; + } + + for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c)) + { + rows++; + for (col = 0; col < ia->column_cnt ; ++col) + { + char *ss; + const struct variable *var = dict_get_var (ssp->dict, col); + + ia->columns[col].contents = xrealloc (ia->columns[col].contents, + sizeof (struct substring) * rows); + + ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict), + var_get_print_format (var)); + + ia->columns[col].contents[rows - 1] = ss_cstr (ss); + } + + if (rows > MAX_PREVIEW_LINES) + { + case_unref (c); + break; + } + } + } + + ia->file.line_cnt = rows; +} diff --git a/src/ui/gui/page-sheet-spec.h b/src/ui/gui/page-sheet-spec.h new file mode 100644 index 0000000000..7a4e8012b2 --- /dev/null +++ b/src/ui/gui/page-sheet-spec.h @@ -0,0 +1,30 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2013 Free Software Foundation + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef PAGE_SHEET_SPEC +#define PAGE_SHEET_SPEC 1 + + +struct sheet_spec_page ; +struct import_assistant; + +/* Initializes IA's sheet_spec substructure. */ +struct sheet_spec_page *sheet_spec_page_create (struct import_assistant *ia); + +char *sheet_spec_gen_syntax (const struct import_assistant *ia); + + +#endif diff --git a/src/ui/gui/text-data-import-dialog.c b/src/ui/gui/text-data-import-dialog.c index d535734d08..662caba809 100644 --- a/src/ui/gui/text-data-import-dialog.c +++ b/src/ui/gui/text-data-import-dialog.c @@ -19,6 +19,7 @@ #include "ui/gui/text-data-import-dialog.h" #include "page-intro.h" +#include "page-sheet-spec.h" #include "page-first-line.h" #include "page-separators.h" #include "page-formats.h" @@ -77,6 +78,7 @@ text_data_import_assistant (PsppireDataWindow *dw) { GtkWindow *parent_window = GTK_WINDOW (dw); struct import_assistant *ia = init_assistant (parent_window); + struct sheet_spec_page *ssp ; if (!init_file (ia, parent_window)) { @@ -84,11 +86,18 @@ text_data_import_assistant (PsppireDataWindow *dw) return; } + ssp = ia->sheet_spec; + if (ia->spreadsheet) + { + ia->sheet_spec = sheet_spec_page_create (ia); + } + else { ia->intro = intro_page_create (ia); ia->separators = separators_page_create (ia); ia->first_line = first_line_page_create (ia); + printf ("%s:%d %p\n", __FILE__, __LINE__, ia->intro); } ia->formats = formats_page_create (ia); @@ -123,6 +132,7 @@ text_data_import_assistant (PsppireDataWindow *dw) break; } + if (ssp) { destroy_formats_page (ia); destroy_separators_page (ia); @@ -223,6 +233,7 @@ generate_syntax (const struct import_assistant *ia) { struct string s = DS_EMPTY_INITIALIZER; + if (ia->spreadsheet == NULL) { syntax_gen_pspp (&s, "GET DATA" @@ -232,7 +243,6 @@ generate_syntax (const struct import_assistant *ia) if (ia->file.encoding && strcmp (ia->file.encoding, "Auto")) syntax_gen_pspp (&s, " /ENCODING=%sq\n", ia->file.encoding); - intro_append_syntax (ia->intro, &s); @@ -245,7 +255,11 @@ generate_syntax (const struct import_assistant *ia) formats_append_syntax (ia, &s); apply_dict (ia->dict, &s); } - + else + { + return sheet_spec_gen_syntax (ia); + } + return ds_cstr (&s); } diff --git a/src/ui/gui/text-data-import.ui b/src/ui/gui/text-data-import.ui index d8a33d118a..a7a393ea11 100644 --- a/src/ui/gui/text-data-import.ui +++ b/src/ui/gui/text-data-import.ui @@ -653,17 +653,140 @@ The selected file contains N lines of text. Only the first M of these will be s - - 100 - 1 - 100 - 1 - 10 - - - 1000 - 100000000 - 1 - 10 + + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + 12 + Importing Spreadsheet Data + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + vertical + 12 + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Enter below the sheet number and the cell range which you wish to import. + True + + + 0 + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + 0 + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + 0 + none + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + 12 + + + True + 3 + 2 + + + True + True + + + + 1 + 2 + + + + + + True + True + 0 + + + 1 + 2 + 1 + 2 + + + + + + True + 1 + _Cells: + True + cell-range-entry + + + GTK_FILL + + + + + + True + 1 + _Sheet Index: + True + sheet-entry + + + 1 + 2 + GTK_FILL + + + + + + Use first row as _variable names + True + True + False + True + right + True + + + 2 + 2 + 3 + + + + + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + <b>Cells to Import</b> + True + + + + + + + False + False + 1 + + + +