Use the msg function to report errors wherever possible.
[pspp] / src / ui / gui / page-formats.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  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 <errno.h>
22 #include <fcntl.h>
23 #include <gtk/gtk.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/format-guesser.h"
31 #include "data/value-labels.h"
32 #include "language/data-io/data-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/line-reader.h"
37 #include "libpspp/message.h"
38 #include "ui/gui/checkbox-treeview.h"
39 #include "ui/gui/dialog-common.h"
40 #include "ui/gui/executor.h"
41 #include "ui/gui/helper.h"
42 #include "ui/gui/builder-wrapper.h"
43 #include "ui/gui/psppire-data-window.h"
44 #include "ui/gui/psppire-dialog.h"
45 #include "ui/gui/psppire-encoding-selector.h"
46 #include "ui/gui/psppire-empty-list-store.h"
47 #include "ui/gui/psppire-var-sheet.h"
48 #include "ui/gui/psppire-scanf.h"
49
50 #include "gl/intprops.h"
51 #include "gl/xalloc.h"
52
53 #include "gettext.h"
54 #define _(msgid) gettext (msgid)
55 #define N_(msgid) msgid
56
57 /* Page where the user verifies and adjusts input formats. */
58 struct formats_page
59   {
60     GtkWidget *page;
61     PsppSheetView *data_tree_view;
62     PsppireDict *psppire_dict;
63     struct variable **modified_vars;
64     size_t modified_var_cnt;
65   };
66
67 /* The "formats" page of the assistant. */
68
69 static void on_variable_change (PsppireDict *dict, int idx,
70                                 unsigned int what,
71                                 const struct variable *oldvar,
72                                 struct import_assistant *);
73
74 static void clear_modified_vars (struct import_assistant *);
75
76 /* Initializes IA's formats substructure. */
77
78 struct formats_page *
79 formats_page_create (struct import_assistant *ia)
80 {
81   GtkBuilder *builder = ia->asst.builder;
82   struct formats_page *p = xzalloc (sizeof *p);
83
84   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Formats"),
85                          GTK_ASSISTANT_PAGE_CONFIRM);
86
87   p->data_tree_view = PSPP_SHEET_VIEW (get_widget_assert (builder, "data"));
88   p->modified_vars = NULL;
89   p->modified_var_cnt = 0;
90
91   return p;
92 }
93
94 /* Frees IA's formats substructure. */
95 void
96 destroy_formats_page (struct import_assistant *ia)
97 {
98   struct formats_page *p = ia->formats;
99
100   if (p->psppire_dict != NULL)
101     {
102       dict_destroy (p->psppire_dict->dict);
103       g_object_unref (p->psppire_dict);
104     }
105   clear_modified_vars (ia);
106 }
107
108 /* Called just before the formats page of the assistant is
109    displayed. */
110 void
111 prepare_formats_page (struct import_assistant *ia)
112 {
113   struct dictionary *dict;
114   PsppireDict *psppire_dict;
115   GtkBin *vars_scroller;
116   GtkWidget *old_var_sheet;
117   PsppireVarSheet *var_sheet;
118   struct formats_page *p = ia->formats;
119   struct fmt_guesser *fg;
120   unsigned long int number = 0;
121   size_t column_idx;
122
123   push_watch_cursor (ia);
124
125   dict = dict_create (get_default_encoding ());
126   fg = fmt_guesser_create ();
127   for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
128     {
129       struct variable *modified_var = 
130         (column_idx < p->modified_var_cnt ? p->modified_vars[column_idx] : NULL);
131       if (modified_var == NULL)
132         {
133           struct column *column = &ia->columns[column_idx];
134           struct variable *var;
135           struct fmt_spec format;
136           char *name;
137           size_t row;
138
139           /* Choose variable name. */
140           name = dict_make_unique_var_name (dict, column->name, &number);
141
142           /* Choose variable format. */
143           fmt_guesser_clear (fg);
144           for (row = ia->skip_lines; row < ia->file.line_cnt; row++)
145             fmt_guesser_add (fg, column->contents[row]);
146           fmt_guesser_guess (fg, &format);
147           fmt_fix_input (&format);
148
149           /* Create variable. */
150           var = dict_create_var_assert (dict, name, fmt_var_width (&format));
151           var_set_both_formats (var, &format);
152
153           free (name);
154         }
155       else
156         {
157           char *name;
158
159           name = dict_make_unique_var_name (dict, var_get_name (modified_var),
160                                             &number);
161           dict_clone_var_as_assert (dict, modified_var, name);
162           free (name);
163         }
164     }
165   fmt_guesser_destroy (fg);
166
167   psppire_dict = psppire_dict_new_from_dict (dict);
168   g_signal_connect (psppire_dict, "variable-changed",
169                     G_CALLBACK (on_variable_change), ia);
170   ia->dict = dict;
171   ia->formats->psppire_dict = psppire_dict;
172
173   /* XXX: PsppireVarStore doesn't hold a reference to
174      psppire_dict for now, but it should.  After it does, we
175      should g_object_ref the psppire_dict here, since we also
176      hold a reference via ia->formats->dict. */
177   var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
178   g_object_set (var_sheet,
179                 "dictionary", psppire_dict,
180                 "may-create-vars", FALSE,
181                 "may-delete-vars", FALSE,
182                 "format-use", FMT_FOR_INPUT,
183                 "enable-grid-lines", PSPP_SHEET_VIEW_GRID_LINES_BOTH,
184                 (void *) NULL);
185
186   vars_scroller = GTK_BIN (get_widget_assert (ia->asst.builder, "vars-scroller"));
187   old_var_sheet = gtk_bin_get_child (vars_scroller);
188   if (old_var_sheet != NULL)
189     gtk_widget_destroy (old_var_sheet);
190   gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
191   gtk_widget_show (GTK_WIDGET (var_sheet));
192
193   gtk_widget_destroy (GTK_WIDGET (ia->formats->data_tree_view));
194   ia->formats->data_tree_view = create_data_tree_view (
195     false,
196     GTK_CONTAINER (get_widget_assert (ia->asst.builder, "data-scroller")),
197     ia);
198
199   gtk_widget_show (ia->asst.paste_button);
200
201   pop_watch_cursor (ia);
202 }
203
204 /* Clears the set of user-modified variables from IA's formats
205    substructure.  This discards user modifications to variable
206    formats, thereby causing formats to revert to their
207    defaults.  */
208 static void
209 clear_modified_vars (struct import_assistant *ia)
210 {
211   struct formats_page *p = ia->formats;
212   size_t i;
213
214   for (i = 0; i < p->modified_var_cnt; i++)
215     var_destroy (p->modified_vars[i]);
216   free (p->modified_vars);
217   p->modified_vars = NULL;
218   p->modified_var_cnt = 0;
219 }
220
221 /* Resets the formats page to its defaults, discarding user
222    modifications. */
223 void
224 reset_formats_page (struct import_assistant *ia)
225 {
226   clear_modified_vars (ia);
227   prepare_formats_page (ia);
228 }
229
230
231
232 /* Called when the user changes one of the variables in the
233    dictionary. */
234 static void
235 on_variable_change (PsppireDict *dict, int dict_idx,
236                     unsigned int what, const struct variable *oldvar,
237                     struct import_assistant *ia)
238 {
239   struct formats_page *p = ia->formats;
240   PsppSheetView *tv = ia->formats->data_tree_view;
241   gint column_idx = dict_idx + 1;
242
243   push_watch_cursor (ia);
244
245   /* Remove previous column and replace with new column. */
246   pspp_sheet_view_remove_column (tv, pspp_sheet_view_get_column (tv, column_idx));
247   pspp_sheet_view_insert_column (tv, make_data_column (ia, tv, false, dict_idx),
248                                  column_idx);
249
250   /* Save a copy of the modified variable in modified_vars, so
251      that its attributes will be preserved if we back up to the
252      previous page with the Prev button and then come back
253      here. */
254   if (dict_idx >= p->modified_var_cnt)
255     {
256       size_t i;
257       p->modified_vars = xnrealloc (p->modified_vars, dict_idx + 1,
258                                     sizeof *p->modified_vars);
259       for (i = 0; i <= dict_idx; i++)
260         p->modified_vars[i] = NULL;
261       p->modified_var_cnt = dict_idx + 1;
262     }
263   if (p->modified_vars[dict_idx])
264     var_destroy (p->modified_vars[dict_idx]);
265   p->modified_vars[dict_idx]
266     = var_clone (psppire_dict_get_variable (dict, dict_idx));
267
268   pop_watch_cursor (ia);
269 }
270
271
272
273
274 void
275 formats_append_syntax (const struct import_assistant *ia, struct string *s)
276 {
277   int i;
278   int var_cnt;
279   ds_put_cstr (s, "  /VARIABLES=\n");
280   
281   var_cnt = dict_get_var_cnt (ia->dict);
282   for (i = 0; i < var_cnt; i++)
283     {
284       struct variable *var = dict_get_var (ia->dict, i);
285       char format_string[FMT_STRING_LEN_MAX + 1];
286       fmt_to_string (var_get_print_format (var), format_string);
287       ds_put_format (s, "    %s %s%s\n",
288                      var_get_name (var), format_string,
289                      i == var_cnt - 1 ? "." : "");
290     }
291 }