-
- line_reader_close (reader);
-
- return true;
-}
-
-/* Frees IA's file substructure. */
-static void
-destroy_file (struct import_assistant *ia)
-{
- struct file *f = &ia->file;
- size_t i;
-
- for (i = 0; i < f->line_cnt; i++)
- ds_destroy (&f->lines[i]);
- free (f->lines);
- g_free (f->file_name);
- g_free (f->encoding);
-}
-
-/* Obtains the file to read from the user. If successful, returns the name of
- the file and stores the user's chosen encoding for the file into *ENCODINGP.
- The caller must free each of these strings with g_free().
-
- On failure, stores a null pointer and stores NULL in *ENCODINGP.
-
- PARENT_WINDOW must be the window to use as the file chooser window's
- parent. */
-static char *
-choose_file (GtkWindow *parent_window, gchar **encodingp)
-{
- char *file_name;
- GtkFileFilter *filter = NULL;
-
- GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Import Delimited Text Data"),
- parent_window,
- GTK_FILE_CHOOSER_ACTION_OPEN,
- GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
- GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
- NULL);
-
- g_object_set (dialog, "local-only", FALSE, NULL);
-
- filter = gtk_file_filter_new ();
- gtk_file_filter_set_name (filter, _("Text files"));
- gtk_file_filter_add_mime_type (filter, "text/*");
- gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-
- filter = gtk_file_filter_new ();
- gtk_file_filter_set_name (filter, _("Text (*.txt) Files"));
- gtk_file_filter_add_pattern (filter, "*.txt");
- gtk_file_filter_add_pattern (filter, "*.TXT");
- gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-
- filter = gtk_file_filter_new ();
- gtk_file_filter_set_name (filter, _("Plain Text (ASCII) Files"));
- gtk_file_filter_add_mime_type (filter, "text/plain");
- gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-
- filter = gtk_file_filter_new ();
- gtk_file_filter_set_name (filter, _("Comma Separated Value Files"));
- gtk_file_filter_add_mime_type (filter, "text/csv");
- gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-
- /* I've never encountered one of these, but it's listed here:
- http://www.iana.org/assignments/media-types/text/tab-separated-values */
- filter = gtk_file_filter_new ();
- gtk_file_filter_set_name (filter, _("Tab Separated Value Files"));
- gtk_file_filter_add_mime_type (filter, "text/tab-separated-values");
- gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-
- gtk_file_chooser_set_extra_widget (
- GTK_FILE_CHOOSER (dialog), psppire_encoding_selector_new ("Auto", true));
-
- filter = gtk_file_filter_new ();
- gtk_file_filter_set_name (filter, _("All Files"));
- gtk_file_filter_add_pattern (filter, "*");
- gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-
- switch (gtk_dialog_run (GTK_DIALOG (dialog)))
- {
- case GTK_RESPONSE_ACCEPT:
- file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
- *encodingp = psppire_encoding_selector_get_encoding (
- gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
- break;
- default:
- file_name = NULL;
- *encodingp = NULL;
- break;
- }
- gtk_widget_destroy (dialog);
-
- return file_name;
-}
-\f
-/* Assistant. */
-
-static void close_assistant (struct import_assistant *, int response);
-static void on_prepare (GtkAssistant *assistant, GtkWidget *page,
- struct import_assistant *);
-static void on_cancel (GtkAssistant *assistant, struct import_assistant *);
-static void on_close (GtkAssistant *assistant, struct import_assistant *);
-static void on_paste (GtkButton *button, struct import_assistant *);
-static void on_reset (GtkButton *button, struct import_assistant *);
-static void close_assistant (struct import_assistant *, int response);
-
-/* Initializes IA's asst substructure. PARENT_WINDOW must be the
- window to use as the assistant window's parent. */
-static void
-init_assistant (struct import_assistant *ia, GtkWindow *parent_window)
-{
- struct assistant *a = &ia->asst;
-
- a->builder = builder_new ("text-data-import.ui");
- a->assistant = GTK_ASSISTANT (gtk_assistant_new ());
- g_signal_connect (a->assistant, "prepare", G_CALLBACK (on_prepare), ia);
- g_signal_connect (a->assistant, "cancel", G_CALLBACK (on_cancel), ia);
- g_signal_connect (a->assistant, "close", G_CALLBACK (on_close), ia);
- a->paste_button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
- gtk_assistant_add_action_widget (a->assistant, a->paste_button);
- g_signal_connect (a->paste_button, "clicked", G_CALLBACK (on_paste), ia);
- a->reset_button = gtk_button_new_from_stock ("pspp-stock-reset");
- gtk_assistant_add_action_widget (a->assistant, a->reset_button);
- g_signal_connect (a->reset_button, "clicked", G_CALLBACK (on_reset), ia);
- gtk_window_set_title (GTK_WINDOW (a->assistant),
- _("Importing Delimited Text Data"));
- gtk_window_set_transient_for (GTK_WINDOW (a->assistant), parent_window);
- gtk_window_set_icon_name (GTK_WINDOW (a->assistant), "pspp");
-
- a->prop_renderer = gtk_cell_renderer_text_new ();
- g_object_ref_sink (a->prop_renderer);
- a->fixed_renderer = gtk_cell_renderer_text_new ();
- g_object_ref_sink (a->fixed_renderer);
- g_object_set (G_OBJECT (a->fixed_renderer),
- "family", "Monospace",
- (void *) NULL);
-}
-
-/* Frees IA's asst substructure. */
-static void
-destroy_assistant (struct import_assistant *ia)
-{
- struct assistant *a = &ia->asst;
-
- g_object_unref (a->prop_renderer);
- g_object_unref (a->fixed_renderer);
- g_object_unref (a->builder);
-}
-
-/* Appends a page of the given TYPE, with PAGE as its content, to
- the GtkAssistant encapsulated by IA. Returns the GtkWidget
- that represents the page. */
-static GtkWidget *
-add_page_to_assistant (struct import_assistant *ia,
- GtkWidget *page, GtkAssistantPageType type)
-{
- const char *title;
- char *title_copy;
- GtkWidget *content;
-
- title = gtk_window_get_title (GTK_WINDOW (page));
- title_copy = xstrdup (title ? title : "");
-
- content = gtk_bin_get_child (GTK_BIN (page));
- assert (content);
- g_object_ref (content);
- gtk_container_remove (GTK_CONTAINER (page), content);
-
- gtk_widget_destroy (page);
-
- gtk_assistant_append_page (ia->asst.assistant, content);
- gtk_assistant_set_page_type (ia->asst.assistant, content, type);
- gtk_assistant_set_page_title (ia->asst.assistant, content, title_copy);
- gtk_assistant_set_page_complete (ia->asst.assistant, content, true);
-
- free (title_copy);
-
- return content;
-}
-
-/* Called just before PAGE is displayed as the current page of
- ASSISTANT, this updates IA content according to the new
- page. */
-static void
-on_prepare (GtkAssistant *assistant, GtkWidget *page,
- struct import_assistant *ia)
-{
-
- if (gtk_assistant_get_page_type (assistant, page)
- == GTK_ASSISTANT_PAGE_CONFIRM)
- gtk_widget_grab_focus (assistant->apply);
- else
- gtk_widget_grab_focus (assistant->forward);
-
- if (page == ia->separators.page)
- prepare_separators_page (ia);
- else if (page == ia->formats.page)
- prepare_formats_page (ia);
-
- gtk_widget_show (ia->asst.reset_button);
- if (page == ia->formats.page)
- gtk_widget_show (ia->asst.paste_button);
- else
- gtk_widget_hide (ia->asst.paste_button);
-}
-
-/* Called when the Cancel button in the assistant is clicked. */
-static void
-on_cancel (GtkAssistant *assistant, struct import_assistant *ia)
-{
- close_assistant (ia, GTK_RESPONSE_CANCEL);
-}
-
-/* Called when the Apply button on the last page of the assistant
- is clicked. */
-static void
-on_close (GtkAssistant *assistant, struct import_assistant *ia)
-{
- close_assistant (ia, GTK_RESPONSE_APPLY);
-}
-
-/* Called when the Paste button on the last page of the assistant
- is clicked. */
-static void
-on_paste (GtkButton *button, struct import_assistant *ia)
-{
- close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
-}
-
-/* Called when the Reset button is clicked. */
-static void
-on_reset (GtkButton *button, struct import_assistant *ia)
-{
- gint page_num = gtk_assistant_get_current_page (ia->asst.assistant);
- GtkWidget *page = gtk_assistant_get_nth_page (ia->asst.assistant, page_num);
-
- if (page == ia->intro.page)
- reset_intro_page (ia);
- else if (page == ia->first_line.page)
- reset_first_line_page (ia);
- else if (page == ia->separators.page)
- reset_separators_page (ia);
- else if (page == ia->formats.page)
- reset_formats_page (ia);
-}
-
-/* Causes the assistant to close, returning RESPONSE for
- interpretation by text_data_import_assistant. */
-static void
-close_assistant (struct import_assistant *ia, int response)
-{
- ia->asst.response = response;
- g_main_loop_quit (ia->asst.main_loop);
- gtk_widget_hide (GTK_WIDGET (ia->asst.assistant));
-}
-\f
-/* The "intro" page of the assistant. */
-
-static void on_intro_amount_changed (struct import_assistant *);
-
-/* Initializes IA's intro substructure. */
-static void
-init_intro_page (struct import_assistant *ia)
-{
- GtkBuilder *builder = ia->asst.builder;
- struct intro_page *p = &ia->intro;
- struct string s;
- GtkWidget *hbox_n_cases ;
- GtkWidget *hbox_percent ;
- GtkWidget *table ;
-
-
- p->n_cases_spin = gtk_spin_button_new_with_range (0, INT_MAX, 100);
-
- hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &p->n_cases_spin);
-
- table = get_widget_assert (builder, "button-table");
-
- gtk_table_attach_defaults (GTK_TABLE (table), hbox_n_cases,
- 1, 2,
- 1, 2);
-
- p->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
-
- hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"), &p->percent_spin);
-
- gtk_table_attach_defaults (GTK_TABLE (table), hbox_percent,
- 1, 2,
- 2, 3);
-
- p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Intro"),
- GTK_ASSISTANT_PAGE_INTRO);
-
- p->all_cases_button = get_widget_assert (builder, "import-all-cases");
-
- p->n_cases_button = get_widget_assert (builder, "import-n-cases");
-
- p->percent_button = get_widget_assert (builder, "import-percent");
-
- g_signal_connect_swapped (p->all_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect_swapped (p->n_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect_swapped (p->percent_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
-
- on_intro_amount_changed (ia);
-
- ds_init_empty (&s);
- ds_put_cstr (&s, _("This assistant will guide you through the process of "
- "importing data into PSPP from a text file with one line "
- "per case, in which fields are separated by tabs, "
- "commas, or other delimiters.\n\n"));
- if (ia->file.total_is_exact)
- ds_put_format (
- &s, ngettext ("The selected file contains %zu line of text. ",
- "The selected file contains %zu lines of text. ",
- ia->file.line_cnt),
- ia->file.line_cnt);
- else if (ia->file.total_lines > 0)
- {
- ds_put_format (
- &s, ngettext (
- "The selected file contains approximately %lu line of text. ",
- "The selected file contains approximately %lu lines of text. ",
- ia->file.total_lines),
- ia->file.total_lines);
- ds_put_format (
- &s, ngettext (
- "Only the first %zu line of the file will be shown for "
- "preview purposes in the following screens. ",
- "Only the first %zu lines of the file will be shown for "
- "preview purposes in the following screens. ",
- ia->file.line_cnt),
- ia->file.line_cnt);
- }
- ds_put_cstr (&s, _("You may choose below how much of the file should "
- "actually be imported."));
- gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
- ds_cstr (&s));
- ds_destroy (&s);
-}
-
-/* Resets IA's intro page to its initial state. */
-static void
-reset_intro_page (struct import_assistant *ia)
-{
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->intro.all_cases_button),
- true);
-}
-
-/* Called when one of the radio buttons is clicked. */
-static void
-on_intro_amount_changed (struct import_assistant *ia)
-{
- struct intro_page *p = &ia->intro;
-
- gtk_widget_set_sensitive (p->n_cases_spin,
- gtk_toggle_button_get_active (
- GTK_TOGGLE_BUTTON (p->n_cases_button)));
-
- gtk_widget_set_sensitive (p->percent_spin,
- gtk_toggle_button_get_active (
- GTK_TOGGLE_BUTTON (p->percent_button)));
-}
-\f
-/* The "first line" page of the assistant. */
-
-static PsppSheetView *create_lines_tree_view (GtkContainer *parent_window,
- struct import_assistant *);
-static void on_first_line_change (PsppSheetSelection *,
- struct import_assistant *);
-static void on_variable_names_cb_toggle (GtkToggleButton *,
- struct import_assistant *);
-static void set_first_line (struct import_assistant *);
-static void get_first_line (struct import_assistant *);
-
-/* Initializes IA's first_line substructure. */
-static void
-init_first_line_page (struct import_assistant *ia)
-{
- struct first_line_page *p = &ia->first_line;
- GtkBuilder *builder = ia->asst.builder;
-
- p->page = add_page_to_assistant (ia, get_widget_assert (builder, "FirstLine"),
- GTK_ASSISTANT_PAGE_CONTENT);
- gtk_widget_destroy (get_widget_assert (builder, "first-line"));
- p->tree_view = create_lines_tree_view (
- GTK_CONTAINER (get_widget_assert (builder, "first-line-scroller")), ia);
- p->variable_names_cb = get_widget_assert (builder, "variable-names");
- pspp_sheet_selection_set_mode (
- pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (p->tree_view)),
- PSPP_SHEET_SELECTION_BROWSE);
- pspp_sheet_view_set_rubber_banding (PSPP_SHEET_VIEW (p->tree_view), TRUE);
- set_first_line (ia);
- g_signal_connect (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (p->tree_view)),
- "changed", G_CALLBACK (on_first_line_change), ia);
- g_signal_connect (p->variable_names_cb, "toggled",
- G_CALLBACK (on_variable_names_cb_toggle), ia);
-}
-
-/* Resets the first_line page to its initial content. */
-static void
-reset_first_line_page (struct import_assistant *ia)
-{
- ia->first_line.skip_lines = 0;
- ia->first_line.variable_names = false;
- set_first_line (ia);
-}
-
-static void
-render_line (PsppSheetViewColumn *tree_column,
- GtkCellRenderer *cell,
- GtkTreeModel *tree_model,
- GtkTreeIter *iter,
- gpointer data)
-{
- gint row = empty_list_store_iter_to_row (iter);
- struct string *lines;
-
- lines = g_object_get_data (G_OBJECT (tree_model), "lines");
- g_return_if_fail (lines != NULL);
-
- g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
-}
-
-
-/* Creates and returns a tree view that contains each of the
- lines in IA's file as a row. */
-static PsppSheetView *
-create_lines_tree_view (GtkContainer *parent, struct import_assistant *ia)
-{
- PsppSheetView *tree_view;
- PsppSheetViewColumn *column;
- size_t max_line_length;
- gint content_width, header_width;
- size_t i;
- const gchar *title = _("Text");
-
- make_tree_view (ia, 0, &tree_view);
-
- column = pspp_sheet_view_column_new_with_attributes (
- title, ia->asst.fixed_renderer, (void *) NULL);
- pspp_sheet_view_column_set_cell_data_func (column, ia->asst.fixed_renderer,
- render_line, NULL, NULL);
- pspp_sheet_view_column_set_resizable (column, TRUE);
-
- max_line_length = 0;
- for (i = 0; i < ia->file.line_cnt; i++)
- {
- size_t w = ds_length (&ia->file.lines[i]);
- max_line_length = MAX (max_line_length, w);
- }
-
- content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
- max_line_length);
- header_width = get_string_width (tree_view, ia->asst.prop_renderer, title);
- pspp_sheet_view_column_set_fixed_width (column, MAX (content_width,
- header_width));
- pspp_sheet_view_append_column (tree_view, column);
-
- gtk_container_add (parent, GTK_WIDGET (tree_view));
- gtk_widget_show (GTK_WIDGET (tree_view));
-
- return tree_view;
-}
-
-/* Called when the line selected in the first_line tree view
- changes. */
-static void
-on_first_line_change (PsppSheetSelection *selection UNUSED,
- struct import_assistant *ia)
-{
- get_first_line (ia);