-/* Chooses a name for each column on the separators page */
-static void
-choose_column_names (PsppireImportAssistant *ia)
-{
- int i;
- unsigned long int generated_name_count = 0;
- dict_clear (ia->dict);
-
- for (i = 0;
- i < gtk_tree_model_get_n_columns (GTK_TREE_MODEL (ia->delimiters_model)) - 1;
- ++i)
- {
- const gchar *candidate_name = NULL;
-
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
- {
- candidate_name = psppire_delimited_text_get_header_title (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
- }
-
- char *name = dict_make_unique_var_name (ia->dict,
- candidate_name,
- &generated_name_count);
-
- dict_create_var_assert (ia->dict, name, 0);
- free (name);
- }
-}
-
-/* Called when the user toggles one of the separators
- checkboxes. */
-static void
-on_separator_toggle (GtkToggleButton *toggle UNUSED,
- PsppireImportAssistant *ia)
-{
- int i;
- GSList *delimiters = NULL;
- for (i = 0; i < SEPARATOR_CNT; i++)
- {
- const struct separator *s = &separators[i];
- GtkWidget *button = get_widget_assert (ia->builder, s->name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
- {
- delimiters = g_slist_prepend (delimiters, GINT_TO_POINTER (s->c));
- }
- }
-
- g_object_set (ia->delimiters_model, "delimiters", delimiters, NULL);
-
- revise_fields_preview (ia);
-}
-
-
-/* Called when the user changes the entry field for custom
- separators. */
-static void
-on_separators_custom_entry_notify (GObject *gobject UNUSED,
- GParamSpec *arg1 UNUSED,
- PsppireImportAssistant *ia)
-{
- revise_fields_preview (ia);
-}
-
-/* Called when the user toggles the checkbox that enables custom
- separators. */
-static void
-on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
- PsppireImportAssistant *ia)
-{
- bool is_active = gtk_toggle_button_get_active (custom_cb);
- gtk_widget_set_sensitive (ia->custom_entry, is_active);
- revise_fields_preview (ia);
-}
-
-/* Called when the user changes the selection in the combo box
- that selects a quote character. */
-static void
-on_quote_combo_change (GtkComboBox *combo, PsppireImportAssistant *ia)
-{
- // revise_fields_preview (ia);
-}
-
-/* Called when the user toggles the checkbox that enables
- quoting. */
-static void
-on_quote_cb_toggle (GtkToggleButton *quote_cb, PsppireImportAssistant *ia)
-{
- bool is_active = gtk_toggle_button_get_active (quote_cb);
- gtk_widget_set_sensitive (ia->quote_combo, is_active);
- revise_fields_preview (ia);
-}
-
-/* Initializes IA's separators substructure. */
-static void
-separators_page_create (PsppireImportAssistant *ia)
-{
- GtkBuilder *builder = ia->builder;
-
- size_t i;
-
- GtkWidget *w = get_widget_assert (builder, "Separators");
-
- g_object_set_data (G_OBJECT (w), "on-entering", prepare_separators_page);
- g_object_set_data (G_OBJECT (w), "on-reset", prepare_separators_page);
-
- add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Choose Separators"));
-
- ia->custom_cb = get_widget_assert (builder, "custom-cb");
- ia->custom_entry = get_widget_assert (builder, "custom-entry");
- ia->quote_combo = get_widget_assert (builder, "quote-combo");
- ia->quote_cb = get_widget_assert (builder, "quote-cb");
-
- gtk_combo_box_set_active (GTK_COMBO_BOX (ia->quote_combo), 0);
-
- if (ia->fields_tree_view == NULL)
- {
- GtkWidget *scroller = get_widget_assert (ia->builder, "fields-scroller");
- ia->fields_tree_view = gtk_tree_view_new ();
- g_object_set (ia->fields_tree_view, "enable-search", FALSE, NULL);
- gtk_container_add (GTK_CONTAINER (scroller), GTK_WIDGET (ia->fields_tree_view));
- gtk_widget_show_all (scroller);
- }
-
- g_signal_connect (ia->quote_combo, "changed",
- G_CALLBACK (on_quote_combo_change), ia);
- g_signal_connect (ia->quote_cb, "toggled",
- G_CALLBACK (on_quote_cb_toggle), ia);
- g_signal_connect (ia->custom_entry, "notify::text",
- G_CALLBACK (on_separators_custom_entry_notify), ia);
- g_signal_connect (ia->custom_cb, "toggled",
- G_CALLBACK (on_separators_custom_cb_toggle), ia);
- for (i = 0; i < SEPARATOR_CNT; i++)
- g_signal_connect (get_widget_assert (builder, separators[i].name),
- "toggled", G_CALLBACK (on_separator_toggle), ia);
-
-}
-
-
-
-\f
-
-
-static struct casereader_random_class my_casereader_class;
-
-static struct ccase *
-my_read (struct casereader *reader, void *aux, casenumber idx)
-{
- PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
- GtkTreeModel *tm = GTK_TREE_MODEL (ia->delimiters_model);
-
- GtkTreePath *tp = gtk_tree_path_new_from_indices (idx, -1);
-
- const struct caseproto *proto = casereader_get_proto (reader);
-
- GtkTreeIter iter;
- struct ccase *c = NULL;
- if (gtk_tree_model_get_iter (tm, &iter, tp))
- {
- c = case_create (proto);
- int i;
- for (i = 0 ; i < caseproto_get_n_widths (proto); ++i)
- {
- GValue value = {0};
- gtk_tree_model_get_value (tm, &iter, i + 1, &value);
-
- const struct variable *var = dict_get_var (ia->casereader_dict, i);
-
- const gchar *ss = g_value_get_string (&value);
- if (ss)
- {
- union value *v = case_data_rw (c, var);
- /* In this reader we derive the union value from the
- string in the tree_model. We retrieve the width and format
- from a dictionary which is stored directly after
- the reader creation. Changes in ia->dict in the
- variable window are not reflected here and therefore
- this is always compatible with the width in the
- caseproto. See bug #58298 */
- char *xx = data_in (ss_cstr (ss),
- "UTF-8",
- var_get_write_format (var)->type,
- v, var_get_width (var), "UTF-8");
-
- /* if (xx) */
- /* g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
- free (xx);
- }
- g_value_unset (&value);
- }
- }
-
- gtk_tree_path_free (tp);
-
- return c;
-}
-
-static void
-my_destroy (struct casereader *reader, void *aux)
-{
- g_print ("%s:%d %p\n", __FILE__, __LINE__, reader);
-}
-
-static void
-my_advance (struct casereader *reader, void *aux, casenumber cnt)
-{
- g_print ("%s:%d\n", __FILE__, __LINE__);
-}
-
-static struct casereader *
-textfile_create_reader (PsppireImportAssistant *ia)
-{
- int n_vars = dict_get_var_cnt (ia->dict);
-
- int i;
-
- struct fmt_guesser **fg = xcalloc (sizeof *fg, n_vars);
- for (i = 0 ; i < n_vars; ++i)
- {
- fg[i] = fmt_guesser_create ();
- }
-
- gint n_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (ia->delimiters_model), NULL);
-
- GtkTreeIter iter;
- gboolean ok;
- for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ia->delimiters_model), &iter);
- ok;
- ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->delimiters_model), &iter))
- {
- for (i = 0 ; i < n_vars; ++i)
- {
- gchar *s = NULL;
- gtk_tree_model_get (GTK_TREE_MODEL (ia->delimiters_model), &iter, i+1, &s, -1);
- if (s)
- fmt_guesser_add (fg[i], ss_cstr (s));
- free (s);
- }
- }
-
- struct caseproto *proto = caseproto_create ();
- for (i = 0 ; i < n_vars; ++i)
- {
- struct fmt_spec fs;
- fmt_guesser_guess (fg[i], &fs);
-
- fmt_fix (&fs, FMT_FOR_INPUT);
-
- struct variable *var = dict_get_var (ia->dict, i);
-
- int width = fmt_var_width (&fs);
-
- var_set_width_and_formats (var, width,
- &fs, &fs);
-
- proto = caseproto_add_width (proto, width);
- fmt_guesser_destroy (fg[i]);
- }
-
- free (fg);
-
- struct casereader *cr = casereader_create_random (proto, n_rows, &my_casereader_class, ia);
- /* Store the dictionary at this point when the casereader is created.
- my_read depends on the dictionary to interpret the strings in the treeview.
- This guarantees that the union value is produced according to the
- caseproto in the reader. */
- ia->casereader_dict = dict_clone (ia->dict);
- caseproto_unref (proto);
- return cr;
-}
-
-/* When during import the variable type is changed, the reader is reinitialized
- based on the new dictionary with a fresh caseprototype. The default behaviour
- when a variable type is changed and the column is resized is that the union
- value is interpreted with new variable type and an overlay for that column
- is generated. Here we reinit to the original reader based on strings.
- As a result you can switch from string to numeric to string without loosing
- the string information. */
-static void
-ia_variable_changed_cb (GObject *obj, gint var_num, guint what,
- const struct variable *oldvar, gpointer data)
-{
- PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (data);
-
- struct caseproto *proto = caseproto_create();
- for (int i = 0; i < dict_get_var_cnt (ia->dict); i++)
- {
- const struct variable *var = dict_get_var (ia->dict, i);
- int width = var_get_width (var);
- proto = caseproto_add_width (proto, width);
- }
-
- gint n_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (ia->delimiters_model), NULL);
-
- PsppireDataStore *store = NULL;
- g_object_get (ia->data_sheet, "data-model", &store, NULL);
-
- struct casereader *cr = casereader_create_random (proto, n_rows,
- &my_casereader_class, ia);
- psppire_data_store_set_reader (store, cr);
- dict_unref (ia->casereader_dict);
- ia->casereader_dict = dict_clone (ia->dict);
-}