LCOV - code coverage report
Current view: top level - borrowed/goffice - go-charmap-sel.c (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 0.0 % 227 0
Test Date: 2025-02-07 16:25:45 Functions: 0.0 % 21 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
       2                 :             : /*
       3                 :             :  * A charmap selector widget.
       4                 :             :  *
       5                 :             :  *  Copyright (C) 2003-2005 Andreas J. Guelzow
       6                 :             :  *
       7                 :             :  *  based on code by:
       8                 :             :  *  Copyright (C) 2000 Marco Pesenti Gritti
       9                 :             :  *  from the galeon code base
      10                 :             :  *
      11                 :             :  *  This program is free software; you can redistribute it and/or modify
      12                 :             :  *  it under the terms of the GNU General Public License as published by
      13                 :             :  *  the Free Software Foundation; either version 2, or (at your option)
      14                 :             :  *  any later version.
      15                 :             :  *
      16                 :             :  *  This program is distributed in the hope that it will be useful,
      17                 :             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :             :  *  GNU General Public License for more details.
      20                 :             :  *
      21                 :             :  *  You should have received a copy of the GNU General Public License
      22                 :             :  *  along with this program; if not, write to the Free Software
      23                 :             :  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
      24                 :             :  */
      25                 :             : 
      26                 :             : #include <config.h>
      27                 :             : #include "go-charmap-sel.h"
      28                 :             : #include "go-optionmenu.h"
      29                 :             : #include "go-glib-extras.h"
      30                 :             : #include <glib/gi18n-lib.h>
      31                 :             : #include <string.h>
      32                 :             : #include <stdlib.h>
      33                 :             : 
      34                 :             : #define CS(x) GO_CHARMAP_SEL (x)
      35                 :             : 
      36                 :             : #define CHARMAP_NAME_KEY "Name of Character Encoding"
      37                 :             : 
      38                 :             : /* ------------------------------------------------------------------------- */
      39                 :             : 
      40                 :             : typedef enum
      41                 :             : {
      42                 :             :     LG_ARABIC,
      43                 :             :     LG_BALTIC,
      44                 :             :     LG_CENTRAL_EUROPEAN,
      45                 :             :     LG_CHINESE,
      46                 :             :     LG_CYRILLIC,
      47                 :             :     LG_GREEK,
      48                 :             :     LG_HEBREW,
      49                 :             :     LG_INDIAN,
      50                 :             :     LG_JAPANESE,
      51                 :             :     LG_KOREAN,
      52                 :             :     LG_TURKISH,
      53                 :             :     LG_UNICODE,
      54                 :             :     LG_VIETNAMESE,
      55                 :             :     LG_WESTERN,
      56                 :             :     LG_OTHER,
      57                 :             :     LG_LAST
      58                 :             : } LanguageGroup;
      59                 :             : 
      60                 :             : typedef struct
      61                 :             : {
      62                 :             :     char const *group_name;
      63                 :             :     LanguageGroup const lgroup;
      64                 :             :     /* Generated stuff follows.  */
      65                 :             :     char *collate_key;
      66                 :             : } LGroupInfo;
      67                 :             : 
      68                 :             : static LGroupInfo lgroups[] =
      69                 :             : {
      70                 :             : { N_("Arabic"), LG_ARABIC },
      71                 :             : { N_("Baltic"), LG_BALTIC },
      72                 :             : { N_("Central European"), LG_CENTRAL_EUROPEAN },
      73                 :             : { N_("Chinese"), LG_CHINESE },
      74                 :             : { N_("Cyrillic"), LG_CYRILLIC },
      75                 :             : { N_("Greek"), LG_GREEK },
      76                 :             : { N_("Hebrew"), LG_HEBREW },
      77                 :             : { N_("Indian"), LG_INDIAN },
      78                 :             : { N_("Japanese"), LG_JAPANESE },
      79                 :             : { N_("Korean"), LG_KOREAN },
      80                 :             : { N_("Turkish"), LG_TURKISH },
      81                 :             : { N_("Unicode"), LG_UNICODE },
      82                 :             : { N_("Vietnamese"), LG_VIETNAMESE },
      83                 :             : { N_("Western"), LG_WESTERN },
      84                 :             : { N_("Other"), LG_OTHER },
      85                 :             : { NULL, LG_LAST } };
      86                 :             : 
      87                 :           0 : static int lgroups_order(const void *_a, const void *_b)
      88                 :             : {
      89                 :           0 :     const LGroupInfo *a = (const LGroupInfo *) _a;
      90                 :           0 :     const LGroupInfo *b = (const LGroupInfo *) _b;
      91                 :             : 
      92                 :           0 :     return strcmp(a->collate_key, b->collate_key);
      93                 :             : }
      94                 :             : 
      95                 :             : /* ------------------------------------------------------------------------- */
      96                 :             : 
      97                 :             : typedef enum
      98                 :             : {
      99                 :             :     CI_MINOR, CI_MAJOR
     100                 :             : } CharsetImportance;
     101                 :             : 
     102                 :             : typedef struct
     103                 :             : {
     104                 :             :     gchar const *charset_title;
     105                 :             :     gchar const *aliases;
     106                 :             :     LanguageGroup const lgroup;
     107                 :             :     CharsetImportance const imp;
     108                 :             :     /* Generated stuff follows.  */
     109                 :             :     char *collate_key;
     110                 :             :     char *to_utf8_iconv_name, *from_utf8_iconv_name;
     111                 :             : } CharsetInfo;
     112                 :             : 
     113                 :             : static CharsetInfo charset_trans_array[] =
     114                 :             :         {
     115                 :             :         { N_("Arabic (IBM-864)"), "IBM864", LG_ARABIC, CI_MINOR },
     116                 :             :         { N_("Arabic (IBM-864-I)"), "IBM864i", LG_ARABIC, CI_MINOR },
     117                 :             :         { N_("Arabic (ISO-8859-6)"), "ISO-8859-6", LG_ARABIC, CI_MINOR },
     118                 :             :         { N_("Arabic (ISO-8859-6-E)"), "ISO-8859-6-E", LG_ARABIC, CI_MINOR },
     119                 :             : 
     120                 :             :         { N_("Arabic (ISO-8859-6-I)"), "ISO-8859-6-I", LG_ARABIC, CI_MINOR },
     121                 :             :         { N_("Arabic (MacArabic)"), "x-mac-arabic", LG_ARABIC, CI_MINOR },
     122                 :             :         { N_("Arabic (Windows-1256)"), "windows-1256", LG_ARABIC, CI_MINOR },
     123                 :             :         { N_("Armenian (ARMSCII-8)"), "armscii-8", LG_OTHER, CI_MINOR },
     124                 :             :         { N_("Baltic (ISO-8859-13)"), "ISO-8859-13", LG_BALTIC, CI_MINOR },
     125                 :             :         { N_("Baltic (ISO-8859-4)"), "ISO-8859-4", LG_BALTIC, CI_MINOR },
     126                 :             :         { N_("Baltic (Windows-1257)"), "windows-1257", LG_BALTIC, CI_MINOR },
     127                 :             :         { N_("Celtic (ISO-8859-14)"), "ISO-8859-14", LG_OTHER, CI_MINOR },
     128                 :             :         { N_("Central European (IBM-852)"), "IBM852", LG_CENTRAL_EUROPEAN,
     129                 :             :                 CI_MINOR },
     130                 :             :         { N_("Central European (ISO-8859-2)"), "ISO-8859-2",
     131                 :             :                 LG_CENTRAL_EUROPEAN, CI_MINOR },
     132                 :             :         { N_("Central European (MacCE)"), "x-mac-ce", LG_CENTRAL_EUROPEAN,
     133                 :             :                 CI_MINOR },
     134                 :             :         { N_("Central European (Windows-1250)"), "windows-1250",
     135                 :             :                 LG_CENTRAL_EUROPEAN, CI_MINOR },
     136                 :             :         { N_("Chinese Simplified (GB18030)"), "gb18030", LG_CHINESE, CI_MINOR },
     137                 :             :         { N_("Chinese Simplified (GB2312)"), "GB2312", LG_CHINESE, CI_MINOR },
     138                 :             :         { N_("Chinese Simplified (GBK)"), "x-gbk", LG_CHINESE, CI_MINOR },
     139                 :             :         { N_("Chinese Simplified (HZ)"), "HZ-GB-2312", LG_CHINESE, CI_MINOR },
     140                 :             :         { N_("Chinese Simplified (Windows-936)"), "windows-936", LG_CHINESE,
     141                 :             :                 CI_MINOR },
     142                 :             :         { N_("Chinese Traditional (Big5)"), "Big5", LG_CHINESE, CI_MINOR },
     143                 :             :         { N_("Chinese Traditional (Big5-HKSCS)"), "Big5-HKSCS", LG_CHINESE,
     144                 :             :                 CI_MINOR },
     145                 :             :                 { N_("Chinese Traditional (EUC-TW)"), "x-euc-tw", LG_CHINESE,
     146                 :             :                         CI_MINOR },
     147                 :             :                 { N_("Croatian (MacCroatian)"), "x-mac-croatian",
     148                 :             :                         LG_CENTRAL_EUROPEAN, CI_MINOR },
     149                 :             :                 { N_("Cyrillic (IBM-855)"), "IBM855", LG_CYRILLIC, CI_MINOR },
     150                 :             :                 { N_("Cyrillic (ISO-8859-5)"), "ISO-8859-5", LG_CYRILLIC,
     151                 :             :                         CI_MINOR },
     152                 :             :                 { N_("Cyrillic (ISO-IR-111)"), "ISO-IR-111", LG_CYRILLIC,
     153                 :             :                         CI_MINOR },
     154                 :             :                 { N_("Cyrillic (KOI8-R)"), "KOI8-R", LG_CYRILLIC, CI_MINOR },
     155                 :             :                 { N_("Cyrillic (MacCyrillic)"), "x-mac-cyrillic", LG_CYRILLIC,
     156                 :             :                         CI_MINOR },
     157                 :             :                 { N_("Cyrillic (Windows-1251)"), "windows-1251", LG_CYRILLIC,
     158                 :             :                         CI_MINOR },
     159                 :             :                 { N_("Russian (CP-866)"), "IBM866", LG_CYRILLIC, CI_MINOR },
     160                 :             :                 { N_("Ukrainian (KOI8-U)"), "KOI8-U", LG_CYRILLIC, CI_MINOR },
     161                 :             :                 { N_("Ukrainian (MacUkrainian)"), "x-mac-ukrainian",
     162                 :             :                         LG_CYRILLIC, CI_MINOR },
     163                 :             :                 { N_("English (ASCII)"), "ANSI_X3.4-1968#ASCII", LG_WESTERN,
     164                 :             :                         CI_MAJOR },
     165                 :             :                 { N_("Farsi (MacFarsi)"), "x-mac-farsi", LG_OTHER, CI_MINOR },
     166                 :             :                 { N_("Georgian (GEOSTD8)"), "geostd8", LG_OTHER, CI_MINOR },
     167                 :             :                 { N_("Greek (ISO-8859-7)"), "ISO-8859-7", LG_GREEK, CI_MINOR },
     168                 :             :                 { N_("Greek (MacGreek)"), "x-mac-greek", LG_GREEK, CI_MINOR },
     169                 :             :                 { N_("Greek (Windows-1253)"), "windows-1253", LG_GREEK, CI_MINOR },
     170                 :             :                 { N_("Gujarati (MacGujarati)"), "x-mac-gujarati", LG_INDIAN,
     171                 :             :                         CI_MINOR },
     172                 :             :                 { N_("Gurmukhi (MacGurmukhi)"), "x-mac-gurmukhi", LG_INDIAN,
     173                 :             :                         CI_MINOR },
     174                 :             :                 { N_("Hebrew (IBM-862)"), "IBM862", LG_HEBREW, CI_MINOR },
     175                 :             :                 { N_("Hebrew (ISO-8859-8-E)"), "ISO-8859-8-E", LG_HEBREW,
     176                 :             :                         CI_MINOR },
     177                 :             :                 { N_("Hebrew (ISO-8859-8-I)"), "ISO-8859-8-I", LG_HEBREW,
     178                 :             :                         CI_MINOR },
     179                 :             :                 { N_("Hebrew (MacHebrew)"), "x-mac-hebrew", LG_HEBREW, CI_MINOR },
     180                 :             :                 { N_("Hebrew (Windows-1255)"), "windows-1255", LG_HEBREW,
     181                 :             :                         CI_MINOR },
     182                 :             :                 { N_("Hindi (MacDevanagari)"), "x-mac-devanagari", LG_INDIAN,
     183                 :             :                         CI_MINOR },
     184                 :             :                 { N_("Icelandic (MacIcelandic)"), "x-mac-icelandic", LG_OTHER,
     185                 :             :                         CI_MINOR },
     186                 :             :                 { N_("Japanese (EUC-JP)"), "EUC-JP", LG_JAPANESE, CI_MINOR },
     187                 :             :                 { N_("Japanese (ISO-2022-JP)"), "ISO-2022-JP", LG_JAPANESE,
     188                 :             :                         CI_MINOR },
     189                 :             :                 { N_("Japanese (Shift_JIS)"), "CP932", LG_JAPANESE, CI_MINOR },
     190                 :             :                 { N_("Korean (EUC-KR)"), "EUC-KR", LG_KOREAN, CI_MINOR },
     191                 :             :                 { N_("Korean (ISO-2022-KR)"), "ISO-2022-KR", LG_KOREAN, CI_MINOR },
     192                 :             :                 { N_("Korean (JOHAB)"), "x-johab", LG_KOREAN, CI_MINOR },
     193                 :             :                 { N_("Korean (UHC)"), "x-windows-949", LG_KOREAN, CI_MINOR },
     194                 :             :                 { N_("Nordic (ISO-8859-10)"), "ISO-8859-10", LG_OTHER, CI_MINOR },
     195                 :             :                 { N_("Romanian (MacRomanian)"), "x-mac-romanian", LG_OTHER,
     196                 :             :                         CI_MINOR },
     197                 :             :                 { N_("Romanian (ISO-8859-16)"), "ISO-8859-16", LG_OTHER,
     198                 :             :                         CI_MINOR },
     199                 :             :                 { N_("South European (ISO-8859-3)"), "ISO-8859-3", LG_OTHER,
     200                 :             :                         CI_MINOR },
     201                 :             :                 { N_("Thai (TIS-620)"), "TIS-620", LG_OTHER, CI_MINOR },
     202                 :             :                 { N_("Turkish (IBM-857)"), "IBM857", LG_TURKISH, CI_MINOR },
     203                 :             :                 { N_("Turkish (ISO-8859-9)"), "ISO-8859-9", LG_TURKISH, CI_MINOR },
     204                 :             :                 { N_("Turkish (MacTurkish)"), "x-mac-turkish", LG_TURKISH,
     205                 :             :                         CI_MINOR },
     206                 :             :                 { N_("Turkish (Windows-1254)"), "windows-1254", LG_TURKISH,
     207                 :             :                         CI_MINOR },
     208                 :             :                 { N_("Unicode (UTF-7)"), "UTF-7", LG_UNICODE, CI_MINOR },
     209                 :             :                 { N_("Unicode (UTF-8)"), "UTF-8", LG_UNICODE, CI_MAJOR },
     210                 :             :                 { N_("Unicode (UTF-16BE)"), "UTF-16BE", LG_UNICODE, CI_MINOR },
     211                 :             :                 { N_("Unicode (UTF-16LE)"), "UTF-16LE", LG_UNICODE, CI_MINOR },
     212                 :             :                 { N_("Unicode (UTF-32BE)"), "UTF-32BE", LG_UNICODE, CI_MINOR },
     213                 :             :                 { N_("Unicode (UTF-32LE)"), "UTF-32LE", LG_UNICODE, CI_MINOR },
     214                 :             :                 { N_("User Defined"), "x-user-defined", LG_OTHER, CI_MINOR },
     215                 :             :                 { N_("Vietnamese (TCVN)"), "x-viet-tcvn5712", LG_VIETNAMESE,
     216                 :             :                         CI_MINOR },
     217                 :             :                 { N_("Vietnamese (VISCII)"), "VISCII", LG_VIETNAMESE, CI_MINOR },
     218                 :             :                 { N_("Vietnamese (VPS)"), "x-viet-vps", LG_VIETNAMESE, CI_MINOR },
     219                 :             :                 { N_("Vietnamese (Windows-1258)"), "windows-1258",
     220                 :             :                         LG_VIETNAMESE, CI_MINOR },
     221                 :             :                 { N_("Visual Hebrew (ISO-8859-8)"), "ISO-8859-8", LG_HEBREW,
     222                 :             :                         CI_MINOR },
     223                 :             :                 { N_("Western (IBM-850)"), "IBM850", LG_WESTERN, CI_MINOR },
     224                 :             :                 { N_("Western (ISO-8859-1)"), "ISO-8859-1", LG_WESTERN, CI_MAJOR },
     225                 :             :                 { N_("Western (ISO-8859-15)"), "ISO-8859-15", LG_WESTERN,
     226                 :             :                         CI_MINOR },
     227                 :             :                 { N_("Western (MacRoman)"), "x-mac-roman", LG_WESTERN, CI_MINOR },
     228                 :             :                 { N_("Western (Windows-1252)"), "windows-1252", LG_WESTERN,
     229                 :             :                         CI_MINOR },
     230                 :             :                 /* charsets without possibly translatable names */
     231                 :             :                 { "T61.8bit", "T61.8bit", LG_OTHER, CI_MINOR },
     232                 :             :                 { "x-imap4-modified-utf7", "x-imap4-modified-utf7", LG_UNICODE,
     233                 :             :                         CI_MINOR },
     234                 :             :                 { "x-u-escaped", "x-u-escaped", LG_OTHER, CI_MINOR },
     235                 :             :                 { NULL, NULL, LG_LAST, 0 } };
     236                 :             : 
     237                 :           0 : static int charset_order(const void *_a, const void *_b)
     238                 :             : {
     239                 :           0 :     const CharsetInfo *a = (const CharsetInfo *) _a;
     240                 :           0 :     const CharsetInfo *b = (const CharsetInfo *) _b;
     241                 :             : 
     242                 :           0 :     if (a->lgroup != b->lgroup)
     243                 :           0 :         return (int) b->lgroup - (int) a->lgroup;
     244                 :             : 
     245                 :           0 :     if (a->imp != b->imp)
     246                 :           0 :         return (int) b->imp - (int) a->imp;
     247                 :             : 
     248                 :           0 :     return strcmp(a->collate_key, b->collate_key);
     249                 :             : }
     250                 :             : 
     251                 :             : /* ------------------------------------------------------------------------- */
     252                 :             : 
     253                 :             : /* name -> CharsetInfo* mapping */
     254                 :             : static GHashTable *encoding_hash;
     255                 :             : 
     256                 :             : struct _GOCharmapSel
     257                 :             : {
     258                 :             :     GtkBox box;
     259                 :             :     GOOptionMenu *encodings;
     260                 :             :     GtkMenu *encodings_menu;
     261                 :             :     GOCharmapSelTestDirection test;
     262                 :             : };
     263                 :             : 
     264                 :             : 
     265                 :             : /* Signals we emit */
     266                 :             : enum
     267                 :             : {
     268                 :             :     CHARMAP_CHANGED, LAST_SIGNAL
     269                 :             : };
     270                 :             : 
     271                 :             : enum
     272                 :             : {
     273                 :             :     PROP_0, PROP_TEST_DIRECTION
     274                 :             : };
     275                 :             : 
     276                 :             : static guint cs_signals[LAST_SIGNAL] =
     277                 :             : { 0 };
     278                 :             : 
     279                 :             : static void cs_set_property(GObject *object, guint prop_id, const GValue *value,
     280                 :             :         GParamSpec *pspec);
     281                 :             : 
     282                 :             : static void cs_get_property(GObject *object, guint prop_id, GValue *value,
     283                 :             :         GParamSpec *pspec);
     284                 :             : 
     285                 :           0 : G_DEFINE_TYPE (GOCharmapSel, go_charmap_sel, GTK_TYPE_BOX)
     286                 :             : 
     287                 :           0 : static gboolean iconv_supported(const char *to, const char *from)
     288                 :             : {
     289                 :           0 :     GIConv ic = g_iconv_open(to, from);
     290                 :           0 :     if (ic == NULL || ic == (GIConv) -1)
     291                 :           0 :         return FALSE;
     292                 :             : 
     293                 :           0 :     g_iconv_close(ic);
     294                 :           0 :     return TRUE;
     295                 :             : }
     296                 :             : 
     297                 :             : const char *
     298                 :           0 : go_charmap_sel_get_encoding_name (G_GNUC_UNUSED GOCharmapSel *cs,
     299                 :             :         const char *encoding)
     300                 :             : {
     301                 :             :     CharsetInfo const *ci;
     302                 :             : 
     303                 :           0 :     g_return_val_if_fail (encoding != NULL, NULL);
     304                 :             : 
     305                 :           0 :     ci = g_hash_table_lookup (encoding_hash, encoding);
     306                 :           0 :     return ci ? _(ci->charset_title) : NULL;
     307                 :             : }
     308                 :             : 
     309                 :             : static char const *
     310                 :           0 : get_locale_encoding_name(GOCharmapSel *cs)
     311                 :             : {
     312                 :             :     char const *locale_encoding;
     313                 :             :     char const *name;
     314                 :             : 
     315                 :           0 :     g_get_charset(&locale_encoding);
     316                 :           0 :     name = go_charmap_sel_get_encoding_name(cs, locale_encoding);
     317                 :           0 :     return name ? name : locale_encoding;
     318                 :             : }
     319                 :             : 
     320                 :           0 : static void encodings_changed_cb(GOOptionMenu *optionmenu, GOCharmapSel *cs)
     321                 :             : {
     322                 :           0 :     g_return_if_fail(GO_IS_CHARMAP_SEL(cs));
     323                 :           0 :     g_return_if_fail(optionmenu == cs->encodings);
     324                 :             : 
     325                 :           0 :     g_signal_emit(G_OBJECT(cs), cs_signals[CHARMAP_CHANGED], 0,
     326                 :             :             go_charmap_sel_get_encoding(cs));
     327                 :             : }
     328                 :             : 
     329                 :           0 : static void set_menu_to_default(GOCharmapSel *cs, gint item)
     330                 :             : {
     331                 :           0 :     GSList sel =
     332                 :           0 :     { GINT_TO_POINTER(item - 1), NULL };
     333                 :             : 
     334                 :           0 :     g_return_if_fail(cs != NULL && GO_IS_CHARMAP_SEL(cs));
     335                 :             : 
     336                 :           0 :     go_option_menu_set_history(cs->encodings, &sel);
     337                 :             : }
     338                 :             : 
     339                 :           0 : static gboolean cs_mnemonic_activate(GtkWidget *w, gboolean group_cycling)
     340                 :             : {
     341                 :           0 :     GOCharmapSel *cs = GO_CHARMAP_SEL(w);
     342                 :           0 :     gtk_widget_grab_focus(GTK_WIDGET(cs->encodings));
     343                 :           0 :     return TRUE;
     344                 :             : }
     345                 :             : 
     346                 :           0 : static void cs_emphasize_label(GtkLabel *label)
     347                 :             : {
     348                 :           0 :     char *text = g_markup_printf_escaped("<b>%s</b>",
     349                 :             :             gtk_label_get_label(label));
     350                 :           0 :     gtk_label_set_use_markup(label, TRUE);
     351                 :           0 :     gtk_label_set_label(label, text);
     352                 :           0 :     g_free(text);
     353                 :           0 : }
     354                 :             : 
     355                 :           0 : static void go_charmap_sel_init(GOCharmapSel *cs)
     356                 :             : {
     357                 :           0 :     gtk_orientable_set_orientation (GTK_ORIENTABLE(cs), GTK_ORIENTATION_HORIZONTAL);
     358                 :             : 
     359                 :           0 :     cs->test = GO_CHARMAP_SEL_TO_UTF8;
     360                 :             : 
     361                 :           0 :     cs->encodings = GO_OPTION_MENU(go_option_menu_new());
     362                 :             : 
     363                 :           0 :     g_signal_connect(G_OBJECT(cs->encodings), "changed",
     364                 :             :             G_CALLBACK(encodings_changed_cb), cs);
     365                 :           0 :     gtk_box_pack_start(GTK_BOX(cs), GTK_WIDGET(cs->encodings), TRUE, TRUE, 0);
     366                 :           0 : }
     367                 :             : 
     368                 :           0 : static void cs_build_menu(GOCharmapSel *cs)
     369                 :             : {
     370                 :             :     GtkWidget *item;
     371                 :             :     GtkMenu *menu;
     372                 :           0 :     LGroupInfo const *lgroup = lgroups;
     373                 :           0 :     gint lg_cnt = 0;
     374                 :             : 
     375                 :           0 :     menu = GTK_MENU(gtk_menu_new());
     376                 :             : 
     377                 :           0 :     while (lgroup->group_name)
     378                 :             :     {
     379                 :             :         CharsetInfo const *charset_trans;
     380                 :           0 :         GtkMenu *submenu = NULL;
     381                 :             : 
     382                 :           0 :         charset_trans = charset_trans_array;
     383                 :             : 
     384                 :           0 :         while (charset_trans->lgroup != LG_LAST)
     385                 :             :         {
     386                 :             :             GtkWidget *subitem;
     387                 :           0 :             if (charset_trans->lgroup == lgroup->lgroup)
     388                 :             :             {
     389                 :           0 :                 const char *name =
     390                 :           0 :                         (cs->test == GO_CHARMAP_SEL_TO_UTF8) ?
     391                 :             :                                 charset_trans->to_utf8_iconv_name :
     392                 :             :                                 charset_trans->from_utf8_iconv_name;
     393                 :           0 :                 if (name)
     394                 :             :                 {
     395                 :           0 :                     if (!submenu)
     396                 :           0 :                         submenu = GTK_MENU(gtk_menu_new());
     397                 :           0 :                     subitem = gtk_check_menu_item_new_with_label(
     398                 :           0 :                             _(charset_trans->charset_title));
     399                 :           0 :                     gtk_check_menu_item_set_draw_as_radio(
     400                 :           0 :                             GTK_CHECK_MENU_ITEM(subitem), TRUE);
     401                 :           0 :                     gtk_widget_show(subitem);
     402                 :           0 :                     gtk_menu_shell_append(GTK_MENU_SHELL(submenu), subitem);
     403                 :           0 :                     if (charset_trans->imp == CI_MAJOR)
     404                 :           0 :                         cs_emphasize_label(
     405                 :           0 :                                 GTK_LABEL(gtk_bin_get_child(GTK_BIN(subitem))));
     406                 :           0 :                     g_object_set_data(G_OBJECT(subitem), CHARMAP_NAME_KEY,
     407                 :             :                             (gpointer) name);
     408                 :             :                 }
     409                 :             :                 else if (0)
     410                 :             :                 {
     411                 :             :                     g_print("Unsupported: %s\n", charset_trans->aliases);
     412                 :             :                 }
     413                 :             :             }
     414                 :           0 :             charset_trans++;
     415                 :             :         }
     416                 :           0 :         if (submenu)
     417                 :             :         {
     418                 :           0 :             GtkWidget *item = gtk_menu_item_new_with_label(
     419                 :           0 :                     _(lgroup->group_name));
     420                 :             : 
     421                 :           0 :             gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), GTK_WIDGET(submenu));
     422                 :           0 :             gtk_widget_show(item);
     423                 :           0 :             gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     424                 :           0 :             lg_cnt++;
     425                 :             :         }
     426                 :           0 :         lgroup++;
     427                 :             :     }
     428                 :           0 :     item = gtk_separator_menu_item_new();
     429                 :           0 :     gtk_widget_show(item);
     430                 :           0 :     gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     431                 :           0 :     lg_cnt++;
     432                 :             : 
     433                 :             :     {
     434                 :           0 :         char *locale_encoding_menu_title = g_strconcat(_("Locale: "),
     435                 :             :                 get_locale_encoding_name(cs),
     436                 :             :                 NULL);
     437                 :           0 :         item = gtk_check_menu_item_new_with_label(locale_encoding_menu_title);
     438                 :           0 :         gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item), TRUE);
     439                 :           0 :         g_free(locale_encoding_menu_title);
     440                 :           0 :         gtk_widget_show(item);
     441                 :           0 :         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     442                 :           0 :         lg_cnt++;
     443                 :           0 :         cs_emphasize_label(GTK_LABEL(gtk_bin_get_child(GTK_BIN(item))));
     444                 :             :     }
     445                 :             : 
     446                 :           0 :     go_option_menu_set_menu(cs->encodings, GTK_WIDGET(menu));
     447                 :           0 :     cs->encodings_menu = menu;
     448                 :           0 :     set_menu_to_default(cs, lg_cnt);
     449                 :           0 : }
     450                 :             : 
     451                 :           0 : static void go_charmap_sel_class_init(GOCharmapSelClass *klass)
     452                 :             : {
     453                 :             :     CharsetInfo *ci;
     454                 :             :     size_t i;
     455                 :             : 
     456                 :           0 :     GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
     457                 :           0 :     GtkWidgetClass *widget_klass = GTK_WIDGET_CLASS(klass);
     458                 :           0 :     widget_klass->mnemonic_activate = cs_mnemonic_activate;
     459                 :             : 
     460                 :           0 :     gobject_class->set_property = cs_set_property;
     461                 :           0 :     gobject_class->get_property = cs_get_property;
     462                 :             : 
     463                 :           0 :     cs_signals[CHARMAP_CHANGED] = g_signal_new("charmap_changed",
     464                 :             :     GO_TYPE_CHARMAP_SEL, G_SIGNAL_RUN_LAST,
     465                 :             :             0,
     466                 :             :             NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
     467                 :             :             G_TYPE_POINTER);
     468                 :             : 
     469                 :           0 :     g_object_class_install_property(gobject_class, PROP_TEST_DIRECTION,
     470                 :             :             g_param_spec_uint("TestDirection", _("Conversion Direction"),
     471                 :             :                     _("This value determines which iconv test to perform."),
     472                 :             :                     (guint) GO_CHARMAP_SEL_TO_UTF8,
     473                 :             :                     (guint) GO_CHARMAP_SEL_FROM_UTF8,
     474                 :             :                     (guint) GO_CHARMAP_SEL_TO_UTF8, G_PARAM_READWRITE));
     475                 :             : 
     476                 :             :     /* ---------------------------------------- */
     477                 :             :     /* Sort the groups by translated name.  */
     478                 :             : 
     479                 :           0 :     for (i = 0; i < G_N_ELEMENTS(lgroups) - 2; i++)
     480                 :             :     {
     481                 :           0 :         const char *cgroup_name = lgroups[i].group_name;
     482                 :           0 :         const char *group_name = _(cgroup_name);
     483                 :           0 :         lgroups[i].collate_key = g_utf8_collate_key(group_name, -1);
     484                 :           0 :         if (!lgroups[i].collate_key)
     485                 :             :         {
     486                 :           0 :             g_warning("Failed to generate collation key for [%s] [%s]",
     487                 :             :                     cgroup_name, group_name);
     488                 :           0 :             lgroups[i].collate_key = g_strdup(group_name);
     489                 :             :         }
     490                 :             :     }
     491                 :           0 :     qsort(lgroups, G_N_ELEMENTS(lgroups) - 2, sizeof(lgroups[0]),
     492                 :             :             lgroups_order);
     493                 :           0 :     for (i = 0; i < G_N_ELEMENTS(lgroups) - 2; i++)
     494                 :             :     {
     495                 :           0 :         g_free(lgroups[i].collate_key);
     496                 :           0 :         lgroups[i].collate_key = NULL;
     497                 :             :     }
     498                 :             : 
     499                 :             :     /* ---------------------------------------- */
     500                 :             :     /* Sort charsets by group/importance/title.  */
     501                 :             : 
     502                 :           0 :     for (i = 0; i < G_N_ELEMENTS(charset_trans_array) - 1; i++)
     503                 :             :     {
     504                 :           0 :         const char *ctitle = charset_trans_array[i].charset_title;
     505                 :           0 :         const char *title = _(ctitle);
     506                 :           0 :         charset_trans_array[i].collate_key = g_utf8_collate_key(title, -1);
     507                 :           0 :         if (!charset_trans_array[i].collate_key)
     508                 :             :         {
     509                 :           0 :             g_warning("Failed to generate collation key for [%s] [%s]", ctitle,
     510                 :             :                     title);
     511                 :           0 :             charset_trans_array[i].collate_key = g_strdup(title);
     512                 :             :         }
     513                 :             :     }
     514                 :           0 :     qsort(charset_trans_array, G_N_ELEMENTS(charset_trans_array) - 1,
     515                 :             :             sizeof(charset_trans_array[0]), charset_order);
     516                 :           0 :     for (i = 0; i < G_N_ELEMENTS(charset_trans_array) - 1; i++)
     517                 :             :     {
     518                 :           0 :         g_free(charset_trans_array[i].collate_key);
     519                 :           0 :         charset_trans_array[i].collate_key = NULL;
     520                 :             :     }
     521                 :             : 
     522                 :             :     /* ---------------------------------------- */
     523                 :             : 
     524                 :           0 :     encoding_hash = g_hash_table_new_full(go_ascii_strcase_hash,
     525                 :             :             go_ascii_strcase_equal, (GDestroyNotify) g_free,
     526                 :             :             NULL);
     527                 :             : 
     528                 :           0 :     for (ci = charset_trans_array; ci->charset_title; ci++)
     529                 :             :     {
     530                 :           0 :         const char *aliases = ci->aliases;
     531                 :           0 :         char *autoaliases = NULL;
     532                 :             : 
     533                 :           0 :         if (strchr(aliases, '#') == NULL)
     534                 :             :         {
     535                 :             :             /* Sigh.  This sucks quite a lot.  */
     536                 :           0 :             if (strncmp(aliases, "ISO-", 4) == 0)
     537                 :             :             {
     538                 :           0 :                 autoaliases = g_strconcat(aliases, "#ISO", aliases + 4, "#ISO_",
     539                 :             :                         aliases + 4,
     540                 :             :                         NULL);
     541                 :             :             }
     542                 :             : 
     543                 :           0 :             if (autoaliases)
     544                 :           0 :                 aliases = autoaliases;
     545                 :             :         }
     546                 :             : 
     547                 :           0 :         ci->to_utf8_iconv_name = ci->from_utf8_iconv_name = NULL;
     548                 :           0 :         while (aliases)
     549                 :             :         {
     550                 :           0 :             const char *sep = strchr(aliases, '#');
     551                 :             :             char *alias;
     552                 :             : 
     553                 :           0 :             if (sep)
     554                 :             :             {
     555                 :           0 :                 alias = g_strndup(aliases, sep - aliases);
     556                 :           0 :                 aliases = sep + 1;
     557                 :             :             }
     558                 :             :             else
     559                 :             :             {
     560                 :           0 :                 alias = g_strdup(aliases);
     561                 :           0 :                 aliases = NULL;
     562                 :             :             }
     563                 :             : 
     564                 :           0 :             if (ci->to_utf8_iconv_name == NULL
     565                 :           0 :                     && iconv_supported("UTF-8", alias))
     566                 :             :             {
     567                 :           0 :                 ci->to_utf8_iconv_name = g_strdup(alias);
     568                 :             :             }
     569                 :             : 
     570                 :           0 :             if (ci->from_utf8_iconv_name == NULL
     571                 :           0 :                     && iconv_supported(alias, "UTF-8"))
     572                 :             :             {
     573                 :           0 :                 ci->from_utf8_iconv_name = g_strdup(alias);
     574                 :             :             }
     575                 :             : 
     576                 :           0 :             g_hash_table_insert(encoding_hash, alias, ci);
     577                 :             :         }
     578                 :             : 
     579                 :           0 :         g_free(autoaliases);
     580                 :             :     }
     581                 :           0 : }
     582                 :             : 
     583                 :             : GtkWidget *
     584                 :           0 : go_charmap_sel_new(GOCharmapSelTestDirection test)
     585                 :             : {
     586                 :           0 :     return g_object_new(GO_TYPE_CHARMAP_SEL, "TestDirection", test, NULL);
     587                 :             : }
     588                 :             : 
     589                 :             : gchar const *
     590                 :           0 : go_charmap_sel_get_encoding(GOCharmapSel *cs)
     591                 :             : {
     592                 :             :     GtkMenuItem *selection;
     593                 :             :     char const *locale_encoding;
     594                 :             :     char const *encoding;
     595                 :             : 
     596                 :           0 :     g_get_charset(&locale_encoding);
     597                 :             : 
     598                 :           0 :     g_return_val_if_fail(GO_IS_CHARMAP_SEL(cs), locale_encoding);
     599                 :             : 
     600                 :           0 :     selection = GTK_MENU_ITEM(go_option_menu_get_history(cs->encodings));
     601                 :           0 :     encoding = (char const *) g_object_get_data(G_OBJECT(selection),
     602                 :             :     CHARMAP_NAME_KEY);
     603                 :           0 :     return encoding ? encoding : locale_encoding;
     604                 :             : }
     605                 :             : 
     606                 :             : struct cb_find_entry
     607                 :             : {
     608                 :             :     const char *enc;
     609                 :             :     gboolean found;
     610                 :             :     int i;
     611                 :             :     GSList *path;
     612                 :             : };
     613                 :             : 
     614                 :           0 : static void cb_find_entry(GtkMenuItem *w, struct cb_find_entry *cl)
     615                 :             : {
     616                 :             :     GtkWidget *sub;
     617                 :             : 
     618                 :           0 :     if (cl->found)
     619                 :           0 :         return;
     620                 :             : 
     621                 :           0 :     sub = gtk_menu_item_get_submenu(w);
     622                 :           0 :     if (sub)
     623                 :             :     {
     624                 :           0 :         GSList *tmp = cl->path = g_slist_prepend(cl->path,
     625                 :           0 :                 GINT_TO_POINTER(cl->i));
     626                 :           0 :         cl->i = 0;
     627                 :             : 
     628                 :           0 :         gtk_container_foreach(GTK_CONTAINER(sub), (GtkCallback) cb_find_entry,
     629                 :             :                 cl);
     630                 :           0 :         if (cl->found)
     631                 :           0 :             return;
     632                 :             : 
     633                 :           0 :         cl->i = GPOINTER_TO_INT(cl->path->data);
     634                 :           0 :         cl->path = cl->path->next;
     635                 :           0 :         g_slist_free_1(tmp);
     636                 :             :     }
     637                 :             :     else
     638                 :             :     {
     639                 :           0 :         const char *this_enc = g_object_get_data(G_OBJECT(w), CHARMAP_NAME_KEY);
     640                 :           0 :         if (this_enc && strcmp(this_enc, cl->enc) == 0)
     641                 :             :         {
     642                 :           0 :             cl->found = TRUE;
     643                 :           0 :             cl->path = g_slist_prepend(cl->path, GINT_TO_POINTER(cl->i));
     644                 :           0 :             cl->path = g_slist_reverse(cl->path);
     645                 :           0 :             return;
     646                 :             :         }
     647                 :             :     }
     648                 :           0 :     cl->i++;
     649                 :             : }
     650                 :             : 
     651                 :           0 : gboolean go_charmap_sel_set_encoding(GOCharmapSel *cs, const char *enc)
     652                 :             : {
     653                 :             :     struct cb_find_entry cl;
     654                 :             :     CharsetInfo const *ci;
     655                 :             : 
     656                 :           0 :     g_return_val_if_fail(GO_IS_CHARMAP_SEL(cs), FALSE);
     657                 :           0 :     g_return_val_if_fail(enc != NULL, FALSE);
     658                 :             : 
     659                 :           0 :     ci = g_hash_table_lookup(encoding_hash, enc);
     660                 :           0 :     if (!ci)
     661                 :           0 :         return FALSE;
     662                 :             : 
     663                 :           0 :     enc = ci->to_utf8_iconv_name;
     664                 :           0 :     if (!enc)
     665                 :           0 :         return FALSE;
     666                 :             : 
     667                 :           0 :     cl.enc = enc;
     668                 :           0 :     cl.found = FALSE;
     669                 :           0 :     cl.i = 0;
     670                 :           0 :     cl.path = NULL;
     671                 :             : 
     672                 :           0 :     gtk_container_foreach(GTK_CONTAINER(cs->encodings_menu),
     673                 :             :             (GtkCallback) cb_find_entry, &cl);
     674                 :           0 :     if (!cl.found)
     675                 :           0 :         return FALSE;
     676                 :             : 
     677                 :           0 :     go_option_menu_set_history(cs->encodings, cl.path);
     678                 :           0 :     g_slist_free(cl.path);
     679                 :             : 
     680                 :           0 :     return TRUE;
     681                 :             : }
     682                 :             : 
     683                 :           0 : static void cs_set_property(GObject *object, guint prop_id, const GValue *value,
     684                 :             :         GParamSpec *pspec)
     685                 :             : {
     686                 :           0 :     GOCharmapSel *cs = GO_CHARMAP_SEL(object);
     687                 :             : 
     688                 :           0 :     switch (prop_id)
     689                 :             :     {
     690                 :           0 :     case PROP_TEST_DIRECTION:
     691                 :           0 :         cs->test = g_value_get_uint(value);
     692                 :           0 :         cs_build_menu(cs);
     693                 :           0 :         break;
     694                 :           0 :     default:
     695                 :           0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     696                 :           0 :         break;
     697                 :             :     }
     698                 :           0 : }
     699                 :             : 
     700                 :           0 : static void cs_get_property(GObject *object, guint prop_id, GValue *value,
     701                 :             :         GParamSpec *pspec)
     702                 :             : {
     703                 :           0 :     GOCharmapSel *cs = GO_CHARMAP_SEL(object);
     704                 :             : 
     705                 :           0 :     switch (prop_id)
     706                 :             :     {
     707                 :           0 :     case PROP_TEST_DIRECTION:
     708                 :           0 :         g_value_set_uint(value, (guint) cs->test);
     709                 :           0 :         break;
     710                 :           0 :     default:
     711                 :           0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     712                 :           0 :         break;
     713                 :             :     }
     714                 :           0 : }
        

Generated by: LCOV version 2.0-1