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 "psppire-import-assistant.h"
46 #include "psppire-scanf.h"
47 #include "psppire-dialog.h"
48 #include "psppire-empty-list-store.h"
49 #include "psppire-encoding-selector.h"
50 #include "psppire-spreadsheet-model.h"
51 #include "ui/syntax-gen.h"
54 #define _(msgid) gettext (msgid)
55 #define N_(msgid) msgid
57 GType psppire_import_assistant_get_type (void)
63 enum { MAX_LINE_LEN = 16384 }; /* Max length of an acceptable line. */
66 /* Sets IA's separators substructure to match the widgets. */
67 static void get_separators (PsppireImportAssistant *ia);
68 static void split_fields (PsppireImportAssistant *ia);
70 /* Chooses a name for each column on the separators page */
71 static void choose_column_names (PsppireImportAssistant *ia);
74 /* Frees IA's file substructure. */
75 static void destroy_file (PsppireImportAssistant *ia);
77 static void clear_fields (PsppireImportAssistant *ia);
80 static void intro_page_create (PsppireImportAssistant *ia);
81 static void first_line_page_create (PsppireImportAssistant *ia);
83 static gboolean process_file (PsppireImportAssistant *ia);
86 static GtkWidget * create_data_tree_view (gboolean input, GtkContainer *parent,
87 PsppireImportAssistant *ia);
89 static void separators_page_create (PsppireImportAssistant *ia);
90 static void formats_page_create (PsppireImportAssistant *ia);
92 static void push_watch_cursor (PsppireImportAssistant *ia);
93 static void pop_watch_cursor (PsppireImportAssistant *ia);
97 static void psppire_import_assistant_init (PsppireImportAssistant *act);
98 static void psppire_import_assistant_class_init (PsppireImportAssistantClass *class);
100 G_DEFINE_TYPE (PsppireImportAssistant, psppire_import_assistant, GTK_TYPE_ASSISTANT);
110 psppire_import_assistant_set_property (GObject *object,
115 // PsppireImportAssistant *act = PSPPIRE_IMPORT_ASSISTANT (object);
120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 psppire_import_assistant_get_property (GObject *object,
132 // PsppireImportAssistant *assistant = PSPPIRE_IMPORT_ASSISTANT (object);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 static GObjectClass * parent_class = NULL;
145 static void destroy_columns (PsppireImportAssistant *ia);
148 psppire_import_assistant_finalize (GObject *object)
150 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (object);
154 spreadsheet_unref (ia->spreadsheet);
156 // clear_fields (ia);
157 destroy_columns (ia);
159 ds_destroy (&ia->separators);
160 ds_destroy (&ia->quotes);
162 g_object_unref (ia->builder);
166 g_object_unref (ia->prop_renderer);
167 g_object_unref (ia->fixed_renderer);
169 if (G_OBJECT_CLASS (parent_class)->finalize)
170 G_OBJECT_CLASS (parent_class)->finalize (object);
175 psppire_import_assistant_class_init (PsppireImportAssistantClass *class)
177 GObjectClass *object_class = G_OBJECT_CLASS (class);
179 parent_class = g_type_class_peek_parent (class);
181 object_class->set_property = psppire_import_assistant_set_property;
182 object_class->get_property = psppire_import_assistant_get_property;
184 object_class->finalize = psppire_import_assistant_finalize;
188 /* Causes the assistant to close, returning RESPONSE for
189 interpretation by text_data_import_assistant. */
191 close_assistant (PsppireImportAssistant *ia, int response)
193 ia->response = response;
194 g_main_loop_quit (ia->main_loop);
195 gtk_widget_hide (GTK_WIDGET (ia));
199 /* Called when the Paste button on the last page of the assistant
202 on_paste (GtkButton *button, PsppireImportAssistant *ia)
204 close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
207 /* Revises the contents of the fields tree view based on the
208 currently chosen set of separators. */
210 revise_fields_preview (PsppireImportAssistant *ia)
212 push_watch_cursor (ia);
216 choose_column_names (ia);
217 ia->fields_tree_view =
218 GTK_WIDGET (create_data_tree_view (TRUE,
219 GTK_CONTAINER (get_widget_assert (ia->builder, "fields-scroller")),
222 pop_watch_cursor (ia);
225 /* Chooses the most common character among those in TARGETS,
226 based on the frequency data in HISTOGRAM, and stores it in
227 RESULT. If there is a tie for the most common character among
228 those in TARGETS, the earliest character is chosen. If none
229 of the TARGETS appear at all, then DEF is used as a
232 find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
233 const char *targets, const char *def,
234 struct string *result)
236 unsigned char max = 0;
237 unsigned long int max_count = 0;
239 for (; *targets != '\0'; targets++)
241 unsigned char c = *targets;
242 unsigned long int count = histogram[c];
243 if (count > max_count)
252 ds_put_byte (result, max);
255 ds_assign_cstr (result, def);
259 /* Picks the most likely separator and quote characters based on
262 choose_likely_separators (PsppireImportAssistant *ia)
264 unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
267 /* Construct a histogram of all the characters used in the
269 for (row = 0; row < ia->line_cnt; row++)
271 struct substring line = ds_ss (&ia->lines[row]);
272 size_t length = ss_length (line);
274 for (i = 0; i < length; i++)
275 histogram[(unsigned char) line.string[i]]++;
278 find_commonest_chars (histogram, "\"'", "", &ia->quotes);
279 find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators);
283 static void set_separators (PsppireImportAssistant *ia);
285 /* Called just before the separators page becomes visible in the
286 assistant, and when the Reset button is clicked. */
288 prepare_separators_page (PsppireImportAssistant *ia, GtkWidget *page)
290 revise_fields_preview (ia);
291 choose_likely_separators (ia);
297 const char *name; /* Name (for use with get_widget_assert). */
298 int c; /* Separator character. */
301 /* All the separators in the dialog box. */
302 static const struct separator separators[] =
314 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
318 /* Sets the widgets to match IA's separators substructure. */
320 set_separators (PsppireImportAssistant *ia)
323 struct string custom;
328 ds_init_empty (&custom);
330 for (i = 0; i < ds_length (&ia->separators); i++)
332 unsigned char c = ds_at (&ia->separators, i);
335 for (j = 0; j < SEPARATOR_CNT; j++)
337 const struct separator *s = &separators[j];
345 ds_put_byte (&custom, c);
349 for (i = 0; i < SEPARATOR_CNT; i++)
351 const struct separator *s = &separators[i];
352 GtkWidget *button = get_widget_assert (ia->builder, s->name);
353 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
354 (seps & (1u << i)) != 0);
356 any_custom = !ds_is_empty (&custom);
357 gtk_entry_set_text (GTK_ENTRY (ia->custom_entry), ds_cstr (&custom));
358 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->custom_cb),
360 gtk_widget_set_sensitive (ia->custom_entry, any_custom);
361 ds_destroy (&custom);
363 any_quotes = !ds_is_empty (&ia->quotes);
365 gtk_entry_set_text (ia->quote_entry,
366 any_quotes ? ds_cstr (&ia->quotes) : "\"");
367 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->quote_cb),
369 gtk_widget_set_sensitive (ia->quote_combo, any_quotes);
373 /* Resets IA's intro page to its initial state. */
375 reset_intro_page (PsppireImportAssistant *ia)
377 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->all_cases_button),
383 /* Clears the set of user-modified variables from IA's formats
384 substructure. This discards user modifications to variable
385 formats, thereby causing formats to revert to their
388 reset_formats_page (PsppireImportAssistant *ia, GtkWidget *page)
392 for (i = 0; i < ia->modified_var_cnt; i++)
393 var_destroy (ia->modified_vars[i]);
394 free (ia->modified_vars);
395 ia->modified_vars = NULL;
396 ia->modified_var_cnt = 0;
399 static void prepare_formats_page (PsppireImportAssistant *ia);
401 /* Called when the Reset button is clicked. */
403 on_reset (GtkButton *button, PsppireImportAssistant *ia)
405 gint pn = gtk_assistant_get_current_page (GTK_ASSISTANT (ia));
407 GtkWidget *page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
409 page_func *on_reset = g_object_get_data (G_OBJECT (page), "on-reset");
418 next_page_func (gint old_page, gpointer data)
424 /* Called just before PAGE is displayed as the current page of
425 IMPORT_ASSISTANT, this updates IA content according to the new
428 on_prepare (GtkAssistant *assistant, GtkWidget *page, PsppireImportAssistant *ia)
430 gtk_widget_show (ia->reset_button);
431 gtk_widget_hide (ia->paste_button);
433 gint pn = gtk_assistant_get_current_page (assistant);
434 gint previous_page_index = ia->current_page;
436 if (previous_page_index >= 0)
438 GtkWidget *closing_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), previous_page_index);
440 if (pn > previous_page_index)
442 page_func *on_forward = g_object_get_data (G_OBJECT (closing_page), "on-forward");
445 on_forward (ia, closing_page);
449 page_func *on_back = g_object_get_data (G_OBJECT (closing_page), "on-back");
452 on_back (ia, closing_page);
457 GtkWidget *new_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
459 page_func *on_entering = g_object_get_data (G_OBJECT (new_page), "on-entering");
462 on_entering (ia, new_page);
465 ia->current_page = pn;
468 /* Called when the Cancel button in the assistant is clicked. */
470 on_cancel (GtkAssistant *assistant, PsppireImportAssistant *ia)
472 close_assistant (ia, GTK_RESPONSE_CANCEL);
475 /* Called when the Apply button on the last page of the assistant
478 on_close (GtkAssistant *assistant, PsppireImportAssistant *ia)
480 close_assistant (ia, GTK_RESPONSE_APPLY);
484 /* Frees IA's file substructure. */
486 destroy_file (PsppireImportAssistant *ia)
490 for (i = 0; i < ia->line_cnt; i++)
491 ds_destroy (&ia->lines[i]);
493 g_free (ia->file_name);
494 ia->file_name = NULL;
496 g_free (ia->encoding);
501 /* Increments the "watch cursor" level, setting the cursor for
502 the assistant window to a watch face to indicate to the user
503 that the ongoing operation may take some time. */
505 push_watch_cursor (PsppireImportAssistant *ia)
507 if (++ia->watch_cursor == 1)
509 GtkWidget *widget = GTK_WIDGET (ia);
510 GdkDisplay *display = gtk_widget_get_display (widget);
511 GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
512 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
513 g_object_unref (cursor);
514 gdk_display_flush (display);
518 /* Decrements the "watch cursor" level. If the level reaches
519 zero, the cursor is reset to its default shape. */
521 pop_watch_cursor (PsppireImportAssistant *ia)
523 if (--ia->watch_cursor == 0)
525 GtkWidget *widget = GTK_WIDGET (ia);
526 gdk_window_set_cursor (gtk_widget_get_window (widget), NULL);
532 process_file (PsppireImportAssistant *ia)
535 struct line_reader *reader = line_reader_for_file (ia->encoding, ia->file_name, O_RDONLY);
538 msg_error (errno, _("Could not open `%s'"),
543 ds_init_empty (&input);
544 for (ia->line_cnt = 0; ia->line_cnt < MAX_PREVIEW_LINES; ia->line_cnt++)
547 if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
548 || ds_length (&input) > MAX_LINE_LEN)
550 if (line_reader_eof (reader))
552 else if (line_reader_error (reader))
553 msg (ME, _("Error reading `%s': %s"),
554 ia->file_name, strerror (line_reader_error (reader)));
556 msg (ME, _("Failed to read `%s', because it contains a line "
557 "over %d bytes long and therefore appears not to be "
559 ia->file_name, MAX_LINE_LEN);
560 line_reader_close (reader);
566 char *s = recode_string ("UTF-8", line_reader_get_encoding (reader), ds_cstr (&input), ds_length (&input));
567 ds_init_cstr (&ia->lines[ia->line_cnt], s);
571 if (ia->line_cnt == 0)
573 msg (ME, _("`%s' is empty."), ia->file_name);
574 line_reader_close (reader);
579 /* Estimate the number of lines in the file. */
580 if (ia->line_cnt < MAX_PREVIEW_LINES)
582 ia->total_lines = ia->line_cnt;
583 ia->total_is_exact = true;
588 off_t position = line_reader_tell (reader);
589 if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
591 ia->total_lines = (double) ia->line_cnt / position * s.st_size;
592 ia->total_is_exact = false;
597 ia->total_is_exact = true;
600 line_reader_close (reader);
606 render_line_number (PsppSheetViewColumn *tree_column,
607 GtkCellRenderer *cell,
608 GtkTreeModel *tree_model,
612 gint row = empty_list_store_iter_to_row (iter);
613 char s[INT_BUFSIZE_BOUND (int)];
614 int first_line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model),
616 sprintf (s, "%d", first_line + row);
617 g_object_set (cell, "text", s, NULL);
623 get_string_width (GtkWidget *treeview, GtkCellRenderer *renderer,
627 g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
628 gtk_cell_renderer_get_preferred_width (renderer, treeview,
635 get_monospace_width (GtkWidget *treeview, GtkCellRenderer *renderer,
642 ds_put_byte_multiple (&s, '0', char_cnt);
643 ds_put_byte (&s, ' ');
644 width = get_string_width (treeview, renderer, ds_cstr (&s));
651 add_line_number_column (const PsppireImportAssistant *ia,
654 PsppSheetViewColumn *column =
655 pspp_sheet_view_column_new_with_attributes (_("Line"), ia->prop_renderer, (void *) NULL);
657 pspp_sheet_view_column_set_fixed_width (column, get_monospace_width (treeview, ia->prop_renderer, 5));
659 pspp_sheet_view_column_set_resizable (column, TRUE);
661 pspp_sheet_view_column_set_cell_data_func (column, ia->prop_renderer,
662 render_line_number, NULL, NULL);
664 pspp_sheet_view_append_column (PSPP_SHEET_VIEW (treeview), column);
669 set_model_on_treeview (PsppireImportAssistant *ia, GtkWidget *tree_view, size_t first_line)
671 GtkTreeModel *model = GTK_TREE_MODEL (psppire_empty_list_store_new (ia->line_cnt - first_line));
673 g_object_set_data (G_OBJECT (model), "lines", &ia->lines + first_line);
674 g_object_set_data (G_OBJECT (model), "first-line", GINT_TO_POINTER (first_line));
676 pspp_sheet_view_set_model (PSPP_SHEET_VIEW (tree_view), model);
678 g_object_unref (model);
683 make_tree_view (const PsppireImportAssistant *ia)
685 GtkWidget *tree_view = pspp_sheet_view_new ();
686 pspp_sheet_view_set_grid_lines (PSPP_SHEET_VIEW (tree_view), PSPP_SHEET_VIEW_GRID_LINES_BOTH);
688 add_line_number_column (ia, tree_view);
694 add_page_to_assistant (PsppireImportAssistant *ia,
695 GtkWidget *page, GtkAssistantPageType type, const gchar *);
699 on_sheet_combo_changed (GtkComboBox *cb, PsppireImportAssistant *ia)
703 GtkTreeModel *model = gtk_combo_box_get_model (cb);
704 GtkBuilder *builder = ia->builder;
705 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
707 gtk_combo_box_get_active_iter (cb, &iter);
708 gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
709 gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : "");
713 /* Prepares IA's sheet_spec page. */
715 prepare_sheet_spec_page (PsppireImportAssistant *ia)
717 GtkBuilder *builder = ia->builder;
718 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
719 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
721 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
722 psppire_spreadsheet_model_new (ia->spreadsheet));
724 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
726 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (readnames_checkbox), FALSE);
731 /* Initializes IA's sheet_spec substructure. */
733 sheet_spec_page_create (PsppireImportAssistant *ia)
735 GtkBuilder *builder = ia->builder;
736 GtkWidget *page = get_widget_assert (builder, "Spreadsheet-Importer");
738 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
739 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
740 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box));
741 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
742 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
746 g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
748 add_page_to_assistant (ia, page,
749 GTK_ASSISTANT_PAGE_CONTENT, _("Importing Spreadsheet Data"));
751 g_object_set_data (G_OBJECT (page), "on-entering", prepare_sheet_spec_page);
756 on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
758 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
759 gchar *f = gtk_file_chooser_get_filename (fc);
762 for(i = gtk_assistant_get_n_pages (GTK_ASSISTANT (ia)); i > 0; --i)
763 gtk_assistant_remove_page (GTK_ASSISTANT (ia), i);
765 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), FALSE);
767 if (f && !g_file_test (f, G_FILE_TEST_IS_DIR))
769 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), TRUE);
772 spreadsheet_unref (ia->spreadsheet);
774 ia->spreadsheet = gnumeric_probe (f, FALSE);
776 if (!ia->spreadsheet)
777 ia->spreadsheet = ods_probe (f, FALSE);
779 if (!ia->spreadsheet)
781 intro_page_create (ia);
782 first_line_page_create (ia);
783 separators_page_create (ia);
787 sheet_spec_page_create (ia);
790 formats_page_create (ia);
796 /* This has to be done on a map signal callback,
797 because GtkFileChooserWidget resets everything when it is mapped. */
799 on_map (PsppireImportAssistant *ia, GtkWidget *page)
801 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
804 gtk_file_chooser_set_filename (fc, ia->file_name);
806 on_chosen (ia, page);
810 chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page)
815 chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page)
819 g_free (ia->file_name);
820 ia->file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
823 g_free (ia->encoding);
824 ia->encoding = psppire_encoding_selector_get_encoding (ia->encoding_selector);
826 if (!ia->spreadsheet)
831 chooser_page_reset (PsppireImportAssistant *ia, GtkWidget *page)
833 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
835 gtk_file_chooser_set_filter (fc, ia->default_filter);
836 gtk_file_chooser_unselect_all (fc);
838 on_chosen (ia, page);
842 chooser_page_create (PsppireImportAssistant *ia)
844 GtkFileFilter *filter = NULL;
846 GtkWidget *chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
848 g_object_set_data (G_OBJECT (chooser), "on-forward", chooser_page_leave);
849 g_object_set_data (G_OBJECT (chooser), "on-reset", chooser_page_reset);
850 g_object_set_data (G_OBJECT (chooser), "on-entering",chooser_page_enter);
852 g_object_set (chooser, "local-only", FALSE, NULL);
855 ia->default_filter = gtk_file_filter_new ();
856 gtk_file_filter_set_name (ia->default_filter, _("All Files"));
857 gtk_file_filter_add_pattern (ia->default_filter, "*");
858 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), ia->default_filter);
860 filter = gtk_file_filter_new ();
861 gtk_file_filter_set_name (filter, _("Text Files"));
862 gtk_file_filter_add_mime_type (filter, "text/*");
863 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
865 filter = gtk_file_filter_new ();
866 gtk_file_filter_set_name (filter, _("Text (*.txt) Files"));
867 gtk_file_filter_add_pattern (filter, "*.txt");
868 gtk_file_filter_add_pattern (filter, "*.TXT");
869 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
871 filter = gtk_file_filter_new ();
872 gtk_file_filter_set_name (filter, _("Plain Text (ASCII) Files"));
873 gtk_file_filter_add_mime_type (filter, "text/plain");
874 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
876 filter = gtk_file_filter_new ();
877 gtk_file_filter_set_name (filter, _("Comma Separated Value Files"));
878 gtk_file_filter_add_mime_type (filter, "text/csv");
879 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
881 /* I've never encountered one of these, but it's listed here:
882 http://www.iana.org/assignments/media-types/text/tab-separated-values */
883 filter = gtk_file_filter_new ();
884 gtk_file_filter_set_name (filter, _("Tab Separated Value Files"));
885 gtk_file_filter_add_mime_type (filter, "text/tab-separated-values");
886 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
888 filter = gtk_file_filter_new ();
889 gtk_file_filter_set_name (filter, _("Gnumeric Spreadsheet Files"));
890 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
891 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
893 filter = gtk_file_filter_new ();
894 gtk_file_filter_set_name (filter, _("OpenDocument Spreadsheet Files"));
895 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
896 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
898 filter = gtk_file_filter_new ();
899 gtk_file_filter_set_name (filter, _("All Spreadsheet Files"));
900 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
901 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
902 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
904 ia->encoding_selector = psppire_encoding_selector_new ("Auto", TRUE);
905 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (chooser), ia->encoding_selector);
907 add_page_to_assistant (ia, chooser,
908 GTK_ASSISTANT_PAGE_INTRO, _("Select File to Import"));
910 g_signal_connect_swapped (chooser, "selection-changed", G_CALLBACK (on_chosen), ia);
911 g_signal_connect_swapped (chooser, "map", G_CALLBACK (on_map), ia);
916 psppire_import_assistant_init (PsppireImportAssistant *ia)
918 ia->builder = builder_new ("text-data-import.ui");
920 ia->current_page = -1 ;
924 ia->file_name = NULL;
926 ia->spreadsheet = NULL;
927 ia->watch_cursor = 0;
929 ia->prop_renderer = gtk_cell_renderer_text_new ();
930 g_object_ref_sink (ia->prop_renderer);
931 ia->fixed_renderer = gtk_cell_renderer_text_new ();
932 g_object_ref_sink (ia->fixed_renderer);
933 g_object_set (G_OBJECT (ia->fixed_renderer),
934 "family", "Monospace",
937 g_signal_connect (ia, "prepare", G_CALLBACK (on_prepare), ia);
938 g_signal_connect (ia, "cancel", G_CALLBACK (on_cancel), ia);
939 g_signal_connect (ia, "close", G_CALLBACK (on_close), ia);
941 ia->paste_button = gtk_button_new_with_label (_("Paste"));
942 ia->reset_button = gtk_button_new_with_label (_("Reset"));
944 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->paste_button);
946 g_signal_connect (ia->paste_button, "clicked", G_CALLBACK (on_paste), ia);
947 g_signal_connect (ia->reset_button, "clicked", G_CALLBACK (on_reset), ia);
949 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->reset_button);
951 gtk_window_set_title (GTK_WINDOW (ia),
952 _("Importing Delimited Text Data"));
954 gtk_window_set_icon_name (GTK_WINDOW (ia), "pspp");
956 chooser_page_create (ia);
958 gtk_assistant_set_forward_page_func (GTK_ASSISTANT (ia), next_page_func, NULL, NULL);
960 gtk_window_fullscreen (GTK_WINDOW (ia));
964 /* Appends a page of the given TYPE, with PAGE as its content, to
965 the GtkAssistant encapsulated by IA. Returns the GtkWidget
966 that represents the page. */
968 add_page_to_assistant (PsppireImportAssistant *ia,
969 GtkWidget *page, GtkAssistantPageType type, const gchar *title)
971 GtkWidget *content = page;
973 gtk_assistant_append_page (GTK_ASSISTANT (ia), content);
974 gtk_assistant_set_page_type (GTK_ASSISTANT(ia), content, type);
975 gtk_assistant_set_page_title (GTK_ASSISTANT(ia), content, title);
976 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), content, TRUE);
982 /* Called when one of the radio buttons is clicked. */
984 on_intro_amount_changed (PsppireImportAssistant *p)
986 gtk_widget_set_sensitive (p->n_cases_spin,
987 gtk_toggle_button_get_active (
988 GTK_TOGGLE_BUTTON (p->n_cases_button)));
990 gtk_widget_set_sensitive (p->percent_spin,
991 gtk_toggle_button_get_active (
992 GTK_TOGGLE_BUTTON (p->percent_button)));
997 render_line (PsppSheetViewColumn *tree_column,
998 GtkCellRenderer *cell,
999 GtkTreeModel *tree_model,
1003 gint row = empty_list_store_iter_to_row (iter);
1004 struct string *lines;
1006 lines = g_object_get_data (G_OBJECT (tree_model), "lines");
1007 g_return_if_fail (lines != NULL);
1009 g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
1012 /* Sets the widgets to match IA's first_line substructure. */
1014 set_first_line (PsppireImportAssistant *ia)
1016 GtkTreePath *path = gtk_tree_path_new_from_indices (ia->skip_lines, -1);
1019 set_model_on_treeview (ia, ia->tree_view, 0);
1021 pspp_sheet_view_set_cursor (PSPP_SHEET_VIEW (ia->tree_view),
1023 gtk_tree_path_free (path);
1025 gtk_toggle_button_set_active (
1026 GTK_TOGGLE_BUTTON (ia->variable_names_cb),
1027 ia->variable_names);
1028 gtk_widget_set_sensitive (ia->variable_names_cb,
1029 ia->skip_lines > 0);
1033 /* Creates and returns a tree view that contains each of the
1034 lines in IA's file as a row. */
1036 create_lines_tree_view (GtkContainer *parent, PsppireImportAssistant *ia)
1038 size_t max_line_length;
1039 gint content_width, header_width;
1041 const gchar *title = _("Text");
1042 GtkWidget *tree_view = make_tree_view (ia);
1043 PsppSheetViewColumn *column =
1044 pspp_sheet_view_column_new_with_attributes (title,
1045 ia->fixed_renderer, (void *) NULL);
1047 pspp_sheet_view_column_set_cell_data_func (column, ia->fixed_renderer,
1048 render_line, NULL, NULL);
1049 pspp_sheet_view_column_set_resizable (column, TRUE);
1050 pspp_sheet_view_column_set_expand (column, TRUE);
1052 max_line_length = 0;
1053 for (i = 0; i < ia->line_cnt; i++)
1055 size_t w = ds_length (&ia->lines[i]);
1056 max_line_length = MAX (max_line_length, w);
1059 content_width = get_monospace_width (tree_view, ia->fixed_renderer,
1061 header_width = get_string_width (tree_view, ia->prop_renderer, title);
1062 pspp_sheet_view_column_set_fixed_width (column, MAX (content_width,
1064 pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), column);
1066 GtkWidget *oldtv = gtk_bin_get_child (GTK_BIN (parent));
1068 gtk_container_remove (parent, oldtv);
1070 gtk_container_add (parent, tree_view);
1071 gtk_widget_show (tree_view);
1077 /* Sets IA's first_line substructure to match the widgets. */
1079 set_first_line_options (PsppireImportAssistant *ia)
1082 GtkTreeModel *model;
1084 PsppSheetSelection *selection = pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view));
1085 if (pspp_sheet_selection_get_selected (selection, &model, &iter))
1087 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
1088 int row = gtk_tree_path_get_indices (path)[0];
1089 gtk_tree_path_free (path);
1091 ia->skip_lines = row;
1092 ia->variable_names =
1094 && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)));
1097 gtk_widget_set_sensitive (ia->variable_names_cb, ia->skip_lines > 0);
1101 reset_first_line_page (PsppireImportAssistant *ia)
1103 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb), FALSE);
1104 PsppSheetSelection *selection = pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view));
1105 pspp_sheet_selection_unselect_all (selection);
1106 gtk_widget_set_sensitive (ia->variable_names_cb, FALSE);
1110 /* Initializes IA's first_line substructure. */
1112 first_line_page_create (PsppireImportAssistant *ia)
1114 GtkWidget *w = get_widget_assert (ia->builder, "FirstLine");
1116 g_object_set_data (G_OBJECT (w), "on-entering", set_first_line);
1118 add_page_to_assistant (ia, w,
1119 GTK_ASSISTANT_PAGE_CONTENT, _("Select the First Line"));
1121 ia->tree_view = GTK_WIDGET (create_lines_tree_view (
1122 GTK_CONTAINER (get_widget_assert (ia->builder, "first-line-scroller")), ia));
1123 ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
1124 pspp_sheet_selection_set_mode (
1125 pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
1126 PSPP_SHEET_SELECTION_BROWSE);
1127 pspp_sheet_view_set_rubber_banding (PSPP_SHEET_VIEW (ia->tree_view), TRUE);
1130 g_signal_connect_swapped (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
1131 "changed", G_CALLBACK (set_first_line_options), ia);
1133 g_signal_connect_swapped (ia->variable_names_cb, "toggled",
1134 G_CALLBACK (set_first_line_options), ia);
1137 g_object_set_data (G_OBJECT (w), "on-reset", reset_first_line_page);
1142 intro_on_enter (PsppireImportAssistant *ia)
1144 GtkBuilder *builder = ia->builder;
1145 GtkWidget *table = get_widget_assert (builder, "button-table");
1149 if (ia->line_cnt > MAX_PREVIEW_LINES)
1150 ia->line_cnt = MAX_PREVIEW_LINES;
1153 ds_put_cstr (&s, _("This assistant will guide you through the process of "
1154 "importing data into PSPP from a text file with one line "
1155 "per case, in which fields are separated by tabs, "
1156 "commas, or other delimiters.\n\n"));
1158 if (ia->total_is_exact)
1161 &s, ngettext ("The selected file contains %'lu line of text. ",
1162 "The selected file contains %'lu lines of text. ",
1166 else if (ia->total_lines > 0)
1170 "The selected file contains approximately %'lu line of text. ",
1171 "The selected file contains approximately %'lu lines of text. ",
1176 "Only the first %zu line of the file will be shown for "
1177 "preview purposes in the following screens. ",
1178 "Only the first %zu lines of the file will be shown for "
1179 "preview purposes in the following screens. ",
1184 ds_put_cstr (&s, _("You may choose below how much of the file should "
1185 "actually be imported."));
1187 gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
1191 GtkWidget *w = gtk_grid_get_child_at (GTK_GRID (table), 1, 1);
1192 int old_value = w ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) : 1;
1194 gtk_container_remove (GTK_CONTAINER (table), w);
1196 w = gtk_grid_get_child_at (GTK_GRID (table), 1, 2);
1198 gtk_container_remove (GTK_CONTAINER (table), w);
1201 GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
1203 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (ia->n_cases_spin));
1204 gtk_adjustment_set_lower (adj, 1.0);
1205 if (ia->total_is_exact)
1206 gtk_adjustment_set_value (adj, old_value);
1207 if (ia->total_is_exact)
1208 gtk_adjustment_set_upper (adj, ia->total_lines);
1210 gtk_adjustment_set_upper (adj, DBL_MAX);
1212 gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
1217 GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
1220 gtk_grid_attach (GTK_GRID (table), hbox_percent,
1224 gtk_widget_show_all (table);
1226 on_intro_amount_changed (ia);
1229 /* Initializes IA's intro substructure. */
1231 intro_page_create (PsppireImportAssistant *ia)
1233 GtkBuilder *builder = ia->builder;
1235 GtkWidget *w = get_widget_assert (builder, "Intro");
1237 ia->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
1240 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Select the Lines to Import"));
1242 ia->all_cases_button = get_widget_assert (builder, "import-all-cases");
1244 ia->n_cases_button = get_widget_assert (builder, "import-n-cases");
1246 ia->percent_button = get_widget_assert (builder, "import-percent");
1248 g_signal_connect_swapped (ia->all_cases_button, "toggled",
1249 G_CALLBACK (on_intro_amount_changed), ia);
1250 g_signal_connect_swapped (ia->n_cases_button, "toggled",
1251 G_CALLBACK (on_intro_amount_changed), ia);
1252 g_signal_connect_swapped (ia->percent_button, "toggled",
1253 G_CALLBACK (on_intro_amount_changed), ia);
1256 g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter);
1257 g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page);
1263 psppire_import_assistant_new (GtkWindow *toplevel)
1265 return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_IMPORT_ASSISTANT,
1266 "transient-for", toplevel,
1276 /* Variable name for this column. This is the variable name
1277 used on the separators page; it can be overridden by the
1278 user on the formats page. */
1281 /* Maximum length of any row in this column. */
1284 /* Contents of this column: contents[row] is the contents for
1287 A null substring indicates a missing column for that row
1288 (because the line contains an insufficient number of
1291 contents[] elements may be substrings of the lines[]
1292 strings that represent the whole lines of the file, to
1293 save memory. Other elements are dynamically allocated
1294 with ss_alloc_substring. */
1295 struct substring *contents;
1300 destroy_columns (PsppireImportAssistant *ia)
1303 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1306 free (col->contents);
1312 /* Called to render one of the cells in the fields preview tree
1315 render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
1316 GtkTreeModel *model, GtkTreeIter *iter,
1319 PsppireImportAssistant *ia = ia_;
1320 struct substring field;
1324 column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
1326 row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
1327 field = ia->columns[column].contents[row];
1328 if (field.string != NULL)
1330 GValue text = {0, };
1331 g_value_init (&text, G_TYPE_STRING);
1332 g_value_take_string (&text, ss_xstrdup (field));
1333 g_object_set_property (G_OBJECT (cell), "text", &text);
1334 g_value_unset (&text);
1335 g_object_set (cell, "background-set", FALSE, (void *) NULL);
1340 "background", "red",
1341 "background-set", TRUE,
1346 /* Parses the contents of the field at (ROW,COLUMN) according to
1347 its variable format. If OUTPUTP is non-null, then *OUTPUTP
1348 receives the formatted output for that field (which must be
1349 freed with free). If TOOLTIPP is non-null, then *TOOLTIPP
1350 receives a message suitable for use in a tooltip, if one is
1351 needed, or a null pointer otherwise. Returns TRUE if a
1352 tooltip message is needed, otherwise FALSE. */
1354 parse_field (PsppireImportAssistant *ia,
1355 size_t row, size_t column,
1356 char **outputp, char **tooltipp)
1358 const struct fmt_spec *in;
1359 struct fmt_spec out;
1363 struct substring field = ia->columns[column].contents[row];
1364 struct variable *var = dict_get_var (ia->dict, column);
1367 value_init (&val, var_get_width (var));
1368 in = var_get_print_format (var);
1369 out = fmt_for_output_from_input (in);
1371 if (field.string != NULL)
1373 char *error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
1374 dict_get_encoding (ia->dict));
1377 tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
1379 (int) field.length, field.string,
1380 fmt_name (in->type), error);
1386 tooltip = xstrdup (_("This input line has too few separators "
1387 "to fill in this field."));
1388 value_set_missing (&val, var_get_width (var));
1390 if (outputp != NULL)
1392 *outputp = data_out (&val, dict_get_encoding (ia->dict), &out);
1394 value_destroy (&val, var_get_width (var));
1396 ok = tooltip == NULL;
1397 if (tooltipp != NULL)
1398 *tooltipp = tooltip;
1405 /* Called to render one of the cells in the data preview tree
1408 render_output_cell (PsppSheetViewColumn *tree_column,
1409 GtkCellRenderer *cell,
1410 GtkTreeModel *model,
1414 PsppireImportAssistant *ia = ia_;
1416 GValue gvalue = { 0, };
1417 bool ok = parse_field (ia,
1418 (empty_list_store_iter_to_row (iter)
1420 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
1424 g_value_init (&gvalue, G_TYPE_STRING);
1425 g_value_take_string (&gvalue, output);
1426 g_object_set_property (G_OBJECT (cell), "text", &gvalue);
1427 g_value_unset (&gvalue);
1430 g_object_set (cell, "background-set", FALSE, (void *) NULL);
1433 "background", "red",
1434 "background-set", TRUE,
1439 /* Utility functions used by multiple pages of the assistant. */
1442 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
1443 const PsppireImportAssistant *ia,
1444 size_t *row, size_t *column)
1446 PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1450 PsppSheetViewColumn *tree_column;
1451 GtkTreeModel *tree_model;
1454 pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
1456 if (!pspp_sheet_view_get_path_at_pos (tree_view, bx, by,
1457 &path, &tree_column, NULL, NULL))
1460 *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
1463 tree_model = pspp_sheet_view_get_model (tree_view);
1464 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
1465 gtk_tree_path_free (path);
1469 *row = empty_list_store_iter_to_row (&iter) + ia->skip_lines;
1477 /* Called to render a tooltip on one of the cells in the fields
1478 preview tree view. */
1480 on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
1481 gboolean keyboard_mode UNUSED,
1482 GtkTooltip *tooltip, PsppireImportAssistant *ia)
1486 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
1489 if (ia->columns[column].contents[row].string != NULL)
1492 gtk_tooltip_set_text (tooltip,
1493 _("This input line has too few separators "
1494 "to fill in this field."));
1499 /* Called to render a tooltip for one of the cells in the data
1500 preview tree view. */
1502 on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
1503 gboolean keyboard_mode UNUSED,
1504 GtkTooltip *tooltip, PsppireImportAssistant *ia)
1509 if (!gtk_widget_get_mapped (widget))
1512 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
1515 if (parse_field (ia, row, column, NULL, &text))
1518 gtk_tooltip_set_text (tooltip, text);
1527 set_quote_list (GtkComboBox *cb)
1529 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
1532 const gchar *seperator[3] = {"'\"", "\'", "\""};
1534 for (i = 0; i < 3; i++)
1536 const gchar *s = seperator[i];
1538 /* Add a new row to the model */
1539 gtk_list_store_append (list, &iter);
1540 gtk_list_store_set (list, &iter,
1546 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
1547 g_object_unref (list);
1549 gtk_combo_box_set_entry_text_column (cb, 0);
1555 /* Sets IA's separators substructure to match the widgets. */
1557 get_separators (PsppireImportAssistant *ia)
1561 ds_clear (&ia->separators);
1562 for (i = 0; i < SEPARATOR_CNT; i++)
1564 const struct separator *sep = &separators[i];
1565 GtkWidget *button = get_widget_assert (ia->builder, sep->name);
1566 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1567 ds_put_byte (&ia->separators, sep->c);
1570 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->custom_cb)))
1571 ds_put_cstr (&ia->separators,
1572 gtk_entry_get_text (GTK_ENTRY (ia->custom_entry)));
1574 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->quote_cb)))
1576 const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo))));
1577 ds_assign_cstr (&ia->quotes, text);
1580 ds_clear (&ia->quotes);
1586 /* Frees and clears the column data in IA's separators
1589 clear_fields (PsppireImportAssistant *ia)
1591 if (ia->column_cnt > 0)
1596 for (row = 0; row < ia->line_cnt; row++)
1598 const struct string *line = &ia->lines[row];
1599 const char *line_start = ds_data (line);
1600 const char *line_end = ds_end (line);
1602 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1604 char *s = ss_data (col->contents[row]);
1605 if (!(s >= line_start && s <= line_end))
1606 ss_dealloc (&col->contents[row]);
1610 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
1613 free (col->contents);
1623 /* Breaks the file data in IA into columns based on the
1624 separators set in IA's separators substructure. */
1626 split_fields (PsppireImportAssistant *ia)
1628 size_t columns_allocated;
1634 /* Is space in the set of separators? */
1635 space_sep = ss_find_byte (ds_ss (&ia->separators), ' ') != SIZE_MAX;
1637 /* Split all the lines, not just those from
1638 ia->first_line.skip_lines on, so that we split the line that
1639 contains variables names if ia->first_line.variable_names is
1641 columns_allocated = 0;
1642 for (row = 0; row < ia->line_cnt; row++)
1644 struct string *line = &ia->lines[row];
1645 struct substring text = ds_ss (line);
1648 for (column_idx = 0; ; column_idx++)
1650 struct substring field = SS_EMPTY_INITIALIZER;
1651 struct column *column;
1655 ss_ltrim (&text, ss_cstr (" "));
1657 if (ss_is_empty (text))
1659 if (column_idx != 0)
1663 else if (!ds_is_empty (&ia->quotes)
1664 && ds_find_byte (&ia->quotes, text.string[0]) != SIZE_MAX)
1666 int quote = ss_get_byte (&text);
1671 while ((c = ss_get_byte (&text)) != EOF)
1673 ds_put_byte (&s, c);
1674 else if (ss_match_byte (&text, quote))
1675 ds_put_byte (&s, quote);
1682 ss_get_bytes (&text, ss_cspan (text, ds_ss (&ia->separators)),
1686 if (column_idx >= ia->column_cnt)
1688 struct column *column;
1690 if (ia->column_cnt >= columns_allocated)
1692 ia->columns = x2nrealloc (ia->columns, &columns_allocated,
1693 sizeof *ia->columns);
1695 column = &ia->columns[ia->column_cnt++];
1696 column->name = NULL;
1698 column->contents = xcalloc (ia->line_cnt,
1699 sizeof *column->contents);
1701 column = &ia->columns[column_idx];
1702 column->contents[row] = field;
1703 if (ss_length (field) > column->width)
1704 column->width = ss_length (field);
1707 ss_ltrim (&text, ss_cstr (" "));
1708 if (ss_is_empty (text))
1710 if (ss_find_byte (ds_ss (&ia->separators), ss_first (text))
1712 ss_advance (&text, 1);
1717 static PsppSheetViewColumn *
1718 make_data_column (PsppireImportAssistant *ia, GtkWidget *tree_view,
1719 bool input, gint dict_idx)
1721 struct variable *var = NULL;
1722 struct column *column = NULL;
1724 gint content_width, header_width;
1725 PsppSheetViewColumn *tree_column;
1729 column = &ia->columns[dict_idx];
1731 var = dict_get_var (ia->dict, dict_idx);
1733 name = escape_underscores (input ? column->name : var_get_name (var));
1734 char_cnt = input ? column->width : 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)
1963 formats_page_create (PsppireImportAssistant *ia)
1965 GtkBuilder *builder = ia->builder;
1968 GtkWidget *w = get_widget_assert (builder, "Formats");
1969 g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
1970 g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
1972 add_page_to_assistant (ia, w,
1973 GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
1975 ia->data_tree_view = NULL;
1976 ia->modified_vars = NULL;
1977 ia->modified_var_cnt = 0;
1984 separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1987 ds_put_cstr (s, " /DELIMITERS=\"");
1988 if (ds_find_byte (&ia->separators, '\t') != SIZE_MAX)
1989 ds_put_cstr (s, "\\t");
1990 if (ds_find_byte (&ia->separators, '\\') != SIZE_MAX)
1991 ds_put_cstr (s, "\\\\");
1992 for (i = 0; i < ds_length (&ia->separators); i++)
1994 char c = ds_at (&ia->separators, i);
1996 ds_put_cstr (s, "\"\"");
1997 else if (c != '\t' && c != '\\')
2000 ds_put_cstr (s, "\"\n");
2001 if (!ds_is_empty (&ia->quotes))
2002 syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
2007 formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2012 g_return_if_fail (ia->dict);
2014 ds_put_cstr (s, " /VARIABLES=\n");
2016 var_cnt = dict_get_var_cnt (ia->dict);
2017 for (i = 0; i < var_cnt; i++)
2019 struct variable *var = dict_get_var (ia->dict, i);
2020 char format_string[FMT_STRING_LEN_MAX + 1];
2021 fmt_to_string (var_get_print_format (var), format_string);
2022 ds_put_format (s, " %s %s%s\n",
2023 var_get_name (var), format_string,
2024 i == var_cnt - 1 ? "." : "");
2030 first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2032 if (ia->skip_lines > 0)
2033 ds_put_format (s, " /FIRSTCASE=%d\n", ia->skip_lines + 1);
2038 intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
2040 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
2041 ds_put_format (s, "N OF CASES %d.\n",
2042 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)));
2043 else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
2044 ds_put_format (s, "SAMPLE %.4g.\n",
2045 gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
2049 /* Emits PSPP syntax to S that applies the dictionary attributes
2050 (such as missing values and value labels) of the variables in
2053 apply_dict (const struct dictionary *dict, struct string *s)
2055 size_t var_cnt = dict_get_var_cnt (dict);
2058 for (i = 0; i < var_cnt; i++)
2060 struct variable *var = dict_get_var (dict, i);
2061 const char *name = var_get_name (var);
2062 enum val_type type = var_get_type (var);
2063 int width = var_get_width (var);
2064 enum measure measure = var_get_measure (var);
2065 enum var_role role = var_get_role (var);
2066 enum alignment alignment = var_get_alignment (var);
2067 const struct fmt_spec *format = var_get_print_format (var);
2069 if (var_has_missing_values (var))
2071 const struct missing_values *mv = var_get_missing_values (var);
2074 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
2075 for (j = 0; j < mv_n_values (mv); j++)
2078 ds_put_cstr (s, ", ");
2079 syntax_gen_value (s, mv_get_value (mv, j), width, format);
2082 if (mv_has_range (mv))
2085 if (mv_has_value (mv))
2086 ds_put_cstr (s, ", ");
2087 mv_get_range (mv, &low, &high);
2088 syntax_gen_num_range (s, low, high, format);
2090 ds_put_cstr (s, ").\n");
2092 if (var_has_value_labels (var))
2094 const struct val_labs *vls = var_get_value_labels (var);
2095 const struct val_lab **labels = val_labs_sorted (vls);
2096 size_t n_labels = val_labs_count (vls);
2099 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
2100 for (i = 0; i < n_labels; i++)
2102 const struct val_lab *vl = labels[i];
2103 ds_put_cstr (s, "\n ");
2104 syntax_gen_value (s, &vl->value, width, format);
2105 ds_put_byte (s, ' ');
2106 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
2109 ds_put_cstr (s, ".\n");
2111 if (var_has_label (var))
2112 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
2113 name, var_get_label (var));
2114 if (measure != var_default_measure (type))
2115 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
2116 name, measure_to_syntax (measure));
2117 if (role != ROLE_INPUT)
2118 syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
2119 var_role_to_syntax (role), name);
2120 if (alignment != var_default_alignment (type))
2121 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
2122 name, alignment_to_syntax (alignment));
2123 if (var_get_display_width (var) != var_default_display_width (width))
2124 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
2125 name, var_get_display_width (var));
2132 sheet_spec_gen_syntax (PsppireImportAssistant *ia)
2134 GtkBuilder *builder = ia->builder;
2135 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
2136 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
2137 GtkWidget *rnc = get_widget_assert (builder, "readnames-checkbox");
2138 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
2139 int sheet_index = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (sheet_entry));
2140 gboolean read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rnc));
2142 struct string s = DS_EMPTY_INITIALIZER;
2144 syntax_gen_pspp (&s,
2148 "\n /SHEET=index %d"
2149 "\n /READNAMES=%ss",
2150 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
2153 read_names ? "ON" : "OFF");
2156 if (range && 0 != strcmp ("", range))
2158 syntax_gen_pspp (&s,
2159 "\n /CELLRANGE=RANGE %sq", range);
2163 syntax_gen_pspp (&s,
2164 "\n /CELLRANGE=FULL");
2168 syntax_gen_pspp (&s, ".");
2171 return ds_cstr (&s);
2177 psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
2179 struct string s = DS_EMPTY_INITIALIZER;
2182 if (!ia->spreadsheet)
2184 if (ia->file_name == NULL)
2187 syntax_gen_pspp (&s,
2192 if (ia->encoding && strcmp (ia->encoding, "Auto"))
2193 syntax_gen_pspp (&s, " /ENCODING=%sq\n", ia->encoding);
2196 " /ARRANGEMENT=DELIMITED\n"
2197 " /DELCASE=LINE\n");
2199 first_line_append_syntax (ia, &s);
2200 separators_append_syntax (ia, &s);
2202 formats_append_syntax (ia, &s);
2203 apply_dict (ia->dict, &s);
2204 intro_append_syntax (ia, &s);
2208 return sheet_spec_gen_syntax (ia);
2213 return ds_cstr (&s);