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 "page-sheet-spec.h"
21 #include "ui/gui/text-data-import-dialog.h"
25 #include <gtk-contrib/psppire-sheet.h>
31 #include "data/data-in.h"
32 #include "data/data-out.h"
33 #include "data/format-guesser.h"
34 #include "data/value-labels.h"
35 #include "data/gnumeric-reader.h"
36 #include "data/ods-reader.h"
37 #include "data/spreadsheet-reader.h"
38 #include "language/data-io/data-parser.h"
39 #include "language/lexer/lexer.h"
40 #include "libpspp/assertion.h"
41 #include "libpspp/i18n.h"
42 #include "libpspp/line-reader.h"
43 #include "libpspp/message.h"
44 #include "ui/gui/checkbox-treeview.h"
45 #include "ui/gui/dialog-common.h"
46 #include "ui/gui/executor.h"
47 #include "ui/gui/helper.h"
48 #include "ui/gui/builder-wrapper.h"
49 #include "ui/gui/psppire-data-window.h"
50 #include "ui/gui/psppire-dialog.h"
51 #include "ui/gui/psppire-encoding-selector.h"
52 #include "ui/gui/psppire-empty-list-store.h"
53 #include "ui/gui/psppire-var-sheet.h"
54 #include "ui/gui/psppire-var-store.h"
55 #include "ui/gui/psppire-scanf.h"
56 #include "ui/syntax-gen.h"
58 #include "ui/gui/psppire-spreadsheet-model.h"
60 #include <data/casereader.h>
63 #include "gl/intprops.h"
64 #include "gl/xalloc.h"
67 #define _(msgid) gettext (msgid)
68 #define N_(msgid) msgid
70 struct import_assistant;
72 /* The "sheet-spec" page of the assistant. */
74 /* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */
75 struct sheet_spec_page
78 struct casereader *reader;
79 struct dictionary *dict;
81 struct spreadsheet_read_info sri;
82 struct spreadsheet_read_options opts;
87 sheet_spec_gen_syntax (const struct import_assistant *ia)
89 const struct sheet_spec_page *ssp = ia->sheet_spec;
91 struct string s = DS_EMPTY_INITIALIZER;
99 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
101 ssp->opts.sheet_index,
102 ssp->sri.read_names ? "ON" : "OFF");
105 if ( ssp->opts.cell_range)
108 "\n /CELLRANGE=RANGE %sq",
109 ssp->opts.cell_range);
114 "\n /CELLRANGE=FULL");
118 syntax_gen_pspp (&s, ".");
126 on_sheet_combo_changed (GtkComboBox *cb, struct import_assistant *ia)
130 GtkTreeModel *model = gtk_combo_box_get_model (cb);
131 GtkBuilder *builder = ia->asst.builder;
132 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
134 gtk_combo_box_get_active_iter (cb, &iter);
135 gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
136 gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : "");
140 /* Initializes IA's sheet_spec substructure. */
141 struct sheet_spec_page *
142 sheet_spec_page_create (struct import_assistant *ia)
144 GtkBuilder *builder = ia->asst.builder;
145 struct sheet_spec_page *p = xzalloc (sizeof (*p));
147 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
148 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
149 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
150 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
154 g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
156 add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
157 GTK_ASSISTANT_PAGE_INTRO);
162 /* Prepares IA's sheet_spec page. */
164 prepare_sheet_spec_page (struct import_assistant *ia)
166 GtkBuilder *builder = ia->asst.builder;
167 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
169 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
170 psppire_spreadsheet_model_new (ia->spreadsheet));
172 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
176 /* Resets IA's sheet_spec page to its initial state. */
178 reset_sheet_spec_page (struct import_assistant *ia)
180 printf ("%s\n", __FUNCTION__);
183 /* Called when the Forward button is clicked,
184 but before displaying the new page.
187 post_sheet_spec_page (struct import_assistant *ia)
194 GtkBuilder *builder = ia->asst.builder;
196 struct sheet_spec_page *ssp = ia->sheet_spec;
197 struct casereader *creader = NULL;
198 struct dictionary *dict = NULL;
200 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
201 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
203 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
204 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
206 gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box));
208 ssp->opts.sheet_name = NULL;
209 ssp->opts.cell_range = NULL;
210 ssp->opts.sheet_index = num + 1;
212 if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
214 ssp->opts.cell_range = range;
217 ssp->sri.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
220 switch (ia->spreadsheet->type)
222 case SPREADSHEET_ODS:
224 creader = ods_make_reader (ia->spreadsheet, &ssp->sri, &ssp->opts);
225 dict = ia->spreadsheet->dict;
228 case SPREADSHEET_GNUMERIC:
230 creader = gnumeric_make_reader (ia->spreadsheet, &ssp->sri, &ssp->opts);
231 dict = ia->spreadsheet->dict;
235 g_assert_not_reached ();
240 ssp->reader = creader;
244 update_assistant (ia);
248 GtkWidget * dialog = gtk_message_dialog_new (NULL,
252 _("An error occurred reading the spreadsheet file."));
254 gtk_dialog_run (GTK_DIALOG (dialog));
255 gtk_widget_destroy (dialog);
261 Update IA according to the contents of DICT and CREADER.
262 CREADER will be destroyed by this function.
265 update_assistant (struct import_assistant *ia)
267 struct sheet_spec_page *ssp = ia->sheet_spec;
275 ia->column_cnt = dict_get_var_cnt (ssp->dict);
276 ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
277 for (col = 0; col < ia->column_cnt ; ++col)
279 const struct variable *var = dict_get_var (ssp->dict, col);
280 ia->columns[col].name = xstrdup (var_get_name (var));
281 ia->columns[col].contents = NULL;
284 for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
287 for (col = 0; col < ia->column_cnt ; ++col)
290 const struct variable *var = dict_get_var (ssp->dict, col);
292 ia->columns[col].contents = xrealloc (ia->columns[col].contents,
293 sizeof (struct substring) * rows);
295 ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict),
296 var_get_print_format (var));
298 ia->columns[col].contents[rows - 1] = ss_cstr (ss);
301 if (rows > MAX_PREVIEW_LINES)
309 ia->file.line_cnt = rows;