2004-10-14 [colin] 0.9.12cvs126.2
[claws.git] / src / codeconv.c
index 966da77328ae9147f74fb4bde70f343e15619915..12c8e93953fcd47b59ca15e4936e9b84030903b8 100644 (file)
@@ -47,7 +47,7 @@ typedef enum
        JIS_AUXKANJI
 } JISState;
 
-#define SUBST_CHAR     '_'
+#define SUBST_CHAR     0x5f;
 #define ESC            '\033'
 
 #define iseuckanji(c) \
@@ -574,6 +574,22 @@ void conv_unreadable_latin(gchar *str)
        }
 }
 
+void conv_unreadable_utf8(gchar *str)
+{
+       register guchar *p = str;
+
+       while (*p != '\0') {
+               /* convert CR+LF -> LF */
+               printf("p %c (%d)\n", *p, *p);
+               if (*p == '\r' && *(p + 1) == '\n')
+                       memmove(p, p + 1, strlen(p));
+               else if (((*p & 0xff) >= 0x7f && (*p & 0xff) <= 0x9f) 
+                        || *p == 0xfc)
+                       *p = SUBST_CHAR;
+               p++;
+       }
+}
+
 void conv_unreadable_locale(gchar *str)
 {
        switch (conv_get_current_charset()) {
@@ -597,6 +613,9 @@ void conv_unreadable_locale(gchar *str)
        case C_EUC_JP:
                conv_unreadable_eucjp(str);
                break;
+       case C_UTF_8:
+               conv_unreadable_utf8(str);
+               break;
        default:
                break;
        }
@@ -909,13 +928,13 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
         * whether iconv is sitting below, or something else */
        gchar *outbuf;
        gsize read_len, written_len;
-       gchar *src_code = conv_get_outgoing_charset_str();
-       gchar *dest_code = conv_get_current_charset_str();
+       gchar *src_code = (char *)conv_get_outgoing_charset_str();
+       gchar *dest_code = (char *)conv_get_current_charset_str();
        
        if (isrc_code)
-               src_code = isrc_code;
+               src_code = (char *)isrc_code;
        if (idest_code)
-               dest_code = idest_code;
+               dest_code = (char *)idest_code;
 
        /* don't convert if current codeset is US-ASCII */
        if (!strcasecmp(dest_code, CS_US_ASCII))
@@ -929,9 +948,6 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
        outbuf = g_convert(inbuf, strlen(inbuf), dest_code, src_code,
                           &read_len, &written_len, NULL);
 
-       if (outbuf == NULL && strcasecmp(src_code, CS_ISO_8859_15)) 
-               /* also try iso-8859-15 */
-               outbuf = conv_iconv_strdup(inbuf, CS_ISO_8859_15, dest_code);
        if (outbuf == NULL)
                g_warning(_("Valid locale type set? (Currently: %s to %s)\n"),
                          src_code, dest_code);
@@ -1388,8 +1404,11 @@ gboolean conv_is_multibyte_encoding(CharSet encoding)
 
 const gchar *conv_get_current_locale(void)
 {
-       const gchar *cur_locale;
+       static const gchar *cur_locale = NULL;
 
+       if (cur_locale != NULL)
+               return cur_locale;
+       
        cur_locale = g_getenv("LC_ALL");
        if (!cur_locale || !strlen(cur_locale)) 
                cur_locale = g_getenv("LC_CTYPE");
@@ -1398,8 +1417,11 @@ const gchar *conv_get_current_locale(void)
        if (!cur_locale || !strlen(cur_locale)) 
                cur_locale = setlocale(LC_CTYPE, NULL);
 
-/*     debug_print("current locale: %s\n",
-                   cur_locale ? cur_locale : "(none)"); */
+       if (cur_locale && strlen(cur_locale)) {
+               gchar *tmp = g_strdup(cur_locale);
+               cur_locale = g_strdup(tmp);
+               g_free(tmp);
+       }
 
        return cur_locale;
 }
@@ -1435,11 +1457,10 @@ void conv_unmime_header_overwrite(gchar *str)
 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                        const gchar *charset)
 {
-       CharSet cur_charset;
        const gchar *locale;
 
-       cur_charset = conv_get_current_charset();
-
+       memset(outbuf, 0, outlen);
+       
 #warning FIXME_GTK2
 /* Should we always ensure to convert? */
        locale = conv_get_current_locale();
@@ -1452,16 +1473,18 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                Xalloca(buf, buflen, return);
                conv_anytodisp(buf, buflen, str);
                unmime_header(outbuf, buf);
-       } else if (g_utf8_validate(str, -1, NULL)) {
-               unmime_header(outbuf, str);
        } else {
-               gchar *buf;
-               gint buflen;
-               const gchar *src_codeset, *dest_codeset;
-               src_codeset = conv_get_current_charset_str();
-               dest_codeset = CS_UTF_8;
-               buf = conv_codeset_strdup(str, src_codeset, dest_codeset);
-               unmime_header(outbuf, buf);
+               gchar *tmp;
+               unmime_header(outbuf, str);
+               if (outbuf && !g_utf8_validate(outbuf, -1, NULL)) {
+                       tmp = conv_codeset_strdup(outbuf,
+                                       conv_get_current_charset_str(),
+                                       CS_UTF_8);
+                       if (tmp) {
+                               strncpy(outbuf, tmp, outlen-1);
+                               g_free(tmp);
+                       }
+               }
        }
 }
 
@@ -1505,7 +1528,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        const guchar *srcp = src;
        guchar *destp = dest;
        gboolean use_base64;
-
+       gchar *testbuf;
+       
        if (MB_CUR_MAX > 1) {
                use_base64 = TRUE;
                mimesep_enc = "?B?";
@@ -1520,6 +1544,13 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        if (!strcmp(out_encoding, CS_US_ASCII))
                out_encoding = CS_ISO_8859_1;
 
+       testbuf = conv_codeset_strdup(src, cur_encoding, out_encoding);
+       
+       if (testbuf != NULL) 
+               g_free(testbuf);
+       else
+               out_encoding = CS_UTF_8;
+       
        mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
                strlen(mimesep_enc) + strlen(MIMESEP_END);