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