2005-04-20 [paul] 1.9.6cvs43
[claws.git] / src / codeconv.c
index 89a2e5aa36491390cc2466aaeca5a3d221cc41c4..71f8804b8c70fd077d2bc5f7dfaeacc50f12763b 100644 (file)
@@ -21,6 +21,8 @@
 #  include "config.h"
 #endif
 
+#include "defs.h"
+
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <string.h>
@@ -823,10 +825,24 @@ static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
        strncpy2(outbuf, inbuf, outlen);
 }
 
+static const gchar *
+conv_get_fallback_for_private_encoding(const gchar *encoding)
+{
+       if (encoding && (encoding[0] == 'X' || encoding[0] == 'x') &&
+           encoding[1] == '-') {
+               if (!g_ascii_strcasecmp(encoding, CS_X_GBK))
+                       return CS_GBK;
+       }
+
+       return encoding;
+}
+
 CodeConverter *conv_code_converter_new(const gchar *src_charset)
 {
        CodeConverter *conv;
 
+       src_charset = conv_get_fallback_for_private_encoding(src_charset);
+
        conv = g_new0(CodeConverter, 1);
        conv->code_conv_func = conv_get_code_conv_func(src_charset, NULL);
        conv->charset_str = g_strdup(src_charset);
@@ -868,6 +884,7 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
+       src_code = conv_get_fallback_for_private_encoding(src_code);
        conv_func = conv_get_code_conv_func(src_code, dest_code);
        if (conv_func != conv_noconv) {
                len = (strlen(inbuf) + 1) * 3;
@@ -1150,7 +1167,7 @@ static const struct {
        {"ko_KR.EUC-KR" , C_EUC_KR      , C_EUC_KR},
        {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
        {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
-       {"zh_CN.GBK"    , C_GBK         , C_GB2312},
+       {"zh_CN.GBK"    , C_GBK         , C_GBK},
        {"zh_CN"        , C_GB2312      , C_GB2312},
        {"zh_HK"        , C_BIG5_HKSCS  , C_BIG5_HKSCS},
        {"zh_TW.eucTW"  , C_EUC_TW      , C_BIG5},
@@ -1517,6 +1534,7 @@ gboolean conv_is_multibyte_encoding(CharSet encoding)
        case C_ISO_2022_CN:
        case C_SHIFT_JIS:
        case C_GB2312:
+       case C_GBK:
        case C_BIG5:
        case C_UTF_8:
        case C_UTF_7:
@@ -1541,37 +1559,33 @@ const gchar *conv_get_current_locale(void)
        return cur_locale;
 }
 
-void conv_unmime_header_overwrite(gchar *str)
+gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
 {
-       gchar *buf;
-       gint buflen;
+       gchar buf[BUFFSIZE];
 
-       buflen = strlen(str) * 2 + 1;
-       Xalloca(buf, buflen, return);
+       if (is_ascii_str(str))
+               return unmime_header(str);
 
-       if (conv_get_locale_charset() == C_EUC_JP)
-               conv_anytodisp(buf, buflen, str);
-       else
-               conv_localetodisp(buf, buflen, str);
-
-       unmime_header(str, buf);
-}
+       if (default_encoding) {
+               gchar *utf8_buf;
 
-void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
-                       const gchar *charset)
-{
-       gchar *buf;
-       gint buflen;
+               utf8_buf = conv_codeset_strdup
+                       (str, default_encoding, CS_INTERNAL);
+               if (utf8_buf) {
+                       gchar *decoded_str;
 
-       buflen = strlen(str) * 2 + 1;
-       Xalloca(buf, buflen, return);
+                       decoded_str = unmime_header(utf8_buf);
+                       g_free(utf8_buf);
+                       return decoded_str;
+               }
+       }
 
        if (conv_get_locale_charset() == C_EUC_JP)
-               conv_anytodisp(buf, buflen, str);
+               conv_anytodisp(buf, sizeof(buf), str);
        else
-               conv_localetodisp(buf, buflen, str);
+               conv_localetodisp(buf, sizeof(buf), str);
 
-       unmime_header(outbuf, buf);
+       return unmime_header(buf);
 }
 
 #define MAX_LINELEN            76