terminal-encoding.c 9.95 KB
Newer Older
1
/*
2
 * Copyright © 2002 Red Hat, Inc.
3
 * Copyright © 2008, 2017 Christian Persch
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8 9
 * (at your option) any later version.
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
15 16
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
#include "config.h"
20

21
#include <string.h>
22 23
#include <search.h>
#include <stdlib.h>
24

25 26
#include <glib.h>
#include <glib/gi18n.h>
27
#include <gtk/gtk.h>
28

29
#include "terminal-app.h"
30 31
#include "terminal-debug.h"
#include "terminal-encoding.h"
32
#include "terminal-schemas.h"
33
#include "terminal-util.h"
34
#include "terminal-libgsystem.h"
35 36 37

/* Overview
 *
38
 * There's a list of character sets stored in gsettings, indicating
39 40 41 42 43 44 45
 * which encodings to display in the encoding menu.
 * 
 * We have a pre-canned list of available encodings
 * (hardcoded in the table below) that can be added to
 * the encoding menu, and to give a human-readable name
 * to certain encodings.
 *
46
 * If the setting list contains an encoding not in the
47 48 49 50
 * predetermined table, then that encoding is
 * labeled "user defined" but still appears in the menu.
 */

51 52
typedef enum {
  GROUP_UNICODE,
53 54
  GROUP_CJKV,
  GROUP_OBSOLETE,
55 56 57 58
  LAST_GROUP
} EncodingGroup;

typedef struct {
59 60
  const char *charset;
  const char *name;
61 62
  EncodingGroup group;
} EncodingEntry;
63

