Move file_name member from sri to spreadsheet struct
[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 "gl/error.h"
57 #include "gl/intprops.h"
58 #include "gl/xalloc.h"
59
60 #include "gettext.h"
61 #define _(msgid) gettext (msgid)
62 #define N_(msgid) msgid
63
64 struct import_assistant;
65
66
67 \f
68 /* The "sheet-spec" page of the assistant. */
69
70
71 /* Initializes IA's sheet_spec substructure. */
72 void
73 init_sheet_spec_page (struct import_assistant *ia)
74 {
75   GtkBuilder *builder = ia->asst.builder;
76   struct sheet_spec_page *p = ia->sheet_spec;
77
78   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
79                                    GTK_ASSISTANT_PAGE_INTRO);
80
81 }
82
83 /* Prepares IA's sheet_spec page. */
84 void
85 prepare_sheet_spec_page (struct import_assistant *ia)
86 {
87   struct sheet_spec_page *p = ia->sheet_spec;
88
89   GtkBuilder *builder = ia->asst.builder;
90   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
91
92   printf ("%s %d\n", __FUNCTION__, p->spreadsheet->sheets);
93
94   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (sheet_entry), 0);
95   gtk_spin_button_set_range (GTK_SPIN_BUTTON (sheet_entry), 1, p->spreadsheet->sheets);
96 }
97
98
99 /* Resets IA's sheet_spec page to its initial state. */
100 void
101 reset_sheet_spec_page (struct import_assistant *ia)
102 {
103   printf ("%s\n", __FUNCTION__);
104 }
105
106 /* Called when the Forward button is clicked, 
107    but before displaying the new page.
108 */
109 void
110 post_sheet_spec_page (struct import_assistant *ia)
111 {
112   int row_start = -1;
113   int row_stop = -1;
114   int col_start = -1;
115   int col_stop = -1;
116
117   GtkBuilder *builder = ia->asst.builder;
118
119   struct sheet_spec_page *ssp = ia->sheet_spec;
120   struct casereader *creader = NULL;
121   struct dictionary *dict = NULL;
122
123   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
124   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
125   GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
126
127   gint num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (sheet_entry));
128
129   const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
130
131   if ( num < 1 )
132     num = 1;
133   
134   ssp->opts.sheet_name = NULL;
135   ssp->opts.cell_range = NULL;
136   ssp->opts.sheet_index = num;
137
138   if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
139     {
140       ssp->opts.cell_range = range;
141     }
142
143   ssp->sri.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
144   ssp->sri.asw = -1;
145
146   switch (ssp->spreadsheet->type)
147     {
148     case SPREADSHEET_ODS:
149       {
150         creader = ods_make_reader (ssp->spreadsheet, &ssp->sri, &ssp->opts);
151         dict = ssp->spreadsheet->dict;
152       }
153       break;
154     case SPREADSHEET_GNUMERIC:
155       {
156         creader = gnumeric_make_reader (ssp->spreadsheet, &ssp->sri, &ssp->opts);
157         dict = ssp->spreadsheet->dict;
158       }
159       break;
160     default:
161       g_assert_not_reached ();
162       break;
163     }
164
165   ssp->dict = dict;
166   ssp->reader = creader;
167
168   if (creader && dict)
169     {
170       update_assistant (ia);
171     }
172   else
173     {
174       GtkWidget * dialog = gtk_message_dialog_new (NULL,
175                               GTK_DIALOG_MODAL,
176                               GTK_MESSAGE_ERROR,
177                               GTK_BUTTONS_CLOSE,
178                               _("An error occurred reading the spreadsheet file."));
179
180       gtk_dialog_run (GTK_DIALOG (dialog));
181       gtk_widget_destroy (dialog);
182     }
183 }
184