Use the msg function to report errors wherever possible.
[pspp] / src / ui / gui / page-assistant.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 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/gtk.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/format-guesser.h"
31 #include "data/value-labels.h"
32 #include "language/data-io/data-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/line-reader.h"
37 #include "libpspp/message.h"
38 #include "ui/gui/checkbox-treeview.h"
39 #include "ui/gui/dialog-common.h"
40 #include "ui/gui/executor.h"
41 #include "ui/gui/helper.h"
42 #include "ui/gui/builder-wrapper.h"
43 #include "ui/gui/psppire-data-window.h"
44 #include "ui/gui/psppire-dialog.h"
45 #include "ui/gui/psppire-encoding-selector.h"
46 #include "ui/gui/psppire-empty-list-store.h"
47 #include "ui/gui/psppire-var-sheet.h"
48
49 #include "gl/intprops.h"
50 #include "gl/xalloc.h"
51
52 #include "gettext.h"
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
55
56 /* Assistant. */
57
58 static void close_assistant (struct import_assistant *, int response);
59 static void on_prepare (GtkAssistant *assistant, GtkWidget *page,
60                         struct import_assistant *);
61 static void on_cancel (GtkAssistant *assistant, struct import_assistant *);
62 static void on_close (GtkAssistant *assistant, struct import_assistant *);
63 static void on_paste (GtkButton *button, struct import_assistant *);
64 static void on_reset (GtkButton *button, struct import_assistant *);
65
66 /* Initializes IA's asst substructure.  PARENT_WINDOW must be the
67    window to use as the assistant window's parent.  */
68 struct import_assistant *
69 init_assistant (GtkWindow *parent_window)
70 {
71   struct import_assistant *ia = xzalloc (sizeof *ia);
72   struct assistant *a = &ia->asst;
73
74   a->builder = builder_new ("text-data-import.ui");
75   a->assistant = GTK_ASSISTANT (gtk_assistant_new ());
76
77   a->prop_renderer = gtk_cell_renderer_text_new ();
78   g_object_ref_sink (a->prop_renderer);
79   a->fixed_renderer = gtk_cell_renderer_text_new ();
80   g_object_ref_sink (a->fixed_renderer);
81   g_object_set (G_OBJECT (a->fixed_renderer),
82                 "family", "Monospace",
83                 (void *) NULL);
84
85   g_signal_connect (a->assistant, "prepare", G_CALLBACK (on_prepare), ia);
86   g_signal_connect (a->assistant, "cancel", G_CALLBACK (on_cancel), ia);
87   g_signal_connect (a->assistant, "close", G_CALLBACK (on_close), ia);
88   a->paste_button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
89   gtk_assistant_add_action_widget (a->assistant, a->paste_button);
90   g_signal_connect (a->paste_button, "clicked", G_CALLBACK (on_paste), ia);
91   a->reset_button = gtk_button_new_from_stock ("pspp-stock-reset");
92   gtk_assistant_add_action_widget (a->assistant, a->reset_button);
93   g_signal_connect (a->reset_button, "clicked", G_CALLBACK (on_reset), ia);
94   gtk_window_set_title (GTK_WINDOW (a->assistant),
95                         _("Importing Delimited Text Data"));
96   gtk_window_set_transient_for (GTK_WINDOW (a->assistant), parent_window);
97   gtk_window_set_icon_name (GTK_WINDOW (a->assistant), "pspp");
98
99
100   return ia;
101 }
102
103 /* Frees IA's asst substructure. */
104 void
105 destroy_assistant (struct import_assistant *ia)
106 {
107   struct assistant *a = &ia->asst;
108
109   g_object_unref (a->prop_renderer);
110   g_object_unref (a->fixed_renderer);
111   g_object_unref (a->builder);
112 }
113
114 /* Appends a page of the given TYPE, with PAGE as its content, to
115    the GtkAssistant encapsulated by IA.  Returns the GtkWidget
116    that represents the page. */
117 GtkWidget *
118 add_page_to_assistant (struct import_assistant *ia,
119                        GtkWidget *page, GtkAssistantPageType type)
120 {
121   const char *title;
122   char *title_copy;
123   GtkWidget *content;
124
125   title = gtk_window_get_title (GTK_WINDOW (page));
126   title_copy = xstrdup (title ? title : "");
127
128   content = gtk_bin_get_child (GTK_BIN (page));
129   assert (content);
130   g_object_ref (content);
131   gtk_container_remove (GTK_CONTAINER (page), content);
132
133   gtk_widget_destroy (page);
134
135   gtk_assistant_append_page (ia->asst.assistant, content);
136   gtk_assistant_set_page_type (ia->asst.assistant, content, type);
137   gtk_assistant_set_page_title (ia->asst.assistant, content, title_copy);
138   gtk_assistant_set_page_complete (ia->asst.assistant, content, true);
139
140   free (title_copy);
141
142   return content;
143 }
144
145 /* Called just before PAGE is displayed as the current page of
146    ASSISTANT, this updates IA content according to the new
147    page. */
148 static void
149 on_prepare (GtkAssistant *assistant, GtkWidget *page,
150             struct import_assistant *ia)
151 {
152   int pn = gtk_assistant_get_current_page (assistant);
153
154   gtk_widget_show (ia->asst.reset_button);
155   gtk_widget_hide (ia->asst.paste_button);
156
157   if ( ia->spreadsheet) 
158     {
159       if (pn == 0)
160         {
161           prepare_sheet_spec_page (ia);
162         }
163       else if (pn == 1)
164         {
165           post_sheet_spec_page (ia);
166           prepare_formats_page (ia);
167         }
168     }
169   else
170     {
171       switch (pn)
172         {
173         case 0:
174           reset_intro_page (ia);
175           break;
176         case 1:
177           reset_first_line_page (ia);
178           break;
179         case 2:
180           prepare_separators_page (ia);
181           reset_separators_page (ia);
182           break;
183         case 3:
184           prepare_formats_page (ia);
185           reset_formats_page (ia);
186           break;
187         }
188     }
189
190
191   if (gtk_assistant_get_page_type (assistant, page)
192       == GTK_ASSISTANT_PAGE_CONFIRM)
193     gtk_widget_grab_focus (assistant->apply);
194   else
195     gtk_widget_grab_focus (assistant->forward);
196 }
197
198 /* Called when the Cancel button in the assistant is clicked. */
199 static void
200 on_cancel (GtkAssistant *assistant, struct import_assistant *ia)
201 {
202   close_assistant (ia, GTK_RESPONSE_CANCEL);
203 }
204
205 /* Called when the Apply button on the last page of the assistant
206    is clicked. */
207 static void
208 on_close (GtkAssistant *assistant, struct import_assistant *ia)
209 {
210   close_assistant (ia, GTK_RESPONSE_APPLY);
211 }
212
213 /* Called when the Paste button on the last page of the assistant
214    is clicked. */
215 static void
216 on_paste (GtkButton *button, struct import_assistant *ia)
217 {
218   close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
219 }
220
221 /* Called when the Reset button is clicked. */
222 static void
223 on_reset (GtkButton *button, struct import_assistant *ia)
224 {
225   gint pn = gtk_assistant_get_current_page (ia->asst.assistant);
226   
227   if ( ia->spreadsheet) 
228     {
229       switch (pn)
230         {
231         case 0:
232           reset_sheet_spec_page (ia);
233          break;
234         case 1:
235           reset_formats_page (ia);
236           break;
237         }
238     }
239   else
240     {
241       switch (pn)
242         {
243         case 0:
244           reset_intro_page (ia);
245           break;
246         case 1:
247           reset_first_line_page (ia);
248           break;
249         case 2:
250           reset_separators_page (ia);
251           break;
252         case 3:
253           reset_formats_page (ia);
254           break;
255         }
256     }
257 }
258
259 /* Causes the assistant to close, returning RESPONSE for
260    interpretation by text_data_import_assistant. */
261 static void
262 close_assistant (struct import_assistant *ia, int response)
263 {
264   ia->asst.response = response;
265   /*  Use our loop_done variable until we find out
266       why      g_main_loop_quit (ia->asst.main_loop); doesn't work.
267   */
268   ia->asst.loop_done = true;
269   gtk_widget_hide (GTK_WIDGET (ia->asst.assistant));
270 }
271