64
/* These MUST be sorted by charset so that bsearch can work! */
65
static const EncodingEntry encodings[] = {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  { "ARMSCII-8",      N_("Armenian"),            GROUP_OBSOLETE },
  { "BIG5",           N_("Chinese Traditional"), GROUP_CJKV },
  { "BIG5-HKSCS",     N_("Chinese Traditional"), GROUP_CJKV },
  { "CP866",          N_("Cyrillic/Russian"),    GROUP_OBSOLETE },
  { "EUC-JP",         N_("Japanese"),            GROUP_CJKV },
  { "EUC-KR",         N_("Korean"),              GROUP_CJKV },
  { "EUC-TW",         N_("Chinese Traditional"), GROUP_CJKV },
  { "GB18030",        N_("Chinese Simplified"),  GROUP_CJKV },
  { "GB2312",         N_("Chinese Simplified"),  GROUP_CJKV },
  { "GBK",            N_("Chinese Simplified"),  GROUP_CJKV },
  { "GEORGIAN-PS",    N_("Georgian"),            GROUP_OBSOLETE },
  { "IBM850",         N_("Western"),             GROUP_OBSOLETE },
  { "IBM852",         N_("Central European"),    GROUP_OBSOLETE },
  { "IBM855",         N_("Cyrillic"),            GROUP_OBSOLETE },
  { "IBM857",         N_("Turkish"),             GROUP_OBSOLETE },
  { "IBM862",         N_("Hebrew"),              GROUP_OBSOLETE },
  { "IBM864",         N_("Arabic"),              GROUP_OBSOLETE },
  { "ISO-2022-JP",    N_("Japanese"),            GROUP_CJKV },
  { "ISO-2022-KR",    N_("Korean"),              GROUP_CJKV },
  { "ISO-8859-1",     N_("Western"),             GROUP_OBSOLETE },
  { "ISO-8859-10",    N_("Nordic"),              GROUP_OBSOLETE },
  { "ISO-8859-13",    N_("Baltic"),              GROUP_OBSOLETE },
  { "ISO-8859-14",    N_("Celtic"),              GROUP_OBSOLETE },
  { "ISO-8859-15",    N_("Western"),             GROUP_OBSOLETE },
  { "ISO-8859-16",    N_("Romanian"),            GROUP_OBSOLETE },
  { "ISO-8859-2",     N_("Central European"),    GROUP_OBSOLETE },
  { "ISO-8859-3",     N_("South European"),      GROUP_OBSOLETE },
  { "ISO-8859-4",     N_("Baltic"),              GROUP_OBSOLETE },
  { "ISO-8859-5",     N_("Cyrillic"),            GROUP_OBSOLETE },
  { "ISO-8859-6",     N_("Arabic"),              GROUP_OBSOLETE },
  { "ISO-8859-7",     N_("Greek"),               GROUP_OBSOLETE },
  { "ISO-8859-8",     N_("Hebrew Visual"),       GROUP_OBSOLETE },
  { "ISO-8859-8-I",   N_("Hebrew"),              GROUP_OBSOLETE },
  { "ISO-8859-9",     N_("Turkish"),             GROUP_OBSOLETE },
  { "ISO-IR-111",     N_("Cyrillic"),            GROUP_OBSOLETE },
   /* { "JOHAB",      N_("Korean"),              GROUP_CJKV }, */
  { "KOI8-R",         N_("Cyrillic"),            GROUP_OBSOLETE },
  { "KOI8-U",         N_("Cyrillic/Ukrainian"),  GROUP_OBSOLETE },
  { "MAC-CYRILLIC",   N_("Cyrillic"),            GROUP_OBSOLETE },
  { "MAC_ARABIC",     N_("Arabic"),              GROUP_OBSOLETE },
  { "MAC_CE",         N_("Central European"),    GROUP_OBSOLETE },
  { "MAC_CROATIAN",   N_("Croatian"),            GROUP_OBSOLETE },
  { "MAC_DEVANAGARI", N_("Hindi"),               GROUP_OBSOLETE },
  { "MAC_FARSI",      N_("Persian"),             GROUP_OBSOLETE },
  { "MAC_GREEK",      N_("Greek"),               GROUP_OBSOLETE },
  { "MAC_GUJARATI",   N_("Gujarati"),            GROUP_OBSOLETE },
  { "MAC_GURMUKHI",   N_("Gurmukhi"),            GROUP_OBSOLETE },
  { "MAC_HEBREW",     N_("Hebrew"),              GROUP_OBSOLETE },
  { "MAC_ICELANDIC",  N_("Icelandic"),           GROUP_OBSOLETE },
  { "MAC_ROMAN",      N_("Western"),             GROUP_OBSOLETE },
  { "MAC_ROMANIAN",   N_("Romanian"),            GROUP_OBSOLETE },
  { "MAC_TURKISH",    N_("Turkish"),             GROUP_OBSOLETE },
  { "MAC_UKRAINIAN",  N_("Cyrillic/Ukrainian"),  GROUP_OBSOLETE },
  { "SHIFT_JIS",      N_("Japanese"),            GROUP_CJKV },
  /* This is TCVN-5712-1, not TCVN-5773:1993 which would be CJKV */
  { "TCVN",           N_("Vietnamese"),          GROUP_OBSOLETE },
  { "TIS-620",        N_("Thai"),                GROUP_OBSOLETE },
123
  /* { "UCS-4",       N_("Unicode"),             GROUP_UNICODE }, */
124
  { "UHC",            N_("Korean"),              GROUP_CJKV },
125 126 127 128
  /* { "UTF-16",      N_("Unicode"),             GROUP_UNICODE }, */
  /* { "UTF-32",      N_("Unicode"),             GROUP_UNICODE }, */
  /* { "UTF-7",       N_("Unicode"),             GROUP_UNICODE }, */
  { "UTF-8",          N_("Unicode"),             GROUP_UNICODE },
129 130 131 132 133 134 135 136 137 138
  { "VISCII",         N_("Vietnamese"),          GROUP_OBSOLETE },
  { "WINDOWS-1250",   N_("Central European"),    GROUP_OBSOLETE },
  { "WINDOWS-1251",   N_("Cyrillic"),            GROUP_OBSOLETE },
  { "WINDOWS-1252",   N_("Western"),             GROUP_OBSOLETE },
  { "WINDOWS-1253",   N_("Greek"),               GROUP_OBSOLETE },
  { "WINDOWS-1254",   N_("Turkish"),             GROUP_OBSOLETE },
  { "WINDOWS-1255",   N_("Hebrew"),              GROUP_OBSOLETE},
  { "WINDOWS-1256",   N_("Arabic"),              GROUP_OBSOLETE },
  { "WINDOWS-1257",   N_("Baltic"),              GROUP_OBSOLETE },
  { "WINDOWS-1258",   N_("Vietnamese"),          GROUP_OBSOLETE },
139
};
140

