34127492fa9393ec03a7a157342cd07d71360fd1
[pspp] / src / ui / gui / text-data-import-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/text-data-import-dialog.h"
20
21 #include "page-intro.h"
22 #include "page-sheet-spec.h"
23 #include "page-first-line.h"
24 #include "page-separators.h"
25 #include "page-formats.h"
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <gtk/gtk.h>
30 #include <limits.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33
34 #include "data/data-in.h"
35 #include "data/data-out.h"
36 #include "data/format-guesser.h"
37 #include "data/value-labels.h"
38 #include "language/data-io/data-parser.h"
39 #include "language/lexer/lexer.h"
40 #include "libpspp/assertion.h"
41 #include "libpspp/i18n.h"
42 #include "libpspp/line-reader.h"
43 #include "libpspp/message.h"
44 #include "ui/gui/dialog-common.h"
45 #include "ui/gui/executor.h"
46 #include "ui/gui/helper.h"
47 #include "ui/gui/builder-wrapper.h"
48 #include "ui/gui/pspp-sheet-selection.h"
49 #include "ui/gui/pspp-sheet-view.h"
50 #include "ui/gui/psppire-data-window.h"
51 #include "ui/gui/psppire-dialog.h"
52 #include "ui/gui/psppire-encoding-selector.h"
53 #include "ui/gui/psppire-empty-list-store.h"
54 #include "ui/gui/psppire-var-sheet.h"
55 #include "ui/gui/psppire-scanf.h"
56 #include "ui/syntax-gen.h"
57
58 #include "gl/intprops.h"
59 #include "gl/xalloc.h"
60
61 #include "gettext.h"
62 #define _(msgid) gettext (msgid)
63 #define N_(msgid) msgid
64
65 struct import_assistant;
66
67 static void apply_dict (const struct dictionary *, struct string *);
68 static char *generate_syntax (const struct import_assistant *);
69
70 static void add_line_number_column (const struct import_assistant *,
71                                     PsppSheetView *);
72
73 /* Pops up the Text Data Import assistant. */
74 void
75 text_data_import_assistant (PsppireDataWindow *dw)
76 {
77   GtkWindow *parent_window = GTK_WINDOW (dw);
78   struct import_assistant *ia = init_assistant (parent_window);
79   struct sheet_spec_page *ssp ;
80
81   if (!init_file (ia, parent_window))
82     {
83       free (ia);
84       return;
85     }
86
87   ssp = ia->sheet_spec;
88
89   if (ia->spreadsheet)
90     {
91       ia->sheet_spec = sheet_spec_page_create (ia);
92     }
93   else
94     {
95       ia->intro = intro_page_create (ia);
96       ia->first_line = first_line_page_create (ia);
97       ia->separators = separators_page_create (ia);
98     }
99   ia->formats = formats_page_create (ia);
100
101   gtk_widget_show_all (GTK_WIDGET (ia->asst.assistant));
102
103   ia->asst.main_loop = g_main_loop_new (NULL, false);
104
105   {  
106   /*
107     Instead of this block,
108     A simple     g_main_loop_run (ia->asst.main_loop);  should work here.  But it seems to crash.
109     I have no idea why.
110   */
111     GMainContext *ctx = g_main_loop_get_context (ia->asst.main_loop);
112     ia->asst.loop_done = false;
113     while (! ia->asst.loop_done)
114       {
115         g_main_context_iteration (ctx, TRUE);
116       }
117   }
118   g_main_loop_unref (ia->asst.main_loop);
119
120   switch (ia->asst.response)
121     {
122     case GTK_RESPONSE_APPLY:
123       {
124         gchar *fn = g_path_get_basename (ia->file.file_name);
125         open_data_window (PSPPIRE_WINDOW (dw), fn, NULL, generate_syntax (ia));
126         g_free (fn);
127       }
128       break;
129     case PSPPIRE_RESPONSE_PASTE:
130       free (paste_syntax_to_window (generate_syntax (ia)));
131       break;
132     default:
133       break;
134     }
135
136   if (ssp) 
137     {
138       destroy_formats_page (ia);
139       destroy_separators_page (ia);
140     }
141 }
142
143 /* Emits PSPP syntax to S that applies the dictionary attributes
144    (such as missing values and value labels) of the variables in
145    DICT.  */
146 static void
147 apply_dict (const struct dictionary *dict, struct string *s)
148 {
149   size_t var_cnt = dict_get_var_cnt (dict);
150   size_t i;
151
152   for (i = 0; i < var_cnt; i++)
153     {
154       struct variable *var = dict_get_var (dict, i);
155       const char *name = var_get_name (var);
156       enum val_type type = var_get_type (var);
157       int width = var_get_width (var);
158       enum measure measure = var_get_measure (var);
159       enum var_role role = var_get_role (var);
160       enum alignment alignment = var_get_alignment (var);
161       const struct fmt_spec *format = var_get_print_format (var);
162
163       if (var_has_missing_values (var))
164         {
165           const struct missing_values *mv = var_get_missing_values (var);
166           size_t j;
167
168           syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
169           for (j = 0; j < mv_n_values (mv); j++)
170             {
171               if (j)
172                 ds_put_cstr (s, ", ");
173               syntax_gen_value (s, mv_get_value (mv, j), width, format);
174             }
175
176           if (mv_has_range (mv))
177             {
178               double low, high;
179               if (mv_has_value (mv))
180                 ds_put_cstr (s, ", ");
181               mv_get_range (mv, &low, &high);
182               syntax_gen_num_range (s, low, high, format);
183             }
184           ds_put_cstr (s, ").\n");
185         }
186       if (var_has_value_labels (var))
187         {
188           const struct val_labs *vls = var_get_value_labels (var);
189           const struct val_lab **labels = val_labs_sorted (vls);
190           size_t n_labels = val_labs_count (vls);
191           size_t i;
192
193           syntax_gen_pspp (s, "VALUE LABELS %ss", name);
194           for (i = 0; i < n_labels; i++)
195             {
196               const struct val_lab *vl = labels[i];
197               ds_put_cstr (s, "\n  ");
198               syntax_gen_value (s, &vl->value, width, format);
199               ds_put_byte (s, ' ');
200               syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
201             }
202           free (labels);
203           ds_put_cstr (s, ".\n");
204         }
205       if (var_has_label (var))
206         syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
207                          name, var_get_label (var));
208       if (measure != var_default_measure (type))
209         syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
210                          name, measure_to_syntax (measure));
211       if (role != ROLE_INPUT)
212         syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
213                          var_role_to_syntax (role), name);
214       if (alignment != var_default_alignment (type))
215         syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
216                          name, alignment_to_syntax (alignment));
217       if (var_get_display_width (var) != var_default_display_width (width))
218         syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
219                          name, var_get_display_width (var));
220     }
221 }
222
223 /* Generates and returns PSPP syntax to execute the import
224    operation described by IA.  The caller must free the syntax
225    with free(). */
226 static char *
227 generate_syntax (const struct import_assistant *ia)
228 {
229   struct string s = DS_EMPTY_INITIALIZER;
230
231   if (ia->spreadsheet == NULL)
232     {
233       syntax_gen_pspp (&s,
234                        "GET DATA"
235                        "\n  /TYPE=TXT"
236                        "\n  /FILE=%sq\n",
237                        ia->file.file_name);
238       if (ia->file.encoding && strcmp (ia->file.encoding, "Auto"))
239         syntax_gen_pspp (&s, "  /ENCODING=%sq\n", ia->file.encoding);
240
241       intro_append_syntax (ia->intro, &s);
242
243
244       ds_put_cstr (&s,
245                    "  /ARRANGEMENT=DELIMITED\n"
246                    "  /DELCASE=LINE\n");
247
248       first_line_append_syntax (ia, &s);
249       separators_append_syntax (ia, &s);
250       formats_append_syntax (ia, &s);
251       apply_dict (ia->dict, &s);
252     }
253   else
254     {
255       return sheet_spec_gen_syntax (ia);
256     }
257   
258   return ds_cstr (&s);
259 }
260
261
262
263 static void render_input_cell (PsppSheetViewColumn *tree_column,
264                                GtkCellRenderer *cell,
265                                GtkTreeModel *model, GtkTreeIter *iter,
266                                gpointer ia);
267
268 static gboolean on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
269                                         gboolean keyboard_mode UNUSED,
270                                         GtkTooltip *tooltip,
271                                         struct import_assistant *);
272
273
274
275 /* Called to render one of the cells in the fields preview tree
276    view. */
277 static void
278 render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
279                    GtkTreeModel *model, GtkTreeIter *iter,
280                    gpointer ia_)
281 {
282   struct import_assistant *ia = ia_;
283   struct substring field;
284   size_t row;
285   gint column;
286
287   column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
288                                                "column-number"));
289   row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
290   field = ia->columns[column].contents[row];
291   if (field.string != NULL)
292     {
293       GValue text = {0, };
294       g_value_init (&text, G_TYPE_STRING);
295       g_value_take_string (&text, ss_xstrdup (field));
296       g_object_set_property (G_OBJECT (cell), "text", &text);
297       g_value_unset (&text);
298       g_object_set (cell, "background-set", FALSE, (void *) NULL);
299     }
300   else
301     g_object_set (cell,
302                   "text", "",
303                   "background", "red",
304                   "background-set", TRUE,
305                   (void *) NULL);
306 }
307
308 static gboolean
309 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
310                       const struct import_assistant *ia,
311                       size_t *row, size_t *column);
312
313
314 /* Called to render a tooltip on one of the cells in the fields
315    preview tree view. */
316 static gboolean
317 on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
318                         gboolean keyboard_mode UNUSED,
319                         GtkTooltip *tooltip, struct import_assistant *ia)
320 {
321   size_t row, column;
322
323   if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
324     return FALSE;
325
326   if (ia->columns[column].contents[row].string != NULL)
327     return FALSE;
328
329   gtk_tooltip_set_text (tooltip,
330                         _("This input line has too few separators "
331                           "to fill in this field."));
332   return TRUE;
333 }
334
335
336 /* Parses the contents of the field at (ROW,COLUMN) according to
337    its variable format.  If OUTPUTP is non-null, then *OUTPUTP
338    receives the formatted output for that field (which must be
339    freed with free).  If TOOLTIPP is non-null, then *TOOLTIPP
340    receives a message suitable for use in a tooltip, if one is
341    needed, or a null pointer otherwise.  Returns true if a
342    tooltip message is needed, otherwise false. */
343 static bool
344 parse_field (struct import_assistant *ia,
345              size_t row, size_t column,
346              char **outputp, char **tooltipp)
347 {
348   const struct fmt_spec *in;
349   struct fmt_spec out;
350   char *tooltip;
351   bool ok;
352
353   struct substring field = ia->columns[column].contents[row];
354   struct variable *var = dict_get_var (ia->dict, column);
355   union value val;
356
357   value_init (&val, var_get_width (var));
358   in = var_get_print_format (var);
359   out = fmt_for_output_from_input (in);
360   tooltip = NULL;
361   if (field.string != NULL)
362     {
363       char *error;
364
365       error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
366                        dict_get_encoding (ia->dict));
367       if (error != NULL)
368         {
369           tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
370                                  "format %s: %s"),
371                                (int) field.length, field.string,
372                                fmt_name (in->type), error);
373           free (error);
374         }
375     }
376   else
377     {
378       tooltip = xstrdup (_("This input line has too few separators "
379                            "to fill in this field."));
380       value_set_missing (&val, var_get_width (var));
381     }
382   if (outputp != NULL)
383     {
384       *outputp = data_out (&val, dict_get_encoding (ia->dict),  &out);
385     }
386   value_destroy (&val, var_get_width (var));
387
388   ok = tooltip == NULL;
389   if (tooltipp != NULL)
390     *tooltipp = tooltip;
391   else
392     free (tooltip);
393   return ok;
394 }
395
396 /* Called to render one of the cells in the data preview tree
397    view. */
398 static void
399 render_output_cell (PsppSheetViewColumn *tree_column,
400                     GtkCellRenderer *cell,
401                     GtkTreeModel *model,
402                     GtkTreeIter *iter,
403                     gpointer ia_)
404 {
405   struct import_assistant *ia = ia_;
406   char *output;
407   GValue gvalue = { 0, };
408   bool ok;
409
410   ok = parse_field (ia,
411                     (empty_list_store_iter_to_row (iter)
412                      + ia->skip_lines),
413                     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
414                                                         "column-number")),
415                     &output, NULL);
416
417   g_value_init (&gvalue, G_TYPE_STRING);
418   g_value_take_string (&gvalue, output);
419   g_object_set_property (G_OBJECT (cell), "text", &gvalue);
420   g_value_unset (&gvalue);
421
422   if (ok)
423     g_object_set (cell, "background-set", FALSE, (void *) NULL);
424   else
425     g_object_set (cell,
426                   "background", "red",
427                   "background-set", TRUE,
428                   (void *) NULL);
429 }
430
431 /* Called to render a tooltip for one of the cells in the data
432    preview tree view. */
433 static gboolean
434 on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
435                          gboolean keyboard_mode UNUSED,
436                          GtkTooltip *tooltip, struct import_assistant *ia)
437 {
438   size_t row, column;
439   char *text;
440
441   if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
442     return FALSE;
443
444   if (parse_field (ia, row, column, NULL, &text))
445     return FALSE;
446
447   gtk_tooltip_set_text (tooltip, text);
448   free (text);
449   return TRUE;
450 }
451 \f
452 /* Utility functions used by multiple pages of the assistant. */
453
454 static gboolean
455 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
456                       const struct import_assistant *ia,
457                       size_t *row, size_t *column)
458 {
459   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
460   gint bx, by;
461   GtkTreePath *path;
462   GtkTreeIter iter;
463   PsppSheetViewColumn *tree_column;
464   GtkTreeModel *tree_model;
465   bool ok;
466
467   /* Check that WIDGET is really visible on the screen before we
468      do anything else.  This is a bug fix for a sticky situation:
469      when text_data_import_assistant() returns, it frees the data
470      necessary to compose the tool tip message, but there may be
471      a tool tip under preparation at that point (even if there is
472      no visible tool tip) that will call back into us a little
473      bit later.  Perhaps the correct solution to this problem is
474      to make the data related to the tool tips part of a GObject
475      that only gets destroyed when all references are released,
476      but this solution appears to be effective too. */
477   if (!gtk_widget_get_mapped (widget))
478     return FALSE;
479
480   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
481                                                        wx, wy, &bx, &by);
482   if (!pspp_sheet_view_get_path_at_pos (tree_view, bx, by,
483                                       &path, &tree_column, NULL, NULL))
484     return FALSE;
485
486   *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
487                                                 "column-number"));
488
489   tree_model = pspp_sheet_view_get_model (tree_view);
490   ok = gtk_tree_model_get_iter (tree_model, &iter, path);
491   gtk_tree_path_free (path);
492   if (!ok)
493     return FALSE;
494
495   *row = empty_list_store_iter_to_row (&iter) + ia->skip_lines;
496   return TRUE;
497 }
498
499 void
500 make_tree_view (const struct import_assistant *ia,
501                 size_t first_line,
502                 PsppSheetView **tree_view)
503 {
504   GtkTreeModel *model;
505
506   *tree_view = PSPP_SHEET_VIEW (pspp_sheet_view_new ());
507   pspp_sheet_view_set_grid_lines (*tree_view, PSPP_SHEET_VIEW_GRID_LINES_BOTH);
508   model = GTK_TREE_MODEL (psppire_empty_list_store_new (
509                                                         ia->file.line_cnt - first_line));
510   g_object_set_data (G_OBJECT (model), "lines", ia->file.lines + first_line);
511   g_object_set_data (G_OBJECT (model), "first-line",
512                      GINT_TO_POINTER (first_line));
513   pspp_sheet_view_set_model (*tree_view, model);
514   g_object_unref (model);
515
516   add_line_number_column (ia, *tree_view);
517 }
518
519 static void
520 render_line_number (PsppSheetViewColumn *tree_column,
521                     GtkCellRenderer *cell,
522                     GtkTreeModel *tree_model,
523                     GtkTreeIter *iter,
524                     gpointer data)
525 {
526   gint row = empty_list_store_iter_to_row (iter);
527   char s[INT_BUFSIZE_BOUND (int)];
528   int first_line;
529
530   first_line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model),
531                                                    "first-line"));
532   sprintf (s, "%d", first_line + row);
533   g_object_set (cell, "text", s, NULL);
534 }
535
536 static void
537 add_line_number_column (const struct import_assistant *ia,
538                         PsppSheetView *treeview)
539 {
540   PsppSheetViewColumn *column;
541
542   column = pspp_sheet_view_column_new_with_attributes (
543     _("Line"), ia->asst.prop_renderer, (void *) NULL);
544   pspp_sheet_view_column_set_fixed_width (
545     column, get_monospace_width (treeview, ia->asst.prop_renderer, 5));
546   pspp_sheet_view_column_set_resizable (column, TRUE);
547   pspp_sheet_view_column_set_cell_data_func (column, ia->asst.prop_renderer,
548                                              render_line_number, NULL, NULL);
549   pspp_sheet_view_append_column (treeview, column);
550 }
551
552 gint
553 get_monospace_width (PsppSheetView *treeview, GtkCellRenderer *renderer,
554                      size_t char_cnt)
555 {
556   struct string s;
557   gint width;
558
559   ds_init_empty (&s);
560   ds_put_byte_multiple (&s, '0', char_cnt);
561   ds_put_byte (&s, ' ');
562   width = get_string_width (treeview, renderer, ds_cstr (&s));
563   ds_destroy (&s);
564
565   return width;
566 }
567
568 gint
569 get_string_width (PsppSheetView *treeview, GtkCellRenderer *renderer,
570                   const char *string)
571 {
572   gint width;
573   g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
574   gtk_cell_renderer_get_preferred_width (renderer, GTK_WIDGET (treeview),
575                               NULL, &width);
576   return width;
577 }
578
579 PsppSheetViewColumn *
580 make_data_column (struct import_assistant *ia, PsppSheetView *tree_view,
581                   bool input, gint dict_idx)
582 {
583   struct variable *var = NULL;
584   struct column *column = NULL;
585   size_t char_cnt;
586   gint content_width, header_width;
587   PsppSheetViewColumn *tree_column;
588   char *name;
589
590   if (input)
591     column = &ia->columns[dict_idx];
592   else
593     var = dict_get_var (ia->dict, dict_idx);
594
595   name = escape_underscores (input ? column->name : var_get_name (var));
596   char_cnt = input ? column->width : var_get_print_format (var)->w;
597   content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
598                                        char_cnt);
599   header_width = get_string_width (tree_view, ia->asst.prop_renderer,
600                                    name);
601
602   tree_column = pspp_sheet_view_column_new ();
603   g_object_set_data (G_OBJECT (tree_column), "column-number",
604                      GINT_TO_POINTER (dict_idx));
605   pspp_sheet_view_column_set_title (tree_column, name);
606   pspp_sheet_view_column_pack_start (tree_column, ia->asst.fixed_renderer,
607                                      FALSE);
608   pspp_sheet_view_column_set_cell_data_func (
609     tree_column, ia->asst.fixed_renderer,
610     input ? render_input_cell : render_output_cell, ia, NULL);
611   pspp_sheet_view_column_set_fixed_width (tree_column, MAX (content_width,
612                                                             header_width));
613
614   free (name);
615
616   return tree_column;
617 }
618
619 PsppSheetView *
620 create_data_tree_view (bool input, GtkContainer *parent,
621                        struct import_assistant *ia)
622 {
623   PsppSheetView *tree_view;
624   gint i;
625
626   make_tree_view (ia, ia->skip_lines, &tree_view);
627   pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (tree_view),
628                                  PSPP_SHEET_SELECTION_NONE);
629
630   for (i = 0; i < ia->column_cnt; i++)
631     pspp_sheet_view_append_column (tree_view,
632                                    make_data_column (ia, tree_view, input, i));
633
634   g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
635   g_signal_connect (tree_view, "query-tooltip",
636                     G_CALLBACK (input ? on_query_input_tooltip
637                                 : on_query_output_tooltip), ia);
638
639
640   gtk_container_add (parent, GTK_WIDGET (tree_view));
641   gtk_widget_show (GTK_WIDGET (tree_view));
642
643   return tree_view;
644 }
645
646 /* Increments the "watch cursor" level, setting the cursor for
647    the assistant window to a watch face to indicate to the user
648    that the ongoing operation may take some time. */
649 void
650 push_watch_cursor (struct import_assistant *ia)
651 {
652   if (++ia->asst.watch_cursor == 1)
653     {
654       GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
655       GdkDisplay *display = gtk_widget_get_display (widget);
656       GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
657       gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
658       g_object_unref (cursor);
659       gdk_display_flush (display);
660     }
661 }
662
663 /* Decrements the "watch cursor" level.  If the level reaches
664    zero, the cursor is reset to its default shape. */
665 void
666 pop_watch_cursor (struct import_assistant *ia)
667 {
668   if (--ia->asst.watch_cursor == 0)
669     {
670       GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
671       gdk_window_set_cursor (gtk_widget_get_window (widget), NULL);
672     }
673 }