sync with sylpheed 0.6.3 release
[claws.git] / src / codeconv.c
index f822a3dcb066873e70212b8b4a2d585cb44b4f3d..becdccfae66a362881eaeb9341dcdda12b1a3be4 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 Hiroyuki Yamamoto
  *
  * 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
@@ -351,14 +351,23 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
                }
        }
 
+       /* don't convert if src and dest codeset are identical */
+       if (src_codeset && dest_codeset &&
+           !strcasecmp(src_codeset, dest_codeset))
+               return g_strdup(inbuf);
+
 #if HAVE_LIBJCONV
        if (src_codeset) {
                codesets = &src_codeset;
                n_codesets = 1;
        } else
                codesets = jconv_info_get_pref_codesets(&n_codesets);
-       if (!dest_codeset)
+       if (!dest_codeset) {
                dest_codeset = conv_get_current_charset_str();
+               /* don't convert if current codeset is US-ASCII */
+               if (!strcasecmp(dest_codeset, CS_US_ASCII))
+                       return g_strdup(inbuf);
+       }
 
        if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
                             codesets, n_codesets,
@@ -414,8 +423,12 @@ CodeConvFunc conv_get_code_conv_func(const gchar *charset)
                code_conv = conv_jistodisp;
        else if (!strcasecmp(charset, CS_US_ASCII))
                code_conv = conv_ustodisp;
+       else if (!strncasecmp(charset, CS_ISO_8859_1, 10))
+               code_conv = conv_latintodisp;
+#if !HAVE_LIBJCONV
        else if (!strncasecmp(charset, "ISO-8859-", 9))
                code_conv = conv_latintodisp;
+#endif
        else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
                 !strcasecmp(charset, "SHIFT-JIS")  ||
                 !strcasecmp(charset, "SJIS")       ||
