gui: Allow File|Open to select an encoding for system files. 20140204030509/pspp 20140205030510/pspp 20140206030503/pspp 20140207030506/pspp 20140208030509/pspp
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 4 Feb 2014 06:56:00 +0000 (22:56 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 4 Feb 2014 06:56:41 +0000 (22:56 -0800)
This should allow old files in unexpected encodings to be more easily read.

Problem reported by Przemek Powalko <pp.alter.ego@gmail.com>.

NEWS
src/ui/gui/psppire-data-window.c
src/ui/gui/psppire-data-window.h
src/ui/gui/psppire-syntax-window.c
src/ui/gui/psppire-window.c
src/ui/gui/psppire-window.h
src/ui/gui/psppire.c
src/ui/gui/text-data-import-dialog.c

diff --git a/NEWS b/NEWS
index e6505528500428dcfffbd08fbd7321239e44efb9..4e7581c2434e83fcc114d14c836b849562f369e9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,14 @@ Changes since 0.8.2:
  * The SYSFILE INFO command now accepts an ENCODING subcommand to
    specify the character encoding of string data in the system file.
 
+ * PSPPIRE graphical user interface improvements:
+
+   - File|Open now allows an encoding to be selected for opening
+     system files.
+
+   - File|Display Data File Information|External File... now allows an
+     encoding to be selected.
+
 Changes from 0.8.1 to 0.8.2:
 
  * Charts are now rendered with colours from the Tango palette instead
index 35ffdb840f403836b2376a168100939331ef5541..28ec605205106c9f8e4ee7e3924a66279572ef4f 100644 (file)
@@ -332,7 +332,8 @@ name_has_suffix (const gchar *name)
 }
 
 static gboolean
-load_file (PsppireWindow *de, const gchar *file_name, gpointer syn)
+load_file (PsppireWindow *de, const gchar *file_name, const char *encoding,
+           gpointer syn)
 {
   const char *mime_type = NULL;
   gchar *syntax = NULL;
@@ -352,8 +353,12 @@ load_file (PsppireWindow *de, const gchar *file_name, gpointer syn)
       syntax_gen_string (&filename, ss_cstr (utf8_file_name));
       
       g_free (utf8_file_name);
-      
-      syntax = g_strdup_printf ("GET FILE=%s.", ds_cstr (&filename));
+
+      if (encoding && encoding[0])
+        syntax = g_strdup_printf ("GET FILE=%s ENCODING='%s'.",
+                                  ds_cstr (&filename), encoding);
+      else
+        syntax = g_strdup_printf ("GET FILE=%s.", ds_cstr (&filename));
       ds_destroy (&filename);
     }
   else
@@ -372,7 +377,7 @@ load_file (PsppireWindow *de, const gchar *file_name, gpointer syn)
       else if (name_has_sav_suffix (file_name))
        mime_type = "application/x-spss-sav";
       
-      add_most_recent (file_name, mime_type);
+      add_most_recent (file_name, mime_type, encoding);
     }
 
   return ok;
@@ -762,7 +767,7 @@ on_recent_data_select (GtkMenuShell *menushell,
 
   g_free (uri);
 
-  open_data_window (window, file, NULL);
+  open_data_window (window, file, NULL, NULL);
 
   g_free (file);
 }
@@ -825,7 +830,7 @@ on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
 
   free (encoding);
 
-  if ( psppire_window_load (PSPPIRE_WINDOW (se), file, NULL) ) 
+  if ( psppire_window_load (PSPPIRE_WINDOW (se), file, encoding, NULL) ) 
     gtk_widget_show (se);
   else
     gtk_widget_destroy (se);
@@ -1368,7 +1373,8 @@ create_data_window (void)
 }
 
 void
-open_data_window (PsppireWindow *victim, const char *file_name, gpointer hint)
+open_data_window (PsppireWindow *victim, const char *file_name,
+                  const char *encoding, gpointer hint)
 {
   GtkWidget *window;
 
@@ -1381,7 +1387,7 @@ open_data_window (PsppireWindow *victim, const char *file_name, gpointer hint)
   else
     window = psppire_data_window_new (NULL);
 
-  psppire_window_load (PSPPIRE_WINDOW (window), file_name, hint);
+  psppire_window_load (PSPPIRE_WINDOW (window), file_name, encoding, hint);
   gtk_widget_show_all (window);
 }
 
index a0e80fc0aba55903af81e3fb029c8fff01e069b1..a578501fc2518a52f9b9d9bbf3c3a425173ac91a 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008, 2010, 2011, 2012, 2013  Free Software Foundation
+   Copyright (C) 2008, 2010, 2011, 2012, 2013, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -101,7 +101,8 @@ PsppireDataWindow *psppire_data_window_for_data_store (PsppireDataStore *);
 
 bool psppire_data_window_is_empty (PsppireDataWindow *);
 void create_data_window (void);
