formats page: Construction is initialisation
[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 <data/casereader.h>
57
58 #include "gl/error.h"
59 #include "gl/intprops.h"
60 #include "gl/xalloc.h"
61
62 #include "gettext.h"
63 #define _(msgid) gettext (msgid)
64 #define N_(msgid) msgid
65
66 struct import_assistant;
67
68
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     struct spreadsheet *spreadsheet;
79     
80     struct spreadsheet_read_info sri;
81     struct spreadsheet_read_options opts;
82   };
83
84
85
86
87
88 /* Initializes IA's sheet_spec substructure. */
89 struct sheet_spec_page *
90 sheet_spec_page_create (struct import_assistant *ia)
91 {
92   GtkBuilder *builder = ia->asst.builder;
93   struct sheet_spec_page *p = xzalloc (sizeof (*p));
94
95   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
96                                    GTK_ASSISTANT_PAGE_INTRO);
97
98   return p;
99 }
100
101 /* Prepares IA's sheet_spec page. */
102 void
103 prepare_sheet_spec_page (struct import_assistant *ia)
104 {
105   struct sheet_spec_page *p = ia->sheet_spec;
106
107   GtkBuilder *builder = ia->asst.builder;
108   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
109
110   printf ("%s %d\n", __FUNCTION__, p->spreadsheet->sheets);
111
112   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (sheet_entry), 0);
113   gtk_spin_button_set_range (GTK_SPIN_BUTTON (sheet_entry), 1, p->spreadsheet->sheets);
114 }
115
116
117 /* Resets IA's sheet_spec page to its initial state. */
118 void
119 reset_sheet_spec_page (struct import_assistant *ia)
120 {
121   printf ("%s\n", __FUNCTION__);
122 }
123
124 /* Called when the Forward button is clicked, 
125    but before displaying the new page.
126 */
127 void
128 post_sheet_spec_page (struct import_assistant *ia)
129 {
130   int row_start = -1;
131   int row_stop = -1;
132   int col_start = -1;
133   int col_stop = -1;
134
135   GtkBuilder *builder = ia->asst.builder;
136
137   struct sheet_spec_page *ssp = ia->sheet_spec;
138   struct casereader *creader = NULL;
139   struct dictionary *dict = NULL;
140
141   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
142   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
143   GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
144
145   gint num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (sheet_entry));
146
147   const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
148
149   if ( num < 1 )
150     num = 1;
151   
152   ssp->opts.sheet_name = NULL;
153   ssp->opts.cell_range = NULL;
154   ssp->opts.sheet_index = num;
155
156   if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
157     {
158       ssp->opts.cell_range = range;
159     }
160
161   ssp->sri.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
162   ssp->sri.asw = -1;
163
164   switch (ssp->spreadsheet->type)
165     {
166     case SPREADSHEET_ODS:
167       {
168         creader = ods_make_reader (ssp->spreadsheet, &ssp->sri, &ssp->opts);
169         dict = ssp->spreadsheet->dict;
170       }
171       break;
172     case SPREADSHEET_GNUMERIC:
173       {
174         creader = gnumeric_make_reader (ssp->spreadsheet, &ssp->sri, &ssp->opts);
175         dict = ssp->spreadsheet->dict;
176       }
177       break;
178     default:
179       g_assert_not_reached ();
180       break;
181     }
182
183   ssp->dict = dict;
184   ssp->reader = creader;
185
186   if (creader && dict)
187     {
188       update_assistant (ia);
189     }
190   else
191     {
192       GtkWidget * dialog = gtk_message_dialog_new (NULL,
193                               GTK_DIALOG_MODAL,
194                               GTK_MESSAGE_ERROR,
195                               GTK_BUTTONS_CLOSE,
196                               _("An error occurred reading the spreadsheet file."));
197
198       gtk_dialog_run (GTK_DIALOG (dialog));
199       gtk_widget_destroy (dialog);
200     }
201 }
202
203
204 /*
205   Update IA according to the contents of DICT and CREADER.
206   CREADER will be destroyed by this function.
207 */
208 void 
209 update_assistant (struct import_assistant *ia)
210 {
211   struct sheet_spec_page *ssp = ia->sheet_spec;
212   int rows = 0;
213
214
215   if (ssp->dict)
216     {
217       struct ccase *c;
218       int col;
219
220       ia->column_cnt = dict_get_var_cnt (ssp->dict);
221       ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
222       for (col = 0; col < ia->column_cnt ; ++col)
223         {
224           const struct variable *var = dict_get_var (ssp->dict, col);
225           ia->columns[col].name = xstrdup (var_get_name (var));
226           ia->columns[col].contents = NULL;
227         }
228
229       for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
230         {
231           rows++;
232           for (col = 0; col < ia->column_cnt ; ++col)
233             {
234               char *ss;
235               const struct variable *var = dict_get_var (ssp->dict, col);
236
237               ia->columns[col].contents = xrealloc (ia->columns[col].contents,
238                                                       sizeof (struct substring) * rows);
239
240               ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict), 
241                              var_get_print_format (var));
242
243               ia->columns[col].contents[rows - 1] = ss_cstr (ss);
244             }
245
246           if (rows > MAX_PREVIEW_LINES)
247             {
248               case_unref (c);
249               break;
250             }
251         }
252     }
253   
254   //  file->line_cnt = rows;
255 }