@@ -435,6 +448,7 @@ static const struct {
        gchar *const name;
 } charsets[] = {
        {C_US_ASCII,            CS_US_ASCII},
+       {C_US_ASCII,            CS_ANSI_X3_4_1968},
        {C_UTF_8,               CS_UTF_8},
        {C_ISO_8859_1,          CS_ISO_8859_1},
        {C_ISO_8859_2,          CS_ISO_8859_2},
@@ -517,8 +531,9 @@ static const struct {
        {"lt_LT"                , C_ISO_8859_13},
        {"lv_LV"                , C_ISO_8859_13},
 
-       {"C"            , C_US_ASCII},
-       {"POSIX"        , C_US_ASCII},
+       {"C"                    , C_US_ASCII},
+       {"POSIX"                , C_US_ASCII},
+       {"ANSI_X3.4-1968"       , C_US_ASCII},
 };
 #endif /* !HAVE_LIBJCONV */
 
@@ -615,16 +630,12 @@ CharSet conv_get_current_charset(void)
 
 const gchar *conv_get_current_charset_str(void)
 {
-#if HAVE_LIBJCONV
-       return jconv_info_get_current_codeset();
-#else
        static const gchar *codeset = NULL;
 
        if (!codeset)
                codeset = conv_get_charset_str(conv_get_current_charset());
 
        return codeset ? codeset : "US-ASCII";
-#endif
 }
 
 CharSet conv_get_outgoing_charset(void)
@@ -645,7 +656,7 @@ CharSet conv_get_outgoing_charset(void)
        /* skip US-ASCII and UTF-8 */
        pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
        for (i = 0; i < n_pref_codesets; i++) {
-               for (j = 2; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
+               for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
                        if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
                                out_charset = charsets[j].charset;
                                return out_charset;
@@ -695,6 +706,20 @@ const gchar *conv_get_outgoing_charset_str(void)
        return str ? str : "US-ASCII";
 }
 
+const gchar *conv_get_current_locale(void)
+{
+       gchar *cur_locale;
+
+       cur_locale = g_getenv("LC_ALL");
+       if (!cur_locale) cur_locale = g_getenv("LANG");
+       if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
+
+       debug_print("current locale: %s\n",
+                   cur_locale ? cur_locale : "(none)");
+
+       return cur_locale;
+}
+
 void conv_unmime_header_overwrite(gchar *str)
 {
        gchar *buf;
@@ -704,17 +729,24 @@ void conv_unmime_header_overwrite(gchar *str)
        cur_charset = conv_get_current_charset();
 
 #if HAVE_LIBJCONV
-       Xstrdup_a(buf, str, {return;});
+       Xstrdup_a(buf, str, return);
        outlen = strlen(str) + 1;
        UnMimeHeaderConv(buf, str, outlen);
-       if (cur_charset == C_EUC_JP)
-               conv_unreadable_eucjp(str);
+       if (cur_charset == C_EUC_JP) {
+               gchar *tmp;
+               gint len;
+
+               len = strlen(str) * 2 + 1;
+               Xalloca(tmp, len, return);
+               conv_jistodisp(tmp, len, str);
+               strncpy2(str, tmp, outlen);
+       }
 #else
        if (cur_charset == C_EUC_JP) {
                gchar *tmp;
                gint len;
 
-               Xstrdup_a(buf, str, {return;});
+               Xstrdup_a(buf, str, return);
                outlen = strlen(str) + 1;
                UnMimeHeader(buf);
                len = strlen(buf) * 2 + 1;
@@ -732,26 +764,23 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
        gchar *buf;
        CharSet cur_charset;
 
-       Xstrdup_a(buf, str, {return;});
        cur_charset = conv_get_current_charset();
+       Xstrdup_a(buf, str, return);
 
 #if HAVE_LIBJCONV
        UnMimeHeaderConv(buf, outbuf, outlen);
-       if (cur_charset == C_EUC_JP)
-               conv_unreadable_eucjp(outbuf);
 #else
        UnMimeHeader(buf);
+       strncpy2(outbuf, buf, outlen);
+#endif
        if (cur_charset == C_EUC_JP) {
-               gchar *tmp;
                gint len;
 
-               len = strlen(buf) * 2 + 1;
-               Xalloca(tmp, len, {strncpy2(outbuf, buf, outlen); return;});
-               conv_jistodisp(tmp, len, buf);
-               strncpy2(outbuf, tmp, outlen);
-       } else
+               len = strlen(outbuf) * 2 + 1;
+               Xalloca(buf, len, return);
+               conv_jistodisp(buf, len, outbuf);
                strncpy2(outbuf, buf, outlen);
-#endif
+       }
 }
 
 #define MAX_ENCLEN     75
@@ -772,7 +801,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        gchar *mimehdr_enctype = "?B?";
        const gchar *mimehdr_charset;
 
-       //g_print("src = %s\n", src);
+       /* g_print("src = %s\n", src); */
        mimehdr_charset = conv_get_outgoing_charset_str();
 
        /* convert to wide-character string */
@@ -782,7 +811,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                strlen(mimehdr_charset) + strlen(mimehdr_enctype);
        mimehdr_begin_len = strlen(mimehdr_init) +
                strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       //line_len = 1;
+       /* line_len = 1; */
        line_len = header_len;
        destp = dest;
        *dest = '\0';
@@ -845,8 +874,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                }
                                /* g_free(raw); */
                                src_codeset = conv_get_current_charset_str();
-                               //printf ("tmp = %s, tlen = %d, mbl\n",
-                               //      tmp, tlen, mbl);
+                               /* printf ("tmp = %s, tlen = %d, mbl\n", 
+                                       tmp, tlen, mbl); */
                                if (jconv_alloc_conv(tmp, tlen + mbl,
                                                     &raw_new, &raw_new_len,
                                                     &src_codeset, 1,
@@ -875,23 +904,21 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                                        break;
                                                }
                                        }
-                               } else {
-                                       if ((line_len + tlen + mbl +
-                                            (*(wtmpp + 1) ? 0 : nspc) +
-                                            (line_len > 1 ? 1 : 0))
-                                           > MAX_LINELEN) {
-                                               g_free(raw_new);
-                                               if (1 + tlen + mbl +
-                                                   (*(wtmpp + 1) ? 0 : nspc)
-                                                   >= MAX_LINELEN) {
-                                                       *tmpp = '\0';
-                                                       break;
-                                               }
-                                               *destp++ = '\n';
-                                               *destp++ = ' ';
-                                               line_len = 1;
-                                               continue;
+                               } else if ((line_len + tlen + mbl +
+                                           (*(wtmpp + 1) ? 0 : nspc) +
+                                           (line_len > 1 ? 1 : 0))
+                                          > MAX_LINELEN) {
+                                       g_free(raw_new);
+                                       if (1 + tlen + mbl +
+                                           (*(wtmpp + 1) ? 0 : nspc)
+                                           >= MAX_LINELEN) {
+                                               *tmpp = '\0';
+                                               break;
                                        }
+                                       *destp++ = '\n';
+                                       *destp++ = ' ';
+                                       line_len = 1;
+                                       continue;
                                }
 
                                tmpp += mbl;
@@ -905,8 +932,10 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
 
                                wtmpp++;
                        }