-void open_data_window (PsppireWindow *victim, const char *file_name, gpointer hint);
+void open_data_window (PsppireWindow *victim, const char *file_name,
+                       const char *encoding, gpointer hint);
 
 G_END_DECLS
 
index 854ff700bd0dd81692c3dc4ffbd7657c78a286a7..35f57e587b4cc950f30eaf8ef40683426e7b46e4 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008, 2009, 2010, 2011, 2012  Free Software Foundation
+   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -664,12 +664,13 @@ on_quit (GtkMenuItem *menuitem, gpointer    user_data)
 
 
 static void
-load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
+load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
+                             const gchar *encoding)
 {
   gboolean ok;
 
   gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
-  ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, NULL);
+  ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
   gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
 
   if (ok )
@@ -688,10 +689,10 @@ create_syntax_window (void)
 void
 open_syntax_window (const char *file_name, const gchar *encoding)
 {
-  GtkWidget *se = psppire_syntax_window_new (encoding);
+  GtkWidget *se = psppire_syntax_window_new (NULL);
 
   if ( file_name)
-    load_and_show_syntax_window (se, file_name);
+    load_and_show_syntax_window (se, file_name, encoding);
 }
 
 
@@ -944,7 +945,8 @@ error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
   Loads the buffer from the file called FILENAME
 */
 gboolean
-syntax_load (PsppireWindow *window, const gchar *filename, gpointer not_used)
+syntax_load (PsppireWindow *window, const gchar *filename,
+             const gchar *encoding, gpointer not_used)
 {
   GError *err = NULL;
   gchar *text_locale = NULL;
@@ -954,8 +956,6 @@ syntax_load (PsppireWindow *window, const gchar *filename, gpointer not_used)
   GtkTextIter iter;
   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
-  gchar *encoding;
-  char *mime_type;
 
   /* FIXME: What if it's a very big file ? */
   if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
@@ -965,13 +965,24 @@ syntax_load (PsppireWindow *window, const gchar *filename, gpointer not_used)
       return FALSE;
     }
 
-  /* Determine the file's encoding and update sw->encoding.  (The ordering is
-     important here because encoding_guess_whole_file() often returns its
-     argument instead of a copy of it.) */
-  encoding = g_strdup (encoding_guess_whole_file (sw->encoding, text_locale,
-                                                  len_locale));
-  g_free (sw->encoding);
-  sw->encoding = encoding;
+  if (!encoding || !encoding[0])
+    {
+      /* Determine the file's encoding and update sw->encoding.  (The ordering
+         is important here because encoding_guess_whole_file() often returns
+         its argument instead of a copy of it.) */
+      char *guessed_encoding;
+
+      guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
+                                                              text_locale,
+                                                              len_locale));
+      g_free (sw->encoding);
+      sw->encoding = guessed_encoding;
+    }
+  else
+    {
+      g_free (sw->encoding);
+      sw->encoding = g_strdup (encoding);
+    }
 
   text_utf8 = recode_substring_pool ("UTF-8", encoding,
                                      ss_buffer (text_locale, len_locale),
@@ -993,9 +1004,7 @@ syntax_load (PsppireWindow *window, const gchar *filename, gpointer not_used)
 
   free (text_utf8);
 
-  mime_type = xasprintf ("text/x-spss-syntax; charset=%s", sw->encoding);
-  add_most_recent (filename, mime_type);
-  free (mime_type);
+  add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
 
   return TRUE;
 }
index 84211133de15190e100671276df48aa3b57faebe..9826b702d9f4e005da364c0244a880f58655e36b 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2009, 2010, 2011, 2013  Free Software Foundation
+   Copyright (C) 2009, 2010, 2011, 2013, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -660,7 +660,8 @@ psppire_window_save_as (PsppireWindow *w)
 static void delete_recent (const char *file_name);
 
 gboolean
-psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint)
+psppire_window_load (PsppireWindow *w, const gchar *file,
+                     const gchar *encoding, gpointer hint)
 {
   gboolean ok;
   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
@@ -671,7 +672,7 @@ psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint)
 
   g_return_val_if_fail (i->load, FALSE);
 
-  ok = i->load (w, file, hint);
+  ok = i->load (w, file, encoding, hint);
 
   if ( ok )
     {
@@ -685,27 +686,6 @@ psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint)
 }
 
 
-static void
-on_selection_changed (GtkFileChooser *chooser, GtkWidget *encoding_selector)
-{
-  const gchar *sysname;
-
-  const gchar *name = gtk_file_chooser_get_filename (chooser);
-
-  if ( NULL == name )
-    return;
-
-  sysname = convert_glib_filename_to_system_filename (name, NULL);
-
-  if ( ! fn_exists (sysname))
-    {
-      gtk_widget_set_sensitive (encoding_selector, FALSE);
-      return;
-    }
-
-  gtk_widget_set_sensitive (encoding_selector, ANY_NO == any_reader_may_open (sysname));
-}
-
 GtkWidget *
 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
 {
@@ -771,17 +751,9 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
       free (dir_name);
     }
 
