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