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 (const 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_fullscreen (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;
1718 gint content_width, header_width;
1719 PsppSheetViewColumn *tree_column;
1723 column = &ia->columns[dict_idx];
1725 var = dict_get_var (ia->dict, dict_idx);
1727 name = escape_underscores (input ? column->name : var_get_name (var));
1728 char_cnt = input ? column->width : var_get_print_format (var)->w;
1729 content_width = get_monospace_width (tree_view, ia->fixed_renderer,
1731 header_width = get_string_width (tree_view, ia->prop_renderer,
1734 tree_column = pspp_sheet_view_column_new ();
1735 g_object_set_data (G_OBJECT (tree_column), "column-number",
1736 GINT_TO_POINTER (dict_idx));
1737 pspp_sheet_view_column_set_title (tree_column, name);
1738 pspp_sheet_view_column_pack_start (tree_column, ia->fixed_renderer,
1740 pspp_sheet_view_column_set_cell_data_func (
1741 tree_column, ia->fixed_renderer,
1742 input ? render_input_cell : render_output_cell, ia, NULL);
1743 pspp_sheet_view_column_set_fixed_width (tree_column, MAX (content_width,
1753 create_data_tree_view (gboolean input, GtkContainer *parent,
1754 PsppireImportAssistant *ia)
1757 GtkWidget *tree_view = make_tree_view (ia);
1759 set_model_on_treeview (ia, tree_view, ia->skip_lines);
1761 pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (tree_view)),
1762 PSPP_SHEET_SELECTION_NONE);
1764 for (i = 0; i < ia->column_cnt; i++)
1766 PsppSheetViewColumn *w = make_data_column (ia, tree_view, input, i);
1768 pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), w);
1771 g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
1772 g_signal_connect (tree_view, "query-tooltip",
1773 G_CALLBACK (input ? on_query_input_tooltip
1774 : on_query_output_tooltip), ia);
1776 GtkWidget *child = gtk_bin_get_child (GTK_BIN (parent));
1779 g_object_ref (child);
1780 gtk_container_remove (parent, child);
1782 gtk_container_add (parent, tree_view);
1784 g_object_unref (child);
1786 gtk_widget_show (tree_view);
1792 /* Chooses a name for each column on the separators page */
1794 choose_column_names (PsppireImportAssistant *ia)
1796 struct dictionary *dict;
1797 unsigned long int generated_name_count = 0;
1801 dict = dict_create (get_default_encoding ());
1802 name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
1803 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1807 hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
1808 name = dict_make_unique_var_name (dict, hint, &generated_name_count);
1812 dict_create_var_assert (dict, name, 0);
1814 dict_destroy (dict);
1819 /* Called when the user toggles one of the separators
1822 on_separator_toggle (GtkToggleButton *toggle UNUSED,
1823 PsppireImportAssistant *ia)
1825 revise_fields_preview (ia);
1828 /* Called when the user changes the entry field for custom
1831 on_separators_custom_entry_notify (GObject *gobject UNUSED,
1832 GParamSpec *arg1 UNUSED,
1833 PsppireImportAssistant *ia)
1835 revise_fields_preview (ia);
1838 /* Called when the user toggles the checkbox that enables custom
1841 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
1842 PsppireImportAssistant *ia)
1844 bool is_active = gtk_toggle_button_get_active (custom_cb);
1845 gtk_widget_set_sensitive (ia->custom_entry, is_active);
1846 revise_fields_preview (ia);
1849 /* Called when the user changes the selection in the combo box
1850 that selects a quote character. */
1852 on_quote_combo_change (GtkComboBox *combo, PsppireImportAssistant *ia)
1854 revise_fields_preview (ia);
1857 /* Called when the user toggles the checkbox that enables
1860 on_quote_cb_toggle (GtkToggleButton *quote_cb, PsppireImportAssistant *ia)
1862 bool is_active = gtk_toggle_button_get_active (quote_cb);
1863 gtk_widget_set_sensitive (ia->quote_combo, is_active);
1864 revise_fields_preview (ia);
1867 /* Initializes IA's separators substructure. */
1869 separators_page_create (PsppireImportAssistant *ia)
1871 GtkBuilder *builder = ia->builder;
1875 GtkWidget *w = get_widget_assert (builder, "Separators");
1877 g_object_set_data (G_OBJECT (w), "on-entering", prepare_separators_page);
1878 g_object_set_data (G_OBJECT (w), "on-reset", prepare_separators_page);
1881 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Choose Separators"));
1883 ia->custom_cb = get_widget_assert (builder, "custom-cb");
1884 ia->custom_entry = get_widget_assert (builder, "custom-entry");
1885 ia->quote_combo = get_widget_assert (builder, "quote-combo");
1886 ia->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo)));
1887 ia->quote_cb = get_widget_assert (builder, "quote-cb");
1889 set_quote_list (GTK_COMBO_BOX (ia->quote_combo));
1890 ia->fields_tree_view = NULL;
1892 g_signal_connect (ia->quote_combo, "changed",
1893 G_CALLBACK (on_quote_combo_change), ia);
1894 g_signal_connect (ia->quote_cb, "toggled",
1895 G_CALLBACK (on_quote_cb_toggle), ia);
1896 g_signal_connect (ia->custom_entry, "notify::text",
1897 G_CALLBACK (on_separators_custom_entry_notify), ia);
1898 g_signal_connect (ia->custom_cb, "toggled",
1899 G_CALLBACK (on_separators_custom_cb_toggle), ia);
1900 for (i = 0; i < SEPARATOR_CNT; i++)
1901 g_signal_connect (get_widget_assert (builder, separators[i].name),
1902 "toggled", G_CALLBACK (on_separator_toggle), ia);
1908 /* Called when the user changes one of the variables in the
1911 on_variable_change (PsppireDict *dict, int dict_idx,
1912 unsigned int what, const struct variable *oldvar,
1913 PsppireImportAssistant *ia)
1915 PsppSheetView *tv = PSPP_SHEET_VIEW (ia->data_tree_view);
1916 gint column_idx = dict_idx + 1;
1918 push_watch_cursor (ia);
1920 /* Remove previous column and replace with new column. */
1921 pspp_sheet_view_remove_column (tv, pspp_sheet_view_get_column (PSPP_SHEET_VIEW (ia->data_tree_view), column_idx));
1922 pspp_sheet_view_insert_column (tv, PSPP_SHEET_VIEW_COLUMN (make_data_column (ia, ia->data_tree_view, FALSE, dict_idx)),
1925 /* Save a copy of the modified variable in modified_vars, so
1926 that its attributes will be preserved if we back up to the
1927 previous page with the Prev button and then come back
1929 if (dict_idx >= ia->modified_var_cnt)
1932 ia->modified_vars = xnrealloc (ia->modified_vars, dict_idx + 1,
1933 sizeof *ia->modified_vars);
1934 for (i = 0; i <= dict_idx; i++)
1935 ia->modified_vars[i] = NULL;
1936 ia->modified_var_cnt = dict_idx + 1;
1938 if (ia->modified_vars[dict_idx])
1939 var_destroy (ia->modified_vars[dict_idx]);
1940 ia->modified_vars[dict_idx]
1941 = var_clone (psppire_dict_get_variable (dict, dict_idx));
1943 pop_watch_cursor (ia);
1949 /* Called just before the formats page of the assistant is
1952 prepare_formats_page (PsppireImportAssistant *ia)
1954 PsppireDict *psppire_dict = NULL;
1955 PsppireVarSheet *var_sheet;
1956 GtkBin *vars_scroller;
1957 GtkWidget *old_var_sheet;
1960 push_watch_cursor (ia);
1962 if (ia->spreadsheet == NULL)
1964 struct fmt_guesser *fg;
1965 unsigned long int number = 0;
1969 ia->dict = dict_create (get_default_encoding ());
1970 fg = fmt_guesser_create ();
1971 for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
1973 struct variable *modified_var =
1974 (column_idx < ia->modified_var_cnt ? ia->modified_vars[column_idx] : NULL);
1976 if (modified_var == NULL)
1978 struct column *column = &ia->columns[column_idx];
1979 struct variable *var;
1980 struct fmt_spec format;
1984 /* Choose variable name. */
1985 name = dict_make_unique_var_name (ia->dict, column->name, &number);
1987 /* Choose variable format. */
1988 fmt_guesser_clear (fg);
1989 for (row = ia->skip_lines; row < ia->line_cnt; row++)
1990 fmt_guesser_add (fg, column->contents[row]);
1991 fmt_guesser_guess (fg, &format);
1992 fmt_fix_input (&format);
1994 /* Create variable. */
1995 var = dict_create_var_assert (ia->dict, name, fmt_var_width (&format));
1996 var_set_both_formats (var, &format);
2004 name = dict_make_unique_var_name (ia->dict, var_get_name (modified_var),
2006 dict_clone_var_as_assert (ia->dict, modified_var, name);
2010 fmt_guesser_destroy (fg);
2019 GtkBuilder *builder = ia->builder;
2021 struct casereader *reader = NULL;
2023 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
2024 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
2025 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
2026 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
2028 gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box));
2030 struct spreadsheet_read_options sro;
2032 sro.sheet_name = NULL;
2033 sro.cell_range = NULL;
2034 sro.sheet_index = num + 1;
2036 if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
2038 sro.cell_range = g_strdup (range);
2041 sro.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
2044 switch (ia->spreadsheet->type)
2046 case SPREADSHEET_ODS:
2047 case SPREADSHEET_GNUMERIC:
2049 reader = spreadsheet_make_reader (ia->spreadsheet, &sro);
2050 ia->dict = dict_clone (ia->spreadsheet->dict);
2054 g_assert_not_reached ();
2057 g_free (sro.cell_range);
2059 if (reader && ia->dict)
2064 ia->column_cnt = dict_get_var_cnt (ia->dict);
2065 ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
2066 for (col = 0; col < ia->column_cnt ; ++col)
2068 const struct variable *var = dict_get_var (ia->dict, col);
2069 ia->columns[col].name = xstrdup (var_get_name (var));
2070 ia->columns[col].contents = NULL;
2073 casenumber rows = 0;
2074 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
2077 for (col = 0; col < ia->column_cnt ; ++col)
2080 const struct variable *var = dict_get_var (ia->dict, col);
2082 ia->columns[col].contents = xrealloc (ia->columns[col].contents,
2083 sizeof (struct substring) * rows);
2085 ss = data_out (case_data (c, var), dict_get_encoding (ia->dict),
2086 var_get_print_format (var));
2088 ia->columns[col].contents[rows - 1] = ss_cstr (ss);
2091 if (rows > MAX_PREVIEW_LINES)
2097 casereader_destroy (reader);
2098 ia->line_cnt = rows;
2102 GtkWidget * dialog = gtk_message_dialog_new (NULL,
2106 _("An error occurred reading the spreadsheet file."));
2108 gtk_dialog_run (GTK_DIALOG (dialog));
2109 gtk_widget_destroy (dialog);
2113 psppire_dict = psppire_dict_new_from_dict (ia->dict);
2114 g_signal_connect (psppire_dict, "variable-changed",
2115 G_CALLBACK (on_variable_change), ia);
2116 ia->psppire_dict = psppire_dict;
2119 /* XXX: PsppireVarStore doesn't hold a reference to
2120 psppire_dict for now, but it should. After it does, we
2121 should g_object_ref the psppire_dict here, since we also
2122 hold a reference via ia->formats->dict. */
2123 var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
2124 g_object_set (var_sheet,
2125 "dictionary", psppire_dict,
2126 "may-create-vars", FALSE,
2127 "may-delete-vars", FALSE,
2128 "format-use", FMT_FOR_INPUT,
2129 "enable-grid-lines", PSPP_SHEET_VIEW_GRID_LINES_BOTH,
2132 vars_scroller = GTK_BIN (get_widget_assert (ia->builder, "vars-scroller"));
2133 old_var_sheet = gtk_bin_get_child (GTK_BIN (vars_scroller));
2134 if (old_var_sheet != NULL)
2135 gtk_container_remove (GTK_CONTAINER (vars_scroller), old_var_sheet);
2136 gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
2137 gtk_widget_show (GTK_WIDGET (var_sheet));
2139 ia->data_tree_view =
2140 GTK_WIDGET (create_data_tree_view (
2142 GTK_CONTAINER (get_widget_assert (ia->builder, "data-scroller")),
2145 gtk_widget_show (ia->paste_button);
2147 pop_watch_cursor (ia);
2151 formats_page_create (PsppireImportAssistant *ia)
2153 GtkBuilder *builder = ia->builder;
2156 GtkWidget *w = get_widget_assert (builder, "Formats");
2157 g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
2158 g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
2160 add_page_to_assistant (ia, w,
2161 GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
2163 ia->data_tree_view = NULL;
2164 ia->modified_vars = NULL;
2165 ia->modified_var_cnt = 0;
2172 separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2175 ds_put_cstr (s, " /DELIMITERS=\"");
2176 if (ds_find_byte (&ia->separators, '\t') != SIZE_MAX)
2177 ds_put_cstr (s, "\\t");
2178 if (ds_find_byte (&ia->separators, '\\') != SIZE_MAX)
2179 ds_put_cstr (s, "\\\\");
2180 for (i = 0; i < ds_length (&ia->separators); i++)
2182 char c = ds_at (&ia->separators, i);
2184 ds_put_cstr (s, "\"\"");
2185 else if (c != '\t' && c != '\\')
2188 ds_put_cstr (s, "\"\n");
2189 if (!ds_is_empty (&ia->quotes))
2190 syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
2195 formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2200 g_return_if_fail (ia->dict);
2202 ds_put_cstr (s, " /VARIABLES=\n");
2204 var_cnt = dict_get_var_cnt (ia->dict);
2205 for (i = 0; i < var_cnt; i++)
2207 struct variable *var = dict_get_var (ia->dict, i);
2208 char format_string[FMT_STRING_LEN_MAX + 1];
2209 fmt_to_string (var_get_print_format (var), format_string);
2210 ds_put_format (s, " %s %s%s\n",
2211 var_get_name (var), format_string,
2212 i == var_cnt - 1 ? "." : "");
2218 first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2220 if (ia->skip_lines > 0)
2221 ds_put_format (s, " /FIRSTCASE=%d\n", ia->skip_lines + 1);
2226 intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2228 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
2229 ds_put_format (s, "N OF CASES %d.\n",
2230 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)));
2231 else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
2232 ds_put_format (s, "SAMPLE %.4g.\n",
2233 gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
2237 /* Emits PSPP syntax to S that applies the dictionary attributes
2238 (such as missing values and value labels) of the variables in
2241 apply_dict (const struct dictionary *dict, struct string *s)
2243 size_t var_cnt = dict_get_var_cnt (dict);
2246 for (i = 0; i < var_cnt; i++)
2248 struct variable *var = dict_get_var (dict, i);
2249 const char *name = var_get_name (var);
2250 enum val_type type = var_get_type (var);
2251 int width = var_get_width (var);
2252 enum measure measure = var_get_measure (var);
2253 enum var_role role = var_get_role (var);
2254 enum alignment alignment = var_get_alignment (var);
2255 const struct fmt_spec *format = var_get_print_format (var);
2257 if (var_has_missing_values (var))
2259 const struct missing_values *mv = var_get_missing_values (var);
2262 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
2263 for (j = 0; j < mv_n_values (mv); j++)
2266 ds_put_cstr (s, ", ");
2267 syntax_gen_value (s, mv_get_value (mv, j), width, format);
2270 if (mv_has_range (mv))
2273 if (mv_has_value (mv))
2274 ds_put_cstr (s, ", ");
2275 mv_get_range (mv, &low, &high);
2276 syntax_gen_num_range (s, low, high, format);
2278 ds_put_cstr (s, ").\n");
2280 if (var_has_value_labels (var))
2282 const struct val_labs *vls = var_get_value_labels (var);
2283 const struct val_lab **labels = val_labs_sorted (vls);
2284 size_t n_labels = val_labs_count (vls);
2287 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
2288 for (i = 0; i < n_labels; i++)
2290 const struct val_lab *vl = labels[i];
2291 ds_put_cstr (s, "\n ");
2292 syntax_gen_value (s, &vl->value, width, format);
2293 ds_put_byte (s, ' ');
2294 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
2297 ds_put_cstr (s, ".\n");
2299 if (var_has_label (var))
2300 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
2301 name, var_get_label (var));
2302 if (measure != var_default_measure (type))
2303 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
2304 name, measure_to_syntax (measure));
2305 if (role != ROLE_INPUT)
2306 syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
2307 var_role_to_syntax (role), name);
2308 if (alignment != var_default_alignment (type))
2309 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
2310 name, alignment_to_syntax (alignment));
2311 if (var_get_display_width (var) != var_default_display_width (width))
2312 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
2313 name, var_get_display_width (var));
2320 sheet_spec_gen_syntax (PsppireImportAssistant *ia)
2322 GtkBuilder *builder = ia->builder;
2323 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
2324 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
2325 GtkWidget *rnc = get_widget_assert (builder, "readnames-checkbox");
2326 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
2327 int sheet_index = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (sheet_entry));
2328 gboolean read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rnc));
2330 struct string s = DS_EMPTY_INITIALIZER;
2332 syntax_gen_pspp (&s,
2336 "\n /SHEET=index %d"
2337 "\n /READNAMES=%ss",
2338 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
2341 read_names ? "ON" : "OFF");
2344 if (range && 0 != strcmp ("", range))
2346 syntax_gen_pspp (&s,
2347 "\n /CELLRANGE=RANGE %sq", range);
2351 syntax_gen_pspp (&s,
2352 "\n /CELLRANGE=FULL");
2356 syntax_gen_pspp (&s, ".");
2359 return ds_cstr (&s);
2363 psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
2365 struct string s = DS_EMPTY_INITIALIZER;
2367 if (!ia->spreadsheet)
2369 if (ia->file_name == NULL)
2372 syntax_gen_pspp (&s,
2377 if (ia->encoding && strcmp (ia->encoding, "Auto"))
2378 syntax_gen_pspp (&s, " /ENCODING=%sq\n", ia->encoding);
2381 " /ARRANGEMENT=DELIMITED\n"
2382 " /DELCASE=LINE\n");
2384 first_line_append_syntax (ia, &s);
2385 separators_append_syntax (ia, &s);
2387 formats_append_syntax (ia, &s);
2388 apply_dict (ia->dict, &s);
2389 intro_append_syntax (ia, &s);
2393 return sheet_spec_gen_syntax (ia);
2396 return ds_cstr (&s);