[gnome-terminal] prefs: Remove hard-coded list of legacy encodings



commit c3cd5ef76247ac6bbca7fc2cccc8059550abec1a
Author: Christian Persch <chpe src gnome org>
Date:   Sun Nov 17 22:07:11 2019 +0100

    prefs: Remove hard-coded list of legacy encodings
    
    Instead use the new vte_get_encodings() to get the list of
    supported legacy encodings, and add a small table to translate
    from iconv names to ICU names where ICU doesn't have an alias
    for the iconv name.
    
    Curiously, some of the charsets in the old hard-coded list are
    actually unknown to iconv.
    
    Blacklist any ISO-2022-* encoding; they cannot work correctly
    as a legacy charset in vte, by design.

 configure.ac                       |   2 +-
 src/Makefile.am                    |   2 -
 src/org.gnome.Terminal.gschema.xml |  74 ---------------
 src/preferences.ui                 |   7 ++
 src/profile-editor.c               |  71 +++++++++-----
 src/terminal-app.c                 |   2 -
 src/terminal-encoding.c            | 188 -------------------------------------
 src/terminal-encoding.h            |  29 ------
 src/terminal-screen.c              |  11 ++-
 src/terminal-util.c                |  64 +++++++++++++
 src/terminal-util.h                |   2 +
 11 files changed, 129 insertions(+), 323 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index dedd66c9..93ac619e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,7 +65,7 @@ case "$with_gtk" in
        GTK_MIN_REQUIRED=3.18
        GTK_MAX_ALLOWED=3.22
        VTE_API_VERSION=2.91
-       VTE_REQUIRED=0.59.0
+       VTE_REQUIRED=0.59.1
        ;;
 esac
 
diff --git a/src/Makefile.am b/src/Makefile.am
index b36cc8e3..e5a19d6c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -49,8 +49,6 @@ gnome_terminal_server_SOURCES = \
        terminal-debug.h \
        terminal-defines.h \
        terminal-enums.h \
-       terminal-encoding.c \
-       terminal-encoding.h \
        terminal-gdbus.c \
        terminal-gdbus.h \
        terminal-headerbar.c \
diff --git a/src/org.gnome.Terminal.gschema.xml b/src/org.gnome.Terminal.gschema.xml
index 6367fe2c..b8d13b64 100644
--- a/src/org.gnome.Terminal.gschema.xml
+++ b/src/org.gnome.Terminal.gschema.xml
@@ -332,80 +332,6 @@
       <summary>Whether to rewrap the terminal contents on window resize</summary>
     </key>
     <key name="encoding" type="s">
-      <choices>
-        <choice value="ISO-8859-1" />
-        <choice value="ISO-8859-2" />
-        <choice value="ISO-8859-3" />
-        <choice value="ISO-8859-4" />
-        <choice value="ISO-8859-5" />
-        <choice value="ISO-8859-6" />
-        <choice value="ISO-8859-7" />
-        <choice value="ISO-8859-8" />
-        <choice value="ISO-8859-8-I" />
-        <choice value="ISO-8859-9" />
-        <choice value="ISO-8859-10" />
-        <choice value="ISO-8859-13" />
-        <choice value="ISO-8859-14" />
-        <choice value="ISO-8859-15" />
-        <choice value="ISO-8859-16" />
-        <!-- <choice value="UTF-7" /> -->
-        <choice value="UTF-8" />
-        <!-- <choice value="UTF-16" /> -->
-        <!-- <choice value="UTF-32" /> -->
-        <!-- <choice value="UCS-4" /> -->
-        <choice value="ARMSCII-8" />
-        <choice value="BIG5" />
-        <choice value="BIG5-HKSCS" />
-        <choice value="CP866" />
-        <choice value="EUC-JP" />
-        <choice value="EUC-KR" />
-        <choice value="EUC-TW" />
-        <choice value="GB18030" />
-        <choice value="GB2312" />
-        <choice value="GBK" />
-        <choice value="GEORGIAN-PS" />
-        <choice value="IBM850" />
-        <choice value="IBM852" />
-        <choice value="IBM855" />
-        <choice value="IBM857" />
-        <choice value="IBM862" />
-        <choice value="IBM864" />
-        <choice value="ISO-2022-JP" />
-        <choice value="ISO-2022-KR" />
-        <choice value="ISO-IR-111" />
-        <!-- <choice value="JOHAB" /> -->
-        <choice value="KOI8-R" />
-        <choice value="KOI8-U" />
-        <choice value="MAC_ARABIC" />
-        <choice value="MAC_CE" />
-        <choice value="MAC_CROATIAN" />
-        <choice value="MAC-CYRILLIC" />
-        <choice value="MAC_DEVANAGARI" />
-        <choice value="MAC_FARSI" />
-        <choice value="MAC_GREEK" />
-        <choice value="MAC_GUJARATI" />
-        <choice value="MAC_GURMUKHI" />
-        <choice value="MAC_HEBREW" />
-        <choice value="MAC_ICELANDIC" />
-        <choice value="MAC_ROMAN" />
-        <choice value="MAC_ROMANIAN" />
-        <choice value="MAC_TURKISH" />
-        <choice value="MAC_UKRAINIAN" />
-        <choice value="SHIFT_JIS" />
-        <choice value="TCVN" />
-        <choice value="TIS-620" />
-        <choice value="UHC" />
-        <choice value="VISCII" />
-        <choice value="WINDOWS-1250" />
-        <choice value="WINDOWS-1251" />
-        <choice value="WINDOWS-1252" />
-        <choice value="WINDOWS-1253" />
-        <choice value="WINDOWS-1254" />
-        <choice value="WINDOWS-1255" />
-        <choice value="WINDOWS-1256" />
-        <choice value="WINDOWS-1257" />
-        <choice value="WINDOWS-1258" />
-      </choices>
       <default>'UTF-8'</default>
       <summary>Which encoding to use</summary>
     </key>
