Merge remote branch 'origin/master' into import-gui
[pspp] / src / ui / gui / page-sheet-spec.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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 "page-sheet-spec.h"
20
21 #include "ui/gui/text-data-import-dialog.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <gtk/gtk.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29
30 #include "data/data-in.h"
31 #include "data/data-out.h"
32 #include "data/format-guesser.h"
33 #include "data/value-labels.h"
34 #include "data/gnumeric-reader.h"
35 #include "data/ods-reader.h"
36 #include "data/spreadsheet-reader.h"
37 #include "language/data-io/data-parser.h"
38 #include "language/lexer/lexer.h"
39 #include "libpspp/assertion.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/line-reader.h"
42 #include "libpspp/message.h"
43 #include "ui/gui/checkbox-treeview.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/psppire-data-window.h"
49 #include "ui/gui/psppire-dialog.h"
50 #include "ui/gui/psppire-encoding-selector.h"
51 #include "ui/gui/psppire-empty-list-store.h"
52 #include "ui/gui/psppire-var-sheet.h"
53 #include "ui/gui/psppire-scanf.h"
54 #include "ui/syntax-gen.h"
55
56 #include "ui/gui/psppire-spreadsheet-model.h"
57
58 #include <data/casereader.h>
59
60 #include "gl/error.h"
61 #include "gl/intprops.h"
62 #include "gl/xalloc.h"
63
64 #include "gettext.h"
65 #define _(msgid) gettext (msgid)
66 #define N_(msgid) msgid
67
68 struct import_assistant;
69
70 /* The "sheet-spec" page of the assistant. */
71
72 /* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */
73 struct sheet_spec_page
74   {
75     GtkWidget *page;
76     struct casereader *reader;
77     struct dictionary *dict;
78     
79     struct spreadsheet_read_options opts;
80   };
81
82
83 char *
84 sheet_spec_gen_syntax (const struct import_assistant *ia)
85 {
86   const struct sheet_spec_page *ssp = ia->sheet_spec;
87   GtkBuilder *builder = ia->asst.builder;
88   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
89   const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
90
91   struct string s = DS_EMPTY_INITIALIZER;
92
93   syntax_gen_pspp (&s,
94                    "GET DATA"
95                    "\n  /TYPE=%ss"
96                    "\n  /FILE=%sq"
97                    "\n  /SHEET=index %d"
98                    "\n  /READNAMES=%ss",
99                    (ia->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
100                    ia->file.file_name,                   
101                    ssp->opts.sheet_index,
102                    ssp->opts.read_names ? "ON" : "OFF");
103
104
105   if (range && 0 != strcmp ("", range))
106     {
107       syntax_gen_pspp (&s,
108                        "\n  /CELLRANGE=RANGE %sq", range);
109     }
110   else
111     {
112       syntax_gen_pspp (&s,
113                        "\n  /CELLRANGE=FULL");
114     }
115
116
117   syntax_gen_pspp (&s, ".");
118
119   
120   return ds_cstr (&s);
121 }
122
123
124 static void 
125 on_sheet_combo_changed (GtkComboBox *cb, struct import_assistant *ia)
126 {
127   GtkTreeIter iter;
128   gchar *range = NULL;
129   GtkTreeModel *model = gtk_combo_box_get_model (cb);
130   GtkBuilder *builder = ia->asst.builder;
131   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
132
133   gtk_combo_box_get_active_iter (cb, &iter);
134   gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
135   gtk_entry_set_text (GTK_ENTRY (range_entry), range ?  range : "");
136   g_free (range);
137 }
138
139 /* Initializes IA's sheet_spec substructure. */
140 struct sheet_spec_page *
141 sheet_spec_page_create (struct import_assistant *ia)
142 {
143   GtkBuilder *builder = ia->asst.builder;
144   struct sheet_spec_page *p = xzalloc (sizeof (*p));
145
146   GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
147   GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
148   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
149   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
150                                   "text", 0,
151                                   NULL);
152
153   g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
154
155   add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
156                          GTK_ASSISTANT_PAGE_INTRO);
157
158   return p;
159 }
160
161 /* Prepares IA's sheet_spec page. */
162 void
163 prepare_sheet_spec_page (struct import_assistant *ia)
164 {
165   GtkBuilder *builder = ia->asst.builder;
166   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
167
168   gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry), 
169                            psppire_spreadsheet_model_new (ia->spreadsheet));
170
171   gtk_combo_box_set_active (GTK_COMBO_BOX (sheet_entry), 0);
172 }
173
174
175 /* Resets IA's sheet_spec page to its initial state. */
176 void
177 reset_sheet_spec_page (struct import_assistant *ia)
178 {
179   printf ("%s\n", __FUNCTION__);
180 }
181
182 /* Called when the Forward button is clicked, 
183    but before displaying the new page.
184 */
185 void
186 post_sheet_spec_page (struct import_assistant *ia)
187 {
188   int row_start = -1;
189   int row_stop = -1;
190   int col_start = -1;
191   int col_stop = -1;
192
193   GtkBuilder *builder = ia->asst.builder;
194
195   struct sheet_spec_page *ssp = ia->sheet_spec;
196   struct casereader *creader = NULL;
197   struct dictionary *dict = NULL;
198
199   GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
200   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
201   const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
202   GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
203
204   gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box));
205   
206   ssp->opts.sheet_name = NULL;
207   ssp->opts.cell_range = NULL;
208   ssp->opts.sheet_index = num + 1;
209
210   if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
211     {
212       ssp->opts.cell_range = range;
213     }
214
215   ssp->opts.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
216   ssp->opts.asw = -1;
217
218   switch (ia->spreadsheet->type)
219     {
220     case SPREADSHEET_ODS:
221     case SPREADSHEET_GNUMERIC:
222       {
223         creader = spreadsheet_make_reader (ia->spreadsheet, &ssp->opts);
224         dict = ia->spreadsheet->dict;
225       }
226       break;
227     default:
228       g_assert_not_reached ();
229       break;
230     }
231
232   ssp->dict = dict;
233   ssp->reader = creader;
234
235   if (creader && dict)
236     {
237       update_assistant (ia);
238     }
239   else
240     {
241       GtkWidget * dialog = gtk_message_dialog_new (NULL,
242                               GTK_DIALOG_MODAL,
243                               GTK_MESSAGE_ERROR,
244                               GTK_BUTTONS_CLOSE,
245                               _("An error occurred reading the spreadsheet file."));
246
247       gtk_dialog_run (GTK_DIALOG (dialog));
248       gtk_widget_destroy (dialog);
249     }
250 }
251
252
253 /*
254   Update IA according to the contents of DICT and CREADER.
255   CREADER will be destroyed by this function.
256 */
257 void 
258 update_assistant (struct import_assistant *ia)
259 {
260   struct sheet_spec_page *ssp = ia->sheet_spec;
261   int rows = 0;
262
263   if (ssp->dict)
264     {
265       struct ccase *c;
266       int col;
267
268       ia->column_cnt = dict_get_var_cnt (ssp->dict);
269       ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
270       for (col = 0; col < ia->column_cnt ; ++col)
271         {
272           const struct variable *var = dict_get_var (ssp->dict, col);
273           ia->columns[col].name = xstrdup (var_get_name (var));
274           ia->columns[col].contents = NULL;
275         }
276
277       for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
278         {
279           rows++;
280           for (col = 0; col < ia->column_cnt ; ++col)
281             {
282               char *ss;
283               const struct variable *var = dict_get_var (ssp->dict, col);
284               
285               ia->columns[col].contents = xrealloc (ia->columns[col].contents,
286                                                     sizeof (struct substring) * rows);
287               
288               ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict), 
289                              var_get_print_format (var));
290               
291               ia->columns[col].contents[rows - 1] = ss_cstr (ss);
292             }
293           
294           if (rows > MAX_PREVIEW_LINES)
295             {
296               case_unref (c);
297               break;
298             }
299         }
300     }
301   
302   ia->file.line_cnt = rows;
303 }