1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2013 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "ui/gui/text-data-import-dialog.h"
23 #include <gtk-contrib/psppire-sheet.h>
29 #include "data/data-in.h"
30 #include "data/data-out.h"
31 #include "data/format-guesser.h"
32 #include "data/value-labels.h"
33 #include "data/gnumeric-reader.h"
34 #include "data/ods-reader.h"
35 #include "data/spreadsheet-reader.h"
36 #include "language/data-io/data-parser.h"
37 #include "language/lexer/lexer.h"
38 #include "libpspp/assertion.h"
39 #include "libpspp/i18n.h"
40 #include "libpspp/line-reader.h"
41 #include "libpspp/message.h"
42 #include "ui/gui/checkbox-treeview.h"
43 #include "ui/gui/dialog-common.h"
44 #include "ui/gui/executor.h"
45 #include "ui/gui/helper.h"
46 #include "ui/gui/builder-wrapper.h"
47 #include "ui/gui/psppire-data-window.h"
48 #include "ui/gui/psppire-dialog.h"
49 #include "ui/gui/psppire-encoding-selector.h"
50 #include "ui/gui/psppire-empty-list-store.h"
51 #include "ui/gui/psppire-var-sheet.h"
52 #include "ui/gui/psppire-var-store.h"
53 #include "ui/gui/psppire-scanf.h"
54 #include "ui/syntax-gen.h"
56 #include <data/casereader.h>
59 #include "gl/intprops.h"
60 #include "gl/xalloc.h"
63 #define _(msgid) gettext (msgid)
64 #define N_(msgid) msgid
66 struct import_assistant;
70 /* The "sheet-spec" page of the assistant. */
72 /* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */
73 struct sheet_spec_page
76 struct casereader *reader;
77 struct dictionary *dict;
79 struct spreadsheet_read_info sri;
80 struct spreadsheet_read_options opts;
84 /* Initializes IA's sheet_spec substructure. */
85 struct sheet_spec_page *
86 sheet_spec_page_create (struct import_assistant *ia)
88 GtkBuilder *builder = ia->asst.builder;
89 struct sheet_spec_page *p = xzalloc (sizeof (*p));
91 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
92 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
93 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
94 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
101 /* Prepares IA's sheet_spec page. */
103 prepare_sheet_spec_page (struct import_assistant *ia)
105 GtkBuilder *builder = ia->asst.builder;
106 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
107 printf ("%s:%d %p\n", __FILE__, __LINE__, ia->spreadsheet);
108 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
109 psppire_spreadsheet_model_new (ia->spreadsheet));
111 gtk_combo_box_set_active (sheet_entry, 0);
115 /* Resets IA's sheet_spec page to its initial state. */
117 reset_sheet_spec_page (struct import_assistant *ia)
119 printf ("%s\n", __FUNCTION__);
122 /* Called when the Forward button is clicked,
123 but before displaying the new page.
126 post_sheet_spec_page (struct import_assistant *ia)
133 GtkBuilder *builder = ia->asst.builder;
135 struct sheet_spec_page *ssp = ia->sheet_spec;
136 struct casereader *creader = NULL;
137 struct dictionary *dict = NULL;
139 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
140 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
141 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
143 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
149 ssp->opts.sheet_name = NULL;
150 ssp->opts.cell_range = NULL;
151 ssp->opts.sheet_index = num;
153 if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
155 ssp->opts.cell_range = range;
158 ssp->sri.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
161 switch (ia->spreadsheet->type)
163 case SPREADSHEET_ODS:
165 creader = ods_make_reader (ia->spreadsheet, &ssp->sri, &ssp->opts);
166 dict = ia->spreadsheet->dict;
169 case SPREADSHEET_GNUMERIC:
171 creader = gnumeric_make_reader (ia->spreadsheet, &ssp->sri, &ssp->opts);
172 dict = ia->spreadsheet->dict;
176 g_assert_not_reached ();
181 ssp->reader = creader;
185 update_assistant (ia);
189 GtkWidget * dialog = gtk_message_dialog_new (NULL,
193 _("An error occurred reading the spreadsheet file."));
195 gtk_dialog_run (GTK_DIALOG (dialog));
196 gtk_widget_destroy (dialog);
202 Update IA according to the contents of DICT and CREADER.
203 CREADER will be destroyed by this function.
206 update_assistant (struct import_assistant *ia)
208 struct sheet_spec_page *ssp = ia->sheet_spec;
216 ia->column_cnt = dict_get_var_cnt (ssp->dict);
217 ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
218 for (col = 0; col < ia->column_cnt ; ++col)
220 const struct variable *var = dict_get_var (ssp->dict, col);
221 ia->columns[col].name = xstrdup (var_get_name (var));
222 ia->columns[col].contents = NULL;
225 for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
228 for (col = 0; col < ia->column_cnt ; ++col)
231 const struct variable *var = dict_get_var (ssp->dict, col);
233 ia->columns[col].contents = xrealloc (ia->columns[col].contents,
234 sizeof (struct substring) * rows);
236 ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict),
237 var_get_print_format (var));
239 ia->columns[col].contents[rows - 1] = ss_cstr (ss);
242 if (rows > MAX_PREVIEW_LINES)
250 // file->line_cnt = rows;