-
-  {
-    GtkWidget *encoding_selector =  psppire_encoding_selector_new ("Auto", true);
-
-    gtk_widget_set_sensitive (encoding_selector, FALSE);
-
-    g_signal_connect (dialog, "selection-changed", G_CALLBACK (on_selection_changed),
-                     encoding_selector);
-
-    gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), encoding_selector);
-  }
+    gtk_file_chooser_set_extra_widget (
+      GTK_FILE_CHOOSER (dialog),
+      psppire_encoding_selector_new ("Auto", true));
 
   return dialog;
 }
@@ -807,7 +779,7 @@ psppire_window_open (PsppireWindow *de)
 
        enum detect_result res = any_reader_may_open (sysname);
        if (ANY_YES == res)
-          open_data_window (de, name, NULL);
+          open_data_window (de, name, encoding, NULL);
        else if (ANY_NO == res)
          open_syntax_window (name, encoding);
 
@@ -828,17 +800,24 @@ psppire_window_open (PsppireWindow *de)
    with associated MIME_TYPE.  If it's already in the list, it moves it to the
    top. */
 void
-add_most_recent (const char *file_name, const char *mime_type)
+add_most_recent (const char *file_name,
+                 const char *mime_type, const char *encoding)
 {
   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
-
   if ( uri )
     {
       GtkRecentData recent_data;
+      gchar *full_mime_type;
+
+      if (encoding && encoding[0])
+        full_mime_type = g_strdup_printf ("%s; charset=%s",
+                                          mime_type, encoding);
+      else
+        full_mime_type = g_strdup (mime_type);
 
       recent_data.display_name = NULL;
       recent_data.description = NULL;
-      recent_data.mime_type = CONST_CAST (gchar *, mime_type);
+      recent_data.mime_type = full_mime_type;
       recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
       recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
       recent_data.groups = NULL;
@@ -848,6 +827,7 @@ add_most_recent (const char *file_name, const char *mime_type)
                                    uri, &recent_data);
 
       g_free (recent_data.app_exec);
+      g_free (full_mime_type);
     }
 
   g_free (uri);
index 7831f53d2776dd8ccfb505ac32949324a1e43c34..0e57b4d78de57965160e67a9802a758649983691 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008, 2009, 2010, 2011, 2013  Free Software Foundation
+   Copyright (C) 2008, 2009, 2010, 2011, 2013, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -91,7 +91,8 @@ struct _PsppireWindowIface
 
   void (*save) (PsppireWindow *w);
   void (*pick_filename) (PsppireWindow *);
-  gboolean (*load) (PsppireWindow *w, const gchar *, gpointer hint);
+  gboolean (*load) (PsppireWindow *w, const gchar *filename,
+                    const gchar *encoding, gpointer hint);
 };
 
 
@@ -112,11 +113,13 @@ gint psppire_window_query_save (PsppireWindow *);
 
 void psppire_window_save (PsppireWindow *w);
 void psppire_window_save_as (PsppireWindow *w);
-gboolean psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint);
+gboolean psppire_window_load (PsppireWindow *w, const gchar *file,
+                              const gchar *encoding, gpointer hint);
 void psppire_window_open (PsppireWindow *de);
 GtkWidget *psppire_window_file_chooser_dialog (PsppireWindow *toplevel);
 
-void add_most_recent (const char *file_name, const char *mime_type);
+void add_most_recent (const char *file_name, const char *mime_type,
+                      const char *encoding);
 
 G_END_DECLS
 
index 85b31d0945ecaf430e5a20b8d411b94e3e943375..a3f6a2aff67d9ebebafd0261c2d08ffb8968b96d 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2004, 2005, 2006, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
+   Copyright (C) 2004, 2005, 2006, 2009, 2010, 2011, 2012, 2013, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -108,7 +108,7 @@ initialize (const char *data_file)
       /* Check to see if the file is a .sav or a .por file.  If not
          assume that it is a syntax file */
       if (res == ANY_YES)
-       open_data_window (NULL, filename, NULL);
+       open_data_window (NULL, filename, NULL, NULL);
       else if (res == ANY_NO)
         {
           create_data_window ();
index d1252592e8e9056dadf0867596c6abe61370e0ca..b0f524ec6a182adc6beb376747ef6f9afbc63f25 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
+   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -122,7 +122,7 @@ text_data_import_assistant (PsppireDataWindow *dw)
     case GTK_RESPONSE_APPLY:
       {
        gchar *fn = g_path_get_basename (ia->file.file_name);
-       open_data_window (PSPPIRE_WINDOW (dw), fn, generate_syntax (ia));
+       open_data_window (PSPPIRE_WINDOW (dw), fn, NULL, generate_syntax (ia));
        g_free (fn);
       }
       break;