diff --git a/src/preferences.ui b/src/preferences.ui
index 627f0430..596d9ec9 100644
--- a/src/preferences.ui
+++ b/src/preferences.ui
@@ -2100,6 +2100,13 @@
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
                                 <property name="focus_on_click">False</property>
+                                <property name="id_column">0</property>
+                                <child>
+                                  <object class="GtkCellRendererText" id="encoding-combobox-renderer"/>
+                                  <attributes>
+                                    <attribute name="text">0</attribute>
+                                  </attributes>
+                                </child>
                               </object>
                               <packing>
                                 <property name="top_attach">2</property>
diff --git a/src/profile-editor.c b/src/profile-editor.c
index d7e81b08..989c6a1a 100644
--- a/src/profile-editor.c
+++ b/src/profile-editor.c
@@ -27,7 +27,6 @@
 #include <gio/gio.h>
 
 #include "terminal-app.h"
-#include "terminal-encoding.h"
 #include "terminal-enums.h"
 #include "profile-editor.h"
 #include "terminal-prefs.h"
@@ -605,30 +604,34 @@ init_color_scheme_menu (GtkWidget *widget)
   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (widget), renderer, "text", 0, NULL);
 }
 
-enum {
-  ENCODINGS_COLUMN_ID = 0,
-  ENCODINGS_COLUMN_TEXT = 1
-};
-
 static void
 init_encodings_combo (GtkWidget *widget)
 {
-  gs_unref_object GtkListStore *store = terminal_encodings_list_store_new (ENCODINGS_COLUMN_ID,
-                                                                           ENCODINGS_COLUMN_TEXT);
+  gs_unref_object GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
 
-  /* Now turn on sorting */
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
+  gs_strfreev char **encodings = vte_get_encodings (TRUE);
+  guint i = 0;
+  for (i = 0; encodings[i] != NULL; i++) {
+    GtkTreeIter iter;
+    gtk_list_store_insert_with_values (store, &iter, -1,
+                                       0, encodings[i],
+                                       -1);
+  }
+
+  if (i == 0) {
+    /* No legacy encodings supported */
+    GtkTreeIter iter;
+    gtk_list_store_insert_with_values (store, &iter, -1,
+                                       0, "UTF-8",
+                                       -1);
+  }
+
+  /* The list returned from vte is already sorted alphabetically */
+  /*  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
                                         ENCODINGS_COLUMN_TEXT,
-                                        GTK_SORT_ASCENDING);
+                                        GTK_SORT_ASCENDING); */
 
-  gtk_combo_box_set_id_column (GTK_COMBO_BOX (widget), ENCODINGS_COLUMN_ID);
   gtk_combo_box_set_model (GTK_COMBO_BOX (widget), GTK_TREE_MODEL (store));
-
-  /* Cell renderer */
-  GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (widget), renderer, TRUE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (widget), renderer,
-                                  "text", ENCODINGS_COLUMN_TEXT, NULL);
 }
 
 static gboolean
