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