1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2015, 2016 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/>. */
25 #include "data/casereader.h"
26 #include "data/data-in.h"
27 #include "data/data-out.h"
28 #include "data/dictionary.h"
29 #include "data/format-guesser.h"
30 #include "data/format.h"
31 #include "data/gnumeric-reader.h"
32 #include "data/ods-reader.h"
33 #include "data/spreadsheet-reader.h"
34 #include "data/value-labels.h"
36 #include "gl/intprops.h"
38 #include "libpspp/i18n.h"
39 #include "libpspp/line-reader.h"
40 #include "libpspp/message.h"
41 #include "libpspp/str.h"
43 #include "builder-wrapper.h"
45 #include "pspp-sheet-view.h"
46 #include "pspp-sheet-selection.h"
47 #include "psppire-import-assistant.h"
48 #include "psppire-scanf.h"
49 #include "psppire-dialog.h"
50 #include "psppire-empty-list-store.h"
51 #include "psppire-encoding-selector.h"
52 #include "psppire-spreadsheet-model.h"
53 #include "psppire-var-sheet.h"
54 #include "ui/syntax-gen.h"
57 #define _(msgid) gettext (msgid)
58 #define N_(msgid) msgid
60 enum { MAX_LINE_LEN = 16384 }; /* Max length of an acceptable line. */
63 /* Sets IA's separators substructure to match the widgets. */
64 static void get_separators (PsppireImportAssistant *ia);
65 static void split_fields (PsppireImportAssistant *ia);
67 /* Chooses a name for each column on the separators page */
68 static void choose_column_names (PsppireImportAssistant *ia);
71 /* Frees IA's file substructure. */
72 static void destroy_file (PsppireImportAssistant *ia);
74 static void clear_fields (PsppireImportAssistant *ia);
77 static void intro_page_create (PsppireImportAssistant *ia);
78 static void first_line_page_create (PsppireImportAssistant *ia);
80 static gboolean process_file (PsppireImportAssistant *ia);
83 static GtkWidget * create_data_tree_view (gboolean input, GtkContainer *parent,
84 PsppireImportAssistant *ia);
86 static void separators_page_create (PsppireImportAssistant *ia);
87 static void formats_page_create (PsppireImportAssistant *ia);
89 static void push_watch_cursor (PsppireImportAssistant *ia);
90 static void pop_watch_cursor (PsppireImportAssistant *ia);
94 static void psppire_import_assistant_init (PsppireImportAssistant *act);
95 static void psppire_import_assistant_class_init (PsppireImportAssistantClass *class);
97 G_DEFINE_TYPE (PsppireImportAssistant, psppire_import_assistant, GTK_TYPE_ASSISTANT);
107 psppire_import_assistant_set_property (GObject *object,
112 // PsppireImportAssistant *act = PSPPIRE_IMPORT_ASSISTANT (object);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 psppire_import_assistant_get_property (GObject *object,
129 // PsppireImportAssistant *assistant = PSPPIRE_IMPORT_ASSISTANT (object);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 static GObjectClass * parent_class = NULL;
142 static void destroy_columns (PsppireImportAssistant *ia);
145 psppire_import_assistant_finalize (GObject *object)
147 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (object);
151 spreadsheet_unref (ia->spreadsheet);
153 // clear_fields (ia);
154 destroy_columns (ia);
156 ds_destroy (&ia->separators);
157 ds_destroy (&ia->quotes);
159 g_object_unref (ia->builder);
163 g_object_unref (ia->prop_renderer);
164 g_object_unref (ia->fixed_renderer);
166 if (G_OBJECT_CLASS (parent_class)->finalize)
167 G_OBJECT_CLASS (parent_class)->finalize (object);
172 psppire_import_assistant_class_init (PsppireImportAssistantClass *class)
174 GObjectClass *object_class = G_OBJECT_CLASS (class);
176 parent_class = g_type_class_peek_parent (class);
178 object_class->set_property = psppire_import_assistant_set_property;
179 object_class->get_property = psppire_import_assistant_get_property;
181 object_class->finalize = psppire_import_assistant_finalize;
185 /* Causes the assistant to close, returning RESPONSE for
186 interpretation by text_data_import_assistant. */
188 close_assistant (PsppireImportAssistant *ia, int response)
190 ia->response = response;
191 g_main_loop_quit (ia->main_loop);
192 gtk_widget_hide (GTK_WIDGET (ia));
196 /* Called when the Paste button on the last page of the assistant
199 on_paste (GtkButton *button, PsppireImportAssistant *ia)
201 close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
204 /* Revises the contents of the fields tree view based on the
205 currently chosen set of separators. */
207 revise_fields_preview (PsppireImportAssistant *ia)
209 push_watch_cursor (ia);
213 choose_column_names (ia);
214 ia->fields_tree_view =
215 GTK_WIDGET (create_data_tree_view (TRUE,
216 GTK_CONTAINER (get_widget_assert (ia->builder, "fields-scroller")),
219 pop_watch_cursor (ia);
222 /* Chooses the most common character among those in TARGETS,
223 based on the frequency data in HISTOGRAM, and stores it in
224 RESULT. If there is a tie for the most common character among
225 those in TARGETS, the earliest character is chosen. If none
226 of the TARGETS appear at all, then DEF is used as a
229 find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
230 const char *targets, const char *def,
231 struct string *result)
233 unsigned char max = 0;
234 unsigned long int max_count = 0;
236 for (; *targets != '\0'; targets++)
238 unsigned char c = *targets;
239 unsigned long int count = histogram[c];
240 if (count > max_count)
249 ds_put_byte (result, max);
252 ds_assign_cstr (result, def);
256 /* Picks the most likely separator and quote characters based on
259 choose_likely_separators (PsppireImportAssistant *ia)
261 unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
264 /* Construct a histogram of all the characters used in the
266 for (row = 0; row < ia->line_cnt; row++)
268 struct substring line = ds_ss (&ia->lines[row]);
269 size_t length = ss_length (line);
271 for (i = 0; i < length; i++)
272 histogram[(unsigned char) line.string[i]]++;
275 find_commonest_chars (histogram, "\"'", "", &ia->quotes);
276 find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators);
280 static void set_separators (PsppireImportAssistant *ia);
282 /* Called just before the separators page becomes visible in the
283 assistant, and when the Reset button is clicked. */
285 prepare_separators_page (PsppireImportAssistant *ia, GtkWidget *page)
287 revise_fields_preview (ia);
288 choose_likely_separators (ia);
294 const char *name; /* Name (for use with get_widget_assert). */
295 int c; /* Separator character. */
298 /* All the separators in the dialog box. */
299 static const struct separator separators[] =
311 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
315 /* Sets the widgets to match IA's separators substructure. */
317 set_separators (PsppireImportAssistant *ia)
320 struct string custom;
325 ds_init_empty (&custom);
327 for (i = 0; i < ds_length (&ia->separators); i++)
329 unsigned char c = ds_at (&ia->separators, i);
332 for (j = 0; j < SEPARATOR_CNT; j++)
334 const struct separator *s = &separators[j];
342 ds_put_byte (&custom, c);
346 for (i = 0; i < SEPARATOR_CNT; i++)
348 const struct separator *s = &separators[i];
349 GtkWidget *button = get_widget_assert (ia->builder, s->name);
350 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
351 (seps & (1u << i)) != 0);
353 any_custom = !ds_is_empty (&custom);
354 gtk_entry_set_text (GTK_ENTRY (ia->custom_entry), ds_cstr (&custom));
355 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->custom_cb),
357 gtk_widget_set_sensitive (ia->custom_entry, any_custom);
358 ds_destroy (&custom);
360 any_quotes = !ds_is_empty (&ia->quotes);
362 gtk_entry_set_text (ia->quote_entry,
363 any_quotes ? ds_cstr (&ia->quotes) : "\"");
364 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->quote_cb),
366 gtk_widget_set_sensitive (ia->quote_combo, any_quotes);
370 /* Resets IA's intro page to its initial state. */
372 reset_intro_page (PsppireImportAssistant *ia)
374 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->all_cases_button),
380 /* Clears the set of user-modified variables from IA's formats
381 substructure. This discards user modifications to variable
382 formats, thereby causing formats to revert to their
385 reset_formats_page (PsppireImportAssistant *ia, GtkWidget *page)
389 for (i = 0; i < ia->modified_var_cnt; i++)
390 var_destroy (ia->modified_vars[i]);
391 free (ia->modified_vars);
392 ia->modified_vars = NULL;
393 ia->modified_var_cnt = 0;
396 static void prepare_formats_page (PsppireImportAssistant *ia);
398 /* Called when the Reset button is clicked. */
400 on_reset (GtkButton *button, PsppireImportAssistant *ia)
402 gint pn = gtk_assistant_get_current_page (GTK_ASSISTANT (ia));
404 GtkWidget *page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
406 page_func *on_reset = g_object_get_data (G_OBJECT (page), "on-reset");
415 next_page_func (gint old_page, gpointer data)
421 /* Called just before PAGE is displayed as the current page of
422 IMPORT_ASSISTANT, this updates IA content according to the new
425 on_prepare (GtkAssistant *assistant, GtkWidget *page, PsppireImportAssistant *ia)
427 gtk_widget_show (ia->reset_button);
428 gtk_widget_hide (ia->paste_button);
430 gint pn = gtk_assistant_get_current_page (assistant);
431 gint previous_page_index = ia->current_page;
433 if (previous_page_index >= 0)
435 GtkWidget *closing_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), previous_page_index);
437 if (pn > previous_page_index)
439 page_func *on_forward = g_object_get_data (G_OBJECT (closing_page), "on-forward");
442 on_forward (ia, closing_page);
446 page_func *on_back = g_object_get_data (G_OBJECT (closing_page), "on-back");
449 on_back (ia, closing_page);
454 GtkWidget *new_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
456 page_func *on_entering = g_object_get_data (G_OBJECT (new_page), "on-entering");
459 on_entering (ia, new_page);
462 ia->current_page = pn;
465 /* Called when the Cancel button in the assistant is clicked. */
467 on_cancel (GtkAssistant *assistant, PsppireImportAssistant *ia)
469 close_assistant (ia, GTK_RESPONSE_CANCEL);
472 /* Called when the Apply button on the last page of the assistant
475 on_close (GtkAssistant *assistant, PsppireImportAssistant *ia)
477 close_assistant (ia, GTK_RESPONSE_APPLY);
481 /* Frees IA's file substructure. */
483 destroy_file (PsppireImportAssistant *ia)
487 for (i = 0; i < ia->line_cnt; i++)
488 ds_destroy (&ia->lines[i]);
490 g_free (ia->file_name);
491 ia->file_name = NULL;
493 g_free (ia->encoding);
498 /* Increments the "watch cursor" level, setting the cursor for
499 the assistant window to a watch face to indicate to the user
500 that the ongoing operation may take some time. */
502 push_watch_cursor (PsppireImportAssistant *ia)
504 if (++ia->watch_cursor == 1)
506 GtkWidget *widget = GTK_WIDGET (ia);
507 GdkDisplay *display = gtk_widget_get_display (widget);
508 GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
509 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
510 g_object_unref (cursor);
511 gdk_display_flush (display);
515 /* Decrements the "watch cursor" level. If the level reaches
516 zero, the cursor is reset to its default shape. */
518 pop_watch_cursor (PsppireImportAssistant *ia)
520 if (--ia->watch_cursor == 0)
522 GtkWidget *widget = GTK_WIDGET (ia);
523 gdk_window_set_cursor (gtk_widget_get_window (widget), NULL);
529 process_file (PsppireImportAssistant *ia)
532 struct line_reader *reader = line_reader_for_file (ia->encoding, ia->file_name, O_RDONLY);
535 msg_error (errno, _("Could not open `%s'"),
540 ds_init_empty (&input);
541 for (ia->line_cnt = 0; ia->line_cnt < MAX_PREVIEW_LINES; ia->line_cnt++)
544 if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
545 || ds_length (&input) > MAX_LINE_LEN)
547 if (line_reader_eof (reader))
549 else if (line_reader_error (reader))
550 msg (ME, _("Error reading `%s': %s"),
551 ia->file_name, strerror (line_reader_error (reader)));
553 msg (ME, _("Failed to read `%s', because it contains a line "
554 "over %d bytes long and therefore appears not to be "
556 ia->file_name, MAX_LINE_LEN);
557 line_reader_close (reader);
563 char *s = recode_string ("UTF-8", line_reader_get_encoding (reader), ds_cstr (&input), ds_length (&input));
564 ds_init_cstr (&ia->lines[ia->line_cnt], s);
568 if (ia->line_cnt == 0)
570 msg (ME, _("`%s' is empty."), ia->file_name);
571 line_reader_close (reader);
576 /* Estimate the number of lines in the file. */
577 if (ia->line_cnt < MAX_PREVIEW_LINES)
579 ia->total_lines = ia->line_cnt;
580 ia->total_is_exact = true;
585 off_t position = line_reader_tell (reader);
586 if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
588 ia->total_lines = (double) ia->line_cnt / position * s.st_size;
589 ia->total_is_exact = false;
594 ia->total_is_exact = true;
597 line_reader_close (reader);
603 render_line_number (PsppSheetViewColumn *tree_column,
604 GtkCellRenderer *cell,
605 GtkTreeModel *tree_model,
609 gint row = empty_list_store_iter_to_row (iter);
610 char s[INT_BUFSIZE_BOUND (int)];
611 int first_line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model),
613 sprintf (s, "%d", first_line + row);
614 g_object_set (cell, "text", s, NULL);
620 get_string_width (GtkWidget *treeview, GtkCellRenderer *renderer,
624 g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
625 gtk_cell_renderer_get_preferred_width (renderer, treeview,
632 get_monospace_width (GtkWidget *treeview, GtkCellRenderer *renderer,
639 ds_put_byte_multiple (&s, '0', char_cnt);
640 ds_put_byte (&s, ' ');
641 width = get_string_width (treeview, renderer, ds_cstr (&s));
648 add_line_number_column (const PsppireImportAssistant *ia,
651 PsppSheetViewColumn *column =
652 pspp_sheet_view_column_new_with_attributes (_("Line"), ia->prop_renderer, (void *) NULL);
654 pspp_sheet_view_column_set_fixed_width (column, get_monospace_width (treeview, ia->prop_renderer, 5));
656 pspp_sheet_view_column_set_resizable (column, TRUE);
658 pspp_sheet_view_column_set_cell_data_func (column, ia->prop_renderer,
659 render_line_number, NULL, NULL);
661 pspp_sheet_view_append_column (PSPP_SHEET_VIEW (treeview), column);
666 set_model_on_treeview (PsppireImportAssistant *ia, GtkWidget *tree_view, size_t first_line)
668 GtkTreeModel *model = GTK_TREE_MODEL (psppire_empty_list_store_new (ia->line_cnt - first_line));
670 g_object_set_data (G_OBJECT (model), "lines", &ia->lines + first_line);
671 g_object_set_data (G_OBJECT (model), "first-line", GINT_TO_POINTER (first_line));
673 pspp_sheet_view_set_model (PSPP_SHEET_VIEW (tree_view), model);
675 g_object_unref (model);
680 make_tree_view (const PsppireImportAssistant *ia)
682 GtkWidget *tree_view = pspp_sheet_view_new ();
683 pspp_sheet_view_set_grid_lines (PSPP_SHEET_VIEW (tree_view), PSPP_SHEET_VIEW_GRID_LINES_BOTH);
685 add_line_number_column (ia, tree_view);
691 add_page_to_assistant (PsppireImportAssistant *ia,
692 GtkWidget *page, GtkAssistantPageType type, const gchar *);
696 on_sheet_combo_changed (GtkComboBox *cb, PsppireImportAssistant *ia)
700 GtkTreeModel *model = gtk_combo_box_get_model (cb);
701 GtkBuilder *builder = ia->builder;
702 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
704 gtk_combo_box_get_active_iter (cb, &iter);
705 gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
706 gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : "");
710 /* Prepares IA's sheet_spec page. */
712 prepare_sheet_spec_page (PsppireImportAssistant *ia)
714 GtkBuilder *builder = ia->builder;
715 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
716 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
718 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
719 psppire_spreadsheet_model_new (ia->spreadsheet));
721 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
723 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (readnames_checkbox), FALSE);
728 /* Initializes IA's sheet_spec substructure. */
730 sheet_spec_page_create (PsppireImportAssistant *ia)
732 GtkBuilder *builder = ia->builder;
733 GtkWidget *page = get_widget_assert (builder, "Spreadsheet-Importer");
735 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
736 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
737 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box));
738 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
739 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
743 g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
745 add_page_to_assistant (ia, page,
746 GTK_ASSISTANT_PAGE_CONTENT, _("Importing Spreadsheet Data"));
748 g_object_set_data (G_OBJECT (page), "on-entering", prepare_sheet_spec_page);
753 on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
755 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
756 gchar *f = gtk_file_chooser_get_filename (fc);
759 for(i = gtk_assistant_get_n_pages (GTK_ASSISTANT (ia)); i > 0; --i)
760 gtk_assistant_remove_page (GTK_ASSISTANT (ia), i);
762 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), FALSE);
764 if (f && !g_file_test (f, G_FILE_TEST_IS_DIR))
766 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), TRUE);
769 spreadsheet_unref (ia->spreadsheet);
771 ia->spreadsheet = gnumeric_probe (f, FALSE);
773 if (!ia->spreadsheet)
774 ia->spreadsheet = ods_probe (f, FALSE);
776 if (!ia->spreadsheet)
778 intro_page_create (ia);
779 first_line_page_create (ia);
780 separators_page_create (ia);
784 sheet_spec_page_create (ia);
787 formats_page_create (ia);
793 /* This has to be done on a map signal callback,
794 because GtkFileChooserWidget resets everything when it is mapped. */
796 on_map (PsppireImportAssistant *ia, GtkWidget *page)
798 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
801 gtk_file_chooser_set_filename (fc, ia->file_name);
803 on_chosen (ia, page);
807 chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page)
812 chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page)
816 g_free (ia->file_name);
817 ia->file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
820 g_free (ia->encoding);
821 ia->encoding = psppire_encoding_selector_get_encoding (ia->encoding_selector);
823 if (!ia->spreadsheet)
828 chooser_page_reset (PsppireImportAssistant *ia, GtkWidget *page)
830 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
832 gtk_file_chooser_set_filter (fc, ia->default_filter);
833 gtk_file_chooser_unselect_all (fc);
835 on_chosen (ia, page);
839 chooser_page_create (PsppireImportAssistant *ia)
841 GtkFileFilter *filter = NULL;
843 GtkWidget *chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
845 g_object_set_data (G_OBJECT (chooser), "on-forward", chooser_page_leave);
846 g_object_set_data (G_OBJECT (chooser), "on-reset", chooser_page_reset);
847 g_object_set_data (G_OBJECT (chooser), "on-entering",chooser_page_enter);
849 g_object_set (chooser, "local-only", FALSE, NULL);
852 ia->default_filter = gtk_file_filter_new ();
853 gtk_file_filter_set_name (ia->default_filter, _("All Files"));
854 gtk_file_filter_add_pattern (ia->default_filter, "*");
855 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), ia->default_filter);
857 filter = gtk_file_filter_new ();
858 gtk_file_filter_set_name (filter, _("Text Files"));
859 gtk_file_filter_add_mime_type (filter, "text/*");
860 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
862 filter = gtk_file_filter_new ();
863 gtk_file_filter_set_name (filter, _("Text (*.txt) Files"));
864 gtk_file_filter_add_pattern (filter, "*.txt");
865 gtk_file_filter_add_pattern (filter, "*.TXT");
866 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
868 filter = gtk_file_filter_new ();
869 gtk_file_filter_set_name (filter, _("Plain Text (ASCII) Files"));
870 gtk_file_filter_add_mime_type (filter, "text/plain");
871 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
873 filter = gtk_file_filter_new ();
874 gtk_file_filter_set_name (filter, _("Comma Separated Value Files"));
875 gtk_file_filter_add_mime_type (filter, "text/csv");
876 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
878 /* I've never encountered one of these, but it's listed here:
879 http://www.iana.org/assignments/media-types/text/tab-separated-values */
880 filter = gtk_file_filter_new ();
881 gtk_file_filter_set_name (filter, _("Tab Separated Value Files"));
882 gtk_file_filter_add_mime_type (filter, "text/tab-separated-values");
883 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
885 filter = gtk_file_filter_new ();
886 gtk_file_filter_set_name (filter, _("Gnumeric Spreadsheet Files"));
887 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
888 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
890 filter = gtk_file_filter_new ();
891 gtk_file_filter_set_name (filter, _("OpenDocument Spreadsheet Files"));
892 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
893 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
895 filter = gtk_file_filter_new ();
896 gtk_file_filter_set_name (filter, _("All Spreadsheet Files"));
897 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
898 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
899 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
901 ia->encoding_selector = psppire_encoding_selector_new ("Auto", TRUE);
902 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (chooser), ia->encoding_selector);
904 add_page_to_assistant (ia, chooser,
905 GTK_ASSISTANT_PAGE_INTRO, _("Select File to Import"));
907 g_signal_connect_swapped (chooser, "selection-changed", G_CALLBACK (on_chosen), ia);
908 g_signal_connect_swapped (chooser, "map", G_CALLBACK (on_map), ia);
913 psppire_import_assistant_init (PsppireImportAssistant *ia)
915 ia->builder = builder_new ("text-data-import.ui");
917 ia->current_page = -1 ;
921 ia->file_name = NULL;
923 ia->spreadsheet = NULL;
924 ia->watch_cursor = 0;
926 ia->prop_renderer = gtk_cell_renderer_text_new ();
927 g_object_ref_sink (ia->prop_renderer);
928 ia->fixed_renderer = gtk_cell_renderer_text_new ();
929 g_object_ref_sink (ia->fixed_renderer);
930 g_object_set (G_OBJECT (ia->fixed_renderer),
931 "family", "Monospace",
934 g_signal_connect (ia, "prepare", G_CALLBACK (on_prepare), ia);
935 g_signal_connect (ia, "cancel", G_CALLBACK (on_cancel), ia);
936 g_signal_connect (ia, "close", G_CALLBACK (on_close), ia);
938 ia->paste_button = gtk_button_new_with_label (_("Paste"));
939 ia->reset_button = gtk_button_new_with_label (_("Reset"));
941 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->paste_button);
943 g_signal_connect (ia->paste_button, "clicked", G_CALLBACK (on_paste), ia);
944 g_signal_connect (ia->reset_button, "clicked", G_CALLBACK (on_reset), ia);
946 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->reset_button);
948 gtk_window_set_title (GTK_WINDOW (ia),
949 _("Importing Delimited Text Data"));
951 gtk_window_set_icon_name (GTK_WINDOW (ia), "pspp");
953 chooser_page_create (ia);
955 gtk_assistant_set_forward_page_func (GTK_ASSISTANT (ia), next_page_func, NULL, NULL);
957 gtk_window_maximize (GTK_WINDOW (ia));
961 /* Appends a page of the given TYPE, with PAGE as its content, to
962 the GtkAssistant encapsulated by IA. Returns the GtkWidget
963 that represents the page. */
965 add_page_to_assistant (PsppireImportAssistant *ia,
966 GtkWidget *page, GtkAssistantPageType type, const gchar *title)
968 GtkWidget *content = page;
970 gtk_assistant_append_page (GTK_ASSISTANT (ia), content);
971 gtk_assistant_set_page_type (GTK_ASSISTANT(ia), content, type);
972 gtk_assistant_set_page_title (GTK_ASSISTANT(ia), content, title);
973 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), content, TRUE);
979 /* Called when one of the radio buttons is clicked. */
981 on_intro_amount_changed (PsppireImportAssistant *p)
983 gtk_widget_set_sensitive (p->n_cases_spin,
984 gtk_toggle_button_get_active (
985 GTK_TOGGLE_BUTTON (p->n_cases_button)));
987 gtk_widget_set_sensitive (p->percent_spin,
988 gtk_toggle_button_get_active (
989 GTK_TOGGLE_BUTTON (p->percent_button)));
994 render_line (PsppSheetViewColumn *tree_column,
995 GtkCellRenderer *cell,
996 GtkTreeModel *tree_model,
1000 gint row = empty_list_store_iter_to_row (iter);
1001 struct string *lines;
1003 lines = g_object_get_data (G_OBJECT (tree_model), "lines");
1004 g_return_if_fail (lines != NULL);
1006 g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
1009 /* Sets the widgets to match IA's first_line substructure. */
1011 set_first_line (PsppireImportAssistant *ia)
1013 GtkTreePath *path = gtk_tree_path_new_from_indices (ia->skip_lines, -1);
1016 set_model_on_treeview (ia, ia->tree_view, 0);
1018 pspp_sheet_view_set_cursor (PSPP_SHEET_VIEW (ia->tree_view),
1020 gtk_tree_path_free (path);
1022 gtk_toggle_button_set_active (
1023 GTK_TOGGLE_BUTTON (ia->variable_names_cb),
1024 ia->variable_names);
1025 gtk_widget_set_sensitive (ia->variable_names_cb,
1026 ia->skip_lines > 0);
1030 /* Creates and returns a tree view that contains each of the
1031 lines in IA's file as a row. */
1033 create_lines_tree_view (GtkContainer *parent, PsppireImportAssistant *ia)
1035 size_t max_line_length;
1036 gint content_width, header_width;
1038 const gchar *title = _("Text");
1039 GtkWidget *tree_view = make_tree_view (ia);
1040 PsppSheetViewColumn *column =
1041 pspp_sheet_view_column_new_with_attributes (title,
1042 ia->fixed_renderer, (void *) NULL);
1044 pspp_sheet_view_column_set_cell_data_func (column, ia->fixed_renderer,
1045 render_line, NULL, NULL);
1046 pspp_sheet_view_column_set_resizable (column, TRUE);
1047 pspp_sheet_view_column_set_expand (column, TRUE);
1049 max_line_length = 0;
1050 for (i = 0; i < ia->line_cnt; i++)
1052 size_t w = ds_length (&ia->lines[i]);
1053 max_line_length = MAX (max_line_length, w);
1056 content_width = get_monospace_width (tree_view, ia->fixed_renderer,
1058 header_width = get_string_width (tree_view, ia->prop_renderer, title);
1059 pspp_sheet_view_column_set_fixed_width (column, MAX (content_width,
1061 pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), column);
1063 GtkWidget *oldtv = gtk_bin_get_child (GTK_BIN (parent));
1065 gtk_container_remove (parent, oldtv);
1067 gtk_container_add (parent, tree_view);
1068 gtk_widget_show (tree_view);
1074 /* Sets IA's first_line substructure to match the widgets. */
1076 set_first_line_options (PsppireImportAssistant *ia)
1079 GtkTreeModel *model;
1081 PsppSheetSelection *selection = pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view));
1082 if (pspp_sheet_selection_get_selected (selection, &model, &iter))
1084 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
1085 int row = gtk_tree_path_get_indices (path)[0];
1086 gtk_tree_path_free (path);
1088 ia->skip_lines = row;
1089 ia->variable_names =
1091 && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)));
1094 gtk_widget_set_sensitive (ia->variable_names_cb, ia->skip_lines > 0);
1098 reset_first_line_page (PsppireImportAssistant *ia)
1100 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb), FALSE);
1101 PsppSheetSelection *selection = pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view));
1102 pspp_sheet_selection_unselect_all (selection);
1103 gtk_widget_set_sensitive (ia->variable_names_cb, FALSE);
1107 /* Initializes IA's first_line substructure. */
1109 first_line_page_create (PsppireImportAssistant *ia)
1111 GtkWidget *w = get_widget_assert (ia->builder, "FirstLine");
1113 g_object_set_data (G_OBJECT (w), "on-entering", set_first_line);
1115 add_page_to_assistant (ia, w,
1116 GTK_ASSISTANT_PAGE_CONTENT, _("Select the First Line"));
1118 ia->tree_view = GTK_WIDGET (create_lines_tree_view (
1119 GTK_CONTAINER (get_widget_assert (ia->builder, "first-line-scroller")), ia));
1120 ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
1121 pspp_sheet_selection_set_mode (
1122 pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
1123 PSPP_SHEET_SELECTION_BROWSE);
1124 pspp_sheet_view_set_rubber_banding (PSPP_SHEET_VIEW (ia->tree_view), TRUE);
1127 g_signal_connect_swapped (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
1128 "changed", G_CALLBACK (set_first_line_options), ia);
1130 g_signal_connect_swapped (ia->variable_names_cb, "toggled",
1131 G_CALLBACK (set_first_line_options), ia);
1134 g_object_set_data (G_OBJECT (w), "on-reset", reset_first_line_page);
1139 intro_on_enter (PsppireImportAssistant *ia)
1141 GtkBuilder *builder = ia->builder;
1142 GtkWidget *table = get_widget_assert (builder, "button-table");
1146 if (ia->line_cnt > MAX_PREVIEW_LINES)
1147 ia->line_cnt = MAX_PREVIEW_LINES;
1150 ds_put_cstr (&s, _("This assistant will guide you through the process of "
1151 "importing data into PSPP from a text file with one line "
1152 "per case, in which fields are separated by tabs, "
1153 "commas, or other delimiters.\n\n"));
1155 if (ia->total_is_exact)
1158 &s, ngettext ("The selected file contains %'lu line of text. ",
1159 "The selected file contains %'lu lines of text. ",
1163 else if (ia->total_lines > 0)
1167 "The selected file contains approximately %'lu line of text. ",
1168 "The selected file contains approximately %'lu lines of text. ",
1173 "Only the first %zu line of the file will be shown for "
1174 "preview purposes in the following screens. ",
1175 "Only the first %zu lines of the file will be shown for "
1176 "preview purposes in the following screens. ",
1181 ds_put_cstr (&s, _("You may choose below how much of the file should "
1182 "actually be imported."));
1184 gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
1188 GtkWidget *w = gtk_grid_get_child_at (GTK_GRID (table), 1, 1);
1189 int old_value = w ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) : 1;
1191 gtk_container_remove (GTK_CONTAINER (table), w);
1193 w = gtk_grid_get_child_at (GTK_GRID (table), 1, 2);
1195 gtk_container_remove (GTK_CONTAINER (table), w);
1198 GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
1200 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (ia->n_cases_spin));
1201 gtk_adjustment_set_lower (adj, 1.0);
1202 if (ia->total_is_exact)
1203 gtk_adjustment_set_value (adj, old_value);
1204 if (ia->total_is_exact)
1205 gtk_adjustment_set_upper (adj, ia->total_lines);
1207 gtk_adjustment_set_upper (adj, DBL_MAX);
1209 gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
1214 GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
1217 gtk_grid_attach (GTK_GRID (table), hbox_percent,
1221 gtk_widget_show_all (table);
1223 on_intro_amount_changed (ia);
1226 /* Initializes IA's intro substructure. */
1228 intro_page_create (PsppireImportAssistant *ia)
1230 GtkBuilder *builder = ia->builder;
1232 GtkWidget *w = get_widget_assert (builder, "Intro");
1234 ia->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
1237 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Select the Lines to Import"));
1239 ia->all_cases_button = get_widget_assert (builder, "import-all-cases");
1241 ia->n_cases_button = get_widget_assert (builder, "import-n-cases");
1243 ia->percent_button = get_widget_assert (builder, "import-percent");
1245 g_signal_connect_swapped (ia->all_cases_button, "toggled",
1246 G_CALLBACK (on_intro_amount_changed), ia);
1247 g_signal_connect_swapped (ia->n_cases_button, "toggled",
1248 G_CALLBACK (on_intro_amount_changed), ia);
1249 g_signal_connect_swapped (ia->percent_button, "toggled",
1250 G_CALLBACK (on_intro_amount_changed), ia);
1253 g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter);
1254 g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page);
1259 psppire_import_assistant_new (GtkWindow *toplevel)
1261 return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_IMPORT_ASSISTANT,
1262 "transient-for", toplevel,
1270 /* Variable name for this column. This is the variable name
1271 used on the separators page; it can be overridden by the
1272 user on the formats page. */
1275 /* Maximum length of any row in this column. */
1278 /* Contents of this column: contents[row] is the contents for
1281 A null substring indicates a missing column for that row
1282 (because the line contains an insufficient number of
1285 contents[] elements may be substrings of the lines[]
1286 strings that represent the whole lines of the file, to
1287 save memory. Other elements are dynamically allocated
1288 with ss_alloc_substring. */
1289 struct substring *contents;
1294 destroy_columns (PsppireImportAssistant *ia)
1297 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1300 free (col->contents);
1306 /* Called to render one of the cells in the fields preview tree
1309 render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
1310 GtkTreeModel *model, GtkTreeIter *iter,
1313 PsppireImportAssistant *ia = ia_;
1314 struct substring field;
1318 column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
1320 row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
1321 field = ia->columns[column].contents[row];
1322 if (field.string != NULL)
1324 GValue text = {0, };
1325 g_value_init (&text, G_TYPE_STRING);
1326 g_value_take_string (&text, ss_xstrdup (field));
1327 g_object_set_property (G_OBJECT (cell), "text", &text);
1328 g_value_unset (&text);
1329 g_object_set (cell, "background-set", FALSE, (void *) NULL);
1334 "background", "red",
1335 "background-set", TRUE,
1340 /* Parses the contents of the field at (ROW,COLUMN) according to
1341 its variable format. If OUTPUTP is non-null, then *OUTPUTP
1342 receives the formatted output for that field (which must be
1343 freed with free). If TOOLTIPP is non-null, then *TOOLTIPP
1344 receives a message suitable for use in a tooltip, if one is
1345 needed, or a null pointer otherwise. Returns TRUE if a
1346 tooltip message is needed, otherwise FALSE. */
1348 parse_field (PsppireImportAssistant *ia,
1349 size_t row, size_t column,
1350 char **outputp, char **tooltipp)
1352 const struct fmt_spec *in;
1353 struct fmt_spec out;
1357 struct substring field = ia->columns[column].contents[row];
1358 struct variable *var = dict_get_var (ia->dict, column);
1361 value_init (&val, var_get_width (var));
1362 in = var_get_print_format (var);
1363 out = fmt_for_output_from_input (in);
1365 if (field.string != NULL)
1367 char *error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
1368 dict_get_encoding (ia->dict));
1371 tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
1373 (int) field.length, field.string,
1374 fmt_name (in->type), error);
1380 tooltip = xstrdup (_("This input line has too few separators "
1381 "to fill in this field."));
1382 value_set_missing (&val, var_get_width (var));
1384 if (outputp != NULL)
1386 *outputp = data_out (&val, dict_get_encoding (ia->dict), &out);
1388 value_destroy (&val, var_get_width (var));
1390 ok = tooltip == NULL;
1391 if (tooltipp != NULL)
1392 *tooltipp = tooltip;
1399 /* Called to render one of the cells in the data preview tree
1402 render_output_cell (PsppSheetViewColumn *tree_column,
1403 GtkCellRenderer *cell,
1404 GtkTreeModel *model,
1408 PsppireImportAssistant *ia = ia_;
1410 GValue gvalue = { 0, };
1411 bool ok = parse_field (ia,
1412 (empty_list_store_iter_to_row (iter)
1414 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
1418 g_value_init (&gvalue, G_TYPE_STRING);
1419 g_value_take_string (&gvalue, output);
1420 g_object_set_property (G_OBJECT (cell), "text", &gvalue);
1421 g_value_unset (&gvalue);
1424 g_object_set (cell, "background-set", FALSE, (void *) NULL);
1427 "background", "red",
1428 "background-set", TRUE,
1433 /* Utility functions used by multiple pages of the assistant. */
1436 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
1437 const PsppireImportAssistant *ia,
1438 size_t *row, size_t *column)
1440 PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1444 PsppSheetViewColumn *tree_column;
1445 GtkTreeModel *tree_model;
1448 pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
1450 if (!pspp_sheet_view_get_path_at_pos (tree_view, bx, by,
1451 &path, &tree_column, NULL, NULL))
1454 *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
1457 tree_model = pspp_sheet_view_get_model (tree_view);
1458 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
1459 gtk_tree_path_free (path);
1463 *row = empty_list_store_iter_to_row (&iter) + ia->skip_lines;
1471 /* Called to render a tooltip on one of the cells in the fields
1472 preview tree view. */
1474 on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
1475 gboolean keyboard_mode UNUSED,
1476 GtkTooltip *tooltip, PsppireImportAssistant *ia)
1480 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
1483 if (ia->columns[column].contents[row].string != NULL)
1486 gtk_tooltip_set_text (tooltip,
1487 _("This input line has too few separators "
1488 "to fill in this field."));
1493 /* Called to render a tooltip for one of the cells in the data
1494 preview tree view. */
1496 on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
1497 gboolean keyboard_mode UNUSED,
1498 GtkTooltip *tooltip, PsppireImportAssistant *ia)
1503 if (!gtk_widget_get_mapped (widget))
1506 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
1509 if (parse_field (ia, row, column, NULL, &text))
1512 gtk_tooltip_set_text (tooltip, text);
1521 set_quote_list (GtkComboBox *cb)
1523 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
1526 const gchar *seperator[3] = {"'\"", "\'", "\""};
1528 for (i = 0; i < 3; i++)
1530 const gchar *s = seperator[i];
1532 /* Add a new row to the model */
1533 gtk_list_store_append (list, &iter);
1534 gtk_list_store_set (list, &iter,
1540 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
1541 g_object_unref (list);
1543 gtk_combo_box_set_entry_text_column (cb, 0);
1549 /* Sets IA's separators substructure to match the widgets. */
1551 get_separators (PsppireImportAssistant *ia)
1555 ds_clear (&ia->separators);
1556 for (i = 0; i < SEPARATOR_CNT; i++)
1558 const struct separator *sep = &separators[i];
1559 GtkWidget *button = get_widget_assert (ia->builder, sep->name);
1560 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1561 ds_put_byte (&ia->separators, sep->c);
1564 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->custom_cb)))
1565 ds_put_cstr (&ia->separators,
1566 gtk_entry_get_text (GTK_ENTRY (ia->custom_entry)));
1568 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->quote_cb)))
1570 const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo))));
1571 ds_assign_cstr (&ia->quotes, text);
1574 ds_clear (&ia->quotes);
1580 /* Frees and clears the column data in IA's separators
1583 clear_fields (PsppireImportAssistant *ia)
1585 if (ia->column_cnt > 0)
1590 for (row = 0; row < ia->line_cnt; row++)
1592 const struct string *line = &ia->lines[row];
1593 const char *line_start = ds_data (line);
1594 const char *line_end = ds_end (line);
1596 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1598 char *s = ss_data (col->contents[row]);
1599 if (!(s >= line_start && s <= line_end))
1600 ss_dealloc (&col->contents[row]);
1604 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1607 free (col->contents);
1617 /* Breaks the file data in IA into columns based on the
1618 separators set in IA's separators substructure. */
1620 split_fields (PsppireImportAssistant *ia)
1622 size_t columns_allocated;
1628 /* Is space in the set of separators? */
1629 space_sep = ss_find_byte (ds_ss (&ia->separators), ' ') != SIZE_MAX;
1631 /* Split all the lines, not just those from
1632 ia->first_line.skip_lines on, so that we split the line that
1633 contains variables names if ia->first_line.variable_names is
1635 columns_allocated = 0;
1636 for (row = 0; row < ia->line_cnt; row++)
1638 struct string *line = &ia->lines[row];
1639 struct substring text = ds_ss (line);
1642 for (column_idx = 0; ; column_idx++)
1644 struct substring field = SS_EMPTY_INITIALIZER;
1645 struct column *column;
1649 ss_ltrim (&text, ss_cstr (" "));
1651 if (ss_is_empty (text))
1653 if (column_idx != 0)
1657 else if (!ds_is_empty (&ia->quotes)
1658 && ds_find_byte (&ia->quotes, text.string[0]) != SIZE_MAX)
1660 int quote = ss_get_byte (&text);
1665 while ((c = ss_get_byte (&text)) != EOF)
1667 ds_put_byte (&s, c);
1668 else if (ss_match_byte (&text, quote))
1669 ds_put_byte (&s, quote);
1676 ss_get_bytes (&text, ss_cspan (text, ds_ss (&ia->separators)),
1680 if (column_idx >= ia->column_cnt)
1682 struct column *column;
1684 if (ia->column_cnt >= columns_allocated)
1686 ia->columns = x2nrealloc (ia->columns, &columns_allocated,
1687 sizeof *ia->columns);
1689 column = &ia->columns[ia->column_cnt++];
1690 column->name = NULL;
1692 column->contents = xcalloc (ia->line_cnt,
1693 sizeof *column->contents);
1695 column = &ia->columns[column_idx];
1696 column->contents[row] = field;
1697 if (ss_length (field) > column->width)
1698 column->width = ss_length (field);
1701 ss_ltrim (&text, ss_cstr (" "));
1702 if (ss_is_empty (text))
1704 if (ss_find_byte (ds_ss (&ia->separators), ss_first (text))
1706 ss_advance (&text, 1);
1711 static PsppSheetViewColumn *
1712 make_data_column (PsppireImportAssistant *ia, GtkWidget *tree_view,
1713 bool input, gint dict_idx)
1715 struct variable *var = NULL;
1716 struct column *column = NULL;
1717 size_t char_cnt = 0;
1718 gint content_width, header_width;
1719 PsppSheetViewColumn *tree_column;
1724 column = &ia->columns[dict_idx];
1725 name = escape_underscores (column->name);
1726 char_cnt = column->width;
1730 var = dict_get_var (ia->dict, dict_idx);
1731 name = escape_underscores (var_get_name (var));
1732 char_cnt = var_get_print_format (var)->w;
1735 content_width = get_monospace_width (tree_view, ia->fixed_renderer,
1737 header_width = get_string_width (tree_view, ia->prop_renderer,
1740 tree_column = pspp_sheet_view_column_new ();
1741 g_object_set_data (G_OBJECT (tree_column), "column-number",
1742 GINT_TO_POINTER (dict_idx));
1743 pspp_sheet_view_column_set_title (tree_column, name);
1744 pspp_sheet_view_column_pack_start (tree_column, ia->fixed_renderer,
1746 pspp_sheet_view_column_set_cell_data_func (
1747 tree_column, ia->fixed_renderer,
1748 input ? render_input_cell : render_output_cell, ia, NULL);
1749 pspp_sheet_view_column_set_fixed_width (tree_column, MAX (content_width,
1759 create_data_tree_view (gboolean input, GtkContainer *parent,
1760 PsppireImportAssistant *ia)
1763 GtkWidget *tree_view = make_tree_view (ia);
1765 set_model_on_treeview (ia, tree_view, ia->skip_lines);
1767 pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (tree_view)),
1768 PSPP_SHEET_SELECTION_NONE);
1770 for (i = 0; i < ia->column_cnt; i++)
1772 PsppSheetViewColumn *w = make_data_column (ia, tree_view, input, i);
1774 pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), w);
1777 g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
1778 g_signal_connect (tree_view, "query-tooltip",
1779 G_CALLBACK (input ? on_query_input_tooltip
1780 : on_query_output_tooltip), ia);
1782 GtkWidget *child = gtk_bin_get_child (GTK_BIN (parent));
1785 g_object_ref (child);
1786 gtk_container_remove (parent, child);
1788 gtk_container_add (parent, tree_view);
1790 g_object_unref (child);
1792 gtk_widget_show (tree_view);
1798 /* Chooses a name for each column on the separators page */
1800 choose_column_names (PsppireImportAssistant *ia)
1802 struct dictionary *dict;
1803 unsigned long int generated_name_count = 0;
1807 dict = dict_create (get_default_encoding ());
1808 name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
1809 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1813 hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
1814 name = dict_make_unique_var_name (dict, hint, &generated_name_count);
1818 dict_create_var_assert (dict, name, 0);
1820 dict_destroy (dict);
1825 /* Called when the user toggles one of the separators
1828 on_separator_toggle (GtkToggleButton *toggle UNUSED,
1829 PsppireImportAssistant *ia)
1831 revise_fields_preview (ia);
1834 /* Called when the user changes the entry field for custom
1837 on_separators_custom_entry_notify (GObject *gobject UNUSED,
1838 GParamSpec *arg1 UNUSED,
1839 PsppireImportAssistant *ia)
1841 revise_fields_preview (ia);
1844 /* Called when the user toggles the checkbox that enables custom
1847 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
1848 PsppireImportAssistant *ia)
1850 bool is_active = gtk_toggle_button_get_active (custom_cb);
1851 gtk_widget_set_sensitive (ia->custom_entry, is_active);
1852 revise_fields_preview (ia);
1855 /* Called when the user changes the selection in the combo box
1856 that selects a quote character. */
1858 on_quote_combo_change (GtkComboBox *combo, PsppireImportAssistant *ia)
1860 revise_fields_preview (ia);
1863 /* Called when the user toggles the checkbox that enables
1866 on_quote_cb_toggle (GtkToggleButton *quote_cb, PsppireImportAssistant *ia)
1868 bool is_active = gtk_toggle_button_get_active (quote_cb);
1869 gtk_widget_set_sensitive (ia->quote_combo, is_active);
1870 revise_fields_preview (ia);
1873 /* Initializes IA's separators substructure. */
1875 separators_page_create (PsppireImportAssistant *ia)
1877 GtkBuilder *builder = ia->builder;
1881 GtkWidget *w = get_widget_assert (builder, "Separators");
1883 g_object_set_data (G_OBJECT (w), "on-entering", prepare_separators_page);
1884 g_object_set_data (G_OBJECT (w), "on-reset", prepare_separators_page);
1887 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Choose Separators"));
1889 ia->custom_cb = get_widget_assert (builder, "custom-cb");
1890 ia->custom_entry = get_widget_assert (builder, "custom-entry");
1891 ia->quote_combo = get_widget_assert (builder, "quote-combo");
1892 ia->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo)));
1893 ia->quote_cb = get_widget_assert (builder, "quote-cb");
1895 set_quote_list (GTK_COMBO_BOX (ia->quote_combo));
1896 ia->fields_tree_view = NULL;
1898 g_signal_connect (ia->quote_combo, "changed",
1899 G_CALLBACK (on_quote_combo_change), ia);
1900 g_signal_connect (ia->quote_cb, "toggled",
1901 G_CALLBACK (on_quote_cb_toggle), ia);
1902 g_signal_connect (ia->custom_entry, "notify::text",
1903 G_CALLBACK (on_separators_custom_entry_notify), ia);
1904 g_signal_connect (ia->custom_cb, "toggled",
1905 G_CALLBACK (on_separators_custom_cb_toggle), ia);
1906 for (i = 0; i < SEPARATOR_CNT; i++)
1907 g_signal_connect (get_widget_assert (builder, separators[i].name),
1908 "toggled", G_CALLBACK (on_separator_toggle), ia);
1914 /* Called when the user changes one of the variables in the
1917 on_variable_change (PsppireDict *dict, int dict_idx,
1918 unsigned int what, const struct variable *oldvar,
1919 PsppireImportAssistant *ia)
1921 PsppSheetView *tv = PSPP_SHEET_VIEW (ia->data_tree_view);
1922 gint column_idx = dict_idx + 1;
1924 push_watch_cursor (ia);
1926 /* Remove previous column and replace with new column. */
1927 pspp_sheet_view_remove_column (tv, pspp_sheet_view_get_column (PSPP_SHEET_VIEW (ia->data_tree_view), column_idx));
1928 pspp_sheet_view_insert_column (tv, PSPP_SHEET_VIEW_COLUMN (make_data_column (ia, ia->data_tree_view, FALSE, dict_idx)),
1931 /* Save a copy of the modified variable in modified_vars, so
1932 that its attributes will be preserved if we back up to the
1933 previous page with the Prev button and then come back
1935 if (dict_idx >= ia->modified_var_cnt)
1938 ia->modified_vars = xnrealloc (ia->modified_vars, dict_idx + 1,
1939 sizeof *ia->modified_vars);
1940 for (i = 0; i <= dict_idx; i++)
1941 ia->modified_vars[i] = NULL;
1942 ia->modified_var_cnt = dict_idx + 1;
1944 if (ia->modified_vars[dict_idx])
1945 var_destroy (ia->modified_vars[dict_idx]);
1946 ia->modified_vars[dict_idx]
1947 = var_clone (psppire_dict_get_variable (dict, dict_idx));
1949 pop_watch_cursor (ia);
1955 /* Called just before the formats page of the assistant is
1958 prepare_formats_page (PsppireImportAssistant *ia)
1960 PsppireDict *psppire_dict = NULL;
1961 PsppireVarSheet *var_sheet;
1962 GtkBin *vars_scroller;
1963 GtkWidget *old_var_sheet;
1966 push_watch_cursor (ia);
1968 if (ia->spreadsheet == NULL)
1970 struct fmt_guesser *fg;
1971 unsigned long int number = 0;
1975 ia->dict = dict_create (get_default_encoding ());
1976 fg = fmt_guesser_create ();
1977 for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
1979 struct variable *modified_var =
1980 (column_idx < ia->modified_var_cnt ? ia->modified_vars[column_idx] : NULL);
1982 if (modified_var == NULL)
1984 struct column *column = &ia->columns[column_idx];
1985 struct variable *var;
1986 struct fmt_spec format;
1990 /* Choose variable name. */
1991 name = dict_make_unique_var_name (ia->dict, column->name, &number);
1993 /* Choose variable format. */
1994 fmt_guesser_clear (fg);
1995 for (row = ia->skip_lines; row < ia->line_cnt; row++)
1996 fmt_guesser_add (fg, column->contents[row]);
1997 fmt_guesser_guess (fg, &format);
1998 fmt_fix_input (&format);
2000 /* Create variable. */
2001 var = dict_create_var_assert (ia->dict, name, fmt_var_width (&format));
2002 var_set_both_formats (var, &format);
2010 name = dict_make_unique_var_name (ia->dict, var_get_name (modified_var),
2012 dict_clone_var_as_assert (ia->dict, modified_var, name);
2016 fmt_guesser_destroy (fg);
2025 GtkBuilder *builder = ia->builder;
2027 struct casereader *reader = NULL;
2029 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
2030 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
2031 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
2032 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
2034 gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box));
2036 struct spreadsheet_read_options sro;
2038 sro.sheet_name = NULL;
2039 sro.cell_range = NULL;
2040 sro.sheet_index = num + 1;
2042 if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
2044 sro.cell_range = g_strdup (range);
2047 sro.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
2050 switch (ia->spreadsheet->type)
2052 case SPREADSHEET_ODS:
2053 case SPREADSHEET_GNUMERIC:
2055 reader = spreadsheet_make_reader (ia->spreadsheet, &sro);
2056 ia->dict = dict_clone (ia->spreadsheet->dict);
2060 g_assert_not_reached ();
2063 g_free (sro.cell_range);
2065 if (reader && ia->dict)
2070 ia->column_cnt = dict_get_var_cnt (ia->dict);
2071 ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
2072 for (col = 0; col < ia->column_cnt ; ++col)
2074 const struct variable *var = dict_get_var (ia->dict, col);
2075 ia->columns[col].name = xstrdup (var_get_name (var));
2076 ia->columns[col].contents = NULL;
2079 casenumber rows = 0;
2080 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
2083 for (col = 0; col < ia->column_cnt ; ++col)
2086 const struct variable *var = dict_get_var (ia->dict, col);
2088 ia->columns[col].contents = xrealloc (ia->columns[col].contents,
2089 sizeof (struct substring) * rows);
2091 ss = data_out (case_data (c, var), dict_get_encoding (ia->dict),
2092 var_get_print_format (var));
2094 ia->columns[col].contents[rows - 1] = ss_cstr (ss);
2097 if (rows > MAX_PREVIEW_LINES)
2103 casereader_destroy (reader);
2104 ia->line_cnt = rows;
2108 GtkWidget * dialog = gtk_message_dialog_new (NULL,
2112 _("An error occurred reading the spreadsheet file."));
2114 gtk_dialog_run (GTK_DIALOG (dialog));
2115 gtk_widget_destroy (dialog);
2119 psppire_dict = psppire_dict_new_from_dict (ia->dict);
2120 g_signal_connect (psppire_dict, "variable-changed",
2121 G_CALLBACK (on_variable_change), ia);
2122 ia->psppire_dict = psppire_dict;
2125 /* XXX: PsppireVarStore doesn't hold a reference to
2126 psppire_dict for now, but it should. After it does, we
2127 should g_object_ref the psppire_dict here, since we also
2128 hold a reference via ia->formats->dict. */
2129 var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
2130 g_object_set (var_sheet,
2131 "dictionary", psppire_dict,
2132 "may-create-vars", FALSE,
2133 "may-delete-vars", FALSE,
2134 "format-use", FMT_FOR_INPUT,
2135 "enable-grid-lines", PSPP_SHEET_VIEW_GRID_LINES_BOTH,
2138 vars_scroller = GTK_BIN (get_widget_assert (ia->builder, "vars-scroller"));
2139 old_var_sheet = gtk_bin_get_child (GTK_BIN (vars_scroller));
2140 if (old_var_sheet != NULL)
2141 gtk_container_remove (GTK_CONTAINER (vars_scroller), old_var_sheet);
2142 gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
2143 gtk_widget_show (GTK_WIDGET (var_sheet));
2145 ia->data_tree_view =
2146 GTK_WIDGET (create_data_tree_view (
2148 GTK_CONTAINER (get_widget_assert (ia->builder, "data-scroller")),
2151 gtk_widget_show (ia->paste_button);
2153 pop_watch_cursor (ia);
2157 formats_page_create (PsppireImportAssistant *ia)
2159 GtkBuilder *builder = ia->builder;
2162 GtkWidget *w = get_widget_assert (builder, "Formats");
2163 g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
2164 g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
2166 add_page_to_assistant (ia, w,
2167 GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
2169 ia->data_tree_view = NULL;
2170 ia->modified_vars = NULL;
2171 ia->modified_var_cnt = 0;
2178 separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2181 ds_put_cstr (s, " /DELIMITERS=\"");
2182 if (ds_find_byte (&ia->separators, '\t') != SIZE_MAX)
2183 ds_put_cstr (s, "\\t");
2184 if (ds_find_byte (&ia->separators, '\\') != SIZE_MAX)
2185 ds_put_cstr (s, "\\\\");
2186 for (i = 0; i < ds_length (&ia->separators); i++)
2188 char c = ds_at (&ia->separators, i);
2190 ds_put_cstr (s, "\"\"");
2191 else if (c != '\t' && c != '\\')
2194 ds_put_cstr (s, "\"\n");
2195 if (!ds_is_empty (&ia->quotes))
2196 syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
2201 formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2206 g_return_if_fail (ia->dict);
2208 ds_put_cstr (s, " /VARIABLES=\n");
2210 var_cnt = dict_get_var_cnt (ia->dict);
2211 for (i = 0; i < var_cnt; i++)
2213 struct variable *var = dict_get_var (ia->dict, i);
2214 char format_string[FMT_STRING_LEN_MAX + 1];
2215 fmt_to_string (var_get_print_format (var), format_string);
2216 ds_put_format (s, " %s %s%s\n",
2217 var_get_name (var), format_string,
2218 i == var_cnt - 1 ? "." : "");
2224 first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2226 if (ia->skip_lines > 0)
2227 ds_put_format (s, " /FIRSTCASE=%d\n", ia->skip_lines + 1);
2232 intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2234 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
2235 ds_put_format (s, "N OF CASES %d.\n",
2236 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)));
2237 else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
2238 ds_put_format (s, "SAMPLE %.4g.\n",
2239 gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
2243 /* Emits PSPP syntax to S that applies the dictionary attributes
2244 (such as missing values and value labels) of the variables in
2247 apply_dict (const struct dictionary *dict, struct string *s)
2249 size_t var_cnt = dict_get_var_cnt (dict);
2252 for (i = 0; i < var_cnt; i++)
2254 struct variable *var = dict_get_var (dict, i);
2255 const char *name = var_get_name (var);
2256 enum val_type type = var_get_type (var);
2257 int width = var_get_width (var);
2258 enum measure measure = var_get_measure (var);
2259 enum var_role role = var_get_role (var);
2260 enum alignment alignment = var_get_alignment (var);
2261 const struct fmt_spec *format = var_get_print_format (var);
2263 if (var_has_missing_values (var))
2265 const struct missing_values *mv = var_get_missing_values (var);
2268 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
2269 for (j = 0; j < mv_n_values (mv); j++)
2272 ds_put_cstr (s, ", ");
2273 syntax_gen_value (s, mv_get_value (mv, j), width, format);
2276 if (mv_has_range (mv))
2279 if (mv_has_value (mv))
2280 ds_put_cstr (s, ", ");
2281 mv_get_range (mv, &low, &high);
2282 syntax_gen_num_range (s, low, high, format);
2284 ds_put_cstr (s, ").\n");
2286 if (var_has_value_labels (var))
2288 const struct val_labs *vls = var_get_value_labels (var);
2289 const struct val_lab **labels = val_labs_sorted (vls);
2290 size_t n_labels = val_labs_count (vls);
2293 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
2294 for (i = 0; i < n_labels; i++)
2296 const struct val_lab *vl = labels[i];
2297 ds_put_cstr (s, "\n ");
2298 syntax_gen_value (s, &vl->value, width, format);
2299 ds_put_byte (s, ' ');
2300 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
2303 ds_put_cstr (s, ".\n");
2305 if (var_has_label (var))
2306 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
2307 name, var_get_label (var));
2308 if (measure != var_default_measure (type))
2309 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
2310 name, measure_to_syntax (measure));
2311 if (role != ROLE_INPUT)
2312 syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
2313 var_role_to_syntax (role), name);
2314 if (alignment != var_default_alignment (type))
2315 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
2316 name, alignment_to_syntax (alignment));
2317 if (var_get_display_width (var) != var_default_display_width (width))
2318 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
2319 name, var_get_display_width (var));
2326 sheet_spec_gen_syntax (PsppireImportAssistant *ia)
2328 GtkBuilder *builder = ia->builder;
2329 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
2330 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
2331 GtkWidget *rnc = get_widget_assert (builder, "readnames-checkbox");
2332 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
2333 int sheet_index = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (sheet_entry));
2334 gboolean read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rnc));
2336 struct string s = DS_EMPTY_INITIALIZER;
2338 syntax_gen_pspp (&s,
2342 "\n /SHEET=index %d"
2343 "\n /READNAMES=%ss",
2344 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
2347 read_names ? "ON" : "OFF");
2350 if (range && 0 != strcmp ("", range))
2352 syntax_gen_pspp (&s,
2353 "\n /CELLRANGE=RANGE %sq", range);
2357 syntax_gen_pspp (&s,
2358 "\n /CELLRANGE=FULL");
2362 syntax_gen_pspp (&s, ".");
2365 return ds_cstr (&s);
2369 psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
2371 struct string s = DS_EMPTY_INITIALIZER;
2373 if (!ia->spreadsheet)
2375 if (ia->file_name == NULL)
2378 syntax_gen_pspp (&s,
2383 if (ia->encoding && strcmp (ia->encoding, "Auto"))
2384 syntax_gen_pspp (&s, " /ENCODING=%sq\n", ia->encoding);
2387 " /ARRANGEMENT=DELIMITED\n"
2388 " /DELCASE=LINE\n");
2390 first_line_append_syntax (ia, &s);
2391 separators_append_syntax (ia, &s);
2393 formats_append_syntax (ia, &s);
2394 apply_dict (ia->dict, &s);
2395 intro_append_syntax (ia, &s);
2399 return sheet_spec_gen_syntax (ia);
2402 return ds_cstr (&s);