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/>. */
24 #include "data/casereader.h"
25 #include "data/data-in.h"
26 #include "data/data-out.h"
27 #include "data/dictionary.h"
28 #include "data/format-guesser.h"
29 #include "data/format.h"
30 #include "data/gnumeric-reader.h"
31 #include "data/ods-reader.h"
32 #include "data/spreadsheet-reader.h"
33 #include "data/value-labels.h"
34 #include "data/casereader-provider.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/hmap.h"
42 #include "libpspp/hash-functions.h"
43 #include "libpspp/str.h"
45 #include "builder-wrapper.h"
47 #include "psppire-import-assistant.h"
48 #include "psppire-scanf.h"
49 #include "psppire-dialog.h"
50 #include "psppire-encoding-selector.h"
51 #include "psppire-spreadsheet-model.h"
52 #include "psppire-text-file.h"
53 #include "psppire-delimited-text.h"
54 #include "psppire-data-sheet.h"
55 #include "psppire-data-store.h"
56 #include "psppire-dict.h"
57 #include "psppire-variable-sheet.h"
59 #include "ui/syntax-gen.h"
62 #define _(msgid) gettext (msgid)
63 #define N_(msgid) msgid
65 enum { MAX_LINE_LEN = 16384 }; /* Max length of an acceptable line. */
68 /* Sets IA's separators substructure to match the widgets. */
69 static void split_fields (PsppireImportAssistant *ia);
71 /* Chooses a name for each column on the separators page */
72 static void choose_column_names (PsppireImportAssistant *ia);
76 static void intro_page_create (PsppireImportAssistant *ia);
77 static void first_line_page_create (PsppireImportAssistant *ia);
79 static void separators_page_create (PsppireImportAssistant *ia);
80 static void formats_page_create (PsppireImportAssistant *ia);
82 static void push_watch_cursor (PsppireImportAssistant *ia);
83 static void pop_watch_cursor (PsppireImportAssistant *ia);
87 static void psppire_import_assistant_init (PsppireImportAssistant *act);
88 static void psppire_import_assistant_class_init (PsppireImportAssistantClass *class);
90 G_DEFINE_TYPE (PsppireImportAssistant, psppire_import_assistant, GTK_TYPE_ASSISTANT);
100 psppire_import_assistant_set_property (GObject *object,
105 // PsppireImportAssistant *act = PSPPIRE_IMPORT_ASSISTANT (object);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 psppire_import_assistant_get_property (GObject *object,
122 // PsppireImportAssistant *assistant = PSPPIRE_IMPORT_ASSISTANT (object);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 static GObjectClass * parent_class = NULL;
136 psppire_import_assistant_finalize (GObject *object)
138 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (object);
142 spreadsheet_unref (ia->spreadsheet);
144 ds_destroy (&ia->quotes);
146 g_object_unref (ia->builder);
148 if (G_OBJECT_CLASS (parent_class)->finalize)
149 G_OBJECT_CLASS (parent_class)->finalize (object);
154 psppire_import_assistant_class_init (PsppireImportAssistantClass *class)
156 GObjectClass *object_class = G_OBJECT_CLASS (class);
158 parent_class = g_type_class_peek_parent (class);
160 object_class->set_property = psppire_import_assistant_set_property;
161 object_class->get_property = psppire_import_assistant_get_property;
163 object_class->finalize = psppire_import_assistant_finalize;
167 /* Causes the assistant to close, returning RESPONSE for
168 interpretation by text_data_import_assistant. */
170 close_assistant (PsppireImportAssistant *ia, int response)
172 ia->response = response;
173 g_main_loop_quit (ia->main_loop);
174 gtk_widget_hide (GTK_WIDGET (ia));
178 /* Called when the Paste button on the last page of the assistant
181 on_paste (GtkButton *button, PsppireImportAssistant *ia)
183 close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
187 /* Revises the contents of the fields tree view based on the
188 currently chosen set of separators. */
190 revise_fields_preview (PsppireImportAssistant *ia)
192 choose_column_names (ia);
198 const char *name; /* Name (for use with get_widget_assert). */
199 gunichar c; /* Separator character. */
202 /* All the separators in the dialog box. */
203 static const struct separator separators[] =
215 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
217 struct separator_count_node
219 struct hmap_node node;
220 int occurance; /* The number of times the separator occurs in a line */
221 int quantity; /* The number of lines with this occurance */
225 /* Picks the most likely separator and quote characters based on
228 choose_likely_separators (PsppireImportAssistant *ia)
231 g_object_get (ia->delimiters_model, "first-line", &first_line, NULL);
237 struct hmap count_map[SEPARATOR_CNT];
238 for (j = 0; j < SEPARATOR_CNT; ++j)
239 hmap_init (count_map + j);
241 GtkTreePath *p = gtk_tree_path_new_from_indices (first_line, -1);
243 for (valid = gtk_tree_model_get_iter (GTK_TREE_MODEL (ia->text_file), &iter, p);
245 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->text_file), &iter))
247 gchar *line_text = NULL;
248 gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &line_text, -1);
250 gint *counts = xzalloc (sizeof *counts * SEPARATOR_CNT);
252 struct substring cs = ss_cstr (line_text);
254 UINT32_MAX != ss_first_mb (cs);
257 ucs4_t character = ss_first_mb (cs);
260 for (s = 0; s < SEPARATOR_CNT; ++s)
262 if (character == separators[s].c)
268 for (j = 0; j < SEPARATOR_CNT; ++j)
272 struct separator_count_node *cn = NULL;
273 unsigned int hash = hash_int (counts[j], 0);
274 HMAP_FOR_EACH_WITH_HASH (cn, struct separator_count_node, node, hash, &count_map[j])
276 if (cn->occurance == counts[j])
282 struct separator_count_node *new_cn = xzalloc (sizeof *new_cn);
283 new_cn->occurance = counts[j];
284 new_cn->quantity = 1;
285 hmap_insert (&count_map[j], &new_cn->node, hash);
295 gtk_tree_path_free (p);
297 int most_frequent = -1;
299 for (j = 0; j < SEPARATOR_CNT; ++j)
301 struct separator_count_node *cn;
302 HMAP_FOR_EACH (cn, struct separator_count_node, node, &count_map[j])
304 if (largest < cn->quantity)
306 largest = cn->quantity;
310 hmap_destroy (&count_map[j]);
313 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (get_widget_assert (ia->builder, separators[most_frequent].name)), TRUE);
317 repopulate_delimiter_columns (PsppireImportAssistant *ia)
319 /* Remove all the columns */
320 while (gtk_tree_view_get_n_columns (GTK_TREE_VIEW (ia->fields_tree_view)) > 0)
322 GtkTreeViewColumn *tvc = gtk_tree_view_get_column (GTK_TREE_VIEW (ia->fields_tree_view), 0);
323 gtk_tree_view_remove_column (GTK_TREE_VIEW (ia->fields_tree_view), tvc);
326 gint n_fields = gtk_tree_model_get_n_columns (ia->delimiters_model);
328 /* ... and put them back again. */
330 for (f = gtk_tree_view_get_n_columns (GTK_TREE_VIEW (ia->fields_tree_view));
333 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
335 const gchar *title = NULL;
341 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
344 psppire_delimited_text_get_header_title
345 (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), f - 1);
351 GtkTreeViewColumn *column =
352 gtk_tree_view_column_new_with_attributes (title,
356 g_object_set (column,
358 "sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE,
361 gtk_tree_view_append_column (GTK_TREE_VIEW (ia->fields_tree_view), column);
366 reset_tree_view_model (PsppireImportAssistant *ia)
368 GtkTreeModel *tm = gtk_tree_view_get_model (GTK_TREE_VIEW (ia->fields_tree_view));
370 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), NULL);
373 repopulate_delimiter_columns (ia);
375 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), tm);
376 // gtk_tree_view_columns_autosize (GTK_TREE_VIEW (ia->fields_tree_view));
381 /* Called just before the separators page becomes visible in the
382 assistant, and when the Reset button is clicked. */
384 prepare_separators_page (PsppireImportAssistant *ia, GtkWidget *page)
386 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), ia->delimiters_model);
388 g_signal_connect_swapped (ia->delimiters_model, "notify::delimiters",
389 G_CALLBACK (reset_tree_view_model), ia);
392 repopulate_delimiter_columns (ia);
394 revise_fields_preview (ia);
395 choose_likely_separators (ia);
398 /* Resets IA's intro page to its initial state. */
400 reset_intro_page (PsppireImportAssistant *ia)
402 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->all_cases_button),
408 /* Clears the set of user-modified variables from IA's formats
409 substructure. This discards user modifications to variable
410 formats, thereby causing formats to revert to their
413 reset_formats_page (PsppireImportAssistant *ia, GtkWidget *page)
417 static void prepare_formats_page (PsppireImportAssistant *ia);
419 /* Called when the Reset button is clicked. */
421 on_reset (GtkButton *button, PsppireImportAssistant *ia)
423 gint pn = gtk_assistant_get_current_page (GTK_ASSISTANT (ia));
425 GtkWidget *page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
427 page_func *on_reset = g_object_get_data (G_OBJECT (page), "on-reset");
436 next_page_func (gint old_page, gpointer data)
442 /* Called just before PAGE is displayed as the current page of
443 IMPORT_ASSISTANT, this updates IA content according to the new
446 on_prepare (GtkAssistant *assistant, GtkWidget *page, PsppireImportAssistant *ia)
448 gtk_widget_show (ia->reset_button);
449 gtk_widget_hide (ia->paste_button);
451 gint pn = gtk_assistant_get_current_page (assistant);
452 gint previous_page_index = ia->current_page;
454 if (previous_page_index >= 0)
456 GtkWidget *closing_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), previous_page_index);
458 if (pn > previous_page_index)
460 page_func *on_forward = g_object_get_data (G_OBJECT (closing_page), "on-forward");
463 on_forward (ia, closing_page);
467 page_func *on_back = g_object_get_data (G_OBJECT (closing_page), "on-back");
470 on_back (ia, closing_page);
475 GtkWidget *new_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (ia), pn);
477 page_func *on_entering = g_object_get_data (G_OBJECT (new_page), "on-entering");
480 on_entering (ia, new_page);
483 ia->current_page = pn;
486 /* Called when the Cancel button in the assistant is clicked. */
488 on_cancel (GtkAssistant *assistant, PsppireImportAssistant *ia)
490 close_assistant (ia, GTK_RESPONSE_CANCEL);
493 /* Called when the Apply button on the last page of the assistant
496 on_close (GtkAssistant *assistant, PsppireImportAssistant *ia)
498 close_assistant (ia, GTK_RESPONSE_APPLY);
503 add_page_to_assistant (PsppireImportAssistant *ia,
504 GtkWidget *page, GtkAssistantPageType type, const gchar *);
508 on_sheet_combo_changed (GtkComboBox *cb, PsppireImportAssistant *ia)
512 GtkTreeModel *model = gtk_combo_box_get_model (cb);
513 GtkBuilder *builder = ia->builder;
514 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
516 gtk_combo_box_get_active_iter (cb, &iter);
517 gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
518 gtk_entry_set_text (GTK_ENTRY (range_entry), range ? range : "");
522 /* Prepares IA's sheet_spec page. */
524 prepare_sheet_spec_page (PsppireImportAssistant *ia)
526 GtkBuilder *builder = ia->builder;
527 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
528 GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
530 gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
531 psppire_spreadsheet_model_new (ia->spreadsheet));
533 gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
535 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (readnames_checkbox), FALSE);
539 /* Initializes IA's sheet_spec substructure. */
541 sheet_spec_page_create (PsppireImportAssistant *ia)
543 GtkBuilder *builder = ia->builder;
544 GtkWidget *page = get_widget_assert (builder, "Spreadsheet-Importer");
546 GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
547 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
548 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box));
549 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
550 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
554 g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
556 add_page_to_assistant (ia, page,
557 GTK_ASSISTANT_PAGE_CONTENT, _("Importing Spreadsheet Data"));
559 g_object_set_data (G_OBJECT (page), "on-entering", prepare_sheet_spec_page);
563 on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
565 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
566 gchar *f = gtk_file_chooser_get_filename (fc);
569 for(i = gtk_assistant_get_n_pages (GTK_ASSISTANT (ia)); i > 0; --i)
570 gtk_assistant_remove_page (GTK_ASSISTANT (ia), i);
572 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), FALSE);
574 if (f && !g_file_test (f, G_FILE_TEST_IS_DIR))
576 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), TRUE);
579 spreadsheet_unref (ia->spreadsheet);
581 ia->spreadsheet = gnumeric_probe (f, FALSE);
583 if (!ia->spreadsheet)
584 ia->spreadsheet = ods_probe (f, FALSE);
586 if (!ia->spreadsheet)
588 intro_page_create (ia);
589 first_line_page_create (ia);
590 separators_page_create (ia);
594 sheet_spec_page_create (ia);
597 formats_page_create (ia);
603 /* This has to be done on a map signal callback,
604 because GtkFileChooserWidget resets everything when it is mapped. */
606 on_map (PsppireImportAssistant *ia, GtkWidget *page)
609 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
612 gtk_file_chooser_set_filename (fc, ia->file_name);
615 on_chosen (ia, page);
621 chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page)
626 chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page)
628 g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
629 g_free (ia->file_name);
630 ia->file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
631 gchar *encoding = psppire_encoding_selector_get_encoding (ia->encoding_selector);
633 if (!ia->spreadsheet)
635 ia->text_file = psppire_text_file_new (ia->file_name, encoding);
636 gtk_tree_view_set_model (GTK_TREE_VIEW (ia->first_line_tree_view),
637 GTK_TREE_MODEL (ia->text_file));
645 chooser_page_reset (PsppireImportAssistant *ia, GtkWidget *page)
647 GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
649 gtk_file_chooser_set_filter (fc, ia->default_filter);
650 gtk_file_chooser_unselect_all (fc);
652 on_chosen (ia, page);
658 chooser_page_create (PsppireImportAssistant *ia)
660 GtkFileFilter *filter = NULL;
662 GtkWidget *chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
664 g_object_set_data (G_OBJECT (chooser), "on-forward", chooser_page_leave);
665 g_object_set_data (G_OBJECT (chooser), "on-reset", chooser_page_reset);
666 g_object_set_data (G_OBJECT (chooser), "on-entering",chooser_page_enter);
668 g_object_set (chooser, "local-only", FALSE, NULL);
671 ia->default_filter = gtk_file_filter_new ();
672 gtk_file_filter_set_name (ia->default_filter, _("All Files"));
673 gtk_file_filter_add_pattern (ia->default_filter, "*");
674 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), ia->default_filter);
676 filter = gtk_file_filter_new ();
677 gtk_file_filter_set_name (filter, _("Text Files"));
678 gtk_file_filter_add_mime_type (filter, "text/*");
679 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
681 filter = gtk_file_filter_new ();
682 gtk_file_filter_set_name (filter, _("Text (*.txt) Files"));
683 gtk_file_filter_add_pattern (filter, "*.txt");
684 gtk_file_filter_add_pattern (filter, "*.TXT");
685 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
687 filter = gtk_file_filter_new ();
688 gtk_file_filter_set_name (filter, _("Plain Text (ASCII) Files"));
689 gtk_file_filter_add_mime_type (filter, "text/plain");
690 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
692 filter = gtk_file_filter_new ();
693 gtk_file_filter_set_name (filter, _("Comma Separated Value Files"));
694 gtk_file_filter_add_mime_type (filter, "text/csv");
695 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
697 /* I've never encountered one of these, but it's listed here:
698 http://www.iana.org/assignments/media-types/text/tab-separated-values */
699 filter = gtk_file_filter_new ();
700 gtk_file_filter_set_name (filter, _("Tab Separated Value Files"));
701 gtk_file_filter_add_mime_type (filter, "text/tab-separated-values");
702 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
704 filter = gtk_file_filter_new ();
705 gtk_file_filter_set_name (filter, _("Gnumeric Spreadsheet Files"));
706 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
707 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
709 filter = gtk_file_filter_new ();
710 gtk_file_filter_set_name (filter, _("OpenDocument Spreadsheet Files"));
711 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
712 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
714 filter = gtk_file_filter_new ();
715 gtk_file_filter_set_name (filter, _("All Spreadsheet Files"));
716 gtk_file_filter_add_mime_type (filter, "application/x-gnumeric");
717 gtk_file_filter_add_mime_type (filter, "application/vnd.oasis.opendocument.spreadsheet");
718 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
720 ia->encoding_selector = psppire_encoding_selector_new ("Auto", TRUE);
721 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (chooser), ia->encoding_selector);
723 add_page_to_assistant (ia, chooser,
724 GTK_ASSISTANT_PAGE_INTRO, _("Select File to Import"));
726 g_signal_connect_swapped (chooser, "selection-changed", G_CALLBACK (on_chosen), ia);
727 g_signal_connect_swapped (chooser, "map", G_CALLBACK (on_map), ia);
733 psppire_import_assistant_init (PsppireImportAssistant *ia)
735 ia->builder = builder_new ("text-data-import.ui");
737 ia->current_page = -1 ;
738 ia->file_name = NULL;
740 ia->spreadsheet = NULL;
742 g_signal_connect (ia, "prepare", G_CALLBACK (on_prepare), ia);
743 g_signal_connect (ia, "cancel", G_CALLBACK (on_cancel), ia);
744 g_signal_connect (ia, "close", G_CALLBACK (on_close), ia);
746 ia->paste_button = gtk_button_new_with_label (_("Paste"));
747 ia->reset_button = gtk_button_new_with_label (_("Reset"));
749 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->paste_button);
751 g_signal_connect (ia->paste_button, "clicked", G_CALLBACK (on_paste), ia);
752 g_signal_connect (ia->reset_button, "clicked", G_CALLBACK (on_reset), ia);
754 gtk_assistant_add_action_widget (GTK_ASSISTANT(ia), ia->reset_button);
756 gtk_window_set_title (GTK_WINDOW (ia),
757 _("Importing Delimited Text Data"));
759 gtk_window_set_icon_name (GTK_WINDOW (ia), "pspp");
761 chooser_page_create (ia);
763 gtk_assistant_set_forward_page_func (GTK_ASSISTANT (ia), next_page_func, NULL, NULL);
765 gtk_window_maximize (GTK_WINDOW (ia));
769 /* Appends a page of the given TYPE, with PAGE as its content, to
770 the GtkAssistant encapsulated by IA. Returns the GtkWidget
771 that represents the page. */
773 add_page_to_assistant (PsppireImportAssistant *ia,
774 GtkWidget *page, GtkAssistantPageType type, const gchar *title)
776 GtkWidget *content = page;
778 gtk_assistant_append_page (GTK_ASSISTANT (ia), content);
779 gtk_assistant_set_page_type (GTK_ASSISTANT(ia), content, type);
780 gtk_assistant_set_page_title (GTK_ASSISTANT(ia), content, title);
781 gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), content, TRUE);
787 /* Called when one of the radio buttons is clicked. */
789 on_intro_amount_changed (PsppireImportAssistant *p)
791 gtk_widget_set_sensitive (p->n_cases_spin,
792 gtk_toggle_button_get_active (
793 GTK_TOGGLE_BUTTON (p->n_cases_button)));
795 gtk_widget_set_sensitive (p->percent_spin,
796 gtk_toggle_button_get_active (
797 GTK_TOGGLE_BUTTON (p->percent_button)));
803 on_treeview_selection_change (PsppireImportAssistant *ia)
805 GtkTreeSelection *selection =
806 gtk_tree_view_get_selection (GTK_TREE_VIEW (ia->first_line_tree_view));
807 GtkTreeModel *model = NULL;
809 if (gtk_tree_selection_get_selected (selection, &model, &iter))
812 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
813 gint *index = gtk_tree_path_get_indices (path);
817 gtk_tree_path_free (path);
819 gtk_widget_set_sensitive (ia->variable_names_cb, n > 0);
822 = psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file));
823 g_object_set (ia->delimiters_model, "first-line", n, NULL);
825 g_print ("%s:%d DT %p first line %d\n", __FILE__, __LINE__, ia->delimiters_model, n);
830 /* Initializes IA's first_line substructure. */
832 first_line_page_create (PsppireImportAssistant *ia)
834 g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
835 GtkWidget *w = get_widget_assert (ia->builder, "FirstLine");
837 g_object_set_data (G_OBJECT (w), "on-entering", on_treeview_selection_change);
839 add_page_to_assistant (ia, w,
840 GTK_ASSISTANT_PAGE_CONTENT, _("Select the First Line"));
842 GtkWidget *scrolled_window = get_widget_assert (ia->builder, "first-line-scroller");
844 if (ia->first_line_tree_view == NULL)
846 ia->first_line_tree_view = gtk_tree_view_new ();
848 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (ia->first_line_tree_view), TRUE);
850 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
851 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes (_("Line"), renderer,
854 gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
856 renderer = gtk_cell_renderer_text_new ();
857 column = gtk_tree_view_column_new_with_attributes (_("Text"), renderer, "text", 1, NULL);
858 gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
860 gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view);
862 g_signal_connect_swapped (ia->first_line_tree_view, "cursor-changed",
863 G_CALLBACK (on_treeview_selection_change), ia);
865 gtk_widget_show_all (scrolled_window);
867 ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
875 intro_on_enter (PsppireImportAssistant *ia)
877 GtkBuilder *builder = ia->builder;
878 GtkWidget *table = get_widget_assert (builder, "button-table");
883 ds_put_cstr (&s, _("This assistant will guide you through the process of "
884 "importing data into PSPP from a text file with one line "
885 "per case, in which fields are separated by tabs, "
886 "commas, or other delimiters.\n\n"));
890 if (ia->text_file->total_is_exact)
893 &s, ngettext ("The selected file contains %'lu line of text. ",
894 "The selected file contains %'lu lines of text. ",
895 ia->text_file->total_lines),
896 ia->text_file->total_lines);
898 else if (ia->text_file->total_lines > 0)
902 "The selected file contains approximately %'lu line of text. ",
903 "The selected file contains approximately %'lu lines of text. ",
904 ia->text_file->total_lines),
905 ia->text_file->total_lines);
908 "Only the first %zu line of the file will be shown for "
909 "preview purposes in the following screens. ",
910 "Only the first %zu lines of the file will be shown for "
911 "preview purposes in the following screens. ",
912 ia->text_file->line_cnt),
913 ia->text_file->line_cnt);
917 ds_put_cstr (&s, _("You may choose below how much of the file should "
918 "actually be imported."));
920 gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
924 GtkWidget *w = gtk_grid_get_child_at (GTK_GRID (table), 1, 1);
925 int old_value = w ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) : 1;
927 gtk_container_remove (GTK_CONTAINER (table), w);
929 w = gtk_grid_get_child_at (GTK_GRID (table), 1, 2);
931 gtk_container_remove (GTK_CONTAINER (table), w);
934 GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
936 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (ia->n_cases_spin));
937 gtk_adjustment_set_lower (adj, 1.0);
941 if (psppire_text_file_get_total_exact (ia->text_file))
943 gulong total_lines = psppire_text_file_get_n_lines (ia->text_file);
944 gtk_adjustment_set_upper (adj, total_lines);
945 gtk_adjustment_set_value (adj, old_value);
948 gtk_adjustment_set_upper (adj, DBL_MAX);
950 gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
955 GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
958 gtk_grid_attach (GTK_GRID (table), hbox_percent,
962 gtk_widget_show_all (table);
964 on_intro_amount_changed (ia);
967 /* Initializes IA's intro substructure. */
969 intro_page_create (PsppireImportAssistant *ia)
971 GtkBuilder *builder = ia->builder;
973 GtkWidget *w = get_widget_assert (builder, "Intro");
975 ia->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
978 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Select the Lines to Import"));
980 ia->all_cases_button = get_widget_assert (builder, "import-all-cases");
982 ia->n_cases_button = get_widget_assert (builder, "import-n-cases");
984 ia->percent_button = get_widget_assert (builder, "import-percent");
986 g_signal_connect_swapped (ia->all_cases_button, "toggled",
987 G_CALLBACK (on_intro_amount_changed), ia);
988 g_signal_connect_swapped (ia->n_cases_button, "toggled",
989 G_CALLBACK (on_intro_amount_changed), ia);
990 g_signal_connect_swapped (ia->percent_button, "toggled",
991 G_CALLBACK (on_intro_amount_changed), ia);
994 g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter);
995 g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page);
1000 psppire_import_assistant_new (GtkWindow *toplevel)
1002 return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_IMPORT_ASSISTANT,
1003 /* Some window managers (notably ratpoison)
1004 ignore the maximise command when a window is
1005 transient. This causes problems for this
1007 /* "transient-for", toplevel, */
1016 set_quote_list (GtkComboBox *cb)
1018 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
1021 const gchar *separator[3] = {"'\"", "\'", "\""};
1023 for (i = 0; i < 3; i++)
1025 const gchar *s = separator[i];
1027 /* Add a new row to the model */
1028 gtk_list_store_append (list, &iter);
1029 gtk_list_store_set (list, &iter,
1035 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
1036 g_object_unref (list);
1038 gtk_combo_box_set_entry_text_column (cb, 0);
1043 /* Chooses a name for each column on the separators page */
1045 choose_column_names (PsppireImportAssistant *ia)
1048 unsigned long int generated_name_count = 0;
1049 dict_clear (ia->dict);
1051 g_print ("%s:%d XXX %d\n", __FILE__, __LINE__, gtk_tree_model_get_n_columns (ia->delimiters_model));
1053 for (i = 0; i < gtk_tree_model_get_n_columns (ia->delimiters_model) - 1; ++i)
1055 const gchar *candidate_name = NULL;
1057 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
1059 candidate_name = psppire_delimited_text_get_header_title (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
1062 g_print ("%s:%d CN is %s\n", __FILE__, __LINE__, candidate_name);
1064 char *name = dict_make_unique_var_name (ia->dict,
1066 &generated_name_count);
1068 dict_create_var_assert (ia->dict, name, 0);
1075 /* Called when the user toggles one of the separators
1078 on_separator_toggle (GtkToggleButton *toggle UNUSED,
1079 PsppireImportAssistant *ia)
1082 GSList *delimiters = NULL;
1083 for (i = 0; i < SEPARATOR_CNT; i++)
1085 const struct separator *s = &separators[i];
1086 GtkWidget *button = get_widget_assert (ia->builder, s->name);
1087 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1089 delimiters = g_slist_prepend (delimiters, GINT_TO_POINTER (s->c));
1093 g_object_set (ia->delimiters_model, "delimiters", delimiters, NULL);
1095 revise_fields_preview (ia);
1099 /* Called when the user changes the entry field for custom
1102 on_separators_custom_entry_notify (GObject *gobject UNUSED,
1103 GParamSpec *arg1 UNUSED,
1104 PsppireImportAssistant *ia)
1106 revise_fields_preview (ia);
1109 /* Called when the user toggles the checkbox that enables custom
1112 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
1113 PsppireImportAssistant *ia)
1115 bool is_active = gtk_toggle_button_get_active (custom_cb);
1116 gtk_widget_set_sensitive (ia->custom_entry, is_active);
1117 revise_fields_preview (ia);
1120 /* Called when the user changes the selection in the combo box
1121 that selects a quote character. */
1123 on_quote_combo_change (GtkComboBox *combo, PsppireImportAssistant *ia)
1125 // revise_fields_preview (ia);
1128 /* Called when the user toggles the checkbox that enables
1131 on_quote_cb_toggle (GtkToggleButton *quote_cb, PsppireImportAssistant *ia)
1133 bool is_active = gtk_toggle_button_get_active (quote_cb);
1134 gtk_widget_set_sensitive (ia->quote_combo, is_active);
1135 revise_fields_preview (ia);
1138 /* Initializes IA's separators substructure. */
1140 separators_page_create (PsppireImportAssistant *ia)
1142 GtkBuilder *builder = ia->builder;
1146 GtkWidget *w = get_widget_assert (builder, "Separators");
1148 g_object_set_data (G_OBJECT (w), "on-entering", prepare_separators_page);
1149 g_object_set_data (G_OBJECT (w), "on-reset", prepare_separators_page);
1151 add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Choose Separators"));
1153 ia->custom_cb = get_widget_assert (builder, "custom-cb");
1154 ia->custom_entry = get_widget_assert (builder, "custom-entry");
1155 ia->quote_combo = get_widget_assert (builder, "quote-combo");
1156 ia->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo)));
1157 ia->quote_cb = get_widget_assert (builder, "quote-cb");
1159 set_quote_list (GTK_COMBO_BOX (ia->quote_combo));
1161 if (ia->fields_tree_view == NULL)
1163 GtkWidget *scroller = get_widget_assert (ia->builder, "fields-scroller");
1164 ia->fields_tree_view = gtk_tree_view_new ();
1165 gtk_container_add (GTK_CONTAINER (scroller), GTK_WIDGET (ia->fields_tree_view));
1166 gtk_widget_show_all (scroller);
1169 g_signal_connect (ia->quote_combo, "changed",
1170 G_CALLBACK (on_quote_combo_change), ia);
1171 g_signal_connect (ia->quote_cb, "toggled",
1172 G_CALLBACK (on_quote_cb_toggle), ia);
1173 g_signal_connect (ia->custom_entry, "notify::text",
1174 G_CALLBACK (on_separators_custom_entry_notify), ia);
1175 g_signal_connect (ia->custom_cb, "toggled",
1176 G_CALLBACK (on_separators_custom_cb_toggle), ia);
1177 for (i = 0; i < SEPARATOR_CNT; i++)
1178 g_signal_connect (get_widget_assert (builder, separators[i].name),
1179 "toggled", G_CALLBACK (on_separator_toggle), ia);
1188 static struct casereader_random_class my_casereader_class;
1190 static struct ccase *
1191 my_read (struct casereader *reader, void *aux, casenumber idx)
1193 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
1194 GtkTreeModel *tm = GTK_TREE_MODEL (ia->delimiters_model);
1196 GtkTreePath *tp = gtk_tree_path_new_from_indices (idx, -1);
1198 const struct caseproto *proto = casereader_get_proto (reader);
1201 struct ccase *c = NULL;
1202 if (gtk_tree_model_get_iter (tm, &iter, tp))
1204 c = case_create (proto);
1206 for (i = 0 ; i < caseproto_get_n_widths (proto); ++i)
1209 gtk_tree_model_get_value (tm, &iter, i + 1, &value);
1211 const struct variable *var = dict_get_var (ia->dict, i);
1213 const gchar *ss = g_value_get_string (&value);
1215 union value *v = case_data_rw (c, var);
1216 char *xx = data_in (ss_cstr (ss),
1218 var_get_write_format (var)->type,
1219 v, var_get_width (var), "UTF-8");
1222 /* g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
1224 g_value_unset (&value);
1228 gtk_tree_path_free (tp);
1234 my_destroy (struct casereader *reader, void *aux)
1236 g_print ("%s:%d %p\n", __FILE__, __LINE__, reader);
1240 my_advance (struct casereader *reader, void *aux, casenumber cnt)
1242 g_print ("%s:%d\n", __FILE__, __LINE__);
1246 foo (struct dictionary *dict, void *aux)
1248 PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
1249 g_print ("%s:%d\n", __FILE__, __LINE__);
1251 struct caseproto *proto = caseproto_create ();
1254 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1256 const struct variable *var = dict_get_var (ia->dict, i);
1257 proto = caseproto_add_width (proto, var_get_width (var));
1261 gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
1263 struct casereader *reader =
1264 casereader_create_random (proto, n_rows, &my_casereader_class, ia);
1267 PsppireDataStore *store = NULL;
1269 g_object_get (ia->data_sheet, "data-model", &store, NULL);
1271 psppire_data_store_set_reader (store, reader);
1274 /* Called just before the formats page of the assistant is
1277 prepare_formats_page (PsppireImportAssistant *ia)
1279 PsppireDict *dict = psppire_dict_new_from_dict (ia->dict);
1280 g_object_set (ia->var_sheet, "data-model", dict, NULL);
1282 my_casereader_class.read = my_read;
1283 my_casereader_class.destroy = my_destroy;
1284 my_casereader_class.advance = my_advance;
1286 struct caseproto *proto = caseproto_create ();
1289 struct fmt_guesser **fg = xcalloc (sizeof *fg, dict_get_var_cnt (ia->dict));
1290 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1292 fg[i] = fmt_guesser_create ();
1295 gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
1299 for (ok = gtk_tree_model_get_iter_first (ia->delimiters_model, &iter);
1301 ok = gtk_tree_model_iter_next (ia->delimiters_model, &iter))
1303 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1306 gtk_tree_model_get (ia->delimiters_model, &iter, i+1, &s, -1);
1307 fmt_guesser_add (fg[i], ss_cstr (s));
1312 for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
1315 fmt_guesser_guess (fg[i], &fs);
1317 fmt_fix (&fs, FMT_FOR_INPUT);
1319 struct variable *var = dict_get_var (ia->dict, i);
1321 int width = fmt_var_width (&fs);
1323 var_set_width_and_formats (var, width,
1326 proto = caseproto_add_width (proto, width);
1327 fmt_guesser_destroy (fg[i]);
1332 // dict_set_change_callback (ia->dict, foo, ia);
1334 struct casereader *reader =
1335 casereader_create_random (proto, n_rows, &my_casereader_class, ia);
1337 PsppireDataStore *store = psppire_data_store_new (dict);
1338 psppire_data_store_set_reader (store, reader);
1340 g_object_set (ia->data_sheet, "data-model", store, NULL);
1344 g_object_get (get_widget_assert (ia->builder, "vpaned1"),
1345 "max-position", &pmax, NULL);
1348 g_object_set (get_widget_assert (ia->builder, "vpaned1"),
1349 "position", pmax / 2, NULL);
1351 gtk_widget_show (ia->paste_button);
1355 formats_page_create (PsppireImportAssistant *ia)
1357 GtkBuilder *builder = ia->builder;
1359 GtkWidget *w = get_widget_assert (builder, "Formats");
1360 g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
1361 g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
1363 GtkWidget *vars_scroller = get_widget_assert (builder, "vars-scroller");
1364 if (ia->var_sheet == NULL)
1366 ia->var_sheet = psppire_variable_sheet_new ();
1368 gtk_container_add (GTK_CONTAINER (vars_scroller), ia->var_sheet);
1370 ia->dict = dict_create (get_default_encoding ());
1372 gtk_widget_show_all (vars_scroller);
1374 GtkWidget *data_scroller = get_widget_assert (builder, "data-scroller");
1375 if (ia->data_sheet == NULL)
1377 ia->data_sheet = psppire_data_sheet_new ();
1379 gtk_container_add (GTK_CONTAINER (data_scroller), ia->data_sheet);
1381 gtk_widget_show_all (data_scroller);
1384 add_page_to_assistant (ia, w,
1385 GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
1392 separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1396 ds_put_cstr (s, " /DELIMITERS=\"");
1398 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (get_widget_assert (ia->builder, "tab"))))
1399 ds_put_cstr (s, "\\t");
1400 for (i = 0; i < SEPARATOR_CNT; i++)
1402 const struct separator *seps = &separators[i];
1403 GtkWidget *button = get_widget_assert (ia->builder, seps->name);
1404 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1406 if (seps->c == '\t')
1409 ds_put_byte (s, seps->c);
1412 ds_put_cstr (s, "\"\n");
1413 if (!ds_is_empty (&ia->quotes))
1414 syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
1418 formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1423 g_return_if_fail (ia->dict);
1425 ds_put_cstr (s, " /VARIABLES=\n");
1427 var_cnt = dict_get_var_cnt (ia->dict);
1428 for (i = 0; i < var_cnt; i++)
1430 struct variable *var = dict_get_var (ia->dict, i);
1431 char format_string[FMT_STRING_LEN_MAX + 1];
1432 fmt_to_string (var_get_print_format (var), format_string);
1433 ds_put_format (s, " %s %s%s\n",
1434 var_get_name (var), format_string,
1435 i == var_cnt - 1 ? "." : "");
1440 first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1442 gint first_case = 0;
1443 g_object_get (ia->delimiters_model, "first-line", &first_case, NULL);
1446 ds_put_format (s, " /FIRSTCASE=%d\n", first_case + 1);
1450 intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
1452 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
1453 ds_put_format (s, "N OF CASES %d.\n",
1454 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)));
1455 else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
1456 ds_put_format (s, "SAMPLE %.4g.\n",
1457 gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
1461 /* Emits PSPP syntax to S that applies the dictionary attributes
1462 (such as missing values and value labels) of the variables in
1465 apply_dict (const struct dictionary *dict, struct string *s)
1467 size_t var_cnt = dict_get_var_cnt (dict);
1470 for (i = 0; i < var_cnt; i++)
1472 struct variable *var = dict_get_var (dict, i);
1473 const char *name = var_get_name (var);
1474 enum val_type type = var_get_type (var);
1475 int width = var_get_width (var);
1476 enum measure measure = var_get_measure (var);
1477 enum var_role role = var_get_role (var);
1478 enum alignment alignment = var_get_alignment (var);
1479 const struct fmt_spec *format = var_get_print_format (var);
1481 if (var_has_missing_values (var))
1483 const struct missing_values *mv = var_get_missing_values (var);
1486 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
1487 for (j = 0; j < mv_n_values (mv); j++)
1490 ds_put_cstr (s, ", ");
1491 syntax_gen_value (s, mv_get_value (mv, j), width, format);
1494 if (mv_has_range (mv))
1497 if (mv_has_value (mv))
1498 ds_put_cstr (s, ", ");
1499 mv_get_range (mv, &low, &high);
1500 syntax_gen_num_range (s, low, high, format);
1502 ds_put_cstr (s, ").\n");
1504 if (var_has_value_labels (var))
1506 const struct val_labs *vls = var_get_value_labels (var);
1507 const struct val_lab **labels = val_labs_sorted (vls);
1508 size_t n_labels = val_labs_count (vls);
1511 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
1512 for (i = 0; i < n_labels; i++)
1514 const struct val_lab *vl = labels[i];
1515 ds_put_cstr (s, "\n ");
1516 syntax_gen_value (s, &vl->value, width, format);
1517 ds_put_byte (s, ' ');
1518 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
1521 ds_put_cstr (s, ".\n");
1523 if (var_has_label (var))
1524 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
1525 name, var_get_label (var));
1526 if (measure != var_default_measure (type))
1527 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
1528 name, measure_to_syntax (measure));
1529 if (role != ROLE_INPUT)
1530 syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
1531 var_role_to_syntax (role), name);
1532 if (alignment != var_default_alignment (type))
1533 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
1534 name, alignment_to_syntax (alignment));
1535 if (var_get_display_width (var) != var_default_display_width (width))
1536 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
1537 name, var_get_display_width (var));
1544 sheet_spec_gen_syntax (PsppireImportAssistant *ia)
1546 GtkBuilder *builder = ia->builder;
1547 GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
1548 GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
1549 GtkWidget *rnc = get_widget_assert (builder, "readnames-checkbox");
1550 const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
1551 int sheet_index = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (sheet_entry));
1552 gboolean read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rnc));
1554 struct string s = DS_EMPTY_INITIALIZER;
1557 g_object_get (ia->text_file, "file-name", &filename, NULL);
1558 syntax_gen_pspp (&s,
1562 "\n /SHEET=index %d"
1563 "\n /READNAMES=%ss",
1564 (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
1567 read_names ? "ON" : "OFF");
1569 if (range && 0 != strcmp ("", range))
1571 syntax_gen_pspp (&s,
1572 "\n /CELLRANGE=RANGE %sq", range);
1576 syntax_gen_pspp (&s,
1577 "\n /CELLRANGE=FULL");
1581 syntax_gen_pspp (&s, ".");
1584 return ds_cstr (&s);
1589 psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
1591 struct string s = DS_EMPTY_INITIALIZER;
1593 if (!ia->spreadsheet)
1595 gchar *file_name = NULL;
1596 gchar *encoding = NULL;
1597 g_object_get (ia->text_file,
1598 "file-name", &file_name,
1599 "encoding", &encoding,
1602 if (file_name == NULL)
1605 syntax_gen_pspp (&s,
1610 if (encoding && strcmp (encoding, "Auto"))
1611 syntax_gen_pspp (&s, " /ENCODING=%sq\n", encoding);
1614 " /ARRANGEMENT=DELIMITED\n"
1615 " /DELCASE=LINE\n");
1617 first_line_append_syntax (ia, &s);
1618 separators_append_syntax (ia, &s);
1620 formats_append_syntax (ia, &s);
1621 apply_dict (ia->dict, &s);
1622 intro_append_syntax (ia, &s);
1626 return sheet_spec_gen_syntax (ia);
1629 return ds_cstr (&s);