@@ -754,6 +757,27 @@ monospace_filter (const PangoFontFamily *family,
   return pango_font_family_is_monospace ((PangoFontFamily *) family);
 }
 
+static gboolean
+translate_encoding (GValue *value,
+                    GVariant *variant,
+                    gpointer user_data)
+{
+  /* We previously had a list of choices for the 'encodings' key,
+   * but now just use ICU which doesn't know all these names.
+   * If there is no replacement, the mapping fails, which means
+   * the default (UTF-8) is used.
+   */
+
+  const char *str;
+  g_variant_get (variant, "&s", &str);
+
+  const char *replacement = terminal_util_translate_encoding (str);
+  if (replacement != NULL)
+    g_value_set_string (value, replacement);
+
+  return replacement != NULL;
+}
+
 /* Called once per Preferences window, to initialize stuff that doesn't depend on the profile being edited */
 void
 profile_prefs_init (void)
@@ -1170,10 +1194,13 @@ profile_prefs_load (const char *uuid, GSettings *profile)
 
   /* Compatibility options */
   w = (GtkWidget *) gtk_builder_get_object (builder, "encoding-combobox");
-  profile_prefs_settings_bind (profile,
-                               TERMINAL_PROFILE_ENCODING_KEY,
-                               w,
-                               "active-id", G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+  profile_prefs_settings_bind_with_mapping (profile,
+                                            TERMINAL_PROFILE_ENCODING_KEY,
+                                            w,
+                                            "active-id", G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET,
+                                            (GSettingsBindGetMapping) translate_encoding,
+                                            (GSettingsBindSetMapping) NULL,
+                                            NULL, NULL);
 
   w = (GtkWidget *) gtk_builder_get_object (builder, "cjk-ambiguous-width-combobox");
   profile_prefs_settings_bind (profile, TERMINAL_PROFILE_CJK_UTF8_AMBIGUOUS_WIDTH_KEY,
diff --git a/src/terminal-app.c b/src/terminal-app.c
index bc04661e..b509048f 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -38,7 +38,6 @@
 #include "terminal-profiles-list.h"
 #include "terminal-util.h"
 #include "profile-editor.h"
-#include "terminal-encoding.h"
 #include "terminal-schemas.h"
 #include "terminal-gdbus.h"
 #include "terminal-defines.h"
@@ -1016,7 +1015,6 @@ terminal_app_new_terminal (TerminalApp     *app,
 
   g_return_val_if_fail (TERMINAL_IS_APP (app), NULL);
   g_return_val_if_fail (TERMINAL_IS_WINDOW (window), NULL);
-  g_return_val_if_fail (charset == NULL || terminal_encodings_is_known_charset (charset), NULL);
 
   screen = terminal_screen_new (profile, charset, override_command, title,
                                 working_dir, child_env, zoom);
diff --git a/src/terminal-screen.c b/src/terminal-screen.c
index 6e8c1c6e..42837d3c 100644
--- a/src/terminal-screen.c
+++ b/src/terminal-screen.c
@@ -49,7 +49,6 @@
 #include "terminal-app.h"
 #include "terminal-debug.h"
 #include "terminal-defines.h"
-#include "terminal-encoding.h"
 #include "terminal-enums.h"
 #include "terminal-intl.h"
 #include "terminal-marshal.h"
@@ -646,10 +645,11 @@ terminal_screen_new (GSettings       *profile,
 
   /* If we got an encoding together with an override command,
    * override the profile encoding; otherwise use the profile
-   * encoding.
+   * encoding (set above). Note that this will still use the
+   * profile's encoding if it's changed during the lifetime
+   * of this terminal.
    */
   if (charset != NULL &&
-      terminal_encodings_is_known_charset (charset) &&
       override_command != NULL) {
     vte_terminal_set_encoding (VTE_TERMINAL (screen),
                                charset,
@@ -768,8 +768,9 @@ terminal_screen_profile_changed_cb (GSettings     *profile,
   if (!prop_name || prop_name == I_(TERMINAL_PROFILE_ENCODING_KEY))
     {
       gs_free char *charset = g_settings_get_string (profile, TERMINAL_PROFILE_ENCODING_KEY);
-      g_warn_if_fail (terminal_encodings_is_known_charset (charset));
-      vte_terminal_set_encoding (vte_terminal, charset, NULL);
+      const char *encoding = terminal_util_translate_encoding (charset);
+      if (encoding != NULL)
+        vte_terminal_set_encoding (vte_terminal, encoding, NULL);
     }
 
   if (!prop_name || prop_name == I_(TERMINAL_PROFILE_CJK_UTF8_AMBIGUOUS_WIDTH_KEY))
diff --git a/src/terminal-util.c b/src/terminal-util.c
index ab749d83..a854e9cf 100644
--- a/src/terminal-util.c
+++ b/src/terminal-util.c
@@ -1438,6 +1438,70 @@ terminal_util_save_print_settings (GtkPrintSettings *settings,
   save_cache_keyfile (keyfile, TERMINAL_PRINT_SETTINGS_FILENAME);
 }
 
+/*
+ * terminal_util_translate_encoding:
+ * @encoding: the encoding name
+ *
+ * Translates old encoding name to the one supported by ICU, or
+ * to %NULL if the encoding is not known to ICU.
+ *
+ * Returns: (transfer none): the translated encoding, or %NULL if
+ *   not translation was possible.
+ */
+const char*
+terminal_util_translate_encoding (const char *encoding)
+{
+  if (vte_get_encoding_supported (encoding))
+    return encoding;
+
+  /* ICU knows (or has aliases for) most of the old names, except the following */
+  struct {
+    const char *name;
+    const char *replacement;
+  } translations[] = {
+    { "ARMSCII-8",      NULL           }, /* apparently not supported by ICU */
+    { "GEORGIAN-PS",    NULL           }, /* no idea which charset this even is */
+    { "ISO-IR-111",     NULL           }, /* ISO-IR-111 refers to ECMA-94, but that
+                                           * standard does not contain cyrillic letters.
+                                           * ECMA-94 refers to ECMA-113 (ISO-IR-144),
+                                           * whose assignment differs greatly from ISO-IR-111,
+                                           * so it cannot be that either.
+                                           */
+    /* All the MAC_* charsets appear to be unknown to even glib iconv, so
+     * why did we have them in our list in the first place?
+     */
+    { "MAC_DEVANAGARI", NULL           }, /* apparently not supported by ICU */
+    { "MAC_FARSI",      NULL           }, /* apparently not supported by ICU */
+    { "MAC_GREEK",      "x-MacGreek"   },
+    { "MAC_GUJARATI",   NULL           }, /* apparently not supported by ICU */
+    { "MAC_GURMUKHI",   NULL           }, /* apparently not supported by ICU */
+    { "MAC_ICELANDIC",  NULL           }, /* apparently not supported by ICU */
+    { "MAC_ROMANIAN",   "x-macroman"   }, /* not sure this is the right one */
+    { "MAC_TURKISH",    "x-MacTurkish" },
+    { "MAC_UKRAINIAN",  "x-MacUkraine" },
+
+    { "TCVN",           NULL           }, /* apparently not supported by ICU */
+    { "UHC",            "cp949"        },
+    { "VISCII",         NULL           }, /* apparently not supported by ICU */
+
+    /* ISO-2022-* are known to ICU, but they simply cannot work in vte as
+     * I/O encoding, so don't even try.
+     */
+    { "ISO-2022-JP",    NULL           },
+    { "ISO-2022-KR",    NULL           },
+  };
+
+  const char *replacement = NULL;
+  for (guint i = 0; i < G_N_ELEMENTS (translations); ++i) {
+    if (g_str_equal (encoding, translations[i].name)) {
+      replacement = translations[i].replacement;
+      break;
+    }
+  }
+
+  return replacement;
+}
+
 /* BEGIN code copied from glib
  *
  * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
diff --git a/src/terminal-util.h b/src/terminal-util.h
index 37e04dba..a8a75475 100644
--- a/src/terminal-util.h
+++ b/src/terminal-util.h
@@ -108,6 +108,8 @@ void terminal_util_load_print_settings (GtkPrintSettings **settings,
 void terminal_util_save_print_settings (GtkPrintSettings *settings,
                                         GtkPageSetup *page_setup);
 
+const char *terminal_util_translate_encoding (const char *encoding);
+
 char *terminal_util_find_program_in_path (const char *path,
                                           const char *program);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]