2005-10-22 [colin] 1.9.15cvs93
[claws.git] / src / codeconv.c
index 2c4f0177b03b5993542525099ae42e624be93806..b9920685ebb5ccfc122abd2b401d9e104fb83b91 100644 (file)
@@ -126,6 +126,13 @@ static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
 
+static gboolean strict_mode = FALSE;
+
+void codeconv_set_strict(gboolean mode)
+{
+       strict_mode = mode;
+}
+
 static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
@@ -670,16 +677,31 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *tmpstr;
 
+       codeconv_set_strict(TRUE);
        tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
                                   CS_INTERNAL);
+       codeconv_set_strict(FALSE);
        if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
+               return;
        } else if (tmpstr && !g_utf8_validate(tmpstr, -1, NULL)) {
                g_free(tmpstr);
+               codeconv_set_strict(TRUE);
+               tmpstr = conv_iconv_strdup(inbuf, 
+                               conv_get_locale_charset_str_no_utf8(),
+                               CS_INTERNAL);
+               codeconv_set_strict(FALSE);
+       }
+       if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+               return;
+       } else {
+               if (tmpstr)
+                       g_free(tmpstr);
                conv_utf8todisp(outbuf, outlen, inbuf);
-       } else
-               conv_utf8todisp(outbuf, outlen, inbuf);
+       }
 }
 
 static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
@@ -907,6 +929,10 @@ gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
        while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
                               &outbuf_p, &out_left)) == (size_t)-1) {
                if (EILSEQ == errno) {
+                       if (strict_mode) {
+                               g_free(outbuf);
+                               return NULL;
+                       }
                        //g_print("iconv(): at %d: %s\n", in_size - in_left, g_strerror(errno));
                        inbuf_p++;
                        in_left--;
@@ -1313,6 +1339,9 @@ static CharSet conv_get_locale_charset_no_utf8(void)
        gchar *tmp;
        gint i;
 
+       if (prefs_common.broken_are_utf8)
+               return conv_get_locale_charset();
+
        if (cur_charset != -1)
                return cur_charset;
 
@@ -1657,9 +1686,14 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                out_str = conv_codeset_strdup
                                        (part_str, cur_encoding, out_encoding);
                                if (!out_str) {
-                                       g_warning("conv_encode_header(): code conversion failed\n");
-                                       conv_unreadable_8bit(part_str);
-                                       out_str = g_strdup(part_str);
+                                       if (strict_mode) {
+                                               *dest = '\0';
+                                               return;
+                                       } else {
+                                               g_warning("conv_encode_header(): code conversion failed\n");
+                                               conv_unreadable_8bit(part_str);
+                                               out_str = g_strdup(part_str);
+                                       }
                                }
                                out_str_len = strlen(out_str);