1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2015, 2016, 2017 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/>. */
21 #include "data/casereader.h"
22 #include "data/data-in.h"
23 #include "data/data-out.h"
24 #include "data/dictionary.h"
25 #include "data/format-guesser.h"
26 #include "data/format.h"
27 #include "data/gnumeric-reader.h"
28 #include "data/ods-reader.h"
29 #include "data/spreadsheet-reader.h"
30 #include "data/value-labels.h"
31 #include "data/casereader-provider.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/line-reader.h"
35 #include "libpspp/message.h"
36 #include "libpspp/hmap.h"
37 #include "libpspp/hash-functions.h"
38 #include "libpspp/str.h"
40 #include "builder-wrapper.h"
42 #include "psppire-data-sheet.h"
43 #include "psppire-data-store.h"
44 #include "psppire-dialog.h"
45 #include "psppire-delimited-text.h"
46 #include "psppire-dict.h"
47 #include "psppire-encoding-selector.h"
48 #include "psppire-import-assistant.h"
49 #include "psppire-scanf.h"
50 #include "psppire-spreadsheet-model.h"
51 #include "psppire-text-file.h"
52 #include "psppire-variable-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 /* Chooses a name for each column on the separators page */
64 static void choose_column_names (PsppireImportAssistant *ia);
66 static void intro_page_create (PsppireImportAssistant *ia);
67 static void first_line_page_create (PsppireImportAssistant *ia);
69 static void separators_page_create (PsppireImportAssistant *ia);
70 static void formats_page_create (PsppireImportAssistant *ia);
72 static void psppire_import_assistant_init (PsppireImportAssistant *act);
73 static void psppire_import_assistant_class_init (PsppireImportAssistantClass *class);
75 G_DEFINE_TYPE (PsppireImportAssistant, psppire_import_assistant, GTK_TYPE_ASSISTANT);
85 psppire_import_assistant_set_property (GObject *object,
90 // PsppireImportAssistant *act = PSPPIRE_IMPORT_ASSISTANT (object);
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 psppire_import_assistant_get_property (GObject *object,
107 // PsppireImportAssistant *assistant = PSPPIRE_IMPORT_ASSISTANT (object);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 static GObjectClass * parent_class = NULL;
121 psppire_import_assistant_finalize (GObject *object)
123 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (object);
126 spreadsheet_unref (ia->spreadsheet);
128 ds_destroy (&ia->quotes);
130 g_object_unref (ia->builder);
133 g_main_loop_unref (ia->main_loop);
135 if (G_OBJECT_CLASS (parent_class)->finalize)
136 G_OBJECT_CLASS (parent_class)->finalize (object);
141 psppire_import_assistant_class_init (PsppireImportAssistantClass *class)
143 GObjectClass *object_class = G_OBJECT_CLASS (class);
145 parent_class = g_type_class_peek_parent (class);
147 object_class->set_property = psppire_import_assistant_set_property;
148 object_class->get_property = psppire_import_assistant_get_property;
150 object_class->finalize = psppire_import_assistant_finalize;
154 /* Causes the assistant to close, returning RESPONSE for
155 interpretation by text_data_import_assistant. */
157 close_assistant (PsppireImportAssistant *ia, int response)
159 ia->response = response;
160 g_main_loop_quit (ia->main_loop);
161 gtk_widget_hide (GTK_WIDGET (ia));
165 /* Called when the Paste button on the last page of the assistant
168 on_paste (GtkButton *button, PsppireImportAssistant *ia)
170 close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
174 /* Revises the contents of the fields tree view based on the
175 currently chosen set of separators. */
177 revise_fields_preview (PsppireImportAssistant *ia)
179 choose_column_names (ia);
185 const char *name; /* Name (for use with get_widget_assert). */
186 gunichar c; /* Separator character. */
189 /* All the separators in the dialog box. */
190 static const struct separator separators[] =
203 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
205 struct separator_count_node
207 struct hmap_node node;
208 int occurance; /* The number of times the separator occurs in a line */
209 int quantity; /* The number of lines with this occurance */
213 /* Picks the most likely separator and quote characters based on
216 choose_likely_separators (PsppireImportAssistant *ia)
219 g_object_get (ia->delimiters_model, "first-line", &first_line, NULL);
225 struct hmap count_map[SEPARATOR_CNT];
226 for (j = 0; j < SEPARATOR_CNT; ++j)
227 hmap_init (count_map + j);
229 GtkTreePath *p = gtk_tree_path_new_from_indices (first_line, -1);
231 for (valid = gtk_tree_model_get_iter (GTK_TREE_MODEL (ia->text_file), &iter, p);
233 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->text_file), &iter))
235 gchar *line_text = NULL;
236 gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &line_text, -1);
238 gint *counts = xzalloc (sizeof *counts * SEPARATOR_CNT);
240 struct substring cs = ss_cstr (line_text);
242 UINT32_MAX != ss_first_mb (cs);
245 ucs4_t character = ss_first_mb (cs);
248 for (s = 0; s < SEPARATOR_CNT; ++s)
250 if (character == separators[s].c)
256 for (j = 0; j < SEPARATOR_CNT; ++j)
260 struct separator_count_node *cn = NULL;
261 unsigned int hash = hash_int (counts[j], 0);
262 HMAP_FOR_EACH_WITH_HASH (cn, struct separator_count_node, node, hash, &count_map[j])
264 if (cn->occurance == counts[j])
270 struct separator_count_node *new_cn = xzalloc (sizeof *new_cn);
271 new_cn->occurance = counts[j];
272 new_cn->quantity = 1;
273 hmap_insert (&count_map[j], &new_cn->node, hash);
283 gtk_tree_path_free (p);
285 int most_frequent = -1;
287 for (j = 0; j < SEPARATOR_CNT; ++j)
289 struct separator_count_node *cn;
290 HMAP_FOR_EACH (cn, struct separator_count_node, node, &count_map[j])
292 if (largest < cn->quantity)
294 largest = cn->quantity;
298 hmap_destroy (&count_map[j]);
301 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (get_widget_assert (ia->builder, separators[most_frequent].name)), TRUE);
305 repopulate_delimiter_columns (PsppireImportAssistant *ia)
307 /* Remove all the columns */
308 while (gtk_tree_view_get_n_columns (GTK_TREE_VIEW (ia->fields_tree_view)) > 0)
310 GtkTreeViewColumn *tvc = gtk_tree_view_get_column (GTK_TREE_VIEW (ia->fields_tree_view), 0);
311 gtk_tree_view_remove_column (GTK_TREE_VIEW (ia->fields_tree_view), tvc);
314 gint n_fields = gtk_tree_model_get_n_columns (ia->delimiters_model);
316 /* ... and put them back again. */
318 for (f = gtk_tree_view_get_n_columns (GTK_TREE_VIEW (ia->fields_tree_view));
321 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
323 const gchar *title = NULL;
329 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
332 psppire_delimited_text_get_header_title
333 (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), f - 1);
339 GtkTreeViewColumn *column =
340 gtk_tree_view_column_new_with_attributes (title,
344 g_object_set (column,
346 "sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE,
349 gtk_tree_view_append_column (GTK_TREE_VIEW (ia->fields_tree_view), column);
354 reset_tree_view_model (PsppireImportAssistant *ia)
356 GtkTreeModel *tm = gtk_tree_view_get_model (GTK_TREE_VIEW (ia->fields_tree_view));
358 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), NULL);
361 repopulate_delimiter_columns (ia);
363 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), tm);
364 // gtk_tree_view_columns_autosize (GTK_TREE_VIEW (ia->fields_tree_view));
369 /* Called just before the separators page becomes visible in the
370 assistant, and when the Reset button is clicked. */
372 prepare_separators_page (PsppireImportAssistant *ia, GtkWidget *page)
374 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), ia->delimiters_model);
376 g_signal_connect_swapped (ia->delimiters_model, "notify::delimiters",
377 G_CALLBACK (reset_tree_view_model), ia);
380 repopulate_delimiter_columns (ia);
382 revise_fields_preview (ia);
383 choose_likely_separators (ia);
386 /* Resets IA's intro page to its initial state. */
388 reset_intro_page (PsppireImportAssistant *ia)
390 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->all_cases_button),
396 /* Clears the set of user-modified variables from IA's formats
397 substructure. This discards user modifications to variable
398 formats, thereby causing formats to revert to their
401 reset_formats_page (PsppireImportAssistant *ia, GtkWidget *page)
405 static void prepare_formats_page (PsppireImportAssistant *ia);
407 /* Called when the Reset button is clicked. */
409 on_reset (GtkButton *button, PsppireImportAssistant *ia)
411 gint pn = gtk_assistant_get_current_page (GTK_ASSISTANT (ia));
413 GtkWidget *page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
415 page_func *on_reset = g_object_get_data (G_OBJECT (page), "on-reset");
424 next_page_func (gint old_page, gpointer data)
430 /* Called just before PAGE is displayed as the current page of
431 IMPORT_ASSISTANT, this updates IA content according to the new
434 on_prepare (GtkAssistant *assistant, GtkWidget *page, PsppireImportAssistant *ia)
436 gtk_widget_show (ia->reset_button);
437 gtk_widget_hide (ia->paste_button);
439 gint pn = gtk_assistant_get_current_page (assistant);
440 gint previous_page_index = ia->current_page;
442 if (previous_page_index >= 0)
444 GtkWidget *closing_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), previous_page_index);
446 if (pn > previous_page_index)
448 page_func *on_forward = g_object_get_data (G_OBJECT (closing_page), "on-forward");
451 on_forward (ia, closing_page);
455 page_func *on_back = g_object_get_data (G_OBJECT (closing_page), "on-back");
458 on_back (ia, closing_page);
463 GtkWidget *new_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
465 page_func *on_entering = g_object_get_data (G_OBJECT (new_page), "on-entering");
468 on_entering (ia, new_page);
471 ia->current_page = pn;
474 /* Called when the Cancel button in the assistant is clicked. */
476 on_cancel (GtkAssistant *assistant, PsppireImportAssistant *ia)
478 close_assistant (ia, GTK_RESPONSE_CANCEL);
481 /* Called when the Apply button on the last page of the assistant
484 on_close (GtkAssistant *assistant, PsppireImportAssistant *ia)
486 close_assistant (ia, GTK_RESPONSE_APPLY);
491 add_page_to_assistant (PsppireImportAssistant *ia,
492 GtkWidget *page, GtkAssistantPageType type, const gchar *);
496 on_sheet_combo_changed (GtkComboBox *cb, PsppireImportAssistant *ia)
500 GtkTreeModel *model = gtk_combo_box_get_model (cb);
501 GtkBuilder *builder = ia->builder;
502 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
504 gtk_combo_box_get_active_iter (cb, &iter);
505 gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
506 gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : "");
510 /* Prepares IA's sheet_spec page. */
512 prepare_sheet_spec_page (PsppireImportAssistant *ia)
514 GtkBuilder *builder = ia->builder;
515 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
516 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
518 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
519 psppire_spreadsheet_model_new (ia->spreadsheet));
521 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
523 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (readnames_checkbox), FALSE);
527 /* Initializes IA's sheet_spec substructure. */
529 sheet_spec_page_create (PsppireImportAssistant *ia)
531 GtkBuilder *builder = ia->builder;
532 GtkWidget *page = get_widget_assert (builder, "Spreadsheet-Importer");
534 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
535 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
536 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box));
537 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
538 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
542 g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
544 add_page_to_assistant (ia, page,
545 GTK_ASSISTANT_PAGE_CONTENT, _("Importing Spreadsheet Data"));
547 g_object_set_data (G_OBJECT (page), "on-entering", prepare_sheet_spec_page);
551 on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
553 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
554 gchar *f = gtk_file_chooser_get_filename (fc);
557 for(i = gtk_assistant_get_n_pages (GTK_ASSISTANT (ia)); i > 0; --i)
558 gtk_assistant_remove_page (GTK_ASSISTANT (ia), i);
560 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), FALSE);
562 if (f && g_file_test (f, G_FILE_TEST_IS_REGULAR))
564 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), TRUE);
567 spreadsheet_unref (ia->spreadsheet);
569 ia->spreadsheet = gnumeric_probe (f, FALSE);
571 if (!ia->spreadsheet)
572 ia->spreadsheet = ods_probe (f, FALSE);
576 sheet_spec_page_create (ia);
580 intro_page_create (ia);
581 first_line_page_create (ia);
582 separators_page_create (ia);
585 formats_page_create (ia);
591 /* This has to be done on a map signal callback,
592 because GtkFileChooserWidget resets everything when it is mapped. */
594 on_map (PsppireImportAssistant *ia, GtkWidget *page)
597 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
600 gtk_file_chooser_set_filename (fc, ia->file_name);
603 on_chosen (ia, page);
609 chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page)
614 chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page)
616 g_free (ia->file_name);
617 ia->file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
618 gchar *encoding = psppire_encoding_selector_get_encoding (ia->encoding_selector);
620 if (!ia->spreadsheet)
622 ia->text_file = psppire_text_file_new (ia->file_name, encoding);
623 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->first_line_tree_view),
624 GTK_TREE_MODEL (ia->text_file));
632 chooser_page_reset (PsppireImportAssistant *ia, GtkWidget *page)
634 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
636 gtk_file_chooser_set_filter (fc, ia->default_filter);
637 gtk_file_chooser_unselect_all (fc);
639 on_chosen (ia, page);
645 chooser_page_create (PsppireImportAssistant *ia)
647 GtkFileFilter *filter = NULL;
649 GtkWidget *chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
651 g_object_set_data (G_OBJECT (chooser), "on-forward", chooser_page_leave);
652 g_object_set_data (G_OBJECT (chooser), "on-reset", chooser_page_reset);
653 g_object_set_data (G_OBJECT (chooser), "on-entering",chooser_page_enter);
655 g_object_set (chooser, "local-only", FALSE, NULL);
658 ia->default_filter = gtk_file_filter_new ();
659 gtk_file_filter_set_name (ia->default_filter, _("All Files"));
660 gtk_file_filter_add_pattern (ia->default_filter, "*");
661 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), ia->default_filter);
663 filter = gtk_file_filter_new ();
664 gtk_file_filter_set_name (filter, _("Text Files"));
665 gtk_file_filter_add_mime_type (filter, "text/*");
666 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
668 filter = gtk_file_filter_new ();
669 gtk_file_filter_set_name (filter, _("Text (*.txt) Files"));
670 gtk_file_filter_add_pattern (filter, "*.txt");
671 gtk_file_filter_add_pattern (filter, "*.TXT");
672 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
674 filter = gtk_file_filter_new ();
675 gtk_file_filter_set_name (filter, _("Plain Text (ASCII) Files"));
676 gtk_file_filter_add_mime_type (filter, "text/plain");
677 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
679 filter = gtk_file_filter_new ();
680 gtk_file_filter_set_name (filter, _("Comma Separated Value Files"));
681 gtk_file_filter_add_mime_type (filter, "text/csv");
682 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
684 /* I've never encountered one of these, but it's listed here:
685 http://www.iana.org/assignments/media-types/text/tab-separated-values */
686 filter = gtk_file_filter_new ();
687 gtk_file_filter_set_name (filter, _("Tab Separated Value Files"));
688 gtk_file_filter_add_mime_type (filter, "text/tab-separated-values");
689 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
691 filter = gtk_file_filter_new ();
692 gtk_file_filter_set_name (filter, _("Gnumeric Spreadsheet Files"));
693 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
694 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
696 filter = gtk_file_filter_new ();
697 gtk_file_filter_set_name (filter, _("OpenDocument Spreadsheet Files"));
698 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
699 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
701 filter = gtk_file_filter_new ();
702 gtk_file_filter_set_name (filter, _("All Spreadsheet Files"));
703 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
704 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
705 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
707 ia->encoding_selector = psppire_encoding_selector_new ("Auto", TRUE);
708 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (chooser), ia->encoding_selector);
710 add_page_to_assistant (ia, chooser,
711 GTK_ASSISTANT_PAGE_INTRO, _("Select File to Import"));
713 g_signal_connect_swapped (chooser, "selection-changed", G_CALLBACK (on_chosen), ia);
714 g_signal_connect_swapped (chooser, "map", G_CALLBACK (on_map), ia);
720 psppire_import_assistant_init (PsppireImportAssistant *ia)
722 ia->builder = builder_new ("text-data-import.ui");
724 ia->current_page = -1 ;
725 ia->file_name = NULL;
727 ia->spreadsheet = NULL;
729 ia->main_loop = g_main_loop_new (NULL, TRUE);
731 g_signal_connect (ia, "prepare", G_CALLBACK (on_prepare), ia);
732 g_signal_connect (ia, "cancel", G_CALLBACK (on_cancel), ia);
733 g_signal_connect (ia, "close", G_CALLBACK (on_close), ia);
735 ia->paste_button = gtk_button_new_with_label (_("Paste"));
736 ia->reset_button = gtk_button_new_with_label (_("Reset"));
738 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->paste_button);
740 g_signal_connect (ia->paste_button, "clicked", G_CALLBACK (on_paste), ia);
741 g_signal_connect (ia->reset_button, "clicked", G_CALLBACK (on_reset), ia);
743 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->reset_button);
745 gtk_window_set_title (GTK_WINDOW (ia),
746 _("Importing Delimited Text Data"));
748 gtk_window_set_icon_name (GTK_WINDOW (ia), "pspp");
750 chooser_page_create (ia);
752 gtk_assistant_set_forward_page_func (GTK_ASSISTANT (ia), next_page_func, NULL, NULL);
754 gtk_window_maximize (GTK_WINDOW (ia));
758 /* Appends a page of the given TYPE, with PAGE as its content, to
759 the GtkAssistant encapsulated by IA. Returns the GtkWidget
760 that represents the page. */
762 add_page_to_assistant (PsppireImportAssistant *ia,
763 GtkWidget *page, GtkAssistantPageType type, const gchar *title)
765 GtkWidget *content = page;
767 gtk_assistant_append_page (GTK_ASSISTANT (ia), content);
768 gtk_assistant_set_page_type (GTK_ASSISTANT(ia), content, type);
769 gtk_assistant_set_page_title (GTK_ASSISTANT(ia), content, title);
770 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), content, TRUE);
776 /* Called when one of the radio buttons is clicked. */
778 on_intro_amount_changed (PsppireImportAssistant *p)
780 gtk_widget_set_sensitive (p->n_cases_spin,
781 gtk_toggle_button_get_active
782 (GTK_TOGGLE_BUTTON (p->n_cases_button)));
784 gtk_widget_set_sensitive (p->percent_spin,
785 gtk_toggle_button_get_active
786 (GTK_TOGGLE_BUTTON (p->percent_button)));
790 on_treeview_selection_change (PsppireImportAssistant *ia)
792 GtkTreeSelection *selection =
793 gtk_tree_view_get_selection (GTK_TREE_VIEW (ia->first_line_tree_view));
794 GtkTreeModel *model = NULL;
796 if (gtk_tree_selection_get_selected (selection, &model, &iter))
800 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
801 gint *index = gtk_tree_path_get_indices (path);
803 gtk_tree_path_free (path);
804 g_object_get (model, "maximum-lines", &max_lines, NULL);
805 gtk_widget_set_sensitive (ia->variable_names_cb,
806 (n > 0 && n < max_lines));
807 ia->delimiters_model =
808 psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file));
809 g_object_set (ia->delimiters_model, "first-line", n, NULL);
814 render_text_preview_line (GtkTreeViewColumn *tree_column,
815 GtkCellRenderer *cell,
816 GtkTreeModel *tree_model,
821 Set the text to a "insensitive" state if the row
822 is greater than what the user declared to be the maximum.
824 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (data);
825 GtkTreePath *path = gtk_tree_model_get_path (tree_model, iter);
826 gint *ii = gtk_tree_path_get_indices (path);
828 g_object_get (tree_model, "maximum-lines", &max_lines, NULL);
829 g_object_set (cell, "sensitive", (*ii < max_lines), NULL);
830 gtk_tree_path_free (path);
833 /* Initializes IA's first_line substructure. */
835 first_line_page_create (PsppireImportAssistant *ia)
837 GtkWidget *w = get_widget_assert (ia->builder, "FirstLine");
839 g_object_set_data (G_OBJECT (w), "on-entering", on_treeview_selection_change);
841 add_page_to_assistant (ia, w,
842 GTK_ASSISTANT_PAGE_CONTENT, _("Select the First Line"));
844 GtkWidget *scrolled_window = get_widget_assert (ia->builder, "first-line-scroller");
846 if (ia->first_line_tree_view == NULL)
848 ia->first_line_tree_view = gtk_tree_view_new ();
849 g_object_set (ia->first_line_tree_view, "enable-search", FALSE, NULL);
851 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (ia->first_line_tree_view), TRUE);
853 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
854 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes (_("Line"), renderer,
858 gtk_tree_view_column_set_cell_data_func (column, renderer, render_text_preview_line, ia, 0);
859 gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
861 renderer = gtk_cell_renderer_text_new ();
862 column = gtk_tree_view_column_new_with_attributes (_("Text"), renderer, "text", 1, NULL);
863 gtk_tree_view_column_set_cell_data_func (column, renderer, render_text_preview_line, ia, 0);
865 gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
867 g_signal_connect_swapped (ia->first_line_tree_view, "cursor-changed",
868 G_CALLBACK (on_treeview_selection_change), ia);
869 gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view);
872 gtk_widget_show_all (scrolled_window);
874 ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
878 intro_on_leave (PsppireImportAssistant *ia)
881 g_object_get (ia->text_file, "line-count", &lc, NULL);
882 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
884 gint max_lines = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin));
885 g_object_set (ia->text_file, "maximum-lines", max_lines, NULL);
887 else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
889 gdouble percent = gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin));
890 g_object_set (ia->text_file, "maximum-lines", (gint) (lc * percent / 100.0), NULL);
894 g_object_set (ia->text_file, "maximum-lines", lc, NULL);
900 intro_on_enter (PsppireImportAssistant *ia)
902 GtkBuilder *builder = ia->builder;
903 GtkWidget *table = get_widget_assert (builder, "button-table");
908 ds_put_cstr (&s, _("This assistant will guide you through the process of "
909 "importing data into PSPP from a text file with one line "
910 "per case, in which fields are separated by tabs, "
911 "commas, or other delimiters.\n\n"));
915 if (ia->text_file->total_is_exact)
918 &s, ngettext ("The selected file contains %'lu line of text. ",
919 "The selected file contains %'lu lines of text. ",
920 ia->text_file->total_lines),
921 ia->text_file->total_lines);
923 else if (ia->text_file->total_lines > 0)
927 "The selected file contains approximately %'lu line of text. ",
928 "The selected file contains approximately %'lu lines of text. ",
929 ia->text_file->total_lines),
930 ia->text_file->total_lines);
933 "Only the first %zu line of the file will be shown for "
934 "preview purposes in the following screens. ",
935 "Only the first %zu lines of the file will be shown for "
936 "preview purposes in the following screens. ",
937 ia->text_file->line_cnt),
938 ia->text_file->line_cnt);
942 ds_put_cstr (&s, _("You may choose below how much of the file should "
943 "actually be imported."));
945 gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
949 if (gtk_grid_get_child_at (GTK_GRID (table), 1, 1) == NULL)
951 GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
952 gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
957 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (ia->n_cases_spin));
958 gtk_adjustment_set_lower (adj, 1.0);
960 if (gtk_grid_get_child_at (GTK_GRID (table), 1, 2) == NULL)
962 GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
965 gtk_grid_attach (GTK_GRID (table), hbox_percent,
970 gtk_widget_show_all (table);
972 on_intro_amount_changed (ia);
975 /* Initializes IA's intro substructure. */
977 intro_page_create (PsppireImportAssistant *ia)
979 GtkBuilder *builder = ia->builder;
981 GtkWidget *w = get_widget_assert (builder, "Intro");
983 ia->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
986 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Select the Lines to Import"));
988 ia->all_cases_button = get_widget_assert (builder, "import-all-cases");
990 ia->n_cases_button = get_widget_assert (builder, "import-n-cases");
992 ia->percent_button = get_widget_assert (builder, "import-percent");
994 g_signal_connect_swapped (ia->all_cases_button, "toggled",
995 G_CALLBACK (on_intro_amount_changed), ia);
996 g_signal_connect_swapped (ia->n_cases_button, "toggled",
997 G_CALLBACK (on_intro_amount_changed), ia);
998 g_signal_connect_swapped (ia->percent_button, "toggled",
999 G_CALLBACK (on_intro_amount_changed), ia);
1002 g_object_set_data (G_OBJECT (w), "on-forward", intro_on_leave);
1003 g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter);
1004 g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page);
1009 psppire_import_assistant_new (GtkWindow *toplevel)
1011 return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_IMPORT_ASSISTANT,
1012 /* Some window managers (notably ratpoison)
1013 ignore the maximise command when a window is
1014 transient. This causes problems for this
1016 /* "transient-for", toplevel, */
1025 set_quote_list (GtkComboBox *cb)
1027 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
1030 const gchar *separator[3] = {"'\"", "\'", "\""};
1032 for (i = 0; i < 3; i++)
1034 const gchar *s = separator[i];
1036 /* Add a new row to the model */
1037 gtk_list_store_append (list, &iter);
1038 gtk_list_store_set (list, &iter,
1044 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
1045 g_object_unref (list);
1047 gtk_combo_box_set_entry_text_column (cb, 0);
1052 /* Chooses a name for each column on the separators page */
1054 choose_column_names (PsppireImportAssistant *ia)
1057 unsigned long int generated_name_count = 0;
1058 dict_clear (ia->dict);
1060 for (i = 0; i < gtk_tree_model_get_n_columns (ia->delimiters_model) - 1; ++i)
1062 const gchar *candidate_name = NULL;
1064 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
1066 candidate_name = psppire_delimited_text_get_header_title (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
1069 char *name = dict_make_unique_var_name (ia->dict,
1071 &generated_name_count);
1073 dict_create_var_assert (ia->dict, name, 0);
1080 /* Called when the user toggles one of the separators
1083 on_separator_toggle (GtkToggleButton *toggle UNUSED,
1084 PsppireImportAssistant *ia)
1087 GSList *delimiters = NULL;
1088 for (i = 0; i < SEPARATOR_CNT; i++)
1090 const struct separator *s = &separators[i];
1091 GtkWidget *button = get_widget_assert (ia->builder, s->name);
1092 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1094 delimiters = g_slist_prepend (delimiters, GINT_TO_POINTER (s->c));
1098 g_object_set (ia->delimiters_model, "delimiters", delimiters, NULL);
1100 revise_fields_preview (ia);
1104 /* Called when the user changes the entry field for custom
1107 on_separators_custom_entry_notify (GObject *gobject UNUSED,
1108 GParamSpec *arg1 UNUSED,
1109 PsppireImportAssistant *ia)
1111 revise_fields_preview (ia);
1114 /* Called when the user toggles the checkbox that enables custom
1117 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
1118 PsppireImportAssistant *ia)
1120 bool is_active = gtk_toggle_button_get_active (custom_cb);
1121 gtk_widget_set_sensitive (ia->custom_entry, is_active);
1122 revise_fields_preview (ia);
1125 /* Called when the user changes the selection in the combo box
1126 that selects a quote character. */
1128 on_quote_combo_change (GtkComboBox *combo, PsppireImportAssistant *ia)
1130 // revise_fields_preview (ia);
1133 /* Called when the user toggles the checkbox that enables
1136 on_quote_cb_toggle (GtkToggleButton *quote_cb, PsppireImportAssistant *ia)
1138 bool is_active = gtk_toggle_button_get_active (quote_cb);
1139 gtk_widget_set_sensitive (ia->quote_combo, is_active);
1140 revise_fields_preview (ia);
1143 /* Initializes IA's separators substructure. */
1145 separators_page_create (PsppireImportAssistant *ia)
1147 GtkBuilder *builder = ia->builder;
1151 GtkWidget *w = get_widget_assert (builder, "Separators");
1153 g_object_set_data (G_OBJECT (w), "on-entering", prepare_separators_page);
1154 g_object_set_data (G_OBJECT (w), "on-reset", prepare_separators_page);
1156 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Choose Separators"));
1158 ia->custom_cb = get_widget_assert (builder, "custom-cb");
1159 ia->custom_entry = get_widget_assert (builder, "custom-entry");
1160 ia->quote_combo = get_widget_assert (builder, "quote-combo");
1161 ia->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo)));
1162 ia->quote_cb = get_widget_assert (builder, "quote-cb");
1164 set_quote_list (GTK_COMBO_BOX (ia->quote_combo));
1166 if (ia->fields_tree_view == NULL)
1168 GtkWidget *scroller = get_widget_assert (ia->builder, "fields-scroller");
1169 ia->fields_tree_view = gtk_tree_view_new ();
1170 g_object_set (ia->fields_tree_view, "enable-search", FALSE, NULL);
1171 gtk_container_add (GTK_CONTAINER (scroller), GTK_WIDGET (ia->fields_tree_view));
1172 gtk_widget_show_all (scroller);
1175 g_signal_connect (ia->quote_combo, "changed",
1176 G_CALLBACK (on_quote_combo_change), ia);
1177 g_signal_connect (ia->quote_cb, "toggled",
1178 G_CALLBACK (on_quote_cb_toggle), ia);
1179 g_signal_connect (ia->custom_entry, "notify::text",
1180 G_CALLBACK (on_separators_custom_entry_notify), ia);
1181 g_signal_connect (ia->custom_cb, "toggled",
1182 G_CALLBACK (on_separators_custom_cb_toggle), ia);
1183 for (i = 0; i < SEPARATOR_CNT; i++)
1184 g_signal_connect (get_widget_assert (builder, separators[i].name),
1185 "toggled", G_CALLBACK (on_separator_toggle), ia);
1194 static struct casereader_random_class my_casereader_class;
1196 static struct ccase *
1197 my_read (struct casereader *reader, void *aux, casenumber idx)
1199 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
1200 GtkTreeModel *tm = GTK_TREE_MODEL (ia->delimiters_model);
1202 GtkTreePath *tp = gtk_tree_path_new_from_indices (idx, -1);
1204 const struct caseproto *proto = casereader_get_proto (reader);
1207 struct ccase *c = NULL;
1208 if (gtk_tree_model_get_iter (tm, &iter, tp))
1210 c = case_create (proto);
1212 for (i = 0 ; i < caseproto_get_n_widths (proto); ++i)
1215 gtk_tree_model_get_value (tm, &iter, i + 1, &value);
1217 const struct variable *var = dict_get_var (ia->dict, i);
1219 const gchar *ss = g_value_get_string (&value);
1222 union value *v = case_data_rw (c, var);
1223 char *xx = data_in (ss_cstr (ss),
1225 var_get_write_format (var)->type,
1226 v, var_get_width (var), "UTF-8");
1229 /* g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
1232 g_value_unset (&value);
1236 gtk_tree_path_free (tp);
1242 my_destroy (struct casereader *reader, void *aux)
1244 g_print ("%s:%d %p\n", __FILE__, __LINE__, reader);
1248 my_advance (struct casereader *reader, void *aux, casenumber cnt)
1250 g_print ("%s:%d\n", __FILE__, __LINE__);
1254 foo (struct dictionary *dict, void *aux)
1256 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
1257 g_print ("%s:%d\n", __FILE__, __LINE__);
1259 struct caseproto *proto = caseproto_create ();
1262 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1264 const struct variable *var = dict_get_var (ia->dict, i);
1265 proto = caseproto_add_width (proto, var_get_width (var));
1269 gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
1271 struct casereader *reader =
1272 casereader_create_random (proto, n_rows, &my_casereader_class, ia);
1275 PsppireDataStore *store = NULL;
1277 g_object_get (ia->data_sheet, "data-model", &store, NULL);
1279 psppire_data_store_set_reader (store, reader);
1282 /* Called just before the formats page of the assistant is
1285 prepare_formats_page (PsppireImportAssistant *ia)
1287 PsppireDict *dict = psppire_dict_new_from_dict (ia->dict);
1288 g_object_set (ia->var_sheet, "data-model", dict, NULL);
1290 my_casereader_class.read = my_read;
1291 my_casereader_class.destroy = my_destroy;
1292 my_casereader_class.advance = my_advance;
1294 struct caseproto *proto = caseproto_create ();
1297 struct fmt_guesser **fg = xcalloc (sizeof *fg, dict_get_var_cnt (ia->dict));
1298 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1300 fg[i] = fmt_guesser_create ();
1303 gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
1307 for (ok = gtk_tree_model_get_iter_first (ia->delimiters_model, &iter);
1309 ok = gtk_tree_model_iter_next (ia->delimiters_model, &iter))
1311 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1314 gtk_tree_model_get (ia->delimiters_model, &iter, i+1, &s, -1);
1316 fmt_guesser_add (fg[i], ss_cstr (s));
1321 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1324 fmt_guesser_guess (fg[i], &fs);
1326 fmt_fix (&fs, FMT_FOR_INPUT);
1328 struct variable *var = dict_get_var (ia->dict, i);
1330 int width = fmt_var_width (&fs);
1332 var_set_width_and_formats (var, width,
1335 proto = caseproto_add_width (proto, width);
1336 fmt_guesser_destroy (fg[i]);
1341 // dict_set_change_callback (ia->dict, foo, ia);
1343 struct casereader *reader =
1344 casereader_create_random (proto, n_rows, &my_casereader_class, ia);
1346 PsppireDataStore *store = psppire_data_store_new (dict);
1347 psppire_data_store_set_reader (store, reader);
1349 g_object_set (ia->data_sheet, "data-model", store, NULL);
1353 g_object_get (get_widget_assert (ia->builder, "vpaned1"),
1354 "max-position", &pmax, NULL);
1357 g_object_set (get_widget_assert (ia->builder, "vpaned1"),
1358 "position", pmax / 2, NULL);
1360 gtk_widget_show (ia->paste_button);
1364 formats_page_create (PsppireImportAssistant *ia)
1366 GtkBuilder *builder = ia->builder;
1368 GtkWidget *w = get_widget_assert (builder, "Formats");
1369 g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
1370 g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
1372 GtkWidget *vars_scroller = get_widget_assert (builder, "vars-scroller");
1373 if (ia->var_sheet == NULL)
1375 ia->var_sheet = psppire_variable_sheet_new ();
1377 gtk_container_add (GTK_CONTAINER (vars_scroller), ia->var_sheet);
1379 ia->dict = dict_create (get_default_encoding ());
1381 gtk_widget_show_all (vars_scroller);
1383 GtkWidget *data_scroller = get_widget_assert (builder, "data-scroller");
1384 if (ia->data_sheet == NULL)
1386 ia->data_sheet = psppire_data_sheet_new ();
1388 gtk_container_add (GTK_CONTAINER (data_scroller), ia->data_sheet);
1390 gtk_widget_show_all (data_scroller);
1393 add_page_to_assistant (ia, w,
1394 GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
1401 separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1405 ds_put_cstr (s, " /DELIMITERS=\"");
1407 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (get_widget_assert (ia->builder, "tab"))))
1408 ds_put_cstr (s, "\\t");
1409 for (i = 0; i < SEPARATOR_CNT; i++)
1411 const struct separator *seps = &separators[i];
1412 GtkWidget *button = get_widget_assert (ia->builder, seps->name);
1413 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1415 if (seps->c == '\t')
1418 ds_put_byte (s, seps->c);
1421 ds_put_cstr (s, "\"\n");
1422 if (!ds_is_empty (&ia->quotes))
1423 syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
1427 formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1432 g_return_if_fail (ia->dict);
1434 ds_put_cstr (s, " /VARIABLES=\n");
1436 var_cnt = dict_get_var_cnt (ia->dict);
1437 for (i = 0; i < var_cnt; i++)
1439 struct variable *var = dict_get_var (ia->dict, i);
1440 char format_string[FMT_STRING_LEN_MAX + 1];
1441 fmt_to_string (var_get_print_format (var), format_string);
1442 ds_put_format (s, " %s %s%s\n",
1443 var_get_name (var), format_string,
1444 i == var_cnt - 1 ? "." : "");
1449 first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1451 gint first_case = 0;
1452 g_object_get (ia->delimiters_model, "first-line", &first_case, NULL);
1455 ds_put_format (s, " /FIRSTCASE=%d\n", first_case + 1);
1459 intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1461 gint first_line = 0;
1462 g_object_get (ia->delimiters_model, "first-line", &first_line, NULL);
1464 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
1465 ds_put_format (s, "SELECT IF ($CASENUM <= %d).\n",
1466 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) - first_line);
1467 else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
1468 ds_put_format (s, "SAMPLE %.4g.\n",
1469 gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
1473 /* Emits PSPP syntax to S that applies the dictionary attributes
1474 (such as missing values and value labels) of the variables in
1477 apply_dict (const struct dictionary *dict, struct string *s)
1479 size_t var_cnt = dict_get_var_cnt (dict);
1482 for (i = 0; i < var_cnt; i++)
1484 struct variable *var = dict_get_var (dict, i);
1485 const char *name = var_get_name (var);
1486 enum val_type type = var_get_type (var);
1487 int width = var_get_width (var);
1488 enum measure measure = var_get_measure (var);
1489 enum var_role role = var_get_role (var);
1490 enum alignment alignment = var_get_alignment (var);
1491 const struct fmt_spec *format = var_get_print_format (var);
1493 if (var_has_missing_values (var))
1495 const struct missing_values *mv = var_get_missing_values (var);
1498 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
1499 for (j = 0; j < mv_n_values (mv); j++)
1502 ds_put_cstr (s, ", ");
1503 syntax_gen_value (s, mv_get_value (mv, j), width, format);
1506 if (mv_has_range (mv))
1509 if (mv_has_value (mv))
1510 ds_put_cstr (s, ", ");
1511 mv_get_range (mv, &low, &high);
1512 syntax_gen_num_range (s, low, high, format);
1514 ds_put_cstr (s, ").\n");
1516 if (var_has_value_labels (var))
1518 const struct val_labs *vls = var_get_value_labels (var);
1519 const struct val_lab **labels = val_labs_sorted (vls);
1520 size_t n_labels = val_labs_count (vls);
1523 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
1524 for (i = 0; i < n_labels; i++)
1526 const struct val_lab *vl = labels[i];
1527 ds_put_cstr (s, "\n ");
1528 syntax_gen_value (s, &vl->value, width, format);
1529 ds_put_byte (s, ' ');
1530 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
1533 ds_put_cstr (s, ".\n");
1535 if (var_has_label (var))
1536 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
1537 name, var_get_label (var));
1538 if (measure != var_default_measure (type))
1539 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
1540 name, measure_to_syntax (measure));
1541 if (role != ROLE_INPUT)
1542 syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
1543 var_role_to_syntax (role), name);
1544 if (alignment != var_default_alignment (type))
1545 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
1546 name, alignment_to_syntax (alignment));
1547 if (var_get_display_width (var) != var_default_display_width (width))
1548 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
1549 name, var_get_display_width (var));
1556 sheet_spec_gen_syntax (PsppireImportAssistant *ia)
1558 GtkBuilder *builder = ia->builder;
1559 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
1560 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
1561 GtkWidget *rnc = get_widget_assert (builder, "readnames-checkbox");
1562 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
1563 int sheet_index = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (sheet_entry));
1564 gboolean read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rnc));
1566 struct string s = DS_EMPTY_INITIALIZER;
1569 g_object_get (ia->text_file, "file-name", &filename, NULL);
1570 syntax_gen_pspp (&s,
1574 "\n /SHEET=index %d"
1575 "\n /READNAMES=%ss",
1576 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
1579 read_names ? "ON" : "OFF");
1581 if (range && 0 != strcmp ("", range))
1583 syntax_gen_pspp (&s,
1584 "\n /CELLRANGE=RANGE %sq", range);
1588 syntax_gen_pspp (&s,
1589 "\n /CELLRANGE=FULL");
1593 syntax_gen_pspp (&s, ".");
1596 return ds_cstr (&s);
1601 psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
1603 struct string s = DS_EMPTY_INITIALIZER;
1605 if (!ia->spreadsheet)
1607 gchar *file_name = NULL;
1608 gchar *encoding = NULL;
1609 g_object_get (ia->text_file,
1610 "file-name", &file_name,
1611 "encoding", &encoding,
1614 if (file_name == NULL)
1617 syntax_gen_pspp (&s,
1622 if (encoding && strcmp (encoding, "Auto"))
1623 syntax_gen_pspp (&s, " /ENCODING=%sq\n", encoding);
1626 " /ARRANGEMENT=DELIMITED\n"
1627 " /DELCASE=LINE\n");
1629 first_line_append_syntax (ia, &s);
1630 separators_append_syntax (ia, &s);
1632 formats_append_syntax (ia, &s);
1633 apply_dict (ia->dict, &s);
1634 intro_append_syntax (ia, &s);
1638 return sheet_spec_gen_syntax (ia);
1641 return ds_cstr (&s);
1646 psppire_import_assistant_run (PsppireImportAssistant *asst)
1648 g_main_loop_run (asst->main_loop);
1649 return asst->response;