Make the data visible
[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 "ui/gui/text-data-import-dialog.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <gtk-contrib/psppire-sheet.h>
24 #include <gtk/gtk.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28
29 #include "data/data-in.h"
30 #include "data/data-out.h"
31 #include "data/format-guesser.h"
32 #include "data/value-labels.h"
33 #include "data/gnumeric-reader.h"
34 #include "data/ods-reader.h"
35 #include "data/spreadsheet-reader.h"
36 #include "language/data-io/data-parser.h"
37 #include "language/lexer/lexer.h"
38 #include "libpspp/assertion.h"
39 #include "libpspp/i18n.h"
40 #include "libpspp/line-reader.h"
41 #include "libpspp/message.h"
42 #include "ui/gui/checkbox-treeview.h"
43 #include "ui/gui/dialog-common.h"
44 #include "ui/gui/executor.h"
45 #include "ui/gui/helper.h"
46 #include "ui/gui/builder-wrapper.h"
47 #include "ui/gui/psppire-data-window.h"
48 #include "ui/gui/psppire-dialog.h"
49 #include "ui/gui/psppire-encoding-selector.h"
50 #include "ui/gui/psppire-empty-list-store.h"
51 #include "ui/gui/psppire-var-sheet.h"
52 #include "ui/gui/psppire-var-store.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_info sri;
80     struct spreadsheet_read_options opts;
81   };
82
83 static void 
84 on_sheet_combo_changed (GtkComboBox *cb, struct import_assistant *ia)
85 {
86   GtkTreeIter iter;
87   gchar *range = NULL;
88   GtkTreeModel *model = gtk_combo_box_get_model (cb);
89   GtkBuilder *builder = ia->asst.builder;
90   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
91
92   gtk_combo_box_get_active_iter (cb, &iter);
93   gtk_tree_model_get (model, &iter, PSPPIRE_SPREADSHEET_MODEL_COL_RANGE, &range, -1);
94   gtk_entry_set_text (GTK_ENTRY (range_entry), range ?  range : "");
95   g_free (range);
96 }
97
98 /* Initializes IA's sheet_spec substructure. */
99 struct sheet_spec_page *
100 sheet_spec_page_create (struct import_assistant *ia)
101 {
102   GtkBuilder *builder = ia->asst.builder;
103   struct sheet_spec_page *p = xzalloc (sizeof (*p));
104
105   GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
106   GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
107   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
108   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
109                                   "text", 0,
110                                   NULL);
111
112   g_signal_connect (combo_box, "changed", G_CALLBACK (on_sheet_combo_changed), ia);
113
114   add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
115                          GTK_ASSISTANT_PAGE_INTRO);
116
117   return p;
118 }
119
120 /* Prepares IA's sheet_spec page. */
121 void
122 prepare_sheet_spec_page (struct import_assistant *ia)
123 {
124   GtkBuilder *builder = ia->asst.builder;
125   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
126   printf ("%s:%d %p\n", __FILE__, __LINE__, ia->spreadsheet);
127   gtk_combo_box_set_model (GTK_COMBO_BOX (sheet_entry), 
128                            psppire_spreadsheet_model_new (ia->spreadsheet));
129
130   gtk_combo_box_set_active (sheet_entry, 0);
131 }
132
133
134 /* Resets IA's sheet_spec page to its initial state. */
135 void
136 reset_sheet_spec_page (struct import_assistant *ia)
137 {
138   printf ("%s\n", __FUNCTION__);
139 }
140
141 /* Called when the Forward button is clicked, 
142    but before displaying the new page.
143 */
144 void
145 post_sheet_spec_page (struct import_assistant *ia)
146 {
147   int row_start = -1;
148   int row_stop = -1;
149   int col_start = -1;
150   int col_stop = -1;
151
152   GtkBuilder *builder = ia->asst.builder;
153
154   struct sheet_spec_page *ssp = ia->sheet_spec;
155   struct casereader *creader = NULL;
156   struct dictionary *dict = NULL;
157
158   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
159   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
160   GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
161
162   const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
163
164   gint num = 1;
165   
166   ssp->opts.sheet_name = NULL;
167   ssp->opts.cell_range = NULL;
168   ssp->opts.sheet_index = num;
169
170   if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
171     {
172       ssp->opts.cell_range = range;
173     }
174
175   ssp->sri.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
176   ssp->sri.asw = -1;
177
178   switch (ia->spreadsheet->type)
179     {
180     case SPREADSHEET_ODS:
181       {
182         printf ("%s:%d\n", __FILE__, __LINE__);
183         creader = ods_make_reader (ia->spreadsheet, &ssp->sri, &ssp->opts);
184         dict = ia->spreadsheet->dict;
185       }
186       break;
187     case SPREADSHEET_GNUMERIC:
188       {
189         printf ("%s:%d\n",__FILE__, __LINE__);
190         creader = gnumeric_make_reader (ia->spreadsheet, &ssp->sri, &ssp->opts);
191         dict = ia->spreadsheet->dict;
192         printf ("%s:%d Reader %p Dict %p\n",__FILE__, __LINE__, creader, dict);
193       }
194       break;
195     default:
196       g_assert_not_reached ();
197       break;
198     }
199
200   ssp->dict = dict;
201   ssp->reader = creader;
202
203   if (creader && dict)
204     {
205       update_assistant (ia);
206     }
207   else
208     {
209       GtkWidget * dialog = gtk_message_dialog_new (NULL,
210                               GTK_DIALOG_MODAL,
211                               GTK_MESSAGE_ERROR,
212                               GTK_BUTTONS_CLOSE,
213                               _("An error occurred reading the spreadsheet file."));
214
215       gtk_dialog_run (GTK_DIALOG (dialog));
216       gtk_widget_destroy (dialog);
217     }
218 }
219
220
221 /*
222   Update IA according to the contents of DICT and CREADER.
223   CREADER will be destroyed by this function.
224 */
225 void 
226 update_assistant (struct import_assistant *ia)
227 {
228   struct sheet_spec_page *ssp = ia->sheet_spec;
229   int rows = 0;
230
231   if (ssp->dict)
232     {
233       struct ccase *c;
234       int col;
235
236       ia->column_cnt = dict_get_var_cnt (ssp->dict);
237       ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
238       for (col = 0; col < ia->column_cnt ; ++col)
239         {
240           const struct variable *var = dict_get_var (ssp->dict, col);
241           ia->columns[col].name = xstrdup (var_get_name (var));
242           ia->columns[col].contents = NULL;
243         }
244
245       for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
246         {
247           rows++;
248           for (col = 0; col < ia->column_cnt ; ++col)
249             {
250               char *ss;
251               const struct variable *var = dict_get_var (ssp->dict, col);
252
253               ia->columns[col].contents = xrealloc (ia->columns[col].contents,
254                                                       sizeof (struct substring) * rows);
255
256               ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict), 
257                              var_get_print_format (var));
258
259               ia->columns[col].contents[rows - 1] = ss_cstr (ss);
260             }
261
262           if (rows > MAX_PREVIEW_LINES)
263             {
264               case_unref (c);
265               break;
266             }
267         }
268     }
269   
270   ia->file.line_cnt = rows;
271 }