src/ui/gui/psppire-conf.c: Use GError instead of strerror.
[pspp] / src / ui / gui / psppire-import-assistant.c
index 8270938373f6ae9e5be4954d5921b87f9d35075d..a968431d865bf043759adbdafc8368c5c504cfa4 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2015, 2016, 2017, 2018  Free Software Foundation
+   Copyright (C) 2015, 2016, 2017, 2018, 2020  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -128,6 +128,7 @@ psppire_import_assistant_finalize (GObject *object)
   ds_destroy (&ia->quotes);
 
   dict_unref (ia->dict);
+  dict_unref (ia->casereader_dict);
 
   g_object_unref (ia->builder);
 
@@ -284,23 +285,30 @@ choose_likely_separators (PsppireImportAssistant *ia)
     }
   gtk_tree_path_free (p);
 
-  int most_frequent = -1;
-  int largest = 0;
-  for (j = 0; j < SEPARATOR_CNT; ++j)
+  if (hmap_count (count_map) > 0)
     {
-      struct separator_count_node *cn;
-      HMAP_FOR_EACH (cn, struct separator_count_node, node, &count_map[j])
-       {
-         if (largest < cn->quantity)
-           {
-             largest = cn->quantity;
-             most_frequent = j;
-           }
-       }
-      hmap_destroy (&count_map[j]);
-    }
+      int most_frequent = -1;
+      int largest = 0;
+      for (j = 0; j < SEPARATOR_CNT; ++j)
+        {
+          struct separator_count_node *cn;
+          HMAP_FOR_EACH (cn, struct separator_count_node, node, &count_map[j])
+            {
+              if (largest < cn->quantity)
+                {
+                  largest = cn->quantity;
+                  most_frequent = j;
+                }
+            }
+          hmap_destroy (&count_map[j]);
+        }
+
+      g_return_if_fail (most_frequent >= 0);
 
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (get_widget_assert (ia->builder, separators[most_frequent].name)), TRUE);
+      GtkWidget *toggle =
+        get_widget_assert (ia->builder, separators[most_frequent].name);
+      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), TRUE);
+    }
 }
 
 static void
@@ -519,8 +527,11 @@ prepare_sheet_spec_page (PsppireImportAssistant *ia)
   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
   GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
 
-  gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry),
-                          psppire_spreadsheet_model_new (ia->spreadsheet));
+  GtkTreeModel *model = psppire_spreadsheet_model_new (ia->spreadsheet);
+  gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry), model);
+
+  gint items = gtk_tree_model_iter_n_children (model, NULL);
+  gtk_widget_set_sensitive (sheet_entry, items > 1);
 
   gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
 
@@ -644,6 +655,11 @@ chooser_page_reset (PsppireImportAssistant *ia, GtkWidget *page)
 }
 
 
+static void
+on_file_activated (GtkFileChooser *chooser, PsppireImportAssistant *ia)
+{
+  gtk_assistant_next_page (GTK_ASSISTANT (ia));
+}
 
 static void
 chooser_page_create (PsppireImportAssistant *ia)
@@ -652,6 +668,8 @@ chooser_page_create (PsppireImportAssistant *ia)
 
   GtkWidget *chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
 
+  g_signal_connect (chooser, "file-activated", G_CALLBACK (on_file_activated), ia);
+
   g_object_set_data (G_OBJECT (chooser), "on-forward", chooser_page_leave);
   g_object_set_data (G_OBJECT (chooser), "on-reset",   chooser_page_reset);
   g_object_set_data (G_OBJECT (chooser), "on-entering",chooser_page_enter);
@@ -729,6 +747,8 @@ psppire_import_assistant_init (PsppireImportAssistant *ia)
   ia->file_name = NULL;
 
   ia->spreadsheet = NULL;
+  ia->dict = NULL;
+  ia->casereader_dict = NULL;
 
   ia->main_loop = g_main_loop_new (NULL, TRUE);
 
@@ -1024,32 +1044,6 @@ psppire_import_assistant_new (GtkWindow *toplevel)
 
 \f
 
-static void
-set_quote_list (GtkComboBox *cb)
-{
-  GtkListStore *list =  gtk_list_store_new (1, G_TYPE_STRING);
-  GtkTreeIter iter;
-  gint i;
-  const gchar *separator[3] = {"'\"", "\'", "\""};
-
-  for (i = 0; i < 3; i++)
-    {
-      const gchar *s = separator[i];
-
-      /* Add a new row to the model */
-      gtk_list_store_append (list, &iter);
-      gtk_list_store_set (list, &iter,
-                          0, s,
-                          -1);
-
-    }
-
-  gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
-  g_object_unref (list);
-
-  gtk_combo_box_set_entry_text_column (cb, 0);
-}
-
 /* Chooses a name for each column on the separators page */
 static void
 choose_column_names (PsppireImportAssistant *ia)
@@ -1159,10 +1153,9 @@ separators_page_create (PsppireImportAssistant *ia)
   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_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo)));
   ia->quote_cb = get_widget_assert (builder, "quote-cb");
 
-  set_quote_list (GTK_COMBO_BOX (ia->quote_combo));
+  gtk_combo_box_set_active (GTK_COMBO_BOX (ia->quote_combo), 0);
 
   if (ia->fields_tree_view == NULL)
     {
@@ -1215,12 +1208,19 @@ my_read (struct casereader *reader, void *aux, casenumber idx)
          GValue value = {0};
          gtk_tree_model_get_value (tm, &iter, i + 1, &value);
 
-         const struct variable *var = dict_get_var (ia->dict, i);
+         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,
@@ -1258,7 +1258,7 @@ textfile_create_reader (PsppireImportAssistant *ia)
 
   int i;
 
-  struct fmt_guesser **fg = xcalloc (sizeof *fg, n_vars);
+  struct fmt_guesser **fg = XCALLOC (n_vars,  struct fmt_guesser *);
   for (i = 0 ; i < n_vars; ++i)
     {
       fg[i] = fmt_guesser_create ();
@@ -1304,10 +1304,47 @@ textfile_create_reader (PsppireImportAssistant *ia)
   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);
+}
 
 /* Called just before the formats page of the assistant is
    displayed. */
@@ -1347,6 +1384,9 @@ prepare_formats_page (PsppireImportAssistant *ia)
       PsppireDict *dict = psppire_dict_new_from_dict (ia->dict);
       PsppireDataStore *store = psppire_data_store_new (dict);
       psppire_data_store_set_reader (store, reader);
+      g_signal_connect (dict, "variable-changed",
+                        G_CALLBACK (ia_variable_changed_cb),
+                        ia);
 
       g_object_set (ia->data_sheet, "data-model", store, NULL);
       g_object_set (ia->var_sheet, "data-model", dict, NULL);
@@ -1423,8 +1463,15 @@ separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
        }
     }
   ds_put_cstr (s, "\"\n");
-  if (!ds_is_empty (&ia->quotes))
-    syntax_gen_pspp (s, "  /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
+
+  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->quote_cb)))
+    {
+      GtkComboBoxText *cbt = GTK_COMBO_BOX_TEXT (ia->quote_combo);
+      gchar *quotes = gtk_combo_box_text_get_active_text (cbt);
+      if (quotes && *quotes)
+        syntax_gen_pspp (s, "  /QUALIFIER=%sq\n", quotes);
+      free (quotes);
+    }
 }
 
 static void