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"
30 #include "data/data-in.h"
31 #include "data/data-out.h"
32 #include "data/format-guesser.h"
33 #include "data/value-labels.h"
34 #include "data/gnumeric-reader.h"
35 #include "data/ods-reader.h"
36 #include "data/spreadsheet-reader.h"
37 #include "language/data-io/data-parser.h"
38 #include "language/lexer/lexer.h"
39 #include "libpspp/assertion.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/line-reader.h"
42 #include "libpspp/message.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-scanf.h"
53 #include "ui/syntax-gen.h"
55 #include "ui/gui/psppire-spreadsheet-model.h"
57 #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;
68 /* The "sheet-spec" page of the assistant. */
70 /* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */
71 struct sheet_spec_page
74 struct casereader *reader;
75 struct dictionary *dict;
77 struct spreadsheet_read_options opts;
82 sheet_spec_gen_syntax (const struct import_assistant *ia)
84 const struct sheet_spec_page *ssp = ia->sheet_spec;
85 GtkBuilder *builder = ia->asst.builder;
86 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
87 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
89 struct string s = DS_EMPTY_INITIALIZER;
97 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
99 ssp->opts.sheet_index,
100 ssp->opts.read_names ? "ON" : "OFF");
103 if (range && 0 != strcmp ("", range))
106 "\n /CELLRANGE=RANGE %sq", range);
111 "\n /CELLRANGE=FULL");
115 syntax_gen_pspp (&s, ".");
123 on_sheet_combo_changed (GtkComboBox *cb, struct import_assistant *ia)
127 GtkTreeModel *model = gtk_combo_box_get_model (cb);
128 GtkBuilder *builder = ia->asst.builder;
129 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
131 gtk_combo_box_get_active_iter (cb, &iter);
132 gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
133 gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : "");
137 /* Initializes IA's sheet_spec substructure. */
138 struct sheet_spec_page *
139 sheet_spec_page_create (struct import_assistant *ia)
141 GtkBuilder *builder = ia->asst.builder;
142 struct sheet_spec_page *p = xzalloc (sizeof (*p));
144 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
145 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
146 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
147 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
151 g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
153 add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
154 GTK_ASSISTANT_PAGE_INTRO);
159 /* Prepares IA's sheet_spec page. */
161 prepare_sheet_spec_page (struct import_assistant *ia)
163 GtkBuilder *builder = ia->asst.builder;
164 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
165 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
167 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
168 psppire_spreadsheet_model_new (ia->spreadsheet));
170 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
172 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (readnames_checkbox), FALSE);
176 /* Resets IA's sheet_spec page to its initial state. */
178 reset_sheet_spec_page (struct import_assistant *ia)
180 GtkBuilder *builder = ia->asst.builder;
181 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
182 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
184 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
186 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (readnames_checkbox), FALSE);
189 /* Called when the Forward button is clicked,
190 but before displaying the new page.
193 post_sheet_spec_page (struct import_assistant *ia)
200 GtkBuilder *builder = ia->asst.builder;
202 struct sheet_spec_page *ssp = ia->sheet_spec;
203 struct casereader *creader = NULL;
204 struct dictionary *dict = NULL;
206 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
207 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
208 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
209 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
211 gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box));
213 ssp->opts.sheet_name = NULL;
214 ssp->opts.cell_range = NULL;
215 ssp->opts.sheet_index = num + 1;
217 if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
219 ssp->opts.cell_range = g_strdup (range);
222 ssp->opts.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
225 switch (ia->spreadsheet->type)
227 case SPREADSHEET_ODS:
228 case SPREADSHEET_GNUMERIC:
230 creader = spreadsheet_make_reader (ia->spreadsheet, &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;