gui: Allow File|Open to select an encoding for system files.
[pspp] / src / ui / gui / psppire-window.c
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);