Replace use of deprecated GTimeVal with GDateTime
[pspp] / src / ui / gui / psppire-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009, 2010, 2011, 2013, 2014, 2020 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 #include <gl/relocatable.h>
19
20 #include "psppire-window.h"
21 #include "psppire-window-base.h"
22
23 #include <gtk/gtk.h>
24
25 #include <stdlib.h>
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 #include "data/any-reader.h"
32 #include "data/file-handle-def.h"
33 #include "data/dataset.h"
34 #include "libpspp/version.h"
35 #include "output/group-item.h"
36 #include "output/pivot-table.h"
37 #include "output/spv/spv.h"
38 #include "output/spv/spv-output.h"
39 #include "output/spv/spv-select.h"
40
41 #include "helper.h"
42 #include "psppire-data-window.h"
43 #include "psppire-encoding-selector.h"
44 #include "psppire-syntax-window.h"
45 #include "psppire-window-register.h"
46
47 static void psppire_window_base_init     (PsppireWindowClass *class);
48 static void psppire_window_class_init    (PsppireWindowClass *class);
49 static void psppire_window_init          (PsppireWindow      *window);
50
51
52 static GObjectClass *parent_class;
53
54 GType
55 psppire_window_get_type (void)
56 {
57   static GType psppire_window_type = 0;
58
59   if (!psppire_window_type)
60     {
61       static const GTypeInfo psppire_window_info =
62       {
63         sizeof (PsppireWindowClass),
64         (GBaseInitFunc) psppire_window_base_init,
65         (GBaseFinalizeFunc) NULL,
66         (GClassInitFunc) psppire_window_class_init,
67         (GClassFinalizeFunc) NULL,
68         NULL,
69         sizeof (PsppireWindow),
70         0,
71         (GInstanceInitFunc) psppire_window_init,
72       };
73
74       psppire_window_type =
75         g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
76                                 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
77     }
78
79   return psppire_window_type;
80 }
81
82
83 /* Properties */
84 enum
85 {
86   PROP_0,
87   PROP_FILENAME,
88   PROP_DESCRIPTION,
89   PROP_ID
90 };
91
92
93 static void
94 psppire_window_set_title (PsppireWindow *window)
95 {
96   GString *title = g_string_sized_new (80);
97
98   if (window->edited != NULL)
99     g_string_append_c (title, '*');
100
101   if (window->basename || window->id)
102     {
103       if (window->basename)
104         g_string_append_printf (title, "%s ", window->basename);
105
106       if (window->id)
107         g_string_append_printf (title, "[%s] ", window->id);
108
109       g_string_append_unichar (title, 0x2014); /* em dash */
110       g_string_append_c (title, ' '); /* em dash */
111     }
112
113   g_string_append_printf (title, "PSPPIRE %s", window->description);
114
115   int minor = 1;
116   sscanf (bare_version, "%*d.%d.%*d", &minor);
117   if (minor % 2)
118     g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
119
120   gtk_window_set_title (GTK_WINDOW (window), title->str);
121
122   g_string_free (title, TRUE);
123 }
124
125 static void
126 psppire_window_update_list_name (PsppireWindow *window)
127 {
128   PsppireWindowRegister *reg = psppire_window_register_new ();
129   GString *candidate = g_string_sized_new (80);
130   int n;
131
132   n = 1;
133   do
134     {
135       /* Compose a name. */
136       g_string_truncate (candidate, 0);
137       if (window->filename)
138         {
139           gchar *display_filename = g_filename_display_name (window->filename);
140           g_string_append (candidate, display_filename);
141           g_free (display_filename);
142
143           if (window->id)
144             g_string_append_printf (candidate, " [%s]", window->id);
145         }
146       else if (window->id)
147         g_string_append_printf (candidate, "[%s]", window->id);
148       else
149         g_string_append (candidate, window->description);
150
151       if (n++ > 1)
152         g_string_append_printf (candidate, " #%d", n);
153
154       if (window->list_name && !strcmp (candidate->str, window->list_name))
155         {
156           /* Keep the existing name. */
157           g_string_free (candidate, TRUE);
158           return;
159         }
160     }
161   while (psppire_window_register_lookup (reg, candidate->str));
162
163   if (window->list_name)
164     psppire_window_register_remove (reg, window->list_name);
165
166   g_free (window->list_name);
167   window->list_name = g_string_free (candidate, FALSE);
168
169   psppire_window_register_insert (reg, window, window->list_name);
170 }
171
172 static void
173 psppire_window_name_changed (PsppireWindow *window)
174 {
175   psppire_window_set_title (window);
176   psppire_window_update_list_name (window);
177 }
178
179 static void
180 psppire_window_set_property (GObject         *object,
181                              guint            prop_id,
182                              const GValue    *value,
183                              GParamSpec      *pspec)
184 {
185   PsppireWindow *window = PSPPIRE_WINDOW (object);
186
187   switch (prop_id)
188     {
189     case PROP_DESCRIPTION:
190       g_free (window->description);
191       window->description = g_value_dup_string (value);
192       psppire_window_set_title (window);
193       break;
194     case PROP_FILENAME:
195       g_free (window->filename);
196       window->filename = g_value_dup_string (value);
197       g_free (window->basename);
198       window->basename = (window->filename
199                           ? g_filename_display_basename (window->filename)
200                           : NULL);
201       psppire_window_name_changed (window);
202       break;
203     case PROP_ID:
204       g_free (window->id);
205       window->id = g_value_dup_string (value);
206       psppire_window_name_changed (window);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211     };
212 }
213
214
215 static void
216 psppire_window_get_property (GObject         *object,
217                              guint            prop_id,
218                              GValue          *value,
219                              GParamSpec      *pspec)
220 {
221   PsppireWindow *window = PSPPIRE_WINDOW (object);
222
223   switch (prop_id)
224     {
225     case PROP_FILENAME:
226       g_value_set_string (value, window->filename);
227       break;
228     case PROP_DESCRIPTION:
229       g_value_set_string (value, window->description);
230       break;
231     case PROP_ID:
232       g_value_set_string (value, window->id);
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236       break;
237     };
238 }
239
240
241 static void
242 psppire_window_finalize (GObject *object)
243 {
244   PsppireWindow *window = PSPPIRE_WINDOW (object);
245
246   PsppireWindowRegister *reg = psppire_window_register_new ();
247
248   if (window->edited)
249     g_date_time_unref (window->edited);
250
251   g_signal_handler_disconnect (reg, window->remove_handler);
252   g_signal_handler_disconnect (reg, window->insert_handler);
253   psppire_window_register_remove (reg, window->list_name);
254   g_free (window->filename);
255   g_free (window->basename);
256   g_free (window->id);
257   g_free (window->description);
258   g_free (window->list_name);
259
260   g_hash_table_destroy (window->menuitem_table);
261
262   if (G_OBJECT_CLASS (parent_class)->finalize)
263     G_OBJECT_CLASS (parent_class)->finalize (object);
264 }
265
266 static void
267 psppire_window_class_init (PsppireWindowClass *class)
268 {
269   GObjectClass *object_class = G_OBJECT_CLASS (class);
270
271   GParamSpec *description_spec =
272     null_if_empty_param ("description",
273                        "Description",
274                        "A string describing the usage of the window",
275                          NULL, /*Should be overridden by derived classes */
276                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
277
278   GParamSpec *filename_spec =
279     null_if_empty_param ("filename",
280                        "File name",
281                        "The name of the file associated with this window, if any",
282                          NULL,
283                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
284
285   GParamSpec *id_spec =
286     null_if_empty_param ("id",
287                          "Identifier",
288                          "The PSPP language identifier for the data associated "
289                          "with this window (e.g. dataset name)",
290                          NULL,
291                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
292
293   object_class->set_property = psppire_window_set_property;
294   object_class->get_property = psppire_window_get_property;
295
296   g_object_class_install_property (object_class,
297                                    PROP_DESCRIPTION,
298                                    description_spec);
299
300   g_object_class_install_property (object_class,
301                                    PROP_FILENAME,
302                                    filename_spec);
303
304   g_object_class_install_property (object_class,
305                                    PROP_ID,
306                                    id_spec);
307
308   parent_class = g_type_class_peek_parent (class);
309 }
310
311
312 static void
313 psppire_window_base_init (PsppireWindowClass *class)
314 {
315   GObjectClass *object_class = G_OBJECT_CLASS (class);
316
317   object_class->finalize = psppire_window_finalize;
318 }
319
320
321
322 static void
323 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
324 {
325   gchar *filename;
326   GtkWidget *item;
327   filename = g_filename_display_name (key);
328   item = gtk_check_menu_item_new_with_label (filename);
329   g_free (filename);
330
331   g_hash_table_insert (window->menuitem_table, key, item);
332 }
333
334 static void
335 insert_item (gpointer key, gpointer value, gpointer data)
336 {
337   PsppireWindow *window = PSPPIRE_WINDOW (data);
338
339   if (NULL != g_hash_table_lookup (window->menuitem_table, key))
340     return;
341
342   insert_menuitem_into_menu (window, key);
343 }
344
345 /* Insert a new item into the window menu */
346 static void
347 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
348 {
349   PsppireWindow *window = PSPPIRE_WINDOW (data);
350
351   insert_menuitem_into_menu (window, (gpointer) key);
352 }
353
354
355 static void
356 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
357 {
358   PsppireWindow *window = PSPPIRE_WINDOW (data);
359   g_hash_table_remove (window->menuitem_table, key);
360 }
361
362 static void
363 insert_existing_items (PsppireWindow *window)
364 {
365   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
366 }
367
368
369 static gboolean
370 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
371 {
372   PsppireWindowRegister *reg = psppire_window_register_new ();
373
374   if (w->edited != NULL)
375     {
376       gint response = psppire_window_query_save (w);
377
378       switch (response)
379         {
380         default:
381         case GTK_RESPONSE_CANCEL:
382           return TRUE;
383           break;
384         case GTK_RESPONSE_APPLY:
385           psppire_window_save (w);
386           if (w->edited != NULL)
387             {
388               /* Save failed, or user exited Save As dialog with Cancel. */
389               return TRUE;
390             }
391           break;
392         case GTK_RESPONSE_REJECT:
393           break;
394         }
395     }
396
397   if (1 == psppire_window_register_n_items (reg))
398     gtk_main_quit ();
399
400   return FALSE;
401 }
402
403
404 static void
405 psppire_window_init (PsppireWindow *window)
406 {
407   window->filename = NULL;
408   window->basename = NULL;
409   window->id = NULL;
410   window->description = NULL;
411   window->list_name = NULL;
412   window->edited = NULL;
413
414   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
415
416
417   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
418
419   window->insert_handler = g_signal_connect (psppire_window_register_new (),
420                                              "inserted",
421                                              G_CALLBACK (insert_menuitem),
422                                              window);
423
424   window->remove_handler = g_signal_connect (psppire_window_register_new (),
425                                              "removed",
426                                              G_CALLBACK (remove_menuitem),
427                                              window);
428
429   window->added_separator = FALSE;
430
431   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
432
433   g_object_set (window, "icon-name", "pspp", NULL);
434 }
435
436 /*
437    Ask the user if the buffer should be saved.
438    Return the response.
439 */
440 gint
441 psppire_window_query_save (PsppireWindow *se)
442 {
443   gint response;
444   GtkWidget *dialog;
445   GtkWidget *cancel_button;
446
447   gchar *description;
448
449   GDateTime *now = g_date_time_new_now_utc ();
450   GTimeSpan timespan = g_date_time_difference (now, se->edited);
451   g_date_time_unref (now);
452
453   if (se->filename)
454     description = g_filename_display_basename (se->filename);
455   else if (se->id)
456     description = g_strdup (se->id);
457   else
458     description = g_strdup (se->description);
459   dialog =
460     gtk_message_dialog_new (GTK_WINDOW (se),
461                             GTK_DIALOG_MODAL,
462                             GTK_MESSAGE_WARNING,
463                             GTK_BUTTONS_NONE,
464                             _("Save the changes to `%s' before closing?"),
465                             description);
466   g_free (description);
467
468   g_object_set (dialog, "icon-name", "pspp", NULL);
469
470   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
471                                             _("If you don't save, changes from the last %ld seconds will be permanently lost."),
472                                             timespan / G_TIME_SPAN_SECOND);
473
474   gtk_dialog_add_button  (GTK_DIALOG (dialog),
475                           _("Close _without saving"),
476                           GTK_RESPONSE_REJECT);
477
478   cancel_button = gtk_dialog_add_button  (GTK_DIALOG (dialog),
479                                           _("Cancel"),
480                                           GTK_RESPONSE_CANCEL);
481
482   gtk_dialog_add_button  (GTK_DIALOG (dialog),
483                           _("Save"),
484                           GTK_RESPONSE_APPLY);
485
486   gtk_widget_grab_focus (cancel_button);
487
488   response = gtk_dialog_run (GTK_DIALOG (dialog));
489
490   gtk_widget_destroy (dialog);
491
492   return response;
493 }
494
495
496 /* The return value is encoded in the glib filename encoding. */
497 const gchar *
498 psppire_window_get_filename (PsppireWindow *w)
499 {
500   return w->filename;
501 }
502
503
504 /* FILENAME must be encoded in the glib filename encoding. */
505 void
506 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
507 {
508   g_object_set (w, "filename", filename, NULL);
509 }
510
511 void
512 psppire_window_set_unsaved (PsppireWindow *w)
513 {
514   if (w->edited == NULL)
515     w->edited = g_date_time_new_now_utc ();
516
517   psppire_window_set_title (w);
518 }
519
520 gboolean
521 psppire_window_get_unsaved (PsppireWindow *w)
522 {
523   return w->edited != NULL;
524 }
525
526
527 \f
528
529
530 static void
531 minimise_window (gpointer key, gpointer value, gpointer data)
532 {
533   gtk_window_iconify (GTK_WINDOW (value));
534 }
535
536
537 void
538 psppire_window_minimise_all (void)
539 {
540   PsppireWindowRegister *reg = psppire_window_register_new ();
541
542   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
543 }
544
545
546 \f
547
548 GType
549 psppire_window_model_get_type (void)
550 {
551   static GType window_model_type = 0;
552
553   if (! window_model_type)
554     {
555       static const GTypeInfo window_model_info =
556       {
557         sizeof (PsppireWindowIface), /* class_size */
558         NULL,           /* base_init */
559         NULL,           /* base_finalize */
560         NULL,
561         NULL,           /* class_finalize */
562         NULL,           /* class_data */
563         0,
564         0,              /* n_preallocs */
565         NULL
566       };
567
568       window_model_type =
569         g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
570                                 &window_model_info, 0);
571
572       g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
573     }
574
575   return window_model_type;
576 }
577
578
579 void
580 psppire_window_save (PsppireWindow *w)
581 {
582   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
583
584   g_assert (i);
585   g_return_if_fail (i->save);
586
587   if (w->filename == NULL)
588     psppire_window_save_as (w);
589   else
590     {
591       i->save (w);
592       if (w->edited)
593         g_date_time_unref (w->edited);
594       w->edited = NULL;
595
596       psppire_window_set_title (w);
597     }
598 }
599
600 void
601 psppire_window_save_as (PsppireWindow *w)
602 {
603   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
604   gchar *old_filename;
605
606   g_assert (i);
607   g_return_if_fail (i->pick_filename);
608
609   old_filename = w->filename;
610   w->filename = NULL;
611
612   i->pick_filename (w);
613   if (w->filename == NULL)
614     w->filename = old_filename;
615   else
616     {
617       g_free (old_filename);
618       psppire_window_save (w);
619     }
620 }
621
622 static void delete_recent (const char *file_name);
623
624 gboolean
625 psppire_window_load (PsppireWindow *w, const gchar *file,
626                      const gchar *encoding, gpointer hint)
627 {
628   gboolean ok;
629   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
630
631   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
632
633   g_assert (i);
634
635   g_return_val_if_fail (i->load, FALSE);
636
637   ok = i->load (w, file, encoding, hint);
638
639   if (ok)
640     {
641       psppire_window_set_filename (w, file);
642       if (w->edited)
643         g_date_time_unref (w->edited);
644       w->edited = NULL;
645     }
646   else
647     delete_recent (file);
648
649   return ok;
650 }
651
652
653 GtkWidget *
654 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
655 {
656   GtkFileFilter *filter = gtk_file_filter_new ();
657   GtkWidget *dialog =
658     gtk_file_chooser_dialog_new (_("Open"),
659                                  GTK_WINDOW (toplevel),
660                                  GTK_FILE_CHOOSER_ACTION_OPEN,
661                                  _("Cancel"), GTK_RESPONSE_CANCEL,
662                                  _("Open"), GTK_RESPONSE_ACCEPT,
663                                  NULL);
664
665   g_object_set (dialog, "local-only", FALSE, NULL);
666
667   gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
668   gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
669   gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
670   gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
671   gtk_file_filter_add_pattern (filter, "*.zsav");
672   gtk_file_filter_add_pattern (filter, "*.sps");
673   gtk_file_filter_add_pattern (filter, "*.SPS");
674   gtk_file_filter_add_pattern (filter, "*.spv");
675   gtk_file_filter_add_pattern (filter, "*.SPV");
676   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
677
678   filter = gtk_file_filter_new ();
679   gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
680   gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
681   gtk_file_filter_add_pattern (filter, "*.zsav");
682   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
683
684   filter = gtk_file_filter_new ();
685   gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
686   gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
687   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
688
689   filter = gtk_file_filter_new ();
690   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
691   gtk_file_filter_add_pattern (filter, "*.sps");
692   gtk_file_filter_add_pattern (filter, "*.SPS");
693   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
694
695   filter = gtk_file_filter_new ();
696   gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
697   gtk_file_filter_add_pattern (filter, "*.spv");
698   gtk_file_filter_add_pattern (filter, "*.SPV");
699   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
700
701   filter = gtk_file_filter_new ();
702   gtk_file_filter_set_name (filter, _("All Files"));
703   gtk_file_filter_add_pattern (filter, "*");
704   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
705
706   if (toplevel->filename)
707     {
708       const gchar *filename = toplevel->filename;
709       gchar *dir_name;
710
711       if (! g_path_is_absolute (filename))
712         {
713           gchar *path =
714             g_build_filename (g_get_current_dir (), filename, NULL);
715           dir_name = g_path_get_dirname (path);
716           g_free (path);
717         }
718       else
719         {
720           dir_name = g_path_get_dirname (filename);
721         }
722       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
723                                            dir_name);
724       free (dir_name);
725     }
726
727     gtk_file_chooser_set_extra_widget (
728       GTK_FILE_CHOOSER (dialog),
729       psppire_encoding_selector_new ("Auto", true));
730
731   return dialog;
732 }
733
734 struct item_path
735   {
736     const struct spv_item **nodes;
737     size_t n;
738
739 #define N_STUB 10
740     const struct spv_item *stub[N_STUB];
741   };
742
743 static void
744 swap_nodes (const struct spv_item **a, const struct spv_item **b)
745 {
746   const struct spv_item *tmp = *a;
747   *a = *b;
748   *b = tmp;
749 }
750
751 static void
752 get_path (const struct spv_item *item, struct item_path *path)
753 {
754   size_t allocated = 10;
755   path->nodes = path->stub;
756   path->n = 0;
757
758   while (item)
759     {
760       if (path->n >= allocated)
761         {
762           if (path->nodes == path->stub)
763             path->nodes = xmemdup (path->stub, sizeof path->stub);
764           path->nodes = x2nrealloc (path->nodes, &allocated,
765                                     sizeof *path->nodes);
766         }
767       path->nodes[path->n++] = item;
768       item = item->parent;
769     }
770
771   for (size_t i = 0; i < path->n / 2; i++)
772     swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
773 }
774
775 static void
776 free_path (struct item_path *path)
777 {
778   if (path && path->nodes != path->stub)
779     free (path->nodes);
780 }
781
782 static void
783 dump_heading_transition (const struct spv_item *old,
784                          const struct spv_item *new)
785 {
786   if (old == new)
787     return;
788
789   struct item_path old_path, new_path;
790   get_path (old, &old_path);
791   get_path (new, &new_path);
792
793   size_t common = 0;
794   for (; common < old_path.n && common < new_path.n; common++)
795     if (old_path.nodes[common] != new_path.nodes[common])
796       break;
797
798   for (size_t i = common; i < old_path.n; i++)
799     group_close_item_submit (group_close_item_create ());
800   for (size_t i = common; i < new_path.n; i++)
801     group_open_item_submit (group_open_item_create (
802                               new_path.nodes[i]->command_id));
803
804   free_path (&old_path);
805   free_path (&new_path);
806 }
807
808 void
809 read_spv_file (const char *filename)
810 {
811   struct spv_reader *spv;
812   char *error = spv_open (filename, &spv);
813   if (error)
814     {
815       /* XXX */
816       fprintf (stderr, "%s\n", error);
817       return;
818     }
819
820   struct spv_item **items;
821   size_t n_items;
822   spv_select (spv, NULL, 0, &items, &n_items);
823   struct spv_item *prev_heading = spv_get_root (spv);
824   for (size_t i = 0; i < n_items; i++)
825     {
826       struct spv_item *heading
827         = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
828       dump_heading_transition (prev_heading, heading);
829       if (items[i]->type == SPV_ITEM_TEXT)
830         spv_text_submit (items[i]);
831       else if (items[i]->type == SPV_ITEM_TABLE)
832         pivot_table_submit (spv_item_get_table (items[i]));
833       prev_heading = heading;
834     }
835   dump_heading_transition (prev_heading, spv_get_root (spv));
836   free (items);
837   spv_close (spv);
838 }
839
840 /* Callback for the file_open action.
841    Prompts for a filename and opens it */
842 void
843 psppire_window_open (PsppireWindow *de)
844 {
845   GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
846
847   gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
848
849   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
850     {
851     case GTK_RESPONSE_ACCEPT:
852       {
853         gchar *name =
854           gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
855
856         const gchar **cs = NULL;
857         g_get_filename_charsets (&cs);
858
859         gchar *encoding = psppire_encoding_selector_get_encoding (
860           gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
861
862         struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
863
864         int retval = any_reader_detect (fh, NULL);
865         if (retval == 1)
866           open_data_window (de, name, encoding, NULL);
867         else if (retval == 0)
868           {
869             char *error = spv_detect (name);
870             if (!error)
871               read_spv_file (name);
872             else
873               {
874                 free (error);
875                 open_syntax_window (name, encoding);
876               }
877           }
878
879         g_free (encoding);
880         fh_unref (fh);
881         g_free (name);
882       }
883       break;
884     default:
885       break;
886     }
887
888   gtk_widget_destroy (dialog);
889 }
890
891
892 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
893    with associated MIME_TYPE.  If it's already in the list, it moves it to the
894    top. */
895 void
896 add_most_recent (const char *file_name,
897                  const char *mime_type, const char *encoding)
898 {
899   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
900   if (uri)
901     {
902       GtkRecentData recent_data;
903       gchar *full_mime_type;
904
905       if (encoding && encoding[0])
906         full_mime_type = g_strdup_printf ("%s; charset=%s",
907                                           mime_type, encoding);
908       else
909         full_mime_type = g_strdup (mime_type);
910
911       recent_data.display_name = NULL;
912       recent_data.description = NULL;
913       recent_data.mime_type = full_mime_type;
914       recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
915       recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
916       recent_data.groups = NULL;
917       recent_data.is_private = FALSE;
918
919       gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
920                                    uri, &recent_data);
921
922       g_free (recent_data.app_exec);
923       g_free (full_mime_type);
924     }
925
926   g_free (uri);
927 }
928
929
930
931 /*
932    If FILE_NAME exists in the recent list, then  delete it.
933  */
934 static void
935 delete_recent (const char *file_name)
936 {
937   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
938
939   if (uri)
940     gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);
941
942   g_free (uri);
943 }
944