2005-10-22 [colin] 1.9.15cvs92
[claws.git] / src / codeconv.c
index 3e7676b11b0f82f822078b61b471729c888348ec..e3a9a834aa616c55e7d93a98ba092bb3adee1372 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -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,14 +677,25 @@ 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);
-               conv_utf8todisp(outbuf, outlen, inbuf);
+               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);
        } else
                conv_utf8todisp(outbuf, outlen, inbuf);
 }
@@ -907,6 +925,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--;
@@ -1305,6 +1327,60 @@ CharSet conv_get_locale_charset(void)
        return cur_charset;
 }
 
+static CharSet conv_get_locale_charset_no_utf8(void)
+{
+       static CharSet cur_charset = -1;
+       const gchar *cur_locale;
+       const gchar *p;
+       gchar *tmp;
+       gint i;
+
+       if (prefs_common.broken_are_utf8)
+               return conv_get_locale_charset();
+
+       if (cur_charset != -1)
+               return cur_charset;
+
+       cur_locale = conv_get_current_locale();
+       if (!cur_locale) {
+               cur_charset = C_US_ASCII;
+               return cur_charset;
+       }
+
+       if (strcasestr(cur_locale, "UTF-8")) {
+               tmp = g_strdup(cur_locale);
+               *(strcasestr(tmp, ".UTF-8")) = '\0';
+               cur_locale = tmp;
+       }
+
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               cur_charset = C_ISO_8859_15;
+               return cur_charset;
+       }
+
+       for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
+               const gchar *p;
+
+               /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
+                  "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
+               if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
+                                strlen(locale_table[i].locale))) {
+                       cur_charset = locale_table[i].charset;
+                       return cur_charset;
+               } else if ((p = strchr(locale_table[i].locale, '_')) &&
+                        !strchr(p + 1, '.')) {
+                       if (strlen(cur_locale) == 2 &&
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                               cur_charset = locale_table[i].charset;
+                               return cur_charset;
+                       }
+               }
+       }
+
+       cur_charset = C_AUTO;
+       return cur_charset;
+}
+
 const gchar *conv_get_locale_charset_str(void)
 {
        static const gchar *codeset = NULL;
@@ -1315,6 +1391,16 @@ const gchar *conv_get_locale_charset_str(void)
        return codeset ? codeset : CS_INTERNAL;
 }
 
+const gchar *conv_get_locale_charset_str_no_utf8(void)
+{
+       static const gchar *codeset = NULL;
+
+       if (!codeset)
+               codeset = conv_get_charset_str(conv_get_locale_charset_no_utf8());
+
+       return codeset ? codeset : CS_INTERNAL;
+}
+
 CharSet conv_get_internal_charset(void)
 {
        return C_INTERNAL;
@@ -1596,9 +1682,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);