-                       //g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
-                       //      tmp, tlen, mb_seqlen);
+                       /*
+                         g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
+                               tmp, tlen, mb_seqlen);
+                       */                              
 
                        if (tlen == 0 || raw_len == 0) {
                                g_free(tmp);
@@ -942,7 +971,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
 
                        g_free(tmp);
                        g_free(raw);
-                       //g_print("line_len = %d\n\n", line_len);
+                       /* g_print("line_len = %d\n\n", line_len); */
                } while (*wtmpp != (wchar_t)0);
 
                while (iswspace(*wsrcp)) {
@@ -961,7 +990,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
 
        g_free(wsrc);
 
-       //g_print("dest = %s\n", dest);
+       /* g_print("dest = %s\n", dest);  */
 }
 #else /* !HAVE_LIBJCONV */
 
@@ -979,7 +1008,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        gchar *mimehdr_enctype = "?B?";
        const gchar *mimehdr_charset;
 
-       //g_print("src = %s\n", src);
+       /* g_print("src = %s\n", src); */
        mimehdr_charset = conv_get_outgoing_charset_str();
        if (strcmp(mimehdr_charset, "ISO-2022-JP") != 0) {
                /* currently only supports Japanese */
@@ -994,7 +1023,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                      strlen(mimehdr_charset) + strlen(mimehdr_enctype);
        mimehdr_begin_len = strlen(mimehdr_init) +
                            strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       //line_len = 1;
+       /* line_len = 1; */
        line_len = header_len;
        destp = dest;
        *dest = '\0';
@@ -1069,22 +1098,20 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                                        break;
                                                }
                                        }
-                               } else {
-                                       if ((line_len + tlen + mbl +
-                                            (*(wtmpp + 1) ? 0 : nspc) +
-                                            (line_len > 1 ? 1 : 0))
-                                           > MAX_LINELEN) {
-                                               if (1 + tlen + mbl +
-                                                   (*(wtmpp + 1) ? 0 : nspc)
-                                                   >= MAX_LINELEN) {
-                                                       *tmpp = '\0';
-                                                       break;
-                                               }
-                                               *destp++ = '\n';
-                                               *destp++ = ' ';
-                                               line_len = 1;
-                                               continue;
+                               } else if ((line_len + tlen + mbl +
+                                           (*(wtmpp + 1) ? 0 : nspc) +
+                                           (line_len > 1 ? 1 : 0))
+                                          > MAX_LINELEN) {
+                                       if (1 + tlen + mbl +
+                                           (*(wtmpp + 1) ? 0 : nspc)
+                                           >= MAX_LINELEN) {
+                                               *tmpp = '\0';
+                                               break;
                                        }
+                                       *destp++ = '\n';
+                                       *destp++ = ' ';
+                                       line_len = 1;
+                                       continue;
                                }
 
                                tmpp += mbl;
@@ -1095,8 +1122,9 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
 
                                wtmpp++;
                        }
-                       //g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
-                       //      tmp, tlen, mb_seqlen);
+                       /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
+                               tmp, tlen, mb_seqlen);
+                       */                              
 
                        if (tlen == 0) {
                                g_free(tmp);
@@ -1137,7 +1165,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        }
 
                        g_free(tmp);
-                       //g_print("line_len = %d\n\n", line_len);
+                       /* g_print("line_len = %d\n\n", line_len); */
                } while (*wtmpp != (wchar_t)0);
 
                while (iswspace(*wsrcp)) {
@@ -1156,6 +1184,6 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
 
        g_free(wsrc);
 
-       //g_print("dest = %s\n", dest);
+       /* g_print("dest = %s\n", dest); */
 }
 #endif /* HAVE_LIBJCONV */