141 142 143 144 145
static const struct {
  EncodingGroup group;
  const char *name;
} group_names[] = {
  { GROUP_UNICODE,  N_("Unicode") },
146 147
  { GROUP_CJKV,     N_("Legacy CJK Encodings") },
  { GROUP_OBSOLETE, N_("Obsolete Encodings") },
148
};
149

150
#define EM_DASH "—"
151

152 153 154
static int
compare_encoding_entry_cb (const void *ap,
                           const void *bp)
155
{
156 157
  const EncodingEntry *a = ap;
  const EncodingEntry *b = bp;
158

159 160 161
  int r = a->group - b->group;
  if (r != 0)
    return r;
162

163 164 165
  r = g_utf8_collate (a->name, b->name);
  if (r != 0)
    return r;
166

167
  return strcmp (a->charset, b->charset);
168 169
}

170 171 172 173 174 175 176 177 178
/**
 * terminal_encodings_append_menu:
 *
 * Appends to known encodings to a #GMenu, sorted in groups and
 * alphabetically by name inside the groups. The action name
 * used when activating the menu items is "win.encoding".
 */
void
terminal_encodings_append_menu (GMenu *menu)
179
{
180 181 182 183
  /* First, sort the encodings */
  gs_free EncodingEntry *array = g_memdup (encodings, sizeof encodings);
  for (guint i = 0; i < G_N_ELEMENTS (encodings); i++)
    array[i].name = _(array[i].name); /* translate */
184

185 186
  qsort (array, G_N_ELEMENTS (encodings), sizeof array[0],
         compare_encoding_entry_cb);
187

188 189
  for (guint group = 0 ; group < LAST_GROUP; group++) {
    gs_unref_object GMenu *section = g_menu_new ();
190

191 192 193
    for (guint i = 0; i < G_N_ELEMENTS (encodings); i++) {
      if (array[i].group != group)
        continue;
194

195 196 197 198 199 200 201 202 203
      gs_free_gstring GString *str = g_string_sized_new (128);
      g_string_append (str, array[i].name);
      g_string_append (str, " " EM_DASH " ");
      for (const char *p = array[i].charset; *p; p++) {
        if (*p == '_')
          g_string_append (str, "__");
        else
          g_string_append_c (str, *p);
      }
204

205 206
      gs_unref_object GMenuItem *item = g_menu_item_new (str->str, NULL);
      g_menu_item_set_action_and_target (item, "win.encoding", "s", array[i].charset);
207

208 209 210 211
      g_menu_append_item (section, item);
    }

    g_menu_append_section (menu, _(group_names[group].name), G_MENU_MODEL (section));
212
  }
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

/**
 * terminal_encodings_list_store_new:
 *
 * Creates a #GtkListStore containing the known encodings.
 * The model containing 2 columns, the 0th one with the
 * charset name, and the 1st one with the label.
 * The model is unsorted.
 *
 * Returns: (transfer full): a new #GtkTreeModel
 */
GtkListStore *
terminal_encodings_list_store_new (int column_id,
                                   int column_text)
{
  GtkListStore *store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
230

231 232
  for (guint i = 0; i < G_N_ELEMENTS (encodings); i++) {
    gs_free char *name = g_strdup_printf ("%s " EM_DASH " %s",
233
                                          _(encodings[i].name), encodings[i].charset);
234

235 236 237 238 239 240
    GtkTreeIter iter;
    gtk_list_store_insert_with_values (store, &iter, -1,
                                       column_id, encodings[i].charset,
                                       column_text, name,
                                       -1);
  }
241

242 243
  return store;
}
244

245 246 247
static int
compare_charset_cb (const void *ap,
                    const void *bp)
248
{
249 250
  const EncodingEntry *a = ap;
  const EncodingEntry *b = bp;
251

252 253
  return strcmp (a->charset, b->charset);
}
254

255 256 257 258 259 260 261 262
gboolean
terminal_encodings_is_known_charset (const char *charset)
{
  EncodingEntry key = { charset, NULL, 0 };
  return bsearch (&key,
                  encodings, G_N_ELEMENTS (encodings),
                  sizeof (encodings[0]),
                  compare_charset_cb) != NULL;
263
}